blob: 9362ef0ba1ee18698477272da7e4e041b72e4322 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
5 * Copyright (C) 2018, ARM Limited, All Rights Reserved
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 * This file is part of mbed TLS (https://tls.mbed.org)
21 */
22
Gilles Peskinedb09ef62020-06-03 01:43:33 +020023#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010024
25#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030026
itayzafrir7723ab12019-02-14 10:28:02 +020027#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010028#include "psa/crypto.h"
29
Gilles Peskine039b90c2018-12-07 18:24:41 +010030#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010031#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020032#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020033#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020034#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010035#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010036/* Include internal declarations that are useful for implementing persistently
37 * stored keys. */
38#include "psa_crypto_storage.h"
39
Gilles Peskineb46bef22019-07-30 21:32:04 +020040#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010041#include <stdlib.h>
42#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010043#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020044#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045#define mbedtls_calloc calloc
46#define mbedtls_free free
47#endif
48
Gilles Peskinea5905292018-02-07 20:59:33 +010049#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020050#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000051#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020052#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/blowfish.h"
54#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020055#include "mbedtls/chacha20.h"
56#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010057#include "mbedtls/cipher.h"
58#include "mbedtls/ccm.h"
59#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010060#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020062#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010063#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010064#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010065#include "mbedtls/error.h"
66#include "mbedtls/gcm.h"
67#include "mbedtls/md2.h"
68#include "mbedtls/md4.h"
69#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010070#include "mbedtls/md.h"
71#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010072#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010073#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010074#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000075#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010077#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/sha1.h"
79#include "mbedtls/sha256.h"
80#include "mbedtls/sha512.h"
81#include "mbedtls/xtea.h"
82
Gilles Peskine996deb12018-08-01 15:45:45 +020083#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
84
Gilles Peskine9ef733f2018-02-07 21:05:37 +010085/* constant-time buffer comparison */
86static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
87{
88 size_t i;
89 unsigned char diff = 0;
90
91 for( i = 0; i < n; i++ )
92 diff |= a[i] ^ b[i];
93
94 return( diff );
95}
96
97
98
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010099/****************************************************************/
100/* Global data, support functions and library management */
101/****************************************************************/
102
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200103static int key_type_is_raw_bytes( psa_key_type_t type )
104{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200105 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200106}
107
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100108/* Values for psa_global_data_t::rng_state */
109#define RNG_NOT_INITIALIZED 0
110#define RNG_INITIALIZED 1
111#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100112
Gilles Peskine2d277862018-06-18 15:41:12 +0200113typedef struct
114{
Gilles Peskine5e769522018-11-20 21:59:56 +0100115 void (* entropy_init )( mbedtls_entropy_context *ctx );
116 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100117 mbedtls_entropy_context entropy;
118 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100119 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100120 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100121} psa_global_data_t;
122
123static psa_global_data_t global_data;
124
itayzafrir0adf0fc2018-09-06 16:24:41 +0300125#define GUARD_MODULE_INITIALIZED \
126 if( global_data.initialized == 0 ) \
127 return( PSA_ERROR_BAD_STATE );
128
Gilles Peskinee59236f2018-01-27 23:32:46 +0100129static psa_status_t mbedtls_to_psa_error( int ret )
130{
Gilles Peskinea5905292018-02-07 20:59:33 +0100131 /* If there's both a high-level code and low-level code, dispatch on
132 * the high-level code. */
133 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100134 {
135 case 0:
136 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100137
138 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
139 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
140 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
141 return( PSA_ERROR_NOT_SUPPORTED );
142 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
143 return( PSA_ERROR_HARDWARE_FAILURE );
144
145 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
146 return( PSA_ERROR_HARDWARE_FAILURE );
147
Gilles Peskine9a944802018-06-21 09:35:35 +0200148 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
149 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
150 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
151 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
152 case MBEDTLS_ERR_ASN1_INVALID_DATA:
153 return( PSA_ERROR_INVALID_ARGUMENT );
154 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
155 return( PSA_ERROR_INSUFFICIENT_MEMORY );
156 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
157 return( PSA_ERROR_BUFFER_TOO_SMALL );
158
Jaeden Amero93e21112019-02-20 13:57:28 +0000159#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000160 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
162 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
163#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100164 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
165 return( PSA_ERROR_NOT_SUPPORTED );
166 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
167 return( PSA_ERROR_HARDWARE_FAILURE );
168
Jaeden Amero93e21112019-02-20 13:57:28 +0000169#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000170 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
172 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
173#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
175 return( PSA_ERROR_NOT_SUPPORTED );
176 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
177 return( PSA_ERROR_HARDWARE_FAILURE );
178
179 case MBEDTLS_ERR_CCM_BAD_INPUT:
180 return( PSA_ERROR_INVALID_ARGUMENT );
181 case MBEDTLS_ERR_CCM_AUTH_FAILED:
182 return( PSA_ERROR_INVALID_SIGNATURE );
183 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
184 return( PSA_ERROR_HARDWARE_FAILURE );
185
Gilles Peskine26869f22019-05-06 15:25:00 +0200186 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
187 return( PSA_ERROR_INVALID_ARGUMENT );
188
189 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
190 return( PSA_ERROR_BAD_STATE );
191 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
192 return( PSA_ERROR_INVALID_SIGNATURE );
193
Gilles Peskinea5905292018-02-07 20:59:33 +0100194 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
195 return( PSA_ERROR_NOT_SUPPORTED );
196 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
197 return( PSA_ERROR_INVALID_ARGUMENT );
198 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
199 return( PSA_ERROR_INSUFFICIENT_MEMORY );
200 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
201 return( PSA_ERROR_INVALID_PADDING );
202 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
203 return( PSA_ERROR_BAD_STATE );
204 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
205 return( PSA_ERROR_INVALID_SIGNATURE );
206 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200207 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100208 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
209 return( PSA_ERROR_HARDWARE_FAILURE );
210
211 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
214 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
215 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
216 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
217 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
218 return( PSA_ERROR_NOT_SUPPORTED );
219 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
220 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
221
222 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
223 return( PSA_ERROR_NOT_SUPPORTED );
224 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
225 return( PSA_ERROR_HARDWARE_FAILURE );
226
Gilles Peskinee59236f2018-01-27 23:32:46 +0100227 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
228 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
229 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
230 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100231
232 case MBEDTLS_ERR_GCM_AUTH_FAILED:
233 return( PSA_ERROR_INVALID_SIGNATURE );
234 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200235 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100236 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
237 return( PSA_ERROR_HARDWARE_FAILURE );
238
239 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
240 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
241 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
244 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
245 return( PSA_ERROR_NOT_SUPPORTED );
246 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
247 return( PSA_ERROR_INVALID_ARGUMENT );
248 case MBEDTLS_ERR_MD_ALLOC_FAILED:
249 return( PSA_ERROR_INSUFFICIENT_MEMORY );
250 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
251 return( PSA_ERROR_STORAGE_FAILURE );
252 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
253 return( PSA_ERROR_HARDWARE_FAILURE );
254
Gilles Peskinef76aa772018-10-29 19:24:33 +0100255 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
256 return( PSA_ERROR_STORAGE_FAILURE );
257 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
258 return( PSA_ERROR_INVALID_ARGUMENT );
259 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
262 return( PSA_ERROR_BUFFER_TOO_SMALL );
263 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
264 return( PSA_ERROR_INVALID_ARGUMENT );
265 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
270 return( PSA_ERROR_INSUFFICIENT_MEMORY );
271
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100272 case MBEDTLS_ERR_PK_ALLOC_FAILED:
273 return( PSA_ERROR_INSUFFICIENT_MEMORY );
274 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
275 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
276 return( PSA_ERROR_INVALID_ARGUMENT );
277 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100278 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100279 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
280 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
281 return( PSA_ERROR_INVALID_ARGUMENT );
282 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
283 return( PSA_ERROR_NOT_SUPPORTED );
284 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
285 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
286 return( PSA_ERROR_NOT_PERMITTED );
287 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
288 return( PSA_ERROR_INVALID_ARGUMENT );
289 case MBEDTLS_ERR_PK_INVALID_ALG:
290 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
291 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
292 return( PSA_ERROR_NOT_SUPPORTED );
293 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
294 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100295 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
296 return( PSA_ERROR_HARDWARE_FAILURE );
297
Gilles Peskineff2d2002019-05-06 15:26:23 +0200298 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
299 return( PSA_ERROR_HARDWARE_FAILURE );
300 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
301 return( PSA_ERROR_NOT_SUPPORTED );
302
Gilles Peskinea5905292018-02-07 20:59:33 +0100303 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
304 return( PSA_ERROR_HARDWARE_FAILURE );
305
306 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
307 return( PSA_ERROR_INVALID_ARGUMENT );
308 case MBEDTLS_ERR_RSA_INVALID_PADDING:
309 return( PSA_ERROR_INVALID_PADDING );
310 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
311 return( PSA_ERROR_HARDWARE_FAILURE );
312 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
313 return( PSA_ERROR_INVALID_ARGUMENT );
314 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
315 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200316 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
318 return( PSA_ERROR_INVALID_SIGNATURE );
319 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
320 return( PSA_ERROR_BUFFER_TOO_SMALL );
321 case MBEDTLS_ERR_RSA_RNG_FAILED:
322 return( PSA_ERROR_INSUFFICIENT_MEMORY );
323 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
324 return( PSA_ERROR_NOT_SUPPORTED );
325 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327
328 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
329 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
330 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
331 return( PSA_ERROR_HARDWARE_FAILURE );
332
333 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
334 return( PSA_ERROR_INVALID_ARGUMENT );
335 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
336 return( PSA_ERROR_HARDWARE_FAILURE );
337
itayzafrir5c753392018-05-08 11:18:38 +0300338 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300339 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300340 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300341 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
342 return( PSA_ERROR_BUFFER_TOO_SMALL );
343 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
344 return( PSA_ERROR_NOT_SUPPORTED );
345 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
346 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
347 return( PSA_ERROR_INVALID_SIGNATURE );
348 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
349 return( PSA_ERROR_INSUFFICIENT_MEMORY );
350 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
351 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000352 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
353 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300354
Gilles Peskinee59236f2018-01-27 23:32:46 +0100355 default:
David Saadab4ecc272019-02-14 13:48:10 +0200356 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100357 }
358}
359
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200360
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200361
362
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100363/****************************************************************/
364/* Key management */
365/****************************************************************/
366
Gilles Peskine73167e12019-07-12 23:44:37 +0200367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
368static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
369{
Gilles Peskine8e338702019-07-30 20:06:31 +0200370 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200371}
372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
373
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100374#if defined(MBEDTLS_ECP_C)
Gilles Peskine5055b232019-12-12 17:49:31 +0100375mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve,
376 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200377{
378 switch( curve )
379 {
Gilles Peskine228abc52019-12-03 17:24:19 +0100380 case PSA_ECC_CURVE_SECP_R1:
381 switch( byte_length )
382 {
383 case PSA_BITS_TO_BYTES( 192 ):
384 return( MBEDTLS_ECP_DP_SECP192R1 );
385 case PSA_BITS_TO_BYTES( 224 ):
386 return( MBEDTLS_ECP_DP_SECP224R1 );
387 case PSA_BITS_TO_BYTES( 256 ):
388 return( MBEDTLS_ECP_DP_SECP256R1 );
389 case PSA_BITS_TO_BYTES( 384 ):
390 return( MBEDTLS_ECP_DP_SECP384R1 );
391 case PSA_BITS_TO_BYTES( 521 ):
392 return( MBEDTLS_ECP_DP_SECP521R1 );
393 default:
394 return( MBEDTLS_ECP_DP_NONE );
395 }
396 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100397
398 case PSA_ECC_CURVE_BRAINPOOL_P_R1:
399 switch( byte_length )
400 {
401 case PSA_BITS_TO_BYTES( 256 ):
402 return( MBEDTLS_ECP_DP_BP256R1 );
403 case PSA_BITS_TO_BYTES( 384 ):
404 return( MBEDTLS_ECP_DP_BP384R1 );
405 case PSA_BITS_TO_BYTES( 512 ):
406 return( MBEDTLS_ECP_DP_BP512R1 );
407 default:
408 return( MBEDTLS_ECP_DP_NONE );
409 }
410 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100411
412 case PSA_ECC_CURVE_MONTGOMERY:
413 switch( byte_length )
414 {
415 case PSA_BITS_TO_BYTES( 255 ):
416 return( MBEDTLS_ECP_DP_CURVE25519 );
417 case PSA_BITS_TO_BYTES( 448 ):
418 return( MBEDTLS_ECP_DP_CURVE448 );
419 default:
420 return( MBEDTLS_ECP_DP_NONE );
421 }
422 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100423
424 case PSA_ECC_CURVE_SECP_K1:
425 switch( byte_length )
426 {
427 case PSA_BITS_TO_BYTES( 192 ):
428 return( MBEDTLS_ECP_DP_SECP192K1 );
429 case PSA_BITS_TO_BYTES( 224 ):
430 return( MBEDTLS_ECP_DP_SECP224K1 );
431 case PSA_BITS_TO_BYTES( 256 ):
432 return( MBEDTLS_ECP_DP_SECP256K1 );
433 default:
434 return( MBEDTLS_ECP_DP_NONE );
435 }
436 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100437
Gilles Peskine12313cd2018-06-20 00:20:32 +0200438 default:
439 return( MBEDTLS_ECP_DP_NONE );
440 }
441}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100442#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200443
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200444static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
445 size_t bits,
446 struct raw_data *raw )
447{
448 /* Check that the bit size is acceptable for the key type */
449 switch( type )
450 {
451 case PSA_KEY_TYPE_RAW_DATA:
452#if defined(MBEDTLS_MD_C)
453 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200454#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200455 case PSA_KEY_TYPE_DERIVE:
456 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200457#if defined(MBEDTLS_AES_C)
458 case PSA_KEY_TYPE_AES:
459 if( bits != 128 && bits != 192 && bits != 256 )
460 return( PSA_ERROR_INVALID_ARGUMENT );
461 break;
462#endif
463#if defined(MBEDTLS_CAMELLIA_C)
464 case PSA_KEY_TYPE_CAMELLIA:
465 if( bits != 128 && bits != 192 && bits != 256 )
466 return( PSA_ERROR_INVALID_ARGUMENT );
467 break;
468#endif
469#if defined(MBEDTLS_DES_C)
470 case PSA_KEY_TYPE_DES:
471 if( bits != 64 && bits != 128 && bits != 192 )
472 return( PSA_ERROR_INVALID_ARGUMENT );
473 break;
474#endif
475#if defined(MBEDTLS_ARC4_C)
476 case PSA_KEY_TYPE_ARC4:
477 if( bits < 8 || bits > 2048 )
478 return( PSA_ERROR_INVALID_ARGUMENT );
479 break;
480#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200481#if defined(MBEDTLS_CHACHA20_C)
482 case PSA_KEY_TYPE_CHACHA20:
483 if( bits != 256 )
484 return( PSA_ERROR_INVALID_ARGUMENT );
485 break;
486#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200487 default:
488 return( PSA_ERROR_NOT_SUPPORTED );
489 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200490 if( bits % 8 != 0 )
491 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200492
493 /* Allocate memory for the key */
494 raw->bytes = PSA_BITS_TO_BYTES( bits );
495 raw->data = mbedtls_calloc( 1, raw->bytes );
496 if( raw->data == NULL )
497 {
498 raw->bytes = 0;
499 return( PSA_ERROR_INSUFFICIENT_MEMORY );
500 }
501 return( PSA_SUCCESS );
502}
503
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200504#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100505/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
506 * that are not a multiple of 8) well. For example, there is only
507 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
508 * way to return the exact bit size of a key.
509 * To keep things simple, reject non-byte-aligned key sizes. */
510static psa_status_t psa_check_rsa_key_byte_aligned(
511 const mbedtls_rsa_context *rsa )
512{
513 mbedtls_mpi n;
514 psa_status_t status;
515 mbedtls_mpi_init( &n );
516 status = mbedtls_to_psa_error(
517 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
518 if( status == PSA_SUCCESS )
519 {
520 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
521 status = PSA_ERROR_NOT_SUPPORTED;
522 }
523 mbedtls_mpi_free( &n );
524 return( status );
525}
526
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000527static psa_status_t psa_import_rsa_key( psa_key_type_t type,
528 const uint8_t *data,
529 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200530 mbedtls_rsa_context **p_rsa )
531{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000532 psa_status_t status;
533 mbedtls_pk_context pk;
534 mbedtls_rsa_context *rsa;
535 size_t bits;
536
537 mbedtls_pk_init( &pk );
538
539 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200540 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000541 status = mbedtls_to_psa_error(
542 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200543 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000544 status = mbedtls_to_psa_error(
545 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
546 if( status != PSA_SUCCESS )
547 goto exit;
548
549 /* We have something that the pkparse module recognizes. If it is a
550 * valid RSA key, store it. */
551 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200552 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000553 status = PSA_ERROR_INVALID_ARGUMENT;
554 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200555 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000556
557 rsa = mbedtls_pk_rsa( pk );
558 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
559 * supports non-byte-aligned key sizes, but not well. For example,
560 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
561 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
562 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
563 {
564 status = PSA_ERROR_NOT_SUPPORTED;
565 goto exit;
566 }
567 status = psa_check_rsa_key_byte_aligned( rsa );
568
569exit:
570 /* Free the content of the pk object only on error. */
571 if( status != PSA_SUCCESS )
572 {
573 mbedtls_pk_free( &pk );
574 return( status );
575 }
576
577 /* On success, store the content of the object in the RSA context. */
578 *p_rsa = rsa;
579
580 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200581}
582#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
583
Jaeden Ameroccdce902019-01-10 11:42:27 +0000584#if defined(MBEDTLS_ECP_C)
Gilles Peskine4cd32772019-12-02 20:49:42 +0100585static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve,
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100586 size_t data_length,
587 int is_public,
Gilles Peskine4cd32772019-12-02 20:49:42 +0100588 mbedtls_ecp_keypair **p_ecp )
589{
590 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
591 *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
592 if( *p_ecp == NULL )
593 return( PSA_ERROR_INSUFFICIENT_MEMORY );
594 mbedtls_ecp_keypair_init( *p_ecp );
595
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100596 if( is_public )
597 {
598 /* A public key is represented as:
599 * - The byte 0x04;
600 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
601 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
602 * So its data length is 2m+1 where n is the key size in bits.
603 */
604 if( ( data_length & 1 ) == 0 )
605 return( PSA_ERROR_INVALID_ARGUMENT );
606 data_length = data_length / 2;
607 }
608
Gilles Peskine4cd32772019-12-02 20:49:42 +0100609 /* Load the group. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100610 grp_id = mbedtls_ecc_group_of_psa( curve, data_length );
611 if( grp_id == MBEDTLS_ECP_DP_NONE )
612 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100613 return( mbedtls_to_psa_error(
614 mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) );
615}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000616
617/* Import a public key given as the uncompressed representation defined by SEC1
618 * 2.3.3 as the content of an ECPoint. */
619static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
620 const uint8_t *data,
621 size_t data_length,
622 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200623{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200624 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000625 mbedtls_ecp_keypair *ecp = NULL;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000626
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100627 status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp );
Jaeden Ameroccdce902019-01-10 11:42:27 +0000628 if( status != PSA_SUCCESS )
629 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100630
Jaeden Ameroccdce902019-01-10 11:42:27 +0000631 /* Load the public value. */
632 status = mbedtls_to_psa_error(
633 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
634 data, data_length ) );
635 if( status != PSA_SUCCESS )
636 goto exit;
637
638 /* Check that the point is on the curve. */
639 status = mbedtls_to_psa_error(
640 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
641 if( status != PSA_SUCCESS )
642 goto exit;
643
644 *p_ecp = ecp;
645 return( PSA_SUCCESS );
646
647exit:
648 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200649 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000650 mbedtls_ecp_keypair_free( ecp );
651 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200652 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000653 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200654}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200655
Gilles Peskinef76aa772018-10-29 19:24:33 +0100656/* Import a private key given as a byte string which is the private value
657 * in big-endian order. */
658static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
659 const uint8_t *data,
660 size_t data_length,
661 mbedtls_ecp_keypair **p_ecp )
662{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200663 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100664 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100665
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100666 status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100667 if( status != PSA_SUCCESS )
668 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100669
Gilles Peskinef76aa772018-10-29 19:24:33 +0100670 /* Load the secret value. */
671 status = mbedtls_to_psa_error(
672 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
673 if( status != PSA_SUCCESS )
674 goto exit;
675 /* Validate the private key. */
676 status = mbedtls_to_psa_error(
677 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
678 if( status != PSA_SUCCESS )
679 goto exit;
680 /* Calculate the public key from the private key. */
681 status = mbedtls_to_psa_error(
682 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
683 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
684 if( status != PSA_SUCCESS )
685 goto exit;
686
687 *p_ecp = ecp;
688 return( PSA_SUCCESS );
689
690exit:
691 if( ecp != NULL )
692 {
693 mbedtls_ecp_keypair_free( ecp );
694 mbedtls_free( ecp );
695 }
696 return( status );
697}
698#endif /* defined(MBEDTLS_ECP_C) */
699
Gilles Peskineb46bef22019-07-30 21:32:04 +0200700
701/** Return the size of the key in the given slot, in bits.
702 *
703 * \param[in] slot A key slot.
704 *
705 * \return The key size in bits, read from the metadata in the slot.
706 */
707static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
708{
709 return( slot->attr.bits );
710}
711
712/** Calculate the size of the key in the given slot, in bits.
713 *
714 * \param[in] slot A key slot containing a transparent key.
715 *
716 * \return The key size in bits, calculated from the key data.
717 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200718static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200719{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200720 size_t bits = 0; /* return 0 on an empty slot */
721
Gilles Peskineb46bef22019-07-30 21:32:04 +0200722 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200723 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200724#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200725 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
726 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200727#endif /* defined(MBEDTLS_RSA_C) */
728#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200729 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
730 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200731#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200732
733 /* We know that the size fits in psa_key_bits_t thanks to checks
734 * when the key was created. */
735 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200736}
737
Gilles Peskine8e338702019-07-30 20:06:31 +0200738/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100739 * previously. This function assumes that the slot does not contain
740 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100741psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
742 const uint8_t *data,
743 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100744{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200745 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746
Gilles Peskine8e338702019-07-30 20:06:31 +0200747 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100748 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200749 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200750 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100751 if( data_length > SIZE_MAX / 8 )
752 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200753 /* Enforce a size limit, and in particular ensure that the bit
754 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200755 if( bit_size > PSA_MAX_KEY_BITS )
756 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200757 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200758 &slot->data.raw );
759 if( status != PSA_SUCCESS )
760 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200761 if( data_length != 0 )
762 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100763 }
764 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100765#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200766 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100767 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200768 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100769 data, data_length,
770 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100771 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200772 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100773 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000774 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200775 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000776 data, data_length,
777 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100778 }
779 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100780#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000781#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200782 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100783 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200784 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000785 data, data_length,
786 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100787 }
788 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000789#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100790 {
791 return( PSA_ERROR_NOT_SUPPORTED );
792 }
Darryl Green940d72c2018-07-13 13:18:51 +0100793
Gilles Peskineb46bef22019-07-30 21:32:04 +0200794 if( status == PSA_SUCCESS )
795 {
796 /* Write the actual key size to the slot.
797 * psa_start_key_creation() wrote the size declared by the
798 * caller, which may be 0 (meaning unspecified) or wrong. */
799 slot->attr.bits = psa_calculate_key_bits( slot );
800 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100801 return( status );
802}
803
Gilles Peskinef603c712019-01-19 13:40:11 +0100804/** Calculate the intersection of two algorithm usage policies.
805 *
806 * Return 0 (which allows no operation) on incompatibility.
807 */
808static psa_algorithm_t psa_key_policy_algorithm_intersection(
809 psa_algorithm_t alg1,
810 psa_algorithm_t alg2 )
811{
Gilles Peskine549ea862019-05-22 11:45:59 +0200812 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100813 if( alg1 == alg2 )
814 return( alg1 );
815 /* If the policies are from the same hash-and-sign family, check
816 * if one is a wildcard. If so the other has the specific algorithm. */
817 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
818 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
819 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
820 {
821 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
822 return( alg2 );
823 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
824 return( alg1 );
825 }
826 /* If the policies are incompatible, allow nothing. */
827 return( 0 );
828}
829
Gilles Peskined6f371b2019-05-10 19:33:38 +0200830static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
831 psa_algorithm_t requested_alg )
832{
Gilles Peskine549ea862019-05-22 11:45:59 +0200833 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200834 if( requested_alg == policy_alg )
835 return( 1 );
836 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200837 * and requested_alg is the same hash-and-sign family with any hash,
838 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200839 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
840 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
841 {
842 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
843 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
844 }
845 /* If it isn't permitted, it's forbidden. */
846 return( 0 );
847}
848
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100849/** Test whether a policy permits an algorithm.
850 *
851 * The caller must test usage flags separately.
852 */
853static int psa_key_policy_permits( const psa_key_policy_t *policy,
854 psa_algorithm_t alg )
855{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200856 return( psa_key_algorithm_permits( policy->alg, alg ) ||
857 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100858}
859
Gilles Peskinef603c712019-01-19 13:40:11 +0100860/** Restrict a key policy based on a constraint.
861 *
862 * \param[in,out] policy The policy to restrict.
863 * \param[in] constraint The policy constraint to apply.
864 *
865 * \retval #PSA_SUCCESS
866 * \c *policy contains the intersection of the original value of
867 * \c *policy and \c *constraint.
868 * \retval #PSA_ERROR_INVALID_ARGUMENT
869 * \c *policy and \c *constraint are incompatible.
870 * \c *policy is unchanged.
871 */
872static psa_status_t psa_restrict_key_policy(
873 psa_key_policy_t *policy,
874 const psa_key_policy_t *constraint )
875{
876 psa_algorithm_t intersection_alg =
877 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200878 psa_algorithm_t intersection_alg2 =
879 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100880 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
881 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200882 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
883 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100884 policy->usage &= constraint->usage;
885 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200886 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100887 return( PSA_SUCCESS );
888}
889
Darryl Green06fd18d2018-07-16 11:21:11 +0100890/** Retrieve a slot which must contain a key. The key must have allow all the
891 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
892 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100893static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100894 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100895 psa_key_usage_t usage,
896 psa_algorithm_t alg )
897{
898 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100899 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100900
901 *p_slot = NULL;
902
Gilles Peskinec5487a82018-12-03 18:08:14 +0100903 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100904 if( status != PSA_SUCCESS )
905 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100906
907 /* Enforce that usage policy for the key slot contains all the flags
908 * required by the usage parameter. There is one exception: public
909 * keys can always be exported, so we treat public key objects as
910 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200911 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100912 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200913 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100914 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100915
916 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200917 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100918 return( PSA_ERROR_NOT_PERMITTED );
919
920 *p_slot = slot;
921 return( PSA_SUCCESS );
922}
Darryl Green940d72c2018-07-13 13:18:51 +0100923
Gilles Peskine28f8f302019-07-24 13:30:31 +0200924/** Retrieve a slot which must contain a transparent key.
925 *
926 * A transparent key is a key for which the key material is directly
927 * available, as opposed to a key in a secure element.
928 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200929 * This is a temporary function to use instead of psa_get_key_from_slot()
930 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200931 */
932#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
933static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
934 psa_key_slot_t **p_slot,
935 psa_key_usage_t usage,
936 psa_algorithm_t alg )
937{
938 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
939 if( status != PSA_SUCCESS )
940 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200941 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200942 {
943 *p_slot = NULL;
944 return( PSA_ERROR_NOT_SUPPORTED );
945 }
946 return( PSA_SUCCESS );
947}
948#else /* MBEDTLS_PSA_CRYPTO_SE_C */
949/* With no secure element support, all keys are transparent. */
950#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
951 psa_get_key_from_slot( handle, p_slot, usage, alg )
952#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
953
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100954/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100955static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000956{
Gilles Peskine73167e12019-07-12 23:44:37 +0200957#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
958 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000959 {
960 /* No key material to clean. */
961 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200962 else
963#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200964 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000965 {
966 /* No key material to clean. */
967 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200968 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000969 {
970 mbedtls_free( slot->data.raw.data );
971 }
972 else
973#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200974 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000975 {
976 mbedtls_rsa_free( slot->data.rsa );
977 mbedtls_free( slot->data.rsa );
978 }
979 else
980#endif /* defined(MBEDTLS_RSA_C) */
981#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200982 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000983 {
984 mbedtls_ecp_keypair_free( slot->data.ecp );
985 mbedtls_free( slot->data.ecp );
986 }
987 else
988#endif /* defined(MBEDTLS_ECP_C) */
989 {
990 /* Shouldn't happen: the key type is not any type that we
991 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200992 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000993 }
994
995 return( PSA_SUCCESS );
996}
997
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100998/** Completely wipe a slot in memory, including its policy.
999 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001000psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001001{
1002 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001003 /* Multipart operations may still be using the key. This is safe
1004 * because all multipart operation objects are independent from
1005 * the key slot: if they need to access the key after the setup
1006 * phase, they have a copy of the key. Note that this means that
1007 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001008 /* At this point, key material and other type-specific content has
1009 * been wiped. Clear remaining metadata. We can call memset and not
1010 * zeroize because the metadata is not particularly sensitive. */
1011 memset( slot, 0, sizeof( *slot ) );
1012 return( status );
1013}
1014
Gilles Peskinec5487a82018-12-03 18:08:14 +01001015psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001016{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001017 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001018 psa_status_t status; /* status of the last operation */
1019 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001020#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1021 psa_se_drv_table_entry_t *driver;
1022#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001023
Gilles Peskine1841cf42019-10-08 15:48:25 +02001024 if( handle == 0 )
1025 return( PSA_SUCCESS );
1026
Gilles Peskinec5487a82018-12-03 18:08:14 +01001027 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001028 if( status != PSA_SUCCESS )
1029 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001030
1031#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001032 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001033 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001034 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001035 /* For a key in a secure element, we need to do three things:
1036 * remove the key file in internal storage, destroy the
1037 * key inside the secure element, and update the driver's
1038 * persistent data. Start a transaction that will encompass these
1039 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001040 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001041 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001042 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001043 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001044 status = psa_crypto_save_transaction( );
1045 if( status != PSA_SUCCESS )
1046 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001047 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001048 /* We should still try to destroy the key in the secure
1049 * element and the key metadata in storage. This is especially
1050 * important if the error is that the storage is full.
1051 * But how to do it exactly without risking an inconsistent
1052 * state after a reset?
1053 * https://github.com/ARMmbed/mbed-crypto/issues/215
1054 */
1055 overall_status = status;
1056 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001057 }
1058
Gilles Peskine354f7672019-07-12 23:46:38 +02001059 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001060 if( overall_status == PSA_SUCCESS )
1061 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001062 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001063#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1064
Darryl Greend49a4992018-06-18 17:27:26 +01001065#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001066 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001067 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001068 status = psa_destroy_persistent_key( slot->attr.id );
1069 if( overall_status == PSA_SUCCESS )
1070 overall_status = status;
1071
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001072 /* TODO: other slots may have a copy of the same key. We should
1073 * invalidate them.
1074 * https://github.com/ARMmbed/mbed-crypto/issues/214
1075 */
Darryl Greend49a4992018-06-18 17:27:26 +01001076 }
1077#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001078
Gilles Peskinefc762652019-07-22 19:30:34 +02001079#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1080 if( driver != NULL )
1081 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001082 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001083 if( overall_status == PSA_SUCCESS )
1084 overall_status = status;
1085 status = psa_crypto_stop_transaction( );
1086 if( overall_status == PSA_SUCCESS )
1087 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001088 }
1089#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1090
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001091#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1092exit:
1093#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001094 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001095 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1096 if( overall_status == PSA_SUCCESS )
1097 overall_status = status;
1098 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001099}
1100
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001101void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001102{
Gilles Peskineb699f072019-04-26 16:06:02 +02001103 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001104 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001105}
1106
Gilles Peskineb699f072019-04-26 16:06:02 +02001107psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1108 psa_key_type_t type,
1109 const uint8_t *data,
1110 size_t data_length )
1111{
1112 uint8_t *copy = NULL;
1113
1114 if( data_length != 0 )
1115 {
1116 copy = mbedtls_calloc( 1, data_length );
1117 if( copy == NULL )
1118 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1119 memcpy( copy, data, data_length );
1120 }
1121 /* After this point, this function is guaranteed to succeed, so it
1122 * can start modifying `*attributes`. */
1123
1124 if( attributes->domain_parameters != NULL )
1125 {
1126 mbedtls_free( attributes->domain_parameters );
1127 attributes->domain_parameters = NULL;
1128 attributes->domain_parameters_size = 0;
1129 }
1130
1131 attributes->domain_parameters = copy;
1132 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001133 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001134 return( PSA_SUCCESS );
1135}
1136
1137psa_status_t psa_get_key_domain_parameters(
1138 const psa_key_attributes_t *attributes,
1139 uint8_t *data, size_t data_size, size_t *data_length )
1140{
1141 if( attributes->domain_parameters_size > data_size )
1142 return( PSA_ERROR_BUFFER_TOO_SMALL );
1143 *data_length = attributes->domain_parameters_size;
1144 if( attributes->domain_parameters_size != 0 )
1145 memcpy( data, attributes->domain_parameters,
1146 attributes->domain_parameters_size );
1147 return( PSA_SUCCESS );
1148}
1149
1150#if defined(MBEDTLS_RSA_C)
1151static psa_status_t psa_get_rsa_public_exponent(
1152 const mbedtls_rsa_context *rsa,
1153 psa_key_attributes_t *attributes )
1154{
1155 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001156 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001157 uint8_t *buffer = NULL;
1158 size_t buflen;
1159 mbedtls_mpi_init( &mpi );
1160
1161 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1162 if( ret != 0 )
1163 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001164 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1165 {
1166 /* It's the default value, which is reported as an empty string,
1167 * so there's nothing to do. */
1168 goto exit;
1169 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001170
1171 buflen = mbedtls_mpi_size( &mpi );
1172 buffer = mbedtls_calloc( 1, buflen );
1173 if( buffer == NULL )
1174 {
1175 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1176 goto exit;
1177 }
1178 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1179 if( ret != 0 )
1180 goto exit;
1181 attributes->domain_parameters = buffer;
1182 attributes->domain_parameters_size = buflen;
1183
1184exit:
1185 mbedtls_mpi_free( &mpi );
1186 if( ret != 0 )
1187 mbedtls_free( buffer );
1188 return( mbedtls_to_psa_error( ret ) );
1189}
1190#endif /* MBEDTLS_RSA_C */
1191
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001192/** Retrieve all the publicly-accessible attributes of a key.
1193 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001194psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1195 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001196{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001197 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001198 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001199
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001200 psa_reset_key_attributes( attributes );
1201
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001202 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001203 if( status != PSA_SUCCESS )
1204 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001205
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001206 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001207 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1208 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1209
1210#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1211 if( psa_key_slot_is_external( slot ) )
1212 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1213#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001214
Gilles Peskine8e338702019-07-30 20:06:31 +02001215 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001216 {
1217#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001218 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001219 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001220#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001221 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001222 * is not yet implemented.
1223 * https://github.com/ARMmbed/mbed-crypto/issues/216
1224 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001225 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001226 break;
1227#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001228 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1229 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001230#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001231 default:
1232 /* Nothing else to do. */
1233 break;
1234 }
1235
1236 if( status != PSA_SUCCESS )
1237 psa_reset_key_attributes( attributes );
1238 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001239}
1240
Gilles Peskinec8000c02019-08-02 20:15:51 +02001241#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1242psa_status_t psa_get_key_slot_number(
1243 const psa_key_attributes_t *attributes,
1244 psa_key_slot_number_t *slot_number )
1245{
1246 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1247 {
1248 *slot_number = attributes->slot_number;
1249 return( PSA_SUCCESS );
1250 }
1251 else
1252 return( PSA_ERROR_INVALID_ARGUMENT );
1253}
1254#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1255
Jaeden Ameroccdce902019-01-10 11:42:27 +00001256#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001257static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1258 unsigned char *buf, size_t size )
1259{
Janos Follath24eed8d2019-11-22 13:21:35 +00001260 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001261 unsigned char *c;
1262 size_t len = 0;
1263
1264 c = buf + size;
1265
1266 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1267
1268 return( (int) len );
1269}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001270#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001271
Gilles Peskinef603c712019-01-19 13:40:11 +01001272static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1273 uint8_t *data,
1274 size_t data_size,
1275 size_t *data_length,
1276 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001277{
Gilles Peskine5d309672019-07-12 23:47:28 +02001278#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1279 const psa_drv_se_t *drv;
1280 psa_drv_se_context_t *drv_context;
1281#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1282
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001283 *data_length = 0;
1284
Gilles Peskine8e338702019-07-30 20:06:31 +02001285 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001286 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001287
Gilles Peskinef9168942019-09-12 19:20:29 +02001288 /* Reject a zero-length output buffer now, since this can never be a
1289 * valid key representation. This way we know that data must be a valid
1290 * pointer and we can do things like memset(data, ..., data_size). */
1291 if( data_size == 0 )
1292 return( PSA_ERROR_BUFFER_TOO_SMALL );
1293
Gilles Peskine5d309672019-07-12 23:47:28 +02001294#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001295 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001296 {
1297 psa_drv_se_export_key_t method;
1298 if( drv->key_management == NULL )
1299 return( PSA_ERROR_NOT_SUPPORTED );
1300 method = ( export_public_key ?
1301 drv->key_management->p_export_public :
1302 drv->key_management->p_export );
1303 if( method == NULL )
1304 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001305 return( method( drv_context,
1306 slot->data.se.slot_number,
1307 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001308 }
1309#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1310
Gilles Peskine8e338702019-07-30 20:06:31 +02001311 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001312 {
1313 if( slot->data.raw.bytes > data_size )
1314 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001315 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1316 memset( data + slot->data.raw.bytes, 0,
1317 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001318 *data_length = slot->data.raw.bytes;
1319 return( PSA_SUCCESS );
1320 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001321#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001322 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001323 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001324 psa_status_t status;
1325
Gilles Peskineb46bef22019-07-30 21:32:04 +02001326 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001327 if( bytes > data_size )
1328 return( PSA_ERROR_BUFFER_TOO_SMALL );
1329 status = mbedtls_to_psa_error(
1330 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1331 if( status != PSA_SUCCESS )
1332 return( status );
1333 memset( data + bytes, 0, data_size - bytes );
1334 *data_length = bytes;
1335 return( PSA_SUCCESS );
1336 }
1337#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001338 else
Moran Peker17e36e12018-05-02 12:55:20 +03001339 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001340#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001341 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1342 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001343 {
Moran Pekera998bc62018-04-16 18:16:20 +03001344 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001345 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001346 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001347 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001348#if defined(MBEDTLS_RSA_C)
1349 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001350 pk.pk_info = &mbedtls_rsa_info;
1351 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001352#else
1353 return( PSA_ERROR_NOT_SUPPORTED );
1354#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001355 }
1356 else
1357 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001358#if defined(MBEDTLS_ECP_C)
1359 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001360 pk.pk_info = &mbedtls_eckey_info;
1361 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001362#else
1363 return( PSA_ERROR_NOT_SUPPORTED );
1364#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001365 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001366 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001367 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001368 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001369 }
Moran Peker17e36e12018-05-02 12:55:20 +03001370 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001371 {
Moran Peker17e36e12018-05-02 12:55:20 +03001372 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001373 }
Moran Peker60364322018-04-29 11:34:58 +03001374 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001375 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001376 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001377 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001378 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001379 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1380 * Move the data to the beginning and erase remaining data
1381 * at the original location. */
1382 if( 2 * (size_t) ret <= data_size )
1383 {
1384 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001385 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001386 }
1387 else if( (size_t) ret < data_size )
1388 {
1389 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001390 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001391 }
Moran Pekera998bc62018-04-16 18:16:20 +03001392 *data_length = ret;
1393 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001394 }
1395 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001396#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001397 {
1398 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001399 it is valid for a special-purpose implementation to omit
1400 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001401 return( PSA_ERROR_NOT_SUPPORTED );
1402 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001403 }
1404}
1405
Gilles Peskinec5487a82018-12-03 18:08:14 +01001406psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001407 uint8_t *data,
1408 size_t data_size,
1409 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001410{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001411 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001412 psa_status_t status;
1413
1414 /* Set the key to empty now, so that even when there are errors, we always
1415 * set data_length to a value between 0 and data_size. On error, setting
1416 * the key to empty is a good choice because an empty key representation is
1417 * unlikely to be accepted anywhere. */
1418 *data_length = 0;
1419
1420 /* Export requires the EXPORT flag. There is an exception for public keys,
1421 * which don't require any flag, but psa_get_key_from_slot takes
1422 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001423 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001424 if( status != PSA_SUCCESS )
1425 return( status );
1426 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001427 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001428}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001429
Gilles Peskinec5487a82018-12-03 18:08:14 +01001430psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001431 uint8_t *data,
1432 size_t data_size,
1433 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001434{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001435 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001436 psa_status_t status;
1437
1438 /* Set the key to empty now, so that even when there are errors, we always
1439 * set data_length to a value between 0 and data_size. On error, setting
1440 * the key to empty is a good choice because an empty key representation is
1441 * unlikely to be accepted anywhere. */
1442 *data_length = 0;
1443
1444 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001445 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001446 if( status != PSA_SUCCESS )
1447 return( status );
1448 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001449 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001450}
1451
Gilles Peskine91e8c332019-08-02 19:19:39 +02001452#if defined(static_assert)
1453static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1454 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001455static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001456 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001457static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001458 "One or more key attribute flag is listed as both internal-only and external-only" );
1459#endif
1460
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001461/** Validate that a key policy is internally well-formed.
1462 *
1463 * This function only rejects invalid policies. It does not validate the
1464 * consistency of the policy with respect to other attributes of the key
1465 * such as the key type.
1466 */
1467static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001468{
1469 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001470 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001471 PSA_KEY_USAGE_ENCRYPT |
1472 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001473 PSA_KEY_USAGE_SIGN_HASH |
1474 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001475 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1476 return( PSA_ERROR_INVALID_ARGUMENT );
1477
Gilles Peskine4747d192019-04-17 15:05:45 +02001478 return( PSA_SUCCESS );
1479}
1480
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001481/** Validate the internal consistency of key attributes.
1482 *
1483 * This function only rejects invalid attribute values. If does not
1484 * validate the consistency of the attributes with any key data that may
1485 * be involved in the creation of the key.
1486 *
1487 * Call this function early in the key creation process.
1488 *
1489 * \param[in] attributes Key attributes for the new key.
1490 * \param[out] p_drv On any return, the driver for the key, if any.
1491 * NULL for a transparent key.
1492 *
1493 */
1494static psa_status_t psa_validate_key_attributes(
1495 const psa_key_attributes_t *attributes,
1496 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001497{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001498 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001499
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001500 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1501 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001502 if( status != PSA_SUCCESS )
1503 return( status );
1504
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001505 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1506 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001507 if( status != PSA_SUCCESS )
1508 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001509
1510 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001511 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001512 return( status );
1513
1514 /* Refuse to create overly large keys.
1515 * Note that this doesn't trigger on import if the attributes don't
1516 * explicitly specify a size (so psa_get_key_bits returns 0), so
1517 * psa_import_key() needs its own checks. */
1518 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1519 return( PSA_ERROR_NOT_SUPPORTED );
1520
Gilles Peskine91e8c332019-08-02 19:19:39 +02001521 /* Reject invalid flags. These should not be reachable through the API. */
1522 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1523 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1524 return( PSA_ERROR_INVALID_ARGUMENT );
1525
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001526 return( PSA_SUCCESS );
1527}
1528
Gilles Peskine4747d192019-04-17 15:05:45 +02001529/** Prepare a key slot to receive key material.
1530 *
1531 * This function allocates a key slot and sets its metadata.
1532 *
1533 * If this function fails, call psa_fail_key_creation().
1534 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001535 * This function is intended to be used as follows:
1536 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1537 * it with the specified attributes, and assign it a handle.
1538 * -# Populate the slot with the key material.
1539 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1540 * In case of failure at any step, stop the sequence and call
1541 * psa_fail_key_creation().
1542 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001543 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001544 * \param[in] attributes Key attributes for the new key.
1545 * \param[out] handle On success, a handle for the allocated slot.
1546 * \param[out] p_slot On success, a pointer to the prepared slot.
1547 * \param[out] p_drv On any return, the driver for the key, if any.
1548 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001549 *
1550 * \retval #PSA_SUCCESS
1551 * The key slot is ready to receive key material.
1552 * \return If this function fails, the key slot is an invalid state.
1553 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001554 */
1555static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001556 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001557 const psa_key_attributes_t *attributes,
1558 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001559 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001560 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001561{
1562 psa_status_t status;
1563 psa_key_slot_t *slot;
1564
Gilles Peskinedf179142019-07-15 22:02:14 +02001565 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001566 *p_drv = NULL;
1567
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001568 status = psa_validate_key_attributes( attributes, p_drv );
1569 if( status != PSA_SUCCESS )
1570 return( status );
1571
Gilles Peskineedbed562019-08-07 18:19:59 +02001572 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001573 if( status != PSA_SUCCESS )
1574 return( status );
1575 slot = *p_slot;
1576
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001577 /* We're storing the declared bit-size of the key. It's up to each
1578 * creation mechanism to verify that this information is correct.
1579 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001580 * an input (generate, device) but not for those where the bit-size
1581 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001582
1583 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001584
Gilles Peskine91e8c332019-08-02 19:19:39 +02001585 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001586 * external-only flags, query `attributes`. Thanks to the check
1587 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001588 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001589 * may have set. */
1590 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001591
Gilles Peskinecbaff462019-07-12 23:46:04 +02001592#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001593 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001594 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001595 * create the key file in internal storage, create the
1596 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001597 * persistent data. This is done by starting a transaction that will
1598 * encompass these three actions.
1599 * For registering a volatile key, we just need to find an appropriate
1600 * slot number inside the SE. Since the key is designated volatile, creating
1601 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001602 /* The first thing to do is to find a slot number for the new key.
1603 * We save the slot number in persistent storage as part of the
1604 * transaction data. It will be needed to recover if the power
1605 * fails during the key creation process, to clean up on the secure
1606 * element side after restarting. Obtaining a slot number from the
1607 * secure element driver updates its persistent state, but we do not yet
1608 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001609 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001610 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001611 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001612 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001613 &slot->data.se.slot_number );
1614 if( status != PSA_SUCCESS )
1615 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001616
1617 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001618 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001619 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1620 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1621 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1622 psa_crypto_transaction.key.id = slot->attr.id;
1623 status = psa_crypto_save_transaction( );
1624 if( status != PSA_SUCCESS )
1625 {
1626 (void) psa_crypto_stop_transaction( );
1627 return( status );
1628 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001629 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001630 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001631
1632 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1633 {
1634 /* Key registration only makes sense with a secure element. */
1635 return( PSA_ERROR_INVALID_ARGUMENT );
1636 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001637#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1638
Darryl Green0c6575a2018-11-07 16:05:30 +00001639 return( status );
1640}
Gilles Peskine4747d192019-04-17 15:05:45 +02001641
1642/** Finalize the creation of a key once its key material has been set.
1643 *
1644 * This entails writing the key to persistent storage.
1645 *
1646 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001647 * See the documentation of psa_start_key_creation() for the intended use
1648 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001649 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001650 * \param[in,out] slot Pointer to the slot with key material.
1651 * \param[in] driver The secure element driver for the key,
1652 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001653 *
1654 * \retval #PSA_SUCCESS
1655 * The key was successfully created. The handle is now valid.
1656 * \return If this function fails, the key slot is an invalid state.
1657 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001658 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001659static psa_status_t psa_finish_key_creation(
1660 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001661 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001662{
1663 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001664 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001665 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001666
1667#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001668 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001669 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001670#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1671 if( driver != NULL )
1672 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001673 psa_se_key_data_storage_t data;
1674#if defined(static_assert)
1675 static_assert( sizeof( slot->data.se.slot_number ) ==
1676 sizeof( data.slot_number ),
1677 "Slot number size does not match psa_se_key_data_storage_t" );
1678 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1679 "Bit-size size does not match psa_se_key_data_storage_t" );
1680#endif
1681 memcpy( &data.slot_number, &slot->data.se.slot_number,
1682 sizeof( slot->data.se.slot_number ) );
1683 memcpy( &data.bits, &slot->attr.bits,
1684 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001685 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001686 (uint8_t*) &data,
1687 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001688 }
1689 else
1690#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1691 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001692 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001693 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001694 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001695 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1696 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001697 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001698 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1699 status = psa_internal_export_key( slot,
1700 buffer, buffer_size, &length,
1701 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001702 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001703 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001704 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001705
Gilles Peskinef9168942019-09-12 19:20:29 +02001706 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001707 mbedtls_free( buffer );
1708 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001709 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001710#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1711
Gilles Peskinecbaff462019-07-12 23:46:04 +02001712#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001713 /* Finish the transaction for a key creation. This does not
1714 * happen when registering an existing key. Detect this case
1715 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001716 * creation of a persistent key in a secure element requires a transaction,
1717 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001718 if( driver != NULL &&
1719 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001720 {
1721 status = psa_save_se_persistent_data( driver );
1722 if( status != PSA_SUCCESS )
1723 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001724 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001725 return( status );
1726 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001727 status = psa_crypto_stop_transaction( );
1728 if( status != PSA_SUCCESS )
1729 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001730 }
1731#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1732
Gilles Peskine4747d192019-04-17 15:05:45 +02001733 return( status );
1734}
1735
1736/** Abort the creation of a key.
1737 *
1738 * You may call this function after calling psa_start_key_creation(),
1739 * or after psa_finish_key_creation() fails. In other circumstances, this
1740 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001741 * See the documentation of psa_start_key_creation() for the intended use
1742 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001743 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001744 * \param[in,out] slot Pointer to the slot with key material.
1745 * \param[in] driver The secure element driver for the key,
1746 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001747 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001748static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001749 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001750{
Gilles Peskine011e4282019-06-26 18:34:38 +02001751 (void) driver;
1752
Gilles Peskine4747d192019-04-17 15:05:45 +02001753 if( slot == NULL )
1754 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001755
1756#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001757 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001758 * element, and the failure happened later (when saving metadata
1759 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001760 * element.
1761 * https://github.com/ARMmbed/mbed-crypto/issues/217
1762 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001763
Gilles Peskined7729582019-08-05 15:55:54 +02001764 /* Abort the ongoing transaction if any (there may not be one if
1765 * the creation process failed before starting one, or if the
1766 * key creation is a registration of a key in a secure element).
1767 * Earlier functions must already have done what it takes to undo any
1768 * partial creation. All that's left is to update the transaction data
1769 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001770 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001771#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1772
Gilles Peskine4747d192019-04-17 15:05:45 +02001773 psa_wipe_key_slot( slot );
1774}
1775
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001776/** Validate optional attributes during key creation.
1777 *
1778 * Some key attributes are optional during key creation. If they are
1779 * specified in the attributes structure, check that they are consistent
1780 * with the data in the slot.
1781 *
1782 * This function should be called near the end of key creation, after
1783 * the slot in memory is fully populated but before saving persistent data.
1784 */
1785static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001786 const psa_key_slot_t *slot,
1787 const psa_key_attributes_t *attributes )
1788{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001789 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001790 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001791 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001792 return( PSA_ERROR_INVALID_ARGUMENT );
1793 }
1794
1795 if( attributes->domain_parameters_size != 0 )
1796 {
1797#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001798 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001799 {
1800 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001801 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001802 mbedtls_mpi_init( &actual );
1803 mbedtls_mpi_init( &required );
1804 ret = mbedtls_rsa_export( slot->data.rsa,
1805 NULL, NULL, NULL, NULL, &actual );
1806 if( ret != 0 )
1807 goto rsa_exit;
1808 ret = mbedtls_mpi_read_binary( &required,
1809 attributes->domain_parameters,
1810 attributes->domain_parameters_size );
1811 if( ret != 0 )
1812 goto rsa_exit;
1813 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1814 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1815 rsa_exit:
1816 mbedtls_mpi_free( &actual );
1817 mbedtls_mpi_free( &required );
1818 if( ret != 0)
1819 return( mbedtls_to_psa_error( ret ) );
1820 }
1821 else
1822#endif
1823 {
1824 return( PSA_ERROR_INVALID_ARGUMENT );
1825 }
1826 }
1827
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001828 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001829 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001830 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001831 return( PSA_ERROR_INVALID_ARGUMENT );
1832 }
1833
1834 return( PSA_SUCCESS );
1835}
1836
Gilles Peskine4747d192019-04-17 15:05:45 +02001837psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001838 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001839 size_t data_length,
1840 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001841{
1842 psa_status_t status;
1843 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001844 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001845
Gilles Peskine0f84d622019-09-12 19:03:13 +02001846 /* Reject zero-length symmetric keys (including raw data key objects).
1847 * This also rejects any key which might be encoded as an empty string,
1848 * which is never valid. */
1849 if( data_length == 0 )
1850 return( PSA_ERROR_INVALID_ARGUMENT );
1851
Gilles Peskinedf179142019-07-15 22:02:14 +02001852 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1853 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001854 if( status != PSA_SUCCESS )
1855 goto exit;
1856
Gilles Peskine5d309672019-07-12 23:47:28 +02001857#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1858 if( driver != NULL )
1859 {
1860 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001861 /* The driver should set the number of key bits, however in
1862 * case it doesn't, we initialize bits to an invalid value. */
1863 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001864 if( drv->key_management == NULL ||
1865 drv->key_management->p_import == NULL )
1866 {
1867 status = PSA_ERROR_NOT_SUPPORTED;
1868 goto exit;
1869 }
1870 status = drv->key_management->p_import(
1871 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001872 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001873 &bits );
1874 if( status != PSA_SUCCESS )
1875 goto exit;
1876 if( bits > PSA_MAX_KEY_BITS )
1877 {
1878 status = PSA_ERROR_NOT_SUPPORTED;
1879 goto exit;
1880 }
1881 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001882 }
1883 else
1884#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1885 {
1886 status = psa_import_key_into_slot( slot, data, data_length );
1887 if( status != PSA_SUCCESS )
1888 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001889 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001890 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001891 if( status != PSA_SUCCESS )
1892 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001893
Gilles Peskine011e4282019-06-26 18:34:38 +02001894 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001895exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001896 if( status != PSA_SUCCESS )
1897 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001898 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001899 *handle = 0;
1900 }
1901 return( status );
1902}
1903
Gilles Peskined7729582019-08-05 15:55:54 +02001904#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1905psa_status_t mbedtls_psa_register_se_key(
1906 const psa_key_attributes_t *attributes )
1907{
1908 psa_status_t status;
1909 psa_key_slot_t *slot = NULL;
1910 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001911 psa_key_handle_t handle = 0;
1912
1913 /* Leaving attributes unspecified is not currently supported.
1914 * It could make sense to query the key type and size from the
1915 * secure element, but not all secure elements support this
1916 * and the driver HAL doesn't currently support it. */
1917 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1918 return( PSA_ERROR_NOT_SUPPORTED );
1919 if( psa_get_key_bits( attributes ) == 0 )
1920 return( PSA_ERROR_NOT_SUPPORTED );
1921
1922 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1923 &handle, &slot, &driver );
1924 if( status != PSA_SUCCESS )
1925 goto exit;
1926
Gilles Peskined7729582019-08-05 15:55:54 +02001927 status = psa_finish_key_creation( slot, driver );
1928
1929exit:
1930 if( status != PSA_SUCCESS )
1931 {
1932 psa_fail_key_creation( slot, driver );
1933 }
1934 /* Registration doesn't keep the key in RAM. */
1935 psa_close_key( handle );
1936 return( status );
1937}
1938#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1939
Gilles Peskinef603c712019-01-19 13:40:11 +01001940static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001941 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001942{
1943 psa_status_t status;
1944 uint8_t *buffer = NULL;
1945 size_t buffer_size = 0;
1946 size_t length;
1947
Gilles Peskine8e338702019-07-30 20:06:31 +02001948 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001949 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001950 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001951 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001952 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001953 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1954 if( status != PSA_SUCCESS )
1955 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001956 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001957 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001958
1959exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001960 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001961 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001962 return( status );
1963}
1964
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001965psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1966 const psa_key_attributes_t *specified_attributes,
1967 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001968{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001969 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001970 psa_key_slot_t *source_slot = NULL;
1971 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001972 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001973 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001974
Gilles Peskine28f8f302019-07-24 13:30:31 +02001975 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001976 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001977 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001978 goto exit;
1979
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001980 status = psa_validate_optional_attributes( source_slot,
1981 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001982 if( status != PSA_SUCCESS )
1983 goto exit;
1984
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001985 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001986 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001987 if( status != PSA_SUCCESS )
1988 goto exit;
1989
Gilles Peskinedf179142019-07-15 22:02:14 +02001990 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1991 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001992 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001993 if( status != PSA_SUCCESS )
1994 goto exit;
1995
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001996#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1997 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01001998 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001999 /* Copying to a secure element is not implemented yet. */
2000 status = PSA_ERROR_NOT_SUPPORTED;
2001 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002002 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002003#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002004
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002005 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002006 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002007 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002008
Gilles Peskine011e4282019-06-26 18:34:38 +02002009 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002010exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002011 if( status != PSA_SUCCESS )
2012 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002013 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002014 *target_handle = 0;
2015 }
2016 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002017}
2018
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002019
2020
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002021/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002022/* Message digests */
2023/****************************************************************/
2024
Gilles Peskineb16841e2019-10-10 20:36:12 +02002025#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002026static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002027{
2028 switch( alg )
2029 {
2030#if defined(MBEDTLS_MD2_C)
2031 case PSA_ALG_MD2:
2032 return( &mbedtls_md2_info );
2033#endif
2034#if defined(MBEDTLS_MD4_C)
2035 case PSA_ALG_MD4:
2036 return( &mbedtls_md4_info );
2037#endif
2038#if defined(MBEDTLS_MD5_C)
2039 case PSA_ALG_MD5:
2040 return( &mbedtls_md5_info );
2041#endif
2042#if defined(MBEDTLS_RIPEMD160_C)
2043 case PSA_ALG_RIPEMD160:
2044 return( &mbedtls_ripemd160_info );
2045#endif
2046#if defined(MBEDTLS_SHA1_C)
2047 case PSA_ALG_SHA_1:
2048 return( &mbedtls_sha1_info );
2049#endif
2050#if defined(MBEDTLS_SHA256_C)
2051 case PSA_ALG_SHA_224:
2052 return( &mbedtls_sha224_info );
2053 case PSA_ALG_SHA_256:
2054 return( &mbedtls_sha256_info );
2055#endif
2056#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002057#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002058 case PSA_ALG_SHA_384:
2059 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002060#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002061 case PSA_ALG_SHA_512:
2062 return( &mbedtls_sha512_info );
2063#endif
2064 default:
2065 return( NULL );
2066 }
2067}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002068#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002069
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002070psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2071{
2072 switch( operation->alg )
2073 {
Gilles Peskine81736312018-06-26 15:04:31 +02002074 case 0:
2075 /* The object has (apparently) been initialized but it is not
2076 * in use. It's ok to call abort on such an object, and there's
2077 * nothing to do. */
2078 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002079#if defined(MBEDTLS_MD2_C)
2080 case PSA_ALG_MD2:
2081 mbedtls_md2_free( &operation->ctx.md2 );
2082 break;
2083#endif
2084#if defined(MBEDTLS_MD4_C)
2085 case PSA_ALG_MD4:
2086 mbedtls_md4_free( &operation->ctx.md4 );
2087 break;
2088#endif
2089#if defined(MBEDTLS_MD5_C)
2090 case PSA_ALG_MD5:
2091 mbedtls_md5_free( &operation->ctx.md5 );
2092 break;
2093#endif
2094#if defined(MBEDTLS_RIPEMD160_C)
2095 case PSA_ALG_RIPEMD160:
2096 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2097 break;
2098#endif
2099#if defined(MBEDTLS_SHA1_C)
2100 case PSA_ALG_SHA_1:
2101 mbedtls_sha1_free( &operation->ctx.sha1 );
2102 break;
2103#endif
2104#if defined(MBEDTLS_SHA256_C)
2105 case PSA_ALG_SHA_224:
2106 case PSA_ALG_SHA_256:
2107 mbedtls_sha256_free( &operation->ctx.sha256 );
2108 break;
2109#endif
2110#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002111#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002112 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002113#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002114 case PSA_ALG_SHA_512:
2115 mbedtls_sha512_free( &operation->ctx.sha512 );
2116 break;
2117#endif
2118 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002119 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002120 }
2121 operation->alg = 0;
2122 return( PSA_SUCCESS );
2123}
2124
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002125psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002126 psa_algorithm_t alg )
2127{
Janos Follath24eed8d2019-11-22 13:21:35 +00002128 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002129
2130 /* A context must be freshly initialized before it can be set up. */
2131 if( operation->alg != 0 )
2132 {
2133 return( PSA_ERROR_BAD_STATE );
2134 }
2135
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002136 switch( alg )
2137 {
2138#if defined(MBEDTLS_MD2_C)
2139 case PSA_ALG_MD2:
2140 mbedtls_md2_init( &operation->ctx.md2 );
2141 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2142 break;
2143#endif
2144#if defined(MBEDTLS_MD4_C)
2145 case PSA_ALG_MD4:
2146 mbedtls_md4_init( &operation->ctx.md4 );
2147 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2148 break;
2149#endif
2150#if defined(MBEDTLS_MD5_C)
2151 case PSA_ALG_MD5:
2152 mbedtls_md5_init( &operation->ctx.md5 );
2153 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2154 break;
2155#endif
2156#if defined(MBEDTLS_RIPEMD160_C)
2157 case PSA_ALG_RIPEMD160:
2158 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2159 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2160 break;
2161#endif
2162#if defined(MBEDTLS_SHA1_C)
2163 case PSA_ALG_SHA_1:
2164 mbedtls_sha1_init( &operation->ctx.sha1 );
2165 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2166 break;
2167#endif
2168#if defined(MBEDTLS_SHA256_C)
2169 case PSA_ALG_SHA_224:
2170 mbedtls_sha256_init( &operation->ctx.sha256 );
2171 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2172 break;
2173 case PSA_ALG_SHA_256:
2174 mbedtls_sha256_init( &operation->ctx.sha256 );
2175 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2176 break;
2177#endif
2178#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002179#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002180 case PSA_ALG_SHA_384:
2181 mbedtls_sha512_init( &operation->ctx.sha512 );
2182 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2183 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002184#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002185 case PSA_ALG_SHA_512:
2186 mbedtls_sha512_init( &operation->ctx.sha512 );
2187 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2188 break;
2189#endif
2190 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002191 return( PSA_ALG_IS_HASH( alg ) ?
2192 PSA_ERROR_NOT_SUPPORTED :
2193 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002194 }
2195 if( ret == 0 )
2196 operation->alg = alg;
2197 else
2198 psa_hash_abort( operation );
2199 return( mbedtls_to_psa_error( ret ) );
2200}
2201
2202psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2203 const uint8_t *input,
2204 size_t input_length )
2205{
Janos Follath24eed8d2019-11-22 13:21:35 +00002206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002207
2208 /* Don't require hash implementations to behave correctly on a
2209 * zero-length input, which may have an invalid pointer. */
2210 if( input_length == 0 )
2211 return( PSA_SUCCESS );
2212
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002213 switch( operation->alg )
2214 {
2215#if defined(MBEDTLS_MD2_C)
2216 case PSA_ALG_MD2:
2217 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2218 input, input_length );
2219 break;
2220#endif
2221#if defined(MBEDTLS_MD4_C)
2222 case PSA_ALG_MD4:
2223 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2224 input, input_length );
2225 break;
2226#endif
2227#if defined(MBEDTLS_MD5_C)
2228 case PSA_ALG_MD5:
2229 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2230 input, input_length );
2231 break;
2232#endif
2233#if defined(MBEDTLS_RIPEMD160_C)
2234 case PSA_ALG_RIPEMD160:
2235 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2236 input, input_length );
2237 break;
2238#endif
2239#if defined(MBEDTLS_SHA1_C)
2240 case PSA_ALG_SHA_1:
2241 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2242 input, input_length );
2243 break;
2244#endif
2245#if defined(MBEDTLS_SHA256_C)
2246 case PSA_ALG_SHA_224:
2247 case PSA_ALG_SHA_256:
2248 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2249 input, input_length );
2250 break;
2251#endif
2252#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002253#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002254 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002255#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002256 case PSA_ALG_SHA_512:
2257 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2258 input, input_length );
2259 break;
2260#endif
2261 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002262 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002263 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002264
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002265 if( ret != 0 )
2266 psa_hash_abort( operation );
2267 return( mbedtls_to_psa_error( ret ) );
2268}
2269
2270psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2271 uint8_t *hash,
2272 size_t hash_size,
2273 size_t *hash_length )
2274{
itayzafrir40835d42018-08-02 13:14:17 +03002275 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002276 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002277 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002278
2279 /* Fill the output buffer with something that isn't a valid hash
2280 * (barring an attack on the hash and deliberately-crafted input),
2281 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002282 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002283 /* If hash_size is 0 then hash may be NULL and then the
2284 * call to memset would have undefined behavior. */
2285 if( hash_size != 0 )
2286 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002287
2288 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002289 {
2290 status = PSA_ERROR_BUFFER_TOO_SMALL;
2291 goto exit;
2292 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002293
2294 switch( operation->alg )
2295 {
2296#if defined(MBEDTLS_MD2_C)
2297 case PSA_ALG_MD2:
2298 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2299 break;
2300#endif
2301#if defined(MBEDTLS_MD4_C)
2302 case PSA_ALG_MD4:
2303 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2304 break;
2305#endif
2306#if defined(MBEDTLS_MD5_C)
2307 case PSA_ALG_MD5:
2308 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2309 break;
2310#endif
2311#if defined(MBEDTLS_RIPEMD160_C)
2312 case PSA_ALG_RIPEMD160:
2313 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2314 break;
2315#endif
2316#if defined(MBEDTLS_SHA1_C)
2317 case PSA_ALG_SHA_1:
2318 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2319 break;
2320#endif
2321#if defined(MBEDTLS_SHA256_C)
2322 case PSA_ALG_SHA_224:
2323 case PSA_ALG_SHA_256:
2324 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2325 break;
2326#endif
2327#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002328#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002329 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002330#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002331 case PSA_ALG_SHA_512:
2332 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2333 break;
2334#endif
2335 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002336 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002337 }
itayzafrir40835d42018-08-02 13:14:17 +03002338 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002339
itayzafrir40835d42018-08-02 13:14:17 +03002340exit:
2341 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002342 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002343 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002344 return( psa_hash_abort( operation ) );
2345 }
2346 else
2347 {
2348 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002349 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002350 }
2351}
2352
Gilles Peskine2d277862018-06-18 15:41:12 +02002353psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2354 const uint8_t *hash,
2355 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002356{
2357 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2358 size_t actual_hash_length;
2359 psa_status_t status = psa_hash_finish( operation,
2360 actual_hash, sizeof( actual_hash ),
2361 &actual_hash_length );
2362 if( status != PSA_SUCCESS )
2363 return( status );
2364 if( actual_hash_length != hash_length )
2365 return( PSA_ERROR_INVALID_SIGNATURE );
2366 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2367 return( PSA_ERROR_INVALID_SIGNATURE );
2368 return( PSA_SUCCESS );
2369}
2370
Gilles Peskine0a749c82019-11-28 19:33:58 +01002371psa_status_t psa_hash_compute( psa_algorithm_t alg,
2372 const uint8_t *input, size_t input_length,
2373 uint8_t *hash, size_t hash_size,
2374 size_t *hash_length )
2375{
2376 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2377 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2378
2379 *hash_length = hash_size;
2380 status = psa_hash_setup( &operation, alg );
2381 if( status != PSA_SUCCESS )
2382 goto exit;
2383 status = psa_hash_update( &operation, input, input_length );
2384 if( status != PSA_SUCCESS )
2385 goto exit;
2386 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2387 if( status != PSA_SUCCESS )
2388 goto exit;
2389
2390exit:
2391 if( status == PSA_SUCCESS )
2392 status = psa_hash_abort( &operation );
2393 else
2394 psa_hash_abort( &operation );
2395 return( status );
2396}
2397
2398psa_status_t psa_hash_compare( psa_algorithm_t alg,
2399 const uint8_t *input, size_t input_length,
2400 const uint8_t *hash, size_t hash_length )
2401{
2402 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2403 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2404
2405 status = psa_hash_setup( &operation, alg );
2406 if( status != PSA_SUCCESS )
2407 goto exit;
2408 status = psa_hash_update( &operation, input, input_length );
2409 if( status != PSA_SUCCESS )
2410 goto exit;
2411 status = psa_hash_verify( &operation, hash, hash_length );
2412 if( status != PSA_SUCCESS )
2413 goto exit;
2414
2415exit:
2416 if( status == PSA_SUCCESS )
2417 status = psa_hash_abort( &operation );
2418 else
2419 psa_hash_abort( &operation );
2420 return( status );
2421}
2422
Gilles Peskineeb35d782019-01-22 17:56:16 +01002423psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2424 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002425{
2426 if( target_operation->alg != 0 )
2427 return( PSA_ERROR_BAD_STATE );
2428
2429 switch( source_operation->alg )
2430 {
2431 case 0:
2432 return( PSA_ERROR_BAD_STATE );
2433#if defined(MBEDTLS_MD2_C)
2434 case PSA_ALG_MD2:
2435 mbedtls_md2_clone( &target_operation->ctx.md2,
2436 &source_operation->ctx.md2 );
2437 break;
2438#endif
2439#if defined(MBEDTLS_MD4_C)
2440 case PSA_ALG_MD4:
2441 mbedtls_md4_clone( &target_operation->ctx.md4,
2442 &source_operation->ctx.md4 );
2443 break;
2444#endif
2445#if defined(MBEDTLS_MD5_C)
2446 case PSA_ALG_MD5:
2447 mbedtls_md5_clone( &target_operation->ctx.md5,
2448 &source_operation->ctx.md5 );
2449 break;
2450#endif
2451#if defined(MBEDTLS_RIPEMD160_C)
2452 case PSA_ALG_RIPEMD160:
2453 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2454 &source_operation->ctx.ripemd160 );
2455 break;
2456#endif
2457#if defined(MBEDTLS_SHA1_C)
2458 case PSA_ALG_SHA_1:
2459 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2460 &source_operation->ctx.sha1 );
2461 break;
2462#endif
2463#if defined(MBEDTLS_SHA256_C)
2464 case PSA_ALG_SHA_224:
2465 case PSA_ALG_SHA_256:
2466 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2467 &source_operation->ctx.sha256 );
2468 break;
2469#endif
2470#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002471#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002472 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002473#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002474 case PSA_ALG_SHA_512:
2475 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2476 &source_operation->ctx.sha512 );
2477 break;
2478#endif
2479 default:
2480 return( PSA_ERROR_NOT_SUPPORTED );
2481 }
2482
2483 target_operation->alg = source_operation->alg;
2484 return( PSA_SUCCESS );
2485}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002486
2487
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002488/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002489/* MAC */
2490/****************************************************************/
2491
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002492static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002493 psa_algorithm_t alg,
2494 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002495 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002496 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002497{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002498 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002499 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002500
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002501 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002502 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002503
Gilles Peskine8c9def32018-02-08 10:02:12 +01002504 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2505 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002506 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002507 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002508 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002509 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002510 mode = MBEDTLS_MODE_STREAM;
2511 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002512 case PSA_ALG_CTR:
2513 mode = MBEDTLS_MODE_CTR;
2514 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002515 case PSA_ALG_CFB:
2516 mode = MBEDTLS_MODE_CFB;
2517 break;
2518 case PSA_ALG_OFB:
2519 mode = MBEDTLS_MODE_OFB;
2520 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002521 case PSA_ALG_ECB_NO_PADDING:
2522 mode = MBEDTLS_MODE_ECB;
2523 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002524 case PSA_ALG_CBC_NO_PADDING:
2525 mode = MBEDTLS_MODE_CBC;
2526 break;
2527 case PSA_ALG_CBC_PKCS7:
2528 mode = MBEDTLS_MODE_CBC;
2529 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002530 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002531 mode = MBEDTLS_MODE_CCM;
2532 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002533 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002534 mode = MBEDTLS_MODE_GCM;
2535 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002536 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2537 mode = MBEDTLS_MODE_CHACHAPOLY;
2538 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002539 default:
2540 return( NULL );
2541 }
2542 }
2543 else if( alg == PSA_ALG_CMAC )
2544 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002545 else
2546 return( NULL );
2547
2548 switch( key_type )
2549 {
2550 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002551 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002552 break;
2553 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002554 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2555 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002556 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002557 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002558 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002559 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002560 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2561 * but two-key Triple-DES is functionally three-key Triple-DES
2562 * with K1=K3, so that's how we present it to mbedtls. */
2563 if( key_bits == 128 )
2564 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002565 break;
2566 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002567 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002568 break;
2569 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002570 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002571 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002572 case PSA_KEY_TYPE_CHACHA20:
2573 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2574 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002575 default:
2576 return( NULL );
2577 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002578 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002579 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002580
Jaeden Amero23bbb752018-06-26 14:16:54 +01002581 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2582 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002583}
2584
Gilles Peskinea05219c2018-11-16 16:02:56 +01002585#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002586static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002587{
Gilles Peskine2d277862018-06-18 15:41:12 +02002588 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002589 {
2590 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002591 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002592 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002593 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002594 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002595 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002596 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002597 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002598 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002599 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002600 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002601 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002602 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002603 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002604 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002605 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002606 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002607 return( 128 );
2608 default:
2609 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002610 }
2611}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002612#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002613
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002614/* Initialize the MAC operation structure. Once this function has been
2615 * called, psa_mac_abort can run and will do the right thing. */
2616static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2617 psa_algorithm_t alg )
2618{
2619 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2620
2621 operation->alg = alg;
2622 operation->key_set = 0;
2623 operation->iv_set = 0;
2624 operation->iv_required = 0;
2625 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002626 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002627
2628#if defined(MBEDTLS_CMAC_C)
2629 if( alg == PSA_ALG_CMAC )
2630 {
2631 operation->iv_required = 0;
2632 mbedtls_cipher_init( &operation->ctx.cmac );
2633 status = PSA_SUCCESS;
2634 }
2635 else
2636#endif /* MBEDTLS_CMAC_C */
2637#if defined(MBEDTLS_MD_C)
2638 if( PSA_ALG_IS_HMAC( operation->alg ) )
2639 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002640 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2641 operation->ctx.hmac.hash_ctx.alg = 0;
2642 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002643 }
2644 else
2645#endif /* MBEDTLS_MD_C */
2646 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002647 if( ! PSA_ALG_IS_MAC( alg ) )
2648 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002649 }
2650
2651 if( status != PSA_SUCCESS )
2652 memset( operation, 0, sizeof( *operation ) );
2653 return( status );
2654}
2655
Gilles Peskine01126fa2018-07-12 17:04:55 +02002656#if defined(MBEDTLS_MD_C)
2657static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2658{
Gilles Peskine3f108122018-12-07 18:14:53 +01002659 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002660 return( psa_hash_abort( &hmac->hash_ctx ) );
2661}
2662#endif /* MBEDTLS_MD_C */
2663
Gilles Peskine8c9def32018-02-08 10:02:12 +01002664psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2665{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002666 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002667 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002668 /* The object has (apparently) been initialized but it is not
2669 * in use. It's ok to call abort on such an object, and there's
2670 * nothing to do. */
2671 return( PSA_SUCCESS );
2672 }
2673 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002674#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002675 if( operation->alg == PSA_ALG_CMAC )
2676 {
2677 mbedtls_cipher_free( &operation->ctx.cmac );
2678 }
2679 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002680#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002681#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002682 if( PSA_ALG_IS_HMAC( operation->alg ) )
2683 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002684 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002685 }
2686 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002687#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002688 {
2689 /* Sanity check (shouldn't happen: operation->alg should
2690 * always have been initialized to a valid value). */
2691 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002692 }
Moran Peker41deec42018-04-04 15:43:05 +03002693
Gilles Peskine8c9def32018-02-08 10:02:12 +01002694 operation->alg = 0;
2695 operation->key_set = 0;
2696 operation->iv_set = 0;
2697 operation->iv_required = 0;
2698 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002699 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002700
Gilles Peskine8c9def32018-02-08 10:02:12 +01002701 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002702
2703bad_state:
2704 /* If abort is called on an uninitialized object, we can't trust
2705 * anything. Wipe the object in case it contains confidential data.
2706 * This may result in a memory leak if a pointer gets overwritten,
2707 * but it's too late to do anything about this. */
2708 memset( operation, 0, sizeof( *operation ) );
2709 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002710}
2711
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002712#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002713static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002714 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002715 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002716 const mbedtls_cipher_info_t *cipher_info )
2717{
Janos Follath24eed8d2019-11-22 13:21:35 +00002718 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002719
2720 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002721
2722 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2723 if( ret != 0 )
2724 return( ret );
2725
2726 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2727 slot->data.raw.data,
2728 key_bits );
2729 return( ret );
2730}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002731#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002732
Gilles Peskine248051a2018-06-20 16:09:38 +02002733#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002734static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2735 const uint8_t *key,
2736 size_t key_length,
2737 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002738{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002739 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002740 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002741 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002742 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002743 psa_status_t status;
2744
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002745 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2746 * overflow below. This should never trigger if the hash algorithm
2747 * is implemented correctly. */
2748 /* The size checks against the ipad and opad buffers cannot be written
2749 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2750 * because that triggers -Wlogical-op on GCC 7.3. */
2751 if( block_size > sizeof( ipad ) )
2752 return( PSA_ERROR_NOT_SUPPORTED );
2753 if( block_size > sizeof( hmac->opad ) )
2754 return( PSA_ERROR_NOT_SUPPORTED );
2755 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002756 return( PSA_ERROR_NOT_SUPPORTED );
2757
Gilles Peskined223b522018-06-11 18:12:58 +02002758 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002759 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002760 status = psa_hash_compute( hash_alg, key, key_length,
2761 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002762 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002763 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002764 }
Gilles Peskine96889972018-07-12 17:07:03 +02002765 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2766 * but it is permitted. It is common when HMAC is used in HKDF, for
2767 * example. Don't call `memcpy` in the 0-length because `key` could be
2768 * an invalid pointer which would make the behavior undefined. */
2769 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002770 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002771
Gilles Peskined223b522018-06-11 18:12:58 +02002772 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2773 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002774 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002775 ipad[i] ^= 0x36;
2776 memset( ipad + key_length, 0x36, block_size - key_length );
2777
2778 /* Copy the key material from ipad to opad, flipping the requisite bits,
2779 * and filling the rest of opad with the requisite constant. */
2780 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002781 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2782 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002783
Gilles Peskine01126fa2018-07-12 17:04:55 +02002784 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002785 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002786 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002787
Gilles Peskine01126fa2018-07-12 17:04:55 +02002788 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002789
2790cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002791 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002792
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002793 return( status );
2794}
Gilles Peskine248051a2018-06-20 16:09:38 +02002795#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002796
Gilles Peskine89167cb2018-07-08 20:12:23 +02002797static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002798 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002799 psa_algorithm_t alg,
2800 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002801{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002802 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002803 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002804 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002805 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002806 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002807 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002808 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002809
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002810 /* A context must be freshly initialized before it can be set up. */
2811 if( operation->alg != 0 )
2812 {
2813 return( PSA_ERROR_BAD_STATE );
2814 }
2815
Gilles Peskined911eb72018-08-14 15:18:45 +02002816 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002817 if( status != PSA_SUCCESS )
2818 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002819 if( is_sign )
2820 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002821
Gilles Peskine28f8f302019-07-24 13:30:31 +02002822 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002823 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002824 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002825 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002826
Gilles Peskine8c9def32018-02-08 10:02:12 +01002827#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002828 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002829 {
2830 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002831 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002832 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002833 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002834 if( cipher_info == NULL )
2835 {
2836 status = PSA_ERROR_NOT_SUPPORTED;
2837 goto exit;
2838 }
2839 operation->mac_size = cipher_info->block_size;
2840 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2841 status = mbedtls_to_psa_error( ret );
2842 }
2843 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002844#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002845#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002846 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002847 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002848 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002849 if( hash_alg == 0 )
2850 {
2851 status = PSA_ERROR_NOT_SUPPORTED;
2852 goto exit;
2853 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002854
2855 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2856 /* Sanity check. This shouldn't fail on a valid configuration. */
2857 if( operation->mac_size == 0 ||
2858 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2859 {
2860 status = PSA_ERROR_NOT_SUPPORTED;
2861 goto exit;
2862 }
2863
Gilles Peskine8e338702019-07-30 20:06:31 +02002864 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002865 {
2866 status = PSA_ERROR_INVALID_ARGUMENT;
2867 goto exit;
2868 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002869
Gilles Peskine01126fa2018-07-12 17:04:55 +02002870 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2871 slot->data.raw.data,
2872 slot->data.raw.bytes,
2873 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002874 }
2875 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002876#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002877 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002878 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002879 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002880 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002881
Gilles Peskined911eb72018-08-14 15:18:45 +02002882 if( truncated == 0 )
2883 {
2884 /* The "normal" case: untruncated algorithm. Nothing to do. */
2885 }
2886 else if( truncated < 4 )
2887 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002888 /* A very short MAC is too short for security since it can be
2889 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2890 * so we make this our minimum, even though 32 bits is still
2891 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002892 status = PSA_ERROR_NOT_SUPPORTED;
2893 }
2894 else if( truncated > operation->mac_size )
2895 {
2896 /* It's impossible to "truncate" to a larger length. */
2897 status = PSA_ERROR_INVALID_ARGUMENT;
2898 }
2899 else
2900 operation->mac_size = truncated;
2901
Gilles Peskinefbfac682018-07-08 20:51:54 +02002902exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002903 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002904 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002905 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002906 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002907 else
2908 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002909 operation->key_set = 1;
2910 }
2911 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002912}
2913
Gilles Peskine89167cb2018-07-08 20:12:23 +02002914psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002915 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002916 psa_algorithm_t alg )
2917{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002918 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002919}
2920
2921psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002922 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002923 psa_algorithm_t alg )
2924{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002925 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002926}
2927
Gilles Peskine8c9def32018-02-08 10:02:12 +01002928psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2929 const uint8_t *input,
2930 size_t input_length )
2931{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002932 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002933 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002934 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002935 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002936 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002937 operation->has_input = 1;
2938
Gilles Peskine8c9def32018-02-08 10:02:12 +01002939#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002940 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002941 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002942 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2943 input, input_length );
2944 status = mbedtls_to_psa_error( ret );
2945 }
2946 else
2947#endif /* MBEDTLS_CMAC_C */
2948#if defined(MBEDTLS_MD_C)
2949 if( PSA_ALG_IS_HMAC( operation->alg ) )
2950 {
2951 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2952 input_length );
2953 }
2954 else
2955#endif /* MBEDTLS_MD_C */
2956 {
2957 /* This shouldn't happen if `operation` was initialized by
2958 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002959 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002960 }
2961
Gilles Peskinefbfac682018-07-08 20:51:54 +02002962 if( status != PSA_SUCCESS )
2963 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002964 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002965}
2966
Gilles Peskine01126fa2018-07-12 17:04:55 +02002967#if defined(MBEDTLS_MD_C)
2968static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2969 uint8_t *mac,
2970 size_t mac_size )
2971{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002972 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002973 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2974 size_t hash_size = 0;
2975 size_t block_size = psa_get_hash_block_size( hash_alg );
2976 psa_status_t status;
2977
Gilles Peskine01126fa2018-07-12 17:04:55 +02002978 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2979 if( status != PSA_SUCCESS )
2980 return( status );
2981 /* From here on, tmp needs to be wiped. */
2982
2983 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2984 if( status != PSA_SUCCESS )
2985 goto exit;
2986
2987 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2988 if( status != PSA_SUCCESS )
2989 goto exit;
2990
2991 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2992 if( status != PSA_SUCCESS )
2993 goto exit;
2994
Gilles Peskined911eb72018-08-14 15:18:45 +02002995 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2996 if( status != PSA_SUCCESS )
2997 goto exit;
2998
2999 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003000
3001exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003002 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003003 return( status );
3004}
3005#endif /* MBEDTLS_MD_C */
3006
mohammad16036df908f2018-04-02 08:34:15 -07003007static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003008 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003009 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003010{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003011 if( ! operation->key_set )
3012 return( PSA_ERROR_BAD_STATE );
3013 if( operation->iv_required && ! operation->iv_set )
3014 return( PSA_ERROR_BAD_STATE );
3015
Gilles Peskine8c9def32018-02-08 10:02:12 +01003016 if( mac_size < operation->mac_size )
3017 return( PSA_ERROR_BUFFER_TOO_SMALL );
3018
Gilles Peskine8c9def32018-02-08 10:02:12 +01003019#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003020 if( operation->alg == PSA_ALG_CMAC )
3021 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003022 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3023 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3024 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003025 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003026 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003027 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003028 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003029 else
3030#endif /* MBEDTLS_CMAC_C */
3031#if defined(MBEDTLS_MD_C)
3032 if( PSA_ALG_IS_HMAC( operation->alg ) )
3033 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003034 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003035 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003036 }
3037 else
3038#endif /* MBEDTLS_MD_C */
3039 {
3040 /* This shouldn't happen if `operation` was initialized by
3041 * a setup function. */
3042 return( PSA_ERROR_BAD_STATE );
3043 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003044}
3045
Gilles Peskineacd4be32018-07-08 19:56:25 +02003046psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3047 uint8_t *mac,
3048 size_t mac_size,
3049 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003050{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003051 psa_status_t status;
3052
Jaeden Amero252ef282019-02-15 14:05:35 +00003053 if( operation->alg == 0 )
3054 {
3055 return( PSA_ERROR_BAD_STATE );
3056 }
3057
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003058 /* Fill the output buffer with something that isn't a valid mac
3059 * (barring an attack on the mac and deliberately-crafted input),
3060 * in case the caller doesn't check the return status properly. */
3061 *mac_length = mac_size;
3062 /* If mac_size is 0 then mac may be NULL and then the
3063 * call to memset would have undefined behavior. */
3064 if( mac_size != 0 )
3065 memset( mac, '!', mac_size );
3066
Gilles Peskine89167cb2018-07-08 20:12:23 +02003067 if( ! operation->is_sign )
3068 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003069 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003070 }
mohammad16036df908f2018-04-02 08:34:15 -07003071
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003072 status = psa_mac_finish_internal( operation, mac, mac_size );
3073
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003074 if( status == PSA_SUCCESS )
3075 {
3076 status = psa_mac_abort( operation );
3077 if( status == PSA_SUCCESS )
3078 *mac_length = operation->mac_size;
3079 else
3080 memset( mac, '!', mac_size );
3081 }
3082 else
3083 psa_mac_abort( operation );
3084 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003085}
3086
Gilles Peskineacd4be32018-07-08 19:56:25 +02003087psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3088 const uint8_t *mac,
3089 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003090{
Gilles Peskine828ed142018-06-18 23:25:51 +02003091 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003092 psa_status_t status;
3093
Jaeden Amero252ef282019-02-15 14:05:35 +00003094 if( operation->alg == 0 )
3095 {
3096 return( PSA_ERROR_BAD_STATE );
3097 }
3098
Gilles Peskine89167cb2018-07-08 20:12:23 +02003099 if( operation->is_sign )
3100 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003101 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003102 }
3103 if( operation->mac_size != mac_length )
3104 {
3105 status = PSA_ERROR_INVALID_SIGNATURE;
3106 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003107 }
mohammad16036df908f2018-04-02 08:34:15 -07003108
3109 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003110 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003111 if( status != PSA_SUCCESS )
3112 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003113
3114 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3115 status = PSA_ERROR_INVALID_SIGNATURE;
3116
3117cleanup:
3118 if( status == PSA_SUCCESS )
3119 status = psa_mac_abort( operation );
3120 else
3121 psa_mac_abort( operation );
3122
Gilles Peskine3f108122018-12-07 18:14:53 +01003123 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003124
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003125 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003126}
3127
3128
Gilles Peskine20035e32018-02-03 22:44:14 +01003129
Gilles Peskine20035e32018-02-03 22:44:14 +01003130/****************************************************************/
3131/* Asymmetric cryptography */
3132/****************************************************************/
3133
Gilles Peskine2b450e32018-06-27 15:42:46 +02003134#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003135/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003136 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003137static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3138 size_t hash_length,
3139 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003140{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003141 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003142 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003143 *md_alg = mbedtls_md_get_type( md_info );
3144
3145 /* The Mbed TLS RSA module uses an unsigned int for hash length
3146 * parameters. Validate that it fits so that we don't risk an
3147 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003148#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003149 if( hash_length > UINT_MAX )
3150 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003151#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003152
3153#if defined(MBEDTLS_PKCS1_V15)
3154 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3155 * must be correct. */
3156 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3157 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003158 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003159 if( md_info == NULL )
3160 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003161 if( mbedtls_md_get_size( md_info ) != hash_length )
3162 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003163 }
3164#endif /* MBEDTLS_PKCS1_V15 */
3165
3166#if defined(MBEDTLS_PKCS1_V21)
3167 /* PSS requires a hash internally. */
3168 if( PSA_ALG_IS_RSA_PSS( alg ) )
3169 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003170 if( md_info == NULL )
3171 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003172 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003173#endif /* MBEDTLS_PKCS1_V21 */
3174
Gilles Peskine61b91d42018-06-08 16:09:36 +02003175 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003176}
3177
Gilles Peskine2b450e32018-06-27 15:42:46 +02003178static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3179 psa_algorithm_t alg,
3180 const uint8_t *hash,
3181 size_t hash_length,
3182 uint8_t *signature,
3183 size_t signature_size,
3184 size_t *signature_length )
3185{
3186 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003187 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003188 mbedtls_md_type_t md_alg;
3189
3190 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3191 if( status != PSA_SUCCESS )
3192 return( status );
3193
Gilles Peskine630a18a2018-06-29 17:49:35 +02003194 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003195 return( PSA_ERROR_BUFFER_TOO_SMALL );
3196
3197#if defined(MBEDTLS_PKCS1_V15)
3198 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3199 {
3200 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3201 MBEDTLS_MD_NONE );
3202 ret = mbedtls_rsa_pkcs1_sign( rsa,
3203 mbedtls_ctr_drbg_random,
3204 &global_data.ctr_drbg,
3205 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003206 md_alg,
3207 (unsigned int) hash_length,
3208 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003209 signature );
3210 }
3211 else
3212#endif /* MBEDTLS_PKCS1_V15 */
3213#if defined(MBEDTLS_PKCS1_V21)
3214 if( PSA_ALG_IS_RSA_PSS( alg ) )
3215 {
3216 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3217 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3218 mbedtls_ctr_drbg_random,
3219 &global_data.ctr_drbg,
3220 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003221 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003222 (unsigned int) hash_length,
3223 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003224 signature );
3225 }
3226 else
3227#endif /* MBEDTLS_PKCS1_V21 */
3228 {
3229 return( PSA_ERROR_INVALID_ARGUMENT );
3230 }
3231
3232 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003233 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003234 return( mbedtls_to_psa_error( ret ) );
3235}
3236
3237static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3238 psa_algorithm_t alg,
3239 const uint8_t *hash,
3240 size_t hash_length,
3241 const uint8_t *signature,
3242 size_t signature_length )
3243{
3244 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003245 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003246 mbedtls_md_type_t md_alg;
3247
3248 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3249 if( status != PSA_SUCCESS )
3250 return( status );
3251
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003252 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3253 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003254
3255#if defined(MBEDTLS_PKCS1_V15)
3256 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3257 {
3258 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3259 MBEDTLS_MD_NONE );
3260 ret = mbedtls_rsa_pkcs1_verify( rsa,
3261 mbedtls_ctr_drbg_random,
3262 &global_data.ctr_drbg,
3263 MBEDTLS_RSA_PUBLIC,
3264 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003265 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003266 hash,
3267 signature );
3268 }
3269 else
3270#endif /* MBEDTLS_PKCS1_V15 */
3271#if defined(MBEDTLS_PKCS1_V21)
3272 if( PSA_ALG_IS_RSA_PSS( alg ) )
3273 {
3274 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3275 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3276 mbedtls_ctr_drbg_random,
3277 &global_data.ctr_drbg,
3278 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003279 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003280 (unsigned int) hash_length,
3281 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003282 signature );
3283 }
3284 else
3285#endif /* MBEDTLS_PKCS1_V21 */
3286 {
3287 return( PSA_ERROR_INVALID_ARGUMENT );
3288 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003289
3290 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3291 * the rest of the signature is invalid". This has little use in
3292 * practice and PSA doesn't report this distinction. */
3293 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3294 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003295 return( mbedtls_to_psa_error( ret ) );
3296}
3297#endif /* MBEDTLS_RSA_C */
3298
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003299#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003300/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3301 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3302 * (even though these functions don't modify it). */
3303static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3304 psa_algorithm_t alg,
3305 const uint8_t *hash,
3306 size_t hash_length,
3307 uint8_t *signature,
3308 size_t signature_size,
3309 size_t *signature_length )
3310{
Janos Follath24eed8d2019-11-22 13:21:35 +00003311 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003312 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003313 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003314 mbedtls_mpi_init( &r );
3315 mbedtls_mpi_init( &s );
3316
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003317 if( signature_size < 2 * curve_bytes )
3318 {
3319 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3320 goto cleanup;
3321 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003322
Gilles Peskinea05219c2018-11-16 16:02:56 +01003323#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003324 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3325 {
3326 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3327 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3328 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003329 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3330 &ecp->d, hash,
3331 hash_length, md_alg,
3332 mbedtls_ctr_drbg_random,
3333 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003334 }
3335 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003336#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003337 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003338 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003339 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3340 hash, hash_length,
3341 mbedtls_ctr_drbg_random,
3342 &global_data.ctr_drbg ) );
3343 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003344
3345 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3346 signature,
3347 curve_bytes ) );
3348 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3349 signature + curve_bytes,
3350 curve_bytes ) );
3351
3352cleanup:
3353 mbedtls_mpi_free( &r );
3354 mbedtls_mpi_free( &s );
3355 if( ret == 0 )
3356 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003357 return( mbedtls_to_psa_error( ret ) );
3358}
3359
3360static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3361 const uint8_t *hash,
3362 size_t hash_length,
3363 const uint8_t *signature,
3364 size_t signature_length )
3365{
Janos Follath24eed8d2019-11-22 13:21:35 +00003366 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003367 mbedtls_mpi r, s;
3368 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3369 mbedtls_mpi_init( &r );
3370 mbedtls_mpi_init( &s );
3371
3372 if( signature_length != 2 * curve_bytes )
3373 return( PSA_ERROR_INVALID_SIGNATURE );
3374
3375 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3376 signature,
3377 curve_bytes ) );
3378 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3379 signature + curve_bytes,
3380 curve_bytes ) );
3381
3382 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3383 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003384
3385cleanup:
3386 mbedtls_mpi_free( &r );
3387 mbedtls_mpi_free( &s );
3388 return( mbedtls_to_psa_error( ret ) );
3389}
3390#endif /* MBEDTLS_ECDSA_C */
3391
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003392psa_status_t psa_sign_hash( psa_key_handle_t handle,
3393 psa_algorithm_t alg,
3394 const uint8_t *hash,
3395 size_t hash_length,
3396 uint8_t *signature,
3397 size_t signature_size,
3398 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003399{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003400 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003401 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003402#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3403 const psa_drv_se_t *drv;
3404 psa_drv_se_context_t *drv_context;
3405#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003406
3407 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003408 /* Immediately reject a zero-length signature buffer. This guarantees
3409 * that signature must be a valid pointer. (On the other hand, the hash
3410 * buffer can in principle be empty since it doesn't actually have
3411 * to be a hash.) */
3412 if( signature_size == 0 )
3413 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003414
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003415 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003416 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003417 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003418 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003419 {
3420 status = PSA_ERROR_INVALID_ARGUMENT;
3421 goto exit;
3422 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003423
Gilles Peskineedc64242019-08-07 21:05:07 +02003424#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3425 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3426 {
3427 if( drv->asymmetric == NULL ||
3428 drv->asymmetric->p_sign == NULL )
3429 {
3430 status = PSA_ERROR_NOT_SUPPORTED;
3431 goto exit;
3432 }
3433 status = drv->asymmetric->p_sign( drv_context,
3434 slot->data.se.slot_number,
3435 alg,
3436 hash, hash_length,
3437 signature, signature_size,
3438 signature_length );
3439 }
3440 else
3441#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003442#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003443 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003444 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003445 status = psa_rsa_sign( slot->data.rsa,
3446 alg,
3447 hash, hash_length,
3448 signature, signature_size,
3449 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003450 }
3451 else
3452#endif /* defined(MBEDTLS_RSA_C) */
3453#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003454 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003455 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003456#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003457 if(
3458#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3459 PSA_ALG_IS_ECDSA( alg )
3460#else
3461 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3462#endif
3463 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003464 status = psa_ecdsa_sign( slot->data.ecp,
3465 alg,
3466 hash, hash_length,
3467 signature, signature_size,
3468 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003469 else
3470#endif /* defined(MBEDTLS_ECDSA_C) */
3471 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003472 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003473 }
itayzafrir5c753392018-05-08 11:18:38 +03003474 }
3475 else
3476#endif /* defined(MBEDTLS_ECP_C) */
3477 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003478 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003479 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003480
3481exit:
3482 /* Fill the unused part of the output buffer (the whole buffer on error,
3483 * the trailing part on success) with something that isn't a valid mac
3484 * (barring an attack on the mac and deliberately-crafted input),
3485 * in case the caller doesn't check the return status properly. */
3486 if( status == PSA_SUCCESS )
3487 memset( signature + *signature_length, '!',
3488 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003489 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003490 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003491 /* If signature_size is 0 then we have nothing to do. We must not call
3492 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003493 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003494}
3495
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003496psa_status_t psa_verify_hash( psa_key_handle_t handle,
3497 psa_algorithm_t alg,
3498 const uint8_t *hash,
3499 size_t hash_length,
3500 const uint8_t *signature,
3501 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003502{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003503 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003504 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003505#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3506 const psa_drv_se_t *drv;
3507 psa_drv_se_context_t *drv_context;
3508#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003509
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003510 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003511 if( status != PSA_SUCCESS )
3512 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003513
Gilles Peskineedc64242019-08-07 21:05:07 +02003514#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3515 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3516 {
3517 if( drv->asymmetric == NULL ||
3518 drv->asymmetric->p_verify == NULL )
3519 return( PSA_ERROR_NOT_SUPPORTED );
3520 return( drv->asymmetric->p_verify( drv_context,
3521 slot->data.se.slot_number,
3522 alg,
3523 hash, hash_length,
3524 signature, signature_length ) );
3525 }
3526 else
3527#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003528#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003529 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003530 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003531 return( psa_rsa_verify( slot->data.rsa,
3532 alg,
3533 hash, hash_length,
3534 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003535 }
3536 else
3537#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003538#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003539 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003540 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003541#if defined(MBEDTLS_ECDSA_C)
3542 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003543 return( psa_ecdsa_verify( slot->data.ecp,
3544 hash, hash_length,
3545 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003546 else
3547#endif /* defined(MBEDTLS_ECDSA_C) */
3548 {
3549 return( PSA_ERROR_INVALID_ARGUMENT );
3550 }
itayzafrir5c753392018-05-08 11:18:38 +03003551 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003552 else
3553#endif /* defined(MBEDTLS_ECP_C) */
3554 {
3555 return( PSA_ERROR_NOT_SUPPORTED );
3556 }
3557}
3558
Gilles Peskine072ac562018-06-30 00:21:29 +02003559#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3560static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3561 mbedtls_rsa_context *rsa )
3562{
3563 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3564 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3565 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3566 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3567}
3568#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3569
Gilles Peskinec5487a82018-12-03 18:08:14 +01003570psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003571 psa_algorithm_t alg,
3572 const uint8_t *input,
3573 size_t input_length,
3574 const uint8_t *salt,
3575 size_t salt_length,
3576 uint8_t *output,
3577 size_t output_size,
3578 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003579{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003580 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003581 psa_status_t status;
3582
Darryl Green5cc689a2018-07-24 15:34:10 +01003583 (void) input;
3584 (void) input_length;
3585 (void) salt;
3586 (void) output;
3587 (void) output_size;
3588
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003589 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003590
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003591 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3592 return( PSA_ERROR_INVALID_ARGUMENT );
3593
Gilles Peskine28f8f302019-07-24 13:30:31 +02003594 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003595 if( status != PSA_SUCCESS )
3596 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003597 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3598 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003599 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003600
3601#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003602 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003603 {
3604 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003605 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003606 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003607 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003608#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003609 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003610 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003611 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3612 mbedtls_ctr_drbg_random,
3613 &global_data.ctr_drbg,
3614 MBEDTLS_RSA_PUBLIC,
3615 input_length,
3616 input,
3617 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003618 }
3619 else
3620#endif /* MBEDTLS_PKCS1_V15 */
3621#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003622 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003623 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003624 psa_rsa_oaep_set_padding_mode( alg, rsa );
3625 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3626 mbedtls_ctr_drbg_random,
3627 &global_data.ctr_drbg,
3628 MBEDTLS_RSA_PUBLIC,
3629 salt, salt_length,
3630 input_length,
3631 input,
3632 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003633 }
3634 else
3635#endif /* MBEDTLS_PKCS1_V21 */
3636 {
3637 return( PSA_ERROR_INVALID_ARGUMENT );
3638 }
3639 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003640 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003641 return( mbedtls_to_psa_error( ret ) );
3642 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003643 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003644#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003645 {
3646 return( PSA_ERROR_NOT_SUPPORTED );
3647 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003648}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003649
Gilles Peskinec5487a82018-12-03 18:08:14 +01003650psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003651 psa_algorithm_t alg,
3652 const uint8_t *input,
3653 size_t input_length,
3654 const uint8_t *salt,
3655 size_t salt_length,
3656 uint8_t *output,
3657 size_t output_size,
3658 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003659{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003660 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003661 psa_status_t status;
3662
Darryl Green5cc689a2018-07-24 15:34:10 +01003663 (void) input;
3664 (void) input_length;
3665 (void) salt;
3666 (void) output;
3667 (void) output_size;
3668
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003669 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003670
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003671 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3672 return( PSA_ERROR_INVALID_ARGUMENT );
3673
Gilles Peskine28f8f302019-07-24 13:30:31 +02003674 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003675 if( status != PSA_SUCCESS )
3676 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003677 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003678 return( PSA_ERROR_INVALID_ARGUMENT );
3679
3680#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003681 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003682 {
3683 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003684 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003685
Gilles Peskine630a18a2018-06-29 17:49:35 +02003686 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003687 return( PSA_ERROR_INVALID_ARGUMENT );
3688
3689#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003690 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003691 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003692 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3693 mbedtls_ctr_drbg_random,
3694 &global_data.ctr_drbg,
3695 MBEDTLS_RSA_PRIVATE,
3696 output_length,
3697 input,
3698 output,
3699 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003700 }
3701 else
3702#endif /* MBEDTLS_PKCS1_V15 */
3703#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003704 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003705 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003706 psa_rsa_oaep_set_padding_mode( alg, rsa );
3707 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3708 mbedtls_ctr_drbg_random,
3709 &global_data.ctr_drbg,
3710 MBEDTLS_RSA_PRIVATE,
3711 salt, salt_length,
3712 output_length,
3713 input,
3714 output,
3715 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003716 }
3717 else
3718#endif /* MBEDTLS_PKCS1_V21 */
3719 {
3720 return( PSA_ERROR_INVALID_ARGUMENT );
3721 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003722
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003723 return( mbedtls_to_psa_error( ret ) );
3724 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003725 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003726#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003727 {
3728 return( PSA_ERROR_NOT_SUPPORTED );
3729 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003730}
Gilles Peskine20035e32018-02-03 22:44:14 +01003731
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003732
3733
mohammad1603503973b2018-03-12 15:59:30 +02003734/****************************************************************/
3735/* Symmetric cryptography */
3736/****************************************************************/
3737
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003738/* Initialize the cipher operation structure. Once this function has been
3739 * called, psa_cipher_abort can run and will do the right thing. */
3740static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3741 psa_algorithm_t alg )
3742{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003743 if( ! PSA_ALG_IS_CIPHER( alg ) )
3744 {
3745 memset( operation, 0, sizeof( *operation ) );
3746 return( PSA_ERROR_INVALID_ARGUMENT );
3747 }
3748
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003749 operation->alg = alg;
3750 operation->key_set = 0;
3751 operation->iv_set = 0;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003752 if( alg == PSA_ALG_ECB_NO_PADDING ) {
3753 operation->iv_required = 0;
3754 } else {
3755 operation->iv_required = 1;
3756 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003757 operation->iv_size = 0;
3758 operation->block_size = 0;
3759 mbedtls_cipher_init( &operation->ctx.cipher );
3760 return( PSA_SUCCESS );
3761}
3762
Gilles Peskinee553c652018-06-04 16:22:46 +02003763static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003764 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003765 psa_algorithm_t alg,
3766 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003767{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003768 int ret = 0;
3769 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003770 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003771 size_t key_bits;
3772 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003773 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3774 PSA_KEY_USAGE_ENCRYPT :
3775 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003776
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003777 /* A context must be freshly initialized before it can be set up. */
3778 if( operation->alg != 0 )
3779 {
3780 return( PSA_ERROR_BAD_STATE );
3781 }
3782
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003783 status = psa_cipher_init( operation, alg );
3784 if( status != PSA_SUCCESS )
3785 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003786
Gilles Peskine28f8f302019-07-24 13:30:31 +02003787 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003788 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003789 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003790 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003791
Gilles Peskine8e338702019-07-30 20:06:31 +02003792 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003793 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003794 {
3795 status = PSA_ERROR_NOT_SUPPORTED;
3796 goto exit;
3797 }
mohammad1603503973b2018-03-12 15:59:30 +02003798
mohammad1603503973b2018-03-12 15:59:30 +02003799 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003800 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003801 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003802
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003803#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003804 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003805 {
3806 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003807 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003808 memcpy( keys, slot->data.raw.data, 16 );
3809 memcpy( keys + 16, slot->data.raw.data, 8 );
3810 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3811 keys,
3812 192, cipher_operation );
3813 }
3814 else
3815#endif
3816 {
3817 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3818 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003819 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003820 }
Moran Peker41deec42018-04-04 15:43:05 +03003821 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003822 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003823
mohammad16038481e742018-03-18 13:57:31 +02003824#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003825 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003826 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003827 case PSA_ALG_CBC_NO_PADDING:
3828 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3829 MBEDTLS_PADDING_NONE );
3830 break;
3831 case PSA_ALG_CBC_PKCS7:
3832 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3833 MBEDTLS_PADDING_PKCS7 );
3834 break;
3835 default:
3836 /* The algorithm doesn't involve padding. */
3837 ret = 0;
3838 break;
3839 }
3840 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003841 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003842#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3843
mohammad1603503973b2018-03-12 15:59:30 +02003844 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003845 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003846 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003847 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG && alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02003848 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003849 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003850 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003851#if defined(MBEDTLS_CHACHA20_C)
3852 else
3853 if( alg == PSA_ALG_CHACHA20 )
3854 operation->iv_size = 12;
3855#endif
mohammad1603503973b2018-03-12 15:59:30 +02003856
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003857exit:
3858 if( status == 0 )
3859 status = mbedtls_to_psa_error( ret );
3860 if( status != 0 )
3861 psa_cipher_abort( operation );
3862 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003863}
3864
Gilles Peskinefe119512018-07-08 21:39:34 +02003865psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003866 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003867 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003868{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003869 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003870}
3871
Gilles Peskinefe119512018-07-08 21:39:34 +02003872psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003873 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003874 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003875{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003876 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003877}
3878
Gilles Peskinefe119512018-07-08 21:39:34 +02003879psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003880 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003881 size_t iv_size,
3882 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003883{
itayzafrir534bd7c2018-08-02 13:56:32 +03003884 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003885 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003886 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003887 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003888 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003889 }
Moran Peker41deec42018-04-04 15:43:05 +03003890 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003891 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003892 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003893 goto exit;
3894 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003895 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3896 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003897 if( ret != 0 )
3898 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003899 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003900 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003901 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003902
mohammad16038481e742018-03-18 13:57:31 +02003903 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003904 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003905
Moran Peker395db872018-05-31 14:07:14 +03003906exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003907 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003908 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003909 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003910}
3911
Gilles Peskinefe119512018-07-08 21:39:34 +02003912psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003913 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003914 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003915{
itayzafrir534bd7c2018-08-02 13:56:32 +03003916 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003917 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003918 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003919 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003920 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003921 }
Moran Pekera28258c2018-05-29 16:25:04 +03003922 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003923 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003924 status = PSA_ERROR_INVALID_ARGUMENT;
3925 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003926 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003927 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3928 status = mbedtls_to_psa_error( ret );
3929exit:
3930 if( status == PSA_SUCCESS )
3931 operation->iv_set = 1;
3932 else
mohammad1603503973b2018-03-12 15:59:30 +02003933 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003934 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003935}
3936
Gilles Peskinee553c652018-06-04 16:22:46 +02003937psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3938 const uint8_t *input,
3939 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003940 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003941 size_t output_size,
3942 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003943{
itayzafrir534bd7c2018-08-02 13:56:32 +03003944 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003945 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003946 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003947
3948 if( operation->alg == 0 )
3949 {
3950 return( PSA_ERROR_BAD_STATE );
3951 }
3952
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003953 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003954 {
3955 /* Take the unprocessed partial block left over from previous
3956 * update calls, if any, plus the input to this call. Remove
3957 * the last partial block, if any. You get the data that will be
3958 * output in this call. */
3959 expected_output_size =
3960 ( operation->ctx.cipher.unprocessed_len + input_length )
3961 / operation->block_size * operation->block_size;
3962 }
3963 else
3964 {
3965 expected_output_size = input_length;
3966 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003967
Gilles Peskine89d789c2018-06-04 17:17:16 +02003968 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003969 {
3970 status = PSA_ERROR_BUFFER_TOO_SMALL;
3971 goto exit;
3972 }
mohammad160382759612018-03-12 18:16:40 +02003973
mohammad1603503973b2018-03-12 15:59:30 +02003974 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003975 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003976 status = mbedtls_to_psa_error( ret );
3977exit:
3978 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003979 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003980 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003981}
3982
Gilles Peskinee553c652018-06-04 16:22:46 +02003983psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3984 uint8_t *output,
3985 size_t output_size,
3986 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003987{
David Saadab4ecc272019-02-14 13:48:10 +02003988 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003989 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003990 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003991
mohammad1603503973b2018-03-12 15:59:30 +02003992 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003993 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003994 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003995 }
3996 if( operation->iv_required && ! operation->iv_set )
3997 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003998 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003999 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004000
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004001 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004002 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004003 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
4004 ( operation->alg == PSA_ALG_CBC_NO_PADDING &&
4005 operation->ctx.cipher.operation == MBEDTLS_ENCRYPT ) ) {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004006 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004007 goto error;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004008 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004009 }
4010
Janos Follath315b51c2018-07-09 16:04:51 +01004011 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4012 temp_output_buffer,
4013 output_length );
4014 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004015 {
Janos Follath315b51c2018-07-09 16:04:51 +01004016 status = mbedtls_to_psa_error( cipher_ret );
4017 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004018 }
Janos Follath315b51c2018-07-09 16:04:51 +01004019
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004020 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004021 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004022 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004023 memcpy( output, temp_output_buffer, *output_length );
4024 else
4025 {
Janos Follath315b51c2018-07-09 16:04:51 +01004026 status = PSA_ERROR_BUFFER_TOO_SMALL;
4027 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004028 }
mohammad1603503973b2018-03-12 15:59:30 +02004029
Gilles Peskine3f108122018-12-07 18:14:53 +01004030 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004031 status = psa_cipher_abort( operation );
4032
4033 return( status );
4034
4035error:
4036
4037 *output_length = 0;
4038
Gilles Peskine3f108122018-12-07 18:14:53 +01004039 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004040 (void) psa_cipher_abort( operation );
4041
4042 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004043}
4044
Gilles Peskinee553c652018-06-04 16:22:46 +02004045psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4046{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004047 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004048 {
4049 /* The object has (apparently) been initialized but it is not
4050 * in use. It's ok to call abort on such an object, and there's
4051 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004052 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004053 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004054
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004055 /* Sanity check (shouldn't happen: operation->alg should
4056 * always have been initialized to a valid value). */
4057 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4058 return( PSA_ERROR_BAD_STATE );
4059
mohammad1603503973b2018-03-12 15:59:30 +02004060 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004061
Moran Peker41deec42018-04-04 15:43:05 +03004062 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004063 operation->key_set = 0;
4064 operation->iv_set = 0;
4065 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004066 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004067 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004068
Moran Peker395db872018-05-31 14:07:14 +03004069 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004070}
4071
Gilles Peskinea0655c32018-04-30 17:06:50 +02004072
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004073
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004074
mohammad16035955c982018-04-26 00:53:03 +03004075/****************************************************************/
4076/* AEAD */
4077/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004078
Gilles Peskineedf9a652018-08-17 18:11:56 +02004079typedef struct
4080{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004081 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004082 const mbedtls_cipher_info_t *cipher_info;
4083 union
4084 {
4085#if defined(MBEDTLS_CCM_C)
4086 mbedtls_ccm_context ccm;
4087#endif /* MBEDTLS_CCM_C */
4088#if defined(MBEDTLS_GCM_C)
4089 mbedtls_gcm_context gcm;
4090#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004091#if defined(MBEDTLS_CHACHAPOLY_C)
4092 mbedtls_chachapoly_context chachapoly;
4093#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004094 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004095 psa_algorithm_t core_alg;
4096 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004097 uint8_t tag_length;
4098} aead_operation_t;
4099
Gilles Peskine30a9e412019-01-14 18:36:12 +01004100static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004101{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004102 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004103 {
4104#if defined(MBEDTLS_CCM_C)
4105 case PSA_ALG_CCM:
4106 mbedtls_ccm_free( &operation->ctx.ccm );
4107 break;
4108#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004109#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004110 case PSA_ALG_GCM:
4111 mbedtls_gcm_free( &operation->ctx.gcm );
4112 break;
4113#endif /* MBEDTLS_GCM_C */
4114 }
4115}
4116
4117static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004118 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004119 psa_key_usage_t usage,
4120 psa_algorithm_t alg )
4121{
4122 psa_status_t status;
4123 size_t key_bits;
4124 mbedtls_cipher_id_t cipher_id;
4125
Gilles Peskine28f8f302019-07-24 13:30:31 +02004126 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004127 if( status != PSA_SUCCESS )
4128 return( status );
4129
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004130 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004131
4132 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004133 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004134 &cipher_id );
4135 if( operation->cipher_info == NULL )
4136 return( PSA_ERROR_NOT_SUPPORTED );
4137
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004138 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004139 {
4140#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004141 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4142 operation->core_alg = PSA_ALG_CCM;
4143 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004144 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4145 * The call to mbedtls_ccm_encrypt_and_tag or
4146 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004147 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004148 return( PSA_ERROR_INVALID_ARGUMENT );
4149 mbedtls_ccm_init( &operation->ctx.ccm );
4150 status = mbedtls_to_psa_error(
4151 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4152 operation->slot->data.raw.data,
4153 (unsigned int) key_bits ) );
4154 if( status != 0 )
4155 goto cleanup;
4156 break;
4157#endif /* MBEDTLS_CCM_C */
4158
4159#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004160 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4161 operation->core_alg = PSA_ALG_GCM;
4162 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004163 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4164 * The call to mbedtls_gcm_crypt_and_tag or
4165 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004166 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004167 return( PSA_ERROR_INVALID_ARGUMENT );
4168 mbedtls_gcm_init( &operation->ctx.gcm );
4169 status = mbedtls_to_psa_error(
4170 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4171 operation->slot->data.raw.data,
4172 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004173 if( status != 0 )
4174 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004175 break;
4176#endif /* MBEDTLS_GCM_C */
4177
Gilles Peskine26869f22019-05-06 15:25:00 +02004178#if defined(MBEDTLS_CHACHAPOLY_C)
4179 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4180 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4181 operation->full_tag_length = 16;
4182 /* We only support the default tag length. */
4183 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4184 return( PSA_ERROR_NOT_SUPPORTED );
4185 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4186 status = mbedtls_to_psa_error(
4187 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4188 operation->slot->data.raw.data ) );
4189 if( status != 0 )
4190 goto cleanup;
4191 break;
4192#endif /* MBEDTLS_CHACHAPOLY_C */
4193
Gilles Peskineedf9a652018-08-17 18:11:56 +02004194 default:
4195 return( PSA_ERROR_NOT_SUPPORTED );
4196 }
4197
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004198 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4199 {
4200 status = PSA_ERROR_INVALID_ARGUMENT;
4201 goto cleanup;
4202 }
4203 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004204
Gilles Peskineedf9a652018-08-17 18:11:56 +02004205 return( PSA_SUCCESS );
4206
4207cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004208 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004209 return( status );
4210}
4211
Gilles Peskinec5487a82018-12-03 18:08:14 +01004212psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004213 psa_algorithm_t alg,
4214 const uint8_t *nonce,
4215 size_t nonce_length,
4216 const uint8_t *additional_data,
4217 size_t additional_data_length,
4218 const uint8_t *plaintext,
4219 size_t plaintext_length,
4220 uint8_t *ciphertext,
4221 size_t ciphertext_size,
4222 size_t *ciphertext_length )
4223{
mohammad16035955c982018-04-26 00:53:03 +03004224 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004225 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004226 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004227
mohammad1603f08a5502018-06-03 15:05:47 +03004228 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004229
Gilles Peskinec5487a82018-12-03 18:08:14 +01004230 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004231 if( status != PSA_SUCCESS )
4232 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004233
Gilles Peskineedf9a652018-08-17 18:11:56 +02004234 /* For all currently supported modes, the tag is at the end of the
4235 * ciphertext. */
4236 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4237 {
4238 status = PSA_ERROR_BUFFER_TOO_SMALL;
4239 goto exit;
4240 }
4241 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004242
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004243#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004244 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004245 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004246 status = mbedtls_to_psa_error(
4247 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4248 MBEDTLS_GCM_ENCRYPT,
4249 plaintext_length,
4250 nonce, nonce_length,
4251 additional_data, additional_data_length,
4252 plaintext, ciphertext,
4253 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004254 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004255 else
4256#endif /* MBEDTLS_GCM_C */
4257#if defined(MBEDTLS_CCM_C)
4258 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004259 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004260 status = mbedtls_to_psa_error(
4261 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4262 plaintext_length,
4263 nonce, nonce_length,
4264 additional_data,
4265 additional_data_length,
4266 plaintext, ciphertext,
4267 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004268 }
mohammad16035c8845f2018-05-09 05:40:09 -07004269 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004270#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004271#if defined(MBEDTLS_CHACHAPOLY_C)
4272 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4273 {
4274 if( nonce_length != 12 || operation.tag_length != 16 )
4275 {
4276 status = PSA_ERROR_NOT_SUPPORTED;
4277 goto exit;
4278 }
4279 status = mbedtls_to_psa_error(
4280 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4281 plaintext_length,
4282 nonce,
4283 additional_data,
4284 additional_data_length,
4285 plaintext,
4286 ciphertext,
4287 tag ) );
4288 }
4289 else
4290#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004291 {
mohammad1603554faad2018-06-03 15:07:38 +03004292 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004293 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004294
Gilles Peskineedf9a652018-08-17 18:11:56 +02004295 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4296 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004297
Gilles Peskineedf9a652018-08-17 18:11:56 +02004298exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004299 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004300 if( status == PSA_SUCCESS )
4301 *ciphertext_length = plaintext_length + operation.tag_length;
4302 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004303}
4304
Gilles Peskineee652a32018-06-01 19:23:52 +02004305/* Locate the tag in a ciphertext buffer containing the encrypted data
4306 * followed by the tag. Return the length of the part preceding the tag in
4307 * *plaintext_length. This is the size of the plaintext in modes where
4308 * the encrypted data has the same size as the plaintext, such as
4309 * CCM and GCM. */
4310static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4311 const uint8_t *ciphertext,
4312 size_t ciphertext_length,
4313 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004314 const uint8_t **p_tag )
4315{
4316 size_t payload_length;
4317 if( tag_length > ciphertext_length )
4318 return( PSA_ERROR_INVALID_ARGUMENT );
4319 payload_length = ciphertext_length - tag_length;
4320 if( payload_length > plaintext_size )
4321 return( PSA_ERROR_BUFFER_TOO_SMALL );
4322 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004323 return( PSA_SUCCESS );
4324}
4325
Gilles Peskinec5487a82018-12-03 18:08:14 +01004326psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004327 psa_algorithm_t alg,
4328 const uint8_t *nonce,
4329 size_t nonce_length,
4330 const uint8_t *additional_data,
4331 size_t additional_data_length,
4332 const uint8_t *ciphertext,
4333 size_t ciphertext_length,
4334 uint8_t *plaintext,
4335 size_t plaintext_size,
4336 size_t *plaintext_length )
4337{
mohammad16035955c982018-04-26 00:53:03 +03004338 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004339 aead_operation_t operation;
4340 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004341
Gilles Peskineee652a32018-06-01 19:23:52 +02004342 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004343
Gilles Peskinec5487a82018-12-03 18:08:14 +01004344 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004345 if( status != PSA_SUCCESS )
4346 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004347
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004348 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4349 ciphertext, ciphertext_length,
4350 plaintext_size, &tag );
4351 if( status != PSA_SUCCESS )
4352 goto exit;
4353
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004354#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004355 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004356 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004357 status = mbedtls_to_psa_error(
4358 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4359 ciphertext_length - operation.tag_length,
4360 nonce, nonce_length,
4361 additional_data,
4362 additional_data_length,
4363 tag, operation.tag_length,
4364 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004365 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004366 else
4367#endif /* MBEDTLS_GCM_C */
4368#if defined(MBEDTLS_CCM_C)
4369 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004370 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004371 status = mbedtls_to_psa_error(
4372 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4373 ciphertext_length - operation.tag_length,
4374 nonce, nonce_length,
4375 additional_data,
4376 additional_data_length,
4377 ciphertext, plaintext,
4378 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004379 }
mohammad160339574652018-06-01 04:39:53 -07004380 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004381#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004382#if defined(MBEDTLS_CHACHAPOLY_C)
4383 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4384 {
4385 if( nonce_length != 12 || operation.tag_length != 16 )
4386 {
4387 status = PSA_ERROR_NOT_SUPPORTED;
4388 goto exit;
4389 }
4390 status = mbedtls_to_psa_error(
4391 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4392 ciphertext_length - operation.tag_length,
4393 nonce,
4394 additional_data,
4395 additional_data_length,
4396 tag,
4397 ciphertext,
4398 plaintext ) );
4399 }
4400 else
4401#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004402 {
mohammad1603554faad2018-06-03 15:07:38 +03004403 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004404 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004405
Gilles Peskineedf9a652018-08-17 18:11:56 +02004406 if( status != PSA_SUCCESS && plaintext_size != 0 )
4407 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004408
Gilles Peskineedf9a652018-08-17 18:11:56 +02004409exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004410 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004411 if( status == PSA_SUCCESS )
4412 *plaintext_length = ciphertext_length - operation.tag_length;
4413 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004414}
4415
Gilles Peskinea0655c32018-04-30 17:06:50 +02004416
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004417
Gilles Peskine20035e32018-02-03 22:44:14 +01004418/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004419/* Generators */
4420/****************************************************************/
4421
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004422#define HKDF_STATE_INIT 0 /* no input yet */
4423#define HKDF_STATE_STARTED 1 /* got salt */
4424#define HKDF_STATE_KEYED 2 /* got key */
4425#define HKDF_STATE_OUTPUT 3 /* output started */
4426
Gilles Peskinecbe66502019-05-16 16:59:18 +02004427static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004428 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004429{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004430 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4431 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004432 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004433 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004434}
4435
4436
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004437psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004438{
4439 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004440 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004441 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004442 {
4443 /* The object has (apparently) been initialized but it is not
4444 * in use. It's ok to call abort on such an object, and there's
4445 * nothing to do. */
4446 }
4447 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004448#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004449 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004450 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004451 mbedtls_free( operation->ctx.hkdf.info );
4452 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004453 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004454 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004455 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004456 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004457 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004458 if( operation->ctx.tls12_prf.seed != NULL )
4459 {
4460 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4461 operation->ctx.tls12_prf.seed_length );
4462 mbedtls_free( operation->ctx.tls12_prf.seed );
4463 }
4464
4465 if( operation->ctx.tls12_prf.label != NULL )
4466 {
4467 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4468 operation->ctx.tls12_prf.label_length );
4469 mbedtls_free( operation->ctx.tls12_prf.label );
4470 }
4471
4472 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4473
4474 /* We leave the fields Ai and output_block to be erased safely by the
4475 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004476 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004477 else
4478#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004479 {
4480 status = PSA_ERROR_BAD_STATE;
4481 }
Janos Follath083036a2019-06-11 10:22:26 +01004482 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004483 return( status );
4484}
4485
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004486psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004487 size_t *capacity)
4488{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004489 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004490 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004491 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004492 return PSA_ERROR_BAD_STATE;
4493 }
4494
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004495 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004496 return( PSA_SUCCESS );
4497}
4498
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004499psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004500 size_t capacity )
4501{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004502 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004503 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004504 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004505 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004506 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004507 return( PSA_SUCCESS );
4508}
4509
Gilles Peskinebef7f142018-07-12 17:22:21 +02004510#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004511/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004512 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004513static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004514 psa_algorithm_t hash_alg,
4515 uint8_t *output,
4516 size_t output_length )
4517{
4518 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4519 psa_status_t status;
4520
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004521 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4522 return( PSA_ERROR_BAD_STATE );
4523 hkdf->state = HKDF_STATE_OUTPUT;
4524
Gilles Peskinebef7f142018-07-12 17:22:21 +02004525 while( output_length != 0 )
4526 {
4527 /* Copy what remains of the current block */
4528 uint8_t n = hash_length - hkdf->offset_in_block;
4529 if( n > output_length )
4530 n = (uint8_t) output_length;
4531 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4532 output += n;
4533 output_length -= n;
4534 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004535 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004536 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004537 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004538 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004539 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004540 * object was corrupted or if this function is called directly
4541 * inside the library. */
4542 if( hkdf->block_number == 0xff )
4543 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004544
4545 /* We need a new block */
4546 ++hkdf->block_number;
4547 hkdf->offset_in_block = 0;
4548 status = psa_hmac_setup_internal( &hkdf->hmac,
4549 hkdf->prk, hash_length,
4550 hash_alg );
4551 if( status != PSA_SUCCESS )
4552 return( status );
4553 if( hkdf->block_number != 1 )
4554 {
4555 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4556 hkdf->output_block,
4557 hash_length );
4558 if( status != PSA_SUCCESS )
4559 return( status );
4560 }
4561 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4562 hkdf->info,
4563 hkdf->info_length );
4564 if( status != PSA_SUCCESS )
4565 return( status );
4566 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4567 &hkdf->block_number, 1 );
4568 if( status != PSA_SUCCESS )
4569 return( status );
4570 status = psa_hmac_finish_internal( &hkdf->hmac,
4571 hkdf->output_block,
4572 sizeof( hkdf->output_block ) );
4573 if( status != PSA_SUCCESS )
4574 return( status );
4575 }
4576
4577 return( PSA_SUCCESS );
4578}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004579
Janos Follath7742fee2019-06-17 12:58:10 +01004580static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4581 psa_tls12_prf_key_derivation_t *tls12_prf,
4582 psa_algorithm_t alg )
4583{
4584 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4585 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004586 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4587 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004588
Janos Follath7742fee2019-06-17 12:58:10 +01004589 /* We can't be wanting more output after block 0xff, otherwise
4590 * the capacity check in psa_key_derivation_output_bytes() would have
4591 * prevented this call. It could happen only if the operation
4592 * object was corrupted or if this function is called directly
4593 * inside the library. */
4594 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004595 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004596
4597 /* We need a new block */
4598 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004599 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004600
4601 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4602 *
4603 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4604 *
4605 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4606 * HMAC_hash(secret, A(2) + seed) +
4607 * HMAC_hash(secret, A(3) + seed) + ...
4608 *
4609 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004610 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004611 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004612 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004613 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004614 * is currently extracted as `output_block` and where i is
4615 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004616 */
4617
Janos Follathea29bfb2019-06-19 12:21:20 +01004618 /* Save the hash context before using it, to preserve the hash state with
4619 * only the inner padding in it. We need this, because inner padding depends
4620 * on the key (secret in the RFC's terminology). */
4621 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4622 if( status != PSA_SUCCESS )
4623 goto cleanup;
4624
4625 /* Calculate A(i) where i = tls12_prf->block_number. */
4626 if( tls12_prf->block_number == 1 )
4627 {
4628 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4629 * the variable seed and in this instance means it in the context of the
4630 * P_hash function, where seed = label + seed.) */
4631 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4632 tls12_prf->label, tls12_prf->label_length );
4633 if( status != PSA_SUCCESS )
4634 goto cleanup;
4635 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4636 tls12_prf->seed, tls12_prf->seed_length );
4637 if( status != PSA_SUCCESS )
4638 goto cleanup;
4639 }
4640 else
4641 {
4642 /* A(i) = HMAC_hash(secret, A(i-1)) */
4643 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4644 tls12_prf->Ai, hash_length );
4645 if( status != PSA_SUCCESS )
4646 goto cleanup;
4647 }
4648
4649 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4650 tls12_prf->Ai, hash_length );
4651 if( status != PSA_SUCCESS )
4652 goto cleanup;
4653 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4654 if( status != PSA_SUCCESS )
4655 goto cleanup;
4656
4657 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4658 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4659 tls12_prf->Ai, hash_length );
4660 if( status != PSA_SUCCESS )
4661 goto cleanup;
4662 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4663 tls12_prf->label, tls12_prf->label_length );
4664 if( status != PSA_SUCCESS )
4665 goto cleanup;
4666 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4667 tls12_prf->seed, tls12_prf->seed_length );
4668 if( status != PSA_SUCCESS )
4669 goto cleanup;
4670 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4671 tls12_prf->output_block, hash_length );
4672 if( status != PSA_SUCCESS )
4673 goto cleanup;
4674 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4675 if( status != PSA_SUCCESS )
4676 goto cleanup;
4677
Janos Follath7742fee2019-06-17 12:58:10 +01004678
4679cleanup:
4680
Janos Follathea29bfb2019-06-19 12:21:20 +01004681 cleanup_status = psa_hash_abort( &backup );
4682 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4683 status = cleanup_status;
4684
4685 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004686}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004687
Janos Follath844eb0e2019-06-19 12:10:49 +01004688static psa_status_t psa_key_derivation_tls12_prf_read(
4689 psa_tls12_prf_key_derivation_t *tls12_prf,
4690 psa_algorithm_t alg,
4691 uint8_t *output,
4692 size_t output_length )
4693{
4694 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4695 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4696 psa_status_t status;
4697 uint8_t offset, length;
4698
4699 while( output_length != 0 )
4700 {
4701 /* Check if we have fully processed the current block. */
4702 if( tls12_prf->left_in_block == 0 )
4703 {
4704 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4705 alg );
4706 if( status != PSA_SUCCESS )
4707 return( status );
4708
4709 continue;
4710 }
4711
4712 if( tls12_prf->left_in_block > output_length )
4713 length = (uint8_t) output_length;
4714 else
4715 length = tls12_prf->left_in_block;
4716
4717 offset = hash_length - tls12_prf->left_in_block;
4718 memcpy( output, tls12_prf->output_block + offset, length );
4719 output += length;
4720 output_length -= length;
4721 tls12_prf->left_in_block -= length;
4722 }
4723
4724 return( PSA_SUCCESS );
4725}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004726#endif /* MBEDTLS_MD_C */
4727
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004728psa_status_t psa_key_derivation_output_bytes(
4729 psa_key_derivation_operation_t *operation,
4730 uint8_t *output,
4731 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004732{
4733 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004734 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004735
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004736 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004737 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004738 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004739 return PSA_ERROR_BAD_STATE;
4740 }
4741
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004742 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004743 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004744 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004745 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004746 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004747 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004748 goto exit;
4749 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004750 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004751 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004752 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004753 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004754 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4755 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004756 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004757 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004758 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004759 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004760 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004761
Gilles Peskinebef7f142018-07-12 17:22:21 +02004762#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004763 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004764 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004765 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004766 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004767 output, output_length );
4768 }
Janos Follathadbec812019-06-14 11:05:39 +01004769 else
Janos Follathadbec812019-06-14 11:05:39 +01004770 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004771 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004772 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004773 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004774 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004775 output_length );
4776 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004777 else
4778#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004779 {
4780 return( PSA_ERROR_BAD_STATE );
4781 }
4782
4783exit:
4784 if( status != PSA_SUCCESS )
4785 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004786 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004787 * This allows us to differentiate between exhausted operations and
4788 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4789 * operations. */
4790 psa_algorithm_t alg = operation->alg;
4791 psa_key_derivation_abort( operation );
4792 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004793 memset( output, '!', output_length );
4794 }
4795 return( status );
4796}
4797
Gilles Peskine08542d82018-07-19 17:05:42 +02004798#if defined(MBEDTLS_DES_C)
4799static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4800{
4801 if( data_size >= 8 )
4802 mbedtls_des_key_set_parity( data );
4803 if( data_size >= 16 )
4804 mbedtls_des_key_set_parity( data + 8 );
4805 if( data_size >= 24 )
4806 mbedtls_des_key_set_parity( data + 16 );
4807}
4808#endif /* MBEDTLS_DES_C */
4809
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004810static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004811 psa_key_slot_t *slot,
4812 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004813 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004814{
4815 uint8_t *data = NULL;
4816 size_t bytes = PSA_BITS_TO_BYTES( bits );
4817 psa_status_t status;
4818
Gilles Peskine8e338702019-07-30 20:06:31 +02004819 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004820 return( PSA_ERROR_INVALID_ARGUMENT );
4821 if( bits % 8 != 0 )
4822 return( PSA_ERROR_INVALID_ARGUMENT );
4823 data = mbedtls_calloc( 1, bytes );
4824 if( data == NULL )
4825 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4826
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004827 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004828 if( status != PSA_SUCCESS )
4829 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004830#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004831 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004832 psa_des_set_key_parity( data, bytes );
4833#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004834 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004835
4836exit:
4837 mbedtls_free( data );
4838 return( status );
4839}
4840
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004841psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004842 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004843 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004844{
4845 psa_status_t status;
4846 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004847 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004848
4849 /* Reject any attempt to create a zero-length key so that we don't
4850 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4851 if( psa_get_key_bits( attributes ) == 0 )
4852 return( PSA_ERROR_INVALID_ARGUMENT );
4853
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004854 if( ! operation->can_output_key )
4855 return( PSA_ERROR_NOT_PERMITTED );
4856
Gilles Peskinedf179142019-07-15 22:02:14 +02004857 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4858 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004859#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4860 if( driver != NULL )
4861 {
4862 /* Deriving a key in a secure element is not implemented yet. */
4863 status = PSA_ERROR_NOT_SUPPORTED;
4864 }
4865#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004866 if( status == PSA_SUCCESS )
4867 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004868 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004869 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004870 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004871 }
4872 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004873 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004874 if( status != PSA_SUCCESS )
4875 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004876 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004877 *handle = 0;
4878 }
4879 return( status );
4880}
4881
Gilles Peskineeab56e42018-07-12 17:12:33 +02004882
4883
4884/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004885/* Key derivation */
4886/****************************************************************/
4887
Gilles Peskine969c5d62019-01-16 15:53:06 +01004888static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004889 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004890 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004891{
Janos Follath5fe19732019-06-20 15:09:30 +01004892 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4893 * initialisers for this union leave some bytes unspecified.) */
4894 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4895
Gilles Peskine969c5d62019-01-16 15:53:06 +01004896 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004897#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004898 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4899 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4900 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004901 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004902 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004903 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4904 if( hash_size == 0 )
4905 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004906 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4907 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004908 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004909 {
4910 return( PSA_ERROR_NOT_SUPPORTED );
4911 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004912 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004913 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004914 }
4915#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004916 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004917 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004918}
4919
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004920psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004921 psa_algorithm_t alg )
4922{
4923 psa_status_t status;
4924
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004925 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004926 return( PSA_ERROR_BAD_STATE );
4927
Gilles Peskine6843c292019-01-18 16:44:49 +01004928 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4929 return( PSA_ERROR_INVALID_ARGUMENT );
4930 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004931 {
4932 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004933 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004934 }
4935 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4936 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004937 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004938 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004939 else
4940 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004941
4942 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004943 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004944 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004945}
4946
4947#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004948static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004949 psa_algorithm_t hash_alg,
4950 psa_key_derivation_step_t step,
4951 const uint8_t *data,
4952 size_t data_length )
4953{
4954 psa_status_t status;
4955 switch( step )
4956 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004957 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004958 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004959 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004960 status = psa_hmac_setup_internal( &hkdf->hmac,
4961 data, data_length,
4962 hash_alg );
4963 if( status != PSA_SUCCESS )
4964 return( status );
4965 hkdf->state = HKDF_STATE_STARTED;
4966 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004967 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004968 /* If no salt was provided, use an empty salt. */
4969 if( hkdf->state == HKDF_STATE_INIT )
4970 {
4971 status = psa_hmac_setup_internal( &hkdf->hmac,
4972 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004973 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004974 if( status != PSA_SUCCESS )
4975 return( status );
4976 hkdf->state = HKDF_STATE_STARTED;
4977 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004978 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004979 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004980 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4981 data, data_length );
4982 if( status != PSA_SUCCESS )
4983 return( status );
4984 status = psa_hmac_finish_internal( &hkdf->hmac,
4985 hkdf->prk,
4986 sizeof( hkdf->prk ) );
4987 if( status != PSA_SUCCESS )
4988 return( status );
4989 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4990 hkdf->block_number = 0;
4991 hkdf->state = HKDF_STATE_KEYED;
4992 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004993 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004994 if( hkdf->state == HKDF_STATE_OUTPUT )
4995 return( PSA_ERROR_BAD_STATE );
4996 if( hkdf->info_set )
4997 return( PSA_ERROR_BAD_STATE );
4998 hkdf->info_length = data_length;
4999 if( data_length != 0 )
5000 {
5001 hkdf->info = mbedtls_calloc( 1, data_length );
5002 if( hkdf->info == NULL )
5003 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5004 memcpy( hkdf->info, data, data_length );
5005 }
5006 hkdf->info_set = 1;
5007 return( PSA_SUCCESS );
5008 default:
5009 return( PSA_ERROR_INVALID_ARGUMENT );
5010 }
5011}
Janos Follathb03233e2019-06-11 15:30:30 +01005012
Janos Follathf08e2652019-06-13 09:05:41 +01005013static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5014 const uint8_t *data,
5015 size_t data_length )
5016{
5017 if( prf->state != TLS12_PRF_STATE_INIT )
5018 return( PSA_ERROR_BAD_STATE );
5019
Janos Follathd6dce9f2019-07-04 09:11:38 +01005020 if( data_length != 0 )
5021 {
5022 prf->seed = mbedtls_calloc( 1, data_length );
5023 if( prf->seed == NULL )
5024 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005025
Janos Follathd6dce9f2019-07-04 09:11:38 +01005026 memcpy( prf->seed, data, data_length );
5027 prf->seed_length = data_length;
5028 }
Janos Follathf08e2652019-06-13 09:05:41 +01005029
5030 prf->state = TLS12_PRF_STATE_SEED_SET;
5031
5032 return( PSA_SUCCESS );
5033}
5034
Janos Follath81550542019-06-13 14:26:34 +01005035static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5036 psa_algorithm_t hash_alg,
5037 const uint8_t *data,
5038 size_t data_length )
5039{
5040 psa_status_t status;
5041 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5042 return( PSA_ERROR_BAD_STATE );
5043
5044 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5045 if( status != PSA_SUCCESS )
5046 return( status );
5047
5048 prf->state = TLS12_PRF_STATE_KEY_SET;
5049
5050 return( PSA_SUCCESS );
5051}
5052
Janos Follath6660f0e2019-06-17 08:44:03 +01005053static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5054 psa_tls12_prf_key_derivation_t *prf,
5055 psa_algorithm_t hash_alg,
5056 const uint8_t *data,
5057 size_t data_length )
5058{
5059 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005060 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5061 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005062
5063 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5064 return( PSA_ERROR_INVALID_ARGUMENT );
5065
5066 /* Quoting RFC 4279, Section 2:
5067 *
5068 * The premaster secret is formed as follows: if the PSK is N octets
5069 * long, concatenate a uint16 with the value N, N zero octets, a second
5070 * uint16 with the value N, and the PSK itself.
5071 */
5072
Janos Follath40e13932019-06-26 13:22:29 +01005073 *cur++ = ( data_length >> 8 ) & 0xff;
5074 *cur++ = ( data_length >> 0 ) & 0xff;
5075 memset( cur, 0, data_length );
5076 cur += data_length;
5077 *cur++ = pms[0];
5078 *cur++ = pms[1];
5079 memcpy( cur, data, data_length );
5080 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005081
Janos Follath40e13932019-06-26 13:22:29 +01005082 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005083
5084 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5085 return( status );
5086}
5087
Janos Follath63028dd2019-06-13 09:15:47 +01005088static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5089 const uint8_t *data,
5090 size_t data_length )
5091{
5092 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5093 return( PSA_ERROR_BAD_STATE );
5094
Janos Follathd6dce9f2019-07-04 09:11:38 +01005095 if( data_length != 0 )
5096 {
5097 prf->label = mbedtls_calloc( 1, data_length );
5098 if( prf->label == NULL )
5099 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005100
Janos Follathd6dce9f2019-07-04 09:11:38 +01005101 memcpy( prf->label, data, data_length );
5102 prf->label_length = data_length;
5103 }
Janos Follath63028dd2019-06-13 09:15:47 +01005104
5105 prf->state = TLS12_PRF_STATE_LABEL_SET;
5106
5107 return( PSA_SUCCESS );
5108}
5109
Janos Follathb03233e2019-06-11 15:30:30 +01005110static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5111 psa_algorithm_t hash_alg,
5112 psa_key_derivation_step_t step,
5113 const uint8_t *data,
5114 size_t data_length )
5115{
Janos Follathb03233e2019-06-11 15:30:30 +01005116 switch( step )
5117 {
Janos Follathf08e2652019-06-13 09:05:41 +01005118 case PSA_KEY_DERIVATION_INPUT_SEED:
5119 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005120 case PSA_KEY_DERIVATION_INPUT_SECRET:
5121 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005122 case PSA_KEY_DERIVATION_INPUT_LABEL:
5123 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005124 default:
5125 return( PSA_ERROR_INVALID_ARGUMENT );
5126 }
5127}
Janos Follath6660f0e2019-06-17 08:44:03 +01005128
5129static psa_status_t psa_tls12_prf_psk_to_ms_input(
5130 psa_tls12_prf_key_derivation_t *prf,
5131 psa_algorithm_t hash_alg,
5132 psa_key_derivation_step_t step,
5133 const uint8_t *data,
5134 size_t data_length )
5135{
5136 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005137 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005138 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5139 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005140 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005141
5142 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5143}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005144#endif /* MBEDTLS_MD_C */
5145
Gilles Peskineb8965192019-09-24 16:21:10 +02005146/** Check whether the given key type is acceptable for the given
5147 * input step of a key derivation.
5148 *
5149 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5150 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5151 * Both secret and non-secret inputs can alternatively have the type
5152 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5153 * that the input was passed as a buffer rather than via a key object.
5154 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005155static int psa_key_derivation_check_input_type(
5156 psa_key_derivation_step_t step,
5157 psa_key_type_t key_type )
5158{
5159 switch( step )
5160 {
5161 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005162 if( key_type == PSA_KEY_TYPE_DERIVE )
5163 return( PSA_SUCCESS );
5164 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005165 return( PSA_SUCCESS );
5166 break;
5167 case PSA_KEY_DERIVATION_INPUT_LABEL:
5168 case PSA_KEY_DERIVATION_INPUT_SALT:
5169 case PSA_KEY_DERIVATION_INPUT_INFO:
5170 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005171 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5172 return( PSA_SUCCESS );
5173 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005174 return( PSA_SUCCESS );
5175 break;
5176 }
5177 return( PSA_ERROR_INVALID_ARGUMENT );
5178}
5179
Janos Follathb80a94e2019-06-12 15:54:46 +01005180static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005181 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005182 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005183 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005184 const uint8_t *data,
5185 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005186{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005187 psa_status_t status;
5188 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5189
5190 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005191 if( status != PSA_SUCCESS )
5192 goto exit;
5193
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005194#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005195 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005196 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005197 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005198 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005199 step, data, data_length );
5200 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005201 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005202 {
Janos Follathb03233e2019-06-11 15:30:30 +01005203 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5204 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5205 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005206 }
5207 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5208 {
5209 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5210 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5211 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005212 }
5213 else
5214#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005215 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005216 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005217 return( PSA_ERROR_BAD_STATE );
5218 }
5219
Gilles Peskine224b0d62019-09-23 18:13:17 +02005220exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005221 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005222 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005223 return( status );
5224}
5225
Janos Follath51f4a0f2019-06-14 11:35:55 +01005226psa_status_t psa_key_derivation_input_bytes(
5227 psa_key_derivation_operation_t *operation,
5228 psa_key_derivation_step_t step,
5229 const uint8_t *data,
5230 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005231{
Gilles Peskineb8965192019-09-24 16:21:10 +02005232 return( psa_key_derivation_input_internal( operation, step,
5233 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005234 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005235}
5236
Janos Follath51f4a0f2019-06-14 11:35:55 +01005237psa_status_t psa_key_derivation_input_key(
5238 psa_key_derivation_operation_t *operation,
5239 psa_key_derivation_step_t step,
5240 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005241{
5242 psa_key_slot_t *slot;
5243 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005244
Gilles Peskine28f8f302019-07-24 13:30:31 +02005245 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005246 PSA_KEY_USAGE_DERIVE,
5247 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005248 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005249 {
5250 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005251 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005252 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005253
5254 /* Passing a key object as a SECRET input unlocks the permission
5255 * to output to a key object. */
5256 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5257 operation->can_output_key = 1;
5258
Janos Follathb80a94e2019-06-12 15:54:46 +01005259 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005260 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005261 slot->data.raw.data,
5262 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005263}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005264
5265
5266
5267/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005268/* Key agreement */
5269/****************************************************************/
5270
Gilles Peskinea05219c2018-11-16 16:02:56 +01005271#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005272static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5273 size_t peer_key_length,
5274 const mbedtls_ecp_keypair *our_key,
5275 uint8_t *shared_secret,
5276 size_t shared_secret_size,
5277 size_t *shared_secret_length )
5278{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005279 mbedtls_ecp_keypair *their_key = NULL;
5280 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005281 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005282 size_t bits = 0;
5283 psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005284 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005285
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005286 status = psa_import_ec_public_key( curve,
5287 peer_key, peer_key_length,
5288 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005289 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005290 goto exit;
5291
Jaeden Amero97271b32019-01-10 19:38:51 +00005292 status = mbedtls_to_psa_error(
5293 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5294 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005295 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005296 status = mbedtls_to_psa_error(
5297 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5298 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005299 goto exit;
5300
Jaeden Amero97271b32019-01-10 19:38:51 +00005301 status = mbedtls_to_psa_error(
5302 mbedtls_ecdh_calc_secret( &ecdh,
5303 shared_secret_length,
5304 shared_secret, shared_secret_size,
5305 mbedtls_ctr_drbg_random,
5306 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005307 if( status != PSA_SUCCESS )
5308 goto exit;
5309 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5310 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005311
5312exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005313 if( status != PSA_SUCCESS )
5314 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005315 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005316 mbedtls_ecp_keypair_free( their_key );
5317 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005318 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005319}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005320#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005321
Gilles Peskine01d718c2018-09-18 12:01:02 +02005322#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5323
Gilles Peskine0216fe12019-04-11 21:23:21 +02005324static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5325 psa_key_slot_t *private_key,
5326 const uint8_t *peer_key,
5327 size_t peer_key_length,
5328 uint8_t *shared_secret,
5329 size_t shared_secret_size,
5330 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005331{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005332 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005333 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005334#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005335 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005336 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005337 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005338 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5339 private_key->data.ecp,
5340 shared_secret, shared_secret_size,
5341 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005342#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005343 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005344 (void) private_key;
5345 (void) peer_key;
5346 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005347 (void) shared_secret;
5348 (void) shared_secret_size;
5349 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005350 return( PSA_ERROR_NOT_SUPPORTED );
5351 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005352}
5353
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005354/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005355 * to potentially free embedded data structures and wipe confidential data.
5356 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005357static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005358 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005359 psa_key_slot_t *private_key,
5360 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005361 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005362{
5363 psa_status_t status;
5364 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5365 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005366 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005367
5368 /* Step 1: run the secret agreement algorithm to generate the shared
5369 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005370 status = psa_key_agreement_raw_internal( ka_alg,
5371 private_key,
5372 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005373 shared_secret,
5374 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005375 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005376 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005377 goto exit;
5378
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005379 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005380 * the shared secret. A shared secret is permitted wherever a key
5381 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005382 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005383 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005384 shared_secret,
5385 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005386
Gilles Peskine12313cd2018-06-20 00:20:32 +02005387exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005388 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005389 return( status );
5390}
5391
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005392psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005393 psa_key_derivation_step_t step,
5394 psa_key_handle_t private_key,
5395 const uint8_t *peer_key,
5396 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005397{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005398 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005399 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005400 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005401 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005402 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005403 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005404 if( status != PSA_SUCCESS )
5405 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005406 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005407 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005408 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005409 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005410 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005411 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005412}
5413
Gilles Peskinebe697d82019-05-16 18:00:41 +02005414psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5415 psa_key_handle_t private_key,
5416 const uint8_t *peer_key,
5417 size_t peer_key_length,
5418 uint8_t *output,
5419 size_t output_size,
5420 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005421{
5422 psa_key_slot_t *slot;
5423 psa_status_t status;
5424
5425 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5426 {
5427 status = PSA_ERROR_INVALID_ARGUMENT;
5428 goto exit;
5429 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005430 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005431 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005432 if( status != PSA_SUCCESS )
5433 goto exit;
5434
5435 status = psa_key_agreement_raw_internal( alg, slot,
5436 peer_key, peer_key_length,
5437 output, output_size,
5438 output_length );
5439
5440exit:
5441 if( status != PSA_SUCCESS )
5442 {
5443 /* If an error happens and is not handled properly, the output
5444 * may be used as a key to protect sensitive data. Arrange for such
5445 * a key to be random, which is likely to result in decryption or
5446 * verification errors. This is better than filling the buffer with
5447 * some constant data such as zeros, which would result in the data
5448 * being protected with a reproducible, easily knowable key.
5449 */
5450 psa_generate_random( output, output_size );
5451 *output_length = output_size;
5452 }
5453 return( status );
5454}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005455
5456
5457/****************************************************************/
5458/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005459/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005460
Gilles Peskine4c317f42018-07-12 01:24:09 +02005461psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005462 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005463{
Janos Follath24eed8d2019-11-22 13:21:35 +00005464 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005465 GUARD_MODULE_INITIALIZED;
5466
Gilles Peskinef181eca2019-08-07 13:49:00 +02005467 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5468 {
5469 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5470 output,
5471 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5472 if( ret != 0 )
5473 return( mbedtls_to_psa_error( ret ) );
5474 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5475 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5476 }
5477
Gilles Peskinee59236f2018-01-27 23:32:46 +01005478 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5479 return( mbedtls_to_psa_error( ret ) );
5480}
5481
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005482#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5483#include "mbedtls/entropy_poll.h"
5484
Gilles Peskine7228da22019-07-15 11:06:15 +02005485psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005486 size_t seed_size )
5487{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005488 if( global_data.initialized )
5489 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005490
5491 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5492 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5493 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5494 return( PSA_ERROR_INVALID_ARGUMENT );
5495
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005496 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005497}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005498#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005499
Gilles Peskinee56e8782019-04-26 17:34:02 +02005500#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5501static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5502 size_t domain_parameters_size,
5503 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005504{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005505 size_t i;
5506 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005507
Gilles Peskinee56e8782019-04-26 17:34:02 +02005508 if( domain_parameters_size == 0 )
5509 {
5510 *exponent = 65537;
5511 return( PSA_SUCCESS );
5512 }
5513
5514 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5515 * support values that fit in a 32-bit integer, which is larger than
5516 * int on just about every platform anyway. */
5517 if( domain_parameters_size > sizeof( acc ) )
5518 return( PSA_ERROR_NOT_SUPPORTED );
5519 for( i = 0; i < domain_parameters_size; i++ )
5520 acc = ( acc << 8 ) | domain_parameters[i];
5521 if( acc > INT_MAX )
5522 return( PSA_ERROR_NOT_SUPPORTED );
5523 *exponent = acc;
5524 return( PSA_SUCCESS );
5525}
5526#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5527
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005528static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005529 psa_key_slot_t *slot, size_t bits,
5530 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005531{
Gilles Peskine8e338702019-07-30 20:06:31 +02005532 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005533
Gilles Peskinee56e8782019-04-26 17:34:02 +02005534 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005535 return( PSA_ERROR_INVALID_ARGUMENT );
5536
Gilles Peskinee59236f2018-01-27 23:32:46 +01005537 if( key_type_is_raw_bytes( type ) )
5538 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005539 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005540 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5541 if( status != PSA_SUCCESS )
5542 return( status );
5543 status = psa_generate_random( slot->data.raw.data,
5544 slot->data.raw.bytes );
5545 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005546 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005547#if defined(MBEDTLS_DES_C)
5548 if( type == PSA_KEY_TYPE_DES )
5549 psa_des_set_key_parity( slot->data.raw.data,
5550 slot->data.raw.bytes );
5551#endif /* MBEDTLS_DES_C */
5552 }
5553 else
5554
5555#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005556 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005557 {
5558 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005559 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005560 int exponent;
5561 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005562 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5563 return( PSA_ERROR_NOT_SUPPORTED );
5564 /* Accept only byte-aligned keys, for the same reasons as
5565 * in psa_import_rsa_key(). */
5566 if( bits % 8 != 0 )
5567 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005568 status = psa_read_rsa_exponent( domain_parameters,
5569 domain_parameters_size,
5570 &exponent );
5571 if( status != PSA_SUCCESS )
5572 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005573 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5574 if( rsa == NULL )
5575 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5576 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5577 ret = mbedtls_rsa_gen_key( rsa,
5578 mbedtls_ctr_drbg_random,
5579 &global_data.ctr_drbg,
5580 (unsigned int) bits,
5581 exponent );
5582 if( ret != 0 )
5583 {
5584 mbedtls_rsa_free( rsa );
5585 mbedtls_free( rsa );
5586 return( mbedtls_to_psa_error( ret ) );
5587 }
5588 slot->data.rsa = rsa;
5589 }
5590 else
5591#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5592
5593#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005594 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005595 {
5596 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005597 mbedtls_ecp_group_id grp_id =
5598 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005599 const mbedtls_ecp_curve_info *curve_info =
5600 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5601 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005602 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005603 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005604 return( PSA_ERROR_NOT_SUPPORTED );
5605 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5606 return( PSA_ERROR_NOT_SUPPORTED );
5607 if( curve_info->bit_size != bits )
5608 return( PSA_ERROR_INVALID_ARGUMENT );
5609 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5610 if( ecp == NULL )
5611 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5612 mbedtls_ecp_keypair_init( ecp );
5613 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5614 mbedtls_ctr_drbg_random,
5615 &global_data.ctr_drbg );
5616 if( ret != 0 )
5617 {
5618 mbedtls_ecp_keypair_free( ecp );
5619 mbedtls_free( ecp );
5620 return( mbedtls_to_psa_error( ret ) );
5621 }
5622 slot->data.ecp = ecp;
5623 }
5624 else
5625#endif /* MBEDTLS_ECP_C */
5626
5627 return( PSA_ERROR_NOT_SUPPORTED );
5628
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005629 return( PSA_SUCCESS );
5630}
Darryl Green0c6575a2018-11-07 16:05:30 +00005631
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005632psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005633 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005634{
5635 psa_status_t status;
5636 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005637 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005638
Gilles Peskine0f84d622019-09-12 19:03:13 +02005639 /* Reject any attempt to create a zero-length key so that we don't
5640 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5641 if( psa_get_key_bits( attributes ) == 0 )
5642 return( PSA_ERROR_INVALID_ARGUMENT );
5643
Gilles Peskinedf179142019-07-15 22:02:14 +02005644 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5645 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005646 if( status != PSA_SUCCESS )
5647 goto exit;
5648
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005649#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5650 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005651 {
Gilles Peskine11792082019-08-06 18:36:36 +02005652 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5653 size_t pubkey_length = 0; /* We don't support this feature yet */
5654 if( drv->key_management == NULL ||
5655 drv->key_management->p_generate == NULL )
5656 {
5657 status = PSA_ERROR_NOT_SUPPORTED;
5658 goto exit;
5659 }
5660 status = drv->key_management->p_generate(
5661 psa_get_se_driver_context( driver ),
5662 slot->data.se.slot_number, attributes,
5663 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005664 }
Gilles Peskine11792082019-08-06 18:36:36 +02005665 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005666#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005667 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005668 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005669 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005670 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005671 }
Gilles Peskine11792082019-08-06 18:36:36 +02005672
5673exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005674 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005675 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005676 if( status != PSA_SUCCESS )
5677 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005678 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005679 *handle = 0;
5680 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005681 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005682}
5683
5684
Gilles Peskinee59236f2018-01-27 23:32:46 +01005685
5686/****************************************************************/
5687/* Module setup */
5688/****************************************************************/
5689
Gilles Peskine5e769522018-11-20 21:59:56 +01005690psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5691 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5692 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5693{
5694 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5695 return( PSA_ERROR_BAD_STATE );
5696 global_data.entropy_init = entropy_init;
5697 global_data.entropy_free = entropy_free;
5698 return( PSA_SUCCESS );
5699}
5700
Gilles Peskinee59236f2018-01-27 23:32:46 +01005701void mbedtls_psa_crypto_free( void )
5702{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005703 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005704 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5705 {
5706 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005707 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005708 }
5709 /* Wipe all remaining data, including configuration.
5710 * In particular, this sets all state indicator to the value
5711 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005712 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005713#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005714 /* Unregister all secure element drivers, so that we restart from
5715 * a pristine state. */
5716 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005717#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005718}
5719
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005720#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5721/** Recover a transaction that was interrupted by a power failure.
5722 *
5723 * This function is called during initialization, before psa_crypto_init()
5724 * returns. If this function returns a failure status, the initialization
5725 * fails.
5726 */
5727static psa_status_t psa_crypto_recover_transaction(
5728 const psa_crypto_transaction_t *transaction )
5729{
5730 switch( transaction->unknown.type )
5731 {
5732 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5733 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005734 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005735 * is implemented.
5736 * https://github.com/ARMmbed/mbed-crypto/issues/218
5737 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005738 default:
5739 /* We found an unsupported transaction in the storage.
5740 * We don't know what state the storage is in. Give up. */
5741 return( PSA_ERROR_STORAGE_FAILURE );
5742 }
5743}
5744#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5745
Gilles Peskinee59236f2018-01-27 23:32:46 +01005746psa_status_t psa_crypto_init( void )
5747{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005748 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005749 const unsigned char drbg_seed[] = "PSA";
5750
Gilles Peskinec6b69072018-11-20 21:42:52 +01005751 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005752 if( global_data.initialized != 0 )
5753 return( PSA_SUCCESS );
5754
Gilles Peskine5e769522018-11-20 21:59:56 +01005755 /* Set default configuration if
5756 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5757 if( global_data.entropy_init == NULL )
5758 global_data.entropy_init = mbedtls_entropy_init;
5759 if( global_data.entropy_free == NULL )
5760 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005761
Gilles Peskinec6b69072018-11-20 21:42:52 +01005762 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005763 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005764#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5765 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5766 /* The PSA entropy injection feature depends on using NV seed as an entropy
5767 * source. Add NV seed as an entropy source for PSA entropy injection. */
5768 mbedtls_entropy_add_source( &global_data.entropy,
5769 mbedtls_nv_seed_poll, NULL,
5770 MBEDTLS_ENTROPY_BLOCK_SIZE,
5771 MBEDTLS_ENTROPY_SOURCE_STRONG );
5772#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005773 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005774 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005775 status = mbedtls_to_psa_error(
5776 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5777 mbedtls_entropy_func,
5778 &global_data.entropy,
5779 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5780 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005781 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005782 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005783
Gilles Peskine66fb1262018-12-10 16:29:04 +01005784 status = psa_initialize_key_slots( );
5785 if( status != PSA_SUCCESS )
5786 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005787
Gilles Peskined9348f22019-10-01 15:22:29 +02005788#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5789 status = psa_init_all_se_drivers( );
5790 if( status != PSA_SUCCESS )
5791 goto exit;
5792#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5793
Gilles Peskine4b734222019-07-24 15:56:31 +02005794#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005795 status = psa_crypto_load_transaction( );
5796 if( status == PSA_SUCCESS )
5797 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005798 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5799 if( status != PSA_SUCCESS )
5800 goto exit;
5801 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005802 }
5803 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5804 {
5805 /* There's no transaction to complete. It's all good. */
5806 status = PSA_SUCCESS;
5807 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005808#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005809
Gilles Peskinec6b69072018-11-20 21:42:52 +01005810 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005811 global_data.initialized = 1;
5812
5813exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005814 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005815 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005816 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005817}
5818
5819#endif /* MBEDTLS_PSA_CRYPTO_C */