blob: 1151d17f7392520639b87fd792a02c933552888e [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
Steven Cooremane3fd3922020-06-11 16:50:36 +0200673 /* Load and validate the secret key */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100674 status = mbedtls_to_psa_error(
Steven Cooremane3fd3922020-06-11 16:50:36 +0200675 mbedtls_ecp_read_key( ecp->grp.id, ecp, data, data_length ) );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100676 if( status != PSA_SUCCESS )
677 goto exit;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200678
Gilles Peskinef76aa772018-10-29 19:24:33 +0100679 /* Calculate the public key from the private key. */
680 status = mbedtls_to_psa_error(
681 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
682 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
683 if( status != PSA_SUCCESS )
684 goto exit;
685
686 *p_ecp = ecp;
687 return( PSA_SUCCESS );
688
689exit:
690 if( ecp != NULL )
691 {
692 mbedtls_ecp_keypair_free( ecp );
693 mbedtls_free( ecp );
694 }
695 return( status );
696}
697#endif /* defined(MBEDTLS_ECP_C) */
698
Gilles Peskineb46bef22019-07-30 21:32:04 +0200699
700/** Return the size of the key in the given slot, in bits.
701 *
702 * \param[in] slot A key slot.
703 *
704 * \return The key size in bits, read from the metadata in the slot.
705 */
706static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
707{
708 return( slot->attr.bits );
709}
710
711/** Calculate the size of the key in the given slot, in bits.
712 *
713 * \param[in] slot A key slot containing a transparent key.
714 *
715 * \return The key size in bits, calculated from the key data.
716 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200717static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200718{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200719 size_t bits = 0; /* return 0 on an empty slot */
720
Gilles Peskineb46bef22019-07-30 21:32:04 +0200721 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200722 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200723#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200724 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
725 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200726#endif /* defined(MBEDTLS_RSA_C) */
727#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200728 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
729 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200730#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200731
732 /* We know that the size fits in psa_key_bits_t thanks to checks
733 * when the key was created. */
734 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200735}
736
Gilles Peskine8e338702019-07-30 20:06:31 +0200737/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100738 * previously. This function assumes that the slot does not contain
739 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100740psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
741 const uint8_t *data,
742 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200744 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100745
Gilles Peskine8e338702019-07-30 20:06:31 +0200746 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100747 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200748 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200749 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100750 if( data_length > SIZE_MAX / 8 )
751 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200752 /* Enforce a size limit, and in particular ensure that the bit
753 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200754 if( bit_size > PSA_MAX_KEY_BITS )
755 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200756 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200757 &slot->data.raw );
758 if( status != PSA_SUCCESS )
759 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200760 if( data_length != 0 )
761 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100762 }
763 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100764#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200765 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100766 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200767 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100768 data, data_length,
769 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100770 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200771 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100772 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000773 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200774 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000775 data, data_length,
776 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100777 }
778 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100779#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000780#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200781 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100782 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200783 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000784 data, data_length,
785 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100786 }
787 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000788#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100789 {
790 return( PSA_ERROR_NOT_SUPPORTED );
791 }
Darryl Green940d72c2018-07-13 13:18:51 +0100792
Gilles Peskineb46bef22019-07-30 21:32:04 +0200793 if( status == PSA_SUCCESS )
794 {
795 /* Write the actual key size to the slot.
796 * psa_start_key_creation() wrote the size declared by the
797 * caller, which may be 0 (meaning unspecified) or wrong. */
798 slot->attr.bits = psa_calculate_key_bits( slot );
799 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100800 return( status );
801}
802
Gilles Peskinef603c712019-01-19 13:40:11 +0100803/** Calculate the intersection of two algorithm usage policies.
804 *
805 * Return 0 (which allows no operation) on incompatibility.
806 */
807static psa_algorithm_t psa_key_policy_algorithm_intersection(
808 psa_algorithm_t alg1,
809 psa_algorithm_t alg2 )
810{
Gilles Peskine549ea862019-05-22 11:45:59 +0200811 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100812 if( alg1 == alg2 )
813 return( alg1 );
814 /* If the policies are from the same hash-and-sign family, check
815 * if one is a wildcard. If so the other has the specific algorithm. */
816 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
817 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
818 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
819 {
820 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
821 return( alg2 );
822 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
823 return( alg1 );
824 }
825 /* If the policies are incompatible, allow nothing. */
826 return( 0 );
827}
828
Gilles Peskined6f371b2019-05-10 19:33:38 +0200829static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
830 psa_algorithm_t requested_alg )
831{
Gilles Peskine549ea862019-05-22 11:45:59 +0200832 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200833 if( requested_alg == policy_alg )
834 return( 1 );
835 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200836 * and requested_alg is the same hash-and-sign family with any hash,
837 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200838 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
839 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
840 {
841 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
842 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
843 }
844 /* If it isn't permitted, it's forbidden. */
845 return( 0 );
846}
847
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100848/** Test whether a policy permits an algorithm.
849 *
850 * The caller must test usage flags separately.
851 */
852static int psa_key_policy_permits( const psa_key_policy_t *policy,
853 psa_algorithm_t alg )
854{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200855 return( psa_key_algorithm_permits( policy->alg, alg ) ||
856 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100857}
858
Gilles Peskinef603c712019-01-19 13:40:11 +0100859/** Restrict a key policy based on a constraint.
860 *
861 * \param[in,out] policy The policy to restrict.
862 * \param[in] constraint The policy constraint to apply.
863 *
864 * \retval #PSA_SUCCESS
865 * \c *policy contains the intersection of the original value of
866 * \c *policy and \c *constraint.
867 * \retval #PSA_ERROR_INVALID_ARGUMENT
868 * \c *policy and \c *constraint are incompatible.
869 * \c *policy is unchanged.
870 */
871static psa_status_t psa_restrict_key_policy(
872 psa_key_policy_t *policy,
873 const psa_key_policy_t *constraint )
874{
875 psa_algorithm_t intersection_alg =
876 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200877 psa_algorithm_t intersection_alg2 =
878 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100879 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
880 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200881 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
882 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100883 policy->usage &= constraint->usage;
884 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200885 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100886 return( PSA_SUCCESS );
887}
888
Darryl Green06fd18d2018-07-16 11:21:11 +0100889/** Retrieve a slot which must contain a key. The key must have allow all the
890 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
891 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100892static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100893 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100894 psa_key_usage_t usage,
895 psa_algorithm_t alg )
896{
897 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100898 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100899
900 *p_slot = NULL;
901
Gilles Peskinec5487a82018-12-03 18:08:14 +0100902 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100903 if( status != PSA_SUCCESS )
904 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100905
906 /* Enforce that usage policy for the key slot contains all the flags
907 * required by the usage parameter. There is one exception: public
908 * keys can always be exported, so we treat public key objects as
909 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200910 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100911 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200912 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100913 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100914
915 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200916 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100917 return( PSA_ERROR_NOT_PERMITTED );
918
919 *p_slot = slot;
920 return( PSA_SUCCESS );
921}
Darryl Green940d72c2018-07-13 13:18:51 +0100922
Gilles Peskine28f8f302019-07-24 13:30:31 +0200923/** Retrieve a slot which must contain a transparent key.
924 *
925 * A transparent key is a key for which the key material is directly
926 * available, as opposed to a key in a secure element.
927 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200928 * This is a temporary function to use instead of psa_get_key_from_slot()
929 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200930 */
931#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
932static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
933 psa_key_slot_t **p_slot,
934 psa_key_usage_t usage,
935 psa_algorithm_t alg )
936{
937 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
938 if( status != PSA_SUCCESS )
939 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200940 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200941 {
942 *p_slot = NULL;
943 return( PSA_ERROR_NOT_SUPPORTED );
944 }
945 return( PSA_SUCCESS );
946}
947#else /* MBEDTLS_PSA_CRYPTO_SE_C */
948/* With no secure element support, all keys are transparent. */
949#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
950 psa_get_key_from_slot( handle, p_slot, usage, alg )
951#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
952
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100953/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100954static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000955{
Gilles Peskine73167e12019-07-12 23:44:37 +0200956#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
957 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000958 {
959 /* No key material to clean. */
960 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200961 else
962#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200963 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000964 {
965 /* No key material to clean. */
966 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200967 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000968 {
969 mbedtls_free( slot->data.raw.data );
970 }
971 else
972#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200973 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000974 {
975 mbedtls_rsa_free( slot->data.rsa );
976 mbedtls_free( slot->data.rsa );
977 }
978 else
979#endif /* defined(MBEDTLS_RSA_C) */
980#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200981 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000982 {
983 mbedtls_ecp_keypair_free( slot->data.ecp );
984 mbedtls_free( slot->data.ecp );
985 }
986 else
987#endif /* defined(MBEDTLS_ECP_C) */
988 {
989 /* Shouldn't happen: the key type is not any type that we
990 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200991 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000992 }
993
994 return( PSA_SUCCESS );
995}
996
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100997/** Completely wipe a slot in memory, including its policy.
998 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100999psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001000{
1001 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001002 /* Multipart operations may still be using the key. This is safe
1003 * because all multipart operation objects are independent from
1004 * the key slot: if they need to access the key after the setup
1005 * phase, they have a copy of the key. Note that this means that
1006 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001007 /* At this point, key material and other type-specific content has
1008 * been wiped. Clear remaining metadata. We can call memset and not
1009 * zeroize because the metadata is not particularly sensitive. */
1010 memset( slot, 0, sizeof( *slot ) );
1011 return( status );
1012}
1013
Gilles Peskinec5487a82018-12-03 18:08:14 +01001014psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001015{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001016 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001017 psa_status_t status; /* status of the last operation */
1018 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001019#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1020 psa_se_drv_table_entry_t *driver;
1021#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001022
Gilles Peskine1841cf42019-10-08 15:48:25 +02001023 if( handle == 0 )
1024 return( PSA_SUCCESS );
1025
Gilles Peskinec5487a82018-12-03 18:08:14 +01001026 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001027 if( status != PSA_SUCCESS )
1028 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001029
1030#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001031 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001032 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001033 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001034 /* For a key in a secure element, we need to do three things:
1035 * remove the key file in internal storage, destroy the
1036 * key inside the secure element, and update the driver's
1037 * persistent data. Start a transaction that will encompass these
1038 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001039 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001040 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001041 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001042 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001043 status = psa_crypto_save_transaction( );
1044 if( status != PSA_SUCCESS )
1045 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001046 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001047 /* We should still try to destroy the key in the secure
1048 * element and the key metadata in storage. This is especially
1049 * important if the error is that the storage is full.
1050 * But how to do it exactly without risking an inconsistent
1051 * state after a reset?
1052 * https://github.com/ARMmbed/mbed-crypto/issues/215
1053 */
1054 overall_status = status;
1055 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001056 }
1057
Gilles Peskine354f7672019-07-12 23:46:38 +02001058 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001059 if( overall_status == PSA_SUCCESS )
1060 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001061 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001062#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1063
Darryl Greend49a4992018-06-18 17:27:26 +01001064#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001065 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001066 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001067 status = psa_destroy_persistent_key( slot->attr.id );
1068 if( overall_status == PSA_SUCCESS )
1069 overall_status = status;
1070
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001071 /* TODO: other slots may have a copy of the same key. We should
1072 * invalidate them.
1073 * https://github.com/ARMmbed/mbed-crypto/issues/214
1074 */
Darryl Greend49a4992018-06-18 17:27:26 +01001075 }
1076#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001077
Gilles Peskinefc762652019-07-22 19:30:34 +02001078#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1079 if( driver != NULL )
1080 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001081 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001082 if( overall_status == PSA_SUCCESS )
1083 overall_status = status;
1084 status = psa_crypto_stop_transaction( );
1085 if( overall_status == PSA_SUCCESS )
1086 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001087 }
1088#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1089
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001090#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1091exit:
1092#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001093 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001094 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1095 if( overall_status == PSA_SUCCESS )
1096 overall_status = status;
1097 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001098}
1099
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001100void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001101{
Gilles Peskineb699f072019-04-26 16:06:02 +02001102 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001103 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001104}
1105
Gilles Peskineb699f072019-04-26 16:06:02 +02001106psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1107 psa_key_type_t type,
1108 const uint8_t *data,
1109 size_t data_length )
1110{
1111 uint8_t *copy = NULL;
1112
1113 if( data_length != 0 )
1114 {
1115 copy = mbedtls_calloc( 1, data_length );
1116 if( copy == NULL )
1117 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1118 memcpy( copy, data, data_length );
1119 }
1120 /* After this point, this function is guaranteed to succeed, so it
1121 * can start modifying `*attributes`. */
1122
1123 if( attributes->domain_parameters != NULL )
1124 {
1125 mbedtls_free( attributes->domain_parameters );
1126 attributes->domain_parameters = NULL;
1127 attributes->domain_parameters_size = 0;
1128 }
1129
1130 attributes->domain_parameters = copy;
1131 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001132 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001133 return( PSA_SUCCESS );
1134}
1135
1136psa_status_t psa_get_key_domain_parameters(
1137 const psa_key_attributes_t *attributes,
1138 uint8_t *data, size_t data_size, size_t *data_length )
1139{
1140 if( attributes->domain_parameters_size > data_size )
1141 return( PSA_ERROR_BUFFER_TOO_SMALL );
1142 *data_length = attributes->domain_parameters_size;
1143 if( attributes->domain_parameters_size != 0 )
1144 memcpy( data, attributes->domain_parameters,
1145 attributes->domain_parameters_size );
1146 return( PSA_SUCCESS );
1147}
1148
1149#if defined(MBEDTLS_RSA_C)
1150static psa_status_t psa_get_rsa_public_exponent(
1151 const mbedtls_rsa_context *rsa,
1152 psa_key_attributes_t *attributes )
1153{
1154 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001155 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001156 uint8_t *buffer = NULL;
1157 size_t buflen;
1158 mbedtls_mpi_init( &mpi );
1159
1160 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1161 if( ret != 0 )
1162 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001163 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1164 {
1165 /* It's the default value, which is reported as an empty string,
1166 * so there's nothing to do. */
1167 goto exit;
1168 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001169
1170 buflen = mbedtls_mpi_size( &mpi );
1171 buffer = mbedtls_calloc( 1, buflen );
1172 if( buffer == NULL )
1173 {
1174 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1175 goto exit;
1176 }
1177 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1178 if( ret != 0 )
1179 goto exit;
1180 attributes->domain_parameters = buffer;
1181 attributes->domain_parameters_size = buflen;
1182
1183exit:
1184 mbedtls_mpi_free( &mpi );
1185 if( ret != 0 )
1186 mbedtls_free( buffer );
1187 return( mbedtls_to_psa_error( ret ) );
1188}
1189#endif /* MBEDTLS_RSA_C */
1190
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001191/** Retrieve all the publicly-accessible attributes of a key.
1192 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001193psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1194 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001195{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001196 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001197 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001198
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001199 psa_reset_key_attributes( attributes );
1200
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001201 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001202 if( status != PSA_SUCCESS )
1203 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001204
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001205 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001206 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1207 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1208
1209#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1210 if( psa_key_slot_is_external( slot ) )
1211 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1212#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001213
Gilles Peskine8e338702019-07-30 20:06:31 +02001214 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001215 {
1216#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001217 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001218 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001219#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001220 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001221 * is not yet implemented.
1222 * https://github.com/ARMmbed/mbed-crypto/issues/216
1223 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001224 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001225 break;
1226#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001227 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1228 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001229#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001230 default:
1231 /* Nothing else to do. */
1232 break;
1233 }
1234
1235 if( status != PSA_SUCCESS )
1236 psa_reset_key_attributes( attributes );
1237 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001238}
1239
Gilles Peskinec8000c02019-08-02 20:15:51 +02001240#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1241psa_status_t psa_get_key_slot_number(
1242 const psa_key_attributes_t *attributes,
1243 psa_key_slot_number_t *slot_number )
1244{
1245 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1246 {
1247 *slot_number = attributes->slot_number;
1248 return( PSA_SUCCESS );
1249 }
1250 else
1251 return( PSA_ERROR_INVALID_ARGUMENT );
1252}
1253#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1254
Jaeden Ameroccdce902019-01-10 11:42:27 +00001255#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001256static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1257 unsigned char *buf, size_t size )
1258{
Janos Follath24eed8d2019-11-22 13:21:35 +00001259 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001260 unsigned char *c;
1261 size_t len = 0;
1262
1263 c = buf + size;
1264
1265 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1266
1267 return( (int) len );
1268}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001269#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001270
Gilles Peskinef603c712019-01-19 13:40:11 +01001271static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1272 uint8_t *data,
1273 size_t data_size,
1274 size_t *data_length,
1275 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001276{
Gilles Peskine5d309672019-07-12 23:47:28 +02001277#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1278 const psa_drv_se_t *drv;
1279 psa_drv_se_context_t *drv_context;
1280#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1281
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001282 *data_length = 0;
1283
Gilles Peskine8e338702019-07-30 20:06:31 +02001284 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001285 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001286
Gilles Peskinef9168942019-09-12 19:20:29 +02001287 /* Reject a zero-length output buffer now, since this can never be a
1288 * valid key representation. This way we know that data must be a valid
1289 * pointer and we can do things like memset(data, ..., data_size). */
1290 if( data_size == 0 )
1291 return( PSA_ERROR_BUFFER_TOO_SMALL );
1292
Gilles Peskine5d309672019-07-12 23:47:28 +02001293#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001294 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001295 {
1296 psa_drv_se_export_key_t method;
1297 if( drv->key_management == NULL )
1298 return( PSA_ERROR_NOT_SUPPORTED );
1299 method = ( export_public_key ?
1300 drv->key_management->p_export_public :
1301 drv->key_management->p_export );
1302 if( method == NULL )
1303 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001304 return( method( drv_context,
1305 slot->data.se.slot_number,
1306 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001307 }
1308#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1309
Gilles Peskine8e338702019-07-30 20:06:31 +02001310 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001311 {
1312 if( slot->data.raw.bytes > data_size )
1313 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001314 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1315 memset( data + slot->data.raw.bytes, 0,
1316 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001317 *data_length = slot->data.raw.bytes;
1318 return( PSA_SUCCESS );
1319 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001320#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001321 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001322 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001323 psa_status_t status;
Steven Cooremane3fd3922020-06-11 16:50:36 +02001324 size_t actual_data_size;
Darryl Greendd8fb772018-11-07 16:00:44 +00001325
Gilles Peskineb46bef22019-07-30 21:32:04 +02001326 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001327 if( bytes > data_size )
1328 return( PSA_ERROR_BUFFER_TOO_SMALL );
1329 status = mbedtls_to_psa_error(
Steven Cooremane3fd3922020-06-11 16:50:36 +02001330 mbedtls_ecp_write_key(slot->data.ecp->grp.id, slot->data.ecp,
1331 &actual_data_size, data, bytes) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001332 if( status != PSA_SUCCESS )
1333 return( status );
1334 memset( data + bytes, 0, data_size - bytes );
1335 *data_length = bytes;
1336 return( PSA_SUCCESS );
1337 }
1338#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001339 else
Moran Peker17e36e12018-05-02 12:55:20 +03001340 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001341#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001342 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1343 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001344 {
Moran Pekera998bc62018-04-16 18:16:20 +03001345 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001346 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001347 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001348 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001349#if defined(MBEDTLS_RSA_C)
1350 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001351 pk.pk_info = &mbedtls_rsa_info;
1352 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001353#else
1354 return( PSA_ERROR_NOT_SUPPORTED );
1355#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001356 }
1357 else
1358 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001359#if defined(MBEDTLS_ECP_C)
1360 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001361 pk.pk_info = &mbedtls_eckey_info;
1362 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001363#else
1364 return( PSA_ERROR_NOT_SUPPORTED );
1365#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001366 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001367 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001368 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001369 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001370 }
Moran Peker17e36e12018-05-02 12:55:20 +03001371 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001372 {
Moran Peker17e36e12018-05-02 12:55:20 +03001373 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001374 }
Moran Peker60364322018-04-29 11:34:58 +03001375 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001376 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001377 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001378 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001379 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001380 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1381 * Move the data to the beginning and erase remaining data
1382 * at the original location. */
1383 if( 2 * (size_t) ret <= data_size )
1384 {
1385 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001386 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001387 }
1388 else if( (size_t) ret < data_size )
1389 {
1390 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001391 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001392 }
Moran Pekera998bc62018-04-16 18:16:20 +03001393 *data_length = ret;
1394 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001395 }
1396 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001397#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001398 {
1399 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001400 it is valid for a special-purpose implementation to omit
1401 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001402 return( PSA_ERROR_NOT_SUPPORTED );
1403 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001404 }
1405}
1406
Gilles Peskinec5487a82018-12-03 18:08:14 +01001407psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001408 uint8_t *data,
1409 size_t data_size,
1410 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001411{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001412 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001413 psa_status_t status;
1414
1415 /* Set the key to empty now, so that even when there are errors, we always
1416 * set data_length to a value between 0 and data_size. On error, setting
1417 * the key to empty is a good choice because an empty key representation is
1418 * unlikely to be accepted anywhere. */
1419 *data_length = 0;
1420
1421 /* Export requires the EXPORT flag. There is an exception for public keys,
1422 * which don't require any flag, but psa_get_key_from_slot takes
1423 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001424 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001425 if( status != PSA_SUCCESS )
1426 return( status );
1427 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001428 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001429}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001430
Gilles Peskinec5487a82018-12-03 18:08:14 +01001431psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001432 uint8_t *data,
1433 size_t data_size,
1434 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001435{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001436 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001437 psa_status_t status;
1438
1439 /* Set the key to empty now, so that even when there are errors, we always
1440 * set data_length to a value between 0 and data_size. On error, setting
1441 * the key to empty is a good choice because an empty key representation is
1442 * unlikely to be accepted anywhere. */
1443 *data_length = 0;
1444
1445 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001446 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001447 if( status != PSA_SUCCESS )
1448 return( status );
1449 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001450 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001451}
1452
Gilles Peskine91e8c332019-08-02 19:19:39 +02001453#if defined(static_assert)
1454static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1455 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001456static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001457 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001458static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001459 "One or more key attribute flag is listed as both internal-only and external-only" );
1460#endif
1461
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001462/** Validate that a key policy is internally well-formed.
1463 *
1464 * This function only rejects invalid policies. It does not validate the
1465 * consistency of the policy with respect to other attributes of the key
1466 * such as the key type.
1467 */
1468static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001469{
1470 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001471 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001472 PSA_KEY_USAGE_ENCRYPT |
1473 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001474 PSA_KEY_USAGE_SIGN_HASH |
1475 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001476 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1477 return( PSA_ERROR_INVALID_ARGUMENT );
1478
Gilles Peskine4747d192019-04-17 15:05:45 +02001479 return( PSA_SUCCESS );
1480}
1481
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001482/** Validate the internal consistency of key attributes.
1483 *
1484 * This function only rejects invalid attribute values. If does not
1485 * validate the consistency of the attributes with any key data that may
1486 * be involved in the creation of the key.
1487 *
1488 * Call this function early in the key creation process.
1489 *
1490 * \param[in] attributes Key attributes for the new key.
1491 * \param[out] p_drv On any return, the driver for the key, if any.
1492 * NULL for a transparent key.
1493 *
1494 */
1495static psa_status_t psa_validate_key_attributes(
1496 const psa_key_attributes_t *attributes,
1497 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001498{
1499 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001500
1501 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001502 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001503 status = psa_validate_persistent_key_parameters(
1504 attributes->core.lifetime, attributes->core.id,
1505 p_drv, 1 );
1506 if( status != PSA_SUCCESS )
1507 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001508 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001509
1510 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001511 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001512 return( status );
1513
1514 /* Refuse to create overly large keys.
1515 * Note that this doesn't trigger on import if the attributes don't
1516 * explicitly specify a size (so psa_get_key_bits returns 0), so
1517 * psa_import_key() needs its own checks. */
1518 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1519 return( PSA_ERROR_NOT_SUPPORTED );
1520
Gilles Peskine91e8c332019-08-02 19:19:39 +02001521 /* Reject invalid flags. These should not be reachable through the API. */
1522 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1523 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1524 return( PSA_ERROR_INVALID_ARGUMENT );
1525
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001526 return( PSA_SUCCESS );
1527}
1528
Gilles Peskine4747d192019-04-17 15:05:45 +02001529/** Prepare a key slot to receive key material.
1530 *
1531 * This function allocates a key slot and sets its metadata.
1532 *
1533 * If this function fails, call psa_fail_key_creation().
1534 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001535 * This function is intended to be used as follows:
1536 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1537 * it with the specified attributes, and assign it a handle.
1538 * -# Populate the slot with the key material.
1539 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1540 * In case of failure at any step, stop the sequence and call
1541 * psa_fail_key_creation().
1542 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001543 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001544 * \param[in] attributes Key attributes for the new key.
1545 * \param[out] handle On success, a handle for the allocated slot.
1546 * \param[out] p_slot On success, a pointer to the prepared slot.
1547 * \param[out] p_drv On any return, the driver for the key, if any.
1548 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001549 *
1550 * \retval #PSA_SUCCESS
1551 * The key slot is ready to receive key material.
1552 * \return If this function fails, the key slot is an invalid state.
1553 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001554 */
1555static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001556 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001557 const psa_key_attributes_t *attributes,
1558 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001559 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001560 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001561{
1562 psa_status_t status;
1563 psa_key_slot_t *slot;
1564
Gilles Peskinedf179142019-07-15 22:02:14 +02001565 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001566 *p_drv = NULL;
1567
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001568 status = psa_validate_key_attributes( attributes, p_drv );
1569 if( status != PSA_SUCCESS )
1570 return( status );
1571
Gilles Peskineedbed562019-08-07 18:19:59 +02001572 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001573 if( status != PSA_SUCCESS )
1574 return( status );
1575 slot = *p_slot;
1576
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001577 /* We're storing the declared bit-size of the key. It's up to each
1578 * creation mechanism to verify that this information is correct.
1579 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001580 * an input (generate, device) but not for those where the bit-size
1581 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001582
1583 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001584
Gilles Peskine91e8c332019-08-02 19:19:39 +02001585 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001586 * external-only flags, query `attributes`. Thanks to the check
1587 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001588 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001589 * may have set. */
1590 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001591
Gilles Peskinecbaff462019-07-12 23:46:04 +02001592#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001593 /* For a key in a secure element, we need to do three things
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001594 * when creating or registering a key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001595 * create the key file in internal storage, create the
1596 * key inside the secure element, and update the driver's
1597 * persistent data. Start a transaction that will encompass these
1598 * three actions. */
1599 /* The first thing to do is to find a slot number for the new key.
1600 * We save the slot number in persistent storage as part of the
1601 * transaction data. It will be needed to recover if the power
1602 * fails during the key creation process, to clean up on the secure
1603 * element side after restarting. Obtaining a slot number from the
1604 * secure element driver updates its persistent state, but we do not yet
1605 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001606 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001607 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001608 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001609 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001610 &slot->data.se.slot_number );
1611 if( status != PSA_SUCCESS )
1612 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001613 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001614 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001615 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001616 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001617 status = psa_crypto_save_transaction( );
1618 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001619 {
1620 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001621 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001622 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001623 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001624
1625 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1626 {
1627 /* Key registration only makes sense with a secure element. */
1628 return( PSA_ERROR_INVALID_ARGUMENT );
1629 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001630#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1631
Darryl Green0c6575a2018-11-07 16:05:30 +00001632 return( status );
1633}
Gilles Peskine4747d192019-04-17 15:05:45 +02001634
1635/** Finalize the creation of a key once its key material has been set.
1636 *
1637 * This entails writing the key to persistent storage.
1638 *
1639 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001640 * See the documentation of psa_start_key_creation() for the intended use
1641 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001642 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001643 * \param[in,out] slot Pointer to the slot with key material.
1644 * \param[in] driver The secure element driver for the key,
1645 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001646 *
1647 * \retval #PSA_SUCCESS
1648 * The key was successfully created. The handle is now valid.
1649 * \return If this function fails, the key slot is an invalid state.
1650 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001651 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001652static psa_status_t psa_finish_key_creation(
1653 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001654 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001655{
1656 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001657 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001658 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001659
1660#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001661 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001662 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001663#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1664 if( driver != NULL )
1665 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001666 psa_se_key_data_storage_t data;
1667#if defined(static_assert)
1668 static_assert( sizeof( slot->data.se.slot_number ) ==
1669 sizeof( data.slot_number ),
1670 "Slot number size does not match psa_se_key_data_storage_t" );
1671 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1672 "Bit-size size does not match psa_se_key_data_storage_t" );
1673#endif
1674 memcpy( &data.slot_number, &slot->data.se.slot_number,
1675 sizeof( slot->data.se.slot_number ) );
1676 memcpy( &data.bits, &slot->attr.bits,
1677 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001678 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001679 (uint8_t*) &data,
1680 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001681 }
1682 else
1683#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1684 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001685 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001686 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001687 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001688 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1689 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001690 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001691 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1692 status = psa_internal_export_key( slot,
1693 buffer, buffer_size, &length,
1694 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001695 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001696 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001697 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001698
Gilles Peskinef9168942019-09-12 19:20:29 +02001699 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001700 mbedtls_free( buffer );
1701 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001702 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001703#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1704
Gilles Peskinecbaff462019-07-12 23:46:04 +02001705#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001706 /* Finish the transaction for a key creation. This does not
1707 * happen when registering an existing key. Detect this case
1708 * by checking whether a transaction is in progress (actual
1709 * creation of a key in a secure element requires a transaction,
1710 * but registration doesn't use one). */
1711 if( driver != NULL &&
1712 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001713 {
1714 status = psa_save_se_persistent_data( driver );
1715 if( status != PSA_SUCCESS )
1716 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001717 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001718 return( status );
1719 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001720 status = psa_crypto_stop_transaction( );
1721 if( status != PSA_SUCCESS )
1722 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001723 }
1724#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1725
Gilles Peskine4747d192019-04-17 15:05:45 +02001726 return( status );
1727}
1728
1729/** Abort the creation of a key.
1730 *
1731 * You may call this function after calling psa_start_key_creation(),
1732 * or after psa_finish_key_creation() fails. In other circumstances, this
1733 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001734 * See the documentation of psa_start_key_creation() for the intended use
1735 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001736 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001737 * \param[in,out] slot Pointer to the slot with key material.
1738 * \param[in] driver The secure element driver for the key,
1739 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001740 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001741static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001742 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001743{
Gilles Peskine011e4282019-06-26 18:34:38 +02001744 (void) driver;
1745
Gilles Peskine4747d192019-04-17 15:05:45 +02001746 if( slot == NULL )
1747 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001748
1749#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001750 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001751 * element, and the failure happened later (when saving metadata
1752 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001753 * element.
1754 * https://github.com/ARMmbed/mbed-crypto/issues/217
1755 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001756
Gilles Peskined7729582019-08-05 15:55:54 +02001757 /* Abort the ongoing transaction if any (there may not be one if
1758 * the creation process failed before starting one, or if the
1759 * key creation is a registration of a key in a secure element).
1760 * Earlier functions must already have done what it takes to undo any
1761 * partial creation. All that's left is to update the transaction data
1762 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001763 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001764#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1765
Gilles Peskine4747d192019-04-17 15:05:45 +02001766 psa_wipe_key_slot( slot );
1767}
1768
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001769/** Validate optional attributes during key creation.
1770 *
1771 * Some key attributes are optional during key creation. If they are
1772 * specified in the attributes structure, check that they are consistent
1773 * with the data in the slot.
1774 *
1775 * This function should be called near the end of key creation, after
1776 * the slot in memory is fully populated but before saving persistent data.
1777 */
1778static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001779 const psa_key_slot_t *slot,
1780 const psa_key_attributes_t *attributes )
1781{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001782 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001783 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001784 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001785 return( PSA_ERROR_INVALID_ARGUMENT );
1786 }
1787
1788 if( attributes->domain_parameters_size != 0 )
1789 {
1790#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001791 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001792 {
1793 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001794 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001795 mbedtls_mpi_init( &actual );
1796 mbedtls_mpi_init( &required );
1797 ret = mbedtls_rsa_export( slot->data.rsa,
1798 NULL, NULL, NULL, NULL, &actual );
1799 if( ret != 0 )
1800 goto rsa_exit;
1801 ret = mbedtls_mpi_read_binary( &required,
1802 attributes->domain_parameters,
1803 attributes->domain_parameters_size );
1804 if( ret != 0 )
1805 goto rsa_exit;
1806 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1807 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1808 rsa_exit:
1809 mbedtls_mpi_free( &actual );
1810 mbedtls_mpi_free( &required );
1811 if( ret != 0)
1812 return( mbedtls_to_psa_error( ret ) );
1813 }
1814 else
1815#endif
1816 {
1817 return( PSA_ERROR_INVALID_ARGUMENT );
1818 }
1819 }
1820
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001821 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001822 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001823 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001824 return( PSA_ERROR_INVALID_ARGUMENT );
1825 }
1826
1827 return( PSA_SUCCESS );
1828}
1829
Gilles Peskine4747d192019-04-17 15:05:45 +02001830psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001831 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001832 size_t data_length,
1833 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001834{
1835 psa_status_t status;
1836 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001837 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001838
Gilles Peskine0f84d622019-09-12 19:03:13 +02001839 /* Reject zero-length symmetric keys (including raw data key objects).
1840 * This also rejects any key which might be encoded as an empty string,
1841 * which is never valid. */
1842 if( data_length == 0 )
1843 return( PSA_ERROR_INVALID_ARGUMENT );
1844
Gilles Peskinedf179142019-07-15 22:02:14 +02001845 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1846 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001847 if( status != PSA_SUCCESS )
1848 goto exit;
1849
Gilles Peskine5d309672019-07-12 23:47:28 +02001850#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1851 if( driver != NULL )
1852 {
1853 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001854 /* The driver should set the number of key bits, however in
1855 * case it doesn't, we initialize bits to an invalid value. */
1856 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001857 if( drv->key_management == NULL ||
1858 drv->key_management->p_import == NULL )
1859 {
1860 status = PSA_ERROR_NOT_SUPPORTED;
1861 goto exit;
1862 }
1863 status = drv->key_management->p_import(
1864 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001865 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001866 &bits );
1867 if( status != PSA_SUCCESS )
1868 goto exit;
1869 if( bits > PSA_MAX_KEY_BITS )
1870 {
1871 status = PSA_ERROR_NOT_SUPPORTED;
1872 goto exit;
1873 }
1874 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001875 }
1876 else
1877#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1878 {
1879 status = psa_import_key_into_slot( slot, data, data_length );
1880 if( status != PSA_SUCCESS )
1881 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001882 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001883 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001884 if( status != PSA_SUCCESS )
1885 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001886
Gilles Peskine011e4282019-06-26 18:34:38 +02001887 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001888exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001889 if( status != PSA_SUCCESS )
1890 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001891 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001892 *handle = 0;
1893 }
1894 return( status );
1895}
1896
Gilles Peskined7729582019-08-05 15:55:54 +02001897#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1898psa_status_t mbedtls_psa_register_se_key(
1899 const psa_key_attributes_t *attributes )
1900{
1901 psa_status_t status;
1902 psa_key_slot_t *slot = NULL;
1903 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001904 psa_key_handle_t handle = 0;
1905
1906 /* Leaving attributes unspecified is not currently supported.
1907 * It could make sense to query the key type and size from the
1908 * secure element, but not all secure elements support this
1909 * and the driver HAL doesn't currently support it. */
1910 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1911 return( PSA_ERROR_NOT_SUPPORTED );
1912 if( psa_get_key_bits( attributes ) == 0 )
1913 return( PSA_ERROR_NOT_SUPPORTED );
1914
1915 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1916 &handle, &slot, &driver );
1917 if( status != PSA_SUCCESS )
1918 goto exit;
1919
Gilles Peskined7729582019-08-05 15:55:54 +02001920 status = psa_finish_key_creation( slot, driver );
1921
1922exit:
1923 if( status != PSA_SUCCESS )
1924 {
1925 psa_fail_key_creation( slot, driver );
1926 }
1927 /* Registration doesn't keep the key in RAM. */
1928 psa_close_key( handle );
1929 return( status );
1930}
1931#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1932
Gilles Peskinef603c712019-01-19 13:40:11 +01001933static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001934 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001935{
1936 psa_status_t status;
1937 uint8_t *buffer = NULL;
1938 size_t buffer_size = 0;
1939 size_t length;
1940
Gilles Peskine8e338702019-07-30 20:06:31 +02001941 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001942 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001943 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001944 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001945 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001946 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1947 if( status != PSA_SUCCESS )
1948 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001949 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001950 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001951
1952exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001953 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001954 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001955 return( status );
1956}
1957
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001958psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1959 const psa_key_attributes_t *specified_attributes,
1960 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001961{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001962 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001963 psa_key_slot_t *source_slot = NULL;
1964 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001965 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001966 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001967
Gilles Peskine28f8f302019-07-24 13:30:31 +02001968 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001969 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001970 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001971 goto exit;
1972
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001973 status = psa_validate_optional_attributes( source_slot,
1974 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001975 if( status != PSA_SUCCESS )
1976 goto exit;
1977
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001978 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001979 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001980 if( status != PSA_SUCCESS )
1981 goto exit;
1982
Gilles Peskinedf179142019-07-15 22:02:14 +02001983 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1984 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001985 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001986 if( status != PSA_SUCCESS )
1987 goto exit;
1988
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001989#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1990 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01001991 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001992 /* Copying to a secure element is not implemented yet. */
1993 status = PSA_ERROR_NOT_SUPPORTED;
1994 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001995 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001996#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01001997
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001998 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01001999 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002000 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002001
Gilles Peskine011e4282019-06-26 18:34:38 +02002002 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002003exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002004 if( status != PSA_SUCCESS )
2005 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002006 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002007 *target_handle = 0;
2008 }
2009 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002010}
2011
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002012
2013
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002014/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002015/* Message digests */
2016/****************************************************************/
2017
Gilles Peskineb16841e2019-10-10 20:36:12 +02002018#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002019static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002020{
2021 switch( alg )
2022 {
2023#if defined(MBEDTLS_MD2_C)
2024 case PSA_ALG_MD2:
2025 return( &mbedtls_md2_info );
2026#endif
2027#if defined(MBEDTLS_MD4_C)
2028 case PSA_ALG_MD4:
2029 return( &mbedtls_md4_info );
2030#endif
2031#if defined(MBEDTLS_MD5_C)
2032 case PSA_ALG_MD5:
2033 return( &mbedtls_md5_info );
2034#endif
2035#if defined(MBEDTLS_RIPEMD160_C)
2036 case PSA_ALG_RIPEMD160:
2037 return( &mbedtls_ripemd160_info );
2038#endif
2039#if defined(MBEDTLS_SHA1_C)
2040 case PSA_ALG_SHA_1:
2041 return( &mbedtls_sha1_info );
2042#endif
2043#if defined(MBEDTLS_SHA256_C)
2044 case PSA_ALG_SHA_224:
2045 return( &mbedtls_sha224_info );
2046 case PSA_ALG_SHA_256:
2047 return( &mbedtls_sha256_info );
2048#endif
2049#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002050#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002051 case PSA_ALG_SHA_384:
2052 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002053#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002054 case PSA_ALG_SHA_512:
2055 return( &mbedtls_sha512_info );
2056#endif
2057 default:
2058 return( NULL );
2059 }
2060}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002061#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002062
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002063psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2064{
2065 switch( operation->alg )
2066 {
Gilles Peskine81736312018-06-26 15:04:31 +02002067 case 0:
2068 /* The object has (apparently) been initialized but it is not
2069 * in use. It's ok to call abort on such an object, and there's
2070 * nothing to do. */
2071 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002072#if defined(MBEDTLS_MD2_C)
2073 case PSA_ALG_MD2:
2074 mbedtls_md2_free( &operation->ctx.md2 );
2075 break;
2076#endif
2077#if defined(MBEDTLS_MD4_C)
2078 case PSA_ALG_MD4:
2079 mbedtls_md4_free( &operation->ctx.md4 );
2080 break;
2081#endif
2082#if defined(MBEDTLS_MD5_C)
2083 case PSA_ALG_MD5:
2084 mbedtls_md5_free( &operation->ctx.md5 );
2085 break;
2086#endif
2087#if defined(MBEDTLS_RIPEMD160_C)
2088 case PSA_ALG_RIPEMD160:
2089 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2090 break;
2091#endif
2092#if defined(MBEDTLS_SHA1_C)
2093 case PSA_ALG_SHA_1:
2094 mbedtls_sha1_free( &operation->ctx.sha1 );
2095 break;
2096#endif
2097#if defined(MBEDTLS_SHA256_C)
2098 case PSA_ALG_SHA_224:
2099 case PSA_ALG_SHA_256:
2100 mbedtls_sha256_free( &operation->ctx.sha256 );
2101 break;
2102#endif
2103#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002104#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002105 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002106#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002107 case PSA_ALG_SHA_512:
2108 mbedtls_sha512_free( &operation->ctx.sha512 );
2109 break;
2110#endif
2111 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002112 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002113 }
2114 operation->alg = 0;
2115 return( PSA_SUCCESS );
2116}
2117
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002118psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002119 psa_algorithm_t alg )
2120{
Janos Follath24eed8d2019-11-22 13:21:35 +00002121 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002122
2123 /* A context must be freshly initialized before it can be set up. */
2124 if( operation->alg != 0 )
2125 {
2126 return( PSA_ERROR_BAD_STATE );
2127 }
2128
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002129 switch( alg )
2130 {
2131#if defined(MBEDTLS_MD2_C)
2132 case PSA_ALG_MD2:
2133 mbedtls_md2_init( &operation->ctx.md2 );
2134 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2135 break;
2136#endif
2137#if defined(MBEDTLS_MD4_C)
2138 case PSA_ALG_MD4:
2139 mbedtls_md4_init( &operation->ctx.md4 );
2140 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2141 break;
2142#endif
2143#if defined(MBEDTLS_MD5_C)
2144 case PSA_ALG_MD5:
2145 mbedtls_md5_init( &operation->ctx.md5 );
2146 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2147 break;
2148#endif
2149#if defined(MBEDTLS_RIPEMD160_C)
2150 case PSA_ALG_RIPEMD160:
2151 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2152 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2153 break;
2154#endif
2155#if defined(MBEDTLS_SHA1_C)
2156 case PSA_ALG_SHA_1:
2157 mbedtls_sha1_init( &operation->ctx.sha1 );
2158 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2159 break;
2160#endif
2161#if defined(MBEDTLS_SHA256_C)
2162 case PSA_ALG_SHA_224:
2163 mbedtls_sha256_init( &operation->ctx.sha256 );
2164 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2165 break;
2166 case PSA_ALG_SHA_256:
2167 mbedtls_sha256_init( &operation->ctx.sha256 );
2168 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2169 break;
2170#endif
2171#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002172#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002173 case PSA_ALG_SHA_384:
2174 mbedtls_sha512_init( &operation->ctx.sha512 );
2175 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2176 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002177#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002178 case PSA_ALG_SHA_512:
2179 mbedtls_sha512_init( &operation->ctx.sha512 );
2180 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2181 break;
2182#endif
2183 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002184 return( PSA_ALG_IS_HASH( alg ) ?
2185 PSA_ERROR_NOT_SUPPORTED :
2186 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002187 }
2188 if( ret == 0 )
2189 operation->alg = alg;
2190 else
2191 psa_hash_abort( operation );
2192 return( mbedtls_to_psa_error( ret ) );
2193}
2194
2195psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2196 const uint8_t *input,
2197 size_t input_length )
2198{
Janos Follath24eed8d2019-11-22 13:21:35 +00002199 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002200
2201 /* Don't require hash implementations to behave correctly on a
2202 * zero-length input, which may have an invalid pointer. */
2203 if( input_length == 0 )
2204 return( PSA_SUCCESS );
2205
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002206 switch( operation->alg )
2207 {
2208#if defined(MBEDTLS_MD2_C)
2209 case PSA_ALG_MD2:
2210 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2211 input, input_length );
2212 break;
2213#endif
2214#if defined(MBEDTLS_MD4_C)
2215 case PSA_ALG_MD4:
2216 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2217 input, input_length );
2218 break;
2219#endif
2220#if defined(MBEDTLS_MD5_C)
2221 case PSA_ALG_MD5:
2222 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2223 input, input_length );
2224 break;
2225#endif
2226#if defined(MBEDTLS_RIPEMD160_C)
2227 case PSA_ALG_RIPEMD160:
2228 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2229 input, input_length );
2230 break;
2231#endif
2232#if defined(MBEDTLS_SHA1_C)
2233 case PSA_ALG_SHA_1:
2234 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2235 input, input_length );
2236 break;
2237#endif
2238#if defined(MBEDTLS_SHA256_C)
2239 case PSA_ALG_SHA_224:
2240 case PSA_ALG_SHA_256:
2241 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2242 input, input_length );
2243 break;
2244#endif
2245#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002246#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002247 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002248#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002249 case PSA_ALG_SHA_512:
2250 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2251 input, input_length );
2252 break;
2253#endif
2254 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002255 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002256 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002257
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002258 if( ret != 0 )
2259 psa_hash_abort( operation );
2260 return( mbedtls_to_psa_error( ret ) );
2261}
2262
2263psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2264 uint8_t *hash,
2265 size_t hash_size,
2266 size_t *hash_length )
2267{
itayzafrir40835d42018-08-02 13:14:17 +03002268 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002269 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002270 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002271
2272 /* Fill the output buffer with something that isn't a valid hash
2273 * (barring an attack on the hash and deliberately-crafted input),
2274 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002275 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002276 /* If hash_size is 0 then hash may be NULL and then the
2277 * call to memset would have undefined behavior. */
2278 if( hash_size != 0 )
2279 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002280
2281 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002282 {
2283 status = PSA_ERROR_BUFFER_TOO_SMALL;
2284 goto exit;
2285 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002286
2287 switch( operation->alg )
2288 {
2289#if defined(MBEDTLS_MD2_C)
2290 case PSA_ALG_MD2:
2291 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2292 break;
2293#endif
2294#if defined(MBEDTLS_MD4_C)
2295 case PSA_ALG_MD4:
2296 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2297 break;
2298#endif
2299#if defined(MBEDTLS_MD5_C)
2300 case PSA_ALG_MD5:
2301 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2302 break;
2303#endif
2304#if defined(MBEDTLS_RIPEMD160_C)
2305 case PSA_ALG_RIPEMD160:
2306 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2307 break;
2308#endif
2309#if defined(MBEDTLS_SHA1_C)
2310 case PSA_ALG_SHA_1:
2311 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2312 break;
2313#endif
2314#if defined(MBEDTLS_SHA256_C)
2315 case PSA_ALG_SHA_224:
2316 case PSA_ALG_SHA_256:
2317 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2318 break;
2319#endif
2320#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002321#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002322 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002323#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002324 case PSA_ALG_SHA_512:
2325 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2326 break;
2327#endif
2328 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002329 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002330 }
itayzafrir40835d42018-08-02 13:14:17 +03002331 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002332
itayzafrir40835d42018-08-02 13:14:17 +03002333exit:
2334 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002335 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002336 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002337 return( psa_hash_abort( operation ) );
2338 }
2339 else
2340 {
2341 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002342 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002343 }
2344}
2345
Gilles Peskine2d277862018-06-18 15:41:12 +02002346psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2347 const uint8_t *hash,
2348 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002349{
2350 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2351 size_t actual_hash_length;
2352 psa_status_t status = psa_hash_finish( operation,
2353 actual_hash, sizeof( actual_hash ),
2354 &actual_hash_length );
2355 if( status != PSA_SUCCESS )
2356 return( status );
2357 if( actual_hash_length != hash_length )
2358 return( PSA_ERROR_INVALID_SIGNATURE );
2359 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2360 return( PSA_ERROR_INVALID_SIGNATURE );
2361 return( PSA_SUCCESS );
2362}
2363
Gilles Peskine0a749c82019-11-28 19:33:58 +01002364psa_status_t psa_hash_compute( psa_algorithm_t alg,
2365 const uint8_t *input, size_t input_length,
2366 uint8_t *hash, size_t hash_size,
2367 size_t *hash_length )
2368{
2369 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2370 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2371
2372 *hash_length = hash_size;
2373 status = psa_hash_setup( &operation, alg );
2374 if( status != PSA_SUCCESS )
2375 goto exit;
2376 status = psa_hash_update( &operation, input, input_length );
2377 if( status != PSA_SUCCESS )
2378 goto exit;
2379 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2380 if( status != PSA_SUCCESS )
2381 goto exit;
2382
2383exit:
2384 if( status == PSA_SUCCESS )
2385 status = psa_hash_abort( &operation );
2386 else
2387 psa_hash_abort( &operation );
2388 return( status );
2389}
2390
2391psa_status_t psa_hash_compare( psa_algorithm_t alg,
2392 const uint8_t *input, size_t input_length,
2393 const uint8_t *hash, size_t hash_length )
2394{
2395 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2396 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2397
2398 status = psa_hash_setup( &operation, alg );
2399 if( status != PSA_SUCCESS )
2400 goto exit;
2401 status = psa_hash_update( &operation, input, input_length );
2402 if( status != PSA_SUCCESS )
2403 goto exit;
2404 status = psa_hash_verify( &operation, hash, hash_length );
2405 if( status != PSA_SUCCESS )
2406 goto exit;
2407
2408exit:
2409 if( status == PSA_SUCCESS )
2410 status = psa_hash_abort( &operation );
2411 else
2412 psa_hash_abort( &operation );
2413 return( status );
2414}
2415
Gilles Peskineeb35d782019-01-22 17:56:16 +01002416psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2417 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002418{
2419 if( target_operation->alg != 0 )
2420 return( PSA_ERROR_BAD_STATE );
2421
2422 switch( source_operation->alg )
2423 {
2424 case 0:
2425 return( PSA_ERROR_BAD_STATE );
2426#if defined(MBEDTLS_MD2_C)
2427 case PSA_ALG_MD2:
2428 mbedtls_md2_clone( &target_operation->ctx.md2,
2429 &source_operation->ctx.md2 );
2430 break;
2431#endif
2432#if defined(MBEDTLS_MD4_C)
2433 case PSA_ALG_MD4:
2434 mbedtls_md4_clone( &target_operation->ctx.md4,
2435 &source_operation->ctx.md4 );
2436 break;
2437#endif
2438#if defined(MBEDTLS_MD5_C)
2439 case PSA_ALG_MD5:
2440 mbedtls_md5_clone( &target_operation->ctx.md5,
2441 &source_operation->ctx.md5 );
2442 break;
2443#endif
2444#if defined(MBEDTLS_RIPEMD160_C)
2445 case PSA_ALG_RIPEMD160:
2446 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2447 &source_operation->ctx.ripemd160 );
2448 break;
2449#endif
2450#if defined(MBEDTLS_SHA1_C)
2451 case PSA_ALG_SHA_1:
2452 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2453 &source_operation->ctx.sha1 );
2454 break;
2455#endif
2456#if defined(MBEDTLS_SHA256_C)
2457 case PSA_ALG_SHA_224:
2458 case PSA_ALG_SHA_256:
2459 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2460 &source_operation->ctx.sha256 );
2461 break;
2462#endif
2463#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002464#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002465 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002466#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002467 case PSA_ALG_SHA_512:
2468 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2469 &source_operation->ctx.sha512 );
2470 break;
2471#endif
2472 default:
2473 return( PSA_ERROR_NOT_SUPPORTED );
2474 }
2475
2476 target_operation->alg = source_operation->alg;
2477 return( PSA_SUCCESS );
2478}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002479
2480
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002481/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002482/* MAC */
2483/****************************************************************/
2484
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002485static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002486 psa_algorithm_t alg,
2487 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002488 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002489 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002490{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002491 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002492 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002493
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002494 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002495 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002496
Gilles Peskine8c9def32018-02-08 10:02:12 +01002497 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2498 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002499 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002500 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002501 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002502 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002503 mode = MBEDTLS_MODE_STREAM;
2504 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002505 case PSA_ALG_CTR:
2506 mode = MBEDTLS_MODE_CTR;
2507 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002508 case PSA_ALG_CFB:
2509 mode = MBEDTLS_MODE_CFB;
2510 break;
2511 case PSA_ALG_OFB:
2512 mode = MBEDTLS_MODE_OFB;
2513 break;
2514 case PSA_ALG_CBC_NO_PADDING:
2515 mode = MBEDTLS_MODE_CBC;
2516 break;
2517 case PSA_ALG_CBC_PKCS7:
2518 mode = MBEDTLS_MODE_CBC;
2519 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002520 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002521 mode = MBEDTLS_MODE_CCM;
2522 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002523 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002524 mode = MBEDTLS_MODE_GCM;
2525 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002526 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2527 mode = MBEDTLS_MODE_CHACHAPOLY;
2528 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002529 default:
2530 return( NULL );
2531 }
2532 }
2533 else if( alg == PSA_ALG_CMAC )
2534 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002535 else
2536 return( NULL );
2537
2538 switch( key_type )
2539 {
2540 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002541 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002542 break;
2543 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002544 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2545 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002546 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002547 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002548 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002549 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002550 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2551 * but two-key Triple-DES is functionally three-key Triple-DES
2552 * with K1=K3, so that's how we present it to mbedtls. */
2553 if( key_bits == 128 )
2554 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002555 break;
2556 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002557 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002558 break;
2559 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002560 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002561 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002562 case PSA_KEY_TYPE_CHACHA20:
2563 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2564 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002565 default:
2566 return( NULL );
2567 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002568 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002569 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002570
Jaeden Amero23bbb752018-06-26 14:16:54 +01002571 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2572 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002573}
2574
Gilles Peskinea05219c2018-11-16 16:02:56 +01002575#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002576static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002577{
Gilles Peskine2d277862018-06-18 15:41:12 +02002578 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002579 {
2580 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002581 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002582 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002583 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002584 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002585 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002586 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002587 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002588 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002589 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002590 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002591 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002592 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002593 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002594 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002595 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002596 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002597 return( 128 );
2598 default:
2599 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002600 }
2601}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002602#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002603
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002604/* Initialize the MAC operation structure. Once this function has been
2605 * called, psa_mac_abort can run and will do the right thing. */
2606static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2607 psa_algorithm_t alg )
2608{
2609 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2610
2611 operation->alg = alg;
2612 operation->key_set = 0;
2613 operation->iv_set = 0;
2614 operation->iv_required = 0;
2615 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002616 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002617
2618#if defined(MBEDTLS_CMAC_C)
2619 if( alg == PSA_ALG_CMAC )
2620 {
2621 operation->iv_required = 0;
2622 mbedtls_cipher_init( &operation->ctx.cmac );
2623 status = PSA_SUCCESS;
2624 }
2625 else
2626#endif /* MBEDTLS_CMAC_C */
2627#if defined(MBEDTLS_MD_C)
2628 if( PSA_ALG_IS_HMAC( operation->alg ) )
2629 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002630 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2631 operation->ctx.hmac.hash_ctx.alg = 0;
2632 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002633 }
2634 else
2635#endif /* MBEDTLS_MD_C */
2636 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002637 if( ! PSA_ALG_IS_MAC( alg ) )
2638 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002639 }
2640
2641 if( status != PSA_SUCCESS )
2642 memset( operation, 0, sizeof( *operation ) );
2643 return( status );
2644}
2645
Gilles Peskine01126fa2018-07-12 17:04:55 +02002646#if defined(MBEDTLS_MD_C)
2647static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2648{
Gilles Peskine3f108122018-12-07 18:14:53 +01002649 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002650 return( psa_hash_abort( &hmac->hash_ctx ) );
2651}
2652#endif /* MBEDTLS_MD_C */
2653
Gilles Peskine8c9def32018-02-08 10:02:12 +01002654psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2655{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002656 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002657 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002658 /* The object has (apparently) been initialized but it is not
2659 * in use. It's ok to call abort on such an object, and there's
2660 * nothing to do. */
2661 return( PSA_SUCCESS );
2662 }
2663 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002664#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002665 if( operation->alg == PSA_ALG_CMAC )
2666 {
2667 mbedtls_cipher_free( &operation->ctx.cmac );
2668 }
2669 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002670#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002671#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002672 if( PSA_ALG_IS_HMAC( operation->alg ) )
2673 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002674 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002675 }
2676 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002677#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002678 {
2679 /* Sanity check (shouldn't happen: operation->alg should
2680 * always have been initialized to a valid value). */
2681 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002682 }
Moran Peker41deec42018-04-04 15:43:05 +03002683
Gilles Peskine8c9def32018-02-08 10:02:12 +01002684 operation->alg = 0;
2685 operation->key_set = 0;
2686 operation->iv_set = 0;
2687 operation->iv_required = 0;
2688 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002689 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002690
Gilles Peskine8c9def32018-02-08 10:02:12 +01002691 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002692
2693bad_state:
2694 /* If abort is called on an uninitialized object, we can't trust
2695 * anything. Wipe the object in case it contains confidential data.
2696 * This may result in a memory leak if a pointer gets overwritten,
2697 * but it's too late to do anything about this. */
2698 memset( operation, 0, sizeof( *operation ) );
2699 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002700}
2701
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002702#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002703static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002704 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002705 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002706 const mbedtls_cipher_info_t *cipher_info )
2707{
Janos Follath24eed8d2019-11-22 13:21:35 +00002708 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002709
2710 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002711
2712 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2713 if( ret != 0 )
2714 return( ret );
2715
2716 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2717 slot->data.raw.data,
2718 key_bits );
2719 return( ret );
2720}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002721#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002722
Gilles Peskine248051a2018-06-20 16:09:38 +02002723#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002724static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2725 const uint8_t *key,
2726 size_t key_length,
2727 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002728{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002729 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002730 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002731 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002732 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002733 psa_status_t status;
2734
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002735 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2736 * overflow below. This should never trigger if the hash algorithm
2737 * is implemented correctly. */
2738 /* The size checks against the ipad and opad buffers cannot be written
2739 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2740 * because that triggers -Wlogical-op on GCC 7.3. */
2741 if( block_size > sizeof( ipad ) )
2742 return( PSA_ERROR_NOT_SUPPORTED );
2743 if( block_size > sizeof( hmac->opad ) )
2744 return( PSA_ERROR_NOT_SUPPORTED );
2745 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002746 return( PSA_ERROR_NOT_SUPPORTED );
2747
Gilles Peskined223b522018-06-11 18:12:58 +02002748 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002749 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002750 status = psa_hash_compute( hash_alg, key, key_length,
2751 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002752 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002753 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002754 }
Gilles Peskine96889972018-07-12 17:07:03 +02002755 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2756 * but it is permitted. It is common when HMAC is used in HKDF, for
2757 * example. Don't call `memcpy` in the 0-length because `key` could be
2758 * an invalid pointer which would make the behavior undefined. */
2759 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002760 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002761
Gilles Peskined223b522018-06-11 18:12:58 +02002762 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2763 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002764 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002765 ipad[i] ^= 0x36;
2766 memset( ipad + key_length, 0x36, block_size - key_length );
2767
2768 /* Copy the key material from ipad to opad, flipping the requisite bits,
2769 * and filling the rest of opad with the requisite constant. */
2770 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002771 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2772 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002773
Gilles Peskine01126fa2018-07-12 17:04:55 +02002774 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002775 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002776 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002777
Gilles Peskine01126fa2018-07-12 17:04:55 +02002778 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002779
2780cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002781 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002782
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002783 return( status );
2784}
Gilles Peskine248051a2018-06-20 16:09:38 +02002785#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002786
Gilles Peskine89167cb2018-07-08 20:12:23 +02002787static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002788 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002789 psa_algorithm_t alg,
2790 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002791{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002792 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002793 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002794 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002795 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002796 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002797 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002798 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002799
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002800 /* A context must be freshly initialized before it can be set up. */
2801 if( operation->alg != 0 )
2802 {
2803 return( PSA_ERROR_BAD_STATE );
2804 }
2805
Gilles Peskined911eb72018-08-14 15:18:45 +02002806 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002807 if( status != PSA_SUCCESS )
2808 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002809 if( is_sign )
2810 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002811
Gilles Peskine28f8f302019-07-24 13:30:31 +02002812 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002813 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002814 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002815 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002816
Gilles Peskine8c9def32018-02-08 10:02:12 +01002817#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002818 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002819 {
2820 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002821 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002822 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002823 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002824 if( cipher_info == NULL )
2825 {
2826 status = PSA_ERROR_NOT_SUPPORTED;
2827 goto exit;
2828 }
2829 operation->mac_size = cipher_info->block_size;
2830 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2831 status = mbedtls_to_psa_error( ret );
2832 }
2833 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002834#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002835#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002836 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002837 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002838 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002839 if( hash_alg == 0 )
2840 {
2841 status = PSA_ERROR_NOT_SUPPORTED;
2842 goto exit;
2843 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002844
2845 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2846 /* Sanity check. This shouldn't fail on a valid configuration. */
2847 if( operation->mac_size == 0 ||
2848 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2849 {
2850 status = PSA_ERROR_NOT_SUPPORTED;
2851 goto exit;
2852 }
2853
Gilles Peskine8e338702019-07-30 20:06:31 +02002854 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002855 {
2856 status = PSA_ERROR_INVALID_ARGUMENT;
2857 goto exit;
2858 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002859
Gilles Peskine01126fa2018-07-12 17:04:55 +02002860 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2861 slot->data.raw.data,
2862 slot->data.raw.bytes,
2863 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002864 }
2865 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002866#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002867 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002868 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002869 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002870 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002871
Gilles Peskined911eb72018-08-14 15:18:45 +02002872 if( truncated == 0 )
2873 {
2874 /* The "normal" case: untruncated algorithm. Nothing to do. */
2875 }
2876 else if( truncated < 4 )
2877 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002878 /* A very short MAC is too short for security since it can be
2879 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2880 * so we make this our minimum, even though 32 bits is still
2881 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002882 status = PSA_ERROR_NOT_SUPPORTED;
2883 }
2884 else if( truncated > operation->mac_size )
2885 {
2886 /* It's impossible to "truncate" to a larger length. */
2887 status = PSA_ERROR_INVALID_ARGUMENT;
2888 }
2889 else
2890 operation->mac_size = truncated;
2891
Gilles Peskinefbfac682018-07-08 20:51:54 +02002892exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002893 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002894 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002895 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002896 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002897 else
2898 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002899 operation->key_set = 1;
2900 }
2901 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002902}
2903
Gilles Peskine89167cb2018-07-08 20:12:23 +02002904psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002905 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002906 psa_algorithm_t alg )
2907{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002908 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002909}
2910
2911psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002912 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002913 psa_algorithm_t alg )
2914{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002915 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002916}
2917
Gilles Peskine8c9def32018-02-08 10:02:12 +01002918psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2919 const uint8_t *input,
2920 size_t input_length )
2921{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002922 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002923 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002924 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002925 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002926 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002927 operation->has_input = 1;
2928
Gilles Peskine8c9def32018-02-08 10:02:12 +01002929#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002930 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002931 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002932 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2933 input, input_length );
2934 status = mbedtls_to_psa_error( ret );
2935 }
2936 else
2937#endif /* MBEDTLS_CMAC_C */
2938#if defined(MBEDTLS_MD_C)
2939 if( PSA_ALG_IS_HMAC( operation->alg ) )
2940 {
2941 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2942 input_length );
2943 }
2944 else
2945#endif /* MBEDTLS_MD_C */
2946 {
2947 /* This shouldn't happen if `operation` was initialized by
2948 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002949 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002950 }
2951
Gilles Peskinefbfac682018-07-08 20:51:54 +02002952 if( status != PSA_SUCCESS )
2953 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002954 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002955}
2956
Gilles Peskine01126fa2018-07-12 17:04:55 +02002957#if defined(MBEDTLS_MD_C)
2958static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2959 uint8_t *mac,
2960 size_t mac_size )
2961{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002962 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002963 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2964 size_t hash_size = 0;
2965 size_t block_size = psa_get_hash_block_size( hash_alg );
2966 psa_status_t status;
2967
Gilles Peskine01126fa2018-07-12 17:04:55 +02002968 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2969 if( status != PSA_SUCCESS )
2970 return( status );
2971 /* From here on, tmp needs to be wiped. */
2972
2973 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2974 if( status != PSA_SUCCESS )
2975 goto exit;
2976
2977 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2978 if( status != PSA_SUCCESS )
2979 goto exit;
2980
2981 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2982 if( status != PSA_SUCCESS )
2983 goto exit;
2984
Gilles Peskined911eb72018-08-14 15:18:45 +02002985 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2986 if( status != PSA_SUCCESS )
2987 goto exit;
2988
2989 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002990
2991exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002992 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002993 return( status );
2994}
2995#endif /* MBEDTLS_MD_C */
2996
mohammad16036df908f2018-04-02 08:34:15 -07002997static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002998 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002999 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003000{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003001 if( ! operation->key_set )
3002 return( PSA_ERROR_BAD_STATE );
3003 if( operation->iv_required && ! operation->iv_set )
3004 return( PSA_ERROR_BAD_STATE );
3005
Gilles Peskine8c9def32018-02-08 10:02:12 +01003006 if( mac_size < operation->mac_size )
3007 return( PSA_ERROR_BUFFER_TOO_SMALL );
3008
Gilles Peskine8c9def32018-02-08 10:02:12 +01003009#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003010 if( operation->alg == PSA_ALG_CMAC )
3011 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003012 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3013 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3014 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003015 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003016 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003017 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003018 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003019 else
3020#endif /* MBEDTLS_CMAC_C */
3021#if defined(MBEDTLS_MD_C)
3022 if( PSA_ALG_IS_HMAC( operation->alg ) )
3023 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003024 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003025 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003026 }
3027 else
3028#endif /* MBEDTLS_MD_C */
3029 {
3030 /* This shouldn't happen if `operation` was initialized by
3031 * a setup function. */
3032 return( PSA_ERROR_BAD_STATE );
3033 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003034}
3035
Gilles Peskineacd4be32018-07-08 19:56:25 +02003036psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3037 uint8_t *mac,
3038 size_t mac_size,
3039 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003040{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003041 psa_status_t status;
3042
Jaeden Amero252ef282019-02-15 14:05:35 +00003043 if( operation->alg == 0 )
3044 {
3045 return( PSA_ERROR_BAD_STATE );
3046 }
3047
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003048 /* Fill the output buffer with something that isn't a valid mac
3049 * (barring an attack on the mac and deliberately-crafted input),
3050 * in case the caller doesn't check the return status properly. */
3051 *mac_length = mac_size;
3052 /* If mac_size is 0 then mac may be NULL and then the
3053 * call to memset would have undefined behavior. */
3054 if( mac_size != 0 )
3055 memset( mac, '!', mac_size );
3056
Gilles Peskine89167cb2018-07-08 20:12:23 +02003057 if( ! operation->is_sign )
3058 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003059 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003060 }
mohammad16036df908f2018-04-02 08:34:15 -07003061
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003062 status = psa_mac_finish_internal( operation, mac, mac_size );
3063
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003064 if( status == PSA_SUCCESS )
3065 {
3066 status = psa_mac_abort( operation );
3067 if( status == PSA_SUCCESS )
3068 *mac_length = operation->mac_size;
3069 else
3070 memset( mac, '!', mac_size );
3071 }
3072 else
3073 psa_mac_abort( operation );
3074 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003075}
3076
Gilles Peskineacd4be32018-07-08 19:56:25 +02003077psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3078 const uint8_t *mac,
3079 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003080{
Gilles Peskine828ed142018-06-18 23:25:51 +02003081 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003082 psa_status_t status;
3083
Jaeden Amero252ef282019-02-15 14:05:35 +00003084 if( operation->alg == 0 )
3085 {
3086 return( PSA_ERROR_BAD_STATE );
3087 }
3088
Gilles Peskine89167cb2018-07-08 20:12:23 +02003089 if( operation->is_sign )
3090 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003091 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003092 }
3093 if( operation->mac_size != mac_length )
3094 {
3095 status = PSA_ERROR_INVALID_SIGNATURE;
3096 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003097 }
mohammad16036df908f2018-04-02 08:34:15 -07003098
3099 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003100 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003101 if( status != PSA_SUCCESS )
3102 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003103
3104 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3105 status = PSA_ERROR_INVALID_SIGNATURE;
3106
3107cleanup:
3108 if( status == PSA_SUCCESS )
3109 status = psa_mac_abort( operation );
3110 else
3111 psa_mac_abort( operation );
3112
Gilles Peskine3f108122018-12-07 18:14:53 +01003113 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003114
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003115 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003116}
3117
3118
Gilles Peskine20035e32018-02-03 22:44:14 +01003119
Gilles Peskine20035e32018-02-03 22:44:14 +01003120/****************************************************************/
3121/* Asymmetric cryptography */
3122/****************************************************************/
3123
Gilles Peskine2b450e32018-06-27 15:42:46 +02003124#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003125/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003126 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003127static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3128 size_t hash_length,
3129 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003130{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003131 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003132 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003133 *md_alg = mbedtls_md_get_type( md_info );
3134
3135 /* The Mbed TLS RSA module uses an unsigned int for hash length
3136 * parameters. Validate that it fits so that we don't risk an
3137 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003138#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003139 if( hash_length > UINT_MAX )
3140 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003141#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003142
3143#if defined(MBEDTLS_PKCS1_V15)
3144 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3145 * must be correct. */
3146 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3147 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003148 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003149 if( md_info == NULL )
3150 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003151 if( mbedtls_md_get_size( md_info ) != hash_length )
3152 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003153 }
3154#endif /* MBEDTLS_PKCS1_V15 */
3155
3156#if defined(MBEDTLS_PKCS1_V21)
3157 /* PSS requires a hash internally. */
3158 if( PSA_ALG_IS_RSA_PSS( alg ) )
3159 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003160 if( md_info == NULL )
3161 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003162 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003163#endif /* MBEDTLS_PKCS1_V21 */
3164
Gilles Peskine61b91d42018-06-08 16:09:36 +02003165 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003166}
3167
Gilles Peskine2b450e32018-06-27 15:42:46 +02003168static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3169 psa_algorithm_t alg,
3170 const uint8_t *hash,
3171 size_t hash_length,
3172 uint8_t *signature,
3173 size_t signature_size,
3174 size_t *signature_length )
3175{
3176 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003177 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003178 mbedtls_md_type_t md_alg;
3179
3180 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3181 if( status != PSA_SUCCESS )
3182 return( status );
3183
Gilles Peskine630a18a2018-06-29 17:49:35 +02003184 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003185 return( PSA_ERROR_BUFFER_TOO_SMALL );
3186
3187#if defined(MBEDTLS_PKCS1_V15)
3188 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3189 {
3190 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3191 MBEDTLS_MD_NONE );
3192 ret = mbedtls_rsa_pkcs1_sign( rsa,
3193 mbedtls_ctr_drbg_random,
3194 &global_data.ctr_drbg,
3195 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003196 md_alg,
3197 (unsigned int) hash_length,
3198 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003199 signature );
3200 }
3201 else
3202#endif /* MBEDTLS_PKCS1_V15 */
3203#if defined(MBEDTLS_PKCS1_V21)
3204 if( PSA_ALG_IS_RSA_PSS( alg ) )
3205 {
3206 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3207 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3208 mbedtls_ctr_drbg_random,
3209 &global_data.ctr_drbg,
3210 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003211 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003212 (unsigned int) hash_length,
3213 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003214 signature );
3215 }
3216 else
3217#endif /* MBEDTLS_PKCS1_V21 */
3218 {
3219 return( PSA_ERROR_INVALID_ARGUMENT );
3220 }
3221
3222 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003223 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003224 return( mbedtls_to_psa_error( ret ) );
3225}
3226
3227static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3228 psa_algorithm_t alg,
3229 const uint8_t *hash,
3230 size_t hash_length,
3231 const uint8_t *signature,
3232 size_t signature_length )
3233{
3234 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003235 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003236 mbedtls_md_type_t md_alg;
3237
3238 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3239 if( status != PSA_SUCCESS )
3240 return( status );
3241
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003242 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3243 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003244
3245#if defined(MBEDTLS_PKCS1_V15)
3246 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3247 {
3248 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3249 MBEDTLS_MD_NONE );
3250 ret = mbedtls_rsa_pkcs1_verify( rsa,
3251 mbedtls_ctr_drbg_random,
3252 &global_data.ctr_drbg,
3253 MBEDTLS_RSA_PUBLIC,
3254 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003255 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003256 hash,
3257 signature );
3258 }
3259 else
3260#endif /* MBEDTLS_PKCS1_V15 */
3261#if defined(MBEDTLS_PKCS1_V21)
3262 if( PSA_ALG_IS_RSA_PSS( alg ) )
3263 {
3264 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3265 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3266 mbedtls_ctr_drbg_random,
3267 &global_data.ctr_drbg,
3268 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003269 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003270 (unsigned int) hash_length,
3271 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003272 signature );
3273 }
3274 else
3275#endif /* MBEDTLS_PKCS1_V21 */
3276 {
3277 return( PSA_ERROR_INVALID_ARGUMENT );
3278 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003279
3280 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3281 * the rest of the signature is invalid". This has little use in
3282 * practice and PSA doesn't report this distinction. */
3283 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3284 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003285 return( mbedtls_to_psa_error( ret ) );
3286}
3287#endif /* MBEDTLS_RSA_C */
3288
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003289#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003290/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3291 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3292 * (even though these functions don't modify it). */
3293static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3294 psa_algorithm_t alg,
3295 const uint8_t *hash,
3296 size_t hash_length,
3297 uint8_t *signature,
3298 size_t signature_size,
3299 size_t *signature_length )
3300{
Janos Follath24eed8d2019-11-22 13:21:35 +00003301 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003302 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003303 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003304 mbedtls_mpi_init( &r );
3305 mbedtls_mpi_init( &s );
3306
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003307 if( signature_size < 2 * curve_bytes )
3308 {
3309 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3310 goto cleanup;
3311 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003312
Gilles Peskinea05219c2018-11-16 16:02:56 +01003313#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003314 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3315 {
3316 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3317 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3318 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003319 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3320 &ecp->d, hash,
3321 hash_length, md_alg,
3322 mbedtls_ctr_drbg_random,
3323 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003324 }
3325 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003326#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003327 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003328 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003329 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3330 hash, hash_length,
3331 mbedtls_ctr_drbg_random,
3332 &global_data.ctr_drbg ) );
3333 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003334
3335 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3336 signature,
3337 curve_bytes ) );
3338 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3339 signature + curve_bytes,
3340 curve_bytes ) );
3341
3342cleanup:
3343 mbedtls_mpi_free( &r );
3344 mbedtls_mpi_free( &s );
3345 if( ret == 0 )
3346 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003347 return( mbedtls_to_psa_error( ret ) );
3348}
3349
3350static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3351 const uint8_t *hash,
3352 size_t hash_length,
3353 const uint8_t *signature,
3354 size_t signature_length )
3355{
Janos Follath24eed8d2019-11-22 13:21:35 +00003356 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003357 mbedtls_mpi r, s;
3358 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3359 mbedtls_mpi_init( &r );
3360 mbedtls_mpi_init( &s );
3361
3362 if( signature_length != 2 * curve_bytes )
3363 return( PSA_ERROR_INVALID_SIGNATURE );
3364
3365 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3366 signature,
3367 curve_bytes ) );
3368 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3369 signature + curve_bytes,
3370 curve_bytes ) );
3371
3372 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3373 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003374
3375cleanup:
3376 mbedtls_mpi_free( &r );
3377 mbedtls_mpi_free( &s );
3378 return( mbedtls_to_psa_error( ret ) );
3379}
3380#endif /* MBEDTLS_ECDSA_C */
3381
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003382psa_status_t psa_sign_hash( psa_key_handle_t handle,
3383 psa_algorithm_t alg,
3384 const uint8_t *hash,
3385 size_t hash_length,
3386 uint8_t *signature,
3387 size_t signature_size,
3388 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003389{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003390 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003391 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003392#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3393 const psa_drv_se_t *drv;
3394 psa_drv_se_context_t *drv_context;
3395#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003396
3397 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003398 /* Immediately reject a zero-length signature buffer. This guarantees
3399 * that signature must be a valid pointer. (On the other hand, the hash
3400 * buffer can in principle be empty since it doesn't actually have
3401 * to be a hash.) */
3402 if( signature_size == 0 )
3403 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003404
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003405 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003406 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003407 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003408 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003409 {
3410 status = PSA_ERROR_INVALID_ARGUMENT;
3411 goto exit;
3412 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003413
Gilles Peskineedc64242019-08-07 21:05:07 +02003414#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3415 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3416 {
3417 if( drv->asymmetric == NULL ||
3418 drv->asymmetric->p_sign == NULL )
3419 {
3420 status = PSA_ERROR_NOT_SUPPORTED;
3421 goto exit;
3422 }
3423 status = drv->asymmetric->p_sign( drv_context,
3424 slot->data.se.slot_number,
3425 alg,
3426 hash, hash_length,
3427 signature, signature_size,
3428 signature_length );
3429 }
3430 else
3431#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003432#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003433 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003434 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003435 status = psa_rsa_sign( slot->data.rsa,
3436 alg,
3437 hash, hash_length,
3438 signature, signature_size,
3439 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003440 }
3441 else
3442#endif /* defined(MBEDTLS_RSA_C) */
3443#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003444 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003445 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003446#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003447 if(
3448#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3449 PSA_ALG_IS_ECDSA( alg )
3450#else
3451 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3452#endif
3453 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003454 status = psa_ecdsa_sign( slot->data.ecp,
3455 alg,
3456 hash, hash_length,
3457 signature, signature_size,
3458 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003459 else
3460#endif /* defined(MBEDTLS_ECDSA_C) */
3461 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003462 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003463 }
itayzafrir5c753392018-05-08 11:18:38 +03003464 }
3465 else
3466#endif /* defined(MBEDTLS_ECP_C) */
3467 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003468 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003469 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003470
3471exit:
3472 /* Fill the unused part of the output buffer (the whole buffer on error,
3473 * the trailing part on success) with something that isn't a valid mac
3474 * (barring an attack on the mac and deliberately-crafted input),
3475 * in case the caller doesn't check the return status properly. */
3476 if( status == PSA_SUCCESS )
3477 memset( signature + *signature_length, '!',
3478 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003479 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003480 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003481 /* If signature_size is 0 then we have nothing to do. We must not call
3482 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003483 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003484}
3485
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003486psa_status_t psa_verify_hash( psa_key_handle_t handle,
3487 psa_algorithm_t alg,
3488 const uint8_t *hash,
3489 size_t hash_length,
3490 const uint8_t *signature,
3491 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003492{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003493 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003494 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003495#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3496 const psa_drv_se_t *drv;
3497 psa_drv_se_context_t *drv_context;
3498#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003499
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003500 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003501 if( status != PSA_SUCCESS )
3502 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003503
Gilles Peskineedc64242019-08-07 21:05:07 +02003504#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3505 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3506 {
3507 if( drv->asymmetric == NULL ||
3508 drv->asymmetric->p_verify == NULL )
3509 return( PSA_ERROR_NOT_SUPPORTED );
3510 return( drv->asymmetric->p_verify( drv_context,
3511 slot->data.se.slot_number,
3512 alg,
3513 hash, hash_length,
3514 signature, signature_length ) );
3515 }
3516 else
3517#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003518#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003519 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003520 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003521 return( psa_rsa_verify( slot->data.rsa,
3522 alg,
3523 hash, hash_length,
3524 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003525 }
3526 else
3527#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003528#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003529 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003530 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003531#if defined(MBEDTLS_ECDSA_C)
3532 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003533 return( psa_ecdsa_verify( slot->data.ecp,
3534 hash, hash_length,
3535 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003536 else
3537#endif /* defined(MBEDTLS_ECDSA_C) */
3538 {
3539 return( PSA_ERROR_INVALID_ARGUMENT );
3540 }
itayzafrir5c753392018-05-08 11:18:38 +03003541 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003542 else
3543#endif /* defined(MBEDTLS_ECP_C) */
3544 {
3545 return( PSA_ERROR_NOT_SUPPORTED );
3546 }
3547}
3548
Gilles Peskine072ac562018-06-30 00:21:29 +02003549#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3550static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3551 mbedtls_rsa_context *rsa )
3552{
3553 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3554 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3555 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3556 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3557}
3558#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3559
Gilles Peskinec5487a82018-12-03 18:08:14 +01003560psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003561 psa_algorithm_t alg,
3562 const uint8_t *input,
3563 size_t input_length,
3564 const uint8_t *salt,
3565 size_t salt_length,
3566 uint8_t *output,
3567 size_t output_size,
3568 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003569{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003570 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003571 psa_status_t status;
3572
Darryl Green5cc689a2018-07-24 15:34:10 +01003573 (void) input;
3574 (void) input_length;
3575 (void) salt;
3576 (void) output;
3577 (void) output_size;
3578
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003579 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003580
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003581 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3582 return( PSA_ERROR_INVALID_ARGUMENT );
3583
Gilles Peskine28f8f302019-07-24 13:30:31 +02003584 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003585 if( status != PSA_SUCCESS )
3586 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003587 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3588 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003589 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003590
3591#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003592 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003593 {
3594 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003595 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003596 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003597 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003598#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003599 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003600 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003601 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3602 mbedtls_ctr_drbg_random,
3603 &global_data.ctr_drbg,
3604 MBEDTLS_RSA_PUBLIC,
3605 input_length,
3606 input,
3607 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003608 }
3609 else
3610#endif /* MBEDTLS_PKCS1_V15 */
3611#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003612 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003613 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003614 psa_rsa_oaep_set_padding_mode( alg, rsa );
3615 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3616 mbedtls_ctr_drbg_random,
3617 &global_data.ctr_drbg,
3618 MBEDTLS_RSA_PUBLIC,
3619 salt, salt_length,
3620 input_length,
3621 input,
3622 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003623 }
3624 else
3625#endif /* MBEDTLS_PKCS1_V21 */
3626 {
3627 return( PSA_ERROR_INVALID_ARGUMENT );
3628 }
3629 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003630 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003631 return( mbedtls_to_psa_error( ret ) );
3632 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003633 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003634#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003635 {
3636 return( PSA_ERROR_NOT_SUPPORTED );
3637 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003638}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003639
Gilles Peskinec5487a82018-12-03 18:08:14 +01003640psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003641 psa_algorithm_t alg,
3642 const uint8_t *input,
3643 size_t input_length,
3644 const uint8_t *salt,
3645 size_t salt_length,
3646 uint8_t *output,
3647 size_t output_size,
3648 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003649{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003650 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003651 psa_status_t status;
3652
Darryl Green5cc689a2018-07-24 15:34:10 +01003653 (void) input;
3654 (void) input_length;
3655 (void) salt;
3656 (void) output;
3657 (void) output_size;
3658
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003659 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003660
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003661 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3662 return( PSA_ERROR_INVALID_ARGUMENT );
3663
Gilles Peskine28f8f302019-07-24 13:30:31 +02003664 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003665 if( status != PSA_SUCCESS )
3666 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003667 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003668 return( PSA_ERROR_INVALID_ARGUMENT );
3669
3670#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003671 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003672 {
3673 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003674 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003675
Gilles Peskine630a18a2018-06-29 17:49:35 +02003676 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003677 return( PSA_ERROR_INVALID_ARGUMENT );
3678
3679#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003680 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003681 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003682 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3683 mbedtls_ctr_drbg_random,
3684 &global_data.ctr_drbg,
3685 MBEDTLS_RSA_PRIVATE,
3686 output_length,
3687 input,
3688 output,
3689 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003690 }
3691 else
3692#endif /* MBEDTLS_PKCS1_V15 */
3693#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003694 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003695 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003696 psa_rsa_oaep_set_padding_mode( alg, rsa );
3697 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3698 mbedtls_ctr_drbg_random,
3699 &global_data.ctr_drbg,
3700 MBEDTLS_RSA_PRIVATE,
3701 salt, salt_length,
3702 output_length,
3703 input,
3704 output,
3705 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003706 }
3707 else
3708#endif /* MBEDTLS_PKCS1_V21 */
3709 {
3710 return( PSA_ERROR_INVALID_ARGUMENT );
3711 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003712
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003713 return( mbedtls_to_psa_error( ret ) );
3714 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003715 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003716#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003717 {
3718 return( PSA_ERROR_NOT_SUPPORTED );
3719 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003720}
Gilles Peskine20035e32018-02-03 22:44:14 +01003721
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003722
3723
mohammad1603503973b2018-03-12 15:59:30 +02003724/****************************************************************/
3725/* Symmetric cryptography */
3726/****************************************************************/
3727
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003728/* Initialize the cipher operation structure. Once this function has been
3729 * called, psa_cipher_abort can run and will do the right thing. */
3730static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3731 psa_algorithm_t alg )
3732{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003733 if( ! PSA_ALG_IS_CIPHER( alg ) )
3734 {
3735 memset( operation, 0, sizeof( *operation ) );
3736 return( PSA_ERROR_INVALID_ARGUMENT );
3737 }
3738
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003739 operation->alg = alg;
3740 operation->key_set = 0;
3741 operation->iv_set = 0;
3742 operation->iv_required = 1;
3743 operation->iv_size = 0;
3744 operation->block_size = 0;
3745 mbedtls_cipher_init( &operation->ctx.cipher );
3746 return( PSA_SUCCESS );
3747}
3748
Gilles Peskinee553c652018-06-04 16:22:46 +02003749static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003750 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003751 psa_algorithm_t alg,
3752 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003753{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003754 int ret = 0;
3755 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003756 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003757 size_t key_bits;
3758 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003759 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3760 PSA_KEY_USAGE_ENCRYPT :
3761 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003762
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003763 /* A context must be freshly initialized before it can be set up. */
3764 if( operation->alg != 0 )
3765 {
3766 return( PSA_ERROR_BAD_STATE );
3767 }
3768
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003769 status = psa_cipher_init( operation, alg );
3770 if( status != PSA_SUCCESS )
3771 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003772
Gilles Peskine28f8f302019-07-24 13:30:31 +02003773 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003774 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003775 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003776 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003777
Gilles Peskine8e338702019-07-30 20:06:31 +02003778 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003779 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003780 {
3781 status = PSA_ERROR_NOT_SUPPORTED;
3782 goto exit;
3783 }
mohammad1603503973b2018-03-12 15:59:30 +02003784
mohammad1603503973b2018-03-12 15:59:30 +02003785 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003786 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003787 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003788
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003789#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003790 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003791 {
3792 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003793 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003794 memcpy( keys, slot->data.raw.data, 16 );
3795 memcpy( keys + 16, slot->data.raw.data, 8 );
3796 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3797 keys,
3798 192, cipher_operation );
3799 }
3800 else
3801#endif
3802 {
3803 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3804 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003805 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003806 }
Moran Peker41deec42018-04-04 15:43:05 +03003807 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003808 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003809
mohammad16038481e742018-03-18 13:57:31 +02003810#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003811 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003812 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003813 case PSA_ALG_CBC_NO_PADDING:
3814 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3815 MBEDTLS_PADDING_NONE );
3816 break;
3817 case PSA_ALG_CBC_PKCS7:
3818 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3819 MBEDTLS_PADDING_PKCS7 );
3820 break;
3821 default:
3822 /* The algorithm doesn't involve padding. */
3823 ret = 0;
3824 break;
3825 }
3826 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003827 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003828#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3829
mohammad1603503973b2018-03-12 15:59:30 +02003830 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003831 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003832 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003833 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003834 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003835 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003836 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003837#if defined(MBEDTLS_CHACHA20_C)
3838 else
3839 if( alg == PSA_ALG_CHACHA20 )
3840 operation->iv_size = 12;
3841#endif
mohammad1603503973b2018-03-12 15:59:30 +02003842
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003843exit:
3844 if( status == 0 )
3845 status = mbedtls_to_psa_error( ret );
3846 if( status != 0 )
3847 psa_cipher_abort( operation );
3848 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003849}
3850
Gilles Peskinefe119512018-07-08 21:39:34 +02003851psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003852 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003853 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003854{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003855 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003856}
3857
Gilles Peskinefe119512018-07-08 21:39:34 +02003858psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003859 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003860 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003861{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003862 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003863}
3864
Gilles Peskinefe119512018-07-08 21:39:34 +02003865psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003866 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003867 size_t iv_size,
3868 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003869{
itayzafrir534bd7c2018-08-02 13:56:32 +03003870 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003871 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003872 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003873 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003874 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003875 }
Moran Peker41deec42018-04-04 15:43:05 +03003876 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003877 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003878 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003879 goto exit;
3880 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003881 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3882 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003883 if( ret != 0 )
3884 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003885 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003886 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003887 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003888
mohammad16038481e742018-03-18 13:57:31 +02003889 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003890 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003891
Moran Peker395db872018-05-31 14:07:14 +03003892exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003893 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003894 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003895 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003896}
3897
Gilles Peskinefe119512018-07-08 21:39:34 +02003898psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003899 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003900 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003901{
itayzafrir534bd7c2018-08-02 13:56:32 +03003902 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003903 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003904 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003905 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003906 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003907 }
Moran Pekera28258c2018-05-29 16:25:04 +03003908 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003909 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003910 status = PSA_ERROR_INVALID_ARGUMENT;
3911 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003912 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003913 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3914 status = mbedtls_to_psa_error( ret );
3915exit:
3916 if( status == PSA_SUCCESS )
3917 operation->iv_set = 1;
3918 else
mohammad1603503973b2018-03-12 15:59:30 +02003919 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003920 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003921}
3922
Gilles Peskinee553c652018-06-04 16:22:46 +02003923psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3924 const uint8_t *input,
3925 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003926 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003927 size_t output_size,
3928 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003929{
itayzafrir534bd7c2018-08-02 13:56:32 +03003930 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003931 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003932 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003933
3934 if( operation->alg == 0 )
3935 {
3936 return( PSA_ERROR_BAD_STATE );
3937 }
3938
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003939 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003940 {
3941 /* Take the unprocessed partial block left over from previous
3942 * update calls, if any, plus the input to this call. Remove
3943 * the last partial block, if any. You get the data that will be
3944 * output in this call. */
3945 expected_output_size =
3946 ( operation->ctx.cipher.unprocessed_len + input_length )
3947 / operation->block_size * operation->block_size;
3948 }
3949 else
3950 {
3951 expected_output_size = input_length;
3952 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003953
Gilles Peskine89d789c2018-06-04 17:17:16 +02003954 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003955 {
3956 status = PSA_ERROR_BUFFER_TOO_SMALL;
3957 goto exit;
3958 }
mohammad160382759612018-03-12 18:16:40 +02003959
mohammad1603503973b2018-03-12 15:59:30 +02003960 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003961 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003962 status = mbedtls_to_psa_error( ret );
3963exit:
3964 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003965 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003966 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003967}
3968
Gilles Peskinee553c652018-06-04 16:22:46 +02003969psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3970 uint8_t *output,
3971 size_t output_size,
3972 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003973{
David Saadab4ecc272019-02-14 13:48:10 +02003974 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003975 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003976 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003977
mohammad1603503973b2018-03-12 15:59:30 +02003978 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003979 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003980 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003981 }
3982 if( operation->iv_required && ! operation->iv_set )
3983 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003984 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003985 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003986
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003987 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003988 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3989 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003990 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003991 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003992 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003993 }
3994
Janos Follath315b51c2018-07-09 16:04:51 +01003995 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3996 temp_output_buffer,
3997 output_length );
3998 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003999 {
Janos Follath315b51c2018-07-09 16:04:51 +01004000 status = mbedtls_to_psa_error( cipher_ret );
4001 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004002 }
Janos Follath315b51c2018-07-09 16:04:51 +01004003
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004004 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004005 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004006 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004007 memcpy( output, temp_output_buffer, *output_length );
4008 else
4009 {
Janos Follath315b51c2018-07-09 16:04:51 +01004010 status = PSA_ERROR_BUFFER_TOO_SMALL;
4011 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004012 }
mohammad1603503973b2018-03-12 15:59:30 +02004013
Gilles Peskine3f108122018-12-07 18:14:53 +01004014 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004015 status = psa_cipher_abort( operation );
4016
4017 return( status );
4018
4019error:
4020
4021 *output_length = 0;
4022
Gilles Peskine3f108122018-12-07 18:14:53 +01004023 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004024 (void) psa_cipher_abort( operation );
4025
4026 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004027}
4028
Gilles Peskinee553c652018-06-04 16:22:46 +02004029psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4030{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004031 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004032 {
4033 /* The object has (apparently) been initialized but it is not
4034 * in use. It's ok to call abort on such an object, and there's
4035 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004036 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004037 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004038
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004039 /* Sanity check (shouldn't happen: operation->alg should
4040 * always have been initialized to a valid value). */
4041 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4042 return( PSA_ERROR_BAD_STATE );
4043
mohammad1603503973b2018-03-12 15:59:30 +02004044 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004045
Moran Peker41deec42018-04-04 15:43:05 +03004046 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004047 operation->key_set = 0;
4048 operation->iv_set = 0;
4049 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004050 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004051 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004052
Moran Peker395db872018-05-31 14:07:14 +03004053 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004054}
4055
Gilles Peskinea0655c32018-04-30 17:06:50 +02004056
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004057
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004058
mohammad16035955c982018-04-26 00:53:03 +03004059/****************************************************************/
4060/* AEAD */
4061/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004062
Gilles Peskineedf9a652018-08-17 18:11:56 +02004063typedef struct
4064{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004065 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004066 const mbedtls_cipher_info_t *cipher_info;
4067 union
4068 {
4069#if defined(MBEDTLS_CCM_C)
4070 mbedtls_ccm_context ccm;
4071#endif /* MBEDTLS_CCM_C */
4072#if defined(MBEDTLS_GCM_C)
4073 mbedtls_gcm_context gcm;
4074#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004075#if defined(MBEDTLS_CHACHAPOLY_C)
4076 mbedtls_chachapoly_context chachapoly;
4077#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004078 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004079 psa_algorithm_t core_alg;
4080 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004081 uint8_t tag_length;
4082} aead_operation_t;
4083
Gilles Peskine30a9e412019-01-14 18:36:12 +01004084static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004085{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004086 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004087 {
4088#if defined(MBEDTLS_CCM_C)
4089 case PSA_ALG_CCM:
4090 mbedtls_ccm_free( &operation->ctx.ccm );
4091 break;
4092#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004093#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004094 case PSA_ALG_GCM:
4095 mbedtls_gcm_free( &operation->ctx.gcm );
4096 break;
4097#endif /* MBEDTLS_GCM_C */
4098 }
4099}
4100
4101static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004102 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004103 psa_key_usage_t usage,
4104 psa_algorithm_t alg )
4105{
4106 psa_status_t status;
4107 size_t key_bits;
4108 mbedtls_cipher_id_t cipher_id;
4109
Gilles Peskine28f8f302019-07-24 13:30:31 +02004110 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004111 if( status != PSA_SUCCESS )
4112 return( status );
4113
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004114 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004115
4116 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004117 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004118 &cipher_id );
4119 if( operation->cipher_info == NULL )
4120 return( PSA_ERROR_NOT_SUPPORTED );
4121
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004122 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004123 {
4124#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004125 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4126 operation->core_alg = PSA_ALG_CCM;
4127 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004128 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4129 * The call to mbedtls_ccm_encrypt_and_tag or
4130 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004131 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004132 return( PSA_ERROR_INVALID_ARGUMENT );
4133 mbedtls_ccm_init( &operation->ctx.ccm );
4134 status = mbedtls_to_psa_error(
4135 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4136 operation->slot->data.raw.data,
4137 (unsigned int) key_bits ) );
4138 if( status != 0 )
4139 goto cleanup;
4140 break;
4141#endif /* MBEDTLS_CCM_C */
4142
4143#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004144 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4145 operation->core_alg = PSA_ALG_GCM;
4146 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004147 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4148 * The call to mbedtls_gcm_crypt_and_tag or
4149 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004150 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004151 return( PSA_ERROR_INVALID_ARGUMENT );
4152 mbedtls_gcm_init( &operation->ctx.gcm );
4153 status = mbedtls_to_psa_error(
4154 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4155 operation->slot->data.raw.data,
4156 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004157 if( status != 0 )
4158 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004159 break;
4160#endif /* MBEDTLS_GCM_C */
4161
Gilles Peskine26869f22019-05-06 15:25:00 +02004162#if defined(MBEDTLS_CHACHAPOLY_C)
4163 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4164 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4165 operation->full_tag_length = 16;
4166 /* We only support the default tag length. */
4167 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4168 return( PSA_ERROR_NOT_SUPPORTED );
4169 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4170 status = mbedtls_to_psa_error(
4171 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4172 operation->slot->data.raw.data ) );
4173 if( status != 0 )
4174 goto cleanup;
4175 break;
4176#endif /* MBEDTLS_CHACHAPOLY_C */
4177
Gilles Peskineedf9a652018-08-17 18:11:56 +02004178 default:
4179 return( PSA_ERROR_NOT_SUPPORTED );
4180 }
4181
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004182 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4183 {
4184 status = PSA_ERROR_INVALID_ARGUMENT;
4185 goto cleanup;
4186 }
4187 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004188
Gilles Peskineedf9a652018-08-17 18:11:56 +02004189 return( PSA_SUCCESS );
4190
4191cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004192 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004193 return( status );
4194}
4195
Gilles Peskinec5487a82018-12-03 18:08:14 +01004196psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004197 psa_algorithm_t alg,
4198 const uint8_t *nonce,
4199 size_t nonce_length,
4200 const uint8_t *additional_data,
4201 size_t additional_data_length,
4202 const uint8_t *plaintext,
4203 size_t plaintext_length,
4204 uint8_t *ciphertext,
4205 size_t ciphertext_size,
4206 size_t *ciphertext_length )
4207{
mohammad16035955c982018-04-26 00:53:03 +03004208 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004209 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004210 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004211
mohammad1603f08a5502018-06-03 15:05:47 +03004212 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004213
Gilles Peskinec5487a82018-12-03 18:08:14 +01004214 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004215 if( status != PSA_SUCCESS )
4216 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004217
Gilles Peskineedf9a652018-08-17 18:11:56 +02004218 /* For all currently supported modes, the tag is at the end of the
4219 * ciphertext. */
4220 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4221 {
4222 status = PSA_ERROR_BUFFER_TOO_SMALL;
4223 goto exit;
4224 }
4225 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004226
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004227#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004228 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004229 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004230 status = mbedtls_to_psa_error(
4231 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4232 MBEDTLS_GCM_ENCRYPT,
4233 plaintext_length,
4234 nonce, nonce_length,
4235 additional_data, additional_data_length,
4236 plaintext, ciphertext,
4237 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004238 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004239 else
4240#endif /* MBEDTLS_GCM_C */
4241#if defined(MBEDTLS_CCM_C)
4242 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004243 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004244 status = mbedtls_to_psa_error(
4245 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4246 plaintext_length,
4247 nonce, nonce_length,
4248 additional_data,
4249 additional_data_length,
4250 plaintext, ciphertext,
4251 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004252 }
mohammad16035c8845f2018-05-09 05:40:09 -07004253 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004254#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004255#if defined(MBEDTLS_CHACHAPOLY_C)
4256 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4257 {
4258 if( nonce_length != 12 || operation.tag_length != 16 )
4259 {
4260 status = PSA_ERROR_NOT_SUPPORTED;
4261 goto exit;
4262 }
4263 status = mbedtls_to_psa_error(
4264 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4265 plaintext_length,
4266 nonce,
4267 additional_data,
4268 additional_data_length,
4269 plaintext,
4270 ciphertext,
4271 tag ) );
4272 }
4273 else
4274#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004275 {
mohammad1603554faad2018-06-03 15:07:38 +03004276 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004277 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004278
Gilles Peskineedf9a652018-08-17 18:11:56 +02004279 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4280 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004281
Gilles Peskineedf9a652018-08-17 18:11:56 +02004282exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004283 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004284 if( status == PSA_SUCCESS )
4285 *ciphertext_length = plaintext_length + operation.tag_length;
4286 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004287}
4288
Gilles Peskineee652a32018-06-01 19:23:52 +02004289/* Locate the tag in a ciphertext buffer containing the encrypted data
4290 * followed by the tag. Return the length of the part preceding the tag in
4291 * *plaintext_length. This is the size of the plaintext in modes where
4292 * the encrypted data has the same size as the plaintext, such as
4293 * CCM and GCM. */
4294static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4295 const uint8_t *ciphertext,
4296 size_t ciphertext_length,
4297 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004298 const uint8_t **p_tag )
4299{
4300 size_t payload_length;
4301 if( tag_length > ciphertext_length )
4302 return( PSA_ERROR_INVALID_ARGUMENT );
4303 payload_length = ciphertext_length - tag_length;
4304 if( payload_length > plaintext_size )
4305 return( PSA_ERROR_BUFFER_TOO_SMALL );
4306 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004307 return( PSA_SUCCESS );
4308}
4309
Gilles Peskinec5487a82018-12-03 18:08:14 +01004310psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004311 psa_algorithm_t alg,
4312 const uint8_t *nonce,
4313 size_t nonce_length,
4314 const uint8_t *additional_data,
4315 size_t additional_data_length,
4316 const uint8_t *ciphertext,
4317 size_t ciphertext_length,
4318 uint8_t *plaintext,
4319 size_t plaintext_size,
4320 size_t *plaintext_length )
4321{
mohammad16035955c982018-04-26 00:53:03 +03004322 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004323 aead_operation_t operation;
4324 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004325
Gilles Peskineee652a32018-06-01 19:23:52 +02004326 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004327
Gilles Peskinec5487a82018-12-03 18:08:14 +01004328 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004329 if( status != PSA_SUCCESS )
4330 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004331
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004332 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4333 ciphertext, ciphertext_length,
4334 plaintext_size, &tag );
4335 if( status != PSA_SUCCESS )
4336 goto exit;
4337
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004338#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004339 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004340 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004341 status = mbedtls_to_psa_error(
4342 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4343 ciphertext_length - operation.tag_length,
4344 nonce, nonce_length,
4345 additional_data,
4346 additional_data_length,
4347 tag, operation.tag_length,
4348 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004349 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004350 else
4351#endif /* MBEDTLS_GCM_C */
4352#if defined(MBEDTLS_CCM_C)
4353 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004354 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004355 status = mbedtls_to_psa_error(
4356 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4357 ciphertext_length - operation.tag_length,
4358 nonce, nonce_length,
4359 additional_data,
4360 additional_data_length,
4361 ciphertext, plaintext,
4362 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004363 }
mohammad160339574652018-06-01 04:39:53 -07004364 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004365#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004366#if defined(MBEDTLS_CHACHAPOLY_C)
4367 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4368 {
4369 if( nonce_length != 12 || operation.tag_length != 16 )
4370 {
4371 status = PSA_ERROR_NOT_SUPPORTED;
4372 goto exit;
4373 }
4374 status = mbedtls_to_psa_error(
4375 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4376 ciphertext_length - operation.tag_length,
4377 nonce,
4378 additional_data,
4379 additional_data_length,
4380 tag,
4381 ciphertext,
4382 plaintext ) );
4383 }
4384 else
4385#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004386 {
mohammad1603554faad2018-06-03 15:07:38 +03004387 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004388 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004389
Gilles Peskineedf9a652018-08-17 18:11:56 +02004390 if( status != PSA_SUCCESS && plaintext_size != 0 )
4391 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004392
Gilles Peskineedf9a652018-08-17 18:11:56 +02004393exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004394 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004395 if( status == PSA_SUCCESS )
4396 *plaintext_length = ciphertext_length - operation.tag_length;
4397 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004398}
4399
Gilles Peskinea0655c32018-04-30 17:06:50 +02004400
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004401
Gilles Peskine20035e32018-02-03 22:44:14 +01004402/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004403/* Generators */
4404/****************************************************************/
4405
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004406#define HKDF_STATE_INIT 0 /* no input yet */
4407#define HKDF_STATE_STARTED 1 /* got salt */
4408#define HKDF_STATE_KEYED 2 /* got key */
4409#define HKDF_STATE_OUTPUT 3 /* output started */
4410
Gilles Peskinecbe66502019-05-16 16:59:18 +02004411static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004412 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004413{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004414 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4415 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004416 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004417 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004418}
4419
4420
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004421psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004422{
4423 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004424 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004425 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004426 {
4427 /* The object has (apparently) been initialized but it is not
4428 * in use. It's ok to call abort on such an object, and there's
4429 * nothing to do. */
4430 }
4431 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004432#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004433 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004434 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004435 mbedtls_free( operation->ctx.hkdf.info );
4436 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004437 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004438 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004439 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004440 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004441 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004442 if( operation->ctx.tls12_prf.seed != NULL )
4443 {
4444 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4445 operation->ctx.tls12_prf.seed_length );
4446 mbedtls_free( operation->ctx.tls12_prf.seed );
4447 }
4448
4449 if( operation->ctx.tls12_prf.label != NULL )
4450 {
4451 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4452 operation->ctx.tls12_prf.label_length );
4453 mbedtls_free( operation->ctx.tls12_prf.label );
4454 }
4455
4456 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4457
4458 /* We leave the fields Ai and output_block to be erased safely by the
4459 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004460 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004461 else
4462#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004463 {
4464 status = PSA_ERROR_BAD_STATE;
4465 }
Janos Follath083036a2019-06-11 10:22:26 +01004466 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004467 return( status );
4468}
4469
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004470psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004471 size_t *capacity)
4472{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004473 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004474 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004475 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004476 return PSA_ERROR_BAD_STATE;
4477 }
4478
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004479 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004480 return( PSA_SUCCESS );
4481}
4482
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004483psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004484 size_t capacity )
4485{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004486 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004487 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004488 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004489 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004490 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004491 return( PSA_SUCCESS );
4492}
4493
Gilles Peskinebef7f142018-07-12 17:22:21 +02004494#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004495/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004496 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004497static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004498 psa_algorithm_t hash_alg,
4499 uint8_t *output,
4500 size_t output_length )
4501{
4502 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4503 psa_status_t status;
4504
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004505 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4506 return( PSA_ERROR_BAD_STATE );
4507 hkdf->state = HKDF_STATE_OUTPUT;
4508
Gilles Peskinebef7f142018-07-12 17:22:21 +02004509 while( output_length != 0 )
4510 {
4511 /* Copy what remains of the current block */
4512 uint8_t n = hash_length - hkdf->offset_in_block;
4513 if( n > output_length )
4514 n = (uint8_t) output_length;
4515 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4516 output += n;
4517 output_length -= n;
4518 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004519 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004520 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004521 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004522 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004523 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004524 * object was corrupted or if this function is called directly
4525 * inside the library. */
4526 if( hkdf->block_number == 0xff )
4527 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004528
4529 /* We need a new block */
4530 ++hkdf->block_number;
4531 hkdf->offset_in_block = 0;
4532 status = psa_hmac_setup_internal( &hkdf->hmac,
4533 hkdf->prk, hash_length,
4534 hash_alg );
4535 if( status != PSA_SUCCESS )
4536 return( status );
4537 if( hkdf->block_number != 1 )
4538 {
4539 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4540 hkdf->output_block,
4541 hash_length );
4542 if( status != PSA_SUCCESS )
4543 return( status );
4544 }
4545 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4546 hkdf->info,
4547 hkdf->info_length );
4548 if( status != PSA_SUCCESS )
4549 return( status );
4550 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4551 &hkdf->block_number, 1 );
4552 if( status != PSA_SUCCESS )
4553 return( status );
4554 status = psa_hmac_finish_internal( &hkdf->hmac,
4555 hkdf->output_block,
4556 sizeof( hkdf->output_block ) );
4557 if( status != PSA_SUCCESS )
4558 return( status );
4559 }
4560
4561 return( PSA_SUCCESS );
4562}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004563
Janos Follath7742fee2019-06-17 12:58:10 +01004564static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4565 psa_tls12_prf_key_derivation_t *tls12_prf,
4566 psa_algorithm_t alg )
4567{
4568 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4569 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004570 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4571 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004572
Janos Follath7742fee2019-06-17 12:58:10 +01004573 /* We can't be wanting more output after block 0xff, otherwise
4574 * the capacity check in psa_key_derivation_output_bytes() would have
4575 * prevented this call. It could happen only if the operation
4576 * object was corrupted or if this function is called directly
4577 * inside the library. */
4578 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004579 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004580
4581 /* We need a new block */
4582 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004583 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004584
4585 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4586 *
4587 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4588 *
4589 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4590 * HMAC_hash(secret, A(2) + seed) +
4591 * HMAC_hash(secret, A(3) + seed) + ...
4592 *
4593 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004594 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004595 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004596 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004597 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004598 * is currently extracted as `output_block` and where i is
4599 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004600 */
4601
Janos Follathea29bfb2019-06-19 12:21:20 +01004602 /* Save the hash context before using it, to preserve the hash state with
4603 * only the inner padding in it. We need this, because inner padding depends
4604 * on the key (secret in the RFC's terminology). */
4605 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4606 if( status != PSA_SUCCESS )
4607 goto cleanup;
4608
4609 /* Calculate A(i) where i = tls12_prf->block_number. */
4610 if( tls12_prf->block_number == 1 )
4611 {
4612 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4613 * the variable seed and in this instance means it in the context of the
4614 * P_hash function, where seed = label + seed.) */
4615 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4616 tls12_prf->label, tls12_prf->label_length );
4617 if( status != PSA_SUCCESS )
4618 goto cleanup;
4619 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4620 tls12_prf->seed, tls12_prf->seed_length );
4621 if( status != PSA_SUCCESS )
4622 goto cleanup;
4623 }
4624 else
4625 {
4626 /* A(i) = HMAC_hash(secret, A(i-1)) */
4627 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4628 tls12_prf->Ai, hash_length );
4629 if( status != PSA_SUCCESS )
4630 goto cleanup;
4631 }
4632
4633 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4634 tls12_prf->Ai, hash_length );
4635 if( status != PSA_SUCCESS )
4636 goto cleanup;
4637 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4638 if( status != PSA_SUCCESS )
4639 goto cleanup;
4640
4641 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4642 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4643 tls12_prf->Ai, hash_length );
4644 if( status != PSA_SUCCESS )
4645 goto cleanup;
4646 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4647 tls12_prf->label, tls12_prf->label_length );
4648 if( status != PSA_SUCCESS )
4649 goto cleanup;
4650 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4651 tls12_prf->seed, tls12_prf->seed_length );
4652 if( status != PSA_SUCCESS )
4653 goto cleanup;
4654 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4655 tls12_prf->output_block, hash_length );
4656 if( status != PSA_SUCCESS )
4657 goto cleanup;
4658 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4659 if( status != PSA_SUCCESS )
4660 goto cleanup;
4661
Janos Follath7742fee2019-06-17 12:58:10 +01004662
4663cleanup:
4664
Janos Follathea29bfb2019-06-19 12:21:20 +01004665 cleanup_status = psa_hash_abort( &backup );
4666 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4667 status = cleanup_status;
4668
4669 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004670}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004671
Janos Follath844eb0e2019-06-19 12:10:49 +01004672static psa_status_t psa_key_derivation_tls12_prf_read(
4673 psa_tls12_prf_key_derivation_t *tls12_prf,
4674 psa_algorithm_t alg,
4675 uint8_t *output,
4676 size_t output_length )
4677{
4678 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4679 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4680 psa_status_t status;
4681 uint8_t offset, length;
4682
4683 while( output_length != 0 )
4684 {
4685 /* Check if we have fully processed the current block. */
4686 if( tls12_prf->left_in_block == 0 )
4687 {
4688 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4689 alg );
4690 if( status != PSA_SUCCESS )
4691 return( status );
4692
4693 continue;
4694 }
4695
4696 if( tls12_prf->left_in_block > output_length )
4697 length = (uint8_t) output_length;
4698 else
4699 length = tls12_prf->left_in_block;
4700
4701 offset = hash_length - tls12_prf->left_in_block;
4702 memcpy( output, tls12_prf->output_block + offset, length );
4703 output += length;
4704 output_length -= length;
4705 tls12_prf->left_in_block -= length;
4706 }
4707
4708 return( PSA_SUCCESS );
4709}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004710#endif /* MBEDTLS_MD_C */
4711
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004712psa_status_t psa_key_derivation_output_bytes(
4713 psa_key_derivation_operation_t *operation,
4714 uint8_t *output,
4715 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004716{
4717 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004718 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004719
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004720 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004721 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004722 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004723 return PSA_ERROR_BAD_STATE;
4724 }
4725
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004726 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004727 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004728 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004729 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004730 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004731 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004732 goto exit;
4733 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004734 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004735 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004736 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004737 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004738 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4739 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004740 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004741 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004742 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004743 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004744 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004745
Gilles Peskinebef7f142018-07-12 17:22:21 +02004746#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004747 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004748 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004749 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004750 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004751 output, output_length );
4752 }
Janos Follathadbec812019-06-14 11:05:39 +01004753 else
Janos Follathadbec812019-06-14 11:05:39 +01004754 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004755 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004756 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004757 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004758 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004759 output_length );
4760 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004761 else
4762#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004763 {
4764 return( PSA_ERROR_BAD_STATE );
4765 }
4766
4767exit:
4768 if( status != PSA_SUCCESS )
4769 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004770 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004771 * This allows us to differentiate between exhausted operations and
4772 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4773 * operations. */
4774 psa_algorithm_t alg = operation->alg;
4775 psa_key_derivation_abort( operation );
4776 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004777 memset( output, '!', output_length );
4778 }
4779 return( status );
4780}
4781
Gilles Peskine08542d82018-07-19 17:05:42 +02004782#if defined(MBEDTLS_DES_C)
4783static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4784{
4785 if( data_size >= 8 )
4786 mbedtls_des_key_set_parity( data );
4787 if( data_size >= 16 )
4788 mbedtls_des_key_set_parity( data + 8 );
4789 if( data_size >= 24 )
4790 mbedtls_des_key_set_parity( data + 16 );
4791}
4792#endif /* MBEDTLS_DES_C */
4793
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004794static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004795 psa_key_slot_t *slot,
4796 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004797 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004798{
4799 uint8_t *data = NULL;
4800 size_t bytes = PSA_BITS_TO_BYTES( bits );
4801 psa_status_t status;
4802
Gilles Peskine8e338702019-07-30 20:06:31 +02004803 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004804 return( PSA_ERROR_INVALID_ARGUMENT );
4805 if( bits % 8 != 0 )
4806 return( PSA_ERROR_INVALID_ARGUMENT );
4807 data = mbedtls_calloc( 1, bytes );
4808 if( data == NULL )
4809 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4810
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004811 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004812 if( status != PSA_SUCCESS )
4813 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004814#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004815 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004816 psa_des_set_key_parity( data, bytes );
4817#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004818 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004819
4820exit:
4821 mbedtls_free( data );
4822 return( status );
4823}
4824
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004825psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004826 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004827 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004828{
4829 psa_status_t status;
4830 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004831 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004832
4833 /* Reject any attempt to create a zero-length key so that we don't
4834 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4835 if( psa_get_key_bits( attributes ) == 0 )
4836 return( PSA_ERROR_INVALID_ARGUMENT );
4837
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004838 if( ! operation->can_output_key )
4839 return( PSA_ERROR_NOT_PERMITTED );
4840
Gilles Peskinedf179142019-07-15 22:02:14 +02004841 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4842 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004843#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4844 if( driver != NULL )
4845 {
4846 /* Deriving a key in a secure element is not implemented yet. */
4847 status = PSA_ERROR_NOT_SUPPORTED;
4848 }
4849#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004850 if( status == PSA_SUCCESS )
4851 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004852 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004853 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004854 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004855 }
4856 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004857 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004858 if( status != PSA_SUCCESS )
4859 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004860 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004861 *handle = 0;
4862 }
4863 return( status );
4864}
4865
Gilles Peskineeab56e42018-07-12 17:12:33 +02004866
4867
4868/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004869/* Key derivation */
4870/****************************************************************/
4871
Gilles Peskine969c5d62019-01-16 15:53:06 +01004872static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004873 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004874 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004875{
Janos Follath5fe19732019-06-20 15:09:30 +01004876 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4877 * initialisers for this union leave some bytes unspecified.) */
4878 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4879
Gilles Peskine969c5d62019-01-16 15:53:06 +01004880 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004881#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004882 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4883 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4884 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004885 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004886 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004887 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4888 if( hash_size == 0 )
4889 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004890 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4891 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004892 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004893 {
4894 return( PSA_ERROR_NOT_SUPPORTED );
4895 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004896 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004897 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004898 }
4899#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004900 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004901 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004902}
4903
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004904psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004905 psa_algorithm_t alg )
4906{
4907 psa_status_t status;
4908
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004909 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004910 return( PSA_ERROR_BAD_STATE );
4911
Gilles Peskine6843c292019-01-18 16:44:49 +01004912 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4913 return( PSA_ERROR_INVALID_ARGUMENT );
4914 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004915 {
4916 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004917 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004918 }
4919 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4920 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004921 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004922 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004923 else
4924 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004925
4926 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004927 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004928 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004929}
4930
4931#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004932static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004933 psa_algorithm_t hash_alg,
4934 psa_key_derivation_step_t step,
4935 const uint8_t *data,
4936 size_t data_length )
4937{
4938 psa_status_t status;
4939 switch( step )
4940 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004941 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004942 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004943 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004944 status = psa_hmac_setup_internal( &hkdf->hmac,
4945 data, data_length,
4946 hash_alg );
4947 if( status != PSA_SUCCESS )
4948 return( status );
4949 hkdf->state = HKDF_STATE_STARTED;
4950 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004951 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004952 /* If no salt was provided, use an empty salt. */
4953 if( hkdf->state == HKDF_STATE_INIT )
4954 {
4955 status = psa_hmac_setup_internal( &hkdf->hmac,
4956 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004957 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004958 if( status != PSA_SUCCESS )
4959 return( status );
4960 hkdf->state = HKDF_STATE_STARTED;
4961 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004962 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004963 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004964 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4965 data, data_length );
4966 if( status != PSA_SUCCESS )
4967 return( status );
4968 status = psa_hmac_finish_internal( &hkdf->hmac,
4969 hkdf->prk,
4970 sizeof( hkdf->prk ) );
4971 if( status != PSA_SUCCESS )
4972 return( status );
4973 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4974 hkdf->block_number = 0;
4975 hkdf->state = HKDF_STATE_KEYED;
4976 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004977 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004978 if( hkdf->state == HKDF_STATE_OUTPUT )
4979 return( PSA_ERROR_BAD_STATE );
4980 if( hkdf->info_set )
4981 return( PSA_ERROR_BAD_STATE );
4982 hkdf->info_length = data_length;
4983 if( data_length != 0 )
4984 {
4985 hkdf->info = mbedtls_calloc( 1, data_length );
4986 if( hkdf->info == NULL )
4987 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4988 memcpy( hkdf->info, data, data_length );
4989 }
4990 hkdf->info_set = 1;
4991 return( PSA_SUCCESS );
4992 default:
4993 return( PSA_ERROR_INVALID_ARGUMENT );
4994 }
4995}
Janos Follathb03233e2019-06-11 15:30:30 +01004996
Janos Follathf08e2652019-06-13 09:05:41 +01004997static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4998 const uint8_t *data,
4999 size_t data_length )
5000{
5001 if( prf->state != TLS12_PRF_STATE_INIT )
5002 return( PSA_ERROR_BAD_STATE );
5003
Janos Follathd6dce9f2019-07-04 09:11:38 +01005004 if( data_length != 0 )
5005 {
5006 prf->seed = mbedtls_calloc( 1, data_length );
5007 if( prf->seed == NULL )
5008 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005009
Janos Follathd6dce9f2019-07-04 09:11:38 +01005010 memcpy( prf->seed, data, data_length );
5011 prf->seed_length = data_length;
5012 }
Janos Follathf08e2652019-06-13 09:05:41 +01005013
5014 prf->state = TLS12_PRF_STATE_SEED_SET;
5015
5016 return( PSA_SUCCESS );
5017}
5018
Janos Follath81550542019-06-13 14:26:34 +01005019static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5020 psa_algorithm_t hash_alg,
5021 const uint8_t *data,
5022 size_t data_length )
5023{
5024 psa_status_t status;
5025 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5026 return( PSA_ERROR_BAD_STATE );
5027
5028 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5029 if( status != PSA_SUCCESS )
5030 return( status );
5031
5032 prf->state = TLS12_PRF_STATE_KEY_SET;
5033
5034 return( PSA_SUCCESS );
5035}
5036
Janos Follath6660f0e2019-06-17 08:44:03 +01005037static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5038 psa_tls12_prf_key_derivation_t *prf,
5039 psa_algorithm_t hash_alg,
5040 const uint8_t *data,
5041 size_t data_length )
5042{
5043 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005044 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5045 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005046
5047 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5048 return( PSA_ERROR_INVALID_ARGUMENT );
5049
5050 /* Quoting RFC 4279, Section 2:
5051 *
5052 * The premaster secret is formed as follows: if the PSK is N octets
5053 * long, concatenate a uint16 with the value N, N zero octets, a second
5054 * uint16 with the value N, and the PSK itself.
5055 */
5056
Janos Follath40e13932019-06-26 13:22:29 +01005057 *cur++ = ( data_length >> 8 ) & 0xff;
5058 *cur++ = ( data_length >> 0 ) & 0xff;
5059 memset( cur, 0, data_length );
5060 cur += data_length;
5061 *cur++ = pms[0];
5062 *cur++ = pms[1];
5063 memcpy( cur, data, data_length );
5064 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005065
Janos Follath40e13932019-06-26 13:22:29 +01005066 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005067
5068 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5069 return( status );
5070}
5071
Janos Follath63028dd2019-06-13 09:15:47 +01005072static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5073 const uint8_t *data,
5074 size_t data_length )
5075{
5076 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5077 return( PSA_ERROR_BAD_STATE );
5078
Janos Follathd6dce9f2019-07-04 09:11:38 +01005079 if( data_length != 0 )
5080 {
5081 prf->label = mbedtls_calloc( 1, data_length );
5082 if( prf->label == NULL )
5083 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005084
Janos Follathd6dce9f2019-07-04 09:11:38 +01005085 memcpy( prf->label, data, data_length );
5086 prf->label_length = data_length;
5087 }
Janos Follath63028dd2019-06-13 09:15:47 +01005088
5089 prf->state = TLS12_PRF_STATE_LABEL_SET;
5090
5091 return( PSA_SUCCESS );
5092}
5093
Janos Follathb03233e2019-06-11 15:30:30 +01005094static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5095 psa_algorithm_t hash_alg,
5096 psa_key_derivation_step_t step,
5097 const uint8_t *data,
5098 size_t data_length )
5099{
Janos Follathb03233e2019-06-11 15:30:30 +01005100 switch( step )
5101 {
Janos Follathf08e2652019-06-13 09:05:41 +01005102 case PSA_KEY_DERIVATION_INPUT_SEED:
5103 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005104 case PSA_KEY_DERIVATION_INPUT_SECRET:
5105 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005106 case PSA_KEY_DERIVATION_INPUT_LABEL:
5107 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005108 default:
5109 return( PSA_ERROR_INVALID_ARGUMENT );
5110 }
5111}
Janos Follath6660f0e2019-06-17 08:44:03 +01005112
5113static psa_status_t psa_tls12_prf_psk_to_ms_input(
5114 psa_tls12_prf_key_derivation_t *prf,
5115 psa_algorithm_t hash_alg,
5116 psa_key_derivation_step_t step,
5117 const uint8_t *data,
5118 size_t data_length )
5119{
5120 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005121 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005122 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5123 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005124 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005125
5126 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5127}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005128#endif /* MBEDTLS_MD_C */
5129
Gilles Peskineb8965192019-09-24 16:21:10 +02005130/** Check whether the given key type is acceptable for the given
5131 * input step of a key derivation.
5132 *
5133 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5134 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5135 * Both secret and non-secret inputs can alternatively have the type
5136 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5137 * that the input was passed as a buffer rather than via a key object.
5138 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005139static int psa_key_derivation_check_input_type(
5140 psa_key_derivation_step_t step,
5141 psa_key_type_t key_type )
5142{
5143 switch( step )
5144 {
5145 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005146 if( key_type == PSA_KEY_TYPE_DERIVE )
5147 return( PSA_SUCCESS );
5148 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005149 return( PSA_SUCCESS );
5150 break;
5151 case PSA_KEY_DERIVATION_INPUT_LABEL:
5152 case PSA_KEY_DERIVATION_INPUT_SALT:
5153 case PSA_KEY_DERIVATION_INPUT_INFO:
5154 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005155 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5156 return( PSA_SUCCESS );
5157 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005158 return( PSA_SUCCESS );
5159 break;
5160 }
5161 return( PSA_ERROR_INVALID_ARGUMENT );
5162}
5163
Janos Follathb80a94e2019-06-12 15:54:46 +01005164static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005165 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005166 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005167 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005168 const uint8_t *data,
5169 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005170{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005171 psa_status_t status;
5172 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5173
5174 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005175 if( status != PSA_SUCCESS )
5176 goto exit;
5177
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005178#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005179 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005180 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005181 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005182 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005183 step, data, data_length );
5184 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005185 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005186 {
Janos Follathb03233e2019-06-11 15:30:30 +01005187 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5188 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5189 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005190 }
5191 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5192 {
5193 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5194 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5195 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005196 }
5197 else
5198#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005199 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005200 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005201 return( PSA_ERROR_BAD_STATE );
5202 }
5203
Gilles Peskine224b0d62019-09-23 18:13:17 +02005204exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005205 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005206 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005207 return( status );
5208}
5209
Janos Follath51f4a0f2019-06-14 11:35:55 +01005210psa_status_t psa_key_derivation_input_bytes(
5211 psa_key_derivation_operation_t *operation,
5212 psa_key_derivation_step_t step,
5213 const uint8_t *data,
5214 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005215{
Gilles Peskineb8965192019-09-24 16:21:10 +02005216 return( psa_key_derivation_input_internal( operation, step,
5217 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005218 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005219}
5220
Janos Follath51f4a0f2019-06-14 11:35:55 +01005221psa_status_t psa_key_derivation_input_key(
5222 psa_key_derivation_operation_t *operation,
5223 psa_key_derivation_step_t step,
5224 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005225{
5226 psa_key_slot_t *slot;
5227 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005228
Gilles Peskine28f8f302019-07-24 13:30:31 +02005229 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005230 PSA_KEY_USAGE_DERIVE,
5231 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005232 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005233 {
5234 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005235 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005236 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005237
5238 /* Passing a key object as a SECRET input unlocks the permission
5239 * to output to a key object. */
5240 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5241 operation->can_output_key = 1;
5242
Janos Follathb80a94e2019-06-12 15:54:46 +01005243 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005244 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005245 slot->data.raw.data,
5246 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005247}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005248
5249
5250
5251/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005252/* Key agreement */
5253/****************************************************************/
5254
Gilles Peskinea05219c2018-11-16 16:02:56 +01005255#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005256static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5257 size_t peer_key_length,
5258 const mbedtls_ecp_keypair *our_key,
5259 uint8_t *shared_secret,
5260 size_t shared_secret_size,
5261 size_t *shared_secret_length )
5262{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005263 mbedtls_ecp_keypair *their_key = NULL;
5264 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005265 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005266 size_t bits = 0;
5267 psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005268 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005269
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005270 status = psa_import_ec_public_key( curve,
5271 peer_key, peer_key_length,
5272 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005273 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005274 goto exit;
5275
Jaeden Amero97271b32019-01-10 19:38:51 +00005276 status = mbedtls_to_psa_error(
5277 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5278 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005279 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005280 status = mbedtls_to_psa_error(
5281 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5282 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005283 goto exit;
5284
Jaeden Amero97271b32019-01-10 19:38:51 +00005285 status = mbedtls_to_psa_error(
5286 mbedtls_ecdh_calc_secret( &ecdh,
5287 shared_secret_length,
5288 shared_secret, shared_secret_size,
5289 mbedtls_ctr_drbg_random,
5290 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005291 if( status != PSA_SUCCESS )
5292 goto exit;
5293 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5294 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005295
5296exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005297 if( status != PSA_SUCCESS )
5298 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005299 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005300 mbedtls_ecp_keypair_free( their_key );
5301 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005302 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005303}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005304#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005305
Gilles Peskine01d718c2018-09-18 12:01:02 +02005306#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5307
Gilles Peskine0216fe12019-04-11 21:23:21 +02005308static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5309 psa_key_slot_t *private_key,
5310 const uint8_t *peer_key,
5311 size_t peer_key_length,
5312 uint8_t *shared_secret,
5313 size_t shared_secret_size,
5314 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005315{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005316 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005317 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005318#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005319 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005320 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005321 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005322 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5323 private_key->data.ecp,
5324 shared_secret, shared_secret_size,
5325 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005326#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005327 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005328 (void) private_key;
5329 (void) peer_key;
5330 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005331 (void) shared_secret;
5332 (void) shared_secret_size;
5333 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005334 return( PSA_ERROR_NOT_SUPPORTED );
5335 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005336}
5337
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005338/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005339 * to potentially free embedded data structures and wipe confidential data.
5340 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005341static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005342 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005343 psa_key_slot_t *private_key,
5344 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005345 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005346{
5347 psa_status_t status;
5348 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5349 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005350 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005351
5352 /* Step 1: run the secret agreement algorithm to generate the shared
5353 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005354 status = psa_key_agreement_raw_internal( ka_alg,
5355 private_key,
5356 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005357 shared_secret,
5358 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005359 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005360 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005361 goto exit;
5362
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005363 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005364 * the shared secret. A shared secret is permitted wherever a key
5365 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005366 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005367 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005368 shared_secret,
5369 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005370
Gilles Peskine12313cd2018-06-20 00:20:32 +02005371exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005372 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005373 return( status );
5374}
5375
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005376psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005377 psa_key_derivation_step_t step,
5378 psa_key_handle_t private_key,
5379 const uint8_t *peer_key,
5380 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005381{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005382 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005383 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005384 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005385 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005386 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005387 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005388 if( status != PSA_SUCCESS )
5389 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005390 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005391 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005392 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005393 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005394 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005395 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005396}
5397
Gilles Peskinebe697d82019-05-16 18:00:41 +02005398psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5399 psa_key_handle_t private_key,
5400 const uint8_t *peer_key,
5401 size_t peer_key_length,
5402 uint8_t *output,
5403 size_t output_size,
5404 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005405{
5406 psa_key_slot_t *slot;
5407 psa_status_t status;
5408
5409 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5410 {
5411 status = PSA_ERROR_INVALID_ARGUMENT;
5412 goto exit;
5413 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005414 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005415 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005416 if( status != PSA_SUCCESS )
5417 goto exit;
5418
5419 status = psa_key_agreement_raw_internal( alg, slot,
5420 peer_key, peer_key_length,
5421 output, output_size,
5422 output_length );
5423
5424exit:
5425 if( status != PSA_SUCCESS )
5426 {
5427 /* If an error happens and is not handled properly, the output
5428 * may be used as a key to protect sensitive data. Arrange for such
5429 * a key to be random, which is likely to result in decryption or
5430 * verification errors. This is better than filling the buffer with
5431 * some constant data such as zeros, which would result in the data
5432 * being protected with a reproducible, easily knowable key.
5433 */
5434 psa_generate_random( output, output_size );
5435 *output_length = output_size;
5436 }
5437 return( status );
5438}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005439
5440
5441/****************************************************************/
5442/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005443/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005444
Gilles Peskine4c317f42018-07-12 01:24:09 +02005445psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005446 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005447{
Janos Follath24eed8d2019-11-22 13:21:35 +00005448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005449 GUARD_MODULE_INITIALIZED;
5450
Gilles Peskinef181eca2019-08-07 13:49:00 +02005451 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5452 {
5453 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5454 output,
5455 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5456 if( ret != 0 )
5457 return( mbedtls_to_psa_error( ret ) );
5458 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5459 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5460 }
5461
Gilles Peskinee59236f2018-01-27 23:32:46 +01005462 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5463 return( mbedtls_to_psa_error( ret ) );
5464}
5465
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005466#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5467#include "mbedtls/entropy_poll.h"
5468
Gilles Peskine7228da22019-07-15 11:06:15 +02005469psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005470 size_t seed_size )
5471{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005472 if( global_data.initialized )
5473 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005474
5475 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5476 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5477 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5478 return( PSA_ERROR_INVALID_ARGUMENT );
5479
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005480 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005481}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005482#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005483
Gilles Peskinee56e8782019-04-26 17:34:02 +02005484#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5485static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5486 size_t domain_parameters_size,
5487 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005488{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005489 size_t i;
5490 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005491
Gilles Peskinee56e8782019-04-26 17:34:02 +02005492 if( domain_parameters_size == 0 )
5493 {
5494 *exponent = 65537;
5495 return( PSA_SUCCESS );
5496 }
5497
5498 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5499 * support values that fit in a 32-bit integer, which is larger than
5500 * int on just about every platform anyway. */
5501 if( domain_parameters_size > sizeof( acc ) )
5502 return( PSA_ERROR_NOT_SUPPORTED );
5503 for( i = 0; i < domain_parameters_size; i++ )
5504 acc = ( acc << 8 ) | domain_parameters[i];
5505 if( acc > INT_MAX )
5506 return( PSA_ERROR_NOT_SUPPORTED );
5507 *exponent = acc;
5508 return( PSA_SUCCESS );
5509}
5510#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5511
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005512static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005513 psa_key_slot_t *slot, size_t bits,
5514 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005515{
Gilles Peskine8e338702019-07-30 20:06:31 +02005516 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005517
Gilles Peskinee56e8782019-04-26 17:34:02 +02005518 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005519 return( PSA_ERROR_INVALID_ARGUMENT );
5520
Gilles Peskinee59236f2018-01-27 23:32:46 +01005521 if( key_type_is_raw_bytes( type ) )
5522 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005523 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005524 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5525 if( status != PSA_SUCCESS )
5526 return( status );
5527 status = psa_generate_random( slot->data.raw.data,
5528 slot->data.raw.bytes );
5529 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005530 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005531#if defined(MBEDTLS_DES_C)
5532 if( type == PSA_KEY_TYPE_DES )
5533 psa_des_set_key_parity( slot->data.raw.data,
5534 slot->data.raw.bytes );
5535#endif /* MBEDTLS_DES_C */
5536 }
5537 else
5538
5539#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005540 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005541 {
5542 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005543 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005544 int exponent;
5545 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005546 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5547 return( PSA_ERROR_NOT_SUPPORTED );
5548 /* Accept only byte-aligned keys, for the same reasons as
5549 * in psa_import_rsa_key(). */
5550 if( bits % 8 != 0 )
5551 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005552 status = psa_read_rsa_exponent( domain_parameters,
5553 domain_parameters_size,
5554 &exponent );
5555 if( status != PSA_SUCCESS )
5556 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005557 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5558 if( rsa == NULL )
5559 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5560 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5561 ret = mbedtls_rsa_gen_key( rsa,
5562 mbedtls_ctr_drbg_random,
5563 &global_data.ctr_drbg,
5564 (unsigned int) bits,
5565 exponent );
5566 if( ret != 0 )
5567 {
5568 mbedtls_rsa_free( rsa );
5569 mbedtls_free( rsa );
5570 return( mbedtls_to_psa_error( ret ) );
5571 }
5572 slot->data.rsa = rsa;
5573 }
5574 else
5575#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5576
5577#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005578 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005579 {
5580 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005581 mbedtls_ecp_group_id grp_id =
5582 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005583 const mbedtls_ecp_curve_info *curve_info =
5584 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5585 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005586 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005587 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005588 return( PSA_ERROR_NOT_SUPPORTED );
5589 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5590 return( PSA_ERROR_NOT_SUPPORTED );
5591 if( curve_info->bit_size != bits )
5592 return( PSA_ERROR_INVALID_ARGUMENT );
5593 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5594 if( ecp == NULL )
5595 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5596 mbedtls_ecp_keypair_init( ecp );
5597 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5598 mbedtls_ctr_drbg_random,
5599 &global_data.ctr_drbg );
5600 if( ret != 0 )
5601 {
5602 mbedtls_ecp_keypair_free( ecp );
5603 mbedtls_free( ecp );
5604 return( mbedtls_to_psa_error( ret ) );
5605 }
5606 slot->data.ecp = ecp;
5607 }
5608 else
5609#endif /* MBEDTLS_ECP_C */
5610
5611 return( PSA_ERROR_NOT_SUPPORTED );
5612
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005613 return( PSA_SUCCESS );
5614}
Darryl Green0c6575a2018-11-07 16:05:30 +00005615
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005616psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005617 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005618{
5619 psa_status_t status;
5620 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005621 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005622
Gilles Peskine0f84d622019-09-12 19:03:13 +02005623 /* Reject any attempt to create a zero-length key so that we don't
5624 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5625 if( psa_get_key_bits( attributes ) == 0 )
5626 return( PSA_ERROR_INVALID_ARGUMENT );
5627
Gilles Peskinedf179142019-07-15 22:02:14 +02005628 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5629 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005630 if( status != PSA_SUCCESS )
5631 goto exit;
5632
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005633#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5634 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005635 {
Gilles Peskine11792082019-08-06 18:36:36 +02005636 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5637 size_t pubkey_length = 0; /* We don't support this feature yet */
5638 if( drv->key_management == NULL ||
5639 drv->key_management->p_generate == NULL )
5640 {
5641 status = PSA_ERROR_NOT_SUPPORTED;
5642 goto exit;
5643 }
5644 status = drv->key_management->p_generate(
5645 psa_get_se_driver_context( driver ),
5646 slot->data.se.slot_number, attributes,
5647 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005648 }
Gilles Peskine11792082019-08-06 18:36:36 +02005649 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005650#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005651 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005652 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005653 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005654 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005655 }
Gilles Peskine11792082019-08-06 18:36:36 +02005656
5657exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005658 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005659 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005660 if( status != PSA_SUCCESS )
5661 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005662 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005663 *handle = 0;
5664 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005665 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005666}
5667
5668
Gilles Peskinee59236f2018-01-27 23:32:46 +01005669
5670/****************************************************************/
5671/* Module setup */
5672/****************************************************************/
5673
Gilles Peskine5e769522018-11-20 21:59:56 +01005674psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5675 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5676 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5677{
5678 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5679 return( PSA_ERROR_BAD_STATE );
5680 global_data.entropy_init = entropy_init;
5681 global_data.entropy_free = entropy_free;
5682 return( PSA_SUCCESS );
5683}
5684
Gilles Peskinee59236f2018-01-27 23:32:46 +01005685void mbedtls_psa_crypto_free( void )
5686{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005687 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005688 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5689 {
5690 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005691 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005692 }
5693 /* Wipe all remaining data, including configuration.
5694 * In particular, this sets all state indicator to the value
5695 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005696 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005697#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005698 /* Unregister all secure element drivers, so that we restart from
5699 * a pristine state. */
5700 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005701#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005702}
5703
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005704#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5705/** Recover a transaction that was interrupted by a power failure.
5706 *
5707 * This function is called during initialization, before psa_crypto_init()
5708 * returns. If this function returns a failure status, the initialization
5709 * fails.
5710 */
5711static psa_status_t psa_crypto_recover_transaction(
5712 const psa_crypto_transaction_t *transaction )
5713{
5714 switch( transaction->unknown.type )
5715 {
5716 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5717 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005718 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005719 * is implemented.
5720 * https://github.com/ARMmbed/mbed-crypto/issues/218
5721 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005722 default:
5723 /* We found an unsupported transaction in the storage.
5724 * We don't know what state the storage is in. Give up. */
5725 return( PSA_ERROR_STORAGE_FAILURE );
5726 }
5727}
5728#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5729
Gilles Peskinee59236f2018-01-27 23:32:46 +01005730psa_status_t psa_crypto_init( void )
5731{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005732 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005733 const unsigned char drbg_seed[] = "PSA";
5734
Gilles Peskinec6b69072018-11-20 21:42:52 +01005735 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005736 if( global_data.initialized != 0 )
5737 return( PSA_SUCCESS );
5738
Gilles Peskine5e769522018-11-20 21:59:56 +01005739 /* Set default configuration if
5740 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5741 if( global_data.entropy_init == NULL )
5742 global_data.entropy_init = mbedtls_entropy_init;
5743 if( global_data.entropy_free == NULL )
5744 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005745
Gilles Peskinec6b69072018-11-20 21:42:52 +01005746 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005747 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005748#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5749 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5750 /* The PSA entropy injection feature depends on using NV seed as an entropy
5751 * source. Add NV seed as an entropy source for PSA entropy injection. */
5752 mbedtls_entropy_add_source( &global_data.entropy,
5753 mbedtls_nv_seed_poll, NULL,
5754 MBEDTLS_ENTROPY_BLOCK_SIZE,
5755 MBEDTLS_ENTROPY_SOURCE_STRONG );
5756#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005757 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005758 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005759 status = mbedtls_to_psa_error(
5760 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5761 mbedtls_entropy_func,
5762 &global_data.entropy,
5763 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5764 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005765 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005766 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005767
Gilles Peskine66fb1262018-12-10 16:29:04 +01005768 status = psa_initialize_key_slots( );
5769 if( status != PSA_SUCCESS )
5770 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005771
Gilles Peskined9348f22019-10-01 15:22:29 +02005772#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5773 status = psa_init_all_se_drivers( );
5774 if( status != PSA_SUCCESS )
5775 goto exit;
5776#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5777
Gilles Peskine4b734222019-07-24 15:56:31 +02005778#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005779 status = psa_crypto_load_transaction( );
5780 if( status == PSA_SUCCESS )
5781 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005782 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5783 if( status != PSA_SUCCESS )
5784 goto exit;
5785 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005786 }
5787 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5788 {
5789 /* There's no transaction to complete. It's all good. */
5790 status = PSA_SUCCESS;
5791 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005792#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005793
Gilles Peskinec6b69072018-11-20 21:42:52 +01005794 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005795 global_data.initialized = 1;
5796
5797exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005798 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005799 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005800 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005801}
5802
5803#endif /* MBEDTLS_PSA_CRYPTO_C */