blob: b6a34dfd3de2e42f508778b1843209c1854af976 [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
Steven Cooremana01795d2020-07-24 22:48:15 +0200495#if defined(MBEDTLS_RSA_C)
496
497#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100498/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
499 * that are not a multiple of 8) well. For example, there is only
500 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
501 * way to return the exact bit size of a key.
502 * To keep things simple, reject non-byte-aligned key sizes. */
503static psa_status_t psa_check_rsa_key_byte_aligned(
504 const mbedtls_rsa_context *rsa )
505{
506 mbedtls_mpi n;
507 psa_status_t status;
508 mbedtls_mpi_init( &n );
509 status = mbedtls_to_psa_error(
510 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
511 if( status == PSA_SUCCESS )
512 {
513 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
514 status = PSA_ERROR_NOT_SUPPORTED;
515 }
516 mbedtls_mpi_free( &n );
517 return( status );
518}
Steven Cooremana01795d2020-07-24 22:48:15 +0200519#endif /* MBEDTLS_PK_PARSE_C */
Gilles Peskine86a440b2018-11-12 18:39:40 +0100520
Steven Cooremana01795d2020-07-24 22:48:15 +0200521/** Load the contents of a key slot into an internal RSA representation
522 *
Steven Cooremana2371e52020-07-28 14:30:39 +0200523 * \param[in] slot The slot from which to load the representation
524 * \param[out] p_rsa Returns a pointer to an RSA context on success.
525 * The caller is responsible for freeing both the
526 * contents of the context and the context itself
527 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200528 */
529static psa_status_t psa_load_rsa_representation( const psa_key_slot_t *slot,
Steven Cooremana2371e52020-07-28 14:30:39 +0200530 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200531{
Steven Cooremana01795d2020-07-24 22:48:15 +0200532#if defined(MBEDTLS_PK_PARSE_C)
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000533 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200534 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000535 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200536 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000537
538 /* Parse the data. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200539 if( PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000540 status = mbedtls_to_psa_error(
Steven Cooremana01795d2020-07-24 22:48:15 +0200541 mbedtls_pk_parse_key( &ctx, slot->data.key.data, slot->data.key.bytes, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200542 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000543 status = mbedtls_to_psa_error(
Steven Cooremana01795d2020-07-24 22:48:15 +0200544 mbedtls_pk_parse_public_key( &ctx, slot->data.key.data, slot->data.key.bytes ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000545 if( status != PSA_SUCCESS )
546 goto exit;
547
548 /* We have something that the pkparse module recognizes. If it is a
549 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200550 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200551 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000552 status = PSA_ERROR_INVALID_ARGUMENT;
553 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200554 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000555
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000556 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
557 * supports non-byte-aligned key sizes, but not well. For example,
558 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200559 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000560 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
561 {
562 status = PSA_ERROR_NOT_SUPPORTED;
563 goto exit;
564 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200565 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
566
567 if( status != PSA_SUCCESS )
568 goto exit;
569
Steven Cooremana2371e52020-07-28 14:30:39 +0200570 /* Copy out the pointer to the RSA context, and reset the PK context
571 * such that pk_free doesn't free the RSA context we just grabbed. */
572 *p_rsa = mbedtls_pk_rsa( ctx );
573 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000574
575exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200576 mbedtls_pk_free( &ctx );
577 return( status );
578#else
579 (void) slot;
580 (void) rsa;
581 return( PSA_ERROR_NOT_SUPPORTED );
582#endif /* MBEDTLS_PK_PARSE_C */
583}
584
585/** Export an RSA key to export representation
586 *
587 * \param[in] type The type of key (public/private) to export
588 * \param[in] rsa The internal RSA representation from which to export
589 * \param[out] data The buffer to export to
590 * \param[in] data_size The length of the buffer to export to
591 * \param[out] data_length The amount of bytes written to \p data
592 */
593static psa_status_t psa_export_rsa_key( psa_key_type_t type,
594 mbedtls_rsa_context *rsa,
595 uint8_t *data,
596 size_t data_size,
597 size_t *data_length )
598{
599#if defined(MBEDTLS_PK_WRITE_C)
600 int ret;
601 mbedtls_pk_context pk;
602 uint8_t *pos = data + data_size;
603
604 mbedtls_pk_init( &pk );
605 pk.pk_info = &mbedtls_rsa_info;
606 pk.pk_ctx = rsa;
607
608 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200609 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
610 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200611 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
612 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
613 else
614 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
615
616 if( ret < 0 )
617 return mbedtls_to_psa_error( ret );
618
619 /* The mbedtls_pk_xxx functions write to the end of the buffer.
620 * Move the data to the beginning and erase remaining data
621 * at the original location. */
622 if( 2 * (size_t) ret <= data_size )
623 {
624 memcpy( data, data + data_size - ret, ret );
625 memset( data + data_size - ret, 0, ret );
626 }
627 else if( (size_t) ret < data_size )
628 {
629 memmove( data, data + data_size - ret, ret );
630 memset( data + ret, 0, data_size - ret );
631 }
632
633 *data_length = ret;
634 return( PSA_SUCCESS );
635#else
636 (void) type;
637 (void) rsa;
638 (void) data;
639 (void) data_size;
640 (void) data_length;
641 return( PSA_ERROR_NOT_SUPPORTED );
642#endif /* MBEDTLS_PK_WRITE_C */
643}
644
645/** Import an RSA key from import representation to a slot
646 *
647 * \param[in,out] slot The slot where to store the export representation to
648 * \param[in] data The buffer containing the import representation
649 * \param[in] data_length The amount of bytes in \p data
650 */
651static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
652 const uint8_t *data,
653 size_t data_length )
654{
655 psa_status_t status;
656 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200657 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200658
659 /* Temporarily load input into slot. The cast here is safe since it'll
660 * only be used for load_rsa_representation, which doesn't modify the
661 * buffer. */
662 slot->data.key.data = (uint8_t *)data;
663 slot->data.key.bytes = data_length;
664
665 /* Parse input */
666 status = psa_load_rsa_representation( slot, &rsa );
667 if( status != PSA_SUCCESS )
668 goto exit;
669
670 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200671 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200672
Steven Cooreman75b74362020-07-28 14:30:13 +0200673 /* Re-export the data to PSA export format, such that we can store export
674 * representation in the key slot. Export representation in case of RSA is
675 * the smallest representation that's allowed as input, so a straight-up
676 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200677 output = mbedtls_calloc( 1, data_length );
678
679 if( output == NULL )
680 {
681 status = PSA_ERROR_INSUFFICIENT_MEMORY;
682 goto exit;
683 }
684
Steven Cooremana01795d2020-07-24 22:48:15 +0200685 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200686 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200687 output,
688 data_length,
689 &data_length);
690
691exit:
692 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200693 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200694 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200695
696 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000697 if( status != PSA_SUCCESS )
698 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200699 mbedtls_free( output );
700 slot->data.key.data = NULL;
701 slot->data.key.bytes = 0;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000702 return( status );
703 }
704
Steven Cooremana01795d2020-07-24 22:48:15 +0200705 /* On success, store the allocated export-formatted key. */
706 slot->data.key.data = output;
707 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000708
709 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200710}
Steven Cooremana01795d2020-07-24 22:48:15 +0200711#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200712
Jaeden Ameroccdce902019-01-10 11:42:27 +0000713#if defined(MBEDTLS_ECP_C)
Steven Cooremana2371e52020-07-28 14:30:39 +0200714/** Load the contents of a key slot into an internal ECP representation
715 *
716 * \param[in] slot The slot from which to load the representation
717 * \param[out] p_ecp Returns a pointer to an ECP context on success.
718 * The caller is responsible for freeing both the
719 * contents of the context and the context itself
720 * when done.
721 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200722static psa_status_t psa_load_ecp_representation( const psa_key_slot_t *slot,
Steven Cooremana2371e52020-07-28 14:30:39 +0200723 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100724{
725 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200726 size_t data_length = slot->data.key.bytes;
727 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200728 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100729
Steven Cooremanacda8342020-07-24 23:09:52 +0200730 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100731 {
732 /* A public key is represented as:
733 * - The byte 0x04;
734 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
735 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
736 * So its data length is 2m+1 where n is the key size in bits.
737 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200738 if( ( slot->data.key.bytes & 1 ) == 0 )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100739 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +0200740 data_length = slot->data.key.bytes / 2;
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100741 }
742
Steven Cooreman6d839f02020-07-30 11:36:45 +0200743 /* Allocate and initialize a key representation. */
744 ecp = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
745 if( ecp == NULL )
746 return PSA_ERROR_INSUFFICIENT_MEMORY;
Steven Cooremana2371e52020-07-28 14:30:39 +0200747 mbedtls_ecp_keypair_init( ecp );
748
Gilles Peskine4cd32772019-12-02 20:49:42 +0100749 /* Load the group. */
Steven Cooreman6d839f02020-07-30 11:36:45 +0200750 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ),
Steven Cooremanacda8342020-07-24 23:09:52 +0200751 data_length );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100752 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200753 {
754 status = PSA_ERROR_INVALID_ARGUMENT;
755 goto exit;
756 }
757
Steven Cooremanacda8342020-07-24 23:09:52 +0200758 status = mbedtls_to_psa_error(
759 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
760 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200761 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200762
Steven Cooreman6d839f02020-07-30 11:36:45 +0200763 /* Load the key material. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200764 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
765 {
766 /* Load the public value. */
767 status = mbedtls_to_psa_error(
768 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
769 slot->data.key.data,
770 slot->data.key.bytes ) );
771 if( status != PSA_SUCCESS )
772 goto exit;
773
774 /* Check that the point is on the curve. */
775 status = mbedtls_to_psa_error(
776 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
777 if( status != PSA_SUCCESS )
778 goto exit;
779 }
780 else
781 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200782 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200783 status = mbedtls_to_psa_error(
784 mbedtls_ecp_read_key( ecp->grp.id,
785 ecp,
786 slot->data.key.data,
787 slot->data.key.bytes ) );
788
789 if( status != PSA_SUCCESS )
790 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200791 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200792
793 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200794exit:
795 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200796 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200797 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200798 mbedtls_free( ecp );
799 }
800
Steven Cooremanacda8342020-07-24 23:09:52 +0200801 return status;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100802}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000803
Steven Cooremanacda8342020-07-24 23:09:52 +0200804static psa_status_t psa_export_ecp_key( psa_key_type_t type,
805 mbedtls_ecp_keypair *ecp,
806 uint8_t *data,
807 size_t data_size,
808 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200809{
Steven Cooremanacda8342020-07-24 23:09:52 +0200810 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000811
Steven Cooremanacda8342020-07-24 23:09:52 +0200812 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200813 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200814 /* Check whether the public part is loaded */
815 if( mbedtls_ecp_is_zero( &ecp->Q ) )
816 {
817 /* Calculate the public key */
818 status = mbedtls_to_psa_error(
819 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
820 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
821 if( status != PSA_SUCCESS )
822 return status;
823 }
824
825 return( mbedtls_to_psa_error(
826 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
827 MBEDTLS_ECP_PF_UNCOMPRESSED,
828 data_length,
829 data,
830 data_size ) ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200831 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200832 else
833 {
834 if( data_size < PSA_BITS_TO_BYTES(ecp->grp.nbits) )
835 return( PSA_ERROR_BUFFER_TOO_SMALL );
836
837 status = mbedtls_to_psa_error(
838 mbedtls_ecp_write_key( ecp,
839 data,
840 PSA_BITS_TO_BYTES(ecp->grp.nbits) ) );
841 if( status == PSA_SUCCESS )
842 {
843 *data_length = PSA_BITS_TO_BYTES(ecp->grp.nbits);
844 }
845
846 return( status );
847 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200848}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200849
Steven Cooremanacda8342020-07-24 23:09:52 +0200850static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
851 const uint8_t *data,
852 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100853{
Steven Cooremanacda8342020-07-24 23:09:52 +0200854 psa_status_t status;
855 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200856 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100857
Steven Cooremanacda8342020-07-24 23:09:52 +0200858 /* Temporarily load input into slot. The cast here is safe since it'll
859 * only be used for load_ecp_representation, which doesn't modify the
860 * buffer. */
861 slot->data.key.data = (uint8_t *)data;
862 slot->data.key.bytes = data_length;
863
864 /* Parse input */
865 status = psa_load_ecp_representation( slot, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100866 if( status != PSA_SUCCESS )
867 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100868
Steven Cooremanacda8342020-07-24 23:09:52 +0200869 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200870 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200871 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200872 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200873
Steven Cooremanacda8342020-07-24 23:09:52 +0200874 /* Re-export the data to PSA export format. There is currently no support
875 * for other input formats then the export format, so this is a 1-1
876 * copy operation. */
877 output = mbedtls_calloc( 1, data_length );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100878
Steven Cooremanacda8342020-07-24 23:09:52 +0200879 if( output == NULL )
880 {
881 status = PSA_ERROR_INSUFFICIENT_MEMORY;
882 goto exit;
883 }
884
885 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200886 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200887 output,
888 data_length,
889 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100890
891exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200892 /* Always free the PK object (will also free contained ECP context) */
893 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200894 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200895
896 /* Free the allocated buffer only on error. */
897 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100898 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200899 mbedtls_free( output );
900 slot->data.key.data = NULL;
901 slot->data.key.bytes = 0;
902 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100903 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200904
905 /* On success, store the allocated export-formatted key. */
906 slot->data.key.data = output;
907 slot->data.key.bytes = data_length;
908
909 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100910}
911#endif /* defined(MBEDTLS_ECP_C) */
912
Gilles Peskineb46bef22019-07-30 21:32:04 +0200913/** Return the size of the key in the given slot, in bits.
914 *
915 * \param[in] slot A key slot.
916 *
917 * \return The key size in bits, read from the metadata in the slot.
918 */
919static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
920{
921 return( slot->attr.bits );
922}
923
Steven Cooreman75b74362020-07-28 14:30:13 +0200924/** Try to allocate a buffer to an empty key slot.
925 *
926 * \param[in,out] slot Key slot to attach buffer to.
927 * \param[in] buffer_length Requested size of the buffer.
928 *
929 * \retval #PSA_SUCCESS
930 * The buffer has been successfully allocated.
931 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
932 * Not enough memory was available for allocation.
933 * \retval #PSA_ERROR_ALREADY_EXISTS
934 * Trying to allocate a buffer to a non-empty key slot.
935 */
936static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
937 size_t buffer_length )
938{
939 if( slot->data.key.data != NULL )
940 return PSA_ERROR_ALREADY_EXISTS;
941
942 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
943 if( slot->data.key.data == NULL )
944 return PSA_ERROR_INSUFFICIENT_MEMORY;
945
946 slot->data.key.bytes = buffer_length;
947 return PSA_SUCCESS;
948}
949
Gilles Peskine8e338702019-07-30 20:06:31 +0200950/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100951 * previously. This function assumes that the slot does not contain
952 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100953psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
954 const uint8_t *data,
955 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100956{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200957 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100958
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200959 /* zero-length keys are never supported. */
960 if( data_length == 0 )
961 return( PSA_ERROR_NOT_SUPPORTED );
962
Gilles Peskine8e338702019-07-30 20:06:31 +0200963 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100964 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200965 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200966
Steven Cooreman75b74362020-07-28 14:30:13 +0200967 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
968 if( data_length > SIZE_MAX / 8 )
969 return( PSA_ERROR_NOT_SUPPORTED );
970
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200971 /* Enforce a size limit, and in particular ensure that the bit
972 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200973 if( bit_size > PSA_MAX_KEY_BITS )
974 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200975
976 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200977 if( status != PSA_SUCCESS )
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200978 return status;
979
980 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +0200981 status = psa_allocate_buffer_to_slot( slot, data_length );
982 if( status != PSA_SUCCESS )
983 return status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200984
985 /* copy key into allocated buffer */
986 memcpy(slot->data.key.data, data, data_length);
987
988 /* Write the actual key size to the slot.
989 * psa_start_key_creation() wrote the size declared by the
990 * caller, which may be 0 (meaning unspecified) or wrong. */
991 slot->attr.bits = (psa_key_bits_t) bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100992 }
Steven Cooreman19fd5742020-07-24 23:31:01 +0200993 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
994 {
Gilles Peskinef76aa772018-10-29 19:24:33 +0100995#if defined(MBEDTLS_ECP_C)
Steven Cooreman19fd5742020-07-24 23:31:01 +0200996 status = psa_import_ecp_key( slot,
997 data, data_length );
998#else
999 /* No drivers have been implemented yet, so without mbed TLS backing
1000 * there's no way to do ECP with the current library. */
1001 return( PSA_ERROR_NOT_SUPPORTED );
1002#endif /* defined(MBEDTLS_ECP_C) */
1003 }
1004 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +01001005 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001006#if defined(MBEDTLS_RSA_C)
1007 status = psa_import_rsa_key( slot,
1008 data, data_length );
1009#else
1010 /* No drivers have been implemented yet, so without mbed TLS backing
1011 * there's no way to do RSA with the current library. */
1012 status = PSA_ERROR_NOT_SUPPORTED;
1013#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001014 }
1015 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001016 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001017 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001018 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001019 }
Darryl Green940d72c2018-07-13 13:18:51 +01001020
Darryl Green06fd18d2018-07-16 11:21:11 +01001021 return( status );
1022}
1023
Gilles Peskinef603c712019-01-19 13:40:11 +01001024/** Calculate the intersection of two algorithm usage policies.
1025 *
1026 * Return 0 (which allows no operation) on incompatibility.
1027 */
1028static psa_algorithm_t psa_key_policy_algorithm_intersection(
1029 psa_algorithm_t alg1,
1030 psa_algorithm_t alg2 )
1031{
Gilles Peskine549ea862019-05-22 11:45:59 +02001032 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001033 if( alg1 == alg2 )
1034 return( alg1 );
1035 /* If the policies are from the same hash-and-sign family, check
1036 * if one is a wildcard. If so the other has the specific algorithm. */
1037 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1038 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1039 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1040 {
1041 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1042 return( alg2 );
1043 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1044 return( alg1 );
1045 }
1046 /* If the policies are incompatible, allow nothing. */
1047 return( 0 );
1048}
1049
Gilles Peskined6f371b2019-05-10 19:33:38 +02001050static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1051 psa_algorithm_t requested_alg )
1052{
Gilles Peskine549ea862019-05-22 11:45:59 +02001053 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001054 if( requested_alg == policy_alg )
1055 return( 1 );
1056 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001057 * and requested_alg is the same hash-and-sign family with any hash,
1058 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001059 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1060 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1061 {
1062 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1063 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1064 }
1065 /* If it isn't permitted, it's forbidden. */
1066 return( 0 );
1067}
1068
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001069/** Test whether a policy permits an algorithm.
1070 *
1071 * The caller must test usage flags separately.
1072 */
1073static int psa_key_policy_permits( const psa_key_policy_t *policy,
1074 psa_algorithm_t alg )
1075{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001076 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1077 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001078}
1079
Gilles Peskinef603c712019-01-19 13:40:11 +01001080/** Restrict a key policy based on a constraint.
1081 *
1082 * \param[in,out] policy The policy to restrict.
1083 * \param[in] constraint The policy constraint to apply.
1084 *
1085 * \retval #PSA_SUCCESS
1086 * \c *policy contains the intersection of the original value of
1087 * \c *policy and \c *constraint.
1088 * \retval #PSA_ERROR_INVALID_ARGUMENT
1089 * \c *policy and \c *constraint are incompatible.
1090 * \c *policy is unchanged.
1091 */
1092static psa_status_t psa_restrict_key_policy(
1093 psa_key_policy_t *policy,
1094 const psa_key_policy_t *constraint )
1095{
1096 psa_algorithm_t intersection_alg =
1097 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001098 psa_algorithm_t intersection_alg2 =
1099 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001100 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1101 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001102 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1103 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001104 policy->usage &= constraint->usage;
1105 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001106 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001107 return( PSA_SUCCESS );
1108}
1109
Darryl Green06fd18d2018-07-16 11:21:11 +01001110/** Retrieve a slot which must contain a key. The key must have allow all the
1111 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1112 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001113static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001114 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001115 psa_key_usage_t usage,
1116 psa_algorithm_t alg )
1117{
1118 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001119 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001120
1121 *p_slot = NULL;
1122
Gilles Peskinec5487a82018-12-03 18:08:14 +01001123 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001124 if( status != PSA_SUCCESS )
1125 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001126
1127 /* Enforce that usage policy for the key slot contains all the flags
1128 * required by the usage parameter. There is one exception: public
1129 * keys can always be exported, so we treat public key objects as
1130 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001131 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001132 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001133 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001134 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001135
1136 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001137 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001138 return( PSA_ERROR_NOT_PERMITTED );
1139
1140 *p_slot = slot;
1141 return( PSA_SUCCESS );
1142}
Darryl Green940d72c2018-07-13 13:18:51 +01001143
Gilles Peskine28f8f302019-07-24 13:30:31 +02001144/** Retrieve a slot which must contain a transparent key.
1145 *
1146 * A transparent key is a key for which the key material is directly
1147 * available, as opposed to a key in a secure element.
1148 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001149 * This is a temporary function to use instead of psa_get_key_from_slot()
1150 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001151 */
1152#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1153static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1154 psa_key_slot_t **p_slot,
1155 psa_key_usage_t usage,
1156 psa_algorithm_t alg )
1157{
1158 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1159 if( status != PSA_SUCCESS )
1160 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001161 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001162 {
1163 *p_slot = NULL;
1164 return( PSA_ERROR_NOT_SUPPORTED );
1165 }
1166 return( PSA_SUCCESS );
1167}
1168#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1169/* With no secure element support, all keys are transparent. */
1170#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1171 psa_get_key_from_slot( handle, p_slot, usage, alg )
1172#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1173
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001174/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001175static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001176{
Steven Cooreman75b74362020-07-28 14:30:13 +02001177 /* Check whether key is already clean */
1178 if( slot->data.key.data == NULL )
1179 return PSA_SUCCESS;
1180
Gilles Peskine73167e12019-07-12 23:44:37 +02001181#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1182 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001183 {
1184 /* No key material to clean. */
1185 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001186 else
1187#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +02001188 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +00001189 {
1190 /* No key material to clean. */
1191 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001192 else if( key_type_is_raw_bytes( slot->attr.type ) ||
1193 PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1194 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001195 {
Steven Cooremana01795d2020-07-24 22:48:15 +02001196 mbedtls_free( slot->data.key.data );
1197 slot->data.key.data = NULL;
1198 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001199 }
1200 else
Darryl Green40225ba2018-11-15 14:48:15 +00001201 {
1202 /* Shouldn't happen: the key type is not any type that we
1203 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001204 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +00001205 }
1206
1207 return( PSA_SUCCESS );
1208}
1209
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001210/** Completely wipe a slot in memory, including its policy.
1211 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001212psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001213{
1214 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001215 /* Multipart operations may still be using the key. This is safe
1216 * because all multipart operation objects are independent from
1217 * the key slot: if they need to access the key after the setup
1218 * phase, they have a copy of the key. Note that this means that
1219 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001220 /* At this point, key material and other type-specific content has
1221 * been wiped. Clear remaining metadata. We can call memset and not
1222 * zeroize because the metadata is not particularly sensitive. */
1223 memset( slot, 0, sizeof( *slot ) );
1224 return( status );
1225}
1226
Gilles Peskinec5487a82018-12-03 18:08:14 +01001227psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001228{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001229 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001230 psa_status_t status; /* status of the last operation */
1231 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001232#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1233 psa_se_drv_table_entry_t *driver;
1234#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001235
Gilles Peskine1841cf42019-10-08 15:48:25 +02001236 if( handle == 0 )
1237 return( PSA_SUCCESS );
1238
Gilles Peskinec5487a82018-12-03 18:08:14 +01001239 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001240 if( status != PSA_SUCCESS )
1241 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001242
1243#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001244 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001245 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001246 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001247 /* For a key in a secure element, we need to do three things:
1248 * remove the key file in internal storage, destroy the
1249 * key inside the secure element, and update the driver's
1250 * persistent data. Start a transaction that will encompass these
1251 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001252 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001253 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001254 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001255 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001256 status = psa_crypto_save_transaction( );
1257 if( status != PSA_SUCCESS )
1258 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001259 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001260 /* We should still try to destroy the key in the secure
1261 * element and the key metadata in storage. This is especially
1262 * important if the error is that the storage is full.
1263 * But how to do it exactly without risking an inconsistent
1264 * state after a reset?
1265 * https://github.com/ARMmbed/mbed-crypto/issues/215
1266 */
1267 overall_status = status;
1268 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001269 }
1270
Gilles Peskine354f7672019-07-12 23:46:38 +02001271 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001272 if( overall_status == PSA_SUCCESS )
1273 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001274 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001275#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1276
Darryl Greend49a4992018-06-18 17:27:26 +01001277#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001278 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001279 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001280 status = psa_destroy_persistent_key( slot->attr.id );
1281 if( overall_status == PSA_SUCCESS )
1282 overall_status = status;
1283
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001284 /* TODO: other slots may have a copy of the same key. We should
1285 * invalidate them.
1286 * https://github.com/ARMmbed/mbed-crypto/issues/214
1287 */
Darryl Greend49a4992018-06-18 17:27:26 +01001288 }
1289#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001290
Gilles Peskinefc762652019-07-22 19:30:34 +02001291#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1292 if( driver != NULL )
1293 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001294 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001295 if( overall_status == PSA_SUCCESS )
1296 overall_status = status;
1297 status = psa_crypto_stop_transaction( );
1298 if( overall_status == PSA_SUCCESS )
1299 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001300 }
1301#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1302
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001303#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1304exit:
1305#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001306 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001307 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1308 if( overall_status == PSA_SUCCESS )
1309 overall_status = status;
1310 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001311}
1312
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001313void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001314{
Gilles Peskineb699f072019-04-26 16:06:02 +02001315 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001316 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001317}
1318
Gilles Peskineb699f072019-04-26 16:06:02 +02001319psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1320 psa_key_type_t type,
1321 const uint8_t *data,
1322 size_t data_length )
1323{
1324 uint8_t *copy = NULL;
1325
1326 if( data_length != 0 )
1327 {
1328 copy = mbedtls_calloc( 1, data_length );
1329 if( copy == NULL )
1330 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1331 memcpy( copy, data, data_length );
1332 }
1333 /* After this point, this function is guaranteed to succeed, so it
1334 * can start modifying `*attributes`. */
1335
1336 if( attributes->domain_parameters != NULL )
1337 {
1338 mbedtls_free( attributes->domain_parameters );
1339 attributes->domain_parameters = NULL;
1340 attributes->domain_parameters_size = 0;
1341 }
1342
1343 attributes->domain_parameters = copy;
1344 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001345 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001346 return( PSA_SUCCESS );
1347}
1348
1349psa_status_t psa_get_key_domain_parameters(
1350 const psa_key_attributes_t *attributes,
1351 uint8_t *data, size_t data_size, size_t *data_length )
1352{
1353 if( attributes->domain_parameters_size > data_size )
1354 return( PSA_ERROR_BUFFER_TOO_SMALL );
1355 *data_length = attributes->domain_parameters_size;
1356 if( attributes->domain_parameters_size != 0 )
1357 memcpy( data, attributes->domain_parameters,
1358 attributes->domain_parameters_size );
1359 return( PSA_SUCCESS );
1360}
1361
1362#if defined(MBEDTLS_RSA_C)
1363static psa_status_t psa_get_rsa_public_exponent(
1364 const mbedtls_rsa_context *rsa,
1365 psa_key_attributes_t *attributes )
1366{
1367 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001368 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001369 uint8_t *buffer = NULL;
1370 size_t buflen;
1371 mbedtls_mpi_init( &mpi );
1372
1373 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1374 if( ret != 0 )
1375 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001376 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1377 {
1378 /* It's the default value, which is reported as an empty string,
1379 * so there's nothing to do. */
1380 goto exit;
1381 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001382
1383 buflen = mbedtls_mpi_size( &mpi );
1384 buffer = mbedtls_calloc( 1, buflen );
1385 if( buffer == NULL )
1386 {
1387 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1388 goto exit;
1389 }
1390 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1391 if( ret != 0 )
1392 goto exit;
1393 attributes->domain_parameters = buffer;
1394 attributes->domain_parameters_size = buflen;
1395
1396exit:
1397 mbedtls_mpi_free( &mpi );
1398 if( ret != 0 )
1399 mbedtls_free( buffer );
1400 return( mbedtls_to_psa_error( ret ) );
1401}
1402#endif /* MBEDTLS_RSA_C */
1403
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001404/** Retrieve all the publicly-accessible attributes of a key.
1405 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001406psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1407 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001408{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001409 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001410 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001411
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001412 psa_reset_key_attributes( attributes );
1413
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001414 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001415 if( status != PSA_SUCCESS )
1416 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001417
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001418 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001419 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1420 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1421
1422#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1423 if( psa_key_slot_is_external( slot ) )
1424 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1425#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001426
Gilles Peskine8e338702019-07-30 20:06:31 +02001427 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001428 {
1429#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001430 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001431 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001432#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001433 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001434 * is not yet implemented.
1435 * https://github.com/ARMmbed/mbed-crypto/issues/216
1436 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001437 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001438 break;
1439#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001440 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001441 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001442
1443 status = psa_load_rsa_representation( slot, &rsa );
1444 if( status != PSA_SUCCESS )
1445 break;
1446
Steven Cooremana2371e52020-07-28 14:30:39 +02001447 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001448 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001449 mbedtls_rsa_free( rsa );
1450 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001451 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001452 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001453#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001454 default:
1455 /* Nothing else to do. */
1456 break;
1457 }
1458
1459 if( status != PSA_SUCCESS )
1460 psa_reset_key_attributes( attributes );
1461 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001462}
1463
Gilles Peskinec8000c02019-08-02 20:15:51 +02001464#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1465psa_status_t psa_get_key_slot_number(
1466 const psa_key_attributes_t *attributes,
1467 psa_key_slot_number_t *slot_number )
1468{
1469 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1470 {
1471 *slot_number = attributes->slot_number;
1472 return( PSA_SUCCESS );
1473 }
1474 else
1475 return( PSA_ERROR_INVALID_ARGUMENT );
1476}
1477#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1478
Steven Cooremana01795d2020-07-24 22:48:15 +02001479static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1480 uint8_t *data,
1481 size_t data_size,
1482 size_t *data_length )
1483{
1484 if( slot->data.key.bytes > data_size )
1485 return( PSA_ERROR_BUFFER_TOO_SMALL );
1486 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1487 memset( data + slot->data.key.bytes, 0,
1488 data_size - slot->data.key.bytes );
1489 *data_length = slot->data.key.bytes;
1490 return( PSA_SUCCESS );
1491}
1492
Gilles Peskinef603c712019-01-19 13:40:11 +01001493static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1494 uint8_t *data,
1495 size_t data_size,
1496 size_t *data_length,
1497 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001498{
Gilles Peskine5d309672019-07-12 23:47:28 +02001499#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1500 const psa_drv_se_t *drv;
1501 psa_drv_se_context_t *drv_context;
1502#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1503
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001504 *data_length = 0;
1505
Gilles Peskine8e338702019-07-30 20:06:31 +02001506 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001507 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001508
Gilles Peskinef9168942019-09-12 19:20:29 +02001509 /* Reject a zero-length output buffer now, since this can never be a
1510 * valid key representation. This way we know that data must be a valid
1511 * pointer and we can do things like memset(data, ..., data_size). */
1512 if( data_size == 0 )
1513 return( PSA_ERROR_BUFFER_TOO_SMALL );
1514
Gilles Peskine5d309672019-07-12 23:47:28 +02001515#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001516 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001517 {
1518 psa_drv_se_export_key_t method;
1519 if( drv->key_management == NULL )
1520 return( PSA_ERROR_NOT_SUPPORTED );
1521 method = ( export_public_key ?
1522 drv->key_management->p_export_public :
1523 drv->key_management->p_export );
1524 if( method == NULL )
1525 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001526 return( method( drv_context,
1527 slot->data.se.slot_number,
1528 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001529 }
1530#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1531
Gilles Peskine8e338702019-07-30 20:06:31 +02001532 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001533 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001534 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001535 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001536 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1537 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001538 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001539 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001540 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001541 /* Exporting public -> public */
1542 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1543 }
1544 else if( !export_public_key )
1545 {
1546 /* Exporting private -> private */
1547 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1548 }
1549 /* Need to export the public part of a private key,
1550 * so conversion is needed */
1551 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1552 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001553#if defined(MBEDTLS_RSA_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001554 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001555 psa_status_t status = psa_load_rsa_representation( slot, &rsa );
1556 if( status != PSA_SUCCESS )
1557 return status;
Steven Cooremana01795d2020-07-24 22:48:15 +02001558
Steven Cooreman560c28a2020-07-24 23:20:24 +02001559 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001560 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001561 data,
1562 data_size,
1563 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001564
Steven Cooremana2371e52020-07-28 14:30:39 +02001565 mbedtls_rsa_free( rsa );
1566 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001567
Steven Cooreman560c28a2020-07-24 23:20:24 +02001568 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001569#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001570 /* We don't know how to convert a private RSA key to public. */
1571 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001572#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001573 }
1574 else
Moran Pekera998bc62018-04-16 18:16:20 +03001575 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001576#if defined(MBEDTLS_ECP_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001577 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001578 psa_status_t status = psa_load_ecp_representation( slot, &ecp );
1579 if( status != PSA_SUCCESS )
1580 return status;
1581
1582 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1583 PSA_KEY_TYPE_ECC_GET_FAMILY(
1584 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001585 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001586 data,
1587 data_size,
1588 data_length );
1589
Steven Cooremana2371e52020-07-28 14:30:39 +02001590 mbedtls_ecp_keypair_free( ecp );
1591 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001592 return( status );
1593#else
1594 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001595 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001596#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001597 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001598 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001599 else
1600 {
1601 /* This shouldn't happen in the reference implementation, but
1602 it is valid for a special-purpose implementation to omit
1603 support for exporting certain key types. */
1604 return( PSA_ERROR_NOT_SUPPORTED );
1605 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001606}
1607
Gilles Peskinec5487a82018-12-03 18:08:14 +01001608psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001609 uint8_t *data,
1610 size_t data_size,
1611 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001612{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001613 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001614 psa_status_t status;
1615
1616 /* Set the key to empty now, so that even when there are errors, we always
1617 * set data_length to a value between 0 and data_size. On error, setting
1618 * the key to empty is a good choice because an empty key representation is
1619 * unlikely to be accepted anywhere. */
1620 *data_length = 0;
1621
1622 /* Export requires the EXPORT flag. There is an exception for public keys,
1623 * which don't require any flag, but psa_get_key_from_slot takes
1624 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001625 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001626 if( status != PSA_SUCCESS )
1627 return( status );
1628 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001629 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001630}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001631
Gilles Peskinec5487a82018-12-03 18:08:14 +01001632psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001633 uint8_t *data,
1634 size_t data_size,
1635 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001636{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001637 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001638 psa_status_t status;
1639
1640 /* Set the key to empty now, so that even when there are errors, we always
1641 * set data_length to a value between 0 and data_size. On error, setting
1642 * the key to empty is a good choice because an empty key representation is
1643 * unlikely to be accepted anywhere. */
1644 *data_length = 0;
1645
1646 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001647 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001648 if( status != PSA_SUCCESS )
1649 return( status );
1650 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001651 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001652}
1653
Gilles Peskine91e8c332019-08-02 19:19:39 +02001654#if defined(static_assert)
1655static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1656 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001657static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001658 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001659static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001660 "One or more key attribute flag is listed as both internal-only and external-only" );
1661#endif
1662
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001663/** Validate that a key policy is internally well-formed.
1664 *
1665 * This function only rejects invalid policies. It does not validate the
1666 * consistency of the policy with respect to other attributes of the key
1667 * such as the key type.
1668 */
1669static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001670{
1671 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001672 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001673 PSA_KEY_USAGE_ENCRYPT |
1674 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001675 PSA_KEY_USAGE_SIGN_HASH |
1676 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001677 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1678 return( PSA_ERROR_INVALID_ARGUMENT );
1679
Gilles Peskine4747d192019-04-17 15:05:45 +02001680 return( PSA_SUCCESS );
1681}
1682
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001683/** Validate the internal consistency of key attributes.
1684 *
1685 * This function only rejects invalid attribute values. If does not
1686 * validate the consistency of the attributes with any key data that may
1687 * be involved in the creation of the key.
1688 *
1689 * Call this function early in the key creation process.
1690 *
1691 * \param[in] attributes Key attributes for the new key.
1692 * \param[out] p_drv On any return, the driver for the key, if any.
1693 * NULL for a transparent key.
1694 *
1695 */
1696static psa_status_t psa_validate_key_attributes(
1697 const psa_key_attributes_t *attributes,
1698 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001699{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001700 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001701
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001702 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1703 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001704 if( status != PSA_SUCCESS )
1705 return( status );
1706
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001707 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1708 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001709 if( status != PSA_SUCCESS )
1710 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001711
1712 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001713 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001714 return( status );
1715
1716 /* Refuse to create overly large keys.
1717 * Note that this doesn't trigger on import if the attributes don't
1718 * explicitly specify a size (so psa_get_key_bits returns 0), so
1719 * psa_import_key() needs its own checks. */
1720 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1721 return( PSA_ERROR_NOT_SUPPORTED );
1722
Gilles Peskine91e8c332019-08-02 19:19:39 +02001723 /* Reject invalid flags. These should not be reachable through the API. */
1724 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1725 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1726 return( PSA_ERROR_INVALID_ARGUMENT );
1727
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001728 return( PSA_SUCCESS );
1729}
1730
Gilles Peskine4747d192019-04-17 15:05:45 +02001731/** Prepare a key slot to receive key material.
1732 *
1733 * This function allocates a key slot and sets its metadata.
1734 *
1735 * If this function fails, call psa_fail_key_creation().
1736 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001737 * This function is intended to be used as follows:
1738 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1739 * it with the specified attributes, and assign it a handle.
1740 * -# Populate the slot with the key material.
1741 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1742 * In case of failure at any step, stop the sequence and call
1743 * psa_fail_key_creation().
1744 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001745 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001746 * \param[in] attributes Key attributes for the new key.
1747 * \param[out] handle On success, a handle for the allocated slot.
1748 * \param[out] p_slot On success, a pointer to the prepared slot.
1749 * \param[out] p_drv On any return, the driver for the key, if any.
1750 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001751 *
1752 * \retval #PSA_SUCCESS
1753 * The key slot is ready to receive key material.
1754 * \return If this function fails, the key slot is an invalid state.
1755 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001756 */
1757static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001758 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001759 const psa_key_attributes_t *attributes,
1760 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001761 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001762 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001763{
1764 psa_status_t status;
1765 psa_key_slot_t *slot;
1766
Gilles Peskinedf179142019-07-15 22:02:14 +02001767 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001768 *p_drv = NULL;
1769
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001770 status = psa_validate_key_attributes( attributes, p_drv );
1771 if( status != PSA_SUCCESS )
1772 return( status );
1773
Gilles Peskineedbed562019-08-07 18:19:59 +02001774 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001775 if( status != PSA_SUCCESS )
1776 return( status );
1777 slot = *p_slot;
1778
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001779 /* We're storing the declared bit-size of the key. It's up to each
1780 * creation mechanism to verify that this information is correct.
1781 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001782 * an input (generate, device) but not for those where the bit-size
1783 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001784
1785 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001786
Gilles Peskine91e8c332019-08-02 19:19:39 +02001787 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001788 * external-only flags, query `attributes`. Thanks to the check
1789 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001790 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001791 * may have set. */
1792 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001793
Gilles Peskinecbaff462019-07-12 23:46:04 +02001794#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001795 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001796 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001797 * create the key file in internal storage, create the
1798 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001799 * persistent data. This is done by starting a transaction that will
1800 * encompass these three actions.
1801 * For registering a volatile key, we just need to find an appropriate
1802 * slot number inside the SE. Since the key is designated volatile, creating
1803 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001804 /* The first thing to do is to find a slot number for the new key.
1805 * We save the slot number in persistent storage as part of the
1806 * transaction data. It will be needed to recover if the power
1807 * fails during the key creation process, to clean up on the secure
1808 * element side after restarting. Obtaining a slot number from the
1809 * secure element driver updates its persistent state, but we do not yet
1810 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001811 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001812 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001813 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001814 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001815 &slot->data.se.slot_number );
1816 if( status != PSA_SUCCESS )
1817 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001818
1819 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001820 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001821 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1822 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1823 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1824 psa_crypto_transaction.key.id = slot->attr.id;
1825 status = psa_crypto_save_transaction( );
1826 if( status != PSA_SUCCESS )
1827 {
1828 (void) psa_crypto_stop_transaction( );
1829 return( status );
1830 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001831 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001832 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001833
1834 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1835 {
1836 /* Key registration only makes sense with a secure element. */
1837 return( PSA_ERROR_INVALID_ARGUMENT );
1838 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001839#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1840
Darryl Green0c6575a2018-11-07 16:05:30 +00001841 return( status );
1842}
Gilles Peskine4747d192019-04-17 15:05:45 +02001843
1844/** Finalize the creation of a key once its key material has been set.
1845 *
1846 * This entails writing the key to persistent storage.
1847 *
1848 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001849 * See the documentation of psa_start_key_creation() for the intended use
1850 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001851 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001852 * \param[in,out] slot Pointer to the slot with key material.
1853 * \param[in] driver The secure element driver for the key,
1854 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001855 *
1856 * \retval #PSA_SUCCESS
1857 * The key was successfully created. The handle is now valid.
1858 * \return If this function fails, the key slot is an invalid state.
1859 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001860 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001861static psa_status_t psa_finish_key_creation(
1862 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001863 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001864{
1865 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001866 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001867 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001868
1869#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001870 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001871 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001872#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1873 if( driver != NULL )
1874 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001875 psa_se_key_data_storage_t data;
1876#if defined(static_assert)
1877 static_assert( sizeof( slot->data.se.slot_number ) ==
1878 sizeof( data.slot_number ),
1879 "Slot number size does not match psa_se_key_data_storage_t" );
1880 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1881 "Bit-size size does not match psa_se_key_data_storage_t" );
1882#endif
1883 memcpy( &data.slot_number, &slot->data.se.slot_number,
1884 sizeof( slot->data.se.slot_number ) );
1885 memcpy( &data.bits, &slot->attr.bits,
1886 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001887 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001888 (uint8_t*) &data,
1889 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001890 }
1891 else
1892#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1893 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001894 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001895 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001896 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001897 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1898 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001899 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001900 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1901 status = psa_internal_export_key( slot,
1902 buffer, buffer_size, &length,
1903 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001904 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001905 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001906 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001907
Gilles Peskinef9168942019-09-12 19:20:29 +02001908 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001909 mbedtls_free( buffer );
1910 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001911 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001912#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1913
Gilles Peskinecbaff462019-07-12 23:46:04 +02001914#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001915 /* Finish the transaction for a key creation. This does not
1916 * happen when registering an existing key. Detect this case
1917 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001918 * creation of a persistent key in a secure element requires a transaction,
1919 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001920 if( driver != NULL &&
1921 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001922 {
1923 status = psa_save_se_persistent_data( driver );
1924 if( status != PSA_SUCCESS )
1925 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001926 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001927 return( status );
1928 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001929 status = psa_crypto_stop_transaction( );
1930 if( status != PSA_SUCCESS )
1931 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001932 }
1933#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1934
Gilles Peskine4747d192019-04-17 15:05:45 +02001935 return( status );
1936}
1937
1938/** Abort the creation of a key.
1939 *
1940 * You may call this function after calling psa_start_key_creation(),
1941 * or after psa_finish_key_creation() fails. In other circumstances, this
1942 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001943 * See the documentation of psa_start_key_creation() for the intended use
1944 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001945 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001946 * \param[in,out] slot Pointer to the slot with key material.
1947 * \param[in] driver The secure element driver for the key,
1948 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001949 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001950static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001951 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001952{
Gilles Peskine011e4282019-06-26 18:34:38 +02001953 (void) driver;
1954
Gilles Peskine4747d192019-04-17 15:05:45 +02001955 if( slot == NULL )
1956 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001957
1958#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001959 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001960 * element, and the failure happened later (when saving metadata
1961 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001962 * element.
1963 * https://github.com/ARMmbed/mbed-crypto/issues/217
1964 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001965
Gilles Peskined7729582019-08-05 15:55:54 +02001966 /* Abort the ongoing transaction if any (there may not be one if
1967 * the creation process failed before starting one, or if the
1968 * key creation is a registration of a key in a secure element).
1969 * Earlier functions must already have done what it takes to undo any
1970 * partial creation. All that's left is to update the transaction data
1971 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001972 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001973#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1974
Gilles Peskine4747d192019-04-17 15:05:45 +02001975 psa_wipe_key_slot( slot );
1976}
1977
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001978/** Validate optional attributes during key creation.
1979 *
1980 * Some key attributes are optional during key creation. If they are
1981 * specified in the attributes structure, check that they are consistent
1982 * with the data in the slot.
1983 *
1984 * This function should be called near the end of key creation, after
1985 * the slot in memory is fully populated but before saving persistent data.
1986 */
1987static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001988 const psa_key_slot_t *slot,
1989 const psa_key_attributes_t *attributes )
1990{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001991 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001992 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001993 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001994 return( PSA_ERROR_INVALID_ARGUMENT );
1995 }
1996
1997 if( attributes->domain_parameters_size != 0 )
1998 {
1999#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02002000 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002001 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002002 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002003 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002004 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002005
2006 psa_status_t status = psa_load_rsa_representation( slot, &rsa );
2007 if( status != PSA_SUCCESS )
2008 return status;
Steven Cooreman75b74362020-07-28 14:30:13 +02002009
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002010 mbedtls_mpi_init( &actual );
2011 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002012 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002013 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002014 mbedtls_rsa_free( rsa );
2015 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002016 if( ret != 0 )
2017 goto rsa_exit;
2018 ret = mbedtls_mpi_read_binary( &required,
2019 attributes->domain_parameters,
2020 attributes->domain_parameters_size );
2021 if( ret != 0 )
2022 goto rsa_exit;
2023 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2024 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2025 rsa_exit:
2026 mbedtls_mpi_free( &actual );
2027 mbedtls_mpi_free( &required );
2028 if( ret != 0)
2029 return( mbedtls_to_psa_error( ret ) );
2030 }
2031 else
2032#endif
2033 {
2034 return( PSA_ERROR_INVALID_ARGUMENT );
2035 }
2036 }
2037
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002038 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002039 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002040 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002041 return( PSA_ERROR_INVALID_ARGUMENT );
2042 }
2043
2044 return( PSA_SUCCESS );
2045}
2046
Gilles Peskine4747d192019-04-17 15:05:45 +02002047psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002048 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002049 size_t data_length,
2050 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002051{
2052 psa_status_t status;
2053 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002054 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002055
Gilles Peskine0f84d622019-09-12 19:03:13 +02002056 /* Reject zero-length symmetric keys (including raw data key objects).
2057 * This also rejects any key which might be encoded as an empty string,
2058 * which is never valid. */
2059 if( data_length == 0 )
2060 return( PSA_ERROR_INVALID_ARGUMENT );
2061
Gilles Peskinedf179142019-07-15 22:02:14 +02002062 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2063 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002064 if( status != PSA_SUCCESS )
2065 goto exit;
2066
Gilles Peskine5d309672019-07-12 23:47:28 +02002067#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2068 if( driver != NULL )
2069 {
2070 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002071 /* The driver should set the number of key bits, however in
2072 * case it doesn't, we initialize bits to an invalid value. */
2073 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002074 if( drv->key_management == NULL ||
2075 drv->key_management->p_import == NULL )
2076 {
2077 status = PSA_ERROR_NOT_SUPPORTED;
2078 goto exit;
2079 }
2080 status = drv->key_management->p_import(
2081 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002082 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002083 &bits );
2084 if( status != PSA_SUCCESS )
2085 goto exit;
2086 if( bits > PSA_MAX_KEY_BITS )
2087 {
2088 status = PSA_ERROR_NOT_SUPPORTED;
2089 goto exit;
2090 }
2091 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002092 }
2093 else
2094#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2095 {
2096 status = psa_import_key_into_slot( slot, data, data_length );
2097 if( status != PSA_SUCCESS )
2098 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002099 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002100 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002101 if( status != PSA_SUCCESS )
2102 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002103
Gilles Peskine011e4282019-06-26 18:34:38 +02002104 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002105exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002106 if( status != PSA_SUCCESS )
2107 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002108 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002109 *handle = 0;
2110 }
2111 return( status );
2112}
2113
Gilles Peskined7729582019-08-05 15:55:54 +02002114#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2115psa_status_t mbedtls_psa_register_se_key(
2116 const psa_key_attributes_t *attributes )
2117{
2118 psa_status_t status;
2119 psa_key_slot_t *slot = NULL;
2120 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002121 psa_key_handle_t handle = 0;
2122
2123 /* Leaving attributes unspecified is not currently supported.
2124 * It could make sense to query the key type and size from the
2125 * secure element, but not all secure elements support this
2126 * and the driver HAL doesn't currently support it. */
2127 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2128 return( PSA_ERROR_NOT_SUPPORTED );
2129 if( psa_get_key_bits( attributes ) == 0 )
2130 return( PSA_ERROR_NOT_SUPPORTED );
2131
2132 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2133 &handle, &slot, &driver );
2134 if( status != PSA_SUCCESS )
2135 goto exit;
2136
Gilles Peskined7729582019-08-05 15:55:54 +02002137 status = psa_finish_key_creation( slot, driver );
2138
2139exit:
2140 if( status != PSA_SUCCESS )
2141 {
2142 psa_fail_key_creation( slot, driver );
2143 }
2144 /* Registration doesn't keep the key in RAM. */
2145 psa_close_key( handle );
2146 return( status );
2147}
2148#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2149
Gilles Peskinef603c712019-01-19 13:40:11 +01002150static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002151 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002152{
2153 psa_status_t status;
2154 uint8_t *buffer = NULL;
2155 size_t buffer_size = 0;
2156 size_t length;
2157
Gilles Peskine8e338702019-07-30 20:06:31 +02002158 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002159 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01002160 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02002161 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01002162 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01002163 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
2164 if( status != PSA_SUCCESS )
2165 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02002166 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002167 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01002168
2169exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02002170 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01002171 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01002172 return( status );
2173}
2174
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002175psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2176 const psa_key_attributes_t *specified_attributes,
2177 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002178{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002179 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002180 psa_key_slot_t *source_slot = NULL;
2181 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002182 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002183 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002184
Gilles Peskine28f8f302019-07-24 13:30:31 +02002185 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002186 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002187 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002188 goto exit;
2189
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002190 status = psa_validate_optional_attributes( source_slot,
2191 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002192 if( status != PSA_SUCCESS )
2193 goto exit;
2194
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002195 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002196 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002197 if( status != PSA_SUCCESS )
2198 goto exit;
2199
Gilles Peskinedf179142019-07-15 22:02:14 +02002200 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2201 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002202 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002203 if( status != PSA_SUCCESS )
2204 goto exit;
2205
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002206#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2207 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002208 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002209 /* Copying to a secure element is not implemented yet. */
2210 status = PSA_ERROR_NOT_SUPPORTED;
2211 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002212 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002213#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002214
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002215 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002216 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002217 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002218
Gilles Peskine011e4282019-06-26 18:34:38 +02002219 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002220exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002221 if( status != PSA_SUCCESS )
2222 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002223 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002224 *target_handle = 0;
2225 }
2226 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002227}
2228
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002229
2230
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002231/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002232/* Message digests */
2233/****************************************************************/
2234
Gilles Peskineb16841e2019-10-10 20:36:12 +02002235#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002236static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002237{
2238 switch( alg )
2239 {
2240#if defined(MBEDTLS_MD2_C)
2241 case PSA_ALG_MD2:
2242 return( &mbedtls_md2_info );
2243#endif
2244#if defined(MBEDTLS_MD4_C)
2245 case PSA_ALG_MD4:
2246 return( &mbedtls_md4_info );
2247#endif
2248#if defined(MBEDTLS_MD5_C)
2249 case PSA_ALG_MD5:
2250 return( &mbedtls_md5_info );
2251#endif
2252#if defined(MBEDTLS_RIPEMD160_C)
2253 case PSA_ALG_RIPEMD160:
2254 return( &mbedtls_ripemd160_info );
2255#endif
2256#if defined(MBEDTLS_SHA1_C)
2257 case PSA_ALG_SHA_1:
2258 return( &mbedtls_sha1_info );
2259#endif
2260#if defined(MBEDTLS_SHA256_C)
2261 case PSA_ALG_SHA_224:
2262 return( &mbedtls_sha224_info );
2263 case PSA_ALG_SHA_256:
2264 return( &mbedtls_sha256_info );
2265#endif
2266#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002267#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002268 case PSA_ALG_SHA_384:
2269 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002270#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002271 case PSA_ALG_SHA_512:
2272 return( &mbedtls_sha512_info );
2273#endif
2274 default:
2275 return( NULL );
2276 }
2277}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002278#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002279
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002280psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2281{
2282 switch( operation->alg )
2283 {
Gilles Peskine81736312018-06-26 15:04:31 +02002284 case 0:
2285 /* The object has (apparently) been initialized but it is not
2286 * in use. It's ok to call abort on such an object, and there's
2287 * nothing to do. */
2288 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002289#if defined(MBEDTLS_MD2_C)
2290 case PSA_ALG_MD2:
2291 mbedtls_md2_free( &operation->ctx.md2 );
2292 break;
2293#endif
2294#if defined(MBEDTLS_MD4_C)
2295 case PSA_ALG_MD4:
2296 mbedtls_md4_free( &operation->ctx.md4 );
2297 break;
2298#endif
2299#if defined(MBEDTLS_MD5_C)
2300 case PSA_ALG_MD5:
2301 mbedtls_md5_free( &operation->ctx.md5 );
2302 break;
2303#endif
2304#if defined(MBEDTLS_RIPEMD160_C)
2305 case PSA_ALG_RIPEMD160:
2306 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2307 break;
2308#endif
2309#if defined(MBEDTLS_SHA1_C)
2310 case PSA_ALG_SHA_1:
2311 mbedtls_sha1_free( &operation->ctx.sha1 );
2312 break;
2313#endif
2314#if defined(MBEDTLS_SHA256_C)
2315 case PSA_ALG_SHA_224:
2316 case PSA_ALG_SHA_256:
2317 mbedtls_sha256_free( &operation->ctx.sha256 );
2318 break;
2319#endif
2320#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002321#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002322 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002323#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002324 case PSA_ALG_SHA_512:
2325 mbedtls_sha512_free( &operation->ctx.sha512 );
2326 break;
2327#endif
2328 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002329 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002330 }
2331 operation->alg = 0;
2332 return( PSA_SUCCESS );
2333}
2334
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002335psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002336 psa_algorithm_t alg )
2337{
Janos Follath24eed8d2019-11-22 13:21:35 +00002338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002339
2340 /* A context must be freshly initialized before it can be set up. */
2341 if( operation->alg != 0 )
2342 {
2343 return( PSA_ERROR_BAD_STATE );
2344 }
2345
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002346 switch( alg )
2347 {
2348#if defined(MBEDTLS_MD2_C)
2349 case PSA_ALG_MD2:
2350 mbedtls_md2_init( &operation->ctx.md2 );
2351 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2352 break;
2353#endif
2354#if defined(MBEDTLS_MD4_C)
2355 case PSA_ALG_MD4:
2356 mbedtls_md4_init( &operation->ctx.md4 );
2357 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2358 break;
2359#endif
2360#if defined(MBEDTLS_MD5_C)
2361 case PSA_ALG_MD5:
2362 mbedtls_md5_init( &operation->ctx.md5 );
2363 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2364 break;
2365#endif
2366#if defined(MBEDTLS_RIPEMD160_C)
2367 case PSA_ALG_RIPEMD160:
2368 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2369 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2370 break;
2371#endif
2372#if defined(MBEDTLS_SHA1_C)
2373 case PSA_ALG_SHA_1:
2374 mbedtls_sha1_init( &operation->ctx.sha1 );
2375 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2376 break;
2377#endif
2378#if defined(MBEDTLS_SHA256_C)
2379 case PSA_ALG_SHA_224:
2380 mbedtls_sha256_init( &operation->ctx.sha256 );
2381 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2382 break;
2383 case PSA_ALG_SHA_256:
2384 mbedtls_sha256_init( &operation->ctx.sha256 );
2385 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2386 break;
2387#endif
2388#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002389#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002390 case PSA_ALG_SHA_384:
2391 mbedtls_sha512_init( &operation->ctx.sha512 );
2392 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2393 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002394#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002395 case PSA_ALG_SHA_512:
2396 mbedtls_sha512_init( &operation->ctx.sha512 );
2397 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2398 break;
2399#endif
2400 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002401 return( PSA_ALG_IS_HASH( alg ) ?
2402 PSA_ERROR_NOT_SUPPORTED :
2403 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002404 }
2405 if( ret == 0 )
2406 operation->alg = alg;
2407 else
2408 psa_hash_abort( operation );
2409 return( mbedtls_to_psa_error( ret ) );
2410}
2411
2412psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2413 const uint8_t *input,
2414 size_t input_length )
2415{
Janos Follath24eed8d2019-11-22 13:21:35 +00002416 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002417
2418 /* Don't require hash implementations to behave correctly on a
2419 * zero-length input, which may have an invalid pointer. */
2420 if( input_length == 0 )
2421 return( PSA_SUCCESS );
2422
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002423 switch( operation->alg )
2424 {
2425#if defined(MBEDTLS_MD2_C)
2426 case PSA_ALG_MD2:
2427 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2428 input, input_length );
2429 break;
2430#endif
2431#if defined(MBEDTLS_MD4_C)
2432 case PSA_ALG_MD4:
2433 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2434 input, input_length );
2435 break;
2436#endif
2437#if defined(MBEDTLS_MD5_C)
2438 case PSA_ALG_MD5:
2439 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2440 input, input_length );
2441 break;
2442#endif
2443#if defined(MBEDTLS_RIPEMD160_C)
2444 case PSA_ALG_RIPEMD160:
2445 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2446 input, input_length );
2447 break;
2448#endif
2449#if defined(MBEDTLS_SHA1_C)
2450 case PSA_ALG_SHA_1:
2451 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2452 input, input_length );
2453 break;
2454#endif
2455#if defined(MBEDTLS_SHA256_C)
2456 case PSA_ALG_SHA_224:
2457 case PSA_ALG_SHA_256:
2458 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2459 input, input_length );
2460 break;
2461#endif
2462#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002463#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002464 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002465#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002466 case PSA_ALG_SHA_512:
2467 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2468 input, input_length );
2469 break;
2470#endif
2471 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002472 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002473 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002474
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002475 if( ret != 0 )
2476 psa_hash_abort( operation );
2477 return( mbedtls_to_psa_error( ret ) );
2478}
2479
2480psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2481 uint8_t *hash,
2482 size_t hash_size,
2483 size_t *hash_length )
2484{
itayzafrir40835d42018-08-02 13:14:17 +03002485 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002486 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002487 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002488
2489 /* Fill the output buffer with something that isn't a valid hash
2490 * (barring an attack on the hash and deliberately-crafted input),
2491 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002492 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002493 /* If hash_size is 0 then hash may be NULL and then the
2494 * call to memset would have undefined behavior. */
2495 if( hash_size != 0 )
2496 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002497
2498 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002499 {
2500 status = PSA_ERROR_BUFFER_TOO_SMALL;
2501 goto exit;
2502 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002503
2504 switch( operation->alg )
2505 {
2506#if defined(MBEDTLS_MD2_C)
2507 case PSA_ALG_MD2:
2508 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2509 break;
2510#endif
2511#if defined(MBEDTLS_MD4_C)
2512 case PSA_ALG_MD4:
2513 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2514 break;
2515#endif
2516#if defined(MBEDTLS_MD5_C)
2517 case PSA_ALG_MD5:
2518 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2519 break;
2520#endif
2521#if defined(MBEDTLS_RIPEMD160_C)
2522 case PSA_ALG_RIPEMD160:
2523 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2524 break;
2525#endif
2526#if defined(MBEDTLS_SHA1_C)
2527 case PSA_ALG_SHA_1:
2528 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2529 break;
2530#endif
2531#if defined(MBEDTLS_SHA256_C)
2532 case PSA_ALG_SHA_224:
2533 case PSA_ALG_SHA_256:
2534 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2535 break;
2536#endif
2537#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002538#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002539 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002540#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002541 case PSA_ALG_SHA_512:
2542 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2543 break;
2544#endif
2545 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002546 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002547 }
itayzafrir40835d42018-08-02 13:14:17 +03002548 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002549
itayzafrir40835d42018-08-02 13:14:17 +03002550exit:
2551 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002552 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002553 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002554 return( psa_hash_abort( operation ) );
2555 }
2556 else
2557 {
2558 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002559 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002560 }
2561}
2562
Gilles Peskine2d277862018-06-18 15:41:12 +02002563psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2564 const uint8_t *hash,
2565 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002566{
2567 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2568 size_t actual_hash_length;
2569 psa_status_t status = psa_hash_finish( operation,
2570 actual_hash, sizeof( actual_hash ),
2571 &actual_hash_length );
2572 if( status != PSA_SUCCESS )
2573 return( status );
2574 if( actual_hash_length != hash_length )
2575 return( PSA_ERROR_INVALID_SIGNATURE );
2576 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2577 return( PSA_ERROR_INVALID_SIGNATURE );
2578 return( PSA_SUCCESS );
2579}
2580
Gilles Peskine0a749c82019-11-28 19:33:58 +01002581psa_status_t psa_hash_compute( psa_algorithm_t alg,
2582 const uint8_t *input, size_t input_length,
2583 uint8_t *hash, size_t hash_size,
2584 size_t *hash_length )
2585{
2586 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2587 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2588
2589 *hash_length = hash_size;
2590 status = psa_hash_setup( &operation, alg );
2591 if( status != PSA_SUCCESS )
2592 goto exit;
2593 status = psa_hash_update( &operation, input, input_length );
2594 if( status != PSA_SUCCESS )
2595 goto exit;
2596 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2597 if( status != PSA_SUCCESS )
2598 goto exit;
2599
2600exit:
2601 if( status == PSA_SUCCESS )
2602 status = psa_hash_abort( &operation );
2603 else
2604 psa_hash_abort( &operation );
2605 return( status );
2606}
2607
2608psa_status_t psa_hash_compare( psa_algorithm_t alg,
2609 const uint8_t *input, size_t input_length,
2610 const uint8_t *hash, size_t hash_length )
2611{
2612 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2613 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2614
2615 status = psa_hash_setup( &operation, alg );
2616 if( status != PSA_SUCCESS )
2617 goto exit;
2618 status = psa_hash_update( &operation, input, input_length );
2619 if( status != PSA_SUCCESS )
2620 goto exit;
2621 status = psa_hash_verify( &operation, hash, hash_length );
2622 if( status != PSA_SUCCESS )
2623 goto exit;
2624
2625exit:
2626 if( status == PSA_SUCCESS )
2627 status = psa_hash_abort( &operation );
2628 else
2629 psa_hash_abort( &operation );
2630 return( status );
2631}
2632
Gilles Peskineeb35d782019-01-22 17:56:16 +01002633psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2634 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002635{
2636 if( target_operation->alg != 0 )
2637 return( PSA_ERROR_BAD_STATE );
2638
2639 switch( source_operation->alg )
2640 {
2641 case 0:
2642 return( PSA_ERROR_BAD_STATE );
2643#if defined(MBEDTLS_MD2_C)
2644 case PSA_ALG_MD2:
2645 mbedtls_md2_clone( &target_operation->ctx.md2,
2646 &source_operation->ctx.md2 );
2647 break;
2648#endif
2649#if defined(MBEDTLS_MD4_C)
2650 case PSA_ALG_MD4:
2651 mbedtls_md4_clone( &target_operation->ctx.md4,
2652 &source_operation->ctx.md4 );
2653 break;
2654#endif
2655#if defined(MBEDTLS_MD5_C)
2656 case PSA_ALG_MD5:
2657 mbedtls_md5_clone( &target_operation->ctx.md5,
2658 &source_operation->ctx.md5 );
2659 break;
2660#endif
2661#if defined(MBEDTLS_RIPEMD160_C)
2662 case PSA_ALG_RIPEMD160:
2663 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2664 &source_operation->ctx.ripemd160 );
2665 break;
2666#endif
2667#if defined(MBEDTLS_SHA1_C)
2668 case PSA_ALG_SHA_1:
2669 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2670 &source_operation->ctx.sha1 );
2671 break;
2672#endif
2673#if defined(MBEDTLS_SHA256_C)
2674 case PSA_ALG_SHA_224:
2675 case PSA_ALG_SHA_256:
2676 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2677 &source_operation->ctx.sha256 );
2678 break;
2679#endif
2680#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002681#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002682 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002683#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002684 case PSA_ALG_SHA_512:
2685 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2686 &source_operation->ctx.sha512 );
2687 break;
2688#endif
2689 default:
2690 return( PSA_ERROR_NOT_SUPPORTED );
2691 }
2692
2693 target_operation->alg = source_operation->alg;
2694 return( PSA_SUCCESS );
2695}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002696
2697
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002698/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002699/* MAC */
2700/****************************************************************/
2701
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002702static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002703 psa_algorithm_t alg,
2704 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002705 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002706 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002707{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002708 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002709 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002710
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002711 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002712 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002713
Gilles Peskine8c9def32018-02-08 10:02:12 +01002714 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2715 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002716 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002717 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002718 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002719 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002720 mode = MBEDTLS_MODE_STREAM;
2721 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002722 case PSA_ALG_CTR:
2723 mode = MBEDTLS_MODE_CTR;
2724 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002725 case PSA_ALG_CFB:
2726 mode = MBEDTLS_MODE_CFB;
2727 break;
2728 case PSA_ALG_OFB:
2729 mode = MBEDTLS_MODE_OFB;
2730 break;
2731 case PSA_ALG_CBC_NO_PADDING:
2732 mode = MBEDTLS_MODE_CBC;
2733 break;
2734 case PSA_ALG_CBC_PKCS7:
2735 mode = MBEDTLS_MODE_CBC;
2736 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002737 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002738 mode = MBEDTLS_MODE_CCM;
2739 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002740 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002741 mode = MBEDTLS_MODE_GCM;
2742 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002743 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2744 mode = MBEDTLS_MODE_CHACHAPOLY;
2745 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002746 default:
2747 return( NULL );
2748 }
2749 }
2750 else if( alg == PSA_ALG_CMAC )
2751 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002752 else
2753 return( NULL );
2754
2755 switch( key_type )
2756 {
2757 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002758 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002759 break;
2760 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002761 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2762 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002763 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002764 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002765 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002766 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002767 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2768 * but two-key Triple-DES is functionally three-key Triple-DES
2769 * with K1=K3, so that's how we present it to mbedtls. */
2770 if( key_bits == 128 )
2771 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002772 break;
2773 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002774 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002775 break;
2776 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002777 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002778 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002779 case PSA_KEY_TYPE_CHACHA20:
2780 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2781 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002782 default:
2783 return( NULL );
2784 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002785 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002786 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002787
Jaeden Amero23bbb752018-06-26 14:16:54 +01002788 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2789 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002790}
2791
Gilles Peskinea05219c2018-11-16 16:02:56 +01002792#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002793static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002794{
Gilles Peskine2d277862018-06-18 15:41:12 +02002795 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002796 {
2797 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002798 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002799 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002800 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002801 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002802 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002803 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002804 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002805 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002806 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002807 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002808 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002809 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002810 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002811 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002812 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002813 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002814 return( 128 );
2815 default:
2816 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002817 }
2818}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002819#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002820
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002821/* Initialize the MAC operation structure. Once this function has been
2822 * called, psa_mac_abort can run and will do the right thing. */
2823static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2824 psa_algorithm_t alg )
2825{
2826 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2827
2828 operation->alg = alg;
2829 operation->key_set = 0;
2830 operation->iv_set = 0;
2831 operation->iv_required = 0;
2832 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002833 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002834
2835#if defined(MBEDTLS_CMAC_C)
2836 if( alg == PSA_ALG_CMAC )
2837 {
2838 operation->iv_required = 0;
2839 mbedtls_cipher_init( &operation->ctx.cmac );
2840 status = PSA_SUCCESS;
2841 }
2842 else
2843#endif /* MBEDTLS_CMAC_C */
2844#if defined(MBEDTLS_MD_C)
2845 if( PSA_ALG_IS_HMAC( operation->alg ) )
2846 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002847 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2848 operation->ctx.hmac.hash_ctx.alg = 0;
2849 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002850 }
2851 else
2852#endif /* MBEDTLS_MD_C */
2853 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002854 if( ! PSA_ALG_IS_MAC( alg ) )
2855 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002856 }
2857
2858 if( status != PSA_SUCCESS )
2859 memset( operation, 0, sizeof( *operation ) );
2860 return( status );
2861}
2862
Gilles Peskine01126fa2018-07-12 17:04:55 +02002863#if defined(MBEDTLS_MD_C)
2864static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2865{
Gilles Peskine3f108122018-12-07 18:14:53 +01002866 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002867 return( psa_hash_abort( &hmac->hash_ctx ) );
2868}
2869#endif /* MBEDTLS_MD_C */
2870
Gilles Peskine8c9def32018-02-08 10:02:12 +01002871psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2872{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002873 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002874 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002875 /* The object has (apparently) been initialized but it is not
2876 * in use. It's ok to call abort on such an object, and there's
2877 * nothing to do. */
2878 return( PSA_SUCCESS );
2879 }
2880 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002881#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002882 if( operation->alg == PSA_ALG_CMAC )
2883 {
2884 mbedtls_cipher_free( &operation->ctx.cmac );
2885 }
2886 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002887#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002888#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002889 if( PSA_ALG_IS_HMAC( operation->alg ) )
2890 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002891 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002892 }
2893 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002894#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002895 {
2896 /* Sanity check (shouldn't happen: operation->alg should
2897 * always have been initialized to a valid value). */
2898 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002899 }
Moran Peker41deec42018-04-04 15:43:05 +03002900
Gilles Peskine8c9def32018-02-08 10:02:12 +01002901 operation->alg = 0;
2902 operation->key_set = 0;
2903 operation->iv_set = 0;
2904 operation->iv_required = 0;
2905 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002906 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002907
Gilles Peskine8c9def32018-02-08 10:02:12 +01002908 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002909
2910bad_state:
2911 /* If abort is called on an uninitialized object, we can't trust
2912 * anything. Wipe the object in case it contains confidential data.
2913 * This may result in a memory leak if a pointer gets overwritten,
2914 * but it's too late to do anything about this. */
2915 memset( operation, 0, sizeof( *operation ) );
2916 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002917}
2918
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002919#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002920static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002921 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002922 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002923 const mbedtls_cipher_info_t *cipher_info )
2924{
Janos Follath24eed8d2019-11-22 13:21:35 +00002925 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002926
2927 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002928
2929 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2930 if( ret != 0 )
2931 return( ret );
2932
2933 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02002934 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002935 key_bits );
2936 return( ret );
2937}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002938#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002939
Gilles Peskine248051a2018-06-20 16:09:38 +02002940#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002941static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2942 const uint8_t *key,
2943 size_t key_length,
2944 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002945{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002946 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002947 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002948 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002949 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002950 psa_status_t status;
2951
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002952 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2953 * overflow below. This should never trigger if the hash algorithm
2954 * is implemented correctly. */
2955 /* The size checks against the ipad and opad buffers cannot be written
2956 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2957 * because that triggers -Wlogical-op on GCC 7.3. */
2958 if( block_size > sizeof( ipad ) )
2959 return( PSA_ERROR_NOT_SUPPORTED );
2960 if( block_size > sizeof( hmac->opad ) )
2961 return( PSA_ERROR_NOT_SUPPORTED );
2962 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002963 return( PSA_ERROR_NOT_SUPPORTED );
2964
Gilles Peskined223b522018-06-11 18:12:58 +02002965 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002966 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002967 status = psa_hash_compute( hash_alg, key, key_length,
2968 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002969 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002970 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002971 }
Gilles Peskine96889972018-07-12 17:07:03 +02002972 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2973 * but it is permitted. It is common when HMAC is used in HKDF, for
2974 * example. Don't call `memcpy` in the 0-length because `key` could be
2975 * an invalid pointer which would make the behavior undefined. */
2976 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002977 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002978
Gilles Peskined223b522018-06-11 18:12:58 +02002979 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2980 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002981 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002982 ipad[i] ^= 0x36;
2983 memset( ipad + key_length, 0x36, block_size - key_length );
2984
2985 /* Copy the key material from ipad to opad, flipping the requisite bits,
2986 * and filling the rest of opad with the requisite constant. */
2987 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002988 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2989 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002990
Gilles Peskine01126fa2018-07-12 17:04:55 +02002991 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002992 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002993 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002994
Gilles Peskine01126fa2018-07-12 17:04:55 +02002995 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002996
2997cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002998 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002999
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003000 return( status );
3001}
Gilles Peskine248051a2018-06-20 16:09:38 +02003002#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003003
Gilles Peskine89167cb2018-07-08 20:12:23 +02003004static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003005 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003006 psa_algorithm_t alg,
3007 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003008{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003009 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003010 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003011 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003012 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003013 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003014 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003015 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003016
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003017 /* A context must be freshly initialized before it can be set up. */
3018 if( operation->alg != 0 )
3019 {
3020 return( PSA_ERROR_BAD_STATE );
3021 }
3022
Gilles Peskined911eb72018-08-14 15:18:45 +02003023 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003024 if( status != PSA_SUCCESS )
3025 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003026 if( is_sign )
3027 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003028
Gilles Peskine28f8f302019-07-24 13:30:31 +02003029 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003030 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003031 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003032 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003033
Gilles Peskine8c9def32018-02-08 10:02:12 +01003034#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003035 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003036 {
3037 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003038 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003039 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003040 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003041 if( cipher_info == NULL )
3042 {
3043 status = PSA_ERROR_NOT_SUPPORTED;
3044 goto exit;
3045 }
3046 operation->mac_size = cipher_info->block_size;
3047 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3048 status = mbedtls_to_psa_error( ret );
3049 }
3050 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003051#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003052#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003053 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003054 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003055 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003056 if( hash_alg == 0 )
3057 {
3058 status = PSA_ERROR_NOT_SUPPORTED;
3059 goto exit;
3060 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003061
3062 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3063 /* Sanity check. This shouldn't fail on a valid configuration. */
3064 if( operation->mac_size == 0 ||
3065 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3066 {
3067 status = PSA_ERROR_NOT_SUPPORTED;
3068 goto exit;
3069 }
3070
Gilles Peskine8e338702019-07-30 20:06:31 +02003071 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003072 {
3073 status = PSA_ERROR_INVALID_ARGUMENT;
3074 goto exit;
3075 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003076
Gilles Peskine01126fa2018-07-12 17:04:55 +02003077 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003078 slot->data.key.data,
3079 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003080 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003081 }
3082 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003083#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003084 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003085 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003086 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003087 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003088
Gilles Peskined911eb72018-08-14 15:18:45 +02003089 if( truncated == 0 )
3090 {
3091 /* The "normal" case: untruncated algorithm. Nothing to do. */
3092 }
3093 else if( truncated < 4 )
3094 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003095 /* A very short MAC is too short for security since it can be
3096 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3097 * so we make this our minimum, even though 32 bits is still
3098 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003099 status = PSA_ERROR_NOT_SUPPORTED;
3100 }
3101 else if( truncated > operation->mac_size )
3102 {
3103 /* It's impossible to "truncate" to a larger length. */
3104 status = PSA_ERROR_INVALID_ARGUMENT;
3105 }
3106 else
3107 operation->mac_size = truncated;
3108
Gilles Peskinefbfac682018-07-08 20:51:54 +02003109exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003110 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003111 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003112 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003113 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003114 else
3115 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003116 operation->key_set = 1;
3117 }
3118 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003119}
3120
Gilles Peskine89167cb2018-07-08 20:12:23 +02003121psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003122 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003123 psa_algorithm_t alg )
3124{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003125 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003126}
3127
3128psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003129 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003130 psa_algorithm_t alg )
3131{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003132 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003133}
3134
Gilles Peskine8c9def32018-02-08 10:02:12 +01003135psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3136 const uint8_t *input,
3137 size_t input_length )
3138{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003139 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003140 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003141 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003142 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003143 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003144 operation->has_input = 1;
3145
Gilles Peskine8c9def32018-02-08 10:02:12 +01003146#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003147 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003148 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003149 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3150 input, input_length );
3151 status = mbedtls_to_psa_error( ret );
3152 }
3153 else
3154#endif /* MBEDTLS_CMAC_C */
3155#if defined(MBEDTLS_MD_C)
3156 if( PSA_ALG_IS_HMAC( operation->alg ) )
3157 {
3158 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3159 input_length );
3160 }
3161 else
3162#endif /* MBEDTLS_MD_C */
3163 {
3164 /* This shouldn't happen if `operation` was initialized by
3165 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003166 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003167 }
3168
Gilles Peskinefbfac682018-07-08 20:51:54 +02003169 if( status != PSA_SUCCESS )
3170 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003171 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003172}
3173
Gilles Peskine01126fa2018-07-12 17:04:55 +02003174#if defined(MBEDTLS_MD_C)
3175static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3176 uint8_t *mac,
3177 size_t mac_size )
3178{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003179 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003180 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3181 size_t hash_size = 0;
3182 size_t block_size = psa_get_hash_block_size( hash_alg );
3183 psa_status_t status;
3184
Gilles Peskine01126fa2018-07-12 17:04:55 +02003185 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3186 if( status != PSA_SUCCESS )
3187 return( status );
3188 /* From here on, tmp needs to be wiped. */
3189
3190 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3191 if( status != PSA_SUCCESS )
3192 goto exit;
3193
3194 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3195 if( status != PSA_SUCCESS )
3196 goto exit;
3197
3198 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3199 if( status != PSA_SUCCESS )
3200 goto exit;
3201
Gilles Peskined911eb72018-08-14 15:18:45 +02003202 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3203 if( status != PSA_SUCCESS )
3204 goto exit;
3205
3206 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003207
3208exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003209 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003210 return( status );
3211}
3212#endif /* MBEDTLS_MD_C */
3213
mohammad16036df908f2018-04-02 08:34:15 -07003214static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003215 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003216 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003217{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003218 if( ! operation->key_set )
3219 return( PSA_ERROR_BAD_STATE );
3220 if( operation->iv_required && ! operation->iv_set )
3221 return( PSA_ERROR_BAD_STATE );
3222
Gilles Peskine8c9def32018-02-08 10:02:12 +01003223 if( mac_size < operation->mac_size )
3224 return( PSA_ERROR_BUFFER_TOO_SMALL );
3225
Gilles Peskine8c9def32018-02-08 10:02:12 +01003226#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003227 if( operation->alg == PSA_ALG_CMAC )
3228 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003229 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3230 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3231 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003232 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003233 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003234 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003235 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003236 else
3237#endif /* MBEDTLS_CMAC_C */
3238#if defined(MBEDTLS_MD_C)
3239 if( PSA_ALG_IS_HMAC( operation->alg ) )
3240 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003241 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003242 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003243 }
3244 else
3245#endif /* MBEDTLS_MD_C */
3246 {
3247 /* This shouldn't happen if `operation` was initialized by
3248 * a setup function. */
3249 return( PSA_ERROR_BAD_STATE );
3250 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003251}
3252
Gilles Peskineacd4be32018-07-08 19:56:25 +02003253psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3254 uint8_t *mac,
3255 size_t mac_size,
3256 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003257{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003258 psa_status_t status;
3259
Jaeden Amero252ef282019-02-15 14:05:35 +00003260 if( operation->alg == 0 )
3261 {
3262 return( PSA_ERROR_BAD_STATE );
3263 }
3264
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003265 /* Fill the output buffer with something that isn't a valid mac
3266 * (barring an attack on the mac and deliberately-crafted input),
3267 * in case the caller doesn't check the return status properly. */
3268 *mac_length = mac_size;
3269 /* If mac_size is 0 then mac may be NULL and then the
3270 * call to memset would have undefined behavior. */
3271 if( mac_size != 0 )
3272 memset( mac, '!', mac_size );
3273
Gilles Peskine89167cb2018-07-08 20:12:23 +02003274 if( ! operation->is_sign )
3275 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003276 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003277 }
mohammad16036df908f2018-04-02 08:34:15 -07003278
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003279 status = psa_mac_finish_internal( operation, mac, mac_size );
3280
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003281 if( status == PSA_SUCCESS )
3282 {
3283 status = psa_mac_abort( operation );
3284 if( status == PSA_SUCCESS )
3285 *mac_length = operation->mac_size;
3286 else
3287 memset( mac, '!', mac_size );
3288 }
3289 else
3290 psa_mac_abort( operation );
3291 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003292}
3293
Gilles Peskineacd4be32018-07-08 19:56:25 +02003294psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3295 const uint8_t *mac,
3296 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003297{
Gilles Peskine828ed142018-06-18 23:25:51 +02003298 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003299 psa_status_t status;
3300
Jaeden Amero252ef282019-02-15 14:05:35 +00003301 if( operation->alg == 0 )
3302 {
3303 return( PSA_ERROR_BAD_STATE );
3304 }
3305
Gilles Peskine89167cb2018-07-08 20:12:23 +02003306 if( operation->is_sign )
3307 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003308 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003309 }
3310 if( operation->mac_size != mac_length )
3311 {
3312 status = PSA_ERROR_INVALID_SIGNATURE;
3313 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003314 }
mohammad16036df908f2018-04-02 08:34:15 -07003315
3316 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003317 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003318 if( status != PSA_SUCCESS )
3319 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003320
3321 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3322 status = PSA_ERROR_INVALID_SIGNATURE;
3323
3324cleanup:
3325 if( status == PSA_SUCCESS )
3326 status = psa_mac_abort( operation );
3327 else
3328 psa_mac_abort( operation );
3329
Gilles Peskine3f108122018-12-07 18:14:53 +01003330 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003331
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003332 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003333}
3334
3335
Gilles Peskine20035e32018-02-03 22:44:14 +01003336
Gilles Peskine20035e32018-02-03 22:44:14 +01003337/****************************************************************/
3338/* Asymmetric cryptography */
3339/****************************************************************/
3340
Gilles Peskine2b450e32018-06-27 15:42:46 +02003341#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003342/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003343 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003344static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3345 size_t hash_length,
3346 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003347{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003348 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003349 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003350 *md_alg = mbedtls_md_get_type( md_info );
3351
3352 /* The Mbed TLS RSA module uses an unsigned int for hash length
3353 * parameters. Validate that it fits so that we don't risk an
3354 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003355#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003356 if( hash_length > UINT_MAX )
3357 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003358#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003359
3360#if defined(MBEDTLS_PKCS1_V15)
3361 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3362 * must be correct. */
3363 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3364 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003365 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003366 if( md_info == NULL )
3367 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003368 if( mbedtls_md_get_size( md_info ) != hash_length )
3369 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003370 }
3371#endif /* MBEDTLS_PKCS1_V15 */
3372
3373#if defined(MBEDTLS_PKCS1_V21)
3374 /* PSS requires a hash internally. */
3375 if( PSA_ALG_IS_RSA_PSS( alg ) )
3376 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003377 if( md_info == NULL )
3378 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003379 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003380#endif /* MBEDTLS_PKCS1_V21 */
3381
Gilles Peskine61b91d42018-06-08 16:09:36 +02003382 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003383}
3384
Gilles Peskine2b450e32018-06-27 15:42:46 +02003385static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3386 psa_algorithm_t alg,
3387 const uint8_t *hash,
3388 size_t hash_length,
3389 uint8_t *signature,
3390 size_t signature_size,
3391 size_t *signature_length )
3392{
3393 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003395 mbedtls_md_type_t md_alg;
3396
3397 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3398 if( status != PSA_SUCCESS )
3399 return( status );
3400
Gilles Peskine630a18a2018-06-29 17:49:35 +02003401 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003402 return( PSA_ERROR_BUFFER_TOO_SMALL );
3403
3404#if defined(MBEDTLS_PKCS1_V15)
3405 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3406 {
3407 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3408 MBEDTLS_MD_NONE );
3409 ret = mbedtls_rsa_pkcs1_sign( rsa,
3410 mbedtls_ctr_drbg_random,
3411 &global_data.ctr_drbg,
3412 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003413 md_alg,
3414 (unsigned int) hash_length,
3415 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003416 signature );
3417 }
3418 else
3419#endif /* MBEDTLS_PKCS1_V15 */
3420#if defined(MBEDTLS_PKCS1_V21)
3421 if( PSA_ALG_IS_RSA_PSS( alg ) )
3422 {
3423 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3424 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3425 mbedtls_ctr_drbg_random,
3426 &global_data.ctr_drbg,
3427 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003428 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003429 (unsigned int) hash_length,
3430 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003431 signature );
3432 }
3433 else
3434#endif /* MBEDTLS_PKCS1_V21 */
3435 {
3436 return( PSA_ERROR_INVALID_ARGUMENT );
3437 }
3438
3439 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003440 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003441 return( mbedtls_to_psa_error( ret ) );
3442}
3443
3444static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3445 psa_algorithm_t alg,
3446 const uint8_t *hash,
3447 size_t hash_length,
3448 const uint8_t *signature,
3449 size_t signature_length )
3450{
3451 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003453 mbedtls_md_type_t md_alg;
3454
3455 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3456 if( status != PSA_SUCCESS )
3457 return( status );
3458
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003459 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3460 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003461
3462#if defined(MBEDTLS_PKCS1_V15)
3463 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3464 {
3465 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3466 MBEDTLS_MD_NONE );
3467 ret = mbedtls_rsa_pkcs1_verify( rsa,
3468 mbedtls_ctr_drbg_random,
3469 &global_data.ctr_drbg,
3470 MBEDTLS_RSA_PUBLIC,
3471 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003472 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003473 hash,
3474 signature );
3475 }
3476 else
3477#endif /* MBEDTLS_PKCS1_V15 */
3478#if defined(MBEDTLS_PKCS1_V21)
3479 if( PSA_ALG_IS_RSA_PSS( alg ) )
3480 {
3481 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3482 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3483 mbedtls_ctr_drbg_random,
3484 &global_data.ctr_drbg,
3485 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003486 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003487 (unsigned int) hash_length,
3488 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003489 signature );
3490 }
3491 else
3492#endif /* MBEDTLS_PKCS1_V21 */
3493 {
3494 return( PSA_ERROR_INVALID_ARGUMENT );
3495 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003496
3497 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3498 * the rest of the signature is invalid". This has little use in
3499 * practice and PSA doesn't report this distinction. */
3500 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3501 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003502 return( mbedtls_to_psa_error( ret ) );
3503}
3504#endif /* MBEDTLS_RSA_C */
3505
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003506#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003507/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3508 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3509 * (even though these functions don't modify it). */
3510static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3511 psa_algorithm_t alg,
3512 const uint8_t *hash,
3513 size_t hash_length,
3514 uint8_t *signature,
3515 size_t signature_size,
3516 size_t *signature_length )
3517{
Janos Follath24eed8d2019-11-22 13:21:35 +00003518 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003519 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003520 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003521 mbedtls_mpi_init( &r );
3522 mbedtls_mpi_init( &s );
3523
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003524 if( signature_size < 2 * curve_bytes )
3525 {
3526 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3527 goto cleanup;
3528 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003529
Gilles Peskinea05219c2018-11-16 16:02:56 +01003530#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003531 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3532 {
3533 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3534 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3535 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003536 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3537 &ecp->d, hash,
3538 hash_length, md_alg,
3539 mbedtls_ctr_drbg_random,
3540 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003541 }
3542 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003543#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003544 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003545 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003546 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3547 hash, hash_length,
3548 mbedtls_ctr_drbg_random,
3549 &global_data.ctr_drbg ) );
3550 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003551
3552 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3553 signature,
3554 curve_bytes ) );
3555 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3556 signature + curve_bytes,
3557 curve_bytes ) );
3558
3559cleanup:
3560 mbedtls_mpi_free( &r );
3561 mbedtls_mpi_free( &s );
3562 if( ret == 0 )
3563 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003564 return( mbedtls_to_psa_error( ret ) );
3565}
3566
3567static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3568 const uint8_t *hash,
3569 size_t hash_length,
3570 const uint8_t *signature,
3571 size_t signature_length )
3572{
Janos Follath24eed8d2019-11-22 13:21:35 +00003573 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003574 mbedtls_mpi r, s;
3575 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3576 mbedtls_mpi_init( &r );
3577 mbedtls_mpi_init( &s );
3578
3579 if( signature_length != 2 * curve_bytes )
3580 return( PSA_ERROR_INVALID_SIGNATURE );
3581
3582 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3583 signature,
3584 curve_bytes ) );
3585 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3586 signature + curve_bytes,
3587 curve_bytes ) );
3588
Steven Cooremanacda8342020-07-24 23:09:52 +02003589 /* Check whether the public part is loaded. If not, load it. */
3590 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3591 {
3592 MBEDTLS_MPI_CHK(
3593 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3594 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3595 }
3596
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003597 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3598 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003599
3600cleanup:
3601 mbedtls_mpi_free( &r );
3602 mbedtls_mpi_free( &s );
3603 return( mbedtls_to_psa_error( ret ) );
3604}
3605#endif /* MBEDTLS_ECDSA_C */
3606
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003607psa_status_t psa_sign_hash( psa_key_handle_t handle,
3608 psa_algorithm_t alg,
3609 const uint8_t *hash,
3610 size_t hash_length,
3611 uint8_t *signature,
3612 size_t signature_size,
3613 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003614{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003615 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003616 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003617#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3618 const psa_drv_se_t *drv;
3619 psa_drv_se_context_t *drv_context;
3620#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003621
3622 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003623 /* Immediately reject a zero-length signature buffer. This guarantees
3624 * that signature must be a valid pointer. (On the other hand, the hash
3625 * buffer can in principle be empty since it doesn't actually have
3626 * to be a hash.) */
3627 if( signature_size == 0 )
3628 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003629
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003630 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003631 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003632 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003633 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003634 {
3635 status = PSA_ERROR_INVALID_ARGUMENT;
3636 goto exit;
3637 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003638
Gilles Peskineedc64242019-08-07 21:05:07 +02003639#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3640 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3641 {
3642 if( drv->asymmetric == NULL ||
3643 drv->asymmetric->p_sign == NULL )
3644 {
3645 status = PSA_ERROR_NOT_SUPPORTED;
3646 goto exit;
3647 }
3648 status = drv->asymmetric->p_sign( drv_context,
3649 slot->data.se.slot_number,
3650 alg,
3651 hash, hash_length,
3652 signature, signature_size,
3653 signature_length );
3654 }
3655 else
3656#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003657#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003658 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003659 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003660 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003661
3662 status = psa_load_rsa_representation( slot,
3663 &rsa );
3664 if( status != PSA_SUCCESS )
3665 goto exit;
3666
Steven Cooremana2371e52020-07-28 14:30:39 +02003667 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003668 alg,
3669 hash, hash_length,
3670 signature, signature_size,
3671 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003672
Steven Cooremana2371e52020-07-28 14:30:39 +02003673 mbedtls_rsa_free( rsa );
3674 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003675 }
3676 else
3677#endif /* defined(MBEDTLS_RSA_C) */
3678#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003679 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003680 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003681#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003682 if(
3683#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3684 PSA_ALG_IS_ECDSA( alg )
3685#else
3686 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3687#endif
3688 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003689 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003690 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooremanacda8342020-07-24 23:09:52 +02003691 status = psa_load_ecp_representation( slot, &ecp );
3692 if( status != PSA_SUCCESS )
3693 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003694 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003695 alg,
3696 hash, hash_length,
3697 signature, signature_size,
3698 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003699 mbedtls_ecp_keypair_free( ecp );
3700 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003701 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003702 else
3703#endif /* defined(MBEDTLS_ECDSA_C) */
3704 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003705 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003706 }
itayzafrir5c753392018-05-08 11:18:38 +03003707 }
3708 else
3709#endif /* defined(MBEDTLS_ECP_C) */
3710 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003711 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003712 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003713
3714exit:
3715 /* Fill the unused part of the output buffer (the whole buffer on error,
3716 * the trailing part on success) with something that isn't a valid mac
3717 * (barring an attack on the mac and deliberately-crafted input),
3718 * in case the caller doesn't check the return status properly. */
3719 if( status == PSA_SUCCESS )
3720 memset( signature + *signature_length, '!',
3721 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003722 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003723 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003724 /* If signature_size is 0 then we have nothing to do. We must not call
3725 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003726 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003727}
3728
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003729psa_status_t psa_verify_hash( psa_key_handle_t handle,
3730 psa_algorithm_t alg,
3731 const uint8_t *hash,
3732 size_t hash_length,
3733 const uint8_t *signature,
3734 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003735{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003736 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003737 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003738#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3739 const psa_drv_se_t *drv;
3740 psa_drv_se_context_t *drv_context;
3741#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003742
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003743 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003744 if( status != PSA_SUCCESS )
3745 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003746
Gilles Peskineedc64242019-08-07 21:05:07 +02003747#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3748 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3749 {
3750 if( drv->asymmetric == NULL ||
3751 drv->asymmetric->p_verify == NULL )
3752 return( PSA_ERROR_NOT_SUPPORTED );
3753 return( drv->asymmetric->p_verify( drv_context,
3754 slot->data.se.slot_number,
3755 alg,
3756 hash, hash_length,
3757 signature, signature_length ) );
3758 }
3759 else
3760#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003761#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003762 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003763 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003764 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003765
3766 status = psa_load_rsa_representation( slot, &rsa );
3767 if( status != PSA_SUCCESS )
3768 return status;
3769
Steven Cooremana2371e52020-07-28 14:30:39 +02003770 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003771 alg,
3772 hash, hash_length,
3773 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003774 mbedtls_rsa_free( rsa );
3775 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003776 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003777 }
3778 else
3779#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003780#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003781 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003782 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003783#if defined(MBEDTLS_ECDSA_C)
3784 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003785 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003786 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooremanacda8342020-07-24 23:09:52 +02003787 status = psa_load_ecp_representation( slot, &ecp );
3788 if( status != PSA_SUCCESS )
3789 return status;
Steven Cooremana2371e52020-07-28 14:30:39 +02003790 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003791 hash, hash_length,
3792 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003793 mbedtls_ecp_keypair_free( ecp );
3794 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003795 return status;
3796 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003797 else
3798#endif /* defined(MBEDTLS_ECDSA_C) */
3799 {
3800 return( PSA_ERROR_INVALID_ARGUMENT );
3801 }
itayzafrir5c753392018-05-08 11:18:38 +03003802 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003803 else
3804#endif /* defined(MBEDTLS_ECP_C) */
3805 {
3806 return( PSA_ERROR_NOT_SUPPORTED );
3807 }
3808}
3809
Gilles Peskine072ac562018-06-30 00:21:29 +02003810#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3811static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3812 mbedtls_rsa_context *rsa )
3813{
3814 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3815 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3816 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3817 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3818}
3819#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3820
Gilles Peskinec5487a82018-12-03 18:08:14 +01003821psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003822 psa_algorithm_t alg,
3823 const uint8_t *input,
3824 size_t input_length,
3825 const uint8_t *salt,
3826 size_t salt_length,
3827 uint8_t *output,
3828 size_t output_size,
3829 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003830{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003831 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003832 psa_status_t status;
3833
Darryl Green5cc689a2018-07-24 15:34:10 +01003834 (void) input;
3835 (void) input_length;
3836 (void) salt;
3837 (void) output;
3838 (void) output_size;
3839
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003840 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003841
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003842 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3843 return( PSA_ERROR_INVALID_ARGUMENT );
3844
Gilles Peskine28f8f302019-07-24 13:30:31 +02003845 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003846 if( status != PSA_SUCCESS )
3847 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003848 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3849 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003850 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003851
3852#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003853 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003854 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003855 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02003856 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02003857
3858 status = psa_load_rsa_representation( slot, &rsa );
3859 if( status != PSA_SUCCESS )
3860 return status;
Steven Cooremana2371e52020-07-28 14:30:39 +02003861
3862 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003863 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003864 mbedtls_rsa_free( rsa );
3865 mbedtls_free( rsa );
Vikas Katariya21599b62019-08-02 12:26:29 +01003866 return( PSA_ERROR_BUFFER_TOO_SMALL );
Steven Cooremana01795d2020-07-24 22:48:15 +02003867 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003868#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003869 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003870 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003871 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003872 mbedtls_ctr_drbg_random,
3873 &global_data.ctr_drbg,
3874 MBEDTLS_RSA_PUBLIC,
3875 input_length,
3876 input,
3877 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003878 }
3879 else
3880#endif /* MBEDTLS_PKCS1_V15 */
3881#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003882 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003883 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003884 psa_rsa_oaep_set_padding_mode( alg, rsa );
3885 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine072ac562018-06-30 00:21:29 +02003886 mbedtls_ctr_drbg_random,
3887 &global_data.ctr_drbg,
3888 MBEDTLS_RSA_PUBLIC,
3889 salt, salt_length,
3890 input_length,
3891 input,
3892 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003893 }
3894 else
3895#endif /* MBEDTLS_PKCS1_V21 */
3896 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003897 mbedtls_rsa_free( rsa );
3898 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003899 return( PSA_ERROR_INVALID_ARGUMENT );
3900 }
3901 if( ret == 0 )
Steven Cooremana2371e52020-07-28 14:30:39 +02003902 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003903
Steven Cooremana2371e52020-07-28 14:30:39 +02003904 mbedtls_rsa_free( rsa );
3905 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003906 return( mbedtls_to_psa_error( ret ) );
3907 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003908 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003909#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003910 {
3911 return( PSA_ERROR_NOT_SUPPORTED );
3912 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003913}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003914
Gilles Peskinec5487a82018-12-03 18:08:14 +01003915psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003916 psa_algorithm_t alg,
3917 const uint8_t *input,
3918 size_t input_length,
3919 const uint8_t *salt,
3920 size_t salt_length,
3921 uint8_t *output,
3922 size_t output_size,
3923 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003924{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003925 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003926 psa_status_t status;
3927
Darryl Green5cc689a2018-07-24 15:34:10 +01003928 (void) input;
3929 (void) input_length;
3930 (void) salt;
3931 (void) output;
3932 (void) output_size;
3933
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003934 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003935
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003936 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3937 return( PSA_ERROR_INVALID_ARGUMENT );
3938
Gilles Peskine28f8f302019-07-24 13:30:31 +02003939 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003940 if( status != PSA_SUCCESS )
3941 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003942 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003943 return( PSA_ERROR_INVALID_ARGUMENT );
3944
3945#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003946 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003947 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003948 mbedtls_rsa_context *rsa;
Steven Cooreman75b74362020-07-28 14:30:13 +02003949 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02003950
3951 status = psa_load_rsa_representation( slot, &rsa );
3952 if( status != PSA_SUCCESS )
3953 return status;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003954
Steven Cooremana2371e52020-07-28 14:30:39 +02003955 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003956 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003957 mbedtls_rsa_free( rsa );
3958 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003959 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana01795d2020-07-24 22:48:15 +02003960 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003961
3962#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003963 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003964 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003965 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003966 mbedtls_ctr_drbg_random,
3967 &global_data.ctr_drbg,
3968 MBEDTLS_RSA_PRIVATE,
3969 output_length,
3970 input,
3971 output,
3972 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003973 }
3974 else
3975#endif /* MBEDTLS_PKCS1_V15 */
3976#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003977 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003978 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003979 psa_rsa_oaep_set_padding_mode( alg, rsa );
3980 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine072ac562018-06-30 00:21:29 +02003981 mbedtls_ctr_drbg_random,
3982 &global_data.ctr_drbg,
3983 MBEDTLS_RSA_PRIVATE,
3984 salt, salt_length,
3985 output_length,
3986 input,
3987 output,
3988 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003989 }
3990 else
3991#endif /* MBEDTLS_PKCS1_V21 */
3992 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003993 mbedtls_rsa_free( rsa );
3994 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003995 return( PSA_ERROR_INVALID_ARGUMENT );
3996 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003997
Steven Cooremana2371e52020-07-28 14:30:39 +02003998 mbedtls_rsa_free( rsa );
3999 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004000 return( mbedtls_to_psa_error( ret ) );
4001 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004002 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004003#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004004 {
4005 return( PSA_ERROR_NOT_SUPPORTED );
4006 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004007}
Gilles Peskine20035e32018-02-03 22:44:14 +01004008
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004009
4010
mohammad1603503973b2018-03-12 15:59:30 +02004011/****************************************************************/
4012/* Symmetric cryptography */
4013/****************************************************************/
4014
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004015/* Initialize the cipher operation structure. Once this function has been
4016 * called, psa_cipher_abort can run and will do the right thing. */
4017static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
4018 psa_algorithm_t alg )
4019{
Gilles Peskinec06e0712018-06-20 16:21:04 +02004020 if( ! PSA_ALG_IS_CIPHER( alg ) )
4021 {
4022 memset( operation, 0, sizeof( *operation ) );
4023 return( PSA_ERROR_INVALID_ARGUMENT );
4024 }
4025
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004026 operation->alg = alg;
4027 operation->key_set = 0;
4028 operation->iv_set = 0;
4029 operation->iv_required = 1;
4030 operation->iv_size = 0;
4031 operation->block_size = 0;
4032 mbedtls_cipher_init( &operation->ctx.cipher );
4033 return( PSA_SUCCESS );
4034}
4035
Gilles Peskinee553c652018-06-04 16:22:46 +02004036static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004037 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02004038 psa_algorithm_t alg,
4039 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004040{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004041 int ret = 0;
4042 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004043 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004044 size_t key_bits;
4045 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004046 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4047 PSA_KEY_USAGE_ENCRYPT :
4048 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004049
Jaeden Amero36ee5d02019-02-19 09:25:10 +00004050 /* A context must be freshly initialized before it can be set up. */
4051 if( operation->alg != 0 )
4052 {
4053 return( PSA_ERROR_BAD_STATE );
4054 }
4055
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004056 status = psa_cipher_init( operation, alg );
4057 if( status != PSA_SUCCESS )
4058 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004059
Gilles Peskine28f8f302019-07-24 13:30:31 +02004060 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004061 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004062 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004063 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02004064
Gilles Peskine8e338702019-07-30 20:06:31 +02004065 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004066 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004067 {
4068 status = PSA_ERROR_NOT_SUPPORTED;
4069 goto exit;
4070 }
mohammad1603503973b2018-03-12 15:59:30 +02004071
mohammad1603503973b2018-03-12 15:59:30 +02004072 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004073 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004074 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004075
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004076#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004077 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004078 {
4079 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004080 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004081 memcpy( keys, slot->data.key.data, 16 );
4082 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004083 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4084 keys,
4085 192, cipher_operation );
4086 }
4087 else
4088#endif
4089 {
4090 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004091 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004092 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004093 }
Moran Peker41deec42018-04-04 15:43:05 +03004094 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004095 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004096
mohammad16038481e742018-03-18 13:57:31 +02004097#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004098 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004099 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004100 case PSA_ALG_CBC_NO_PADDING:
4101 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4102 MBEDTLS_PADDING_NONE );
4103 break;
4104 case PSA_ALG_CBC_PKCS7:
4105 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4106 MBEDTLS_PADDING_PKCS7 );
4107 break;
4108 default:
4109 /* The algorithm doesn't involve padding. */
4110 ret = 0;
4111 break;
4112 }
4113 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004114 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004115#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4116
mohammad1603503973b2018-03-12 15:59:30 +02004117 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004118 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004119 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004120 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02004121 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004122 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004123 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004124#if defined(MBEDTLS_CHACHA20_C)
4125 else
4126 if( alg == PSA_ALG_CHACHA20 )
4127 operation->iv_size = 12;
4128#endif
mohammad1603503973b2018-03-12 15:59:30 +02004129
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004130exit:
4131 if( status == 0 )
4132 status = mbedtls_to_psa_error( ret );
4133 if( status != 0 )
4134 psa_cipher_abort( operation );
4135 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004136}
4137
Gilles Peskinefe119512018-07-08 21:39:34 +02004138psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004139 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004140 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004141{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004142 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004143}
4144
Gilles Peskinefe119512018-07-08 21:39:34 +02004145psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004146 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004147 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004148{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004149 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004150}
4151
Gilles Peskinefe119512018-07-08 21:39:34 +02004152psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004153 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004154 size_t iv_size,
4155 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004156{
itayzafrir534bd7c2018-08-02 13:56:32 +03004157 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004158 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004159 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004160 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004161 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004162 }
Moran Peker41deec42018-04-04 15:43:05 +03004163 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004164 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004165 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004166 goto exit;
4167 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004168 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4169 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004170 if( ret != 0 )
4171 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004172 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004173 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004174 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004175
mohammad16038481e742018-03-18 13:57:31 +02004176 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004177 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004178
Moran Peker395db872018-05-31 14:07:14 +03004179exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03004180 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03004181 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004182 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004183}
4184
Gilles Peskinefe119512018-07-08 21:39:34 +02004185psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004186 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004187 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004188{
itayzafrir534bd7c2018-08-02 13:56:32 +03004189 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004190 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004191 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004192 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004193 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004194 }
Moran Pekera28258c2018-05-29 16:25:04 +03004195 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004196 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004197 status = PSA_ERROR_INVALID_ARGUMENT;
4198 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004199 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004200 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4201 status = mbedtls_to_psa_error( ret );
4202exit:
4203 if( status == PSA_SUCCESS )
4204 operation->iv_set = 1;
4205 else
mohammad1603503973b2018-03-12 15:59:30 +02004206 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004207 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004208}
4209
Gilles Peskinee553c652018-06-04 16:22:46 +02004210psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4211 const uint8_t *input,
4212 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004213 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004214 size_t output_size,
4215 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004216{
itayzafrir534bd7c2018-08-02 13:56:32 +03004217 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004218 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004219 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00004220
4221 if( operation->alg == 0 )
4222 {
4223 return( PSA_ERROR_BAD_STATE );
4224 }
4225
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004226 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004227 {
4228 /* Take the unprocessed partial block left over from previous
4229 * update calls, if any, plus the input to this call. Remove
4230 * the last partial block, if any. You get the data that will be
4231 * output in this call. */
4232 expected_output_size =
4233 ( operation->ctx.cipher.unprocessed_len + input_length )
4234 / operation->block_size * operation->block_size;
4235 }
4236 else
4237 {
4238 expected_output_size = input_length;
4239 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004240
Gilles Peskine89d789c2018-06-04 17:17:16 +02004241 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004242 {
4243 status = PSA_ERROR_BUFFER_TOO_SMALL;
4244 goto exit;
4245 }
mohammad160382759612018-03-12 18:16:40 +02004246
mohammad1603503973b2018-03-12 15:59:30 +02004247 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02004248 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03004249 status = mbedtls_to_psa_error( ret );
4250exit:
4251 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004252 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004253 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004254}
4255
Gilles Peskinee553c652018-06-04 16:22:46 +02004256psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4257 uint8_t *output,
4258 size_t output_size,
4259 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004260{
David Saadab4ecc272019-02-14 13:48:10 +02004261 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01004262 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02004263 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03004264
mohammad1603503973b2018-03-12 15:59:30 +02004265 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004266 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004267 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004268 }
4269 if( operation->iv_required && ! operation->iv_set )
4270 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004271 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004272 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004273
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004274 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004275 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4276 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004277 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004278 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004279 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004280 }
4281
Janos Follath315b51c2018-07-09 16:04:51 +01004282 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4283 temp_output_buffer,
4284 output_length );
4285 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004286 {
Janos Follath315b51c2018-07-09 16:04:51 +01004287 status = mbedtls_to_psa_error( cipher_ret );
4288 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004289 }
Janos Follath315b51c2018-07-09 16:04:51 +01004290
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004291 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004292 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004293 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004294 memcpy( output, temp_output_buffer, *output_length );
4295 else
4296 {
Janos Follath315b51c2018-07-09 16:04:51 +01004297 status = PSA_ERROR_BUFFER_TOO_SMALL;
4298 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004299 }
mohammad1603503973b2018-03-12 15:59:30 +02004300
Gilles Peskine3f108122018-12-07 18:14:53 +01004301 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004302 status = psa_cipher_abort( operation );
4303
4304 return( status );
4305
4306error:
4307
4308 *output_length = 0;
4309
Gilles Peskine3f108122018-12-07 18:14:53 +01004310 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004311 (void) psa_cipher_abort( operation );
4312
4313 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004314}
4315
Gilles Peskinee553c652018-06-04 16:22:46 +02004316psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4317{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004318 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004319 {
4320 /* The object has (apparently) been initialized but it is not
4321 * in use. It's ok to call abort on such an object, and there's
4322 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004323 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004324 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004325
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004326 /* Sanity check (shouldn't happen: operation->alg should
4327 * always have been initialized to a valid value). */
4328 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4329 return( PSA_ERROR_BAD_STATE );
4330
mohammad1603503973b2018-03-12 15:59:30 +02004331 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004332
Moran Peker41deec42018-04-04 15:43:05 +03004333 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004334 operation->key_set = 0;
4335 operation->iv_set = 0;
4336 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004337 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004338 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004339
Moran Peker395db872018-05-31 14:07:14 +03004340 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004341}
4342
Gilles Peskinea0655c32018-04-30 17:06:50 +02004343
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004344
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004345
mohammad16035955c982018-04-26 00:53:03 +03004346/****************************************************************/
4347/* AEAD */
4348/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004349
Gilles Peskineedf9a652018-08-17 18:11:56 +02004350typedef struct
4351{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004352 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004353 const mbedtls_cipher_info_t *cipher_info;
4354 union
4355 {
4356#if defined(MBEDTLS_CCM_C)
4357 mbedtls_ccm_context ccm;
4358#endif /* MBEDTLS_CCM_C */
4359#if defined(MBEDTLS_GCM_C)
4360 mbedtls_gcm_context gcm;
4361#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004362#if defined(MBEDTLS_CHACHAPOLY_C)
4363 mbedtls_chachapoly_context chachapoly;
4364#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004365 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004366 psa_algorithm_t core_alg;
4367 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004368 uint8_t tag_length;
4369} aead_operation_t;
4370
Gilles Peskine30a9e412019-01-14 18:36:12 +01004371static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004372{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004373 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004374 {
4375#if defined(MBEDTLS_CCM_C)
4376 case PSA_ALG_CCM:
4377 mbedtls_ccm_free( &operation->ctx.ccm );
4378 break;
4379#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004380#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004381 case PSA_ALG_GCM:
4382 mbedtls_gcm_free( &operation->ctx.gcm );
4383 break;
4384#endif /* MBEDTLS_GCM_C */
4385 }
4386}
4387
4388static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004389 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004390 psa_key_usage_t usage,
4391 psa_algorithm_t alg )
4392{
4393 psa_status_t status;
4394 size_t key_bits;
4395 mbedtls_cipher_id_t cipher_id;
4396
Gilles Peskine28f8f302019-07-24 13:30:31 +02004397 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004398 if( status != PSA_SUCCESS )
4399 return( status );
4400
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004401 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004402
4403 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004404 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004405 &cipher_id );
4406 if( operation->cipher_info == NULL )
4407 return( PSA_ERROR_NOT_SUPPORTED );
4408
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004409 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004410 {
4411#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004412 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4413 operation->core_alg = PSA_ALG_CCM;
4414 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004415 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4416 * The call to mbedtls_ccm_encrypt_and_tag or
4417 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004418 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004419 return( PSA_ERROR_INVALID_ARGUMENT );
4420 mbedtls_ccm_init( &operation->ctx.ccm );
4421 status = mbedtls_to_psa_error(
4422 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004423 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004424 (unsigned int) key_bits ) );
4425 if( status != 0 )
4426 goto cleanup;
4427 break;
4428#endif /* MBEDTLS_CCM_C */
4429
4430#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004431 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4432 operation->core_alg = PSA_ALG_GCM;
4433 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004434 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4435 * The call to mbedtls_gcm_crypt_and_tag or
4436 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004437 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004438 return( PSA_ERROR_INVALID_ARGUMENT );
4439 mbedtls_gcm_init( &operation->ctx.gcm );
4440 status = mbedtls_to_psa_error(
4441 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004442 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004443 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004444 if( status != 0 )
4445 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004446 break;
4447#endif /* MBEDTLS_GCM_C */
4448
Gilles Peskine26869f22019-05-06 15:25:00 +02004449#if defined(MBEDTLS_CHACHAPOLY_C)
4450 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4451 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4452 operation->full_tag_length = 16;
4453 /* We only support the default tag length. */
4454 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4455 return( PSA_ERROR_NOT_SUPPORTED );
4456 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4457 status = mbedtls_to_psa_error(
4458 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004459 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004460 if( status != 0 )
4461 goto cleanup;
4462 break;
4463#endif /* MBEDTLS_CHACHAPOLY_C */
4464
Gilles Peskineedf9a652018-08-17 18:11:56 +02004465 default:
4466 return( PSA_ERROR_NOT_SUPPORTED );
4467 }
4468
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004469 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4470 {
4471 status = PSA_ERROR_INVALID_ARGUMENT;
4472 goto cleanup;
4473 }
4474 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004475
Gilles Peskineedf9a652018-08-17 18:11:56 +02004476 return( PSA_SUCCESS );
4477
4478cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004479 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004480 return( status );
4481}
4482
Gilles Peskinec5487a82018-12-03 18:08:14 +01004483psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004484 psa_algorithm_t alg,
4485 const uint8_t *nonce,
4486 size_t nonce_length,
4487 const uint8_t *additional_data,
4488 size_t additional_data_length,
4489 const uint8_t *plaintext,
4490 size_t plaintext_length,
4491 uint8_t *ciphertext,
4492 size_t ciphertext_size,
4493 size_t *ciphertext_length )
4494{
mohammad16035955c982018-04-26 00:53:03 +03004495 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004496 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004497 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004498
mohammad1603f08a5502018-06-03 15:05:47 +03004499 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004500
Gilles Peskinec5487a82018-12-03 18:08:14 +01004501 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004502 if( status != PSA_SUCCESS )
4503 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004504
Gilles Peskineedf9a652018-08-17 18:11:56 +02004505 /* For all currently supported modes, the tag is at the end of the
4506 * ciphertext. */
4507 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4508 {
4509 status = PSA_ERROR_BUFFER_TOO_SMALL;
4510 goto exit;
4511 }
4512 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004513
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004514#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004515 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004516 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004517 status = mbedtls_to_psa_error(
4518 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4519 MBEDTLS_GCM_ENCRYPT,
4520 plaintext_length,
4521 nonce, nonce_length,
4522 additional_data, additional_data_length,
4523 plaintext, ciphertext,
4524 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004525 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004526 else
4527#endif /* MBEDTLS_GCM_C */
4528#if defined(MBEDTLS_CCM_C)
4529 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004530 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004531 status = mbedtls_to_psa_error(
4532 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4533 plaintext_length,
4534 nonce, nonce_length,
4535 additional_data,
4536 additional_data_length,
4537 plaintext, ciphertext,
4538 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004539 }
mohammad16035c8845f2018-05-09 05:40:09 -07004540 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004541#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004542#if defined(MBEDTLS_CHACHAPOLY_C)
4543 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4544 {
4545 if( nonce_length != 12 || operation.tag_length != 16 )
4546 {
4547 status = PSA_ERROR_NOT_SUPPORTED;
4548 goto exit;
4549 }
4550 status = mbedtls_to_psa_error(
4551 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4552 plaintext_length,
4553 nonce,
4554 additional_data,
4555 additional_data_length,
4556 plaintext,
4557 ciphertext,
4558 tag ) );
4559 }
4560 else
4561#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004562 {
mohammad1603554faad2018-06-03 15:07:38 +03004563 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004564 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004565
Gilles Peskineedf9a652018-08-17 18:11:56 +02004566 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4567 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004568
Gilles Peskineedf9a652018-08-17 18:11:56 +02004569exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004570 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004571 if( status == PSA_SUCCESS )
4572 *ciphertext_length = plaintext_length + operation.tag_length;
4573 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004574}
4575
Gilles Peskineee652a32018-06-01 19:23:52 +02004576/* Locate the tag in a ciphertext buffer containing the encrypted data
4577 * followed by the tag. Return the length of the part preceding the tag in
4578 * *plaintext_length. This is the size of the plaintext in modes where
4579 * the encrypted data has the same size as the plaintext, such as
4580 * CCM and GCM. */
4581static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4582 const uint8_t *ciphertext,
4583 size_t ciphertext_length,
4584 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004585 const uint8_t **p_tag )
4586{
4587 size_t payload_length;
4588 if( tag_length > ciphertext_length )
4589 return( PSA_ERROR_INVALID_ARGUMENT );
4590 payload_length = ciphertext_length - tag_length;
4591 if( payload_length > plaintext_size )
4592 return( PSA_ERROR_BUFFER_TOO_SMALL );
4593 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004594 return( PSA_SUCCESS );
4595}
4596
Gilles Peskinec5487a82018-12-03 18:08:14 +01004597psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004598 psa_algorithm_t alg,
4599 const uint8_t *nonce,
4600 size_t nonce_length,
4601 const uint8_t *additional_data,
4602 size_t additional_data_length,
4603 const uint8_t *ciphertext,
4604 size_t ciphertext_length,
4605 uint8_t *plaintext,
4606 size_t plaintext_size,
4607 size_t *plaintext_length )
4608{
mohammad16035955c982018-04-26 00:53:03 +03004609 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004610 aead_operation_t operation;
4611 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004612
Gilles Peskineee652a32018-06-01 19:23:52 +02004613 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004614
Gilles Peskinec5487a82018-12-03 18:08:14 +01004615 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004616 if( status != PSA_SUCCESS )
4617 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004618
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004619 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4620 ciphertext, ciphertext_length,
4621 plaintext_size, &tag );
4622 if( status != PSA_SUCCESS )
4623 goto exit;
4624
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004625#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004626 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004627 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004628 status = mbedtls_to_psa_error(
4629 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4630 ciphertext_length - operation.tag_length,
4631 nonce, nonce_length,
4632 additional_data,
4633 additional_data_length,
4634 tag, operation.tag_length,
4635 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004636 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004637 else
4638#endif /* MBEDTLS_GCM_C */
4639#if defined(MBEDTLS_CCM_C)
4640 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004641 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004642 status = mbedtls_to_psa_error(
4643 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4644 ciphertext_length - operation.tag_length,
4645 nonce, nonce_length,
4646 additional_data,
4647 additional_data_length,
4648 ciphertext, plaintext,
4649 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004650 }
mohammad160339574652018-06-01 04:39:53 -07004651 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004652#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004653#if defined(MBEDTLS_CHACHAPOLY_C)
4654 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4655 {
4656 if( nonce_length != 12 || operation.tag_length != 16 )
4657 {
4658 status = PSA_ERROR_NOT_SUPPORTED;
4659 goto exit;
4660 }
4661 status = mbedtls_to_psa_error(
4662 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4663 ciphertext_length - operation.tag_length,
4664 nonce,
4665 additional_data,
4666 additional_data_length,
4667 tag,
4668 ciphertext,
4669 plaintext ) );
4670 }
4671 else
4672#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004673 {
mohammad1603554faad2018-06-03 15:07:38 +03004674 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004675 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004676
Gilles Peskineedf9a652018-08-17 18:11:56 +02004677 if( status != PSA_SUCCESS && plaintext_size != 0 )
4678 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004679
Gilles Peskineedf9a652018-08-17 18:11:56 +02004680exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004681 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004682 if( status == PSA_SUCCESS )
4683 *plaintext_length = ciphertext_length - operation.tag_length;
4684 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004685}
4686
Gilles Peskinea0655c32018-04-30 17:06:50 +02004687
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004688
Gilles Peskine20035e32018-02-03 22:44:14 +01004689/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004690/* Generators */
4691/****************************************************************/
4692
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004693#define HKDF_STATE_INIT 0 /* no input yet */
4694#define HKDF_STATE_STARTED 1 /* got salt */
4695#define HKDF_STATE_KEYED 2 /* got key */
4696#define HKDF_STATE_OUTPUT 3 /* output started */
4697
Gilles Peskinecbe66502019-05-16 16:59:18 +02004698static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004699 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004700{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004701 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4702 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004703 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004704 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004705}
4706
4707
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004708psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004709{
4710 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004711 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004712 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004713 {
4714 /* The object has (apparently) been initialized but it is not
4715 * in use. It's ok to call abort on such an object, and there's
4716 * nothing to do. */
4717 }
4718 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004719#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004720 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004721 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004722 mbedtls_free( operation->ctx.hkdf.info );
4723 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004724 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004725 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004726 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004727 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004728 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004729 if( operation->ctx.tls12_prf.seed != NULL )
4730 {
4731 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4732 operation->ctx.tls12_prf.seed_length );
4733 mbedtls_free( operation->ctx.tls12_prf.seed );
4734 }
4735
4736 if( operation->ctx.tls12_prf.label != NULL )
4737 {
4738 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4739 operation->ctx.tls12_prf.label_length );
4740 mbedtls_free( operation->ctx.tls12_prf.label );
4741 }
4742
4743 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4744
4745 /* We leave the fields Ai and output_block to be erased safely by the
4746 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004747 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004748 else
4749#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004750 {
4751 status = PSA_ERROR_BAD_STATE;
4752 }
Janos Follath083036a2019-06-11 10:22:26 +01004753 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004754 return( status );
4755}
4756
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004757psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004758 size_t *capacity)
4759{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004760 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004761 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004762 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004763 return PSA_ERROR_BAD_STATE;
4764 }
4765
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004766 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004767 return( PSA_SUCCESS );
4768}
4769
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004770psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004771 size_t capacity )
4772{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004773 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004774 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004775 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004776 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004777 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004778 return( PSA_SUCCESS );
4779}
4780
Gilles Peskinebef7f142018-07-12 17:22:21 +02004781#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004782/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004783 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004784static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004785 psa_algorithm_t hash_alg,
4786 uint8_t *output,
4787 size_t output_length )
4788{
4789 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4790 psa_status_t status;
4791
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004792 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4793 return( PSA_ERROR_BAD_STATE );
4794 hkdf->state = HKDF_STATE_OUTPUT;
4795
Gilles Peskinebef7f142018-07-12 17:22:21 +02004796 while( output_length != 0 )
4797 {
4798 /* Copy what remains of the current block */
4799 uint8_t n = hash_length - hkdf->offset_in_block;
4800 if( n > output_length )
4801 n = (uint8_t) output_length;
4802 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4803 output += n;
4804 output_length -= n;
4805 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004806 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004807 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004808 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004809 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004810 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004811 * object was corrupted or if this function is called directly
4812 * inside the library. */
4813 if( hkdf->block_number == 0xff )
4814 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004815
4816 /* We need a new block */
4817 ++hkdf->block_number;
4818 hkdf->offset_in_block = 0;
4819 status = psa_hmac_setup_internal( &hkdf->hmac,
4820 hkdf->prk, hash_length,
4821 hash_alg );
4822 if( status != PSA_SUCCESS )
4823 return( status );
4824 if( hkdf->block_number != 1 )
4825 {
4826 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4827 hkdf->output_block,
4828 hash_length );
4829 if( status != PSA_SUCCESS )
4830 return( status );
4831 }
4832 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4833 hkdf->info,
4834 hkdf->info_length );
4835 if( status != PSA_SUCCESS )
4836 return( status );
4837 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4838 &hkdf->block_number, 1 );
4839 if( status != PSA_SUCCESS )
4840 return( status );
4841 status = psa_hmac_finish_internal( &hkdf->hmac,
4842 hkdf->output_block,
4843 sizeof( hkdf->output_block ) );
4844 if( status != PSA_SUCCESS )
4845 return( status );
4846 }
4847
4848 return( PSA_SUCCESS );
4849}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004850
Janos Follath7742fee2019-06-17 12:58:10 +01004851static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4852 psa_tls12_prf_key_derivation_t *tls12_prf,
4853 psa_algorithm_t alg )
4854{
4855 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4856 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004857 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4858 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004859
Janos Follath7742fee2019-06-17 12:58:10 +01004860 /* We can't be wanting more output after block 0xff, otherwise
4861 * the capacity check in psa_key_derivation_output_bytes() would have
4862 * prevented this call. It could happen only if the operation
4863 * object was corrupted or if this function is called directly
4864 * inside the library. */
4865 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004866 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004867
4868 /* We need a new block */
4869 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004870 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004871
4872 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4873 *
4874 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4875 *
4876 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4877 * HMAC_hash(secret, A(2) + seed) +
4878 * HMAC_hash(secret, A(3) + seed) + ...
4879 *
4880 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004881 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004882 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004883 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004884 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004885 * is currently extracted as `output_block` and where i is
4886 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004887 */
4888
Janos Follathea29bfb2019-06-19 12:21:20 +01004889 /* Save the hash context before using it, to preserve the hash state with
4890 * only the inner padding in it. We need this, because inner padding depends
4891 * on the key (secret in the RFC's terminology). */
4892 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4893 if( status != PSA_SUCCESS )
4894 goto cleanup;
4895
4896 /* Calculate A(i) where i = tls12_prf->block_number. */
4897 if( tls12_prf->block_number == 1 )
4898 {
4899 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4900 * the variable seed and in this instance means it in the context of the
4901 * P_hash function, where seed = label + seed.) */
4902 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4903 tls12_prf->label, tls12_prf->label_length );
4904 if( status != PSA_SUCCESS )
4905 goto cleanup;
4906 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4907 tls12_prf->seed, tls12_prf->seed_length );
4908 if( status != PSA_SUCCESS )
4909 goto cleanup;
4910 }
4911 else
4912 {
4913 /* A(i) = HMAC_hash(secret, A(i-1)) */
4914 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4915 tls12_prf->Ai, hash_length );
4916 if( status != PSA_SUCCESS )
4917 goto cleanup;
4918 }
4919
4920 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4921 tls12_prf->Ai, hash_length );
4922 if( status != PSA_SUCCESS )
4923 goto cleanup;
4924 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4925 if( status != PSA_SUCCESS )
4926 goto cleanup;
4927
4928 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4929 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4930 tls12_prf->Ai, hash_length );
4931 if( status != PSA_SUCCESS )
4932 goto cleanup;
4933 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4934 tls12_prf->label, tls12_prf->label_length );
4935 if( status != PSA_SUCCESS )
4936 goto cleanup;
4937 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4938 tls12_prf->seed, tls12_prf->seed_length );
4939 if( status != PSA_SUCCESS )
4940 goto cleanup;
4941 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4942 tls12_prf->output_block, hash_length );
4943 if( status != PSA_SUCCESS )
4944 goto cleanup;
4945 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4946 if( status != PSA_SUCCESS )
4947 goto cleanup;
4948
Janos Follath7742fee2019-06-17 12:58:10 +01004949
4950cleanup:
4951
Janos Follathea29bfb2019-06-19 12:21:20 +01004952 cleanup_status = psa_hash_abort( &backup );
4953 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4954 status = cleanup_status;
4955
4956 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004957}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004958
Janos Follath844eb0e2019-06-19 12:10:49 +01004959static psa_status_t psa_key_derivation_tls12_prf_read(
4960 psa_tls12_prf_key_derivation_t *tls12_prf,
4961 psa_algorithm_t alg,
4962 uint8_t *output,
4963 size_t output_length )
4964{
4965 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4966 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4967 psa_status_t status;
4968 uint8_t offset, length;
4969
4970 while( output_length != 0 )
4971 {
4972 /* Check if we have fully processed the current block. */
4973 if( tls12_prf->left_in_block == 0 )
4974 {
4975 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4976 alg );
4977 if( status != PSA_SUCCESS )
4978 return( status );
4979
4980 continue;
4981 }
4982
4983 if( tls12_prf->left_in_block > output_length )
4984 length = (uint8_t) output_length;
4985 else
4986 length = tls12_prf->left_in_block;
4987
4988 offset = hash_length - tls12_prf->left_in_block;
4989 memcpy( output, tls12_prf->output_block + offset, length );
4990 output += length;
4991 output_length -= length;
4992 tls12_prf->left_in_block -= length;
4993 }
4994
4995 return( PSA_SUCCESS );
4996}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004997#endif /* MBEDTLS_MD_C */
4998
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004999psa_status_t psa_key_derivation_output_bytes(
5000 psa_key_derivation_operation_t *operation,
5001 uint8_t *output,
5002 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005003{
5004 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005005 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005006
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005007 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005008 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005009 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005010 return PSA_ERROR_BAD_STATE;
5011 }
5012
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005013 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005014 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005015 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005016 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005017 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005018 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005019 goto exit;
5020 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005021 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005022 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005023 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005024 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005025 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5026 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005027 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005028 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005029 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005030 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005031 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005032
Gilles Peskinebef7f142018-07-12 17:22:21 +02005033#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005034 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005035 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005036 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005037 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005038 output, output_length );
5039 }
Janos Follathadbec812019-06-14 11:05:39 +01005040 else
Janos Follathadbec812019-06-14 11:05:39 +01005041 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005042 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005043 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005044 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005045 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005046 output_length );
5047 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005048 else
5049#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005050 {
5051 return( PSA_ERROR_BAD_STATE );
5052 }
5053
5054exit:
5055 if( status != PSA_SUCCESS )
5056 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005057 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005058 * This allows us to differentiate between exhausted operations and
5059 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5060 * operations. */
5061 psa_algorithm_t alg = operation->alg;
5062 psa_key_derivation_abort( operation );
5063 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005064 memset( output, '!', output_length );
5065 }
5066 return( status );
5067}
5068
Gilles Peskine08542d82018-07-19 17:05:42 +02005069#if defined(MBEDTLS_DES_C)
5070static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5071{
5072 if( data_size >= 8 )
5073 mbedtls_des_key_set_parity( data );
5074 if( data_size >= 16 )
5075 mbedtls_des_key_set_parity( data + 8 );
5076 if( data_size >= 24 )
5077 mbedtls_des_key_set_parity( data + 16 );
5078}
5079#endif /* MBEDTLS_DES_C */
5080
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005081static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005082 psa_key_slot_t *slot,
5083 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005084 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005085{
5086 uint8_t *data = NULL;
5087 size_t bytes = PSA_BITS_TO_BYTES( bits );
5088 psa_status_t status;
5089
Gilles Peskine8e338702019-07-30 20:06:31 +02005090 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005091 return( PSA_ERROR_INVALID_ARGUMENT );
5092 if( bits % 8 != 0 )
5093 return( PSA_ERROR_INVALID_ARGUMENT );
5094 data = mbedtls_calloc( 1, bytes );
5095 if( data == NULL )
5096 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5097
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005098 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005099 if( status != PSA_SUCCESS )
5100 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005101#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005102 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005103 psa_des_set_key_parity( data, bytes );
5104#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005105 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005106
5107exit:
5108 mbedtls_free( data );
5109 return( status );
5110}
5111
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005112psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005113 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005114 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005115{
5116 psa_status_t status;
5117 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005118 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005119
5120 /* Reject any attempt to create a zero-length key so that we don't
5121 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5122 if( psa_get_key_bits( attributes ) == 0 )
5123 return( PSA_ERROR_INVALID_ARGUMENT );
5124
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005125 if( ! operation->can_output_key )
5126 return( PSA_ERROR_NOT_PERMITTED );
5127
Gilles Peskinedf179142019-07-15 22:02:14 +02005128 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5129 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005130#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5131 if( driver != NULL )
5132 {
5133 /* Deriving a key in a secure element is not implemented yet. */
5134 status = PSA_ERROR_NOT_SUPPORTED;
5135 }
5136#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005137 if( status == PSA_SUCCESS )
5138 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005139 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005140 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005141 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005142 }
5143 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005144 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005145 if( status != PSA_SUCCESS )
5146 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005147 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005148 *handle = 0;
5149 }
5150 return( status );
5151}
5152
Gilles Peskineeab56e42018-07-12 17:12:33 +02005153
5154
5155/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005156/* Key derivation */
5157/****************************************************************/
5158
Gilles Peskine969c5d62019-01-16 15:53:06 +01005159static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005160 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005161 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005162{
Janos Follath5fe19732019-06-20 15:09:30 +01005163 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5164 * initialisers for this union leave some bytes unspecified.) */
5165 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5166
Gilles Peskine969c5d62019-01-16 15:53:06 +01005167 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005168#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005169 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5170 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5171 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005172 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005173 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005174 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5175 if( hash_size == 0 )
5176 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005177 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5178 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005179 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005180 {
5181 return( PSA_ERROR_NOT_SUPPORTED );
5182 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005183 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005184 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005185 }
5186#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005187 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005188 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005189}
5190
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005191psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005192 psa_algorithm_t alg )
5193{
5194 psa_status_t status;
5195
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005196 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005197 return( PSA_ERROR_BAD_STATE );
5198
Gilles Peskine6843c292019-01-18 16:44:49 +01005199 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5200 return( PSA_ERROR_INVALID_ARGUMENT );
5201 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005202 {
5203 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005204 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005205 }
5206 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5207 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005208 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005209 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005210 else
5211 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005212
5213 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005214 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005215 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005216}
5217
5218#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005219static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005220 psa_algorithm_t hash_alg,
5221 psa_key_derivation_step_t step,
5222 const uint8_t *data,
5223 size_t data_length )
5224{
5225 psa_status_t status;
5226 switch( step )
5227 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005228 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005229 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005230 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005231 status = psa_hmac_setup_internal( &hkdf->hmac,
5232 data, data_length,
5233 hash_alg );
5234 if( status != PSA_SUCCESS )
5235 return( status );
5236 hkdf->state = HKDF_STATE_STARTED;
5237 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005238 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005239 /* If no salt was provided, use an empty salt. */
5240 if( hkdf->state == HKDF_STATE_INIT )
5241 {
5242 status = psa_hmac_setup_internal( &hkdf->hmac,
5243 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005244 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005245 if( status != PSA_SUCCESS )
5246 return( status );
5247 hkdf->state = HKDF_STATE_STARTED;
5248 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005249 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005250 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005251 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5252 data, data_length );
5253 if( status != PSA_SUCCESS )
5254 return( status );
5255 status = psa_hmac_finish_internal( &hkdf->hmac,
5256 hkdf->prk,
5257 sizeof( hkdf->prk ) );
5258 if( status != PSA_SUCCESS )
5259 return( status );
5260 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5261 hkdf->block_number = 0;
5262 hkdf->state = HKDF_STATE_KEYED;
5263 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005264 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005265 if( hkdf->state == HKDF_STATE_OUTPUT )
5266 return( PSA_ERROR_BAD_STATE );
5267 if( hkdf->info_set )
5268 return( PSA_ERROR_BAD_STATE );
5269 hkdf->info_length = data_length;
5270 if( data_length != 0 )
5271 {
5272 hkdf->info = mbedtls_calloc( 1, data_length );
5273 if( hkdf->info == NULL )
5274 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5275 memcpy( hkdf->info, data, data_length );
5276 }
5277 hkdf->info_set = 1;
5278 return( PSA_SUCCESS );
5279 default:
5280 return( PSA_ERROR_INVALID_ARGUMENT );
5281 }
5282}
Janos Follathb03233e2019-06-11 15:30:30 +01005283
Janos Follathf08e2652019-06-13 09:05:41 +01005284static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5285 const uint8_t *data,
5286 size_t data_length )
5287{
5288 if( prf->state != TLS12_PRF_STATE_INIT )
5289 return( PSA_ERROR_BAD_STATE );
5290
Janos Follathd6dce9f2019-07-04 09:11:38 +01005291 if( data_length != 0 )
5292 {
5293 prf->seed = mbedtls_calloc( 1, data_length );
5294 if( prf->seed == NULL )
5295 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005296
Janos Follathd6dce9f2019-07-04 09:11:38 +01005297 memcpy( prf->seed, data, data_length );
5298 prf->seed_length = data_length;
5299 }
Janos Follathf08e2652019-06-13 09:05:41 +01005300
5301 prf->state = TLS12_PRF_STATE_SEED_SET;
5302
5303 return( PSA_SUCCESS );
5304}
5305
Janos Follath81550542019-06-13 14:26:34 +01005306static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5307 psa_algorithm_t hash_alg,
5308 const uint8_t *data,
5309 size_t data_length )
5310{
5311 psa_status_t status;
5312 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5313 return( PSA_ERROR_BAD_STATE );
5314
5315 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5316 if( status != PSA_SUCCESS )
5317 return( status );
5318
5319 prf->state = TLS12_PRF_STATE_KEY_SET;
5320
5321 return( PSA_SUCCESS );
5322}
5323
Janos Follath6660f0e2019-06-17 08:44:03 +01005324static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5325 psa_tls12_prf_key_derivation_t *prf,
5326 psa_algorithm_t hash_alg,
5327 const uint8_t *data,
5328 size_t data_length )
5329{
5330 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005331 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5332 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005333
5334 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5335 return( PSA_ERROR_INVALID_ARGUMENT );
5336
5337 /* Quoting RFC 4279, Section 2:
5338 *
5339 * The premaster secret is formed as follows: if the PSK is N octets
5340 * long, concatenate a uint16 with the value N, N zero octets, a second
5341 * uint16 with the value N, and the PSK itself.
5342 */
5343
Janos Follath40e13932019-06-26 13:22:29 +01005344 *cur++ = ( data_length >> 8 ) & 0xff;
5345 *cur++ = ( data_length >> 0 ) & 0xff;
5346 memset( cur, 0, data_length );
5347 cur += data_length;
5348 *cur++ = pms[0];
5349 *cur++ = pms[1];
5350 memcpy( cur, data, data_length );
5351 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005352
Janos Follath40e13932019-06-26 13:22:29 +01005353 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005354
5355 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5356 return( status );
5357}
5358
Janos Follath63028dd2019-06-13 09:15:47 +01005359static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5360 const uint8_t *data,
5361 size_t data_length )
5362{
5363 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5364 return( PSA_ERROR_BAD_STATE );
5365
Janos Follathd6dce9f2019-07-04 09:11:38 +01005366 if( data_length != 0 )
5367 {
5368 prf->label = mbedtls_calloc( 1, data_length );
5369 if( prf->label == NULL )
5370 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005371
Janos Follathd6dce9f2019-07-04 09:11:38 +01005372 memcpy( prf->label, data, data_length );
5373 prf->label_length = data_length;
5374 }
Janos Follath63028dd2019-06-13 09:15:47 +01005375
5376 prf->state = TLS12_PRF_STATE_LABEL_SET;
5377
5378 return( PSA_SUCCESS );
5379}
5380
Janos Follathb03233e2019-06-11 15:30:30 +01005381static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5382 psa_algorithm_t hash_alg,
5383 psa_key_derivation_step_t step,
5384 const uint8_t *data,
5385 size_t data_length )
5386{
Janos Follathb03233e2019-06-11 15:30:30 +01005387 switch( step )
5388 {
Janos Follathf08e2652019-06-13 09:05:41 +01005389 case PSA_KEY_DERIVATION_INPUT_SEED:
5390 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005391 case PSA_KEY_DERIVATION_INPUT_SECRET:
5392 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005393 case PSA_KEY_DERIVATION_INPUT_LABEL:
5394 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005395 default:
5396 return( PSA_ERROR_INVALID_ARGUMENT );
5397 }
5398}
Janos Follath6660f0e2019-06-17 08:44:03 +01005399
5400static psa_status_t psa_tls12_prf_psk_to_ms_input(
5401 psa_tls12_prf_key_derivation_t *prf,
5402 psa_algorithm_t hash_alg,
5403 psa_key_derivation_step_t step,
5404 const uint8_t *data,
5405 size_t data_length )
5406{
5407 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005408 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005409 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5410 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005411 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005412
5413 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5414}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005415#endif /* MBEDTLS_MD_C */
5416
Gilles Peskineb8965192019-09-24 16:21:10 +02005417/** Check whether the given key type is acceptable for the given
5418 * input step of a key derivation.
5419 *
5420 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5421 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5422 * Both secret and non-secret inputs can alternatively have the type
5423 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5424 * that the input was passed as a buffer rather than via a key object.
5425 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005426static int psa_key_derivation_check_input_type(
5427 psa_key_derivation_step_t step,
5428 psa_key_type_t key_type )
5429{
5430 switch( step )
5431 {
5432 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005433 if( key_type == PSA_KEY_TYPE_DERIVE )
5434 return( PSA_SUCCESS );
5435 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005436 return( PSA_SUCCESS );
5437 break;
5438 case PSA_KEY_DERIVATION_INPUT_LABEL:
5439 case PSA_KEY_DERIVATION_INPUT_SALT:
5440 case PSA_KEY_DERIVATION_INPUT_INFO:
5441 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005442 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5443 return( PSA_SUCCESS );
5444 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005445 return( PSA_SUCCESS );
5446 break;
5447 }
5448 return( PSA_ERROR_INVALID_ARGUMENT );
5449}
5450
Janos Follathb80a94e2019-06-12 15:54:46 +01005451static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005452 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005453 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005454 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005455 const uint8_t *data,
5456 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005457{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005458 psa_status_t status;
5459 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5460
5461 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005462 if( status != PSA_SUCCESS )
5463 goto exit;
5464
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005465#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005466 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005467 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005468 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005469 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005470 step, data, data_length );
5471 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005472 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005473 {
Janos Follathb03233e2019-06-11 15:30:30 +01005474 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5475 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5476 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005477 }
5478 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5479 {
5480 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5481 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5482 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005483 }
5484 else
5485#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005486 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005487 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005488 return( PSA_ERROR_BAD_STATE );
5489 }
5490
Gilles Peskine224b0d62019-09-23 18:13:17 +02005491exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005492 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005493 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005494 return( status );
5495}
5496
Janos Follath51f4a0f2019-06-14 11:35:55 +01005497psa_status_t psa_key_derivation_input_bytes(
5498 psa_key_derivation_operation_t *operation,
5499 psa_key_derivation_step_t step,
5500 const uint8_t *data,
5501 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005502{
Gilles Peskineb8965192019-09-24 16:21:10 +02005503 return( psa_key_derivation_input_internal( operation, step,
5504 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005505 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005506}
5507
Janos Follath51f4a0f2019-06-14 11:35:55 +01005508psa_status_t psa_key_derivation_input_key(
5509 psa_key_derivation_operation_t *operation,
5510 psa_key_derivation_step_t step,
5511 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005512{
5513 psa_key_slot_t *slot;
5514 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005515
Gilles Peskine28f8f302019-07-24 13:30:31 +02005516 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005517 PSA_KEY_USAGE_DERIVE,
5518 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005519 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005520 {
5521 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005522 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005523 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005524
5525 /* Passing a key object as a SECRET input unlocks the permission
5526 * to output to a key object. */
5527 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5528 operation->can_output_key = 1;
5529
Janos Follathb80a94e2019-06-12 15:54:46 +01005530 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005531 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005532 slot->data.key.data,
5533 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005534}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005535
5536
5537
5538/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005539/* Key agreement */
5540/****************************************************************/
5541
Gilles Peskinea05219c2018-11-16 16:02:56 +01005542#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005543static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5544 size_t peer_key_length,
5545 const mbedtls_ecp_keypair *our_key,
5546 uint8_t *shared_secret,
5547 size_t shared_secret_size,
5548 size_t *shared_secret_length )
5549{
Steven Cooremana2371e52020-07-28 14:30:39 +02005550 mbedtls_ecp_keypair *their_key;
Steven Cooremanacda8342020-07-24 23:09:52 +02005551 psa_key_slot_t their_key_slot;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005552 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005553 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005554 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005555 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005556 mbedtls_ecdh_init( &ecdh );
Steven Cooremanacda8342020-07-24 23:09:52 +02005557 memset(&their_key_slot, 0, sizeof(their_key_slot));
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005558
Steven Cooremanacda8342020-07-24 23:09:52 +02005559 /* Creating ephemeral key slot for import purposes */
5560 their_key_slot.attr.type = PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve);
5561 their_key_slot.data.key.data = (uint8_t*) peer_key;
5562 their_key_slot.data.key.bytes = peer_key_length;
5563
5564 status = psa_load_ecp_representation( &their_key_slot, &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005565 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005566 goto exit;
5567
Jaeden Amero97271b32019-01-10 19:38:51 +00005568 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005569 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005570 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005571 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005572 status = mbedtls_to_psa_error(
5573 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5574 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005575 goto exit;
5576
Jaeden Amero97271b32019-01-10 19:38:51 +00005577 status = mbedtls_to_psa_error(
5578 mbedtls_ecdh_calc_secret( &ecdh,
5579 shared_secret_length,
5580 shared_secret, shared_secret_size,
5581 mbedtls_ctr_drbg_random,
5582 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005583 if( status != PSA_SUCCESS )
5584 goto exit;
5585 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5586 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005587
5588exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005589 if( status != PSA_SUCCESS )
5590 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005591 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005592 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005593 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005594
Jaeden Amero97271b32019-01-10 19:38:51 +00005595 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005596}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005597#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005598
Gilles Peskine01d718c2018-09-18 12:01:02 +02005599#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5600
Gilles Peskine0216fe12019-04-11 21:23:21 +02005601static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5602 psa_key_slot_t *private_key,
5603 const uint8_t *peer_key,
5604 size_t peer_key_length,
5605 uint8_t *shared_secret,
5606 size_t shared_secret_size,
5607 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005608{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005609 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005610 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005611#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005612 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005613 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005614 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005615 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooremanacda8342020-07-24 23:09:52 +02005616 psa_status_t status = psa_load_ecp_representation( private_key, &ecp );
5617 if( status != PSA_SUCCESS )
5618 return status;
5619 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005620 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005621 shared_secret, shared_secret_size,
5622 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005623 mbedtls_ecp_keypair_free( ecp );
5624 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005625 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005626#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005627 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005628 (void) private_key;
5629 (void) peer_key;
5630 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005631 (void) shared_secret;
5632 (void) shared_secret_size;
5633 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005634 return( PSA_ERROR_NOT_SUPPORTED );
5635 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005636}
5637
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005638/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005639 * to potentially free embedded data structures and wipe confidential data.
5640 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005641static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005642 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005643 psa_key_slot_t *private_key,
5644 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005645 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005646{
5647 psa_status_t status;
5648 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5649 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005650 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005651
5652 /* Step 1: run the secret agreement algorithm to generate the shared
5653 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005654 status = psa_key_agreement_raw_internal( ka_alg,
5655 private_key,
5656 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005657 shared_secret,
5658 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005659 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005660 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005661 goto exit;
5662
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005663 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005664 * the shared secret. A shared secret is permitted wherever a key
5665 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005666 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005667 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005668 shared_secret,
5669 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005670
Gilles Peskine12313cd2018-06-20 00:20:32 +02005671exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005672 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005673 return( status );
5674}
5675
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005676psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005677 psa_key_derivation_step_t step,
5678 psa_key_handle_t private_key,
5679 const uint8_t *peer_key,
5680 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005681{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005682 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005683 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005684 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005685 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005686 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005687 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005688 if( status != PSA_SUCCESS )
5689 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005690 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005691 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005692 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005693 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005694 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005695 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005696}
5697
Gilles Peskinebe697d82019-05-16 18:00:41 +02005698psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5699 psa_key_handle_t private_key,
5700 const uint8_t *peer_key,
5701 size_t peer_key_length,
5702 uint8_t *output,
5703 size_t output_size,
5704 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005705{
5706 psa_key_slot_t *slot;
5707 psa_status_t status;
5708
5709 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5710 {
5711 status = PSA_ERROR_INVALID_ARGUMENT;
5712 goto exit;
5713 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005714 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005715 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005716 if( status != PSA_SUCCESS )
5717 goto exit;
5718
5719 status = psa_key_agreement_raw_internal( alg, slot,
5720 peer_key, peer_key_length,
5721 output, output_size,
5722 output_length );
5723
5724exit:
5725 if( status != PSA_SUCCESS )
5726 {
5727 /* If an error happens and is not handled properly, the output
5728 * may be used as a key to protect sensitive data. Arrange for such
5729 * a key to be random, which is likely to result in decryption or
5730 * verification errors. This is better than filling the buffer with
5731 * some constant data such as zeros, which would result in the data
5732 * being protected with a reproducible, easily knowable key.
5733 */
5734 psa_generate_random( output, output_size );
5735 *output_length = output_size;
5736 }
5737 return( status );
5738}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005739
5740
5741/****************************************************************/
5742/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005743/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005744
Gilles Peskine4c317f42018-07-12 01:24:09 +02005745psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005746 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005747{
Janos Follath24eed8d2019-11-22 13:21:35 +00005748 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005749 GUARD_MODULE_INITIALIZED;
5750
Gilles Peskinef181eca2019-08-07 13:49:00 +02005751 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5752 {
5753 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5754 output,
5755 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5756 if( ret != 0 )
5757 return( mbedtls_to_psa_error( ret ) );
5758 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5759 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5760 }
5761
Gilles Peskinee59236f2018-01-27 23:32:46 +01005762 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5763 return( mbedtls_to_psa_error( ret ) );
5764}
5765
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005766#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5767#include "mbedtls/entropy_poll.h"
5768
Gilles Peskine7228da22019-07-15 11:06:15 +02005769psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005770 size_t seed_size )
5771{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005772 if( global_data.initialized )
5773 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005774
5775 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5776 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5777 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5778 return( PSA_ERROR_INVALID_ARGUMENT );
5779
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005780 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005781}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005782#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005783
Gilles Peskinee56e8782019-04-26 17:34:02 +02005784#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5785static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5786 size_t domain_parameters_size,
5787 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005788{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005789 size_t i;
5790 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005791
Gilles Peskinee56e8782019-04-26 17:34:02 +02005792 if( domain_parameters_size == 0 )
5793 {
5794 *exponent = 65537;
5795 return( PSA_SUCCESS );
5796 }
5797
5798 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5799 * support values that fit in a 32-bit integer, which is larger than
5800 * int on just about every platform anyway. */
5801 if( domain_parameters_size > sizeof( acc ) )
5802 return( PSA_ERROR_NOT_SUPPORTED );
5803 for( i = 0; i < domain_parameters_size; i++ )
5804 acc = ( acc << 8 ) | domain_parameters[i];
5805 if( acc > INT_MAX )
5806 return( PSA_ERROR_NOT_SUPPORTED );
5807 *exponent = acc;
5808 return( PSA_SUCCESS );
5809}
5810#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5811
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005812static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005813 psa_key_slot_t *slot, size_t bits,
5814 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005815{
Gilles Peskine8e338702019-07-30 20:06:31 +02005816 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005817
Gilles Peskinee56e8782019-04-26 17:34:02 +02005818 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005819 return( PSA_ERROR_INVALID_ARGUMENT );
5820
Gilles Peskinee59236f2018-01-27 23:32:46 +01005821 if( key_type_is_raw_bytes( type ) )
5822 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005823 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005824
5825 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005826 if( status != PSA_SUCCESS )
5827 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005828
5829 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02005830 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
5831 if( status != PSA_SUCCESS )
5832 return status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005833
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005834 status = psa_generate_random( slot->data.key.data,
5835 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005836 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005837 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005838
5839 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005840#if defined(MBEDTLS_DES_C)
5841 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005842 psa_des_set_key_parity( slot->data.key.data,
5843 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005844#endif /* MBEDTLS_DES_C */
5845 }
5846 else
5847
5848#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005849 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005850 {
Steven Cooremana01795d2020-07-24 22:48:15 +02005851 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005852 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005853 int exponent;
5854 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005855 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5856 return( PSA_ERROR_NOT_SUPPORTED );
5857 /* Accept only byte-aligned keys, for the same reasons as
5858 * in psa_import_rsa_key(). */
5859 if( bits % 8 != 0 )
5860 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005861 status = psa_read_rsa_exponent( domain_parameters,
5862 domain_parameters_size,
5863 &exponent );
5864 if( status != PSA_SUCCESS )
5865 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02005866 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5867 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005868 mbedtls_ctr_drbg_random,
5869 &global_data.ctr_drbg,
5870 (unsigned int) bits,
5871 exponent );
5872 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005873 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02005874
5875 /* Make sure to always have an export representation available */
5876 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
5877
Steven Cooreman75b74362020-07-28 14:30:13 +02005878 status = psa_allocate_buffer_to_slot( slot, bytes );
5879 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02005880 {
5881 mbedtls_rsa_free( &rsa );
Steven Cooreman75b74362020-07-28 14:30:13 +02005882 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005883 }
Steven Cooremana01795d2020-07-24 22:48:15 +02005884
5885 status = psa_export_rsa_key( type,
5886 &rsa,
5887 slot->data.key.data,
5888 bytes,
5889 &slot->data.key.bytes );
5890 mbedtls_rsa_free( &rsa );
5891 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02005892 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02005893 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005894 }
5895 else
5896#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5897
5898#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005899 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005900 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01005901 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005902 mbedtls_ecp_group_id grp_id =
5903 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005904 const mbedtls_ecp_curve_info *curve_info =
5905 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02005906 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005907 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005908 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005909 return( PSA_ERROR_NOT_SUPPORTED );
5910 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5911 return( PSA_ERROR_NOT_SUPPORTED );
5912 if( curve_info->bit_size != bits )
5913 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +02005914 mbedtls_ecp_keypair_init( &ecp );
5915 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005916 mbedtls_ctr_drbg_random,
5917 &global_data.ctr_drbg );
5918 if( ret != 0 )
5919 {
Steven Cooremanacda8342020-07-24 23:09:52 +02005920 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005921 return( mbedtls_to_psa_error( ret ) );
5922 }
Steven Cooremanacda8342020-07-24 23:09:52 +02005923
5924
5925 /* Make sure to always have an export representation available */
5926 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02005927 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
5928 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02005929 {
5930 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman75b74362020-07-28 14:30:13 +02005931 return status;
Steven Cooremanacda8342020-07-24 23:09:52 +02005932 }
Steven Cooreman75b74362020-07-28 14:30:13 +02005933
5934 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02005935 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
5936
5937 mbedtls_ecp_keypair_free( &ecp );
5938 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02005939 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02005940 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005941 }
5942 else
5943#endif /* MBEDTLS_ECP_C */
5944
5945 return( PSA_ERROR_NOT_SUPPORTED );
5946
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005947 return( PSA_SUCCESS );
5948}
Darryl Green0c6575a2018-11-07 16:05:30 +00005949
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005950psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005951 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005952{
5953 psa_status_t status;
5954 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005955 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005956
Gilles Peskine0f84d622019-09-12 19:03:13 +02005957 /* Reject any attempt to create a zero-length key so that we don't
5958 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5959 if( psa_get_key_bits( attributes ) == 0 )
5960 return( PSA_ERROR_INVALID_ARGUMENT );
5961
Gilles Peskinedf179142019-07-15 22:02:14 +02005962 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5963 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005964 if( status != PSA_SUCCESS )
5965 goto exit;
5966
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005967#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5968 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005969 {
Gilles Peskine11792082019-08-06 18:36:36 +02005970 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5971 size_t pubkey_length = 0; /* We don't support this feature yet */
5972 if( drv->key_management == NULL ||
5973 drv->key_management->p_generate == NULL )
5974 {
5975 status = PSA_ERROR_NOT_SUPPORTED;
5976 goto exit;
5977 }
5978 status = drv->key_management->p_generate(
5979 psa_get_se_driver_context( driver ),
5980 slot->data.se.slot_number, attributes,
5981 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005982 }
Gilles Peskine11792082019-08-06 18:36:36 +02005983 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005984#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005985 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005986 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005987 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005988 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005989 }
Gilles Peskine11792082019-08-06 18:36:36 +02005990
5991exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005992 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005993 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005994 if( status != PSA_SUCCESS )
5995 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005996 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005997 *handle = 0;
5998 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005999 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006000}
6001
6002
Gilles Peskinee59236f2018-01-27 23:32:46 +01006003
6004/****************************************************************/
6005/* Module setup */
6006/****************************************************************/
6007
Gilles Peskine5e769522018-11-20 21:59:56 +01006008psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6009 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6010 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6011{
6012 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6013 return( PSA_ERROR_BAD_STATE );
6014 global_data.entropy_init = entropy_init;
6015 global_data.entropy_free = entropy_free;
6016 return( PSA_SUCCESS );
6017}
6018
Gilles Peskinee59236f2018-01-27 23:32:46 +01006019void mbedtls_psa_crypto_free( void )
6020{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006021 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006022 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6023 {
6024 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006025 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006026 }
6027 /* Wipe all remaining data, including configuration.
6028 * In particular, this sets all state indicator to the value
6029 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006030 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006031#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006032 /* Unregister all secure element drivers, so that we restart from
6033 * a pristine state. */
6034 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006035#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006036}
6037
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006038#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6039/** Recover a transaction that was interrupted by a power failure.
6040 *
6041 * This function is called during initialization, before psa_crypto_init()
6042 * returns. If this function returns a failure status, the initialization
6043 * fails.
6044 */
6045static psa_status_t psa_crypto_recover_transaction(
6046 const psa_crypto_transaction_t *transaction )
6047{
6048 switch( transaction->unknown.type )
6049 {
6050 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6051 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006052 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006053 * is implemented.
6054 * https://github.com/ARMmbed/mbed-crypto/issues/218
6055 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006056 default:
6057 /* We found an unsupported transaction in the storage.
6058 * We don't know what state the storage is in. Give up. */
6059 return( PSA_ERROR_STORAGE_FAILURE );
6060 }
6061}
6062#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6063
Gilles Peskinee59236f2018-01-27 23:32:46 +01006064psa_status_t psa_crypto_init( void )
6065{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006066 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006067 const unsigned char drbg_seed[] = "PSA";
6068
Gilles Peskinec6b69072018-11-20 21:42:52 +01006069 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006070 if( global_data.initialized != 0 )
6071 return( PSA_SUCCESS );
6072
Gilles Peskine5e769522018-11-20 21:59:56 +01006073 /* Set default configuration if
6074 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6075 if( global_data.entropy_init == NULL )
6076 global_data.entropy_init = mbedtls_entropy_init;
6077 if( global_data.entropy_free == NULL )
6078 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006079
Gilles Peskinec6b69072018-11-20 21:42:52 +01006080 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006081 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006082#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6083 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6084 /* The PSA entropy injection feature depends on using NV seed as an entropy
6085 * source. Add NV seed as an entropy source for PSA entropy injection. */
6086 mbedtls_entropy_add_source( &global_data.entropy,
6087 mbedtls_nv_seed_poll, NULL,
6088 MBEDTLS_ENTROPY_BLOCK_SIZE,
6089 MBEDTLS_ENTROPY_SOURCE_STRONG );
6090#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006091 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006092 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006093 status = mbedtls_to_psa_error(
6094 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6095 mbedtls_entropy_func,
6096 &global_data.entropy,
6097 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6098 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006099 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006100 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006101
Gilles Peskine66fb1262018-12-10 16:29:04 +01006102 status = psa_initialize_key_slots( );
6103 if( status != PSA_SUCCESS )
6104 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006105
Gilles Peskined9348f22019-10-01 15:22:29 +02006106#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6107 status = psa_init_all_se_drivers( );
6108 if( status != PSA_SUCCESS )
6109 goto exit;
6110#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6111
Gilles Peskine4b734222019-07-24 15:56:31 +02006112#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006113 status = psa_crypto_load_transaction( );
6114 if( status == PSA_SUCCESS )
6115 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006116 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6117 if( status != PSA_SUCCESS )
6118 goto exit;
6119 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006120 }
6121 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6122 {
6123 /* There's no transaction to complete. It's all good. */
6124 status = PSA_SUCCESS;
6125 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006126#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006127
Gilles Peskinec6b69072018-11-20 21:42:52 +01006128 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006129 global_data.initialized = 1;
6130
6131exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006132 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006133 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006134 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006135}
6136
6137#endif /* MBEDTLS_PSA_CRYPTO_C */