blob: e2e99d7d1bff675234314d211f38d933b6d39e47 [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)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100375mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100376 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200377{
378 switch( curve )
379 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100380 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100381 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
Paul Elliott8ff510a2020-06-02 17:19:28 +0100398 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100399 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
Paul Elliott8ff510a2020-06-02 17:19:28 +0100412 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100413 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
Paul Elliott8ff510a2020-06-02 17:19:28 +0100424 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100425 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
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200444static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
445 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446{
447 /* Check that the bit size is acceptable for the key type */
448 switch( type )
449 {
450 case PSA_KEY_TYPE_RAW_DATA:
451#if defined(MBEDTLS_MD_C)
452 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200453#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200454 case PSA_KEY_TYPE_DERIVE:
455 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200456#if defined(MBEDTLS_AES_C)
457 case PSA_KEY_TYPE_AES:
458 if( bits != 128 && bits != 192 && bits != 256 )
459 return( PSA_ERROR_INVALID_ARGUMENT );
460 break;
461#endif
462#if defined(MBEDTLS_CAMELLIA_C)
463 case PSA_KEY_TYPE_CAMELLIA:
464 if( bits != 128 && bits != 192 && bits != 256 )
465 return( PSA_ERROR_INVALID_ARGUMENT );
466 break;
467#endif
468#if defined(MBEDTLS_DES_C)
469 case PSA_KEY_TYPE_DES:
470 if( bits != 64 && bits != 128 && bits != 192 )
471 return( PSA_ERROR_INVALID_ARGUMENT );
472 break;
473#endif
474#if defined(MBEDTLS_ARC4_C)
475 case PSA_KEY_TYPE_ARC4:
476 if( bits < 8 || bits > 2048 )
477 return( PSA_ERROR_INVALID_ARGUMENT );
478 break;
479#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200480#if defined(MBEDTLS_CHACHA20_C)
481 case PSA_KEY_TYPE_CHACHA20:
482 if( bits != 256 )
483 return( PSA_ERROR_INVALID_ARGUMENT );
484 break;
485#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200486 default:
487 return( PSA_ERROR_NOT_SUPPORTED );
488 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200489 if( bits % 8 != 0 )
490 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200491
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200492 return( PSA_SUCCESS );
493}
494
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200495#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100496/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
497 * that are not a multiple of 8) well. For example, there is only
498 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
499 * way to return the exact bit size of a key.
500 * To keep things simple, reject non-byte-aligned key sizes. */
501static psa_status_t psa_check_rsa_key_byte_aligned(
502 const mbedtls_rsa_context *rsa )
503{
504 mbedtls_mpi n;
505 psa_status_t status;
506 mbedtls_mpi_init( &n );
507 status = mbedtls_to_psa_error(
508 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
509 if( status == PSA_SUCCESS )
510 {
511 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
512 status = PSA_ERROR_NOT_SUPPORTED;
513 }
514 mbedtls_mpi_free( &n );
515 return( status );
516}
517
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000518static psa_status_t psa_import_rsa_key( psa_key_type_t type,
519 const uint8_t *data,
520 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200521 mbedtls_rsa_context **p_rsa )
522{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000523 psa_status_t status;
524 mbedtls_pk_context pk;
525 mbedtls_rsa_context *rsa;
526 size_t bits;
527
528 mbedtls_pk_init( &pk );
529
530 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200531 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000532 status = mbedtls_to_psa_error(
533 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200534 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000535 status = mbedtls_to_psa_error(
536 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
537 if( status != PSA_SUCCESS )
538 goto exit;
539
540 /* We have something that the pkparse module recognizes. If it is a
541 * valid RSA key, store it. */
542 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200543 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000544 status = PSA_ERROR_INVALID_ARGUMENT;
545 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200546 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000547
548 rsa = mbedtls_pk_rsa( pk );
549 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
550 * supports non-byte-aligned key sizes, but not well. For example,
551 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
552 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
553 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
554 {
555 status = PSA_ERROR_NOT_SUPPORTED;
556 goto exit;
557 }
558 status = psa_check_rsa_key_byte_aligned( rsa );
559
560exit:
561 /* Free the content of the pk object only on error. */
562 if( status != PSA_SUCCESS )
563 {
564 mbedtls_pk_free( &pk );
565 return( status );
566 }
567
568 /* On success, store the content of the object in the RSA context. */
569 *p_rsa = rsa;
570
571 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200572}
573#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
574
Jaeden Ameroccdce902019-01-10 11:42:27 +0000575#if defined(MBEDTLS_ECP_C)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100576static psa_status_t psa_prepare_import_ec_key( psa_ecc_family_t curve,
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100577 size_t data_length,
578 int is_public,
Gilles Peskine4cd32772019-12-02 20:49:42 +0100579 mbedtls_ecp_keypair **p_ecp )
580{
581 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
582 *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
583 if( *p_ecp == NULL )
584 return( PSA_ERROR_INSUFFICIENT_MEMORY );
585 mbedtls_ecp_keypair_init( *p_ecp );
586
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100587 if( is_public )
588 {
589 /* A public key is represented as:
590 * - The byte 0x04;
591 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
592 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
593 * So its data length is 2m+1 where n is the key size in bits.
594 */
595 if( ( data_length & 1 ) == 0 )
596 return( PSA_ERROR_INVALID_ARGUMENT );
597 data_length = data_length / 2;
598 }
599
Gilles Peskine4cd32772019-12-02 20:49:42 +0100600 /* Load the group. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100601 grp_id = mbedtls_ecc_group_of_psa( curve, data_length );
602 if( grp_id == MBEDTLS_ECP_DP_NONE )
603 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100604 return( mbedtls_to_psa_error(
605 mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) );
606}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000607
608/* Import a public key given as the uncompressed representation defined by SEC1
609 * 2.3.3 as the content of an ECPoint. */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100610static psa_status_t psa_import_ec_public_key( psa_ecc_family_t curve,
Jaeden Ameroccdce902019-01-10 11:42:27 +0000611 const uint8_t *data,
612 size_t data_length,
613 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200614{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200615 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000616 mbedtls_ecp_keypair *ecp = NULL;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000617
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100618 status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp );
Jaeden Ameroccdce902019-01-10 11:42:27 +0000619 if( status != PSA_SUCCESS )
620 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100621
Jaeden Ameroccdce902019-01-10 11:42:27 +0000622 /* Load the public value. */
623 status = mbedtls_to_psa_error(
624 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
625 data, data_length ) );
626 if( status != PSA_SUCCESS )
627 goto exit;
628
629 /* Check that the point is on the curve. */
630 status = mbedtls_to_psa_error(
631 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
632 if( status != PSA_SUCCESS )
633 goto exit;
634
635 *p_ecp = ecp;
636 return( PSA_SUCCESS );
637
638exit:
639 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200640 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000641 mbedtls_ecp_keypair_free( ecp );
642 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200643 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000644 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200645}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200646
Gilles Peskinef76aa772018-10-29 19:24:33 +0100647/* Import a private key given as a byte string which is the private value
648 * in big-endian order. */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100649static psa_status_t psa_import_ec_private_key( psa_ecc_family_t curve,
Gilles Peskinef76aa772018-10-29 19:24:33 +0100650 const uint8_t *data,
651 size_t data_length,
652 mbedtls_ecp_keypair **p_ecp )
653{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200654 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100655 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100656
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100657 status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100658 if( status != PSA_SUCCESS )
659 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100660
Steven Cooremane3fd3922020-06-11 16:50:36 +0200661 /* Load and validate the secret key */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100662 status = mbedtls_to_psa_error(
Steven Cooremane3fd3922020-06-11 16:50:36 +0200663 mbedtls_ecp_read_key( ecp->grp.id, ecp, data, data_length ) );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100664 if( status != PSA_SUCCESS )
665 goto exit;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200666
Gilles Peskinef76aa772018-10-29 19:24:33 +0100667 /* Calculate the public key from the private key. */
668 status = mbedtls_to_psa_error(
669 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
670 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
671 if( status != PSA_SUCCESS )
672 goto exit;
673
674 *p_ecp = ecp;
675 return( PSA_SUCCESS );
676
677exit:
678 if( ecp != NULL )
679 {
680 mbedtls_ecp_keypair_free( ecp );
681 mbedtls_free( ecp );
682 }
683 return( status );
684}
685#endif /* defined(MBEDTLS_ECP_C) */
686
Gilles Peskineb46bef22019-07-30 21:32:04 +0200687
688/** Return the size of the key in the given slot, in bits.
689 *
690 * \param[in] slot A key slot.
691 *
692 * \return The key size in bits, read from the metadata in the slot.
693 */
694static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
695{
696 return( slot->attr.bits );
697}
698
699/** Calculate the size of the key in the given slot, in bits.
700 *
701 * \param[in] slot A key slot containing a transparent key.
702 *
703 * \return The key size in bits, calculated from the key data.
704 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200705static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200706{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200707 size_t bits = 0; /* return 0 on an empty slot */
708
Gilles Peskineb46bef22019-07-30 21:32:04 +0200709 if( key_type_is_raw_bytes( slot->attr.type ) )
Steven Cooreman71fd80d2020-07-07 21:12:27 +0200710 bits = PSA_BYTES_TO_BITS( slot->data.key.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200711#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200712 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
713 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200714#endif /* defined(MBEDTLS_RSA_C) */
715#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200716 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
717 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200718#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200719
720 /* We know that the size fits in psa_key_bits_t thanks to checks
721 * when the key was created. */
722 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200723}
724
Gilles Peskine8e338702019-07-30 20:06:31 +0200725/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100726 * previously. This function assumes that the slot does not contain
727 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100728psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
729 const uint8_t *data,
730 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100731{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200732 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100733
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200734 /* zero-length keys are never supported. */
735 if( data_length == 0 )
736 return( PSA_ERROR_NOT_SUPPORTED );
737
738 /* Ensure that the bytes-to-bit conversion never overflows. */
739 if( data_length > SIZE_MAX / 8 )
740 return( PSA_ERROR_NOT_SUPPORTED );
741
Gilles Peskine8e338702019-07-30 20:06:31 +0200742 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200744 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200745
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200746 /* Enforce a size limit, and in particular ensure that the bit
747 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200748 if( bit_size > PSA_MAX_KEY_BITS )
749 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200750
751 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200752 if( status != PSA_SUCCESS )
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200753 return status;
754
755 /* Allocate memory for the key */
756 slot->data.key.data = mbedtls_calloc( 1, data_length );
757 if( slot->data.key.data == NULL )
758 {
759 return( PSA_ERROR_INSUFFICIENT_MEMORY );
760 }
761 slot->data.key.bytes = data_length;
762
763 /* copy key into allocated buffer */
764 memcpy(slot->data.key.data, data, data_length);
765
766 /* Write the actual key size to the slot.
767 * psa_start_key_creation() wrote the size declared by the
768 * caller, which may be 0 (meaning unspecified) or wrong. */
769 slot->attr.bits = (psa_key_bits_t) bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100770 }
771 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100772#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200773 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100774 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100775 status = psa_import_ec_private_key( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100776 data, data_length,
777 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100778 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200779 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100780 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000781 status = psa_import_ec_public_key(
Paul Elliott8ff510a2020-06-02 17:19:28 +0100782 PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000783 data, data_length,
784 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100785 }
786 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100787#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000788#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200789 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100790 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200791 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000792 data, data_length,
793 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100794 }
795 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000796#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100797 {
798 return( PSA_ERROR_NOT_SUPPORTED );
799 }
Darryl Green940d72c2018-07-13 13:18:51 +0100800
Gilles Peskineb46bef22019-07-30 21:32:04 +0200801 if( status == PSA_SUCCESS )
802 {
803 /* Write the actual key size to the slot.
804 * psa_start_key_creation() wrote the size declared by the
805 * caller, which may be 0 (meaning unspecified) or wrong. */
806 slot->attr.bits = psa_calculate_key_bits( slot );
807 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100808 return( status );
809}
810
Gilles Peskinef603c712019-01-19 13:40:11 +0100811/** Calculate the intersection of two algorithm usage policies.
812 *
813 * Return 0 (which allows no operation) on incompatibility.
814 */
815static psa_algorithm_t psa_key_policy_algorithm_intersection(
816 psa_algorithm_t alg1,
817 psa_algorithm_t alg2 )
818{
Gilles Peskine549ea862019-05-22 11:45:59 +0200819 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100820 if( alg1 == alg2 )
821 return( alg1 );
822 /* If the policies are from the same hash-and-sign family, check
823 * if one is a wildcard. If so the other has the specific algorithm. */
824 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
825 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
826 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
827 {
828 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
829 return( alg2 );
830 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
831 return( alg1 );
832 }
833 /* If the policies are incompatible, allow nothing. */
834 return( 0 );
835}
836
Gilles Peskined6f371b2019-05-10 19:33:38 +0200837static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
838 psa_algorithm_t requested_alg )
839{
Gilles Peskine549ea862019-05-22 11:45:59 +0200840 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200841 if( requested_alg == policy_alg )
842 return( 1 );
843 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200844 * and requested_alg is the same hash-and-sign family with any hash,
845 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200846 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
847 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
848 {
849 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
850 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
851 }
852 /* If it isn't permitted, it's forbidden. */
853 return( 0 );
854}
855
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100856/** Test whether a policy permits an algorithm.
857 *
858 * The caller must test usage flags separately.
859 */
860static int psa_key_policy_permits( const psa_key_policy_t *policy,
861 psa_algorithm_t alg )
862{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200863 return( psa_key_algorithm_permits( policy->alg, alg ) ||
864 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100865}
866
Gilles Peskinef603c712019-01-19 13:40:11 +0100867/** Restrict a key policy based on a constraint.
868 *
869 * \param[in,out] policy The policy to restrict.
870 * \param[in] constraint The policy constraint to apply.
871 *
872 * \retval #PSA_SUCCESS
873 * \c *policy contains the intersection of the original value of
874 * \c *policy and \c *constraint.
875 * \retval #PSA_ERROR_INVALID_ARGUMENT
876 * \c *policy and \c *constraint are incompatible.
877 * \c *policy is unchanged.
878 */
879static psa_status_t psa_restrict_key_policy(
880 psa_key_policy_t *policy,
881 const psa_key_policy_t *constraint )
882{
883 psa_algorithm_t intersection_alg =
884 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200885 psa_algorithm_t intersection_alg2 =
886 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100887 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
888 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200889 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
890 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100891 policy->usage &= constraint->usage;
892 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200893 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100894 return( PSA_SUCCESS );
895}
896
Darryl Green06fd18d2018-07-16 11:21:11 +0100897/** Retrieve a slot which must contain a key. The key must have allow all the
898 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
899 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100900static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100901 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100902 psa_key_usage_t usage,
903 psa_algorithm_t alg )
904{
905 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100906 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100907
908 *p_slot = NULL;
909
Gilles Peskinec5487a82018-12-03 18:08:14 +0100910 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100911 if( status != PSA_SUCCESS )
912 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100913
914 /* Enforce that usage policy for the key slot contains all the flags
915 * required by the usage parameter. There is one exception: public
916 * keys can always be exported, so we treat public key objects as
917 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200918 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100919 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200920 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100921 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100922
923 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200924 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100925 return( PSA_ERROR_NOT_PERMITTED );
926
927 *p_slot = slot;
928 return( PSA_SUCCESS );
929}
Darryl Green940d72c2018-07-13 13:18:51 +0100930
Gilles Peskine28f8f302019-07-24 13:30:31 +0200931/** Retrieve a slot which must contain a transparent key.
932 *
933 * A transparent key is a key for which the key material is directly
934 * available, as opposed to a key in a secure element.
935 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200936 * This is a temporary function to use instead of psa_get_key_from_slot()
937 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200938 */
939#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
940static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
941 psa_key_slot_t **p_slot,
942 psa_key_usage_t usage,
943 psa_algorithm_t alg )
944{
945 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
946 if( status != PSA_SUCCESS )
947 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200948 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200949 {
950 *p_slot = NULL;
951 return( PSA_ERROR_NOT_SUPPORTED );
952 }
953 return( PSA_SUCCESS );
954}
955#else /* MBEDTLS_PSA_CRYPTO_SE_C */
956/* With no secure element support, all keys are transparent. */
957#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
958 psa_get_key_from_slot( handle, p_slot, usage, alg )
959#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
960
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100961/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100962static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000963{
Gilles Peskine73167e12019-07-12 23:44:37 +0200964#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
965 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000966 {
967 /* No key material to clean. */
968 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200969 else
970#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200971 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000972 {
973 /* No key material to clean. */
974 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200975 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000976 {
Steven Cooreman71fd80d2020-07-07 21:12:27 +0200977 mbedtls_free( slot->data.key.data );
Darryl Green40225ba2018-11-15 14:48:15 +0000978 }
979 else
980#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200981 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000982 {
983 mbedtls_rsa_free( slot->data.rsa );
984 mbedtls_free( slot->data.rsa );
985 }
986 else
987#endif /* defined(MBEDTLS_RSA_C) */
988#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200989 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000990 {
991 mbedtls_ecp_keypair_free( slot->data.ecp );
992 mbedtls_free( slot->data.ecp );
993 }
994 else
995#endif /* defined(MBEDTLS_ECP_C) */
996 {
997 /* Shouldn't happen: the key type is not any type that we
998 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200999 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +00001000 }
1001
1002 return( PSA_SUCCESS );
1003}
1004
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001005/** Completely wipe a slot in memory, including its policy.
1006 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001007psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001008{
1009 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001010 /* Multipart operations may still be using the key. This is safe
1011 * because all multipart operation objects are independent from
1012 * the key slot: if they need to access the key after the setup
1013 * phase, they have a copy of the key. Note that this means that
1014 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001015 /* At this point, key material and other type-specific content has
1016 * been wiped. Clear remaining metadata. We can call memset and not
1017 * zeroize because the metadata is not particularly sensitive. */
1018 memset( slot, 0, sizeof( *slot ) );
1019 return( status );
1020}
1021
Gilles Peskinec5487a82018-12-03 18:08:14 +01001022psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001023{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001024 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001025 psa_status_t status; /* status of the last operation */
1026 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001027#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1028 psa_se_drv_table_entry_t *driver;
1029#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001030
Gilles Peskine1841cf42019-10-08 15:48:25 +02001031 if( handle == 0 )
1032 return( PSA_SUCCESS );
1033
Gilles Peskinec5487a82018-12-03 18:08:14 +01001034 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001035 if( status != PSA_SUCCESS )
1036 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001037
1038#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001039 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001040 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001041 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001042 /* For a key in a secure element, we need to do three things:
1043 * remove the key file in internal storage, destroy the
1044 * key inside the secure element, and update the driver's
1045 * persistent data. Start a transaction that will encompass these
1046 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001047 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001048 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001049 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001050 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001051 status = psa_crypto_save_transaction( );
1052 if( status != PSA_SUCCESS )
1053 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001054 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001055 /* We should still try to destroy the key in the secure
1056 * element and the key metadata in storage. This is especially
1057 * important if the error is that the storage is full.
1058 * But how to do it exactly without risking an inconsistent
1059 * state after a reset?
1060 * https://github.com/ARMmbed/mbed-crypto/issues/215
1061 */
1062 overall_status = status;
1063 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001064 }
1065
Gilles Peskine354f7672019-07-12 23:46:38 +02001066 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001067 if( overall_status == PSA_SUCCESS )
1068 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001069 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001070#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1071
Darryl Greend49a4992018-06-18 17:27:26 +01001072#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001073 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001074 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001075 status = psa_destroy_persistent_key( slot->attr.id );
1076 if( overall_status == PSA_SUCCESS )
1077 overall_status = status;
1078
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001079 /* TODO: other slots may have a copy of the same key. We should
1080 * invalidate them.
1081 * https://github.com/ARMmbed/mbed-crypto/issues/214
1082 */
Darryl Greend49a4992018-06-18 17:27:26 +01001083 }
1084#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001085
Gilles Peskinefc762652019-07-22 19:30:34 +02001086#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1087 if( driver != NULL )
1088 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001089 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001090 if( overall_status == PSA_SUCCESS )
1091 overall_status = status;
1092 status = psa_crypto_stop_transaction( );
1093 if( overall_status == PSA_SUCCESS )
1094 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001095 }
1096#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1097
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001098#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1099exit:
1100#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001101 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001102 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1103 if( overall_status == PSA_SUCCESS )
1104 overall_status = status;
1105 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001106}
1107
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001108void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001109{
Gilles Peskineb699f072019-04-26 16:06:02 +02001110 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001111 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001112}
1113
Gilles Peskineb699f072019-04-26 16:06:02 +02001114psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1115 psa_key_type_t type,
1116 const uint8_t *data,
1117 size_t data_length )
1118{
1119 uint8_t *copy = NULL;
1120
1121 if( data_length != 0 )
1122 {
1123 copy = mbedtls_calloc( 1, data_length );
1124 if( copy == NULL )
1125 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1126 memcpy( copy, data, data_length );
1127 }
1128 /* After this point, this function is guaranteed to succeed, so it
1129 * can start modifying `*attributes`. */
1130
1131 if( attributes->domain_parameters != NULL )
1132 {
1133 mbedtls_free( attributes->domain_parameters );
1134 attributes->domain_parameters = NULL;
1135 attributes->domain_parameters_size = 0;
1136 }
1137
1138 attributes->domain_parameters = copy;
1139 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001140 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001141 return( PSA_SUCCESS );
1142}
1143
1144psa_status_t psa_get_key_domain_parameters(
1145 const psa_key_attributes_t *attributes,
1146 uint8_t *data, size_t data_size, size_t *data_length )
1147{
1148 if( attributes->domain_parameters_size > data_size )
1149 return( PSA_ERROR_BUFFER_TOO_SMALL );
1150 *data_length = attributes->domain_parameters_size;
1151 if( attributes->domain_parameters_size != 0 )
1152 memcpy( data, attributes->domain_parameters,
1153 attributes->domain_parameters_size );
1154 return( PSA_SUCCESS );
1155}
1156
1157#if defined(MBEDTLS_RSA_C)
1158static psa_status_t psa_get_rsa_public_exponent(
1159 const mbedtls_rsa_context *rsa,
1160 psa_key_attributes_t *attributes )
1161{
1162 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001163 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001164 uint8_t *buffer = NULL;
1165 size_t buflen;
1166 mbedtls_mpi_init( &mpi );
1167
1168 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1169 if( ret != 0 )
1170 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001171 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1172 {
1173 /* It's the default value, which is reported as an empty string,
1174 * so there's nothing to do. */
1175 goto exit;
1176 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001177
1178 buflen = mbedtls_mpi_size( &mpi );
1179 buffer = mbedtls_calloc( 1, buflen );
1180 if( buffer == NULL )
1181 {
1182 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1183 goto exit;
1184 }
1185 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1186 if( ret != 0 )
1187 goto exit;
1188 attributes->domain_parameters = buffer;
1189 attributes->domain_parameters_size = buflen;
1190
1191exit:
1192 mbedtls_mpi_free( &mpi );
1193 if( ret != 0 )
1194 mbedtls_free( buffer );
1195 return( mbedtls_to_psa_error( ret ) );
1196}
1197#endif /* MBEDTLS_RSA_C */
1198
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001199/** Retrieve all the publicly-accessible attributes of a key.
1200 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001201psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1202 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001203{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001204 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001205 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001206
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001207 psa_reset_key_attributes( attributes );
1208
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001209 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001210 if( status != PSA_SUCCESS )
1211 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001212
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001213 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001214 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1215 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1216
1217#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1218 if( psa_key_slot_is_external( slot ) )
1219 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1220#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001221
Gilles Peskine8e338702019-07-30 20:06:31 +02001222 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001223 {
1224#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001225 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001226 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001227#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001228 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001229 * is not yet implemented.
1230 * https://github.com/ARMmbed/mbed-crypto/issues/216
1231 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001232 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001233 break;
1234#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001235 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1236 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001237#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001238 default:
1239 /* Nothing else to do. */
1240 break;
1241 }
1242
1243 if( status != PSA_SUCCESS )
1244 psa_reset_key_attributes( attributes );
1245 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001246}
1247
Gilles Peskinec8000c02019-08-02 20:15:51 +02001248#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1249psa_status_t psa_get_key_slot_number(
1250 const psa_key_attributes_t *attributes,
1251 psa_key_slot_number_t *slot_number )
1252{
1253 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1254 {
1255 *slot_number = attributes->slot_number;
1256 return( PSA_SUCCESS );
1257 }
1258 else
1259 return( PSA_ERROR_INVALID_ARGUMENT );
1260}
1261#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1262
Jaeden Ameroccdce902019-01-10 11:42:27 +00001263#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001264static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1265 unsigned char *buf, size_t size )
1266{
Janos Follath24eed8d2019-11-22 13:21:35 +00001267 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001268 unsigned char *c;
1269 size_t len = 0;
1270
1271 c = buf + size;
1272
1273 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1274
1275 return( (int) len );
1276}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001277#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001278
Gilles Peskinef603c712019-01-19 13:40:11 +01001279static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1280 uint8_t *data,
1281 size_t data_size,
1282 size_t *data_length,
1283 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001284{
Gilles Peskine5d309672019-07-12 23:47:28 +02001285#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1286 const psa_drv_se_t *drv;
1287 psa_drv_se_context_t *drv_context;
1288#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1289
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001290 *data_length = 0;
1291
Gilles Peskine8e338702019-07-30 20:06:31 +02001292 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001293 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001294
Gilles Peskinef9168942019-09-12 19:20:29 +02001295 /* Reject a zero-length output buffer now, since this can never be a
1296 * valid key representation. This way we know that data must be a valid
1297 * pointer and we can do things like memset(data, ..., data_size). */
1298 if( data_size == 0 )
1299 return( PSA_ERROR_BUFFER_TOO_SMALL );
1300
Gilles Peskine5d309672019-07-12 23:47:28 +02001301#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001302 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001303 {
1304 psa_drv_se_export_key_t method;
1305 if( drv->key_management == NULL )
1306 return( PSA_ERROR_NOT_SUPPORTED );
1307 method = ( export_public_key ?
1308 drv->key_management->p_export_public :
1309 drv->key_management->p_export );
1310 if( method == NULL )
1311 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001312 return( method( drv_context,
1313 slot->data.se.slot_number,
1314 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001315 }
1316#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1317
Gilles Peskine8e338702019-07-30 20:06:31 +02001318 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001319 {
Steven Cooreman71fd80d2020-07-07 21:12:27 +02001320 if( slot->data.key.bytes > data_size )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001321 return( PSA_ERROR_BUFFER_TOO_SMALL );
Steven Cooreman71fd80d2020-07-07 21:12:27 +02001322 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1323 memset( data + slot->data.key.bytes, 0,
1324 data_size - slot->data.key.bytes );
1325 *data_length = slot->data.key.bytes;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001326 return( PSA_SUCCESS );
1327 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001328#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001329 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001330 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001331 psa_status_t status;
1332
Gilles Peskineb46bef22019-07-30 21:32:04 +02001333 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001334 if( bytes > data_size )
1335 return( PSA_ERROR_BUFFER_TOO_SMALL );
1336 status = mbedtls_to_psa_error(
Steven Cooreman0024df62020-07-13 10:59:40 +02001337 mbedtls_ecp_write_key( slot->data.ecp,
1338 data, bytes ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001339 if( status != PSA_SUCCESS )
1340 return( status );
1341 memset( data + bytes, 0, data_size - bytes );
1342 *data_length = bytes;
1343 return( PSA_SUCCESS );
1344 }
1345#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001346 else
Moran Peker17e36e12018-05-02 12:55:20 +03001347 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001348#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001349 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1350 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001351 {
Moran Pekera998bc62018-04-16 18:16:20 +03001352 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001353 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001354 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001355 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001356#if defined(MBEDTLS_RSA_C)
1357 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001358 pk.pk_info = &mbedtls_rsa_info;
1359 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001360#else
1361 return( PSA_ERROR_NOT_SUPPORTED );
1362#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001363 }
1364 else
1365 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001366#if defined(MBEDTLS_ECP_C)
1367 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001368 pk.pk_info = &mbedtls_eckey_info;
1369 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001370#else
1371 return( PSA_ERROR_NOT_SUPPORTED );
1372#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001373 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001374 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001375 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001376 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001377 }
Moran Peker17e36e12018-05-02 12:55:20 +03001378 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001379 {
Moran Peker17e36e12018-05-02 12:55:20 +03001380 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001381 }
Moran Peker60364322018-04-29 11:34:58 +03001382 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001383 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001384 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001385 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001386 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001387 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1388 * Move the data to the beginning and erase remaining data
1389 * at the original location. */
1390 if( 2 * (size_t) ret <= data_size )
1391 {
1392 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001393 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001394 }
1395 else if( (size_t) ret < data_size )
1396 {
1397 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001398 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001399 }
Moran Pekera998bc62018-04-16 18:16:20 +03001400 *data_length = ret;
1401 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001402 }
1403 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001404#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001405 {
1406 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001407 it is valid for a special-purpose implementation to omit
1408 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001409 return( PSA_ERROR_NOT_SUPPORTED );
1410 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001411 }
1412}
1413
Gilles Peskinec5487a82018-12-03 18:08:14 +01001414psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001415 uint8_t *data,
1416 size_t data_size,
1417 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001418{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001419 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001420 psa_status_t status;
1421
1422 /* Set the key to empty now, so that even when there are errors, we always
1423 * set data_length to a value between 0 and data_size. On error, setting
1424 * the key to empty is a good choice because an empty key representation is
1425 * unlikely to be accepted anywhere. */
1426 *data_length = 0;
1427
1428 /* Export requires the EXPORT flag. There is an exception for public keys,
1429 * which don't require any flag, but psa_get_key_from_slot takes
1430 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001431 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001432 if( status != PSA_SUCCESS )
1433 return( status );
1434 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001435 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001436}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001437
Gilles Peskinec5487a82018-12-03 18:08:14 +01001438psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001439 uint8_t *data,
1440 size_t data_size,
1441 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001442{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001443 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001444 psa_status_t status;
1445
1446 /* Set the key to empty now, so that even when there are errors, we always
1447 * set data_length to a value between 0 and data_size. On error, setting
1448 * the key to empty is a good choice because an empty key representation is
1449 * unlikely to be accepted anywhere. */
1450 *data_length = 0;
1451
1452 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001453 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001454 if( status != PSA_SUCCESS )
1455 return( status );
1456 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001457 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001458}
1459
Gilles Peskine91e8c332019-08-02 19:19:39 +02001460#if defined(static_assert)
1461static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1462 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001463static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001464 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001465static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001466 "One or more key attribute flag is listed as both internal-only and external-only" );
1467#endif
1468
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001469/** Validate that a key policy is internally well-formed.
1470 *
1471 * This function only rejects invalid policies. It does not validate the
1472 * consistency of the policy with respect to other attributes of the key
1473 * such as the key type.
1474 */
1475static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001476{
1477 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001478 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001479 PSA_KEY_USAGE_ENCRYPT |
1480 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001481 PSA_KEY_USAGE_SIGN_HASH |
1482 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001483 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1484 return( PSA_ERROR_INVALID_ARGUMENT );
1485
Gilles Peskine4747d192019-04-17 15:05:45 +02001486 return( PSA_SUCCESS );
1487}
1488
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001489/** Validate the internal consistency of key attributes.
1490 *
1491 * This function only rejects invalid attribute values. If does not
1492 * validate the consistency of the attributes with any key data that may
1493 * be involved in the creation of the key.
1494 *
1495 * Call this function early in the key creation process.
1496 *
1497 * \param[in] attributes Key attributes for the new key.
1498 * \param[out] p_drv On any return, the driver for the key, if any.
1499 * NULL for a transparent key.
1500 *
1501 */
1502static psa_status_t psa_validate_key_attributes(
1503 const psa_key_attributes_t *attributes,
1504 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001505{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001506 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001507
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001508 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1509 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001510 if( status != PSA_SUCCESS )
1511 return( status );
1512
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001513 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1514 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001515 if( status != PSA_SUCCESS )
1516 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001517
1518 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001519 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001520 return( status );
1521
1522 /* Refuse to create overly large keys.
1523 * Note that this doesn't trigger on import if the attributes don't
1524 * explicitly specify a size (so psa_get_key_bits returns 0), so
1525 * psa_import_key() needs its own checks. */
1526 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1527 return( PSA_ERROR_NOT_SUPPORTED );
1528
Gilles Peskine91e8c332019-08-02 19:19:39 +02001529 /* Reject invalid flags. These should not be reachable through the API. */
1530 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1531 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1532 return( PSA_ERROR_INVALID_ARGUMENT );
1533
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001534 return( PSA_SUCCESS );
1535}
1536
Gilles Peskine4747d192019-04-17 15:05:45 +02001537/** Prepare a key slot to receive key material.
1538 *
1539 * This function allocates a key slot and sets its metadata.
1540 *
1541 * If this function fails, call psa_fail_key_creation().
1542 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001543 * This function is intended to be used as follows:
1544 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1545 * it with the specified attributes, and assign it a handle.
1546 * -# Populate the slot with the key material.
1547 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1548 * In case of failure at any step, stop the sequence and call
1549 * psa_fail_key_creation().
1550 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001551 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001552 * \param[in] attributes Key attributes for the new key.
1553 * \param[out] handle On success, a handle for the allocated slot.
1554 * \param[out] p_slot On success, a pointer to the prepared slot.
1555 * \param[out] p_drv On any return, the driver for the key, if any.
1556 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001557 *
1558 * \retval #PSA_SUCCESS
1559 * The key slot is ready to receive key material.
1560 * \return If this function fails, the key slot is an invalid state.
1561 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001562 */
1563static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001564 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001565 const psa_key_attributes_t *attributes,
1566 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001567 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001568 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001569{
1570 psa_status_t status;
1571 psa_key_slot_t *slot;
1572
Gilles Peskinedf179142019-07-15 22:02:14 +02001573 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001574 *p_drv = NULL;
1575
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001576 status = psa_validate_key_attributes( attributes, p_drv );
1577 if( status != PSA_SUCCESS )
1578 return( status );
1579
Gilles Peskineedbed562019-08-07 18:19:59 +02001580 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001581 if( status != PSA_SUCCESS )
1582 return( status );
1583 slot = *p_slot;
1584
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001585 /* We're storing the declared bit-size of the key. It's up to each
1586 * creation mechanism to verify that this information is correct.
1587 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001588 * an input (generate, device) but not for those where the bit-size
1589 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001590
1591 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001592
Gilles Peskine91e8c332019-08-02 19:19:39 +02001593 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001594 * external-only flags, query `attributes`. Thanks to the check
1595 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001596 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001597 * may have set. */
1598 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001599
Gilles Peskinecbaff462019-07-12 23:46:04 +02001600#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001601 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001602 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001603 * create the key file in internal storage, create the
1604 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001605 * persistent data. This is done by starting a transaction that will
1606 * encompass these three actions.
1607 * For registering a volatile key, we just need to find an appropriate
1608 * slot number inside the SE. Since the key is designated volatile, creating
1609 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001610 /* The first thing to do is to find a slot number for the new key.
1611 * We save the slot number in persistent storage as part of the
1612 * transaction data. It will be needed to recover if the power
1613 * fails during the key creation process, to clean up on the secure
1614 * element side after restarting. Obtaining a slot number from the
1615 * secure element driver updates its persistent state, but we do not yet
1616 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001617 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001618 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001619 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001620 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001621 &slot->data.se.slot_number );
1622 if( status != PSA_SUCCESS )
1623 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001624
1625 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001626 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001627 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1628 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1629 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1630 psa_crypto_transaction.key.id = slot->attr.id;
1631 status = psa_crypto_save_transaction( );
1632 if( status != PSA_SUCCESS )
1633 {
1634 (void) psa_crypto_stop_transaction( );
1635 return( status );
1636 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001637 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001638 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001639
1640 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1641 {
1642 /* Key registration only makes sense with a secure element. */
1643 return( PSA_ERROR_INVALID_ARGUMENT );
1644 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001645#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1646
Darryl Green0c6575a2018-11-07 16:05:30 +00001647 return( status );
1648}
Gilles Peskine4747d192019-04-17 15:05:45 +02001649
1650/** Finalize the creation of a key once its key material has been set.
1651 *
1652 * This entails writing the key to persistent storage.
1653 *
1654 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001655 * See the documentation of psa_start_key_creation() for the intended use
1656 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001657 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001658 * \param[in,out] slot Pointer to the slot with key material.
1659 * \param[in] driver The secure element driver for the key,
1660 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001661 *
1662 * \retval #PSA_SUCCESS
1663 * The key was successfully created. The handle is now valid.
1664 * \return If this function fails, the key slot is an invalid state.
1665 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001666 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001667static psa_status_t psa_finish_key_creation(
1668 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001669 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001670{
1671 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001672 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001673 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001674
1675#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001676 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001677 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001678#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1679 if( driver != NULL )
1680 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001681 psa_se_key_data_storage_t data;
1682#if defined(static_assert)
1683 static_assert( sizeof( slot->data.se.slot_number ) ==
1684 sizeof( data.slot_number ),
1685 "Slot number size does not match psa_se_key_data_storage_t" );
1686 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1687 "Bit-size size does not match psa_se_key_data_storage_t" );
1688#endif
1689 memcpy( &data.slot_number, &slot->data.se.slot_number,
1690 sizeof( slot->data.se.slot_number ) );
1691 memcpy( &data.bits, &slot->attr.bits,
1692 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001693 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001694 (uint8_t*) &data,
1695 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001696 }
1697 else
1698#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1699 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001700 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001701 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001702 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001703 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1704 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001705 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001706 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1707 status = psa_internal_export_key( slot,
1708 buffer, buffer_size, &length,
1709 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001710 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001711 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001712 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001713
Gilles Peskinef9168942019-09-12 19:20:29 +02001714 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001715 mbedtls_free( buffer );
1716 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001717 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001718#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1719
Gilles Peskinecbaff462019-07-12 23:46:04 +02001720#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001721 /* Finish the transaction for a key creation. This does not
1722 * happen when registering an existing key. Detect this case
1723 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001724 * creation of a persistent key in a secure element requires a transaction,
1725 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001726 if( driver != NULL &&
1727 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001728 {
1729 status = psa_save_se_persistent_data( driver );
1730 if( status != PSA_SUCCESS )
1731 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001732 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001733 return( status );
1734 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001735 status = psa_crypto_stop_transaction( );
1736 if( status != PSA_SUCCESS )
1737 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001738 }
1739#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1740
Gilles Peskine4747d192019-04-17 15:05:45 +02001741 return( status );
1742}
1743
1744/** Abort the creation of a key.
1745 *
1746 * You may call this function after calling psa_start_key_creation(),
1747 * or after psa_finish_key_creation() fails. In other circumstances, this
1748 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001749 * See the documentation of psa_start_key_creation() for the intended use
1750 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001751 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001752 * \param[in,out] slot Pointer to the slot with key material.
1753 * \param[in] driver The secure element driver for the key,
1754 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001755 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001756static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001757 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001758{
Gilles Peskine011e4282019-06-26 18:34:38 +02001759 (void) driver;
1760
Gilles Peskine4747d192019-04-17 15:05:45 +02001761 if( slot == NULL )
1762 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001763
1764#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001765 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001766 * element, and the failure happened later (when saving metadata
1767 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001768 * element.
1769 * https://github.com/ARMmbed/mbed-crypto/issues/217
1770 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001771
Gilles Peskined7729582019-08-05 15:55:54 +02001772 /* Abort the ongoing transaction if any (there may not be one if
1773 * the creation process failed before starting one, or if the
1774 * key creation is a registration of a key in a secure element).
1775 * Earlier functions must already have done what it takes to undo any
1776 * partial creation. All that's left is to update the transaction data
1777 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001778 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001779#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1780
Gilles Peskine4747d192019-04-17 15:05:45 +02001781 psa_wipe_key_slot( slot );
1782}
1783
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001784/** Validate optional attributes during key creation.
1785 *
1786 * Some key attributes are optional during key creation. If they are
1787 * specified in the attributes structure, check that they are consistent
1788 * with the data in the slot.
1789 *
1790 * This function should be called near the end of key creation, after
1791 * the slot in memory is fully populated but before saving persistent data.
1792 */
1793static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001794 const psa_key_slot_t *slot,
1795 const psa_key_attributes_t *attributes )
1796{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001797 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001798 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001799 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001800 return( PSA_ERROR_INVALID_ARGUMENT );
1801 }
1802
1803 if( attributes->domain_parameters_size != 0 )
1804 {
1805#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001806 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001807 {
1808 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001809 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001810 mbedtls_mpi_init( &actual );
1811 mbedtls_mpi_init( &required );
1812 ret = mbedtls_rsa_export( slot->data.rsa,
1813 NULL, NULL, NULL, NULL, &actual );
1814 if( ret != 0 )
1815 goto rsa_exit;
1816 ret = mbedtls_mpi_read_binary( &required,
1817 attributes->domain_parameters,
1818 attributes->domain_parameters_size );
1819 if( ret != 0 )
1820 goto rsa_exit;
1821 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1822 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1823 rsa_exit:
1824 mbedtls_mpi_free( &actual );
1825 mbedtls_mpi_free( &required );
1826 if( ret != 0)
1827 return( mbedtls_to_psa_error( ret ) );
1828 }
1829 else
1830#endif
1831 {
1832 return( PSA_ERROR_INVALID_ARGUMENT );
1833 }
1834 }
1835
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001836 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001837 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001838 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001839 return( PSA_ERROR_INVALID_ARGUMENT );
1840 }
1841
1842 return( PSA_SUCCESS );
1843}
1844
Gilles Peskine4747d192019-04-17 15:05:45 +02001845psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001846 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001847 size_t data_length,
1848 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001849{
1850 psa_status_t status;
1851 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001852 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001853
Gilles Peskine0f84d622019-09-12 19:03:13 +02001854 /* Reject zero-length symmetric keys (including raw data key objects).
1855 * This also rejects any key which might be encoded as an empty string,
1856 * which is never valid. */
1857 if( data_length == 0 )
1858 return( PSA_ERROR_INVALID_ARGUMENT );
1859
Gilles Peskinedf179142019-07-15 22:02:14 +02001860 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1861 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001862 if( status != PSA_SUCCESS )
1863 goto exit;
1864
Gilles Peskine5d309672019-07-12 23:47:28 +02001865#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1866 if( driver != NULL )
1867 {
1868 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001869 /* The driver should set the number of key bits, however in
1870 * case it doesn't, we initialize bits to an invalid value. */
1871 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001872 if( drv->key_management == NULL ||
1873 drv->key_management->p_import == NULL )
1874 {
1875 status = PSA_ERROR_NOT_SUPPORTED;
1876 goto exit;
1877 }
1878 status = drv->key_management->p_import(
1879 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001880 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001881 &bits );
1882 if( status != PSA_SUCCESS )
1883 goto exit;
1884 if( bits > PSA_MAX_KEY_BITS )
1885 {
1886 status = PSA_ERROR_NOT_SUPPORTED;
1887 goto exit;
1888 }
1889 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001890 }
1891 else
1892#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1893 {
1894 status = psa_import_key_into_slot( slot, data, data_length );
1895 if( status != PSA_SUCCESS )
1896 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001897 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001898 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001899 if( status != PSA_SUCCESS )
1900 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001901
Gilles Peskine011e4282019-06-26 18:34:38 +02001902 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001903exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001904 if( status != PSA_SUCCESS )
1905 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001906 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001907 *handle = 0;
1908 }
1909 return( status );
1910}
1911
Gilles Peskined7729582019-08-05 15:55:54 +02001912#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1913psa_status_t mbedtls_psa_register_se_key(
1914 const psa_key_attributes_t *attributes )
1915{
1916 psa_status_t status;
1917 psa_key_slot_t *slot = NULL;
1918 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001919 psa_key_handle_t handle = 0;
1920
1921 /* Leaving attributes unspecified is not currently supported.
1922 * It could make sense to query the key type and size from the
1923 * secure element, but not all secure elements support this
1924 * and the driver HAL doesn't currently support it. */
1925 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1926 return( PSA_ERROR_NOT_SUPPORTED );
1927 if( psa_get_key_bits( attributes ) == 0 )
1928 return( PSA_ERROR_NOT_SUPPORTED );
1929
1930 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1931 &handle, &slot, &driver );
1932 if( status != PSA_SUCCESS )
1933 goto exit;
1934
Gilles Peskined7729582019-08-05 15:55:54 +02001935 status = psa_finish_key_creation( slot, driver );
1936
1937exit:
1938 if( status != PSA_SUCCESS )
1939 {
1940 psa_fail_key_creation( slot, driver );
1941 }
1942 /* Registration doesn't keep the key in RAM. */
1943 psa_close_key( handle );
1944 return( status );
1945}
1946#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1947
Gilles Peskinef603c712019-01-19 13:40:11 +01001948static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001949 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001950{
1951 psa_status_t status;
1952 uint8_t *buffer = NULL;
1953 size_t buffer_size = 0;
1954 size_t length;
1955
Gilles Peskine8e338702019-07-30 20:06:31 +02001956 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001957 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001958 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001959 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001960 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001961 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1962 if( status != PSA_SUCCESS )
1963 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001964 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001965 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001966
1967exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001968 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001969 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001970 return( status );
1971}
1972
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001973psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1974 const psa_key_attributes_t *specified_attributes,
1975 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001976{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001977 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001978 psa_key_slot_t *source_slot = NULL;
1979 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001980 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001981 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001982
Gilles Peskine28f8f302019-07-24 13:30:31 +02001983 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001984 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001985 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001986 goto exit;
1987
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001988 status = psa_validate_optional_attributes( source_slot,
1989 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001990 if( status != PSA_SUCCESS )
1991 goto exit;
1992
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001993 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001994 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001995 if( status != PSA_SUCCESS )
1996 goto exit;
1997
Gilles Peskinedf179142019-07-15 22:02:14 +02001998 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1999 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002000 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002001 if( status != PSA_SUCCESS )
2002 goto exit;
2003
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002004#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2005 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002006 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002007 /* Copying to a secure element is not implemented yet. */
2008 status = PSA_ERROR_NOT_SUPPORTED;
2009 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002010 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002011#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002012
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002013 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002014 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002015 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002016
Gilles Peskine011e4282019-06-26 18:34:38 +02002017 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002018exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002019 if( status != PSA_SUCCESS )
2020 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002021 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002022 *target_handle = 0;
2023 }
2024 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002025}
2026
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002027
2028
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002029/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002030/* Message digests */
2031/****************************************************************/
2032
Gilles Peskineb16841e2019-10-10 20:36:12 +02002033#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002034static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002035{
2036 switch( alg )
2037 {
2038#if defined(MBEDTLS_MD2_C)
2039 case PSA_ALG_MD2:
2040 return( &mbedtls_md2_info );
2041#endif
2042#if defined(MBEDTLS_MD4_C)
2043 case PSA_ALG_MD4:
2044 return( &mbedtls_md4_info );
2045#endif
2046#if defined(MBEDTLS_MD5_C)
2047 case PSA_ALG_MD5:
2048 return( &mbedtls_md5_info );
2049#endif
2050#if defined(MBEDTLS_RIPEMD160_C)
2051 case PSA_ALG_RIPEMD160:
2052 return( &mbedtls_ripemd160_info );
2053#endif
2054#if defined(MBEDTLS_SHA1_C)
2055 case PSA_ALG_SHA_1:
2056 return( &mbedtls_sha1_info );
2057#endif
2058#if defined(MBEDTLS_SHA256_C)
2059 case PSA_ALG_SHA_224:
2060 return( &mbedtls_sha224_info );
2061 case PSA_ALG_SHA_256:
2062 return( &mbedtls_sha256_info );
2063#endif
2064#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002065#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002066 case PSA_ALG_SHA_384:
2067 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002068#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002069 case PSA_ALG_SHA_512:
2070 return( &mbedtls_sha512_info );
2071#endif
2072 default:
2073 return( NULL );
2074 }
2075}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002076#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002077
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002078psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2079{
2080 switch( operation->alg )
2081 {
Gilles Peskine81736312018-06-26 15:04:31 +02002082 case 0:
2083 /* The object has (apparently) been initialized but it is not
2084 * in use. It's ok to call abort on such an object, and there's
2085 * nothing to do. */
2086 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002087#if defined(MBEDTLS_MD2_C)
2088 case PSA_ALG_MD2:
2089 mbedtls_md2_free( &operation->ctx.md2 );
2090 break;
2091#endif
2092#if defined(MBEDTLS_MD4_C)
2093 case PSA_ALG_MD4:
2094 mbedtls_md4_free( &operation->ctx.md4 );
2095 break;
2096#endif
2097#if defined(MBEDTLS_MD5_C)
2098 case PSA_ALG_MD5:
2099 mbedtls_md5_free( &operation->ctx.md5 );
2100 break;
2101#endif
2102#if defined(MBEDTLS_RIPEMD160_C)
2103 case PSA_ALG_RIPEMD160:
2104 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2105 break;
2106#endif
2107#if defined(MBEDTLS_SHA1_C)
2108 case PSA_ALG_SHA_1:
2109 mbedtls_sha1_free( &operation->ctx.sha1 );
2110 break;
2111#endif
2112#if defined(MBEDTLS_SHA256_C)
2113 case PSA_ALG_SHA_224:
2114 case PSA_ALG_SHA_256:
2115 mbedtls_sha256_free( &operation->ctx.sha256 );
2116 break;
2117#endif
2118#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002119#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002120 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002121#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002122 case PSA_ALG_SHA_512:
2123 mbedtls_sha512_free( &operation->ctx.sha512 );
2124 break;
2125#endif
2126 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002127 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002128 }
2129 operation->alg = 0;
2130 return( PSA_SUCCESS );
2131}
2132
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002133psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002134 psa_algorithm_t alg )
2135{
Janos Follath24eed8d2019-11-22 13:21:35 +00002136 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002137
2138 /* A context must be freshly initialized before it can be set up. */
2139 if( operation->alg != 0 )
2140 {
2141 return( PSA_ERROR_BAD_STATE );
2142 }
2143
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002144 switch( alg )
2145 {
2146#if defined(MBEDTLS_MD2_C)
2147 case PSA_ALG_MD2:
2148 mbedtls_md2_init( &operation->ctx.md2 );
2149 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2150 break;
2151#endif
2152#if defined(MBEDTLS_MD4_C)
2153 case PSA_ALG_MD4:
2154 mbedtls_md4_init( &operation->ctx.md4 );
2155 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2156 break;
2157#endif
2158#if defined(MBEDTLS_MD5_C)
2159 case PSA_ALG_MD5:
2160 mbedtls_md5_init( &operation->ctx.md5 );
2161 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2162 break;
2163#endif
2164#if defined(MBEDTLS_RIPEMD160_C)
2165 case PSA_ALG_RIPEMD160:
2166 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2167 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2168 break;
2169#endif
2170#if defined(MBEDTLS_SHA1_C)
2171 case PSA_ALG_SHA_1:
2172 mbedtls_sha1_init( &operation->ctx.sha1 );
2173 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2174 break;
2175#endif
2176#if defined(MBEDTLS_SHA256_C)
2177 case PSA_ALG_SHA_224:
2178 mbedtls_sha256_init( &operation->ctx.sha256 );
2179 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2180 break;
2181 case PSA_ALG_SHA_256:
2182 mbedtls_sha256_init( &operation->ctx.sha256 );
2183 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2184 break;
2185#endif
2186#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002187#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002188 case PSA_ALG_SHA_384:
2189 mbedtls_sha512_init( &operation->ctx.sha512 );
2190 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2191 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002192#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002193 case PSA_ALG_SHA_512:
2194 mbedtls_sha512_init( &operation->ctx.sha512 );
2195 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2196 break;
2197#endif
2198 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002199 return( PSA_ALG_IS_HASH( alg ) ?
2200 PSA_ERROR_NOT_SUPPORTED :
2201 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002202 }
2203 if( ret == 0 )
2204 operation->alg = alg;
2205 else
2206 psa_hash_abort( operation );
2207 return( mbedtls_to_psa_error( ret ) );
2208}
2209
2210psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2211 const uint8_t *input,
2212 size_t input_length )
2213{
Janos Follath24eed8d2019-11-22 13:21:35 +00002214 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002215
2216 /* Don't require hash implementations to behave correctly on a
2217 * zero-length input, which may have an invalid pointer. */
2218 if( input_length == 0 )
2219 return( PSA_SUCCESS );
2220
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002221 switch( operation->alg )
2222 {
2223#if defined(MBEDTLS_MD2_C)
2224 case PSA_ALG_MD2:
2225 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2226 input, input_length );
2227 break;
2228#endif
2229#if defined(MBEDTLS_MD4_C)
2230 case PSA_ALG_MD4:
2231 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2232 input, input_length );
2233 break;
2234#endif
2235#if defined(MBEDTLS_MD5_C)
2236 case PSA_ALG_MD5:
2237 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2238 input, input_length );
2239 break;
2240#endif
2241#if defined(MBEDTLS_RIPEMD160_C)
2242 case PSA_ALG_RIPEMD160:
2243 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2244 input, input_length );
2245 break;
2246#endif
2247#if defined(MBEDTLS_SHA1_C)
2248 case PSA_ALG_SHA_1:
2249 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2250 input, input_length );
2251 break;
2252#endif
2253#if defined(MBEDTLS_SHA256_C)
2254 case PSA_ALG_SHA_224:
2255 case PSA_ALG_SHA_256:
2256 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2257 input, input_length );
2258 break;
2259#endif
2260#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002261#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002262 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002263#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002264 case PSA_ALG_SHA_512:
2265 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2266 input, input_length );
2267 break;
2268#endif
2269 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002270 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002271 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002272
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002273 if( ret != 0 )
2274 psa_hash_abort( operation );
2275 return( mbedtls_to_psa_error( ret ) );
2276}
2277
2278psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2279 uint8_t *hash,
2280 size_t hash_size,
2281 size_t *hash_length )
2282{
itayzafrir40835d42018-08-02 13:14:17 +03002283 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002284 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002285 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002286
2287 /* Fill the output buffer with something that isn't a valid hash
2288 * (barring an attack on the hash and deliberately-crafted input),
2289 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002290 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002291 /* If hash_size is 0 then hash may be NULL and then the
2292 * call to memset would have undefined behavior. */
2293 if( hash_size != 0 )
2294 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002295
2296 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002297 {
2298 status = PSA_ERROR_BUFFER_TOO_SMALL;
2299 goto exit;
2300 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002301
2302 switch( operation->alg )
2303 {
2304#if defined(MBEDTLS_MD2_C)
2305 case PSA_ALG_MD2:
2306 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2307 break;
2308#endif
2309#if defined(MBEDTLS_MD4_C)
2310 case PSA_ALG_MD4:
2311 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2312 break;
2313#endif
2314#if defined(MBEDTLS_MD5_C)
2315 case PSA_ALG_MD5:
2316 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2317 break;
2318#endif
2319#if defined(MBEDTLS_RIPEMD160_C)
2320 case PSA_ALG_RIPEMD160:
2321 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2322 break;
2323#endif
2324#if defined(MBEDTLS_SHA1_C)
2325 case PSA_ALG_SHA_1:
2326 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2327 break;
2328#endif
2329#if defined(MBEDTLS_SHA256_C)
2330 case PSA_ALG_SHA_224:
2331 case PSA_ALG_SHA_256:
2332 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2333 break;
2334#endif
2335#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002336#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002337 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002338#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002339 case PSA_ALG_SHA_512:
2340 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2341 break;
2342#endif
2343 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002344 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002345 }
itayzafrir40835d42018-08-02 13:14:17 +03002346 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002347
itayzafrir40835d42018-08-02 13:14:17 +03002348exit:
2349 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002350 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002351 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002352 return( psa_hash_abort( operation ) );
2353 }
2354 else
2355 {
2356 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002357 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002358 }
2359}
2360
Gilles Peskine2d277862018-06-18 15:41:12 +02002361psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2362 const uint8_t *hash,
2363 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002364{
2365 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2366 size_t actual_hash_length;
2367 psa_status_t status = psa_hash_finish( operation,
2368 actual_hash, sizeof( actual_hash ),
2369 &actual_hash_length );
2370 if( status != PSA_SUCCESS )
2371 return( status );
2372 if( actual_hash_length != hash_length )
2373 return( PSA_ERROR_INVALID_SIGNATURE );
2374 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2375 return( PSA_ERROR_INVALID_SIGNATURE );
2376 return( PSA_SUCCESS );
2377}
2378
Gilles Peskine0a749c82019-11-28 19:33:58 +01002379psa_status_t psa_hash_compute( psa_algorithm_t alg,
2380 const uint8_t *input, size_t input_length,
2381 uint8_t *hash, size_t hash_size,
2382 size_t *hash_length )
2383{
2384 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2385 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2386
2387 *hash_length = hash_size;
2388 status = psa_hash_setup( &operation, alg );
2389 if( status != PSA_SUCCESS )
2390 goto exit;
2391 status = psa_hash_update( &operation, input, input_length );
2392 if( status != PSA_SUCCESS )
2393 goto exit;
2394 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2395 if( status != PSA_SUCCESS )
2396 goto exit;
2397
2398exit:
2399 if( status == PSA_SUCCESS )
2400 status = psa_hash_abort( &operation );
2401 else
2402 psa_hash_abort( &operation );
2403 return( status );
2404}
2405
2406psa_status_t psa_hash_compare( psa_algorithm_t alg,
2407 const uint8_t *input, size_t input_length,
2408 const uint8_t *hash, size_t hash_length )
2409{
2410 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2411 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2412
2413 status = psa_hash_setup( &operation, alg );
2414 if( status != PSA_SUCCESS )
2415 goto exit;
2416 status = psa_hash_update( &operation, input, input_length );
2417 if( status != PSA_SUCCESS )
2418 goto exit;
2419 status = psa_hash_verify( &operation, hash, hash_length );
2420 if( status != PSA_SUCCESS )
2421 goto exit;
2422
2423exit:
2424 if( status == PSA_SUCCESS )
2425 status = psa_hash_abort( &operation );
2426 else
2427 psa_hash_abort( &operation );
2428 return( status );
2429}
2430
Gilles Peskineeb35d782019-01-22 17:56:16 +01002431psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2432 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002433{
2434 if( target_operation->alg != 0 )
2435 return( PSA_ERROR_BAD_STATE );
2436
2437 switch( source_operation->alg )
2438 {
2439 case 0:
2440 return( PSA_ERROR_BAD_STATE );
2441#if defined(MBEDTLS_MD2_C)
2442 case PSA_ALG_MD2:
2443 mbedtls_md2_clone( &target_operation->ctx.md2,
2444 &source_operation->ctx.md2 );
2445 break;
2446#endif
2447#if defined(MBEDTLS_MD4_C)
2448 case PSA_ALG_MD4:
2449 mbedtls_md4_clone( &target_operation->ctx.md4,
2450 &source_operation->ctx.md4 );
2451 break;
2452#endif
2453#if defined(MBEDTLS_MD5_C)
2454 case PSA_ALG_MD5:
2455 mbedtls_md5_clone( &target_operation->ctx.md5,
2456 &source_operation->ctx.md5 );
2457 break;
2458#endif
2459#if defined(MBEDTLS_RIPEMD160_C)
2460 case PSA_ALG_RIPEMD160:
2461 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2462 &source_operation->ctx.ripemd160 );
2463 break;
2464#endif
2465#if defined(MBEDTLS_SHA1_C)
2466 case PSA_ALG_SHA_1:
2467 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2468 &source_operation->ctx.sha1 );
2469 break;
2470#endif
2471#if defined(MBEDTLS_SHA256_C)
2472 case PSA_ALG_SHA_224:
2473 case PSA_ALG_SHA_256:
2474 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2475 &source_operation->ctx.sha256 );
2476 break;
2477#endif
2478#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002479#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002480 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002481#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002482 case PSA_ALG_SHA_512:
2483 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2484 &source_operation->ctx.sha512 );
2485 break;
2486#endif
2487 default:
2488 return( PSA_ERROR_NOT_SUPPORTED );
2489 }
2490
2491 target_operation->alg = source_operation->alg;
2492 return( PSA_SUCCESS );
2493}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002494
2495
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002496/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002497/* MAC */
2498/****************************************************************/
2499
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002500static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002501 psa_algorithm_t alg,
2502 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002503 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002504 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002505{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002506 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002507 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002508
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002509 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002510 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002511
Gilles Peskine8c9def32018-02-08 10:02:12 +01002512 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2513 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002514 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002515 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002516 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002517 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002518 mode = MBEDTLS_MODE_STREAM;
2519 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002520 case PSA_ALG_CTR:
2521 mode = MBEDTLS_MODE_CTR;
2522 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002523 case PSA_ALG_CFB:
2524 mode = MBEDTLS_MODE_CFB;
2525 break;
2526 case PSA_ALG_OFB:
2527 mode = MBEDTLS_MODE_OFB;
2528 break;
2529 case PSA_ALG_CBC_NO_PADDING:
2530 mode = MBEDTLS_MODE_CBC;
2531 break;
2532 case PSA_ALG_CBC_PKCS7:
2533 mode = MBEDTLS_MODE_CBC;
2534 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002535 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002536 mode = MBEDTLS_MODE_CCM;
2537 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002538 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002539 mode = MBEDTLS_MODE_GCM;
2540 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002541 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2542 mode = MBEDTLS_MODE_CHACHAPOLY;
2543 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002544 default:
2545 return( NULL );
2546 }
2547 }
2548 else if( alg == PSA_ALG_CMAC )
2549 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002550 else
2551 return( NULL );
2552
2553 switch( key_type )
2554 {
2555 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002556 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002557 break;
2558 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002559 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2560 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002561 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002562 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002563 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002564 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002565 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2566 * but two-key Triple-DES is functionally three-key Triple-DES
2567 * with K1=K3, so that's how we present it to mbedtls. */
2568 if( key_bits == 128 )
2569 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002570 break;
2571 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002572 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002573 break;
2574 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002575 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002576 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002577 case PSA_KEY_TYPE_CHACHA20:
2578 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2579 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002580 default:
2581 return( NULL );
2582 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002583 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002584 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002585
Jaeden Amero23bbb752018-06-26 14:16:54 +01002586 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2587 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002588}
2589
Gilles Peskinea05219c2018-11-16 16:02:56 +01002590#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002591static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002592{
Gilles Peskine2d277862018-06-18 15:41:12 +02002593 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002594 {
2595 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002596 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002597 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002598 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002599 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002600 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002601 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002602 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002603 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002604 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002605 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002606 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002607 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002608 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002609 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002610 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002611 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002612 return( 128 );
2613 default:
2614 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002615 }
2616}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002617#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002618
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002619/* Initialize the MAC operation structure. Once this function has been
2620 * called, psa_mac_abort can run and will do the right thing. */
2621static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2622 psa_algorithm_t alg )
2623{
2624 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2625
2626 operation->alg = alg;
2627 operation->key_set = 0;
2628 operation->iv_set = 0;
2629 operation->iv_required = 0;
2630 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002631 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002632
2633#if defined(MBEDTLS_CMAC_C)
2634 if( alg == PSA_ALG_CMAC )
2635 {
2636 operation->iv_required = 0;
2637 mbedtls_cipher_init( &operation->ctx.cmac );
2638 status = PSA_SUCCESS;
2639 }
2640 else
2641#endif /* MBEDTLS_CMAC_C */
2642#if defined(MBEDTLS_MD_C)
2643 if( PSA_ALG_IS_HMAC( operation->alg ) )
2644 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002645 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2646 operation->ctx.hmac.hash_ctx.alg = 0;
2647 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002648 }
2649 else
2650#endif /* MBEDTLS_MD_C */
2651 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002652 if( ! PSA_ALG_IS_MAC( alg ) )
2653 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002654 }
2655
2656 if( status != PSA_SUCCESS )
2657 memset( operation, 0, sizeof( *operation ) );
2658 return( status );
2659}
2660
Gilles Peskine01126fa2018-07-12 17:04:55 +02002661#if defined(MBEDTLS_MD_C)
2662static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2663{
Gilles Peskine3f108122018-12-07 18:14:53 +01002664 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002665 return( psa_hash_abort( &hmac->hash_ctx ) );
2666}
2667#endif /* MBEDTLS_MD_C */
2668
Gilles Peskine8c9def32018-02-08 10:02:12 +01002669psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2670{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002671 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002672 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002673 /* The object has (apparently) been initialized but it is not
2674 * in use. It's ok to call abort on such an object, and there's
2675 * nothing to do. */
2676 return( PSA_SUCCESS );
2677 }
2678 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002679#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002680 if( operation->alg == PSA_ALG_CMAC )
2681 {
2682 mbedtls_cipher_free( &operation->ctx.cmac );
2683 }
2684 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002685#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002686#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002687 if( PSA_ALG_IS_HMAC( operation->alg ) )
2688 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002689 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002690 }
2691 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002692#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002693 {
2694 /* Sanity check (shouldn't happen: operation->alg should
2695 * always have been initialized to a valid value). */
2696 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002697 }
Moran Peker41deec42018-04-04 15:43:05 +03002698
Gilles Peskine8c9def32018-02-08 10:02:12 +01002699 operation->alg = 0;
2700 operation->key_set = 0;
2701 operation->iv_set = 0;
2702 operation->iv_required = 0;
2703 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002704 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002705
Gilles Peskine8c9def32018-02-08 10:02:12 +01002706 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002707
2708bad_state:
2709 /* If abort is called on an uninitialized object, we can't trust
2710 * anything. Wipe the object in case it contains confidential data.
2711 * This may result in a memory leak if a pointer gets overwritten,
2712 * but it's too late to do anything about this. */
2713 memset( operation, 0, sizeof( *operation ) );
2714 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002715}
2716
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002717#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002718static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002719 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002720 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002721 const mbedtls_cipher_info_t *cipher_info )
2722{
Janos Follath24eed8d2019-11-22 13:21:35 +00002723 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002724
2725 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002726
2727 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2728 if( ret != 0 )
2729 return( ret );
2730
2731 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02002732 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002733 key_bits );
2734 return( ret );
2735}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002736#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002737
Gilles Peskine248051a2018-06-20 16:09:38 +02002738#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002739static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2740 const uint8_t *key,
2741 size_t key_length,
2742 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002743{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002744 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002745 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002746 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002747 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002748 psa_status_t status;
2749
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002750 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2751 * overflow below. This should never trigger if the hash algorithm
2752 * is implemented correctly. */
2753 /* The size checks against the ipad and opad buffers cannot be written
2754 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2755 * because that triggers -Wlogical-op on GCC 7.3. */
2756 if( block_size > sizeof( ipad ) )
2757 return( PSA_ERROR_NOT_SUPPORTED );
2758 if( block_size > sizeof( hmac->opad ) )
2759 return( PSA_ERROR_NOT_SUPPORTED );
2760 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002761 return( PSA_ERROR_NOT_SUPPORTED );
2762
Gilles Peskined223b522018-06-11 18:12:58 +02002763 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002764 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002765 status = psa_hash_compute( hash_alg, key, key_length,
2766 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002767 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002768 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002769 }
Gilles Peskine96889972018-07-12 17:07:03 +02002770 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2771 * but it is permitted. It is common when HMAC is used in HKDF, for
2772 * example. Don't call `memcpy` in the 0-length because `key` could be
2773 * an invalid pointer which would make the behavior undefined. */
2774 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002775 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002776
Gilles Peskined223b522018-06-11 18:12:58 +02002777 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2778 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002779 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002780 ipad[i] ^= 0x36;
2781 memset( ipad + key_length, 0x36, block_size - key_length );
2782
2783 /* Copy the key material from ipad to opad, flipping the requisite bits,
2784 * and filling the rest of opad with the requisite constant. */
2785 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002786 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2787 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002788
Gilles Peskine01126fa2018-07-12 17:04:55 +02002789 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002790 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002791 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002792
Gilles Peskine01126fa2018-07-12 17:04:55 +02002793 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002794
2795cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002796 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002797
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002798 return( status );
2799}
Gilles Peskine248051a2018-06-20 16:09:38 +02002800#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002801
Gilles Peskine89167cb2018-07-08 20:12:23 +02002802static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002803 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002804 psa_algorithm_t alg,
2805 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002806{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002807 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002808 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002809 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002810 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002811 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002812 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002813 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002814
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002815 /* A context must be freshly initialized before it can be set up. */
2816 if( operation->alg != 0 )
2817 {
2818 return( PSA_ERROR_BAD_STATE );
2819 }
2820
Gilles Peskined911eb72018-08-14 15:18:45 +02002821 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002822 if( status != PSA_SUCCESS )
2823 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002824 if( is_sign )
2825 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002826
Gilles Peskine28f8f302019-07-24 13:30:31 +02002827 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002828 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002829 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002830 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002831
Gilles Peskine8c9def32018-02-08 10:02:12 +01002832#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002833 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002834 {
2835 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002836 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002837 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002838 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002839 if( cipher_info == NULL )
2840 {
2841 status = PSA_ERROR_NOT_SUPPORTED;
2842 goto exit;
2843 }
2844 operation->mac_size = cipher_info->block_size;
2845 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2846 status = mbedtls_to_psa_error( ret );
2847 }
2848 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002849#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002850#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002851 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002852 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002853 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002854 if( hash_alg == 0 )
2855 {
2856 status = PSA_ERROR_NOT_SUPPORTED;
2857 goto exit;
2858 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002859
2860 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2861 /* Sanity check. This shouldn't fail on a valid configuration. */
2862 if( operation->mac_size == 0 ||
2863 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2864 {
2865 status = PSA_ERROR_NOT_SUPPORTED;
2866 goto exit;
2867 }
2868
Gilles Peskine8e338702019-07-30 20:06:31 +02002869 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002870 {
2871 status = PSA_ERROR_INVALID_ARGUMENT;
2872 goto exit;
2873 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002874
Gilles Peskine01126fa2018-07-12 17:04:55 +02002875 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02002876 slot->data.key.data,
2877 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02002878 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002879 }
2880 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002881#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002882 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002883 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002884 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002885 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002886
Gilles Peskined911eb72018-08-14 15:18:45 +02002887 if( truncated == 0 )
2888 {
2889 /* The "normal" case: untruncated algorithm. Nothing to do. */
2890 }
2891 else if( truncated < 4 )
2892 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002893 /* A very short MAC is too short for security since it can be
2894 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2895 * so we make this our minimum, even though 32 bits is still
2896 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002897 status = PSA_ERROR_NOT_SUPPORTED;
2898 }
2899 else if( truncated > operation->mac_size )
2900 {
2901 /* It's impossible to "truncate" to a larger length. */
2902 status = PSA_ERROR_INVALID_ARGUMENT;
2903 }
2904 else
2905 operation->mac_size = truncated;
2906
Gilles Peskinefbfac682018-07-08 20:51:54 +02002907exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002908 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002909 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002910 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002911 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002912 else
2913 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002914 operation->key_set = 1;
2915 }
2916 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002917}
2918
Gilles Peskine89167cb2018-07-08 20:12:23 +02002919psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002920 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002921 psa_algorithm_t alg )
2922{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002923 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002924}
2925
2926psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002927 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002928 psa_algorithm_t alg )
2929{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002930 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002931}
2932
Gilles Peskine8c9def32018-02-08 10:02:12 +01002933psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2934 const uint8_t *input,
2935 size_t input_length )
2936{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002937 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002938 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002939 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002940 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002941 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002942 operation->has_input = 1;
2943
Gilles Peskine8c9def32018-02-08 10:02:12 +01002944#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002945 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002946 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002947 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2948 input, input_length );
2949 status = mbedtls_to_psa_error( ret );
2950 }
2951 else
2952#endif /* MBEDTLS_CMAC_C */
2953#if defined(MBEDTLS_MD_C)
2954 if( PSA_ALG_IS_HMAC( operation->alg ) )
2955 {
2956 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2957 input_length );
2958 }
2959 else
2960#endif /* MBEDTLS_MD_C */
2961 {
2962 /* This shouldn't happen if `operation` was initialized by
2963 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002964 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002965 }
2966
Gilles Peskinefbfac682018-07-08 20:51:54 +02002967 if( status != PSA_SUCCESS )
2968 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002969 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002970}
2971
Gilles Peskine01126fa2018-07-12 17:04:55 +02002972#if defined(MBEDTLS_MD_C)
2973static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2974 uint8_t *mac,
2975 size_t mac_size )
2976{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002977 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002978 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2979 size_t hash_size = 0;
2980 size_t block_size = psa_get_hash_block_size( hash_alg );
2981 psa_status_t status;
2982
Gilles Peskine01126fa2018-07-12 17:04:55 +02002983 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2984 if( status != PSA_SUCCESS )
2985 return( status );
2986 /* From here on, tmp needs to be wiped. */
2987
2988 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2989 if( status != PSA_SUCCESS )
2990 goto exit;
2991
2992 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2993 if( status != PSA_SUCCESS )
2994 goto exit;
2995
2996 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2997 if( status != PSA_SUCCESS )
2998 goto exit;
2999
Gilles Peskined911eb72018-08-14 15:18:45 +02003000 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3001 if( status != PSA_SUCCESS )
3002 goto exit;
3003
3004 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003005
3006exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003007 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003008 return( status );
3009}
3010#endif /* MBEDTLS_MD_C */
3011
mohammad16036df908f2018-04-02 08:34:15 -07003012static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003013 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003014 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003015{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003016 if( ! operation->key_set )
3017 return( PSA_ERROR_BAD_STATE );
3018 if( operation->iv_required && ! operation->iv_set )
3019 return( PSA_ERROR_BAD_STATE );
3020
Gilles Peskine8c9def32018-02-08 10:02:12 +01003021 if( mac_size < operation->mac_size )
3022 return( PSA_ERROR_BUFFER_TOO_SMALL );
3023
Gilles Peskine8c9def32018-02-08 10:02:12 +01003024#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003025 if( operation->alg == PSA_ALG_CMAC )
3026 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003027 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3028 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3029 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003030 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003031 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003032 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003033 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003034 else
3035#endif /* MBEDTLS_CMAC_C */
3036#if defined(MBEDTLS_MD_C)
3037 if( PSA_ALG_IS_HMAC( operation->alg ) )
3038 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003039 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003040 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003041 }
3042 else
3043#endif /* MBEDTLS_MD_C */
3044 {
3045 /* This shouldn't happen if `operation` was initialized by
3046 * a setup function. */
3047 return( PSA_ERROR_BAD_STATE );
3048 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003049}
3050
Gilles Peskineacd4be32018-07-08 19:56:25 +02003051psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3052 uint8_t *mac,
3053 size_t mac_size,
3054 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003055{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003056 psa_status_t status;
3057
Jaeden Amero252ef282019-02-15 14:05:35 +00003058 if( operation->alg == 0 )
3059 {
3060 return( PSA_ERROR_BAD_STATE );
3061 }
3062
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003063 /* Fill the output buffer with something that isn't a valid mac
3064 * (barring an attack on the mac and deliberately-crafted input),
3065 * in case the caller doesn't check the return status properly. */
3066 *mac_length = mac_size;
3067 /* If mac_size is 0 then mac may be NULL and then the
3068 * call to memset would have undefined behavior. */
3069 if( mac_size != 0 )
3070 memset( mac, '!', mac_size );
3071
Gilles Peskine89167cb2018-07-08 20:12:23 +02003072 if( ! operation->is_sign )
3073 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003074 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003075 }
mohammad16036df908f2018-04-02 08:34:15 -07003076
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003077 status = psa_mac_finish_internal( operation, mac, mac_size );
3078
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003079 if( status == PSA_SUCCESS )
3080 {
3081 status = psa_mac_abort( operation );
3082 if( status == PSA_SUCCESS )
3083 *mac_length = operation->mac_size;
3084 else
3085 memset( mac, '!', mac_size );
3086 }
3087 else
3088 psa_mac_abort( operation );
3089 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003090}
3091
Gilles Peskineacd4be32018-07-08 19:56:25 +02003092psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3093 const uint8_t *mac,
3094 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003095{
Gilles Peskine828ed142018-06-18 23:25:51 +02003096 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003097 psa_status_t status;
3098
Jaeden Amero252ef282019-02-15 14:05:35 +00003099 if( operation->alg == 0 )
3100 {
3101 return( PSA_ERROR_BAD_STATE );
3102 }
3103
Gilles Peskine89167cb2018-07-08 20:12:23 +02003104 if( operation->is_sign )
3105 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003106 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003107 }
3108 if( operation->mac_size != mac_length )
3109 {
3110 status = PSA_ERROR_INVALID_SIGNATURE;
3111 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003112 }
mohammad16036df908f2018-04-02 08:34:15 -07003113
3114 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003115 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003116 if( status != PSA_SUCCESS )
3117 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003118
3119 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3120 status = PSA_ERROR_INVALID_SIGNATURE;
3121
3122cleanup:
3123 if( status == PSA_SUCCESS )
3124 status = psa_mac_abort( operation );
3125 else
3126 psa_mac_abort( operation );
3127
Gilles Peskine3f108122018-12-07 18:14:53 +01003128 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003129
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003130 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003131}
3132
3133
Gilles Peskine20035e32018-02-03 22:44:14 +01003134
Gilles Peskine20035e32018-02-03 22:44:14 +01003135/****************************************************************/
3136/* Asymmetric cryptography */
3137/****************************************************************/
3138
Gilles Peskine2b450e32018-06-27 15:42:46 +02003139#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003140/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003141 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003142static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3143 size_t hash_length,
3144 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003145{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003146 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003147 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003148 *md_alg = mbedtls_md_get_type( md_info );
3149
3150 /* The Mbed TLS RSA module uses an unsigned int for hash length
3151 * parameters. Validate that it fits so that we don't risk an
3152 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003153#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003154 if( hash_length > UINT_MAX )
3155 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003156#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003157
3158#if defined(MBEDTLS_PKCS1_V15)
3159 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3160 * must be correct. */
3161 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3162 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003163 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003164 if( md_info == NULL )
3165 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003166 if( mbedtls_md_get_size( md_info ) != hash_length )
3167 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003168 }
3169#endif /* MBEDTLS_PKCS1_V15 */
3170
3171#if defined(MBEDTLS_PKCS1_V21)
3172 /* PSS requires a hash internally. */
3173 if( PSA_ALG_IS_RSA_PSS( alg ) )
3174 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003175 if( md_info == NULL )
3176 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003177 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003178#endif /* MBEDTLS_PKCS1_V21 */
3179
Gilles Peskine61b91d42018-06-08 16:09:36 +02003180 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003181}
3182
Gilles Peskine2b450e32018-06-27 15:42:46 +02003183static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3184 psa_algorithm_t alg,
3185 const uint8_t *hash,
3186 size_t hash_length,
3187 uint8_t *signature,
3188 size_t signature_size,
3189 size_t *signature_length )
3190{
3191 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003192 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003193 mbedtls_md_type_t md_alg;
3194
3195 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3196 if( status != PSA_SUCCESS )
3197 return( status );
3198
Gilles Peskine630a18a2018-06-29 17:49:35 +02003199 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003200 return( PSA_ERROR_BUFFER_TOO_SMALL );
3201
3202#if defined(MBEDTLS_PKCS1_V15)
3203 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3204 {
3205 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3206 MBEDTLS_MD_NONE );
3207 ret = mbedtls_rsa_pkcs1_sign( rsa,
3208 mbedtls_ctr_drbg_random,
3209 &global_data.ctr_drbg,
3210 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003211 md_alg,
3212 (unsigned int) hash_length,
3213 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003214 signature );
3215 }
3216 else
3217#endif /* MBEDTLS_PKCS1_V15 */
3218#if defined(MBEDTLS_PKCS1_V21)
3219 if( PSA_ALG_IS_RSA_PSS( alg ) )
3220 {
3221 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3222 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3223 mbedtls_ctr_drbg_random,
3224 &global_data.ctr_drbg,
3225 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003226 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003227 (unsigned int) hash_length,
3228 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003229 signature );
3230 }
3231 else
3232#endif /* MBEDTLS_PKCS1_V21 */
3233 {
3234 return( PSA_ERROR_INVALID_ARGUMENT );
3235 }
3236
3237 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003238 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003239 return( mbedtls_to_psa_error( ret ) );
3240}
3241
3242static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3243 psa_algorithm_t alg,
3244 const uint8_t *hash,
3245 size_t hash_length,
3246 const uint8_t *signature,
3247 size_t signature_length )
3248{
3249 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003250 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003251 mbedtls_md_type_t md_alg;
3252
3253 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3254 if( status != PSA_SUCCESS )
3255 return( status );
3256
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003257 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3258 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003259
3260#if defined(MBEDTLS_PKCS1_V15)
3261 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3262 {
3263 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3264 MBEDTLS_MD_NONE );
3265 ret = mbedtls_rsa_pkcs1_verify( rsa,
3266 mbedtls_ctr_drbg_random,
3267 &global_data.ctr_drbg,
3268 MBEDTLS_RSA_PUBLIC,
3269 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003270 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003271 hash,
3272 signature );
3273 }
3274 else
3275#endif /* MBEDTLS_PKCS1_V15 */
3276#if defined(MBEDTLS_PKCS1_V21)
3277 if( PSA_ALG_IS_RSA_PSS( alg ) )
3278 {
3279 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3280 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3281 mbedtls_ctr_drbg_random,
3282 &global_data.ctr_drbg,
3283 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003284 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003285 (unsigned int) hash_length,
3286 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003287 signature );
3288 }
3289 else
3290#endif /* MBEDTLS_PKCS1_V21 */
3291 {
3292 return( PSA_ERROR_INVALID_ARGUMENT );
3293 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003294
3295 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3296 * the rest of the signature is invalid". This has little use in
3297 * practice and PSA doesn't report this distinction. */
3298 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3299 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003300 return( mbedtls_to_psa_error( ret ) );
3301}
3302#endif /* MBEDTLS_RSA_C */
3303
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003304#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003305/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3306 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3307 * (even though these functions don't modify it). */
3308static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3309 psa_algorithm_t alg,
3310 const uint8_t *hash,
3311 size_t hash_length,
3312 uint8_t *signature,
3313 size_t signature_size,
3314 size_t *signature_length )
3315{
Janos Follath24eed8d2019-11-22 13:21:35 +00003316 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003317 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003318 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003319 mbedtls_mpi_init( &r );
3320 mbedtls_mpi_init( &s );
3321
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003322 if( signature_size < 2 * curve_bytes )
3323 {
3324 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3325 goto cleanup;
3326 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003327
Gilles Peskinea05219c2018-11-16 16:02:56 +01003328#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003329 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3330 {
3331 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3332 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3333 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003334 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3335 &ecp->d, hash,
3336 hash_length, md_alg,
3337 mbedtls_ctr_drbg_random,
3338 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003339 }
3340 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003341#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003342 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003343 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003344 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3345 hash, hash_length,
3346 mbedtls_ctr_drbg_random,
3347 &global_data.ctr_drbg ) );
3348 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003349
3350 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3351 signature,
3352 curve_bytes ) );
3353 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3354 signature + curve_bytes,
3355 curve_bytes ) );
3356
3357cleanup:
3358 mbedtls_mpi_free( &r );
3359 mbedtls_mpi_free( &s );
3360 if( ret == 0 )
3361 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003362 return( mbedtls_to_psa_error( ret ) );
3363}
3364
3365static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3366 const uint8_t *hash,
3367 size_t hash_length,
3368 const uint8_t *signature,
3369 size_t signature_length )
3370{
Janos Follath24eed8d2019-11-22 13:21:35 +00003371 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003372 mbedtls_mpi r, s;
3373 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3374 mbedtls_mpi_init( &r );
3375 mbedtls_mpi_init( &s );
3376
3377 if( signature_length != 2 * curve_bytes )
3378 return( PSA_ERROR_INVALID_SIGNATURE );
3379
3380 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3381 signature,
3382 curve_bytes ) );
3383 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3384 signature + curve_bytes,
3385 curve_bytes ) );
3386
3387 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3388 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003389
3390cleanup:
3391 mbedtls_mpi_free( &r );
3392 mbedtls_mpi_free( &s );
3393 return( mbedtls_to_psa_error( ret ) );
3394}
3395#endif /* MBEDTLS_ECDSA_C */
3396
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003397psa_status_t psa_sign_hash( psa_key_handle_t handle,
3398 psa_algorithm_t alg,
3399 const uint8_t *hash,
3400 size_t hash_length,
3401 uint8_t *signature,
3402 size_t signature_size,
3403 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003404{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003405 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003406 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003407#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3408 const psa_drv_se_t *drv;
3409 psa_drv_se_context_t *drv_context;
3410#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003411
3412 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003413 /* Immediately reject a zero-length signature buffer. This guarantees
3414 * that signature must be a valid pointer. (On the other hand, the hash
3415 * buffer can in principle be empty since it doesn't actually have
3416 * to be a hash.) */
3417 if( signature_size == 0 )
3418 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003419
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003420 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003421 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003422 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003423 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003424 {
3425 status = PSA_ERROR_INVALID_ARGUMENT;
3426 goto exit;
3427 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003428
Gilles Peskineedc64242019-08-07 21:05:07 +02003429#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3430 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3431 {
3432 if( drv->asymmetric == NULL ||
3433 drv->asymmetric->p_sign == NULL )
3434 {
3435 status = PSA_ERROR_NOT_SUPPORTED;
3436 goto exit;
3437 }
3438 status = drv->asymmetric->p_sign( drv_context,
3439 slot->data.se.slot_number,
3440 alg,
3441 hash, hash_length,
3442 signature, signature_size,
3443 signature_length );
3444 }
3445 else
3446#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003447#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003448 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003449 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003450 status = psa_rsa_sign( slot->data.rsa,
3451 alg,
3452 hash, hash_length,
3453 signature, signature_size,
3454 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003455 }
3456 else
3457#endif /* defined(MBEDTLS_RSA_C) */
3458#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003459 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003460 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003461#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003462 if(
3463#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3464 PSA_ALG_IS_ECDSA( alg )
3465#else
3466 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3467#endif
3468 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003469 status = psa_ecdsa_sign( slot->data.ecp,
3470 alg,
3471 hash, hash_length,
3472 signature, signature_size,
3473 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003474 else
3475#endif /* defined(MBEDTLS_ECDSA_C) */
3476 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003477 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003478 }
itayzafrir5c753392018-05-08 11:18:38 +03003479 }
3480 else
3481#endif /* defined(MBEDTLS_ECP_C) */
3482 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003483 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003484 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003485
3486exit:
3487 /* Fill the unused part of the output buffer (the whole buffer on error,
3488 * the trailing part on success) with something that isn't a valid mac
3489 * (barring an attack on the mac and deliberately-crafted input),
3490 * in case the caller doesn't check the return status properly. */
3491 if( status == PSA_SUCCESS )
3492 memset( signature + *signature_length, '!',
3493 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003494 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003495 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003496 /* If signature_size is 0 then we have nothing to do. We must not call
3497 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003498 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003499}
3500
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003501psa_status_t psa_verify_hash( psa_key_handle_t handle,
3502 psa_algorithm_t alg,
3503 const uint8_t *hash,
3504 size_t hash_length,
3505 const uint8_t *signature,
3506 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003507{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003508 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003509 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003510#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3511 const psa_drv_se_t *drv;
3512 psa_drv_se_context_t *drv_context;
3513#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003514
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003515 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003516 if( status != PSA_SUCCESS )
3517 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003518
Gilles Peskineedc64242019-08-07 21:05:07 +02003519#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3520 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3521 {
3522 if( drv->asymmetric == NULL ||
3523 drv->asymmetric->p_verify == NULL )
3524 return( PSA_ERROR_NOT_SUPPORTED );
3525 return( drv->asymmetric->p_verify( drv_context,
3526 slot->data.se.slot_number,
3527 alg,
3528 hash, hash_length,
3529 signature, signature_length ) );
3530 }
3531 else
3532#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003533#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003534 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003535 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003536 return( psa_rsa_verify( slot->data.rsa,
3537 alg,
3538 hash, hash_length,
3539 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003540 }
3541 else
3542#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003543#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003544 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003545 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003546#if defined(MBEDTLS_ECDSA_C)
3547 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003548 return( psa_ecdsa_verify( slot->data.ecp,
3549 hash, hash_length,
3550 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003551 else
3552#endif /* defined(MBEDTLS_ECDSA_C) */
3553 {
3554 return( PSA_ERROR_INVALID_ARGUMENT );
3555 }
itayzafrir5c753392018-05-08 11:18:38 +03003556 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003557 else
3558#endif /* defined(MBEDTLS_ECP_C) */
3559 {
3560 return( PSA_ERROR_NOT_SUPPORTED );
3561 }
3562}
3563
Gilles Peskine072ac562018-06-30 00:21:29 +02003564#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3565static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3566 mbedtls_rsa_context *rsa )
3567{
3568 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3569 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3570 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3571 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3572}
3573#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3574
Gilles Peskinec5487a82018-12-03 18:08:14 +01003575psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003576 psa_algorithm_t alg,
3577 const uint8_t *input,
3578 size_t input_length,
3579 const uint8_t *salt,
3580 size_t salt_length,
3581 uint8_t *output,
3582 size_t output_size,
3583 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003584{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003585 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003586 psa_status_t status;
3587
Darryl Green5cc689a2018-07-24 15:34:10 +01003588 (void) input;
3589 (void) input_length;
3590 (void) salt;
3591 (void) output;
3592 (void) output_size;
3593
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003594 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003595
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003596 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3597 return( PSA_ERROR_INVALID_ARGUMENT );
3598
Gilles Peskine28f8f302019-07-24 13:30:31 +02003599 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003600 if( status != PSA_SUCCESS )
3601 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003602 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3603 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003604 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003605
3606#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003607 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003608 {
3609 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003610 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003611 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003612 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003613#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003614 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003615 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003616 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3617 mbedtls_ctr_drbg_random,
3618 &global_data.ctr_drbg,
3619 MBEDTLS_RSA_PUBLIC,
3620 input_length,
3621 input,
3622 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003623 }
3624 else
3625#endif /* MBEDTLS_PKCS1_V15 */
3626#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003627 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003628 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003629 psa_rsa_oaep_set_padding_mode( alg, rsa );
3630 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3631 mbedtls_ctr_drbg_random,
3632 &global_data.ctr_drbg,
3633 MBEDTLS_RSA_PUBLIC,
3634 salt, salt_length,
3635 input_length,
3636 input,
3637 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003638 }
3639 else
3640#endif /* MBEDTLS_PKCS1_V21 */
3641 {
3642 return( PSA_ERROR_INVALID_ARGUMENT );
3643 }
3644 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003645 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003646 return( mbedtls_to_psa_error( ret ) );
3647 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003648 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003649#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003650 {
3651 return( PSA_ERROR_NOT_SUPPORTED );
3652 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003653}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003654
Gilles Peskinec5487a82018-12-03 18:08:14 +01003655psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003656 psa_algorithm_t alg,
3657 const uint8_t *input,
3658 size_t input_length,
3659 const uint8_t *salt,
3660 size_t salt_length,
3661 uint8_t *output,
3662 size_t output_size,
3663 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003664{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003665 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003666 psa_status_t status;
3667
Darryl Green5cc689a2018-07-24 15:34:10 +01003668 (void) input;
3669 (void) input_length;
3670 (void) salt;
3671 (void) output;
3672 (void) output_size;
3673
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003674 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003675
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003676 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3677 return( PSA_ERROR_INVALID_ARGUMENT );
3678
Gilles Peskine28f8f302019-07-24 13:30:31 +02003679 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003680 if( status != PSA_SUCCESS )
3681 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003682 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003683 return( PSA_ERROR_INVALID_ARGUMENT );
3684
3685#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003686 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003687 {
3688 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003690
Gilles Peskine630a18a2018-06-29 17:49:35 +02003691 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003692 return( PSA_ERROR_INVALID_ARGUMENT );
3693
3694#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003695 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003696 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003697 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3698 mbedtls_ctr_drbg_random,
3699 &global_data.ctr_drbg,
3700 MBEDTLS_RSA_PRIVATE,
3701 output_length,
3702 input,
3703 output,
3704 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003705 }
3706 else
3707#endif /* MBEDTLS_PKCS1_V15 */
3708#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003709 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003710 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003711 psa_rsa_oaep_set_padding_mode( alg, rsa );
3712 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3713 mbedtls_ctr_drbg_random,
3714 &global_data.ctr_drbg,
3715 MBEDTLS_RSA_PRIVATE,
3716 salt, salt_length,
3717 output_length,
3718 input,
3719 output,
3720 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003721 }
3722 else
3723#endif /* MBEDTLS_PKCS1_V21 */
3724 {
3725 return( PSA_ERROR_INVALID_ARGUMENT );
3726 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003727
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003728 return( mbedtls_to_psa_error( ret ) );
3729 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003730 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003731#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003732 {
3733 return( PSA_ERROR_NOT_SUPPORTED );
3734 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003735}
Gilles Peskine20035e32018-02-03 22:44:14 +01003736
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003737
3738
mohammad1603503973b2018-03-12 15:59:30 +02003739/****************************************************************/
3740/* Symmetric cryptography */
3741/****************************************************************/
3742
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003743/* Initialize the cipher operation structure. Once this function has been
3744 * called, psa_cipher_abort can run and will do the right thing. */
3745static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3746 psa_algorithm_t alg )
3747{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003748 if( ! PSA_ALG_IS_CIPHER( alg ) )
3749 {
3750 memset( operation, 0, sizeof( *operation ) );
3751 return( PSA_ERROR_INVALID_ARGUMENT );
3752 }
3753
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003754 operation->alg = alg;
3755 operation->key_set = 0;
3756 operation->iv_set = 0;
3757 operation->iv_required = 1;
3758 operation->iv_size = 0;
3759 operation->block_size = 0;
3760 mbedtls_cipher_init( &operation->ctx.cipher );
3761 return( PSA_SUCCESS );
3762}
3763
Gilles Peskinee553c652018-06-04 16:22:46 +02003764static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003765 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003766 psa_algorithm_t alg,
3767 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003768{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003769 int ret = 0;
3770 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003771 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003772 size_t key_bits;
3773 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003774 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3775 PSA_KEY_USAGE_ENCRYPT :
3776 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003777
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003778 /* A context must be freshly initialized before it can be set up. */
3779 if( operation->alg != 0 )
3780 {
3781 return( PSA_ERROR_BAD_STATE );
3782 }
3783
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003784 status = psa_cipher_init( operation, alg );
3785 if( status != PSA_SUCCESS )
3786 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003787
Gilles Peskine28f8f302019-07-24 13:30:31 +02003788 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003789 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003790 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003791 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003792
Gilles Peskine8e338702019-07-30 20:06:31 +02003793 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003794 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003795 {
3796 status = PSA_ERROR_NOT_SUPPORTED;
3797 goto exit;
3798 }
mohammad1603503973b2018-03-12 15:59:30 +02003799
mohammad1603503973b2018-03-12 15:59:30 +02003800 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003801 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003802 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003803
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003804#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003805 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003806 {
3807 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003808 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003809 memcpy( keys, slot->data.key.data, 16 );
3810 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003811 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3812 keys,
3813 192, cipher_operation );
3814 }
3815 else
3816#endif
3817 {
3818 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003819 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003820 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003821 }
Moran Peker41deec42018-04-04 15:43:05 +03003822 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003823 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003824
mohammad16038481e742018-03-18 13:57:31 +02003825#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003826 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003827 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003828 case PSA_ALG_CBC_NO_PADDING:
3829 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3830 MBEDTLS_PADDING_NONE );
3831 break;
3832 case PSA_ALG_CBC_PKCS7:
3833 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3834 MBEDTLS_PADDING_PKCS7 );
3835 break;
3836 default:
3837 /* The algorithm doesn't involve padding. */
3838 ret = 0;
3839 break;
3840 }
3841 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003842 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003843#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3844
mohammad1603503973b2018-03-12 15:59:30 +02003845 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003846 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003847 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003848 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003849 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003850 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003851 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003852#if defined(MBEDTLS_CHACHA20_C)
3853 else
3854 if( alg == PSA_ALG_CHACHA20 )
3855 operation->iv_size = 12;
3856#endif
mohammad1603503973b2018-03-12 15:59:30 +02003857
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003858exit:
3859 if( status == 0 )
3860 status = mbedtls_to_psa_error( ret );
3861 if( status != 0 )
3862 psa_cipher_abort( operation );
3863 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003864}
3865
Gilles Peskinefe119512018-07-08 21:39:34 +02003866psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003867 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003868 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003869{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003870 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003871}
3872
Gilles Peskinefe119512018-07-08 21:39:34 +02003873psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003874 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003875 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003876{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003877 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003878}
3879
Gilles Peskinefe119512018-07-08 21:39:34 +02003880psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003881 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003882 size_t iv_size,
3883 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003884{
itayzafrir534bd7c2018-08-02 13:56:32 +03003885 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003886 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003887 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003888 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003889 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003890 }
Moran Peker41deec42018-04-04 15:43:05 +03003891 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003892 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003893 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003894 goto exit;
3895 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003896 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3897 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003898 if( ret != 0 )
3899 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003900 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003901 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003902 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003903
mohammad16038481e742018-03-18 13:57:31 +02003904 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003905 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003906
Moran Peker395db872018-05-31 14:07:14 +03003907exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003908 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003909 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003910 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003911}
3912
Gilles Peskinefe119512018-07-08 21:39:34 +02003913psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003914 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003915 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003916{
itayzafrir534bd7c2018-08-02 13:56:32 +03003917 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003918 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003919 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003920 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003921 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003922 }
Moran Pekera28258c2018-05-29 16:25:04 +03003923 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003924 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003925 status = PSA_ERROR_INVALID_ARGUMENT;
3926 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003927 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003928 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3929 status = mbedtls_to_psa_error( ret );
3930exit:
3931 if( status == PSA_SUCCESS )
3932 operation->iv_set = 1;
3933 else
mohammad1603503973b2018-03-12 15:59:30 +02003934 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003935 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003936}
3937
Gilles Peskinee553c652018-06-04 16:22:46 +02003938psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3939 const uint8_t *input,
3940 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003941 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003942 size_t output_size,
3943 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003944{
itayzafrir534bd7c2018-08-02 13:56:32 +03003945 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003946 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003947 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003948
3949 if( operation->alg == 0 )
3950 {
3951 return( PSA_ERROR_BAD_STATE );
3952 }
3953
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003954 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003955 {
3956 /* Take the unprocessed partial block left over from previous
3957 * update calls, if any, plus the input to this call. Remove
3958 * the last partial block, if any. You get the data that will be
3959 * output in this call. */
3960 expected_output_size =
3961 ( operation->ctx.cipher.unprocessed_len + input_length )
3962 / operation->block_size * operation->block_size;
3963 }
3964 else
3965 {
3966 expected_output_size = input_length;
3967 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003968
Gilles Peskine89d789c2018-06-04 17:17:16 +02003969 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003970 {
3971 status = PSA_ERROR_BUFFER_TOO_SMALL;
3972 goto exit;
3973 }
mohammad160382759612018-03-12 18:16:40 +02003974
mohammad1603503973b2018-03-12 15:59:30 +02003975 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003976 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003977 status = mbedtls_to_psa_error( ret );
3978exit:
3979 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003980 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003981 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003982}
3983
Gilles Peskinee553c652018-06-04 16:22:46 +02003984psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3985 uint8_t *output,
3986 size_t output_size,
3987 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003988{
David Saadab4ecc272019-02-14 13:48:10 +02003989 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003990 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003991 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003992
mohammad1603503973b2018-03-12 15:59:30 +02003993 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003994 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003995 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003996 }
3997 if( operation->iv_required && ! operation->iv_set )
3998 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003999 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004000 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004001
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004002 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004003 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4004 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004005 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004006 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004007 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004008 }
4009
Janos Follath315b51c2018-07-09 16:04:51 +01004010 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4011 temp_output_buffer,
4012 output_length );
4013 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004014 {
Janos Follath315b51c2018-07-09 16:04:51 +01004015 status = mbedtls_to_psa_error( cipher_ret );
4016 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004017 }
Janos Follath315b51c2018-07-09 16:04:51 +01004018
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004019 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004020 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004021 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004022 memcpy( output, temp_output_buffer, *output_length );
4023 else
4024 {
Janos Follath315b51c2018-07-09 16:04:51 +01004025 status = PSA_ERROR_BUFFER_TOO_SMALL;
4026 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004027 }
mohammad1603503973b2018-03-12 15:59:30 +02004028
Gilles Peskine3f108122018-12-07 18:14:53 +01004029 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004030 status = psa_cipher_abort( operation );
4031
4032 return( status );
4033
4034error:
4035
4036 *output_length = 0;
4037
Gilles Peskine3f108122018-12-07 18:14:53 +01004038 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004039 (void) psa_cipher_abort( operation );
4040
4041 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004042}
4043
Gilles Peskinee553c652018-06-04 16:22:46 +02004044psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4045{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004046 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004047 {
4048 /* The object has (apparently) been initialized but it is not
4049 * in use. It's ok to call abort on such an object, and there's
4050 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004051 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004052 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004053
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004054 /* Sanity check (shouldn't happen: operation->alg should
4055 * always have been initialized to a valid value). */
4056 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4057 return( PSA_ERROR_BAD_STATE );
4058
mohammad1603503973b2018-03-12 15:59:30 +02004059 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004060
Moran Peker41deec42018-04-04 15:43:05 +03004061 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004062 operation->key_set = 0;
4063 operation->iv_set = 0;
4064 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004065 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004066 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004067
Moran Peker395db872018-05-31 14:07:14 +03004068 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004069}
4070
Gilles Peskinea0655c32018-04-30 17:06:50 +02004071
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004072
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004073
mohammad16035955c982018-04-26 00:53:03 +03004074/****************************************************************/
4075/* AEAD */
4076/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004077
Gilles Peskineedf9a652018-08-17 18:11:56 +02004078typedef struct
4079{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004080 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004081 const mbedtls_cipher_info_t *cipher_info;
4082 union
4083 {
4084#if defined(MBEDTLS_CCM_C)
4085 mbedtls_ccm_context ccm;
4086#endif /* MBEDTLS_CCM_C */
4087#if defined(MBEDTLS_GCM_C)
4088 mbedtls_gcm_context gcm;
4089#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004090#if defined(MBEDTLS_CHACHAPOLY_C)
4091 mbedtls_chachapoly_context chachapoly;
4092#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004093 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004094 psa_algorithm_t core_alg;
4095 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004096 uint8_t tag_length;
4097} aead_operation_t;
4098
Gilles Peskine30a9e412019-01-14 18:36:12 +01004099static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004100{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004101 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004102 {
4103#if defined(MBEDTLS_CCM_C)
4104 case PSA_ALG_CCM:
4105 mbedtls_ccm_free( &operation->ctx.ccm );
4106 break;
4107#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004108#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004109 case PSA_ALG_GCM:
4110 mbedtls_gcm_free( &operation->ctx.gcm );
4111 break;
4112#endif /* MBEDTLS_GCM_C */
4113 }
4114}
4115
4116static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004117 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004118 psa_key_usage_t usage,
4119 psa_algorithm_t alg )
4120{
4121 psa_status_t status;
4122 size_t key_bits;
4123 mbedtls_cipher_id_t cipher_id;
4124
Gilles Peskine28f8f302019-07-24 13:30:31 +02004125 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004126 if( status != PSA_SUCCESS )
4127 return( status );
4128
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004129 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004130
4131 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004132 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004133 &cipher_id );
4134 if( operation->cipher_info == NULL )
4135 return( PSA_ERROR_NOT_SUPPORTED );
4136
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004137 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004138 {
4139#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004140 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4141 operation->core_alg = PSA_ALG_CCM;
4142 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004143 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4144 * The call to mbedtls_ccm_encrypt_and_tag or
4145 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004146 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004147 return( PSA_ERROR_INVALID_ARGUMENT );
4148 mbedtls_ccm_init( &operation->ctx.ccm );
4149 status = mbedtls_to_psa_error(
4150 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004151 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004152 (unsigned int) key_bits ) );
4153 if( status != 0 )
4154 goto cleanup;
4155 break;
4156#endif /* MBEDTLS_CCM_C */
4157
4158#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004159 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4160 operation->core_alg = PSA_ALG_GCM;
4161 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004162 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4163 * The call to mbedtls_gcm_crypt_and_tag or
4164 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004165 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004166 return( PSA_ERROR_INVALID_ARGUMENT );
4167 mbedtls_gcm_init( &operation->ctx.gcm );
4168 status = mbedtls_to_psa_error(
4169 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004170 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004171 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004172 if( status != 0 )
4173 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004174 break;
4175#endif /* MBEDTLS_GCM_C */
4176
Gilles Peskine26869f22019-05-06 15:25:00 +02004177#if defined(MBEDTLS_CHACHAPOLY_C)
4178 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4179 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4180 operation->full_tag_length = 16;
4181 /* We only support the default tag length. */
4182 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4183 return( PSA_ERROR_NOT_SUPPORTED );
4184 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4185 status = mbedtls_to_psa_error(
4186 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004187 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004188 if( status != 0 )
4189 goto cleanup;
4190 break;
4191#endif /* MBEDTLS_CHACHAPOLY_C */
4192
Gilles Peskineedf9a652018-08-17 18:11:56 +02004193 default:
4194 return( PSA_ERROR_NOT_SUPPORTED );
4195 }
4196
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004197 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4198 {
4199 status = PSA_ERROR_INVALID_ARGUMENT;
4200 goto cleanup;
4201 }
4202 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004203
Gilles Peskineedf9a652018-08-17 18:11:56 +02004204 return( PSA_SUCCESS );
4205
4206cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004207 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004208 return( status );
4209}
4210
Gilles Peskinec5487a82018-12-03 18:08:14 +01004211psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004212 psa_algorithm_t alg,
4213 const uint8_t *nonce,
4214 size_t nonce_length,
4215 const uint8_t *additional_data,
4216 size_t additional_data_length,
4217 const uint8_t *plaintext,
4218 size_t plaintext_length,
4219 uint8_t *ciphertext,
4220 size_t ciphertext_size,
4221 size_t *ciphertext_length )
4222{
mohammad16035955c982018-04-26 00:53:03 +03004223 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004224 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004225 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004226
mohammad1603f08a5502018-06-03 15:05:47 +03004227 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004228
Gilles Peskinec5487a82018-12-03 18:08:14 +01004229 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004230 if( status != PSA_SUCCESS )
4231 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004232
Gilles Peskineedf9a652018-08-17 18:11:56 +02004233 /* For all currently supported modes, the tag is at the end of the
4234 * ciphertext. */
4235 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4236 {
4237 status = PSA_ERROR_BUFFER_TOO_SMALL;
4238 goto exit;
4239 }
4240 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004241
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004242#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004243 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004244 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004245 status = mbedtls_to_psa_error(
4246 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4247 MBEDTLS_GCM_ENCRYPT,
4248 plaintext_length,
4249 nonce, nonce_length,
4250 additional_data, additional_data_length,
4251 plaintext, ciphertext,
4252 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004253 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004254 else
4255#endif /* MBEDTLS_GCM_C */
4256#if defined(MBEDTLS_CCM_C)
4257 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004258 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004259 status = mbedtls_to_psa_error(
4260 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4261 plaintext_length,
4262 nonce, nonce_length,
4263 additional_data,
4264 additional_data_length,
4265 plaintext, ciphertext,
4266 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004267 }
mohammad16035c8845f2018-05-09 05:40:09 -07004268 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004269#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004270#if defined(MBEDTLS_CHACHAPOLY_C)
4271 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4272 {
4273 if( nonce_length != 12 || operation.tag_length != 16 )
4274 {
4275 status = PSA_ERROR_NOT_SUPPORTED;
4276 goto exit;
4277 }
4278 status = mbedtls_to_psa_error(
4279 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4280 plaintext_length,
4281 nonce,
4282 additional_data,
4283 additional_data_length,
4284 plaintext,
4285 ciphertext,
4286 tag ) );
4287 }
4288 else
4289#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004290 {
mohammad1603554faad2018-06-03 15:07:38 +03004291 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004292 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004293
Gilles Peskineedf9a652018-08-17 18:11:56 +02004294 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4295 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004296
Gilles Peskineedf9a652018-08-17 18:11:56 +02004297exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004298 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004299 if( status == PSA_SUCCESS )
4300 *ciphertext_length = plaintext_length + operation.tag_length;
4301 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004302}
4303
Gilles Peskineee652a32018-06-01 19:23:52 +02004304/* Locate the tag in a ciphertext buffer containing the encrypted data
4305 * followed by the tag. Return the length of the part preceding the tag in
4306 * *plaintext_length. This is the size of the plaintext in modes where
4307 * the encrypted data has the same size as the plaintext, such as
4308 * CCM and GCM. */
4309static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4310 const uint8_t *ciphertext,
4311 size_t ciphertext_length,
4312 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004313 const uint8_t **p_tag )
4314{
4315 size_t payload_length;
4316 if( tag_length > ciphertext_length )
4317 return( PSA_ERROR_INVALID_ARGUMENT );
4318 payload_length = ciphertext_length - tag_length;
4319 if( payload_length > plaintext_size )
4320 return( PSA_ERROR_BUFFER_TOO_SMALL );
4321 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004322 return( PSA_SUCCESS );
4323}
4324
Gilles Peskinec5487a82018-12-03 18:08:14 +01004325psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004326 psa_algorithm_t alg,
4327 const uint8_t *nonce,
4328 size_t nonce_length,
4329 const uint8_t *additional_data,
4330 size_t additional_data_length,
4331 const uint8_t *ciphertext,
4332 size_t ciphertext_length,
4333 uint8_t *plaintext,
4334 size_t plaintext_size,
4335 size_t *plaintext_length )
4336{
mohammad16035955c982018-04-26 00:53:03 +03004337 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004338 aead_operation_t operation;
4339 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004340
Gilles Peskineee652a32018-06-01 19:23:52 +02004341 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004342
Gilles Peskinec5487a82018-12-03 18:08:14 +01004343 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004344 if( status != PSA_SUCCESS )
4345 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004346
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004347 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4348 ciphertext, ciphertext_length,
4349 plaintext_size, &tag );
4350 if( status != PSA_SUCCESS )
4351 goto exit;
4352
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004353#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004354 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004355 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004356 status = mbedtls_to_psa_error(
4357 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4358 ciphertext_length - operation.tag_length,
4359 nonce, nonce_length,
4360 additional_data,
4361 additional_data_length,
4362 tag, operation.tag_length,
4363 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004364 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004365 else
4366#endif /* MBEDTLS_GCM_C */
4367#if defined(MBEDTLS_CCM_C)
4368 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004369 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004370 status = mbedtls_to_psa_error(
4371 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4372 ciphertext_length - operation.tag_length,
4373 nonce, nonce_length,
4374 additional_data,
4375 additional_data_length,
4376 ciphertext, plaintext,
4377 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004378 }
mohammad160339574652018-06-01 04:39:53 -07004379 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004380#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004381#if defined(MBEDTLS_CHACHAPOLY_C)
4382 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4383 {
4384 if( nonce_length != 12 || operation.tag_length != 16 )
4385 {
4386 status = PSA_ERROR_NOT_SUPPORTED;
4387 goto exit;
4388 }
4389 status = mbedtls_to_psa_error(
4390 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4391 ciphertext_length - operation.tag_length,
4392 nonce,
4393 additional_data,
4394 additional_data_length,
4395 tag,
4396 ciphertext,
4397 plaintext ) );
4398 }
4399 else
4400#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004401 {
mohammad1603554faad2018-06-03 15:07:38 +03004402 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004403 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004404
Gilles Peskineedf9a652018-08-17 18:11:56 +02004405 if( status != PSA_SUCCESS && plaintext_size != 0 )
4406 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004407
Gilles Peskineedf9a652018-08-17 18:11:56 +02004408exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004409 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004410 if( status == PSA_SUCCESS )
4411 *plaintext_length = ciphertext_length - operation.tag_length;
4412 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004413}
4414
Gilles Peskinea0655c32018-04-30 17:06:50 +02004415
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004416
Gilles Peskine20035e32018-02-03 22:44:14 +01004417/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004418/* Generators */
4419/****************************************************************/
4420
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004421#define HKDF_STATE_INIT 0 /* no input yet */
4422#define HKDF_STATE_STARTED 1 /* got salt */
4423#define HKDF_STATE_KEYED 2 /* got key */
4424#define HKDF_STATE_OUTPUT 3 /* output started */
4425
Gilles Peskinecbe66502019-05-16 16:59:18 +02004426static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004427 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004428{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004429 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4430 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004431 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004432 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004433}
4434
4435
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004436psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004437{
4438 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004439 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004440 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004441 {
4442 /* The object has (apparently) been initialized but it is not
4443 * in use. It's ok to call abort on such an object, and there's
4444 * nothing to do. */
4445 }
4446 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004447#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004448 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004449 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004450 mbedtls_free( operation->ctx.hkdf.info );
4451 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004452 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004453 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004454 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004455 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004456 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004457 if( operation->ctx.tls12_prf.seed != NULL )
4458 {
4459 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4460 operation->ctx.tls12_prf.seed_length );
4461 mbedtls_free( operation->ctx.tls12_prf.seed );
4462 }
4463
4464 if( operation->ctx.tls12_prf.label != NULL )
4465 {
4466 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4467 operation->ctx.tls12_prf.label_length );
4468 mbedtls_free( operation->ctx.tls12_prf.label );
4469 }
4470
4471 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4472
4473 /* We leave the fields Ai and output_block to be erased safely by the
4474 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004475 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004476 else
4477#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004478 {
4479 status = PSA_ERROR_BAD_STATE;
4480 }
Janos Follath083036a2019-06-11 10:22:26 +01004481 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004482 return( status );
4483}
4484
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004485psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004486 size_t *capacity)
4487{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004488 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004489 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004490 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004491 return PSA_ERROR_BAD_STATE;
4492 }
4493
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004494 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004495 return( PSA_SUCCESS );
4496}
4497
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004498psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004499 size_t capacity )
4500{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004501 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004502 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004503 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004504 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004505 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004506 return( PSA_SUCCESS );
4507}
4508
Gilles Peskinebef7f142018-07-12 17:22:21 +02004509#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004510/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004511 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004512static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004513 psa_algorithm_t hash_alg,
4514 uint8_t *output,
4515 size_t output_length )
4516{
4517 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4518 psa_status_t status;
4519
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004520 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4521 return( PSA_ERROR_BAD_STATE );
4522 hkdf->state = HKDF_STATE_OUTPUT;
4523
Gilles Peskinebef7f142018-07-12 17:22:21 +02004524 while( output_length != 0 )
4525 {
4526 /* Copy what remains of the current block */
4527 uint8_t n = hash_length - hkdf->offset_in_block;
4528 if( n > output_length )
4529 n = (uint8_t) output_length;
4530 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4531 output += n;
4532 output_length -= n;
4533 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004534 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004535 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004536 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004537 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004538 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004539 * object was corrupted or if this function is called directly
4540 * inside the library. */
4541 if( hkdf->block_number == 0xff )
4542 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004543
4544 /* We need a new block */
4545 ++hkdf->block_number;
4546 hkdf->offset_in_block = 0;
4547 status = psa_hmac_setup_internal( &hkdf->hmac,
4548 hkdf->prk, hash_length,
4549 hash_alg );
4550 if( status != PSA_SUCCESS )
4551 return( status );
4552 if( hkdf->block_number != 1 )
4553 {
4554 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4555 hkdf->output_block,
4556 hash_length );
4557 if( status != PSA_SUCCESS )
4558 return( status );
4559 }
4560 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4561 hkdf->info,
4562 hkdf->info_length );
4563 if( status != PSA_SUCCESS )
4564 return( status );
4565 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4566 &hkdf->block_number, 1 );
4567 if( status != PSA_SUCCESS )
4568 return( status );
4569 status = psa_hmac_finish_internal( &hkdf->hmac,
4570 hkdf->output_block,
4571 sizeof( hkdf->output_block ) );
4572 if( status != PSA_SUCCESS )
4573 return( status );
4574 }
4575
4576 return( PSA_SUCCESS );
4577}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004578
Janos Follath7742fee2019-06-17 12:58:10 +01004579static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4580 psa_tls12_prf_key_derivation_t *tls12_prf,
4581 psa_algorithm_t alg )
4582{
4583 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4584 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004585 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4586 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004587
Janos Follath7742fee2019-06-17 12:58:10 +01004588 /* We can't be wanting more output after block 0xff, otherwise
4589 * the capacity check in psa_key_derivation_output_bytes() would have
4590 * prevented this call. It could happen only if the operation
4591 * object was corrupted or if this function is called directly
4592 * inside the library. */
4593 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004594 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004595
4596 /* We need a new block */
4597 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004598 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004599
4600 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4601 *
4602 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4603 *
4604 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4605 * HMAC_hash(secret, A(2) + seed) +
4606 * HMAC_hash(secret, A(3) + seed) + ...
4607 *
4608 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004609 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004610 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004611 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004612 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004613 * is currently extracted as `output_block` and where i is
4614 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004615 */
4616
Janos Follathea29bfb2019-06-19 12:21:20 +01004617 /* Save the hash context before using it, to preserve the hash state with
4618 * only the inner padding in it. We need this, because inner padding depends
4619 * on the key (secret in the RFC's terminology). */
4620 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4621 if( status != PSA_SUCCESS )
4622 goto cleanup;
4623
4624 /* Calculate A(i) where i = tls12_prf->block_number. */
4625 if( tls12_prf->block_number == 1 )
4626 {
4627 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4628 * the variable seed and in this instance means it in the context of the
4629 * P_hash function, where seed = label + seed.) */
4630 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4631 tls12_prf->label, tls12_prf->label_length );
4632 if( status != PSA_SUCCESS )
4633 goto cleanup;
4634 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4635 tls12_prf->seed, tls12_prf->seed_length );
4636 if( status != PSA_SUCCESS )
4637 goto cleanup;
4638 }
4639 else
4640 {
4641 /* A(i) = HMAC_hash(secret, A(i-1)) */
4642 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4643 tls12_prf->Ai, hash_length );
4644 if( status != PSA_SUCCESS )
4645 goto cleanup;
4646 }
4647
4648 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4649 tls12_prf->Ai, hash_length );
4650 if( status != PSA_SUCCESS )
4651 goto cleanup;
4652 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4653 if( status != PSA_SUCCESS )
4654 goto cleanup;
4655
4656 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4657 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4658 tls12_prf->Ai, hash_length );
4659 if( status != PSA_SUCCESS )
4660 goto cleanup;
4661 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4662 tls12_prf->label, tls12_prf->label_length );
4663 if( status != PSA_SUCCESS )
4664 goto cleanup;
4665 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4666 tls12_prf->seed, tls12_prf->seed_length );
4667 if( status != PSA_SUCCESS )
4668 goto cleanup;
4669 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4670 tls12_prf->output_block, hash_length );
4671 if( status != PSA_SUCCESS )
4672 goto cleanup;
4673 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4674 if( status != PSA_SUCCESS )
4675 goto cleanup;
4676
Janos Follath7742fee2019-06-17 12:58:10 +01004677
4678cleanup:
4679
Janos Follathea29bfb2019-06-19 12:21:20 +01004680 cleanup_status = psa_hash_abort( &backup );
4681 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4682 status = cleanup_status;
4683
4684 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004685}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004686
Janos Follath844eb0e2019-06-19 12:10:49 +01004687static psa_status_t psa_key_derivation_tls12_prf_read(
4688 psa_tls12_prf_key_derivation_t *tls12_prf,
4689 psa_algorithm_t alg,
4690 uint8_t *output,
4691 size_t output_length )
4692{
4693 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4694 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4695 psa_status_t status;
4696 uint8_t offset, length;
4697
4698 while( output_length != 0 )
4699 {
4700 /* Check if we have fully processed the current block. */
4701 if( tls12_prf->left_in_block == 0 )
4702 {
4703 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4704 alg );
4705 if( status != PSA_SUCCESS )
4706 return( status );
4707
4708 continue;
4709 }
4710
4711 if( tls12_prf->left_in_block > output_length )
4712 length = (uint8_t) output_length;
4713 else
4714 length = tls12_prf->left_in_block;
4715
4716 offset = hash_length - tls12_prf->left_in_block;
4717 memcpy( output, tls12_prf->output_block + offset, length );
4718 output += length;
4719 output_length -= length;
4720 tls12_prf->left_in_block -= length;
4721 }
4722
4723 return( PSA_SUCCESS );
4724}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004725#endif /* MBEDTLS_MD_C */
4726
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004727psa_status_t psa_key_derivation_output_bytes(
4728 psa_key_derivation_operation_t *operation,
4729 uint8_t *output,
4730 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004731{
4732 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004733 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004734
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004735 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004736 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004737 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004738 return PSA_ERROR_BAD_STATE;
4739 }
4740
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004741 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004742 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004743 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004744 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004745 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004746 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004747 goto exit;
4748 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004749 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004750 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004751 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004752 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004753 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4754 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004755 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004756 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004757 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004758 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004759 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004760
Gilles Peskinebef7f142018-07-12 17:22:21 +02004761#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004762 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004763 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004764 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004765 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004766 output, output_length );
4767 }
Janos Follathadbec812019-06-14 11:05:39 +01004768 else
Janos Follathadbec812019-06-14 11:05:39 +01004769 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004770 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004771 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004772 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004773 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004774 output_length );
4775 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004776 else
4777#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004778 {
4779 return( PSA_ERROR_BAD_STATE );
4780 }
4781
4782exit:
4783 if( status != PSA_SUCCESS )
4784 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004785 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004786 * This allows us to differentiate between exhausted operations and
4787 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4788 * operations. */
4789 psa_algorithm_t alg = operation->alg;
4790 psa_key_derivation_abort( operation );
4791 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004792 memset( output, '!', output_length );
4793 }
4794 return( status );
4795}
4796
Gilles Peskine08542d82018-07-19 17:05:42 +02004797#if defined(MBEDTLS_DES_C)
4798static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4799{
4800 if( data_size >= 8 )
4801 mbedtls_des_key_set_parity( data );
4802 if( data_size >= 16 )
4803 mbedtls_des_key_set_parity( data + 8 );
4804 if( data_size >= 24 )
4805 mbedtls_des_key_set_parity( data + 16 );
4806}
4807#endif /* MBEDTLS_DES_C */
4808
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004809static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004810 psa_key_slot_t *slot,
4811 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004812 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004813{
4814 uint8_t *data = NULL;
4815 size_t bytes = PSA_BITS_TO_BYTES( bits );
4816 psa_status_t status;
4817
Gilles Peskine8e338702019-07-30 20:06:31 +02004818 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004819 return( PSA_ERROR_INVALID_ARGUMENT );
4820 if( bits % 8 != 0 )
4821 return( PSA_ERROR_INVALID_ARGUMENT );
4822 data = mbedtls_calloc( 1, bytes );
4823 if( data == NULL )
4824 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4825
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004826 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004827 if( status != PSA_SUCCESS )
4828 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004829#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004830 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004831 psa_des_set_key_parity( data, bytes );
4832#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004833 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004834
4835exit:
4836 mbedtls_free( data );
4837 return( status );
4838}
4839
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004840psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004841 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004842 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004843{
4844 psa_status_t status;
4845 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004846 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004847
4848 /* Reject any attempt to create a zero-length key so that we don't
4849 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4850 if( psa_get_key_bits( attributes ) == 0 )
4851 return( PSA_ERROR_INVALID_ARGUMENT );
4852
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004853 if( ! operation->can_output_key )
4854 return( PSA_ERROR_NOT_PERMITTED );
4855
Gilles Peskinedf179142019-07-15 22:02:14 +02004856 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4857 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004858#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4859 if( driver != NULL )
4860 {
4861 /* Deriving a key in a secure element is not implemented yet. */
4862 status = PSA_ERROR_NOT_SUPPORTED;
4863 }
4864#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004865 if( status == PSA_SUCCESS )
4866 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004867 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004868 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004869 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004870 }
4871 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004872 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004873 if( status != PSA_SUCCESS )
4874 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004875 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004876 *handle = 0;
4877 }
4878 return( status );
4879}
4880
Gilles Peskineeab56e42018-07-12 17:12:33 +02004881
4882
4883/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004884/* Key derivation */
4885/****************************************************************/
4886
Gilles Peskine969c5d62019-01-16 15:53:06 +01004887static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004888 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004889 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004890{
Janos Follath5fe19732019-06-20 15:09:30 +01004891 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4892 * initialisers for this union leave some bytes unspecified.) */
4893 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4894
Gilles Peskine969c5d62019-01-16 15:53:06 +01004895 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004896#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004897 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4898 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4899 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004900 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004901 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004902 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4903 if( hash_size == 0 )
4904 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004905 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4906 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004907 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004908 {
4909 return( PSA_ERROR_NOT_SUPPORTED );
4910 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004911 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004912 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004913 }
4914#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004915 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004916 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004917}
4918
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004919psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004920 psa_algorithm_t alg )
4921{
4922 psa_status_t status;
4923
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004924 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004925 return( PSA_ERROR_BAD_STATE );
4926
Gilles Peskine6843c292019-01-18 16:44:49 +01004927 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4928 return( PSA_ERROR_INVALID_ARGUMENT );
4929 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004930 {
4931 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004932 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004933 }
4934 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4935 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004936 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004937 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004938 else
4939 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004940
4941 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004942 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004943 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004944}
4945
4946#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004947static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004948 psa_algorithm_t hash_alg,
4949 psa_key_derivation_step_t step,
4950 const uint8_t *data,
4951 size_t data_length )
4952{
4953 psa_status_t status;
4954 switch( step )
4955 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004956 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004957 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004958 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004959 status = psa_hmac_setup_internal( &hkdf->hmac,
4960 data, data_length,
4961 hash_alg );
4962 if( status != PSA_SUCCESS )
4963 return( status );
4964 hkdf->state = HKDF_STATE_STARTED;
4965 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004966 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004967 /* If no salt was provided, use an empty salt. */
4968 if( hkdf->state == HKDF_STATE_INIT )
4969 {
4970 status = psa_hmac_setup_internal( &hkdf->hmac,
4971 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004972 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004973 if( status != PSA_SUCCESS )
4974 return( status );
4975 hkdf->state = HKDF_STATE_STARTED;
4976 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004977 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004978 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004979 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4980 data, data_length );
4981 if( status != PSA_SUCCESS )
4982 return( status );
4983 status = psa_hmac_finish_internal( &hkdf->hmac,
4984 hkdf->prk,
4985 sizeof( hkdf->prk ) );
4986 if( status != PSA_SUCCESS )
4987 return( status );
4988 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4989 hkdf->block_number = 0;
4990 hkdf->state = HKDF_STATE_KEYED;
4991 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004992 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004993 if( hkdf->state == HKDF_STATE_OUTPUT )
4994 return( PSA_ERROR_BAD_STATE );
4995 if( hkdf->info_set )
4996 return( PSA_ERROR_BAD_STATE );
4997 hkdf->info_length = data_length;
4998 if( data_length != 0 )
4999 {
5000 hkdf->info = mbedtls_calloc( 1, data_length );
5001 if( hkdf->info == NULL )
5002 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5003 memcpy( hkdf->info, data, data_length );
5004 }
5005 hkdf->info_set = 1;
5006 return( PSA_SUCCESS );
5007 default:
5008 return( PSA_ERROR_INVALID_ARGUMENT );
5009 }
5010}
Janos Follathb03233e2019-06-11 15:30:30 +01005011
Janos Follathf08e2652019-06-13 09:05:41 +01005012static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5013 const uint8_t *data,
5014 size_t data_length )
5015{
5016 if( prf->state != TLS12_PRF_STATE_INIT )
5017 return( PSA_ERROR_BAD_STATE );
5018
Janos Follathd6dce9f2019-07-04 09:11:38 +01005019 if( data_length != 0 )
5020 {
5021 prf->seed = mbedtls_calloc( 1, data_length );
5022 if( prf->seed == NULL )
5023 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005024
Janos Follathd6dce9f2019-07-04 09:11:38 +01005025 memcpy( prf->seed, data, data_length );
5026 prf->seed_length = data_length;
5027 }
Janos Follathf08e2652019-06-13 09:05:41 +01005028
5029 prf->state = TLS12_PRF_STATE_SEED_SET;
5030
5031 return( PSA_SUCCESS );
5032}
5033
Janos Follath81550542019-06-13 14:26:34 +01005034static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5035 psa_algorithm_t hash_alg,
5036 const uint8_t *data,
5037 size_t data_length )
5038{
5039 psa_status_t status;
5040 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5041 return( PSA_ERROR_BAD_STATE );
5042
5043 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5044 if( status != PSA_SUCCESS )
5045 return( status );
5046
5047 prf->state = TLS12_PRF_STATE_KEY_SET;
5048
5049 return( PSA_SUCCESS );
5050}
5051
Janos Follath6660f0e2019-06-17 08:44:03 +01005052static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5053 psa_tls12_prf_key_derivation_t *prf,
5054 psa_algorithm_t hash_alg,
5055 const uint8_t *data,
5056 size_t data_length )
5057{
5058 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005059 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5060 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005061
5062 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5063 return( PSA_ERROR_INVALID_ARGUMENT );
5064
5065 /* Quoting RFC 4279, Section 2:
5066 *
5067 * The premaster secret is formed as follows: if the PSK is N octets
5068 * long, concatenate a uint16 with the value N, N zero octets, a second
5069 * uint16 with the value N, and the PSK itself.
5070 */
5071
Janos Follath40e13932019-06-26 13:22:29 +01005072 *cur++ = ( data_length >> 8 ) & 0xff;
5073 *cur++ = ( data_length >> 0 ) & 0xff;
5074 memset( cur, 0, data_length );
5075 cur += data_length;
5076 *cur++ = pms[0];
5077 *cur++ = pms[1];
5078 memcpy( cur, data, data_length );
5079 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005080
Janos Follath40e13932019-06-26 13:22:29 +01005081 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005082
5083 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5084 return( status );
5085}
5086
Janos Follath63028dd2019-06-13 09:15:47 +01005087static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5088 const uint8_t *data,
5089 size_t data_length )
5090{
5091 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5092 return( PSA_ERROR_BAD_STATE );
5093
Janos Follathd6dce9f2019-07-04 09:11:38 +01005094 if( data_length != 0 )
5095 {
5096 prf->label = mbedtls_calloc( 1, data_length );
5097 if( prf->label == NULL )
5098 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005099
Janos Follathd6dce9f2019-07-04 09:11:38 +01005100 memcpy( prf->label, data, data_length );
5101 prf->label_length = data_length;
5102 }
Janos Follath63028dd2019-06-13 09:15:47 +01005103
5104 prf->state = TLS12_PRF_STATE_LABEL_SET;
5105
5106 return( PSA_SUCCESS );
5107}
5108
Janos Follathb03233e2019-06-11 15:30:30 +01005109static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5110 psa_algorithm_t hash_alg,
5111 psa_key_derivation_step_t step,
5112 const uint8_t *data,
5113 size_t data_length )
5114{
Janos Follathb03233e2019-06-11 15:30:30 +01005115 switch( step )
5116 {
Janos Follathf08e2652019-06-13 09:05:41 +01005117 case PSA_KEY_DERIVATION_INPUT_SEED:
5118 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005119 case PSA_KEY_DERIVATION_INPUT_SECRET:
5120 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005121 case PSA_KEY_DERIVATION_INPUT_LABEL:
5122 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005123 default:
5124 return( PSA_ERROR_INVALID_ARGUMENT );
5125 }
5126}
Janos Follath6660f0e2019-06-17 08:44:03 +01005127
5128static psa_status_t psa_tls12_prf_psk_to_ms_input(
5129 psa_tls12_prf_key_derivation_t *prf,
5130 psa_algorithm_t hash_alg,
5131 psa_key_derivation_step_t step,
5132 const uint8_t *data,
5133 size_t data_length )
5134{
5135 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005136 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005137 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5138 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005139 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005140
5141 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5142}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005143#endif /* MBEDTLS_MD_C */
5144
Gilles Peskineb8965192019-09-24 16:21:10 +02005145/** Check whether the given key type is acceptable for the given
5146 * input step of a key derivation.
5147 *
5148 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5149 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5150 * Both secret and non-secret inputs can alternatively have the type
5151 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5152 * that the input was passed as a buffer rather than via a key object.
5153 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005154static int psa_key_derivation_check_input_type(
5155 psa_key_derivation_step_t step,
5156 psa_key_type_t key_type )
5157{
5158 switch( step )
5159 {
5160 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005161 if( key_type == PSA_KEY_TYPE_DERIVE )
5162 return( PSA_SUCCESS );
5163 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005164 return( PSA_SUCCESS );
5165 break;
5166 case PSA_KEY_DERIVATION_INPUT_LABEL:
5167 case PSA_KEY_DERIVATION_INPUT_SALT:
5168 case PSA_KEY_DERIVATION_INPUT_INFO:
5169 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005170 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5171 return( PSA_SUCCESS );
5172 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005173 return( PSA_SUCCESS );
5174 break;
5175 }
5176 return( PSA_ERROR_INVALID_ARGUMENT );
5177}
5178
Janos Follathb80a94e2019-06-12 15:54:46 +01005179static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005180 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005181 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005182 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005183 const uint8_t *data,
5184 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005185{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005186 psa_status_t status;
5187 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5188
5189 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005190 if( status != PSA_SUCCESS )
5191 goto exit;
5192
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005193#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005194 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005195 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005196 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005197 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005198 step, data, data_length );
5199 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005200 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005201 {
Janos Follathb03233e2019-06-11 15:30:30 +01005202 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5203 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5204 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005205 }
5206 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5207 {
5208 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5209 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5210 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005211 }
5212 else
5213#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005214 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005215 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005216 return( PSA_ERROR_BAD_STATE );
5217 }
5218
Gilles Peskine224b0d62019-09-23 18:13:17 +02005219exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005220 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005221 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005222 return( status );
5223}
5224
Janos Follath51f4a0f2019-06-14 11:35:55 +01005225psa_status_t psa_key_derivation_input_bytes(
5226 psa_key_derivation_operation_t *operation,
5227 psa_key_derivation_step_t step,
5228 const uint8_t *data,
5229 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005230{
Gilles Peskineb8965192019-09-24 16:21:10 +02005231 return( psa_key_derivation_input_internal( operation, step,
5232 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005233 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005234}
5235
Janos Follath51f4a0f2019-06-14 11:35:55 +01005236psa_status_t psa_key_derivation_input_key(
5237 psa_key_derivation_operation_t *operation,
5238 psa_key_derivation_step_t step,
5239 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005240{
5241 psa_key_slot_t *slot;
5242 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005243
Gilles Peskine28f8f302019-07-24 13:30:31 +02005244 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005245 PSA_KEY_USAGE_DERIVE,
5246 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005247 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005248 {
5249 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005250 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005251 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005252
5253 /* Passing a key object as a SECRET input unlocks the permission
5254 * to output to a key object. */
5255 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5256 operation->can_output_key = 1;
5257
Janos Follathb80a94e2019-06-12 15:54:46 +01005258 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005259 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005260 slot->data.key.data,
5261 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005262}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005263
5264
5265
5266/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005267/* Key agreement */
5268/****************************************************************/
5269
Gilles Peskinea05219c2018-11-16 16:02:56 +01005270#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005271static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5272 size_t peer_key_length,
5273 const mbedtls_ecp_keypair *our_key,
5274 uint8_t *shared_secret,
5275 size_t shared_secret_size,
5276 size_t *shared_secret_length )
5277{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005278 mbedtls_ecp_keypair *their_key = NULL;
5279 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005280 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005281 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005282 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005283 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005284
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005285 status = psa_import_ec_public_key( curve,
5286 peer_key, peer_key_length,
5287 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005288 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005289 goto exit;
5290
Jaeden Amero97271b32019-01-10 19:38:51 +00005291 status = mbedtls_to_psa_error(
5292 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5293 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005294 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005295 status = mbedtls_to_psa_error(
5296 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5297 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005298 goto exit;
5299
Jaeden Amero97271b32019-01-10 19:38:51 +00005300 status = mbedtls_to_psa_error(
5301 mbedtls_ecdh_calc_secret( &ecdh,
5302 shared_secret_length,
5303 shared_secret, shared_secret_size,
5304 mbedtls_ctr_drbg_random,
5305 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005306 if( status != PSA_SUCCESS )
5307 goto exit;
5308 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5309 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005310
5311exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005312 if( status != PSA_SUCCESS )
5313 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005314 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005315 mbedtls_ecp_keypair_free( their_key );
5316 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005317 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005318}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005319#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005320
Gilles Peskine01d718c2018-09-18 12:01:02 +02005321#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5322
Gilles Peskine0216fe12019-04-11 21:23:21 +02005323static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5324 psa_key_slot_t *private_key,
5325 const uint8_t *peer_key,
5326 size_t peer_key_length,
5327 uint8_t *shared_secret,
5328 size_t shared_secret_size,
5329 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005330{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005331 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005332 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005333#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005334 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005335 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005336 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005337 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5338 private_key->data.ecp,
5339 shared_secret, shared_secret_size,
5340 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005341#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005342 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005343 (void) private_key;
5344 (void) peer_key;
5345 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005346 (void) shared_secret;
5347 (void) shared_secret_size;
5348 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005349 return( PSA_ERROR_NOT_SUPPORTED );
5350 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005351}
5352
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005353/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005354 * to potentially free embedded data structures and wipe confidential data.
5355 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005356static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005357 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005358 psa_key_slot_t *private_key,
5359 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005360 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005361{
5362 psa_status_t status;
5363 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5364 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005365 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005366
5367 /* Step 1: run the secret agreement algorithm to generate the shared
5368 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005369 status = psa_key_agreement_raw_internal( ka_alg,
5370 private_key,
5371 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005372 shared_secret,
5373 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005374 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005375 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005376 goto exit;
5377
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005378 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005379 * the shared secret. A shared secret is permitted wherever a key
5380 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005381 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005382 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005383 shared_secret,
5384 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005385
Gilles Peskine12313cd2018-06-20 00:20:32 +02005386exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005387 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005388 return( status );
5389}
5390
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005391psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005392 psa_key_derivation_step_t step,
5393 psa_key_handle_t private_key,
5394 const uint8_t *peer_key,
5395 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005396{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005397 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005398 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005399 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005400 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005401 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005402 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005403 if( status != PSA_SUCCESS )
5404 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005405 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005406 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005407 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005408 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005409 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005410 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005411}
5412
Gilles Peskinebe697d82019-05-16 18:00:41 +02005413psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5414 psa_key_handle_t private_key,
5415 const uint8_t *peer_key,
5416 size_t peer_key_length,
5417 uint8_t *output,
5418 size_t output_size,
5419 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005420{
5421 psa_key_slot_t *slot;
5422 psa_status_t status;
5423
5424 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5425 {
5426 status = PSA_ERROR_INVALID_ARGUMENT;
5427 goto exit;
5428 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005429 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005430 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005431 if( status != PSA_SUCCESS )
5432 goto exit;
5433
5434 status = psa_key_agreement_raw_internal( alg, slot,
5435 peer_key, peer_key_length,
5436 output, output_size,
5437 output_length );
5438
5439exit:
5440 if( status != PSA_SUCCESS )
5441 {
5442 /* If an error happens and is not handled properly, the output
5443 * may be used as a key to protect sensitive data. Arrange for such
5444 * a key to be random, which is likely to result in decryption or
5445 * verification errors. This is better than filling the buffer with
5446 * some constant data such as zeros, which would result in the data
5447 * being protected with a reproducible, easily knowable key.
5448 */
5449 psa_generate_random( output, output_size );
5450 *output_length = output_size;
5451 }
5452 return( status );
5453}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005454
5455
5456/****************************************************************/
5457/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005458/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005459
Gilles Peskine4c317f42018-07-12 01:24:09 +02005460psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005461 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005462{
Janos Follath24eed8d2019-11-22 13:21:35 +00005463 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005464 GUARD_MODULE_INITIALIZED;
5465
Gilles Peskinef181eca2019-08-07 13:49:00 +02005466 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5467 {
5468 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5469 output,
5470 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5471 if( ret != 0 )
5472 return( mbedtls_to_psa_error( ret ) );
5473 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5474 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5475 }
5476
Gilles Peskinee59236f2018-01-27 23:32:46 +01005477 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5478 return( mbedtls_to_psa_error( ret ) );
5479}
5480
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005481#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5482#include "mbedtls/entropy_poll.h"
5483
Gilles Peskine7228da22019-07-15 11:06:15 +02005484psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005485 size_t seed_size )
5486{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005487 if( global_data.initialized )
5488 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005489
5490 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5491 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5492 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5493 return( PSA_ERROR_INVALID_ARGUMENT );
5494
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005495 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005496}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005497#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005498
Gilles Peskinee56e8782019-04-26 17:34:02 +02005499#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5500static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5501 size_t domain_parameters_size,
5502 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005503{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005504 size_t i;
5505 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005506
Gilles Peskinee56e8782019-04-26 17:34:02 +02005507 if( domain_parameters_size == 0 )
5508 {
5509 *exponent = 65537;
5510 return( PSA_SUCCESS );
5511 }
5512
5513 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5514 * support values that fit in a 32-bit integer, which is larger than
5515 * int on just about every platform anyway. */
5516 if( domain_parameters_size > sizeof( acc ) )
5517 return( PSA_ERROR_NOT_SUPPORTED );
5518 for( i = 0; i < domain_parameters_size; i++ )
5519 acc = ( acc << 8 ) | domain_parameters[i];
5520 if( acc > INT_MAX )
5521 return( PSA_ERROR_NOT_SUPPORTED );
5522 *exponent = acc;
5523 return( PSA_SUCCESS );
5524}
5525#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5526
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005527static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005528 psa_key_slot_t *slot, size_t bits,
5529 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005530{
Gilles Peskine8e338702019-07-30 20:06:31 +02005531 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005532
Gilles Peskinee56e8782019-04-26 17:34:02 +02005533 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005534 return( PSA_ERROR_INVALID_ARGUMENT );
5535
Gilles Peskinee59236f2018-01-27 23:32:46 +01005536 if( key_type_is_raw_bytes( type ) )
5537 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005538 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005539
5540 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005541 if( status != PSA_SUCCESS )
5542 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005543
5544 /* Allocate memory for the key */
5545 slot->data.key.bytes = PSA_BITS_TO_BYTES( bits );
5546 slot->data.key.data = mbedtls_calloc( 1, slot->data.key.bytes );
5547 if( slot->data.key.data == NULL )
5548 {
5549 slot->data.key.bytes = 0;
5550 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5551 }
5552
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005553 status = psa_generate_random( slot->data.key.data,
5554 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005555 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005556 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005557
5558 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005559#if defined(MBEDTLS_DES_C)
5560 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005561 psa_des_set_key_parity( slot->data.key.data,
5562 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005563#endif /* MBEDTLS_DES_C */
5564 }
5565 else
5566
5567#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005568 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005569 {
5570 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005571 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005572 int exponent;
5573 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005574 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5575 return( PSA_ERROR_NOT_SUPPORTED );
5576 /* Accept only byte-aligned keys, for the same reasons as
5577 * in psa_import_rsa_key(). */
5578 if( bits % 8 != 0 )
5579 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005580 status = psa_read_rsa_exponent( domain_parameters,
5581 domain_parameters_size,
5582 &exponent );
5583 if( status != PSA_SUCCESS )
5584 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005585 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5586 if( rsa == NULL )
5587 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5588 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5589 ret = mbedtls_rsa_gen_key( rsa,
5590 mbedtls_ctr_drbg_random,
5591 &global_data.ctr_drbg,
5592 (unsigned int) bits,
5593 exponent );
5594 if( ret != 0 )
5595 {
5596 mbedtls_rsa_free( rsa );
5597 mbedtls_free( rsa );
5598 return( mbedtls_to_psa_error( ret ) );
5599 }
5600 slot->data.rsa = rsa;
5601 }
5602 else
5603#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5604
5605#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005606 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005607 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01005608 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005609 mbedtls_ecp_group_id grp_id =
5610 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005611 const mbedtls_ecp_curve_info *curve_info =
5612 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5613 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005614 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005615 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005616 return( PSA_ERROR_NOT_SUPPORTED );
5617 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5618 return( PSA_ERROR_NOT_SUPPORTED );
5619 if( curve_info->bit_size != bits )
5620 return( PSA_ERROR_INVALID_ARGUMENT );
5621 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5622 if( ecp == NULL )
5623 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5624 mbedtls_ecp_keypair_init( ecp );
5625 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5626 mbedtls_ctr_drbg_random,
5627 &global_data.ctr_drbg );
5628 if( ret != 0 )
5629 {
5630 mbedtls_ecp_keypair_free( ecp );
5631 mbedtls_free( ecp );
5632 return( mbedtls_to_psa_error( ret ) );
5633 }
5634 slot->data.ecp = ecp;
5635 }
5636 else
5637#endif /* MBEDTLS_ECP_C */
5638
5639 return( PSA_ERROR_NOT_SUPPORTED );
5640
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005641 return( PSA_SUCCESS );
5642}
Darryl Green0c6575a2018-11-07 16:05:30 +00005643
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005644psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005645 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005646{
5647 psa_status_t status;
5648 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005649 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005650
Gilles Peskine0f84d622019-09-12 19:03:13 +02005651 /* Reject any attempt to create a zero-length key so that we don't
5652 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5653 if( psa_get_key_bits( attributes ) == 0 )
5654 return( PSA_ERROR_INVALID_ARGUMENT );
5655
Gilles Peskinedf179142019-07-15 22:02:14 +02005656 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5657 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005658 if( status != PSA_SUCCESS )
5659 goto exit;
5660
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005661#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5662 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005663 {
Gilles Peskine11792082019-08-06 18:36:36 +02005664 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5665 size_t pubkey_length = 0; /* We don't support this feature yet */
5666 if( drv->key_management == NULL ||
5667 drv->key_management->p_generate == NULL )
5668 {
5669 status = PSA_ERROR_NOT_SUPPORTED;
5670 goto exit;
5671 }
5672 status = drv->key_management->p_generate(
5673 psa_get_se_driver_context( driver ),
5674 slot->data.se.slot_number, attributes,
5675 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005676 }
Gilles Peskine11792082019-08-06 18:36:36 +02005677 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005678#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005679 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005680 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005681 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005682 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005683 }
Gilles Peskine11792082019-08-06 18:36:36 +02005684
5685exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005686 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005687 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005688 if( status != PSA_SUCCESS )
5689 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005690 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005691 *handle = 0;
5692 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005693 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005694}
5695
5696
Gilles Peskinee59236f2018-01-27 23:32:46 +01005697
5698/****************************************************************/
5699/* Module setup */
5700/****************************************************************/
5701
Gilles Peskine5e769522018-11-20 21:59:56 +01005702psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5703 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5704 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5705{
5706 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5707 return( PSA_ERROR_BAD_STATE );
5708 global_data.entropy_init = entropy_init;
5709 global_data.entropy_free = entropy_free;
5710 return( PSA_SUCCESS );
5711}
5712
Gilles Peskinee59236f2018-01-27 23:32:46 +01005713void mbedtls_psa_crypto_free( void )
5714{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005715 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005716 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5717 {
5718 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005719 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005720 }
5721 /* Wipe all remaining data, including configuration.
5722 * In particular, this sets all state indicator to the value
5723 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005724 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005725#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005726 /* Unregister all secure element drivers, so that we restart from
5727 * a pristine state. */
5728 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005729#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005730}
5731
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005732#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5733/** Recover a transaction that was interrupted by a power failure.
5734 *
5735 * This function is called during initialization, before psa_crypto_init()
5736 * returns. If this function returns a failure status, the initialization
5737 * fails.
5738 */
5739static psa_status_t psa_crypto_recover_transaction(
5740 const psa_crypto_transaction_t *transaction )
5741{
5742 switch( transaction->unknown.type )
5743 {
5744 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5745 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005746 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005747 * is implemented.
5748 * https://github.com/ARMmbed/mbed-crypto/issues/218
5749 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005750 default:
5751 /* We found an unsupported transaction in the storage.
5752 * We don't know what state the storage is in. Give up. */
5753 return( PSA_ERROR_STORAGE_FAILURE );
5754 }
5755}
5756#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5757
Gilles Peskinee59236f2018-01-27 23:32:46 +01005758psa_status_t psa_crypto_init( void )
5759{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005760 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005761 const unsigned char drbg_seed[] = "PSA";
5762
Gilles Peskinec6b69072018-11-20 21:42:52 +01005763 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005764 if( global_data.initialized != 0 )
5765 return( PSA_SUCCESS );
5766
Gilles Peskine5e769522018-11-20 21:59:56 +01005767 /* Set default configuration if
5768 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5769 if( global_data.entropy_init == NULL )
5770 global_data.entropy_init = mbedtls_entropy_init;
5771 if( global_data.entropy_free == NULL )
5772 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005773
Gilles Peskinec6b69072018-11-20 21:42:52 +01005774 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005775 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005776#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5777 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5778 /* The PSA entropy injection feature depends on using NV seed as an entropy
5779 * source. Add NV seed as an entropy source for PSA entropy injection. */
5780 mbedtls_entropy_add_source( &global_data.entropy,
5781 mbedtls_nv_seed_poll, NULL,
5782 MBEDTLS_ENTROPY_BLOCK_SIZE,
5783 MBEDTLS_ENTROPY_SOURCE_STRONG );
5784#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005785 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005786 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005787 status = mbedtls_to_psa_error(
5788 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5789 mbedtls_entropy_func,
5790 &global_data.entropy,
5791 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5792 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005793 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005794 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005795
Gilles Peskine66fb1262018-12-10 16:29:04 +01005796 status = psa_initialize_key_slots( );
5797 if( status != PSA_SUCCESS )
5798 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005799
Gilles Peskined9348f22019-10-01 15:22:29 +02005800#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5801 status = psa_init_all_se_drivers( );
5802 if( status != PSA_SUCCESS )
5803 goto exit;
5804#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5805
Gilles Peskine4b734222019-07-24 15:56:31 +02005806#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005807 status = psa_crypto_load_transaction( );
5808 if( status == PSA_SUCCESS )
5809 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005810 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5811 if( status != PSA_SUCCESS )
5812 goto exit;
5813 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005814 }
5815 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5816 {
5817 /* There's no transaction to complete. It's all good. */
5818 status = PSA_SUCCESS;
5819 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005820#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005821
Gilles Peskinec6b69072018-11-20 21:42:52 +01005822 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005823 global_data.initialized = 1;
5824
5825exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005826 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005827 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005828 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005829}
5830
5831#endif /* MBEDTLS_PSA_CRYPTO_C */