blob: 15a612b680154004145a78ba9b53d556d99a2673 [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 );
694 if( rsa != NULL )
695 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200696
697 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000698 if( status != PSA_SUCCESS )
699 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200700 mbedtls_free( output );
701 slot->data.key.data = NULL;
702 slot->data.key.bytes = 0;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000703 return( status );
704 }
705
Steven Cooremana01795d2020-07-24 22:48:15 +0200706 /* On success, store the allocated export-formatted key. */
707 slot->data.key.data = output;
708 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000709
710 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200711}
Steven Cooremana01795d2020-07-24 22:48:15 +0200712#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200713
Jaeden Ameroccdce902019-01-10 11:42:27 +0000714#if defined(MBEDTLS_ECP_C)
Steven Cooremana2371e52020-07-28 14:30:39 +0200715/** Load the contents of a key slot into an internal ECP representation
716 *
717 * \param[in] slot The slot from which to load the representation
718 * \param[out] p_ecp Returns a pointer to an ECP context on success.
719 * The caller is responsible for freeing both the
720 * contents of the context and the context itself
721 * when done.
722 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200723static psa_status_t psa_load_ecp_representation( const psa_key_slot_t *slot,
Steven Cooremana2371e52020-07-28 14:30:39 +0200724 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100725{
726 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200727 size_t data_length = slot->data.key.bytes;
728 psa_status_t status;
Steven Cooremana2371e52020-07-28 14:30:39 +0200729 mbedtls_ecp_keypair *ecp = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
730
731 if( ecp == NULL )
732 return PSA_ERROR_INSUFFICIENT_MEMORY;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100733
Steven Cooremanacda8342020-07-24 23:09:52 +0200734 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100735 {
736 /* A public key is represented as:
737 * - The byte 0x04;
738 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
739 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
740 * So its data length is 2m+1 where n is the key size in bits.
741 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200742 if( ( slot->data.key.bytes & 1 ) == 0 )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100743 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +0200744 data_length = slot->data.key.bytes / 2;
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100745 }
746
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 Cooremanacda8342020-07-24 23:09:52 +0200750 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type),
751 data_length );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100752 if( grp_id == MBEDTLS_ECP_DP_NONE )
753 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +0200754 status = mbedtls_to_psa_error(
755 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
756 if( status != PSA_SUCCESS )
757 return( status );
758
759 /* Load the key material */
760 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
761 {
762 /* Load the public value. */
763 status = mbedtls_to_psa_error(
764 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
765 slot->data.key.data,
766 slot->data.key.bytes ) );
767 if( status != PSA_SUCCESS )
768 goto exit;
769
770 /* Check that the point is on the curve. */
771 status = mbedtls_to_psa_error(
772 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
773 if( status != PSA_SUCCESS )
774 goto exit;
775 }
776 else
777 {
778 /* Load the secret value. */
779 status = mbedtls_to_psa_error(
780 mbedtls_ecp_read_key( ecp->grp.id,
781 ecp,
782 slot->data.key.data,
783 slot->data.key.bytes ) );
784
785 if( status != PSA_SUCCESS )
786 goto exit;
787 /* Validate the private key. */
788 status = mbedtls_to_psa_error(
789 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
790 if( status != PSA_SUCCESS )
791 goto exit;
792 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200793
794 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200795exit:
796 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200797 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200798 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200799 mbedtls_free( ecp );
800 }
801
Steven Cooremanacda8342020-07-24 23:09:52 +0200802 return status;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100803}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000804
Steven Cooremanacda8342020-07-24 23:09:52 +0200805static psa_status_t psa_export_ecp_key( psa_key_type_t type,
806 mbedtls_ecp_keypair *ecp,
807 uint8_t *data,
808 size_t data_size,
809 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200810{
Steven Cooremanacda8342020-07-24 23:09:52 +0200811 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000812
Steven Cooremanacda8342020-07-24 23:09:52 +0200813 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200814 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200815 /* Check whether the public part is loaded */
816 if( mbedtls_ecp_is_zero( &ecp->Q ) )
817 {
818 /* Calculate the public key */
819 status = mbedtls_to_psa_error(
820 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
821 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
822 if( status != PSA_SUCCESS )
823 return status;
824 }
825
826 return( mbedtls_to_psa_error(
827 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
828 MBEDTLS_ECP_PF_UNCOMPRESSED,
829 data_length,
830 data,
831 data_size ) ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200832 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200833 else
834 {
835 if( data_size < PSA_BITS_TO_BYTES(ecp->grp.nbits) )
836 return( PSA_ERROR_BUFFER_TOO_SMALL );
837
838 status = mbedtls_to_psa_error(
839 mbedtls_ecp_write_key( ecp,
840 data,
841 PSA_BITS_TO_BYTES(ecp->grp.nbits) ) );
842 if( status == PSA_SUCCESS )
843 {
844 *data_length = PSA_BITS_TO_BYTES(ecp->grp.nbits);
845 }
846
847 return( status );
848 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200849}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200850
Steven Cooremanacda8342020-07-24 23:09:52 +0200851static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
852 const uint8_t *data,
853 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100854{
Steven Cooremanacda8342020-07-24 23:09:52 +0200855 psa_status_t status;
856 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200857 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100858
Steven Cooremanacda8342020-07-24 23:09:52 +0200859 /* Temporarily load input into slot. The cast here is safe since it'll
860 * only be used for load_ecp_representation, which doesn't modify the
861 * buffer. */
862 slot->data.key.data = (uint8_t *)data;
863 slot->data.key.bytes = data_length;
864
865 /* Parse input */
866 status = psa_load_ecp_representation( slot, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100867 if( status != PSA_SUCCESS )
868 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100869
Steven Cooremanacda8342020-07-24 23:09:52 +0200870 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200871 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200872 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200873 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200874
Steven Cooremanacda8342020-07-24 23:09:52 +0200875 /* Re-export the data to PSA export format. There is currently no support
876 * for other input formats then the export format, so this is a 1-1
877 * copy operation. */
878 output = mbedtls_calloc( 1, data_length );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100879
Steven Cooremanacda8342020-07-24 23:09:52 +0200880 if( output == NULL )
881 {
882 status = PSA_ERROR_INSUFFICIENT_MEMORY;
883 goto exit;
884 }
885
886 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200887 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200888 output,
889 data_length,
890 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100891
892exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200893 /* Always free the PK object (will also free contained ECP context) */
894 mbedtls_ecp_keypair_free( ecp );
895 if( ecp != NULL )
896 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200897
898 /* Free the allocated buffer only on error. */
899 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100900 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200901 mbedtls_free( output );
902 slot->data.key.data = NULL;
903 slot->data.key.bytes = 0;
904 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100905 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200906
907 /* On success, store the allocated export-formatted key. */
908 slot->data.key.data = output;
909 slot->data.key.bytes = data_length;
910
911 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100912}
913#endif /* defined(MBEDTLS_ECP_C) */
914
Gilles Peskineb46bef22019-07-30 21:32:04 +0200915/** Return the size of the key in the given slot, in bits.
916 *
917 * \param[in] slot A key slot.
918 *
919 * \return The key size in bits, read from the metadata in the slot.
920 */
921static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
922{
923 return( slot->attr.bits );
924}
925
Steven Cooreman75b74362020-07-28 14:30:13 +0200926/** Try to allocate a buffer to an empty key slot.
927 *
928 * \param[in,out] slot Key slot to attach buffer to.
929 * \param[in] buffer_length Requested size of the buffer.
930 *
931 * \retval #PSA_SUCCESS
932 * The buffer has been successfully allocated.
933 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
934 * Not enough memory was available for allocation.
935 * \retval #PSA_ERROR_ALREADY_EXISTS
936 * Trying to allocate a buffer to a non-empty key slot.
937 */
938static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
939 size_t buffer_length )
940{
941 if( slot->data.key.data != NULL )
942 return PSA_ERROR_ALREADY_EXISTS;
943
944 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
945 if( slot->data.key.data == NULL )
946 return PSA_ERROR_INSUFFICIENT_MEMORY;
947
948 slot->data.key.bytes = buffer_length;
949 return PSA_SUCCESS;
950}
951
Gilles Peskine8e338702019-07-30 20:06:31 +0200952/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100953 * previously. This function assumes that the slot does not contain
954 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100955psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
956 const uint8_t *data,
957 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100958{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200959 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100960
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200961 /* zero-length keys are never supported. */
962 if( data_length == 0 )
963 return( PSA_ERROR_NOT_SUPPORTED );
964
Gilles Peskine8e338702019-07-30 20:06:31 +0200965 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100966 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200967 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200968
Steven Cooreman75b74362020-07-28 14:30:13 +0200969 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
970 if( data_length > SIZE_MAX / 8 )
971 return( PSA_ERROR_NOT_SUPPORTED );
972
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200973 /* Enforce a size limit, and in particular ensure that the bit
974 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200975 if( bit_size > PSA_MAX_KEY_BITS )
976 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200977
978 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200979 if( status != PSA_SUCCESS )
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200980 return status;
981
982 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +0200983 status = psa_allocate_buffer_to_slot( slot, data_length );
984 if( status != PSA_SUCCESS )
985 return status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200986
987 /* copy key into allocated buffer */
988 memcpy(slot->data.key.data, data, data_length);
989
990 /* Write the actual key size to the slot.
991 * psa_start_key_creation() wrote the size declared by the
992 * caller, which may be 0 (meaning unspecified) or wrong. */
993 slot->attr.bits = (psa_key_bits_t) bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100994 }
Steven Cooreman19fd5742020-07-24 23:31:01 +0200995 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
996 {
Gilles Peskinef76aa772018-10-29 19:24:33 +0100997#if defined(MBEDTLS_ECP_C)
Steven Cooreman19fd5742020-07-24 23:31:01 +0200998 status = psa_import_ecp_key( slot,
999 data, data_length );
1000#else
1001 /* No drivers have been implemented yet, so without mbed TLS backing
1002 * there's no way to do ECP with the current library. */
1003 return( PSA_ERROR_NOT_SUPPORTED );
1004#endif /* defined(MBEDTLS_ECP_C) */
1005 }
1006 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +01001007 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001008#if defined(MBEDTLS_RSA_C)
1009 status = psa_import_rsa_key( slot,
1010 data, data_length );
1011#else
1012 /* No drivers have been implemented yet, so without mbed TLS backing
1013 * there's no way to do RSA with the current library. */
1014 status = PSA_ERROR_NOT_SUPPORTED;
1015#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001016 }
1017 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001018 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001019 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001020 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001021 }
Darryl Green940d72c2018-07-13 13:18:51 +01001022
Darryl Green06fd18d2018-07-16 11:21:11 +01001023 return( status );
1024}
1025
Gilles Peskinef603c712019-01-19 13:40:11 +01001026/** Calculate the intersection of two algorithm usage policies.
1027 *
1028 * Return 0 (which allows no operation) on incompatibility.
1029 */
1030static psa_algorithm_t psa_key_policy_algorithm_intersection(
1031 psa_algorithm_t alg1,
1032 psa_algorithm_t alg2 )
1033{
Gilles Peskine549ea862019-05-22 11:45:59 +02001034 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001035 if( alg1 == alg2 )
1036 return( alg1 );
1037 /* If the policies are from the same hash-and-sign family, check
1038 * if one is a wildcard. If so the other has the specific algorithm. */
1039 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1040 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1041 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1042 {
1043 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1044 return( alg2 );
1045 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1046 return( alg1 );
1047 }
1048 /* If the policies are incompatible, allow nothing. */
1049 return( 0 );
1050}
1051
Gilles Peskined6f371b2019-05-10 19:33:38 +02001052static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1053 psa_algorithm_t requested_alg )
1054{
Gilles Peskine549ea862019-05-22 11:45:59 +02001055 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001056 if( requested_alg == policy_alg )
1057 return( 1 );
1058 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001059 * and requested_alg is the same hash-and-sign family with any hash,
1060 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001061 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1062 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1063 {
1064 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1065 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1066 }
1067 /* If it isn't permitted, it's forbidden. */
1068 return( 0 );
1069}
1070
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001071/** Test whether a policy permits an algorithm.
1072 *
1073 * The caller must test usage flags separately.
1074 */
1075static int psa_key_policy_permits( const psa_key_policy_t *policy,
1076 psa_algorithm_t alg )
1077{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001078 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1079 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001080}
1081
Gilles Peskinef603c712019-01-19 13:40:11 +01001082/** Restrict a key policy based on a constraint.
1083 *
1084 * \param[in,out] policy The policy to restrict.
1085 * \param[in] constraint The policy constraint to apply.
1086 *
1087 * \retval #PSA_SUCCESS
1088 * \c *policy contains the intersection of the original value of
1089 * \c *policy and \c *constraint.
1090 * \retval #PSA_ERROR_INVALID_ARGUMENT
1091 * \c *policy and \c *constraint are incompatible.
1092 * \c *policy is unchanged.
1093 */
1094static psa_status_t psa_restrict_key_policy(
1095 psa_key_policy_t *policy,
1096 const psa_key_policy_t *constraint )
1097{
1098 psa_algorithm_t intersection_alg =
1099 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001100 psa_algorithm_t intersection_alg2 =
1101 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001102 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1103 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001104 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1105 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001106 policy->usage &= constraint->usage;
1107 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001108 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001109 return( PSA_SUCCESS );
1110}
1111
Darryl Green06fd18d2018-07-16 11:21:11 +01001112/** Retrieve a slot which must contain a key. The key must have allow all the
1113 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1114 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001115static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001116 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001117 psa_key_usage_t usage,
1118 psa_algorithm_t alg )
1119{
1120 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001121 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001122
1123 *p_slot = NULL;
1124
Gilles Peskinec5487a82018-12-03 18:08:14 +01001125 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001126 if( status != PSA_SUCCESS )
1127 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001128
1129 /* Enforce that usage policy for the key slot contains all the flags
1130 * required by the usage parameter. There is one exception: public
1131 * keys can always be exported, so we treat public key objects as
1132 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001133 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001134 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001135 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001136 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001137
1138 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001139 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001140 return( PSA_ERROR_NOT_PERMITTED );
1141
1142 *p_slot = slot;
1143 return( PSA_SUCCESS );
1144}
Darryl Green940d72c2018-07-13 13:18:51 +01001145
Gilles Peskine28f8f302019-07-24 13:30:31 +02001146/** Retrieve a slot which must contain a transparent key.
1147 *
1148 * A transparent key is a key for which the key material is directly
1149 * available, as opposed to a key in a secure element.
1150 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001151 * This is a temporary function to use instead of psa_get_key_from_slot()
1152 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001153 */
1154#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1155static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1156 psa_key_slot_t **p_slot,
1157 psa_key_usage_t usage,
1158 psa_algorithm_t alg )
1159{
1160 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1161 if( status != PSA_SUCCESS )
1162 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001163 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001164 {
1165 *p_slot = NULL;
1166 return( PSA_ERROR_NOT_SUPPORTED );
1167 }
1168 return( PSA_SUCCESS );
1169}
1170#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1171/* With no secure element support, all keys are transparent. */
1172#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1173 psa_get_key_from_slot( handle, p_slot, usage, alg )
1174#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1175
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001176/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001177static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001178{
Steven Cooreman75b74362020-07-28 14:30:13 +02001179 /* Check whether key is already clean */
1180 if( slot->data.key.data == NULL )
1181 return PSA_SUCCESS;
1182
Gilles Peskine73167e12019-07-12 23:44:37 +02001183#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1184 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001185 {
1186 /* No key material to clean. */
1187 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001188 else
1189#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +02001190 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +00001191 {
1192 /* No key material to clean. */
1193 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001194 else if( key_type_is_raw_bytes( slot->attr.type ) ||
1195 PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1196 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001197 {
Steven Cooremana01795d2020-07-24 22:48:15 +02001198 mbedtls_free( slot->data.key.data );
1199 slot->data.key.data = NULL;
1200 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001201 }
1202 else
Darryl Green40225ba2018-11-15 14:48:15 +00001203 {
1204 /* Shouldn't happen: the key type is not any type that we
1205 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001206 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +00001207 }
1208
1209 return( PSA_SUCCESS );
1210}
1211
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001212/** Completely wipe a slot in memory, including its policy.
1213 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001214psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001215{
1216 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001217 /* Multipart operations may still be using the key. This is safe
1218 * because all multipart operation objects are independent from
1219 * the key slot: if they need to access the key after the setup
1220 * phase, they have a copy of the key. Note that this means that
1221 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001222 /* At this point, key material and other type-specific content has
1223 * been wiped. Clear remaining metadata. We can call memset and not
1224 * zeroize because the metadata is not particularly sensitive. */
1225 memset( slot, 0, sizeof( *slot ) );
1226 return( status );
1227}
1228
Gilles Peskinec5487a82018-12-03 18:08:14 +01001229psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001230{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001231 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001232 psa_status_t status; /* status of the last operation */
1233 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001234#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1235 psa_se_drv_table_entry_t *driver;
1236#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001237
Gilles Peskine1841cf42019-10-08 15:48:25 +02001238 if( handle == 0 )
1239 return( PSA_SUCCESS );
1240
Gilles Peskinec5487a82018-12-03 18:08:14 +01001241 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001242 if( status != PSA_SUCCESS )
1243 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001244
1245#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001246 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001247 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001248 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001249 /* For a key in a secure element, we need to do three things:
1250 * remove the key file in internal storage, destroy the
1251 * key inside the secure element, and update the driver's
1252 * persistent data. Start a transaction that will encompass these
1253 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001254 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001255 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001256 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001257 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001258 status = psa_crypto_save_transaction( );
1259 if( status != PSA_SUCCESS )
1260 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001261 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001262 /* We should still try to destroy the key in the secure
1263 * element and the key metadata in storage. This is especially
1264 * important if the error is that the storage is full.
1265 * But how to do it exactly without risking an inconsistent
1266 * state after a reset?
1267 * https://github.com/ARMmbed/mbed-crypto/issues/215
1268 */
1269 overall_status = status;
1270 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001271 }
1272
Gilles Peskine354f7672019-07-12 23:46:38 +02001273 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001274 if( overall_status == PSA_SUCCESS )
1275 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001276 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001277#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1278
Darryl Greend49a4992018-06-18 17:27:26 +01001279#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001280 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001281 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001282 status = psa_destroy_persistent_key( slot->attr.id );
1283 if( overall_status == PSA_SUCCESS )
1284 overall_status = status;
1285
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001286 /* TODO: other slots may have a copy of the same key. We should
1287 * invalidate them.
1288 * https://github.com/ARMmbed/mbed-crypto/issues/214
1289 */
Darryl Greend49a4992018-06-18 17:27:26 +01001290 }
1291#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001292
Gilles Peskinefc762652019-07-22 19:30:34 +02001293#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1294 if( driver != NULL )
1295 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001296 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001297 if( overall_status == PSA_SUCCESS )
1298 overall_status = status;
1299 status = psa_crypto_stop_transaction( );
1300 if( overall_status == PSA_SUCCESS )
1301 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001302 }
1303#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1304
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001305#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1306exit:
1307#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001308 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001309 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1310 if( overall_status == PSA_SUCCESS )
1311 overall_status = status;
1312 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001313}
1314
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001315void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001316{
Gilles Peskineb699f072019-04-26 16:06:02 +02001317 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001318 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001319}
1320
Gilles Peskineb699f072019-04-26 16:06:02 +02001321psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1322 psa_key_type_t type,
1323 const uint8_t *data,
1324 size_t data_length )
1325{
1326 uint8_t *copy = NULL;
1327
1328 if( data_length != 0 )
1329 {
1330 copy = mbedtls_calloc( 1, data_length );
1331 if( copy == NULL )
1332 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1333 memcpy( copy, data, data_length );
1334 }
1335 /* After this point, this function is guaranteed to succeed, so it
1336 * can start modifying `*attributes`. */
1337
1338 if( attributes->domain_parameters != NULL )
1339 {
1340 mbedtls_free( attributes->domain_parameters );
1341 attributes->domain_parameters = NULL;
1342 attributes->domain_parameters_size = 0;
1343 }
1344
1345 attributes->domain_parameters = copy;
1346 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001347 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001348 return( PSA_SUCCESS );
1349}
1350
1351psa_status_t psa_get_key_domain_parameters(
1352 const psa_key_attributes_t *attributes,
1353 uint8_t *data, size_t data_size, size_t *data_length )
1354{
1355 if( attributes->domain_parameters_size > data_size )
1356 return( PSA_ERROR_BUFFER_TOO_SMALL );
1357 *data_length = attributes->domain_parameters_size;
1358 if( attributes->domain_parameters_size != 0 )
1359 memcpy( data, attributes->domain_parameters,
1360 attributes->domain_parameters_size );
1361 return( PSA_SUCCESS );
1362}
1363
1364#if defined(MBEDTLS_RSA_C)
1365static psa_status_t psa_get_rsa_public_exponent(
1366 const mbedtls_rsa_context *rsa,
1367 psa_key_attributes_t *attributes )
1368{
1369 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001370 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001371 uint8_t *buffer = NULL;
1372 size_t buflen;
1373 mbedtls_mpi_init( &mpi );
1374
1375 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1376 if( ret != 0 )
1377 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001378 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1379 {
1380 /* It's the default value, which is reported as an empty string,
1381 * so there's nothing to do. */
1382 goto exit;
1383 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001384
1385 buflen = mbedtls_mpi_size( &mpi );
1386 buffer = mbedtls_calloc( 1, buflen );
1387 if( buffer == NULL )
1388 {
1389 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1390 goto exit;
1391 }
1392 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1393 if( ret != 0 )
1394 goto exit;
1395 attributes->domain_parameters = buffer;
1396 attributes->domain_parameters_size = buflen;
1397
1398exit:
1399 mbedtls_mpi_free( &mpi );
1400 if( ret != 0 )
1401 mbedtls_free( buffer );
1402 return( mbedtls_to_psa_error( ret ) );
1403}
1404#endif /* MBEDTLS_RSA_C */
1405
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001406/** Retrieve all the publicly-accessible attributes of a key.
1407 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001408psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1409 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001410{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001411 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001412 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001413
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001414 psa_reset_key_attributes( attributes );
1415
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001416 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001417 if( status != PSA_SUCCESS )
1418 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001419
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001420 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001421 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1422 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1423
1424#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1425 if( psa_key_slot_is_external( slot ) )
1426 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1427#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001428
Gilles Peskine8e338702019-07-30 20:06:31 +02001429 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001430 {
1431#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001432 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001433 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001434#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001435 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001436 * is not yet implemented.
1437 * https://github.com/ARMmbed/mbed-crypto/issues/216
1438 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001439 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001440 break;
1441#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001442 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001443 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001444
1445 status = psa_load_rsa_representation( slot, &rsa );
1446 if( status != PSA_SUCCESS )
1447 break;
1448
Steven Cooremana2371e52020-07-28 14:30:39 +02001449 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001450 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001451 mbedtls_rsa_free( rsa );
1452 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001453 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001454 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001455#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001456 default:
1457 /* Nothing else to do. */
1458 break;
1459 }
1460
1461 if( status != PSA_SUCCESS )
1462 psa_reset_key_attributes( attributes );
1463 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001464}
1465
Gilles Peskinec8000c02019-08-02 20:15:51 +02001466#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1467psa_status_t psa_get_key_slot_number(
1468 const psa_key_attributes_t *attributes,
1469 psa_key_slot_number_t *slot_number )
1470{
1471 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1472 {
1473 *slot_number = attributes->slot_number;
1474 return( PSA_SUCCESS );
1475 }
1476 else
1477 return( PSA_ERROR_INVALID_ARGUMENT );
1478}
1479#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1480
Steven Cooremana01795d2020-07-24 22:48:15 +02001481static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1482 uint8_t *data,
1483 size_t data_size,
1484 size_t *data_length )
1485{
1486 if( slot->data.key.bytes > data_size )
1487 return( PSA_ERROR_BUFFER_TOO_SMALL );
1488 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1489 memset( data + slot->data.key.bytes, 0,
1490 data_size - slot->data.key.bytes );
1491 *data_length = slot->data.key.bytes;
1492 return( PSA_SUCCESS );
1493}
1494
Gilles Peskinef603c712019-01-19 13:40:11 +01001495static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1496 uint8_t *data,
1497 size_t data_size,
1498 size_t *data_length,
1499 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001500{
Gilles Peskine5d309672019-07-12 23:47:28 +02001501#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1502 const psa_drv_se_t *drv;
1503 psa_drv_se_context_t *drv_context;
1504#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1505
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001506 *data_length = 0;
1507
Gilles Peskine8e338702019-07-30 20:06:31 +02001508 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001509 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001510
Gilles Peskinef9168942019-09-12 19:20:29 +02001511 /* Reject a zero-length output buffer now, since this can never be a
1512 * valid key representation. This way we know that data must be a valid
1513 * pointer and we can do things like memset(data, ..., data_size). */
1514 if( data_size == 0 )
1515 return( PSA_ERROR_BUFFER_TOO_SMALL );
1516
Gilles Peskine5d309672019-07-12 23:47:28 +02001517#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001518 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001519 {
1520 psa_drv_se_export_key_t method;
1521 if( drv->key_management == NULL )
1522 return( PSA_ERROR_NOT_SUPPORTED );
1523 method = ( export_public_key ?
1524 drv->key_management->p_export_public :
1525 drv->key_management->p_export );
1526 if( method == NULL )
1527 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001528 return( method( drv_context,
1529 slot->data.se.slot_number,
1530 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001531 }
1532#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1533
Gilles Peskine8e338702019-07-30 20:06:31 +02001534 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001535 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001536 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001537 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001538 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1539 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001540 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001541 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001542 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001543 /* Exporting public -> public */
1544 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1545 }
1546 else if( !export_public_key )
1547 {
1548 /* Exporting private -> private */
1549 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1550 }
1551 /* Need to export the public part of a private key,
1552 * so conversion is needed */
1553 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1554 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001555#if defined(MBEDTLS_RSA_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001556 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001557 psa_status_t status = psa_load_rsa_representation( slot, &rsa );
1558 if( status != PSA_SUCCESS )
1559 return status;
Steven Cooremana01795d2020-07-24 22:48:15 +02001560
Steven Cooreman560c28a2020-07-24 23:20:24 +02001561 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001562 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001563 data,
1564 data_size,
1565 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001566
Steven Cooremana2371e52020-07-28 14:30:39 +02001567 mbedtls_rsa_free( rsa );
1568 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001569
Steven Cooreman560c28a2020-07-24 23:20:24 +02001570 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001571#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001572 /* We don't know how to convert a private RSA key to public. */
1573 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001574#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001575 }
1576 else
Moran Pekera998bc62018-04-16 18:16:20 +03001577 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001578#if defined(MBEDTLS_ECP_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001579 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman560c28a2020-07-24 23:20:24 +02001580 psa_status_t status = psa_load_ecp_representation( slot, &ecp );
1581 if( status != PSA_SUCCESS )
1582 return status;
1583
1584 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1585 PSA_KEY_TYPE_ECC_GET_FAMILY(
1586 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001587 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001588 data,
1589 data_size,
1590 data_length );
1591
Steven Cooremana2371e52020-07-28 14:30:39 +02001592 mbedtls_ecp_keypair_free( ecp );
1593 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001594 return( status );
1595#else
1596 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001597 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001598#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001599 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001600 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001601 else
1602 {
1603 /* This shouldn't happen in the reference implementation, but
1604 it is valid for a special-purpose implementation to omit
1605 support for exporting certain key types. */
1606 return( PSA_ERROR_NOT_SUPPORTED );
1607 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001608}
1609
Gilles Peskinec5487a82018-12-03 18:08:14 +01001610psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001611 uint8_t *data,
1612 size_t data_size,
1613 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001614{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001615 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001616 psa_status_t status;
1617
1618 /* Set the key to empty now, so that even when there are errors, we always
1619 * set data_length to a value between 0 and data_size. On error, setting
1620 * the key to empty is a good choice because an empty key representation is
1621 * unlikely to be accepted anywhere. */
1622 *data_length = 0;
1623
1624 /* Export requires the EXPORT flag. There is an exception for public keys,
1625 * which don't require any flag, but psa_get_key_from_slot takes
1626 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001627 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001628 if( status != PSA_SUCCESS )
1629 return( status );
1630 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001631 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001632}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001633
Gilles Peskinec5487a82018-12-03 18:08:14 +01001634psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001635 uint8_t *data,
1636 size_t data_size,
1637 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001638{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001639 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001640 psa_status_t status;
1641
1642 /* Set the key to empty now, so that even when there are errors, we always
1643 * set data_length to a value between 0 and data_size. On error, setting
1644 * the key to empty is a good choice because an empty key representation is
1645 * unlikely to be accepted anywhere. */
1646 *data_length = 0;
1647
1648 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001649 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001650 if( status != PSA_SUCCESS )
1651 return( status );
1652 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001653 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001654}
1655
Gilles Peskine91e8c332019-08-02 19:19:39 +02001656#if defined(static_assert)
1657static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1658 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001659static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001660 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001661static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001662 "One or more key attribute flag is listed as both internal-only and external-only" );
1663#endif
1664
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001665/** Validate that a key policy is internally well-formed.
1666 *
1667 * This function only rejects invalid policies. It does not validate the
1668 * consistency of the policy with respect to other attributes of the key
1669 * such as the key type.
1670 */
1671static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001672{
1673 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001674 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001675 PSA_KEY_USAGE_ENCRYPT |
1676 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001677 PSA_KEY_USAGE_SIGN_HASH |
1678 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001679 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1680 return( PSA_ERROR_INVALID_ARGUMENT );
1681
Gilles Peskine4747d192019-04-17 15:05:45 +02001682 return( PSA_SUCCESS );
1683}
1684
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001685/** Validate the internal consistency of key attributes.
1686 *
1687 * This function only rejects invalid attribute values. If does not
1688 * validate the consistency of the attributes with any key data that may
1689 * be involved in the creation of the key.
1690 *
1691 * Call this function early in the key creation process.
1692 *
1693 * \param[in] attributes Key attributes for the new key.
1694 * \param[out] p_drv On any return, the driver for the key, if any.
1695 * NULL for a transparent key.
1696 *
1697 */
1698static psa_status_t psa_validate_key_attributes(
1699 const psa_key_attributes_t *attributes,
1700 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001701{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001702 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001703
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001704 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1705 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001706 if( status != PSA_SUCCESS )
1707 return( status );
1708
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001709 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1710 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001711 if( status != PSA_SUCCESS )
1712 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001713
1714 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001715 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001716 return( status );
1717
1718 /* Refuse to create overly large keys.
1719 * Note that this doesn't trigger on import if the attributes don't
1720 * explicitly specify a size (so psa_get_key_bits returns 0), so
1721 * psa_import_key() needs its own checks. */
1722 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1723 return( PSA_ERROR_NOT_SUPPORTED );
1724
Gilles Peskine91e8c332019-08-02 19:19:39 +02001725 /* Reject invalid flags. These should not be reachable through the API. */
1726 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1727 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1728 return( PSA_ERROR_INVALID_ARGUMENT );
1729
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001730 return( PSA_SUCCESS );
1731}
1732
Gilles Peskine4747d192019-04-17 15:05:45 +02001733/** Prepare a key slot to receive key material.
1734 *
1735 * This function allocates a key slot and sets its metadata.
1736 *
1737 * If this function fails, call psa_fail_key_creation().
1738 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001739 * This function is intended to be used as follows:
1740 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1741 * it with the specified attributes, and assign it a handle.
1742 * -# Populate the slot with the key material.
1743 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1744 * In case of failure at any step, stop the sequence and call
1745 * psa_fail_key_creation().
1746 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001747 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001748 * \param[in] attributes Key attributes for the new key.
1749 * \param[out] handle On success, a handle for the allocated slot.
1750 * \param[out] p_slot On success, a pointer to the prepared slot.
1751 * \param[out] p_drv On any return, the driver for the key, if any.
1752 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001753 *
1754 * \retval #PSA_SUCCESS
1755 * The key slot is ready to receive key material.
1756 * \return If this function fails, the key slot is an invalid state.
1757 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001758 */
1759static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001760 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001761 const psa_key_attributes_t *attributes,
1762 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001763 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001764 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001765{
1766 psa_status_t status;
1767 psa_key_slot_t *slot;
1768
Gilles Peskinedf179142019-07-15 22:02:14 +02001769 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001770 *p_drv = NULL;
1771
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001772 status = psa_validate_key_attributes( attributes, p_drv );
1773 if( status != PSA_SUCCESS )
1774 return( status );
1775
Gilles Peskineedbed562019-08-07 18:19:59 +02001776 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001777 if( status != PSA_SUCCESS )
1778 return( status );
1779 slot = *p_slot;
1780
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001781 /* We're storing the declared bit-size of the key. It's up to each
1782 * creation mechanism to verify that this information is correct.
1783 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001784 * an input (generate, device) but not for those where the bit-size
1785 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001786
1787 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001788
Gilles Peskine91e8c332019-08-02 19:19:39 +02001789 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001790 * external-only flags, query `attributes`. Thanks to the check
1791 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001792 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001793 * may have set. */
1794 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001795
Gilles Peskinecbaff462019-07-12 23:46:04 +02001796#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001797 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001798 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001799 * create the key file in internal storage, create the
1800 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001801 * persistent data. This is done by starting a transaction that will
1802 * encompass these three actions.
1803 * For registering a volatile key, we just need to find an appropriate
1804 * slot number inside the SE. Since the key is designated volatile, creating
1805 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001806 /* The first thing to do is to find a slot number for the new key.
1807 * We save the slot number in persistent storage as part of the
1808 * transaction data. It will be needed to recover if the power
1809 * fails during the key creation process, to clean up on the secure
1810 * element side after restarting. Obtaining a slot number from the
1811 * secure element driver updates its persistent state, but we do not yet
1812 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001813 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001814 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001815 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001816 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001817 &slot->data.se.slot_number );
1818 if( status != PSA_SUCCESS )
1819 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001820
1821 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001822 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001823 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1824 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1825 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1826 psa_crypto_transaction.key.id = slot->attr.id;
1827 status = psa_crypto_save_transaction( );
1828 if( status != PSA_SUCCESS )
1829 {
1830 (void) psa_crypto_stop_transaction( );
1831 return( status );
1832 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001833 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001834 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001835
1836 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1837 {
1838 /* Key registration only makes sense with a secure element. */
1839 return( PSA_ERROR_INVALID_ARGUMENT );
1840 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001841#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1842
Darryl Green0c6575a2018-11-07 16:05:30 +00001843 return( status );
1844}
Gilles Peskine4747d192019-04-17 15:05:45 +02001845
1846/** Finalize the creation of a key once its key material has been set.
1847 *
1848 * This entails writing the key to persistent storage.
1849 *
1850 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001851 * See the documentation of psa_start_key_creation() for the intended use
1852 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001853 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001854 * \param[in,out] slot Pointer to the slot with key material.
1855 * \param[in] driver The secure element driver for the key,
1856 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001857 *
1858 * \retval #PSA_SUCCESS
1859 * The key was successfully created. The handle is now valid.
1860 * \return If this function fails, the key slot is an invalid state.
1861 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001862 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001863static psa_status_t psa_finish_key_creation(
1864 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001865 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001866{
1867 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001868 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001869 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001870
1871#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001872 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001873 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001874#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1875 if( driver != NULL )
1876 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001877 psa_se_key_data_storage_t data;
1878#if defined(static_assert)
1879 static_assert( sizeof( slot->data.se.slot_number ) ==
1880 sizeof( data.slot_number ),
1881 "Slot number size does not match psa_se_key_data_storage_t" );
1882 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1883 "Bit-size size does not match psa_se_key_data_storage_t" );
1884#endif
1885 memcpy( &data.slot_number, &slot->data.se.slot_number,
1886 sizeof( slot->data.se.slot_number ) );
1887 memcpy( &data.bits, &slot->attr.bits,
1888 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001889 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001890 (uint8_t*) &data,
1891 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001892 }
1893 else
1894#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1895 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001896 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001897 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001898 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001899 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1900 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001901 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001902 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1903 status = psa_internal_export_key( slot,
1904 buffer, buffer_size, &length,
1905 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001906 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001907 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001908 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001909
Gilles Peskinef9168942019-09-12 19:20:29 +02001910 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001911 mbedtls_free( buffer );
1912 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001913 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001914#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1915
Gilles Peskinecbaff462019-07-12 23:46:04 +02001916#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001917 /* Finish the transaction for a key creation. This does not
1918 * happen when registering an existing key. Detect this case
1919 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001920 * creation of a persistent key in a secure element requires a transaction,
1921 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001922 if( driver != NULL &&
1923 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001924 {
1925 status = psa_save_se_persistent_data( driver );
1926 if( status != PSA_SUCCESS )
1927 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001928 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001929 return( status );
1930 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001931 status = psa_crypto_stop_transaction( );
1932 if( status != PSA_SUCCESS )
1933 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001934 }
1935#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1936
Gilles Peskine4747d192019-04-17 15:05:45 +02001937 return( status );
1938}
1939
1940/** Abort the creation of a key.
1941 *
1942 * You may call this function after calling psa_start_key_creation(),
1943 * or after psa_finish_key_creation() fails. In other circumstances, this
1944 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001945 * See the documentation of psa_start_key_creation() for the intended use
1946 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001947 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001948 * \param[in,out] slot Pointer to the slot with key material.
1949 * \param[in] driver The secure element driver for the key,
1950 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001951 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001952static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001953 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001954{
Gilles Peskine011e4282019-06-26 18:34:38 +02001955 (void) driver;
1956
Gilles Peskine4747d192019-04-17 15:05:45 +02001957 if( slot == NULL )
1958 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001959
1960#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001961 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001962 * element, and the failure happened later (when saving metadata
1963 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001964 * element.
1965 * https://github.com/ARMmbed/mbed-crypto/issues/217
1966 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001967
Gilles Peskined7729582019-08-05 15:55:54 +02001968 /* Abort the ongoing transaction if any (there may not be one if
1969 * the creation process failed before starting one, or if the
1970 * key creation is a registration of a key in a secure element).
1971 * Earlier functions must already have done what it takes to undo any
1972 * partial creation. All that's left is to update the transaction data
1973 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001974 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001975#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1976
Gilles Peskine4747d192019-04-17 15:05:45 +02001977 psa_wipe_key_slot( slot );
1978}
1979
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001980/** Validate optional attributes during key creation.
1981 *
1982 * Some key attributes are optional during key creation. If they are
1983 * specified in the attributes structure, check that they are consistent
1984 * with the data in the slot.
1985 *
1986 * This function should be called near the end of key creation, after
1987 * the slot in memory is fully populated but before saving persistent data.
1988 */
1989static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001990 const psa_key_slot_t *slot,
1991 const psa_key_attributes_t *attributes )
1992{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001993 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001994 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001995 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001996 return( PSA_ERROR_INVALID_ARGUMENT );
1997 }
1998
1999 if( attributes->domain_parameters_size != 0 )
2000 {
2001#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02002002 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002003 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002004 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002005 mbedtls_mpi actual, required;
Steven Cooremana01795d2020-07-24 22:48:15 +02002006
2007 psa_status_t status = psa_load_rsa_representation( slot, &rsa );
2008 if( status != PSA_SUCCESS )
2009 return status;
Steven Cooreman75b74362020-07-28 14:30:13 +02002010
Janos Follath24eed8d2019-11-22 13:21:35 +00002011 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002012 mbedtls_mpi_init( &actual );
2013 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002014 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002015 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002016 mbedtls_rsa_free( rsa );
2017 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002018 if( ret != 0 )
2019 goto rsa_exit;
2020 ret = mbedtls_mpi_read_binary( &required,
2021 attributes->domain_parameters,
2022 attributes->domain_parameters_size );
2023 if( ret != 0 )
2024 goto rsa_exit;
2025 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2026 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2027 rsa_exit:
2028 mbedtls_mpi_free( &actual );
2029 mbedtls_mpi_free( &required );
2030 if( ret != 0)
2031 return( mbedtls_to_psa_error( ret ) );
2032 }
2033 else
2034#endif
2035 {
2036 return( PSA_ERROR_INVALID_ARGUMENT );
2037 }
2038 }
2039
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002040 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002041 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002042 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002043 return( PSA_ERROR_INVALID_ARGUMENT );
2044 }
2045
2046 return( PSA_SUCCESS );
2047}
2048
Gilles Peskine4747d192019-04-17 15:05:45 +02002049psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002050 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002051 size_t data_length,
2052 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002053{
2054 psa_status_t status;
2055 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002056 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002057
Gilles Peskine0f84d622019-09-12 19:03:13 +02002058 /* Reject zero-length symmetric keys (including raw data key objects).
2059 * This also rejects any key which might be encoded as an empty string,
2060 * which is never valid. */
2061 if( data_length == 0 )
2062 return( PSA_ERROR_INVALID_ARGUMENT );
2063
Gilles Peskinedf179142019-07-15 22:02:14 +02002064 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2065 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002066 if( status != PSA_SUCCESS )
2067 goto exit;
2068
Gilles Peskine5d309672019-07-12 23:47:28 +02002069#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2070 if( driver != NULL )
2071 {
2072 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002073 /* The driver should set the number of key bits, however in
2074 * case it doesn't, we initialize bits to an invalid value. */
2075 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002076 if( drv->key_management == NULL ||
2077 drv->key_management->p_import == NULL )
2078 {
2079 status = PSA_ERROR_NOT_SUPPORTED;
2080 goto exit;
2081 }
2082 status = drv->key_management->p_import(
2083 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002084 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002085 &bits );
2086 if( status != PSA_SUCCESS )
2087 goto exit;
2088 if( bits > PSA_MAX_KEY_BITS )
2089 {
2090 status = PSA_ERROR_NOT_SUPPORTED;
2091 goto exit;
2092 }
2093 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002094 }
2095 else
2096#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2097 {
2098 status = psa_import_key_into_slot( slot, data, data_length );
2099 if( status != PSA_SUCCESS )
2100 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002101 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002102 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002103 if( status != PSA_SUCCESS )
2104 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002105
Gilles Peskine011e4282019-06-26 18:34:38 +02002106 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002107exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002108 if( status != PSA_SUCCESS )
2109 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002110 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002111 *handle = 0;
2112 }
2113 return( status );
2114}
2115
Gilles Peskined7729582019-08-05 15:55:54 +02002116#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2117psa_status_t mbedtls_psa_register_se_key(
2118 const psa_key_attributes_t *attributes )
2119{
2120 psa_status_t status;
2121 psa_key_slot_t *slot = NULL;
2122 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002123 psa_key_handle_t handle = 0;
2124
2125 /* Leaving attributes unspecified is not currently supported.
2126 * It could make sense to query the key type and size from the
2127 * secure element, but not all secure elements support this
2128 * and the driver HAL doesn't currently support it. */
2129 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2130 return( PSA_ERROR_NOT_SUPPORTED );
2131 if( psa_get_key_bits( attributes ) == 0 )
2132 return( PSA_ERROR_NOT_SUPPORTED );
2133
2134 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2135 &handle, &slot, &driver );
2136 if( status != PSA_SUCCESS )
2137 goto exit;
2138
Gilles Peskined7729582019-08-05 15:55:54 +02002139 status = psa_finish_key_creation( slot, driver );
2140
2141exit:
2142 if( status != PSA_SUCCESS )
2143 {
2144 psa_fail_key_creation( slot, driver );
2145 }
2146 /* Registration doesn't keep the key in RAM. */
2147 psa_close_key( handle );
2148 return( status );
2149}
2150#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2151
Gilles Peskinef603c712019-01-19 13:40:11 +01002152static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002153 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002154{
2155 psa_status_t status;
2156 uint8_t *buffer = NULL;
2157 size_t buffer_size = 0;
2158 size_t length;
2159
Gilles Peskine8e338702019-07-30 20:06:31 +02002160 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002161 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01002162 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02002163 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01002164 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01002165 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
2166 if( status != PSA_SUCCESS )
2167 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02002168 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002169 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01002170
2171exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02002172 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01002173 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01002174 return( status );
2175}
2176
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002177psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2178 const psa_key_attributes_t *specified_attributes,
2179 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002180{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002181 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002182 psa_key_slot_t *source_slot = NULL;
2183 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002184 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002185 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002186
Gilles Peskine28f8f302019-07-24 13:30:31 +02002187 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002188 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002189 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002190 goto exit;
2191
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002192 status = psa_validate_optional_attributes( source_slot,
2193 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002194 if( status != PSA_SUCCESS )
2195 goto exit;
2196
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002197 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002198 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002199 if( status != PSA_SUCCESS )
2200 goto exit;
2201
Gilles Peskinedf179142019-07-15 22:02:14 +02002202 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2203 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002204 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002205 if( status != PSA_SUCCESS )
2206 goto exit;
2207
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002208#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2209 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002210 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002211 /* Copying to a secure element is not implemented yet. */
2212 status = PSA_ERROR_NOT_SUPPORTED;
2213 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002214 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002215#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002216
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002217 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002218 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002219 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002220
Gilles Peskine011e4282019-06-26 18:34:38 +02002221 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002222exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002223 if( status != PSA_SUCCESS )
2224 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002225 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002226 *target_handle = 0;
2227 }
2228 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002229}
2230
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002231
2232
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002233/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002234/* Message digests */
2235/****************************************************************/
2236
Gilles Peskineb16841e2019-10-10 20:36:12 +02002237#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002238static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002239{
2240 switch( alg )
2241 {
2242#if defined(MBEDTLS_MD2_C)
2243 case PSA_ALG_MD2:
2244 return( &mbedtls_md2_info );
2245#endif
2246#if defined(MBEDTLS_MD4_C)
2247 case PSA_ALG_MD4:
2248 return( &mbedtls_md4_info );
2249#endif
2250#if defined(MBEDTLS_MD5_C)
2251 case PSA_ALG_MD5:
2252 return( &mbedtls_md5_info );
2253#endif
2254#if defined(MBEDTLS_RIPEMD160_C)
2255 case PSA_ALG_RIPEMD160:
2256 return( &mbedtls_ripemd160_info );
2257#endif
2258#if defined(MBEDTLS_SHA1_C)
2259 case PSA_ALG_SHA_1:
2260 return( &mbedtls_sha1_info );
2261#endif
2262#if defined(MBEDTLS_SHA256_C)
2263 case PSA_ALG_SHA_224:
2264 return( &mbedtls_sha224_info );
2265 case PSA_ALG_SHA_256:
2266 return( &mbedtls_sha256_info );
2267#endif
2268#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002269#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002270 case PSA_ALG_SHA_384:
2271 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002272#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002273 case PSA_ALG_SHA_512:
2274 return( &mbedtls_sha512_info );
2275#endif
2276 default:
2277 return( NULL );
2278 }
2279}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002280#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002281
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002282psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2283{
2284 switch( operation->alg )
2285 {
Gilles Peskine81736312018-06-26 15:04:31 +02002286 case 0:
2287 /* The object has (apparently) been initialized but it is not
2288 * in use. It's ok to call abort on such an object, and there's
2289 * nothing to do. */
2290 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002291#if defined(MBEDTLS_MD2_C)
2292 case PSA_ALG_MD2:
2293 mbedtls_md2_free( &operation->ctx.md2 );
2294 break;
2295#endif
2296#if defined(MBEDTLS_MD4_C)
2297 case PSA_ALG_MD4:
2298 mbedtls_md4_free( &operation->ctx.md4 );
2299 break;
2300#endif
2301#if defined(MBEDTLS_MD5_C)
2302 case PSA_ALG_MD5:
2303 mbedtls_md5_free( &operation->ctx.md5 );
2304 break;
2305#endif
2306#if defined(MBEDTLS_RIPEMD160_C)
2307 case PSA_ALG_RIPEMD160:
2308 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2309 break;
2310#endif
2311#if defined(MBEDTLS_SHA1_C)
2312 case PSA_ALG_SHA_1:
2313 mbedtls_sha1_free( &operation->ctx.sha1 );
2314 break;
2315#endif
2316#if defined(MBEDTLS_SHA256_C)
2317 case PSA_ALG_SHA_224:
2318 case PSA_ALG_SHA_256:
2319 mbedtls_sha256_free( &operation->ctx.sha256 );
2320 break;
2321#endif
2322#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002323#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002324 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002325#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002326 case PSA_ALG_SHA_512:
2327 mbedtls_sha512_free( &operation->ctx.sha512 );
2328 break;
2329#endif
2330 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002331 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002332 }
2333 operation->alg = 0;
2334 return( PSA_SUCCESS );
2335}
2336
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002337psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002338 psa_algorithm_t alg )
2339{
Janos Follath24eed8d2019-11-22 13:21:35 +00002340 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002341
2342 /* A context must be freshly initialized before it can be set up. */
2343 if( operation->alg != 0 )
2344 {
2345 return( PSA_ERROR_BAD_STATE );
2346 }
2347
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002348 switch( alg )
2349 {
2350#if defined(MBEDTLS_MD2_C)
2351 case PSA_ALG_MD2:
2352 mbedtls_md2_init( &operation->ctx.md2 );
2353 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2354 break;
2355#endif
2356#if defined(MBEDTLS_MD4_C)
2357 case PSA_ALG_MD4:
2358 mbedtls_md4_init( &operation->ctx.md4 );
2359 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2360 break;
2361#endif
2362#if defined(MBEDTLS_MD5_C)
2363 case PSA_ALG_MD5:
2364 mbedtls_md5_init( &operation->ctx.md5 );
2365 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2366 break;
2367#endif
2368#if defined(MBEDTLS_RIPEMD160_C)
2369 case PSA_ALG_RIPEMD160:
2370 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2371 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2372 break;
2373#endif
2374#if defined(MBEDTLS_SHA1_C)
2375 case PSA_ALG_SHA_1:
2376 mbedtls_sha1_init( &operation->ctx.sha1 );
2377 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2378 break;
2379#endif
2380#if defined(MBEDTLS_SHA256_C)
2381 case PSA_ALG_SHA_224:
2382 mbedtls_sha256_init( &operation->ctx.sha256 );
2383 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2384 break;
2385 case PSA_ALG_SHA_256:
2386 mbedtls_sha256_init( &operation->ctx.sha256 );
2387 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2388 break;
2389#endif
2390#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002391#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002392 case PSA_ALG_SHA_384:
2393 mbedtls_sha512_init( &operation->ctx.sha512 );
2394 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2395 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002396#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002397 case PSA_ALG_SHA_512:
2398 mbedtls_sha512_init( &operation->ctx.sha512 );
2399 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2400 break;
2401#endif
2402 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002403 return( PSA_ALG_IS_HASH( alg ) ?
2404 PSA_ERROR_NOT_SUPPORTED :
2405 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002406 }
2407 if( ret == 0 )
2408 operation->alg = alg;
2409 else
2410 psa_hash_abort( operation );
2411 return( mbedtls_to_psa_error( ret ) );
2412}
2413
2414psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2415 const uint8_t *input,
2416 size_t input_length )
2417{
Janos Follath24eed8d2019-11-22 13:21:35 +00002418 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002419
2420 /* Don't require hash implementations to behave correctly on a
2421 * zero-length input, which may have an invalid pointer. */
2422 if( input_length == 0 )
2423 return( PSA_SUCCESS );
2424
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002425 switch( operation->alg )
2426 {
2427#if defined(MBEDTLS_MD2_C)
2428 case PSA_ALG_MD2:
2429 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2430 input, input_length );
2431 break;
2432#endif
2433#if defined(MBEDTLS_MD4_C)
2434 case PSA_ALG_MD4:
2435 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2436 input, input_length );
2437 break;
2438#endif
2439#if defined(MBEDTLS_MD5_C)
2440 case PSA_ALG_MD5:
2441 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2442 input, input_length );
2443 break;
2444#endif
2445#if defined(MBEDTLS_RIPEMD160_C)
2446 case PSA_ALG_RIPEMD160:
2447 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2448 input, input_length );
2449 break;
2450#endif
2451#if defined(MBEDTLS_SHA1_C)
2452 case PSA_ALG_SHA_1:
2453 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2454 input, input_length );
2455 break;
2456#endif
2457#if defined(MBEDTLS_SHA256_C)
2458 case PSA_ALG_SHA_224:
2459 case PSA_ALG_SHA_256:
2460 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2461 input, input_length );
2462 break;
2463#endif
2464#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002465#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002466 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002467#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002468 case PSA_ALG_SHA_512:
2469 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2470 input, input_length );
2471 break;
2472#endif
2473 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002474 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002475 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002476
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002477 if( ret != 0 )
2478 psa_hash_abort( operation );
2479 return( mbedtls_to_psa_error( ret ) );
2480}
2481
2482psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2483 uint8_t *hash,
2484 size_t hash_size,
2485 size_t *hash_length )
2486{
itayzafrir40835d42018-08-02 13:14:17 +03002487 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002488 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002489 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002490
2491 /* Fill the output buffer with something that isn't a valid hash
2492 * (barring an attack on the hash and deliberately-crafted input),
2493 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002494 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002495 /* If hash_size is 0 then hash may be NULL and then the
2496 * call to memset would have undefined behavior. */
2497 if( hash_size != 0 )
2498 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002499
2500 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002501 {
2502 status = PSA_ERROR_BUFFER_TOO_SMALL;
2503 goto exit;
2504 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002505
2506 switch( operation->alg )
2507 {
2508#if defined(MBEDTLS_MD2_C)
2509 case PSA_ALG_MD2:
2510 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2511 break;
2512#endif
2513#if defined(MBEDTLS_MD4_C)
2514 case PSA_ALG_MD4:
2515 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2516 break;
2517#endif
2518#if defined(MBEDTLS_MD5_C)
2519 case PSA_ALG_MD5:
2520 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2521 break;
2522#endif
2523#if defined(MBEDTLS_RIPEMD160_C)
2524 case PSA_ALG_RIPEMD160:
2525 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2526 break;
2527#endif
2528#if defined(MBEDTLS_SHA1_C)
2529 case PSA_ALG_SHA_1:
2530 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2531 break;
2532#endif
2533#if defined(MBEDTLS_SHA256_C)
2534 case PSA_ALG_SHA_224:
2535 case PSA_ALG_SHA_256:
2536 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2537 break;
2538#endif
2539#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002540#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002541 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002542#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002543 case PSA_ALG_SHA_512:
2544 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2545 break;
2546#endif
2547 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002548 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002549 }
itayzafrir40835d42018-08-02 13:14:17 +03002550 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002551
itayzafrir40835d42018-08-02 13:14:17 +03002552exit:
2553 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002554 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002555 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002556 return( psa_hash_abort( operation ) );
2557 }
2558 else
2559 {
2560 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002561 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002562 }
2563}
2564
Gilles Peskine2d277862018-06-18 15:41:12 +02002565psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2566 const uint8_t *hash,
2567 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002568{
2569 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2570 size_t actual_hash_length;
2571 psa_status_t status = psa_hash_finish( operation,
2572 actual_hash, sizeof( actual_hash ),
2573 &actual_hash_length );
2574 if( status != PSA_SUCCESS )
2575 return( status );
2576 if( actual_hash_length != hash_length )
2577 return( PSA_ERROR_INVALID_SIGNATURE );
2578 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2579 return( PSA_ERROR_INVALID_SIGNATURE );
2580 return( PSA_SUCCESS );
2581}
2582
Gilles Peskine0a749c82019-11-28 19:33:58 +01002583psa_status_t psa_hash_compute( psa_algorithm_t alg,
2584 const uint8_t *input, size_t input_length,
2585 uint8_t *hash, size_t hash_size,
2586 size_t *hash_length )
2587{
2588 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2589 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2590
2591 *hash_length = hash_size;
2592 status = psa_hash_setup( &operation, alg );
2593 if( status != PSA_SUCCESS )
2594 goto exit;
2595 status = psa_hash_update( &operation, input, input_length );
2596 if( status != PSA_SUCCESS )
2597 goto exit;
2598 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2599 if( status != PSA_SUCCESS )
2600 goto exit;
2601
2602exit:
2603 if( status == PSA_SUCCESS )
2604 status = psa_hash_abort( &operation );
2605 else
2606 psa_hash_abort( &operation );
2607 return( status );
2608}
2609
2610psa_status_t psa_hash_compare( psa_algorithm_t alg,
2611 const uint8_t *input, size_t input_length,
2612 const uint8_t *hash, size_t hash_length )
2613{
2614 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2615 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2616
2617 status = psa_hash_setup( &operation, alg );
2618 if( status != PSA_SUCCESS )
2619 goto exit;
2620 status = psa_hash_update( &operation, input, input_length );
2621 if( status != PSA_SUCCESS )
2622 goto exit;
2623 status = psa_hash_verify( &operation, hash, hash_length );
2624 if( status != PSA_SUCCESS )
2625 goto exit;
2626
2627exit:
2628 if( status == PSA_SUCCESS )
2629 status = psa_hash_abort( &operation );
2630 else
2631 psa_hash_abort( &operation );
2632 return( status );
2633}
2634
Gilles Peskineeb35d782019-01-22 17:56:16 +01002635psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2636 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002637{
2638 if( target_operation->alg != 0 )
2639 return( PSA_ERROR_BAD_STATE );
2640
2641 switch( source_operation->alg )
2642 {
2643 case 0:
2644 return( PSA_ERROR_BAD_STATE );
2645#if defined(MBEDTLS_MD2_C)
2646 case PSA_ALG_MD2:
2647 mbedtls_md2_clone( &target_operation->ctx.md2,
2648 &source_operation->ctx.md2 );
2649 break;
2650#endif
2651#if defined(MBEDTLS_MD4_C)
2652 case PSA_ALG_MD4:
2653 mbedtls_md4_clone( &target_operation->ctx.md4,
2654 &source_operation->ctx.md4 );
2655 break;
2656#endif
2657#if defined(MBEDTLS_MD5_C)
2658 case PSA_ALG_MD5:
2659 mbedtls_md5_clone( &target_operation->ctx.md5,
2660 &source_operation->ctx.md5 );
2661 break;
2662#endif
2663#if defined(MBEDTLS_RIPEMD160_C)
2664 case PSA_ALG_RIPEMD160:
2665 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2666 &source_operation->ctx.ripemd160 );
2667 break;
2668#endif
2669#if defined(MBEDTLS_SHA1_C)
2670 case PSA_ALG_SHA_1:
2671 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2672 &source_operation->ctx.sha1 );
2673 break;
2674#endif
2675#if defined(MBEDTLS_SHA256_C)
2676 case PSA_ALG_SHA_224:
2677 case PSA_ALG_SHA_256:
2678 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2679 &source_operation->ctx.sha256 );
2680 break;
2681#endif
2682#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002683#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002684 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002685#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002686 case PSA_ALG_SHA_512:
2687 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2688 &source_operation->ctx.sha512 );
2689 break;
2690#endif
2691 default:
2692 return( PSA_ERROR_NOT_SUPPORTED );
2693 }
2694
2695 target_operation->alg = source_operation->alg;
2696 return( PSA_SUCCESS );
2697}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002698
2699
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002700/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002701/* MAC */
2702/****************************************************************/
2703
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002704static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002705 psa_algorithm_t alg,
2706 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002707 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002708 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002709{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002710 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002711 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002712
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002713 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002714 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002715
Gilles Peskine8c9def32018-02-08 10:02:12 +01002716 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2717 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002718 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002719 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002720 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002721 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002722 mode = MBEDTLS_MODE_STREAM;
2723 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002724 case PSA_ALG_CTR:
2725 mode = MBEDTLS_MODE_CTR;
2726 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002727 case PSA_ALG_CFB:
2728 mode = MBEDTLS_MODE_CFB;
2729 break;
2730 case PSA_ALG_OFB:
2731 mode = MBEDTLS_MODE_OFB;
2732 break;
2733 case PSA_ALG_CBC_NO_PADDING:
2734 mode = MBEDTLS_MODE_CBC;
2735 break;
2736 case PSA_ALG_CBC_PKCS7:
2737 mode = MBEDTLS_MODE_CBC;
2738 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002739 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002740 mode = MBEDTLS_MODE_CCM;
2741 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002742 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002743 mode = MBEDTLS_MODE_GCM;
2744 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002745 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2746 mode = MBEDTLS_MODE_CHACHAPOLY;
2747 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002748 default:
2749 return( NULL );
2750 }
2751 }
2752 else if( alg == PSA_ALG_CMAC )
2753 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002754 else
2755 return( NULL );
2756
2757 switch( key_type )
2758 {
2759 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002760 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002761 break;
2762 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002763 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2764 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002765 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002766 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002767 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002768 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002769 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2770 * but two-key Triple-DES is functionally three-key Triple-DES
2771 * with K1=K3, so that's how we present it to mbedtls. */
2772 if( key_bits == 128 )
2773 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002774 break;
2775 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002776 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002777 break;
2778 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002779 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002780 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002781 case PSA_KEY_TYPE_CHACHA20:
2782 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2783 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002784 default:
2785 return( NULL );
2786 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002787 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002788 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002789
Jaeden Amero23bbb752018-06-26 14:16:54 +01002790 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2791 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002792}
2793
Gilles Peskinea05219c2018-11-16 16:02:56 +01002794#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002795static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002796{
Gilles Peskine2d277862018-06-18 15:41:12 +02002797 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002798 {
2799 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002800 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002801 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002802 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002803 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002804 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002805 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002806 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002807 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002808 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002809 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002810 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002811 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002812 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002813 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002814 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002815 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002816 return( 128 );
2817 default:
2818 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002819 }
2820}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002821#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002822
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002823/* Initialize the MAC operation structure. Once this function has been
2824 * called, psa_mac_abort can run and will do the right thing. */
2825static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2826 psa_algorithm_t alg )
2827{
2828 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2829
2830 operation->alg = alg;
2831 operation->key_set = 0;
2832 operation->iv_set = 0;
2833 operation->iv_required = 0;
2834 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002835 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002836
2837#if defined(MBEDTLS_CMAC_C)
2838 if( alg == PSA_ALG_CMAC )
2839 {
2840 operation->iv_required = 0;
2841 mbedtls_cipher_init( &operation->ctx.cmac );
2842 status = PSA_SUCCESS;
2843 }
2844 else
2845#endif /* MBEDTLS_CMAC_C */
2846#if defined(MBEDTLS_MD_C)
2847 if( PSA_ALG_IS_HMAC( operation->alg ) )
2848 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002849 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2850 operation->ctx.hmac.hash_ctx.alg = 0;
2851 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002852 }
2853 else
2854#endif /* MBEDTLS_MD_C */
2855 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002856 if( ! PSA_ALG_IS_MAC( alg ) )
2857 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002858 }
2859
2860 if( status != PSA_SUCCESS )
2861 memset( operation, 0, sizeof( *operation ) );
2862 return( status );
2863}
2864
Gilles Peskine01126fa2018-07-12 17:04:55 +02002865#if defined(MBEDTLS_MD_C)
2866static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2867{
Gilles Peskine3f108122018-12-07 18:14:53 +01002868 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002869 return( psa_hash_abort( &hmac->hash_ctx ) );
2870}
2871#endif /* MBEDTLS_MD_C */
2872
Gilles Peskine8c9def32018-02-08 10:02:12 +01002873psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2874{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002875 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002876 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002877 /* The object has (apparently) been initialized but it is not
2878 * in use. It's ok to call abort on such an object, and there's
2879 * nothing to do. */
2880 return( PSA_SUCCESS );
2881 }
2882 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002883#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002884 if( operation->alg == PSA_ALG_CMAC )
2885 {
2886 mbedtls_cipher_free( &operation->ctx.cmac );
2887 }
2888 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002889#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002890#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002891 if( PSA_ALG_IS_HMAC( operation->alg ) )
2892 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002893 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002894 }
2895 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002896#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002897 {
2898 /* Sanity check (shouldn't happen: operation->alg should
2899 * always have been initialized to a valid value). */
2900 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002901 }
Moran Peker41deec42018-04-04 15:43:05 +03002902
Gilles Peskine8c9def32018-02-08 10:02:12 +01002903 operation->alg = 0;
2904 operation->key_set = 0;
2905 operation->iv_set = 0;
2906 operation->iv_required = 0;
2907 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002908 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002909
Gilles Peskine8c9def32018-02-08 10:02:12 +01002910 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002911
2912bad_state:
2913 /* If abort is called on an uninitialized object, we can't trust
2914 * anything. Wipe the object in case it contains confidential data.
2915 * This may result in a memory leak if a pointer gets overwritten,
2916 * but it's too late to do anything about this. */
2917 memset( operation, 0, sizeof( *operation ) );
2918 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002919}
2920
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002921#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002922static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002923 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002924 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002925 const mbedtls_cipher_info_t *cipher_info )
2926{
Janos Follath24eed8d2019-11-22 13:21:35 +00002927 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002928
2929 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002930
2931 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2932 if( ret != 0 )
2933 return( ret );
2934
2935 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02002936 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002937 key_bits );
2938 return( ret );
2939}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002940#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002941
Gilles Peskine248051a2018-06-20 16:09:38 +02002942#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002943static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2944 const uint8_t *key,
2945 size_t key_length,
2946 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002947{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002948 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002949 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002950 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002951 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002952 psa_status_t status;
2953
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002954 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2955 * overflow below. This should never trigger if the hash algorithm
2956 * is implemented correctly. */
2957 /* The size checks against the ipad and opad buffers cannot be written
2958 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2959 * because that triggers -Wlogical-op on GCC 7.3. */
2960 if( block_size > sizeof( ipad ) )
2961 return( PSA_ERROR_NOT_SUPPORTED );
2962 if( block_size > sizeof( hmac->opad ) )
2963 return( PSA_ERROR_NOT_SUPPORTED );
2964 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002965 return( PSA_ERROR_NOT_SUPPORTED );
2966
Gilles Peskined223b522018-06-11 18:12:58 +02002967 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002968 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002969 status = psa_hash_compute( hash_alg, key, key_length,
2970 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002971 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002972 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002973 }
Gilles Peskine96889972018-07-12 17:07:03 +02002974 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2975 * but it is permitted. It is common when HMAC is used in HKDF, for
2976 * example. Don't call `memcpy` in the 0-length because `key` could be
2977 * an invalid pointer which would make the behavior undefined. */
2978 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002979 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002980
Gilles Peskined223b522018-06-11 18:12:58 +02002981 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2982 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002983 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002984 ipad[i] ^= 0x36;
2985 memset( ipad + key_length, 0x36, block_size - key_length );
2986
2987 /* Copy the key material from ipad to opad, flipping the requisite bits,
2988 * and filling the rest of opad with the requisite constant. */
2989 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002990 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2991 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002992
Gilles Peskine01126fa2018-07-12 17:04:55 +02002993 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002994 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002995 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002996
Gilles Peskine01126fa2018-07-12 17:04:55 +02002997 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002998
2999cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03003000 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003001
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003002 return( status );
3003}
Gilles Peskine248051a2018-06-20 16:09:38 +02003004#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003005
Gilles Peskine89167cb2018-07-08 20:12:23 +02003006static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003007 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003008 psa_algorithm_t alg,
3009 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003010{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003011 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003012 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003013 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003014 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003015 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003016 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003017 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003018
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003019 /* A context must be freshly initialized before it can be set up. */
3020 if( operation->alg != 0 )
3021 {
3022 return( PSA_ERROR_BAD_STATE );
3023 }
3024
Gilles Peskined911eb72018-08-14 15:18:45 +02003025 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003026 if( status != PSA_SUCCESS )
3027 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003028 if( is_sign )
3029 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003030
Gilles Peskine28f8f302019-07-24 13:30:31 +02003031 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003032 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003033 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003034 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003035
Gilles Peskine8c9def32018-02-08 10:02:12 +01003036#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003037 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003038 {
3039 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003040 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003041 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003042 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003043 if( cipher_info == NULL )
3044 {
3045 status = PSA_ERROR_NOT_SUPPORTED;
3046 goto exit;
3047 }
3048 operation->mac_size = cipher_info->block_size;
3049 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3050 status = mbedtls_to_psa_error( ret );
3051 }
3052 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003053#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003054#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003055 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003056 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003057 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003058 if( hash_alg == 0 )
3059 {
3060 status = PSA_ERROR_NOT_SUPPORTED;
3061 goto exit;
3062 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003063
3064 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3065 /* Sanity check. This shouldn't fail on a valid configuration. */
3066 if( operation->mac_size == 0 ||
3067 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3068 {
3069 status = PSA_ERROR_NOT_SUPPORTED;
3070 goto exit;
3071 }
3072
Gilles Peskine8e338702019-07-30 20:06:31 +02003073 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003074 {
3075 status = PSA_ERROR_INVALID_ARGUMENT;
3076 goto exit;
3077 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003078
Gilles Peskine01126fa2018-07-12 17:04:55 +02003079 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003080 slot->data.key.data,
3081 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003082 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003083 }
3084 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003085#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003086 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003087 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003088 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003089 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003090
Gilles Peskined911eb72018-08-14 15:18:45 +02003091 if( truncated == 0 )
3092 {
3093 /* The "normal" case: untruncated algorithm. Nothing to do. */
3094 }
3095 else if( truncated < 4 )
3096 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003097 /* A very short MAC is too short for security since it can be
3098 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3099 * so we make this our minimum, even though 32 bits is still
3100 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003101 status = PSA_ERROR_NOT_SUPPORTED;
3102 }
3103 else if( truncated > operation->mac_size )
3104 {
3105 /* It's impossible to "truncate" to a larger length. */
3106 status = PSA_ERROR_INVALID_ARGUMENT;
3107 }
3108 else
3109 operation->mac_size = truncated;
3110
Gilles Peskinefbfac682018-07-08 20:51:54 +02003111exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003112 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003113 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003114 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003115 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003116 else
3117 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003118 operation->key_set = 1;
3119 }
3120 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003121}
3122
Gilles Peskine89167cb2018-07-08 20:12:23 +02003123psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003124 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003125 psa_algorithm_t alg )
3126{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003127 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003128}
3129
3130psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003131 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003132 psa_algorithm_t alg )
3133{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003134 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003135}
3136
Gilles Peskine8c9def32018-02-08 10:02:12 +01003137psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3138 const uint8_t *input,
3139 size_t input_length )
3140{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003141 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003142 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003143 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003144 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003145 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003146 operation->has_input = 1;
3147
Gilles Peskine8c9def32018-02-08 10:02:12 +01003148#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003149 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003150 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003151 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3152 input, input_length );
3153 status = mbedtls_to_psa_error( ret );
3154 }
3155 else
3156#endif /* MBEDTLS_CMAC_C */
3157#if defined(MBEDTLS_MD_C)
3158 if( PSA_ALG_IS_HMAC( operation->alg ) )
3159 {
3160 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3161 input_length );
3162 }
3163 else
3164#endif /* MBEDTLS_MD_C */
3165 {
3166 /* This shouldn't happen if `operation` was initialized by
3167 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003168 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003169 }
3170
Gilles Peskinefbfac682018-07-08 20:51:54 +02003171 if( status != PSA_SUCCESS )
3172 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003173 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003174}
3175
Gilles Peskine01126fa2018-07-12 17:04:55 +02003176#if defined(MBEDTLS_MD_C)
3177static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3178 uint8_t *mac,
3179 size_t mac_size )
3180{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003181 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003182 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3183 size_t hash_size = 0;
3184 size_t block_size = psa_get_hash_block_size( hash_alg );
3185 psa_status_t status;
3186
Gilles Peskine01126fa2018-07-12 17:04:55 +02003187 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3188 if( status != PSA_SUCCESS )
3189 return( status );
3190 /* From here on, tmp needs to be wiped. */
3191
3192 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3193 if( status != PSA_SUCCESS )
3194 goto exit;
3195
3196 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3197 if( status != PSA_SUCCESS )
3198 goto exit;
3199
3200 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3201 if( status != PSA_SUCCESS )
3202 goto exit;
3203
Gilles Peskined911eb72018-08-14 15:18:45 +02003204 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3205 if( status != PSA_SUCCESS )
3206 goto exit;
3207
3208 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003209
3210exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003211 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003212 return( status );
3213}
3214#endif /* MBEDTLS_MD_C */
3215
mohammad16036df908f2018-04-02 08:34:15 -07003216static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003217 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003218 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003219{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003220 if( ! operation->key_set )
3221 return( PSA_ERROR_BAD_STATE );
3222 if( operation->iv_required && ! operation->iv_set )
3223 return( PSA_ERROR_BAD_STATE );
3224
Gilles Peskine8c9def32018-02-08 10:02:12 +01003225 if( mac_size < operation->mac_size )
3226 return( PSA_ERROR_BUFFER_TOO_SMALL );
3227
Gilles Peskine8c9def32018-02-08 10:02:12 +01003228#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003229 if( operation->alg == PSA_ALG_CMAC )
3230 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003231 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3232 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3233 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003234 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003235 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003236 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003237 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003238 else
3239#endif /* MBEDTLS_CMAC_C */
3240#if defined(MBEDTLS_MD_C)
3241 if( PSA_ALG_IS_HMAC( operation->alg ) )
3242 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003243 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003244 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003245 }
3246 else
3247#endif /* MBEDTLS_MD_C */
3248 {
3249 /* This shouldn't happen if `operation` was initialized by
3250 * a setup function. */
3251 return( PSA_ERROR_BAD_STATE );
3252 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003253}
3254
Gilles Peskineacd4be32018-07-08 19:56:25 +02003255psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3256 uint8_t *mac,
3257 size_t mac_size,
3258 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003259{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003260 psa_status_t status;
3261
Jaeden Amero252ef282019-02-15 14:05:35 +00003262 if( operation->alg == 0 )
3263 {
3264 return( PSA_ERROR_BAD_STATE );
3265 }
3266
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003267 /* Fill the output buffer with something that isn't a valid mac
3268 * (barring an attack on the mac and deliberately-crafted input),
3269 * in case the caller doesn't check the return status properly. */
3270 *mac_length = mac_size;
3271 /* If mac_size is 0 then mac may be NULL and then the
3272 * call to memset would have undefined behavior. */
3273 if( mac_size != 0 )
3274 memset( mac, '!', mac_size );
3275
Gilles Peskine89167cb2018-07-08 20:12:23 +02003276 if( ! operation->is_sign )
3277 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003278 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003279 }
mohammad16036df908f2018-04-02 08:34:15 -07003280
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003281 status = psa_mac_finish_internal( operation, mac, mac_size );
3282
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003283 if( status == PSA_SUCCESS )
3284 {
3285 status = psa_mac_abort( operation );
3286 if( status == PSA_SUCCESS )
3287 *mac_length = operation->mac_size;
3288 else
3289 memset( mac, '!', mac_size );
3290 }
3291 else
3292 psa_mac_abort( operation );
3293 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003294}
3295
Gilles Peskineacd4be32018-07-08 19:56:25 +02003296psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3297 const uint8_t *mac,
3298 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003299{
Gilles Peskine828ed142018-06-18 23:25:51 +02003300 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003301 psa_status_t status;
3302
Jaeden Amero252ef282019-02-15 14:05:35 +00003303 if( operation->alg == 0 )
3304 {
3305 return( PSA_ERROR_BAD_STATE );
3306 }
3307
Gilles Peskine89167cb2018-07-08 20:12:23 +02003308 if( operation->is_sign )
3309 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003310 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003311 }
3312 if( operation->mac_size != mac_length )
3313 {
3314 status = PSA_ERROR_INVALID_SIGNATURE;
3315 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003316 }
mohammad16036df908f2018-04-02 08:34:15 -07003317
3318 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003319 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003320 if( status != PSA_SUCCESS )
3321 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003322
3323 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3324 status = PSA_ERROR_INVALID_SIGNATURE;
3325
3326cleanup:
3327 if( status == PSA_SUCCESS )
3328 status = psa_mac_abort( operation );
3329 else
3330 psa_mac_abort( operation );
3331
Gilles Peskine3f108122018-12-07 18:14:53 +01003332 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003333
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003334 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003335}
3336
3337
Gilles Peskine20035e32018-02-03 22:44:14 +01003338
Gilles Peskine20035e32018-02-03 22:44:14 +01003339/****************************************************************/
3340/* Asymmetric cryptography */
3341/****************************************************************/
3342
Gilles Peskine2b450e32018-06-27 15:42:46 +02003343#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003344/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003345 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003346static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3347 size_t hash_length,
3348 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003349{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003350 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003351 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003352 *md_alg = mbedtls_md_get_type( md_info );
3353
3354 /* The Mbed TLS RSA module uses an unsigned int for hash length
3355 * parameters. Validate that it fits so that we don't risk an
3356 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003357#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003358 if( hash_length > UINT_MAX )
3359 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003360#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003361
3362#if defined(MBEDTLS_PKCS1_V15)
3363 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3364 * must be correct. */
3365 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3366 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003367 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003368 if( md_info == NULL )
3369 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003370 if( mbedtls_md_get_size( md_info ) != hash_length )
3371 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003372 }
3373#endif /* MBEDTLS_PKCS1_V15 */
3374
3375#if defined(MBEDTLS_PKCS1_V21)
3376 /* PSS requires a hash internally. */
3377 if( PSA_ALG_IS_RSA_PSS( alg ) )
3378 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003379 if( md_info == NULL )
3380 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003381 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003382#endif /* MBEDTLS_PKCS1_V21 */
3383
Gilles Peskine61b91d42018-06-08 16:09:36 +02003384 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003385}
3386
Gilles Peskine2b450e32018-06-27 15:42:46 +02003387static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3388 psa_algorithm_t alg,
3389 const uint8_t *hash,
3390 size_t hash_length,
3391 uint8_t *signature,
3392 size_t signature_size,
3393 size_t *signature_length )
3394{
3395 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003397 mbedtls_md_type_t md_alg;
3398
3399 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3400 if( status != PSA_SUCCESS )
3401 return( status );
3402
Gilles Peskine630a18a2018-06-29 17:49:35 +02003403 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003404 return( PSA_ERROR_BUFFER_TOO_SMALL );
3405
3406#if defined(MBEDTLS_PKCS1_V15)
3407 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3408 {
3409 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3410 MBEDTLS_MD_NONE );
3411 ret = mbedtls_rsa_pkcs1_sign( rsa,
3412 mbedtls_ctr_drbg_random,
3413 &global_data.ctr_drbg,
3414 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003415 md_alg,
3416 (unsigned int) hash_length,
3417 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003418 signature );
3419 }
3420 else
3421#endif /* MBEDTLS_PKCS1_V15 */
3422#if defined(MBEDTLS_PKCS1_V21)
3423 if( PSA_ALG_IS_RSA_PSS( alg ) )
3424 {
3425 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3426 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3427 mbedtls_ctr_drbg_random,
3428 &global_data.ctr_drbg,
3429 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003430 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003431 (unsigned int) hash_length,
3432 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003433 signature );
3434 }
3435 else
3436#endif /* MBEDTLS_PKCS1_V21 */
3437 {
3438 return( PSA_ERROR_INVALID_ARGUMENT );
3439 }
3440
3441 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003442 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003443 return( mbedtls_to_psa_error( ret ) );
3444}
3445
3446static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3447 psa_algorithm_t alg,
3448 const uint8_t *hash,
3449 size_t hash_length,
3450 const uint8_t *signature,
3451 size_t signature_length )
3452{
3453 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003454 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003455 mbedtls_md_type_t md_alg;
3456
3457 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3458 if( status != PSA_SUCCESS )
3459 return( status );
3460
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003461 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3462 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003463
3464#if defined(MBEDTLS_PKCS1_V15)
3465 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3466 {
3467 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3468 MBEDTLS_MD_NONE );
3469 ret = mbedtls_rsa_pkcs1_verify( rsa,
3470 mbedtls_ctr_drbg_random,
3471 &global_data.ctr_drbg,
3472 MBEDTLS_RSA_PUBLIC,
3473 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003474 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003475 hash,
3476 signature );
3477 }
3478 else
3479#endif /* MBEDTLS_PKCS1_V15 */
3480#if defined(MBEDTLS_PKCS1_V21)
3481 if( PSA_ALG_IS_RSA_PSS( alg ) )
3482 {
3483 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3484 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3485 mbedtls_ctr_drbg_random,
3486 &global_data.ctr_drbg,
3487 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003488 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003489 (unsigned int) hash_length,
3490 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003491 signature );
3492 }
3493 else
3494#endif /* MBEDTLS_PKCS1_V21 */
3495 {
3496 return( PSA_ERROR_INVALID_ARGUMENT );
3497 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003498
3499 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3500 * the rest of the signature is invalid". This has little use in
3501 * practice and PSA doesn't report this distinction. */
3502 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3503 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003504 return( mbedtls_to_psa_error( ret ) );
3505}
3506#endif /* MBEDTLS_RSA_C */
3507
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003508#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003509/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3510 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3511 * (even though these functions don't modify it). */
3512static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3513 psa_algorithm_t alg,
3514 const uint8_t *hash,
3515 size_t hash_length,
3516 uint8_t *signature,
3517 size_t signature_size,
3518 size_t *signature_length )
3519{
Janos Follath24eed8d2019-11-22 13:21:35 +00003520 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003521 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003522 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003523 mbedtls_mpi_init( &r );
3524 mbedtls_mpi_init( &s );
3525
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003526 if( signature_size < 2 * curve_bytes )
3527 {
3528 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3529 goto cleanup;
3530 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003531
Gilles Peskinea05219c2018-11-16 16:02:56 +01003532#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003533 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3534 {
3535 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3536 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3537 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003538 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3539 &ecp->d, hash,
3540 hash_length, md_alg,
3541 mbedtls_ctr_drbg_random,
3542 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003543 }
3544 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003545#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003546 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003547 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003548 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3549 hash, hash_length,
3550 mbedtls_ctr_drbg_random,
3551 &global_data.ctr_drbg ) );
3552 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003553
3554 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3555 signature,
3556 curve_bytes ) );
3557 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3558 signature + curve_bytes,
3559 curve_bytes ) );
3560
3561cleanup:
3562 mbedtls_mpi_free( &r );
3563 mbedtls_mpi_free( &s );
3564 if( ret == 0 )
3565 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003566 return( mbedtls_to_psa_error( ret ) );
3567}
3568
3569static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3570 const uint8_t *hash,
3571 size_t hash_length,
3572 const uint8_t *signature,
3573 size_t signature_length )
3574{
Janos Follath24eed8d2019-11-22 13:21:35 +00003575 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003576 mbedtls_mpi r, s;
3577 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3578 mbedtls_mpi_init( &r );
3579 mbedtls_mpi_init( &s );
3580
3581 if( signature_length != 2 * curve_bytes )
3582 return( PSA_ERROR_INVALID_SIGNATURE );
3583
3584 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3585 signature,
3586 curve_bytes ) );
3587 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3588 signature + curve_bytes,
3589 curve_bytes ) );
3590
Steven Cooremanacda8342020-07-24 23:09:52 +02003591 /* Check whether the public part is loaded. If not, load it. */
3592 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3593 {
3594 MBEDTLS_MPI_CHK(
3595 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3596 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3597 }
3598
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003599 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3600 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003601
3602cleanup:
3603 mbedtls_mpi_free( &r );
3604 mbedtls_mpi_free( &s );
3605 return( mbedtls_to_psa_error( ret ) );
3606}
3607#endif /* MBEDTLS_ECDSA_C */
3608
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003609psa_status_t psa_sign_hash( psa_key_handle_t handle,
3610 psa_algorithm_t alg,
3611 const uint8_t *hash,
3612 size_t hash_length,
3613 uint8_t *signature,
3614 size_t signature_size,
3615 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003616{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003617 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003618 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003619#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3620 const psa_drv_se_t *drv;
3621 psa_drv_se_context_t *drv_context;
3622#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003623
3624 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003625 /* Immediately reject a zero-length signature buffer. This guarantees
3626 * that signature must be a valid pointer. (On the other hand, the hash
3627 * buffer can in principle be empty since it doesn't actually have
3628 * to be a hash.) */
3629 if( signature_size == 0 )
3630 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003631
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003632 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003633 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003634 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003635 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003636 {
3637 status = PSA_ERROR_INVALID_ARGUMENT;
3638 goto exit;
3639 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003640
Gilles Peskineedc64242019-08-07 21:05:07 +02003641#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3642 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3643 {
3644 if( drv->asymmetric == NULL ||
3645 drv->asymmetric->p_sign == NULL )
3646 {
3647 status = PSA_ERROR_NOT_SUPPORTED;
3648 goto exit;
3649 }
3650 status = drv->asymmetric->p_sign( drv_context,
3651 slot->data.se.slot_number,
3652 alg,
3653 hash, hash_length,
3654 signature, signature_size,
3655 signature_length );
3656 }
3657 else
3658#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003659#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003660 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003661 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003662 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003663
3664 status = psa_load_rsa_representation( slot,
3665 &rsa );
3666 if( status != PSA_SUCCESS )
3667 goto exit;
3668
Steven Cooremana2371e52020-07-28 14:30:39 +02003669 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003670 alg,
3671 hash, hash_length,
3672 signature, signature_size,
3673 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003674
Steven Cooremana2371e52020-07-28 14:30:39 +02003675 mbedtls_rsa_free( rsa );
3676 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003677 }
3678 else
3679#endif /* defined(MBEDTLS_RSA_C) */
3680#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003681 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003682 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003683#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003684 if(
3685#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3686 PSA_ALG_IS_ECDSA( alg )
3687#else
3688 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3689#endif
3690 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003691 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003692 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooremanacda8342020-07-24 23:09:52 +02003693 status = psa_load_ecp_representation( slot, &ecp );
3694 if( status != PSA_SUCCESS )
3695 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003696 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003697 alg,
3698 hash, hash_length,
3699 signature, signature_size,
3700 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003701 mbedtls_ecp_keypair_free( ecp );
3702 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003703 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003704 else
3705#endif /* defined(MBEDTLS_ECDSA_C) */
3706 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003707 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003708 }
itayzafrir5c753392018-05-08 11:18:38 +03003709 }
3710 else
3711#endif /* defined(MBEDTLS_ECP_C) */
3712 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003713 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003714 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003715
3716exit:
3717 /* Fill the unused part of the output buffer (the whole buffer on error,
3718 * the trailing part on success) with something that isn't a valid mac
3719 * (barring an attack on the mac and deliberately-crafted input),
3720 * in case the caller doesn't check the return status properly. */
3721 if( status == PSA_SUCCESS )
3722 memset( signature + *signature_length, '!',
3723 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003724 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003725 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003726 /* If signature_size is 0 then we have nothing to do. We must not call
3727 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003728 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003729}
3730
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003731psa_status_t psa_verify_hash( psa_key_handle_t handle,
3732 psa_algorithm_t alg,
3733 const uint8_t *hash,
3734 size_t hash_length,
3735 const uint8_t *signature,
3736 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003737{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003738 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003739 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003740#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3741 const psa_drv_se_t *drv;
3742 psa_drv_se_context_t *drv_context;
3743#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003744
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003745 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003746 if( status != PSA_SUCCESS )
3747 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003748
Gilles Peskineedc64242019-08-07 21:05:07 +02003749#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3750 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3751 {
3752 if( drv->asymmetric == NULL ||
3753 drv->asymmetric->p_verify == NULL )
3754 return( PSA_ERROR_NOT_SUPPORTED );
3755 return( drv->asymmetric->p_verify( drv_context,
3756 slot->data.se.slot_number,
3757 alg,
3758 hash, hash_length,
3759 signature, signature_length ) );
3760 }
3761 else
3762#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003763#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003764 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003765 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003766 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003767
3768 status = psa_load_rsa_representation( slot, &rsa );
3769 if( status != PSA_SUCCESS )
3770 return status;
3771
Steven Cooremana2371e52020-07-28 14:30:39 +02003772 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003773 alg,
3774 hash, hash_length,
3775 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003776 mbedtls_rsa_free( rsa );
3777 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003778 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003779 }
3780 else
3781#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003782#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003783 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003784 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003785#if defined(MBEDTLS_ECDSA_C)
3786 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003787 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003788 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooremanacda8342020-07-24 23:09:52 +02003789 status = psa_load_ecp_representation( slot, &ecp );
3790 if( status != PSA_SUCCESS )
3791 return status;
Steven Cooremana2371e52020-07-28 14:30:39 +02003792 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003793 hash, hash_length,
3794 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003795 mbedtls_ecp_keypair_free( ecp );
3796 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003797 return status;
3798 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003799 else
3800#endif /* defined(MBEDTLS_ECDSA_C) */
3801 {
3802 return( PSA_ERROR_INVALID_ARGUMENT );
3803 }
itayzafrir5c753392018-05-08 11:18:38 +03003804 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003805 else
3806#endif /* defined(MBEDTLS_ECP_C) */
3807 {
3808 return( PSA_ERROR_NOT_SUPPORTED );
3809 }
3810}
3811
Gilles Peskine072ac562018-06-30 00:21:29 +02003812#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3813static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3814 mbedtls_rsa_context *rsa )
3815{
3816 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3817 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3818 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3819 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3820}
3821#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3822
Gilles Peskinec5487a82018-12-03 18:08:14 +01003823psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003824 psa_algorithm_t alg,
3825 const uint8_t *input,
3826 size_t input_length,
3827 const uint8_t *salt,
3828 size_t salt_length,
3829 uint8_t *output,
3830 size_t output_size,
3831 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003832{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003833 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003834 psa_status_t status;
3835
Darryl Green5cc689a2018-07-24 15:34:10 +01003836 (void) input;
3837 (void) input_length;
3838 (void) salt;
3839 (void) output;
3840 (void) output_size;
3841
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003842 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003843
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003844 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3845 return( PSA_ERROR_INVALID_ARGUMENT );
3846
Gilles Peskine28f8f302019-07-24 13:30:31 +02003847 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003848 if( status != PSA_SUCCESS )
3849 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003850 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3851 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003852 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003853
3854#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003855 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003856 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003857 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02003858 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02003859
3860 status = psa_load_rsa_representation( slot, &rsa );
3861 if( status != PSA_SUCCESS )
3862 return status;
Steven Cooremana2371e52020-07-28 14:30:39 +02003863
3864 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003865 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003866 mbedtls_rsa_free( rsa );
3867 mbedtls_free( rsa );
Vikas Katariya21599b62019-08-02 12:26:29 +01003868 return( PSA_ERROR_BUFFER_TOO_SMALL );
Steven Cooremana01795d2020-07-24 22:48:15 +02003869 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003870#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003871 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003872 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003873 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003874 mbedtls_ctr_drbg_random,
3875 &global_data.ctr_drbg,
3876 MBEDTLS_RSA_PUBLIC,
3877 input_length,
3878 input,
3879 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003880 }
3881 else
3882#endif /* MBEDTLS_PKCS1_V15 */
3883#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003884 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003885 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003886 psa_rsa_oaep_set_padding_mode( alg, rsa );
3887 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine072ac562018-06-30 00:21:29 +02003888 mbedtls_ctr_drbg_random,
3889 &global_data.ctr_drbg,
3890 MBEDTLS_RSA_PUBLIC,
3891 salt, salt_length,
3892 input_length,
3893 input,
3894 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003895 }
3896 else
3897#endif /* MBEDTLS_PKCS1_V21 */
3898 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003899 mbedtls_rsa_free( rsa );
3900 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003901 return( PSA_ERROR_INVALID_ARGUMENT );
3902 }
3903 if( ret == 0 )
Steven Cooremana2371e52020-07-28 14:30:39 +02003904 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003905
Steven Cooremana2371e52020-07-28 14:30:39 +02003906 mbedtls_rsa_free( rsa );
3907 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003908 return( mbedtls_to_psa_error( ret ) );
3909 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003910 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003911#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003912 {
3913 return( PSA_ERROR_NOT_SUPPORTED );
3914 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003915}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003916
Gilles Peskinec5487a82018-12-03 18:08:14 +01003917psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003918 psa_algorithm_t alg,
3919 const uint8_t *input,
3920 size_t input_length,
3921 const uint8_t *salt,
3922 size_t salt_length,
3923 uint8_t *output,
3924 size_t output_size,
3925 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003926{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003927 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003928 psa_status_t status;
3929
Darryl Green5cc689a2018-07-24 15:34:10 +01003930 (void) input;
3931 (void) input_length;
3932 (void) salt;
3933 (void) output;
3934 (void) output_size;
3935
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003936 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003937
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003938 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3939 return( PSA_ERROR_INVALID_ARGUMENT );
3940
Gilles Peskine28f8f302019-07-24 13:30:31 +02003941 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003942 if( status != PSA_SUCCESS )
3943 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003944 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003945 return( PSA_ERROR_INVALID_ARGUMENT );
3946
3947#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003948 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003949 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003950 mbedtls_rsa_context *rsa;
Steven Cooreman75b74362020-07-28 14:30:13 +02003951 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02003952
3953 status = psa_load_rsa_representation( slot, &rsa );
3954 if( status != PSA_SUCCESS )
3955 return status;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003956
Steven Cooremana2371e52020-07-28 14:30:39 +02003957 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003958 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003959 mbedtls_rsa_free( rsa );
3960 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003961 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana01795d2020-07-24 22:48:15 +02003962 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003963
3964#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003965 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003966 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003967 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003968 mbedtls_ctr_drbg_random,
3969 &global_data.ctr_drbg,
3970 MBEDTLS_RSA_PRIVATE,
3971 output_length,
3972 input,
3973 output,
3974 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003975 }
3976 else
3977#endif /* MBEDTLS_PKCS1_V15 */
3978#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003979 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003980 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003981 psa_rsa_oaep_set_padding_mode( alg, rsa );
3982 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine072ac562018-06-30 00:21:29 +02003983 mbedtls_ctr_drbg_random,
3984 &global_data.ctr_drbg,
3985 MBEDTLS_RSA_PRIVATE,
3986 salt, salt_length,
3987 output_length,
3988 input,
3989 output,
3990 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003991 }
3992 else
3993#endif /* MBEDTLS_PKCS1_V21 */
3994 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003995 mbedtls_rsa_free( rsa );
3996 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003997 return( PSA_ERROR_INVALID_ARGUMENT );
3998 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003999
Steven Cooremana2371e52020-07-28 14:30:39 +02004000 mbedtls_rsa_free( rsa );
4001 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004002 return( mbedtls_to_psa_error( ret ) );
4003 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004004 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004005#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004006 {
4007 return( PSA_ERROR_NOT_SUPPORTED );
4008 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004009}
Gilles Peskine20035e32018-02-03 22:44:14 +01004010
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004011
4012
mohammad1603503973b2018-03-12 15:59:30 +02004013/****************************************************************/
4014/* Symmetric cryptography */
4015/****************************************************************/
4016
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004017/* Initialize the cipher operation structure. Once this function has been
4018 * called, psa_cipher_abort can run and will do the right thing. */
4019static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
4020 psa_algorithm_t alg )
4021{
Gilles Peskinec06e0712018-06-20 16:21:04 +02004022 if( ! PSA_ALG_IS_CIPHER( alg ) )
4023 {
4024 memset( operation, 0, sizeof( *operation ) );
4025 return( PSA_ERROR_INVALID_ARGUMENT );
4026 }
4027
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004028 operation->alg = alg;
4029 operation->key_set = 0;
4030 operation->iv_set = 0;
4031 operation->iv_required = 1;
4032 operation->iv_size = 0;
4033 operation->block_size = 0;
4034 mbedtls_cipher_init( &operation->ctx.cipher );
4035 return( PSA_SUCCESS );
4036}
4037
Gilles Peskinee553c652018-06-04 16:22:46 +02004038static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004039 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02004040 psa_algorithm_t alg,
4041 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004042{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004043 int ret = 0;
4044 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004045 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004046 size_t key_bits;
4047 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004048 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4049 PSA_KEY_USAGE_ENCRYPT :
4050 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004051
Jaeden Amero36ee5d02019-02-19 09:25:10 +00004052 /* A context must be freshly initialized before it can be set up. */
4053 if( operation->alg != 0 )
4054 {
4055 return( PSA_ERROR_BAD_STATE );
4056 }
4057
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004058 status = psa_cipher_init( operation, alg );
4059 if( status != PSA_SUCCESS )
4060 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004061
Gilles Peskine28f8f302019-07-24 13:30:31 +02004062 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004063 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004064 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004065 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02004066
Gilles Peskine8e338702019-07-30 20:06:31 +02004067 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004068 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004069 {
4070 status = PSA_ERROR_NOT_SUPPORTED;
4071 goto exit;
4072 }
mohammad1603503973b2018-03-12 15:59:30 +02004073
mohammad1603503973b2018-03-12 15:59:30 +02004074 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004075 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004076 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004077
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004078#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004079 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004080 {
4081 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004082 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004083 memcpy( keys, slot->data.key.data, 16 );
4084 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004085 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4086 keys,
4087 192, cipher_operation );
4088 }
4089 else
4090#endif
4091 {
4092 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004093 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004094 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004095 }
Moran Peker41deec42018-04-04 15:43:05 +03004096 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004097 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004098
mohammad16038481e742018-03-18 13:57:31 +02004099#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004100 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004101 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004102 case PSA_ALG_CBC_NO_PADDING:
4103 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4104 MBEDTLS_PADDING_NONE );
4105 break;
4106 case PSA_ALG_CBC_PKCS7:
4107 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4108 MBEDTLS_PADDING_PKCS7 );
4109 break;
4110 default:
4111 /* The algorithm doesn't involve padding. */
4112 ret = 0;
4113 break;
4114 }
4115 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004116 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004117#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4118
mohammad1603503973b2018-03-12 15:59:30 +02004119 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004120 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004121 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004122 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02004123 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004124 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004125 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004126#if defined(MBEDTLS_CHACHA20_C)
4127 else
4128 if( alg == PSA_ALG_CHACHA20 )
4129 operation->iv_size = 12;
4130#endif
mohammad1603503973b2018-03-12 15:59:30 +02004131
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004132exit:
4133 if( status == 0 )
4134 status = mbedtls_to_psa_error( ret );
4135 if( status != 0 )
4136 psa_cipher_abort( operation );
4137 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004138}
4139
Gilles Peskinefe119512018-07-08 21:39:34 +02004140psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004141 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004142 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004143{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004144 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004145}
4146
Gilles Peskinefe119512018-07-08 21:39:34 +02004147psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004148 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004149 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004150{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004151 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004152}
4153
Gilles Peskinefe119512018-07-08 21:39:34 +02004154psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004155 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004156 size_t iv_size,
4157 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004158{
itayzafrir534bd7c2018-08-02 13:56:32 +03004159 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004160 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004161 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004162 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004163 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004164 }
Moran Peker41deec42018-04-04 15:43:05 +03004165 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004166 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004167 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004168 goto exit;
4169 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004170 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4171 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004172 if( ret != 0 )
4173 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004174 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004175 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004176 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004177
mohammad16038481e742018-03-18 13:57:31 +02004178 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004179 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004180
Moran Peker395db872018-05-31 14:07:14 +03004181exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03004182 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03004183 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004184 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004185}
4186
Gilles Peskinefe119512018-07-08 21:39:34 +02004187psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004188 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004189 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004190{
itayzafrir534bd7c2018-08-02 13:56:32 +03004191 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004192 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004193 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004194 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004195 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004196 }
Moran Pekera28258c2018-05-29 16:25:04 +03004197 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004198 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004199 status = PSA_ERROR_INVALID_ARGUMENT;
4200 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004201 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004202 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4203 status = mbedtls_to_psa_error( ret );
4204exit:
4205 if( status == PSA_SUCCESS )
4206 operation->iv_set = 1;
4207 else
mohammad1603503973b2018-03-12 15:59:30 +02004208 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004209 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004210}
4211
Gilles Peskinee553c652018-06-04 16:22:46 +02004212psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4213 const uint8_t *input,
4214 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004215 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004216 size_t output_size,
4217 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004218{
itayzafrir534bd7c2018-08-02 13:56:32 +03004219 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004220 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004221 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00004222
4223 if( operation->alg == 0 )
4224 {
4225 return( PSA_ERROR_BAD_STATE );
4226 }
4227
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004228 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004229 {
4230 /* Take the unprocessed partial block left over from previous
4231 * update calls, if any, plus the input to this call. Remove
4232 * the last partial block, if any. You get the data that will be
4233 * output in this call. */
4234 expected_output_size =
4235 ( operation->ctx.cipher.unprocessed_len + input_length )
4236 / operation->block_size * operation->block_size;
4237 }
4238 else
4239 {
4240 expected_output_size = input_length;
4241 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004242
Gilles Peskine89d789c2018-06-04 17:17:16 +02004243 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004244 {
4245 status = PSA_ERROR_BUFFER_TOO_SMALL;
4246 goto exit;
4247 }
mohammad160382759612018-03-12 18:16:40 +02004248
mohammad1603503973b2018-03-12 15:59:30 +02004249 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02004250 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03004251 status = mbedtls_to_psa_error( ret );
4252exit:
4253 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004254 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004255 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004256}
4257
Gilles Peskinee553c652018-06-04 16:22:46 +02004258psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4259 uint8_t *output,
4260 size_t output_size,
4261 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004262{
David Saadab4ecc272019-02-14 13:48:10 +02004263 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01004264 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02004265 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03004266
mohammad1603503973b2018-03-12 15:59:30 +02004267 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004268 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004269 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004270 }
4271 if( operation->iv_required && ! operation->iv_set )
4272 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004273 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004274 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004275
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004276 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004277 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4278 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004279 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004280 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004281 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004282 }
4283
Janos Follath315b51c2018-07-09 16:04:51 +01004284 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4285 temp_output_buffer,
4286 output_length );
4287 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004288 {
Janos Follath315b51c2018-07-09 16:04:51 +01004289 status = mbedtls_to_psa_error( cipher_ret );
4290 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004291 }
Janos Follath315b51c2018-07-09 16:04:51 +01004292
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004293 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004294 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004295 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004296 memcpy( output, temp_output_buffer, *output_length );
4297 else
4298 {
Janos Follath315b51c2018-07-09 16:04:51 +01004299 status = PSA_ERROR_BUFFER_TOO_SMALL;
4300 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004301 }
mohammad1603503973b2018-03-12 15:59:30 +02004302
Gilles Peskine3f108122018-12-07 18:14:53 +01004303 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004304 status = psa_cipher_abort( operation );
4305
4306 return( status );
4307
4308error:
4309
4310 *output_length = 0;
4311
Gilles Peskine3f108122018-12-07 18:14:53 +01004312 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004313 (void) psa_cipher_abort( operation );
4314
4315 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004316}
4317
Gilles Peskinee553c652018-06-04 16:22:46 +02004318psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4319{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004320 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004321 {
4322 /* The object has (apparently) been initialized but it is not
4323 * in use. It's ok to call abort on such an object, and there's
4324 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004325 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004326 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004327
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004328 /* Sanity check (shouldn't happen: operation->alg should
4329 * always have been initialized to a valid value). */
4330 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4331 return( PSA_ERROR_BAD_STATE );
4332
mohammad1603503973b2018-03-12 15:59:30 +02004333 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004334
Moran Peker41deec42018-04-04 15:43:05 +03004335 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004336 operation->key_set = 0;
4337 operation->iv_set = 0;
4338 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004339 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004340 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004341
Moran Peker395db872018-05-31 14:07:14 +03004342 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004343}
4344
Gilles Peskinea0655c32018-04-30 17:06:50 +02004345
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004346
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004347
mohammad16035955c982018-04-26 00:53:03 +03004348/****************************************************************/
4349/* AEAD */
4350/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004351
Gilles Peskineedf9a652018-08-17 18:11:56 +02004352typedef struct
4353{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004354 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004355 const mbedtls_cipher_info_t *cipher_info;
4356 union
4357 {
4358#if defined(MBEDTLS_CCM_C)
4359 mbedtls_ccm_context ccm;
4360#endif /* MBEDTLS_CCM_C */
4361#if defined(MBEDTLS_GCM_C)
4362 mbedtls_gcm_context gcm;
4363#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004364#if defined(MBEDTLS_CHACHAPOLY_C)
4365 mbedtls_chachapoly_context chachapoly;
4366#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004367 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004368 psa_algorithm_t core_alg;
4369 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004370 uint8_t tag_length;
4371} aead_operation_t;
4372
Gilles Peskine30a9e412019-01-14 18:36:12 +01004373static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004374{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004375 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004376 {
4377#if defined(MBEDTLS_CCM_C)
4378 case PSA_ALG_CCM:
4379 mbedtls_ccm_free( &operation->ctx.ccm );
4380 break;
4381#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004382#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004383 case PSA_ALG_GCM:
4384 mbedtls_gcm_free( &operation->ctx.gcm );
4385 break;
4386#endif /* MBEDTLS_GCM_C */
4387 }
4388}
4389
4390static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004391 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004392 psa_key_usage_t usage,
4393 psa_algorithm_t alg )
4394{
4395 psa_status_t status;
4396 size_t key_bits;
4397 mbedtls_cipher_id_t cipher_id;
4398
Gilles Peskine28f8f302019-07-24 13:30:31 +02004399 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004400 if( status != PSA_SUCCESS )
4401 return( status );
4402
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004403 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004404
4405 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004406 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004407 &cipher_id );
4408 if( operation->cipher_info == NULL )
4409 return( PSA_ERROR_NOT_SUPPORTED );
4410
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004411 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004412 {
4413#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004414 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4415 operation->core_alg = PSA_ALG_CCM;
4416 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004417 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4418 * The call to mbedtls_ccm_encrypt_and_tag or
4419 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004420 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004421 return( PSA_ERROR_INVALID_ARGUMENT );
4422 mbedtls_ccm_init( &operation->ctx.ccm );
4423 status = mbedtls_to_psa_error(
4424 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004425 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004426 (unsigned int) key_bits ) );
4427 if( status != 0 )
4428 goto cleanup;
4429 break;
4430#endif /* MBEDTLS_CCM_C */
4431
4432#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004433 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4434 operation->core_alg = PSA_ALG_GCM;
4435 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004436 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4437 * The call to mbedtls_gcm_crypt_and_tag or
4438 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004439 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004440 return( PSA_ERROR_INVALID_ARGUMENT );
4441 mbedtls_gcm_init( &operation->ctx.gcm );
4442 status = mbedtls_to_psa_error(
4443 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004444 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004445 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004446 if( status != 0 )
4447 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004448 break;
4449#endif /* MBEDTLS_GCM_C */
4450
Gilles Peskine26869f22019-05-06 15:25:00 +02004451#if defined(MBEDTLS_CHACHAPOLY_C)
4452 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4453 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4454 operation->full_tag_length = 16;
4455 /* We only support the default tag length. */
4456 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4457 return( PSA_ERROR_NOT_SUPPORTED );
4458 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4459 status = mbedtls_to_psa_error(
4460 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004461 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004462 if( status != 0 )
4463 goto cleanup;
4464 break;
4465#endif /* MBEDTLS_CHACHAPOLY_C */
4466
Gilles Peskineedf9a652018-08-17 18:11:56 +02004467 default:
4468 return( PSA_ERROR_NOT_SUPPORTED );
4469 }
4470
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004471 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4472 {
4473 status = PSA_ERROR_INVALID_ARGUMENT;
4474 goto cleanup;
4475 }
4476 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004477
Gilles Peskineedf9a652018-08-17 18:11:56 +02004478 return( PSA_SUCCESS );
4479
4480cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004481 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004482 return( status );
4483}
4484
Gilles Peskinec5487a82018-12-03 18:08:14 +01004485psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004486 psa_algorithm_t alg,
4487 const uint8_t *nonce,
4488 size_t nonce_length,
4489 const uint8_t *additional_data,
4490 size_t additional_data_length,
4491 const uint8_t *plaintext,
4492 size_t plaintext_length,
4493 uint8_t *ciphertext,
4494 size_t ciphertext_size,
4495 size_t *ciphertext_length )
4496{
mohammad16035955c982018-04-26 00:53:03 +03004497 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004498 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004499 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004500
mohammad1603f08a5502018-06-03 15:05:47 +03004501 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004502
Gilles Peskinec5487a82018-12-03 18:08:14 +01004503 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004504 if( status != PSA_SUCCESS )
4505 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004506
Gilles Peskineedf9a652018-08-17 18:11:56 +02004507 /* For all currently supported modes, the tag is at the end of the
4508 * ciphertext. */
4509 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4510 {
4511 status = PSA_ERROR_BUFFER_TOO_SMALL;
4512 goto exit;
4513 }
4514 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004515
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004516#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004517 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004518 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004519 status = mbedtls_to_psa_error(
4520 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4521 MBEDTLS_GCM_ENCRYPT,
4522 plaintext_length,
4523 nonce, nonce_length,
4524 additional_data, additional_data_length,
4525 plaintext, ciphertext,
4526 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004527 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004528 else
4529#endif /* MBEDTLS_GCM_C */
4530#if defined(MBEDTLS_CCM_C)
4531 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004532 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004533 status = mbedtls_to_psa_error(
4534 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4535 plaintext_length,
4536 nonce, nonce_length,
4537 additional_data,
4538 additional_data_length,
4539 plaintext, ciphertext,
4540 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004541 }
mohammad16035c8845f2018-05-09 05:40:09 -07004542 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004543#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004544#if defined(MBEDTLS_CHACHAPOLY_C)
4545 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4546 {
4547 if( nonce_length != 12 || operation.tag_length != 16 )
4548 {
4549 status = PSA_ERROR_NOT_SUPPORTED;
4550 goto exit;
4551 }
4552 status = mbedtls_to_psa_error(
4553 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4554 plaintext_length,
4555 nonce,
4556 additional_data,
4557 additional_data_length,
4558 plaintext,
4559 ciphertext,
4560 tag ) );
4561 }
4562 else
4563#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004564 {
mohammad1603554faad2018-06-03 15:07:38 +03004565 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004566 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004567
Gilles Peskineedf9a652018-08-17 18:11:56 +02004568 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4569 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004570
Gilles Peskineedf9a652018-08-17 18:11:56 +02004571exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004572 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004573 if( status == PSA_SUCCESS )
4574 *ciphertext_length = plaintext_length + operation.tag_length;
4575 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004576}
4577
Gilles Peskineee652a32018-06-01 19:23:52 +02004578/* Locate the tag in a ciphertext buffer containing the encrypted data
4579 * followed by the tag. Return the length of the part preceding the tag in
4580 * *plaintext_length. This is the size of the plaintext in modes where
4581 * the encrypted data has the same size as the plaintext, such as
4582 * CCM and GCM. */
4583static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4584 const uint8_t *ciphertext,
4585 size_t ciphertext_length,
4586 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004587 const uint8_t **p_tag )
4588{
4589 size_t payload_length;
4590 if( tag_length > ciphertext_length )
4591 return( PSA_ERROR_INVALID_ARGUMENT );
4592 payload_length = ciphertext_length - tag_length;
4593 if( payload_length > plaintext_size )
4594 return( PSA_ERROR_BUFFER_TOO_SMALL );
4595 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004596 return( PSA_SUCCESS );
4597}
4598
Gilles Peskinec5487a82018-12-03 18:08:14 +01004599psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004600 psa_algorithm_t alg,
4601 const uint8_t *nonce,
4602 size_t nonce_length,
4603 const uint8_t *additional_data,
4604 size_t additional_data_length,
4605 const uint8_t *ciphertext,
4606 size_t ciphertext_length,
4607 uint8_t *plaintext,
4608 size_t plaintext_size,
4609 size_t *plaintext_length )
4610{
mohammad16035955c982018-04-26 00:53:03 +03004611 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004612 aead_operation_t operation;
4613 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004614
Gilles Peskineee652a32018-06-01 19:23:52 +02004615 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004616
Gilles Peskinec5487a82018-12-03 18:08:14 +01004617 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004618 if( status != PSA_SUCCESS )
4619 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004620
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004621 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4622 ciphertext, ciphertext_length,
4623 plaintext_size, &tag );
4624 if( status != PSA_SUCCESS )
4625 goto exit;
4626
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004627#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004628 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004629 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004630 status = mbedtls_to_psa_error(
4631 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4632 ciphertext_length - operation.tag_length,
4633 nonce, nonce_length,
4634 additional_data,
4635 additional_data_length,
4636 tag, operation.tag_length,
4637 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004638 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004639 else
4640#endif /* MBEDTLS_GCM_C */
4641#if defined(MBEDTLS_CCM_C)
4642 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004643 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004644 status = mbedtls_to_psa_error(
4645 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4646 ciphertext_length - operation.tag_length,
4647 nonce, nonce_length,
4648 additional_data,
4649 additional_data_length,
4650 ciphertext, plaintext,
4651 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004652 }
mohammad160339574652018-06-01 04:39:53 -07004653 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004654#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004655#if defined(MBEDTLS_CHACHAPOLY_C)
4656 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4657 {
4658 if( nonce_length != 12 || operation.tag_length != 16 )
4659 {
4660 status = PSA_ERROR_NOT_SUPPORTED;
4661 goto exit;
4662 }
4663 status = mbedtls_to_psa_error(
4664 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4665 ciphertext_length - operation.tag_length,
4666 nonce,
4667 additional_data,
4668 additional_data_length,
4669 tag,
4670 ciphertext,
4671 plaintext ) );
4672 }
4673 else
4674#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004675 {
mohammad1603554faad2018-06-03 15:07:38 +03004676 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004677 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004678
Gilles Peskineedf9a652018-08-17 18:11:56 +02004679 if( status != PSA_SUCCESS && plaintext_size != 0 )
4680 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004681
Gilles Peskineedf9a652018-08-17 18:11:56 +02004682exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004683 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004684 if( status == PSA_SUCCESS )
4685 *plaintext_length = ciphertext_length - operation.tag_length;
4686 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004687}
4688
Gilles Peskinea0655c32018-04-30 17:06:50 +02004689
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004690
Gilles Peskine20035e32018-02-03 22:44:14 +01004691/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004692/* Generators */
4693/****************************************************************/
4694
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004695#define HKDF_STATE_INIT 0 /* no input yet */
4696#define HKDF_STATE_STARTED 1 /* got salt */
4697#define HKDF_STATE_KEYED 2 /* got key */
4698#define HKDF_STATE_OUTPUT 3 /* output started */
4699
Gilles Peskinecbe66502019-05-16 16:59:18 +02004700static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004701 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004702{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004703 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4704 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004705 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004706 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004707}
4708
4709
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004710psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004711{
4712 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004713 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004714 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004715 {
4716 /* The object has (apparently) been initialized but it is not
4717 * in use. It's ok to call abort on such an object, and there's
4718 * nothing to do. */
4719 }
4720 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004721#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004722 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004723 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004724 mbedtls_free( operation->ctx.hkdf.info );
4725 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004726 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004727 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004728 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004729 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004730 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004731 if( operation->ctx.tls12_prf.seed != NULL )
4732 {
4733 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4734 operation->ctx.tls12_prf.seed_length );
4735 mbedtls_free( operation->ctx.tls12_prf.seed );
4736 }
4737
4738 if( operation->ctx.tls12_prf.label != NULL )
4739 {
4740 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4741 operation->ctx.tls12_prf.label_length );
4742 mbedtls_free( operation->ctx.tls12_prf.label );
4743 }
4744
4745 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4746
4747 /* We leave the fields Ai and output_block to be erased safely by the
4748 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004749 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004750 else
4751#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004752 {
4753 status = PSA_ERROR_BAD_STATE;
4754 }
Janos Follath083036a2019-06-11 10:22:26 +01004755 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004756 return( status );
4757}
4758
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004759psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004760 size_t *capacity)
4761{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004762 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004763 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004764 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004765 return PSA_ERROR_BAD_STATE;
4766 }
4767
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004768 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004769 return( PSA_SUCCESS );
4770}
4771
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004772psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004773 size_t capacity )
4774{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004775 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004776 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004777 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004778 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004779 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004780 return( PSA_SUCCESS );
4781}
4782
Gilles Peskinebef7f142018-07-12 17:22:21 +02004783#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004784/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004785 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004786static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004787 psa_algorithm_t hash_alg,
4788 uint8_t *output,
4789 size_t output_length )
4790{
4791 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4792 psa_status_t status;
4793
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004794 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4795 return( PSA_ERROR_BAD_STATE );
4796 hkdf->state = HKDF_STATE_OUTPUT;
4797
Gilles Peskinebef7f142018-07-12 17:22:21 +02004798 while( output_length != 0 )
4799 {
4800 /* Copy what remains of the current block */
4801 uint8_t n = hash_length - hkdf->offset_in_block;
4802 if( n > output_length )
4803 n = (uint8_t) output_length;
4804 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4805 output += n;
4806 output_length -= n;
4807 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004808 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004809 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004810 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004811 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004812 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004813 * object was corrupted or if this function is called directly
4814 * inside the library. */
4815 if( hkdf->block_number == 0xff )
4816 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004817
4818 /* We need a new block */
4819 ++hkdf->block_number;
4820 hkdf->offset_in_block = 0;
4821 status = psa_hmac_setup_internal( &hkdf->hmac,
4822 hkdf->prk, hash_length,
4823 hash_alg );
4824 if( status != PSA_SUCCESS )
4825 return( status );
4826 if( hkdf->block_number != 1 )
4827 {
4828 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4829 hkdf->output_block,
4830 hash_length );
4831 if( status != PSA_SUCCESS )
4832 return( status );
4833 }
4834 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4835 hkdf->info,
4836 hkdf->info_length );
4837 if( status != PSA_SUCCESS )
4838 return( status );
4839 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4840 &hkdf->block_number, 1 );
4841 if( status != PSA_SUCCESS )
4842 return( status );
4843 status = psa_hmac_finish_internal( &hkdf->hmac,
4844 hkdf->output_block,
4845 sizeof( hkdf->output_block ) );
4846 if( status != PSA_SUCCESS )
4847 return( status );
4848 }
4849
4850 return( PSA_SUCCESS );
4851}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004852
Janos Follath7742fee2019-06-17 12:58:10 +01004853static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4854 psa_tls12_prf_key_derivation_t *tls12_prf,
4855 psa_algorithm_t alg )
4856{
4857 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4858 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004859 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4860 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004861
Janos Follath7742fee2019-06-17 12:58:10 +01004862 /* We can't be wanting more output after block 0xff, otherwise
4863 * the capacity check in psa_key_derivation_output_bytes() would have
4864 * prevented this call. It could happen only if the operation
4865 * object was corrupted or if this function is called directly
4866 * inside the library. */
4867 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004868 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004869
4870 /* We need a new block */
4871 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004872 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004873
4874 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4875 *
4876 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4877 *
4878 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4879 * HMAC_hash(secret, A(2) + seed) +
4880 * HMAC_hash(secret, A(3) + seed) + ...
4881 *
4882 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004883 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004884 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004885 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004886 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004887 * is currently extracted as `output_block` and where i is
4888 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004889 */
4890
Janos Follathea29bfb2019-06-19 12:21:20 +01004891 /* Save the hash context before using it, to preserve the hash state with
4892 * only the inner padding in it. We need this, because inner padding depends
4893 * on the key (secret in the RFC's terminology). */
4894 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4895 if( status != PSA_SUCCESS )
4896 goto cleanup;
4897
4898 /* Calculate A(i) where i = tls12_prf->block_number. */
4899 if( tls12_prf->block_number == 1 )
4900 {
4901 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4902 * the variable seed and in this instance means it in the context of the
4903 * P_hash function, where seed = label + seed.) */
4904 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4905 tls12_prf->label, tls12_prf->label_length );
4906 if( status != PSA_SUCCESS )
4907 goto cleanup;
4908 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4909 tls12_prf->seed, tls12_prf->seed_length );
4910 if( status != PSA_SUCCESS )
4911 goto cleanup;
4912 }
4913 else
4914 {
4915 /* A(i) = HMAC_hash(secret, A(i-1)) */
4916 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4917 tls12_prf->Ai, hash_length );
4918 if( status != PSA_SUCCESS )
4919 goto cleanup;
4920 }
4921
4922 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4923 tls12_prf->Ai, hash_length );
4924 if( status != PSA_SUCCESS )
4925 goto cleanup;
4926 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4927 if( status != PSA_SUCCESS )
4928 goto cleanup;
4929
4930 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4931 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4932 tls12_prf->Ai, hash_length );
4933 if( status != PSA_SUCCESS )
4934 goto cleanup;
4935 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4936 tls12_prf->label, tls12_prf->label_length );
4937 if( status != PSA_SUCCESS )
4938 goto cleanup;
4939 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4940 tls12_prf->seed, tls12_prf->seed_length );
4941 if( status != PSA_SUCCESS )
4942 goto cleanup;
4943 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4944 tls12_prf->output_block, hash_length );
4945 if( status != PSA_SUCCESS )
4946 goto cleanup;
4947 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4948 if( status != PSA_SUCCESS )
4949 goto cleanup;
4950
Janos Follath7742fee2019-06-17 12:58:10 +01004951
4952cleanup:
4953
Janos Follathea29bfb2019-06-19 12:21:20 +01004954 cleanup_status = psa_hash_abort( &backup );
4955 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4956 status = cleanup_status;
4957
4958 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004959}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004960
Janos Follath844eb0e2019-06-19 12:10:49 +01004961static psa_status_t psa_key_derivation_tls12_prf_read(
4962 psa_tls12_prf_key_derivation_t *tls12_prf,
4963 psa_algorithm_t alg,
4964 uint8_t *output,
4965 size_t output_length )
4966{
4967 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4968 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4969 psa_status_t status;
4970 uint8_t offset, length;
4971
4972 while( output_length != 0 )
4973 {
4974 /* Check if we have fully processed the current block. */
4975 if( tls12_prf->left_in_block == 0 )
4976 {
4977 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4978 alg );
4979 if( status != PSA_SUCCESS )
4980 return( status );
4981
4982 continue;
4983 }
4984
4985 if( tls12_prf->left_in_block > output_length )
4986 length = (uint8_t) output_length;
4987 else
4988 length = tls12_prf->left_in_block;
4989
4990 offset = hash_length - tls12_prf->left_in_block;
4991 memcpy( output, tls12_prf->output_block + offset, length );
4992 output += length;
4993 output_length -= length;
4994 tls12_prf->left_in_block -= length;
4995 }
4996
4997 return( PSA_SUCCESS );
4998}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004999#endif /* MBEDTLS_MD_C */
5000
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005001psa_status_t psa_key_derivation_output_bytes(
5002 psa_key_derivation_operation_t *operation,
5003 uint8_t *output,
5004 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005005{
5006 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005007 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005008
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005009 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005010 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005011 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005012 return PSA_ERROR_BAD_STATE;
5013 }
5014
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005015 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005016 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005017 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005018 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005019 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005020 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005021 goto exit;
5022 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005023 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005024 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005025 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005026 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005027 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5028 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005029 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005030 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005031 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005032 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005033 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005034
Gilles Peskinebef7f142018-07-12 17:22:21 +02005035#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005036 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005037 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005038 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005039 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005040 output, output_length );
5041 }
Janos Follathadbec812019-06-14 11:05:39 +01005042 else
Janos Follathadbec812019-06-14 11:05:39 +01005043 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005044 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005045 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005046 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005047 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005048 output_length );
5049 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005050 else
5051#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005052 {
5053 return( PSA_ERROR_BAD_STATE );
5054 }
5055
5056exit:
5057 if( status != PSA_SUCCESS )
5058 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005059 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005060 * This allows us to differentiate between exhausted operations and
5061 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5062 * operations. */
5063 psa_algorithm_t alg = operation->alg;
5064 psa_key_derivation_abort( operation );
5065 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005066 memset( output, '!', output_length );
5067 }
5068 return( status );
5069}
5070
Gilles Peskine08542d82018-07-19 17:05:42 +02005071#if defined(MBEDTLS_DES_C)
5072static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5073{
5074 if( data_size >= 8 )
5075 mbedtls_des_key_set_parity( data );
5076 if( data_size >= 16 )
5077 mbedtls_des_key_set_parity( data + 8 );
5078 if( data_size >= 24 )
5079 mbedtls_des_key_set_parity( data + 16 );
5080}
5081#endif /* MBEDTLS_DES_C */
5082
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005083static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005084 psa_key_slot_t *slot,
5085 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005086 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005087{
5088 uint8_t *data = NULL;
5089 size_t bytes = PSA_BITS_TO_BYTES( bits );
5090 psa_status_t status;
5091
Gilles Peskine8e338702019-07-30 20:06:31 +02005092 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005093 return( PSA_ERROR_INVALID_ARGUMENT );
5094 if( bits % 8 != 0 )
5095 return( PSA_ERROR_INVALID_ARGUMENT );
5096 data = mbedtls_calloc( 1, bytes );
5097 if( data == NULL )
5098 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5099
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005100 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005101 if( status != PSA_SUCCESS )
5102 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005103#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005104 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005105 psa_des_set_key_parity( data, bytes );
5106#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005107 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005108
5109exit:
5110 mbedtls_free( data );
5111 return( status );
5112}
5113
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005114psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005115 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005116 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005117{
5118 psa_status_t status;
5119 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005120 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005121
5122 /* Reject any attempt to create a zero-length key so that we don't
5123 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5124 if( psa_get_key_bits( attributes ) == 0 )
5125 return( PSA_ERROR_INVALID_ARGUMENT );
5126
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005127 if( ! operation->can_output_key )
5128 return( PSA_ERROR_NOT_PERMITTED );
5129
Gilles Peskinedf179142019-07-15 22:02:14 +02005130 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5131 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005132#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5133 if( driver != NULL )
5134 {
5135 /* Deriving a key in a secure element is not implemented yet. */
5136 status = PSA_ERROR_NOT_SUPPORTED;
5137 }
5138#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005139 if( status == PSA_SUCCESS )
5140 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005141 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005142 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005143 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005144 }
5145 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005146 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005147 if( status != PSA_SUCCESS )
5148 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005149 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005150 *handle = 0;
5151 }
5152 return( status );
5153}
5154
Gilles Peskineeab56e42018-07-12 17:12:33 +02005155
5156
5157/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005158/* Key derivation */
5159/****************************************************************/
5160
Gilles Peskine969c5d62019-01-16 15:53:06 +01005161static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005162 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005163 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005164{
Janos Follath5fe19732019-06-20 15:09:30 +01005165 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5166 * initialisers for this union leave some bytes unspecified.) */
5167 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5168
Gilles Peskine969c5d62019-01-16 15:53:06 +01005169 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005170#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005171 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5172 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5173 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005174 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005175 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005176 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5177 if( hash_size == 0 )
5178 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005179 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5180 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005181 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005182 {
5183 return( PSA_ERROR_NOT_SUPPORTED );
5184 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005185 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005186 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005187 }
5188#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005189 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005190 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005191}
5192
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005193psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005194 psa_algorithm_t alg )
5195{
5196 psa_status_t status;
5197
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005198 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005199 return( PSA_ERROR_BAD_STATE );
5200
Gilles Peskine6843c292019-01-18 16:44:49 +01005201 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5202 return( PSA_ERROR_INVALID_ARGUMENT );
5203 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005204 {
5205 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005206 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005207 }
5208 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5209 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005210 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005211 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005212 else
5213 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005214
5215 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005216 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005217 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005218}
5219
5220#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005221static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005222 psa_algorithm_t hash_alg,
5223 psa_key_derivation_step_t step,
5224 const uint8_t *data,
5225 size_t data_length )
5226{
5227 psa_status_t status;
5228 switch( step )
5229 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005230 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005231 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005232 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005233 status = psa_hmac_setup_internal( &hkdf->hmac,
5234 data, data_length,
5235 hash_alg );
5236 if( status != PSA_SUCCESS )
5237 return( status );
5238 hkdf->state = HKDF_STATE_STARTED;
5239 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005240 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005241 /* If no salt was provided, use an empty salt. */
5242 if( hkdf->state == HKDF_STATE_INIT )
5243 {
5244 status = psa_hmac_setup_internal( &hkdf->hmac,
5245 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005246 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005247 if( status != PSA_SUCCESS )
5248 return( status );
5249 hkdf->state = HKDF_STATE_STARTED;
5250 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005251 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005252 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005253 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5254 data, data_length );
5255 if( status != PSA_SUCCESS )
5256 return( status );
5257 status = psa_hmac_finish_internal( &hkdf->hmac,
5258 hkdf->prk,
5259 sizeof( hkdf->prk ) );
5260 if( status != PSA_SUCCESS )
5261 return( status );
5262 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5263 hkdf->block_number = 0;
5264 hkdf->state = HKDF_STATE_KEYED;
5265 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005266 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005267 if( hkdf->state == HKDF_STATE_OUTPUT )
5268 return( PSA_ERROR_BAD_STATE );
5269 if( hkdf->info_set )
5270 return( PSA_ERROR_BAD_STATE );
5271 hkdf->info_length = data_length;
5272 if( data_length != 0 )
5273 {
5274 hkdf->info = mbedtls_calloc( 1, data_length );
5275 if( hkdf->info == NULL )
5276 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5277 memcpy( hkdf->info, data, data_length );
5278 }
5279 hkdf->info_set = 1;
5280 return( PSA_SUCCESS );
5281 default:
5282 return( PSA_ERROR_INVALID_ARGUMENT );
5283 }
5284}
Janos Follathb03233e2019-06-11 15:30:30 +01005285
Janos Follathf08e2652019-06-13 09:05:41 +01005286static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5287 const uint8_t *data,
5288 size_t data_length )
5289{
5290 if( prf->state != TLS12_PRF_STATE_INIT )
5291 return( PSA_ERROR_BAD_STATE );
5292
Janos Follathd6dce9f2019-07-04 09:11:38 +01005293 if( data_length != 0 )
5294 {
5295 prf->seed = mbedtls_calloc( 1, data_length );
5296 if( prf->seed == NULL )
5297 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005298
Janos Follathd6dce9f2019-07-04 09:11:38 +01005299 memcpy( prf->seed, data, data_length );
5300 prf->seed_length = data_length;
5301 }
Janos Follathf08e2652019-06-13 09:05:41 +01005302
5303 prf->state = TLS12_PRF_STATE_SEED_SET;
5304
5305 return( PSA_SUCCESS );
5306}
5307
Janos Follath81550542019-06-13 14:26:34 +01005308static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5309 psa_algorithm_t hash_alg,
5310 const uint8_t *data,
5311 size_t data_length )
5312{
5313 psa_status_t status;
5314 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5315 return( PSA_ERROR_BAD_STATE );
5316
5317 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5318 if( status != PSA_SUCCESS )
5319 return( status );
5320
5321 prf->state = TLS12_PRF_STATE_KEY_SET;
5322
5323 return( PSA_SUCCESS );
5324}
5325
Janos Follath6660f0e2019-06-17 08:44:03 +01005326static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5327 psa_tls12_prf_key_derivation_t *prf,
5328 psa_algorithm_t hash_alg,
5329 const uint8_t *data,
5330 size_t data_length )
5331{
5332 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005333 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5334 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005335
5336 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5337 return( PSA_ERROR_INVALID_ARGUMENT );
5338
5339 /* Quoting RFC 4279, Section 2:
5340 *
5341 * The premaster secret is formed as follows: if the PSK is N octets
5342 * long, concatenate a uint16 with the value N, N zero octets, a second
5343 * uint16 with the value N, and the PSK itself.
5344 */
5345
Janos Follath40e13932019-06-26 13:22:29 +01005346 *cur++ = ( data_length >> 8 ) & 0xff;
5347 *cur++ = ( data_length >> 0 ) & 0xff;
5348 memset( cur, 0, data_length );
5349 cur += data_length;
5350 *cur++ = pms[0];
5351 *cur++ = pms[1];
5352 memcpy( cur, data, data_length );
5353 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005354
Janos Follath40e13932019-06-26 13:22:29 +01005355 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005356
5357 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5358 return( status );
5359}
5360
Janos Follath63028dd2019-06-13 09:15:47 +01005361static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5362 const uint8_t *data,
5363 size_t data_length )
5364{
5365 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5366 return( PSA_ERROR_BAD_STATE );
5367
Janos Follathd6dce9f2019-07-04 09:11:38 +01005368 if( data_length != 0 )
5369 {
5370 prf->label = mbedtls_calloc( 1, data_length );
5371 if( prf->label == NULL )
5372 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005373
Janos Follathd6dce9f2019-07-04 09:11:38 +01005374 memcpy( prf->label, data, data_length );
5375 prf->label_length = data_length;
5376 }
Janos Follath63028dd2019-06-13 09:15:47 +01005377
5378 prf->state = TLS12_PRF_STATE_LABEL_SET;
5379
5380 return( PSA_SUCCESS );
5381}
5382
Janos Follathb03233e2019-06-11 15:30:30 +01005383static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5384 psa_algorithm_t hash_alg,
5385 psa_key_derivation_step_t step,
5386 const uint8_t *data,
5387 size_t data_length )
5388{
Janos Follathb03233e2019-06-11 15:30:30 +01005389 switch( step )
5390 {
Janos Follathf08e2652019-06-13 09:05:41 +01005391 case PSA_KEY_DERIVATION_INPUT_SEED:
5392 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005393 case PSA_KEY_DERIVATION_INPUT_SECRET:
5394 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005395 case PSA_KEY_DERIVATION_INPUT_LABEL:
5396 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005397 default:
5398 return( PSA_ERROR_INVALID_ARGUMENT );
5399 }
5400}
Janos Follath6660f0e2019-06-17 08:44:03 +01005401
5402static psa_status_t psa_tls12_prf_psk_to_ms_input(
5403 psa_tls12_prf_key_derivation_t *prf,
5404 psa_algorithm_t hash_alg,
5405 psa_key_derivation_step_t step,
5406 const uint8_t *data,
5407 size_t data_length )
5408{
5409 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005410 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005411 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5412 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005413 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005414
5415 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5416}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005417#endif /* MBEDTLS_MD_C */
5418
Gilles Peskineb8965192019-09-24 16:21:10 +02005419/** Check whether the given key type is acceptable for the given
5420 * input step of a key derivation.
5421 *
5422 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5423 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5424 * Both secret and non-secret inputs can alternatively have the type
5425 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5426 * that the input was passed as a buffer rather than via a key object.
5427 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005428static int psa_key_derivation_check_input_type(
5429 psa_key_derivation_step_t step,
5430 psa_key_type_t key_type )
5431{
5432 switch( step )
5433 {
5434 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005435 if( key_type == PSA_KEY_TYPE_DERIVE )
5436 return( PSA_SUCCESS );
5437 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005438 return( PSA_SUCCESS );
5439 break;
5440 case PSA_KEY_DERIVATION_INPUT_LABEL:
5441 case PSA_KEY_DERIVATION_INPUT_SALT:
5442 case PSA_KEY_DERIVATION_INPUT_INFO:
5443 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005444 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5445 return( PSA_SUCCESS );
5446 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005447 return( PSA_SUCCESS );
5448 break;
5449 }
5450 return( PSA_ERROR_INVALID_ARGUMENT );
5451}
5452
Janos Follathb80a94e2019-06-12 15:54:46 +01005453static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005454 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005455 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005456 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005457 const uint8_t *data,
5458 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005459{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005460 psa_status_t status;
5461 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5462
5463 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005464 if( status != PSA_SUCCESS )
5465 goto exit;
5466
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005467#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005468 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005469 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005470 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005471 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005472 step, data, data_length );
5473 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005474 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005475 {
Janos Follathb03233e2019-06-11 15:30:30 +01005476 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5477 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5478 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005479 }
5480 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5481 {
5482 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5483 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5484 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005485 }
5486 else
5487#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005488 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005489 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005490 return( PSA_ERROR_BAD_STATE );
5491 }
5492
Gilles Peskine224b0d62019-09-23 18:13:17 +02005493exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005494 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005495 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005496 return( status );
5497}
5498
Janos Follath51f4a0f2019-06-14 11:35:55 +01005499psa_status_t psa_key_derivation_input_bytes(
5500 psa_key_derivation_operation_t *operation,
5501 psa_key_derivation_step_t step,
5502 const uint8_t *data,
5503 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005504{
Gilles Peskineb8965192019-09-24 16:21:10 +02005505 return( psa_key_derivation_input_internal( operation, step,
5506 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005507 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005508}
5509
Janos Follath51f4a0f2019-06-14 11:35:55 +01005510psa_status_t psa_key_derivation_input_key(
5511 psa_key_derivation_operation_t *operation,
5512 psa_key_derivation_step_t step,
5513 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005514{
5515 psa_key_slot_t *slot;
5516 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005517
Gilles Peskine28f8f302019-07-24 13:30:31 +02005518 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005519 PSA_KEY_USAGE_DERIVE,
5520 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005521 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005522 {
5523 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005524 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005525 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005526
5527 /* Passing a key object as a SECRET input unlocks the permission
5528 * to output to a key object. */
5529 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5530 operation->can_output_key = 1;
5531
Janos Follathb80a94e2019-06-12 15:54:46 +01005532 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005533 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005534 slot->data.key.data,
5535 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005536}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005537
5538
5539
5540/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005541/* Key agreement */
5542/****************************************************************/
5543
Gilles Peskinea05219c2018-11-16 16:02:56 +01005544#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005545static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5546 size_t peer_key_length,
5547 const mbedtls_ecp_keypair *our_key,
5548 uint8_t *shared_secret,
5549 size_t shared_secret_size,
5550 size_t *shared_secret_length )
5551{
Steven Cooremana2371e52020-07-28 14:30:39 +02005552 mbedtls_ecp_keypair *their_key;
Steven Cooremanacda8342020-07-24 23:09:52 +02005553 psa_key_slot_t their_key_slot;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005554 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005555 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005556 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005557 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005558 mbedtls_ecdh_init( &ecdh );
Steven Cooremanacda8342020-07-24 23:09:52 +02005559 memset(&their_key_slot, 0, sizeof(their_key_slot));
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005560
Steven Cooremanacda8342020-07-24 23:09:52 +02005561 /* Creating ephemeral key slot for import purposes */
5562 their_key_slot.attr.type = PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve);
5563 their_key_slot.data.key.data = (uint8_t*) peer_key;
5564 their_key_slot.data.key.bytes = peer_key_length;
5565
5566 status = psa_load_ecp_representation( &their_key_slot, &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005567 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005568 goto exit;
5569
Jaeden Amero97271b32019-01-10 19:38:51 +00005570 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005571 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005572 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005573 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005574 status = mbedtls_to_psa_error(
5575 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5576 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005577 goto exit;
5578
Jaeden Amero97271b32019-01-10 19:38:51 +00005579 status = mbedtls_to_psa_error(
5580 mbedtls_ecdh_calc_secret( &ecdh,
5581 shared_secret_length,
5582 shared_secret, shared_secret_size,
5583 mbedtls_ctr_drbg_random,
5584 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005585 if( status != PSA_SUCCESS )
5586 goto exit;
5587 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5588 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005589
5590exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005591 if( status != PSA_SUCCESS )
5592 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005593 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005594 mbedtls_ecp_keypair_free( their_key );
5595 if( their_key != NULL)
5596 mbedtls_free( their_key );
5597
Jaeden Amero97271b32019-01-10 19:38:51 +00005598 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005599}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005600#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005601
Gilles Peskine01d718c2018-09-18 12:01:02 +02005602#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5603
Gilles Peskine0216fe12019-04-11 21:23:21 +02005604static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5605 psa_key_slot_t *private_key,
5606 const uint8_t *peer_key,
5607 size_t peer_key_length,
5608 uint8_t *shared_secret,
5609 size_t shared_secret_size,
5610 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005611{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005612 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005613 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005614#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005615 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005616 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005617 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005618 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooremanacda8342020-07-24 23:09:52 +02005619 psa_status_t status = psa_load_ecp_representation( private_key, &ecp );
5620 if( status != PSA_SUCCESS )
5621 return status;
5622 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005623 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005624 shared_secret, shared_secret_size,
5625 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005626 mbedtls_ecp_keypair_free( ecp );
5627 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005628 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005629#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005630 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005631 (void) private_key;
5632 (void) peer_key;
5633 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005634 (void) shared_secret;
5635 (void) shared_secret_size;
5636 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005637 return( PSA_ERROR_NOT_SUPPORTED );
5638 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005639}
5640
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005641/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005642 * to potentially free embedded data structures and wipe confidential data.
5643 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005644static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005645 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005646 psa_key_slot_t *private_key,
5647 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005648 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005649{
5650 psa_status_t status;
5651 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5652 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005653 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005654
5655 /* Step 1: run the secret agreement algorithm to generate the shared
5656 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005657 status = psa_key_agreement_raw_internal( ka_alg,
5658 private_key,
5659 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005660 shared_secret,
5661 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005662 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005663 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005664 goto exit;
5665
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005666 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005667 * the shared secret. A shared secret is permitted wherever a key
5668 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005669 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005670 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005671 shared_secret,
5672 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005673
Gilles Peskine12313cd2018-06-20 00:20:32 +02005674exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005675 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005676 return( status );
5677}
5678
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005679psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005680 psa_key_derivation_step_t step,
5681 psa_key_handle_t private_key,
5682 const uint8_t *peer_key,
5683 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005684{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005685 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005686 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005687 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005688 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005689 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005690 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005691 if( status != PSA_SUCCESS )
5692 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005693 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005694 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005695 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005696 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005697 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005698 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005699}
5700
Gilles Peskinebe697d82019-05-16 18:00:41 +02005701psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5702 psa_key_handle_t private_key,
5703 const uint8_t *peer_key,
5704 size_t peer_key_length,
5705 uint8_t *output,
5706 size_t output_size,
5707 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005708{
5709 psa_key_slot_t *slot;
5710 psa_status_t status;
5711
5712 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5713 {
5714 status = PSA_ERROR_INVALID_ARGUMENT;
5715 goto exit;
5716 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005717 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005718 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005719 if( status != PSA_SUCCESS )
5720 goto exit;
5721
5722 status = psa_key_agreement_raw_internal( alg, slot,
5723 peer_key, peer_key_length,
5724 output, output_size,
5725 output_length );
5726
5727exit:
5728 if( status != PSA_SUCCESS )
5729 {
5730 /* If an error happens and is not handled properly, the output
5731 * may be used as a key to protect sensitive data. Arrange for such
5732 * a key to be random, which is likely to result in decryption or
5733 * verification errors. This is better than filling the buffer with
5734 * some constant data such as zeros, which would result in the data
5735 * being protected with a reproducible, easily knowable key.
5736 */
5737 psa_generate_random( output, output_size );
5738 *output_length = output_size;
5739 }
5740 return( status );
5741}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005742
5743
5744/****************************************************************/
5745/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005746/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005747
Gilles Peskine4c317f42018-07-12 01:24:09 +02005748psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005749 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005750{
Janos Follath24eed8d2019-11-22 13:21:35 +00005751 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005752 GUARD_MODULE_INITIALIZED;
5753
Gilles Peskinef181eca2019-08-07 13:49:00 +02005754 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5755 {
5756 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5757 output,
5758 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5759 if( ret != 0 )
5760 return( mbedtls_to_psa_error( ret ) );
5761 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5762 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5763 }
5764
Gilles Peskinee59236f2018-01-27 23:32:46 +01005765 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5766 return( mbedtls_to_psa_error( ret ) );
5767}
5768
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005769#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5770#include "mbedtls/entropy_poll.h"
5771
Gilles Peskine7228da22019-07-15 11:06:15 +02005772psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005773 size_t seed_size )
5774{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005775 if( global_data.initialized )
5776 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005777
5778 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5779 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5780 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5781 return( PSA_ERROR_INVALID_ARGUMENT );
5782
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005783 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005784}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005785#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005786
Gilles Peskinee56e8782019-04-26 17:34:02 +02005787#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5788static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5789 size_t domain_parameters_size,
5790 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005791{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005792 size_t i;
5793 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005794
Gilles Peskinee56e8782019-04-26 17:34:02 +02005795 if( domain_parameters_size == 0 )
5796 {
5797 *exponent = 65537;
5798 return( PSA_SUCCESS );
5799 }
5800
5801 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5802 * support values that fit in a 32-bit integer, which is larger than
5803 * int on just about every platform anyway. */
5804 if( domain_parameters_size > sizeof( acc ) )
5805 return( PSA_ERROR_NOT_SUPPORTED );
5806 for( i = 0; i < domain_parameters_size; i++ )
5807 acc = ( acc << 8 ) | domain_parameters[i];
5808 if( acc > INT_MAX )
5809 return( PSA_ERROR_NOT_SUPPORTED );
5810 *exponent = acc;
5811 return( PSA_SUCCESS );
5812}
5813#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5814
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005815static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005816 psa_key_slot_t *slot, size_t bits,
5817 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005818{
Gilles Peskine8e338702019-07-30 20:06:31 +02005819 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005820
Gilles Peskinee56e8782019-04-26 17:34:02 +02005821 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005822 return( PSA_ERROR_INVALID_ARGUMENT );
5823
Gilles Peskinee59236f2018-01-27 23:32:46 +01005824 if( key_type_is_raw_bytes( type ) )
5825 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005826 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005827
5828 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005829 if( status != PSA_SUCCESS )
5830 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005831
5832 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02005833 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
5834 if( status != PSA_SUCCESS )
5835 return status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005836
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005837 status = psa_generate_random( slot->data.key.data,
5838 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005839 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005840 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005841
5842 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005843#if defined(MBEDTLS_DES_C)
5844 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005845 psa_des_set_key_parity( slot->data.key.data,
5846 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005847#endif /* MBEDTLS_DES_C */
5848 }
5849 else
5850
5851#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005852 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005853 {
Steven Cooremana01795d2020-07-24 22:48:15 +02005854 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005855 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005856 int exponent;
5857 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005858 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5859 return( PSA_ERROR_NOT_SUPPORTED );
5860 /* Accept only byte-aligned keys, for the same reasons as
5861 * in psa_import_rsa_key(). */
5862 if( bits % 8 != 0 )
5863 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005864 status = psa_read_rsa_exponent( domain_parameters,
5865 domain_parameters_size,
5866 &exponent );
5867 if( status != PSA_SUCCESS )
5868 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02005869 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5870 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005871 mbedtls_ctr_drbg_random,
5872 &global_data.ctr_drbg,
5873 (unsigned int) bits,
5874 exponent );
5875 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005876 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02005877
5878 /* Make sure to always have an export representation available */
5879 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
5880
Steven Cooreman75b74362020-07-28 14:30:13 +02005881 status = psa_allocate_buffer_to_slot( slot, bytes );
5882 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02005883 {
5884 mbedtls_rsa_free( &rsa );
Steven Cooreman75b74362020-07-28 14:30:13 +02005885 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005886 }
Steven Cooremana01795d2020-07-24 22:48:15 +02005887
5888 status = psa_export_rsa_key( type,
5889 &rsa,
5890 slot->data.key.data,
5891 bytes,
5892 &slot->data.key.bytes );
5893 mbedtls_rsa_free( &rsa );
5894 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02005895 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02005896 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005897 }
5898 else
5899#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5900
5901#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005902 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005903 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01005904 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005905 mbedtls_ecp_group_id grp_id =
5906 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005907 const mbedtls_ecp_curve_info *curve_info =
5908 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02005909 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005910 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005911 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005912 return( PSA_ERROR_NOT_SUPPORTED );
5913 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5914 return( PSA_ERROR_NOT_SUPPORTED );
5915 if( curve_info->bit_size != bits )
5916 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +02005917 mbedtls_ecp_keypair_init( &ecp );
5918 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005919 mbedtls_ctr_drbg_random,
5920 &global_data.ctr_drbg );
5921 if( ret != 0 )
5922 {
Steven Cooremanacda8342020-07-24 23:09:52 +02005923 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005924 return( mbedtls_to_psa_error( ret ) );
5925 }
Steven Cooremanacda8342020-07-24 23:09:52 +02005926
5927
5928 /* Make sure to always have an export representation available */
5929 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02005930 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
5931 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02005932 {
5933 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman75b74362020-07-28 14:30:13 +02005934 return status;
Steven Cooremanacda8342020-07-24 23:09:52 +02005935 }
Steven Cooreman75b74362020-07-28 14:30:13 +02005936
5937 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02005938 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
5939
5940 mbedtls_ecp_keypair_free( &ecp );
5941 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02005942 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02005943 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005944 }
5945 else
5946#endif /* MBEDTLS_ECP_C */
5947
5948 return( PSA_ERROR_NOT_SUPPORTED );
5949
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005950 return( PSA_SUCCESS );
5951}
Darryl Green0c6575a2018-11-07 16:05:30 +00005952
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005953psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005954 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005955{
5956 psa_status_t status;
5957 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005958 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005959
Gilles Peskine0f84d622019-09-12 19:03:13 +02005960 /* Reject any attempt to create a zero-length key so that we don't
5961 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5962 if( psa_get_key_bits( attributes ) == 0 )
5963 return( PSA_ERROR_INVALID_ARGUMENT );
5964
Gilles Peskinedf179142019-07-15 22:02:14 +02005965 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5966 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005967 if( status != PSA_SUCCESS )
5968 goto exit;
5969
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005970#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5971 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005972 {
Gilles Peskine11792082019-08-06 18:36:36 +02005973 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5974 size_t pubkey_length = 0; /* We don't support this feature yet */
5975 if( drv->key_management == NULL ||
5976 drv->key_management->p_generate == NULL )
5977 {
5978 status = PSA_ERROR_NOT_SUPPORTED;
5979 goto exit;
5980 }
5981 status = drv->key_management->p_generate(
5982 psa_get_se_driver_context( driver ),
5983 slot->data.se.slot_number, attributes,
5984 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005985 }
Gilles Peskine11792082019-08-06 18:36:36 +02005986 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005987#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005988 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005989 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005990 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005991 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005992 }
Gilles Peskine11792082019-08-06 18:36:36 +02005993
5994exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005995 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005996 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005997 if( status != PSA_SUCCESS )
5998 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005999 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006000 *handle = 0;
6001 }
Darryl Green0c6575a2018-11-07 16:05:30 +00006002 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006003}
6004
6005
Gilles Peskinee59236f2018-01-27 23:32:46 +01006006
6007/****************************************************************/
6008/* Module setup */
6009/****************************************************************/
6010
Gilles Peskine5e769522018-11-20 21:59:56 +01006011psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6012 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6013 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6014{
6015 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6016 return( PSA_ERROR_BAD_STATE );
6017 global_data.entropy_init = entropy_init;
6018 global_data.entropy_free = entropy_free;
6019 return( PSA_SUCCESS );
6020}
6021
Gilles Peskinee59236f2018-01-27 23:32:46 +01006022void mbedtls_psa_crypto_free( void )
6023{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006024 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006025 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6026 {
6027 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006028 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006029 }
6030 /* Wipe all remaining data, including configuration.
6031 * In particular, this sets all state indicator to the value
6032 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006033 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006034#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006035 /* Unregister all secure element drivers, so that we restart from
6036 * a pristine state. */
6037 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006038#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006039}
6040
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006041#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6042/** Recover a transaction that was interrupted by a power failure.
6043 *
6044 * This function is called during initialization, before psa_crypto_init()
6045 * returns. If this function returns a failure status, the initialization
6046 * fails.
6047 */
6048static psa_status_t psa_crypto_recover_transaction(
6049 const psa_crypto_transaction_t *transaction )
6050{
6051 switch( transaction->unknown.type )
6052 {
6053 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6054 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006055 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006056 * is implemented.
6057 * https://github.com/ARMmbed/mbed-crypto/issues/218
6058 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006059 default:
6060 /* We found an unsupported transaction in the storage.
6061 * We don't know what state the storage is in. Give up. */
6062 return( PSA_ERROR_STORAGE_FAILURE );
6063 }
6064}
6065#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6066
Gilles Peskinee59236f2018-01-27 23:32:46 +01006067psa_status_t psa_crypto_init( void )
6068{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006069 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006070 const unsigned char drbg_seed[] = "PSA";
6071
Gilles Peskinec6b69072018-11-20 21:42:52 +01006072 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006073 if( global_data.initialized != 0 )
6074 return( PSA_SUCCESS );
6075
Gilles Peskine5e769522018-11-20 21:59:56 +01006076 /* Set default configuration if
6077 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6078 if( global_data.entropy_init == NULL )
6079 global_data.entropy_init = mbedtls_entropy_init;
6080 if( global_data.entropy_free == NULL )
6081 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006082
Gilles Peskinec6b69072018-11-20 21:42:52 +01006083 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006084 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006085#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6086 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6087 /* The PSA entropy injection feature depends on using NV seed as an entropy
6088 * source. Add NV seed as an entropy source for PSA entropy injection. */
6089 mbedtls_entropy_add_source( &global_data.entropy,
6090 mbedtls_nv_seed_poll, NULL,
6091 MBEDTLS_ENTROPY_BLOCK_SIZE,
6092 MBEDTLS_ENTROPY_SOURCE_STRONG );
6093#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006094 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006095 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006096 status = mbedtls_to_psa_error(
6097 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6098 mbedtls_entropy_func,
6099 &global_data.entropy,
6100 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6101 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006102 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006103 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006104
Gilles Peskine66fb1262018-12-10 16:29:04 +01006105 status = psa_initialize_key_slots( );
6106 if( status != PSA_SUCCESS )
6107 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006108
Gilles Peskined9348f22019-10-01 15:22:29 +02006109#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6110 status = psa_init_all_se_drivers( );
6111 if( status != PSA_SUCCESS )
6112 goto exit;
6113#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6114
Gilles Peskine4b734222019-07-24 15:56:31 +02006115#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006116 status = psa_crypto_load_transaction( );
6117 if( status == PSA_SUCCESS )
6118 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006119 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6120 if( status != PSA_SUCCESS )
6121 goto exit;
6122 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006123 }
6124 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6125 {
6126 /* There's no transaction to complete. It's all good. */
6127 status = PSA_SUCCESS;
6128 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006129#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006130
Gilles Peskinec6b69072018-11-20 21:42:52 +01006131 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006132 global_data.initialized = 1;
6133
6134exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006135 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006136 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006137 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006138}
6139
6140#endif /* MBEDTLS_PSA_CRYPTO_C */