blob: aea4924d310b73b1453d1ca311c007c6825feda0 [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 Peskine5055b232019-12-12 17:49:31 +0100378mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve,
379 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200380{
381 switch( curve )
382 {
Gilles Peskine228abc52019-12-03 17:24:19 +0100383 case PSA_ECC_CURVE_SECP_R1:
384 switch( byte_length )
385 {
386 case PSA_BITS_TO_BYTES( 192 ):
387 return( MBEDTLS_ECP_DP_SECP192R1 );
388 case PSA_BITS_TO_BYTES( 224 ):
389 return( MBEDTLS_ECP_DP_SECP224R1 );
390 case PSA_BITS_TO_BYTES( 256 ):
391 return( MBEDTLS_ECP_DP_SECP256R1 );
392 case PSA_BITS_TO_BYTES( 384 ):
393 return( MBEDTLS_ECP_DP_SECP384R1 );
394 case PSA_BITS_TO_BYTES( 521 ):
395 return( MBEDTLS_ECP_DP_SECP521R1 );
396 default:
397 return( MBEDTLS_ECP_DP_NONE );
398 }
399 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100400
401 case PSA_ECC_CURVE_BRAINPOOL_P_R1:
402 switch( byte_length )
403 {
404 case PSA_BITS_TO_BYTES( 256 ):
405 return( MBEDTLS_ECP_DP_BP256R1 );
406 case PSA_BITS_TO_BYTES( 384 ):
407 return( MBEDTLS_ECP_DP_BP384R1 );
408 case PSA_BITS_TO_BYTES( 512 ):
409 return( MBEDTLS_ECP_DP_BP512R1 );
410 default:
411 return( MBEDTLS_ECP_DP_NONE );
412 }
413 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100414
415 case PSA_ECC_CURVE_MONTGOMERY:
416 switch( byte_length )
417 {
418 case PSA_BITS_TO_BYTES( 255 ):
419 return( MBEDTLS_ECP_DP_CURVE25519 );
420 case PSA_BITS_TO_BYTES( 448 ):
421 return( MBEDTLS_ECP_DP_CURVE448 );
422 default:
423 return( MBEDTLS_ECP_DP_NONE );
424 }
425 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100426
427 case PSA_ECC_CURVE_SECP_K1:
428 switch( byte_length )
429 {
430 case PSA_BITS_TO_BYTES( 192 ):
431 return( MBEDTLS_ECP_DP_SECP192K1 );
432 case PSA_BITS_TO_BYTES( 224 ):
433 return( MBEDTLS_ECP_DP_SECP224K1 );
434 case PSA_BITS_TO_BYTES( 256 ):
435 return( MBEDTLS_ECP_DP_SECP256K1 );
436 default:
437 return( MBEDTLS_ECP_DP_NONE );
438 }
439 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100440
Gilles Peskine12313cd2018-06-20 00:20:32 +0200441 default:
442 return( MBEDTLS_ECP_DP_NONE );
443 }
444}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100445#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200446
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200447static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
448 size_t bits,
449 struct raw_data *raw )
450{
451 /* Check that the bit size is acceptable for the key type */
452 switch( type )
453 {
454 case PSA_KEY_TYPE_RAW_DATA:
455#if defined(MBEDTLS_MD_C)
456 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200457#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200458 case PSA_KEY_TYPE_DERIVE:
459 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460#if defined(MBEDTLS_AES_C)
461 case PSA_KEY_TYPE_AES:
462 if( bits != 128 && bits != 192 && bits != 256 )
463 return( PSA_ERROR_INVALID_ARGUMENT );
464 break;
465#endif
466#if defined(MBEDTLS_CAMELLIA_C)
467 case PSA_KEY_TYPE_CAMELLIA:
468 if( bits != 128 && bits != 192 && bits != 256 )
469 return( PSA_ERROR_INVALID_ARGUMENT );
470 break;
471#endif
472#if defined(MBEDTLS_DES_C)
473 case PSA_KEY_TYPE_DES:
474 if( bits != 64 && bits != 128 && bits != 192 )
475 return( PSA_ERROR_INVALID_ARGUMENT );
476 break;
477#endif
478#if defined(MBEDTLS_ARC4_C)
479 case PSA_KEY_TYPE_ARC4:
480 if( bits < 8 || bits > 2048 )
481 return( PSA_ERROR_INVALID_ARGUMENT );
482 break;
483#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200484#if defined(MBEDTLS_CHACHA20_C)
485 case PSA_KEY_TYPE_CHACHA20:
486 if( bits != 256 )
487 return( PSA_ERROR_INVALID_ARGUMENT );
488 break;
489#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490 default:
491 return( PSA_ERROR_NOT_SUPPORTED );
492 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200493 if( bits % 8 != 0 )
494 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495
496 /* Allocate memory for the key */
497 raw->bytes = PSA_BITS_TO_BYTES( bits );
498 raw->data = mbedtls_calloc( 1, raw->bytes );
499 if( raw->data == NULL )
500 {
501 raw->bytes = 0;
502 return( PSA_ERROR_INSUFFICIENT_MEMORY );
503 }
504 return( PSA_SUCCESS );
505}
506
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200507#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100508/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
509 * that are not a multiple of 8) well. For example, there is only
510 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
511 * way to return the exact bit size of a key.
512 * To keep things simple, reject non-byte-aligned key sizes. */
513static psa_status_t psa_check_rsa_key_byte_aligned(
514 const mbedtls_rsa_context *rsa )
515{
516 mbedtls_mpi n;
517 psa_status_t status;
518 mbedtls_mpi_init( &n );
519 status = mbedtls_to_psa_error(
520 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
521 if( status == PSA_SUCCESS )
522 {
523 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
524 status = PSA_ERROR_NOT_SUPPORTED;
525 }
526 mbedtls_mpi_free( &n );
527 return( status );
528}
529
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000530static psa_status_t psa_import_rsa_key( psa_key_type_t type,
531 const uint8_t *data,
532 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200533 mbedtls_rsa_context **p_rsa )
534{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000535 psa_status_t status;
536 mbedtls_pk_context pk;
537 mbedtls_rsa_context *rsa;
538 size_t bits;
539
540 mbedtls_pk_init( &pk );
541
542 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200543 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000544 status = mbedtls_to_psa_error(
545 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200546 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000547 status = mbedtls_to_psa_error(
548 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
549 if( status != PSA_SUCCESS )
550 goto exit;
551
552 /* We have something that the pkparse module recognizes. If it is a
553 * valid RSA key, store it. */
554 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200555 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000556 status = PSA_ERROR_INVALID_ARGUMENT;
557 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200558 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000559
560 rsa = mbedtls_pk_rsa( pk );
561 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
562 * supports non-byte-aligned key sizes, but not well. For example,
563 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
564 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
565 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
566 {
567 status = PSA_ERROR_NOT_SUPPORTED;
568 goto exit;
569 }
570 status = psa_check_rsa_key_byte_aligned( rsa );
571
572exit:
573 /* Free the content of the pk object only on error. */
574 if( status != PSA_SUCCESS )
575 {
576 mbedtls_pk_free( &pk );
577 return( status );
578 }
579
580 /* On success, store the content of the object in the RSA context. */
581 *p_rsa = rsa;
582
583 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200584}
585#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
586
Jaeden Ameroccdce902019-01-10 11:42:27 +0000587#if defined(MBEDTLS_ECP_C)
Gilles Peskine4cd32772019-12-02 20:49:42 +0100588static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve,
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100589 size_t data_length,
590 int is_public,
Gilles Peskine4cd32772019-12-02 20:49:42 +0100591 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
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100599 if( is_public )
600 {
601 /* A public key is represented as:
602 * - The byte 0x04;
603 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
604 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
605 * So its data length is 2m+1 where n is the key size in bits.
606 */
607 if( ( data_length & 1 ) == 0 )
608 return( PSA_ERROR_INVALID_ARGUMENT );
609 data_length = data_length / 2;
610 }
611
Gilles Peskine4cd32772019-12-02 20:49:42 +0100612 /* Load the group. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100613 grp_id = mbedtls_ecc_group_of_psa( curve, data_length );
614 if( grp_id == MBEDTLS_ECP_DP_NONE )
615 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100616 return( mbedtls_to_psa_error(
617 mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) );
618}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000619
620/* Import a public key given as the uncompressed representation defined by SEC1
621 * 2.3.3 as the content of an ECPoint. */
622static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
623 const uint8_t *data,
624 size_t data_length,
625 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200626{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200627 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000628 mbedtls_ecp_keypair *ecp = NULL;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000629
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100630 status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp );
Jaeden Ameroccdce902019-01-10 11:42:27 +0000631 if( status != PSA_SUCCESS )
632 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100633
Jaeden Ameroccdce902019-01-10 11:42:27 +0000634 /* Load the public value. */
635 status = mbedtls_to_psa_error(
636 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
637 data, data_length ) );
638 if( status != PSA_SUCCESS )
639 goto exit;
640
641 /* Check that the point is on the curve. */
642 status = mbedtls_to_psa_error(
643 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
644 if( status != PSA_SUCCESS )
645 goto exit;
646
647 *p_ecp = ecp;
648 return( PSA_SUCCESS );
649
650exit:
651 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200652 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000653 mbedtls_ecp_keypair_free( ecp );
654 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200655 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000656 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200657}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200658
Gilles Peskinef76aa772018-10-29 19:24:33 +0100659/* Import a private key given as a byte string which is the private value
660 * in big-endian order. */
661static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
662 const uint8_t *data,
663 size_t data_length,
664 mbedtls_ecp_keypair **p_ecp )
665{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200666 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100667 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100668
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100669 status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100670 if( status != PSA_SUCCESS )
671 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100672
Gilles Peskinef76aa772018-10-29 19:24:33 +0100673 /* Load the secret value. */
674 status = mbedtls_to_psa_error(
675 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
676 if( status != PSA_SUCCESS )
677 goto exit;
678 /* Validate the private key. */
679 status = mbedtls_to_psa_error(
680 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
681 if( status != PSA_SUCCESS )
682 goto exit;
683 /* Calculate the public key from the private key. */
684 status = mbedtls_to_psa_error(
685 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
686 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
687 if( status != PSA_SUCCESS )
688 goto exit;
689
690 *p_ecp = ecp;
691 return( PSA_SUCCESS );
692
693exit:
694 if( ecp != NULL )
695 {
696 mbedtls_ecp_keypair_free( ecp );
697 mbedtls_free( ecp );
698 }
699 return( status );
700}
701#endif /* defined(MBEDTLS_ECP_C) */
702
Gilles Peskineb46bef22019-07-30 21:32:04 +0200703
704/** Return the size of the key in the given slot, in bits.
705 *
706 * \param[in] slot A key slot.
707 *
708 * \return The key size in bits, read from the metadata in the slot.
709 */
710static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
711{
712 return( slot->attr.bits );
713}
714
715/** Calculate the size of the key in the given slot, in bits.
716 *
717 * \param[in] slot A key slot containing a transparent key.
718 *
719 * \return The key size in bits, calculated from the key data.
720 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200721static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200722{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200723 size_t bits = 0; /* return 0 on an empty slot */
724
Gilles Peskineb46bef22019-07-30 21:32:04 +0200725 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200726 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200727#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200728 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
729 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200730#endif /* defined(MBEDTLS_RSA_C) */
731#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200732 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
733 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200734#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200735
736 /* We know that the size fits in psa_key_bits_t thanks to checks
737 * when the key was created. */
738 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200739}
740
Gilles Peskine8e338702019-07-30 20:06:31 +0200741/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100742 * previously. This function assumes that the slot does not contain
743 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100744psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
745 const uint8_t *data,
746 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100747{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200748 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100749
Gilles Peskine8e338702019-07-30 20:06:31 +0200750 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100751 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200752 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200753 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100754 if( data_length > SIZE_MAX / 8 )
755 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200756 /* Enforce a size limit, and in particular ensure that the bit
757 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200758 if( bit_size > PSA_MAX_KEY_BITS )
759 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200760 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200761 &slot->data.raw );
762 if( status != PSA_SUCCESS )
763 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200764 if( data_length != 0 )
765 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100766 }
767 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100768#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200769 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100770 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200771 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100772 data, data_length,
773 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100774 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200775 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100776 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000777 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200778 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000779 data, data_length,
780 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100781 }
782 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100783#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000784#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200785 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100786 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200787 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000788 data, data_length,
789 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100790 }
791 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000792#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100793 {
794 return( PSA_ERROR_NOT_SUPPORTED );
795 }
Darryl Green940d72c2018-07-13 13:18:51 +0100796
Gilles Peskineb46bef22019-07-30 21:32:04 +0200797 if( status == PSA_SUCCESS )
798 {
799 /* Write the actual key size to the slot.
800 * psa_start_key_creation() wrote the size declared by the
801 * caller, which may be 0 (meaning unspecified) or wrong. */
802 slot->attr.bits = psa_calculate_key_bits( slot );
803 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100804 return( status );
805}
806
Gilles Peskinef603c712019-01-19 13:40:11 +0100807/** Calculate the intersection of two algorithm usage policies.
808 *
809 * Return 0 (which allows no operation) on incompatibility.
810 */
811static psa_algorithm_t psa_key_policy_algorithm_intersection(
812 psa_algorithm_t alg1,
813 psa_algorithm_t alg2 )
814{
Gilles Peskine549ea862019-05-22 11:45:59 +0200815 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100816 if( alg1 == alg2 )
817 return( alg1 );
818 /* If the policies are from the same hash-and-sign family, check
819 * if one is a wildcard. If so the other has the specific algorithm. */
820 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
821 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
822 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
823 {
824 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
825 return( alg2 );
826 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
827 return( alg1 );
828 }
829 /* If the policies are incompatible, allow nothing. */
830 return( 0 );
831}
832
Gilles Peskined6f371b2019-05-10 19:33:38 +0200833static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
834 psa_algorithm_t requested_alg )
835{
Gilles Peskine549ea862019-05-22 11:45:59 +0200836 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200837 if( requested_alg == policy_alg )
838 return( 1 );
839 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200840 * and requested_alg is the same hash-and-sign family with any hash,
841 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200842 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
843 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
844 {
845 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
846 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
847 }
848 /* If it isn't permitted, it's forbidden. */
849 return( 0 );
850}
851
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100852/** Test whether a policy permits an algorithm.
853 *
854 * The caller must test usage flags separately.
855 */
856static int psa_key_policy_permits( const psa_key_policy_t *policy,
857 psa_algorithm_t alg )
858{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200859 return( psa_key_algorithm_permits( policy->alg, alg ) ||
860 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100861}
862
Gilles Peskinef603c712019-01-19 13:40:11 +0100863/** Restrict a key policy based on a constraint.
864 *
865 * \param[in,out] policy The policy to restrict.
866 * \param[in] constraint The policy constraint to apply.
867 *
868 * \retval #PSA_SUCCESS
869 * \c *policy contains the intersection of the original value of
870 * \c *policy and \c *constraint.
871 * \retval #PSA_ERROR_INVALID_ARGUMENT
872 * \c *policy and \c *constraint are incompatible.
873 * \c *policy is unchanged.
874 */
875static psa_status_t psa_restrict_key_policy(
876 psa_key_policy_t *policy,
877 const psa_key_policy_t *constraint )
878{
879 psa_algorithm_t intersection_alg =
880 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200881 psa_algorithm_t intersection_alg2 =
882 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100883 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
884 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200885 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
886 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100887 policy->usage &= constraint->usage;
888 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200889 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100890 return( PSA_SUCCESS );
891}
892
Darryl Green06fd18d2018-07-16 11:21:11 +0100893/** Retrieve a slot which must contain a key. The key must have allow all the
894 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
895 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100896static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100897 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100898 psa_key_usage_t usage,
899 psa_algorithm_t alg )
900{
901 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100902 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100903
904 *p_slot = NULL;
905
Gilles Peskinec5487a82018-12-03 18:08:14 +0100906 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100907 if( status != PSA_SUCCESS )
908 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100909
910 /* Enforce that usage policy for the key slot contains all the flags
911 * required by the usage parameter. There is one exception: public
912 * keys can always be exported, so we treat public key objects as
913 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200914 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100915 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200916 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100917 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100918
919 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200920 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100921 return( PSA_ERROR_NOT_PERMITTED );
922
923 *p_slot = slot;
924 return( PSA_SUCCESS );
925}
Darryl Green940d72c2018-07-13 13:18:51 +0100926
Gilles Peskine28f8f302019-07-24 13:30:31 +0200927/** Retrieve a slot which must contain a transparent key.
928 *
929 * A transparent key is a key for which the key material is directly
930 * available, as opposed to a key in a secure element.
931 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200932 * This is a temporary function to use instead of psa_get_key_from_slot()
933 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200934 */
935#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
936static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
937 psa_key_slot_t **p_slot,
938 psa_key_usage_t usage,
939 psa_algorithm_t alg )
940{
941 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
942 if( status != PSA_SUCCESS )
943 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200944 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200945 {
946 *p_slot = NULL;
947 return( PSA_ERROR_NOT_SUPPORTED );
948 }
949 return( PSA_SUCCESS );
950}
951#else /* MBEDTLS_PSA_CRYPTO_SE_C */
952/* With no secure element support, all keys are transparent. */
953#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
954 psa_get_key_from_slot( handle, p_slot, usage, alg )
955#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
956
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100957/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100958static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000959{
Gilles Peskine73167e12019-07-12 23:44:37 +0200960#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
961 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000962 {
963 /* No key material to clean. */
964 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200965 else
966#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200967 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000968 {
969 /* No key material to clean. */
970 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200971 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000972 {
973 mbedtls_free( slot->data.raw.data );
974 }
975 else
976#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200977 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000978 {
979 mbedtls_rsa_free( slot->data.rsa );
980 mbedtls_free( slot->data.rsa );
981 }
982 else
983#endif /* defined(MBEDTLS_RSA_C) */
984#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200985 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000986 {
987 mbedtls_ecp_keypair_free( slot->data.ecp );
988 mbedtls_free( slot->data.ecp );
989 }
990 else
991#endif /* defined(MBEDTLS_ECP_C) */
992 {
993 /* Shouldn't happen: the key type is not any type that we
994 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200995 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000996 }
997
998 return( PSA_SUCCESS );
999}
1000
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001001/** Completely wipe a slot in memory, including its policy.
1002 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001003psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001004{
1005 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001006 /* Multipart operations may still be using the key. This is safe
1007 * because all multipart operation objects are independent from
1008 * the key slot: if they need to access the key after the setup
1009 * phase, they have a copy of the key. Note that this means that
1010 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001011 /* At this point, key material and other type-specific content has
1012 * been wiped. Clear remaining metadata. We can call memset and not
1013 * zeroize because the metadata is not particularly sensitive. */
1014 memset( slot, 0, sizeof( *slot ) );
1015 return( status );
1016}
1017
Gilles Peskinec5487a82018-12-03 18:08:14 +01001018psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001019{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001020 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001021 psa_status_t status; /* status of the last operation */
1022 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001023#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1024 psa_se_drv_table_entry_t *driver;
1025#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001026
Gilles Peskine1841cf42019-10-08 15:48:25 +02001027 if( handle == 0 )
1028 return( PSA_SUCCESS );
1029
Gilles Peskinec5487a82018-12-03 18:08:14 +01001030 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001031 if( status != PSA_SUCCESS )
1032 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001033
1034#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001035 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001036 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001037 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001038 /* For a key in a secure element, we need to do three things:
1039 * remove the key file in internal storage, destroy the
1040 * key inside the secure element, and update the driver's
1041 * persistent data. Start a transaction that will encompass these
1042 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001043 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001044 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001045 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001046 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001047 status = psa_crypto_save_transaction( );
1048 if( status != PSA_SUCCESS )
1049 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001050 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001051 /* We should still try to destroy the key in the secure
1052 * element and the key metadata in storage. This is especially
1053 * important if the error is that the storage is full.
1054 * But how to do it exactly without risking an inconsistent
1055 * state after a reset?
1056 * https://github.com/ARMmbed/mbed-crypto/issues/215
1057 */
1058 overall_status = status;
1059 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001060 }
1061
Gilles Peskine354f7672019-07-12 23:46:38 +02001062 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001063 if( overall_status == PSA_SUCCESS )
1064 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001065 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001066#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1067
Darryl Greend49a4992018-06-18 17:27:26 +01001068#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001069 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001070 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001071 status = psa_destroy_persistent_key( slot->attr.id );
1072 if( overall_status == PSA_SUCCESS )
1073 overall_status = status;
1074
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001075 /* TODO: other slots may have a copy of the same key. We should
1076 * invalidate them.
1077 * https://github.com/ARMmbed/mbed-crypto/issues/214
1078 */
Darryl Greend49a4992018-06-18 17:27:26 +01001079 }
1080#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001081
Gilles Peskinefc762652019-07-22 19:30:34 +02001082#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1083 if( driver != NULL )
1084 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001085 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001086 if( overall_status == PSA_SUCCESS )
1087 overall_status = status;
1088 status = psa_crypto_stop_transaction( );
1089 if( overall_status == PSA_SUCCESS )
1090 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001091 }
1092#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1093
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001094#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1095exit:
1096#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001097 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001098 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1099 if( overall_status == PSA_SUCCESS )
1100 overall_status = status;
1101 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001102}
1103
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001104void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001105{
Gilles Peskineb699f072019-04-26 16:06:02 +02001106 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001107 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001108}
1109
Gilles Peskineb699f072019-04-26 16:06:02 +02001110psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1111 psa_key_type_t type,
1112 const uint8_t *data,
1113 size_t data_length )
1114{
1115 uint8_t *copy = NULL;
1116
1117 if( data_length != 0 )
1118 {
1119 copy = mbedtls_calloc( 1, data_length );
1120 if( copy == NULL )
1121 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1122 memcpy( copy, data, data_length );
1123 }
1124 /* After this point, this function is guaranteed to succeed, so it
1125 * can start modifying `*attributes`. */
1126
1127 if( attributes->domain_parameters != NULL )
1128 {
1129 mbedtls_free( attributes->domain_parameters );
1130 attributes->domain_parameters = NULL;
1131 attributes->domain_parameters_size = 0;
1132 }
1133
1134 attributes->domain_parameters = copy;
1135 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001136 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001137 return( PSA_SUCCESS );
1138}
1139
1140psa_status_t psa_get_key_domain_parameters(
1141 const psa_key_attributes_t *attributes,
1142 uint8_t *data, size_t data_size, size_t *data_length )
1143{
1144 if( attributes->domain_parameters_size > data_size )
1145 return( PSA_ERROR_BUFFER_TOO_SMALL );
1146 *data_length = attributes->domain_parameters_size;
1147 if( attributes->domain_parameters_size != 0 )
1148 memcpy( data, attributes->domain_parameters,
1149 attributes->domain_parameters_size );
1150 return( PSA_SUCCESS );
1151}
1152
1153#if defined(MBEDTLS_RSA_C)
1154static psa_status_t psa_get_rsa_public_exponent(
1155 const mbedtls_rsa_context *rsa,
1156 psa_key_attributes_t *attributes )
1157{
1158 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001159 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001160 uint8_t *buffer = NULL;
1161 size_t buflen;
1162 mbedtls_mpi_init( &mpi );
1163
1164 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1165 if( ret != 0 )
1166 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001167 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1168 {
1169 /* It's the default value, which is reported as an empty string,
1170 * so there's nothing to do. */
1171 goto exit;
1172 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001173
1174 buflen = mbedtls_mpi_size( &mpi );
1175 buffer = mbedtls_calloc( 1, buflen );
1176 if( buffer == NULL )
1177 {
1178 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1179 goto exit;
1180 }
1181 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1182 if( ret != 0 )
1183 goto exit;
1184 attributes->domain_parameters = buffer;
1185 attributes->domain_parameters_size = buflen;
1186
1187exit:
1188 mbedtls_mpi_free( &mpi );
1189 if( ret != 0 )
1190 mbedtls_free( buffer );
1191 return( mbedtls_to_psa_error( ret ) );
1192}
1193#endif /* MBEDTLS_RSA_C */
1194
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001195/** Retrieve all the publicly-accessible attributes of a key.
1196 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001197psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1198 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001199{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001200 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001201 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001202
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001203 psa_reset_key_attributes( attributes );
1204
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001205 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001206 if( status != PSA_SUCCESS )
1207 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001208
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001209 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001210 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1211 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1212
1213#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1214 if( psa_key_slot_is_external( slot ) )
1215 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1216#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001217
Gilles Peskine8e338702019-07-30 20:06:31 +02001218 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001219 {
1220#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001221 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001222 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001223#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001224 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001225 * is not yet implemented.
1226 * https://github.com/ARMmbed/mbed-crypto/issues/216
1227 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001228 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001229 break;
1230#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001231 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1232 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001233#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001234 default:
1235 /* Nothing else to do. */
1236 break;
1237 }
1238
1239 if( status != PSA_SUCCESS )
1240 psa_reset_key_attributes( attributes );
1241 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001242}
1243
Gilles Peskinec8000c02019-08-02 20:15:51 +02001244#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1245psa_status_t psa_get_key_slot_number(
1246 const psa_key_attributes_t *attributes,
1247 psa_key_slot_number_t *slot_number )
1248{
1249 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1250 {
1251 *slot_number = attributes->slot_number;
1252 return( PSA_SUCCESS );
1253 }
1254 else
1255 return( PSA_ERROR_INVALID_ARGUMENT );
1256}
1257#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1258
Jaeden Ameroccdce902019-01-10 11:42:27 +00001259#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001260static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1261 unsigned char *buf, size_t size )
1262{
Janos Follath24eed8d2019-11-22 13:21:35 +00001263 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001264 unsigned char *c;
1265 size_t len = 0;
1266
1267 c = buf + size;
1268
1269 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1270
1271 return( (int) len );
1272}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001273#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001274
Gilles Peskinef603c712019-01-19 13:40:11 +01001275static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1276 uint8_t *data,
1277 size_t data_size,
1278 size_t *data_length,
1279 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001280{
Gilles Peskine5d309672019-07-12 23:47:28 +02001281#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1282 const psa_drv_se_t *drv;
1283 psa_drv_se_context_t *drv_context;
1284#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1285
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001286 *data_length = 0;
1287
Gilles Peskine8e338702019-07-30 20:06:31 +02001288 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001289 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001290
Gilles Peskinef9168942019-09-12 19:20:29 +02001291 /* Reject a zero-length output buffer now, since this can never be a
1292 * valid key representation. This way we know that data must be a valid
1293 * pointer and we can do things like memset(data, ..., data_size). */
1294 if( data_size == 0 )
1295 return( PSA_ERROR_BUFFER_TOO_SMALL );
1296
Gilles Peskine5d309672019-07-12 23:47:28 +02001297#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001298 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001299 {
1300 psa_drv_se_export_key_t method;
1301 if( drv->key_management == NULL )
1302 return( PSA_ERROR_NOT_SUPPORTED );
1303 method = ( export_public_key ?
1304 drv->key_management->p_export_public :
1305 drv->key_management->p_export );
1306 if( method == NULL )
1307 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001308 return( method( drv_context,
1309 slot->data.se.slot_number,
1310 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001311 }
1312#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1313
Gilles Peskine8e338702019-07-30 20:06:31 +02001314 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001315 {
1316 if( slot->data.raw.bytes > data_size )
1317 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001318 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1319 memset( data + slot->data.raw.bytes, 0,
1320 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001321 *data_length = slot->data.raw.bytes;
1322 return( PSA_SUCCESS );
1323 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001324#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001325 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001326 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001327 psa_status_t status;
1328
Gilles Peskineb46bef22019-07-30 21:32:04 +02001329 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001330 if( bytes > data_size )
1331 return( PSA_ERROR_BUFFER_TOO_SMALL );
1332 status = mbedtls_to_psa_error(
1333 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1334 if( status != PSA_SUCCESS )
1335 return( status );
1336 memset( data + bytes, 0, data_size - bytes );
1337 *data_length = bytes;
1338 return( PSA_SUCCESS );
1339 }
1340#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001341 else
Moran Peker17e36e12018-05-02 12:55:20 +03001342 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001343#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001344 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1345 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001346 {
Moran Pekera998bc62018-04-16 18:16:20 +03001347 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001348 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001349 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001350 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001351#if defined(MBEDTLS_RSA_C)
1352 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001353 pk.pk_info = &mbedtls_rsa_info;
1354 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001355#else
1356 return( PSA_ERROR_NOT_SUPPORTED );
1357#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001358 }
1359 else
1360 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001361#if defined(MBEDTLS_ECP_C)
1362 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001363 pk.pk_info = &mbedtls_eckey_info;
1364 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001365#else
1366 return( PSA_ERROR_NOT_SUPPORTED );
1367#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001368 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001369 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001370 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001371 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001372 }
Moran Peker17e36e12018-05-02 12:55:20 +03001373 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001374 {
Moran Peker17e36e12018-05-02 12:55:20 +03001375 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001376 }
Moran Peker60364322018-04-29 11:34:58 +03001377 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001378 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001379 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001380 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001381 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001382 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1383 * Move the data to the beginning and erase remaining data
1384 * at the original location. */
1385 if( 2 * (size_t) ret <= data_size )
1386 {
1387 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001388 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001389 }
1390 else if( (size_t) ret < data_size )
1391 {
1392 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001393 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001394 }
Moran Pekera998bc62018-04-16 18:16:20 +03001395 *data_length = ret;
1396 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001397 }
1398 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001399#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001400 {
1401 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001402 it is valid for a special-purpose implementation to omit
1403 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001404 return( PSA_ERROR_NOT_SUPPORTED );
1405 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001406 }
1407}
1408
Gilles Peskinec5487a82018-12-03 18:08:14 +01001409psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001410 uint8_t *data,
1411 size_t data_size,
1412 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001413{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001414 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001415 psa_status_t status;
1416
1417 /* Set the key to empty now, so that even when there are errors, we always
1418 * set data_length to a value between 0 and data_size. On error, setting
1419 * the key to empty is a good choice because an empty key representation is
1420 * unlikely to be accepted anywhere. */
1421 *data_length = 0;
1422
1423 /* Export requires the EXPORT flag. There is an exception for public keys,
1424 * which don't require any flag, but psa_get_key_from_slot takes
1425 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001426 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001427 if( status != PSA_SUCCESS )
1428 return( status );
1429 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001430 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001431}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001432
Gilles Peskinec5487a82018-12-03 18:08:14 +01001433psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001434 uint8_t *data,
1435 size_t data_size,
1436 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001437{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001438 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001439 psa_status_t status;
1440
1441 /* Set the key to empty now, so that even when there are errors, we always
1442 * set data_length to a value between 0 and data_size. On error, setting
1443 * the key to empty is a good choice because an empty key representation is
1444 * unlikely to be accepted anywhere. */
1445 *data_length = 0;
1446
1447 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001448 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001449 if( status != PSA_SUCCESS )
1450 return( status );
1451 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001452 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001453}
1454
Gilles Peskine91e8c332019-08-02 19:19:39 +02001455#if defined(static_assert)
1456static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1457 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001458static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001459 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001460static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001461 "One or more key attribute flag is listed as both internal-only and external-only" );
1462#endif
1463
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001464/** Validate that a key policy is internally well-formed.
1465 *
1466 * This function only rejects invalid policies. It does not validate the
1467 * consistency of the policy with respect to other attributes of the key
1468 * such as the key type.
1469 */
1470static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001471{
1472 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001473 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001474 PSA_KEY_USAGE_ENCRYPT |
1475 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001476 PSA_KEY_USAGE_SIGN_HASH |
1477 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001478 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1479 return( PSA_ERROR_INVALID_ARGUMENT );
1480
Gilles Peskine4747d192019-04-17 15:05:45 +02001481 return( PSA_SUCCESS );
1482}
1483
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001484/** Validate the internal consistency of key attributes.
1485 *
1486 * This function only rejects invalid attribute values. If does not
1487 * validate the consistency of the attributes with any key data that may
1488 * be involved in the creation of the key.
1489 *
1490 * Call this function early in the key creation process.
1491 *
1492 * \param[in] attributes Key attributes for the new key.
1493 * \param[out] p_drv On any return, the driver for the key, if any.
1494 * NULL for a transparent key.
1495 *
1496 */
1497static psa_status_t psa_validate_key_attributes(
1498 const psa_key_attributes_t *attributes,
1499 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001500{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001501 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001502
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001503 status = psa_validate_key_location( attributes, p_drv );
1504 if( status != PSA_SUCCESS )
1505 return( status );
1506
1507 status = psa_validate_key_persistence( attributes );
1508 if( status != PSA_SUCCESS )
1509 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001510
1511 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001512 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001513 return( status );
1514
1515 /* Refuse to create overly large keys.
1516 * Note that this doesn't trigger on import if the attributes don't
1517 * explicitly specify a size (so psa_get_key_bits returns 0), so
1518 * psa_import_key() needs its own checks. */
1519 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1520 return( PSA_ERROR_NOT_SUPPORTED );
1521
Gilles Peskine91e8c332019-08-02 19:19:39 +02001522 /* Reject invalid flags. These should not be reachable through the API. */
1523 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1524 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1525 return( PSA_ERROR_INVALID_ARGUMENT );
1526
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001527 return( PSA_SUCCESS );
1528}
1529
Gilles Peskine4747d192019-04-17 15:05:45 +02001530/** Prepare a key slot to receive key material.
1531 *
1532 * This function allocates a key slot and sets its metadata.
1533 *
1534 * If this function fails, call psa_fail_key_creation().
1535 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001536 * This function is intended to be used as follows:
1537 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1538 * it with the specified attributes, and assign it a handle.
1539 * -# Populate the slot with the key material.
1540 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1541 * In case of failure at any step, stop the sequence and call
1542 * psa_fail_key_creation().
1543 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001544 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001545 * \param[in] attributes Key attributes for the new key.
1546 * \param[out] handle On success, a handle for the allocated slot.
1547 * \param[out] p_slot On success, a pointer to the prepared slot.
1548 * \param[out] p_drv On any return, the driver for the key, if any.
1549 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001550 *
1551 * \retval #PSA_SUCCESS
1552 * The key slot is ready to receive key material.
1553 * \return If this function fails, the key slot is an invalid state.
1554 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001555 */
1556static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001557 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001558 const psa_key_attributes_t *attributes,
1559 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001560 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001561 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001562{
1563 psa_status_t status;
1564 psa_key_slot_t *slot;
1565
Gilles Peskinedf179142019-07-15 22:02:14 +02001566 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001567 *p_drv = NULL;
1568
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001569 status = psa_validate_key_attributes( attributes, p_drv );
1570 if( status != PSA_SUCCESS )
1571 return( status );
1572
Gilles Peskineedbed562019-08-07 18:19:59 +02001573 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001574 if( status != PSA_SUCCESS )
1575 return( status );
1576 slot = *p_slot;
1577
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001578 /* We're storing the declared bit-size of the key. It's up to each
1579 * creation mechanism to verify that this information is correct.
1580 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001581 * an input (generate, device) but not for those where the bit-size
1582 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001583
1584 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001585
Gilles Peskine91e8c332019-08-02 19:19:39 +02001586 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001587 * external-only flags, query `attributes`. Thanks to the check
1588 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001589 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001590 * may have set. */
1591 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001592
Gilles Peskinecbaff462019-07-12 23:46:04 +02001593#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001594 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001595 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001596 * create the key file in internal storage, create the
1597 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001598 * persistent data. This is done by starting a transaction that will
1599 * encompass these three actions.
1600 * For registering a volatile key, we just need to find an appropriate
1601 * slot number inside the SE. Since the key is designated volatile, creating
1602 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001603 /* The first thing to do is to find a slot number for the new key.
1604 * We save the slot number in persistent storage as part of the
1605 * transaction data. It will be needed to recover if the power
1606 * fails during the key creation process, to clean up on the secure
1607 * element side after restarting. Obtaining a slot number from the
1608 * secure element driver updates its persistent state, but we do not yet
1609 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001610 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001611 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001612 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001613 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001614 &slot->data.se.slot_number );
1615 if( status != PSA_SUCCESS )
1616 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001617
1618 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001619 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001620 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1621 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1622 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1623 psa_crypto_transaction.key.id = slot->attr.id;
1624 status = psa_crypto_save_transaction( );
1625 if( status != PSA_SUCCESS )
1626 {
1627 (void) psa_crypto_stop_transaction( );
1628 return( status );
1629 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001630 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001631 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001632
1633 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1634 {
1635 /* Key registration only makes sense with a secure element. */
1636 return( PSA_ERROR_INVALID_ARGUMENT );
1637 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001638#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1639
Darryl Green0c6575a2018-11-07 16:05:30 +00001640 return( status );
1641}
Gilles Peskine4747d192019-04-17 15:05:45 +02001642
1643/** Finalize the creation of a key once its key material has been set.
1644 *
1645 * This entails writing the key to persistent storage.
1646 *
1647 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001648 * See the documentation of psa_start_key_creation() for the intended use
1649 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001650 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001651 * \param[in,out] slot Pointer to the slot with key material.
1652 * \param[in] driver The secure element driver for the key,
1653 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001654 *
1655 * \retval #PSA_SUCCESS
1656 * The key was successfully created. The handle is now valid.
1657 * \return If this function fails, the key slot is an invalid state.
1658 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001659 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001660static psa_status_t psa_finish_key_creation(
1661 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001662 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001663{
1664 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001665 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001666 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001667
1668#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001669 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001670 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001671#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1672 if( driver != NULL )
1673 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001674 psa_se_key_data_storage_t data;
1675#if defined(static_assert)
1676 static_assert( sizeof( slot->data.se.slot_number ) ==
1677 sizeof( data.slot_number ),
1678 "Slot number size does not match psa_se_key_data_storage_t" );
1679 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1680 "Bit-size size does not match psa_se_key_data_storage_t" );
1681#endif
1682 memcpy( &data.slot_number, &slot->data.se.slot_number,
1683 sizeof( slot->data.se.slot_number ) );
1684 memcpy( &data.bits, &slot->attr.bits,
1685 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001686 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001687 (uint8_t*) &data,
1688 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001689 }
1690 else
1691#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1692 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001693 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001694 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001695 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001696 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1697 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001698 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001699 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1700 status = psa_internal_export_key( slot,
1701 buffer, buffer_size, &length,
1702 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001703 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001704 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001705 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001706
Gilles Peskinef9168942019-09-12 19:20:29 +02001707 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001708 mbedtls_free( buffer );
1709 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001710 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001711#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1712
Gilles Peskinecbaff462019-07-12 23:46:04 +02001713#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001714 /* Finish the transaction for a key creation. This does not
1715 * happen when registering an existing key. Detect this case
1716 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001717 * creation of a persistent key in a secure element requires a transaction,
1718 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001719 if( driver != NULL &&
1720 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001721 {
1722 status = psa_save_se_persistent_data( driver );
1723 if( status != PSA_SUCCESS )
1724 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001725 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001726 return( status );
1727 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001728 status = psa_crypto_stop_transaction( );
1729 if( status != PSA_SUCCESS )
1730 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001731 }
1732#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1733
Gilles Peskine4747d192019-04-17 15:05:45 +02001734 return( status );
1735}
1736
1737/** Abort the creation of a key.
1738 *
1739 * You may call this function after calling psa_start_key_creation(),
1740 * or after psa_finish_key_creation() fails. In other circumstances, this
1741 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001742 * See the documentation of psa_start_key_creation() for the intended use
1743 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001744 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001745 * \param[in,out] slot Pointer to the slot with key material.
1746 * \param[in] driver The secure element driver for the key,
1747 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001748 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001749static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001750 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001751{
Gilles Peskine011e4282019-06-26 18:34:38 +02001752 (void) driver;
1753
Gilles Peskine4747d192019-04-17 15:05:45 +02001754 if( slot == NULL )
1755 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001756
1757#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001758 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001759 * element, and the failure happened later (when saving metadata
1760 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001761 * element.
1762 * https://github.com/ARMmbed/mbed-crypto/issues/217
1763 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001764
Gilles Peskined7729582019-08-05 15:55:54 +02001765 /* Abort the ongoing transaction if any (there may not be one if
1766 * the creation process failed before starting one, or if the
1767 * key creation is a registration of a key in a secure element).
1768 * Earlier functions must already have done what it takes to undo any
1769 * partial creation. All that's left is to update the transaction data
1770 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001771 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001772#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1773
Gilles Peskine4747d192019-04-17 15:05:45 +02001774 psa_wipe_key_slot( slot );
1775}
1776
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001777/** Validate optional attributes during key creation.
1778 *
1779 * Some key attributes are optional during key creation. If they are
1780 * specified in the attributes structure, check that they are consistent
1781 * with the data in the slot.
1782 *
1783 * This function should be called near the end of key creation, after
1784 * the slot in memory is fully populated but before saving persistent data.
1785 */
1786static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001787 const psa_key_slot_t *slot,
1788 const psa_key_attributes_t *attributes )
1789{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001790 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001791 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001792 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001793 return( PSA_ERROR_INVALID_ARGUMENT );
1794 }
1795
1796 if( attributes->domain_parameters_size != 0 )
1797 {
1798#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001799 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001800 {
1801 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001802 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001803 mbedtls_mpi_init( &actual );
1804 mbedtls_mpi_init( &required );
1805 ret = mbedtls_rsa_export( slot->data.rsa,
1806 NULL, NULL, NULL, NULL, &actual );
1807 if( ret != 0 )
1808 goto rsa_exit;
1809 ret = mbedtls_mpi_read_binary( &required,
1810 attributes->domain_parameters,
1811 attributes->domain_parameters_size );
1812 if( ret != 0 )
1813 goto rsa_exit;
1814 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1815 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1816 rsa_exit:
1817 mbedtls_mpi_free( &actual );
1818 mbedtls_mpi_free( &required );
1819 if( ret != 0)
1820 return( mbedtls_to_psa_error( ret ) );
1821 }
1822 else
1823#endif
1824 {
1825 return( PSA_ERROR_INVALID_ARGUMENT );
1826 }
1827 }
1828
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001829 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001830 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001831 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001832 return( PSA_ERROR_INVALID_ARGUMENT );
1833 }
1834
1835 return( PSA_SUCCESS );
1836}
1837
Gilles Peskine4747d192019-04-17 15:05:45 +02001838psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001839 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001840 size_t data_length,
1841 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001842{
1843 psa_status_t status;
1844 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001845 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001846
Gilles Peskine0f84d622019-09-12 19:03:13 +02001847 /* Reject zero-length symmetric keys (including raw data key objects).
1848 * This also rejects any key which might be encoded as an empty string,
1849 * which is never valid. */
1850 if( data_length == 0 )
1851 return( PSA_ERROR_INVALID_ARGUMENT );
1852
Gilles Peskinedf179142019-07-15 22:02:14 +02001853 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1854 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001855 if( status != PSA_SUCCESS )
1856 goto exit;
1857
Gilles Peskine5d309672019-07-12 23:47:28 +02001858#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1859 if( driver != NULL )
1860 {
1861 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001862 /* The driver should set the number of key bits, however in
1863 * case it doesn't, we initialize bits to an invalid value. */
1864 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001865 if( drv->key_management == NULL ||
1866 drv->key_management->p_import == NULL )
1867 {
1868 status = PSA_ERROR_NOT_SUPPORTED;
1869 goto exit;
1870 }
1871 status = drv->key_management->p_import(
1872 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001873 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001874 &bits );
1875 if( status != PSA_SUCCESS )
1876 goto exit;
1877 if( bits > PSA_MAX_KEY_BITS )
1878 {
1879 status = PSA_ERROR_NOT_SUPPORTED;
1880 goto exit;
1881 }
1882 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001883 }
1884 else
1885#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1886 {
1887 status = psa_import_key_into_slot( slot, data, data_length );
1888 if( status != PSA_SUCCESS )
1889 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001890 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001891 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001892 if( status != PSA_SUCCESS )
1893 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001894
Gilles Peskine011e4282019-06-26 18:34:38 +02001895 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001896exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001897 if( status != PSA_SUCCESS )
1898 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001899 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001900 *handle = 0;
1901 }
1902 return( status );
1903}
1904
Gilles Peskined7729582019-08-05 15:55:54 +02001905#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1906psa_status_t mbedtls_psa_register_se_key(
1907 const psa_key_attributes_t *attributes )
1908{
1909 psa_status_t status;
1910 psa_key_slot_t *slot = NULL;
1911 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001912 psa_key_handle_t handle = 0;
1913
1914 /* Leaving attributes unspecified is not currently supported.
1915 * It could make sense to query the key type and size from the
1916 * secure element, but not all secure elements support this
1917 * and the driver HAL doesn't currently support it. */
1918 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1919 return( PSA_ERROR_NOT_SUPPORTED );
1920 if( psa_get_key_bits( attributes ) == 0 )
1921 return( PSA_ERROR_NOT_SUPPORTED );
1922
1923 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1924 &handle, &slot, &driver );
1925 if( status != PSA_SUCCESS )
1926 goto exit;
1927
Gilles Peskined7729582019-08-05 15:55:54 +02001928 status = psa_finish_key_creation( slot, driver );
1929
1930exit:
1931 if( status != PSA_SUCCESS )
1932 {
1933 psa_fail_key_creation( slot, driver );
1934 }
1935 /* Registration doesn't keep the key in RAM. */
1936 psa_close_key( handle );
1937 return( status );
1938}
1939#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1940
Gilles Peskinef603c712019-01-19 13:40:11 +01001941static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001942 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001943{
1944 psa_status_t status;
1945 uint8_t *buffer = NULL;
1946 size_t buffer_size = 0;
1947 size_t length;
1948
Gilles Peskine8e338702019-07-30 20:06:31 +02001949 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001950 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001951 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001952 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001953 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001954 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1955 if( status != PSA_SUCCESS )
1956 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001957 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001958 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001959
1960exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001961 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001962 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001963 return( status );
1964}
1965
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001966psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1967 const psa_key_attributes_t *specified_attributes,
1968 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001969{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001970 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001971 psa_key_slot_t *source_slot = NULL;
1972 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001973 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001974 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001975
Gilles Peskine28f8f302019-07-24 13:30:31 +02001976 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001977 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001978 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001979 goto exit;
1980
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001981 status = psa_validate_optional_attributes( source_slot,
1982 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001983 if( status != PSA_SUCCESS )
1984 goto exit;
1985
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001986 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001987 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001988 if( status != PSA_SUCCESS )
1989 goto exit;
1990
Gilles Peskinedf179142019-07-15 22:02:14 +02001991 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1992 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001993 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001994 if( status != PSA_SUCCESS )
1995 goto exit;
1996
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001997#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1998 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01001999 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002000 /* Copying to a secure element is not implemented yet. */
2001 status = PSA_ERROR_NOT_SUPPORTED;
2002 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002003 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002004#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002005
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002006 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002007 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002008 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002009
Gilles Peskine011e4282019-06-26 18:34:38 +02002010 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002011exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002012 if( status != PSA_SUCCESS )
2013 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002014 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002015 *target_handle = 0;
2016 }
2017 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002018}
2019
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002020
2021
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002022/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002023/* Message digests */
2024/****************************************************************/
2025
Gilles Peskineb16841e2019-10-10 20:36:12 +02002026#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002027static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002028{
2029 switch( alg )
2030 {
2031#if defined(MBEDTLS_MD2_C)
2032 case PSA_ALG_MD2:
2033 return( &mbedtls_md2_info );
2034#endif
2035#if defined(MBEDTLS_MD4_C)
2036 case PSA_ALG_MD4:
2037 return( &mbedtls_md4_info );
2038#endif
2039#if defined(MBEDTLS_MD5_C)
2040 case PSA_ALG_MD5:
2041 return( &mbedtls_md5_info );
2042#endif
2043#if defined(MBEDTLS_RIPEMD160_C)
2044 case PSA_ALG_RIPEMD160:
2045 return( &mbedtls_ripemd160_info );
2046#endif
2047#if defined(MBEDTLS_SHA1_C)
2048 case PSA_ALG_SHA_1:
2049 return( &mbedtls_sha1_info );
2050#endif
2051#if defined(MBEDTLS_SHA256_C)
2052 case PSA_ALG_SHA_224:
2053 return( &mbedtls_sha224_info );
2054 case PSA_ALG_SHA_256:
2055 return( &mbedtls_sha256_info );
2056#endif
2057#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002058#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002059 case PSA_ALG_SHA_384:
2060 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002061#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002062 case PSA_ALG_SHA_512:
2063 return( &mbedtls_sha512_info );
2064#endif
2065 default:
2066 return( NULL );
2067 }
2068}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002069#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002070
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002071psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2072{
2073 switch( operation->alg )
2074 {
Gilles Peskine81736312018-06-26 15:04:31 +02002075 case 0:
2076 /* The object has (apparently) been initialized but it is not
2077 * in use. It's ok to call abort on such an object, and there's
2078 * nothing to do. */
2079 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002080#if defined(MBEDTLS_MD2_C)
2081 case PSA_ALG_MD2:
2082 mbedtls_md2_free( &operation->ctx.md2 );
2083 break;
2084#endif
2085#if defined(MBEDTLS_MD4_C)
2086 case PSA_ALG_MD4:
2087 mbedtls_md4_free( &operation->ctx.md4 );
2088 break;
2089#endif
2090#if defined(MBEDTLS_MD5_C)
2091 case PSA_ALG_MD5:
2092 mbedtls_md5_free( &operation->ctx.md5 );
2093 break;
2094#endif
2095#if defined(MBEDTLS_RIPEMD160_C)
2096 case PSA_ALG_RIPEMD160:
2097 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2098 break;
2099#endif
2100#if defined(MBEDTLS_SHA1_C)
2101 case PSA_ALG_SHA_1:
2102 mbedtls_sha1_free( &operation->ctx.sha1 );
2103 break;
2104#endif
2105#if defined(MBEDTLS_SHA256_C)
2106 case PSA_ALG_SHA_224:
2107 case PSA_ALG_SHA_256:
2108 mbedtls_sha256_free( &operation->ctx.sha256 );
2109 break;
2110#endif
2111#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002112#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002113 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002114#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002115 case PSA_ALG_SHA_512:
2116 mbedtls_sha512_free( &operation->ctx.sha512 );
2117 break;
2118#endif
2119 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002120 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002121 }
2122 operation->alg = 0;
2123 return( PSA_SUCCESS );
2124}
2125
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002126psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002127 psa_algorithm_t alg )
2128{
Janos Follath24eed8d2019-11-22 13:21:35 +00002129 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002130
2131 /* A context must be freshly initialized before it can be set up. */
2132 if( operation->alg != 0 )
2133 {
2134 return( PSA_ERROR_BAD_STATE );
2135 }
2136
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002137 switch( alg )
2138 {
2139#if defined(MBEDTLS_MD2_C)
2140 case PSA_ALG_MD2:
2141 mbedtls_md2_init( &operation->ctx.md2 );
2142 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2143 break;
2144#endif
2145#if defined(MBEDTLS_MD4_C)
2146 case PSA_ALG_MD4:
2147 mbedtls_md4_init( &operation->ctx.md4 );
2148 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2149 break;
2150#endif
2151#if defined(MBEDTLS_MD5_C)
2152 case PSA_ALG_MD5:
2153 mbedtls_md5_init( &operation->ctx.md5 );
2154 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2155 break;
2156#endif
2157#if defined(MBEDTLS_RIPEMD160_C)
2158 case PSA_ALG_RIPEMD160:
2159 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2160 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2161 break;
2162#endif
2163#if defined(MBEDTLS_SHA1_C)
2164 case PSA_ALG_SHA_1:
2165 mbedtls_sha1_init( &operation->ctx.sha1 );
2166 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2167 break;
2168#endif
2169#if defined(MBEDTLS_SHA256_C)
2170 case PSA_ALG_SHA_224:
2171 mbedtls_sha256_init( &operation->ctx.sha256 );
2172 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2173 break;
2174 case PSA_ALG_SHA_256:
2175 mbedtls_sha256_init( &operation->ctx.sha256 );
2176 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2177 break;
2178#endif
2179#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002180#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002181 case PSA_ALG_SHA_384:
2182 mbedtls_sha512_init( &operation->ctx.sha512 );
2183 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2184 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002185#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002186 case PSA_ALG_SHA_512:
2187 mbedtls_sha512_init( &operation->ctx.sha512 );
2188 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2189 break;
2190#endif
2191 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002192 return( PSA_ALG_IS_HASH( alg ) ?
2193 PSA_ERROR_NOT_SUPPORTED :
2194 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002195 }
2196 if( ret == 0 )
2197 operation->alg = alg;
2198 else
2199 psa_hash_abort( operation );
2200 return( mbedtls_to_psa_error( ret ) );
2201}
2202
2203psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2204 const uint8_t *input,
2205 size_t input_length )
2206{
Janos Follath24eed8d2019-11-22 13:21:35 +00002207 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002208
2209 /* Don't require hash implementations to behave correctly on a
2210 * zero-length input, which may have an invalid pointer. */
2211 if( input_length == 0 )
2212 return( PSA_SUCCESS );
2213
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002214 switch( operation->alg )
2215 {
2216#if defined(MBEDTLS_MD2_C)
2217 case PSA_ALG_MD2:
2218 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2219 input, input_length );
2220 break;
2221#endif
2222#if defined(MBEDTLS_MD4_C)
2223 case PSA_ALG_MD4:
2224 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2225 input, input_length );
2226 break;
2227#endif
2228#if defined(MBEDTLS_MD5_C)
2229 case PSA_ALG_MD5:
2230 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2231 input, input_length );
2232 break;
2233#endif
2234#if defined(MBEDTLS_RIPEMD160_C)
2235 case PSA_ALG_RIPEMD160:
2236 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2237 input, input_length );
2238 break;
2239#endif
2240#if defined(MBEDTLS_SHA1_C)
2241 case PSA_ALG_SHA_1:
2242 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2243 input, input_length );
2244 break;
2245#endif
2246#if defined(MBEDTLS_SHA256_C)
2247 case PSA_ALG_SHA_224:
2248 case PSA_ALG_SHA_256:
2249 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2250 input, input_length );
2251 break;
2252#endif
2253#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002254#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002255 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002256#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002257 case PSA_ALG_SHA_512:
2258 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2259 input, input_length );
2260 break;
2261#endif
2262 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002263 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002264 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002265
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002266 if( ret != 0 )
2267 psa_hash_abort( operation );
2268 return( mbedtls_to_psa_error( ret ) );
2269}
2270
2271psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2272 uint8_t *hash,
2273 size_t hash_size,
2274 size_t *hash_length )
2275{
itayzafrir40835d42018-08-02 13:14:17 +03002276 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002277 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002278 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002279
2280 /* Fill the output buffer with something that isn't a valid hash
2281 * (barring an attack on the hash and deliberately-crafted input),
2282 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002283 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002284 /* If hash_size is 0 then hash may be NULL and then the
2285 * call to memset would have undefined behavior. */
2286 if( hash_size != 0 )
2287 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002288
2289 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002290 {
2291 status = PSA_ERROR_BUFFER_TOO_SMALL;
2292 goto exit;
2293 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002294
2295 switch( operation->alg )
2296 {
2297#if defined(MBEDTLS_MD2_C)
2298 case PSA_ALG_MD2:
2299 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2300 break;
2301#endif
2302#if defined(MBEDTLS_MD4_C)
2303 case PSA_ALG_MD4:
2304 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2305 break;
2306#endif
2307#if defined(MBEDTLS_MD5_C)
2308 case PSA_ALG_MD5:
2309 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2310 break;
2311#endif
2312#if defined(MBEDTLS_RIPEMD160_C)
2313 case PSA_ALG_RIPEMD160:
2314 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2315 break;
2316#endif
2317#if defined(MBEDTLS_SHA1_C)
2318 case PSA_ALG_SHA_1:
2319 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2320 break;
2321#endif
2322#if defined(MBEDTLS_SHA256_C)
2323 case PSA_ALG_SHA_224:
2324 case PSA_ALG_SHA_256:
2325 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2326 break;
2327#endif
2328#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002329#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002330 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002331#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002332 case PSA_ALG_SHA_512:
2333 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2334 break;
2335#endif
2336 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002337 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002338 }
itayzafrir40835d42018-08-02 13:14:17 +03002339 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002340
itayzafrir40835d42018-08-02 13:14:17 +03002341exit:
2342 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002343 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002344 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002345 return( psa_hash_abort( operation ) );
2346 }
2347 else
2348 {
2349 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002350 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002351 }
2352}
2353
Gilles Peskine2d277862018-06-18 15:41:12 +02002354psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2355 const uint8_t *hash,
2356 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002357{
2358 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2359 size_t actual_hash_length;
2360 psa_status_t status = psa_hash_finish( operation,
2361 actual_hash, sizeof( actual_hash ),
2362 &actual_hash_length );
2363 if( status != PSA_SUCCESS )
2364 return( status );
2365 if( actual_hash_length != hash_length )
2366 return( PSA_ERROR_INVALID_SIGNATURE );
2367 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2368 return( PSA_ERROR_INVALID_SIGNATURE );
2369 return( PSA_SUCCESS );
2370}
2371
Gilles Peskine0a749c82019-11-28 19:33:58 +01002372psa_status_t psa_hash_compute( psa_algorithm_t alg,
2373 const uint8_t *input, size_t input_length,
2374 uint8_t *hash, size_t hash_size,
2375 size_t *hash_length )
2376{
2377 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2378 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2379
2380 *hash_length = hash_size;
2381 status = psa_hash_setup( &operation, alg );
2382 if( status != PSA_SUCCESS )
2383 goto exit;
2384 status = psa_hash_update( &operation, input, input_length );
2385 if( status != PSA_SUCCESS )
2386 goto exit;
2387 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2388 if( status != PSA_SUCCESS )
2389 goto exit;
2390
2391exit:
2392 if( status == PSA_SUCCESS )
2393 status = psa_hash_abort( &operation );
2394 else
2395 psa_hash_abort( &operation );
2396 return( status );
2397}
2398
2399psa_status_t psa_hash_compare( psa_algorithm_t alg,
2400 const uint8_t *input, size_t input_length,
2401 const uint8_t *hash, size_t hash_length )
2402{
2403 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2404 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2405
2406 status = psa_hash_setup( &operation, alg );
2407 if( status != PSA_SUCCESS )
2408 goto exit;
2409 status = psa_hash_update( &operation, input, input_length );
2410 if( status != PSA_SUCCESS )
2411 goto exit;
2412 status = psa_hash_verify( &operation, hash, hash_length );
2413 if( status != PSA_SUCCESS )
2414 goto exit;
2415
2416exit:
2417 if( status == PSA_SUCCESS )
2418 status = psa_hash_abort( &operation );
2419 else
2420 psa_hash_abort( &operation );
2421 return( status );
2422}
2423
Gilles Peskineeb35d782019-01-22 17:56:16 +01002424psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2425 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002426{
2427 if( target_operation->alg != 0 )
2428 return( PSA_ERROR_BAD_STATE );
2429
2430 switch( source_operation->alg )
2431 {
2432 case 0:
2433 return( PSA_ERROR_BAD_STATE );
2434#if defined(MBEDTLS_MD2_C)
2435 case PSA_ALG_MD2:
2436 mbedtls_md2_clone( &target_operation->ctx.md2,
2437 &source_operation->ctx.md2 );
2438 break;
2439#endif
2440#if defined(MBEDTLS_MD4_C)
2441 case PSA_ALG_MD4:
2442 mbedtls_md4_clone( &target_operation->ctx.md4,
2443 &source_operation->ctx.md4 );
2444 break;
2445#endif
2446#if defined(MBEDTLS_MD5_C)
2447 case PSA_ALG_MD5:
2448 mbedtls_md5_clone( &target_operation->ctx.md5,
2449 &source_operation->ctx.md5 );
2450 break;
2451#endif
2452#if defined(MBEDTLS_RIPEMD160_C)
2453 case PSA_ALG_RIPEMD160:
2454 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2455 &source_operation->ctx.ripemd160 );
2456 break;
2457#endif
2458#if defined(MBEDTLS_SHA1_C)
2459 case PSA_ALG_SHA_1:
2460 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2461 &source_operation->ctx.sha1 );
2462 break;
2463#endif
2464#if defined(MBEDTLS_SHA256_C)
2465 case PSA_ALG_SHA_224:
2466 case PSA_ALG_SHA_256:
2467 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2468 &source_operation->ctx.sha256 );
2469 break;
2470#endif
2471#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002472#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002473 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002474#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002475 case PSA_ALG_SHA_512:
2476 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2477 &source_operation->ctx.sha512 );
2478 break;
2479#endif
2480 default:
2481 return( PSA_ERROR_NOT_SUPPORTED );
2482 }
2483
2484 target_operation->alg = source_operation->alg;
2485 return( PSA_SUCCESS );
2486}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002487
2488
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002489/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002490/* MAC */
2491/****************************************************************/
2492
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002493static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002494 psa_algorithm_t alg,
2495 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002496 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002497 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002498{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002499 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002500 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002501
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002502 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002503 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002504
Gilles Peskine8c9def32018-02-08 10:02:12 +01002505 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2506 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002507 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002508 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002509 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002510 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002511 mode = MBEDTLS_MODE_STREAM;
2512 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002513 case PSA_ALG_CTR:
2514 mode = MBEDTLS_MODE_CTR;
2515 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002516 case PSA_ALG_CFB:
2517 mode = MBEDTLS_MODE_CFB;
2518 break;
2519 case PSA_ALG_OFB:
2520 mode = MBEDTLS_MODE_OFB;
2521 break;
2522 case PSA_ALG_CBC_NO_PADDING:
2523 mode = MBEDTLS_MODE_CBC;
2524 break;
2525 case PSA_ALG_CBC_PKCS7:
2526 mode = MBEDTLS_MODE_CBC;
2527 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002528 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002529 mode = MBEDTLS_MODE_CCM;
2530 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002531 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002532 mode = MBEDTLS_MODE_GCM;
2533 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002534 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2535 mode = MBEDTLS_MODE_CHACHAPOLY;
2536 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002537 default:
2538 return( NULL );
2539 }
2540 }
2541 else if( alg == PSA_ALG_CMAC )
2542 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002543 else
2544 return( NULL );
2545
2546 switch( key_type )
2547 {
2548 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002549 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002550 break;
2551 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002552 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2553 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002554 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002555 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002556 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002557 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002558 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2559 * but two-key Triple-DES is functionally three-key Triple-DES
2560 * with K1=K3, so that's how we present it to mbedtls. */
2561 if( key_bits == 128 )
2562 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002563 break;
2564 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002565 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002566 break;
2567 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002568 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002569 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002570 case PSA_KEY_TYPE_CHACHA20:
2571 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2572 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002573 default:
2574 return( NULL );
2575 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002576 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002577 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002578
Jaeden Amero23bbb752018-06-26 14:16:54 +01002579 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2580 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002581}
2582
Gilles Peskinea05219c2018-11-16 16:02:56 +01002583#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002584static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002585{
Gilles Peskine2d277862018-06-18 15:41:12 +02002586 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002587 {
2588 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002589 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002590 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002591 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002592 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002593 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002594 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002595 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002596 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002597 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002598 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002599 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002600 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002601 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002602 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002603 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002604 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002605 return( 128 );
2606 default:
2607 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002608 }
2609}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002610#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002611
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002612/* Initialize the MAC operation structure. Once this function has been
2613 * called, psa_mac_abort can run and will do the right thing. */
2614static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2615 psa_algorithm_t alg )
2616{
2617 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2618
2619 operation->alg = alg;
2620 operation->key_set = 0;
2621 operation->iv_set = 0;
2622 operation->iv_required = 0;
2623 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002624 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002625
2626#if defined(MBEDTLS_CMAC_C)
2627 if( alg == PSA_ALG_CMAC )
2628 {
2629 operation->iv_required = 0;
2630 mbedtls_cipher_init( &operation->ctx.cmac );
2631 status = PSA_SUCCESS;
2632 }
2633 else
2634#endif /* MBEDTLS_CMAC_C */
2635#if defined(MBEDTLS_MD_C)
2636 if( PSA_ALG_IS_HMAC( operation->alg ) )
2637 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002638 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2639 operation->ctx.hmac.hash_ctx.alg = 0;
2640 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002641 }
2642 else
2643#endif /* MBEDTLS_MD_C */
2644 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002645 if( ! PSA_ALG_IS_MAC( alg ) )
2646 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002647 }
2648
2649 if( status != PSA_SUCCESS )
2650 memset( operation, 0, sizeof( *operation ) );
2651 return( status );
2652}
2653
Gilles Peskine01126fa2018-07-12 17:04:55 +02002654#if defined(MBEDTLS_MD_C)
2655static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2656{
Gilles Peskine3f108122018-12-07 18:14:53 +01002657 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002658 return( psa_hash_abort( &hmac->hash_ctx ) );
2659}
2660#endif /* MBEDTLS_MD_C */
2661
Gilles Peskine8c9def32018-02-08 10:02:12 +01002662psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2663{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002664 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002665 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002666 /* The object has (apparently) been initialized but it is not
2667 * in use. It's ok to call abort on such an object, and there's
2668 * nothing to do. */
2669 return( PSA_SUCCESS );
2670 }
2671 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002672#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002673 if( operation->alg == PSA_ALG_CMAC )
2674 {
2675 mbedtls_cipher_free( &operation->ctx.cmac );
2676 }
2677 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002678#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002679#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002680 if( PSA_ALG_IS_HMAC( operation->alg ) )
2681 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002682 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002683 }
2684 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002685#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002686 {
2687 /* Sanity check (shouldn't happen: operation->alg should
2688 * always have been initialized to a valid value). */
2689 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002690 }
Moran Peker41deec42018-04-04 15:43:05 +03002691
Gilles Peskine8c9def32018-02-08 10:02:12 +01002692 operation->alg = 0;
2693 operation->key_set = 0;
2694 operation->iv_set = 0;
2695 operation->iv_required = 0;
2696 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002697 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002698
Gilles Peskine8c9def32018-02-08 10:02:12 +01002699 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002700
2701bad_state:
2702 /* If abort is called on an uninitialized object, we can't trust
2703 * anything. Wipe the object in case it contains confidential data.
2704 * This may result in a memory leak if a pointer gets overwritten,
2705 * but it's too late to do anything about this. */
2706 memset( operation, 0, sizeof( *operation ) );
2707 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002708}
2709
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002710#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002711static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002712 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002713 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002714 const mbedtls_cipher_info_t *cipher_info )
2715{
Janos Follath24eed8d2019-11-22 13:21:35 +00002716 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002717
2718 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002719
2720 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2721 if( ret != 0 )
2722 return( ret );
2723
2724 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2725 slot->data.raw.data,
2726 key_bits );
2727 return( ret );
2728}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002729#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002730
Gilles Peskine248051a2018-06-20 16:09:38 +02002731#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002732static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2733 const uint8_t *key,
2734 size_t key_length,
2735 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002736{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002737 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002738 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002739 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002740 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002741 psa_status_t status;
2742
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002743 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2744 * overflow below. This should never trigger if the hash algorithm
2745 * is implemented correctly. */
2746 /* The size checks against the ipad and opad buffers cannot be written
2747 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2748 * because that triggers -Wlogical-op on GCC 7.3. */
2749 if( block_size > sizeof( ipad ) )
2750 return( PSA_ERROR_NOT_SUPPORTED );
2751 if( block_size > sizeof( hmac->opad ) )
2752 return( PSA_ERROR_NOT_SUPPORTED );
2753 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002754 return( PSA_ERROR_NOT_SUPPORTED );
2755
Gilles Peskined223b522018-06-11 18:12:58 +02002756 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002757 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002758 status = psa_hash_compute( hash_alg, key, key_length,
2759 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002760 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002761 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002762 }
Gilles Peskine96889972018-07-12 17:07:03 +02002763 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2764 * but it is permitted. It is common when HMAC is used in HKDF, for
2765 * example. Don't call `memcpy` in the 0-length because `key` could be
2766 * an invalid pointer which would make the behavior undefined. */
2767 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002768 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002769
Gilles Peskined223b522018-06-11 18:12:58 +02002770 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2771 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002772 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002773 ipad[i] ^= 0x36;
2774 memset( ipad + key_length, 0x36, block_size - key_length );
2775
2776 /* Copy the key material from ipad to opad, flipping the requisite bits,
2777 * and filling the rest of opad with the requisite constant. */
2778 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002779 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2780 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002781
Gilles Peskine01126fa2018-07-12 17:04:55 +02002782 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002783 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002784 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002785
Gilles Peskine01126fa2018-07-12 17:04:55 +02002786 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002787
2788cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002789 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002790
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002791 return( status );
2792}
Gilles Peskine248051a2018-06-20 16:09:38 +02002793#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002794
Gilles Peskine89167cb2018-07-08 20:12:23 +02002795static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002796 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002797 psa_algorithm_t alg,
2798 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002799{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002800 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002801 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002802 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002803 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002804 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002805 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002806 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002807
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002808 /* A context must be freshly initialized before it can be set up. */
2809 if( operation->alg != 0 )
2810 {
2811 return( PSA_ERROR_BAD_STATE );
2812 }
2813
Gilles Peskined911eb72018-08-14 15:18:45 +02002814 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002815 if( status != PSA_SUCCESS )
2816 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002817 if( is_sign )
2818 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002819
Gilles Peskine28f8f302019-07-24 13:30:31 +02002820 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002821 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002822 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002823 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002824
Gilles Peskine8c9def32018-02-08 10:02:12 +01002825#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002826 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002827 {
2828 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002829 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002830 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002831 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002832 if( cipher_info == NULL )
2833 {
2834 status = PSA_ERROR_NOT_SUPPORTED;
2835 goto exit;
2836 }
2837 operation->mac_size = cipher_info->block_size;
2838 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2839 status = mbedtls_to_psa_error( ret );
2840 }
2841 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002842#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002843#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002844 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002845 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002846 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002847 if( hash_alg == 0 )
2848 {
2849 status = PSA_ERROR_NOT_SUPPORTED;
2850 goto exit;
2851 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002852
2853 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2854 /* Sanity check. This shouldn't fail on a valid configuration. */
2855 if( operation->mac_size == 0 ||
2856 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2857 {
2858 status = PSA_ERROR_NOT_SUPPORTED;
2859 goto exit;
2860 }
2861
Gilles Peskine8e338702019-07-30 20:06:31 +02002862 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002863 {
2864 status = PSA_ERROR_INVALID_ARGUMENT;
2865 goto exit;
2866 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002867
Gilles Peskine01126fa2018-07-12 17:04:55 +02002868 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2869 slot->data.raw.data,
2870 slot->data.raw.bytes,
2871 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002872 }
2873 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002874#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002875 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002876 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002877 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002878 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002879
Gilles Peskined911eb72018-08-14 15:18:45 +02002880 if( truncated == 0 )
2881 {
2882 /* The "normal" case: untruncated algorithm. Nothing to do. */
2883 }
2884 else if( truncated < 4 )
2885 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002886 /* A very short MAC is too short for security since it can be
2887 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2888 * so we make this our minimum, even though 32 bits is still
2889 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002890 status = PSA_ERROR_NOT_SUPPORTED;
2891 }
2892 else if( truncated > operation->mac_size )
2893 {
2894 /* It's impossible to "truncate" to a larger length. */
2895 status = PSA_ERROR_INVALID_ARGUMENT;
2896 }
2897 else
2898 operation->mac_size = truncated;
2899
Gilles Peskinefbfac682018-07-08 20:51:54 +02002900exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002901 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002902 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002903 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002904 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002905 else
2906 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002907 operation->key_set = 1;
2908 }
2909 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002910}
2911
Gilles Peskine89167cb2018-07-08 20:12:23 +02002912psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002913 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002914 psa_algorithm_t alg )
2915{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002916 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002917}
2918
2919psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002920 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002921 psa_algorithm_t alg )
2922{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002923 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002924}
2925
Gilles Peskine8c9def32018-02-08 10:02:12 +01002926psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2927 const uint8_t *input,
2928 size_t input_length )
2929{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002930 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002931 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002932 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002933 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002934 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002935 operation->has_input = 1;
2936
Gilles Peskine8c9def32018-02-08 10:02:12 +01002937#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002938 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002939 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002940 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2941 input, input_length );
2942 status = mbedtls_to_psa_error( ret );
2943 }
2944 else
2945#endif /* MBEDTLS_CMAC_C */
2946#if defined(MBEDTLS_MD_C)
2947 if( PSA_ALG_IS_HMAC( operation->alg ) )
2948 {
2949 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2950 input_length );
2951 }
2952 else
2953#endif /* MBEDTLS_MD_C */
2954 {
2955 /* This shouldn't happen if `operation` was initialized by
2956 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002957 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002958 }
2959
Gilles Peskinefbfac682018-07-08 20:51:54 +02002960 if( status != PSA_SUCCESS )
2961 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002962 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002963}
2964
Gilles Peskine01126fa2018-07-12 17:04:55 +02002965#if defined(MBEDTLS_MD_C)
2966static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2967 uint8_t *mac,
2968 size_t mac_size )
2969{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002970 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002971 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2972 size_t hash_size = 0;
2973 size_t block_size = psa_get_hash_block_size( hash_alg );
2974 psa_status_t status;
2975
Gilles Peskine01126fa2018-07-12 17:04:55 +02002976 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2977 if( status != PSA_SUCCESS )
2978 return( status );
2979 /* From here on, tmp needs to be wiped. */
2980
2981 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2982 if( status != PSA_SUCCESS )
2983 goto exit;
2984
2985 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2986 if( status != PSA_SUCCESS )
2987 goto exit;
2988
2989 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2990 if( status != PSA_SUCCESS )
2991 goto exit;
2992
Gilles Peskined911eb72018-08-14 15:18:45 +02002993 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2994 if( status != PSA_SUCCESS )
2995 goto exit;
2996
2997 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002998
2999exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003000 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003001 return( status );
3002}
3003#endif /* MBEDTLS_MD_C */
3004
mohammad16036df908f2018-04-02 08:34:15 -07003005static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003006 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003007 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003008{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003009 if( ! operation->key_set )
3010 return( PSA_ERROR_BAD_STATE );
3011 if( operation->iv_required && ! operation->iv_set )
3012 return( PSA_ERROR_BAD_STATE );
3013
Gilles Peskine8c9def32018-02-08 10:02:12 +01003014 if( mac_size < operation->mac_size )
3015 return( PSA_ERROR_BUFFER_TOO_SMALL );
3016
Gilles Peskine8c9def32018-02-08 10:02:12 +01003017#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003018 if( operation->alg == PSA_ALG_CMAC )
3019 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003020 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3021 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3022 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003023 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003024 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003025 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003026 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003027 else
3028#endif /* MBEDTLS_CMAC_C */
3029#if defined(MBEDTLS_MD_C)
3030 if( PSA_ALG_IS_HMAC( operation->alg ) )
3031 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003032 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003033 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003034 }
3035 else
3036#endif /* MBEDTLS_MD_C */
3037 {
3038 /* This shouldn't happen if `operation` was initialized by
3039 * a setup function. */
3040 return( PSA_ERROR_BAD_STATE );
3041 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003042}
3043
Gilles Peskineacd4be32018-07-08 19:56:25 +02003044psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3045 uint8_t *mac,
3046 size_t mac_size,
3047 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003048{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003049 psa_status_t status;
3050
Jaeden Amero252ef282019-02-15 14:05:35 +00003051 if( operation->alg == 0 )
3052 {
3053 return( PSA_ERROR_BAD_STATE );
3054 }
3055
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003056 /* Fill the output buffer with something that isn't a valid mac
3057 * (barring an attack on the mac and deliberately-crafted input),
3058 * in case the caller doesn't check the return status properly. */
3059 *mac_length = mac_size;
3060 /* If mac_size is 0 then mac may be NULL and then the
3061 * call to memset would have undefined behavior. */
3062 if( mac_size != 0 )
3063 memset( mac, '!', mac_size );
3064
Gilles Peskine89167cb2018-07-08 20:12:23 +02003065 if( ! operation->is_sign )
3066 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003067 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003068 }
mohammad16036df908f2018-04-02 08:34:15 -07003069
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003070 status = psa_mac_finish_internal( operation, mac, mac_size );
3071
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003072 if( status == PSA_SUCCESS )
3073 {
3074 status = psa_mac_abort( operation );
3075 if( status == PSA_SUCCESS )
3076 *mac_length = operation->mac_size;
3077 else
3078 memset( mac, '!', mac_size );
3079 }
3080 else
3081 psa_mac_abort( operation );
3082 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003083}
3084
Gilles Peskineacd4be32018-07-08 19:56:25 +02003085psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3086 const uint8_t *mac,
3087 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003088{
Gilles Peskine828ed142018-06-18 23:25:51 +02003089 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003090 psa_status_t status;
3091
Jaeden Amero252ef282019-02-15 14:05:35 +00003092 if( operation->alg == 0 )
3093 {
3094 return( PSA_ERROR_BAD_STATE );
3095 }
3096
Gilles Peskine89167cb2018-07-08 20:12:23 +02003097 if( operation->is_sign )
3098 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003099 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003100 }
3101 if( operation->mac_size != mac_length )
3102 {
3103 status = PSA_ERROR_INVALID_SIGNATURE;
3104 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003105 }
mohammad16036df908f2018-04-02 08:34:15 -07003106
3107 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003108 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003109 if( status != PSA_SUCCESS )
3110 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003111
3112 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3113 status = PSA_ERROR_INVALID_SIGNATURE;
3114
3115cleanup:
3116 if( status == PSA_SUCCESS )
3117 status = psa_mac_abort( operation );
3118 else
3119 psa_mac_abort( operation );
3120
Gilles Peskine3f108122018-12-07 18:14:53 +01003121 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003122
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003123 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003124}
3125
3126
Gilles Peskine20035e32018-02-03 22:44:14 +01003127
Gilles Peskine20035e32018-02-03 22:44:14 +01003128/****************************************************************/
3129/* Asymmetric cryptography */
3130/****************************************************************/
3131
Gilles Peskine2b450e32018-06-27 15:42:46 +02003132#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003133/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003134 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003135static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3136 size_t hash_length,
3137 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003138{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003139 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003140 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003141 *md_alg = mbedtls_md_get_type( md_info );
3142
3143 /* The Mbed TLS RSA module uses an unsigned int for hash length
3144 * parameters. Validate that it fits so that we don't risk an
3145 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003146#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003147 if( hash_length > UINT_MAX )
3148 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003149#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003150
3151#if defined(MBEDTLS_PKCS1_V15)
3152 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3153 * must be correct. */
3154 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3155 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003156 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003157 if( md_info == NULL )
3158 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003159 if( mbedtls_md_get_size( md_info ) != hash_length )
3160 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003161 }
3162#endif /* MBEDTLS_PKCS1_V15 */
3163
3164#if defined(MBEDTLS_PKCS1_V21)
3165 /* PSS requires a hash internally. */
3166 if( PSA_ALG_IS_RSA_PSS( alg ) )
3167 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003168 if( md_info == NULL )
3169 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003170 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003171#endif /* MBEDTLS_PKCS1_V21 */
3172
Gilles Peskine61b91d42018-06-08 16:09:36 +02003173 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003174}
3175
Gilles Peskine2b450e32018-06-27 15:42:46 +02003176static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3177 psa_algorithm_t alg,
3178 const uint8_t *hash,
3179 size_t hash_length,
3180 uint8_t *signature,
3181 size_t signature_size,
3182 size_t *signature_length )
3183{
3184 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003185 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003186 mbedtls_md_type_t md_alg;
3187
3188 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3189 if( status != PSA_SUCCESS )
3190 return( status );
3191
Gilles Peskine630a18a2018-06-29 17:49:35 +02003192 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003193 return( PSA_ERROR_BUFFER_TOO_SMALL );
3194
3195#if defined(MBEDTLS_PKCS1_V15)
3196 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3197 {
3198 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3199 MBEDTLS_MD_NONE );
3200 ret = mbedtls_rsa_pkcs1_sign( rsa,
3201 mbedtls_ctr_drbg_random,
3202 &global_data.ctr_drbg,
3203 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003204 md_alg,
3205 (unsigned int) hash_length,
3206 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003207 signature );
3208 }
3209 else
3210#endif /* MBEDTLS_PKCS1_V15 */
3211#if defined(MBEDTLS_PKCS1_V21)
3212 if( PSA_ALG_IS_RSA_PSS( alg ) )
3213 {
3214 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3215 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3216 mbedtls_ctr_drbg_random,
3217 &global_data.ctr_drbg,
3218 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003219 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003220 (unsigned int) hash_length,
3221 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003222 signature );
3223 }
3224 else
3225#endif /* MBEDTLS_PKCS1_V21 */
3226 {
3227 return( PSA_ERROR_INVALID_ARGUMENT );
3228 }
3229
3230 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003231 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003232 return( mbedtls_to_psa_error( ret ) );
3233}
3234
3235static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3236 psa_algorithm_t alg,
3237 const uint8_t *hash,
3238 size_t hash_length,
3239 const uint8_t *signature,
3240 size_t signature_length )
3241{
3242 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003243 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003244 mbedtls_md_type_t md_alg;
3245
3246 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3247 if( status != PSA_SUCCESS )
3248 return( status );
3249
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003250 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3251 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003252
3253#if defined(MBEDTLS_PKCS1_V15)
3254 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3255 {
3256 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3257 MBEDTLS_MD_NONE );
3258 ret = mbedtls_rsa_pkcs1_verify( rsa,
3259 mbedtls_ctr_drbg_random,
3260 &global_data.ctr_drbg,
3261 MBEDTLS_RSA_PUBLIC,
3262 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003263 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003264 hash,
3265 signature );
3266 }
3267 else
3268#endif /* MBEDTLS_PKCS1_V15 */
3269#if defined(MBEDTLS_PKCS1_V21)
3270 if( PSA_ALG_IS_RSA_PSS( alg ) )
3271 {
3272 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3273 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3274 mbedtls_ctr_drbg_random,
3275 &global_data.ctr_drbg,
3276 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003277 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003278 (unsigned int) hash_length,
3279 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003280 signature );
3281 }
3282 else
3283#endif /* MBEDTLS_PKCS1_V21 */
3284 {
3285 return( PSA_ERROR_INVALID_ARGUMENT );
3286 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003287
3288 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3289 * the rest of the signature is invalid". This has little use in
3290 * practice and PSA doesn't report this distinction. */
3291 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3292 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003293 return( mbedtls_to_psa_error( ret ) );
3294}
3295#endif /* MBEDTLS_RSA_C */
3296
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003297#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003298/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3299 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3300 * (even though these functions don't modify it). */
3301static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3302 psa_algorithm_t alg,
3303 const uint8_t *hash,
3304 size_t hash_length,
3305 uint8_t *signature,
3306 size_t signature_size,
3307 size_t *signature_length )
3308{
Janos Follath24eed8d2019-11-22 13:21:35 +00003309 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003310 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003311 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003312 mbedtls_mpi_init( &r );
3313 mbedtls_mpi_init( &s );
3314
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003315 if( signature_size < 2 * curve_bytes )
3316 {
3317 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3318 goto cleanup;
3319 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003320
Gilles Peskinea05219c2018-11-16 16:02:56 +01003321#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003322 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3323 {
3324 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3325 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3326 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003327 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3328 &ecp->d, hash,
3329 hash_length, md_alg,
3330 mbedtls_ctr_drbg_random,
3331 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003332 }
3333 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003334#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003335 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003336 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003337 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3338 hash, hash_length,
3339 mbedtls_ctr_drbg_random,
3340 &global_data.ctr_drbg ) );
3341 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003342
3343 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3344 signature,
3345 curve_bytes ) );
3346 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3347 signature + curve_bytes,
3348 curve_bytes ) );
3349
3350cleanup:
3351 mbedtls_mpi_free( &r );
3352 mbedtls_mpi_free( &s );
3353 if( ret == 0 )
3354 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003355 return( mbedtls_to_psa_error( ret ) );
3356}
3357
3358static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3359 const uint8_t *hash,
3360 size_t hash_length,
3361 const uint8_t *signature,
3362 size_t signature_length )
3363{
Janos Follath24eed8d2019-11-22 13:21:35 +00003364 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003365 mbedtls_mpi r, s;
3366 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3367 mbedtls_mpi_init( &r );
3368 mbedtls_mpi_init( &s );
3369
3370 if( signature_length != 2 * curve_bytes )
3371 return( PSA_ERROR_INVALID_SIGNATURE );
3372
3373 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3374 signature,
3375 curve_bytes ) );
3376 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3377 signature + curve_bytes,
3378 curve_bytes ) );
3379
3380 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3381 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003382
3383cleanup:
3384 mbedtls_mpi_free( &r );
3385 mbedtls_mpi_free( &s );
3386 return( mbedtls_to_psa_error( ret ) );
3387}
3388#endif /* MBEDTLS_ECDSA_C */
3389
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003390psa_status_t psa_sign_hash( psa_key_handle_t handle,
3391 psa_algorithm_t alg,
3392 const uint8_t *hash,
3393 size_t hash_length,
3394 uint8_t *signature,
3395 size_t signature_size,
3396 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003397{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003398 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003399 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003400#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3401 const psa_drv_se_t *drv;
3402 psa_drv_se_context_t *drv_context;
3403#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003404
3405 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003406 /* Immediately reject a zero-length signature buffer. This guarantees
3407 * that signature must be a valid pointer. (On the other hand, the hash
3408 * buffer can in principle be empty since it doesn't actually have
3409 * to be a hash.) */
3410 if( signature_size == 0 )
3411 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003412
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003413 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003414 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003415 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003416 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003417 {
3418 status = PSA_ERROR_INVALID_ARGUMENT;
3419 goto exit;
3420 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003421
Gilles Peskineedc64242019-08-07 21:05:07 +02003422#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3423 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3424 {
3425 if( drv->asymmetric == NULL ||
3426 drv->asymmetric->p_sign == NULL )
3427 {
3428 status = PSA_ERROR_NOT_SUPPORTED;
3429 goto exit;
3430 }
3431 status = drv->asymmetric->p_sign( drv_context,
3432 slot->data.se.slot_number,
3433 alg,
3434 hash, hash_length,
3435 signature, signature_size,
3436 signature_length );
3437 }
3438 else
3439#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003440#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003441 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003442 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003443 status = psa_rsa_sign( slot->data.rsa,
3444 alg,
3445 hash, hash_length,
3446 signature, signature_size,
3447 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003448 }
3449 else
3450#endif /* defined(MBEDTLS_RSA_C) */
3451#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003452 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003453 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003454#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003455 if(
3456#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3457 PSA_ALG_IS_ECDSA( alg )
3458#else
3459 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3460#endif
3461 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003462 status = psa_ecdsa_sign( slot->data.ecp,
3463 alg,
3464 hash, hash_length,
3465 signature, signature_size,
3466 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003467 else
3468#endif /* defined(MBEDTLS_ECDSA_C) */
3469 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003470 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003471 }
itayzafrir5c753392018-05-08 11:18:38 +03003472 }
3473 else
3474#endif /* defined(MBEDTLS_ECP_C) */
3475 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003476 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003477 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003478
3479exit:
3480 /* Fill the unused part of the output buffer (the whole buffer on error,
3481 * the trailing part on success) with something that isn't a valid mac
3482 * (barring an attack on the mac and deliberately-crafted input),
3483 * in case the caller doesn't check the return status properly. */
3484 if( status == PSA_SUCCESS )
3485 memset( signature + *signature_length, '!',
3486 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003487 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003488 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003489 /* If signature_size is 0 then we have nothing to do. We must not call
3490 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003491 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003492}
3493
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003494psa_status_t psa_verify_hash( psa_key_handle_t handle,
3495 psa_algorithm_t alg,
3496 const uint8_t *hash,
3497 size_t hash_length,
3498 const uint8_t *signature,
3499 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003500{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003501 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003502 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003503#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3504 const psa_drv_se_t *drv;
3505 psa_drv_se_context_t *drv_context;
3506#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003507
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003508 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003509 if( status != PSA_SUCCESS )
3510 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003511
Gilles Peskineedc64242019-08-07 21:05:07 +02003512#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3513 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3514 {
3515 if( drv->asymmetric == NULL ||
3516 drv->asymmetric->p_verify == NULL )
3517 return( PSA_ERROR_NOT_SUPPORTED );
3518 return( drv->asymmetric->p_verify( drv_context,
3519 slot->data.se.slot_number,
3520 alg,
3521 hash, hash_length,
3522 signature, signature_length ) );
3523 }
3524 else
3525#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003526#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003527 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003528 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003529 return( psa_rsa_verify( slot->data.rsa,
3530 alg,
3531 hash, hash_length,
3532 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003533 }
3534 else
3535#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003536#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003537 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003538 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003539#if defined(MBEDTLS_ECDSA_C)
3540 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003541 return( psa_ecdsa_verify( slot->data.ecp,
3542 hash, hash_length,
3543 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003544 else
3545#endif /* defined(MBEDTLS_ECDSA_C) */
3546 {
3547 return( PSA_ERROR_INVALID_ARGUMENT );
3548 }
itayzafrir5c753392018-05-08 11:18:38 +03003549 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003550 else
3551#endif /* defined(MBEDTLS_ECP_C) */
3552 {
3553 return( PSA_ERROR_NOT_SUPPORTED );
3554 }
3555}
3556
Gilles Peskine072ac562018-06-30 00:21:29 +02003557#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3558static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3559 mbedtls_rsa_context *rsa )
3560{
3561 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3562 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3563 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3564 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3565}
3566#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3567
Gilles Peskinec5487a82018-12-03 18:08:14 +01003568psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003569 psa_algorithm_t alg,
3570 const uint8_t *input,
3571 size_t input_length,
3572 const uint8_t *salt,
3573 size_t salt_length,
3574 uint8_t *output,
3575 size_t output_size,
3576 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003577{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003578 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003579 psa_status_t status;
3580
Darryl Green5cc689a2018-07-24 15:34:10 +01003581 (void) input;
3582 (void) input_length;
3583 (void) salt;
3584 (void) output;
3585 (void) output_size;
3586
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003587 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003588
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003589 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3590 return( PSA_ERROR_INVALID_ARGUMENT );
3591
Gilles Peskine28f8f302019-07-24 13:30:31 +02003592 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003593 if( status != PSA_SUCCESS )
3594 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003595 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3596 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003597 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003598
3599#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003600 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003601 {
3602 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003603 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003604 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003605 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003606#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003607 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003608 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003609 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3610 mbedtls_ctr_drbg_random,
3611 &global_data.ctr_drbg,
3612 MBEDTLS_RSA_PUBLIC,
3613 input_length,
3614 input,
3615 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003616 }
3617 else
3618#endif /* MBEDTLS_PKCS1_V15 */
3619#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003620 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003621 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003622 psa_rsa_oaep_set_padding_mode( alg, rsa );
3623 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3624 mbedtls_ctr_drbg_random,
3625 &global_data.ctr_drbg,
3626 MBEDTLS_RSA_PUBLIC,
3627 salt, salt_length,
3628 input_length,
3629 input,
3630 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003631 }
3632 else
3633#endif /* MBEDTLS_PKCS1_V21 */
3634 {
3635 return( PSA_ERROR_INVALID_ARGUMENT );
3636 }
3637 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003638 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003639 return( mbedtls_to_psa_error( ret ) );
3640 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003641 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003642#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003643 {
3644 return( PSA_ERROR_NOT_SUPPORTED );
3645 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003646}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003647
Gilles Peskinec5487a82018-12-03 18:08:14 +01003648psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003649 psa_algorithm_t alg,
3650 const uint8_t *input,
3651 size_t input_length,
3652 const uint8_t *salt,
3653 size_t salt_length,
3654 uint8_t *output,
3655 size_t output_size,
3656 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003657{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003658 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003659 psa_status_t status;
3660
Darryl Green5cc689a2018-07-24 15:34:10 +01003661 (void) input;
3662 (void) input_length;
3663 (void) salt;
3664 (void) output;
3665 (void) output_size;
3666
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003667 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003668
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003669 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3670 return( PSA_ERROR_INVALID_ARGUMENT );
3671
Gilles Peskine28f8f302019-07-24 13:30:31 +02003672 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003673 if( status != PSA_SUCCESS )
3674 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003675 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003676 return( PSA_ERROR_INVALID_ARGUMENT );
3677
3678#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003679 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003680 {
3681 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003682 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003683
Gilles Peskine630a18a2018-06-29 17:49:35 +02003684 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003685 return( PSA_ERROR_INVALID_ARGUMENT );
3686
3687#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003688 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003689 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003690 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3691 mbedtls_ctr_drbg_random,
3692 &global_data.ctr_drbg,
3693 MBEDTLS_RSA_PRIVATE,
3694 output_length,
3695 input,
3696 output,
3697 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003698 }
3699 else
3700#endif /* MBEDTLS_PKCS1_V15 */
3701#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003702 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003703 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003704 psa_rsa_oaep_set_padding_mode( alg, rsa );
3705 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3706 mbedtls_ctr_drbg_random,
3707 &global_data.ctr_drbg,
3708 MBEDTLS_RSA_PRIVATE,
3709 salt, salt_length,
3710 output_length,
3711 input,
3712 output,
3713 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003714 }
3715 else
3716#endif /* MBEDTLS_PKCS1_V21 */
3717 {
3718 return( PSA_ERROR_INVALID_ARGUMENT );
3719 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003720
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003721 return( mbedtls_to_psa_error( ret ) );
3722 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003723 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003724#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003725 {
3726 return( PSA_ERROR_NOT_SUPPORTED );
3727 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003728}
Gilles Peskine20035e32018-02-03 22:44:14 +01003729
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003730
3731
mohammad1603503973b2018-03-12 15:59:30 +02003732/****************************************************************/
3733/* Symmetric cryptography */
3734/****************************************************************/
3735
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003736/* Initialize the cipher operation structure. Once this function has been
3737 * called, psa_cipher_abort can run and will do the right thing. */
3738static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3739 psa_algorithm_t alg )
3740{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003741 if( ! PSA_ALG_IS_CIPHER( alg ) )
3742 {
3743 memset( operation, 0, sizeof( *operation ) );
3744 return( PSA_ERROR_INVALID_ARGUMENT );
3745 }
3746
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003747 operation->alg = alg;
3748 operation->key_set = 0;
3749 operation->iv_set = 0;
3750 operation->iv_required = 1;
3751 operation->iv_size = 0;
3752 operation->block_size = 0;
3753 mbedtls_cipher_init( &operation->ctx.cipher );
3754 return( PSA_SUCCESS );
3755}
3756
Gilles Peskinee553c652018-06-04 16:22:46 +02003757static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003758 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003759 psa_algorithm_t alg,
3760 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003761{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003762 int ret = 0;
3763 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003764 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003765 size_t key_bits;
3766 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003767 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3768 PSA_KEY_USAGE_ENCRYPT :
3769 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003770
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003771 /* A context must be freshly initialized before it can be set up. */
3772 if( operation->alg != 0 )
3773 {
3774 return( PSA_ERROR_BAD_STATE );
3775 }
3776
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003777 status = psa_cipher_init( operation, alg );
3778 if( status != PSA_SUCCESS )
3779 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003780
Gilles Peskine28f8f302019-07-24 13:30:31 +02003781 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003782 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003783 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003784 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003785
Gilles Peskine8e338702019-07-30 20:06:31 +02003786 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003787 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003788 {
3789 status = PSA_ERROR_NOT_SUPPORTED;
3790 goto exit;
3791 }
mohammad1603503973b2018-03-12 15:59:30 +02003792
mohammad1603503973b2018-03-12 15:59:30 +02003793 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003794 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003795 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003796
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003797#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003798 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003799 {
3800 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003801 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003802 memcpy( keys, slot->data.raw.data, 16 );
3803 memcpy( keys + 16, slot->data.raw.data, 8 );
3804 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3805 keys,
3806 192, cipher_operation );
3807 }
3808 else
3809#endif
3810 {
3811 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3812 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003813 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003814 }
Moran Peker41deec42018-04-04 15:43:05 +03003815 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003816 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003817
mohammad16038481e742018-03-18 13:57:31 +02003818#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003819 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003820 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003821 case PSA_ALG_CBC_NO_PADDING:
3822 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3823 MBEDTLS_PADDING_NONE );
3824 break;
3825 case PSA_ALG_CBC_PKCS7:
3826 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3827 MBEDTLS_PADDING_PKCS7 );
3828 break;
3829 default:
3830 /* The algorithm doesn't involve padding. */
3831 ret = 0;
3832 break;
3833 }
3834 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003835 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003836#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3837
mohammad1603503973b2018-03-12 15:59:30 +02003838 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003839 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003840 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003841 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003842 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003843 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003844 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003845#if defined(MBEDTLS_CHACHA20_C)
3846 else
3847 if( alg == PSA_ALG_CHACHA20 )
3848 operation->iv_size = 12;
3849#endif
mohammad1603503973b2018-03-12 15:59:30 +02003850
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003851exit:
3852 if( status == 0 )
3853 status = mbedtls_to_psa_error( ret );
3854 if( status != 0 )
3855 psa_cipher_abort( operation );
3856 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003857}
3858
Gilles Peskinefe119512018-07-08 21:39:34 +02003859psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003860 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003861 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003862{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003863 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003864}
3865
Gilles Peskinefe119512018-07-08 21:39:34 +02003866psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003867 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003868 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003869{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003870 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003871}
3872
Gilles Peskinefe119512018-07-08 21:39:34 +02003873psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003874 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003875 size_t iv_size,
3876 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003877{
itayzafrir534bd7c2018-08-02 13:56:32 +03003878 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003879 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003880 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003881 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003882 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003883 }
Moran Peker41deec42018-04-04 15:43:05 +03003884 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003885 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003886 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003887 goto exit;
3888 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003889 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3890 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003891 if( ret != 0 )
3892 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003893 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003894 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003895 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003896
mohammad16038481e742018-03-18 13:57:31 +02003897 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003898 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003899
Moran Peker395db872018-05-31 14:07:14 +03003900exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003901 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003902 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003903 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003904}
3905
Gilles Peskinefe119512018-07-08 21:39:34 +02003906psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003907 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003908 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003909{
itayzafrir534bd7c2018-08-02 13:56:32 +03003910 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003911 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003912 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003913 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003914 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003915 }
Moran Pekera28258c2018-05-29 16:25:04 +03003916 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003917 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003918 status = PSA_ERROR_INVALID_ARGUMENT;
3919 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003920 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003921 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3922 status = mbedtls_to_psa_error( ret );
3923exit:
3924 if( status == PSA_SUCCESS )
3925 operation->iv_set = 1;
3926 else
mohammad1603503973b2018-03-12 15:59:30 +02003927 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003928 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003929}
3930
Gilles Peskinee553c652018-06-04 16:22:46 +02003931psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3932 const uint8_t *input,
3933 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003934 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003935 size_t output_size,
3936 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003937{
itayzafrir534bd7c2018-08-02 13:56:32 +03003938 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003939 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003940 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003941
3942 if( operation->alg == 0 )
3943 {
3944 return( PSA_ERROR_BAD_STATE );
3945 }
3946
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003947 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003948 {
3949 /* Take the unprocessed partial block left over from previous
3950 * update calls, if any, plus the input to this call. Remove
3951 * the last partial block, if any. You get the data that will be
3952 * output in this call. */
3953 expected_output_size =
3954 ( operation->ctx.cipher.unprocessed_len + input_length )
3955 / operation->block_size * operation->block_size;
3956 }
3957 else
3958 {
3959 expected_output_size = input_length;
3960 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003961
Gilles Peskine89d789c2018-06-04 17:17:16 +02003962 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003963 {
3964 status = PSA_ERROR_BUFFER_TOO_SMALL;
3965 goto exit;
3966 }
mohammad160382759612018-03-12 18:16:40 +02003967
mohammad1603503973b2018-03-12 15:59:30 +02003968 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003969 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003970 status = mbedtls_to_psa_error( ret );
3971exit:
3972 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003973 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003974 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003975}
3976
Gilles Peskinee553c652018-06-04 16:22:46 +02003977psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3978 uint8_t *output,
3979 size_t output_size,
3980 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003981{
David Saadab4ecc272019-02-14 13:48:10 +02003982 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003983 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003984 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003985
mohammad1603503973b2018-03-12 15:59:30 +02003986 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003987 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003988 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003989 }
3990 if( operation->iv_required && ! operation->iv_set )
3991 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003992 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003993 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003994
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003995 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003996 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3997 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003998 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003999 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004000 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004001 }
4002
Janos Follath315b51c2018-07-09 16:04:51 +01004003 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4004 temp_output_buffer,
4005 output_length );
4006 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004007 {
Janos Follath315b51c2018-07-09 16:04:51 +01004008 status = mbedtls_to_psa_error( cipher_ret );
4009 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004010 }
Janos Follath315b51c2018-07-09 16:04:51 +01004011
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004012 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004013 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004014 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004015 memcpy( output, temp_output_buffer, *output_length );
4016 else
4017 {
Janos Follath315b51c2018-07-09 16:04:51 +01004018 status = PSA_ERROR_BUFFER_TOO_SMALL;
4019 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004020 }
mohammad1603503973b2018-03-12 15:59:30 +02004021
Gilles Peskine3f108122018-12-07 18:14:53 +01004022 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004023 status = psa_cipher_abort( operation );
4024
4025 return( status );
4026
4027error:
4028
4029 *output_length = 0;
4030
Gilles Peskine3f108122018-12-07 18:14:53 +01004031 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004032 (void) psa_cipher_abort( operation );
4033
4034 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004035}
4036
Gilles Peskinee553c652018-06-04 16:22:46 +02004037psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4038{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004039 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004040 {
4041 /* The object has (apparently) been initialized but it is not
4042 * in use. It's ok to call abort on such an object, and there's
4043 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004044 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004045 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004046
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004047 /* Sanity check (shouldn't happen: operation->alg should
4048 * always have been initialized to a valid value). */
4049 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4050 return( PSA_ERROR_BAD_STATE );
4051
mohammad1603503973b2018-03-12 15:59:30 +02004052 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004053
Moran Peker41deec42018-04-04 15:43:05 +03004054 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004055 operation->key_set = 0;
4056 operation->iv_set = 0;
4057 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004058 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004059 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004060
Moran Peker395db872018-05-31 14:07:14 +03004061 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004062}
4063
Gilles Peskinea0655c32018-04-30 17:06:50 +02004064
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004065
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004066
mohammad16035955c982018-04-26 00:53:03 +03004067/****************************************************************/
4068/* AEAD */
4069/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004070
Gilles Peskineedf9a652018-08-17 18:11:56 +02004071typedef struct
4072{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004073 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004074 const mbedtls_cipher_info_t *cipher_info;
4075 union
4076 {
4077#if defined(MBEDTLS_CCM_C)
4078 mbedtls_ccm_context ccm;
4079#endif /* MBEDTLS_CCM_C */
4080#if defined(MBEDTLS_GCM_C)
4081 mbedtls_gcm_context gcm;
4082#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004083#if defined(MBEDTLS_CHACHAPOLY_C)
4084 mbedtls_chachapoly_context chachapoly;
4085#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004086 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004087 psa_algorithm_t core_alg;
4088 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004089 uint8_t tag_length;
4090} aead_operation_t;
4091
Gilles Peskine30a9e412019-01-14 18:36:12 +01004092static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004093{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004094 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004095 {
4096#if defined(MBEDTLS_CCM_C)
4097 case PSA_ALG_CCM:
4098 mbedtls_ccm_free( &operation->ctx.ccm );
4099 break;
4100#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004101#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004102 case PSA_ALG_GCM:
4103 mbedtls_gcm_free( &operation->ctx.gcm );
4104 break;
4105#endif /* MBEDTLS_GCM_C */
4106 }
4107}
4108
4109static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004110 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004111 psa_key_usage_t usage,
4112 psa_algorithm_t alg )
4113{
4114 psa_status_t status;
4115 size_t key_bits;
4116 mbedtls_cipher_id_t cipher_id;
4117
Gilles Peskine28f8f302019-07-24 13:30:31 +02004118 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004119 if( status != PSA_SUCCESS )
4120 return( status );
4121
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004122 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004123
4124 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004125 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004126 &cipher_id );
4127 if( operation->cipher_info == NULL )
4128 return( PSA_ERROR_NOT_SUPPORTED );
4129
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004130 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004131 {
4132#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004133 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4134 operation->core_alg = PSA_ALG_CCM;
4135 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004136 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4137 * The call to mbedtls_ccm_encrypt_and_tag or
4138 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004139 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004140 return( PSA_ERROR_INVALID_ARGUMENT );
4141 mbedtls_ccm_init( &operation->ctx.ccm );
4142 status = mbedtls_to_psa_error(
4143 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4144 operation->slot->data.raw.data,
4145 (unsigned int) key_bits ) );
4146 if( status != 0 )
4147 goto cleanup;
4148 break;
4149#endif /* MBEDTLS_CCM_C */
4150
4151#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004152 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4153 operation->core_alg = PSA_ALG_GCM;
4154 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004155 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4156 * The call to mbedtls_gcm_crypt_and_tag or
4157 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004158 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004159 return( PSA_ERROR_INVALID_ARGUMENT );
4160 mbedtls_gcm_init( &operation->ctx.gcm );
4161 status = mbedtls_to_psa_error(
4162 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4163 operation->slot->data.raw.data,
4164 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004165 if( status != 0 )
4166 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004167 break;
4168#endif /* MBEDTLS_GCM_C */
4169
Gilles Peskine26869f22019-05-06 15:25:00 +02004170#if defined(MBEDTLS_CHACHAPOLY_C)
4171 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4172 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4173 operation->full_tag_length = 16;
4174 /* We only support the default tag length. */
4175 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4176 return( PSA_ERROR_NOT_SUPPORTED );
4177 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4178 status = mbedtls_to_psa_error(
4179 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4180 operation->slot->data.raw.data ) );
4181 if( status != 0 )
4182 goto cleanup;
4183 break;
4184#endif /* MBEDTLS_CHACHAPOLY_C */
4185
Gilles Peskineedf9a652018-08-17 18:11:56 +02004186 default:
4187 return( PSA_ERROR_NOT_SUPPORTED );
4188 }
4189
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004190 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4191 {
4192 status = PSA_ERROR_INVALID_ARGUMENT;
4193 goto cleanup;
4194 }
4195 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004196
Gilles Peskineedf9a652018-08-17 18:11:56 +02004197 return( PSA_SUCCESS );
4198
4199cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004200 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004201 return( status );
4202}
4203
Gilles Peskinec5487a82018-12-03 18:08:14 +01004204psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004205 psa_algorithm_t alg,
4206 const uint8_t *nonce,
4207 size_t nonce_length,
4208 const uint8_t *additional_data,
4209 size_t additional_data_length,
4210 const uint8_t *plaintext,
4211 size_t plaintext_length,
4212 uint8_t *ciphertext,
4213 size_t ciphertext_size,
4214 size_t *ciphertext_length )
4215{
mohammad16035955c982018-04-26 00:53:03 +03004216 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004217 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004218 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004219
mohammad1603f08a5502018-06-03 15:05:47 +03004220 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004221
Gilles Peskinec5487a82018-12-03 18:08:14 +01004222 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004223 if( status != PSA_SUCCESS )
4224 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004225
Gilles Peskineedf9a652018-08-17 18:11:56 +02004226 /* For all currently supported modes, the tag is at the end of the
4227 * ciphertext. */
4228 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4229 {
4230 status = PSA_ERROR_BUFFER_TOO_SMALL;
4231 goto exit;
4232 }
4233 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004234
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004235#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004236 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004237 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004238 status = mbedtls_to_psa_error(
4239 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4240 MBEDTLS_GCM_ENCRYPT,
4241 plaintext_length,
4242 nonce, nonce_length,
4243 additional_data, additional_data_length,
4244 plaintext, ciphertext,
4245 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004246 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004247 else
4248#endif /* MBEDTLS_GCM_C */
4249#if defined(MBEDTLS_CCM_C)
4250 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004251 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004252 status = mbedtls_to_psa_error(
4253 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4254 plaintext_length,
4255 nonce, nonce_length,
4256 additional_data,
4257 additional_data_length,
4258 plaintext, ciphertext,
4259 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004260 }
mohammad16035c8845f2018-05-09 05:40:09 -07004261 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004262#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004263#if defined(MBEDTLS_CHACHAPOLY_C)
4264 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4265 {
4266 if( nonce_length != 12 || operation.tag_length != 16 )
4267 {
4268 status = PSA_ERROR_NOT_SUPPORTED;
4269 goto exit;
4270 }
4271 status = mbedtls_to_psa_error(
4272 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4273 plaintext_length,
4274 nonce,
4275 additional_data,
4276 additional_data_length,
4277 plaintext,
4278 ciphertext,
4279 tag ) );
4280 }
4281 else
4282#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004283 {
mohammad1603554faad2018-06-03 15:07:38 +03004284 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004285 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004286
Gilles Peskineedf9a652018-08-17 18:11:56 +02004287 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4288 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004289
Gilles Peskineedf9a652018-08-17 18:11:56 +02004290exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004291 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004292 if( status == PSA_SUCCESS )
4293 *ciphertext_length = plaintext_length + operation.tag_length;
4294 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004295}
4296
Gilles Peskineee652a32018-06-01 19:23:52 +02004297/* Locate the tag in a ciphertext buffer containing the encrypted data
4298 * followed by the tag. Return the length of the part preceding the tag in
4299 * *plaintext_length. This is the size of the plaintext in modes where
4300 * the encrypted data has the same size as the plaintext, such as
4301 * CCM and GCM. */
4302static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4303 const uint8_t *ciphertext,
4304 size_t ciphertext_length,
4305 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004306 const uint8_t **p_tag )
4307{
4308 size_t payload_length;
4309 if( tag_length > ciphertext_length )
4310 return( PSA_ERROR_INVALID_ARGUMENT );
4311 payload_length = ciphertext_length - tag_length;
4312 if( payload_length > plaintext_size )
4313 return( PSA_ERROR_BUFFER_TOO_SMALL );
4314 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004315 return( PSA_SUCCESS );
4316}
4317
Gilles Peskinec5487a82018-12-03 18:08:14 +01004318psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004319 psa_algorithm_t alg,
4320 const uint8_t *nonce,
4321 size_t nonce_length,
4322 const uint8_t *additional_data,
4323 size_t additional_data_length,
4324 const uint8_t *ciphertext,
4325 size_t ciphertext_length,
4326 uint8_t *plaintext,
4327 size_t plaintext_size,
4328 size_t *plaintext_length )
4329{
mohammad16035955c982018-04-26 00:53:03 +03004330 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004331 aead_operation_t operation;
4332 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004333
Gilles Peskineee652a32018-06-01 19:23:52 +02004334 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004335
Gilles Peskinec5487a82018-12-03 18:08:14 +01004336 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004337 if( status != PSA_SUCCESS )
4338 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004339
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004340 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4341 ciphertext, ciphertext_length,
4342 plaintext_size, &tag );
4343 if( status != PSA_SUCCESS )
4344 goto exit;
4345
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004346#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004347 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004348 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004349 status = mbedtls_to_psa_error(
4350 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4351 ciphertext_length - operation.tag_length,
4352 nonce, nonce_length,
4353 additional_data,
4354 additional_data_length,
4355 tag, operation.tag_length,
4356 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004357 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004358 else
4359#endif /* MBEDTLS_GCM_C */
4360#if defined(MBEDTLS_CCM_C)
4361 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004362 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004363 status = mbedtls_to_psa_error(
4364 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4365 ciphertext_length - operation.tag_length,
4366 nonce, nonce_length,
4367 additional_data,
4368 additional_data_length,
4369 ciphertext, plaintext,
4370 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004371 }
mohammad160339574652018-06-01 04:39:53 -07004372 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004373#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004374#if defined(MBEDTLS_CHACHAPOLY_C)
4375 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4376 {
4377 if( nonce_length != 12 || operation.tag_length != 16 )
4378 {
4379 status = PSA_ERROR_NOT_SUPPORTED;
4380 goto exit;
4381 }
4382 status = mbedtls_to_psa_error(
4383 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4384 ciphertext_length - operation.tag_length,
4385 nonce,
4386 additional_data,
4387 additional_data_length,
4388 tag,
4389 ciphertext,
4390 plaintext ) );
4391 }
4392 else
4393#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004394 {
mohammad1603554faad2018-06-03 15:07:38 +03004395 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004396 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004397
Gilles Peskineedf9a652018-08-17 18:11:56 +02004398 if( status != PSA_SUCCESS && plaintext_size != 0 )
4399 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004400
Gilles Peskineedf9a652018-08-17 18:11:56 +02004401exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004402 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004403 if( status == PSA_SUCCESS )
4404 *plaintext_length = ciphertext_length - operation.tag_length;
4405 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004406}
4407
Gilles Peskinea0655c32018-04-30 17:06:50 +02004408
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004409
Gilles Peskine20035e32018-02-03 22:44:14 +01004410/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004411/* Generators */
4412/****************************************************************/
4413
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004414#define HKDF_STATE_INIT 0 /* no input yet */
4415#define HKDF_STATE_STARTED 1 /* got salt */
4416#define HKDF_STATE_KEYED 2 /* got key */
4417#define HKDF_STATE_OUTPUT 3 /* output started */
4418
Gilles Peskinecbe66502019-05-16 16:59:18 +02004419static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004420 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004421{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004422 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4423 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004424 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004425 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004426}
4427
4428
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004429psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004430{
4431 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004432 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004433 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004434 {
4435 /* The object has (apparently) been initialized but it is not
4436 * in use. It's ok to call abort on such an object, and there's
4437 * nothing to do. */
4438 }
4439 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004440#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004441 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004442 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004443 mbedtls_free( operation->ctx.hkdf.info );
4444 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004445 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004446 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004447 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004448 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004449 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004450 if( operation->ctx.tls12_prf.seed != NULL )
4451 {
4452 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4453 operation->ctx.tls12_prf.seed_length );
4454 mbedtls_free( operation->ctx.tls12_prf.seed );
4455 }
4456
4457 if( operation->ctx.tls12_prf.label != NULL )
4458 {
4459 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4460 operation->ctx.tls12_prf.label_length );
4461 mbedtls_free( operation->ctx.tls12_prf.label );
4462 }
4463
4464 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4465
4466 /* We leave the fields Ai and output_block to be erased safely by the
4467 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004468 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004469 else
4470#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004471 {
4472 status = PSA_ERROR_BAD_STATE;
4473 }
Janos Follath083036a2019-06-11 10:22:26 +01004474 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004475 return( status );
4476}
4477
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004478psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004479 size_t *capacity)
4480{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004481 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004482 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004483 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004484 return PSA_ERROR_BAD_STATE;
4485 }
4486
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004487 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004488 return( PSA_SUCCESS );
4489}
4490
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004491psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004492 size_t capacity )
4493{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004494 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004495 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004496 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004497 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004498 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004499 return( PSA_SUCCESS );
4500}
4501
Gilles Peskinebef7f142018-07-12 17:22:21 +02004502#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004503/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004504 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004505static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004506 psa_algorithm_t hash_alg,
4507 uint8_t *output,
4508 size_t output_length )
4509{
4510 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4511 psa_status_t status;
4512
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004513 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4514 return( PSA_ERROR_BAD_STATE );
4515 hkdf->state = HKDF_STATE_OUTPUT;
4516
Gilles Peskinebef7f142018-07-12 17:22:21 +02004517 while( output_length != 0 )
4518 {
4519 /* Copy what remains of the current block */
4520 uint8_t n = hash_length - hkdf->offset_in_block;
4521 if( n > output_length )
4522 n = (uint8_t) output_length;
4523 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4524 output += n;
4525 output_length -= n;
4526 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004527 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004528 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004529 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004530 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004531 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004532 * object was corrupted or if this function is called directly
4533 * inside the library. */
4534 if( hkdf->block_number == 0xff )
4535 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004536
4537 /* We need a new block */
4538 ++hkdf->block_number;
4539 hkdf->offset_in_block = 0;
4540 status = psa_hmac_setup_internal( &hkdf->hmac,
4541 hkdf->prk, hash_length,
4542 hash_alg );
4543 if( status != PSA_SUCCESS )
4544 return( status );
4545 if( hkdf->block_number != 1 )
4546 {
4547 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4548 hkdf->output_block,
4549 hash_length );
4550 if( status != PSA_SUCCESS )
4551 return( status );
4552 }
4553 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4554 hkdf->info,
4555 hkdf->info_length );
4556 if( status != PSA_SUCCESS )
4557 return( status );
4558 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4559 &hkdf->block_number, 1 );
4560 if( status != PSA_SUCCESS )
4561 return( status );
4562 status = psa_hmac_finish_internal( &hkdf->hmac,
4563 hkdf->output_block,
4564 sizeof( hkdf->output_block ) );
4565 if( status != PSA_SUCCESS )
4566 return( status );
4567 }
4568
4569 return( PSA_SUCCESS );
4570}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004571
Janos Follath7742fee2019-06-17 12:58:10 +01004572static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4573 psa_tls12_prf_key_derivation_t *tls12_prf,
4574 psa_algorithm_t alg )
4575{
4576 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4577 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004578 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4579 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004580
Janos Follath7742fee2019-06-17 12:58:10 +01004581 /* We can't be wanting more output after block 0xff, otherwise
4582 * the capacity check in psa_key_derivation_output_bytes() would have
4583 * prevented this call. It could happen only if the operation
4584 * object was corrupted or if this function is called directly
4585 * inside the library. */
4586 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004587 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004588
4589 /* We need a new block */
4590 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004591 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004592
4593 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4594 *
4595 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4596 *
4597 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4598 * HMAC_hash(secret, A(2) + seed) +
4599 * HMAC_hash(secret, A(3) + seed) + ...
4600 *
4601 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004602 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004603 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004604 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004605 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004606 * is currently extracted as `output_block` and where i is
4607 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004608 */
4609
Janos Follathea29bfb2019-06-19 12:21:20 +01004610 /* Save the hash context before using it, to preserve the hash state with
4611 * only the inner padding in it. We need this, because inner padding depends
4612 * on the key (secret in the RFC's terminology). */
4613 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4614 if( status != PSA_SUCCESS )
4615 goto cleanup;
4616
4617 /* Calculate A(i) where i = tls12_prf->block_number. */
4618 if( tls12_prf->block_number == 1 )
4619 {
4620 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4621 * the variable seed and in this instance means it in the context of the
4622 * P_hash function, where seed = label + seed.) */
4623 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4624 tls12_prf->label, tls12_prf->label_length );
4625 if( status != PSA_SUCCESS )
4626 goto cleanup;
4627 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4628 tls12_prf->seed, tls12_prf->seed_length );
4629 if( status != PSA_SUCCESS )
4630 goto cleanup;
4631 }
4632 else
4633 {
4634 /* A(i) = HMAC_hash(secret, A(i-1)) */
4635 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4636 tls12_prf->Ai, hash_length );
4637 if( status != PSA_SUCCESS )
4638 goto cleanup;
4639 }
4640
4641 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4642 tls12_prf->Ai, hash_length );
4643 if( status != PSA_SUCCESS )
4644 goto cleanup;
4645 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4646 if( status != PSA_SUCCESS )
4647 goto cleanup;
4648
4649 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4650 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4651 tls12_prf->Ai, hash_length );
4652 if( status != PSA_SUCCESS )
4653 goto cleanup;
4654 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4655 tls12_prf->label, tls12_prf->label_length );
4656 if( status != PSA_SUCCESS )
4657 goto cleanup;
4658 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4659 tls12_prf->seed, tls12_prf->seed_length );
4660 if( status != PSA_SUCCESS )
4661 goto cleanup;
4662 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4663 tls12_prf->output_block, hash_length );
4664 if( status != PSA_SUCCESS )
4665 goto cleanup;
4666 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4667 if( status != PSA_SUCCESS )
4668 goto cleanup;
4669
Janos Follath7742fee2019-06-17 12:58:10 +01004670
4671cleanup:
4672
Janos Follathea29bfb2019-06-19 12:21:20 +01004673 cleanup_status = psa_hash_abort( &backup );
4674 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4675 status = cleanup_status;
4676
4677 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004678}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004679
Janos Follath844eb0e2019-06-19 12:10:49 +01004680static psa_status_t psa_key_derivation_tls12_prf_read(
4681 psa_tls12_prf_key_derivation_t *tls12_prf,
4682 psa_algorithm_t alg,
4683 uint8_t *output,
4684 size_t output_length )
4685{
4686 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4687 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4688 psa_status_t status;
4689 uint8_t offset, length;
4690
4691 while( output_length != 0 )
4692 {
4693 /* Check if we have fully processed the current block. */
4694 if( tls12_prf->left_in_block == 0 )
4695 {
4696 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4697 alg );
4698 if( status != PSA_SUCCESS )
4699 return( status );
4700
4701 continue;
4702 }
4703
4704 if( tls12_prf->left_in_block > output_length )
4705 length = (uint8_t) output_length;
4706 else
4707 length = tls12_prf->left_in_block;
4708
4709 offset = hash_length - tls12_prf->left_in_block;
4710 memcpy( output, tls12_prf->output_block + offset, length );
4711 output += length;
4712 output_length -= length;
4713 tls12_prf->left_in_block -= length;
4714 }
4715
4716 return( PSA_SUCCESS );
4717}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004718#endif /* MBEDTLS_MD_C */
4719
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004720psa_status_t psa_key_derivation_output_bytes(
4721 psa_key_derivation_operation_t *operation,
4722 uint8_t *output,
4723 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004724{
4725 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004726 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004727
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004728 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004729 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004730 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004731 return PSA_ERROR_BAD_STATE;
4732 }
4733
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004734 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004735 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004736 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004737 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004738 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004739 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004740 goto exit;
4741 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004742 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004743 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004744 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004745 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004746 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4747 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004748 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004749 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004750 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004751 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004752 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004753
Gilles Peskinebef7f142018-07-12 17:22:21 +02004754#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004755 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004756 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004757 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004758 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004759 output, output_length );
4760 }
Janos Follathadbec812019-06-14 11:05:39 +01004761 else
Janos Follathadbec812019-06-14 11:05:39 +01004762 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004763 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004764 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004765 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004766 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004767 output_length );
4768 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004769 else
4770#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004771 {
4772 return( PSA_ERROR_BAD_STATE );
4773 }
4774
4775exit:
4776 if( status != PSA_SUCCESS )
4777 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004778 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004779 * This allows us to differentiate between exhausted operations and
4780 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4781 * operations. */
4782 psa_algorithm_t alg = operation->alg;
4783 psa_key_derivation_abort( operation );
4784 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004785 memset( output, '!', output_length );
4786 }
4787 return( status );
4788}
4789
Gilles Peskine08542d82018-07-19 17:05:42 +02004790#if defined(MBEDTLS_DES_C)
4791static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4792{
4793 if( data_size >= 8 )
4794 mbedtls_des_key_set_parity( data );
4795 if( data_size >= 16 )
4796 mbedtls_des_key_set_parity( data + 8 );
4797 if( data_size >= 24 )
4798 mbedtls_des_key_set_parity( data + 16 );
4799}
4800#endif /* MBEDTLS_DES_C */
4801
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004802static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004803 psa_key_slot_t *slot,
4804 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004805 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004806{
4807 uint8_t *data = NULL;
4808 size_t bytes = PSA_BITS_TO_BYTES( bits );
4809 psa_status_t status;
4810
Gilles Peskine8e338702019-07-30 20:06:31 +02004811 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004812 return( PSA_ERROR_INVALID_ARGUMENT );
4813 if( bits % 8 != 0 )
4814 return( PSA_ERROR_INVALID_ARGUMENT );
4815 data = mbedtls_calloc( 1, bytes );
4816 if( data == NULL )
4817 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4818
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004819 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004820 if( status != PSA_SUCCESS )
4821 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004822#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004823 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004824 psa_des_set_key_parity( data, bytes );
4825#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004826 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004827
4828exit:
4829 mbedtls_free( data );
4830 return( status );
4831}
4832
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004833psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004834 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004835 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004836{
4837 psa_status_t status;
4838 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004839 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004840
4841 /* Reject any attempt to create a zero-length key so that we don't
4842 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4843 if( psa_get_key_bits( attributes ) == 0 )
4844 return( PSA_ERROR_INVALID_ARGUMENT );
4845
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004846 if( ! operation->can_output_key )
4847 return( PSA_ERROR_NOT_PERMITTED );
4848
Gilles Peskinedf179142019-07-15 22:02:14 +02004849 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4850 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004851#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4852 if( driver != NULL )
4853 {
4854 /* Deriving a key in a secure element is not implemented yet. */
4855 status = PSA_ERROR_NOT_SUPPORTED;
4856 }
4857#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004858 if( status == PSA_SUCCESS )
4859 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004860 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004861 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004862 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004863 }
4864 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004865 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004866 if( status != PSA_SUCCESS )
4867 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004868 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004869 *handle = 0;
4870 }
4871 return( status );
4872}
4873
Gilles Peskineeab56e42018-07-12 17:12:33 +02004874
4875
4876/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004877/* Key derivation */
4878/****************************************************************/
4879
Gilles Peskine969c5d62019-01-16 15:53:06 +01004880static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004881 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004882 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004883{
Janos Follath5fe19732019-06-20 15:09:30 +01004884 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4885 * initialisers for this union leave some bytes unspecified.) */
4886 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4887
Gilles Peskine969c5d62019-01-16 15:53:06 +01004888 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004889#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004890 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4891 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4892 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004893 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004894 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004895 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4896 if( hash_size == 0 )
4897 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004898 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4899 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004900 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004901 {
4902 return( PSA_ERROR_NOT_SUPPORTED );
4903 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004904 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004905 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004906 }
4907#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004908 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004909 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004910}
4911
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004912psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004913 psa_algorithm_t alg )
4914{
4915 psa_status_t status;
4916
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004917 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004918 return( PSA_ERROR_BAD_STATE );
4919
Gilles Peskine6843c292019-01-18 16:44:49 +01004920 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4921 return( PSA_ERROR_INVALID_ARGUMENT );
4922 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004923 {
4924 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004925 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004926 }
4927 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4928 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004929 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004930 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004931 else
4932 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004933
4934 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004935 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004936 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004937}
4938
4939#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004940static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004941 psa_algorithm_t hash_alg,
4942 psa_key_derivation_step_t step,
4943 const uint8_t *data,
4944 size_t data_length )
4945{
4946 psa_status_t status;
4947 switch( step )
4948 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004949 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004950 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004951 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004952 status = psa_hmac_setup_internal( &hkdf->hmac,
4953 data, data_length,
4954 hash_alg );
4955 if( status != PSA_SUCCESS )
4956 return( status );
4957 hkdf->state = HKDF_STATE_STARTED;
4958 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004959 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004960 /* If no salt was provided, use an empty salt. */
4961 if( hkdf->state == HKDF_STATE_INIT )
4962 {
4963 status = psa_hmac_setup_internal( &hkdf->hmac,
4964 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004965 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004966 if( status != PSA_SUCCESS )
4967 return( status );
4968 hkdf->state = HKDF_STATE_STARTED;
4969 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004970 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004971 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004972 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4973 data, data_length );
4974 if( status != PSA_SUCCESS )
4975 return( status );
4976 status = psa_hmac_finish_internal( &hkdf->hmac,
4977 hkdf->prk,
4978 sizeof( hkdf->prk ) );
4979 if( status != PSA_SUCCESS )
4980 return( status );
4981 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4982 hkdf->block_number = 0;
4983 hkdf->state = HKDF_STATE_KEYED;
4984 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004985 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004986 if( hkdf->state == HKDF_STATE_OUTPUT )
4987 return( PSA_ERROR_BAD_STATE );
4988 if( hkdf->info_set )
4989 return( PSA_ERROR_BAD_STATE );
4990 hkdf->info_length = data_length;
4991 if( data_length != 0 )
4992 {
4993 hkdf->info = mbedtls_calloc( 1, data_length );
4994 if( hkdf->info == NULL )
4995 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4996 memcpy( hkdf->info, data, data_length );
4997 }
4998 hkdf->info_set = 1;
4999 return( PSA_SUCCESS );
5000 default:
5001 return( PSA_ERROR_INVALID_ARGUMENT );
5002 }
5003}
Janos Follathb03233e2019-06-11 15:30:30 +01005004
Janos Follathf08e2652019-06-13 09:05:41 +01005005static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5006 const uint8_t *data,
5007 size_t data_length )
5008{
5009 if( prf->state != TLS12_PRF_STATE_INIT )
5010 return( PSA_ERROR_BAD_STATE );
5011
Janos Follathd6dce9f2019-07-04 09:11:38 +01005012 if( data_length != 0 )
5013 {
5014 prf->seed = mbedtls_calloc( 1, data_length );
5015 if( prf->seed == NULL )
5016 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005017
Janos Follathd6dce9f2019-07-04 09:11:38 +01005018 memcpy( prf->seed, data, data_length );
5019 prf->seed_length = data_length;
5020 }
Janos Follathf08e2652019-06-13 09:05:41 +01005021
5022 prf->state = TLS12_PRF_STATE_SEED_SET;
5023
5024 return( PSA_SUCCESS );
5025}
5026
Janos Follath81550542019-06-13 14:26:34 +01005027static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5028 psa_algorithm_t hash_alg,
5029 const uint8_t *data,
5030 size_t data_length )
5031{
5032 psa_status_t status;
5033 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5034 return( PSA_ERROR_BAD_STATE );
5035
5036 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5037 if( status != PSA_SUCCESS )
5038 return( status );
5039
5040 prf->state = TLS12_PRF_STATE_KEY_SET;
5041
5042 return( PSA_SUCCESS );
5043}
5044
Janos Follath6660f0e2019-06-17 08:44:03 +01005045static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5046 psa_tls12_prf_key_derivation_t *prf,
5047 psa_algorithm_t hash_alg,
5048 const uint8_t *data,
5049 size_t data_length )
5050{
5051 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005052 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5053 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005054
5055 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5056 return( PSA_ERROR_INVALID_ARGUMENT );
5057
5058 /* Quoting RFC 4279, Section 2:
5059 *
5060 * The premaster secret is formed as follows: if the PSK is N octets
5061 * long, concatenate a uint16 with the value N, N zero octets, a second
5062 * uint16 with the value N, and the PSK itself.
5063 */
5064
Janos Follath40e13932019-06-26 13:22:29 +01005065 *cur++ = ( data_length >> 8 ) & 0xff;
5066 *cur++ = ( data_length >> 0 ) & 0xff;
5067 memset( cur, 0, data_length );
5068 cur += data_length;
5069 *cur++ = pms[0];
5070 *cur++ = pms[1];
5071 memcpy( cur, data, data_length );
5072 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005073
Janos Follath40e13932019-06-26 13:22:29 +01005074 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005075
5076 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5077 return( status );
5078}
5079
Janos Follath63028dd2019-06-13 09:15:47 +01005080static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5081 const uint8_t *data,
5082 size_t data_length )
5083{
5084 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5085 return( PSA_ERROR_BAD_STATE );
5086
Janos Follathd6dce9f2019-07-04 09:11:38 +01005087 if( data_length != 0 )
5088 {
5089 prf->label = mbedtls_calloc( 1, data_length );
5090 if( prf->label == NULL )
5091 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005092
Janos Follathd6dce9f2019-07-04 09:11:38 +01005093 memcpy( prf->label, data, data_length );
5094 prf->label_length = data_length;
5095 }
Janos Follath63028dd2019-06-13 09:15:47 +01005096
5097 prf->state = TLS12_PRF_STATE_LABEL_SET;
5098
5099 return( PSA_SUCCESS );
5100}
5101
Janos Follathb03233e2019-06-11 15:30:30 +01005102static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5103 psa_algorithm_t hash_alg,
5104 psa_key_derivation_step_t step,
5105 const uint8_t *data,
5106 size_t data_length )
5107{
Janos Follathb03233e2019-06-11 15:30:30 +01005108 switch( step )
5109 {
Janos Follathf08e2652019-06-13 09:05:41 +01005110 case PSA_KEY_DERIVATION_INPUT_SEED:
5111 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005112 case PSA_KEY_DERIVATION_INPUT_SECRET:
5113 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005114 case PSA_KEY_DERIVATION_INPUT_LABEL:
5115 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005116 default:
5117 return( PSA_ERROR_INVALID_ARGUMENT );
5118 }
5119}
Janos Follath6660f0e2019-06-17 08:44:03 +01005120
5121static psa_status_t psa_tls12_prf_psk_to_ms_input(
5122 psa_tls12_prf_key_derivation_t *prf,
5123 psa_algorithm_t hash_alg,
5124 psa_key_derivation_step_t step,
5125 const uint8_t *data,
5126 size_t data_length )
5127{
5128 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005129 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005130 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5131 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005132 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005133
5134 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5135}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005136#endif /* MBEDTLS_MD_C */
5137
Gilles Peskineb8965192019-09-24 16:21:10 +02005138/** Check whether the given key type is acceptable for the given
5139 * input step of a key derivation.
5140 *
5141 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5142 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5143 * Both secret and non-secret inputs can alternatively have the type
5144 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5145 * that the input was passed as a buffer rather than via a key object.
5146 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005147static int psa_key_derivation_check_input_type(
5148 psa_key_derivation_step_t step,
5149 psa_key_type_t key_type )
5150{
5151 switch( step )
5152 {
5153 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005154 if( key_type == PSA_KEY_TYPE_DERIVE )
5155 return( PSA_SUCCESS );
5156 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005157 return( PSA_SUCCESS );
5158 break;
5159 case PSA_KEY_DERIVATION_INPUT_LABEL:
5160 case PSA_KEY_DERIVATION_INPUT_SALT:
5161 case PSA_KEY_DERIVATION_INPUT_INFO:
5162 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005163 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5164 return( PSA_SUCCESS );
5165 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005166 return( PSA_SUCCESS );
5167 break;
5168 }
5169 return( PSA_ERROR_INVALID_ARGUMENT );
5170}
5171
Janos Follathb80a94e2019-06-12 15:54:46 +01005172static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005173 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005174 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005175 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005176 const uint8_t *data,
5177 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005178{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005179 psa_status_t status;
5180 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5181
5182 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005183 if( status != PSA_SUCCESS )
5184 goto exit;
5185
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005186#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005187 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005188 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005189 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005190 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005191 step, data, data_length );
5192 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005193 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005194 {
Janos Follathb03233e2019-06-11 15:30:30 +01005195 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5196 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5197 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005198 }
5199 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5200 {
5201 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5202 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5203 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005204 }
5205 else
5206#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005207 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005208 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005209 return( PSA_ERROR_BAD_STATE );
5210 }
5211
Gilles Peskine224b0d62019-09-23 18:13:17 +02005212exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005213 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005214 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005215 return( status );
5216}
5217
Janos Follath51f4a0f2019-06-14 11:35:55 +01005218psa_status_t psa_key_derivation_input_bytes(
5219 psa_key_derivation_operation_t *operation,
5220 psa_key_derivation_step_t step,
5221 const uint8_t *data,
5222 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005223{
Gilles Peskineb8965192019-09-24 16:21:10 +02005224 return( psa_key_derivation_input_internal( operation, step,
5225 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005226 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005227}
5228
Janos Follath51f4a0f2019-06-14 11:35:55 +01005229psa_status_t psa_key_derivation_input_key(
5230 psa_key_derivation_operation_t *operation,
5231 psa_key_derivation_step_t step,
5232 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005233{
5234 psa_key_slot_t *slot;
5235 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005236
Gilles Peskine28f8f302019-07-24 13:30:31 +02005237 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005238 PSA_KEY_USAGE_DERIVE,
5239 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005240 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005241 {
5242 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005243 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005244 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005245
5246 /* Passing a key object as a SECRET input unlocks the permission
5247 * to output to a key object. */
5248 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5249 operation->can_output_key = 1;
5250
Janos Follathb80a94e2019-06-12 15:54:46 +01005251 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005252 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005253 slot->data.raw.data,
5254 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005255}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005256
5257
5258
5259/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005260/* Key agreement */
5261/****************************************************************/
5262
Gilles Peskinea05219c2018-11-16 16:02:56 +01005263#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005264static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5265 size_t peer_key_length,
5266 const mbedtls_ecp_keypair *our_key,
5267 uint8_t *shared_secret,
5268 size_t shared_secret_size,
5269 size_t *shared_secret_length )
5270{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005271 mbedtls_ecp_keypair *their_key = NULL;
5272 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005273 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005274 size_t bits = 0;
5275 psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005276 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005277
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005278 status = psa_import_ec_public_key( curve,
5279 peer_key, peer_key_length,
5280 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005281 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005282 goto exit;
5283
Jaeden Amero97271b32019-01-10 19:38:51 +00005284 status = mbedtls_to_psa_error(
5285 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5286 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005287 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005288 status = mbedtls_to_psa_error(
5289 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5290 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005291 goto exit;
5292
Jaeden Amero97271b32019-01-10 19:38:51 +00005293 status = mbedtls_to_psa_error(
5294 mbedtls_ecdh_calc_secret( &ecdh,
5295 shared_secret_length,
5296 shared_secret, shared_secret_size,
5297 mbedtls_ctr_drbg_random,
5298 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005299 if( status != PSA_SUCCESS )
5300 goto exit;
5301 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5302 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005303
5304exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005305 if( status != PSA_SUCCESS )
5306 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005307 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005308 mbedtls_ecp_keypair_free( their_key );
5309 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005310 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005311}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005312#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005313
Gilles Peskine01d718c2018-09-18 12:01:02 +02005314#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5315
Gilles Peskine0216fe12019-04-11 21:23:21 +02005316static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5317 psa_key_slot_t *private_key,
5318 const uint8_t *peer_key,
5319 size_t peer_key_length,
5320 uint8_t *shared_secret,
5321 size_t shared_secret_size,
5322 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005323{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005324 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005325 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005326#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005327 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005328 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005329 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005330 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5331 private_key->data.ecp,
5332 shared_secret, shared_secret_size,
5333 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005334#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005335 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005336 (void) private_key;
5337 (void) peer_key;
5338 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005339 (void) shared_secret;
5340 (void) shared_secret_size;
5341 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005342 return( PSA_ERROR_NOT_SUPPORTED );
5343 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005344}
5345
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005346/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005347 * to potentially free embedded data structures and wipe confidential data.
5348 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005349static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005350 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005351 psa_key_slot_t *private_key,
5352 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005353 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005354{
5355 psa_status_t status;
5356 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5357 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005358 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005359
5360 /* Step 1: run the secret agreement algorithm to generate the shared
5361 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005362 status = psa_key_agreement_raw_internal( ka_alg,
5363 private_key,
5364 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005365 shared_secret,
5366 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005367 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005368 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005369 goto exit;
5370
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005371 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005372 * the shared secret. A shared secret is permitted wherever a key
5373 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005374 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005375 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005376 shared_secret,
5377 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005378
Gilles Peskine12313cd2018-06-20 00:20:32 +02005379exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005380 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005381 return( status );
5382}
5383
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005384psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005385 psa_key_derivation_step_t step,
5386 psa_key_handle_t private_key,
5387 const uint8_t *peer_key,
5388 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005389{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005390 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005391 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005392 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005393 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005394 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005395 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005396 if( status != PSA_SUCCESS )
5397 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005398 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005399 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005400 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005401 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005402 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005403 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005404}
5405
Gilles Peskinebe697d82019-05-16 18:00:41 +02005406psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5407 psa_key_handle_t private_key,
5408 const uint8_t *peer_key,
5409 size_t peer_key_length,
5410 uint8_t *output,
5411 size_t output_size,
5412 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005413{
5414 psa_key_slot_t *slot;
5415 psa_status_t status;
5416
5417 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5418 {
5419 status = PSA_ERROR_INVALID_ARGUMENT;
5420 goto exit;
5421 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005422 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005423 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005424 if( status != PSA_SUCCESS )
5425 goto exit;
5426
5427 status = psa_key_agreement_raw_internal( alg, slot,
5428 peer_key, peer_key_length,
5429 output, output_size,
5430 output_length );
5431
5432exit:
5433 if( status != PSA_SUCCESS )
5434 {
5435 /* If an error happens and is not handled properly, the output
5436 * may be used as a key to protect sensitive data. Arrange for such
5437 * a key to be random, which is likely to result in decryption or
5438 * verification errors. This is better than filling the buffer with
5439 * some constant data such as zeros, which would result in the data
5440 * being protected with a reproducible, easily knowable key.
5441 */
5442 psa_generate_random( output, output_size );
5443 *output_length = output_size;
5444 }
5445 return( status );
5446}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005447
5448
5449/****************************************************************/
5450/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005451/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005452
Gilles Peskine4c317f42018-07-12 01:24:09 +02005453psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005454 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005455{
Janos Follath24eed8d2019-11-22 13:21:35 +00005456 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005457 GUARD_MODULE_INITIALIZED;
5458
Gilles Peskinef181eca2019-08-07 13:49:00 +02005459 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5460 {
5461 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5462 output,
5463 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5464 if( ret != 0 )
5465 return( mbedtls_to_psa_error( ret ) );
5466 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5467 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5468 }
5469
Gilles Peskinee59236f2018-01-27 23:32:46 +01005470 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5471 return( mbedtls_to_psa_error( ret ) );
5472}
5473
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005474#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5475#include "mbedtls/entropy_poll.h"
5476
Gilles Peskine7228da22019-07-15 11:06:15 +02005477psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005478 size_t seed_size )
5479{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005480 if( global_data.initialized )
5481 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005482
5483 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5484 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5485 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5486 return( PSA_ERROR_INVALID_ARGUMENT );
5487
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005488 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005489}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005490#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005491
Gilles Peskinee56e8782019-04-26 17:34:02 +02005492#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5493static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5494 size_t domain_parameters_size,
5495 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005496{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005497 size_t i;
5498 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005499
Gilles Peskinee56e8782019-04-26 17:34:02 +02005500 if( domain_parameters_size == 0 )
5501 {
5502 *exponent = 65537;
5503 return( PSA_SUCCESS );
5504 }
5505
5506 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5507 * support values that fit in a 32-bit integer, which is larger than
5508 * int on just about every platform anyway. */
5509 if( domain_parameters_size > sizeof( acc ) )
5510 return( PSA_ERROR_NOT_SUPPORTED );
5511 for( i = 0; i < domain_parameters_size; i++ )
5512 acc = ( acc << 8 ) | domain_parameters[i];
5513 if( acc > INT_MAX )
5514 return( PSA_ERROR_NOT_SUPPORTED );
5515 *exponent = acc;
5516 return( PSA_SUCCESS );
5517}
5518#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5519
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005520static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005521 psa_key_slot_t *slot, size_t bits,
5522 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005523{
Gilles Peskine8e338702019-07-30 20:06:31 +02005524 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005525
Gilles Peskinee56e8782019-04-26 17:34:02 +02005526 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005527 return( PSA_ERROR_INVALID_ARGUMENT );
5528
Gilles Peskinee59236f2018-01-27 23:32:46 +01005529 if( key_type_is_raw_bytes( type ) )
5530 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005531 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005532 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5533 if( status != PSA_SUCCESS )
5534 return( status );
5535 status = psa_generate_random( slot->data.raw.data,
5536 slot->data.raw.bytes );
5537 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005538 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005539#if defined(MBEDTLS_DES_C)
5540 if( type == PSA_KEY_TYPE_DES )
5541 psa_des_set_key_parity( slot->data.raw.data,
5542 slot->data.raw.bytes );
5543#endif /* MBEDTLS_DES_C */
5544 }
5545 else
5546
5547#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005548 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005549 {
5550 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005552 int exponent;
5553 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005554 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5555 return( PSA_ERROR_NOT_SUPPORTED );
5556 /* Accept only byte-aligned keys, for the same reasons as
5557 * in psa_import_rsa_key(). */
5558 if( bits % 8 != 0 )
5559 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005560 status = psa_read_rsa_exponent( domain_parameters,
5561 domain_parameters_size,
5562 &exponent );
5563 if( status != PSA_SUCCESS )
5564 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005565 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5566 if( rsa == NULL )
5567 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5568 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5569 ret = mbedtls_rsa_gen_key( rsa,
5570 mbedtls_ctr_drbg_random,
5571 &global_data.ctr_drbg,
5572 (unsigned int) bits,
5573 exponent );
5574 if( ret != 0 )
5575 {
5576 mbedtls_rsa_free( rsa );
5577 mbedtls_free( rsa );
5578 return( mbedtls_to_psa_error( ret ) );
5579 }
5580 slot->data.rsa = rsa;
5581 }
5582 else
5583#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5584
5585#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005586 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005587 {
5588 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005589 mbedtls_ecp_group_id grp_id =
5590 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005591 const mbedtls_ecp_curve_info *curve_info =
5592 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5593 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005594 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005595 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005596 return( PSA_ERROR_NOT_SUPPORTED );
5597 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5598 return( PSA_ERROR_NOT_SUPPORTED );
5599 if( curve_info->bit_size != bits )
5600 return( PSA_ERROR_INVALID_ARGUMENT );
5601 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5602 if( ecp == NULL )
5603 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5604 mbedtls_ecp_keypair_init( ecp );
5605 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5606 mbedtls_ctr_drbg_random,
5607 &global_data.ctr_drbg );
5608 if( ret != 0 )
5609 {
5610 mbedtls_ecp_keypair_free( ecp );
5611 mbedtls_free( ecp );
5612 return( mbedtls_to_psa_error( ret ) );
5613 }
5614 slot->data.ecp = ecp;
5615 }
5616 else
5617#endif /* MBEDTLS_ECP_C */
5618
5619 return( PSA_ERROR_NOT_SUPPORTED );
5620
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005621 return( PSA_SUCCESS );
5622}
Darryl Green0c6575a2018-11-07 16:05:30 +00005623
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005624psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005625 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005626{
5627 psa_status_t status;
5628 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005629 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005630
Gilles Peskine0f84d622019-09-12 19:03:13 +02005631 /* Reject any attempt to create a zero-length key so that we don't
5632 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5633 if( psa_get_key_bits( attributes ) == 0 )
5634 return( PSA_ERROR_INVALID_ARGUMENT );
5635
Gilles Peskinedf179142019-07-15 22:02:14 +02005636 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5637 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005638 if( status != PSA_SUCCESS )
5639 goto exit;
5640
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005641#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5642 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005643 {
Gilles Peskine11792082019-08-06 18:36:36 +02005644 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5645 size_t pubkey_length = 0; /* We don't support this feature yet */
5646 if( drv->key_management == NULL ||
5647 drv->key_management->p_generate == NULL )
5648 {
5649 status = PSA_ERROR_NOT_SUPPORTED;
5650 goto exit;
5651 }
5652 status = drv->key_management->p_generate(
5653 psa_get_se_driver_context( driver ),
5654 slot->data.se.slot_number, attributes,
5655 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005656 }
Gilles Peskine11792082019-08-06 18:36:36 +02005657 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005658#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005659 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005660 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005661 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005662 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005663 }
Gilles Peskine11792082019-08-06 18:36:36 +02005664
5665exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005666 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005667 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005668 if( status != PSA_SUCCESS )
5669 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005670 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005671 *handle = 0;
5672 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005673 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005674}
5675
5676
Gilles Peskinee59236f2018-01-27 23:32:46 +01005677
5678/****************************************************************/
5679/* Module setup */
5680/****************************************************************/
5681
Gilles Peskine5e769522018-11-20 21:59:56 +01005682psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5683 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5684 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5685{
5686 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5687 return( PSA_ERROR_BAD_STATE );
5688 global_data.entropy_init = entropy_init;
5689 global_data.entropy_free = entropy_free;
5690 return( PSA_SUCCESS );
5691}
5692
Gilles Peskinee59236f2018-01-27 23:32:46 +01005693void mbedtls_psa_crypto_free( void )
5694{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005695 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005696 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5697 {
5698 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005699 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005700 }
5701 /* Wipe all remaining data, including configuration.
5702 * In particular, this sets all state indicator to the value
5703 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005704 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005705#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005706 /* Unregister all secure element drivers, so that we restart from
5707 * a pristine state. */
5708 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005709#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005710}
5711
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005712#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5713/** Recover a transaction that was interrupted by a power failure.
5714 *
5715 * This function is called during initialization, before psa_crypto_init()
5716 * returns. If this function returns a failure status, the initialization
5717 * fails.
5718 */
5719static psa_status_t psa_crypto_recover_transaction(
5720 const psa_crypto_transaction_t *transaction )
5721{
5722 switch( transaction->unknown.type )
5723 {
5724 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5725 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005726 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005727 * is implemented.
5728 * https://github.com/ARMmbed/mbed-crypto/issues/218
5729 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005730 default:
5731 /* We found an unsupported transaction in the storage.
5732 * We don't know what state the storage is in. Give up. */
5733 return( PSA_ERROR_STORAGE_FAILURE );
5734 }
5735}
5736#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5737
Gilles Peskinee59236f2018-01-27 23:32:46 +01005738psa_status_t psa_crypto_init( void )
5739{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005740 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005741 const unsigned char drbg_seed[] = "PSA";
5742
Gilles Peskinec6b69072018-11-20 21:42:52 +01005743 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005744 if( global_data.initialized != 0 )
5745 return( PSA_SUCCESS );
5746
Gilles Peskine5e769522018-11-20 21:59:56 +01005747 /* Set default configuration if
5748 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5749 if( global_data.entropy_init == NULL )
5750 global_data.entropy_init = mbedtls_entropy_init;
5751 if( global_data.entropy_free == NULL )
5752 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005753
Gilles Peskinec6b69072018-11-20 21:42:52 +01005754 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005755 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005756#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5757 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5758 /* The PSA entropy injection feature depends on using NV seed as an entropy
5759 * source. Add NV seed as an entropy source for PSA entropy injection. */
5760 mbedtls_entropy_add_source( &global_data.entropy,
5761 mbedtls_nv_seed_poll, NULL,
5762 MBEDTLS_ENTROPY_BLOCK_SIZE,
5763 MBEDTLS_ENTROPY_SOURCE_STRONG );
5764#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005765 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005766 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005767 status = mbedtls_to_psa_error(
5768 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5769 mbedtls_entropy_func,
5770 &global_data.entropy,
5771 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5772 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005773 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005774 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005775
Gilles Peskine66fb1262018-12-10 16:29:04 +01005776 status = psa_initialize_key_slots( );
5777 if( status != PSA_SUCCESS )
5778 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005779
Gilles Peskined9348f22019-10-01 15:22:29 +02005780#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5781 status = psa_init_all_se_drivers( );
5782 if( status != PSA_SUCCESS )
5783 goto exit;
5784#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5785
Gilles Peskine4b734222019-07-24 15:56:31 +02005786#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005787 status = psa_crypto_load_transaction( );
5788 if( status == PSA_SUCCESS )
5789 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005790 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5791 if( status != PSA_SUCCESS )
5792 goto exit;
5793 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005794 }
5795 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5796 {
5797 /* There's no transaction to complete. It's all good. */
5798 status = PSA_SUCCESS;
5799 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005800#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005801
Gilles Peskinec6b69072018-11-20 21:42:52 +01005802 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005803 global_data.initialized = 1;
5804
5805exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005806 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005807 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005808 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005809}
5810
5811#endif /* MBEDTLS_PSA_CRYPTO_C */