blob: a0851c7f79daf5ef9d2a43dbf8046c4d59caa4d9 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb46bef22019-07-30 21:32:04 +020043#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include <stdlib.h>
45#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020047#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#define mbedtls_calloc calloc
49#define mbedtls_free free
50#endif
51
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020053#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000054#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020055#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/blowfish.h"
57#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020058#include "mbedtls/chacha20.h"
59#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020065#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/error.h"
69#include "mbedtls/gcm.h"
70#include "mbedtls/md2.h"
71#include "mbedtls/md4.h"
72#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
74#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000078#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/sha1.h"
82#include "mbedtls/sha256.h"
83#include "mbedtls/sha512.h"
84#include "mbedtls/xtea.h"
85
Gilles Peskine996deb12018-08-01 15:45:45 +020086#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
87
Gilles Peskine9ef733f2018-02-07 21:05:37 +010088/* constant-time buffer comparison */
89static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
90{
91 size_t i;
92 unsigned char diff = 0;
93
94 for( i = 0; i < n; i++ )
95 diff |= a[i] ^ b[i];
96
97 return( diff );
98}
99
100
101
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100102/****************************************************************/
103/* Global data, support functions and library management */
104/****************************************************************/
105
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200106static int key_type_is_raw_bytes( psa_key_type_t type )
107{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200108 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200109}
110
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100111/* Values for psa_global_data_t::rng_state */
112#define RNG_NOT_INITIALIZED 0
113#define RNG_INITIALIZED 1
114#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100115
Gilles Peskine2d277862018-06-18 15:41:12 +0200116typedef struct
117{
Gilles Peskine5e769522018-11-20 21:59:56 +0100118 void (* entropy_init )( mbedtls_entropy_context *ctx );
119 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100120 mbedtls_entropy_context entropy;
121 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100122 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100123 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100124} psa_global_data_t;
125
126static psa_global_data_t global_data;
127
itayzafrir0adf0fc2018-09-06 16:24:41 +0300128#define GUARD_MODULE_INITIALIZED \
129 if( global_data.initialized == 0 ) \
130 return( PSA_ERROR_BAD_STATE );
131
Gilles Peskinee59236f2018-01-27 23:32:46 +0100132static psa_status_t mbedtls_to_psa_error( int ret )
133{
Gilles Peskinea5905292018-02-07 20:59:33 +0100134 /* If there's both a high-level code and low-level code, dispatch on
135 * the high-level code. */
136 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100137 {
138 case 0:
139 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100140
141 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
142 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
143 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
144 return( PSA_ERROR_NOT_SUPPORTED );
145 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
146 return( PSA_ERROR_HARDWARE_FAILURE );
147
148 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
149 return( PSA_ERROR_HARDWARE_FAILURE );
150
Gilles Peskine9a944802018-06-21 09:35:35 +0200151 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
152 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
153 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
154 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
155 case MBEDTLS_ERR_ASN1_INVALID_DATA:
156 return( PSA_ERROR_INVALID_ARGUMENT );
157 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
158 return( PSA_ERROR_INSUFFICIENT_MEMORY );
159 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
160 return( PSA_ERROR_BUFFER_TOO_SMALL );
161
Jaeden Amero93e21112019-02-20 13:57:28 +0000162#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000163 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000164#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
165 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
166#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100167 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
168 return( PSA_ERROR_NOT_SUPPORTED );
169 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
170 return( PSA_ERROR_HARDWARE_FAILURE );
171
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000173 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
175 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
176#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100177 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
178 return( PSA_ERROR_NOT_SUPPORTED );
179 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
180 return( PSA_ERROR_HARDWARE_FAILURE );
181
182 case MBEDTLS_ERR_CCM_BAD_INPUT:
183 return( PSA_ERROR_INVALID_ARGUMENT );
184 case MBEDTLS_ERR_CCM_AUTH_FAILED:
185 return( PSA_ERROR_INVALID_SIGNATURE );
186 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
187 return( PSA_ERROR_HARDWARE_FAILURE );
188
Gilles Peskine26869f22019-05-06 15:25:00 +0200189 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
190 return( PSA_ERROR_INVALID_ARGUMENT );
191
192 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
193 return( PSA_ERROR_BAD_STATE );
194 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
195 return( PSA_ERROR_INVALID_SIGNATURE );
196
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
198 return( PSA_ERROR_NOT_SUPPORTED );
199 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
200 return( PSA_ERROR_INVALID_ARGUMENT );
201 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
202 return( PSA_ERROR_INSUFFICIENT_MEMORY );
203 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
204 return( PSA_ERROR_INVALID_PADDING );
205 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
206 return( PSA_ERROR_BAD_STATE );
207 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
208 return( PSA_ERROR_INVALID_SIGNATURE );
209 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200210 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100211 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
214 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
215 return( PSA_ERROR_HARDWARE_FAILURE );
216
217 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
218 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
219 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
220 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
221 return( PSA_ERROR_NOT_SUPPORTED );
222 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
223 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
224
225 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
226 return( PSA_ERROR_NOT_SUPPORTED );
227 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
228 return( PSA_ERROR_HARDWARE_FAILURE );
229
Gilles Peskinee59236f2018-01-27 23:32:46 +0100230 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
231 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
232 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
233 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100234
235 case MBEDTLS_ERR_GCM_AUTH_FAILED:
236 return( PSA_ERROR_INVALID_SIGNATURE );
237 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200238 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100239 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
240 return( PSA_ERROR_HARDWARE_FAILURE );
241
242 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
243 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
244 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
245 return( PSA_ERROR_HARDWARE_FAILURE );
246
247 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
248 return( PSA_ERROR_NOT_SUPPORTED );
249 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
250 return( PSA_ERROR_INVALID_ARGUMENT );
251 case MBEDTLS_ERR_MD_ALLOC_FAILED:
252 return( PSA_ERROR_INSUFFICIENT_MEMORY );
253 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
254 return( PSA_ERROR_STORAGE_FAILURE );
255 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
256 return( PSA_ERROR_HARDWARE_FAILURE );
257
Gilles Peskinef76aa772018-10-29 19:24:33 +0100258 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
259 return( PSA_ERROR_STORAGE_FAILURE );
260 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
261 return( PSA_ERROR_INVALID_ARGUMENT );
262 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
265 return( PSA_ERROR_BUFFER_TOO_SMALL );
266 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
271 return( PSA_ERROR_INVALID_ARGUMENT );
272 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
273 return( PSA_ERROR_INSUFFICIENT_MEMORY );
274
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100275 case MBEDTLS_ERR_PK_ALLOC_FAILED:
276 return( PSA_ERROR_INSUFFICIENT_MEMORY );
277 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
278 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
279 return( PSA_ERROR_INVALID_ARGUMENT );
280 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100281 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100282 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
283 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
284 return( PSA_ERROR_INVALID_ARGUMENT );
285 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
286 return( PSA_ERROR_NOT_SUPPORTED );
287 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
288 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
289 return( PSA_ERROR_NOT_PERMITTED );
290 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_PK_INVALID_ALG:
293 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
294 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
295 return( PSA_ERROR_NOT_SUPPORTED );
296 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
297 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100298 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
299 return( PSA_ERROR_HARDWARE_FAILURE );
300
Gilles Peskineff2d2002019-05-06 15:26:23 +0200301 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
302 return( PSA_ERROR_HARDWARE_FAILURE );
303 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
304 return( PSA_ERROR_NOT_SUPPORTED );
305
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
307 return( PSA_ERROR_HARDWARE_FAILURE );
308
309 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
310 return( PSA_ERROR_INVALID_ARGUMENT );
311 case MBEDTLS_ERR_RSA_INVALID_PADDING:
312 return( PSA_ERROR_INVALID_PADDING );
313 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
314 return( PSA_ERROR_HARDWARE_FAILURE );
315 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
316 return( PSA_ERROR_INVALID_ARGUMENT );
317 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
318 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200319 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100320 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
321 return( PSA_ERROR_INVALID_SIGNATURE );
322 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
323 return( PSA_ERROR_BUFFER_TOO_SMALL );
324 case MBEDTLS_ERR_RSA_RNG_FAILED:
325 return( PSA_ERROR_INSUFFICIENT_MEMORY );
326 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
327 return( PSA_ERROR_NOT_SUPPORTED );
328 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330
331 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
332 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
333 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
334 return( PSA_ERROR_HARDWARE_FAILURE );
335
336 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
337 return( PSA_ERROR_INVALID_ARGUMENT );
338 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
339 return( PSA_ERROR_HARDWARE_FAILURE );
340
itayzafrir5c753392018-05-08 11:18:38 +0300341 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300342 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300343 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300344 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
345 return( PSA_ERROR_BUFFER_TOO_SMALL );
346 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
347 return( PSA_ERROR_NOT_SUPPORTED );
348 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
349 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
350 return( PSA_ERROR_INVALID_SIGNATURE );
351 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
352 return( PSA_ERROR_INSUFFICIENT_MEMORY );
353 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
354 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000355 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
356 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300357
Gilles Peskinee59236f2018-01-27 23:32:46 +0100358 default:
David Saadab4ecc272019-02-14 13:48:10 +0200359 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100360 }
361}
362
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200363
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200364
365
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100366/****************************************************************/
367/* Key management */
368/****************************************************************/
369
Gilles Peskine73167e12019-07-12 23:44:37 +0200370#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
371static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
372{
Gilles Peskine8e338702019-07-30 20:06:31 +0200373 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200374}
375#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
376
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100377#if defined(MBEDTLS_ECP_C)
Gilles Peskine5055b232019-12-12 17:49:31 +0100378mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve,
379 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200380{
381 switch( curve )
382 {
Gilles Peskine228abc52019-12-03 17:24:19 +0100383 case PSA_ECC_CURVE_SECP_R1:
384 switch( byte_length )
385 {
386 case PSA_BITS_TO_BYTES( 192 ):
387 return( MBEDTLS_ECP_DP_SECP192R1 );
388 case PSA_BITS_TO_BYTES( 224 ):
389 return( MBEDTLS_ECP_DP_SECP224R1 );
390 case PSA_BITS_TO_BYTES( 256 ):
391 return( MBEDTLS_ECP_DP_SECP256R1 );
392 case PSA_BITS_TO_BYTES( 384 ):
393 return( MBEDTLS_ECP_DP_SECP384R1 );
394 case PSA_BITS_TO_BYTES( 521 ):
395 return( MBEDTLS_ECP_DP_SECP521R1 );
396 default:
397 return( MBEDTLS_ECP_DP_NONE );
398 }
399 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100400
401 case PSA_ECC_CURVE_BRAINPOOL_P_R1:
402 switch( byte_length )
403 {
404 case PSA_BITS_TO_BYTES( 256 ):
405 return( MBEDTLS_ECP_DP_BP256R1 );
406 case PSA_BITS_TO_BYTES( 384 ):
407 return( MBEDTLS_ECP_DP_BP384R1 );
408 case PSA_BITS_TO_BYTES( 512 ):
409 return( MBEDTLS_ECP_DP_BP512R1 );
410 default:
411 return( MBEDTLS_ECP_DP_NONE );
412 }
413 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100414
415 case PSA_ECC_CURVE_MONTGOMERY:
416 switch( byte_length )
417 {
418 case PSA_BITS_TO_BYTES( 255 ):
419 return( MBEDTLS_ECP_DP_CURVE25519 );
420 case PSA_BITS_TO_BYTES( 448 ):
421 return( MBEDTLS_ECP_DP_CURVE448 );
422 default:
423 return( MBEDTLS_ECP_DP_NONE );
424 }
425 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100426
427 case PSA_ECC_CURVE_SECP_K1:
428 switch( byte_length )
429 {
430 case PSA_BITS_TO_BYTES( 192 ):
431 return( MBEDTLS_ECP_DP_SECP192K1 );
432 case PSA_BITS_TO_BYTES( 224 ):
433 return( MBEDTLS_ECP_DP_SECP224K1 );
434 case PSA_BITS_TO_BYTES( 256 ):
435 return( MBEDTLS_ECP_DP_SECP256K1 );
436 default:
437 return( MBEDTLS_ECP_DP_NONE );
438 }
439 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100440
Gilles Peskine12313cd2018-06-20 00:20:32 +0200441 default:
442 return( MBEDTLS_ECP_DP_NONE );
443 }
444}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100445#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200446
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200447static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
448 size_t bits,
449 struct raw_data *raw )
450{
451 /* Check that the bit size is acceptable for the key type */
452 switch( type )
453 {
454 case PSA_KEY_TYPE_RAW_DATA:
455#if defined(MBEDTLS_MD_C)
456 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200457#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200458 case PSA_KEY_TYPE_DERIVE:
459 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460#if defined(MBEDTLS_AES_C)
461 case PSA_KEY_TYPE_AES:
462 if( bits != 128 && bits != 192 && bits != 256 )
463 return( PSA_ERROR_INVALID_ARGUMENT );
464 break;
465#endif
466#if defined(MBEDTLS_CAMELLIA_C)
467 case PSA_KEY_TYPE_CAMELLIA:
468 if( bits != 128 && bits != 192 && bits != 256 )
469 return( PSA_ERROR_INVALID_ARGUMENT );
470 break;
471#endif
472#if defined(MBEDTLS_DES_C)
473 case PSA_KEY_TYPE_DES:
474 if( bits != 64 && bits != 128 && bits != 192 )
475 return( PSA_ERROR_INVALID_ARGUMENT );
476 break;
477#endif
478#if defined(MBEDTLS_ARC4_C)
479 case PSA_KEY_TYPE_ARC4:
480 if( bits < 8 || bits > 2048 )
481 return( PSA_ERROR_INVALID_ARGUMENT );
482 break;
483#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200484#if defined(MBEDTLS_CHACHA20_C)
485 case PSA_KEY_TYPE_CHACHA20:
486 if( bits != 256 )
487 return( PSA_ERROR_INVALID_ARGUMENT );
488 break;
489#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490 default:
491 return( PSA_ERROR_NOT_SUPPORTED );
492 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200493 if( bits % 8 != 0 )
494 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495
496 /* Allocate memory for the key */
497 raw->bytes = PSA_BITS_TO_BYTES( bits );
498 raw->data = mbedtls_calloc( 1, raw->bytes );
499 if( raw->data == NULL )
500 {
501 raw->bytes = 0;
502 return( PSA_ERROR_INSUFFICIENT_MEMORY );
503 }
504 return( PSA_SUCCESS );
505}
506
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200507#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100508/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
509 * that are not a multiple of 8) well. For example, there is only
510 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
511 * way to return the exact bit size of a key.
512 * To keep things simple, reject non-byte-aligned key sizes. */
513static psa_status_t psa_check_rsa_key_byte_aligned(
514 const mbedtls_rsa_context *rsa )
515{
516 mbedtls_mpi n;
517 psa_status_t status;
518 mbedtls_mpi_init( &n );
519 status = mbedtls_to_psa_error(
520 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
521 if( status == PSA_SUCCESS )
522 {
523 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
524 status = PSA_ERROR_NOT_SUPPORTED;
525 }
526 mbedtls_mpi_free( &n );
527 return( status );
528}
529
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000530static psa_status_t psa_import_rsa_key( psa_key_type_t type,
531 const uint8_t *data,
532 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200533 mbedtls_rsa_context **p_rsa )
534{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000535 psa_status_t status;
536 mbedtls_pk_context pk;
537 mbedtls_rsa_context *rsa;
538 size_t bits;
539
540 mbedtls_pk_init( &pk );
541
542 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200543 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000544 status = mbedtls_to_psa_error(
545 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200546 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000547 status = mbedtls_to_psa_error(
548 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
549 if( status != PSA_SUCCESS )
550 goto exit;
551
552 /* We have something that the pkparse module recognizes. If it is a
553 * valid RSA key, store it. */
554 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200555 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000556 status = PSA_ERROR_INVALID_ARGUMENT;
557 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200558 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000559
560 rsa = mbedtls_pk_rsa( pk );
561 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
562 * supports non-byte-aligned key sizes, but not well. For example,
563 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
564 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
565 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
566 {
567 status = PSA_ERROR_NOT_SUPPORTED;
568 goto exit;
569 }
570 status = psa_check_rsa_key_byte_aligned( rsa );
571
572exit:
573 /* Free the content of the pk object only on error. */
574 if( status != PSA_SUCCESS )
575 {
576 mbedtls_pk_free( &pk );
577 return( status );
578 }
579
580 /* On success, store the content of the object in the RSA context. */
581 *p_rsa = rsa;
582
583 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200584}
585#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
586
Jaeden Ameroccdce902019-01-10 11:42:27 +0000587#if defined(MBEDTLS_ECP_C)
Gilles Peskine4cd32772019-12-02 20:49:42 +0100588static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve,
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100589 size_t data_length,
590 int is_public,
Gilles Peskine4cd32772019-12-02 20:49:42 +0100591 mbedtls_ecp_keypair **p_ecp )
592{
593 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
594 *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
595 if( *p_ecp == NULL )
596 return( PSA_ERROR_INSUFFICIENT_MEMORY );
597 mbedtls_ecp_keypair_init( *p_ecp );
598
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100599 if( is_public )
600 {
601 /* A public key is represented as:
602 * - The byte 0x04;
603 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
604 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
605 * So its data length is 2m+1 where n is the key size in bits.
606 */
607 if( ( data_length & 1 ) == 0 )
608 return( PSA_ERROR_INVALID_ARGUMENT );
609 data_length = data_length / 2;
610 }
611
Gilles Peskine4cd32772019-12-02 20:49:42 +0100612 /* Load the group. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100613 grp_id = mbedtls_ecc_group_of_psa( curve, data_length );
614 if( grp_id == MBEDTLS_ECP_DP_NONE )
615 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100616 return( mbedtls_to_psa_error(
617 mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) );
618}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000619
620/* Import a public key given as the uncompressed representation defined by SEC1
621 * 2.3.3 as the content of an ECPoint. */
622static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
623 const uint8_t *data,
624 size_t data_length,
625 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200626{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200627 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000628 mbedtls_ecp_keypair *ecp = NULL;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000629
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100630 status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp );
Jaeden Ameroccdce902019-01-10 11:42:27 +0000631 if( status != PSA_SUCCESS )
632 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100633
Jaeden Ameroccdce902019-01-10 11:42:27 +0000634 /* Load the public value. */
635 status = mbedtls_to_psa_error(
636 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
637 data, data_length ) );
638 if( status != PSA_SUCCESS )
639 goto exit;
640
641 /* Check that the point is on the curve. */
642 status = mbedtls_to_psa_error(
643 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
644 if( status != PSA_SUCCESS )
645 goto exit;
646
647 *p_ecp = ecp;
648 return( PSA_SUCCESS );
649
650exit:
651 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200652 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000653 mbedtls_ecp_keypair_free( ecp );
654 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200655 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000656 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200657}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200658
Gilles Peskinef76aa772018-10-29 19:24:33 +0100659/* Import a private key given as a byte string which is the private value
660 * in big-endian order. */
661static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
662 const uint8_t *data,
663 size_t data_length,
664 mbedtls_ecp_keypair **p_ecp )
665{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200666 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100667 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100668
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100669 status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100670 if( status != PSA_SUCCESS )
671 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100672
Gilles Peskinef76aa772018-10-29 19:24:33 +0100673 /* Load the secret value. */
674 status = mbedtls_to_psa_error(
675 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
676 if( status != PSA_SUCCESS )
677 goto exit;
678 /* Validate the private key. */
679 status = mbedtls_to_psa_error(
680 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
681 if( status != PSA_SUCCESS )
682 goto exit;
683 /* Calculate the public key from the private key. */
684 status = mbedtls_to_psa_error(
685 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
686 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
687 if( status != PSA_SUCCESS )
688 goto exit;
689
690 *p_ecp = ecp;
691 return( PSA_SUCCESS );
692
693exit:
694 if( ecp != NULL )
695 {
696 mbedtls_ecp_keypair_free( ecp );
697 mbedtls_free( ecp );
698 }
699 return( status );
700}
701#endif /* defined(MBEDTLS_ECP_C) */
702
Gilles Peskineb46bef22019-07-30 21:32:04 +0200703
704/** Return the size of the key in the given slot, in bits.
705 *
706 * \param[in] slot A key slot.
707 *
708 * \return The key size in bits, read from the metadata in the slot.
709 */
710static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
711{
712 return( slot->attr.bits );
713}
714
715/** Calculate the size of the key in the given slot, in bits.
716 *
717 * \param[in] slot A key slot containing a transparent key.
718 *
719 * \return The key size in bits, calculated from the key data.
720 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200721static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200722{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200723 size_t bits = 0; /* return 0 on an empty slot */
724
Gilles Peskineb46bef22019-07-30 21:32:04 +0200725 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200726 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200727#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200728 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
729 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200730#endif /* defined(MBEDTLS_RSA_C) */
731#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200732 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
733 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200734#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200735
736 /* We know that the size fits in psa_key_bits_t thanks to checks
737 * when the key was created. */
738 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200739}
740
Gilles Peskine8e338702019-07-30 20:06:31 +0200741/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100742 * previously. This function assumes that the slot does not contain
743 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100744psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
745 const uint8_t *data,
746 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100747{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200748 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100749
Gilles Peskine8e338702019-07-30 20:06:31 +0200750 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100751 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200752 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200753 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100754 if( data_length > SIZE_MAX / 8 )
755 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200756 /* Enforce a size limit, and in particular ensure that the bit
757 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200758 if( bit_size > PSA_MAX_KEY_BITS )
759 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200760 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200761 &slot->data.raw );
762 if( status != PSA_SUCCESS )
763 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200764 if( data_length != 0 )
765 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100766 }
767 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100768#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200769 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100770 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200771 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100772 data, data_length,
773 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100774 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200775 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100776 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000777 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200778 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000779 data, data_length,
780 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100781 }
782 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100783#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000784#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200785 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100786 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200787 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000788 data, data_length,
789 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100790 }
791 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000792#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100793 {
794 return( PSA_ERROR_NOT_SUPPORTED );
795 }
Darryl Green940d72c2018-07-13 13:18:51 +0100796
Gilles Peskineb46bef22019-07-30 21:32:04 +0200797 if( status == PSA_SUCCESS )
798 {
799 /* Write the actual key size to the slot.
800 * psa_start_key_creation() wrote the size declared by the
801 * caller, which may be 0 (meaning unspecified) or wrong. */
802 slot->attr.bits = psa_calculate_key_bits( slot );
803 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100804 return( status );
805}
806
Gilles Peskinef603c712019-01-19 13:40:11 +0100807/** Calculate the intersection of two algorithm usage policies.
808 *
809 * Return 0 (which allows no operation) on incompatibility.
810 */
811static psa_algorithm_t psa_key_policy_algorithm_intersection(
812 psa_algorithm_t alg1,
813 psa_algorithm_t alg2 )
814{
Gilles Peskine549ea862019-05-22 11:45:59 +0200815 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100816 if( alg1 == alg2 )
817 return( alg1 );
818 /* If the policies are from the same hash-and-sign family, check
819 * if one is a wildcard. If so the other has the specific algorithm. */
820 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
821 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
822 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
823 {
824 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
825 return( alg2 );
826 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
827 return( alg1 );
828 }
829 /* If the policies are incompatible, allow nothing. */
830 return( 0 );
831}
832
Gilles Peskined6f371b2019-05-10 19:33:38 +0200833static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
834 psa_algorithm_t requested_alg )
835{
Gilles Peskine549ea862019-05-22 11:45:59 +0200836 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200837 if( requested_alg == policy_alg )
838 return( 1 );
839 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200840 * and requested_alg is the same hash-and-sign family with any hash,
841 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200842 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
843 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
844 {
845 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
846 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
847 }
848 /* If it isn't permitted, it's forbidden. */
849 return( 0 );
850}
851
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100852/** Test whether a policy permits an algorithm.
853 *
854 * The caller must test usage flags separately.
855 */
856static int psa_key_policy_permits( const psa_key_policy_t *policy,
857 psa_algorithm_t alg )
858{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200859 return( psa_key_algorithm_permits( policy->alg, alg ) ||
860 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100861}
862
Gilles Peskinef603c712019-01-19 13:40:11 +0100863/** Restrict a key policy based on a constraint.
864 *
865 * \param[in,out] policy The policy to restrict.
866 * \param[in] constraint The policy constraint to apply.
867 *
868 * \retval #PSA_SUCCESS
869 * \c *policy contains the intersection of the original value of
870 * \c *policy and \c *constraint.
871 * \retval #PSA_ERROR_INVALID_ARGUMENT
872 * \c *policy and \c *constraint are incompatible.
873 * \c *policy is unchanged.
874 */
875static psa_status_t psa_restrict_key_policy(
876 psa_key_policy_t *policy,
877 const psa_key_policy_t *constraint )
878{
879 psa_algorithm_t intersection_alg =
880 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200881 psa_algorithm_t intersection_alg2 =
882 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100883 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
884 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200885 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
886 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100887 policy->usage &= constraint->usage;
888 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200889 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100890 return( PSA_SUCCESS );
891}
892
Darryl Green06fd18d2018-07-16 11:21:11 +0100893/** Retrieve a slot which must contain a key. The key must have allow all the
894 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
895 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100896static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100897 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100898 psa_key_usage_t usage,
899 psa_algorithm_t alg )
900{
901 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100902 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100903
904 *p_slot = NULL;
905
Gilles Peskinec5487a82018-12-03 18:08:14 +0100906 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100907 if( status != PSA_SUCCESS )
908 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100909
910 /* Enforce that usage policy for the key slot contains all the flags
911 * required by the usage parameter. There is one exception: public
912 * keys can always be exported, so we treat public key objects as
913 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200914 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100915 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200916 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100917 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100918
919 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200920 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100921 return( PSA_ERROR_NOT_PERMITTED );
922
923 *p_slot = slot;
924 return( PSA_SUCCESS );
925}
Darryl Green940d72c2018-07-13 13:18:51 +0100926
Gilles Peskine28f8f302019-07-24 13:30:31 +0200927/** Retrieve a slot which must contain a transparent key.
928 *
929 * A transparent key is a key for which the key material is directly
930 * available, as opposed to a key in a secure element.
931 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200932 * This is a temporary function to use instead of psa_get_key_from_slot()
933 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200934 */
935#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
936static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
937 psa_key_slot_t **p_slot,
938 psa_key_usage_t usage,
939 psa_algorithm_t alg )
940{
941 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
942 if( status != PSA_SUCCESS )
943 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200944 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200945 {
946 *p_slot = NULL;
947 return( PSA_ERROR_NOT_SUPPORTED );
948 }
949 return( PSA_SUCCESS );
950}
951#else /* MBEDTLS_PSA_CRYPTO_SE_C */
952/* With no secure element support, all keys are transparent. */
953#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
954 psa_get_key_from_slot( handle, p_slot, usage, alg )
955#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
956
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100957/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100958static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000959{
Gilles Peskine73167e12019-07-12 23:44:37 +0200960#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
961 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000962 {
963 /* No key material to clean. */
964 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200965 else
966#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200967 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000968 {
969 /* No key material to clean. */
970 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200971 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000972 {
973 mbedtls_free( slot->data.raw.data );
974 }
975 else
976#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200977 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000978 {
979 mbedtls_rsa_free( slot->data.rsa );
980 mbedtls_free( slot->data.rsa );
981 }
982 else
983#endif /* defined(MBEDTLS_RSA_C) */
984#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200985 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000986 {
987 mbedtls_ecp_keypair_free( slot->data.ecp );
988 mbedtls_free( slot->data.ecp );
989 }
990 else
991#endif /* defined(MBEDTLS_ECP_C) */
992 {
993 /* Shouldn't happen: the key type is not any type that we
994 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200995 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000996 }
997
998 return( PSA_SUCCESS );
999}
1000
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001001/** Completely wipe a slot in memory, including its policy.
1002 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001003psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001004{
1005 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001006 /* Multipart operations may still be using the key. This is safe
1007 * because all multipart operation objects are independent from
1008 * the key slot: if they need to access the key after the setup
1009 * phase, they have a copy of the key. Note that this means that
1010 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001011 /* At this point, key material and other type-specific content has
1012 * been wiped. Clear remaining metadata. We can call memset and not
1013 * zeroize because the metadata is not particularly sensitive. */
1014 memset( slot, 0, sizeof( *slot ) );
1015 return( status );
1016}
1017
Gilles Peskinec5487a82018-12-03 18:08:14 +01001018psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001019{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001020 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001021 psa_status_t status; /* status of the last operation */
1022 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001023#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1024 psa_se_drv_table_entry_t *driver;
1025#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001026
Gilles Peskine1841cf42019-10-08 15:48:25 +02001027 if( handle == 0 )
1028 return( PSA_SUCCESS );
1029
Gilles Peskinec5487a82018-12-03 18:08:14 +01001030 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001031 if( status != PSA_SUCCESS )
1032 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001033
1034#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001035 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001036 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001037 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001038 /* For a key in a secure element, we need to do three things:
1039 * remove the key file in internal storage, destroy the
1040 * key inside the secure element, and update the driver's
1041 * persistent data. Start a transaction that will encompass these
1042 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001043 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001044 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001045 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001046 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001047 status = psa_crypto_save_transaction( );
1048 if( status != PSA_SUCCESS )
1049 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001050 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001051 /* We should still try to destroy the key in the secure
1052 * element and the key metadata in storage. This is especially
1053 * important if the error is that the storage is full.
1054 * But how to do it exactly without risking an inconsistent
1055 * state after a reset?
1056 * https://github.com/ARMmbed/mbed-crypto/issues/215
1057 */
1058 overall_status = status;
1059 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001060 }
1061
Gilles Peskine354f7672019-07-12 23:46:38 +02001062 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001063 if( overall_status == PSA_SUCCESS )
1064 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001065 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001066#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1067
Darryl Greend49a4992018-06-18 17:27:26 +01001068#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001069 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001070 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001071 status = psa_destroy_persistent_key( slot->attr.id );
1072 if( overall_status == PSA_SUCCESS )
1073 overall_status = status;
1074
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001075 /* TODO: other slots may have a copy of the same key. We should
1076 * invalidate them.
1077 * https://github.com/ARMmbed/mbed-crypto/issues/214
1078 */
Darryl Greend49a4992018-06-18 17:27:26 +01001079 }
1080#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001081
Gilles Peskinefc762652019-07-22 19:30:34 +02001082#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1083 if( driver != NULL )
1084 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001085 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001086 if( overall_status == PSA_SUCCESS )
1087 overall_status = status;
1088 status = psa_crypto_stop_transaction( );
1089 if( overall_status == PSA_SUCCESS )
1090 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001091 }
1092#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1093
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001094#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1095exit:
1096#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001097 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001098 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1099 if( overall_status == PSA_SUCCESS )
1100 overall_status = status;
1101 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001102}
1103
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001104void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001105{
Gilles Peskineb699f072019-04-26 16:06:02 +02001106 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001107 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001108}
1109
Gilles Peskineb699f072019-04-26 16:06:02 +02001110psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1111 psa_key_type_t type,
1112 const uint8_t *data,
1113 size_t data_length )
1114{
1115 uint8_t *copy = NULL;
1116
1117 if( data_length != 0 )
1118 {
1119 copy = mbedtls_calloc( 1, data_length );
1120 if( copy == NULL )
1121 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1122 memcpy( copy, data, data_length );
1123 }
1124 /* After this point, this function is guaranteed to succeed, so it
1125 * can start modifying `*attributes`. */
1126
1127 if( attributes->domain_parameters != NULL )
1128 {
1129 mbedtls_free( attributes->domain_parameters );
1130 attributes->domain_parameters = NULL;
1131 attributes->domain_parameters_size = 0;
1132 }
1133
1134 attributes->domain_parameters = copy;
1135 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001136 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001137 return( PSA_SUCCESS );
1138}
1139
1140psa_status_t psa_get_key_domain_parameters(
1141 const psa_key_attributes_t *attributes,
1142 uint8_t *data, size_t data_size, size_t *data_length )
1143{
1144 if( attributes->domain_parameters_size > data_size )
1145 return( PSA_ERROR_BUFFER_TOO_SMALL );
1146 *data_length = attributes->domain_parameters_size;
1147 if( attributes->domain_parameters_size != 0 )
1148 memcpy( data, attributes->domain_parameters,
1149 attributes->domain_parameters_size );
1150 return( PSA_SUCCESS );
1151}
1152
1153#if defined(MBEDTLS_RSA_C)
1154static psa_status_t psa_get_rsa_public_exponent(
1155 const mbedtls_rsa_context *rsa,
1156 psa_key_attributes_t *attributes )
1157{
1158 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001159 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001160 uint8_t *buffer = NULL;
1161 size_t buflen;
1162 mbedtls_mpi_init( &mpi );
1163
1164 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1165 if( ret != 0 )
1166 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001167 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1168 {
1169 /* It's the default value, which is reported as an empty string,
1170 * so there's nothing to do. */
1171 goto exit;
1172 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001173
1174 buflen = mbedtls_mpi_size( &mpi );
1175 buffer = mbedtls_calloc( 1, buflen );
1176 if( buffer == NULL )
1177 {
1178 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1179 goto exit;
1180 }
1181 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1182 if( ret != 0 )
1183 goto exit;
1184 attributes->domain_parameters = buffer;
1185 attributes->domain_parameters_size = buflen;
1186
1187exit:
1188 mbedtls_mpi_free( &mpi );
1189 if( ret != 0 )
1190 mbedtls_free( buffer );
1191 return( mbedtls_to_psa_error( ret ) );
1192}
1193#endif /* MBEDTLS_RSA_C */
1194
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001195/** Retrieve all the publicly-accessible attributes of a key.
1196 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001197psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1198 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001199{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001200 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001201 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001202
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001203 psa_reset_key_attributes( attributes );
1204
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001205 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001206 if( status != PSA_SUCCESS )
1207 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001208
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001209 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001210 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1211 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1212
1213#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1214 if( psa_key_slot_is_external( slot ) )
1215 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1216#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001217
Gilles Peskine8e338702019-07-30 20:06:31 +02001218 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001219 {
1220#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001221 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001222 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001223#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001224 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001225 * is not yet implemented.
1226 * https://github.com/ARMmbed/mbed-crypto/issues/216
1227 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001228 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001229 break;
1230#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001231 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1232 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001233#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001234 default:
1235 /* Nothing else to do. */
1236 break;
1237 }
1238
1239 if( status != PSA_SUCCESS )
1240 psa_reset_key_attributes( attributes );
1241 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001242}
1243
Gilles Peskinec8000c02019-08-02 20:15:51 +02001244#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1245psa_status_t psa_get_key_slot_number(
1246 const psa_key_attributes_t *attributes,
1247 psa_key_slot_number_t *slot_number )
1248{
1249 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1250 {
1251 *slot_number = attributes->slot_number;
1252 return( PSA_SUCCESS );
1253 }
1254 else
1255 return( PSA_ERROR_INVALID_ARGUMENT );
1256}
1257#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1258
Jaeden Ameroccdce902019-01-10 11:42:27 +00001259#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001260static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1261 unsigned char *buf, size_t size )
1262{
Janos Follath24eed8d2019-11-22 13:21:35 +00001263 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001264 unsigned char *c;
1265 size_t len = 0;
1266
1267 c = buf + size;
1268
1269 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1270
1271 return( (int) len );
1272}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001273#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001274
Gilles Peskinef603c712019-01-19 13:40:11 +01001275static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1276 uint8_t *data,
1277 size_t data_size,
1278 size_t *data_length,
1279 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001280{
Gilles Peskine5d309672019-07-12 23:47:28 +02001281#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1282 const psa_drv_se_t *drv;
1283 psa_drv_se_context_t *drv_context;
1284#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1285
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001286 *data_length = 0;
1287
Gilles Peskine8e338702019-07-30 20:06:31 +02001288 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001289 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001290
Gilles Peskinef9168942019-09-12 19:20:29 +02001291 /* Reject a zero-length output buffer now, since this can never be a
1292 * valid key representation. This way we know that data must be a valid
1293 * pointer and we can do things like memset(data, ..., data_size). */
1294 if( data_size == 0 )
1295 return( PSA_ERROR_BUFFER_TOO_SMALL );
1296
Gilles Peskine5d309672019-07-12 23:47:28 +02001297#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001298 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001299 {
1300 psa_drv_se_export_key_t method;
1301 if( drv->key_management == NULL )
1302 return( PSA_ERROR_NOT_SUPPORTED );
1303 method = ( export_public_key ?
1304 drv->key_management->p_export_public :
1305 drv->key_management->p_export );
1306 if( method == NULL )
1307 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001308 return( method( drv_context,
1309 slot->data.se.slot_number,
1310 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001311 }
1312#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1313
Gilles Peskine8e338702019-07-30 20:06:31 +02001314 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001315 {
1316 if( slot->data.raw.bytes > data_size )
1317 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001318 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1319 memset( data + slot->data.raw.bytes, 0,
1320 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001321 *data_length = slot->data.raw.bytes;
1322 return( PSA_SUCCESS );
1323 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001324#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001325 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001326 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001327 psa_status_t status;
1328
Gilles Peskineb46bef22019-07-30 21:32:04 +02001329 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001330 if( bytes > data_size )
1331 return( PSA_ERROR_BUFFER_TOO_SMALL );
1332 status = mbedtls_to_psa_error(
1333 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1334 if( status != PSA_SUCCESS )
1335 return( status );
1336 memset( data + bytes, 0, data_size - bytes );
1337 *data_length = bytes;
1338 return( PSA_SUCCESS );
1339 }
1340#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001341 else
Moran Peker17e36e12018-05-02 12:55:20 +03001342 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001343#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001344 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1345 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001346 {
Moran Pekera998bc62018-04-16 18:16:20 +03001347 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001348 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001349 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001350 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001351#if defined(MBEDTLS_RSA_C)
1352 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001353 pk.pk_info = &mbedtls_rsa_info;
1354 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001355#else
1356 return( PSA_ERROR_NOT_SUPPORTED );
1357#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001358 }
1359 else
1360 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001361#if defined(MBEDTLS_ECP_C)
1362 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001363 pk.pk_info = &mbedtls_eckey_info;
1364 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001365#else
1366 return( PSA_ERROR_NOT_SUPPORTED );
1367#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001368 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001369 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001370 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001371 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001372 }
Moran Peker17e36e12018-05-02 12:55:20 +03001373 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001374 {
Moran Peker17e36e12018-05-02 12:55:20 +03001375 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001376 }
Moran Peker60364322018-04-29 11:34:58 +03001377 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001378 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001379 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001380 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001381 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001382 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1383 * Move the data to the beginning and erase remaining data
1384 * at the original location. */
1385 if( 2 * (size_t) ret <= data_size )
1386 {
1387 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001388 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001389 }
1390 else if( (size_t) ret < data_size )
1391 {
1392 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001393 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001394 }
Moran Pekera998bc62018-04-16 18:16:20 +03001395 *data_length = ret;
1396 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001397 }
1398 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001399#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001400 {
1401 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001402 it is valid for a special-purpose implementation to omit
1403 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001404 return( PSA_ERROR_NOT_SUPPORTED );
1405 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001406 }
1407}
1408
Gilles Peskinec5487a82018-12-03 18:08:14 +01001409psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001410 uint8_t *data,
1411 size_t data_size,
1412 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001413{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001414 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001415 psa_status_t status;
1416
1417 /* Set the key to empty now, so that even when there are errors, we always
1418 * set data_length to a value between 0 and data_size. On error, setting
1419 * the key to empty is a good choice because an empty key representation is
1420 * unlikely to be accepted anywhere. */
1421 *data_length = 0;
1422
1423 /* Export requires the EXPORT flag. There is an exception for public keys,
1424 * which don't require any flag, but psa_get_key_from_slot takes
1425 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001426 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001427 if( status != PSA_SUCCESS )
1428 return( status );
1429 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001430 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001431}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001432
Gilles Peskinec5487a82018-12-03 18:08:14 +01001433psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001434 uint8_t *data,
1435 size_t data_size,
1436 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001437{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001438 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001439 psa_status_t status;
1440
1441 /* Set the key to empty now, so that even when there are errors, we always
1442 * set data_length to a value between 0 and data_size. On error, setting
1443 * the key to empty is a good choice because an empty key representation is
1444 * unlikely to be accepted anywhere. */
1445 *data_length = 0;
1446
1447 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001448 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001449 if( status != PSA_SUCCESS )
1450 return( status );
1451 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001452 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001453}
1454
Gilles Peskine91e8c332019-08-02 19:19:39 +02001455#if defined(static_assert)
1456static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1457 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001458static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001459 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001460static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001461 "One or more key attribute flag is listed as both internal-only and external-only" );
1462#endif
1463
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001464/** Validate that a key policy is internally well-formed.
1465 *
1466 * This function only rejects invalid policies. It does not validate the
1467 * consistency of the policy with respect to other attributes of the key
1468 * such as the key type.
1469 */
1470static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001471{
1472 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001473 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001474 PSA_KEY_USAGE_ENCRYPT |
1475 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001476 PSA_KEY_USAGE_SIGN_HASH |
1477 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001478 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1479 return( PSA_ERROR_INVALID_ARGUMENT );
1480
Gilles Peskine4747d192019-04-17 15:05:45 +02001481 return( PSA_SUCCESS );
1482}
1483
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001484/** Validate the internal consistency of key attributes.
1485 *
1486 * This function only rejects invalid attribute values. If does not
1487 * validate the consistency of the attributes with any key data that may
1488 * be involved in the creation of the key.
1489 *
1490 * Call this function early in the key creation process.
1491 *
1492 * \param[in] attributes Key attributes for the new key.
1493 * \param[out] p_drv On any return, the driver for the key, if any.
1494 * NULL for a transparent key.
1495 *
1496 */
1497static psa_status_t psa_validate_key_attributes(
1498 const psa_key_attributes_t *attributes,
1499 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001500{
1501 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001502
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001503 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Darryl Green0c6575a2018-11-07 16:05:30 +00001504 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001505 status = psa_validate_persistent_key_parameters(
1506 attributes->core.lifetime, attributes->core.id,
1507 p_drv, 1 );
1508 if( status != PSA_SUCCESS )
1509 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001510 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001511
1512 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001513 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001514 return( status );
1515
1516 /* Refuse to create overly large keys.
1517 * Note that this doesn't trigger on import if the attributes don't
1518 * explicitly specify a size (so psa_get_key_bits returns 0), so
1519 * psa_import_key() needs its own checks. */
1520 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1521 return( PSA_ERROR_NOT_SUPPORTED );
1522
Gilles Peskine91e8c332019-08-02 19:19:39 +02001523 /* Reject invalid flags. These should not be reachable through the API. */
1524 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1525 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1526 return( PSA_ERROR_INVALID_ARGUMENT );
1527
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001528 return( PSA_SUCCESS );
1529}
1530
Gilles Peskine4747d192019-04-17 15:05:45 +02001531/** Prepare a key slot to receive key material.
1532 *
1533 * This function allocates a key slot and sets its metadata.
1534 *
1535 * If this function fails, call psa_fail_key_creation().
1536 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001537 * This function is intended to be used as follows:
1538 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1539 * it with the specified attributes, and assign it a handle.
1540 * -# Populate the slot with the key material.
1541 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1542 * In case of failure at any step, stop the sequence and call
1543 * psa_fail_key_creation().
1544 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001545 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001546 * \param[in] attributes Key attributes for the new key.
1547 * \param[out] handle On success, a handle for the allocated slot.
1548 * \param[out] p_slot On success, a pointer to the prepared slot.
1549 * \param[out] p_drv On any return, the driver for the key, if any.
1550 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001551 *
1552 * \retval #PSA_SUCCESS
1553 * The key slot is ready to receive key material.
1554 * \return If this function fails, the key slot is an invalid state.
1555 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001556 */
1557static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001558 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001559 const psa_key_attributes_t *attributes,
1560 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001561 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001562 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001563{
1564 psa_status_t status;
1565 psa_key_slot_t *slot;
1566
Gilles Peskinedf179142019-07-15 22:02:14 +02001567 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001568 *p_drv = NULL;
1569
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001570 status = psa_validate_key_attributes( attributes, p_drv );
1571 if( status != PSA_SUCCESS )
1572 return( status );
1573
Gilles Peskineedbed562019-08-07 18:19:59 +02001574 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001575 if( status != PSA_SUCCESS )
1576 return( status );
1577 slot = *p_slot;
1578
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001579 /* We're storing the declared bit-size of the key. It's up to each
1580 * creation mechanism to verify that this information is correct.
1581 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001582 * an input (generate, device) but not for those where the bit-size
1583 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001584
1585 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001586
Gilles Peskine91e8c332019-08-02 19:19:39 +02001587 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001588 * external-only flags, query `attributes`. Thanks to the check
1589 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001590 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001591 * may have set. */
1592 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001593
Gilles Peskinecbaff462019-07-12 23:46:04 +02001594#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001595 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001596 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001597 * create the key file in internal storage, create the
1598 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001599 * persistent data. This is done by starting a transaction that will
1600 * encompass these three actions.
1601 * For registering a volatile key, we just need to find an appropriate
1602 * slot number inside the SE. Since the key is designated volatile, creating
1603 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001604 /* The first thing to do is to find a slot number for the new key.
1605 * We save the slot number in persistent storage as part of the
1606 * transaction data. It will be needed to recover if the power
1607 * fails during the key creation process, to clean up on the secure
1608 * element side after restarting. Obtaining a slot number from the
1609 * secure element driver updates its persistent state, but we do not yet
1610 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001611 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001612 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001613 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001614 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001615 &slot->data.se.slot_number );
1616 if( status != PSA_SUCCESS )
1617 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001618
1619 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001620 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001621 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1622 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1623 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1624 psa_crypto_transaction.key.id = slot->attr.id;
1625 status = psa_crypto_save_transaction( );
1626 if( status != PSA_SUCCESS )
1627 {
1628 (void) psa_crypto_stop_transaction( );
1629 return( status );
1630 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001631 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001632 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001633
1634 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1635 {
1636 /* Key registration only makes sense with a secure element. */
1637 return( PSA_ERROR_INVALID_ARGUMENT );
1638 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001639#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1640
Darryl Green0c6575a2018-11-07 16:05:30 +00001641 return( status );
1642}
Gilles Peskine4747d192019-04-17 15:05:45 +02001643
1644/** Finalize the creation of a key once its key material has been set.
1645 *
1646 * This entails writing the key to persistent storage.
1647 *
1648 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001649 * See the documentation of psa_start_key_creation() for the intended use
1650 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001651 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001652 * \param[in,out] slot Pointer to the slot with key material.
1653 * \param[in] driver The secure element driver for the key,
1654 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001655 *
1656 * \retval #PSA_SUCCESS
1657 * The key was successfully created. The handle is now valid.
1658 * \return If this function fails, the key slot is an invalid state.
1659 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001660 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001661static psa_status_t psa_finish_key_creation(
1662 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001663 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001664{
1665 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001666 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001667 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001668
1669#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001670 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001671 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001672#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1673 if( driver != NULL )
1674 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001675 psa_se_key_data_storage_t data;
1676#if defined(static_assert)
1677 static_assert( sizeof( slot->data.se.slot_number ) ==
1678 sizeof( data.slot_number ),
1679 "Slot number size does not match psa_se_key_data_storage_t" );
1680 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1681 "Bit-size size does not match psa_se_key_data_storage_t" );
1682#endif
1683 memcpy( &data.slot_number, &slot->data.se.slot_number,
1684 sizeof( slot->data.se.slot_number ) );
1685 memcpy( &data.bits, &slot->attr.bits,
1686 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001687 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001688 (uint8_t*) &data,
1689 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001690 }
1691 else
1692#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1693 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001694 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001695 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001696 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001697 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1698 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001699 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001700 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1701 status = psa_internal_export_key( slot,
1702 buffer, buffer_size, &length,
1703 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001704 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001705 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001706 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001707
Gilles Peskinef9168942019-09-12 19:20:29 +02001708 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001709 mbedtls_free( buffer );
1710 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001711 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001712#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1713
Gilles Peskinecbaff462019-07-12 23:46:04 +02001714#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001715 /* Finish the transaction for a key creation. This does not
1716 * happen when registering an existing key. Detect this case
1717 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001718 * creation of a persistent key in a secure element requires a transaction,
1719 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001720 if( driver != NULL &&
1721 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001722 {
1723 status = psa_save_se_persistent_data( driver );
1724 if( status != PSA_SUCCESS )
1725 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001726 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001727 return( status );
1728 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001729 status = psa_crypto_stop_transaction( );
1730 if( status != PSA_SUCCESS )
1731 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001732 }
1733#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1734
Gilles Peskine4747d192019-04-17 15:05:45 +02001735 return( status );
1736}
1737
1738/** Abort the creation of a key.
1739 *
1740 * You may call this function after calling psa_start_key_creation(),
1741 * or after psa_finish_key_creation() fails. In other circumstances, this
1742 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001743 * See the documentation of psa_start_key_creation() for the intended use
1744 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001745 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001746 * \param[in,out] slot Pointer to the slot with key material.
1747 * \param[in] driver The secure element driver for the key,
1748 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001749 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001750static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001751 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001752{
Gilles Peskine011e4282019-06-26 18:34:38 +02001753 (void) driver;
1754
Gilles Peskine4747d192019-04-17 15:05:45 +02001755 if( slot == NULL )
1756 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001757
1758#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001759 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001760 * element, and the failure happened later (when saving metadata
1761 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001762 * element.
1763 * https://github.com/ARMmbed/mbed-crypto/issues/217
1764 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001765
Gilles Peskined7729582019-08-05 15:55:54 +02001766 /* Abort the ongoing transaction if any (there may not be one if
1767 * the creation process failed before starting one, or if the
1768 * key creation is a registration of a key in a secure element).
1769 * Earlier functions must already have done what it takes to undo any
1770 * partial creation. All that's left is to update the transaction data
1771 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001772 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001773#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1774
Gilles Peskine4747d192019-04-17 15:05:45 +02001775 psa_wipe_key_slot( slot );
1776}
1777
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001778/** Validate optional attributes during key creation.
1779 *
1780 * Some key attributes are optional during key creation. If they are
1781 * specified in the attributes structure, check that they are consistent
1782 * with the data in the slot.
1783 *
1784 * This function should be called near the end of key creation, after
1785 * the slot in memory is fully populated but before saving persistent data.
1786 */
1787static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001788 const psa_key_slot_t *slot,
1789 const psa_key_attributes_t *attributes )
1790{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001791 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001792 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001793 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001794 return( PSA_ERROR_INVALID_ARGUMENT );
1795 }
1796
1797 if( attributes->domain_parameters_size != 0 )
1798 {
1799#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001800 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001801 {
1802 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001803 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001804 mbedtls_mpi_init( &actual );
1805 mbedtls_mpi_init( &required );
1806 ret = mbedtls_rsa_export( slot->data.rsa,
1807 NULL, NULL, NULL, NULL, &actual );
1808 if( ret != 0 )
1809 goto rsa_exit;
1810 ret = mbedtls_mpi_read_binary( &required,
1811 attributes->domain_parameters,
1812 attributes->domain_parameters_size );
1813 if( ret != 0 )
1814 goto rsa_exit;
1815 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1816 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1817 rsa_exit:
1818 mbedtls_mpi_free( &actual );
1819 mbedtls_mpi_free( &required );
1820 if( ret != 0)
1821 return( mbedtls_to_psa_error( ret ) );
1822 }
1823 else
1824#endif
1825 {
1826 return( PSA_ERROR_INVALID_ARGUMENT );
1827 }
1828 }
1829
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001830 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001831 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001832 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001833 return( PSA_ERROR_INVALID_ARGUMENT );
1834 }
1835
1836 return( PSA_SUCCESS );
1837}
1838
Gilles Peskine4747d192019-04-17 15:05:45 +02001839psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001840 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001841 size_t data_length,
1842 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001843{
1844 psa_status_t status;
1845 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001846 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001847
Gilles Peskine0f84d622019-09-12 19:03:13 +02001848 /* Reject zero-length symmetric keys (including raw data key objects).
1849 * This also rejects any key which might be encoded as an empty string,
1850 * which is never valid. */
1851 if( data_length == 0 )
1852 return( PSA_ERROR_INVALID_ARGUMENT );
1853
Gilles Peskinedf179142019-07-15 22:02:14 +02001854 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1855 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001856 if( status != PSA_SUCCESS )
1857 goto exit;
1858
Gilles Peskine5d309672019-07-12 23:47:28 +02001859#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1860 if( driver != NULL )
1861 {
1862 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001863 /* The driver should set the number of key bits, however in
1864 * case it doesn't, we initialize bits to an invalid value. */
1865 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001866 if( drv->key_management == NULL ||
1867 drv->key_management->p_import == NULL )
1868 {
1869 status = PSA_ERROR_NOT_SUPPORTED;
1870 goto exit;
1871 }
1872 status = drv->key_management->p_import(
1873 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001874 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001875 &bits );
1876 if( status != PSA_SUCCESS )
1877 goto exit;
1878 if( bits > PSA_MAX_KEY_BITS )
1879 {
1880 status = PSA_ERROR_NOT_SUPPORTED;
1881 goto exit;
1882 }
1883 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001884 }
1885 else
1886#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1887 {
1888 status = psa_import_key_into_slot( slot, data, data_length );
1889 if( status != PSA_SUCCESS )
1890 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001891 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001892 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001893 if( status != PSA_SUCCESS )
1894 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001895
Gilles Peskine011e4282019-06-26 18:34:38 +02001896 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001897exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001898 if( status != PSA_SUCCESS )
1899 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001900 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001901 *handle = 0;
1902 }
1903 return( status );
1904}
1905
Gilles Peskined7729582019-08-05 15:55:54 +02001906#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1907psa_status_t mbedtls_psa_register_se_key(
1908 const psa_key_attributes_t *attributes )
1909{
1910 psa_status_t status;
1911 psa_key_slot_t *slot = NULL;
1912 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001913 psa_key_handle_t handle = 0;
1914
1915 /* Leaving attributes unspecified is not currently supported.
1916 * It could make sense to query the key type and size from the
1917 * secure element, but not all secure elements support this
1918 * and the driver HAL doesn't currently support it. */
1919 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1920 return( PSA_ERROR_NOT_SUPPORTED );
1921 if( psa_get_key_bits( attributes ) == 0 )
1922 return( PSA_ERROR_NOT_SUPPORTED );
1923
1924 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1925 &handle, &slot, &driver );
1926 if( status != PSA_SUCCESS )
1927 goto exit;
1928
Gilles Peskined7729582019-08-05 15:55:54 +02001929 status = psa_finish_key_creation( slot, driver );
1930
1931exit:
1932 if( status != PSA_SUCCESS )
1933 {
1934 psa_fail_key_creation( slot, driver );
1935 }
1936 /* Registration doesn't keep the key in RAM. */
1937 psa_close_key( handle );
1938 return( status );
1939}
1940#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1941
Gilles Peskinef603c712019-01-19 13:40:11 +01001942static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001943 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001944{
1945 psa_status_t status;
1946 uint8_t *buffer = NULL;
1947 size_t buffer_size = 0;
1948 size_t length;
1949
Gilles Peskine8e338702019-07-30 20:06:31 +02001950 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001951 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001952 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001953 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001954 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001955 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1956 if( status != PSA_SUCCESS )
1957 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001958 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001959 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001960
1961exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001962 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001963 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001964 return( status );
1965}
1966
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001967psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1968 const psa_key_attributes_t *specified_attributes,
1969 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001970{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001971 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001972 psa_key_slot_t *source_slot = NULL;
1973 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001974 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001975 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001976
Gilles Peskine28f8f302019-07-24 13:30:31 +02001977 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001978 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001979 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001980 goto exit;
1981
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001982 status = psa_validate_optional_attributes( source_slot,
1983 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001984 if( status != PSA_SUCCESS )
1985 goto exit;
1986
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001987 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001988 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001989 if( status != PSA_SUCCESS )
1990 goto exit;
1991
Gilles Peskinedf179142019-07-15 22:02:14 +02001992 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1993 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001994 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001995 if( status != PSA_SUCCESS )
1996 goto exit;
1997
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001998#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1999 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002000 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002001 /* Copying to a secure element is not implemented yet. */
2002 status = PSA_ERROR_NOT_SUPPORTED;
2003 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002004 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002005#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002006
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002007 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002008 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002009 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002010
Gilles Peskine011e4282019-06-26 18:34:38 +02002011 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002012exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002013 if( status != PSA_SUCCESS )
2014 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002015 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002016 *target_handle = 0;
2017 }
2018 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002019}
2020
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002021
2022
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002023/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002024/* Message digests */
2025/****************************************************************/
2026
Gilles Peskineb16841e2019-10-10 20:36:12 +02002027#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002028static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002029{
2030 switch( alg )
2031 {
2032#if defined(MBEDTLS_MD2_C)
2033 case PSA_ALG_MD2:
2034 return( &mbedtls_md2_info );
2035#endif
2036#if defined(MBEDTLS_MD4_C)
2037 case PSA_ALG_MD4:
2038 return( &mbedtls_md4_info );
2039#endif
2040#if defined(MBEDTLS_MD5_C)
2041 case PSA_ALG_MD5:
2042 return( &mbedtls_md5_info );
2043#endif
2044#if defined(MBEDTLS_RIPEMD160_C)
2045 case PSA_ALG_RIPEMD160:
2046 return( &mbedtls_ripemd160_info );
2047#endif
2048#if defined(MBEDTLS_SHA1_C)
2049 case PSA_ALG_SHA_1:
2050 return( &mbedtls_sha1_info );
2051#endif
2052#if defined(MBEDTLS_SHA256_C)
2053 case PSA_ALG_SHA_224:
2054 return( &mbedtls_sha224_info );
2055 case PSA_ALG_SHA_256:
2056 return( &mbedtls_sha256_info );
2057#endif
2058#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002059#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002060 case PSA_ALG_SHA_384:
2061 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002062#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002063 case PSA_ALG_SHA_512:
2064 return( &mbedtls_sha512_info );
2065#endif
2066 default:
2067 return( NULL );
2068 }
2069}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002070#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002071
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002072psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2073{
2074 switch( operation->alg )
2075 {
Gilles Peskine81736312018-06-26 15:04:31 +02002076 case 0:
2077 /* The object has (apparently) been initialized but it is not
2078 * in use. It's ok to call abort on such an object, and there's
2079 * nothing to do. */
2080 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002081#if defined(MBEDTLS_MD2_C)
2082 case PSA_ALG_MD2:
2083 mbedtls_md2_free( &operation->ctx.md2 );
2084 break;
2085#endif
2086#if defined(MBEDTLS_MD4_C)
2087 case PSA_ALG_MD4:
2088 mbedtls_md4_free( &operation->ctx.md4 );
2089 break;
2090#endif
2091#if defined(MBEDTLS_MD5_C)
2092 case PSA_ALG_MD5:
2093 mbedtls_md5_free( &operation->ctx.md5 );
2094 break;
2095#endif
2096#if defined(MBEDTLS_RIPEMD160_C)
2097 case PSA_ALG_RIPEMD160:
2098 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2099 break;
2100#endif
2101#if defined(MBEDTLS_SHA1_C)
2102 case PSA_ALG_SHA_1:
2103 mbedtls_sha1_free( &operation->ctx.sha1 );
2104 break;
2105#endif
2106#if defined(MBEDTLS_SHA256_C)
2107 case PSA_ALG_SHA_224:
2108 case PSA_ALG_SHA_256:
2109 mbedtls_sha256_free( &operation->ctx.sha256 );
2110 break;
2111#endif
2112#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002113#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002114 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002115#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002116 case PSA_ALG_SHA_512:
2117 mbedtls_sha512_free( &operation->ctx.sha512 );
2118 break;
2119#endif
2120 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002121 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002122 }
2123 operation->alg = 0;
2124 return( PSA_SUCCESS );
2125}
2126
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002127psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002128 psa_algorithm_t alg )
2129{
Janos Follath24eed8d2019-11-22 13:21:35 +00002130 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002131
2132 /* A context must be freshly initialized before it can be set up. */
2133 if( operation->alg != 0 )
2134 {
2135 return( PSA_ERROR_BAD_STATE );
2136 }
2137
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002138 switch( alg )
2139 {
2140#if defined(MBEDTLS_MD2_C)
2141 case PSA_ALG_MD2:
2142 mbedtls_md2_init( &operation->ctx.md2 );
2143 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2144 break;
2145#endif
2146#if defined(MBEDTLS_MD4_C)
2147 case PSA_ALG_MD4:
2148 mbedtls_md4_init( &operation->ctx.md4 );
2149 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2150 break;
2151#endif
2152#if defined(MBEDTLS_MD5_C)
2153 case PSA_ALG_MD5:
2154 mbedtls_md5_init( &operation->ctx.md5 );
2155 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2156 break;
2157#endif
2158#if defined(MBEDTLS_RIPEMD160_C)
2159 case PSA_ALG_RIPEMD160:
2160 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2161 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2162 break;
2163#endif
2164#if defined(MBEDTLS_SHA1_C)
2165 case PSA_ALG_SHA_1:
2166 mbedtls_sha1_init( &operation->ctx.sha1 );
2167 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2168 break;
2169#endif
2170#if defined(MBEDTLS_SHA256_C)
2171 case PSA_ALG_SHA_224:
2172 mbedtls_sha256_init( &operation->ctx.sha256 );
2173 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2174 break;
2175 case PSA_ALG_SHA_256:
2176 mbedtls_sha256_init( &operation->ctx.sha256 );
2177 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2178 break;
2179#endif
2180#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002181#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002182 case PSA_ALG_SHA_384:
2183 mbedtls_sha512_init( &operation->ctx.sha512 );
2184 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2185 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002186#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002187 case PSA_ALG_SHA_512:
2188 mbedtls_sha512_init( &operation->ctx.sha512 );
2189 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2190 break;
2191#endif
2192 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002193 return( PSA_ALG_IS_HASH( alg ) ?
2194 PSA_ERROR_NOT_SUPPORTED :
2195 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002196 }
2197 if( ret == 0 )
2198 operation->alg = alg;
2199 else
2200 psa_hash_abort( operation );
2201 return( mbedtls_to_psa_error( ret ) );
2202}
2203
2204psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2205 const uint8_t *input,
2206 size_t input_length )
2207{
Janos Follath24eed8d2019-11-22 13:21:35 +00002208 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002209
2210 /* Don't require hash implementations to behave correctly on a
2211 * zero-length input, which may have an invalid pointer. */
2212 if( input_length == 0 )
2213 return( PSA_SUCCESS );
2214
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002215 switch( operation->alg )
2216 {
2217#if defined(MBEDTLS_MD2_C)
2218 case PSA_ALG_MD2:
2219 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2220 input, input_length );
2221 break;
2222#endif
2223#if defined(MBEDTLS_MD4_C)
2224 case PSA_ALG_MD4:
2225 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2226 input, input_length );
2227 break;
2228#endif
2229#if defined(MBEDTLS_MD5_C)
2230 case PSA_ALG_MD5:
2231 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2232 input, input_length );
2233 break;
2234#endif
2235#if defined(MBEDTLS_RIPEMD160_C)
2236 case PSA_ALG_RIPEMD160:
2237 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2238 input, input_length );
2239 break;
2240#endif
2241#if defined(MBEDTLS_SHA1_C)
2242 case PSA_ALG_SHA_1:
2243 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2244 input, input_length );
2245 break;
2246#endif
2247#if defined(MBEDTLS_SHA256_C)
2248 case PSA_ALG_SHA_224:
2249 case PSA_ALG_SHA_256:
2250 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2251 input, input_length );
2252 break;
2253#endif
2254#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002255#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002256 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002257#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002258 case PSA_ALG_SHA_512:
2259 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2260 input, input_length );
2261 break;
2262#endif
2263 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002264 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002265 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002266
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002267 if( ret != 0 )
2268 psa_hash_abort( operation );
2269 return( mbedtls_to_psa_error( ret ) );
2270}
2271
2272psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2273 uint8_t *hash,
2274 size_t hash_size,
2275 size_t *hash_length )
2276{
itayzafrir40835d42018-08-02 13:14:17 +03002277 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002278 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002279 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002280
2281 /* Fill the output buffer with something that isn't a valid hash
2282 * (barring an attack on the hash and deliberately-crafted input),
2283 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002284 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002285 /* If hash_size is 0 then hash may be NULL and then the
2286 * call to memset would have undefined behavior. */
2287 if( hash_size != 0 )
2288 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002289
2290 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002291 {
2292 status = PSA_ERROR_BUFFER_TOO_SMALL;
2293 goto exit;
2294 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002295
2296 switch( operation->alg )
2297 {
2298#if defined(MBEDTLS_MD2_C)
2299 case PSA_ALG_MD2:
2300 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2301 break;
2302#endif
2303#if defined(MBEDTLS_MD4_C)
2304 case PSA_ALG_MD4:
2305 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2306 break;
2307#endif
2308#if defined(MBEDTLS_MD5_C)
2309 case PSA_ALG_MD5:
2310 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2311 break;
2312#endif
2313#if defined(MBEDTLS_RIPEMD160_C)
2314 case PSA_ALG_RIPEMD160:
2315 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2316 break;
2317#endif
2318#if defined(MBEDTLS_SHA1_C)
2319 case PSA_ALG_SHA_1:
2320 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2321 break;
2322#endif
2323#if defined(MBEDTLS_SHA256_C)
2324 case PSA_ALG_SHA_224:
2325 case PSA_ALG_SHA_256:
2326 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2327 break;
2328#endif
2329#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002330#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002331 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002332#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002333 case PSA_ALG_SHA_512:
2334 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2335 break;
2336#endif
2337 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002338 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002339 }
itayzafrir40835d42018-08-02 13:14:17 +03002340 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002341
itayzafrir40835d42018-08-02 13:14:17 +03002342exit:
2343 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002344 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002345 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002346 return( psa_hash_abort( operation ) );
2347 }
2348 else
2349 {
2350 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002351 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002352 }
2353}
2354
Gilles Peskine2d277862018-06-18 15:41:12 +02002355psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2356 const uint8_t *hash,
2357 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002358{
2359 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2360 size_t actual_hash_length;
2361 psa_status_t status = psa_hash_finish( operation,
2362 actual_hash, sizeof( actual_hash ),
2363 &actual_hash_length );
2364 if( status != PSA_SUCCESS )
2365 return( status );
2366 if( actual_hash_length != hash_length )
2367 return( PSA_ERROR_INVALID_SIGNATURE );
2368 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2369 return( PSA_ERROR_INVALID_SIGNATURE );
2370 return( PSA_SUCCESS );
2371}
2372
Gilles Peskine0a749c82019-11-28 19:33:58 +01002373psa_status_t psa_hash_compute( psa_algorithm_t alg,
2374 const uint8_t *input, size_t input_length,
2375 uint8_t *hash, size_t hash_size,
2376 size_t *hash_length )
2377{
2378 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2379 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2380
2381 *hash_length = hash_size;
2382 status = psa_hash_setup( &operation, alg );
2383 if( status != PSA_SUCCESS )
2384 goto exit;
2385 status = psa_hash_update( &operation, input, input_length );
2386 if( status != PSA_SUCCESS )
2387 goto exit;
2388 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2389 if( status != PSA_SUCCESS )
2390 goto exit;
2391
2392exit:
2393 if( status == PSA_SUCCESS )
2394 status = psa_hash_abort( &operation );
2395 else
2396 psa_hash_abort( &operation );
2397 return( status );
2398}
2399
2400psa_status_t psa_hash_compare( psa_algorithm_t alg,
2401 const uint8_t *input, size_t input_length,
2402 const uint8_t *hash, size_t hash_length )
2403{
2404 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2405 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2406
2407 status = psa_hash_setup( &operation, alg );
2408 if( status != PSA_SUCCESS )
2409 goto exit;
2410 status = psa_hash_update( &operation, input, input_length );
2411 if( status != PSA_SUCCESS )
2412 goto exit;
2413 status = psa_hash_verify( &operation, hash, hash_length );
2414 if( status != PSA_SUCCESS )
2415 goto exit;
2416
2417exit:
2418 if( status == PSA_SUCCESS )
2419 status = psa_hash_abort( &operation );
2420 else
2421 psa_hash_abort( &operation );
2422 return( status );
2423}
2424
Gilles Peskineeb35d782019-01-22 17:56:16 +01002425psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2426 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002427{
2428 if( target_operation->alg != 0 )
2429 return( PSA_ERROR_BAD_STATE );
2430
2431 switch( source_operation->alg )
2432 {
2433 case 0:
2434 return( PSA_ERROR_BAD_STATE );
2435#if defined(MBEDTLS_MD2_C)
2436 case PSA_ALG_MD2:
2437 mbedtls_md2_clone( &target_operation->ctx.md2,
2438 &source_operation->ctx.md2 );
2439 break;
2440#endif
2441#if defined(MBEDTLS_MD4_C)
2442 case PSA_ALG_MD4:
2443 mbedtls_md4_clone( &target_operation->ctx.md4,
2444 &source_operation->ctx.md4 );
2445 break;
2446#endif
2447#if defined(MBEDTLS_MD5_C)
2448 case PSA_ALG_MD5:
2449 mbedtls_md5_clone( &target_operation->ctx.md5,
2450 &source_operation->ctx.md5 );
2451 break;
2452#endif
2453#if defined(MBEDTLS_RIPEMD160_C)
2454 case PSA_ALG_RIPEMD160:
2455 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2456 &source_operation->ctx.ripemd160 );
2457 break;
2458#endif
2459#if defined(MBEDTLS_SHA1_C)
2460 case PSA_ALG_SHA_1:
2461 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2462 &source_operation->ctx.sha1 );
2463 break;
2464#endif
2465#if defined(MBEDTLS_SHA256_C)
2466 case PSA_ALG_SHA_224:
2467 case PSA_ALG_SHA_256:
2468 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2469 &source_operation->ctx.sha256 );
2470 break;
2471#endif
2472#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002473#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002474 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002475#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002476 case PSA_ALG_SHA_512:
2477 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2478 &source_operation->ctx.sha512 );
2479 break;
2480#endif
2481 default:
2482 return( PSA_ERROR_NOT_SUPPORTED );
2483 }
2484
2485 target_operation->alg = source_operation->alg;
2486 return( PSA_SUCCESS );
2487}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002488
2489
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002490/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002491/* MAC */
2492/****************************************************************/
2493
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002494static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002495 psa_algorithm_t alg,
2496 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002497 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002498 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002499{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002500 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002501 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002502
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002503 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002504 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002505
Gilles Peskine8c9def32018-02-08 10:02:12 +01002506 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2507 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002508 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002509 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002510 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002511 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002512 mode = MBEDTLS_MODE_STREAM;
2513 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002514 case PSA_ALG_CTR:
2515 mode = MBEDTLS_MODE_CTR;
2516 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002517 case PSA_ALG_CFB:
2518 mode = MBEDTLS_MODE_CFB;
2519 break;
2520 case PSA_ALG_OFB:
2521 mode = MBEDTLS_MODE_OFB;
2522 break;
2523 case PSA_ALG_CBC_NO_PADDING:
2524 mode = MBEDTLS_MODE_CBC;
2525 break;
2526 case PSA_ALG_CBC_PKCS7:
2527 mode = MBEDTLS_MODE_CBC;
2528 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002529 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002530 mode = MBEDTLS_MODE_CCM;
2531 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002532 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002533 mode = MBEDTLS_MODE_GCM;
2534 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002535 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2536 mode = MBEDTLS_MODE_CHACHAPOLY;
2537 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002538 default:
2539 return( NULL );
2540 }
2541 }
2542 else if( alg == PSA_ALG_CMAC )
2543 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002544 else
2545 return( NULL );
2546
2547 switch( key_type )
2548 {
2549 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002550 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002551 break;
2552 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002553 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2554 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002555 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002556 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002557 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002558 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002559 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2560 * but two-key Triple-DES is functionally three-key Triple-DES
2561 * with K1=K3, so that's how we present it to mbedtls. */
2562 if( key_bits == 128 )
2563 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002564 break;
2565 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002566 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002567 break;
2568 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002569 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002570 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002571 case PSA_KEY_TYPE_CHACHA20:
2572 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2573 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002574 default:
2575 return( NULL );
2576 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002577 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002578 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002579
Jaeden Amero23bbb752018-06-26 14:16:54 +01002580 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2581 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002582}
2583
Gilles Peskinea05219c2018-11-16 16:02:56 +01002584#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002585static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002586{
Gilles Peskine2d277862018-06-18 15:41:12 +02002587 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002588 {
2589 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002590 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002591 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002592 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002593 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002594 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002595 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002596 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002597 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002598 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002599 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002600 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002601 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002602 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002603 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002604 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002605 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002606 return( 128 );
2607 default:
2608 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002609 }
2610}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002611#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002612
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002613/* Initialize the MAC operation structure. Once this function has been
2614 * called, psa_mac_abort can run and will do the right thing. */
2615static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2616 psa_algorithm_t alg )
2617{
2618 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2619
2620 operation->alg = alg;
2621 operation->key_set = 0;
2622 operation->iv_set = 0;
2623 operation->iv_required = 0;
2624 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002625 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002626
2627#if defined(MBEDTLS_CMAC_C)
2628 if( alg == PSA_ALG_CMAC )
2629 {
2630 operation->iv_required = 0;
2631 mbedtls_cipher_init( &operation->ctx.cmac );
2632 status = PSA_SUCCESS;
2633 }
2634 else
2635#endif /* MBEDTLS_CMAC_C */
2636#if defined(MBEDTLS_MD_C)
2637 if( PSA_ALG_IS_HMAC( operation->alg ) )
2638 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002639 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2640 operation->ctx.hmac.hash_ctx.alg = 0;
2641 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002642 }
2643 else
2644#endif /* MBEDTLS_MD_C */
2645 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002646 if( ! PSA_ALG_IS_MAC( alg ) )
2647 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002648 }
2649
2650 if( status != PSA_SUCCESS )
2651 memset( operation, 0, sizeof( *operation ) );
2652 return( status );
2653}
2654
Gilles Peskine01126fa2018-07-12 17:04:55 +02002655#if defined(MBEDTLS_MD_C)
2656static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2657{
Gilles Peskine3f108122018-12-07 18:14:53 +01002658 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002659 return( psa_hash_abort( &hmac->hash_ctx ) );
2660}
2661#endif /* MBEDTLS_MD_C */
2662
Gilles Peskine8c9def32018-02-08 10:02:12 +01002663psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2664{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002665 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002666 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002667 /* The object has (apparently) been initialized but it is not
2668 * in use. It's ok to call abort on such an object, and there's
2669 * nothing to do. */
2670 return( PSA_SUCCESS );
2671 }
2672 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002673#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002674 if( operation->alg == PSA_ALG_CMAC )
2675 {
2676 mbedtls_cipher_free( &operation->ctx.cmac );
2677 }
2678 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002679#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002680#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002681 if( PSA_ALG_IS_HMAC( operation->alg ) )
2682 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002683 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002684 }
2685 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002686#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002687 {
2688 /* Sanity check (shouldn't happen: operation->alg should
2689 * always have been initialized to a valid value). */
2690 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002691 }
Moran Peker41deec42018-04-04 15:43:05 +03002692
Gilles Peskine8c9def32018-02-08 10:02:12 +01002693 operation->alg = 0;
2694 operation->key_set = 0;
2695 operation->iv_set = 0;
2696 operation->iv_required = 0;
2697 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002698 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002699
Gilles Peskine8c9def32018-02-08 10:02:12 +01002700 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002701
2702bad_state:
2703 /* If abort is called on an uninitialized object, we can't trust
2704 * anything. Wipe the object in case it contains confidential data.
2705 * This may result in a memory leak if a pointer gets overwritten,
2706 * but it's too late to do anything about this. */
2707 memset( operation, 0, sizeof( *operation ) );
2708 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002709}
2710
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002711#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002712static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002713 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002714 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002715 const mbedtls_cipher_info_t *cipher_info )
2716{
Janos Follath24eed8d2019-11-22 13:21:35 +00002717 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002718
2719 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002720
2721 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2722 if( ret != 0 )
2723 return( ret );
2724
2725 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2726 slot->data.raw.data,
2727 key_bits );
2728 return( ret );
2729}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002730#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002731
Gilles Peskine248051a2018-06-20 16:09:38 +02002732#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002733static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2734 const uint8_t *key,
2735 size_t key_length,
2736 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002737{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002738 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002739 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002740 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002741 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002742 psa_status_t status;
2743
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002744 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2745 * overflow below. This should never trigger if the hash algorithm
2746 * is implemented correctly. */
2747 /* The size checks against the ipad and opad buffers cannot be written
2748 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2749 * because that triggers -Wlogical-op on GCC 7.3. */
2750 if( block_size > sizeof( ipad ) )
2751 return( PSA_ERROR_NOT_SUPPORTED );
2752 if( block_size > sizeof( hmac->opad ) )
2753 return( PSA_ERROR_NOT_SUPPORTED );
2754 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002755 return( PSA_ERROR_NOT_SUPPORTED );
2756
Gilles Peskined223b522018-06-11 18:12:58 +02002757 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002758 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002759 status = psa_hash_compute( hash_alg, key, key_length,
2760 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002761 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002762 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002763 }
Gilles Peskine96889972018-07-12 17:07:03 +02002764 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2765 * but it is permitted. It is common when HMAC is used in HKDF, for
2766 * example. Don't call `memcpy` in the 0-length because `key` could be
2767 * an invalid pointer which would make the behavior undefined. */
2768 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002769 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002770
Gilles Peskined223b522018-06-11 18:12:58 +02002771 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2772 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002773 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002774 ipad[i] ^= 0x36;
2775 memset( ipad + key_length, 0x36, block_size - key_length );
2776
2777 /* Copy the key material from ipad to opad, flipping the requisite bits,
2778 * and filling the rest of opad with the requisite constant. */
2779 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002780 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2781 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002782
Gilles Peskine01126fa2018-07-12 17:04:55 +02002783 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002784 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002785 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002786
Gilles Peskine01126fa2018-07-12 17:04:55 +02002787 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002788
2789cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002790 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002791
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002792 return( status );
2793}
Gilles Peskine248051a2018-06-20 16:09:38 +02002794#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002795
Gilles Peskine89167cb2018-07-08 20:12:23 +02002796static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002797 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002798 psa_algorithm_t alg,
2799 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002800{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002801 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002802 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002803 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002804 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002805 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002806 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002807 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002808
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002809 /* A context must be freshly initialized before it can be set up. */
2810 if( operation->alg != 0 )
2811 {
2812 return( PSA_ERROR_BAD_STATE );
2813 }
2814
Gilles Peskined911eb72018-08-14 15:18:45 +02002815 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002816 if( status != PSA_SUCCESS )
2817 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002818 if( is_sign )
2819 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002820
Gilles Peskine28f8f302019-07-24 13:30:31 +02002821 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002822 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002823 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002824 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002825
Gilles Peskine8c9def32018-02-08 10:02:12 +01002826#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002827 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002828 {
2829 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002830 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002831 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002832 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002833 if( cipher_info == NULL )
2834 {
2835 status = PSA_ERROR_NOT_SUPPORTED;
2836 goto exit;
2837 }
2838 operation->mac_size = cipher_info->block_size;
2839 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2840 status = mbedtls_to_psa_error( ret );
2841 }
2842 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002843#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002844#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002845 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002846 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002847 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002848 if( hash_alg == 0 )
2849 {
2850 status = PSA_ERROR_NOT_SUPPORTED;
2851 goto exit;
2852 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002853
2854 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2855 /* Sanity check. This shouldn't fail on a valid configuration. */
2856 if( operation->mac_size == 0 ||
2857 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2858 {
2859 status = PSA_ERROR_NOT_SUPPORTED;
2860 goto exit;
2861 }
2862
Gilles Peskine8e338702019-07-30 20:06:31 +02002863 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002864 {
2865 status = PSA_ERROR_INVALID_ARGUMENT;
2866 goto exit;
2867 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002868
Gilles Peskine01126fa2018-07-12 17:04:55 +02002869 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2870 slot->data.raw.data,
2871 slot->data.raw.bytes,
2872 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002873 }
2874 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002875#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002876 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002877 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002878 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002879 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002880
Gilles Peskined911eb72018-08-14 15:18:45 +02002881 if( truncated == 0 )
2882 {
2883 /* The "normal" case: untruncated algorithm. Nothing to do. */
2884 }
2885 else if( truncated < 4 )
2886 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002887 /* A very short MAC is too short for security since it can be
2888 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2889 * so we make this our minimum, even though 32 bits is still
2890 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002891 status = PSA_ERROR_NOT_SUPPORTED;
2892 }
2893 else if( truncated > operation->mac_size )
2894 {
2895 /* It's impossible to "truncate" to a larger length. */
2896 status = PSA_ERROR_INVALID_ARGUMENT;
2897 }
2898 else
2899 operation->mac_size = truncated;
2900
Gilles Peskinefbfac682018-07-08 20:51:54 +02002901exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002902 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002903 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002904 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002905 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002906 else
2907 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002908 operation->key_set = 1;
2909 }
2910 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002911}
2912
Gilles Peskine89167cb2018-07-08 20:12:23 +02002913psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002914 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002915 psa_algorithm_t alg )
2916{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002917 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002918}
2919
2920psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002921 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002922 psa_algorithm_t alg )
2923{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002924 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002925}
2926
Gilles Peskine8c9def32018-02-08 10:02:12 +01002927psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2928 const uint8_t *input,
2929 size_t input_length )
2930{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002931 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002932 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002933 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002934 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002935 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002936 operation->has_input = 1;
2937
Gilles Peskine8c9def32018-02-08 10:02:12 +01002938#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002939 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002940 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002941 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2942 input, input_length );
2943 status = mbedtls_to_psa_error( ret );
2944 }
2945 else
2946#endif /* MBEDTLS_CMAC_C */
2947#if defined(MBEDTLS_MD_C)
2948 if( PSA_ALG_IS_HMAC( operation->alg ) )
2949 {
2950 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2951 input_length );
2952 }
2953 else
2954#endif /* MBEDTLS_MD_C */
2955 {
2956 /* This shouldn't happen if `operation` was initialized by
2957 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002958 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002959 }
2960
Gilles Peskinefbfac682018-07-08 20:51:54 +02002961 if( status != PSA_SUCCESS )
2962 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002963 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002964}
2965
Gilles Peskine01126fa2018-07-12 17:04:55 +02002966#if defined(MBEDTLS_MD_C)
2967static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2968 uint8_t *mac,
2969 size_t mac_size )
2970{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002971 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002972 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2973 size_t hash_size = 0;
2974 size_t block_size = psa_get_hash_block_size( hash_alg );
2975 psa_status_t status;
2976
Gilles Peskine01126fa2018-07-12 17:04:55 +02002977 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2978 if( status != PSA_SUCCESS )
2979 return( status );
2980 /* From here on, tmp needs to be wiped. */
2981
2982 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2983 if( status != PSA_SUCCESS )
2984 goto exit;
2985
2986 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2987 if( status != PSA_SUCCESS )
2988 goto exit;
2989
2990 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2991 if( status != PSA_SUCCESS )
2992 goto exit;
2993
Gilles Peskined911eb72018-08-14 15:18:45 +02002994 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2995 if( status != PSA_SUCCESS )
2996 goto exit;
2997
2998 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002999
3000exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003001 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003002 return( status );
3003}
3004#endif /* MBEDTLS_MD_C */
3005
mohammad16036df908f2018-04-02 08:34:15 -07003006static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003007 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003008 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003009{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003010 if( ! operation->key_set )
3011 return( PSA_ERROR_BAD_STATE );
3012 if( operation->iv_required && ! operation->iv_set )
3013 return( PSA_ERROR_BAD_STATE );
3014
Gilles Peskine8c9def32018-02-08 10:02:12 +01003015 if( mac_size < operation->mac_size )
3016 return( PSA_ERROR_BUFFER_TOO_SMALL );
3017
Gilles Peskine8c9def32018-02-08 10:02:12 +01003018#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003019 if( operation->alg == PSA_ALG_CMAC )
3020 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003021 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3022 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3023 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003024 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003025 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003026 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003027 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003028 else
3029#endif /* MBEDTLS_CMAC_C */
3030#if defined(MBEDTLS_MD_C)
3031 if( PSA_ALG_IS_HMAC( operation->alg ) )
3032 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003033 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003034 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003035 }
3036 else
3037#endif /* MBEDTLS_MD_C */
3038 {
3039 /* This shouldn't happen if `operation` was initialized by
3040 * a setup function. */
3041 return( PSA_ERROR_BAD_STATE );
3042 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003043}
3044
Gilles Peskineacd4be32018-07-08 19:56:25 +02003045psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3046 uint8_t *mac,
3047 size_t mac_size,
3048 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003049{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003050 psa_status_t status;
3051
Jaeden Amero252ef282019-02-15 14:05:35 +00003052 if( operation->alg == 0 )
3053 {
3054 return( PSA_ERROR_BAD_STATE );
3055 }
3056
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003057 /* Fill the output buffer with something that isn't a valid mac
3058 * (barring an attack on the mac and deliberately-crafted input),
3059 * in case the caller doesn't check the return status properly. */
3060 *mac_length = mac_size;
3061 /* If mac_size is 0 then mac may be NULL and then the
3062 * call to memset would have undefined behavior. */
3063 if( mac_size != 0 )
3064 memset( mac, '!', mac_size );
3065
Gilles Peskine89167cb2018-07-08 20:12:23 +02003066 if( ! operation->is_sign )
3067 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003068 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003069 }
mohammad16036df908f2018-04-02 08:34:15 -07003070
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003071 status = psa_mac_finish_internal( operation, mac, mac_size );
3072
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003073 if( status == PSA_SUCCESS )
3074 {
3075 status = psa_mac_abort( operation );
3076 if( status == PSA_SUCCESS )
3077 *mac_length = operation->mac_size;
3078 else
3079 memset( mac, '!', mac_size );
3080 }
3081 else
3082 psa_mac_abort( operation );
3083 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003084}
3085
Gilles Peskineacd4be32018-07-08 19:56:25 +02003086psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3087 const uint8_t *mac,
3088 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003089{
Gilles Peskine828ed142018-06-18 23:25:51 +02003090 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003091 psa_status_t status;
3092
Jaeden Amero252ef282019-02-15 14:05:35 +00003093 if( operation->alg == 0 )
3094 {
3095 return( PSA_ERROR_BAD_STATE );
3096 }
3097
Gilles Peskine89167cb2018-07-08 20:12:23 +02003098 if( operation->is_sign )
3099 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003100 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003101 }
3102 if( operation->mac_size != mac_length )
3103 {
3104 status = PSA_ERROR_INVALID_SIGNATURE;
3105 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003106 }
mohammad16036df908f2018-04-02 08:34:15 -07003107
3108 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003109 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003110 if( status != PSA_SUCCESS )
3111 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003112
3113 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3114 status = PSA_ERROR_INVALID_SIGNATURE;
3115
3116cleanup:
3117 if( status == PSA_SUCCESS )
3118 status = psa_mac_abort( operation );
3119 else
3120 psa_mac_abort( operation );
3121
Gilles Peskine3f108122018-12-07 18:14:53 +01003122 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003123
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003124 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003125}
3126
3127
Gilles Peskine20035e32018-02-03 22:44:14 +01003128
Gilles Peskine20035e32018-02-03 22:44:14 +01003129/****************************************************************/
3130/* Asymmetric cryptography */
3131/****************************************************************/
3132
Gilles Peskine2b450e32018-06-27 15:42:46 +02003133#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003134/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003135 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003136static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3137 size_t hash_length,
3138 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003139{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003140 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003141 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003142 *md_alg = mbedtls_md_get_type( md_info );
3143
3144 /* The Mbed TLS RSA module uses an unsigned int for hash length
3145 * parameters. Validate that it fits so that we don't risk an
3146 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003147#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003148 if( hash_length > UINT_MAX )
3149 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003150#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003151
3152#if defined(MBEDTLS_PKCS1_V15)
3153 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3154 * must be correct. */
3155 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3156 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003157 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003158 if( md_info == NULL )
3159 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003160 if( mbedtls_md_get_size( md_info ) != hash_length )
3161 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003162 }
3163#endif /* MBEDTLS_PKCS1_V15 */
3164
3165#if defined(MBEDTLS_PKCS1_V21)
3166 /* PSS requires a hash internally. */
3167 if( PSA_ALG_IS_RSA_PSS( alg ) )
3168 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003169 if( md_info == NULL )
3170 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003171 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003172#endif /* MBEDTLS_PKCS1_V21 */
3173
Gilles Peskine61b91d42018-06-08 16:09:36 +02003174 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003175}
3176
Gilles Peskine2b450e32018-06-27 15:42:46 +02003177static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3178 psa_algorithm_t alg,
3179 const uint8_t *hash,
3180 size_t hash_length,
3181 uint8_t *signature,
3182 size_t signature_size,
3183 size_t *signature_length )
3184{
3185 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003186 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003187 mbedtls_md_type_t md_alg;
3188
3189 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3190 if( status != PSA_SUCCESS )
3191 return( status );
3192
Gilles Peskine630a18a2018-06-29 17:49:35 +02003193 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003194 return( PSA_ERROR_BUFFER_TOO_SMALL );
3195
3196#if defined(MBEDTLS_PKCS1_V15)
3197 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3198 {
3199 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3200 MBEDTLS_MD_NONE );
3201 ret = mbedtls_rsa_pkcs1_sign( rsa,
3202 mbedtls_ctr_drbg_random,
3203 &global_data.ctr_drbg,
3204 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003205 md_alg,
3206 (unsigned int) hash_length,
3207 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003208 signature );
3209 }
3210 else
3211#endif /* MBEDTLS_PKCS1_V15 */
3212#if defined(MBEDTLS_PKCS1_V21)
3213 if( PSA_ALG_IS_RSA_PSS( alg ) )
3214 {
3215 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3216 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3217 mbedtls_ctr_drbg_random,
3218 &global_data.ctr_drbg,
3219 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003220 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003221 (unsigned int) hash_length,
3222 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003223 signature );
3224 }
3225 else
3226#endif /* MBEDTLS_PKCS1_V21 */
3227 {
3228 return( PSA_ERROR_INVALID_ARGUMENT );
3229 }
3230
3231 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003232 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003233 return( mbedtls_to_psa_error( ret ) );
3234}
3235
3236static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3237 psa_algorithm_t alg,
3238 const uint8_t *hash,
3239 size_t hash_length,
3240 const uint8_t *signature,
3241 size_t signature_length )
3242{
3243 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003244 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003245 mbedtls_md_type_t md_alg;
3246
3247 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3248 if( status != PSA_SUCCESS )
3249 return( status );
3250
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003251 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3252 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003253
3254#if defined(MBEDTLS_PKCS1_V15)
3255 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3256 {
3257 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3258 MBEDTLS_MD_NONE );
3259 ret = mbedtls_rsa_pkcs1_verify( rsa,
3260 mbedtls_ctr_drbg_random,
3261 &global_data.ctr_drbg,
3262 MBEDTLS_RSA_PUBLIC,
3263 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003264 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003265 hash,
3266 signature );
3267 }
3268 else
3269#endif /* MBEDTLS_PKCS1_V15 */
3270#if defined(MBEDTLS_PKCS1_V21)
3271 if( PSA_ALG_IS_RSA_PSS( alg ) )
3272 {
3273 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3274 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3275 mbedtls_ctr_drbg_random,
3276 &global_data.ctr_drbg,
3277 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003278 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003279 (unsigned int) hash_length,
3280 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003281 signature );
3282 }
3283 else
3284#endif /* MBEDTLS_PKCS1_V21 */
3285 {
3286 return( PSA_ERROR_INVALID_ARGUMENT );
3287 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003288
3289 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3290 * the rest of the signature is invalid". This has little use in
3291 * practice and PSA doesn't report this distinction. */
3292 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3293 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003294 return( mbedtls_to_psa_error( ret ) );
3295}
3296#endif /* MBEDTLS_RSA_C */
3297
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003298#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003299/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3300 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3301 * (even though these functions don't modify it). */
3302static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3303 psa_algorithm_t alg,
3304 const uint8_t *hash,
3305 size_t hash_length,
3306 uint8_t *signature,
3307 size_t signature_size,
3308 size_t *signature_length )
3309{
Janos Follath24eed8d2019-11-22 13:21:35 +00003310 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003311 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003312 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003313 mbedtls_mpi_init( &r );
3314 mbedtls_mpi_init( &s );
3315
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003316 if( signature_size < 2 * curve_bytes )
3317 {
3318 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3319 goto cleanup;
3320 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003321
Gilles Peskinea05219c2018-11-16 16:02:56 +01003322#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003323 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3324 {
3325 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3326 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3327 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003328 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3329 &ecp->d, hash,
3330 hash_length, md_alg,
3331 mbedtls_ctr_drbg_random,
3332 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003333 }
3334 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003335#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003336 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003337 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003338 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3339 hash, hash_length,
3340 mbedtls_ctr_drbg_random,
3341 &global_data.ctr_drbg ) );
3342 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003343
3344 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3345 signature,
3346 curve_bytes ) );
3347 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3348 signature + curve_bytes,
3349 curve_bytes ) );
3350
3351cleanup:
3352 mbedtls_mpi_free( &r );
3353 mbedtls_mpi_free( &s );
3354 if( ret == 0 )
3355 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003356 return( mbedtls_to_psa_error( ret ) );
3357}
3358
3359static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3360 const uint8_t *hash,
3361 size_t hash_length,
3362 const uint8_t *signature,
3363 size_t signature_length )
3364{
Janos Follath24eed8d2019-11-22 13:21:35 +00003365 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003366 mbedtls_mpi r, s;
3367 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3368 mbedtls_mpi_init( &r );
3369 mbedtls_mpi_init( &s );
3370
3371 if( signature_length != 2 * curve_bytes )
3372 return( PSA_ERROR_INVALID_SIGNATURE );
3373
3374 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3375 signature,
3376 curve_bytes ) );
3377 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3378 signature + curve_bytes,
3379 curve_bytes ) );
3380
3381 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3382 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003383
3384cleanup:
3385 mbedtls_mpi_free( &r );
3386 mbedtls_mpi_free( &s );
3387 return( mbedtls_to_psa_error( ret ) );
3388}
3389#endif /* MBEDTLS_ECDSA_C */
3390
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003391psa_status_t psa_sign_hash( psa_key_handle_t handle,
3392 psa_algorithm_t alg,
3393 const uint8_t *hash,
3394 size_t hash_length,
3395 uint8_t *signature,
3396 size_t signature_size,
3397 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003398{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003399 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003400 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003401#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3402 const psa_drv_se_t *drv;
3403 psa_drv_se_context_t *drv_context;
3404#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003405
3406 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003407 /* Immediately reject a zero-length signature buffer. This guarantees
3408 * that signature must be a valid pointer. (On the other hand, the hash
3409 * buffer can in principle be empty since it doesn't actually have
3410 * to be a hash.) */
3411 if( signature_size == 0 )
3412 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003413
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003414 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003415 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003416 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003417 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003418 {
3419 status = PSA_ERROR_INVALID_ARGUMENT;
3420 goto exit;
3421 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003422
Gilles Peskineedc64242019-08-07 21:05:07 +02003423#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3424 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3425 {
3426 if( drv->asymmetric == NULL ||
3427 drv->asymmetric->p_sign == NULL )
3428 {
3429 status = PSA_ERROR_NOT_SUPPORTED;
3430 goto exit;
3431 }
3432 status = drv->asymmetric->p_sign( drv_context,
3433 slot->data.se.slot_number,
3434 alg,
3435 hash, hash_length,
3436 signature, signature_size,
3437 signature_length );
3438 }
3439 else
3440#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003441#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003442 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003443 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003444 status = psa_rsa_sign( slot->data.rsa,
3445 alg,
3446 hash, hash_length,
3447 signature, signature_size,
3448 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003449 }
3450 else
3451#endif /* defined(MBEDTLS_RSA_C) */
3452#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003453 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003454 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003455#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003456 if(
3457#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3458 PSA_ALG_IS_ECDSA( alg )
3459#else
3460 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3461#endif
3462 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003463 status = psa_ecdsa_sign( slot->data.ecp,
3464 alg,
3465 hash, hash_length,
3466 signature, signature_size,
3467 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003468 else
3469#endif /* defined(MBEDTLS_ECDSA_C) */
3470 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003471 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003472 }
itayzafrir5c753392018-05-08 11:18:38 +03003473 }
3474 else
3475#endif /* defined(MBEDTLS_ECP_C) */
3476 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003477 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003478 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003479
3480exit:
3481 /* Fill the unused part of the output buffer (the whole buffer on error,
3482 * the trailing part on success) with something that isn't a valid mac
3483 * (barring an attack on the mac and deliberately-crafted input),
3484 * in case the caller doesn't check the return status properly. */
3485 if( status == PSA_SUCCESS )
3486 memset( signature + *signature_length, '!',
3487 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003488 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003489 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003490 /* If signature_size is 0 then we have nothing to do. We must not call
3491 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003492 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003493}
3494
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003495psa_status_t psa_verify_hash( psa_key_handle_t handle,
3496 psa_algorithm_t alg,
3497 const uint8_t *hash,
3498 size_t hash_length,
3499 const uint8_t *signature,
3500 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003501{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003502 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003503 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003504#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3505 const psa_drv_se_t *drv;
3506 psa_drv_se_context_t *drv_context;
3507#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003508
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003509 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003510 if( status != PSA_SUCCESS )
3511 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003512
Gilles Peskineedc64242019-08-07 21:05:07 +02003513#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3514 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3515 {
3516 if( drv->asymmetric == NULL ||
3517 drv->asymmetric->p_verify == NULL )
3518 return( PSA_ERROR_NOT_SUPPORTED );
3519 return( drv->asymmetric->p_verify( drv_context,
3520 slot->data.se.slot_number,
3521 alg,
3522 hash, hash_length,
3523 signature, signature_length ) );
3524 }
3525 else
3526#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003527#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003528 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003529 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003530 return( psa_rsa_verify( slot->data.rsa,
3531 alg,
3532 hash, hash_length,
3533 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003534 }
3535 else
3536#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003537#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003538 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003539 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003540#if defined(MBEDTLS_ECDSA_C)
3541 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003542 return( psa_ecdsa_verify( slot->data.ecp,
3543 hash, hash_length,
3544 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003545 else
3546#endif /* defined(MBEDTLS_ECDSA_C) */
3547 {
3548 return( PSA_ERROR_INVALID_ARGUMENT );
3549 }
itayzafrir5c753392018-05-08 11:18:38 +03003550 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003551 else
3552#endif /* defined(MBEDTLS_ECP_C) */
3553 {
3554 return( PSA_ERROR_NOT_SUPPORTED );
3555 }
3556}
3557
Gilles Peskine072ac562018-06-30 00:21:29 +02003558#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3559static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3560 mbedtls_rsa_context *rsa )
3561{
3562 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3563 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3564 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3565 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3566}
3567#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3568
Gilles Peskinec5487a82018-12-03 18:08:14 +01003569psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003570 psa_algorithm_t alg,
3571 const uint8_t *input,
3572 size_t input_length,
3573 const uint8_t *salt,
3574 size_t salt_length,
3575 uint8_t *output,
3576 size_t output_size,
3577 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003578{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003579 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003580 psa_status_t status;
3581
Darryl Green5cc689a2018-07-24 15:34:10 +01003582 (void) input;
3583 (void) input_length;
3584 (void) salt;
3585 (void) output;
3586 (void) output_size;
3587
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003588 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003589
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003590 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3591 return( PSA_ERROR_INVALID_ARGUMENT );
3592
Gilles Peskine28f8f302019-07-24 13:30:31 +02003593 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003594 if( status != PSA_SUCCESS )
3595 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003596 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3597 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003598 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003599
3600#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003601 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003602 {
3603 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003604 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003605 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003606 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003607#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003608 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003609 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003610 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3611 mbedtls_ctr_drbg_random,
3612 &global_data.ctr_drbg,
3613 MBEDTLS_RSA_PUBLIC,
3614 input_length,
3615 input,
3616 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003617 }
3618 else
3619#endif /* MBEDTLS_PKCS1_V15 */
3620#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003621 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003622 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003623 psa_rsa_oaep_set_padding_mode( alg, rsa );
3624 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3625 mbedtls_ctr_drbg_random,
3626 &global_data.ctr_drbg,
3627 MBEDTLS_RSA_PUBLIC,
3628 salt, salt_length,
3629 input_length,
3630 input,
3631 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003632 }
3633 else
3634#endif /* MBEDTLS_PKCS1_V21 */
3635 {
3636 return( PSA_ERROR_INVALID_ARGUMENT );
3637 }
3638 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003639 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003640 return( mbedtls_to_psa_error( ret ) );
3641 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003642 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003643#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003644 {
3645 return( PSA_ERROR_NOT_SUPPORTED );
3646 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003647}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003648
Gilles Peskinec5487a82018-12-03 18:08:14 +01003649psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003650 psa_algorithm_t alg,
3651 const uint8_t *input,
3652 size_t input_length,
3653 const uint8_t *salt,
3654 size_t salt_length,
3655 uint8_t *output,
3656 size_t output_size,
3657 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003658{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003659 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003660 psa_status_t status;
3661
Darryl Green5cc689a2018-07-24 15:34:10 +01003662 (void) input;
3663 (void) input_length;
3664 (void) salt;
3665 (void) output;
3666 (void) output_size;
3667
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003668 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003669
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003670 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3671 return( PSA_ERROR_INVALID_ARGUMENT );
3672
Gilles Peskine28f8f302019-07-24 13:30:31 +02003673 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003674 if( status != PSA_SUCCESS )
3675 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003676 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003677 return( PSA_ERROR_INVALID_ARGUMENT );
3678
3679#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003680 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003681 {
3682 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003683 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003684
Gilles Peskine630a18a2018-06-29 17:49:35 +02003685 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003686 return( PSA_ERROR_INVALID_ARGUMENT );
3687
3688#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003689 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003690 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003691 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3692 mbedtls_ctr_drbg_random,
3693 &global_data.ctr_drbg,
3694 MBEDTLS_RSA_PRIVATE,
3695 output_length,
3696 input,
3697 output,
3698 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003699 }
3700 else
3701#endif /* MBEDTLS_PKCS1_V15 */
3702#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003703 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003704 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003705 psa_rsa_oaep_set_padding_mode( alg, rsa );
3706 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3707 mbedtls_ctr_drbg_random,
3708 &global_data.ctr_drbg,
3709 MBEDTLS_RSA_PRIVATE,
3710 salt, salt_length,
3711 output_length,
3712 input,
3713 output,
3714 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003715 }
3716 else
3717#endif /* MBEDTLS_PKCS1_V21 */
3718 {
3719 return( PSA_ERROR_INVALID_ARGUMENT );
3720 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003721
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003722 return( mbedtls_to_psa_error( ret ) );
3723 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003724 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003725#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003726 {
3727 return( PSA_ERROR_NOT_SUPPORTED );
3728 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003729}
Gilles Peskine20035e32018-02-03 22:44:14 +01003730
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003731
3732
mohammad1603503973b2018-03-12 15:59:30 +02003733/****************************************************************/
3734/* Symmetric cryptography */
3735/****************************************************************/
3736
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003737/* Initialize the cipher operation structure. Once this function has been
3738 * called, psa_cipher_abort can run and will do the right thing. */
3739static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3740 psa_algorithm_t alg )
3741{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003742 if( ! PSA_ALG_IS_CIPHER( alg ) )
3743 {
3744 memset( operation, 0, sizeof( *operation ) );
3745 return( PSA_ERROR_INVALID_ARGUMENT );
3746 }
3747
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003748 operation->alg = alg;
3749 operation->key_set = 0;
3750 operation->iv_set = 0;
3751 operation->iv_required = 1;
3752 operation->iv_size = 0;
3753 operation->block_size = 0;
3754 mbedtls_cipher_init( &operation->ctx.cipher );
3755 return( PSA_SUCCESS );
3756}
3757
Gilles Peskinee553c652018-06-04 16:22:46 +02003758static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003759 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003760 psa_algorithm_t alg,
3761 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003762{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003763 int ret = 0;
3764 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003765 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003766 size_t key_bits;
3767 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003768 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3769 PSA_KEY_USAGE_ENCRYPT :
3770 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003771
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003772 /* A context must be freshly initialized before it can be set up. */
3773 if( operation->alg != 0 )
3774 {
3775 return( PSA_ERROR_BAD_STATE );
3776 }
3777
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003778 status = psa_cipher_init( operation, alg );
3779 if( status != PSA_SUCCESS )
3780 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003781
Gilles Peskine28f8f302019-07-24 13:30:31 +02003782 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003783 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003784 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003785 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003786
Gilles Peskine8e338702019-07-30 20:06:31 +02003787 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003788 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003789 {
3790 status = PSA_ERROR_NOT_SUPPORTED;
3791 goto exit;
3792 }
mohammad1603503973b2018-03-12 15:59:30 +02003793
mohammad1603503973b2018-03-12 15:59:30 +02003794 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003795 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003796 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003797
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003798#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003799 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003800 {
3801 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003802 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003803 memcpy( keys, slot->data.raw.data, 16 );
3804 memcpy( keys + 16, slot->data.raw.data, 8 );
3805 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3806 keys,
3807 192, cipher_operation );
3808 }
3809 else
3810#endif
3811 {
3812 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3813 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003814 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003815 }
Moran Peker41deec42018-04-04 15:43:05 +03003816 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003817 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003818
mohammad16038481e742018-03-18 13:57:31 +02003819#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003820 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003821 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003822 case PSA_ALG_CBC_NO_PADDING:
3823 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3824 MBEDTLS_PADDING_NONE );
3825 break;
3826 case PSA_ALG_CBC_PKCS7:
3827 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3828 MBEDTLS_PADDING_PKCS7 );
3829 break;
3830 default:
3831 /* The algorithm doesn't involve padding. */
3832 ret = 0;
3833 break;
3834 }
3835 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003836 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003837#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3838
mohammad1603503973b2018-03-12 15:59:30 +02003839 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003840 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003841 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003842 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003843 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003844 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003845 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003846#if defined(MBEDTLS_CHACHA20_C)
3847 else
3848 if( alg == PSA_ALG_CHACHA20 )
3849 operation->iv_size = 12;
3850#endif
mohammad1603503973b2018-03-12 15:59:30 +02003851
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003852exit:
3853 if( status == 0 )
3854 status = mbedtls_to_psa_error( ret );
3855 if( status != 0 )
3856 psa_cipher_abort( operation );
3857 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003858}
3859
Gilles Peskinefe119512018-07-08 21:39:34 +02003860psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003861 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003862 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003863{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003864 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003865}
3866
Gilles Peskinefe119512018-07-08 21:39:34 +02003867psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003868 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003869 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003870{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003871 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003872}
3873
Gilles Peskinefe119512018-07-08 21:39:34 +02003874psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003875 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003876 size_t iv_size,
3877 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003878{
itayzafrir534bd7c2018-08-02 13:56:32 +03003879 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003880 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003881 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003882 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003883 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003884 }
Moran Peker41deec42018-04-04 15:43:05 +03003885 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003886 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003887 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003888 goto exit;
3889 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003890 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3891 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003892 if( ret != 0 )
3893 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003894 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003895 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003896 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003897
mohammad16038481e742018-03-18 13:57:31 +02003898 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003899 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003900
Moran Peker395db872018-05-31 14:07:14 +03003901exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003902 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003903 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003904 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003905}
3906
Gilles Peskinefe119512018-07-08 21:39:34 +02003907psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003908 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003909 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003910{
itayzafrir534bd7c2018-08-02 13:56:32 +03003911 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003912 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003913 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003914 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003915 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003916 }
Moran Pekera28258c2018-05-29 16:25:04 +03003917 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003918 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003919 status = PSA_ERROR_INVALID_ARGUMENT;
3920 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003921 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003922 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3923 status = mbedtls_to_psa_error( ret );
3924exit:
3925 if( status == PSA_SUCCESS )
3926 operation->iv_set = 1;
3927 else
mohammad1603503973b2018-03-12 15:59:30 +02003928 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003929 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003930}
3931
Gilles Peskinee553c652018-06-04 16:22:46 +02003932psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3933 const uint8_t *input,
3934 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003935 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003936 size_t output_size,
3937 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003938{
itayzafrir534bd7c2018-08-02 13:56:32 +03003939 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003940 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003941 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003942
3943 if( operation->alg == 0 )
3944 {
3945 return( PSA_ERROR_BAD_STATE );
3946 }
3947
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003948 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003949 {
3950 /* Take the unprocessed partial block left over from previous
3951 * update calls, if any, plus the input to this call. Remove
3952 * the last partial block, if any. You get the data that will be
3953 * output in this call. */
3954 expected_output_size =
3955 ( operation->ctx.cipher.unprocessed_len + input_length )
3956 / operation->block_size * operation->block_size;
3957 }
3958 else
3959 {
3960 expected_output_size = input_length;
3961 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003962
Gilles Peskine89d789c2018-06-04 17:17:16 +02003963 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003964 {
3965 status = PSA_ERROR_BUFFER_TOO_SMALL;
3966 goto exit;
3967 }
mohammad160382759612018-03-12 18:16:40 +02003968
mohammad1603503973b2018-03-12 15:59:30 +02003969 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003970 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003971 status = mbedtls_to_psa_error( ret );
3972exit:
3973 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003974 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003975 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003976}
3977
Gilles Peskinee553c652018-06-04 16:22:46 +02003978psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3979 uint8_t *output,
3980 size_t output_size,
3981 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003982{
David Saadab4ecc272019-02-14 13:48:10 +02003983 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003984 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003985 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003986
mohammad1603503973b2018-03-12 15:59:30 +02003987 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003988 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003989 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003990 }
3991 if( operation->iv_required && ! operation->iv_set )
3992 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003993 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003994 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003995
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003996 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003997 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3998 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003999 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004000 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004001 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004002 }
4003
Janos Follath315b51c2018-07-09 16:04:51 +01004004 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4005 temp_output_buffer,
4006 output_length );
4007 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004008 {
Janos Follath315b51c2018-07-09 16:04:51 +01004009 status = mbedtls_to_psa_error( cipher_ret );
4010 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004011 }
Janos Follath315b51c2018-07-09 16:04:51 +01004012
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004013 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004014 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004015 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004016 memcpy( output, temp_output_buffer, *output_length );
4017 else
4018 {
Janos Follath315b51c2018-07-09 16:04:51 +01004019 status = PSA_ERROR_BUFFER_TOO_SMALL;
4020 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004021 }
mohammad1603503973b2018-03-12 15:59:30 +02004022
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 status = psa_cipher_abort( operation );
4025
4026 return( status );
4027
4028error:
4029
4030 *output_length = 0;
4031
Gilles Peskine3f108122018-12-07 18:14:53 +01004032 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004033 (void) psa_cipher_abort( operation );
4034
4035 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004036}
4037
Gilles Peskinee553c652018-06-04 16:22:46 +02004038psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4039{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004040 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004041 {
4042 /* The object has (apparently) been initialized but it is not
4043 * in use. It's ok to call abort on such an object, and there's
4044 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004045 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004046 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004047
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004048 /* Sanity check (shouldn't happen: operation->alg should
4049 * always have been initialized to a valid value). */
4050 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4051 return( PSA_ERROR_BAD_STATE );
4052
mohammad1603503973b2018-03-12 15:59:30 +02004053 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004054
Moran Peker41deec42018-04-04 15:43:05 +03004055 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004056 operation->key_set = 0;
4057 operation->iv_set = 0;
4058 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004059 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004060 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004061
Moran Peker395db872018-05-31 14:07:14 +03004062 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004063}
4064
Gilles Peskinea0655c32018-04-30 17:06:50 +02004065
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004066
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004067
mohammad16035955c982018-04-26 00:53:03 +03004068/****************************************************************/
4069/* AEAD */
4070/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004071
Gilles Peskineedf9a652018-08-17 18:11:56 +02004072typedef struct
4073{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004074 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004075 const mbedtls_cipher_info_t *cipher_info;
4076 union
4077 {
4078#if defined(MBEDTLS_CCM_C)
4079 mbedtls_ccm_context ccm;
4080#endif /* MBEDTLS_CCM_C */
4081#if defined(MBEDTLS_GCM_C)
4082 mbedtls_gcm_context gcm;
4083#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004084#if defined(MBEDTLS_CHACHAPOLY_C)
4085 mbedtls_chachapoly_context chachapoly;
4086#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004087 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004088 psa_algorithm_t core_alg;
4089 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004090 uint8_t tag_length;
4091} aead_operation_t;
4092
Gilles Peskine30a9e412019-01-14 18:36:12 +01004093static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004094{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004095 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004096 {
4097#if defined(MBEDTLS_CCM_C)
4098 case PSA_ALG_CCM:
4099 mbedtls_ccm_free( &operation->ctx.ccm );
4100 break;
4101#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004102#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004103 case PSA_ALG_GCM:
4104 mbedtls_gcm_free( &operation->ctx.gcm );
4105 break;
4106#endif /* MBEDTLS_GCM_C */
4107 }
4108}
4109
4110static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004111 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004112 psa_key_usage_t usage,
4113 psa_algorithm_t alg )
4114{
4115 psa_status_t status;
4116 size_t key_bits;
4117 mbedtls_cipher_id_t cipher_id;
4118
Gilles Peskine28f8f302019-07-24 13:30:31 +02004119 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004120 if( status != PSA_SUCCESS )
4121 return( status );
4122
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004123 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004124
4125 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004126 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004127 &cipher_id );
4128 if( operation->cipher_info == NULL )
4129 return( PSA_ERROR_NOT_SUPPORTED );
4130
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004131 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004132 {
4133#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004134 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4135 operation->core_alg = PSA_ALG_CCM;
4136 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004137 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4138 * The call to mbedtls_ccm_encrypt_and_tag or
4139 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004140 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004141 return( PSA_ERROR_INVALID_ARGUMENT );
4142 mbedtls_ccm_init( &operation->ctx.ccm );
4143 status = mbedtls_to_psa_error(
4144 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4145 operation->slot->data.raw.data,
4146 (unsigned int) key_bits ) );
4147 if( status != 0 )
4148 goto cleanup;
4149 break;
4150#endif /* MBEDTLS_CCM_C */
4151
4152#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004153 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4154 operation->core_alg = PSA_ALG_GCM;
4155 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004156 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4157 * The call to mbedtls_gcm_crypt_and_tag or
4158 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004159 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004160 return( PSA_ERROR_INVALID_ARGUMENT );
4161 mbedtls_gcm_init( &operation->ctx.gcm );
4162 status = mbedtls_to_psa_error(
4163 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4164 operation->slot->data.raw.data,
4165 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004166 if( status != 0 )
4167 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004168 break;
4169#endif /* MBEDTLS_GCM_C */
4170
Gilles Peskine26869f22019-05-06 15:25:00 +02004171#if defined(MBEDTLS_CHACHAPOLY_C)
4172 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4173 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4174 operation->full_tag_length = 16;
4175 /* We only support the default tag length. */
4176 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4177 return( PSA_ERROR_NOT_SUPPORTED );
4178 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4179 status = mbedtls_to_psa_error(
4180 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4181 operation->slot->data.raw.data ) );
4182 if( status != 0 )
4183 goto cleanup;
4184 break;
4185#endif /* MBEDTLS_CHACHAPOLY_C */
4186
Gilles Peskineedf9a652018-08-17 18:11:56 +02004187 default:
4188 return( PSA_ERROR_NOT_SUPPORTED );
4189 }
4190
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004191 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4192 {
4193 status = PSA_ERROR_INVALID_ARGUMENT;
4194 goto cleanup;
4195 }
4196 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004197
Gilles Peskineedf9a652018-08-17 18:11:56 +02004198 return( PSA_SUCCESS );
4199
4200cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004201 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004202 return( status );
4203}
4204
Gilles Peskinec5487a82018-12-03 18:08:14 +01004205psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004206 psa_algorithm_t alg,
4207 const uint8_t *nonce,
4208 size_t nonce_length,
4209 const uint8_t *additional_data,
4210 size_t additional_data_length,
4211 const uint8_t *plaintext,
4212 size_t plaintext_length,
4213 uint8_t *ciphertext,
4214 size_t ciphertext_size,
4215 size_t *ciphertext_length )
4216{
mohammad16035955c982018-04-26 00:53:03 +03004217 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004218 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004219 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004220
mohammad1603f08a5502018-06-03 15:05:47 +03004221 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004222
Gilles Peskinec5487a82018-12-03 18:08:14 +01004223 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004224 if( status != PSA_SUCCESS )
4225 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004226
Gilles Peskineedf9a652018-08-17 18:11:56 +02004227 /* For all currently supported modes, the tag is at the end of the
4228 * ciphertext. */
4229 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4230 {
4231 status = PSA_ERROR_BUFFER_TOO_SMALL;
4232 goto exit;
4233 }
4234 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004235
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004236#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004237 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004238 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004239 status = mbedtls_to_psa_error(
4240 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4241 MBEDTLS_GCM_ENCRYPT,
4242 plaintext_length,
4243 nonce, nonce_length,
4244 additional_data, additional_data_length,
4245 plaintext, ciphertext,
4246 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004247 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004248 else
4249#endif /* MBEDTLS_GCM_C */
4250#if defined(MBEDTLS_CCM_C)
4251 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004252 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004253 status = mbedtls_to_psa_error(
4254 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4255 plaintext_length,
4256 nonce, nonce_length,
4257 additional_data,
4258 additional_data_length,
4259 plaintext, ciphertext,
4260 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004261 }
mohammad16035c8845f2018-05-09 05:40:09 -07004262 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004263#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004264#if defined(MBEDTLS_CHACHAPOLY_C)
4265 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4266 {
4267 if( nonce_length != 12 || operation.tag_length != 16 )
4268 {
4269 status = PSA_ERROR_NOT_SUPPORTED;
4270 goto exit;
4271 }
4272 status = mbedtls_to_psa_error(
4273 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4274 plaintext_length,
4275 nonce,
4276 additional_data,
4277 additional_data_length,
4278 plaintext,
4279 ciphertext,
4280 tag ) );
4281 }
4282 else
4283#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004284 {
mohammad1603554faad2018-06-03 15:07:38 +03004285 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004286 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004287
Gilles Peskineedf9a652018-08-17 18:11:56 +02004288 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4289 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004290
Gilles Peskineedf9a652018-08-17 18:11:56 +02004291exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004292 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004293 if( status == PSA_SUCCESS )
4294 *ciphertext_length = plaintext_length + operation.tag_length;
4295 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004296}
4297
Gilles Peskineee652a32018-06-01 19:23:52 +02004298/* Locate the tag in a ciphertext buffer containing the encrypted data
4299 * followed by the tag. Return the length of the part preceding the tag in
4300 * *plaintext_length. This is the size of the plaintext in modes where
4301 * the encrypted data has the same size as the plaintext, such as
4302 * CCM and GCM. */
4303static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4304 const uint8_t *ciphertext,
4305 size_t ciphertext_length,
4306 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004307 const uint8_t **p_tag )
4308{
4309 size_t payload_length;
4310 if( tag_length > ciphertext_length )
4311 return( PSA_ERROR_INVALID_ARGUMENT );
4312 payload_length = ciphertext_length - tag_length;
4313 if( payload_length > plaintext_size )
4314 return( PSA_ERROR_BUFFER_TOO_SMALL );
4315 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004316 return( PSA_SUCCESS );
4317}
4318
Gilles Peskinec5487a82018-12-03 18:08:14 +01004319psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004320 psa_algorithm_t alg,
4321 const uint8_t *nonce,
4322 size_t nonce_length,
4323 const uint8_t *additional_data,
4324 size_t additional_data_length,
4325 const uint8_t *ciphertext,
4326 size_t ciphertext_length,
4327 uint8_t *plaintext,
4328 size_t plaintext_size,
4329 size_t *plaintext_length )
4330{
mohammad16035955c982018-04-26 00:53:03 +03004331 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004332 aead_operation_t operation;
4333 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004334
Gilles Peskineee652a32018-06-01 19:23:52 +02004335 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004336
Gilles Peskinec5487a82018-12-03 18:08:14 +01004337 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004338 if( status != PSA_SUCCESS )
4339 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004340
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004341 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4342 ciphertext, ciphertext_length,
4343 plaintext_size, &tag );
4344 if( status != PSA_SUCCESS )
4345 goto exit;
4346
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004347#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004348 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004349 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004350 status = mbedtls_to_psa_error(
4351 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4352 ciphertext_length - operation.tag_length,
4353 nonce, nonce_length,
4354 additional_data,
4355 additional_data_length,
4356 tag, operation.tag_length,
4357 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004358 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004359 else
4360#endif /* MBEDTLS_GCM_C */
4361#if defined(MBEDTLS_CCM_C)
4362 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004363 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004364 status = mbedtls_to_psa_error(
4365 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4366 ciphertext_length - operation.tag_length,
4367 nonce, nonce_length,
4368 additional_data,
4369 additional_data_length,
4370 ciphertext, plaintext,
4371 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004372 }
mohammad160339574652018-06-01 04:39:53 -07004373 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004374#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004375#if defined(MBEDTLS_CHACHAPOLY_C)
4376 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4377 {
4378 if( nonce_length != 12 || operation.tag_length != 16 )
4379 {
4380 status = PSA_ERROR_NOT_SUPPORTED;
4381 goto exit;
4382 }
4383 status = mbedtls_to_psa_error(
4384 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4385 ciphertext_length - operation.tag_length,
4386 nonce,
4387 additional_data,
4388 additional_data_length,
4389 tag,
4390 ciphertext,
4391 plaintext ) );
4392 }
4393 else
4394#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004395 {
mohammad1603554faad2018-06-03 15:07:38 +03004396 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004397 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004398
Gilles Peskineedf9a652018-08-17 18:11:56 +02004399 if( status != PSA_SUCCESS && plaintext_size != 0 )
4400 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004401
Gilles Peskineedf9a652018-08-17 18:11:56 +02004402exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004403 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004404 if( status == PSA_SUCCESS )
4405 *plaintext_length = ciphertext_length - operation.tag_length;
4406 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004407}
4408
Gilles Peskinea0655c32018-04-30 17:06:50 +02004409
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004410
Gilles Peskine20035e32018-02-03 22:44:14 +01004411/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004412/* Generators */
4413/****************************************************************/
4414
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004415#define HKDF_STATE_INIT 0 /* no input yet */
4416#define HKDF_STATE_STARTED 1 /* got salt */
4417#define HKDF_STATE_KEYED 2 /* got key */
4418#define HKDF_STATE_OUTPUT 3 /* output started */
4419
Gilles Peskinecbe66502019-05-16 16:59:18 +02004420static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004421 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004422{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004423 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4424 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004425 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004426 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004427}
4428
4429
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004430psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004431{
4432 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004433 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004434 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004435 {
4436 /* The object has (apparently) been initialized but it is not
4437 * in use. It's ok to call abort on such an object, and there's
4438 * nothing to do. */
4439 }
4440 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004441#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004442 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004443 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004444 mbedtls_free( operation->ctx.hkdf.info );
4445 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004446 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004447 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004448 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004449 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004450 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004451 if( operation->ctx.tls12_prf.seed != NULL )
4452 {
4453 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4454 operation->ctx.tls12_prf.seed_length );
4455 mbedtls_free( operation->ctx.tls12_prf.seed );
4456 }
4457
4458 if( operation->ctx.tls12_prf.label != NULL )
4459 {
4460 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4461 operation->ctx.tls12_prf.label_length );
4462 mbedtls_free( operation->ctx.tls12_prf.label );
4463 }
4464
4465 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4466
4467 /* We leave the fields Ai and output_block to be erased safely by the
4468 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004469 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004470 else
4471#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004472 {
4473 status = PSA_ERROR_BAD_STATE;
4474 }
Janos Follath083036a2019-06-11 10:22:26 +01004475 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004476 return( status );
4477}
4478
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004479psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004480 size_t *capacity)
4481{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004482 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004483 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004484 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004485 return PSA_ERROR_BAD_STATE;
4486 }
4487
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004488 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004489 return( PSA_SUCCESS );
4490}
4491
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004492psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004493 size_t capacity )
4494{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004495 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004496 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004497 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004498 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004499 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004500 return( PSA_SUCCESS );
4501}
4502
Gilles Peskinebef7f142018-07-12 17:22:21 +02004503#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004504/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004505 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004506static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004507 psa_algorithm_t hash_alg,
4508 uint8_t *output,
4509 size_t output_length )
4510{
4511 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4512 psa_status_t status;
4513
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004514 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4515 return( PSA_ERROR_BAD_STATE );
4516 hkdf->state = HKDF_STATE_OUTPUT;
4517
Gilles Peskinebef7f142018-07-12 17:22:21 +02004518 while( output_length != 0 )
4519 {
4520 /* Copy what remains of the current block */
4521 uint8_t n = hash_length - hkdf->offset_in_block;
4522 if( n > output_length )
4523 n = (uint8_t) output_length;
4524 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4525 output += n;
4526 output_length -= n;
4527 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004528 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004529 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004530 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004531 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004532 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004533 * object was corrupted or if this function is called directly
4534 * inside the library. */
4535 if( hkdf->block_number == 0xff )
4536 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004537
4538 /* We need a new block */
4539 ++hkdf->block_number;
4540 hkdf->offset_in_block = 0;
4541 status = psa_hmac_setup_internal( &hkdf->hmac,
4542 hkdf->prk, hash_length,
4543 hash_alg );
4544 if( status != PSA_SUCCESS )
4545 return( status );
4546 if( hkdf->block_number != 1 )
4547 {
4548 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4549 hkdf->output_block,
4550 hash_length );
4551 if( status != PSA_SUCCESS )
4552 return( status );
4553 }
4554 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4555 hkdf->info,
4556 hkdf->info_length );
4557 if( status != PSA_SUCCESS )
4558 return( status );
4559 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4560 &hkdf->block_number, 1 );
4561 if( status != PSA_SUCCESS )
4562 return( status );
4563 status = psa_hmac_finish_internal( &hkdf->hmac,
4564 hkdf->output_block,
4565 sizeof( hkdf->output_block ) );
4566 if( status != PSA_SUCCESS )
4567 return( status );
4568 }
4569
4570 return( PSA_SUCCESS );
4571}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004572
Janos Follath7742fee2019-06-17 12:58:10 +01004573static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4574 psa_tls12_prf_key_derivation_t *tls12_prf,
4575 psa_algorithm_t alg )
4576{
4577 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4578 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004579 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4580 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004581
Janos Follath7742fee2019-06-17 12:58:10 +01004582 /* We can't be wanting more output after block 0xff, otherwise
4583 * the capacity check in psa_key_derivation_output_bytes() would have
4584 * prevented this call. It could happen only if the operation
4585 * object was corrupted or if this function is called directly
4586 * inside the library. */
4587 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004588 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004589
4590 /* We need a new block */
4591 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004592 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004593
4594 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4595 *
4596 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4597 *
4598 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4599 * HMAC_hash(secret, A(2) + seed) +
4600 * HMAC_hash(secret, A(3) + seed) + ...
4601 *
4602 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004603 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004604 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004605 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004606 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004607 * is currently extracted as `output_block` and where i is
4608 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004609 */
4610
Janos Follathea29bfb2019-06-19 12:21:20 +01004611 /* Save the hash context before using it, to preserve the hash state with
4612 * only the inner padding in it. We need this, because inner padding depends
4613 * on the key (secret in the RFC's terminology). */
4614 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4615 if( status != PSA_SUCCESS )
4616 goto cleanup;
4617
4618 /* Calculate A(i) where i = tls12_prf->block_number. */
4619 if( tls12_prf->block_number == 1 )
4620 {
4621 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4622 * the variable seed and in this instance means it in the context of the
4623 * P_hash function, where seed = label + seed.) */
4624 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4625 tls12_prf->label, tls12_prf->label_length );
4626 if( status != PSA_SUCCESS )
4627 goto cleanup;
4628 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4629 tls12_prf->seed, tls12_prf->seed_length );
4630 if( status != PSA_SUCCESS )
4631 goto cleanup;
4632 }
4633 else
4634 {
4635 /* A(i) = HMAC_hash(secret, A(i-1)) */
4636 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4637 tls12_prf->Ai, hash_length );
4638 if( status != PSA_SUCCESS )
4639 goto cleanup;
4640 }
4641
4642 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4643 tls12_prf->Ai, hash_length );
4644 if( status != PSA_SUCCESS )
4645 goto cleanup;
4646 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4647 if( status != PSA_SUCCESS )
4648 goto cleanup;
4649
4650 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4651 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4652 tls12_prf->Ai, hash_length );
4653 if( status != PSA_SUCCESS )
4654 goto cleanup;
4655 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4656 tls12_prf->label, tls12_prf->label_length );
4657 if( status != PSA_SUCCESS )
4658 goto cleanup;
4659 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4660 tls12_prf->seed, tls12_prf->seed_length );
4661 if( status != PSA_SUCCESS )
4662 goto cleanup;
4663 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4664 tls12_prf->output_block, hash_length );
4665 if( status != PSA_SUCCESS )
4666 goto cleanup;
4667 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4668 if( status != PSA_SUCCESS )
4669 goto cleanup;
4670
Janos Follath7742fee2019-06-17 12:58:10 +01004671
4672cleanup:
4673
Janos Follathea29bfb2019-06-19 12:21:20 +01004674 cleanup_status = psa_hash_abort( &backup );
4675 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4676 status = cleanup_status;
4677
4678 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004679}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004680
Janos Follath844eb0e2019-06-19 12:10:49 +01004681static psa_status_t psa_key_derivation_tls12_prf_read(
4682 psa_tls12_prf_key_derivation_t *tls12_prf,
4683 psa_algorithm_t alg,
4684 uint8_t *output,
4685 size_t output_length )
4686{
4687 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4688 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4689 psa_status_t status;
4690 uint8_t offset, length;
4691
4692 while( output_length != 0 )
4693 {
4694 /* Check if we have fully processed the current block. */
4695 if( tls12_prf->left_in_block == 0 )
4696 {
4697 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4698 alg );
4699 if( status != PSA_SUCCESS )
4700 return( status );
4701
4702 continue;
4703 }
4704
4705 if( tls12_prf->left_in_block > output_length )
4706 length = (uint8_t) output_length;
4707 else
4708 length = tls12_prf->left_in_block;
4709
4710 offset = hash_length - tls12_prf->left_in_block;
4711 memcpy( output, tls12_prf->output_block + offset, length );
4712 output += length;
4713 output_length -= length;
4714 tls12_prf->left_in_block -= length;
4715 }
4716
4717 return( PSA_SUCCESS );
4718}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004719#endif /* MBEDTLS_MD_C */
4720
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004721psa_status_t psa_key_derivation_output_bytes(
4722 psa_key_derivation_operation_t *operation,
4723 uint8_t *output,
4724 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004725{
4726 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004727 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004728
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004729 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004730 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004731 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004732 return PSA_ERROR_BAD_STATE;
4733 }
4734
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004735 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004736 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004737 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004738 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004739 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004740 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004741 goto exit;
4742 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004743 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004744 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004745 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004746 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004747 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4748 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004749 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004750 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004751 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004752 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004753 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004754
Gilles Peskinebef7f142018-07-12 17:22:21 +02004755#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004756 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004757 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004758 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004759 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004760 output, output_length );
4761 }
Janos Follathadbec812019-06-14 11:05:39 +01004762 else
Janos Follathadbec812019-06-14 11:05:39 +01004763 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004764 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004765 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004766 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004767 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004768 output_length );
4769 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004770 else
4771#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004772 {
4773 return( PSA_ERROR_BAD_STATE );
4774 }
4775
4776exit:
4777 if( status != PSA_SUCCESS )
4778 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004779 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004780 * This allows us to differentiate between exhausted operations and
4781 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4782 * operations. */
4783 psa_algorithm_t alg = operation->alg;
4784 psa_key_derivation_abort( operation );
4785 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004786 memset( output, '!', output_length );
4787 }
4788 return( status );
4789}
4790
Gilles Peskine08542d82018-07-19 17:05:42 +02004791#if defined(MBEDTLS_DES_C)
4792static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4793{
4794 if( data_size >= 8 )
4795 mbedtls_des_key_set_parity( data );
4796 if( data_size >= 16 )
4797 mbedtls_des_key_set_parity( data + 8 );
4798 if( data_size >= 24 )
4799 mbedtls_des_key_set_parity( data + 16 );
4800}
4801#endif /* MBEDTLS_DES_C */
4802
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004803static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004804 psa_key_slot_t *slot,
4805 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004806 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004807{
4808 uint8_t *data = NULL;
4809 size_t bytes = PSA_BITS_TO_BYTES( bits );
4810 psa_status_t status;
4811
Gilles Peskine8e338702019-07-30 20:06:31 +02004812 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004813 return( PSA_ERROR_INVALID_ARGUMENT );
4814 if( bits % 8 != 0 )
4815 return( PSA_ERROR_INVALID_ARGUMENT );
4816 data = mbedtls_calloc( 1, bytes );
4817 if( data == NULL )
4818 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4819
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004820 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004821 if( status != PSA_SUCCESS )
4822 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004823#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004824 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004825 psa_des_set_key_parity( data, bytes );
4826#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004827 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004828
4829exit:
4830 mbedtls_free( data );
4831 return( status );
4832}
4833
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004834psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004835 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004836 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004837{
4838 psa_status_t status;
4839 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004840 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004841
4842 /* Reject any attempt to create a zero-length key so that we don't
4843 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4844 if( psa_get_key_bits( attributes ) == 0 )
4845 return( PSA_ERROR_INVALID_ARGUMENT );
4846
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004847 if( ! operation->can_output_key )
4848 return( PSA_ERROR_NOT_PERMITTED );
4849
Gilles Peskinedf179142019-07-15 22:02:14 +02004850 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4851 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004852#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4853 if( driver != NULL )
4854 {
4855 /* Deriving a key in a secure element is not implemented yet. */
4856 status = PSA_ERROR_NOT_SUPPORTED;
4857 }
4858#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004859 if( status == PSA_SUCCESS )
4860 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004861 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004862 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004863 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004864 }
4865 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004866 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004867 if( status != PSA_SUCCESS )
4868 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004869 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004870 *handle = 0;
4871 }
4872 return( status );
4873}
4874
Gilles Peskineeab56e42018-07-12 17:12:33 +02004875
4876
4877/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004878/* Key derivation */
4879/****************************************************************/
4880
Gilles Peskine969c5d62019-01-16 15:53:06 +01004881static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004882 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004883 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004884{
Janos Follath5fe19732019-06-20 15:09:30 +01004885 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4886 * initialisers for this union leave some bytes unspecified.) */
4887 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4888
Gilles Peskine969c5d62019-01-16 15:53:06 +01004889 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004890#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004891 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4892 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4893 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004894 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004895 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004896 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4897 if( hash_size == 0 )
4898 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004899 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4900 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004901 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004902 {
4903 return( PSA_ERROR_NOT_SUPPORTED );
4904 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004905 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004906 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004907 }
4908#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004909 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004910 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004911}
4912
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004913psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004914 psa_algorithm_t alg )
4915{
4916 psa_status_t status;
4917
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004918 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004919 return( PSA_ERROR_BAD_STATE );
4920
Gilles Peskine6843c292019-01-18 16:44:49 +01004921 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4922 return( PSA_ERROR_INVALID_ARGUMENT );
4923 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004924 {
4925 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004926 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004927 }
4928 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4929 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004930 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004931 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004932 else
4933 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004934
4935 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004936 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004937 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004938}
4939
4940#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004941static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004942 psa_algorithm_t hash_alg,
4943 psa_key_derivation_step_t step,
4944 const uint8_t *data,
4945 size_t data_length )
4946{
4947 psa_status_t status;
4948 switch( step )
4949 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004950 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004951 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004952 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004953 status = psa_hmac_setup_internal( &hkdf->hmac,
4954 data, data_length,
4955 hash_alg );
4956 if( status != PSA_SUCCESS )
4957 return( status );
4958 hkdf->state = HKDF_STATE_STARTED;
4959 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004960 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004961 /* If no salt was provided, use an empty salt. */
4962 if( hkdf->state == HKDF_STATE_INIT )
4963 {
4964 status = psa_hmac_setup_internal( &hkdf->hmac,
4965 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004966 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004967 if( status != PSA_SUCCESS )
4968 return( status );
4969 hkdf->state = HKDF_STATE_STARTED;
4970 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004971 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004972 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004973 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4974 data, data_length );
4975 if( status != PSA_SUCCESS )
4976 return( status );
4977 status = psa_hmac_finish_internal( &hkdf->hmac,
4978 hkdf->prk,
4979 sizeof( hkdf->prk ) );
4980 if( status != PSA_SUCCESS )
4981 return( status );
4982 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4983 hkdf->block_number = 0;
4984 hkdf->state = HKDF_STATE_KEYED;
4985 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004986 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004987 if( hkdf->state == HKDF_STATE_OUTPUT )
4988 return( PSA_ERROR_BAD_STATE );
4989 if( hkdf->info_set )
4990 return( PSA_ERROR_BAD_STATE );
4991 hkdf->info_length = data_length;
4992 if( data_length != 0 )
4993 {
4994 hkdf->info = mbedtls_calloc( 1, data_length );
4995 if( hkdf->info == NULL )
4996 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4997 memcpy( hkdf->info, data, data_length );
4998 }
4999 hkdf->info_set = 1;
5000 return( PSA_SUCCESS );
5001 default:
5002 return( PSA_ERROR_INVALID_ARGUMENT );
5003 }
5004}
Janos Follathb03233e2019-06-11 15:30:30 +01005005
Janos Follathf08e2652019-06-13 09:05:41 +01005006static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5007 const uint8_t *data,
5008 size_t data_length )
5009{
5010 if( prf->state != TLS12_PRF_STATE_INIT )
5011 return( PSA_ERROR_BAD_STATE );
5012
Janos Follathd6dce9f2019-07-04 09:11:38 +01005013 if( data_length != 0 )
5014 {
5015 prf->seed = mbedtls_calloc( 1, data_length );
5016 if( prf->seed == NULL )
5017 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005018
Janos Follathd6dce9f2019-07-04 09:11:38 +01005019 memcpy( prf->seed, data, data_length );
5020 prf->seed_length = data_length;
5021 }
Janos Follathf08e2652019-06-13 09:05:41 +01005022
5023 prf->state = TLS12_PRF_STATE_SEED_SET;
5024
5025 return( PSA_SUCCESS );
5026}
5027
Janos Follath81550542019-06-13 14:26:34 +01005028static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5029 psa_algorithm_t hash_alg,
5030 const uint8_t *data,
5031 size_t data_length )
5032{
5033 psa_status_t status;
5034 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5035 return( PSA_ERROR_BAD_STATE );
5036
5037 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5038 if( status != PSA_SUCCESS )
5039 return( status );
5040
5041 prf->state = TLS12_PRF_STATE_KEY_SET;
5042
5043 return( PSA_SUCCESS );
5044}
5045
Janos Follath6660f0e2019-06-17 08:44:03 +01005046static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5047 psa_tls12_prf_key_derivation_t *prf,
5048 psa_algorithm_t hash_alg,
5049 const uint8_t *data,
5050 size_t data_length )
5051{
5052 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005053 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5054 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005055
5056 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5057 return( PSA_ERROR_INVALID_ARGUMENT );
5058
5059 /* Quoting RFC 4279, Section 2:
5060 *
5061 * The premaster secret is formed as follows: if the PSK is N octets
5062 * long, concatenate a uint16 with the value N, N zero octets, a second
5063 * uint16 with the value N, and the PSK itself.
5064 */
5065
Janos Follath40e13932019-06-26 13:22:29 +01005066 *cur++ = ( data_length >> 8 ) & 0xff;
5067 *cur++ = ( data_length >> 0 ) & 0xff;
5068 memset( cur, 0, data_length );
5069 cur += data_length;
5070 *cur++ = pms[0];
5071 *cur++ = pms[1];
5072 memcpy( cur, data, data_length );
5073 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005074
Janos Follath40e13932019-06-26 13:22:29 +01005075 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005076
5077 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5078 return( status );
5079}
5080
Janos Follath63028dd2019-06-13 09:15:47 +01005081static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5082 const uint8_t *data,
5083 size_t data_length )
5084{
5085 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5086 return( PSA_ERROR_BAD_STATE );
5087
Janos Follathd6dce9f2019-07-04 09:11:38 +01005088 if( data_length != 0 )
5089 {
5090 prf->label = mbedtls_calloc( 1, data_length );
5091 if( prf->label == NULL )
5092 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005093
Janos Follathd6dce9f2019-07-04 09:11:38 +01005094 memcpy( prf->label, data, data_length );
5095 prf->label_length = data_length;
5096 }
Janos Follath63028dd2019-06-13 09:15:47 +01005097
5098 prf->state = TLS12_PRF_STATE_LABEL_SET;
5099
5100 return( PSA_SUCCESS );
5101}
5102
Janos Follathb03233e2019-06-11 15:30:30 +01005103static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5104 psa_algorithm_t hash_alg,
5105 psa_key_derivation_step_t step,
5106 const uint8_t *data,
5107 size_t data_length )
5108{
Janos Follathb03233e2019-06-11 15:30:30 +01005109 switch( step )
5110 {
Janos Follathf08e2652019-06-13 09:05:41 +01005111 case PSA_KEY_DERIVATION_INPUT_SEED:
5112 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005113 case PSA_KEY_DERIVATION_INPUT_SECRET:
5114 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005115 case PSA_KEY_DERIVATION_INPUT_LABEL:
5116 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005117 default:
5118 return( PSA_ERROR_INVALID_ARGUMENT );
5119 }
5120}
Janos Follath6660f0e2019-06-17 08:44:03 +01005121
5122static psa_status_t psa_tls12_prf_psk_to_ms_input(
5123 psa_tls12_prf_key_derivation_t *prf,
5124 psa_algorithm_t hash_alg,
5125 psa_key_derivation_step_t step,
5126 const uint8_t *data,
5127 size_t data_length )
5128{
5129 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005130 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005131 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5132 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005133 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005134
5135 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5136}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005137#endif /* MBEDTLS_MD_C */
5138
Gilles Peskineb8965192019-09-24 16:21:10 +02005139/** Check whether the given key type is acceptable for the given
5140 * input step of a key derivation.
5141 *
5142 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5143 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5144 * Both secret and non-secret inputs can alternatively have the type
5145 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5146 * that the input was passed as a buffer rather than via a key object.
5147 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005148static int psa_key_derivation_check_input_type(
5149 psa_key_derivation_step_t step,
5150 psa_key_type_t key_type )
5151{
5152 switch( step )
5153 {
5154 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005155 if( key_type == PSA_KEY_TYPE_DERIVE )
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 case PSA_KEY_DERIVATION_INPUT_LABEL:
5161 case PSA_KEY_DERIVATION_INPUT_SALT:
5162 case PSA_KEY_DERIVATION_INPUT_INFO:
5163 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005164 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5165 return( PSA_SUCCESS );
5166 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005167 return( PSA_SUCCESS );
5168 break;
5169 }
5170 return( PSA_ERROR_INVALID_ARGUMENT );
5171}
5172
Janos Follathb80a94e2019-06-12 15:54:46 +01005173static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005174 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005175 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005176 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005177 const uint8_t *data,
5178 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005179{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005180 psa_status_t status;
5181 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5182
5183 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005184 if( status != PSA_SUCCESS )
5185 goto exit;
5186
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005187#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005188 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005189 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005190 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005191 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005192 step, data, data_length );
5193 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005194 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005195 {
Janos Follathb03233e2019-06-11 15:30:30 +01005196 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5197 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5198 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005199 }
5200 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5201 {
5202 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5203 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5204 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005205 }
5206 else
5207#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005208 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005209 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005210 return( PSA_ERROR_BAD_STATE );
5211 }
5212
Gilles Peskine224b0d62019-09-23 18:13:17 +02005213exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005214 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005215 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005216 return( status );
5217}
5218
Janos Follath51f4a0f2019-06-14 11:35:55 +01005219psa_status_t psa_key_derivation_input_bytes(
5220 psa_key_derivation_operation_t *operation,
5221 psa_key_derivation_step_t step,
5222 const uint8_t *data,
5223 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005224{
Gilles Peskineb8965192019-09-24 16:21:10 +02005225 return( psa_key_derivation_input_internal( operation, step,
5226 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005227 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005228}
5229
Janos Follath51f4a0f2019-06-14 11:35:55 +01005230psa_status_t psa_key_derivation_input_key(
5231 psa_key_derivation_operation_t *operation,
5232 psa_key_derivation_step_t step,
5233 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005234{
5235 psa_key_slot_t *slot;
5236 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005237
Gilles Peskine28f8f302019-07-24 13:30:31 +02005238 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005239 PSA_KEY_USAGE_DERIVE,
5240 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005241 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005242 {
5243 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005244 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005245 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005246
5247 /* Passing a key object as a SECRET input unlocks the permission
5248 * to output to a key object. */
5249 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5250 operation->can_output_key = 1;
5251
Janos Follathb80a94e2019-06-12 15:54:46 +01005252 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005253 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005254 slot->data.raw.data,
5255 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005256}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005257
5258
5259
5260/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005261/* Key agreement */
5262/****************************************************************/
5263
Gilles Peskinea05219c2018-11-16 16:02:56 +01005264#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005265static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5266 size_t peer_key_length,
5267 const mbedtls_ecp_keypair *our_key,
5268 uint8_t *shared_secret,
5269 size_t shared_secret_size,
5270 size_t *shared_secret_length )
5271{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005272 mbedtls_ecp_keypair *their_key = NULL;
5273 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005274 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005275 size_t bits = 0;
5276 psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005277 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005278
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005279 status = psa_import_ec_public_key( curve,
5280 peer_key, peer_key_length,
5281 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005282 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_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5287 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005288 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005289 status = mbedtls_to_psa_error(
5290 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5291 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005292 goto exit;
5293
Jaeden Amero97271b32019-01-10 19:38:51 +00005294 status = mbedtls_to_psa_error(
5295 mbedtls_ecdh_calc_secret( &ecdh,
5296 shared_secret_length,
5297 shared_secret, shared_secret_size,
5298 mbedtls_ctr_drbg_random,
5299 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005300 if( status != PSA_SUCCESS )
5301 goto exit;
5302 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5303 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005304
5305exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005306 if( status != PSA_SUCCESS )
5307 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005308 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005309 mbedtls_ecp_keypair_free( their_key );
5310 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005311 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005312}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005313#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005314
Gilles Peskine01d718c2018-09-18 12:01:02 +02005315#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5316
Gilles Peskine0216fe12019-04-11 21:23:21 +02005317static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5318 psa_key_slot_t *private_key,
5319 const uint8_t *peer_key,
5320 size_t peer_key_length,
5321 uint8_t *shared_secret,
5322 size_t shared_secret_size,
5323 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005324{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005325 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005326 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005327#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005328 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005329 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005330 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005331 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5332 private_key->data.ecp,
5333 shared_secret, shared_secret_size,
5334 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005335#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005336 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005337 (void) private_key;
5338 (void) peer_key;
5339 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005340 (void) shared_secret;
5341 (void) shared_secret_size;
5342 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005343 return( PSA_ERROR_NOT_SUPPORTED );
5344 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005345}
5346
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005347/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005348 * to potentially free embedded data structures and wipe confidential data.
5349 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005350static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005351 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005352 psa_key_slot_t *private_key,
5353 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005354 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005355{
5356 psa_status_t status;
5357 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5358 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005359 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005360
5361 /* Step 1: run the secret agreement algorithm to generate the shared
5362 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005363 status = psa_key_agreement_raw_internal( ka_alg,
5364 private_key,
5365 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005366 shared_secret,
5367 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005368 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005369 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005370 goto exit;
5371
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005372 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005373 * the shared secret. A shared secret is permitted wherever a key
5374 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005375 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005376 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005377 shared_secret,
5378 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005379
Gilles Peskine12313cd2018-06-20 00:20:32 +02005380exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005381 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005382 return( status );
5383}
5384
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005385psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005386 psa_key_derivation_step_t step,
5387 psa_key_handle_t private_key,
5388 const uint8_t *peer_key,
5389 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005390{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005391 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005392 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005393 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005394 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005395 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005396 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005397 if( status != PSA_SUCCESS )
5398 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005399 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005400 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005401 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005402 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005403 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005404 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005405}
5406
Gilles Peskinebe697d82019-05-16 18:00:41 +02005407psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5408 psa_key_handle_t private_key,
5409 const uint8_t *peer_key,
5410 size_t peer_key_length,
5411 uint8_t *output,
5412 size_t output_size,
5413 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005414{
5415 psa_key_slot_t *slot;
5416 psa_status_t status;
5417
5418 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5419 {
5420 status = PSA_ERROR_INVALID_ARGUMENT;
5421 goto exit;
5422 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005423 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005424 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005425 if( status != PSA_SUCCESS )
5426 goto exit;
5427
5428 status = psa_key_agreement_raw_internal( alg, slot,
5429 peer_key, peer_key_length,
5430 output, output_size,
5431 output_length );
5432
5433exit:
5434 if( status != PSA_SUCCESS )
5435 {
5436 /* If an error happens and is not handled properly, the output
5437 * may be used as a key to protect sensitive data. Arrange for such
5438 * a key to be random, which is likely to result in decryption or
5439 * verification errors. This is better than filling the buffer with
5440 * some constant data such as zeros, which would result in the data
5441 * being protected with a reproducible, easily knowable key.
5442 */
5443 psa_generate_random( output, output_size );
5444 *output_length = output_size;
5445 }
5446 return( status );
5447}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005448
5449
5450/****************************************************************/
5451/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005452/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005453
Gilles Peskine4c317f42018-07-12 01:24:09 +02005454psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005455 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005456{
Janos Follath24eed8d2019-11-22 13:21:35 +00005457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005458 GUARD_MODULE_INITIALIZED;
5459
Gilles Peskinef181eca2019-08-07 13:49:00 +02005460 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5461 {
5462 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5463 output,
5464 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5465 if( ret != 0 )
5466 return( mbedtls_to_psa_error( ret ) );
5467 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5468 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5469 }
5470
Gilles Peskinee59236f2018-01-27 23:32:46 +01005471 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5472 return( mbedtls_to_psa_error( ret ) );
5473}
5474
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005475#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5476#include "mbedtls/entropy_poll.h"
5477
Gilles Peskine7228da22019-07-15 11:06:15 +02005478psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005479 size_t seed_size )
5480{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005481 if( global_data.initialized )
5482 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005483
5484 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5485 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5486 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5487 return( PSA_ERROR_INVALID_ARGUMENT );
5488
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005489 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005490}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005491#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005492
Gilles Peskinee56e8782019-04-26 17:34:02 +02005493#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5494static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5495 size_t domain_parameters_size,
5496 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005497{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005498 size_t i;
5499 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005500
Gilles Peskinee56e8782019-04-26 17:34:02 +02005501 if( domain_parameters_size == 0 )
5502 {
5503 *exponent = 65537;
5504 return( PSA_SUCCESS );
5505 }
5506
5507 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5508 * support values that fit in a 32-bit integer, which is larger than
5509 * int on just about every platform anyway. */
5510 if( domain_parameters_size > sizeof( acc ) )
5511 return( PSA_ERROR_NOT_SUPPORTED );
5512 for( i = 0; i < domain_parameters_size; i++ )
5513 acc = ( acc << 8 ) | domain_parameters[i];
5514 if( acc > INT_MAX )
5515 return( PSA_ERROR_NOT_SUPPORTED );
5516 *exponent = acc;
5517 return( PSA_SUCCESS );
5518}
5519#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5520
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005521static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005522 psa_key_slot_t *slot, size_t bits,
5523 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005524{
Gilles Peskine8e338702019-07-30 20:06:31 +02005525 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005526
Gilles Peskinee56e8782019-04-26 17:34:02 +02005527 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005528 return( PSA_ERROR_INVALID_ARGUMENT );
5529
Gilles Peskinee59236f2018-01-27 23:32:46 +01005530 if( key_type_is_raw_bytes( type ) )
5531 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005532 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005533 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5534 if( status != PSA_SUCCESS )
5535 return( status );
5536 status = psa_generate_random( slot->data.raw.data,
5537 slot->data.raw.bytes );
5538 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005539 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005540#if defined(MBEDTLS_DES_C)
5541 if( type == PSA_KEY_TYPE_DES )
5542 psa_des_set_key_parity( slot->data.raw.data,
5543 slot->data.raw.bytes );
5544#endif /* MBEDTLS_DES_C */
5545 }
5546 else
5547
5548#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005549 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005550 {
5551 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005552 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005553 int exponent;
5554 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005555 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5556 return( PSA_ERROR_NOT_SUPPORTED );
5557 /* Accept only byte-aligned keys, for the same reasons as
5558 * in psa_import_rsa_key(). */
5559 if( bits % 8 != 0 )
5560 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005561 status = psa_read_rsa_exponent( domain_parameters,
5562 domain_parameters_size,
5563 &exponent );
5564 if( status != PSA_SUCCESS )
5565 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005566 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5567 if( rsa == NULL )
5568 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5569 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5570 ret = mbedtls_rsa_gen_key( rsa,
5571 mbedtls_ctr_drbg_random,
5572 &global_data.ctr_drbg,
5573 (unsigned int) bits,
5574 exponent );
5575 if( ret != 0 )
5576 {
5577 mbedtls_rsa_free( rsa );
5578 mbedtls_free( rsa );
5579 return( mbedtls_to_psa_error( ret ) );
5580 }
5581 slot->data.rsa = rsa;
5582 }
5583 else
5584#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5585
5586#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005587 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005588 {
5589 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005590 mbedtls_ecp_group_id grp_id =
5591 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005592 const mbedtls_ecp_curve_info *curve_info =
5593 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5594 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005595 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005596 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005597 return( PSA_ERROR_NOT_SUPPORTED );
5598 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5599 return( PSA_ERROR_NOT_SUPPORTED );
5600 if( curve_info->bit_size != bits )
5601 return( PSA_ERROR_INVALID_ARGUMENT );
5602 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5603 if( ecp == NULL )
5604 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5605 mbedtls_ecp_keypair_init( ecp );
5606 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5607 mbedtls_ctr_drbg_random,
5608 &global_data.ctr_drbg );
5609 if( ret != 0 )
5610 {
5611 mbedtls_ecp_keypair_free( ecp );
5612 mbedtls_free( ecp );
5613 return( mbedtls_to_psa_error( ret ) );
5614 }
5615 slot->data.ecp = ecp;
5616 }
5617 else
5618#endif /* MBEDTLS_ECP_C */
5619
5620 return( PSA_ERROR_NOT_SUPPORTED );
5621
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005622 return( PSA_SUCCESS );
5623}
Darryl Green0c6575a2018-11-07 16:05:30 +00005624
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005625psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005626 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005627{
5628 psa_status_t status;
5629 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005630 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005631
Gilles Peskine0f84d622019-09-12 19:03:13 +02005632 /* Reject any attempt to create a zero-length key so that we don't
5633 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5634 if( psa_get_key_bits( attributes ) == 0 )
5635 return( PSA_ERROR_INVALID_ARGUMENT );
5636
Gilles Peskinedf179142019-07-15 22:02:14 +02005637 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5638 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005639 if( status != PSA_SUCCESS )
5640 goto exit;
5641
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005642#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5643 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005644 {
Gilles Peskine11792082019-08-06 18:36:36 +02005645 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5646 size_t pubkey_length = 0; /* We don't support this feature yet */
5647 if( drv->key_management == NULL ||
5648 drv->key_management->p_generate == NULL )
5649 {
5650 status = PSA_ERROR_NOT_SUPPORTED;
5651 goto exit;
5652 }
5653 status = drv->key_management->p_generate(
5654 psa_get_se_driver_context( driver ),
5655 slot->data.se.slot_number, attributes,
5656 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005657 }
Gilles Peskine11792082019-08-06 18:36:36 +02005658 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005659#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005660 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005661 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005662 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005663 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005664 }
Gilles Peskine11792082019-08-06 18:36:36 +02005665
5666exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005667 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005668 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005669 if( status != PSA_SUCCESS )
5670 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005671 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005672 *handle = 0;
5673 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005674 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005675}
5676
5677
Gilles Peskinee59236f2018-01-27 23:32:46 +01005678
5679/****************************************************************/
5680/* Module setup */
5681/****************************************************************/
5682
Gilles Peskine5e769522018-11-20 21:59:56 +01005683psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5684 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5685 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5686{
5687 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5688 return( PSA_ERROR_BAD_STATE );
5689 global_data.entropy_init = entropy_init;
5690 global_data.entropy_free = entropy_free;
5691 return( PSA_SUCCESS );
5692}
5693
Gilles Peskinee59236f2018-01-27 23:32:46 +01005694void mbedtls_psa_crypto_free( void )
5695{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005696 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005697 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5698 {
5699 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005700 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005701 }
5702 /* Wipe all remaining data, including configuration.
5703 * In particular, this sets all state indicator to the value
5704 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005705 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005706#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005707 /* Unregister all secure element drivers, so that we restart from
5708 * a pristine state. */
5709 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005710#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005711}
5712
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005713#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5714/** Recover a transaction that was interrupted by a power failure.
5715 *
5716 * This function is called during initialization, before psa_crypto_init()
5717 * returns. If this function returns a failure status, the initialization
5718 * fails.
5719 */
5720static psa_status_t psa_crypto_recover_transaction(
5721 const psa_crypto_transaction_t *transaction )
5722{
5723 switch( transaction->unknown.type )
5724 {
5725 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5726 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005727 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005728 * is implemented.
5729 * https://github.com/ARMmbed/mbed-crypto/issues/218
5730 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005731 default:
5732 /* We found an unsupported transaction in the storage.
5733 * We don't know what state the storage is in. Give up. */
5734 return( PSA_ERROR_STORAGE_FAILURE );
5735 }
5736}
5737#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5738
Gilles Peskinee59236f2018-01-27 23:32:46 +01005739psa_status_t psa_crypto_init( void )
5740{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005741 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005742 const unsigned char drbg_seed[] = "PSA";
5743
Gilles Peskinec6b69072018-11-20 21:42:52 +01005744 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005745 if( global_data.initialized != 0 )
5746 return( PSA_SUCCESS );
5747
Gilles Peskine5e769522018-11-20 21:59:56 +01005748 /* Set default configuration if
5749 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5750 if( global_data.entropy_init == NULL )
5751 global_data.entropy_init = mbedtls_entropy_init;
5752 if( global_data.entropy_free == NULL )
5753 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005754
Gilles Peskinec6b69072018-11-20 21:42:52 +01005755 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005756 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005757#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5758 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5759 /* The PSA entropy injection feature depends on using NV seed as an entropy
5760 * source. Add NV seed as an entropy source for PSA entropy injection. */
5761 mbedtls_entropy_add_source( &global_data.entropy,
5762 mbedtls_nv_seed_poll, NULL,
5763 MBEDTLS_ENTROPY_BLOCK_SIZE,
5764 MBEDTLS_ENTROPY_SOURCE_STRONG );
5765#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005766 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005767 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005768 status = mbedtls_to_psa_error(
5769 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5770 mbedtls_entropy_func,
5771 &global_data.entropy,
5772 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5773 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005774 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005775 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005776
Gilles Peskine66fb1262018-12-10 16:29:04 +01005777 status = psa_initialize_key_slots( );
5778 if( status != PSA_SUCCESS )
5779 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005780
Gilles Peskined9348f22019-10-01 15:22:29 +02005781#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5782 status = psa_init_all_se_drivers( );
5783 if( status != PSA_SUCCESS )
5784 goto exit;
5785#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5786
Gilles Peskine4b734222019-07-24 15:56:31 +02005787#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005788 status = psa_crypto_load_transaction( );
5789 if( status == PSA_SUCCESS )
5790 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005791 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5792 if( status != PSA_SUCCESS )
5793 goto exit;
5794 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005795 }
5796 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5797 {
5798 /* There's no transaction to complete. It's all good. */
5799 status = PSA_SUCCESS;
5800 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005801#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005802
Gilles Peskinec6b69072018-11-20 21:42:52 +01005803 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005804 global_data.initialized = 1;
5805
5806exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005807 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005808 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005809 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005810}
5811
5812#endif /* MBEDTLS_PSA_CRYPTO_C */