blob: 056e67796cd45634928f55c46bb36690af316133 [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 Cooreman7f391872020-07-30 14:57:44 +0200521/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200522 *
Steven Cooreman7f391872020-07-30 14:57:44 +0200523 * \param[in] buffer The buffer from which to load the representation.
524 * \param[in] size The size in bytes of \p buffer.
525 * \param[in] type The type of key contained in the \p buffer.
Steven Cooremana2371e52020-07-28 14:30:39 +0200526 * \param[out] p_rsa Returns a pointer to an RSA context on success.
527 * The caller is responsible for freeing both the
528 * contents of the context and the context itself
529 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200530 */
Steven Cooreman7f391872020-07-30 14:57:44 +0200531static psa_status_t psa_load_rsa_representation( const uint8_t *buffer,
532 size_t size,
533 psa_key_type_t type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200534 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200535{
Steven Cooremana01795d2020-07-24 22:48:15 +0200536#if defined(MBEDTLS_PK_PARSE_C)
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000537 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200538 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000539 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200540 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000541
542 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200543 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000544 status = mbedtls_to_psa_error(
Steven Cooreman7f391872020-07-30 14:57:44 +0200545 mbedtls_pk_parse_key( &ctx, buffer, size, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200546 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000547 status = mbedtls_to_psa_error(
Steven Cooreman7f391872020-07-30 14:57:44 +0200548 mbedtls_pk_parse_public_key( &ctx, buffer, size ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000549 if( status != PSA_SUCCESS )
550 goto exit;
551
552 /* We have something that the pkparse module recognizes. If it is a
553 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200554 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200555 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000556 status = PSA_ERROR_INVALID_ARGUMENT;
557 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200558 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000559
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000560 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
561 * supports non-byte-aligned key sizes, but not well. For example,
562 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200563 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000564 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
565 {
566 status = PSA_ERROR_NOT_SUPPORTED;
567 goto exit;
568 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200569 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
570
571 if( status != PSA_SUCCESS )
572 goto exit;
573
Steven Cooremana2371e52020-07-28 14:30:39 +0200574 /* Copy out the pointer to the RSA context, and reset the PK context
575 * such that pk_free doesn't free the RSA context we just grabbed. */
576 *p_rsa = mbedtls_pk_rsa( ctx );
577 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000578
579exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200580 mbedtls_pk_free( &ctx );
581 return( status );
582#else
Steven Cooreman7f391872020-07-30 14:57:44 +0200583 (void) buffer;
584 (void) size;
585 (void) type;
Steven Cooremana01795d2020-07-24 22:48:15 +0200586 (void) rsa;
587 return( PSA_ERROR_NOT_SUPPORTED );
588#endif /* MBEDTLS_PK_PARSE_C */
589}
590
591/** Export an RSA key to export representation
592 *
593 * \param[in] type The type of key (public/private) to export
594 * \param[in] rsa The internal RSA representation from which to export
595 * \param[out] data The buffer to export to
596 * \param[in] data_size The length of the buffer to export to
597 * \param[out] data_length The amount of bytes written to \p data
598 */
599static psa_status_t psa_export_rsa_key( psa_key_type_t type,
600 mbedtls_rsa_context *rsa,
601 uint8_t *data,
602 size_t data_size,
603 size_t *data_length )
604{
605#if defined(MBEDTLS_PK_WRITE_C)
606 int ret;
607 mbedtls_pk_context pk;
608 uint8_t *pos = data + data_size;
609
610 mbedtls_pk_init( &pk );
611 pk.pk_info = &mbedtls_rsa_info;
612 pk.pk_ctx = rsa;
613
614 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200615 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
616 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200617 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
618 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
619 else
620 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
621
622 if( ret < 0 )
623 return mbedtls_to_psa_error( ret );
624
625 /* The mbedtls_pk_xxx functions write to the end of the buffer.
626 * Move the data to the beginning and erase remaining data
627 * at the original location. */
628 if( 2 * (size_t) ret <= data_size )
629 {
630 memcpy( data, data + data_size - ret, ret );
631 memset( data + data_size - ret, 0, ret );
632 }
633 else if( (size_t) ret < data_size )
634 {
635 memmove( data, data + data_size - ret, ret );
636 memset( data + ret, 0, data_size - ret );
637 }
638
639 *data_length = ret;
640 return( PSA_SUCCESS );
641#else
642 (void) type;
643 (void) rsa;
644 (void) data;
645 (void) data_size;
646 (void) data_length;
647 return( PSA_ERROR_NOT_SUPPORTED );
648#endif /* MBEDTLS_PK_WRITE_C */
649}
650
651/** Import an RSA key from import representation to a slot
652 *
653 * \param[in,out] slot The slot where to store the export representation to
654 * \param[in] data The buffer containing the import representation
655 * \param[in] data_length The amount of bytes in \p data
656 */
657static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
658 const uint8_t *data,
659 size_t data_length )
660{
661 psa_status_t status;
662 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200663 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200664
Steven Cooremana01795d2020-07-24 22:48:15 +0200665 /* Parse input */
Steven Cooreman7f391872020-07-30 14:57:44 +0200666 status = psa_load_rsa_representation( data,
667 data_length,
668 slot->attr.type,
669 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200670 if( status != PSA_SUCCESS )
671 goto exit;
672
673 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200674 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200675
Steven Cooreman75b74362020-07-28 14:30:13 +0200676 /* Re-export the data to PSA export format, such that we can store export
677 * representation in the key slot. Export representation in case of RSA is
678 * the smallest representation that's allowed as input, so a straight-up
679 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200680 output = mbedtls_calloc( 1, data_length );
681
682 if( output == NULL )
683 {
684 status = PSA_ERROR_INSUFFICIENT_MEMORY;
685 goto exit;
686 }
687
Steven Cooremana01795d2020-07-24 22:48:15 +0200688 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200689 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200690 output,
691 data_length,
692 &data_length);
693
694exit:
695 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200696 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200697 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200698
699 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000700 if( status != PSA_SUCCESS )
701 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200702 mbedtls_free( output );
703 slot->data.key.data = NULL;
704 slot->data.key.bytes = 0;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000705 return( status );
706 }
707
Steven Cooremana01795d2020-07-24 22:48:15 +0200708 /* On success, store the allocated export-formatted key. */
709 slot->data.key.data = output;
710 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000711
712 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200713}
Steven Cooremana01795d2020-07-24 22:48:15 +0200714#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200715
Jaeden Ameroccdce902019-01-10 11:42:27 +0000716#if defined(MBEDTLS_ECP_C)
Steven Cooreman7f391872020-07-30 14:57:44 +0200717/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200718 *
Steven Cooreman7f391872020-07-30 14:57:44 +0200719 * \param[in] buffer The buffer from which to load the representation.
720 * \param[in] size The size in bytes of \p buffer.
721 * \param[in] type The type of key contained in the \p buffer.
Steven Cooremana2371e52020-07-28 14:30:39 +0200722 * \param[out] p_ecp Returns a pointer to an ECP context on success.
723 * The caller is responsible for freeing both the
724 * contents of the context and the context itself
725 * when done.
726 */
Steven Cooreman7f391872020-07-30 14:57:44 +0200727static psa_status_t psa_load_ecp_representation( const uint8_t *buffer,
728 size_t size,
729 psa_key_type_t type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200730 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100731{
732 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200733 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200734 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman3fa684e2020-07-30 15:04:07 +0200735 size_t curve_size;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100736
Steven Cooreman7f391872020-07-30 14:57:44 +0200737 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100738 {
Steven Cooreman3fa684e2020-07-30 15:04:07 +0200739 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
740 {
741 /* A Montgomery public key is represented as its raw
742 * compressed public point.
743 */
744 curve_size = size;
745 }
746 else
747 {
748 /* A Weierstrass public key is represented as:
749 * - The byte 0x04;
750 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
751 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
752 * So its data length is 2m+1 where n is the key size in bits.
753 */
754 if( ( size & 1 ) == 0 )
755 return( PSA_ERROR_INVALID_ARGUMENT );
756 curve_size = size / 2;
757 }
758 }
759 else
760 {
761 /* Private keys are represented as the raw private value */
762 curve_size = size;
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100763 }
764
Steven Cooreman6d839f02020-07-30 11:36:45 +0200765 /* Allocate and initialize a key representation. */
766 ecp = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
767 if( ecp == NULL )
768 return PSA_ERROR_INSUFFICIENT_MEMORY;
Steven Cooremana2371e52020-07-28 14:30:39 +0200769 mbedtls_ecp_keypair_init( ecp );
770
Gilles Peskine4cd32772019-12-02 20:49:42 +0100771 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200772 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
773 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100774 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200775 {
776 status = PSA_ERROR_INVALID_ARGUMENT;
777 goto exit;
778 }
779
Steven Cooremanacda8342020-07-24 23:09:52 +0200780 status = mbedtls_to_psa_error(
781 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
782 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200783 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200784
Steven Cooreman6d839f02020-07-30 11:36:45 +0200785 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200786 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200787 {
788 /* Load the public value. */
789 status = mbedtls_to_psa_error(
790 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman7f391872020-07-30 14:57:44 +0200791 buffer,
792 size ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200793 if( status != PSA_SUCCESS )
794 goto exit;
795
796 /* Check that the point is on the curve. */
797 status = mbedtls_to_psa_error(
798 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
799 if( status != PSA_SUCCESS )
800 goto exit;
801 }
802 else
803 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200804 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200805 status = mbedtls_to_psa_error(
806 mbedtls_ecp_read_key( ecp->grp.id,
807 ecp,
Steven Cooreman7f391872020-07-30 14:57:44 +0200808 buffer,
809 size ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200810
811 if( status != PSA_SUCCESS )
812 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200813 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200814
815 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200816exit:
817 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200818 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200819 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200820 mbedtls_free( ecp );
821 }
822
Steven Cooremanacda8342020-07-24 23:09:52 +0200823 return status;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100824}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000825
Steven Cooremanacda8342020-07-24 23:09:52 +0200826static psa_status_t psa_export_ecp_key( psa_key_type_t type,
827 mbedtls_ecp_keypair *ecp,
828 uint8_t *data,
829 size_t data_size,
830 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200831{
Steven Cooremanacda8342020-07-24 23:09:52 +0200832 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000833
Steven Cooremanacda8342020-07-24 23:09:52 +0200834 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200835 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200836 /* Check whether the public part is loaded */
837 if( mbedtls_ecp_is_zero( &ecp->Q ) )
838 {
839 /* Calculate the public key */
840 status = mbedtls_to_psa_error(
841 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
842 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
843 if( status != PSA_SUCCESS )
844 return status;
845 }
846
847 return( mbedtls_to_psa_error(
848 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
849 MBEDTLS_ECP_PF_UNCOMPRESSED,
850 data_length,
851 data,
852 data_size ) ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200853 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200854 else
855 {
856 if( data_size < PSA_BITS_TO_BYTES(ecp->grp.nbits) )
857 return( PSA_ERROR_BUFFER_TOO_SMALL );
858
859 status = mbedtls_to_psa_error(
860 mbedtls_ecp_write_key( ecp,
861 data,
862 PSA_BITS_TO_BYTES(ecp->grp.nbits) ) );
863 if( status == PSA_SUCCESS )
864 {
865 *data_length = PSA_BITS_TO_BYTES(ecp->grp.nbits);
866 }
867
868 return( status );
869 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200870}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200871
Steven Cooremanacda8342020-07-24 23:09:52 +0200872static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
873 const uint8_t *data,
874 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100875{
Steven Cooremanacda8342020-07-24 23:09:52 +0200876 psa_status_t status;
877 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200878 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100879
Steven Cooremanacda8342020-07-24 23:09:52 +0200880 /* Parse input */
Steven Cooreman7f391872020-07-30 14:57:44 +0200881 status = psa_load_ecp_representation( data,
882 data_length,
883 slot->attr.type,
884 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100885 if( status != PSA_SUCCESS )
886 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100887
Steven Cooremanacda8342020-07-24 23:09:52 +0200888 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200889 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200890 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200891 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200892
Steven Cooremanacda8342020-07-24 23:09:52 +0200893 /* Re-export the data to PSA export format. There is currently no support
894 * for other input formats then the export format, so this is a 1-1
895 * copy operation. */
896 output = mbedtls_calloc( 1, data_length );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100897
Steven Cooremanacda8342020-07-24 23:09:52 +0200898 if( output == NULL )
899 {
900 status = PSA_ERROR_INSUFFICIENT_MEMORY;
901 goto exit;
902 }
903
904 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200905 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200906 output,
907 data_length,
908 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100909
910exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200911 /* Always free the PK object (will also free contained ECP context) */
912 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200913 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200914
915 /* Free the allocated buffer only on error. */
916 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100917 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200918 mbedtls_free( output );
919 slot->data.key.data = NULL;
920 slot->data.key.bytes = 0;
921 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100922 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200923
924 /* On success, store the allocated export-formatted key. */
925 slot->data.key.data = output;
926 slot->data.key.bytes = data_length;
927
928 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100929}
930#endif /* defined(MBEDTLS_ECP_C) */
931
Gilles Peskineb46bef22019-07-30 21:32:04 +0200932/** Return the size of the key in the given slot, in bits.
933 *
934 * \param[in] slot A key slot.
935 *
936 * \return The key size in bits, read from the metadata in the slot.
937 */
938static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
939{
940 return( slot->attr.bits );
941}
942
Steven Cooreman75b74362020-07-28 14:30:13 +0200943/** Try to allocate a buffer to an empty key slot.
944 *
945 * \param[in,out] slot Key slot to attach buffer to.
946 * \param[in] buffer_length Requested size of the buffer.
947 *
948 * \retval #PSA_SUCCESS
949 * The buffer has been successfully allocated.
950 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
951 * Not enough memory was available for allocation.
952 * \retval #PSA_ERROR_ALREADY_EXISTS
953 * Trying to allocate a buffer to a non-empty key slot.
954 */
955static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
956 size_t buffer_length )
957{
958 if( slot->data.key.data != NULL )
959 return PSA_ERROR_ALREADY_EXISTS;
960
961 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
962 if( slot->data.key.data == NULL )
963 return PSA_ERROR_INSUFFICIENT_MEMORY;
964
965 slot->data.key.bytes = buffer_length;
966 return PSA_SUCCESS;
967}
968
Gilles Peskine8e338702019-07-30 20:06:31 +0200969/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100970 * previously. This function assumes that the slot does not contain
971 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100972psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
973 const uint8_t *data,
974 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100975{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200976 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200978 /* zero-length keys are never supported. */
979 if( data_length == 0 )
980 return( PSA_ERROR_NOT_SUPPORTED );
981
Gilles Peskine8e338702019-07-30 20:06:31 +0200982 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100983 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200984 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200985
Steven Cooreman75b74362020-07-28 14:30:13 +0200986 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
987 if( data_length > SIZE_MAX / 8 )
988 return( PSA_ERROR_NOT_SUPPORTED );
989
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200990 /* Enforce a size limit, and in particular ensure that the bit
991 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200992 if( bit_size > PSA_MAX_KEY_BITS )
993 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200994
995 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200996 if( status != PSA_SUCCESS )
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200997 return status;
998
999 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02001000 status = psa_allocate_buffer_to_slot( slot, data_length );
1001 if( status != PSA_SUCCESS )
1002 return status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001003
1004 /* copy key into allocated buffer */
1005 memcpy(slot->data.key.data, data, data_length);
1006
1007 /* Write the actual key size to the slot.
1008 * psa_start_key_creation() wrote the size declared by the
1009 * caller, which may be 0 (meaning unspecified) or wrong. */
1010 slot->attr.bits = (psa_key_bits_t) bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001011 }
Steven Cooreman19fd5742020-07-24 23:31:01 +02001012 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1013 {
Gilles Peskinef76aa772018-10-29 19:24:33 +01001014#if defined(MBEDTLS_ECP_C)
Steven Cooreman19fd5742020-07-24 23:31:01 +02001015 status = psa_import_ecp_key( slot,
1016 data, data_length );
1017#else
1018 /* No drivers have been implemented yet, so without mbed TLS backing
1019 * there's no way to do ECP with the current library. */
1020 return( PSA_ERROR_NOT_SUPPORTED );
1021#endif /* defined(MBEDTLS_ECP_C) */
1022 }
1023 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +01001024 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001025#if defined(MBEDTLS_RSA_C)
1026 status = psa_import_rsa_key( slot,
1027 data, data_length );
1028#else
1029 /* No drivers have been implemented yet, so without mbed TLS backing
1030 * there's no way to do RSA with the current library. */
1031 status = PSA_ERROR_NOT_SUPPORTED;
1032#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001033 }
1034 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001035 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001036 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001037 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001038 }
Darryl Green940d72c2018-07-13 13:18:51 +01001039
Darryl Green06fd18d2018-07-16 11:21:11 +01001040 return( status );
1041}
1042
Gilles Peskinef603c712019-01-19 13:40:11 +01001043/** Calculate the intersection of two algorithm usage policies.
1044 *
1045 * Return 0 (which allows no operation) on incompatibility.
1046 */
1047static psa_algorithm_t psa_key_policy_algorithm_intersection(
1048 psa_algorithm_t alg1,
1049 psa_algorithm_t alg2 )
1050{
Gilles Peskine549ea862019-05-22 11:45:59 +02001051 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001052 if( alg1 == alg2 )
1053 return( alg1 );
1054 /* If the policies are from the same hash-and-sign family, check
1055 * if one is a wildcard. If so the other has the specific algorithm. */
1056 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1057 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1058 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1059 {
1060 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1061 return( alg2 );
1062 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1063 return( alg1 );
1064 }
1065 /* If the policies are incompatible, allow nothing. */
1066 return( 0 );
1067}
1068
Gilles Peskined6f371b2019-05-10 19:33:38 +02001069static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1070 psa_algorithm_t requested_alg )
1071{
Gilles Peskine549ea862019-05-22 11:45:59 +02001072 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001073 if( requested_alg == policy_alg )
1074 return( 1 );
1075 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001076 * and requested_alg is the same hash-and-sign family with any hash,
1077 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001078 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1079 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1080 {
1081 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1082 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1083 }
1084 /* If it isn't permitted, it's forbidden. */
1085 return( 0 );
1086}
1087
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001088/** Test whether a policy permits an algorithm.
1089 *
1090 * The caller must test usage flags separately.
1091 */
1092static int psa_key_policy_permits( const psa_key_policy_t *policy,
1093 psa_algorithm_t alg )
1094{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001095 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1096 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001097}
1098
Gilles Peskinef603c712019-01-19 13:40:11 +01001099/** Restrict a key policy based on a constraint.
1100 *
1101 * \param[in,out] policy The policy to restrict.
1102 * \param[in] constraint The policy constraint to apply.
1103 *
1104 * \retval #PSA_SUCCESS
1105 * \c *policy contains the intersection of the original value of
1106 * \c *policy and \c *constraint.
1107 * \retval #PSA_ERROR_INVALID_ARGUMENT
1108 * \c *policy and \c *constraint are incompatible.
1109 * \c *policy is unchanged.
1110 */
1111static psa_status_t psa_restrict_key_policy(
1112 psa_key_policy_t *policy,
1113 const psa_key_policy_t *constraint )
1114{
1115 psa_algorithm_t intersection_alg =
1116 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001117 psa_algorithm_t intersection_alg2 =
1118 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001119 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1120 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001121 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1122 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001123 policy->usage &= constraint->usage;
1124 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001125 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001126 return( PSA_SUCCESS );
1127}
1128
Darryl Green06fd18d2018-07-16 11:21:11 +01001129/** Retrieve a slot which must contain a key. The key must have allow all the
1130 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1131 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001132static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001133 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001134 psa_key_usage_t usage,
1135 psa_algorithm_t alg )
1136{
1137 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001138 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001139
1140 *p_slot = NULL;
1141
Gilles Peskinec5487a82018-12-03 18:08:14 +01001142 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001143 if( status != PSA_SUCCESS )
1144 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001145
1146 /* Enforce that usage policy for the key slot contains all the flags
1147 * required by the usage parameter. There is one exception: public
1148 * keys can always be exported, so we treat public key objects as
1149 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001150 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001151 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001152 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001153 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001154
1155 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001156 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001157 return( PSA_ERROR_NOT_PERMITTED );
1158
1159 *p_slot = slot;
1160 return( PSA_SUCCESS );
1161}
Darryl Green940d72c2018-07-13 13:18:51 +01001162
Gilles Peskine28f8f302019-07-24 13:30:31 +02001163/** Retrieve a slot which must contain a transparent key.
1164 *
1165 * A transparent key is a key for which the key material is directly
1166 * available, as opposed to a key in a secure element.
1167 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001168 * This is a temporary function to use instead of psa_get_key_from_slot()
1169 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001170 */
1171#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1172static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1173 psa_key_slot_t **p_slot,
1174 psa_key_usage_t usage,
1175 psa_algorithm_t alg )
1176{
1177 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1178 if( status != PSA_SUCCESS )
1179 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001180 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001181 {
1182 *p_slot = NULL;
1183 return( PSA_ERROR_NOT_SUPPORTED );
1184 }
1185 return( PSA_SUCCESS );
1186}
1187#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1188/* With no secure element support, all keys are transparent. */
1189#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1190 psa_get_key_from_slot( handle, p_slot, usage, alg )
1191#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1192
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001193/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001194static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001195{
Steven Cooreman75b74362020-07-28 14:30:13 +02001196 /* Check whether key is already clean */
1197 if( slot->data.key.data == NULL )
1198 return PSA_SUCCESS;
1199
Gilles Peskine73167e12019-07-12 23:44:37 +02001200#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1201 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001202 {
1203 /* No key material to clean. */
1204 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001205 else
1206#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +02001207 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +00001208 {
1209 /* No key material to clean. */
1210 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001211 else if( key_type_is_raw_bytes( slot->attr.type ) ||
1212 PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1213 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001214 {
Steven Cooremana01795d2020-07-24 22:48:15 +02001215 mbedtls_free( slot->data.key.data );
1216 slot->data.key.data = NULL;
1217 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001218 }
1219 else
Darryl Green40225ba2018-11-15 14:48:15 +00001220 {
1221 /* Shouldn't happen: the key type is not any type that we
1222 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001223 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +00001224 }
1225
1226 return( PSA_SUCCESS );
1227}
1228
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001229/** Completely wipe a slot in memory, including its policy.
1230 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001231psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001232{
1233 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001234 /* Multipart operations may still be using the key. This is safe
1235 * because all multipart operation objects are independent from
1236 * the key slot: if they need to access the key after the setup
1237 * phase, they have a copy of the key. Note that this means that
1238 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001239 /* At this point, key material and other type-specific content has
1240 * been wiped. Clear remaining metadata. We can call memset and not
1241 * zeroize because the metadata is not particularly sensitive. */
1242 memset( slot, 0, sizeof( *slot ) );
1243 return( status );
1244}
1245
Gilles Peskinec5487a82018-12-03 18:08:14 +01001246psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001247{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001248 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001249 psa_status_t status; /* status of the last operation */
1250 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001251#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1252 psa_se_drv_table_entry_t *driver;
1253#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001254
Gilles Peskine1841cf42019-10-08 15:48:25 +02001255 if( handle == 0 )
1256 return( PSA_SUCCESS );
1257
Gilles Peskinec5487a82018-12-03 18:08:14 +01001258 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001259 if( status != PSA_SUCCESS )
1260 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001261
1262#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001263 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001264 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001265 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001266 /* For a key in a secure element, we need to do three things:
1267 * remove the key file in internal storage, destroy the
1268 * key inside the secure element, and update the driver's
1269 * persistent data. Start a transaction that will encompass these
1270 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001271 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001272 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001273 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001274 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001275 status = psa_crypto_save_transaction( );
1276 if( status != PSA_SUCCESS )
1277 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001278 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001279 /* We should still try to destroy the key in the secure
1280 * element and the key metadata in storage. This is especially
1281 * important if the error is that the storage is full.
1282 * But how to do it exactly without risking an inconsistent
1283 * state after a reset?
1284 * https://github.com/ARMmbed/mbed-crypto/issues/215
1285 */
1286 overall_status = status;
1287 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001288 }
1289
Gilles Peskine354f7672019-07-12 23:46:38 +02001290 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001291 if( overall_status == PSA_SUCCESS )
1292 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001293 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001294#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1295
Darryl Greend49a4992018-06-18 17:27:26 +01001296#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001297 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001298 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001299 status = psa_destroy_persistent_key( slot->attr.id );
1300 if( overall_status == PSA_SUCCESS )
1301 overall_status = status;
1302
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001303 /* TODO: other slots may have a copy of the same key. We should
1304 * invalidate them.
1305 * https://github.com/ARMmbed/mbed-crypto/issues/214
1306 */
Darryl Greend49a4992018-06-18 17:27:26 +01001307 }
1308#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001309
Gilles Peskinefc762652019-07-22 19:30:34 +02001310#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1311 if( driver != NULL )
1312 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001313 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001314 if( overall_status == PSA_SUCCESS )
1315 overall_status = status;
1316 status = psa_crypto_stop_transaction( );
1317 if( overall_status == PSA_SUCCESS )
1318 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001319 }
1320#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1321
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001322#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1323exit:
1324#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001325 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001326 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1327 if( overall_status == PSA_SUCCESS )
1328 overall_status = status;
1329 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001330}
1331
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001332void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001333{
Gilles Peskineb699f072019-04-26 16:06:02 +02001334 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001335 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001336}
1337
Gilles Peskineb699f072019-04-26 16:06:02 +02001338psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1339 psa_key_type_t type,
1340 const uint8_t *data,
1341 size_t data_length )
1342{
1343 uint8_t *copy = NULL;
1344
1345 if( data_length != 0 )
1346 {
1347 copy = mbedtls_calloc( 1, data_length );
1348 if( copy == NULL )
1349 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1350 memcpy( copy, data, data_length );
1351 }
1352 /* After this point, this function is guaranteed to succeed, so it
1353 * can start modifying `*attributes`. */
1354
1355 if( attributes->domain_parameters != NULL )
1356 {
1357 mbedtls_free( attributes->domain_parameters );
1358 attributes->domain_parameters = NULL;
1359 attributes->domain_parameters_size = 0;
1360 }
1361
1362 attributes->domain_parameters = copy;
1363 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001364 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001365 return( PSA_SUCCESS );
1366}
1367
1368psa_status_t psa_get_key_domain_parameters(
1369 const psa_key_attributes_t *attributes,
1370 uint8_t *data, size_t data_size, size_t *data_length )
1371{
1372 if( attributes->domain_parameters_size > data_size )
1373 return( PSA_ERROR_BUFFER_TOO_SMALL );
1374 *data_length = attributes->domain_parameters_size;
1375 if( attributes->domain_parameters_size != 0 )
1376 memcpy( data, attributes->domain_parameters,
1377 attributes->domain_parameters_size );
1378 return( PSA_SUCCESS );
1379}
1380
1381#if defined(MBEDTLS_RSA_C)
1382static psa_status_t psa_get_rsa_public_exponent(
1383 const mbedtls_rsa_context *rsa,
1384 psa_key_attributes_t *attributes )
1385{
1386 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001387 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001388 uint8_t *buffer = NULL;
1389 size_t buflen;
1390 mbedtls_mpi_init( &mpi );
1391
1392 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1393 if( ret != 0 )
1394 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001395 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1396 {
1397 /* It's the default value, which is reported as an empty string,
1398 * so there's nothing to do. */
1399 goto exit;
1400 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001401
1402 buflen = mbedtls_mpi_size( &mpi );
1403 buffer = mbedtls_calloc( 1, buflen );
1404 if( buffer == NULL )
1405 {
1406 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1407 goto exit;
1408 }
1409 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1410 if( ret != 0 )
1411 goto exit;
1412 attributes->domain_parameters = buffer;
1413 attributes->domain_parameters_size = buflen;
1414
1415exit:
1416 mbedtls_mpi_free( &mpi );
1417 if( ret != 0 )
1418 mbedtls_free( buffer );
1419 return( mbedtls_to_psa_error( ret ) );
1420}
1421#endif /* MBEDTLS_RSA_C */
1422
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001423/** Retrieve all the publicly-accessible attributes of a key.
1424 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001425psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1426 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001427{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001428 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001429 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001430
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001431 psa_reset_key_attributes( attributes );
1432
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001433 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001434 if( status != PSA_SUCCESS )
1435 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001436
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001437 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001438 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1439 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1440
1441#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1442 if( psa_key_slot_is_external( slot ) )
1443 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1444#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001445
Gilles Peskine8e338702019-07-30 20:06:31 +02001446 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001447 {
1448#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001449 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001450 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001451#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001452 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001453 * is not yet implemented.
1454 * https://github.com/ARMmbed/mbed-crypto/issues/216
1455 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001456 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001457 break;
1458#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001459 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001460 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001461
Steven Cooreman7f391872020-07-30 14:57:44 +02001462 status = psa_load_rsa_representation( slot->data.key.data,
1463 slot->data.key.bytes,
1464 slot->attr.type,
1465 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001466 if( status != PSA_SUCCESS )
1467 break;
1468
Steven Cooremana2371e52020-07-28 14:30:39 +02001469 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001470 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001471 mbedtls_rsa_free( rsa );
1472 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001473 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001474 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001475#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001476 default:
1477 /* Nothing else to do. */
1478 break;
1479 }
1480
1481 if( status != PSA_SUCCESS )
1482 psa_reset_key_attributes( attributes );
1483 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001484}
1485
Gilles Peskinec8000c02019-08-02 20:15:51 +02001486#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1487psa_status_t psa_get_key_slot_number(
1488 const psa_key_attributes_t *attributes,
1489 psa_key_slot_number_t *slot_number )
1490{
1491 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1492 {
1493 *slot_number = attributes->slot_number;
1494 return( PSA_SUCCESS );
1495 }
1496 else
1497 return( PSA_ERROR_INVALID_ARGUMENT );
1498}
1499#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1500
Steven Cooremana01795d2020-07-24 22:48:15 +02001501static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1502 uint8_t *data,
1503 size_t data_size,
1504 size_t *data_length )
1505{
1506 if( slot->data.key.bytes > data_size )
1507 return( PSA_ERROR_BUFFER_TOO_SMALL );
1508 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1509 memset( data + slot->data.key.bytes, 0,
1510 data_size - slot->data.key.bytes );
1511 *data_length = slot->data.key.bytes;
1512 return( PSA_SUCCESS );
1513}
1514
Gilles Peskinef603c712019-01-19 13:40:11 +01001515static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1516 uint8_t *data,
1517 size_t data_size,
1518 size_t *data_length,
1519 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001520{
Gilles Peskine5d309672019-07-12 23:47:28 +02001521#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1522 const psa_drv_se_t *drv;
1523 psa_drv_se_context_t *drv_context;
1524#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1525
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001526 *data_length = 0;
1527
Gilles Peskine8e338702019-07-30 20:06:31 +02001528 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001529 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001530
Gilles Peskinef9168942019-09-12 19:20:29 +02001531 /* Reject a zero-length output buffer now, since this can never be a
1532 * valid key representation. This way we know that data must be a valid
1533 * pointer and we can do things like memset(data, ..., data_size). */
1534 if( data_size == 0 )
1535 return( PSA_ERROR_BUFFER_TOO_SMALL );
1536
Gilles Peskine5d309672019-07-12 23:47:28 +02001537#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001538 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001539 {
1540 psa_drv_se_export_key_t method;
1541 if( drv->key_management == NULL )
1542 return( PSA_ERROR_NOT_SUPPORTED );
1543 method = ( export_public_key ?
1544 drv->key_management->p_export_public :
1545 drv->key_management->p_export );
1546 if( method == NULL )
1547 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001548 return( method( drv_context,
1549 slot->data.se.slot_number,
1550 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001551 }
1552#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1553
Gilles Peskine8e338702019-07-30 20:06:31 +02001554 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001555 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001556 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001557 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001558 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1559 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001560 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001561 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001562 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001563 /* Exporting public -> public */
1564 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1565 }
1566 else if( !export_public_key )
1567 {
1568 /* Exporting private -> private */
1569 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1570 }
1571 /* Need to export the public part of a private key,
1572 * so conversion is needed */
1573 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1574 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001575#if defined(MBEDTLS_RSA_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001576 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001577 psa_status_t status = psa_load_rsa_representation(
1578 slot->data.key.data,
1579 slot->data.key.bytes,
1580 slot->attr.type,
1581 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001582 if( status != PSA_SUCCESS )
1583 return status;
Steven Cooremana01795d2020-07-24 22:48:15 +02001584
Steven Cooreman560c28a2020-07-24 23:20:24 +02001585 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001586 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001587 data,
1588 data_size,
1589 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001590
Steven Cooremana2371e52020-07-28 14:30:39 +02001591 mbedtls_rsa_free( rsa );
1592 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001593
Steven Cooreman560c28a2020-07-24 23:20:24 +02001594 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001595#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001596 /* We don't know how to convert a private RSA key to public. */
1597 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001598#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001599 }
1600 else
Moran Pekera998bc62018-04-16 18:16:20 +03001601 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001602#if defined(MBEDTLS_ECP_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001603 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001604 psa_status_t status = psa_load_ecp_representation(
1605 slot->data.key.data,
1606 slot->data.key.bytes,
1607 slot->attr.type,
1608 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001609 if( status != PSA_SUCCESS )
1610 return status;
1611
1612 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1613 PSA_KEY_TYPE_ECC_GET_FAMILY(
1614 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001615 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001616 data,
1617 data_size,
1618 data_length );
1619
Steven Cooremana2371e52020-07-28 14:30:39 +02001620 mbedtls_ecp_keypair_free( ecp );
1621 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001622 return( status );
1623#else
1624 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001625 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001626#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001627 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001628 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001629 else
1630 {
1631 /* This shouldn't happen in the reference implementation, but
1632 it is valid for a special-purpose implementation to omit
1633 support for exporting certain key types. */
1634 return( PSA_ERROR_NOT_SUPPORTED );
1635 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001636}
1637
Gilles Peskinec5487a82018-12-03 18:08:14 +01001638psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001639 uint8_t *data,
1640 size_t data_size,
1641 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001642{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001643 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001644 psa_status_t status;
1645
1646 /* Set the key to empty now, so that even when there are errors, we always
1647 * set data_length to a value between 0 and data_size. On error, setting
1648 * the key to empty is a good choice because an empty key representation is
1649 * unlikely to be accepted anywhere. */
1650 *data_length = 0;
1651
1652 /* Export requires the EXPORT flag. There is an exception for public keys,
1653 * which don't require any flag, but psa_get_key_from_slot takes
1654 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001655 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001656 if( status != PSA_SUCCESS )
1657 return( status );
1658 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001659 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001660}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001661
Gilles Peskinec5487a82018-12-03 18:08:14 +01001662psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001663 uint8_t *data,
1664 size_t data_size,
1665 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001666{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001667 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001668 psa_status_t status;
1669
1670 /* Set the key to empty now, so that even when there are errors, we always
1671 * set data_length to a value between 0 and data_size. On error, setting
1672 * the key to empty is a good choice because an empty key representation is
1673 * unlikely to be accepted anywhere. */
1674 *data_length = 0;
1675
1676 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001677 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001678 if( status != PSA_SUCCESS )
1679 return( status );
1680 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001681 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001682}
1683
Gilles Peskine91e8c332019-08-02 19:19:39 +02001684#if defined(static_assert)
1685static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1686 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001687static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001688 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001689static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001690 "One or more key attribute flag is listed as both internal-only and external-only" );
1691#endif
1692
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001693/** Validate that a key policy is internally well-formed.
1694 *
1695 * This function only rejects invalid policies. It does not validate the
1696 * consistency of the policy with respect to other attributes of the key
1697 * such as the key type.
1698 */
1699static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001700{
1701 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001702 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001703 PSA_KEY_USAGE_ENCRYPT |
1704 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001705 PSA_KEY_USAGE_SIGN_HASH |
1706 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001707 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1708 return( PSA_ERROR_INVALID_ARGUMENT );
1709
Gilles Peskine4747d192019-04-17 15:05:45 +02001710 return( PSA_SUCCESS );
1711}
1712
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001713/** Validate the internal consistency of key attributes.
1714 *
1715 * This function only rejects invalid attribute values. If does not
1716 * validate the consistency of the attributes with any key data that may
1717 * be involved in the creation of the key.
1718 *
1719 * Call this function early in the key creation process.
1720 *
1721 * \param[in] attributes Key attributes for the new key.
1722 * \param[out] p_drv On any return, the driver for the key, if any.
1723 * NULL for a transparent key.
1724 *
1725 */
1726static psa_status_t psa_validate_key_attributes(
1727 const psa_key_attributes_t *attributes,
1728 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001729{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001730 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001731
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001732 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1733 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001734 if( status != PSA_SUCCESS )
1735 return( status );
1736
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001737 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1738 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001739 if( status != PSA_SUCCESS )
1740 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001741
1742 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001743 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001744 return( status );
1745
1746 /* Refuse to create overly large keys.
1747 * Note that this doesn't trigger on import if the attributes don't
1748 * explicitly specify a size (so psa_get_key_bits returns 0), so
1749 * psa_import_key() needs its own checks. */
1750 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1751 return( PSA_ERROR_NOT_SUPPORTED );
1752
Gilles Peskine91e8c332019-08-02 19:19:39 +02001753 /* Reject invalid flags. These should not be reachable through the API. */
1754 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1755 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1756 return( PSA_ERROR_INVALID_ARGUMENT );
1757
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001758 return( PSA_SUCCESS );
1759}
1760
Gilles Peskine4747d192019-04-17 15:05:45 +02001761/** Prepare a key slot to receive key material.
1762 *
1763 * This function allocates a key slot and sets its metadata.
1764 *
1765 * If this function fails, call psa_fail_key_creation().
1766 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001767 * This function is intended to be used as follows:
1768 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1769 * it with the specified attributes, and assign it a handle.
1770 * -# Populate the slot with the key material.
1771 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1772 * In case of failure at any step, stop the sequence and call
1773 * psa_fail_key_creation().
1774 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001775 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001776 * \param[in] attributes Key attributes for the new key.
1777 * \param[out] handle On success, a handle for the allocated slot.
1778 * \param[out] p_slot On success, a pointer to the prepared slot.
1779 * \param[out] p_drv On any return, the driver for the key, if any.
1780 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001781 *
1782 * \retval #PSA_SUCCESS
1783 * The key slot is ready to receive key material.
1784 * \return If this function fails, the key slot is an invalid state.
1785 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001786 */
1787static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001788 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001789 const psa_key_attributes_t *attributes,
1790 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001791 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001792 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001793{
1794 psa_status_t status;
1795 psa_key_slot_t *slot;
1796
Gilles Peskinedf179142019-07-15 22:02:14 +02001797 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001798 *p_drv = NULL;
1799
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001800 status = psa_validate_key_attributes( attributes, p_drv );
1801 if( status != PSA_SUCCESS )
1802 return( status );
1803
Gilles Peskineedbed562019-08-07 18:19:59 +02001804 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001805 if( status != PSA_SUCCESS )
1806 return( status );
1807 slot = *p_slot;
1808
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001809 /* We're storing the declared bit-size of the key. It's up to each
1810 * creation mechanism to verify that this information is correct.
1811 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001812 * an input (generate, device) but not for those where the bit-size
1813 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001814
1815 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001816
Gilles Peskine91e8c332019-08-02 19:19:39 +02001817 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001818 * external-only flags, query `attributes`. Thanks to the check
1819 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001820 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001821 * may have set. */
1822 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001823
Gilles Peskinecbaff462019-07-12 23:46:04 +02001824#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001825 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001826 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001827 * create the key file in internal storage, create the
1828 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001829 * persistent data. This is done by starting a transaction that will
1830 * encompass these three actions.
1831 * For registering a volatile key, we just need to find an appropriate
1832 * slot number inside the SE. Since the key is designated volatile, creating
1833 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001834 /* The first thing to do is to find a slot number for the new key.
1835 * We save the slot number in persistent storage as part of the
1836 * transaction data. It will be needed to recover if the power
1837 * fails during the key creation process, to clean up on the secure
1838 * element side after restarting. Obtaining a slot number from the
1839 * secure element driver updates its persistent state, but we do not yet
1840 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001841 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001842 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001843 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001844 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001845 &slot->data.se.slot_number );
1846 if( status != PSA_SUCCESS )
1847 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001848
1849 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001850 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001851 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1852 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1853 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1854 psa_crypto_transaction.key.id = slot->attr.id;
1855 status = psa_crypto_save_transaction( );
1856 if( status != PSA_SUCCESS )
1857 {
1858 (void) psa_crypto_stop_transaction( );
1859 return( status );
1860 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001861 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001862 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001863
1864 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1865 {
1866 /* Key registration only makes sense with a secure element. */
1867 return( PSA_ERROR_INVALID_ARGUMENT );
1868 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001869#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1870
Darryl Green0c6575a2018-11-07 16:05:30 +00001871 return( status );
1872}
Gilles Peskine4747d192019-04-17 15:05:45 +02001873
1874/** Finalize the creation of a key once its key material has been set.
1875 *
1876 * This entails writing the key to persistent storage.
1877 *
1878 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001879 * See the documentation of psa_start_key_creation() for the intended use
1880 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001881 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001882 * \param[in,out] slot Pointer to the slot with key material.
1883 * \param[in] driver The secure element driver for the key,
1884 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001885 *
1886 * \retval #PSA_SUCCESS
1887 * The key was successfully created. The handle is now valid.
1888 * \return If this function fails, the key slot is an invalid state.
1889 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001890 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001891static psa_status_t psa_finish_key_creation(
1892 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001893 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001894{
1895 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001896 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001897 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001898
1899#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001900 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001901 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001902#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1903 if( driver != NULL )
1904 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001905 psa_se_key_data_storage_t data;
1906#if defined(static_assert)
1907 static_assert( sizeof( slot->data.se.slot_number ) ==
1908 sizeof( data.slot_number ),
1909 "Slot number size does not match psa_se_key_data_storage_t" );
1910 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1911 "Bit-size size does not match psa_se_key_data_storage_t" );
1912#endif
1913 memcpy( &data.slot_number, &slot->data.se.slot_number,
1914 sizeof( slot->data.se.slot_number ) );
1915 memcpy( &data.bits, &slot->attr.bits,
1916 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001917 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001918 (uint8_t*) &data,
1919 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001920 }
1921 else
1922#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1923 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001924 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001925 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001926 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001927 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1928 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001929 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001930 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1931 status = psa_internal_export_key( slot,
1932 buffer, buffer_size, &length,
1933 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001934 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001935 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001936 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001937
Gilles Peskinef9168942019-09-12 19:20:29 +02001938 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001939 mbedtls_free( buffer );
1940 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001941 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001942#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1943
Gilles Peskinecbaff462019-07-12 23:46:04 +02001944#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001945 /* Finish the transaction for a key creation. This does not
1946 * happen when registering an existing key. Detect this case
1947 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001948 * creation of a persistent key in a secure element requires a transaction,
1949 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001950 if( driver != NULL &&
1951 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001952 {
1953 status = psa_save_se_persistent_data( driver );
1954 if( status != PSA_SUCCESS )
1955 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001956 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001957 return( status );
1958 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001959 status = psa_crypto_stop_transaction( );
1960 if( status != PSA_SUCCESS )
1961 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001962 }
1963#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1964
Gilles Peskine4747d192019-04-17 15:05:45 +02001965 return( status );
1966}
1967
1968/** Abort the creation of a key.
1969 *
1970 * You may call this function after calling psa_start_key_creation(),
1971 * or after psa_finish_key_creation() fails. In other circumstances, this
1972 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001973 * See the documentation of psa_start_key_creation() for the intended use
1974 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001975 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001976 * \param[in,out] slot Pointer to the slot with key material.
1977 * \param[in] driver The secure element driver for the key,
1978 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001979 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001980static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001981 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001982{
Gilles Peskine011e4282019-06-26 18:34:38 +02001983 (void) driver;
1984
Gilles Peskine4747d192019-04-17 15:05:45 +02001985 if( slot == NULL )
1986 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001987
1988#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001989 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001990 * element, and the failure happened later (when saving metadata
1991 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001992 * element.
1993 * https://github.com/ARMmbed/mbed-crypto/issues/217
1994 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001995
Gilles Peskined7729582019-08-05 15:55:54 +02001996 /* Abort the ongoing transaction if any (there may not be one if
1997 * the creation process failed before starting one, or if the
1998 * key creation is a registration of a key in a secure element).
1999 * Earlier functions must already have done what it takes to undo any
2000 * partial creation. All that's left is to update the transaction data
2001 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002002 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002003#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2004
Gilles Peskine4747d192019-04-17 15:05:45 +02002005 psa_wipe_key_slot( slot );
2006}
2007
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002008/** Validate optional attributes during key creation.
2009 *
2010 * Some key attributes are optional during key creation. If they are
2011 * specified in the attributes structure, check that they are consistent
2012 * with the data in the slot.
2013 *
2014 * This function should be called near the end of key creation, after
2015 * the slot in memory is fully populated but before saving persistent data.
2016 */
2017static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002018 const psa_key_slot_t *slot,
2019 const psa_key_attributes_t *attributes )
2020{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002021 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002022 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002023 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002024 return( PSA_ERROR_INVALID_ARGUMENT );
2025 }
2026
2027 if( attributes->domain_parameters_size != 0 )
2028 {
2029#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02002030 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002031 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002032 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002033 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002034 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002035
Steven Cooreman7f391872020-07-30 14:57:44 +02002036 psa_status_t status = psa_load_rsa_representation(
2037 slot->data.key.data,
2038 slot->data.key.bytes,
2039 slot->attr.type,
2040 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002041 if( status != PSA_SUCCESS )
2042 return status;
Steven Cooreman75b74362020-07-28 14:30:13 +02002043
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002044 mbedtls_mpi_init( &actual );
2045 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002046 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002047 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002048 mbedtls_rsa_free( rsa );
2049 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002050 if( ret != 0 )
2051 goto rsa_exit;
2052 ret = mbedtls_mpi_read_binary( &required,
2053 attributes->domain_parameters,
2054 attributes->domain_parameters_size );
2055 if( ret != 0 )
2056 goto rsa_exit;
2057 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2058 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2059 rsa_exit:
2060 mbedtls_mpi_free( &actual );
2061 mbedtls_mpi_free( &required );
2062 if( ret != 0)
2063 return( mbedtls_to_psa_error( ret ) );
2064 }
2065 else
2066#endif
2067 {
2068 return( PSA_ERROR_INVALID_ARGUMENT );
2069 }
2070 }
2071
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002072 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002073 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002074 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002075 return( PSA_ERROR_INVALID_ARGUMENT );
2076 }
2077
2078 return( PSA_SUCCESS );
2079}
2080
Gilles Peskine4747d192019-04-17 15:05:45 +02002081psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002082 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002083 size_t data_length,
2084 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002085{
2086 psa_status_t status;
2087 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002088 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002089
Gilles Peskine0f84d622019-09-12 19:03:13 +02002090 /* Reject zero-length symmetric keys (including raw data key objects).
2091 * This also rejects any key which might be encoded as an empty string,
2092 * which is never valid. */
2093 if( data_length == 0 )
2094 return( PSA_ERROR_INVALID_ARGUMENT );
2095
Gilles Peskinedf179142019-07-15 22:02:14 +02002096 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2097 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002098 if( status != PSA_SUCCESS )
2099 goto exit;
2100
Gilles Peskine5d309672019-07-12 23:47:28 +02002101#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2102 if( driver != NULL )
2103 {
2104 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002105 /* The driver should set the number of key bits, however in
2106 * case it doesn't, we initialize bits to an invalid value. */
2107 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002108 if( drv->key_management == NULL ||
2109 drv->key_management->p_import == NULL )
2110 {
2111 status = PSA_ERROR_NOT_SUPPORTED;
2112 goto exit;
2113 }
2114 status = drv->key_management->p_import(
2115 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002116 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002117 &bits );
2118 if( status != PSA_SUCCESS )
2119 goto exit;
2120 if( bits > PSA_MAX_KEY_BITS )
2121 {
2122 status = PSA_ERROR_NOT_SUPPORTED;
2123 goto exit;
2124 }
2125 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002126 }
2127 else
2128#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2129 {
2130 status = psa_import_key_into_slot( slot, data, data_length );
2131 if( status != PSA_SUCCESS )
2132 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002133 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002134 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002135 if( status != PSA_SUCCESS )
2136 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002137
Gilles Peskine011e4282019-06-26 18:34:38 +02002138 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002139exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002140 if( status != PSA_SUCCESS )
2141 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002142 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002143 *handle = 0;
2144 }
2145 return( status );
2146}
2147
Gilles Peskined7729582019-08-05 15:55:54 +02002148#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2149psa_status_t mbedtls_psa_register_se_key(
2150 const psa_key_attributes_t *attributes )
2151{
2152 psa_status_t status;
2153 psa_key_slot_t *slot = NULL;
2154 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002155 psa_key_handle_t handle = 0;
2156
2157 /* Leaving attributes unspecified is not currently supported.
2158 * It could make sense to query the key type and size from the
2159 * secure element, but not all secure elements support this
2160 * and the driver HAL doesn't currently support it. */
2161 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2162 return( PSA_ERROR_NOT_SUPPORTED );
2163 if( psa_get_key_bits( attributes ) == 0 )
2164 return( PSA_ERROR_NOT_SUPPORTED );
2165
2166 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2167 &handle, &slot, &driver );
2168 if( status != PSA_SUCCESS )
2169 goto exit;
2170
Gilles Peskined7729582019-08-05 15:55:54 +02002171 status = psa_finish_key_creation( slot, driver );
2172
2173exit:
2174 if( status != PSA_SUCCESS )
2175 {
2176 psa_fail_key_creation( slot, driver );
2177 }
2178 /* Registration doesn't keep the key in RAM. */
2179 psa_close_key( handle );
2180 return( status );
2181}
2182#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2183
Gilles Peskinef603c712019-01-19 13:40:11 +01002184static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002185 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002186{
2187 psa_status_t status;
2188 uint8_t *buffer = NULL;
2189 size_t buffer_size = 0;
2190 size_t length;
2191
Gilles Peskine8e338702019-07-30 20:06:31 +02002192 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002193 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01002194 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02002195 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01002196 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01002197 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
2198 if( status != PSA_SUCCESS )
2199 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02002200 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002201 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01002202
2203exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02002204 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01002205 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01002206 return( status );
2207}
2208
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002209psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2210 const psa_key_attributes_t *specified_attributes,
2211 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002212{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002213 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002214 psa_key_slot_t *source_slot = NULL;
2215 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002216 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002217 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002218
Gilles Peskine28f8f302019-07-24 13:30:31 +02002219 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002220 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002221 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002222 goto exit;
2223
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002224 status = psa_validate_optional_attributes( source_slot,
2225 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002226 if( status != PSA_SUCCESS )
2227 goto exit;
2228
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002229 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002230 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002231 if( status != PSA_SUCCESS )
2232 goto exit;
2233
Gilles Peskinedf179142019-07-15 22:02:14 +02002234 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2235 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002236 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002237 if( status != PSA_SUCCESS )
2238 goto exit;
2239
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002240#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2241 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002242 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002243 /* Copying to a secure element is not implemented yet. */
2244 status = PSA_ERROR_NOT_SUPPORTED;
2245 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002246 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002247#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002248
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002249 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002250 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002251 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002252
Gilles Peskine011e4282019-06-26 18:34:38 +02002253 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002254exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002255 if( status != PSA_SUCCESS )
2256 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002257 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002258 *target_handle = 0;
2259 }
2260 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002261}
2262
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002263
2264
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002265/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002266/* Message digests */
2267/****************************************************************/
2268
Gilles Peskineb16841e2019-10-10 20:36:12 +02002269#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002270static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002271{
2272 switch( alg )
2273 {
2274#if defined(MBEDTLS_MD2_C)
2275 case PSA_ALG_MD2:
2276 return( &mbedtls_md2_info );
2277#endif
2278#if defined(MBEDTLS_MD4_C)
2279 case PSA_ALG_MD4:
2280 return( &mbedtls_md4_info );
2281#endif
2282#if defined(MBEDTLS_MD5_C)
2283 case PSA_ALG_MD5:
2284 return( &mbedtls_md5_info );
2285#endif
2286#if defined(MBEDTLS_RIPEMD160_C)
2287 case PSA_ALG_RIPEMD160:
2288 return( &mbedtls_ripemd160_info );
2289#endif
2290#if defined(MBEDTLS_SHA1_C)
2291 case PSA_ALG_SHA_1:
2292 return( &mbedtls_sha1_info );
2293#endif
2294#if defined(MBEDTLS_SHA256_C)
2295 case PSA_ALG_SHA_224:
2296 return( &mbedtls_sha224_info );
2297 case PSA_ALG_SHA_256:
2298 return( &mbedtls_sha256_info );
2299#endif
2300#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002301#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002302 case PSA_ALG_SHA_384:
2303 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002304#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002305 case PSA_ALG_SHA_512:
2306 return( &mbedtls_sha512_info );
2307#endif
2308 default:
2309 return( NULL );
2310 }
2311}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002312#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002313
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002314psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2315{
2316 switch( operation->alg )
2317 {
Gilles Peskine81736312018-06-26 15:04:31 +02002318 case 0:
2319 /* The object has (apparently) been initialized but it is not
2320 * in use. It's ok to call abort on such an object, and there's
2321 * nothing to do. */
2322 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002323#if defined(MBEDTLS_MD2_C)
2324 case PSA_ALG_MD2:
2325 mbedtls_md2_free( &operation->ctx.md2 );
2326 break;
2327#endif
2328#if defined(MBEDTLS_MD4_C)
2329 case PSA_ALG_MD4:
2330 mbedtls_md4_free( &operation->ctx.md4 );
2331 break;
2332#endif
2333#if defined(MBEDTLS_MD5_C)
2334 case PSA_ALG_MD5:
2335 mbedtls_md5_free( &operation->ctx.md5 );
2336 break;
2337#endif
2338#if defined(MBEDTLS_RIPEMD160_C)
2339 case PSA_ALG_RIPEMD160:
2340 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2341 break;
2342#endif
2343#if defined(MBEDTLS_SHA1_C)
2344 case PSA_ALG_SHA_1:
2345 mbedtls_sha1_free( &operation->ctx.sha1 );
2346 break;
2347#endif
2348#if defined(MBEDTLS_SHA256_C)
2349 case PSA_ALG_SHA_224:
2350 case PSA_ALG_SHA_256:
2351 mbedtls_sha256_free( &operation->ctx.sha256 );
2352 break;
2353#endif
2354#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002355#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002356 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002357#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002358 case PSA_ALG_SHA_512:
2359 mbedtls_sha512_free( &operation->ctx.sha512 );
2360 break;
2361#endif
2362 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002363 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002364 }
2365 operation->alg = 0;
2366 return( PSA_SUCCESS );
2367}
2368
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002369psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002370 psa_algorithm_t alg )
2371{
Janos Follath24eed8d2019-11-22 13:21:35 +00002372 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002373
2374 /* A context must be freshly initialized before it can be set up. */
2375 if( operation->alg != 0 )
2376 {
2377 return( PSA_ERROR_BAD_STATE );
2378 }
2379
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002380 switch( alg )
2381 {
2382#if defined(MBEDTLS_MD2_C)
2383 case PSA_ALG_MD2:
2384 mbedtls_md2_init( &operation->ctx.md2 );
2385 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2386 break;
2387#endif
2388#if defined(MBEDTLS_MD4_C)
2389 case PSA_ALG_MD4:
2390 mbedtls_md4_init( &operation->ctx.md4 );
2391 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2392 break;
2393#endif
2394#if defined(MBEDTLS_MD5_C)
2395 case PSA_ALG_MD5:
2396 mbedtls_md5_init( &operation->ctx.md5 );
2397 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2398 break;
2399#endif
2400#if defined(MBEDTLS_RIPEMD160_C)
2401 case PSA_ALG_RIPEMD160:
2402 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2403 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2404 break;
2405#endif
2406#if defined(MBEDTLS_SHA1_C)
2407 case PSA_ALG_SHA_1:
2408 mbedtls_sha1_init( &operation->ctx.sha1 );
2409 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2410 break;
2411#endif
2412#if defined(MBEDTLS_SHA256_C)
2413 case PSA_ALG_SHA_224:
2414 mbedtls_sha256_init( &operation->ctx.sha256 );
2415 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2416 break;
2417 case PSA_ALG_SHA_256:
2418 mbedtls_sha256_init( &operation->ctx.sha256 );
2419 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2420 break;
2421#endif
2422#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002423#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002424 case PSA_ALG_SHA_384:
2425 mbedtls_sha512_init( &operation->ctx.sha512 );
2426 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2427 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002428#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002429 case PSA_ALG_SHA_512:
2430 mbedtls_sha512_init( &operation->ctx.sha512 );
2431 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2432 break;
2433#endif
2434 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002435 return( PSA_ALG_IS_HASH( alg ) ?
2436 PSA_ERROR_NOT_SUPPORTED :
2437 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002438 }
2439 if( ret == 0 )
2440 operation->alg = alg;
2441 else
2442 psa_hash_abort( operation );
2443 return( mbedtls_to_psa_error( ret ) );
2444}
2445
2446psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2447 const uint8_t *input,
2448 size_t input_length )
2449{
Janos Follath24eed8d2019-11-22 13:21:35 +00002450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002451
2452 /* Don't require hash implementations to behave correctly on a
2453 * zero-length input, which may have an invalid pointer. */
2454 if( input_length == 0 )
2455 return( PSA_SUCCESS );
2456
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002457 switch( operation->alg )
2458 {
2459#if defined(MBEDTLS_MD2_C)
2460 case PSA_ALG_MD2:
2461 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2462 input, input_length );
2463 break;
2464#endif
2465#if defined(MBEDTLS_MD4_C)
2466 case PSA_ALG_MD4:
2467 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2468 input, input_length );
2469 break;
2470#endif
2471#if defined(MBEDTLS_MD5_C)
2472 case PSA_ALG_MD5:
2473 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2474 input, input_length );
2475 break;
2476#endif
2477#if defined(MBEDTLS_RIPEMD160_C)
2478 case PSA_ALG_RIPEMD160:
2479 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2480 input, input_length );
2481 break;
2482#endif
2483#if defined(MBEDTLS_SHA1_C)
2484 case PSA_ALG_SHA_1:
2485 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2486 input, input_length );
2487 break;
2488#endif
2489#if defined(MBEDTLS_SHA256_C)
2490 case PSA_ALG_SHA_224:
2491 case PSA_ALG_SHA_256:
2492 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2493 input, input_length );
2494 break;
2495#endif
2496#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002497#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002498 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002499#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002500 case PSA_ALG_SHA_512:
2501 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2502 input, input_length );
2503 break;
2504#endif
2505 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002506 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002507 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002508
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002509 if( ret != 0 )
2510 psa_hash_abort( operation );
2511 return( mbedtls_to_psa_error( ret ) );
2512}
2513
2514psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2515 uint8_t *hash,
2516 size_t hash_size,
2517 size_t *hash_length )
2518{
itayzafrir40835d42018-08-02 13:14:17 +03002519 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002520 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002521 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002522
2523 /* Fill the output buffer with something that isn't a valid hash
2524 * (barring an attack on the hash and deliberately-crafted input),
2525 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002526 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002527 /* If hash_size is 0 then hash may be NULL and then the
2528 * call to memset would have undefined behavior. */
2529 if( hash_size != 0 )
2530 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002531
2532 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002533 {
2534 status = PSA_ERROR_BUFFER_TOO_SMALL;
2535 goto exit;
2536 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002537
2538 switch( operation->alg )
2539 {
2540#if defined(MBEDTLS_MD2_C)
2541 case PSA_ALG_MD2:
2542 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2543 break;
2544#endif
2545#if defined(MBEDTLS_MD4_C)
2546 case PSA_ALG_MD4:
2547 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2548 break;
2549#endif
2550#if defined(MBEDTLS_MD5_C)
2551 case PSA_ALG_MD5:
2552 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2553 break;
2554#endif
2555#if defined(MBEDTLS_RIPEMD160_C)
2556 case PSA_ALG_RIPEMD160:
2557 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2558 break;
2559#endif
2560#if defined(MBEDTLS_SHA1_C)
2561 case PSA_ALG_SHA_1:
2562 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2563 break;
2564#endif
2565#if defined(MBEDTLS_SHA256_C)
2566 case PSA_ALG_SHA_224:
2567 case PSA_ALG_SHA_256:
2568 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2569 break;
2570#endif
2571#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002572#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002573 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002574#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002575 case PSA_ALG_SHA_512:
2576 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2577 break;
2578#endif
2579 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002580 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002581 }
itayzafrir40835d42018-08-02 13:14:17 +03002582 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002583
itayzafrir40835d42018-08-02 13:14:17 +03002584exit:
2585 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002586 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002587 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002588 return( psa_hash_abort( operation ) );
2589 }
2590 else
2591 {
2592 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002593 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002594 }
2595}
2596
Gilles Peskine2d277862018-06-18 15:41:12 +02002597psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2598 const uint8_t *hash,
2599 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002600{
2601 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2602 size_t actual_hash_length;
2603 psa_status_t status = psa_hash_finish( operation,
2604 actual_hash, sizeof( actual_hash ),
2605 &actual_hash_length );
2606 if( status != PSA_SUCCESS )
2607 return( status );
2608 if( actual_hash_length != hash_length )
2609 return( PSA_ERROR_INVALID_SIGNATURE );
2610 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2611 return( PSA_ERROR_INVALID_SIGNATURE );
2612 return( PSA_SUCCESS );
2613}
2614
Gilles Peskine0a749c82019-11-28 19:33:58 +01002615psa_status_t psa_hash_compute( psa_algorithm_t alg,
2616 const uint8_t *input, size_t input_length,
2617 uint8_t *hash, size_t hash_size,
2618 size_t *hash_length )
2619{
2620 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2621 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2622
2623 *hash_length = hash_size;
2624 status = psa_hash_setup( &operation, alg );
2625 if( status != PSA_SUCCESS )
2626 goto exit;
2627 status = psa_hash_update( &operation, input, input_length );
2628 if( status != PSA_SUCCESS )
2629 goto exit;
2630 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2631 if( status != PSA_SUCCESS )
2632 goto exit;
2633
2634exit:
2635 if( status == PSA_SUCCESS )
2636 status = psa_hash_abort( &operation );
2637 else
2638 psa_hash_abort( &operation );
2639 return( status );
2640}
2641
2642psa_status_t psa_hash_compare( psa_algorithm_t alg,
2643 const uint8_t *input, size_t input_length,
2644 const uint8_t *hash, size_t hash_length )
2645{
2646 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2647 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2648
2649 status = psa_hash_setup( &operation, alg );
2650 if( status != PSA_SUCCESS )
2651 goto exit;
2652 status = psa_hash_update( &operation, input, input_length );
2653 if( status != PSA_SUCCESS )
2654 goto exit;
2655 status = psa_hash_verify( &operation, hash, hash_length );
2656 if( status != PSA_SUCCESS )
2657 goto exit;
2658
2659exit:
2660 if( status == PSA_SUCCESS )
2661 status = psa_hash_abort( &operation );
2662 else
2663 psa_hash_abort( &operation );
2664 return( status );
2665}
2666
Gilles Peskineeb35d782019-01-22 17:56:16 +01002667psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2668 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002669{
2670 if( target_operation->alg != 0 )
2671 return( PSA_ERROR_BAD_STATE );
2672
2673 switch( source_operation->alg )
2674 {
2675 case 0:
2676 return( PSA_ERROR_BAD_STATE );
2677#if defined(MBEDTLS_MD2_C)
2678 case PSA_ALG_MD2:
2679 mbedtls_md2_clone( &target_operation->ctx.md2,
2680 &source_operation->ctx.md2 );
2681 break;
2682#endif
2683#if defined(MBEDTLS_MD4_C)
2684 case PSA_ALG_MD4:
2685 mbedtls_md4_clone( &target_operation->ctx.md4,
2686 &source_operation->ctx.md4 );
2687 break;
2688#endif
2689#if defined(MBEDTLS_MD5_C)
2690 case PSA_ALG_MD5:
2691 mbedtls_md5_clone( &target_operation->ctx.md5,
2692 &source_operation->ctx.md5 );
2693 break;
2694#endif
2695#if defined(MBEDTLS_RIPEMD160_C)
2696 case PSA_ALG_RIPEMD160:
2697 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2698 &source_operation->ctx.ripemd160 );
2699 break;
2700#endif
2701#if defined(MBEDTLS_SHA1_C)
2702 case PSA_ALG_SHA_1:
2703 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2704 &source_operation->ctx.sha1 );
2705 break;
2706#endif
2707#if defined(MBEDTLS_SHA256_C)
2708 case PSA_ALG_SHA_224:
2709 case PSA_ALG_SHA_256:
2710 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2711 &source_operation->ctx.sha256 );
2712 break;
2713#endif
2714#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002715#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002716 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002717#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002718 case PSA_ALG_SHA_512:
2719 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2720 &source_operation->ctx.sha512 );
2721 break;
2722#endif
2723 default:
2724 return( PSA_ERROR_NOT_SUPPORTED );
2725 }
2726
2727 target_operation->alg = source_operation->alg;
2728 return( PSA_SUCCESS );
2729}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002730
2731
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002732/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002733/* MAC */
2734/****************************************************************/
2735
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002736static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002737 psa_algorithm_t alg,
2738 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002739 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002740 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002741{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002742 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002743 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002744
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002745 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002746 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002747
Gilles Peskine8c9def32018-02-08 10:02:12 +01002748 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2749 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002750 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002751 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002752 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002753 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002754 mode = MBEDTLS_MODE_STREAM;
2755 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002756 case PSA_ALG_CTR:
2757 mode = MBEDTLS_MODE_CTR;
2758 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002759 case PSA_ALG_CFB:
2760 mode = MBEDTLS_MODE_CFB;
2761 break;
2762 case PSA_ALG_OFB:
2763 mode = MBEDTLS_MODE_OFB;
2764 break;
2765 case PSA_ALG_CBC_NO_PADDING:
2766 mode = MBEDTLS_MODE_CBC;
2767 break;
2768 case PSA_ALG_CBC_PKCS7:
2769 mode = MBEDTLS_MODE_CBC;
2770 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002771 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002772 mode = MBEDTLS_MODE_CCM;
2773 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002774 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002775 mode = MBEDTLS_MODE_GCM;
2776 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002777 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2778 mode = MBEDTLS_MODE_CHACHAPOLY;
2779 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002780 default:
2781 return( NULL );
2782 }
2783 }
2784 else if( alg == PSA_ALG_CMAC )
2785 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002786 else
2787 return( NULL );
2788
2789 switch( key_type )
2790 {
2791 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002792 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002793 break;
2794 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002795 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2796 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002797 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002798 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002799 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002800 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002801 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2802 * but two-key Triple-DES is functionally three-key Triple-DES
2803 * with K1=K3, so that's how we present it to mbedtls. */
2804 if( key_bits == 128 )
2805 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002806 break;
2807 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002808 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002809 break;
2810 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002811 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002812 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002813 case PSA_KEY_TYPE_CHACHA20:
2814 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2815 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002816 default:
2817 return( NULL );
2818 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002819 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002820 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002821
Jaeden Amero23bbb752018-06-26 14:16:54 +01002822 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2823 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002824}
2825
Gilles Peskinea05219c2018-11-16 16:02:56 +01002826#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002827static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002828{
Gilles Peskine2d277862018-06-18 15:41:12 +02002829 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002830 {
2831 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002832 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002833 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002834 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002835 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002836 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002837 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002838 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002839 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002840 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002841 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002842 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002843 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002844 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002845 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002846 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002847 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002848 return( 128 );
2849 default:
2850 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002851 }
2852}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002853#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002854
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002855/* Initialize the MAC operation structure. Once this function has been
2856 * called, psa_mac_abort can run and will do the right thing. */
2857static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2858 psa_algorithm_t alg )
2859{
2860 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2861
2862 operation->alg = alg;
2863 operation->key_set = 0;
2864 operation->iv_set = 0;
2865 operation->iv_required = 0;
2866 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002867 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002868
2869#if defined(MBEDTLS_CMAC_C)
2870 if( alg == PSA_ALG_CMAC )
2871 {
2872 operation->iv_required = 0;
2873 mbedtls_cipher_init( &operation->ctx.cmac );
2874 status = PSA_SUCCESS;
2875 }
2876 else
2877#endif /* MBEDTLS_CMAC_C */
2878#if defined(MBEDTLS_MD_C)
2879 if( PSA_ALG_IS_HMAC( operation->alg ) )
2880 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002881 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2882 operation->ctx.hmac.hash_ctx.alg = 0;
2883 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002884 }
2885 else
2886#endif /* MBEDTLS_MD_C */
2887 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002888 if( ! PSA_ALG_IS_MAC( alg ) )
2889 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002890 }
2891
2892 if( status != PSA_SUCCESS )
2893 memset( operation, 0, sizeof( *operation ) );
2894 return( status );
2895}
2896
Gilles Peskine01126fa2018-07-12 17:04:55 +02002897#if defined(MBEDTLS_MD_C)
2898static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2899{
Gilles Peskine3f108122018-12-07 18:14:53 +01002900 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002901 return( psa_hash_abort( &hmac->hash_ctx ) );
2902}
2903#endif /* MBEDTLS_MD_C */
2904
Gilles Peskine8c9def32018-02-08 10:02:12 +01002905psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2906{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002907 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002908 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002909 /* The object has (apparently) been initialized but it is not
2910 * in use. It's ok to call abort on such an object, and there's
2911 * nothing to do. */
2912 return( PSA_SUCCESS );
2913 }
2914 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002915#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002916 if( operation->alg == PSA_ALG_CMAC )
2917 {
2918 mbedtls_cipher_free( &operation->ctx.cmac );
2919 }
2920 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002921#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002922#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002923 if( PSA_ALG_IS_HMAC( operation->alg ) )
2924 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002925 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002926 }
2927 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002928#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002929 {
2930 /* Sanity check (shouldn't happen: operation->alg should
2931 * always have been initialized to a valid value). */
2932 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002933 }
Moran Peker41deec42018-04-04 15:43:05 +03002934
Gilles Peskine8c9def32018-02-08 10:02:12 +01002935 operation->alg = 0;
2936 operation->key_set = 0;
2937 operation->iv_set = 0;
2938 operation->iv_required = 0;
2939 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002940 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002941
Gilles Peskine8c9def32018-02-08 10:02:12 +01002942 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002943
2944bad_state:
2945 /* If abort is called on an uninitialized object, we can't trust
2946 * anything. Wipe the object in case it contains confidential data.
2947 * This may result in a memory leak if a pointer gets overwritten,
2948 * but it's too late to do anything about this. */
2949 memset( operation, 0, sizeof( *operation ) );
2950 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002951}
2952
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002953#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002954static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002955 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002956 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002957 const mbedtls_cipher_info_t *cipher_info )
2958{
Janos Follath24eed8d2019-11-22 13:21:35 +00002959 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002960
2961 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002962
2963 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2964 if( ret != 0 )
2965 return( ret );
2966
2967 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02002968 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002969 key_bits );
2970 return( ret );
2971}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002972#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002973
Gilles Peskine248051a2018-06-20 16:09:38 +02002974#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002975static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2976 const uint8_t *key,
2977 size_t key_length,
2978 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002979{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002980 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002981 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002982 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002983 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002984 psa_status_t status;
2985
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002986 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2987 * overflow below. This should never trigger if the hash algorithm
2988 * is implemented correctly. */
2989 /* The size checks against the ipad and opad buffers cannot be written
2990 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2991 * because that triggers -Wlogical-op on GCC 7.3. */
2992 if( block_size > sizeof( ipad ) )
2993 return( PSA_ERROR_NOT_SUPPORTED );
2994 if( block_size > sizeof( hmac->opad ) )
2995 return( PSA_ERROR_NOT_SUPPORTED );
2996 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002997 return( PSA_ERROR_NOT_SUPPORTED );
2998
Gilles Peskined223b522018-06-11 18:12:58 +02002999 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003000 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003001 status = psa_hash_compute( hash_alg, key, key_length,
3002 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003003 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003004 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003005 }
Gilles Peskine96889972018-07-12 17:07:03 +02003006 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3007 * but it is permitted. It is common when HMAC is used in HKDF, for
3008 * example. Don't call `memcpy` in the 0-length because `key` could be
3009 * an invalid pointer which would make the behavior undefined. */
3010 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003011 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003012
Gilles Peskined223b522018-06-11 18:12:58 +02003013 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3014 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003015 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003016 ipad[i] ^= 0x36;
3017 memset( ipad + key_length, 0x36, block_size - key_length );
3018
3019 /* Copy the key material from ipad to opad, flipping the requisite bits,
3020 * and filling the rest of opad with the requisite constant. */
3021 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003022 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3023 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003024
Gilles Peskine01126fa2018-07-12 17:04:55 +02003025 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003026 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003027 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003028
Gilles Peskine01126fa2018-07-12 17:04:55 +02003029 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003030
3031cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03003032 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003033
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003034 return( status );
3035}
Gilles Peskine248051a2018-06-20 16:09:38 +02003036#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003037
Gilles Peskine89167cb2018-07-08 20:12:23 +02003038static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003039 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003040 psa_algorithm_t alg,
3041 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003042{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003043 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003044 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003045 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003046 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003047 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003048 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003049 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003050
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003051 /* A context must be freshly initialized before it can be set up. */
3052 if( operation->alg != 0 )
3053 {
3054 return( PSA_ERROR_BAD_STATE );
3055 }
3056
Gilles Peskined911eb72018-08-14 15:18:45 +02003057 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003058 if( status != PSA_SUCCESS )
3059 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003060 if( is_sign )
3061 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003062
Gilles Peskine28f8f302019-07-24 13:30:31 +02003063 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003064 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003065 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003066 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003067
Gilles Peskine8c9def32018-02-08 10:02:12 +01003068#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003069 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003070 {
3071 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003072 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003073 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003074 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003075 if( cipher_info == NULL )
3076 {
3077 status = PSA_ERROR_NOT_SUPPORTED;
3078 goto exit;
3079 }
3080 operation->mac_size = cipher_info->block_size;
3081 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3082 status = mbedtls_to_psa_error( ret );
3083 }
3084 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003085#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003086#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003087 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003088 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003089 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003090 if( hash_alg == 0 )
3091 {
3092 status = PSA_ERROR_NOT_SUPPORTED;
3093 goto exit;
3094 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003095
3096 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3097 /* Sanity check. This shouldn't fail on a valid configuration. */
3098 if( operation->mac_size == 0 ||
3099 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3100 {
3101 status = PSA_ERROR_NOT_SUPPORTED;
3102 goto exit;
3103 }
3104
Gilles Peskine8e338702019-07-30 20:06:31 +02003105 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003106 {
3107 status = PSA_ERROR_INVALID_ARGUMENT;
3108 goto exit;
3109 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003110
Gilles Peskine01126fa2018-07-12 17:04:55 +02003111 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003112 slot->data.key.data,
3113 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003114 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003115 }
3116 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003117#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003118 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003119 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003120 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003121 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003122
Gilles Peskined911eb72018-08-14 15:18:45 +02003123 if( truncated == 0 )
3124 {
3125 /* The "normal" case: untruncated algorithm. Nothing to do. */
3126 }
3127 else if( truncated < 4 )
3128 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003129 /* A very short MAC is too short for security since it can be
3130 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3131 * so we make this our minimum, even though 32 bits is still
3132 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003133 status = PSA_ERROR_NOT_SUPPORTED;
3134 }
3135 else if( truncated > operation->mac_size )
3136 {
3137 /* It's impossible to "truncate" to a larger length. */
3138 status = PSA_ERROR_INVALID_ARGUMENT;
3139 }
3140 else
3141 operation->mac_size = truncated;
3142
Gilles Peskinefbfac682018-07-08 20:51:54 +02003143exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003144 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003145 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003146 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003147 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003148 else
3149 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003150 operation->key_set = 1;
3151 }
3152 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003153}
3154
Gilles Peskine89167cb2018-07-08 20:12:23 +02003155psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003156 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003157 psa_algorithm_t alg )
3158{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003159 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003160}
3161
3162psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003163 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003164 psa_algorithm_t alg )
3165{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003166 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003167}
3168
Gilles Peskine8c9def32018-02-08 10:02:12 +01003169psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3170 const uint8_t *input,
3171 size_t input_length )
3172{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003173 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003174 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003175 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003176 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003177 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003178 operation->has_input = 1;
3179
Gilles Peskine8c9def32018-02-08 10:02:12 +01003180#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003181 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003182 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003183 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3184 input, input_length );
3185 status = mbedtls_to_psa_error( ret );
3186 }
3187 else
3188#endif /* MBEDTLS_CMAC_C */
3189#if defined(MBEDTLS_MD_C)
3190 if( PSA_ALG_IS_HMAC( operation->alg ) )
3191 {
3192 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3193 input_length );
3194 }
3195 else
3196#endif /* MBEDTLS_MD_C */
3197 {
3198 /* This shouldn't happen if `operation` was initialized by
3199 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003200 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003201 }
3202
Gilles Peskinefbfac682018-07-08 20:51:54 +02003203 if( status != PSA_SUCCESS )
3204 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003205 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003206}
3207
Gilles Peskine01126fa2018-07-12 17:04:55 +02003208#if defined(MBEDTLS_MD_C)
3209static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3210 uint8_t *mac,
3211 size_t mac_size )
3212{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003213 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003214 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3215 size_t hash_size = 0;
3216 size_t block_size = psa_get_hash_block_size( hash_alg );
3217 psa_status_t status;
3218
Gilles Peskine01126fa2018-07-12 17:04:55 +02003219 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3220 if( status != PSA_SUCCESS )
3221 return( status );
3222 /* From here on, tmp needs to be wiped. */
3223
3224 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3225 if( status != PSA_SUCCESS )
3226 goto exit;
3227
3228 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3229 if( status != PSA_SUCCESS )
3230 goto exit;
3231
3232 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3233 if( status != PSA_SUCCESS )
3234 goto exit;
3235
Gilles Peskined911eb72018-08-14 15:18:45 +02003236 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3237 if( status != PSA_SUCCESS )
3238 goto exit;
3239
3240 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003241
3242exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003243 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003244 return( status );
3245}
3246#endif /* MBEDTLS_MD_C */
3247
mohammad16036df908f2018-04-02 08:34:15 -07003248static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003249 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003250 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003251{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003252 if( ! operation->key_set )
3253 return( PSA_ERROR_BAD_STATE );
3254 if( operation->iv_required && ! operation->iv_set )
3255 return( PSA_ERROR_BAD_STATE );
3256
Gilles Peskine8c9def32018-02-08 10:02:12 +01003257 if( mac_size < operation->mac_size )
3258 return( PSA_ERROR_BUFFER_TOO_SMALL );
3259
Gilles Peskine8c9def32018-02-08 10:02:12 +01003260#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003261 if( operation->alg == PSA_ALG_CMAC )
3262 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003263 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3264 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3265 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003266 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003267 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003268 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003269 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003270 else
3271#endif /* MBEDTLS_CMAC_C */
3272#if defined(MBEDTLS_MD_C)
3273 if( PSA_ALG_IS_HMAC( operation->alg ) )
3274 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003275 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003276 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003277 }
3278 else
3279#endif /* MBEDTLS_MD_C */
3280 {
3281 /* This shouldn't happen if `operation` was initialized by
3282 * a setup function. */
3283 return( PSA_ERROR_BAD_STATE );
3284 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003285}
3286
Gilles Peskineacd4be32018-07-08 19:56:25 +02003287psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3288 uint8_t *mac,
3289 size_t mac_size,
3290 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003291{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003292 psa_status_t status;
3293
Jaeden Amero252ef282019-02-15 14:05:35 +00003294 if( operation->alg == 0 )
3295 {
3296 return( PSA_ERROR_BAD_STATE );
3297 }
3298
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003299 /* Fill the output buffer with something that isn't a valid mac
3300 * (barring an attack on the mac and deliberately-crafted input),
3301 * in case the caller doesn't check the return status properly. */
3302 *mac_length = mac_size;
3303 /* If mac_size is 0 then mac may be NULL and then the
3304 * call to memset would have undefined behavior. */
3305 if( mac_size != 0 )
3306 memset( mac, '!', mac_size );
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 Peskine89167cb2018-07-08 20:12:23 +02003311 }
mohammad16036df908f2018-04-02 08:34:15 -07003312
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003313 status = psa_mac_finish_internal( operation, mac, mac_size );
3314
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003315 if( status == PSA_SUCCESS )
3316 {
3317 status = psa_mac_abort( operation );
3318 if( status == PSA_SUCCESS )
3319 *mac_length = operation->mac_size;
3320 else
3321 memset( mac, '!', mac_size );
3322 }
3323 else
3324 psa_mac_abort( operation );
3325 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003326}
3327
Gilles Peskineacd4be32018-07-08 19:56:25 +02003328psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3329 const uint8_t *mac,
3330 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003331{
Gilles Peskine828ed142018-06-18 23:25:51 +02003332 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003333 psa_status_t status;
3334
Jaeden Amero252ef282019-02-15 14:05:35 +00003335 if( operation->alg == 0 )
3336 {
3337 return( PSA_ERROR_BAD_STATE );
3338 }
3339
Gilles Peskine89167cb2018-07-08 20:12:23 +02003340 if( operation->is_sign )
3341 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003342 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003343 }
3344 if( operation->mac_size != mac_length )
3345 {
3346 status = PSA_ERROR_INVALID_SIGNATURE;
3347 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003348 }
mohammad16036df908f2018-04-02 08:34:15 -07003349
3350 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003351 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003352 if( status != PSA_SUCCESS )
3353 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003354
3355 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3356 status = PSA_ERROR_INVALID_SIGNATURE;
3357
3358cleanup:
3359 if( status == PSA_SUCCESS )
3360 status = psa_mac_abort( operation );
3361 else
3362 psa_mac_abort( operation );
3363
Gilles Peskine3f108122018-12-07 18:14:53 +01003364 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003365
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003366 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003367}
3368
3369
Gilles Peskine20035e32018-02-03 22:44:14 +01003370
Gilles Peskine20035e32018-02-03 22:44:14 +01003371/****************************************************************/
3372/* Asymmetric cryptography */
3373/****************************************************************/
3374
Gilles Peskine2b450e32018-06-27 15:42:46 +02003375#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003376/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003377 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003378static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3379 size_t hash_length,
3380 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003381{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003382 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003383 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003384 *md_alg = mbedtls_md_get_type( md_info );
3385
3386 /* The Mbed TLS RSA module uses an unsigned int for hash length
3387 * parameters. Validate that it fits so that we don't risk an
3388 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003389#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003390 if( hash_length > UINT_MAX )
3391 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003392#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003393
3394#if defined(MBEDTLS_PKCS1_V15)
3395 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3396 * must be correct. */
3397 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3398 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003399 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003400 if( md_info == NULL )
3401 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003402 if( mbedtls_md_get_size( md_info ) != hash_length )
3403 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003404 }
3405#endif /* MBEDTLS_PKCS1_V15 */
3406
3407#if defined(MBEDTLS_PKCS1_V21)
3408 /* PSS requires a hash internally. */
3409 if( PSA_ALG_IS_RSA_PSS( alg ) )
3410 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003411 if( md_info == NULL )
3412 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003413 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003414#endif /* MBEDTLS_PKCS1_V21 */
3415
Gilles Peskine61b91d42018-06-08 16:09:36 +02003416 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003417}
3418
Gilles Peskine2b450e32018-06-27 15:42:46 +02003419static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3420 psa_algorithm_t alg,
3421 const uint8_t *hash,
3422 size_t hash_length,
3423 uint8_t *signature,
3424 size_t signature_size,
3425 size_t *signature_length )
3426{
3427 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003428 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003429 mbedtls_md_type_t md_alg;
3430
3431 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3432 if( status != PSA_SUCCESS )
3433 return( status );
3434
Gilles Peskine630a18a2018-06-29 17:49:35 +02003435 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003436 return( PSA_ERROR_BUFFER_TOO_SMALL );
3437
3438#if defined(MBEDTLS_PKCS1_V15)
3439 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3440 {
3441 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3442 MBEDTLS_MD_NONE );
3443 ret = mbedtls_rsa_pkcs1_sign( rsa,
3444 mbedtls_ctr_drbg_random,
3445 &global_data.ctr_drbg,
3446 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003447 md_alg,
3448 (unsigned int) hash_length,
3449 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003450 signature );
3451 }
3452 else
3453#endif /* MBEDTLS_PKCS1_V15 */
3454#if defined(MBEDTLS_PKCS1_V21)
3455 if( PSA_ALG_IS_RSA_PSS( alg ) )
3456 {
3457 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3458 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3459 mbedtls_ctr_drbg_random,
3460 &global_data.ctr_drbg,
3461 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003462 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003463 (unsigned int) hash_length,
3464 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003465 signature );
3466 }
3467 else
3468#endif /* MBEDTLS_PKCS1_V21 */
3469 {
3470 return( PSA_ERROR_INVALID_ARGUMENT );
3471 }
3472
3473 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003474 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003475 return( mbedtls_to_psa_error( ret ) );
3476}
3477
3478static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3479 psa_algorithm_t alg,
3480 const uint8_t *hash,
3481 size_t hash_length,
3482 const uint8_t *signature,
3483 size_t signature_length )
3484{
3485 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003486 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003487 mbedtls_md_type_t md_alg;
3488
3489 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3490 if( status != PSA_SUCCESS )
3491 return( status );
3492
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003493 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3494 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003495
3496#if defined(MBEDTLS_PKCS1_V15)
3497 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3498 {
3499 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3500 MBEDTLS_MD_NONE );
3501 ret = mbedtls_rsa_pkcs1_verify( rsa,
3502 mbedtls_ctr_drbg_random,
3503 &global_data.ctr_drbg,
3504 MBEDTLS_RSA_PUBLIC,
3505 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003506 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003507 hash,
3508 signature );
3509 }
3510 else
3511#endif /* MBEDTLS_PKCS1_V15 */
3512#if defined(MBEDTLS_PKCS1_V21)
3513 if( PSA_ALG_IS_RSA_PSS( alg ) )
3514 {
3515 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3516 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3517 mbedtls_ctr_drbg_random,
3518 &global_data.ctr_drbg,
3519 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003520 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003521 (unsigned int) hash_length,
3522 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003523 signature );
3524 }
3525 else
3526#endif /* MBEDTLS_PKCS1_V21 */
3527 {
3528 return( PSA_ERROR_INVALID_ARGUMENT );
3529 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003530
3531 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3532 * the rest of the signature is invalid". This has little use in
3533 * practice and PSA doesn't report this distinction. */
3534 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3535 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003536 return( mbedtls_to_psa_error( ret ) );
3537}
3538#endif /* MBEDTLS_RSA_C */
3539
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003540#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003541/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3542 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3543 * (even though these functions don't modify it). */
3544static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3545 psa_algorithm_t alg,
3546 const uint8_t *hash,
3547 size_t hash_length,
3548 uint8_t *signature,
3549 size_t signature_size,
3550 size_t *signature_length )
3551{
Janos Follath24eed8d2019-11-22 13:21:35 +00003552 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003553 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003554 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003555 mbedtls_mpi_init( &r );
3556 mbedtls_mpi_init( &s );
3557
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003558 if( signature_size < 2 * curve_bytes )
3559 {
3560 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3561 goto cleanup;
3562 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003563
Gilles Peskinea05219c2018-11-16 16:02:56 +01003564#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003565 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3566 {
3567 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3568 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3569 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003570 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3571 &ecp->d, hash,
3572 hash_length, md_alg,
3573 mbedtls_ctr_drbg_random,
3574 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003575 }
3576 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003577#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003578 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003579 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003580 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3581 hash, hash_length,
3582 mbedtls_ctr_drbg_random,
3583 &global_data.ctr_drbg ) );
3584 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003585
3586 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3587 signature,
3588 curve_bytes ) );
3589 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3590 signature + curve_bytes,
3591 curve_bytes ) );
3592
3593cleanup:
3594 mbedtls_mpi_free( &r );
3595 mbedtls_mpi_free( &s );
3596 if( ret == 0 )
3597 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003598 return( mbedtls_to_psa_error( ret ) );
3599}
3600
3601static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3602 const uint8_t *hash,
3603 size_t hash_length,
3604 const uint8_t *signature,
3605 size_t signature_length )
3606{
Janos Follath24eed8d2019-11-22 13:21:35 +00003607 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003608 mbedtls_mpi r, s;
3609 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3610 mbedtls_mpi_init( &r );
3611 mbedtls_mpi_init( &s );
3612
3613 if( signature_length != 2 * curve_bytes )
3614 return( PSA_ERROR_INVALID_SIGNATURE );
3615
3616 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3617 signature,
3618 curve_bytes ) );
3619 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3620 signature + curve_bytes,
3621 curve_bytes ) );
3622
Steven Cooremanacda8342020-07-24 23:09:52 +02003623 /* Check whether the public part is loaded. If not, load it. */
3624 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3625 {
3626 MBEDTLS_MPI_CHK(
3627 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3628 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3629 }
3630
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003631 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3632 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003633
3634cleanup:
3635 mbedtls_mpi_free( &r );
3636 mbedtls_mpi_free( &s );
3637 return( mbedtls_to_psa_error( ret ) );
3638}
3639#endif /* MBEDTLS_ECDSA_C */
3640
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003641psa_status_t psa_sign_hash( psa_key_handle_t handle,
3642 psa_algorithm_t alg,
3643 const uint8_t *hash,
3644 size_t hash_length,
3645 uint8_t *signature,
3646 size_t signature_size,
3647 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003648{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003649 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003650 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003651#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3652 const psa_drv_se_t *drv;
3653 psa_drv_se_context_t *drv_context;
3654#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003655
3656 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003657 /* Immediately reject a zero-length signature buffer. This guarantees
3658 * that signature must be a valid pointer. (On the other hand, the hash
3659 * buffer can in principle be empty since it doesn't actually have
3660 * to be a hash.) */
3661 if( signature_size == 0 )
3662 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003663
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003664 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003665 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003666 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003667 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003668 {
3669 status = PSA_ERROR_INVALID_ARGUMENT;
3670 goto exit;
3671 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003672
Gilles Peskineedc64242019-08-07 21:05:07 +02003673#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3674 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3675 {
3676 if( drv->asymmetric == NULL ||
3677 drv->asymmetric->p_sign == NULL )
3678 {
3679 status = PSA_ERROR_NOT_SUPPORTED;
3680 goto exit;
3681 }
3682 status = drv->asymmetric->p_sign( drv_context,
3683 slot->data.se.slot_number,
3684 alg,
3685 hash, hash_length,
3686 signature, signature_size,
3687 signature_length );
3688 }
3689 else
3690#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003691#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003692 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003693 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003694 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003695
Steven Cooreman7f391872020-07-30 14:57:44 +02003696 status = psa_load_rsa_representation( slot->data.key.data,
3697 slot->data.key.bytes,
3698 slot->attr.type,
Steven Cooremana01795d2020-07-24 22:48:15 +02003699 &rsa );
3700 if( status != PSA_SUCCESS )
3701 goto exit;
3702
Steven Cooremana2371e52020-07-28 14:30:39 +02003703 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003704 alg,
3705 hash, hash_length,
3706 signature, signature_size,
3707 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003708
Steven Cooremana2371e52020-07-28 14:30:39 +02003709 mbedtls_rsa_free( rsa );
3710 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003711 }
3712 else
3713#endif /* defined(MBEDTLS_RSA_C) */
3714#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003715 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003716 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003717#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003718 if(
3719#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3720 PSA_ALG_IS_ECDSA( alg )
3721#else
3722 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3723#endif
3724 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003725 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003726 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02003727 status = psa_load_ecp_representation( slot->data.key.data,
3728 slot->data.key.bytes,
3729 slot->attr.type,
3730 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003731 if( status != PSA_SUCCESS )
3732 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003733 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003734 alg,
3735 hash, hash_length,
3736 signature, signature_size,
3737 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003738 mbedtls_ecp_keypair_free( ecp );
3739 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003740 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003741 else
3742#endif /* defined(MBEDTLS_ECDSA_C) */
3743 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003744 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003745 }
itayzafrir5c753392018-05-08 11:18:38 +03003746 }
3747 else
3748#endif /* defined(MBEDTLS_ECP_C) */
3749 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003750 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003751 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003752
3753exit:
3754 /* Fill the unused part of the output buffer (the whole buffer on error,
3755 * the trailing part on success) with something that isn't a valid mac
3756 * (barring an attack on the mac and deliberately-crafted input),
3757 * in case the caller doesn't check the return status properly. */
3758 if( status == PSA_SUCCESS )
3759 memset( signature + *signature_length, '!',
3760 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003761 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003762 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003763 /* If signature_size is 0 then we have nothing to do. We must not call
3764 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003765 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003766}
3767
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003768psa_status_t psa_verify_hash( psa_key_handle_t handle,
3769 psa_algorithm_t alg,
3770 const uint8_t *hash,
3771 size_t hash_length,
3772 const uint8_t *signature,
3773 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003774{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003775 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003776 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003777#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3778 const psa_drv_se_t *drv;
3779 psa_drv_se_context_t *drv_context;
3780#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003781
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003782 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003783 if( status != PSA_SUCCESS )
3784 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003785
Gilles Peskineedc64242019-08-07 21:05:07 +02003786#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3787 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3788 {
3789 if( drv->asymmetric == NULL ||
3790 drv->asymmetric->p_verify == NULL )
3791 return( PSA_ERROR_NOT_SUPPORTED );
3792 return( drv->asymmetric->p_verify( drv_context,
3793 slot->data.se.slot_number,
3794 alg,
3795 hash, hash_length,
3796 signature, signature_length ) );
3797 }
3798 else
3799#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003800#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003801 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003802 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003803 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003804
Steven Cooreman7f391872020-07-30 14:57:44 +02003805 status = psa_load_rsa_representation( slot->data.key.data,
3806 slot->data.key.bytes,
3807 slot->attr.type,
3808 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003809 if( status != PSA_SUCCESS )
3810 return status;
3811
Steven Cooremana2371e52020-07-28 14:30:39 +02003812 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003813 alg,
3814 hash, hash_length,
3815 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003816 mbedtls_rsa_free( rsa );
3817 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003818 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003819 }
3820 else
3821#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003822#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003823 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003824 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003825#if defined(MBEDTLS_ECDSA_C)
3826 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003827 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003828 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02003829 status = psa_load_ecp_representation( slot->data.key.data,
3830 slot->data.key.bytes,
3831 slot->attr.type,
3832 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003833 if( status != PSA_SUCCESS )
3834 return status;
Steven Cooremana2371e52020-07-28 14:30:39 +02003835 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003836 hash, hash_length,
3837 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003838 mbedtls_ecp_keypair_free( ecp );
3839 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003840 return status;
3841 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003842 else
3843#endif /* defined(MBEDTLS_ECDSA_C) */
3844 {
3845 return( PSA_ERROR_INVALID_ARGUMENT );
3846 }
itayzafrir5c753392018-05-08 11:18:38 +03003847 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003848 else
3849#endif /* defined(MBEDTLS_ECP_C) */
3850 {
3851 return( PSA_ERROR_NOT_SUPPORTED );
3852 }
3853}
3854
Gilles Peskine072ac562018-06-30 00:21:29 +02003855#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3856static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3857 mbedtls_rsa_context *rsa )
3858{
3859 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3860 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3861 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3862 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3863}
3864#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3865
Gilles Peskinec5487a82018-12-03 18:08:14 +01003866psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003867 psa_algorithm_t alg,
3868 const uint8_t *input,
3869 size_t input_length,
3870 const uint8_t *salt,
3871 size_t salt_length,
3872 uint8_t *output,
3873 size_t output_size,
3874 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003875{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003876 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003877 psa_status_t status;
3878
Darryl Green5cc689a2018-07-24 15:34:10 +01003879 (void) input;
3880 (void) input_length;
3881 (void) salt;
3882 (void) output;
3883 (void) output_size;
3884
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003885 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003886
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003887 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3888 return( PSA_ERROR_INVALID_ARGUMENT );
3889
Gilles Peskine28f8f302019-07-24 13:30:31 +02003890 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003891 if( status != PSA_SUCCESS )
3892 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003893 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3894 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003895 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003896
3897#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003898 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003899 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003900 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02003901 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02003902
Steven Cooreman7f391872020-07-30 14:57:44 +02003903 status = psa_load_rsa_representation( slot->data.key.data,
3904 slot->data.key.bytes,
3905 slot->attr.type,
3906 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003907 if( status != PSA_SUCCESS )
3908 return status;
Steven Cooremana2371e52020-07-28 14:30:39 +02003909
3910 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003911 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003912 mbedtls_rsa_free( rsa );
3913 mbedtls_free( rsa );
Vikas Katariya21599b62019-08-02 12:26:29 +01003914 return( PSA_ERROR_BUFFER_TOO_SMALL );
Steven Cooremana01795d2020-07-24 22:48:15 +02003915 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003916#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003917 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003918 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003919 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003920 mbedtls_ctr_drbg_random,
3921 &global_data.ctr_drbg,
3922 MBEDTLS_RSA_PUBLIC,
3923 input_length,
3924 input,
3925 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003926 }
3927 else
3928#endif /* MBEDTLS_PKCS1_V15 */
3929#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003930 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003931 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003932 psa_rsa_oaep_set_padding_mode( alg, rsa );
3933 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine072ac562018-06-30 00:21:29 +02003934 mbedtls_ctr_drbg_random,
3935 &global_data.ctr_drbg,
3936 MBEDTLS_RSA_PUBLIC,
3937 salt, salt_length,
3938 input_length,
3939 input,
3940 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003941 }
3942 else
3943#endif /* MBEDTLS_PKCS1_V21 */
3944 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003945 mbedtls_rsa_free( rsa );
3946 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003947 return( PSA_ERROR_INVALID_ARGUMENT );
3948 }
3949 if( ret == 0 )
Steven Cooremana2371e52020-07-28 14:30:39 +02003950 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003951
Steven Cooremana2371e52020-07-28 14:30:39 +02003952 mbedtls_rsa_free( rsa );
3953 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003954 return( mbedtls_to_psa_error( ret ) );
3955 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003956 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003957#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003958 {
3959 return( PSA_ERROR_NOT_SUPPORTED );
3960 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003961}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003962
Gilles Peskinec5487a82018-12-03 18:08:14 +01003963psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003964 psa_algorithm_t alg,
3965 const uint8_t *input,
3966 size_t input_length,
3967 const uint8_t *salt,
3968 size_t salt_length,
3969 uint8_t *output,
3970 size_t output_size,
3971 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003972{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003973 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003974 psa_status_t status;
3975
Darryl Green5cc689a2018-07-24 15:34:10 +01003976 (void) input;
3977 (void) input_length;
3978 (void) salt;
3979 (void) output;
3980 (void) output_size;
3981
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003982 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003983
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003984 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3985 return( PSA_ERROR_INVALID_ARGUMENT );
3986
Gilles Peskine28f8f302019-07-24 13:30:31 +02003987 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003988 if( status != PSA_SUCCESS )
3989 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003990 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003991 return( PSA_ERROR_INVALID_ARGUMENT );
3992
3993#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003994 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003995 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003996 mbedtls_rsa_context *rsa;
Steven Cooreman75b74362020-07-28 14:30:13 +02003997 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02003998
Steven Cooreman7f391872020-07-30 14:57:44 +02003999 status = psa_load_rsa_representation( slot->data.key.data,
4000 slot->data.key.bytes,
4001 slot->attr.type,
4002 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004003 if( status != PSA_SUCCESS )
4004 return status;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004005
Steven Cooremana2371e52020-07-28 14:30:39 +02004006 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004007 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004008 mbedtls_rsa_free( rsa );
4009 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004010 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana01795d2020-07-24 22:48:15 +02004011 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004012
4013#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004014 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004015 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004016 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004017 mbedtls_ctr_drbg_random,
4018 &global_data.ctr_drbg,
4019 MBEDTLS_RSA_PRIVATE,
4020 output_length,
4021 input,
4022 output,
4023 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004024 }
4025 else
4026#endif /* MBEDTLS_PKCS1_V15 */
4027#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004028 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004029 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004030 psa_rsa_oaep_set_padding_mode( alg, rsa );
4031 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine072ac562018-06-30 00:21:29 +02004032 mbedtls_ctr_drbg_random,
4033 &global_data.ctr_drbg,
4034 MBEDTLS_RSA_PRIVATE,
4035 salt, salt_length,
4036 output_length,
4037 input,
4038 output,
4039 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004040 }
4041 else
4042#endif /* MBEDTLS_PKCS1_V21 */
4043 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004044 mbedtls_rsa_free( rsa );
4045 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004046 return( PSA_ERROR_INVALID_ARGUMENT );
4047 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004048
Steven Cooremana2371e52020-07-28 14:30:39 +02004049 mbedtls_rsa_free( rsa );
4050 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004051 return( mbedtls_to_psa_error( ret ) );
4052 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004053 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004054#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004055 {
4056 return( PSA_ERROR_NOT_SUPPORTED );
4057 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004058}
Gilles Peskine20035e32018-02-03 22:44:14 +01004059
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004060
4061
mohammad1603503973b2018-03-12 15:59:30 +02004062/****************************************************************/
4063/* Symmetric cryptography */
4064/****************************************************************/
4065
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004066/* Initialize the cipher operation structure. Once this function has been
4067 * called, psa_cipher_abort can run and will do the right thing. */
4068static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
4069 psa_algorithm_t alg )
4070{
Gilles Peskinec06e0712018-06-20 16:21:04 +02004071 if( ! PSA_ALG_IS_CIPHER( alg ) )
4072 {
4073 memset( operation, 0, sizeof( *operation ) );
4074 return( PSA_ERROR_INVALID_ARGUMENT );
4075 }
4076
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004077 operation->alg = alg;
4078 operation->key_set = 0;
4079 operation->iv_set = 0;
4080 operation->iv_required = 1;
4081 operation->iv_size = 0;
4082 operation->block_size = 0;
4083 mbedtls_cipher_init( &operation->ctx.cipher );
4084 return( PSA_SUCCESS );
4085}
4086
Gilles Peskinee553c652018-06-04 16:22:46 +02004087static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004088 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02004089 psa_algorithm_t alg,
4090 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004091{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004092 int ret = 0;
4093 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004094 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004095 size_t key_bits;
4096 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004097 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4098 PSA_KEY_USAGE_ENCRYPT :
4099 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004100
Jaeden Amero36ee5d02019-02-19 09:25:10 +00004101 /* A context must be freshly initialized before it can be set up. */
4102 if( operation->alg != 0 )
4103 {
4104 return( PSA_ERROR_BAD_STATE );
4105 }
4106
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004107 status = psa_cipher_init( operation, alg );
4108 if( status != PSA_SUCCESS )
4109 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004110
Gilles Peskine28f8f302019-07-24 13:30:31 +02004111 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004112 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004113 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004114 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02004115
Gilles Peskine8e338702019-07-30 20:06:31 +02004116 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004117 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004118 {
4119 status = PSA_ERROR_NOT_SUPPORTED;
4120 goto exit;
4121 }
mohammad1603503973b2018-03-12 15:59:30 +02004122
mohammad1603503973b2018-03-12 15:59:30 +02004123 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004124 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004125 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004126
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004127#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004128 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004129 {
4130 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004131 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004132 memcpy( keys, slot->data.key.data, 16 );
4133 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004134 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4135 keys,
4136 192, cipher_operation );
4137 }
4138 else
4139#endif
4140 {
4141 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004142 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004143 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004144 }
Moran Peker41deec42018-04-04 15:43:05 +03004145 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004146 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004147
mohammad16038481e742018-03-18 13:57:31 +02004148#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004149 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004150 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004151 case PSA_ALG_CBC_NO_PADDING:
4152 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4153 MBEDTLS_PADDING_NONE );
4154 break;
4155 case PSA_ALG_CBC_PKCS7:
4156 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4157 MBEDTLS_PADDING_PKCS7 );
4158 break;
4159 default:
4160 /* The algorithm doesn't involve padding. */
4161 ret = 0;
4162 break;
4163 }
4164 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004165 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004166#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4167
mohammad1603503973b2018-03-12 15:59:30 +02004168 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004169 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004170 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004171 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02004172 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004173 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004174 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004175#if defined(MBEDTLS_CHACHA20_C)
4176 else
4177 if( alg == PSA_ALG_CHACHA20 )
4178 operation->iv_size = 12;
4179#endif
mohammad1603503973b2018-03-12 15:59:30 +02004180
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004181exit:
4182 if( status == 0 )
4183 status = mbedtls_to_psa_error( ret );
4184 if( status != 0 )
4185 psa_cipher_abort( operation );
4186 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004187}
4188
Gilles Peskinefe119512018-07-08 21:39:34 +02004189psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004190 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004191 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004192{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004193 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004194}
4195
Gilles Peskinefe119512018-07-08 21:39:34 +02004196psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004197 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004198 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004199{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004200 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004201}
4202
Gilles Peskinefe119512018-07-08 21:39:34 +02004203psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004204 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004205 size_t iv_size,
4206 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004207{
itayzafrir534bd7c2018-08-02 13:56:32 +03004208 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004209 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004210 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004211 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004212 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004213 }
Moran Peker41deec42018-04-04 15:43:05 +03004214 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004215 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004216 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004217 goto exit;
4218 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004219 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4220 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004221 if( ret != 0 )
4222 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004223 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004224 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004225 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004226
mohammad16038481e742018-03-18 13:57:31 +02004227 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004228 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004229
Moran Peker395db872018-05-31 14:07:14 +03004230exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03004231 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03004232 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004233 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004234}
4235
Gilles Peskinefe119512018-07-08 21:39:34 +02004236psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004237 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004238 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004239{
itayzafrir534bd7c2018-08-02 13:56:32 +03004240 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004241 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004242 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004243 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004244 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004245 }
Moran Pekera28258c2018-05-29 16:25:04 +03004246 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004247 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004248 status = PSA_ERROR_INVALID_ARGUMENT;
4249 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004250 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004251 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4252 status = mbedtls_to_psa_error( ret );
4253exit:
4254 if( status == PSA_SUCCESS )
4255 operation->iv_set = 1;
4256 else
mohammad1603503973b2018-03-12 15:59:30 +02004257 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004258 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004259}
4260
Gilles Peskinee553c652018-06-04 16:22:46 +02004261psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4262 const uint8_t *input,
4263 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004264 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004265 size_t output_size,
4266 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004267{
itayzafrir534bd7c2018-08-02 13:56:32 +03004268 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004269 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004270 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00004271
4272 if( operation->alg == 0 )
4273 {
4274 return( PSA_ERROR_BAD_STATE );
4275 }
4276
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004277 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004278 {
4279 /* Take the unprocessed partial block left over from previous
4280 * update calls, if any, plus the input to this call. Remove
4281 * the last partial block, if any. You get the data that will be
4282 * output in this call. */
4283 expected_output_size =
4284 ( operation->ctx.cipher.unprocessed_len + input_length )
4285 / operation->block_size * operation->block_size;
4286 }
4287 else
4288 {
4289 expected_output_size = input_length;
4290 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004291
Gilles Peskine89d789c2018-06-04 17:17:16 +02004292 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004293 {
4294 status = PSA_ERROR_BUFFER_TOO_SMALL;
4295 goto exit;
4296 }
mohammad160382759612018-03-12 18:16:40 +02004297
mohammad1603503973b2018-03-12 15:59:30 +02004298 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02004299 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03004300 status = mbedtls_to_psa_error( ret );
4301exit:
4302 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004303 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004304 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004305}
4306
Gilles Peskinee553c652018-06-04 16:22:46 +02004307psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4308 uint8_t *output,
4309 size_t output_size,
4310 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004311{
David Saadab4ecc272019-02-14 13:48:10 +02004312 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01004313 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02004314 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03004315
mohammad1603503973b2018-03-12 15:59:30 +02004316 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004317 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004318 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004319 }
4320 if( operation->iv_required && ! operation->iv_set )
4321 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004322 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004323 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004324
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004325 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004326 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4327 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004328 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004329 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004330 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004331 }
4332
Janos Follath315b51c2018-07-09 16:04:51 +01004333 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4334 temp_output_buffer,
4335 output_length );
4336 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004337 {
Janos Follath315b51c2018-07-09 16:04:51 +01004338 status = mbedtls_to_psa_error( cipher_ret );
4339 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004340 }
Janos Follath315b51c2018-07-09 16:04:51 +01004341
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004342 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004343 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004344 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004345 memcpy( output, temp_output_buffer, *output_length );
4346 else
4347 {
Janos Follath315b51c2018-07-09 16:04:51 +01004348 status = PSA_ERROR_BUFFER_TOO_SMALL;
4349 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004350 }
mohammad1603503973b2018-03-12 15:59:30 +02004351
Gilles Peskine3f108122018-12-07 18:14:53 +01004352 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004353 status = psa_cipher_abort( operation );
4354
4355 return( status );
4356
4357error:
4358
4359 *output_length = 0;
4360
Gilles Peskine3f108122018-12-07 18:14:53 +01004361 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004362 (void) psa_cipher_abort( operation );
4363
4364 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004365}
4366
Gilles Peskinee553c652018-06-04 16:22:46 +02004367psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4368{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004369 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004370 {
4371 /* The object has (apparently) been initialized but it is not
4372 * in use. It's ok to call abort on such an object, and there's
4373 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004374 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004375 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004376
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004377 /* Sanity check (shouldn't happen: operation->alg should
4378 * always have been initialized to a valid value). */
4379 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4380 return( PSA_ERROR_BAD_STATE );
4381
mohammad1603503973b2018-03-12 15:59:30 +02004382 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004383
Moran Peker41deec42018-04-04 15:43:05 +03004384 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004385 operation->key_set = 0;
4386 operation->iv_set = 0;
4387 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004388 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004389 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004390
Moran Peker395db872018-05-31 14:07:14 +03004391 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004392}
4393
Gilles Peskinea0655c32018-04-30 17:06:50 +02004394
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004395
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004396
mohammad16035955c982018-04-26 00:53:03 +03004397/****************************************************************/
4398/* AEAD */
4399/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004400
Gilles Peskineedf9a652018-08-17 18:11:56 +02004401typedef struct
4402{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004403 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004404 const mbedtls_cipher_info_t *cipher_info;
4405 union
4406 {
4407#if defined(MBEDTLS_CCM_C)
4408 mbedtls_ccm_context ccm;
4409#endif /* MBEDTLS_CCM_C */
4410#if defined(MBEDTLS_GCM_C)
4411 mbedtls_gcm_context gcm;
4412#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004413#if defined(MBEDTLS_CHACHAPOLY_C)
4414 mbedtls_chachapoly_context chachapoly;
4415#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004416 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004417 psa_algorithm_t core_alg;
4418 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004419 uint8_t tag_length;
4420} aead_operation_t;
4421
Gilles Peskine30a9e412019-01-14 18:36:12 +01004422static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004423{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004424 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004425 {
4426#if defined(MBEDTLS_CCM_C)
4427 case PSA_ALG_CCM:
4428 mbedtls_ccm_free( &operation->ctx.ccm );
4429 break;
4430#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004431#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004432 case PSA_ALG_GCM:
4433 mbedtls_gcm_free( &operation->ctx.gcm );
4434 break;
4435#endif /* MBEDTLS_GCM_C */
4436 }
4437}
4438
4439static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004440 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004441 psa_key_usage_t usage,
4442 psa_algorithm_t alg )
4443{
4444 psa_status_t status;
4445 size_t key_bits;
4446 mbedtls_cipher_id_t cipher_id;
4447
Gilles Peskine28f8f302019-07-24 13:30:31 +02004448 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004449 if( status != PSA_SUCCESS )
4450 return( status );
4451
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004452 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004453
4454 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004455 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004456 &cipher_id );
4457 if( operation->cipher_info == NULL )
4458 return( PSA_ERROR_NOT_SUPPORTED );
4459
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004460 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004461 {
4462#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004463 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4464 operation->core_alg = PSA_ALG_CCM;
4465 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004466 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4467 * The call to mbedtls_ccm_encrypt_and_tag or
4468 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004469 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004470 return( PSA_ERROR_INVALID_ARGUMENT );
4471 mbedtls_ccm_init( &operation->ctx.ccm );
4472 status = mbedtls_to_psa_error(
4473 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004474 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004475 (unsigned int) key_bits ) );
4476 if( status != 0 )
4477 goto cleanup;
4478 break;
4479#endif /* MBEDTLS_CCM_C */
4480
4481#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004482 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4483 operation->core_alg = PSA_ALG_GCM;
4484 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004485 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4486 * The call to mbedtls_gcm_crypt_and_tag or
4487 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004488 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004489 return( PSA_ERROR_INVALID_ARGUMENT );
4490 mbedtls_gcm_init( &operation->ctx.gcm );
4491 status = mbedtls_to_psa_error(
4492 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004493 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004494 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004495 if( status != 0 )
4496 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004497 break;
4498#endif /* MBEDTLS_GCM_C */
4499
Gilles Peskine26869f22019-05-06 15:25:00 +02004500#if defined(MBEDTLS_CHACHAPOLY_C)
4501 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4502 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4503 operation->full_tag_length = 16;
4504 /* We only support the default tag length. */
4505 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4506 return( PSA_ERROR_NOT_SUPPORTED );
4507 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4508 status = mbedtls_to_psa_error(
4509 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004510 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004511 if( status != 0 )
4512 goto cleanup;
4513 break;
4514#endif /* MBEDTLS_CHACHAPOLY_C */
4515
Gilles Peskineedf9a652018-08-17 18:11:56 +02004516 default:
4517 return( PSA_ERROR_NOT_SUPPORTED );
4518 }
4519
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004520 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4521 {
4522 status = PSA_ERROR_INVALID_ARGUMENT;
4523 goto cleanup;
4524 }
4525 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004526
Gilles Peskineedf9a652018-08-17 18:11:56 +02004527 return( PSA_SUCCESS );
4528
4529cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004530 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004531 return( status );
4532}
4533
Gilles Peskinec5487a82018-12-03 18:08:14 +01004534psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004535 psa_algorithm_t alg,
4536 const uint8_t *nonce,
4537 size_t nonce_length,
4538 const uint8_t *additional_data,
4539 size_t additional_data_length,
4540 const uint8_t *plaintext,
4541 size_t plaintext_length,
4542 uint8_t *ciphertext,
4543 size_t ciphertext_size,
4544 size_t *ciphertext_length )
4545{
mohammad16035955c982018-04-26 00:53:03 +03004546 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004547 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004548 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004549
mohammad1603f08a5502018-06-03 15:05:47 +03004550 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004551
Gilles Peskinec5487a82018-12-03 18:08:14 +01004552 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004553 if( status != PSA_SUCCESS )
4554 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004555
Gilles Peskineedf9a652018-08-17 18:11:56 +02004556 /* For all currently supported modes, the tag is at the end of the
4557 * ciphertext. */
4558 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4559 {
4560 status = PSA_ERROR_BUFFER_TOO_SMALL;
4561 goto exit;
4562 }
4563 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004564
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004565#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004566 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004567 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004568 status = mbedtls_to_psa_error(
4569 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4570 MBEDTLS_GCM_ENCRYPT,
4571 plaintext_length,
4572 nonce, nonce_length,
4573 additional_data, additional_data_length,
4574 plaintext, ciphertext,
4575 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004576 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004577 else
4578#endif /* MBEDTLS_GCM_C */
4579#if defined(MBEDTLS_CCM_C)
4580 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004581 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004582 status = mbedtls_to_psa_error(
4583 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4584 plaintext_length,
4585 nonce, nonce_length,
4586 additional_data,
4587 additional_data_length,
4588 plaintext, ciphertext,
4589 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004590 }
mohammad16035c8845f2018-05-09 05:40:09 -07004591 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004592#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004593#if defined(MBEDTLS_CHACHAPOLY_C)
4594 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4595 {
4596 if( nonce_length != 12 || operation.tag_length != 16 )
4597 {
4598 status = PSA_ERROR_NOT_SUPPORTED;
4599 goto exit;
4600 }
4601 status = mbedtls_to_psa_error(
4602 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4603 plaintext_length,
4604 nonce,
4605 additional_data,
4606 additional_data_length,
4607 plaintext,
4608 ciphertext,
4609 tag ) );
4610 }
4611 else
4612#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004613 {
mohammad1603554faad2018-06-03 15:07:38 +03004614 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004615 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004616
Gilles Peskineedf9a652018-08-17 18:11:56 +02004617 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4618 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004619
Gilles Peskineedf9a652018-08-17 18:11:56 +02004620exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004621 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004622 if( status == PSA_SUCCESS )
4623 *ciphertext_length = plaintext_length + operation.tag_length;
4624 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004625}
4626
Gilles Peskineee652a32018-06-01 19:23:52 +02004627/* Locate the tag in a ciphertext buffer containing the encrypted data
4628 * followed by the tag. Return the length of the part preceding the tag in
4629 * *plaintext_length. This is the size of the plaintext in modes where
4630 * the encrypted data has the same size as the plaintext, such as
4631 * CCM and GCM. */
4632static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4633 const uint8_t *ciphertext,
4634 size_t ciphertext_length,
4635 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004636 const uint8_t **p_tag )
4637{
4638 size_t payload_length;
4639 if( tag_length > ciphertext_length )
4640 return( PSA_ERROR_INVALID_ARGUMENT );
4641 payload_length = ciphertext_length - tag_length;
4642 if( payload_length > plaintext_size )
4643 return( PSA_ERROR_BUFFER_TOO_SMALL );
4644 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004645 return( PSA_SUCCESS );
4646}
4647
Gilles Peskinec5487a82018-12-03 18:08:14 +01004648psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004649 psa_algorithm_t alg,
4650 const uint8_t *nonce,
4651 size_t nonce_length,
4652 const uint8_t *additional_data,
4653 size_t additional_data_length,
4654 const uint8_t *ciphertext,
4655 size_t ciphertext_length,
4656 uint8_t *plaintext,
4657 size_t plaintext_size,
4658 size_t *plaintext_length )
4659{
mohammad16035955c982018-04-26 00:53:03 +03004660 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004661 aead_operation_t operation;
4662 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004663
Gilles Peskineee652a32018-06-01 19:23:52 +02004664 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004665
Gilles Peskinec5487a82018-12-03 18:08:14 +01004666 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004667 if( status != PSA_SUCCESS )
4668 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004669
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004670 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4671 ciphertext, ciphertext_length,
4672 plaintext_size, &tag );
4673 if( status != PSA_SUCCESS )
4674 goto exit;
4675
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004676#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004677 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004678 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004679 status = mbedtls_to_psa_error(
4680 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4681 ciphertext_length - operation.tag_length,
4682 nonce, nonce_length,
4683 additional_data,
4684 additional_data_length,
4685 tag, operation.tag_length,
4686 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004687 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004688 else
4689#endif /* MBEDTLS_GCM_C */
4690#if defined(MBEDTLS_CCM_C)
4691 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004692 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004693 status = mbedtls_to_psa_error(
4694 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4695 ciphertext_length - operation.tag_length,
4696 nonce, nonce_length,
4697 additional_data,
4698 additional_data_length,
4699 ciphertext, plaintext,
4700 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004701 }
mohammad160339574652018-06-01 04:39:53 -07004702 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004703#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004704#if defined(MBEDTLS_CHACHAPOLY_C)
4705 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4706 {
4707 if( nonce_length != 12 || operation.tag_length != 16 )
4708 {
4709 status = PSA_ERROR_NOT_SUPPORTED;
4710 goto exit;
4711 }
4712 status = mbedtls_to_psa_error(
4713 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4714 ciphertext_length - operation.tag_length,
4715 nonce,
4716 additional_data,
4717 additional_data_length,
4718 tag,
4719 ciphertext,
4720 plaintext ) );
4721 }
4722 else
4723#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004724 {
mohammad1603554faad2018-06-03 15:07:38 +03004725 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004726 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004727
Gilles Peskineedf9a652018-08-17 18:11:56 +02004728 if( status != PSA_SUCCESS && plaintext_size != 0 )
4729 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004730
Gilles Peskineedf9a652018-08-17 18:11:56 +02004731exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004732 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004733 if( status == PSA_SUCCESS )
4734 *plaintext_length = ciphertext_length - operation.tag_length;
4735 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004736}
4737
Gilles Peskinea0655c32018-04-30 17:06:50 +02004738
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004739
Gilles Peskine20035e32018-02-03 22:44:14 +01004740/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004741/* Generators */
4742/****************************************************************/
4743
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004744#define HKDF_STATE_INIT 0 /* no input yet */
4745#define HKDF_STATE_STARTED 1 /* got salt */
4746#define HKDF_STATE_KEYED 2 /* got key */
4747#define HKDF_STATE_OUTPUT 3 /* output started */
4748
Gilles Peskinecbe66502019-05-16 16:59:18 +02004749static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004750 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004751{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004752 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4753 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004754 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004755 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004756}
4757
4758
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004759psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004760{
4761 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004762 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004763 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004764 {
4765 /* The object has (apparently) been initialized but it is not
4766 * in use. It's ok to call abort on such an object, and there's
4767 * nothing to do. */
4768 }
4769 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004770#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004771 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004772 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004773 mbedtls_free( operation->ctx.hkdf.info );
4774 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004775 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004776 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004777 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004778 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004779 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004780 if( operation->ctx.tls12_prf.seed != NULL )
4781 {
4782 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4783 operation->ctx.tls12_prf.seed_length );
4784 mbedtls_free( operation->ctx.tls12_prf.seed );
4785 }
4786
4787 if( operation->ctx.tls12_prf.label != NULL )
4788 {
4789 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4790 operation->ctx.tls12_prf.label_length );
4791 mbedtls_free( operation->ctx.tls12_prf.label );
4792 }
4793
4794 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4795
4796 /* We leave the fields Ai and output_block to be erased safely by the
4797 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004798 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004799 else
4800#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004801 {
4802 status = PSA_ERROR_BAD_STATE;
4803 }
Janos Follath083036a2019-06-11 10:22:26 +01004804 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004805 return( status );
4806}
4807
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004808psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004809 size_t *capacity)
4810{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004811 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004812 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004813 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004814 return PSA_ERROR_BAD_STATE;
4815 }
4816
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004817 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004818 return( PSA_SUCCESS );
4819}
4820
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004821psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004822 size_t capacity )
4823{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004824 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004825 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004826 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004827 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004828 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004829 return( PSA_SUCCESS );
4830}
4831
Gilles Peskinebef7f142018-07-12 17:22:21 +02004832#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004833/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004834 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004835static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004836 psa_algorithm_t hash_alg,
4837 uint8_t *output,
4838 size_t output_length )
4839{
4840 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4841 psa_status_t status;
4842
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004843 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4844 return( PSA_ERROR_BAD_STATE );
4845 hkdf->state = HKDF_STATE_OUTPUT;
4846
Gilles Peskinebef7f142018-07-12 17:22:21 +02004847 while( output_length != 0 )
4848 {
4849 /* Copy what remains of the current block */
4850 uint8_t n = hash_length - hkdf->offset_in_block;
4851 if( n > output_length )
4852 n = (uint8_t) output_length;
4853 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4854 output += n;
4855 output_length -= n;
4856 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004857 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004858 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004859 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004860 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004861 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004862 * object was corrupted or if this function is called directly
4863 * inside the library. */
4864 if( hkdf->block_number == 0xff )
4865 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004866
4867 /* We need a new block */
4868 ++hkdf->block_number;
4869 hkdf->offset_in_block = 0;
4870 status = psa_hmac_setup_internal( &hkdf->hmac,
4871 hkdf->prk, hash_length,
4872 hash_alg );
4873 if( status != PSA_SUCCESS )
4874 return( status );
4875 if( hkdf->block_number != 1 )
4876 {
4877 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4878 hkdf->output_block,
4879 hash_length );
4880 if( status != PSA_SUCCESS )
4881 return( status );
4882 }
4883 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4884 hkdf->info,
4885 hkdf->info_length );
4886 if( status != PSA_SUCCESS )
4887 return( status );
4888 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4889 &hkdf->block_number, 1 );
4890 if( status != PSA_SUCCESS )
4891 return( status );
4892 status = psa_hmac_finish_internal( &hkdf->hmac,
4893 hkdf->output_block,
4894 sizeof( hkdf->output_block ) );
4895 if( status != PSA_SUCCESS )
4896 return( status );
4897 }
4898
4899 return( PSA_SUCCESS );
4900}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004901
Janos Follath7742fee2019-06-17 12:58:10 +01004902static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4903 psa_tls12_prf_key_derivation_t *tls12_prf,
4904 psa_algorithm_t alg )
4905{
4906 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4907 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004908 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4909 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004910
Janos Follath7742fee2019-06-17 12:58:10 +01004911 /* We can't be wanting more output after block 0xff, otherwise
4912 * the capacity check in psa_key_derivation_output_bytes() would have
4913 * prevented this call. It could happen only if the operation
4914 * object was corrupted or if this function is called directly
4915 * inside the library. */
4916 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004917 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004918
4919 /* We need a new block */
4920 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004921 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004922
4923 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4924 *
4925 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4926 *
4927 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4928 * HMAC_hash(secret, A(2) + seed) +
4929 * HMAC_hash(secret, A(3) + seed) + ...
4930 *
4931 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004932 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004933 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004934 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004935 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004936 * is currently extracted as `output_block` and where i is
4937 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004938 */
4939
Janos Follathea29bfb2019-06-19 12:21:20 +01004940 /* Save the hash context before using it, to preserve the hash state with
4941 * only the inner padding in it. We need this, because inner padding depends
4942 * on the key (secret in the RFC's terminology). */
4943 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4944 if( status != PSA_SUCCESS )
4945 goto cleanup;
4946
4947 /* Calculate A(i) where i = tls12_prf->block_number. */
4948 if( tls12_prf->block_number == 1 )
4949 {
4950 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4951 * the variable seed and in this instance means it in the context of the
4952 * P_hash function, where seed = label + seed.) */
4953 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4954 tls12_prf->label, tls12_prf->label_length );
4955 if( status != PSA_SUCCESS )
4956 goto cleanup;
4957 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4958 tls12_prf->seed, tls12_prf->seed_length );
4959 if( status != PSA_SUCCESS )
4960 goto cleanup;
4961 }
4962 else
4963 {
4964 /* A(i) = HMAC_hash(secret, A(i-1)) */
4965 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4966 tls12_prf->Ai, hash_length );
4967 if( status != PSA_SUCCESS )
4968 goto cleanup;
4969 }
4970
4971 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4972 tls12_prf->Ai, hash_length );
4973 if( status != PSA_SUCCESS )
4974 goto cleanup;
4975 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4976 if( status != PSA_SUCCESS )
4977 goto cleanup;
4978
4979 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4980 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4981 tls12_prf->Ai, hash_length );
4982 if( status != PSA_SUCCESS )
4983 goto cleanup;
4984 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4985 tls12_prf->label, tls12_prf->label_length );
4986 if( status != PSA_SUCCESS )
4987 goto cleanup;
4988 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4989 tls12_prf->seed, tls12_prf->seed_length );
4990 if( status != PSA_SUCCESS )
4991 goto cleanup;
4992 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4993 tls12_prf->output_block, hash_length );
4994 if( status != PSA_SUCCESS )
4995 goto cleanup;
4996 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4997 if( status != PSA_SUCCESS )
4998 goto cleanup;
4999
Janos Follath7742fee2019-06-17 12:58:10 +01005000
5001cleanup:
5002
Janos Follathea29bfb2019-06-19 12:21:20 +01005003 cleanup_status = psa_hash_abort( &backup );
5004 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5005 status = cleanup_status;
5006
5007 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005008}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005009
Janos Follath844eb0e2019-06-19 12:10:49 +01005010static psa_status_t psa_key_derivation_tls12_prf_read(
5011 psa_tls12_prf_key_derivation_t *tls12_prf,
5012 psa_algorithm_t alg,
5013 uint8_t *output,
5014 size_t output_length )
5015{
5016 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5017 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5018 psa_status_t status;
5019 uint8_t offset, length;
5020
5021 while( output_length != 0 )
5022 {
5023 /* Check if we have fully processed the current block. */
5024 if( tls12_prf->left_in_block == 0 )
5025 {
5026 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5027 alg );
5028 if( status != PSA_SUCCESS )
5029 return( status );
5030
5031 continue;
5032 }
5033
5034 if( tls12_prf->left_in_block > output_length )
5035 length = (uint8_t) output_length;
5036 else
5037 length = tls12_prf->left_in_block;
5038
5039 offset = hash_length - tls12_prf->left_in_block;
5040 memcpy( output, tls12_prf->output_block + offset, length );
5041 output += length;
5042 output_length -= length;
5043 tls12_prf->left_in_block -= length;
5044 }
5045
5046 return( PSA_SUCCESS );
5047}
Gilles Peskinebef7f142018-07-12 17:22:21 +02005048#endif /* MBEDTLS_MD_C */
5049
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005050psa_status_t psa_key_derivation_output_bytes(
5051 psa_key_derivation_operation_t *operation,
5052 uint8_t *output,
5053 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005054{
5055 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005056 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005057
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005058 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005059 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005060 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005061 return PSA_ERROR_BAD_STATE;
5062 }
5063
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005064 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005065 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005066 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005067 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005068 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005069 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005070 goto exit;
5071 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005072 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005073 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005074 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005075 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005076 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5077 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005078 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005079 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005080 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005081 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005082 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005083
Gilles Peskinebef7f142018-07-12 17:22:21 +02005084#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005085 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005086 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005087 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005088 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005089 output, output_length );
5090 }
Janos Follathadbec812019-06-14 11:05:39 +01005091 else
Janos Follathadbec812019-06-14 11:05:39 +01005092 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005093 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005094 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005095 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005096 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005097 output_length );
5098 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005099 else
5100#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005101 {
5102 return( PSA_ERROR_BAD_STATE );
5103 }
5104
5105exit:
5106 if( status != PSA_SUCCESS )
5107 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005108 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005109 * This allows us to differentiate between exhausted operations and
5110 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5111 * operations. */
5112 psa_algorithm_t alg = operation->alg;
5113 psa_key_derivation_abort( operation );
5114 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005115 memset( output, '!', output_length );
5116 }
5117 return( status );
5118}
5119
Gilles Peskine08542d82018-07-19 17:05:42 +02005120#if defined(MBEDTLS_DES_C)
5121static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5122{
5123 if( data_size >= 8 )
5124 mbedtls_des_key_set_parity( data );
5125 if( data_size >= 16 )
5126 mbedtls_des_key_set_parity( data + 8 );
5127 if( data_size >= 24 )
5128 mbedtls_des_key_set_parity( data + 16 );
5129}
5130#endif /* MBEDTLS_DES_C */
5131
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005132static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005133 psa_key_slot_t *slot,
5134 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005135 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005136{
5137 uint8_t *data = NULL;
5138 size_t bytes = PSA_BITS_TO_BYTES( bits );
5139 psa_status_t status;
5140
Gilles Peskine8e338702019-07-30 20:06:31 +02005141 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005142 return( PSA_ERROR_INVALID_ARGUMENT );
5143 if( bits % 8 != 0 )
5144 return( PSA_ERROR_INVALID_ARGUMENT );
5145 data = mbedtls_calloc( 1, bytes );
5146 if( data == NULL )
5147 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5148
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005149 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005150 if( status != PSA_SUCCESS )
5151 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005152#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005153 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005154 psa_des_set_key_parity( data, bytes );
5155#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005156 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005157
5158exit:
5159 mbedtls_free( data );
5160 return( status );
5161}
5162
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005163psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005164 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005165 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005166{
5167 psa_status_t status;
5168 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005169 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005170
5171 /* Reject any attempt to create a zero-length key so that we don't
5172 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5173 if( psa_get_key_bits( attributes ) == 0 )
5174 return( PSA_ERROR_INVALID_ARGUMENT );
5175
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005176 if( ! operation->can_output_key )
5177 return( PSA_ERROR_NOT_PERMITTED );
5178
Gilles Peskinedf179142019-07-15 22:02:14 +02005179 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5180 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005181#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5182 if( driver != NULL )
5183 {
5184 /* Deriving a key in a secure element is not implemented yet. */
5185 status = PSA_ERROR_NOT_SUPPORTED;
5186 }
5187#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005188 if( status == PSA_SUCCESS )
5189 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005190 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005191 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005192 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005193 }
5194 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005195 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005196 if( status != PSA_SUCCESS )
5197 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005198 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005199 *handle = 0;
5200 }
5201 return( status );
5202}
5203
Gilles Peskineeab56e42018-07-12 17:12:33 +02005204
5205
5206/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005207/* Key derivation */
5208/****************************************************************/
5209
Gilles Peskine969c5d62019-01-16 15:53:06 +01005210static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005211 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005212 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005213{
Janos Follath5fe19732019-06-20 15:09:30 +01005214 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5215 * initialisers for this union leave some bytes unspecified.) */
5216 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5217
Gilles Peskine969c5d62019-01-16 15:53:06 +01005218 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005219#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005220 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5221 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5222 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005223 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005224 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005225 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5226 if( hash_size == 0 )
5227 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005228 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5229 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005230 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005231 {
5232 return( PSA_ERROR_NOT_SUPPORTED );
5233 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005234 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005235 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005236 }
5237#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005238 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005239 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005240}
5241
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005242psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005243 psa_algorithm_t alg )
5244{
5245 psa_status_t status;
5246
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005247 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005248 return( PSA_ERROR_BAD_STATE );
5249
Gilles Peskine6843c292019-01-18 16:44:49 +01005250 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5251 return( PSA_ERROR_INVALID_ARGUMENT );
5252 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005253 {
5254 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005255 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005256 }
5257 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5258 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005259 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005260 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005261 else
5262 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005263
5264 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005265 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005266 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005267}
5268
5269#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005270static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005271 psa_algorithm_t hash_alg,
5272 psa_key_derivation_step_t step,
5273 const uint8_t *data,
5274 size_t data_length )
5275{
5276 psa_status_t status;
5277 switch( step )
5278 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005279 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005280 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005281 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005282 status = psa_hmac_setup_internal( &hkdf->hmac,
5283 data, data_length,
5284 hash_alg );
5285 if( status != PSA_SUCCESS )
5286 return( status );
5287 hkdf->state = HKDF_STATE_STARTED;
5288 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005289 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005290 /* If no salt was provided, use an empty salt. */
5291 if( hkdf->state == HKDF_STATE_INIT )
5292 {
5293 status = psa_hmac_setup_internal( &hkdf->hmac,
5294 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005295 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005296 if( status != PSA_SUCCESS )
5297 return( status );
5298 hkdf->state = HKDF_STATE_STARTED;
5299 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005300 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005301 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005302 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5303 data, data_length );
5304 if( status != PSA_SUCCESS )
5305 return( status );
5306 status = psa_hmac_finish_internal( &hkdf->hmac,
5307 hkdf->prk,
5308 sizeof( hkdf->prk ) );
5309 if( status != PSA_SUCCESS )
5310 return( status );
5311 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5312 hkdf->block_number = 0;
5313 hkdf->state = HKDF_STATE_KEYED;
5314 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005315 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005316 if( hkdf->state == HKDF_STATE_OUTPUT )
5317 return( PSA_ERROR_BAD_STATE );
5318 if( hkdf->info_set )
5319 return( PSA_ERROR_BAD_STATE );
5320 hkdf->info_length = data_length;
5321 if( data_length != 0 )
5322 {
5323 hkdf->info = mbedtls_calloc( 1, data_length );
5324 if( hkdf->info == NULL )
5325 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5326 memcpy( hkdf->info, data, data_length );
5327 }
5328 hkdf->info_set = 1;
5329 return( PSA_SUCCESS );
5330 default:
5331 return( PSA_ERROR_INVALID_ARGUMENT );
5332 }
5333}
Janos Follathb03233e2019-06-11 15:30:30 +01005334
Janos Follathf08e2652019-06-13 09:05:41 +01005335static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5336 const uint8_t *data,
5337 size_t data_length )
5338{
5339 if( prf->state != TLS12_PRF_STATE_INIT )
5340 return( PSA_ERROR_BAD_STATE );
5341
Janos Follathd6dce9f2019-07-04 09:11:38 +01005342 if( data_length != 0 )
5343 {
5344 prf->seed = mbedtls_calloc( 1, data_length );
5345 if( prf->seed == NULL )
5346 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005347
Janos Follathd6dce9f2019-07-04 09:11:38 +01005348 memcpy( prf->seed, data, data_length );
5349 prf->seed_length = data_length;
5350 }
Janos Follathf08e2652019-06-13 09:05:41 +01005351
5352 prf->state = TLS12_PRF_STATE_SEED_SET;
5353
5354 return( PSA_SUCCESS );
5355}
5356
Janos Follath81550542019-06-13 14:26:34 +01005357static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5358 psa_algorithm_t hash_alg,
5359 const uint8_t *data,
5360 size_t data_length )
5361{
5362 psa_status_t status;
5363 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5364 return( PSA_ERROR_BAD_STATE );
5365
5366 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5367 if( status != PSA_SUCCESS )
5368 return( status );
5369
5370 prf->state = TLS12_PRF_STATE_KEY_SET;
5371
5372 return( PSA_SUCCESS );
5373}
5374
Janos Follath6660f0e2019-06-17 08:44:03 +01005375static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5376 psa_tls12_prf_key_derivation_t *prf,
5377 psa_algorithm_t hash_alg,
5378 const uint8_t *data,
5379 size_t data_length )
5380{
5381 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005382 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5383 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005384
5385 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5386 return( PSA_ERROR_INVALID_ARGUMENT );
5387
5388 /* Quoting RFC 4279, Section 2:
5389 *
5390 * The premaster secret is formed as follows: if the PSK is N octets
5391 * long, concatenate a uint16 with the value N, N zero octets, a second
5392 * uint16 with the value N, and the PSK itself.
5393 */
5394
Janos Follath40e13932019-06-26 13:22:29 +01005395 *cur++ = ( data_length >> 8 ) & 0xff;
5396 *cur++ = ( data_length >> 0 ) & 0xff;
5397 memset( cur, 0, data_length );
5398 cur += data_length;
5399 *cur++ = pms[0];
5400 *cur++ = pms[1];
5401 memcpy( cur, data, data_length );
5402 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005403
Janos Follath40e13932019-06-26 13:22:29 +01005404 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005405
5406 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5407 return( status );
5408}
5409
Janos Follath63028dd2019-06-13 09:15:47 +01005410static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5411 const uint8_t *data,
5412 size_t data_length )
5413{
5414 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5415 return( PSA_ERROR_BAD_STATE );
5416
Janos Follathd6dce9f2019-07-04 09:11:38 +01005417 if( data_length != 0 )
5418 {
5419 prf->label = mbedtls_calloc( 1, data_length );
5420 if( prf->label == NULL )
5421 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005422
Janos Follathd6dce9f2019-07-04 09:11:38 +01005423 memcpy( prf->label, data, data_length );
5424 prf->label_length = data_length;
5425 }
Janos Follath63028dd2019-06-13 09:15:47 +01005426
5427 prf->state = TLS12_PRF_STATE_LABEL_SET;
5428
5429 return( PSA_SUCCESS );
5430}
5431
Janos Follathb03233e2019-06-11 15:30:30 +01005432static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5433 psa_algorithm_t hash_alg,
5434 psa_key_derivation_step_t step,
5435 const uint8_t *data,
5436 size_t data_length )
5437{
Janos Follathb03233e2019-06-11 15:30:30 +01005438 switch( step )
5439 {
Janos Follathf08e2652019-06-13 09:05:41 +01005440 case PSA_KEY_DERIVATION_INPUT_SEED:
5441 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005442 case PSA_KEY_DERIVATION_INPUT_SECRET:
5443 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005444 case PSA_KEY_DERIVATION_INPUT_LABEL:
5445 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005446 default:
5447 return( PSA_ERROR_INVALID_ARGUMENT );
5448 }
5449}
Janos Follath6660f0e2019-06-17 08:44:03 +01005450
5451static psa_status_t psa_tls12_prf_psk_to_ms_input(
5452 psa_tls12_prf_key_derivation_t *prf,
5453 psa_algorithm_t hash_alg,
5454 psa_key_derivation_step_t step,
5455 const uint8_t *data,
5456 size_t data_length )
5457{
5458 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005459 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005460 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5461 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005462 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005463
5464 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5465}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005466#endif /* MBEDTLS_MD_C */
5467
Gilles Peskineb8965192019-09-24 16:21:10 +02005468/** Check whether the given key type is acceptable for the given
5469 * input step of a key derivation.
5470 *
5471 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5472 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5473 * Both secret and non-secret inputs can alternatively have the type
5474 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5475 * that the input was passed as a buffer rather than via a key object.
5476 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005477static int psa_key_derivation_check_input_type(
5478 psa_key_derivation_step_t step,
5479 psa_key_type_t key_type )
5480{
5481 switch( step )
5482 {
5483 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005484 if( key_type == PSA_KEY_TYPE_DERIVE )
5485 return( PSA_SUCCESS );
5486 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005487 return( PSA_SUCCESS );
5488 break;
5489 case PSA_KEY_DERIVATION_INPUT_LABEL:
5490 case PSA_KEY_DERIVATION_INPUT_SALT:
5491 case PSA_KEY_DERIVATION_INPUT_INFO:
5492 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005493 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5494 return( PSA_SUCCESS );
5495 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005496 return( PSA_SUCCESS );
5497 break;
5498 }
5499 return( PSA_ERROR_INVALID_ARGUMENT );
5500}
5501
Janos Follathb80a94e2019-06-12 15:54:46 +01005502static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005503 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005504 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005505 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005506 const uint8_t *data,
5507 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005508{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005509 psa_status_t status;
5510 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5511
5512 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005513 if( status != PSA_SUCCESS )
5514 goto exit;
5515
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005516#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005517 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005518 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005519 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005520 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005521 step, data, data_length );
5522 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005523 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005524 {
Janos Follathb03233e2019-06-11 15:30:30 +01005525 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5526 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5527 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005528 }
5529 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5530 {
5531 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5532 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5533 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005534 }
5535 else
5536#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005537 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005538 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005539 return( PSA_ERROR_BAD_STATE );
5540 }
5541
Gilles Peskine224b0d62019-09-23 18:13:17 +02005542exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005543 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005544 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005545 return( status );
5546}
5547
Janos Follath51f4a0f2019-06-14 11:35:55 +01005548psa_status_t psa_key_derivation_input_bytes(
5549 psa_key_derivation_operation_t *operation,
5550 psa_key_derivation_step_t step,
5551 const uint8_t *data,
5552 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005553{
Gilles Peskineb8965192019-09-24 16:21:10 +02005554 return( psa_key_derivation_input_internal( operation, step,
5555 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005556 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005557}
5558
Janos Follath51f4a0f2019-06-14 11:35:55 +01005559psa_status_t psa_key_derivation_input_key(
5560 psa_key_derivation_operation_t *operation,
5561 psa_key_derivation_step_t step,
5562 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005563{
5564 psa_key_slot_t *slot;
5565 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005566
Gilles Peskine28f8f302019-07-24 13:30:31 +02005567 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005568 PSA_KEY_USAGE_DERIVE,
5569 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005570 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005571 {
5572 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005573 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005574 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005575
5576 /* Passing a key object as a SECRET input unlocks the permission
5577 * to output to a key object. */
5578 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5579 operation->can_output_key = 1;
5580
Janos Follathb80a94e2019-06-12 15:54:46 +01005581 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005582 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005583 slot->data.key.data,
5584 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005585}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005586
5587
5588
5589/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005590/* Key agreement */
5591/****************************************************************/
5592
Gilles Peskinea05219c2018-11-16 16:02:56 +01005593#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005594static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5595 size_t peer_key_length,
5596 const mbedtls_ecp_keypair *our_key,
5597 uint8_t *shared_secret,
5598 size_t shared_secret_size,
5599 size_t *shared_secret_length )
5600{
Steven Cooremana2371e52020-07-28 14:30:39 +02005601 mbedtls_ecp_keypair *their_key;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005602 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005603 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005604 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005605 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005606 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005607
Steven Cooreman7f391872020-07-30 14:57:44 +02005608 status = psa_load_ecp_representation( peer_key,
5609 peer_key_length,
5610 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5611 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005612 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005613 goto exit;
5614
Jaeden Amero97271b32019-01-10 19:38:51 +00005615 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005616 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005617 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005618 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005619 status = mbedtls_to_psa_error(
5620 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5621 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005622 goto exit;
5623
Jaeden Amero97271b32019-01-10 19:38:51 +00005624 status = mbedtls_to_psa_error(
5625 mbedtls_ecdh_calc_secret( &ecdh,
5626 shared_secret_length,
5627 shared_secret, shared_secret_size,
5628 mbedtls_ctr_drbg_random,
5629 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005630 if( status != PSA_SUCCESS )
5631 goto exit;
5632 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5633 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005634
5635exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005636 if( status != PSA_SUCCESS )
5637 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005638 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005639 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005640 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005641
Jaeden Amero97271b32019-01-10 19:38:51 +00005642 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005643}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005644#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005645
Gilles Peskine01d718c2018-09-18 12:01:02 +02005646#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5647
Gilles Peskine0216fe12019-04-11 21:23:21 +02005648static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5649 psa_key_slot_t *private_key,
5650 const uint8_t *peer_key,
5651 size_t peer_key_length,
5652 uint8_t *shared_secret,
5653 size_t shared_secret_size,
5654 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005655{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005656 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005657 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005658#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005659 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005660 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005661 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005662 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02005663 psa_status_t status = psa_load_ecp_representation(
5664 private_key->data.key.data,
5665 private_key->data.key.bytes,
5666 private_key->attr.type,
5667 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005668 if( status != PSA_SUCCESS )
5669 return status;
5670 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005671 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005672 shared_secret, shared_secret_size,
5673 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005674 mbedtls_ecp_keypair_free( ecp );
5675 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005676 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005677#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005678 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005679 (void) private_key;
5680 (void) peer_key;
5681 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005682 (void) shared_secret;
5683 (void) shared_secret_size;
5684 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005685 return( PSA_ERROR_NOT_SUPPORTED );
5686 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005687}
5688
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005689/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005690 * to potentially free embedded data structures and wipe confidential data.
5691 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005692static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005693 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005694 psa_key_slot_t *private_key,
5695 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005696 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005697{
5698 psa_status_t status;
5699 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5700 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005701 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005702
5703 /* Step 1: run the secret agreement algorithm to generate the shared
5704 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005705 status = psa_key_agreement_raw_internal( ka_alg,
5706 private_key,
5707 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005708 shared_secret,
5709 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005710 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005711 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005712 goto exit;
5713
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005714 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005715 * the shared secret. A shared secret is permitted wherever a key
5716 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005717 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005718 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005719 shared_secret,
5720 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005721
Gilles Peskine12313cd2018-06-20 00:20:32 +02005722exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005723 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005724 return( status );
5725}
5726
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005727psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005728 psa_key_derivation_step_t step,
5729 psa_key_handle_t private_key,
5730 const uint8_t *peer_key,
5731 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005732{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005733 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005734 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005735 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005736 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005737 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005738 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005739 if( status != PSA_SUCCESS )
5740 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005741 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005742 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005743 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005744 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005745 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005746 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005747}
5748
Gilles Peskinebe697d82019-05-16 18:00:41 +02005749psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5750 psa_key_handle_t private_key,
5751 const uint8_t *peer_key,
5752 size_t peer_key_length,
5753 uint8_t *output,
5754 size_t output_size,
5755 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005756{
5757 psa_key_slot_t *slot;
5758 psa_status_t status;
5759
5760 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5761 {
5762 status = PSA_ERROR_INVALID_ARGUMENT;
5763 goto exit;
5764 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005765 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005766 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005767 if( status != PSA_SUCCESS )
5768 goto exit;
5769
5770 status = psa_key_agreement_raw_internal( alg, slot,
5771 peer_key, peer_key_length,
5772 output, output_size,
5773 output_length );
5774
5775exit:
5776 if( status != PSA_SUCCESS )
5777 {
5778 /* If an error happens and is not handled properly, the output
5779 * may be used as a key to protect sensitive data. Arrange for such
5780 * a key to be random, which is likely to result in decryption or
5781 * verification errors. This is better than filling the buffer with
5782 * some constant data such as zeros, which would result in the data
5783 * being protected with a reproducible, easily knowable key.
5784 */
5785 psa_generate_random( output, output_size );
5786 *output_length = output_size;
5787 }
5788 return( status );
5789}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005790
5791
5792/****************************************************************/
5793/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005794/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005795
Gilles Peskine4c317f42018-07-12 01:24:09 +02005796psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005797 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005798{
Janos Follath24eed8d2019-11-22 13:21:35 +00005799 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005800 GUARD_MODULE_INITIALIZED;
5801
Gilles Peskinef181eca2019-08-07 13:49:00 +02005802 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5803 {
5804 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5805 output,
5806 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5807 if( ret != 0 )
5808 return( mbedtls_to_psa_error( ret ) );
5809 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5810 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5811 }
5812
Gilles Peskinee59236f2018-01-27 23:32:46 +01005813 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5814 return( mbedtls_to_psa_error( ret ) );
5815}
5816
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005817#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5818#include "mbedtls/entropy_poll.h"
5819
Gilles Peskine7228da22019-07-15 11:06:15 +02005820psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005821 size_t seed_size )
5822{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005823 if( global_data.initialized )
5824 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005825
5826 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5827 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5828 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5829 return( PSA_ERROR_INVALID_ARGUMENT );
5830
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005831 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005832}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005833#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005834
Gilles Peskinee56e8782019-04-26 17:34:02 +02005835#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5836static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5837 size_t domain_parameters_size,
5838 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005839{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005840 size_t i;
5841 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005842
Gilles Peskinee56e8782019-04-26 17:34:02 +02005843 if( domain_parameters_size == 0 )
5844 {
5845 *exponent = 65537;
5846 return( PSA_SUCCESS );
5847 }
5848
5849 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5850 * support values that fit in a 32-bit integer, which is larger than
5851 * int on just about every platform anyway. */
5852 if( domain_parameters_size > sizeof( acc ) )
5853 return( PSA_ERROR_NOT_SUPPORTED );
5854 for( i = 0; i < domain_parameters_size; i++ )
5855 acc = ( acc << 8 ) | domain_parameters[i];
5856 if( acc > INT_MAX )
5857 return( PSA_ERROR_NOT_SUPPORTED );
5858 *exponent = acc;
5859 return( PSA_SUCCESS );
5860}
5861#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5862
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005863static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005864 psa_key_slot_t *slot, size_t bits,
5865 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005866{
Gilles Peskine8e338702019-07-30 20:06:31 +02005867 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005868
Gilles Peskinee56e8782019-04-26 17:34:02 +02005869 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005870 return( PSA_ERROR_INVALID_ARGUMENT );
5871
Gilles Peskinee59236f2018-01-27 23:32:46 +01005872 if( key_type_is_raw_bytes( type ) )
5873 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005874 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005875
5876 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005877 if( status != PSA_SUCCESS )
5878 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005879
5880 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02005881 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
5882 if( status != PSA_SUCCESS )
5883 return status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005884
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005885 status = psa_generate_random( slot->data.key.data,
5886 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005887 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005888 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005889
5890 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005891#if defined(MBEDTLS_DES_C)
5892 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005893 psa_des_set_key_parity( slot->data.key.data,
5894 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005895#endif /* MBEDTLS_DES_C */
5896 }
5897 else
5898
5899#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005900 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005901 {
Steven Cooremana01795d2020-07-24 22:48:15 +02005902 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005903 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005904 int exponent;
5905 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005906 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5907 return( PSA_ERROR_NOT_SUPPORTED );
5908 /* Accept only byte-aligned keys, for the same reasons as
5909 * in psa_import_rsa_key(). */
5910 if( bits % 8 != 0 )
5911 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005912 status = psa_read_rsa_exponent( domain_parameters,
5913 domain_parameters_size,
5914 &exponent );
5915 if( status != PSA_SUCCESS )
5916 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02005917 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5918 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005919 mbedtls_ctr_drbg_random,
5920 &global_data.ctr_drbg,
5921 (unsigned int) bits,
5922 exponent );
5923 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005924 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02005925
5926 /* Make sure to always have an export representation available */
5927 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
5928
Steven Cooreman75b74362020-07-28 14:30:13 +02005929 status = psa_allocate_buffer_to_slot( slot, bytes );
5930 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02005931 {
5932 mbedtls_rsa_free( &rsa );
Steven Cooreman75b74362020-07-28 14:30:13 +02005933 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005934 }
Steven Cooremana01795d2020-07-24 22:48:15 +02005935
5936 status = psa_export_rsa_key( type,
5937 &rsa,
5938 slot->data.key.data,
5939 bytes,
5940 &slot->data.key.bytes );
5941 mbedtls_rsa_free( &rsa );
5942 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02005943 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02005944 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005945 }
5946 else
5947#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5948
5949#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005950 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005951 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01005952 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005953 mbedtls_ecp_group_id grp_id =
5954 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005955 const mbedtls_ecp_curve_info *curve_info =
5956 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02005957 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005958 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005959 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005960 return( PSA_ERROR_NOT_SUPPORTED );
5961 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5962 return( PSA_ERROR_NOT_SUPPORTED );
5963 if( curve_info->bit_size != bits )
5964 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +02005965 mbedtls_ecp_keypair_init( &ecp );
5966 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005967 mbedtls_ctr_drbg_random,
5968 &global_data.ctr_drbg );
5969 if( ret != 0 )
5970 {
Steven Cooremanacda8342020-07-24 23:09:52 +02005971 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005972 return( mbedtls_to_psa_error( ret ) );
5973 }
Steven Cooremanacda8342020-07-24 23:09:52 +02005974
5975
5976 /* Make sure to always have an export representation available */
5977 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02005978 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
5979 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02005980 {
5981 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman75b74362020-07-28 14:30:13 +02005982 return status;
Steven Cooremanacda8342020-07-24 23:09:52 +02005983 }
Steven Cooreman75b74362020-07-28 14:30:13 +02005984
5985 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02005986 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
5987
5988 mbedtls_ecp_keypair_free( &ecp );
5989 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02005990 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02005991 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005992 }
5993 else
5994#endif /* MBEDTLS_ECP_C */
5995
5996 return( PSA_ERROR_NOT_SUPPORTED );
5997
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005998 return( PSA_SUCCESS );
5999}
Darryl Green0c6575a2018-11-07 16:05:30 +00006000
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006001psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006002 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006003{
6004 psa_status_t status;
6005 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006006 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006007
Gilles Peskine0f84d622019-09-12 19:03:13 +02006008 /* Reject any attempt to create a zero-length key so that we don't
6009 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6010 if( psa_get_key_bits( attributes ) == 0 )
6011 return( PSA_ERROR_INVALID_ARGUMENT );
6012
Gilles Peskinedf179142019-07-15 22:02:14 +02006013 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
6014 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006015 if( status != PSA_SUCCESS )
6016 goto exit;
6017
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006018#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6019 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00006020 {
Gilles Peskine11792082019-08-06 18:36:36 +02006021 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
6022 size_t pubkey_length = 0; /* We don't support this feature yet */
6023 if( drv->key_management == NULL ||
6024 drv->key_management->p_generate == NULL )
6025 {
6026 status = PSA_ERROR_NOT_SUPPORTED;
6027 goto exit;
6028 }
6029 status = drv->key_management->p_generate(
6030 psa_get_se_driver_context( driver ),
6031 slot->data.se.slot_number, attributes,
6032 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006033 }
Gilles Peskine11792082019-08-06 18:36:36 +02006034 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006035#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006036 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006037 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02006038 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006039 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006040 }
Gilles Peskine11792082019-08-06 18:36:36 +02006041
6042exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006043 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006044 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006045 if( status != PSA_SUCCESS )
6046 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006047 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006048 *handle = 0;
6049 }
Darryl Green0c6575a2018-11-07 16:05:30 +00006050 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006051}
6052
6053
Gilles Peskinee59236f2018-01-27 23:32:46 +01006054
6055/****************************************************************/
6056/* Module setup */
6057/****************************************************************/
6058
Gilles Peskine5e769522018-11-20 21:59:56 +01006059psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6060 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6061 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6062{
6063 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6064 return( PSA_ERROR_BAD_STATE );
6065 global_data.entropy_init = entropy_init;
6066 global_data.entropy_free = entropy_free;
6067 return( PSA_SUCCESS );
6068}
6069
Gilles Peskinee59236f2018-01-27 23:32:46 +01006070void mbedtls_psa_crypto_free( void )
6071{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006072 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006073 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6074 {
6075 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006076 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006077 }
6078 /* Wipe all remaining data, including configuration.
6079 * In particular, this sets all state indicator to the value
6080 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006081 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006082#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006083 /* Unregister all secure element drivers, so that we restart from
6084 * a pristine state. */
6085 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006086#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006087}
6088
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006089#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6090/** Recover a transaction that was interrupted by a power failure.
6091 *
6092 * This function is called during initialization, before psa_crypto_init()
6093 * returns. If this function returns a failure status, the initialization
6094 * fails.
6095 */
6096static psa_status_t psa_crypto_recover_transaction(
6097 const psa_crypto_transaction_t *transaction )
6098{
6099 switch( transaction->unknown.type )
6100 {
6101 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6102 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006103 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006104 * is implemented.
6105 * https://github.com/ARMmbed/mbed-crypto/issues/218
6106 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006107 default:
6108 /* We found an unsupported transaction in the storage.
6109 * We don't know what state the storage is in. Give up. */
6110 return( PSA_ERROR_STORAGE_FAILURE );
6111 }
6112}
6113#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6114
Gilles Peskinee59236f2018-01-27 23:32:46 +01006115psa_status_t psa_crypto_init( void )
6116{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006117 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006118 const unsigned char drbg_seed[] = "PSA";
6119
Gilles Peskinec6b69072018-11-20 21:42:52 +01006120 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006121 if( global_data.initialized != 0 )
6122 return( PSA_SUCCESS );
6123
Gilles Peskine5e769522018-11-20 21:59:56 +01006124 /* Set default configuration if
6125 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6126 if( global_data.entropy_init == NULL )
6127 global_data.entropy_init = mbedtls_entropy_init;
6128 if( global_data.entropy_free == NULL )
6129 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006130
Gilles Peskinec6b69072018-11-20 21:42:52 +01006131 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006132 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006133#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6134 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6135 /* The PSA entropy injection feature depends on using NV seed as an entropy
6136 * source. Add NV seed as an entropy source for PSA entropy injection. */
6137 mbedtls_entropy_add_source( &global_data.entropy,
6138 mbedtls_nv_seed_poll, NULL,
6139 MBEDTLS_ENTROPY_BLOCK_SIZE,
6140 MBEDTLS_ENTROPY_SOURCE_STRONG );
6141#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006142 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006143 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006144 status = mbedtls_to_psa_error(
6145 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6146 mbedtls_entropy_func,
6147 &global_data.entropy,
6148 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6149 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006150 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006151 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006152
Gilles Peskine66fb1262018-12-10 16:29:04 +01006153 status = psa_initialize_key_slots( );
6154 if( status != PSA_SUCCESS )
6155 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006156
Gilles Peskined9348f22019-10-01 15:22:29 +02006157#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6158 status = psa_init_all_se_drivers( );
6159 if( status != PSA_SUCCESS )
6160 goto exit;
6161#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6162
Gilles Peskine4b734222019-07-24 15:56:31 +02006163#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006164 status = psa_crypto_load_transaction( );
6165 if( status == PSA_SUCCESS )
6166 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006167 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6168 if( status != PSA_SUCCESS )
6169 goto exit;
6170 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006171 }
6172 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6173 {
6174 /* There's no transaction to complete. It's all good. */
6175 status = PSA_SUCCESS;
6176 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006177#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006178
Gilles Peskinec6b69072018-11-20 21:42:52 +01006179 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006180 global_data.initialized = 1;
6181
6182exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006183 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006184 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006185 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006186}
6187
6188#endif /* MBEDTLS_PSA_CRYPTO_C */