blob: 6e9d2596113512c3ee37fdc39698e9b58e59d10a [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 Cooreman7f391872020-07-30 14:57:44 +0200735 size_t curve_size = 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 {
739 /* A public key is represented as:
740 * - The byte 0x04;
741 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
742 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
743 * So its data length is 2m+1 where n is the key size in bits.
744 */
Steven Cooreman7f391872020-07-30 14:57:44 +0200745 if( ( size & 1 ) == 0 )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100746 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman7f391872020-07-30 14:57:44 +0200747 curve_size = size / 2;
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100748 }
749
Steven Cooreman6d839f02020-07-30 11:36:45 +0200750 /* Allocate and initialize a key representation. */
751 ecp = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
752 if( ecp == NULL )
753 return PSA_ERROR_INSUFFICIENT_MEMORY;
Steven Cooremana2371e52020-07-28 14:30:39 +0200754 mbedtls_ecp_keypair_init( ecp );
755
Gilles Peskine4cd32772019-12-02 20:49:42 +0100756 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200757 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
758 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100759 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200760 {
761 status = PSA_ERROR_INVALID_ARGUMENT;
762 goto exit;
763 }
764
Steven Cooremanacda8342020-07-24 23:09:52 +0200765 status = mbedtls_to_psa_error(
766 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
767 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200768 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200769
Steven Cooreman6d839f02020-07-30 11:36:45 +0200770 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200771 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200772 {
773 /* Load the public value. */
774 status = mbedtls_to_psa_error(
775 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman7f391872020-07-30 14:57:44 +0200776 buffer,
777 size ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200778 if( status != PSA_SUCCESS )
779 goto exit;
780
781 /* Check that the point is on the curve. */
782 status = mbedtls_to_psa_error(
783 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
784 if( status != PSA_SUCCESS )
785 goto exit;
786 }
787 else
788 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200789 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200790 status = mbedtls_to_psa_error(
791 mbedtls_ecp_read_key( ecp->grp.id,
792 ecp,
Steven Cooreman7f391872020-07-30 14:57:44 +0200793 buffer,
794 size ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200795
796 if( status != PSA_SUCCESS )
797 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200798 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200799
800 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200801exit:
802 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200803 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200804 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200805 mbedtls_free( ecp );
806 }
807
Steven Cooremanacda8342020-07-24 23:09:52 +0200808 return status;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100809}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000810
Steven Cooremanacda8342020-07-24 23:09:52 +0200811static psa_status_t psa_export_ecp_key( psa_key_type_t type,
812 mbedtls_ecp_keypair *ecp,
813 uint8_t *data,
814 size_t data_size,
815 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200816{
Steven Cooremanacda8342020-07-24 23:09:52 +0200817 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000818
Steven Cooremanacda8342020-07-24 23:09:52 +0200819 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200820 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200821 /* Check whether the public part is loaded */
822 if( mbedtls_ecp_is_zero( &ecp->Q ) )
823 {
824 /* Calculate the public key */
825 status = mbedtls_to_psa_error(
826 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
827 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
828 if( status != PSA_SUCCESS )
829 return status;
830 }
831
832 return( mbedtls_to_psa_error(
833 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
834 MBEDTLS_ECP_PF_UNCOMPRESSED,
835 data_length,
836 data,
837 data_size ) ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200838 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200839 else
840 {
841 if( data_size < PSA_BITS_TO_BYTES(ecp->grp.nbits) )
842 return( PSA_ERROR_BUFFER_TOO_SMALL );
843
844 status = mbedtls_to_psa_error(
845 mbedtls_ecp_write_key( ecp,
846 data,
847 PSA_BITS_TO_BYTES(ecp->grp.nbits) ) );
848 if( status == PSA_SUCCESS )
849 {
850 *data_length = PSA_BITS_TO_BYTES(ecp->grp.nbits);
851 }
852
853 return( status );
854 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200855}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200856
Steven Cooremanacda8342020-07-24 23:09:52 +0200857static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
858 const uint8_t *data,
859 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100860{
Steven Cooremanacda8342020-07-24 23:09:52 +0200861 psa_status_t status;
862 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200863 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100864
Steven Cooremanacda8342020-07-24 23:09:52 +0200865 /* Parse input */
Steven Cooreman7f391872020-07-30 14:57:44 +0200866 status = psa_load_ecp_representation( data,
867 data_length,
868 slot->attr.type,
869 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100870 if( status != PSA_SUCCESS )
871 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100872
Steven Cooremanacda8342020-07-24 23:09:52 +0200873 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200874 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200875 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200876 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200877
Steven Cooremanacda8342020-07-24 23:09:52 +0200878 /* Re-export the data to PSA export format. There is currently no support
879 * for other input formats then the export format, so this is a 1-1
880 * copy operation. */
881 output = mbedtls_calloc( 1, data_length );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100882
Steven Cooremanacda8342020-07-24 23:09:52 +0200883 if( output == NULL )
884 {
885 status = PSA_ERROR_INSUFFICIENT_MEMORY;
886 goto exit;
887 }
888
889 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200890 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200891 output,
892 data_length,
893 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100894
895exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200896 /* Always free the PK object (will also free contained ECP context) */
897 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200898 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200899
900 /* Free the allocated buffer only on error. */
901 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100902 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200903 mbedtls_free( output );
904 slot->data.key.data = NULL;
905 slot->data.key.bytes = 0;
906 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100907 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200908
909 /* On success, store the allocated export-formatted key. */
910 slot->data.key.data = output;
911 slot->data.key.bytes = data_length;
912
913 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100914}
915#endif /* defined(MBEDTLS_ECP_C) */
916
Gilles Peskineb46bef22019-07-30 21:32:04 +0200917/** Return the size of the key in the given slot, in bits.
918 *
919 * \param[in] slot A key slot.
920 *
921 * \return The key size in bits, read from the metadata in the slot.
922 */
923static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
924{
925 return( slot->attr.bits );
926}
927
Steven Cooreman75b74362020-07-28 14:30:13 +0200928/** Try to allocate a buffer to an empty key slot.
929 *
930 * \param[in,out] slot Key slot to attach buffer to.
931 * \param[in] buffer_length Requested size of the buffer.
932 *
933 * \retval #PSA_SUCCESS
934 * The buffer has been successfully allocated.
935 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
936 * Not enough memory was available for allocation.
937 * \retval #PSA_ERROR_ALREADY_EXISTS
938 * Trying to allocate a buffer to a non-empty key slot.
939 */
940static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
941 size_t buffer_length )
942{
943 if( slot->data.key.data != NULL )
944 return PSA_ERROR_ALREADY_EXISTS;
945
946 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
947 if( slot->data.key.data == NULL )
948 return PSA_ERROR_INSUFFICIENT_MEMORY;
949
950 slot->data.key.bytes = buffer_length;
951 return PSA_SUCCESS;
952}
953
Gilles Peskine8e338702019-07-30 20:06:31 +0200954/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100955 * previously. This function assumes that the slot does not contain
956 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100957psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
958 const uint8_t *data,
959 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100960{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200961 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100962
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200963 /* zero-length keys are never supported. */
964 if( data_length == 0 )
965 return( PSA_ERROR_NOT_SUPPORTED );
966
Gilles Peskine8e338702019-07-30 20:06:31 +0200967 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100968 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200969 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200970
Steven Cooreman75b74362020-07-28 14:30:13 +0200971 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
972 if( data_length > SIZE_MAX / 8 )
973 return( PSA_ERROR_NOT_SUPPORTED );
974
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200975 /* Enforce a size limit, and in particular ensure that the bit
976 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200977 if( bit_size > PSA_MAX_KEY_BITS )
978 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200979
980 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200981 if( status != PSA_SUCCESS )
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200982 return status;
983
984 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +0200985 status = psa_allocate_buffer_to_slot( slot, data_length );
986 if( status != PSA_SUCCESS )
987 return status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200988
989 /* copy key into allocated buffer */
990 memcpy(slot->data.key.data, data, data_length);
991
992 /* Write the actual key size to the slot.
993 * psa_start_key_creation() wrote the size declared by the
994 * caller, which may be 0 (meaning unspecified) or wrong. */
995 slot->attr.bits = (psa_key_bits_t) bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100996 }
Steven Cooreman19fd5742020-07-24 23:31:01 +0200997 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
998 {
Gilles Peskinef76aa772018-10-29 19:24:33 +0100999#if defined(MBEDTLS_ECP_C)
Steven Cooreman19fd5742020-07-24 23:31:01 +02001000 status = psa_import_ecp_key( slot,
1001 data, data_length );
1002#else
1003 /* No drivers have been implemented yet, so without mbed TLS backing
1004 * there's no way to do ECP with the current library. */
1005 return( PSA_ERROR_NOT_SUPPORTED );
1006#endif /* defined(MBEDTLS_ECP_C) */
1007 }
1008 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +01001009 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001010#if defined(MBEDTLS_RSA_C)
1011 status = psa_import_rsa_key( slot,
1012 data, data_length );
1013#else
1014 /* No drivers have been implemented yet, so without mbed TLS backing
1015 * there's no way to do RSA with the current library. */
1016 status = PSA_ERROR_NOT_SUPPORTED;
1017#endif /* defined(MBEDTLS_RSA_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001018 }
1019 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001020 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001021 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001022 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001023 }
Darryl Green940d72c2018-07-13 13:18:51 +01001024
Darryl Green06fd18d2018-07-16 11:21:11 +01001025 return( status );
1026}
1027
Gilles Peskinef603c712019-01-19 13:40:11 +01001028/** Calculate the intersection of two algorithm usage policies.
1029 *
1030 * Return 0 (which allows no operation) on incompatibility.
1031 */
1032static psa_algorithm_t psa_key_policy_algorithm_intersection(
1033 psa_algorithm_t alg1,
1034 psa_algorithm_t alg2 )
1035{
Gilles Peskine549ea862019-05-22 11:45:59 +02001036 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001037 if( alg1 == alg2 )
1038 return( alg1 );
1039 /* If the policies are from the same hash-and-sign family, check
1040 * if one is a wildcard. If so the other has the specific algorithm. */
1041 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1042 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1043 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1044 {
1045 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1046 return( alg2 );
1047 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1048 return( alg1 );
1049 }
1050 /* If the policies are incompatible, allow nothing. */
1051 return( 0 );
1052}
1053
Gilles Peskined6f371b2019-05-10 19:33:38 +02001054static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1055 psa_algorithm_t requested_alg )
1056{
Gilles Peskine549ea862019-05-22 11:45:59 +02001057 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001058 if( requested_alg == policy_alg )
1059 return( 1 );
1060 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001061 * and requested_alg is the same hash-and-sign family with any hash,
1062 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001063 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1064 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1065 {
1066 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1067 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1068 }
1069 /* If it isn't permitted, it's forbidden. */
1070 return( 0 );
1071}
1072
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001073/** Test whether a policy permits an algorithm.
1074 *
1075 * The caller must test usage flags separately.
1076 */
1077static int psa_key_policy_permits( const psa_key_policy_t *policy,
1078 psa_algorithm_t alg )
1079{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001080 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1081 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001082}
1083
Gilles Peskinef603c712019-01-19 13:40:11 +01001084/** Restrict a key policy based on a constraint.
1085 *
1086 * \param[in,out] policy The policy to restrict.
1087 * \param[in] constraint The policy constraint to apply.
1088 *
1089 * \retval #PSA_SUCCESS
1090 * \c *policy contains the intersection of the original value of
1091 * \c *policy and \c *constraint.
1092 * \retval #PSA_ERROR_INVALID_ARGUMENT
1093 * \c *policy and \c *constraint are incompatible.
1094 * \c *policy is unchanged.
1095 */
1096static psa_status_t psa_restrict_key_policy(
1097 psa_key_policy_t *policy,
1098 const psa_key_policy_t *constraint )
1099{
1100 psa_algorithm_t intersection_alg =
1101 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001102 psa_algorithm_t intersection_alg2 =
1103 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001104 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1105 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001106 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1107 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001108 policy->usage &= constraint->usage;
1109 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001110 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001111 return( PSA_SUCCESS );
1112}
1113
Darryl Green06fd18d2018-07-16 11:21:11 +01001114/** Retrieve a slot which must contain a key. The key must have allow all the
1115 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
1116 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001117static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001118 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +01001119 psa_key_usage_t usage,
1120 psa_algorithm_t alg )
1121{
1122 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001123 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +01001124
1125 *p_slot = NULL;
1126
Gilles Peskinec5487a82018-12-03 18:08:14 +01001127 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001128 if( status != PSA_SUCCESS )
1129 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001130
1131 /* Enforce that usage policy for the key slot contains all the flags
1132 * required by the usage parameter. There is one exception: public
1133 * keys can always be exported, so we treat public key objects as
1134 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001135 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001136 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +02001137 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +01001138 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001139
1140 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001141 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001142 return( PSA_ERROR_NOT_PERMITTED );
1143
1144 *p_slot = slot;
1145 return( PSA_SUCCESS );
1146}
Darryl Green940d72c2018-07-13 13:18:51 +01001147
Gilles Peskine28f8f302019-07-24 13:30:31 +02001148/** Retrieve a slot which must contain a transparent key.
1149 *
1150 * A transparent key is a key for which the key material is directly
1151 * available, as opposed to a key in a secure element.
1152 *
Gilles Peskine60450a42019-07-25 11:32:45 +02001153 * This is a temporary function to use instead of psa_get_key_from_slot()
1154 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001155 */
1156#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1157static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
1158 psa_key_slot_t **p_slot,
1159 psa_key_usage_t usage,
1160 psa_algorithm_t alg )
1161{
1162 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
1163 if( status != PSA_SUCCESS )
1164 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +02001165 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001166 {
1167 *p_slot = NULL;
1168 return( PSA_ERROR_NOT_SUPPORTED );
1169 }
1170 return( PSA_SUCCESS );
1171}
1172#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1173/* With no secure element support, all keys are transparent. */
1174#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
1175 psa_get_key_from_slot( handle, p_slot, usage, alg )
1176#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1177
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001178/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001179static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001180{
Steven Cooreman75b74362020-07-28 14:30:13 +02001181 /* Check whether key is already clean */
1182 if( slot->data.key.data == NULL )
1183 return PSA_SUCCESS;
1184
Gilles Peskine73167e12019-07-12 23:44:37 +02001185#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1186 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001187 {
1188 /* No key material to clean. */
1189 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001190 else
1191#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +02001192 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +00001193 {
1194 /* No key material to clean. */
1195 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001196 else if( key_type_is_raw_bytes( slot->attr.type ) ||
1197 PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1198 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001199 {
Steven Cooremana01795d2020-07-24 22:48:15 +02001200 mbedtls_free( slot->data.key.data );
1201 slot->data.key.data = NULL;
1202 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001203 }
1204 else
Darryl Green40225ba2018-11-15 14:48:15 +00001205 {
1206 /* Shouldn't happen: the key type is not any type that we
1207 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +02001208 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +00001209 }
1210
1211 return( PSA_SUCCESS );
1212}
1213
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001214/** Completely wipe a slot in memory, including its policy.
1215 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001216psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001217{
1218 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001219 /* Multipart operations may still be using the key. This is safe
1220 * because all multipart operation objects are independent from
1221 * the key slot: if they need to access the key after the setup
1222 * phase, they have a copy of the key. Note that this means that
1223 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001224 /* At this point, key material and other type-specific content has
1225 * been wiped. Clear remaining metadata. We can call memset and not
1226 * zeroize because the metadata is not particularly sensitive. */
1227 memset( slot, 0, sizeof( *slot ) );
1228 return( status );
1229}
1230
Gilles Peskinec5487a82018-12-03 18:08:14 +01001231psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001232{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001233 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001234 psa_status_t status; /* status of the last operation */
1235 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001236#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1237 psa_se_drv_table_entry_t *driver;
1238#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001239
Gilles Peskine1841cf42019-10-08 15:48:25 +02001240 if( handle == 0 )
1241 return( PSA_SUCCESS );
1242
Gilles Peskinec5487a82018-12-03 18:08:14 +01001243 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001244 if( status != PSA_SUCCESS )
1245 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001246
1247#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001248 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001249 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001250 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001251 /* For a key in a secure element, we need to do three things:
1252 * remove the key file in internal storage, destroy the
1253 * key inside the secure element, and update the driver's
1254 * persistent data. Start a transaction that will encompass these
1255 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001256 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001257 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001258 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001259 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001260 status = psa_crypto_save_transaction( );
1261 if( status != PSA_SUCCESS )
1262 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001263 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001264 /* We should still try to destroy the key in the secure
1265 * element and the key metadata in storage. This is especially
1266 * important if the error is that the storage is full.
1267 * But how to do it exactly without risking an inconsistent
1268 * state after a reset?
1269 * https://github.com/ARMmbed/mbed-crypto/issues/215
1270 */
1271 overall_status = status;
1272 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001273 }
1274
Gilles Peskine354f7672019-07-12 23:46:38 +02001275 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001276 if( overall_status == PSA_SUCCESS )
1277 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001278 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001279#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1280
Darryl Greend49a4992018-06-18 17:27:26 +01001281#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001282 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001283 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001284 status = psa_destroy_persistent_key( slot->attr.id );
1285 if( overall_status == PSA_SUCCESS )
1286 overall_status = status;
1287
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001288 /* TODO: other slots may have a copy of the same key. We should
1289 * invalidate them.
1290 * https://github.com/ARMmbed/mbed-crypto/issues/214
1291 */
Darryl Greend49a4992018-06-18 17:27:26 +01001292 }
1293#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001294
Gilles Peskinefc762652019-07-22 19:30:34 +02001295#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1296 if( driver != NULL )
1297 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001298 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001299 if( overall_status == PSA_SUCCESS )
1300 overall_status = status;
1301 status = psa_crypto_stop_transaction( );
1302 if( overall_status == PSA_SUCCESS )
1303 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001304 }
1305#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1306
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001307#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1308exit:
1309#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001310 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001311 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1312 if( overall_status == PSA_SUCCESS )
1313 overall_status = status;
1314 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001315}
1316
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001317void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001318{
Gilles Peskineb699f072019-04-26 16:06:02 +02001319 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001320 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001321}
1322
Gilles Peskineb699f072019-04-26 16:06:02 +02001323psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1324 psa_key_type_t type,
1325 const uint8_t *data,
1326 size_t data_length )
1327{
1328 uint8_t *copy = NULL;
1329
1330 if( data_length != 0 )
1331 {
1332 copy = mbedtls_calloc( 1, data_length );
1333 if( copy == NULL )
1334 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1335 memcpy( copy, data, data_length );
1336 }
1337 /* After this point, this function is guaranteed to succeed, so it
1338 * can start modifying `*attributes`. */
1339
1340 if( attributes->domain_parameters != NULL )
1341 {
1342 mbedtls_free( attributes->domain_parameters );
1343 attributes->domain_parameters = NULL;
1344 attributes->domain_parameters_size = 0;
1345 }
1346
1347 attributes->domain_parameters = copy;
1348 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001349 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001350 return( PSA_SUCCESS );
1351}
1352
1353psa_status_t psa_get_key_domain_parameters(
1354 const psa_key_attributes_t *attributes,
1355 uint8_t *data, size_t data_size, size_t *data_length )
1356{
1357 if( attributes->domain_parameters_size > data_size )
1358 return( PSA_ERROR_BUFFER_TOO_SMALL );
1359 *data_length = attributes->domain_parameters_size;
1360 if( attributes->domain_parameters_size != 0 )
1361 memcpy( data, attributes->domain_parameters,
1362 attributes->domain_parameters_size );
1363 return( PSA_SUCCESS );
1364}
1365
1366#if defined(MBEDTLS_RSA_C)
1367static psa_status_t psa_get_rsa_public_exponent(
1368 const mbedtls_rsa_context *rsa,
1369 psa_key_attributes_t *attributes )
1370{
1371 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001372 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001373 uint8_t *buffer = NULL;
1374 size_t buflen;
1375 mbedtls_mpi_init( &mpi );
1376
1377 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1378 if( ret != 0 )
1379 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001380 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1381 {
1382 /* It's the default value, which is reported as an empty string,
1383 * so there's nothing to do. */
1384 goto exit;
1385 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001386
1387 buflen = mbedtls_mpi_size( &mpi );
1388 buffer = mbedtls_calloc( 1, buflen );
1389 if( buffer == NULL )
1390 {
1391 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1392 goto exit;
1393 }
1394 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1395 if( ret != 0 )
1396 goto exit;
1397 attributes->domain_parameters = buffer;
1398 attributes->domain_parameters_size = buflen;
1399
1400exit:
1401 mbedtls_mpi_free( &mpi );
1402 if( ret != 0 )
1403 mbedtls_free( buffer );
1404 return( mbedtls_to_psa_error( ret ) );
1405}
1406#endif /* MBEDTLS_RSA_C */
1407
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001408/** Retrieve all the publicly-accessible attributes of a key.
1409 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001410psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1411 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001412{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001413 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001414 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001415
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001416 psa_reset_key_attributes( attributes );
1417
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001418 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001419 if( status != PSA_SUCCESS )
1420 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001421
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001422 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001423 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1424 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1425
1426#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1427 if( psa_key_slot_is_external( slot ) )
1428 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1429#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001430
Gilles Peskine8e338702019-07-30 20:06:31 +02001431 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001432 {
1433#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001434 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001435 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001436#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001437 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001438 * is not yet implemented.
1439 * https://github.com/ARMmbed/mbed-crypto/issues/216
1440 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001441 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001442 break;
1443#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001444 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001445 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001446
Steven Cooreman7f391872020-07-30 14:57:44 +02001447 status = psa_load_rsa_representation( slot->data.key.data,
1448 slot->data.key.bytes,
1449 slot->attr.type,
1450 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001451 if( status != PSA_SUCCESS )
1452 break;
1453
Steven Cooremana2371e52020-07-28 14:30:39 +02001454 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001455 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001456 mbedtls_rsa_free( rsa );
1457 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001458 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001459 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001460#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001461 default:
1462 /* Nothing else to do. */
1463 break;
1464 }
1465
1466 if( status != PSA_SUCCESS )
1467 psa_reset_key_attributes( attributes );
1468 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001469}
1470
Gilles Peskinec8000c02019-08-02 20:15:51 +02001471#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1472psa_status_t psa_get_key_slot_number(
1473 const psa_key_attributes_t *attributes,
1474 psa_key_slot_number_t *slot_number )
1475{
1476 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1477 {
1478 *slot_number = attributes->slot_number;
1479 return( PSA_SUCCESS );
1480 }
1481 else
1482 return( PSA_ERROR_INVALID_ARGUMENT );
1483}
1484#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1485
Steven Cooremana01795d2020-07-24 22:48:15 +02001486static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1487 uint8_t *data,
1488 size_t data_size,
1489 size_t *data_length )
1490{
1491 if( slot->data.key.bytes > data_size )
1492 return( PSA_ERROR_BUFFER_TOO_SMALL );
1493 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1494 memset( data + slot->data.key.bytes, 0,
1495 data_size - slot->data.key.bytes );
1496 *data_length = slot->data.key.bytes;
1497 return( PSA_SUCCESS );
1498}
1499
Gilles Peskinef603c712019-01-19 13:40:11 +01001500static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1501 uint8_t *data,
1502 size_t data_size,
1503 size_t *data_length,
1504 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001505{
Gilles Peskine5d309672019-07-12 23:47:28 +02001506#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1507 const psa_drv_se_t *drv;
1508 psa_drv_se_context_t *drv_context;
1509#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1510
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001511 *data_length = 0;
1512
Gilles Peskine8e338702019-07-30 20:06:31 +02001513 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001514 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001515
Gilles Peskinef9168942019-09-12 19:20:29 +02001516 /* Reject a zero-length output buffer now, since this can never be a
1517 * valid key representation. This way we know that data must be a valid
1518 * pointer and we can do things like memset(data, ..., data_size). */
1519 if( data_size == 0 )
1520 return( PSA_ERROR_BUFFER_TOO_SMALL );
1521
Gilles Peskine5d309672019-07-12 23:47:28 +02001522#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001523 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001524 {
1525 psa_drv_se_export_key_t method;
1526 if( drv->key_management == NULL )
1527 return( PSA_ERROR_NOT_SUPPORTED );
1528 method = ( export_public_key ?
1529 drv->key_management->p_export_public :
1530 drv->key_management->p_export );
1531 if( method == NULL )
1532 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001533 return( method( drv_context,
1534 slot->data.se.slot_number,
1535 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001536 }
1537#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1538
Gilles Peskine8e338702019-07-30 20:06:31 +02001539 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001540 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001541 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001542 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001543 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1544 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001545 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001546 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001547 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001548 /* Exporting public -> public */
1549 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1550 }
1551 else if( !export_public_key )
1552 {
1553 /* Exporting private -> private */
1554 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1555 }
1556 /* Need to export the public part of a private key,
1557 * so conversion is needed */
1558 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1559 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001560#if defined(MBEDTLS_RSA_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001561 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001562 psa_status_t status = psa_load_rsa_representation(
1563 slot->data.key.data,
1564 slot->data.key.bytes,
1565 slot->attr.type,
1566 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001567 if( status != PSA_SUCCESS )
1568 return status;
Steven Cooremana01795d2020-07-24 22:48:15 +02001569
Steven Cooreman560c28a2020-07-24 23:20:24 +02001570 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001571 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001572 data,
1573 data_size,
1574 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001575
Steven Cooremana2371e52020-07-28 14:30:39 +02001576 mbedtls_rsa_free( rsa );
1577 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001578
Steven Cooreman560c28a2020-07-24 23:20:24 +02001579 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001580#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001581 /* We don't know how to convert a private RSA key to public. */
1582 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001583#endif
Gilles Peskine969ac722018-01-28 18:16:59 +01001584 }
1585 else
Moran Pekera998bc62018-04-16 18:16:20 +03001586 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001587#if defined(MBEDTLS_ECP_C)
Steven Cooremana2371e52020-07-28 14:30:39 +02001588 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02001589 psa_status_t status = psa_load_ecp_representation(
1590 slot->data.key.data,
1591 slot->data.key.bytes,
1592 slot->attr.type,
1593 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001594 if( status != PSA_SUCCESS )
1595 return status;
1596
1597 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1598 PSA_KEY_TYPE_ECC_GET_FAMILY(
1599 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001600 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001601 data,
1602 data_size,
1603 data_length );
1604
Steven Cooremana2371e52020-07-28 14:30:39 +02001605 mbedtls_ecp_keypair_free( ecp );
1606 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001607 return( status );
1608#else
1609 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001610 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001611#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001612 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001613 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001614 else
1615 {
1616 /* This shouldn't happen in the reference implementation, but
1617 it is valid for a special-purpose implementation to omit
1618 support for exporting certain key types. */
1619 return( PSA_ERROR_NOT_SUPPORTED );
1620 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001621}
1622
Gilles Peskinec5487a82018-12-03 18:08:14 +01001623psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001624 uint8_t *data,
1625 size_t data_size,
1626 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001627{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001628 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001629 psa_status_t status;
1630
1631 /* Set the key to empty now, so that even when there are errors, we always
1632 * set data_length to a value between 0 and data_size. On error, setting
1633 * the key to empty is a good choice because an empty key representation is
1634 * unlikely to be accepted anywhere. */
1635 *data_length = 0;
1636
1637 /* Export requires the EXPORT flag. There is an exception for public keys,
1638 * which don't require any flag, but psa_get_key_from_slot takes
1639 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001640 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001641 if( status != PSA_SUCCESS )
1642 return( status );
1643 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001644 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001645}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001646
Gilles Peskinec5487a82018-12-03 18:08:14 +01001647psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001648 uint8_t *data,
1649 size_t data_size,
1650 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001651{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001652 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001653 psa_status_t status;
1654
1655 /* Set the key to empty now, so that even when there are errors, we always
1656 * set data_length to a value between 0 and data_size. On error, setting
1657 * the key to empty is a good choice because an empty key representation is
1658 * unlikely to be accepted anywhere. */
1659 *data_length = 0;
1660
1661 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001662 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001663 if( status != PSA_SUCCESS )
1664 return( status );
1665 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001666 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001667}
1668
Gilles Peskine91e8c332019-08-02 19:19:39 +02001669#if defined(static_assert)
1670static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1671 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001672static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001673 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001674static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001675 "One or more key attribute flag is listed as both internal-only and external-only" );
1676#endif
1677
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001678/** Validate that a key policy is internally well-formed.
1679 *
1680 * This function only rejects invalid policies. It does not validate the
1681 * consistency of the policy with respect to other attributes of the key
1682 * such as the key type.
1683 */
1684static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001685{
1686 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001687 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001688 PSA_KEY_USAGE_ENCRYPT |
1689 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001690 PSA_KEY_USAGE_SIGN_HASH |
1691 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001692 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1693 return( PSA_ERROR_INVALID_ARGUMENT );
1694
Gilles Peskine4747d192019-04-17 15:05:45 +02001695 return( PSA_SUCCESS );
1696}
1697
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001698/** Validate the internal consistency of key attributes.
1699 *
1700 * This function only rejects invalid attribute values. If does not
1701 * validate the consistency of the attributes with any key data that may
1702 * be involved in the creation of the key.
1703 *
1704 * Call this function early in the key creation process.
1705 *
1706 * \param[in] attributes Key attributes for the new key.
1707 * \param[out] p_drv On any return, the driver for the key, if any.
1708 * NULL for a transparent key.
1709 *
1710 */
1711static psa_status_t psa_validate_key_attributes(
1712 const psa_key_attributes_t *attributes,
1713 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001714{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001715 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001716
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001717 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1718 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001719 if( status != PSA_SUCCESS )
1720 return( status );
1721
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001722 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1723 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001724 if( status != PSA_SUCCESS )
1725 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001726
1727 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001728 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001729 return( status );
1730
1731 /* Refuse to create overly large keys.
1732 * Note that this doesn't trigger on import if the attributes don't
1733 * explicitly specify a size (so psa_get_key_bits returns 0), so
1734 * psa_import_key() needs its own checks. */
1735 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1736 return( PSA_ERROR_NOT_SUPPORTED );
1737
Gilles Peskine91e8c332019-08-02 19:19:39 +02001738 /* Reject invalid flags. These should not be reachable through the API. */
1739 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1740 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1741 return( PSA_ERROR_INVALID_ARGUMENT );
1742
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001743 return( PSA_SUCCESS );
1744}
1745
Gilles Peskine4747d192019-04-17 15:05:45 +02001746/** Prepare a key slot to receive key material.
1747 *
1748 * This function allocates a key slot and sets its metadata.
1749 *
1750 * If this function fails, call psa_fail_key_creation().
1751 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001752 * This function is intended to be used as follows:
1753 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1754 * it with the specified attributes, and assign it a handle.
1755 * -# Populate the slot with the key material.
1756 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1757 * In case of failure at any step, stop the sequence and call
1758 * psa_fail_key_creation().
1759 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001760 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001761 * \param[in] attributes Key attributes for the new key.
1762 * \param[out] handle On success, a handle for the allocated slot.
1763 * \param[out] p_slot On success, a pointer to the prepared slot.
1764 * \param[out] p_drv On any return, the driver for the key, if any.
1765 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001766 *
1767 * \retval #PSA_SUCCESS
1768 * The key slot is ready to receive key material.
1769 * \return If this function fails, the key slot is an invalid state.
1770 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001771 */
1772static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001773 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001774 const psa_key_attributes_t *attributes,
1775 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001776 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001777 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001778{
1779 psa_status_t status;
1780 psa_key_slot_t *slot;
1781
Gilles Peskinedf179142019-07-15 22:02:14 +02001782 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001783 *p_drv = NULL;
1784
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001785 status = psa_validate_key_attributes( attributes, p_drv );
1786 if( status != PSA_SUCCESS )
1787 return( status );
1788
Gilles Peskineedbed562019-08-07 18:19:59 +02001789 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001790 if( status != PSA_SUCCESS )
1791 return( status );
1792 slot = *p_slot;
1793
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001794 /* We're storing the declared bit-size of the key. It's up to each
1795 * creation mechanism to verify that this information is correct.
1796 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001797 * an input (generate, device) but not for those where the bit-size
1798 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001799
1800 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001801
Gilles Peskine91e8c332019-08-02 19:19:39 +02001802 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001803 * external-only flags, query `attributes`. Thanks to the check
1804 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001805 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001806 * may have set. */
1807 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001808
Gilles Peskinecbaff462019-07-12 23:46:04 +02001809#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001810 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001811 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001812 * create the key file in internal storage, create the
1813 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001814 * persistent data. This is done by starting a transaction that will
1815 * encompass these three actions.
1816 * For registering a volatile key, we just need to find an appropriate
1817 * slot number inside the SE. Since the key is designated volatile, creating
1818 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001819 /* The first thing to do is to find a slot number for the new key.
1820 * We save the slot number in persistent storage as part of the
1821 * transaction data. It will be needed to recover if the power
1822 * fails during the key creation process, to clean up on the secure
1823 * element side after restarting. Obtaining a slot number from the
1824 * secure element driver updates its persistent state, but we do not yet
1825 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001826 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001827 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001828 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001829 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001830 &slot->data.se.slot_number );
1831 if( status != PSA_SUCCESS )
1832 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001833
1834 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001835 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001836 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1837 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1838 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1839 psa_crypto_transaction.key.id = slot->attr.id;
1840 status = psa_crypto_save_transaction( );
1841 if( status != PSA_SUCCESS )
1842 {
1843 (void) psa_crypto_stop_transaction( );
1844 return( status );
1845 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001846 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001847 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001848
1849 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1850 {
1851 /* Key registration only makes sense with a secure element. */
1852 return( PSA_ERROR_INVALID_ARGUMENT );
1853 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001854#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1855
Darryl Green0c6575a2018-11-07 16:05:30 +00001856 return( status );
1857}
Gilles Peskine4747d192019-04-17 15:05:45 +02001858
1859/** Finalize the creation of a key once its key material has been set.
1860 *
1861 * This entails writing the key to persistent storage.
1862 *
1863 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001864 * See the documentation of psa_start_key_creation() for the intended use
1865 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001866 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001867 * \param[in,out] slot Pointer to the slot with key material.
1868 * \param[in] driver The secure element driver for the key,
1869 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001870 *
1871 * \retval #PSA_SUCCESS
1872 * The key was successfully created. The handle is now valid.
1873 * \return If this function fails, the key slot is an invalid state.
1874 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001875 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001876static psa_status_t psa_finish_key_creation(
1877 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001878 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001879{
1880 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001881 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001882 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001883
1884#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001885 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001886 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001887#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1888 if( driver != NULL )
1889 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001890 psa_se_key_data_storage_t data;
1891#if defined(static_assert)
1892 static_assert( sizeof( slot->data.se.slot_number ) ==
1893 sizeof( data.slot_number ),
1894 "Slot number size does not match psa_se_key_data_storage_t" );
1895 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1896 "Bit-size size does not match psa_se_key_data_storage_t" );
1897#endif
1898 memcpy( &data.slot_number, &slot->data.se.slot_number,
1899 sizeof( slot->data.se.slot_number ) );
1900 memcpy( &data.bits, &slot->attr.bits,
1901 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001902 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001903 (uint8_t*) &data,
1904 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001905 }
1906 else
1907#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1908 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001909 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001910 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001911 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001912 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1913 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001914 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001915 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1916 status = psa_internal_export_key( slot,
1917 buffer, buffer_size, &length,
1918 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001919 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001920 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001921 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001922
Gilles Peskinef9168942019-09-12 19:20:29 +02001923 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001924 mbedtls_free( buffer );
1925 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001926 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001927#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1928
Gilles Peskinecbaff462019-07-12 23:46:04 +02001929#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001930 /* Finish the transaction for a key creation. This does not
1931 * happen when registering an existing key. Detect this case
1932 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001933 * creation of a persistent key in a secure element requires a transaction,
1934 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001935 if( driver != NULL &&
1936 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001937 {
1938 status = psa_save_se_persistent_data( driver );
1939 if( status != PSA_SUCCESS )
1940 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001941 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001942 return( status );
1943 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001944 status = psa_crypto_stop_transaction( );
1945 if( status != PSA_SUCCESS )
1946 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001947 }
1948#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1949
Gilles Peskine4747d192019-04-17 15:05:45 +02001950 return( status );
1951}
1952
1953/** Abort the creation of a key.
1954 *
1955 * You may call this function after calling psa_start_key_creation(),
1956 * or after psa_finish_key_creation() fails. In other circumstances, this
1957 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001958 * See the documentation of psa_start_key_creation() for the intended use
1959 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001960 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001961 * \param[in,out] slot Pointer to the slot with key material.
1962 * \param[in] driver The secure element driver for the key,
1963 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001964 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001965static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001966 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001967{
Gilles Peskine011e4282019-06-26 18:34:38 +02001968 (void) driver;
1969
Gilles Peskine4747d192019-04-17 15:05:45 +02001970 if( slot == NULL )
1971 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001972
1973#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001974 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001975 * element, and the failure happened later (when saving metadata
1976 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001977 * element.
1978 * https://github.com/ARMmbed/mbed-crypto/issues/217
1979 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001980
Gilles Peskined7729582019-08-05 15:55:54 +02001981 /* Abort the ongoing transaction if any (there may not be one if
1982 * the creation process failed before starting one, or if the
1983 * key creation is a registration of a key in a secure element).
1984 * Earlier functions must already have done what it takes to undo any
1985 * partial creation. All that's left is to update the transaction data
1986 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001987 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001988#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1989
Gilles Peskine4747d192019-04-17 15:05:45 +02001990 psa_wipe_key_slot( slot );
1991}
1992
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001993/** Validate optional attributes during key creation.
1994 *
1995 * Some key attributes are optional during key creation. If they are
1996 * specified in the attributes structure, check that they are consistent
1997 * with the data in the slot.
1998 *
1999 * This function should be called near the end of key creation, after
2000 * the slot in memory is fully populated but before saving persistent data.
2001 */
2002static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002003 const psa_key_slot_t *slot,
2004 const psa_key_attributes_t *attributes )
2005{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002006 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002007 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002008 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002009 return( PSA_ERROR_INVALID_ARGUMENT );
2010 }
2011
2012 if( attributes->domain_parameters_size != 0 )
2013 {
2014#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02002015 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002016 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002017 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002018 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002019 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002020
Steven Cooreman7f391872020-07-30 14:57:44 +02002021 psa_status_t status = psa_load_rsa_representation(
2022 slot->data.key.data,
2023 slot->data.key.bytes,
2024 slot->attr.type,
2025 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002026 if( status != PSA_SUCCESS )
2027 return status;
Steven Cooreman75b74362020-07-28 14:30:13 +02002028
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002029 mbedtls_mpi_init( &actual );
2030 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002031 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002032 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002033 mbedtls_rsa_free( rsa );
2034 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002035 if( ret != 0 )
2036 goto rsa_exit;
2037 ret = mbedtls_mpi_read_binary( &required,
2038 attributes->domain_parameters,
2039 attributes->domain_parameters_size );
2040 if( ret != 0 )
2041 goto rsa_exit;
2042 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2043 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2044 rsa_exit:
2045 mbedtls_mpi_free( &actual );
2046 mbedtls_mpi_free( &required );
2047 if( ret != 0)
2048 return( mbedtls_to_psa_error( ret ) );
2049 }
2050 else
2051#endif
2052 {
2053 return( PSA_ERROR_INVALID_ARGUMENT );
2054 }
2055 }
2056
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002057 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002058 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002059 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002060 return( PSA_ERROR_INVALID_ARGUMENT );
2061 }
2062
2063 return( PSA_SUCCESS );
2064}
2065
Gilles Peskine4747d192019-04-17 15:05:45 +02002066psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002067 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002068 size_t data_length,
2069 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02002070{
2071 psa_status_t status;
2072 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002073 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002074
Gilles Peskine0f84d622019-09-12 19:03:13 +02002075 /* Reject zero-length symmetric keys (including raw data key objects).
2076 * This also rejects any key which might be encoded as an empty string,
2077 * which is never valid. */
2078 if( data_length == 0 )
2079 return( PSA_ERROR_INVALID_ARGUMENT );
2080
Gilles Peskinedf179142019-07-15 22:02:14 +02002081 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
2082 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002083 if( status != PSA_SUCCESS )
2084 goto exit;
2085
Gilles Peskine5d309672019-07-12 23:47:28 +02002086#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2087 if( driver != NULL )
2088 {
2089 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002090 /* The driver should set the number of key bits, however in
2091 * case it doesn't, we initialize bits to an invalid value. */
2092 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002093 if( drv->key_management == NULL ||
2094 drv->key_management->p_import == NULL )
2095 {
2096 status = PSA_ERROR_NOT_SUPPORTED;
2097 goto exit;
2098 }
2099 status = drv->key_management->p_import(
2100 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002101 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002102 &bits );
2103 if( status != PSA_SUCCESS )
2104 goto exit;
2105 if( bits > PSA_MAX_KEY_BITS )
2106 {
2107 status = PSA_ERROR_NOT_SUPPORTED;
2108 goto exit;
2109 }
2110 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002111 }
2112 else
2113#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2114 {
2115 status = psa_import_key_into_slot( slot, data, data_length );
2116 if( status != PSA_SUCCESS )
2117 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002118 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002119 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002120 if( status != PSA_SUCCESS )
2121 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002122
Gilles Peskine011e4282019-06-26 18:34:38 +02002123 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002124exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002125 if( status != PSA_SUCCESS )
2126 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002127 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02002128 *handle = 0;
2129 }
2130 return( status );
2131}
2132
Gilles Peskined7729582019-08-05 15:55:54 +02002133#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2134psa_status_t mbedtls_psa_register_se_key(
2135 const psa_key_attributes_t *attributes )
2136{
2137 psa_status_t status;
2138 psa_key_slot_t *slot = NULL;
2139 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02002140 psa_key_handle_t handle = 0;
2141
2142 /* Leaving attributes unspecified is not currently supported.
2143 * It could make sense to query the key type and size from the
2144 * secure element, but not all secure elements support this
2145 * and the driver HAL doesn't currently support it. */
2146 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2147 return( PSA_ERROR_NOT_SUPPORTED );
2148 if( psa_get_key_bits( attributes ) == 0 )
2149 return( PSA_ERROR_NOT_SUPPORTED );
2150
2151 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
2152 &handle, &slot, &driver );
2153 if( status != PSA_SUCCESS )
2154 goto exit;
2155
Gilles Peskined7729582019-08-05 15:55:54 +02002156 status = psa_finish_key_creation( slot, driver );
2157
2158exit:
2159 if( status != PSA_SUCCESS )
2160 {
2161 psa_fail_key_creation( slot, driver );
2162 }
2163 /* Registration doesn't keep the key in RAM. */
2164 psa_close_key( handle );
2165 return( status );
2166}
2167#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2168
Gilles Peskinef603c712019-01-19 13:40:11 +01002169static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002170 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002171{
2172 psa_status_t status;
2173 uint8_t *buffer = NULL;
2174 size_t buffer_size = 0;
2175 size_t length;
2176
Gilles Peskine8e338702019-07-30 20:06:31 +02002177 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002178 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01002179 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02002180 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01002181 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01002182 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
2183 if( status != PSA_SUCCESS )
2184 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02002185 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002186 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01002187
2188exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02002189 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01002190 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01002191 return( status );
2192}
2193
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002194psa_status_t psa_copy_key( psa_key_handle_t source_handle,
2195 const psa_key_attributes_t *specified_attributes,
2196 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01002197{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002198 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01002199 psa_key_slot_t *source_slot = NULL;
2200 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002201 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002202 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002203
Gilles Peskine28f8f302019-07-24 13:30:31 +02002204 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02002205 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002206 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002207 goto exit;
2208
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002209 status = psa_validate_optional_attributes( source_slot,
2210 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002211 if( status != PSA_SUCCESS )
2212 goto exit;
2213
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002214 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002215 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002216 if( status != PSA_SUCCESS )
2217 goto exit;
2218
Gilles Peskinedf179142019-07-15 22:02:14 +02002219 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
2220 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002221 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002222 if( status != PSA_SUCCESS )
2223 goto exit;
2224
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002225#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2226 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002227 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002228 /* Copying to a secure element is not implemented yet. */
2229 status = PSA_ERROR_NOT_SUPPORTED;
2230 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002231 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002232#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002233
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002234 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002235 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002236 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002237
Gilles Peskine011e4282019-06-26 18:34:38 +02002238 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002239exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002240 if( status != PSA_SUCCESS )
2241 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002242 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002243 *target_handle = 0;
2244 }
2245 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002246}
2247
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002248
2249
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002250/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002251/* Message digests */
2252/****************************************************************/
2253
Gilles Peskineb16841e2019-10-10 20:36:12 +02002254#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002255static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002256{
2257 switch( alg )
2258 {
2259#if defined(MBEDTLS_MD2_C)
2260 case PSA_ALG_MD2:
2261 return( &mbedtls_md2_info );
2262#endif
2263#if defined(MBEDTLS_MD4_C)
2264 case PSA_ALG_MD4:
2265 return( &mbedtls_md4_info );
2266#endif
2267#if defined(MBEDTLS_MD5_C)
2268 case PSA_ALG_MD5:
2269 return( &mbedtls_md5_info );
2270#endif
2271#if defined(MBEDTLS_RIPEMD160_C)
2272 case PSA_ALG_RIPEMD160:
2273 return( &mbedtls_ripemd160_info );
2274#endif
2275#if defined(MBEDTLS_SHA1_C)
2276 case PSA_ALG_SHA_1:
2277 return( &mbedtls_sha1_info );
2278#endif
2279#if defined(MBEDTLS_SHA256_C)
2280 case PSA_ALG_SHA_224:
2281 return( &mbedtls_sha224_info );
2282 case PSA_ALG_SHA_256:
2283 return( &mbedtls_sha256_info );
2284#endif
2285#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002286#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002287 case PSA_ALG_SHA_384:
2288 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002289#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002290 case PSA_ALG_SHA_512:
2291 return( &mbedtls_sha512_info );
2292#endif
2293 default:
2294 return( NULL );
2295 }
2296}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002297#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002298
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002299psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2300{
2301 switch( operation->alg )
2302 {
Gilles Peskine81736312018-06-26 15:04:31 +02002303 case 0:
2304 /* The object has (apparently) been initialized but it is not
2305 * in use. It's ok to call abort on such an object, and there's
2306 * nothing to do. */
2307 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002308#if defined(MBEDTLS_MD2_C)
2309 case PSA_ALG_MD2:
2310 mbedtls_md2_free( &operation->ctx.md2 );
2311 break;
2312#endif
2313#if defined(MBEDTLS_MD4_C)
2314 case PSA_ALG_MD4:
2315 mbedtls_md4_free( &operation->ctx.md4 );
2316 break;
2317#endif
2318#if defined(MBEDTLS_MD5_C)
2319 case PSA_ALG_MD5:
2320 mbedtls_md5_free( &operation->ctx.md5 );
2321 break;
2322#endif
2323#if defined(MBEDTLS_RIPEMD160_C)
2324 case PSA_ALG_RIPEMD160:
2325 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2326 break;
2327#endif
2328#if defined(MBEDTLS_SHA1_C)
2329 case PSA_ALG_SHA_1:
2330 mbedtls_sha1_free( &operation->ctx.sha1 );
2331 break;
2332#endif
2333#if defined(MBEDTLS_SHA256_C)
2334 case PSA_ALG_SHA_224:
2335 case PSA_ALG_SHA_256:
2336 mbedtls_sha256_free( &operation->ctx.sha256 );
2337 break;
2338#endif
2339#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002340#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002341 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002342#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002343 case PSA_ALG_SHA_512:
2344 mbedtls_sha512_free( &operation->ctx.sha512 );
2345 break;
2346#endif
2347 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002348 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002349 }
2350 operation->alg = 0;
2351 return( PSA_SUCCESS );
2352}
2353
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002354psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002355 psa_algorithm_t alg )
2356{
Janos Follath24eed8d2019-11-22 13:21:35 +00002357 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002358
2359 /* A context must be freshly initialized before it can be set up. */
2360 if( operation->alg != 0 )
2361 {
2362 return( PSA_ERROR_BAD_STATE );
2363 }
2364
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002365 switch( alg )
2366 {
2367#if defined(MBEDTLS_MD2_C)
2368 case PSA_ALG_MD2:
2369 mbedtls_md2_init( &operation->ctx.md2 );
2370 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2371 break;
2372#endif
2373#if defined(MBEDTLS_MD4_C)
2374 case PSA_ALG_MD4:
2375 mbedtls_md4_init( &operation->ctx.md4 );
2376 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2377 break;
2378#endif
2379#if defined(MBEDTLS_MD5_C)
2380 case PSA_ALG_MD5:
2381 mbedtls_md5_init( &operation->ctx.md5 );
2382 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2383 break;
2384#endif
2385#if defined(MBEDTLS_RIPEMD160_C)
2386 case PSA_ALG_RIPEMD160:
2387 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2388 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2389 break;
2390#endif
2391#if defined(MBEDTLS_SHA1_C)
2392 case PSA_ALG_SHA_1:
2393 mbedtls_sha1_init( &operation->ctx.sha1 );
2394 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2395 break;
2396#endif
2397#if defined(MBEDTLS_SHA256_C)
2398 case PSA_ALG_SHA_224:
2399 mbedtls_sha256_init( &operation->ctx.sha256 );
2400 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2401 break;
2402 case PSA_ALG_SHA_256:
2403 mbedtls_sha256_init( &operation->ctx.sha256 );
2404 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2405 break;
2406#endif
2407#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002408#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002409 case PSA_ALG_SHA_384:
2410 mbedtls_sha512_init( &operation->ctx.sha512 );
2411 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2412 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002413#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002414 case PSA_ALG_SHA_512:
2415 mbedtls_sha512_init( &operation->ctx.sha512 );
2416 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2417 break;
2418#endif
2419 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002420 return( PSA_ALG_IS_HASH( alg ) ?
2421 PSA_ERROR_NOT_SUPPORTED :
2422 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002423 }
2424 if( ret == 0 )
2425 operation->alg = alg;
2426 else
2427 psa_hash_abort( operation );
2428 return( mbedtls_to_psa_error( ret ) );
2429}
2430
2431psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2432 const uint8_t *input,
2433 size_t input_length )
2434{
Janos Follath24eed8d2019-11-22 13:21:35 +00002435 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002436
2437 /* Don't require hash implementations to behave correctly on a
2438 * zero-length input, which may have an invalid pointer. */
2439 if( input_length == 0 )
2440 return( PSA_SUCCESS );
2441
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002442 switch( operation->alg )
2443 {
2444#if defined(MBEDTLS_MD2_C)
2445 case PSA_ALG_MD2:
2446 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2447 input, input_length );
2448 break;
2449#endif
2450#if defined(MBEDTLS_MD4_C)
2451 case PSA_ALG_MD4:
2452 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2453 input, input_length );
2454 break;
2455#endif
2456#if defined(MBEDTLS_MD5_C)
2457 case PSA_ALG_MD5:
2458 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2459 input, input_length );
2460 break;
2461#endif
2462#if defined(MBEDTLS_RIPEMD160_C)
2463 case PSA_ALG_RIPEMD160:
2464 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2465 input, input_length );
2466 break;
2467#endif
2468#if defined(MBEDTLS_SHA1_C)
2469 case PSA_ALG_SHA_1:
2470 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2471 input, input_length );
2472 break;
2473#endif
2474#if defined(MBEDTLS_SHA256_C)
2475 case PSA_ALG_SHA_224:
2476 case PSA_ALG_SHA_256:
2477 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2478 input, input_length );
2479 break;
2480#endif
2481#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002482#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002483 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002484#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002485 case PSA_ALG_SHA_512:
2486 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2487 input, input_length );
2488 break;
2489#endif
2490 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002491 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002492 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002493
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002494 if( ret != 0 )
2495 psa_hash_abort( operation );
2496 return( mbedtls_to_psa_error( ret ) );
2497}
2498
2499psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2500 uint8_t *hash,
2501 size_t hash_size,
2502 size_t *hash_length )
2503{
itayzafrir40835d42018-08-02 13:14:17 +03002504 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002505 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002506 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002507
2508 /* Fill the output buffer with something that isn't a valid hash
2509 * (barring an attack on the hash and deliberately-crafted input),
2510 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002511 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002512 /* If hash_size is 0 then hash may be NULL and then the
2513 * call to memset would have undefined behavior. */
2514 if( hash_size != 0 )
2515 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002516
2517 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002518 {
2519 status = PSA_ERROR_BUFFER_TOO_SMALL;
2520 goto exit;
2521 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002522
2523 switch( operation->alg )
2524 {
2525#if defined(MBEDTLS_MD2_C)
2526 case PSA_ALG_MD2:
2527 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2528 break;
2529#endif
2530#if defined(MBEDTLS_MD4_C)
2531 case PSA_ALG_MD4:
2532 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2533 break;
2534#endif
2535#if defined(MBEDTLS_MD5_C)
2536 case PSA_ALG_MD5:
2537 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2538 break;
2539#endif
2540#if defined(MBEDTLS_RIPEMD160_C)
2541 case PSA_ALG_RIPEMD160:
2542 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2543 break;
2544#endif
2545#if defined(MBEDTLS_SHA1_C)
2546 case PSA_ALG_SHA_1:
2547 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2548 break;
2549#endif
2550#if defined(MBEDTLS_SHA256_C)
2551 case PSA_ALG_SHA_224:
2552 case PSA_ALG_SHA_256:
2553 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2554 break;
2555#endif
2556#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002557#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002558 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002559#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002560 case PSA_ALG_SHA_512:
2561 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2562 break;
2563#endif
2564 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002565 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002566 }
itayzafrir40835d42018-08-02 13:14:17 +03002567 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002568
itayzafrir40835d42018-08-02 13:14:17 +03002569exit:
2570 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002571 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002572 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002573 return( psa_hash_abort( operation ) );
2574 }
2575 else
2576 {
2577 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002578 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002579 }
2580}
2581
Gilles Peskine2d277862018-06-18 15:41:12 +02002582psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2583 const uint8_t *hash,
2584 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002585{
2586 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2587 size_t actual_hash_length;
2588 psa_status_t status = psa_hash_finish( operation,
2589 actual_hash, sizeof( actual_hash ),
2590 &actual_hash_length );
2591 if( status != PSA_SUCCESS )
2592 return( status );
2593 if( actual_hash_length != hash_length )
2594 return( PSA_ERROR_INVALID_SIGNATURE );
2595 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2596 return( PSA_ERROR_INVALID_SIGNATURE );
2597 return( PSA_SUCCESS );
2598}
2599
Gilles Peskine0a749c82019-11-28 19:33:58 +01002600psa_status_t psa_hash_compute( psa_algorithm_t alg,
2601 const uint8_t *input, size_t input_length,
2602 uint8_t *hash, size_t hash_size,
2603 size_t *hash_length )
2604{
2605 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2606 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2607
2608 *hash_length = hash_size;
2609 status = psa_hash_setup( &operation, alg );
2610 if( status != PSA_SUCCESS )
2611 goto exit;
2612 status = psa_hash_update( &operation, input, input_length );
2613 if( status != PSA_SUCCESS )
2614 goto exit;
2615 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2616 if( status != PSA_SUCCESS )
2617 goto exit;
2618
2619exit:
2620 if( status == PSA_SUCCESS )
2621 status = psa_hash_abort( &operation );
2622 else
2623 psa_hash_abort( &operation );
2624 return( status );
2625}
2626
2627psa_status_t psa_hash_compare( psa_algorithm_t alg,
2628 const uint8_t *input, size_t input_length,
2629 const uint8_t *hash, size_t hash_length )
2630{
2631 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2632 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2633
2634 status = psa_hash_setup( &operation, alg );
2635 if( status != PSA_SUCCESS )
2636 goto exit;
2637 status = psa_hash_update( &operation, input, input_length );
2638 if( status != PSA_SUCCESS )
2639 goto exit;
2640 status = psa_hash_verify( &operation, hash, hash_length );
2641 if( status != PSA_SUCCESS )
2642 goto exit;
2643
2644exit:
2645 if( status == PSA_SUCCESS )
2646 status = psa_hash_abort( &operation );
2647 else
2648 psa_hash_abort( &operation );
2649 return( status );
2650}
2651
Gilles Peskineeb35d782019-01-22 17:56:16 +01002652psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2653 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002654{
2655 if( target_operation->alg != 0 )
2656 return( PSA_ERROR_BAD_STATE );
2657
2658 switch( source_operation->alg )
2659 {
2660 case 0:
2661 return( PSA_ERROR_BAD_STATE );
2662#if defined(MBEDTLS_MD2_C)
2663 case PSA_ALG_MD2:
2664 mbedtls_md2_clone( &target_operation->ctx.md2,
2665 &source_operation->ctx.md2 );
2666 break;
2667#endif
2668#if defined(MBEDTLS_MD4_C)
2669 case PSA_ALG_MD4:
2670 mbedtls_md4_clone( &target_operation->ctx.md4,
2671 &source_operation->ctx.md4 );
2672 break;
2673#endif
2674#if defined(MBEDTLS_MD5_C)
2675 case PSA_ALG_MD5:
2676 mbedtls_md5_clone( &target_operation->ctx.md5,
2677 &source_operation->ctx.md5 );
2678 break;
2679#endif
2680#if defined(MBEDTLS_RIPEMD160_C)
2681 case PSA_ALG_RIPEMD160:
2682 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2683 &source_operation->ctx.ripemd160 );
2684 break;
2685#endif
2686#if defined(MBEDTLS_SHA1_C)
2687 case PSA_ALG_SHA_1:
2688 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2689 &source_operation->ctx.sha1 );
2690 break;
2691#endif
2692#if defined(MBEDTLS_SHA256_C)
2693 case PSA_ALG_SHA_224:
2694 case PSA_ALG_SHA_256:
2695 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2696 &source_operation->ctx.sha256 );
2697 break;
2698#endif
2699#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002700#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002701 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002702#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002703 case PSA_ALG_SHA_512:
2704 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2705 &source_operation->ctx.sha512 );
2706 break;
2707#endif
2708 default:
2709 return( PSA_ERROR_NOT_SUPPORTED );
2710 }
2711
2712 target_operation->alg = source_operation->alg;
2713 return( PSA_SUCCESS );
2714}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002715
2716
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002717/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002718/* MAC */
2719/****************************************************************/
2720
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002721static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002722 psa_algorithm_t alg,
2723 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002724 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002725 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002726{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002727 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002728 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002729
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002730 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002731 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002732
Gilles Peskine8c9def32018-02-08 10:02:12 +01002733 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2734 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002735 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002736 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002737 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002738 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002739 mode = MBEDTLS_MODE_STREAM;
2740 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002741 case PSA_ALG_CTR:
2742 mode = MBEDTLS_MODE_CTR;
2743 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002744 case PSA_ALG_CFB:
2745 mode = MBEDTLS_MODE_CFB;
2746 break;
2747 case PSA_ALG_OFB:
2748 mode = MBEDTLS_MODE_OFB;
2749 break;
2750 case PSA_ALG_CBC_NO_PADDING:
2751 mode = MBEDTLS_MODE_CBC;
2752 break;
2753 case PSA_ALG_CBC_PKCS7:
2754 mode = MBEDTLS_MODE_CBC;
2755 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002756 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002757 mode = MBEDTLS_MODE_CCM;
2758 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002759 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002760 mode = MBEDTLS_MODE_GCM;
2761 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002762 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2763 mode = MBEDTLS_MODE_CHACHAPOLY;
2764 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002765 default:
2766 return( NULL );
2767 }
2768 }
2769 else if( alg == PSA_ALG_CMAC )
2770 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002771 else
2772 return( NULL );
2773
2774 switch( key_type )
2775 {
2776 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002777 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002778 break;
2779 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002780 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2781 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002782 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002783 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002784 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002785 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002786 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2787 * but two-key Triple-DES is functionally three-key Triple-DES
2788 * with K1=K3, so that's how we present it to mbedtls. */
2789 if( key_bits == 128 )
2790 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002791 break;
2792 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002793 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002794 break;
2795 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002796 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002797 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002798 case PSA_KEY_TYPE_CHACHA20:
2799 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2800 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002801 default:
2802 return( NULL );
2803 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002804 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002805 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002806
Jaeden Amero23bbb752018-06-26 14:16:54 +01002807 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2808 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002809}
2810
Gilles Peskinea05219c2018-11-16 16:02:56 +01002811#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002812static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002813{
Gilles Peskine2d277862018-06-18 15:41:12 +02002814 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002815 {
2816 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002817 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002818 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002819 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002820 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002821 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002822 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002823 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002824 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002825 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002826 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002827 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002828 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002829 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002830 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002831 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002832 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002833 return( 128 );
2834 default:
2835 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002836 }
2837}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002838#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002839
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002840/* Initialize the MAC operation structure. Once this function has been
2841 * called, psa_mac_abort can run and will do the right thing. */
2842static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2843 psa_algorithm_t alg )
2844{
2845 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2846
2847 operation->alg = alg;
2848 operation->key_set = 0;
2849 operation->iv_set = 0;
2850 operation->iv_required = 0;
2851 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002852 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002853
2854#if defined(MBEDTLS_CMAC_C)
2855 if( alg == PSA_ALG_CMAC )
2856 {
2857 operation->iv_required = 0;
2858 mbedtls_cipher_init( &operation->ctx.cmac );
2859 status = PSA_SUCCESS;
2860 }
2861 else
2862#endif /* MBEDTLS_CMAC_C */
2863#if defined(MBEDTLS_MD_C)
2864 if( PSA_ALG_IS_HMAC( operation->alg ) )
2865 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002866 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2867 operation->ctx.hmac.hash_ctx.alg = 0;
2868 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002869 }
2870 else
2871#endif /* MBEDTLS_MD_C */
2872 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002873 if( ! PSA_ALG_IS_MAC( alg ) )
2874 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002875 }
2876
2877 if( status != PSA_SUCCESS )
2878 memset( operation, 0, sizeof( *operation ) );
2879 return( status );
2880}
2881
Gilles Peskine01126fa2018-07-12 17:04:55 +02002882#if defined(MBEDTLS_MD_C)
2883static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2884{
Gilles Peskine3f108122018-12-07 18:14:53 +01002885 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002886 return( psa_hash_abort( &hmac->hash_ctx ) );
2887}
2888#endif /* MBEDTLS_MD_C */
2889
Gilles Peskine8c9def32018-02-08 10:02:12 +01002890psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2891{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002892 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002893 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002894 /* The object has (apparently) been initialized but it is not
2895 * in use. It's ok to call abort on such an object, and there's
2896 * nothing to do. */
2897 return( PSA_SUCCESS );
2898 }
2899 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002900#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002901 if( operation->alg == PSA_ALG_CMAC )
2902 {
2903 mbedtls_cipher_free( &operation->ctx.cmac );
2904 }
2905 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002906#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002907#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002908 if( PSA_ALG_IS_HMAC( operation->alg ) )
2909 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002910 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002911 }
2912 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002913#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002914 {
2915 /* Sanity check (shouldn't happen: operation->alg should
2916 * always have been initialized to a valid value). */
2917 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002918 }
Moran Peker41deec42018-04-04 15:43:05 +03002919
Gilles Peskine8c9def32018-02-08 10:02:12 +01002920 operation->alg = 0;
2921 operation->key_set = 0;
2922 operation->iv_set = 0;
2923 operation->iv_required = 0;
2924 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002925 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002926
Gilles Peskine8c9def32018-02-08 10:02:12 +01002927 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002928
2929bad_state:
2930 /* If abort is called on an uninitialized object, we can't trust
2931 * anything. Wipe the object in case it contains confidential data.
2932 * This may result in a memory leak if a pointer gets overwritten,
2933 * but it's too late to do anything about this. */
2934 memset( operation, 0, sizeof( *operation ) );
2935 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002936}
2937
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002938#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002939static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002940 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002941 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002942 const mbedtls_cipher_info_t *cipher_info )
2943{
Janos Follath24eed8d2019-11-22 13:21:35 +00002944 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002945
2946 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002947
2948 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2949 if( ret != 0 )
2950 return( ret );
2951
2952 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02002953 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002954 key_bits );
2955 return( ret );
2956}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002957#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002958
Gilles Peskine248051a2018-06-20 16:09:38 +02002959#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002960static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2961 const uint8_t *key,
2962 size_t key_length,
2963 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002964{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002965 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002966 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002967 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002968 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002969 psa_status_t status;
2970
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002971 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2972 * overflow below. This should never trigger if the hash algorithm
2973 * is implemented correctly. */
2974 /* The size checks against the ipad and opad buffers cannot be written
2975 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2976 * because that triggers -Wlogical-op on GCC 7.3. */
2977 if( block_size > sizeof( ipad ) )
2978 return( PSA_ERROR_NOT_SUPPORTED );
2979 if( block_size > sizeof( hmac->opad ) )
2980 return( PSA_ERROR_NOT_SUPPORTED );
2981 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002982 return( PSA_ERROR_NOT_SUPPORTED );
2983
Gilles Peskined223b522018-06-11 18:12:58 +02002984 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002985 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002986 status = psa_hash_compute( hash_alg, key, key_length,
2987 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002988 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002989 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002990 }
Gilles Peskine96889972018-07-12 17:07:03 +02002991 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2992 * but it is permitted. It is common when HMAC is used in HKDF, for
2993 * example. Don't call `memcpy` in the 0-length because `key` could be
2994 * an invalid pointer which would make the behavior undefined. */
2995 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002996 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002997
Gilles Peskined223b522018-06-11 18:12:58 +02002998 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2999 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003000 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003001 ipad[i] ^= 0x36;
3002 memset( ipad + key_length, 0x36, block_size - key_length );
3003
3004 /* Copy the key material from ipad to opad, flipping the requisite bits,
3005 * and filling the rest of opad with the requisite constant. */
3006 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003007 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3008 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003009
Gilles Peskine01126fa2018-07-12 17:04:55 +02003010 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003011 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003012 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003013
Gilles Peskine01126fa2018-07-12 17:04:55 +02003014 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003015
3016cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03003017 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003018
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003019 return( status );
3020}
Gilles Peskine248051a2018-06-20 16:09:38 +02003021#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003022
Gilles Peskine89167cb2018-07-08 20:12:23 +02003023static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003024 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003025 psa_algorithm_t alg,
3026 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003027{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003028 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003029 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003030 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003031 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003032 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003033 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003034 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003035
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003036 /* A context must be freshly initialized before it can be set up. */
3037 if( operation->alg != 0 )
3038 {
3039 return( PSA_ERROR_BAD_STATE );
3040 }
3041
Gilles Peskined911eb72018-08-14 15:18:45 +02003042 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003043 if( status != PSA_SUCCESS )
3044 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003045 if( is_sign )
3046 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003047
Gilles Peskine28f8f302019-07-24 13:30:31 +02003048 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003049 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003050 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003051 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003052
Gilles Peskine8c9def32018-02-08 10:02:12 +01003053#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003054 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003055 {
3056 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003057 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003058 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003059 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003060 if( cipher_info == NULL )
3061 {
3062 status = PSA_ERROR_NOT_SUPPORTED;
3063 goto exit;
3064 }
3065 operation->mac_size = cipher_info->block_size;
3066 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3067 status = mbedtls_to_psa_error( ret );
3068 }
3069 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003070#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003071#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003072 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003073 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003074 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003075 if( hash_alg == 0 )
3076 {
3077 status = PSA_ERROR_NOT_SUPPORTED;
3078 goto exit;
3079 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003080
3081 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3082 /* Sanity check. This shouldn't fail on a valid configuration. */
3083 if( operation->mac_size == 0 ||
3084 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3085 {
3086 status = PSA_ERROR_NOT_SUPPORTED;
3087 goto exit;
3088 }
3089
Gilles Peskine8e338702019-07-30 20:06:31 +02003090 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003091 {
3092 status = PSA_ERROR_INVALID_ARGUMENT;
3093 goto exit;
3094 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003095
Gilles Peskine01126fa2018-07-12 17:04:55 +02003096 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003097 slot->data.key.data,
3098 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003099 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003100 }
3101 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003102#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003103 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003104 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003105 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003106 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003107
Gilles Peskined911eb72018-08-14 15:18:45 +02003108 if( truncated == 0 )
3109 {
3110 /* The "normal" case: untruncated algorithm. Nothing to do. */
3111 }
3112 else if( truncated < 4 )
3113 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003114 /* A very short MAC is too short for security since it can be
3115 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3116 * so we make this our minimum, even though 32 bits is still
3117 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003118 status = PSA_ERROR_NOT_SUPPORTED;
3119 }
3120 else if( truncated > operation->mac_size )
3121 {
3122 /* It's impossible to "truncate" to a larger length. */
3123 status = PSA_ERROR_INVALID_ARGUMENT;
3124 }
3125 else
3126 operation->mac_size = truncated;
3127
Gilles Peskinefbfac682018-07-08 20:51:54 +02003128exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003129 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003130 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003131 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003132 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003133 else
3134 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003135 operation->key_set = 1;
3136 }
3137 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003138}
3139
Gilles Peskine89167cb2018-07-08 20:12:23 +02003140psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003141 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003142 psa_algorithm_t alg )
3143{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003144 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003145}
3146
3147psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003148 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003149 psa_algorithm_t alg )
3150{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003151 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003152}
3153
Gilles Peskine8c9def32018-02-08 10:02:12 +01003154psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3155 const uint8_t *input,
3156 size_t input_length )
3157{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003158 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003159 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003160 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003161 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003162 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003163 operation->has_input = 1;
3164
Gilles Peskine8c9def32018-02-08 10:02:12 +01003165#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003166 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003167 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003168 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3169 input, input_length );
3170 status = mbedtls_to_psa_error( ret );
3171 }
3172 else
3173#endif /* MBEDTLS_CMAC_C */
3174#if defined(MBEDTLS_MD_C)
3175 if( PSA_ALG_IS_HMAC( operation->alg ) )
3176 {
3177 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3178 input_length );
3179 }
3180 else
3181#endif /* MBEDTLS_MD_C */
3182 {
3183 /* This shouldn't happen if `operation` was initialized by
3184 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003185 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003186 }
3187
Gilles Peskinefbfac682018-07-08 20:51:54 +02003188 if( status != PSA_SUCCESS )
3189 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003190 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003191}
3192
Gilles Peskine01126fa2018-07-12 17:04:55 +02003193#if defined(MBEDTLS_MD_C)
3194static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3195 uint8_t *mac,
3196 size_t mac_size )
3197{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003198 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003199 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3200 size_t hash_size = 0;
3201 size_t block_size = psa_get_hash_block_size( hash_alg );
3202 psa_status_t status;
3203
Gilles Peskine01126fa2018-07-12 17:04:55 +02003204 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3205 if( status != PSA_SUCCESS )
3206 return( status );
3207 /* From here on, tmp needs to be wiped. */
3208
3209 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3210 if( status != PSA_SUCCESS )
3211 goto exit;
3212
3213 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3214 if( status != PSA_SUCCESS )
3215 goto exit;
3216
3217 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3218 if( status != PSA_SUCCESS )
3219 goto exit;
3220
Gilles Peskined911eb72018-08-14 15:18:45 +02003221 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3222 if( status != PSA_SUCCESS )
3223 goto exit;
3224
3225 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003226
3227exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003228 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003229 return( status );
3230}
3231#endif /* MBEDTLS_MD_C */
3232
mohammad16036df908f2018-04-02 08:34:15 -07003233static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003234 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003235 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003236{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003237 if( ! operation->key_set )
3238 return( PSA_ERROR_BAD_STATE );
3239 if( operation->iv_required && ! operation->iv_set )
3240 return( PSA_ERROR_BAD_STATE );
3241
Gilles Peskine8c9def32018-02-08 10:02:12 +01003242 if( mac_size < operation->mac_size )
3243 return( PSA_ERROR_BUFFER_TOO_SMALL );
3244
Gilles Peskine8c9def32018-02-08 10:02:12 +01003245#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003246 if( operation->alg == PSA_ALG_CMAC )
3247 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003248 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3249 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3250 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003251 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003252 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003253 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003254 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003255 else
3256#endif /* MBEDTLS_CMAC_C */
3257#if defined(MBEDTLS_MD_C)
3258 if( PSA_ALG_IS_HMAC( operation->alg ) )
3259 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003260 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003261 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003262 }
3263 else
3264#endif /* MBEDTLS_MD_C */
3265 {
3266 /* This shouldn't happen if `operation` was initialized by
3267 * a setup function. */
3268 return( PSA_ERROR_BAD_STATE );
3269 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003270}
3271
Gilles Peskineacd4be32018-07-08 19:56:25 +02003272psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3273 uint8_t *mac,
3274 size_t mac_size,
3275 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003276{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003277 psa_status_t status;
3278
Jaeden Amero252ef282019-02-15 14:05:35 +00003279 if( operation->alg == 0 )
3280 {
3281 return( PSA_ERROR_BAD_STATE );
3282 }
3283
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003284 /* Fill the output buffer with something that isn't a valid mac
3285 * (barring an attack on the mac and deliberately-crafted input),
3286 * in case the caller doesn't check the return status properly. */
3287 *mac_length = mac_size;
3288 /* If mac_size is 0 then mac may be NULL and then the
3289 * call to memset would have undefined behavior. */
3290 if( mac_size != 0 )
3291 memset( mac, '!', mac_size );
3292
Gilles Peskine89167cb2018-07-08 20:12:23 +02003293 if( ! operation->is_sign )
3294 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003295 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003296 }
mohammad16036df908f2018-04-02 08:34:15 -07003297
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003298 status = psa_mac_finish_internal( operation, mac, mac_size );
3299
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003300 if( status == PSA_SUCCESS )
3301 {
3302 status = psa_mac_abort( operation );
3303 if( status == PSA_SUCCESS )
3304 *mac_length = operation->mac_size;
3305 else
3306 memset( mac, '!', mac_size );
3307 }
3308 else
3309 psa_mac_abort( operation );
3310 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003311}
3312
Gilles Peskineacd4be32018-07-08 19:56:25 +02003313psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3314 const uint8_t *mac,
3315 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003316{
Gilles Peskine828ed142018-06-18 23:25:51 +02003317 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003318 psa_status_t status;
3319
Jaeden Amero252ef282019-02-15 14:05:35 +00003320 if( operation->alg == 0 )
3321 {
3322 return( PSA_ERROR_BAD_STATE );
3323 }
3324
Gilles Peskine89167cb2018-07-08 20:12:23 +02003325 if( operation->is_sign )
3326 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003327 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003328 }
3329 if( operation->mac_size != mac_length )
3330 {
3331 status = PSA_ERROR_INVALID_SIGNATURE;
3332 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003333 }
mohammad16036df908f2018-04-02 08:34:15 -07003334
3335 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003336 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003337 if( status != PSA_SUCCESS )
3338 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003339
3340 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3341 status = PSA_ERROR_INVALID_SIGNATURE;
3342
3343cleanup:
3344 if( status == PSA_SUCCESS )
3345 status = psa_mac_abort( operation );
3346 else
3347 psa_mac_abort( operation );
3348
Gilles Peskine3f108122018-12-07 18:14:53 +01003349 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003350
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003351 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003352}
3353
3354
Gilles Peskine20035e32018-02-03 22:44:14 +01003355
Gilles Peskine20035e32018-02-03 22:44:14 +01003356/****************************************************************/
3357/* Asymmetric cryptography */
3358/****************************************************************/
3359
Gilles Peskine2b450e32018-06-27 15:42:46 +02003360#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003361/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003362 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003363static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3364 size_t hash_length,
3365 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003366{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003367 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003368 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003369 *md_alg = mbedtls_md_get_type( md_info );
3370
3371 /* The Mbed TLS RSA module uses an unsigned int for hash length
3372 * parameters. Validate that it fits so that we don't risk an
3373 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003374#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003375 if( hash_length > UINT_MAX )
3376 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003377#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003378
3379#if defined(MBEDTLS_PKCS1_V15)
3380 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3381 * must be correct. */
3382 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3383 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003384 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003385 if( md_info == NULL )
3386 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003387 if( mbedtls_md_get_size( md_info ) != hash_length )
3388 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003389 }
3390#endif /* MBEDTLS_PKCS1_V15 */
3391
3392#if defined(MBEDTLS_PKCS1_V21)
3393 /* PSS requires a hash internally. */
3394 if( PSA_ALG_IS_RSA_PSS( alg ) )
3395 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003396 if( md_info == NULL )
3397 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003398 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003399#endif /* MBEDTLS_PKCS1_V21 */
3400
Gilles Peskine61b91d42018-06-08 16:09:36 +02003401 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003402}
3403
Gilles Peskine2b450e32018-06-27 15:42:46 +02003404static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3405 psa_algorithm_t alg,
3406 const uint8_t *hash,
3407 size_t hash_length,
3408 uint8_t *signature,
3409 size_t signature_size,
3410 size_t *signature_length )
3411{
3412 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003413 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003414 mbedtls_md_type_t md_alg;
3415
3416 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3417 if( status != PSA_SUCCESS )
3418 return( status );
3419
Gilles Peskine630a18a2018-06-29 17:49:35 +02003420 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003421 return( PSA_ERROR_BUFFER_TOO_SMALL );
3422
3423#if defined(MBEDTLS_PKCS1_V15)
3424 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3425 {
3426 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3427 MBEDTLS_MD_NONE );
3428 ret = mbedtls_rsa_pkcs1_sign( rsa,
3429 mbedtls_ctr_drbg_random,
3430 &global_data.ctr_drbg,
3431 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003432 md_alg,
3433 (unsigned int) hash_length,
3434 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003435 signature );
3436 }
3437 else
3438#endif /* MBEDTLS_PKCS1_V15 */
3439#if defined(MBEDTLS_PKCS1_V21)
3440 if( PSA_ALG_IS_RSA_PSS( alg ) )
3441 {
3442 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3443 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3444 mbedtls_ctr_drbg_random,
3445 &global_data.ctr_drbg,
3446 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003447 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003448 (unsigned int) hash_length,
3449 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003450 signature );
3451 }
3452 else
3453#endif /* MBEDTLS_PKCS1_V21 */
3454 {
3455 return( PSA_ERROR_INVALID_ARGUMENT );
3456 }
3457
3458 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003459 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003460 return( mbedtls_to_psa_error( ret ) );
3461}
3462
3463static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3464 psa_algorithm_t alg,
3465 const uint8_t *hash,
3466 size_t hash_length,
3467 const uint8_t *signature,
3468 size_t signature_length )
3469{
3470 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003471 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003472 mbedtls_md_type_t md_alg;
3473
3474 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3475 if( status != PSA_SUCCESS )
3476 return( status );
3477
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003478 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3479 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003480
3481#if defined(MBEDTLS_PKCS1_V15)
3482 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3483 {
3484 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3485 MBEDTLS_MD_NONE );
3486 ret = mbedtls_rsa_pkcs1_verify( rsa,
3487 mbedtls_ctr_drbg_random,
3488 &global_data.ctr_drbg,
3489 MBEDTLS_RSA_PUBLIC,
3490 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003491 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003492 hash,
3493 signature );
3494 }
3495 else
3496#endif /* MBEDTLS_PKCS1_V15 */
3497#if defined(MBEDTLS_PKCS1_V21)
3498 if( PSA_ALG_IS_RSA_PSS( alg ) )
3499 {
3500 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3501 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3502 mbedtls_ctr_drbg_random,
3503 &global_data.ctr_drbg,
3504 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003505 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003506 (unsigned int) hash_length,
3507 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003508 signature );
3509 }
3510 else
3511#endif /* MBEDTLS_PKCS1_V21 */
3512 {
3513 return( PSA_ERROR_INVALID_ARGUMENT );
3514 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003515
3516 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3517 * the rest of the signature is invalid". This has little use in
3518 * practice and PSA doesn't report this distinction. */
3519 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3520 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003521 return( mbedtls_to_psa_error( ret ) );
3522}
3523#endif /* MBEDTLS_RSA_C */
3524
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003525#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003526/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3527 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3528 * (even though these functions don't modify it). */
3529static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3530 psa_algorithm_t alg,
3531 const uint8_t *hash,
3532 size_t hash_length,
3533 uint8_t *signature,
3534 size_t signature_size,
3535 size_t *signature_length )
3536{
Janos Follath24eed8d2019-11-22 13:21:35 +00003537 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003538 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003539 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003540 mbedtls_mpi_init( &r );
3541 mbedtls_mpi_init( &s );
3542
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003543 if( signature_size < 2 * curve_bytes )
3544 {
3545 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3546 goto cleanup;
3547 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003548
Gilles Peskinea05219c2018-11-16 16:02:56 +01003549#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003550 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3551 {
3552 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3553 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3554 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003555 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3556 &ecp->d, hash,
3557 hash_length, md_alg,
3558 mbedtls_ctr_drbg_random,
3559 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003560 }
3561 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003562#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003563 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003564 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003565 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3566 hash, hash_length,
3567 mbedtls_ctr_drbg_random,
3568 &global_data.ctr_drbg ) );
3569 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003570
3571 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3572 signature,
3573 curve_bytes ) );
3574 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3575 signature + curve_bytes,
3576 curve_bytes ) );
3577
3578cleanup:
3579 mbedtls_mpi_free( &r );
3580 mbedtls_mpi_free( &s );
3581 if( ret == 0 )
3582 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003583 return( mbedtls_to_psa_error( ret ) );
3584}
3585
3586static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3587 const uint8_t *hash,
3588 size_t hash_length,
3589 const uint8_t *signature,
3590 size_t signature_length )
3591{
Janos Follath24eed8d2019-11-22 13:21:35 +00003592 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003593 mbedtls_mpi r, s;
3594 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3595 mbedtls_mpi_init( &r );
3596 mbedtls_mpi_init( &s );
3597
3598 if( signature_length != 2 * curve_bytes )
3599 return( PSA_ERROR_INVALID_SIGNATURE );
3600
3601 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3602 signature,
3603 curve_bytes ) );
3604 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3605 signature + curve_bytes,
3606 curve_bytes ) );
3607
Steven Cooremanacda8342020-07-24 23:09:52 +02003608 /* Check whether the public part is loaded. If not, load it. */
3609 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3610 {
3611 MBEDTLS_MPI_CHK(
3612 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
3613 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
3614 }
3615
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003616 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3617 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003618
3619cleanup:
3620 mbedtls_mpi_free( &r );
3621 mbedtls_mpi_free( &s );
3622 return( mbedtls_to_psa_error( ret ) );
3623}
3624#endif /* MBEDTLS_ECDSA_C */
3625
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003626psa_status_t psa_sign_hash( psa_key_handle_t handle,
3627 psa_algorithm_t alg,
3628 const uint8_t *hash,
3629 size_t hash_length,
3630 uint8_t *signature,
3631 size_t signature_size,
3632 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003633{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003634 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003635 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003636#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3637 const psa_drv_se_t *drv;
3638 psa_drv_se_context_t *drv_context;
3639#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003640
3641 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003642 /* Immediately reject a zero-length signature buffer. This guarantees
3643 * that signature must be a valid pointer. (On the other hand, the hash
3644 * buffer can in principle be empty since it doesn't actually have
3645 * to be a hash.) */
3646 if( signature_size == 0 )
3647 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003648
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003649 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003650 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003651 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003652 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003653 {
3654 status = PSA_ERROR_INVALID_ARGUMENT;
3655 goto exit;
3656 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003657
Gilles Peskineedc64242019-08-07 21:05:07 +02003658#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3659 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3660 {
3661 if( drv->asymmetric == NULL ||
3662 drv->asymmetric->p_sign == NULL )
3663 {
3664 status = PSA_ERROR_NOT_SUPPORTED;
3665 goto exit;
3666 }
3667 status = drv->asymmetric->p_sign( drv_context,
3668 slot->data.se.slot_number,
3669 alg,
3670 hash, hash_length,
3671 signature, signature_size,
3672 signature_length );
3673 }
3674 else
3675#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003676#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003677 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003678 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003679 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003680
Steven Cooreman7f391872020-07-30 14:57:44 +02003681 status = psa_load_rsa_representation( slot->data.key.data,
3682 slot->data.key.bytes,
3683 slot->attr.type,
Steven Cooremana01795d2020-07-24 22:48:15 +02003684 &rsa );
3685 if( status != PSA_SUCCESS )
3686 goto exit;
3687
Steven Cooremana2371e52020-07-28 14:30:39 +02003688 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003689 alg,
3690 hash, hash_length,
3691 signature, signature_size,
3692 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003693
Steven Cooremana2371e52020-07-28 14:30:39 +02003694 mbedtls_rsa_free( rsa );
3695 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003696 }
3697 else
3698#endif /* defined(MBEDTLS_RSA_C) */
3699#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003700 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003701 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003702#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003703 if(
3704#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3705 PSA_ALG_IS_ECDSA( alg )
3706#else
3707 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3708#endif
3709 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003710 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003711 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02003712 status = psa_load_ecp_representation( slot->data.key.data,
3713 slot->data.key.bytes,
3714 slot->attr.type,
3715 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003716 if( status != PSA_SUCCESS )
3717 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003718 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003719 alg,
3720 hash, hash_length,
3721 signature, signature_size,
3722 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003723 mbedtls_ecp_keypair_free( ecp );
3724 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003725 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003726 else
3727#endif /* defined(MBEDTLS_ECDSA_C) */
3728 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003729 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003730 }
itayzafrir5c753392018-05-08 11:18:38 +03003731 }
3732 else
3733#endif /* defined(MBEDTLS_ECP_C) */
3734 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003735 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003736 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003737
3738exit:
3739 /* Fill the unused part of the output buffer (the whole buffer on error,
3740 * the trailing part on success) with something that isn't a valid mac
3741 * (barring an attack on the mac and deliberately-crafted input),
3742 * in case the caller doesn't check the return status properly. */
3743 if( status == PSA_SUCCESS )
3744 memset( signature + *signature_length, '!',
3745 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003746 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003747 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003748 /* If signature_size is 0 then we have nothing to do. We must not call
3749 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003750 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003751}
3752
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003753psa_status_t psa_verify_hash( psa_key_handle_t handle,
3754 psa_algorithm_t alg,
3755 const uint8_t *hash,
3756 size_t hash_length,
3757 const uint8_t *signature,
3758 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003759{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003760 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003761 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003762#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3763 const psa_drv_se_t *drv;
3764 psa_drv_se_context_t *drv_context;
3765#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003766
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003767 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003768 if( status != PSA_SUCCESS )
3769 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003770
Gilles Peskineedc64242019-08-07 21:05:07 +02003771#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3772 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3773 {
3774 if( drv->asymmetric == NULL ||
3775 drv->asymmetric->p_verify == NULL )
3776 return( PSA_ERROR_NOT_SUPPORTED );
3777 return( drv->asymmetric->p_verify( drv_context,
3778 slot->data.se.slot_number,
3779 alg,
3780 hash, hash_length,
3781 signature, signature_length ) );
3782 }
3783 else
3784#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003785#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003786 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003787 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003788 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003789
Steven Cooreman7f391872020-07-30 14:57:44 +02003790 status = psa_load_rsa_representation( slot->data.key.data,
3791 slot->data.key.bytes,
3792 slot->attr.type,
3793 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003794 if( status != PSA_SUCCESS )
3795 return status;
3796
Steven Cooremana2371e52020-07-28 14:30:39 +02003797 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003798 alg,
3799 hash, hash_length,
3800 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003801 mbedtls_rsa_free( rsa );
3802 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003803 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003804 }
3805 else
3806#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003807#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003808 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003809 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003810#if defined(MBEDTLS_ECDSA_C)
3811 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003812 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003813 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02003814 status = psa_load_ecp_representation( slot->data.key.data,
3815 slot->data.key.bytes,
3816 slot->attr.type,
3817 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003818 if( status != PSA_SUCCESS )
3819 return status;
Steven Cooremana2371e52020-07-28 14:30:39 +02003820 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003821 hash, hash_length,
3822 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003823 mbedtls_ecp_keypair_free( ecp );
3824 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003825 return status;
3826 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003827 else
3828#endif /* defined(MBEDTLS_ECDSA_C) */
3829 {
3830 return( PSA_ERROR_INVALID_ARGUMENT );
3831 }
itayzafrir5c753392018-05-08 11:18:38 +03003832 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003833 else
3834#endif /* defined(MBEDTLS_ECP_C) */
3835 {
3836 return( PSA_ERROR_NOT_SUPPORTED );
3837 }
3838}
3839
Gilles Peskine072ac562018-06-30 00:21:29 +02003840#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3841static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3842 mbedtls_rsa_context *rsa )
3843{
3844 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3845 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3846 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3847 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3848}
3849#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3850
Gilles Peskinec5487a82018-12-03 18:08:14 +01003851psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003852 psa_algorithm_t alg,
3853 const uint8_t *input,
3854 size_t input_length,
3855 const uint8_t *salt,
3856 size_t salt_length,
3857 uint8_t *output,
3858 size_t output_size,
3859 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003860{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003861 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003862 psa_status_t status;
3863
Darryl Green5cc689a2018-07-24 15:34:10 +01003864 (void) input;
3865 (void) input_length;
3866 (void) salt;
3867 (void) output;
3868 (void) output_size;
3869
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003870 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003871
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003872 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3873 return( PSA_ERROR_INVALID_ARGUMENT );
3874
Gilles Peskine28f8f302019-07-24 13:30:31 +02003875 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003876 if( status != PSA_SUCCESS )
3877 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003878 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3879 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003880 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003881
3882#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003883 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003884 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003885 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02003886 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02003887
Steven Cooreman7f391872020-07-30 14:57:44 +02003888 status = psa_load_rsa_representation( slot->data.key.data,
3889 slot->data.key.bytes,
3890 slot->attr.type,
3891 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003892 if( status != PSA_SUCCESS )
3893 return status;
Steven Cooremana2371e52020-07-28 14:30:39 +02003894
3895 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003896 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003897 mbedtls_rsa_free( rsa );
3898 mbedtls_free( rsa );
Vikas Katariya21599b62019-08-02 12:26:29 +01003899 return( PSA_ERROR_BUFFER_TOO_SMALL );
Steven Cooremana01795d2020-07-24 22:48:15 +02003900 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003901#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003902 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003903 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003904 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003905 mbedtls_ctr_drbg_random,
3906 &global_data.ctr_drbg,
3907 MBEDTLS_RSA_PUBLIC,
3908 input_length,
3909 input,
3910 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003911 }
3912 else
3913#endif /* MBEDTLS_PKCS1_V15 */
3914#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003915 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003916 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003917 psa_rsa_oaep_set_padding_mode( alg, rsa );
3918 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine072ac562018-06-30 00:21:29 +02003919 mbedtls_ctr_drbg_random,
3920 &global_data.ctr_drbg,
3921 MBEDTLS_RSA_PUBLIC,
3922 salt, salt_length,
3923 input_length,
3924 input,
3925 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003926 }
3927 else
3928#endif /* MBEDTLS_PKCS1_V21 */
3929 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003930 mbedtls_rsa_free( rsa );
3931 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003932 return( PSA_ERROR_INVALID_ARGUMENT );
3933 }
3934 if( ret == 0 )
Steven Cooremana2371e52020-07-28 14:30:39 +02003935 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003936
Steven Cooremana2371e52020-07-28 14:30:39 +02003937 mbedtls_rsa_free( rsa );
3938 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003939 return( mbedtls_to_psa_error( ret ) );
3940 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003941 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003942#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003943 {
3944 return( PSA_ERROR_NOT_SUPPORTED );
3945 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003946}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003947
Gilles Peskinec5487a82018-12-03 18:08:14 +01003948psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003949 psa_algorithm_t alg,
3950 const uint8_t *input,
3951 size_t input_length,
3952 const uint8_t *salt,
3953 size_t salt_length,
3954 uint8_t *output,
3955 size_t output_size,
3956 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003957{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003958 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003959 psa_status_t status;
3960
Darryl Green5cc689a2018-07-24 15:34:10 +01003961 (void) input;
3962 (void) input_length;
3963 (void) salt;
3964 (void) output;
3965 (void) output_size;
3966
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003967 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003968
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003969 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3970 return( PSA_ERROR_INVALID_ARGUMENT );
3971
Gilles Peskine28f8f302019-07-24 13:30:31 +02003972 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003973 if( status != PSA_SUCCESS )
3974 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003975 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003976 return( PSA_ERROR_INVALID_ARGUMENT );
3977
3978#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003979 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003980 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003981 mbedtls_rsa_context *rsa;
Steven Cooreman75b74362020-07-28 14:30:13 +02003982 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02003983
Steven Cooreman7f391872020-07-30 14:57:44 +02003984 status = psa_load_rsa_representation( slot->data.key.data,
3985 slot->data.key.bytes,
3986 slot->attr.type,
3987 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003988 if( status != PSA_SUCCESS )
3989 return status;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003990
Steven Cooremana2371e52020-07-28 14:30:39 +02003991 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003992 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003993 mbedtls_rsa_free( rsa );
3994 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003995 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana01795d2020-07-24 22:48:15 +02003996 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003997
3998#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003999 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004000 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004001 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004002 mbedtls_ctr_drbg_random,
4003 &global_data.ctr_drbg,
4004 MBEDTLS_RSA_PRIVATE,
4005 output_length,
4006 input,
4007 output,
4008 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004009 }
4010 else
4011#endif /* MBEDTLS_PKCS1_V15 */
4012#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004013 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004014 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004015 psa_rsa_oaep_set_padding_mode( alg, rsa );
4016 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine072ac562018-06-30 00:21:29 +02004017 mbedtls_ctr_drbg_random,
4018 &global_data.ctr_drbg,
4019 MBEDTLS_RSA_PRIVATE,
4020 salt, salt_length,
4021 output_length,
4022 input,
4023 output,
4024 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004025 }
4026 else
4027#endif /* MBEDTLS_PKCS1_V21 */
4028 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004029 mbedtls_rsa_free( rsa );
4030 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004031 return( PSA_ERROR_INVALID_ARGUMENT );
4032 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004033
Steven Cooremana2371e52020-07-28 14:30:39 +02004034 mbedtls_rsa_free( rsa );
4035 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004036 return( mbedtls_to_psa_error( ret ) );
4037 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004038 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02004039#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004040 {
4041 return( PSA_ERROR_NOT_SUPPORTED );
4042 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004043}
Gilles Peskine20035e32018-02-03 22:44:14 +01004044
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004045
4046
mohammad1603503973b2018-03-12 15:59:30 +02004047/****************************************************************/
4048/* Symmetric cryptography */
4049/****************************************************************/
4050
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004051/* Initialize the cipher operation structure. Once this function has been
4052 * called, psa_cipher_abort can run and will do the right thing. */
4053static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
4054 psa_algorithm_t alg )
4055{
Gilles Peskinec06e0712018-06-20 16:21:04 +02004056 if( ! PSA_ALG_IS_CIPHER( alg ) )
4057 {
4058 memset( operation, 0, sizeof( *operation ) );
4059 return( PSA_ERROR_INVALID_ARGUMENT );
4060 }
4061
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004062 operation->alg = alg;
4063 operation->key_set = 0;
4064 operation->iv_set = 0;
4065 operation->iv_required = 1;
4066 operation->iv_size = 0;
4067 operation->block_size = 0;
4068 mbedtls_cipher_init( &operation->ctx.cipher );
4069 return( PSA_SUCCESS );
4070}
4071
Gilles Peskinee553c652018-06-04 16:22:46 +02004072static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004073 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02004074 psa_algorithm_t alg,
4075 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004076{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004077 int ret = 0;
4078 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004079 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004080 size_t key_bits;
4081 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004082 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4083 PSA_KEY_USAGE_ENCRYPT :
4084 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004085
Jaeden Amero36ee5d02019-02-19 09:25:10 +00004086 /* A context must be freshly initialized before it can be set up. */
4087 if( operation->alg != 0 )
4088 {
4089 return( PSA_ERROR_BAD_STATE );
4090 }
4091
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004092 status = psa_cipher_init( operation, alg );
4093 if( status != PSA_SUCCESS )
4094 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004095
Gilles Peskine28f8f302019-07-24 13:30:31 +02004096 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004097 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004098 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004099 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02004100
Gilles Peskine8e338702019-07-30 20:06:31 +02004101 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004102 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004103 {
4104 status = PSA_ERROR_NOT_SUPPORTED;
4105 goto exit;
4106 }
mohammad1603503973b2018-03-12 15:59:30 +02004107
mohammad1603503973b2018-03-12 15:59:30 +02004108 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004109 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004110 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004111
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004112#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004113 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004114 {
4115 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004116 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004117 memcpy( keys, slot->data.key.data, 16 );
4118 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004119 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4120 keys,
4121 192, cipher_operation );
4122 }
4123 else
4124#endif
4125 {
4126 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004127 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004128 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004129 }
Moran Peker41deec42018-04-04 15:43:05 +03004130 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004131 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004132
mohammad16038481e742018-03-18 13:57:31 +02004133#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004134 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004135 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004136 case PSA_ALG_CBC_NO_PADDING:
4137 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4138 MBEDTLS_PADDING_NONE );
4139 break;
4140 case PSA_ALG_CBC_PKCS7:
4141 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4142 MBEDTLS_PADDING_PKCS7 );
4143 break;
4144 default:
4145 /* The algorithm doesn't involve padding. */
4146 ret = 0;
4147 break;
4148 }
4149 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004150 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004151#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4152
mohammad1603503973b2018-03-12 15:59:30 +02004153 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004154 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004155 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004156 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02004157 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004158 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004159 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004160#if defined(MBEDTLS_CHACHA20_C)
4161 else
4162 if( alg == PSA_ALG_CHACHA20 )
4163 operation->iv_size = 12;
4164#endif
mohammad1603503973b2018-03-12 15:59:30 +02004165
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004166exit:
4167 if( status == 0 )
4168 status = mbedtls_to_psa_error( ret );
4169 if( status != 0 )
4170 psa_cipher_abort( operation );
4171 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004172}
4173
Gilles Peskinefe119512018-07-08 21:39:34 +02004174psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004175 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004176 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004177{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004178 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004179}
4180
Gilles Peskinefe119512018-07-08 21:39:34 +02004181psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004182 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02004183 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004184{
Gilles Peskinec5487a82018-12-03 18:08:14 +01004185 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004186}
4187
Gilles Peskinefe119512018-07-08 21:39:34 +02004188psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004189 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004190 size_t iv_size,
4191 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004192{
itayzafrir534bd7c2018-08-02 13:56:32 +03004193 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004194 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004195 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004196 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004197 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004198 }
Moran Peker41deec42018-04-04 15:43:05 +03004199 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004200 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004201 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004202 goto exit;
4203 }
Gilles Peskine7e928852018-06-04 16:23:10 +02004204 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
4205 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004206 if( ret != 0 )
4207 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004208 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004209 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004210 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004211
mohammad16038481e742018-03-18 13:57:31 +02004212 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004213 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004214
Moran Peker395db872018-05-31 14:07:14 +03004215exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03004216 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03004217 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004218 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004219}
4220
Gilles Peskinefe119512018-07-08 21:39:34 +02004221psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004222 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004223 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004224{
itayzafrir534bd7c2018-08-02 13:56:32 +03004225 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004226 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02004227 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03004228 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004229 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03004230 }
Moran Pekera28258c2018-05-29 16:25:04 +03004231 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004232 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004233 status = PSA_ERROR_INVALID_ARGUMENT;
4234 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004235 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004236 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4237 status = mbedtls_to_psa_error( ret );
4238exit:
4239 if( status == PSA_SUCCESS )
4240 operation->iv_set = 1;
4241 else
mohammad1603503973b2018-03-12 15:59:30 +02004242 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004243 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004244}
4245
Gilles Peskinee553c652018-06-04 16:22:46 +02004246psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4247 const uint8_t *input,
4248 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004249 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004250 size_t output_size,
4251 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004252{
itayzafrir534bd7c2018-08-02 13:56:32 +03004253 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004254 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004255 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00004256
4257 if( operation->alg == 0 )
4258 {
4259 return( PSA_ERROR_BAD_STATE );
4260 }
4261
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004262 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004263 {
4264 /* Take the unprocessed partial block left over from previous
4265 * update calls, if any, plus the input to this call. Remove
4266 * the last partial block, if any. You get the data that will be
4267 * output in this call. */
4268 expected_output_size =
4269 ( operation->ctx.cipher.unprocessed_len + input_length )
4270 / operation->block_size * operation->block_size;
4271 }
4272 else
4273 {
4274 expected_output_size = input_length;
4275 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004276
Gilles Peskine89d789c2018-06-04 17:17:16 +02004277 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004278 {
4279 status = PSA_ERROR_BUFFER_TOO_SMALL;
4280 goto exit;
4281 }
mohammad160382759612018-03-12 18:16:40 +02004282
mohammad1603503973b2018-03-12 15:59:30 +02004283 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02004284 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03004285 status = mbedtls_to_psa_error( ret );
4286exit:
4287 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004288 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004289 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004290}
4291
Gilles Peskinee553c652018-06-04 16:22:46 +02004292psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4293 uint8_t *output,
4294 size_t output_size,
4295 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004296{
David Saadab4ecc272019-02-14 13:48:10 +02004297 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01004298 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02004299 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03004300
mohammad1603503973b2018-03-12 15:59:30 +02004301 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004302 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004303 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004304 }
4305 if( operation->iv_required && ! operation->iv_set )
4306 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00004307 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03004308 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004309
Gilles Peskine2c5219a2018-06-06 15:12:32 +02004310 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004311 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4312 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004313 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004314 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004315 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004316 }
4317
Janos Follath315b51c2018-07-09 16:04:51 +01004318 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4319 temp_output_buffer,
4320 output_length );
4321 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004322 {
Janos Follath315b51c2018-07-09 16:04:51 +01004323 status = mbedtls_to_psa_error( cipher_ret );
4324 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004325 }
Janos Follath315b51c2018-07-09 16:04:51 +01004326
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004327 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004328 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004329 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004330 memcpy( output, temp_output_buffer, *output_length );
4331 else
4332 {
Janos Follath315b51c2018-07-09 16:04:51 +01004333 status = PSA_ERROR_BUFFER_TOO_SMALL;
4334 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004335 }
mohammad1603503973b2018-03-12 15:59:30 +02004336
Gilles Peskine3f108122018-12-07 18:14:53 +01004337 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004338 status = psa_cipher_abort( operation );
4339
4340 return( status );
4341
4342error:
4343
4344 *output_length = 0;
4345
Gilles Peskine3f108122018-12-07 18:14:53 +01004346 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004347 (void) psa_cipher_abort( operation );
4348
4349 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004350}
4351
Gilles Peskinee553c652018-06-04 16:22:46 +02004352psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4353{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004354 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004355 {
4356 /* The object has (apparently) been initialized but it is not
4357 * in use. It's ok to call abort on such an object, and there's
4358 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004359 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004360 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004361
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004362 /* Sanity check (shouldn't happen: operation->alg should
4363 * always have been initialized to a valid value). */
4364 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4365 return( PSA_ERROR_BAD_STATE );
4366
mohammad1603503973b2018-03-12 15:59:30 +02004367 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004368
Moran Peker41deec42018-04-04 15:43:05 +03004369 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004370 operation->key_set = 0;
4371 operation->iv_set = 0;
4372 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004373 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004374 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004375
Moran Peker395db872018-05-31 14:07:14 +03004376 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004377}
4378
Gilles Peskinea0655c32018-04-30 17:06:50 +02004379
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004380
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004381
mohammad16035955c982018-04-26 00:53:03 +03004382/****************************************************************/
4383/* AEAD */
4384/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004385
Gilles Peskineedf9a652018-08-17 18:11:56 +02004386typedef struct
4387{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004388 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004389 const mbedtls_cipher_info_t *cipher_info;
4390 union
4391 {
4392#if defined(MBEDTLS_CCM_C)
4393 mbedtls_ccm_context ccm;
4394#endif /* MBEDTLS_CCM_C */
4395#if defined(MBEDTLS_GCM_C)
4396 mbedtls_gcm_context gcm;
4397#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004398#if defined(MBEDTLS_CHACHAPOLY_C)
4399 mbedtls_chachapoly_context chachapoly;
4400#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004401 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004402 psa_algorithm_t core_alg;
4403 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004404 uint8_t tag_length;
4405} aead_operation_t;
4406
Gilles Peskine30a9e412019-01-14 18:36:12 +01004407static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004408{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004409 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004410 {
4411#if defined(MBEDTLS_CCM_C)
4412 case PSA_ALG_CCM:
4413 mbedtls_ccm_free( &operation->ctx.ccm );
4414 break;
4415#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004416#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004417 case PSA_ALG_GCM:
4418 mbedtls_gcm_free( &operation->ctx.gcm );
4419 break;
4420#endif /* MBEDTLS_GCM_C */
4421 }
4422}
4423
4424static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004425 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004426 psa_key_usage_t usage,
4427 psa_algorithm_t alg )
4428{
4429 psa_status_t status;
4430 size_t key_bits;
4431 mbedtls_cipher_id_t cipher_id;
4432
Gilles Peskine28f8f302019-07-24 13:30:31 +02004433 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004434 if( status != PSA_SUCCESS )
4435 return( status );
4436
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004437 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004438
4439 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004440 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004441 &cipher_id );
4442 if( operation->cipher_info == NULL )
4443 return( PSA_ERROR_NOT_SUPPORTED );
4444
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004445 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004446 {
4447#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004448 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4449 operation->core_alg = PSA_ALG_CCM;
4450 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004451 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4452 * The call to mbedtls_ccm_encrypt_and_tag or
4453 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004454 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004455 return( PSA_ERROR_INVALID_ARGUMENT );
4456 mbedtls_ccm_init( &operation->ctx.ccm );
4457 status = mbedtls_to_psa_error(
4458 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004459 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004460 (unsigned int) key_bits ) );
4461 if( status != 0 )
4462 goto cleanup;
4463 break;
4464#endif /* MBEDTLS_CCM_C */
4465
4466#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004467 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4468 operation->core_alg = PSA_ALG_GCM;
4469 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004470 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4471 * The call to mbedtls_gcm_crypt_and_tag or
4472 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004473 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004474 return( PSA_ERROR_INVALID_ARGUMENT );
4475 mbedtls_gcm_init( &operation->ctx.gcm );
4476 status = mbedtls_to_psa_error(
4477 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004478 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004479 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004480 if( status != 0 )
4481 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004482 break;
4483#endif /* MBEDTLS_GCM_C */
4484
Gilles Peskine26869f22019-05-06 15:25:00 +02004485#if defined(MBEDTLS_CHACHAPOLY_C)
4486 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4487 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4488 operation->full_tag_length = 16;
4489 /* We only support the default tag length. */
4490 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4491 return( PSA_ERROR_NOT_SUPPORTED );
4492 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4493 status = mbedtls_to_psa_error(
4494 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004495 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004496 if( status != 0 )
4497 goto cleanup;
4498 break;
4499#endif /* MBEDTLS_CHACHAPOLY_C */
4500
Gilles Peskineedf9a652018-08-17 18:11:56 +02004501 default:
4502 return( PSA_ERROR_NOT_SUPPORTED );
4503 }
4504
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004505 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4506 {
4507 status = PSA_ERROR_INVALID_ARGUMENT;
4508 goto cleanup;
4509 }
4510 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004511
Gilles Peskineedf9a652018-08-17 18:11:56 +02004512 return( PSA_SUCCESS );
4513
4514cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004515 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004516 return( status );
4517}
4518
Gilles Peskinec5487a82018-12-03 18:08:14 +01004519psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004520 psa_algorithm_t alg,
4521 const uint8_t *nonce,
4522 size_t nonce_length,
4523 const uint8_t *additional_data,
4524 size_t additional_data_length,
4525 const uint8_t *plaintext,
4526 size_t plaintext_length,
4527 uint8_t *ciphertext,
4528 size_t ciphertext_size,
4529 size_t *ciphertext_length )
4530{
mohammad16035955c982018-04-26 00:53:03 +03004531 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004532 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004533 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004534
mohammad1603f08a5502018-06-03 15:05:47 +03004535 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004536
Gilles Peskinec5487a82018-12-03 18:08:14 +01004537 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004538 if( status != PSA_SUCCESS )
4539 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004540
Gilles Peskineedf9a652018-08-17 18:11:56 +02004541 /* For all currently supported modes, the tag is at the end of the
4542 * ciphertext. */
4543 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4544 {
4545 status = PSA_ERROR_BUFFER_TOO_SMALL;
4546 goto exit;
4547 }
4548 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004549
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004550#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004551 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004552 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004553 status = mbedtls_to_psa_error(
4554 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4555 MBEDTLS_GCM_ENCRYPT,
4556 plaintext_length,
4557 nonce, nonce_length,
4558 additional_data, additional_data_length,
4559 plaintext, ciphertext,
4560 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004561 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004562 else
4563#endif /* MBEDTLS_GCM_C */
4564#if defined(MBEDTLS_CCM_C)
4565 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004566 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004567 status = mbedtls_to_psa_error(
4568 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4569 plaintext_length,
4570 nonce, nonce_length,
4571 additional_data,
4572 additional_data_length,
4573 plaintext, ciphertext,
4574 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004575 }
mohammad16035c8845f2018-05-09 05:40:09 -07004576 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004577#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004578#if defined(MBEDTLS_CHACHAPOLY_C)
4579 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4580 {
4581 if( nonce_length != 12 || operation.tag_length != 16 )
4582 {
4583 status = PSA_ERROR_NOT_SUPPORTED;
4584 goto exit;
4585 }
4586 status = mbedtls_to_psa_error(
4587 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4588 plaintext_length,
4589 nonce,
4590 additional_data,
4591 additional_data_length,
4592 plaintext,
4593 ciphertext,
4594 tag ) );
4595 }
4596 else
4597#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004598 {
mohammad1603554faad2018-06-03 15:07:38 +03004599 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004600 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004601
Gilles Peskineedf9a652018-08-17 18:11:56 +02004602 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4603 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004604
Gilles Peskineedf9a652018-08-17 18:11:56 +02004605exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004606 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004607 if( status == PSA_SUCCESS )
4608 *ciphertext_length = plaintext_length + operation.tag_length;
4609 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004610}
4611
Gilles Peskineee652a32018-06-01 19:23:52 +02004612/* Locate the tag in a ciphertext buffer containing the encrypted data
4613 * followed by the tag. Return the length of the part preceding the tag in
4614 * *plaintext_length. This is the size of the plaintext in modes where
4615 * the encrypted data has the same size as the plaintext, such as
4616 * CCM and GCM. */
4617static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4618 const uint8_t *ciphertext,
4619 size_t ciphertext_length,
4620 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004621 const uint8_t **p_tag )
4622{
4623 size_t payload_length;
4624 if( tag_length > ciphertext_length )
4625 return( PSA_ERROR_INVALID_ARGUMENT );
4626 payload_length = ciphertext_length - tag_length;
4627 if( payload_length > plaintext_size )
4628 return( PSA_ERROR_BUFFER_TOO_SMALL );
4629 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004630 return( PSA_SUCCESS );
4631}
4632
Gilles Peskinec5487a82018-12-03 18:08:14 +01004633psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004634 psa_algorithm_t alg,
4635 const uint8_t *nonce,
4636 size_t nonce_length,
4637 const uint8_t *additional_data,
4638 size_t additional_data_length,
4639 const uint8_t *ciphertext,
4640 size_t ciphertext_length,
4641 uint8_t *plaintext,
4642 size_t plaintext_size,
4643 size_t *plaintext_length )
4644{
mohammad16035955c982018-04-26 00:53:03 +03004645 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004646 aead_operation_t operation;
4647 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004648
Gilles Peskineee652a32018-06-01 19:23:52 +02004649 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004650
Gilles Peskinec5487a82018-12-03 18:08:14 +01004651 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004652 if( status != PSA_SUCCESS )
4653 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004654
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004655 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4656 ciphertext, ciphertext_length,
4657 plaintext_size, &tag );
4658 if( status != PSA_SUCCESS )
4659 goto exit;
4660
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004661#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004662 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004663 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004664 status = mbedtls_to_psa_error(
4665 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4666 ciphertext_length - operation.tag_length,
4667 nonce, nonce_length,
4668 additional_data,
4669 additional_data_length,
4670 tag, operation.tag_length,
4671 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004672 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004673 else
4674#endif /* MBEDTLS_GCM_C */
4675#if defined(MBEDTLS_CCM_C)
4676 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004677 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004678 status = mbedtls_to_psa_error(
4679 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4680 ciphertext_length - operation.tag_length,
4681 nonce, nonce_length,
4682 additional_data,
4683 additional_data_length,
4684 ciphertext, plaintext,
4685 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004686 }
mohammad160339574652018-06-01 04:39:53 -07004687 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004688#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004689#if defined(MBEDTLS_CHACHAPOLY_C)
4690 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4691 {
4692 if( nonce_length != 12 || operation.tag_length != 16 )
4693 {
4694 status = PSA_ERROR_NOT_SUPPORTED;
4695 goto exit;
4696 }
4697 status = mbedtls_to_psa_error(
4698 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4699 ciphertext_length - operation.tag_length,
4700 nonce,
4701 additional_data,
4702 additional_data_length,
4703 tag,
4704 ciphertext,
4705 plaintext ) );
4706 }
4707 else
4708#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004709 {
mohammad1603554faad2018-06-03 15:07:38 +03004710 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004711 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004712
Gilles Peskineedf9a652018-08-17 18:11:56 +02004713 if( status != PSA_SUCCESS && plaintext_size != 0 )
4714 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004715
Gilles Peskineedf9a652018-08-17 18:11:56 +02004716exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004717 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004718 if( status == PSA_SUCCESS )
4719 *plaintext_length = ciphertext_length - operation.tag_length;
4720 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004721}
4722
Gilles Peskinea0655c32018-04-30 17:06:50 +02004723
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004724
Gilles Peskine20035e32018-02-03 22:44:14 +01004725/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004726/* Generators */
4727/****************************************************************/
4728
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004729#define HKDF_STATE_INIT 0 /* no input yet */
4730#define HKDF_STATE_STARTED 1 /* got salt */
4731#define HKDF_STATE_KEYED 2 /* got key */
4732#define HKDF_STATE_OUTPUT 3 /* output started */
4733
Gilles Peskinecbe66502019-05-16 16:59:18 +02004734static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004735 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004736{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004737 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4738 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004739 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004740 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004741}
4742
4743
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004744psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004745{
4746 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004747 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004748 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004749 {
4750 /* The object has (apparently) been initialized but it is not
4751 * in use. It's ok to call abort on such an object, and there's
4752 * nothing to do. */
4753 }
4754 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004755#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004756 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004757 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004758 mbedtls_free( operation->ctx.hkdf.info );
4759 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004760 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004761 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004762 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004763 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004764 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004765 if( operation->ctx.tls12_prf.seed != NULL )
4766 {
4767 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4768 operation->ctx.tls12_prf.seed_length );
4769 mbedtls_free( operation->ctx.tls12_prf.seed );
4770 }
4771
4772 if( operation->ctx.tls12_prf.label != NULL )
4773 {
4774 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4775 operation->ctx.tls12_prf.label_length );
4776 mbedtls_free( operation->ctx.tls12_prf.label );
4777 }
4778
4779 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4780
4781 /* We leave the fields Ai and output_block to be erased safely by the
4782 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004783 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004784 else
4785#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004786 {
4787 status = PSA_ERROR_BAD_STATE;
4788 }
Janos Follath083036a2019-06-11 10:22:26 +01004789 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004790 return( status );
4791}
4792
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004793psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004794 size_t *capacity)
4795{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004796 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004797 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004798 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004799 return PSA_ERROR_BAD_STATE;
4800 }
4801
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004802 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004803 return( PSA_SUCCESS );
4804}
4805
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004806psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004807 size_t capacity )
4808{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004809 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004810 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004811 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004812 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004813 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004814 return( PSA_SUCCESS );
4815}
4816
Gilles Peskinebef7f142018-07-12 17:22:21 +02004817#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004818/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004819 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004820static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004821 psa_algorithm_t hash_alg,
4822 uint8_t *output,
4823 size_t output_length )
4824{
4825 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4826 psa_status_t status;
4827
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004828 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4829 return( PSA_ERROR_BAD_STATE );
4830 hkdf->state = HKDF_STATE_OUTPUT;
4831
Gilles Peskinebef7f142018-07-12 17:22:21 +02004832 while( output_length != 0 )
4833 {
4834 /* Copy what remains of the current block */
4835 uint8_t n = hash_length - hkdf->offset_in_block;
4836 if( n > output_length )
4837 n = (uint8_t) output_length;
4838 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4839 output += n;
4840 output_length -= n;
4841 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004842 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004843 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004844 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004845 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004846 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004847 * object was corrupted or if this function is called directly
4848 * inside the library. */
4849 if( hkdf->block_number == 0xff )
4850 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004851
4852 /* We need a new block */
4853 ++hkdf->block_number;
4854 hkdf->offset_in_block = 0;
4855 status = psa_hmac_setup_internal( &hkdf->hmac,
4856 hkdf->prk, hash_length,
4857 hash_alg );
4858 if( status != PSA_SUCCESS )
4859 return( status );
4860 if( hkdf->block_number != 1 )
4861 {
4862 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4863 hkdf->output_block,
4864 hash_length );
4865 if( status != PSA_SUCCESS )
4866 return( status );
4867 }
4868 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4869 hkdf->info,
4870 hkdf->info_length );
4871 if( status != PSA_SUCCESS )
4872 return( status );
4873 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4874 &hkdf->block_number, 1 );
4875 if( status != PSA_SUCCESS )
4876 return( status );
4877 status = psa_hmac_finish_internal( &hkdf->hmac,
4878 hkdf->output_block,
4879 sizeof( hkdf->output_block ) );
4880 if( status != PSA_SUCCESS )
4881 return( status );
4882 }
4883
4884 return( PSA_SUCCESS );
4885}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004886
Janos Follath7742fee2019-06-17 12:58:10 +01004887static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4888 psa_tls12_prf_key_derivation_t *tls12_prf,
4889 psa_algorithm_t alg )
4890{
4891 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4892 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004893 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4894 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004895
Janos Follath7742fee2019-06-17 12:58:10 +01004896 /* We can't be wanting more output after block 0xff, otherwise
4897 * the capacity check in psa_key_derivation_output_bytes() would have
4898 * prevented this call. It could happen only if the operation
4899 * object was corrupted or if this function is called directly
4900 * inside the library. */
4901 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004902 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004903
4904 /* We need a new block */
4905 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004906 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004907
4908 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4909 *
4910 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4911 *
4912 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4913 * HMAC_hash(secret, A(2) + seed) +
4914 * HMAC_hash(secret, A(3) + seed) + ...
4915 *
4916 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004917 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004918 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004919 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004920 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004921 * is currently extracted as `output_block` and where i is
4922 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004923 */
4924
Janos Follathea29bfb2019-06-19 12:21:20 +01004925 /* Save the hash context before using it, to preserve the hash state with
4926 * only the inner padding in it. We need this, because inner padding depends
4927 * on the key (secret in the RFC's terminology). */
4928 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4929 if( status != PSA_SUCCESS )
4930 goto cleanup;
4931
4932 /* Calculate A(i) where i = tls12_prf->block_number. */
4933 if( tls12_prf->block_number == 1 )
4934 {
4935 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4936 * the variable seed and in this instance means it in the context of the
4937 * P_hash function, where seed = label + seed.) */
4938 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4939 tls12_prf->label, tls12_prf->label_length );
4940 if( status != PSA_SUCCESS )
4941 goto cleanup;
4942 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4943 tls12_prf->seed, tls12_prf->seed_length );
4944 if( status != PSA_SUCCESS )
4945 goto cleanup;
4946 }
4947 else
4948 {
4949 /* A(i) = HMAC_hash(secret, A(i-1)) */
4950 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4951 tls12_prf->Ai, hash_length );
4952 if( status != PSA_SUCCESS )
4953 goto cleanup;
4954 }
4955
4956 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4957 tls12_prf->Ai, hash_length );
4958 if( status != PSA_SUCCESS )
4959 goto cleanup;
4960 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4961 if( status != PSA_SUCCESS )
4962 goto cleanup;
4963
4964 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
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 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4970 tls12_prf->label, tls12_prf->label_length );
4971 if( status != PSA_SUCCESS )
4972 goto cleanup;
4973 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4974 tls12_prf->seed, tls12_prf->seed_length );
4975 if( status != PSA_SUCCESS )
4976 goto cleanup;
4977 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4978 tls12_prf->output_block, hash_length );
4979 if( status != PSA_SUCCESS )
4980 goto cleanup;
4981 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4982 if( status != PSA_SUCCESS )
4983 goto cleanup;
4984
Janos Follath7742fee2019-06-17 12:58:10 +01004985
4986cleanup:
4987
Janos Follathea29bfb2019-06-19 12:21:20 +01004988 cleanup_status = psa_hash_abort( &backup );
4989 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4990 status = cleanup_status;
4991
4992 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004993}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004994
Janos Follath844eb0e2019-06-19 12:10:49 +01004995static psa_status_t psa_key_derivation_tls12_prf_read(
4996 psa_tls12_prf_key_derivation_t *tls12_prf,
4997 psa_algorithm_t alg,
4998 uint8_t *output,
4999 size_t output_length )
5000{
5001 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5002 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5003 psa_status_t status;
5004 uint8_t offset, length;
5005
5006 while( output_length != 0 )
5007 {
5008 /* Check if we have fully processed the current block. */
5009 if( tls12_prf->left_in_block == 0 )
5010 {
5011 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5012 alg );
5013 if( status != PSA_SUCCESS )
5014 return( status );
5015
5016 continue;
5017 }
5018
5019 if( tls12_prf->left_in_block > output_length )
5020 length = (uint8_t) output_length;
5021 else
5022 length = tls12_prf->left_in_block;
5023
5024 offset = hash_length - tls12_prf->left_in_block;
5025 memcpy( output, tls12_prf->output_block + offset, length );
5026 output += length;
5027 output_length -= length;
5028 tls12_prf->left_in_block -= length;
5029 }
5030
5031 return( PSA_SUCCESS );
5032}
Gilles Peskinebef7f142018-07-12 17:22:21 +02005033#endif /* MBEDTLS_MD_C */
5034
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005035psa_status_t psa_key_derivation_output_bytes(
5036 psa_key_derivation_operation_t *operation,
5037 uint8_t *output,
5038 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005039{
5040 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005041 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005042
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005043 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005044 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005045 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005046 return PSA_ERROR_BAD_STATE;
5047 }
5048
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005049 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005050 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005051 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005052 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005053 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005054 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005055 goto exit;
5056 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005057 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005058 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005059 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005060 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005061 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5062 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005063 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005064 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005065 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005066 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005067 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005068
Gilles Peskinebef7f142018-07-12 17:22:21 +02005069#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005070 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005071 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005072 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005073 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005074 output, output_length );
5075 }
Janos Follathadbec812019-06-14 11:05:39 +01005076 else
Janos Follathadbec812019-06-14 11:05:39 +01005077 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01005078 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005079 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005080 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005081 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005082 output_length );
5083 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005084 else
5085#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005086 {
5087 return( PSA_ERROR_BAD_STATE );
5088 }
5089
5090exit:
5091 if( status != PSA_SUCCESS )
5092 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005093 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005094 * This allows us to differentiate between exhausted operations and
5095 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5096 * operations. */
5097 psa_algorithm_t alg = operation->alg;
5098 psa_key_derivation_abort( operation );
5099 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005100 memset( output, '!', output_length );
5101 }
5102 return( status );
5103}
5104
Gilles Peskine08542d82018-07-19 17:05:42 +02005105#if defined(MBEDTLS_DES_C)
5106static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5107{
5108 if( data_size >= 8 )
5109 mbedtls_des_key_set_parity( data );
5110 if( data_size >= 16 )
5111 mbedtls_des_key_set_parity( data + 8 );
5112 if( data_size >= 24 )
5113 mbedtls_des_key_set_parity( data + 16 );
5114}
5115#endif /* MBEDTLS_DES_C */
5116
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005117static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005118 psa_key_slot_t *slot,
5119 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005120 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005121{
5122 uint8_t *data = NULL;
5123 size_t bytes = PSA_BITS_TO_BYTES( bits );
5124 psa_status_t status;
5125
Gilles Peskine8e338702019-07-30 20:06:31 +02005126 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005127 return( PSA_ERROR_INVALID_ARGUMENT );
5128 if( bits % 8 != 0 )
5129 return( PSA_ERROR_INVALID_ARGUMENT );
5130 data = mbedtls_calloc( 1, bytes );
5131 if( data == NULL )
5132 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5133
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005134 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005135 if( status != PSA_SUCCESS )
5136 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005137#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005138 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005139 psa_des_set_key_parity( data, bytes );
5140#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005141 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005142
5143exit:
5144 mbedtls_free( data );
5145 return( status );
5146}
5147
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005148psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005149 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02005150 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005151{
5152 psa_status_t status;
5153 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005154 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005155
5156 /* Reject any attempt to create a zero-length key so that we don't
5157 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5158 if( psa_get_key_bits( attributes ) == 0 )
5159 return( PSA_ERROR_INVALID_ARGUMENT );
5160
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005161 if( ! operation->can_output_key )
5162 return( PSA_ERROR_NOT_PERMITTED );
5163
Gilles Peskinedf179142019-07-15 22:02:14 +02005164 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
5165 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005166#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5167 if( driver != NULL )
5168 {
5169 /* Deriving a key in a secure element is not implemented yet. */
5170 status = PSA_ERROR_NOT_SUPPORTED;
5171 }
5172#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005173 if( status == PSA_SUCCESS )
5174 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005175 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005176 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005177 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005178 }
5179 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005180 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005181 if( status != PSA_SUCCESS )
5182 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005183 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005184 *handle = 0;
5185 }
5186 return( status );
5187}
5188
Gilles Peskineeab56e42018-07-12 17:12:33 +02005189
5190
5191/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005192/* Key derivation */
5193/****************************************************************/
5194
Gilles Peskine969c5d62019-01-16 15:53:06 +01005195static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005196 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005197 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005198{
Janos Follath5fe19732019-06-20 15:09:30 +01005199 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5200 * initialisers for this union leave some bytes unspecified.) */
5201 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5202
Gilles Peskine969c5d62019-01-16 15:53:06 +01005203 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005204#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005205 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
5206 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5207 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005208 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005209 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005210 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5211 if( hash_size == 0 )
5212 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005213 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5214 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005215 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005216 {
5217 return( PSA_ERROR_NOT_SUPPORTED );
5218 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005219 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005220 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005221 }
5222#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005223 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005224 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005225}
5226
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005227psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005228 psa_algorithm_t alg )
5229{
5230 psa_status_t status;
5231
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005232 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005233 return( PSA_ERROR_BAD_STATE );
5234
Gilles Peskine6843c292019-01-18 16:44:49 +01005235 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5236 return( PSA_ERROR_INVALID_ARGUMENT );
5237 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005238 {
5239 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005240 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005241 }
5242 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5243 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005244 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005245 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005246 else
5247 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005248
5249 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005250 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005251 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005252}
5253
5254#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005255static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005256 psa_algorithm_t hash_alg,
5257 psa_key_derivation_step_t step,
5258 const uint8_t *data,
5259 size_t data_length )
5260{
5261 psa_status_t status;
5262 switch( step )
5263 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005264 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005265 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005266 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005267 status = psa_hmac_setup_internal( &hkdf->hmac,
5268 data, data_length,
5269 hash_alg );
5270 if( status != PSA_SUCCESS )
5271 return( status );
5272 hkdf->state = HKDF_STATE_STARTED;
5273 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005274 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005275 /* If no salt was provided, use an empty salt. */
5276 if( hkdf->state == HKDF_STATE_INIT )
5277 {
5278 status = psa_hmac_setup_internal( &hkdf->hmac,
5279 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005280 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005281 if( status != PSA_SUCCESS )
5282 return( status );
5283 hkdf->state = HKDF_STATE_STARTED;
5284 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005285 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005286 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005287 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5288 data, data_length );
5289 if( status != PSA_SUCCESS )
5290 return( status );
5291 status = psa_hmac_finish_internal( &hkdf->hmac,
5292 hkdf->prk,
5293 sizeof( hkdf->prk ) );
5294 if( status != PSA_SUCCESS )
5295 return( status );
5296 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5297 hkdf->block_number = 0;
5298 hkdf->state = HKDF_STATE_KEYED;
5299 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005300 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005301 if( hkdf->state == HKDF_STATE_OUTPUT )
5302 return( PSA_ERROR_BAD_STATE );
5303 if( hkdf->info_set )
5304 return( PSA_ERROR_BAD_STATE );
5305 hkdf->info_length = data_length;
5306 if( data_length != 0 )
5307 {
5308 hkdf->info = mbedtls_calloc( 1, data_length );
5309 if( hkdf->info == NULL )
5310 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5311 memcpy( hkdf->info, data, data_length );
5312 }
5313 hkdf->info_set = 1;
5314 return( PSA_SUCCESS );
5315 default:
5316 return( PSA_ERROR_INVALID_ARGUMENT );
5317 }
5318}
Janos Follathb03233e2019-06-11 15:30:30 +01005319
Janos Follathf08e2652019-06-13 09:05:41 +01005320static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5321 const uint8_t *data,
5322 size_t data_length )
5323{
5324 if( prf->state != TLS12_PRF_STATE_INIT )
5325 return( PSA_ERROR_BAD_STATE );
5326
Janos Follathd6dce9f2019-07-04 09:11:38 +01005327 if( data_length != 0 )
5328 {
5329 prf->seed = mbedtls_calloc( 1, data_length );
5330 if( prf->seed == NULL )
5331 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005332
Janos Follathd6dce9f2019-07-04 09:11:38 +01005333 memcpy( prf->seed, data, data_length );
5334 prf->seed_length = data_length;
5335 }
Janos Follathf08e2652019-06-13 09:05:41 +01005336
5337 prf->state = TLS12_PRF_STATE_SEED_SET;
5338
5339 return( PSA_SUCCESS );
5340}
5341
Janos Follath81550542019-06-13 14:26:34 +01005342static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5343 psa_algorithm_t hash_alg,
5344 const uint8_t *data,
5345 size_t data_length )
5346{
5347 psa_status_t status;
5348 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5349 return( PSA_ERROR_BAD_STATE );
5350
5351 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5352 if( status != PSA_SUCCESS )
5353 return( status );
5354
5355 prf->state = TLS12_PRF_STATE_KEY_SET;
5356
5357 return( PSA_SUCCESS );
5358}
5359
Janos Follath6660f0e2019-06-17 08:44:03 +01005360static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5361 psa_tls12_prf_key_derivation_t *prf,
5362 psa_algorithm_t hash_alg,
5363 const uint8_t *data,
5364 size_t data_length )
5365{
5366 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005367 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5368 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005369
5370 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5371 return( PSA_ERROR_INVALID_ARGUMENT );
5372
5373 /* Quoting RFC 4279, Section 2:
5374 *
5375 * The premaster secret is formed as follows: if the PSK is N octets
5376 * long, concatenate a uint16 with the value N, N zero octets, a second
5377 * uint16 with the value N, and the PSK itself.
5378 */
5379
Janos Follath40e13932019-06-26 13:22:29 +01005380 *cur++ = ( data_length >> 8 ) & 0xff;
5381 *cur++ = ( data_length >> 0 ) & 0xff;
5382 memset( cur, 0, data_length );
5383 cur += data_length;
5384 *cur++ = pms[0];
5385 *cur++ = pms[1];
5386 memcpy( cur, data, data_length );
5387 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005388
Janos Follath40e13932019-06-26 13:22:29 +01005389 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005390
5391 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5392 return( status );
5393}
5394
Janos Follath63028dd2019-06-13 09:15:47 +01005395static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5396 const uint8_t *data,
5397 size_t data_length )
5398{
5399 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5400 return( PSA_ERROR_BAD_STATE );
5401
Janos Follathd6dce9f2019-07-04 09:11:38 +01005402 if( data_length != 0 )
5403 {
5404 prf->label = mbedtls_calloc( 1, data_length );
5405 if( prf->label == NULL )
5406 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005407
Janos Follathd6dce9f2019-07-04 09:11:38 +01005408 memcpy( prf->label, data, data_length );
5409 prf->label_length = data_length;
5410 }
Janos Follath63028dd2019-06-13 09:15:47 +01005411
5412 prf->state = TLS12_PRF_STATE_LABEL_SET;
5413
5414 return( PSA_SUCCESS );
5415}
5416
Janos Follathb03233e2019-06-11 15:30:30 +01005417static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5418 psa_algorithm_t hash_alg,
5419 psa_key_derivation_step_t step,
5420 const uint8_t *data,
5421 size_t data_length )
5422{
Janos Follathb03233e2019-06-11 15:30:30 +01005423 switch( step )
5424 {
Janos Follathf08e2652019-06-13 09:05:41 +01005425 case PSA_KEY_DERIVATION_INPUT_SEED:
5426 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005427 case PSA_KEY_DERIVATION_INPUT_SECRET:
5428 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005429 case PSA_KEY_DERIVATION_INPUT_LABEL:
5430 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005431 default:
5432 return( PSA_ERROR_INVALID_ARGUMENT );
5433 }
5434}
Janos Follath6660f0e2019-06-17 08:44:03 +01005435
5436static psa_status_t psa_tls12_prf_psk_to_ms_input(
5437 psa_tls12_prf_key_derivation_t *prf,
5438 psa_algorithm_t hash_alg,
5439 psa_key_derivation_step_t step,
5440 const uint8_t *data,
5441 size_t data_length )
5442{
5443 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005444 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005445 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5446 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005447 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005448
5449 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5450}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005451#endif /* MBEDTLS_MD_C */
5452
Gilles Peskineb8965192019-09-24 16:21:10 +02005453/** Check whether the given key type is acceptable for the given
5454 * input step of a key derivation.
5455 *
5456 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5457 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5458 * Both secret and non-secret inputs can alternatively have the type
5459 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5460 * that the input was passed as a buffer rather than via a key object.
5461 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005462static int psa_key_derivation_check_input_type(
5463 psa_key_derivation_step_t step,
5464 psa_key_type_t key_type )
5465{
5466 switch( step )
5467 {
5468 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005469 if( key_type == PSA_KEY_TYPE_DERIVE )
5470 return( PSA_SUCCESS );
5471 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005472 return( PSA_SUCCESS );
5473 break;
5474 case PSA_KEY_DERIVATION_INPUT_LABEL:
5475 case PSA_KEY_DERIVATION_INPUT_SALT:
5476 case PSA_KEY_DERIVATION_INPUT_INFO:
5477 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005478 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5479 return( PSA_SUCCESS );
5480 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005481 return( PSA_SUCCESS );
5482 break;
5483 }
5484 return( PSA_ERROR_INVALID_ARGUMENT );
5485}
5486
Janos Follathb80a94e2019-06-12 15:54:46 +01005487static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005488 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005489 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005490 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005491 const uint8_t *data,
5492 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005493{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005494 psa_status_t status;
5495 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5496
5497 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005498 if( status != PSA_SUCCESS )
5499 goto exit;
5500
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005501#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005502 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005503 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005504 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005505 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005506 step, data, data_length );
5507 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005508 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005509 {
Janos Follathb03233e2019-06-11 15:30:30 +01005510 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5511 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5512 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005513 }
5514 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5515 {
5516 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5517 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5518 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005519 }
5520 else
5521#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005522 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005523 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005524 return( PSA_ERROR_BAD_STATE );
5525 }
5526
Gilles Peskine224b0d62019-09-23 18:13:17 +02005527exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005528 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005529 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005530 return( status );
5531}
5532
Janos Follath51f4a0f2019-06-14 11:35:55 +01005533psa_status_t psa_key_derivation_input_bytes(
5534 psa_key_derivation_operation_t *operation,
5535 psa_key_derivation_step_t step,
5536 const uint8_t *data,
5537 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005538{
Gilles Peskineb8965192019-09-24 16:21:10 +02005539 return( psa_key_derivation_input_internal( operation, step,
5540 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005541 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005542}
5543
Janos Follath51f4a0f2019-06-14 11:35:55 +01005544psa_status_t psa_key_derivation_input_key(
5545 psa_key_derivation_operation_t *operation,
5546 psa_key_derivation_step_t step,
5547 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005548{
5549 psa_key_slot_t *slot;
5550 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005551
Gilles Peskine28f8f302019-07-24 13:30:31 +02005552 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005553 PSA_KEY_USAGE_DERIVE,
5554 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005555 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005556 {
5557 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005558 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005559 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005560
5561 /* Passing a key object as a SECRET input unlocks the permission
5562 * to output to a key object. */
5563 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5564 operation->can_output_key = 1;
5565
Janos Follathb80a94e2019-06-12 15:54:46 +01005566 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005567 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005568 slot->data.key.data,
5569 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005570}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005571
5572
5573
5574/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005575/* Key agreement */
5576/****************************************************************/
5577
Gilles Peskinea05219c2018-11-16 16:02:56 +01005578#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005579static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5580 size_t peer_key_length,
5581 const mbedtls_ecp_keypair *our_key,
5582 uint8_t *shared_secret,
5583 size_t shared_secret_size,
5584 size_t *shared_secret_length )
5585{
Steven Cooremana2371e52020-07-28 14:30:39 +02005586 mbedtls_ecp_keypair *their_key;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005587 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005588 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005589 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005590 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005591 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005592
Steven Cooreman7f391872020-07-30 14:57:44 +02005593 status = psa_load_ecp_representation( peer_key,
5594 peer_key_length,
5595 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
5596 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005597 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005598 goto exit;
5599
Jaeden Amero97271b32019-01-10 19:38:51 +00005600 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005601 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005602 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005603 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005604 status = mbedtls_to_psa_error(
5605 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5606 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005607 goto exit;
5608
Jaeden Amero97271b32019-01-10 19:38:51 +00005609 status = mbedtls_to_psa_error(
5610 mbedtls_ecdh_calc_secret( &ecdh,
5611 shared_secret_length,
5612 shared_secret, shared_secret_size,
5613 mbedtls_ctr_drbg_random,
5614 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005615 if( status != PSA_SUCCESS )
5616 goto exit;
5617 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5618 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005619
5620exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005621 if( status != PSA_SUCCESS )
5622 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005623 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005624 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005625 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005626
Jaeden Amero97271b32019-01-10 19:38:51 +00005627 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005628}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005629#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005630
Gilles Peskine01d718c2018-09-18 12:01:02 +02005631#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5632
Gilles Peskine0216fe12019-04-11 21:23:21 +02005633static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5634 psa_key_slot_t *private_key,
5635 const uint8_t *peer_key,
5636 size_t peer_key_length,
5637 uint8_t *shared_secret,
5638 size_t shared_secret_size,
5639 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005640{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005641 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005642 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005643#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005644 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005645 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005646 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005647 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02005648 psa_status_t status = psa_load_ecp_representation(
5649 private_key->data.key.data,
5650 private_key->data.key.bytes,
5651 private_key->attr.type,
5652 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005653 if( status != PSA_SUCCESS )
5654 return status;
5655 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005656 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005657 shared_secret, shared_secret_size,
5658 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005659 mbedtls_ecp_keypair_free( ecp );
5660 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005661 return status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005662#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005663 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005664 (void) private_key;
5665 (void) peer_key;
5666 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005667 (void) shared_secret;
5668 (void) shared_secret_size;
5669 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005670 return( PSA_ERROR_NOT_SUPPORTED );
5671 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005672}
5673
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005674/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005675 * to potentially free embedded data structures and wipe confidential data.
5676 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005677static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005678 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005679 psa_key_slot_t *private_key,
5680 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005681 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005682{
5683 psa_status_t status;
5684 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5685 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005686 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005687
5688 /* Step 1: run the secret agreement algorithm to generate the shared
5689 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005690 status = psa_key_agreement_raw_internal( ka_alg,
5691 private_key,
5692 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005693 shared_secret,
5694 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005695 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005696 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005697 goto exit;
5698
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005699 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005700 * the shared secret. A shared secret is permitted wherever a key
5701 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005702 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005703 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005704 shared_secret,
5705 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005706
Gilles Peskine12313cd2018-06-20 00:20:32 +02005707exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005708 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005709 return( status );
5710}
5711
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005712psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005713 psa_key_derivation_step_t step,
5714 psa_key_handle_t private_key,
5715 const uint8_t *peer_key,
5716 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005717{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005718 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005719 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005720 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005721 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005722 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005723 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005724 if( status != PSA_SUCCESS )
5725 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005726 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005727 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005728 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005729 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005730 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005731 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005732}
5733
Gilles Peskinebe697d82019-05-16 18:00:41 +02005734psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5735 psa_key_handle_t private_key,
5736 const uint8_t *peer_key,
5737 size_t peer_key_length,
5738 uint8_t *output,
5739 size_t output_size,
5740 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005741{
5742 psa_key_slot_t *slot;
5743 psa_status_t status;
5744
5745 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5746 {
5747 status = PSA_ERROR_INVALID_ARGUMENT;
5748 goto exit;
5749 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005750 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005751 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005752 if( status != PSA_SUCCESS )
5753 goto exit;
5754
5755 status = psa_key_agreement_raw_internal( alg, slot,
5756 peer_key, peer_key_length,
5757 output, output_size,
5758 output_length );
5759
5760exit:
5761 if( status != PSA_SUCCESS )
5762 {
5763 /* If an error happens and is not handled properly, the output
5764 * may be used as a key to protect sensitive data. Arrange for such
5765 * a key to be random, which is likely to result in decryption or
5766 * verification errors. This is better than filling the buffer with
5767 * some constant data such as zeros, which would result in the data
5768 * being protected with a reproducible, easily knowable key.
5769 */
5770 psa_generate_random( output, output_size );
5771 *output_length = output_size;
5772 }
5773 return( status );
5774}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005775
5776
5777/****************************************************************/
5778/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005779/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005780
Gilles Peskine4c317f42018-07-12 01:24:09 +02005781psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005782 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005783{
Janos Follath24eed8d2019-11-22 13:21:35 +00005784 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005785 GUARD_MODULE_INITIALIZED;
5786
Gilles Peskinef181eca2019-08-07 13:49:00 +02005787 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5788 {
5789 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5790 output,
5791 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5792 if( ret != 0 )
5793 return( mbedtls_to_psa_error( ret ) );
5794 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5795 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5796 }
5797
Gilles Peskinee59236f2018-01-27 23:32:46 +01005798 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5799 return( mbedtls_to_psa_error( ret ) );
5800}
5801
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005802#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5803#include "mbedtls/entropy_poll.h"
5804
Gilles Peskine7228da22019-07-15 11:06:15 +02005805psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005806 size_t seed_size )
5807{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005808 if( global_data.initialized )
5809 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005810
5811 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5812 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5813 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5814 return( PSA_ERROR_INVALID_ARGUMENT );
5815
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005816 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005817}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005818#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005819
Gilles Peskinee56e8782019-04-26 17:34:02 +02005820#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5821static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5822 size_t domain_parameters_size,
5823 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005824{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005825 size_t i;
5826 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005827
Gilles Peskinee56e8782019-04-26 17:34:02 +02005828 if( domain_parameters_size == 0 )
5829 {
5830 *exponent = 65537;
5831 return( PSA_SUCCESS );
5832 }
5833
5834 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5835 * support values that fit in a 32-bit integer, which is larger than
5836 * int on just about every platform anyway. */
5837 if( domain_parameters_size > sizeof( acc ) )
5838 return( PSA_ERROR_NOT_SUPPORTED );
5839 for( i = 0; i < domain_parameters_size; i++ )
5840 acc = ( acc << 8 ) | domain_parameters[i];
5841 if( acc > INT_MAX )
5842 return( PSA_ERROR_NOT_SUPPORTED );
5843 *exponent = acc;
5844 return( PSA_SUCCESS );
5845}
5846#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5847
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005848static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005849 psa_key_slot_t *slot, size_t bits,
5850 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005851{
Gilles Peskine8e338702019-07-30 20:06:31 +02005852 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005853
Gilles Peskinee56e8782019-04-26 17:34:02 +02005854 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005855 return( PSA_ERROR_INVALID_ARGUMENT );
5856
Gilles Peskinee59236f2018-01-27 23:32:46 +01005857 if( key_type_is_raw_bytes( type ) )
5858 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005859 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005860
5861 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005862 if( status != PSA_SUCCESS )
5863 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005864
5865 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02005866 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
5867 if( status != PSA_SUCCESS )
5868 return status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005869
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005870 status = psa_generate_random( slot->data.key.data,
5871 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005872 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005873 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005874
5875 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005876#if defined(MBEDTLS_DES_C)
5877 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005878 psa_des_set_key_parity( slot->data.key.data,
5879 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005880#endif /* MBEDTLS_DES_C */
5881 }
5882 else
5883
5884#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005885 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005886 {
Steven Cooremana01795d2020-07-24 22:48:15 +02005887 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005888 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005889 int exponent;
5890 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005891 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5892 return( PSA_ERROR_NOT_SUPPORTED );
5893 /* Accept only byte-aligned keys, for the same reasons as
5894 * in psa_import_rsa_key(). */
5895 if( bits % 8 != 0 )
5896 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005897 status = psa_read_rsa_exponent( domain_parameters,
5898 domain_parameters_size,
5899 &exponent );
5900 if( status != PSA_SUCCESS )
5901 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02005902 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5903 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005904 mbedtls_ctr_drbg_random,
5905 &global_data.ctr_drbg,
5906 (unsigned int) bits,
5907 exponent );
5908 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005909 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02005910
5911 /* Make sure to always have an export representation available */
5912 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
5913
Steven Cooreman75b74362020-07-28 14:30:13 +02005914 status = psa_allocate_buffer_to_slot( slot, bytes );
5915 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02005916 {
5917 mbedtls_rsa_free( &rsa );
Steven Cooreman75b74362020-07-28 14:30:13 +02005918 return status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005919 }
Steven Cooremana01795d2020-07-24 22:48:15 +02005920
5921 status = psa_export_rsa_key( type,
5922 &rsa,
5923 slot->data.key.data,
5924 bytes,
5925 &slot->data.key.bytes );
5926 mbedtls_rsa_free( &rsa );
5927 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02005928 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02005929 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005930 }
5931 else
5932#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5933
5934#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005935 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005936 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01005937 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005938 mbedtls_ecp_group_id grp_id =
5939 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005940 const mbedtls_ecp_curve_info *curve_info =
5941 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02005942 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005943 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005944 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005945 return( PSA_ERROR_NOT_SUPPORTED );
5946 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5947 return( PSA_ERROR_NOT_SUPPORTED );
5948 if( curve_info->bit_size != bits )
5949 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremanacda8342020-07-24 23:09:52 +02005950 mbedtls_ecp_keypair_init( &ecp );
5951 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskinee59236f2018-01-27 23:32:46 +01005952 mbedtls_ctr_drbg_random,
5953 &global_data.ctr_drbg );
5954 if( ret != 0 )
5955 {
Steven Cooremanacda8342020-07-24 23:09:52 +02005956 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005957 return( mbedtls_to_psa_error( ret ) );
5958 }
Steven Cooremanacda8342020-07-24 23:09:52 +02005959
5960
5961 /* Make sure to always have an export representation available */
5962 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02005963 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
5964 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02005965 {
5966 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman75b74362020-07-28 14:30:13 +02005967 return status;
Steven Cooremanacda8342020-07-24 23:09:52 +02005968 }
Steven Cooreman75b74362020-07-28 14:30:13 +02005969
5970 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02005971 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
5972
5973 mbedtls_ecp_keypair_free( &ecp );
5974 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02005975 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02005976 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005977 }
5978 else
5979#endif /* MBEDTLS_ECP_C */
5980
5981 return( PSA_ERROR_NOT_SUPPORTED );
5982
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005983 return( PSA_SUCCESS );
5984}
Darryl Green0c6575a2018-11-07 16:05:30 +00005985
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005986psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005987 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005988{
5989 psa_status_t status;
5990 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005991 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005992
Gilles Peskine0f84d622019-09-12 19:03:13 +02005993 /* Reject any attempt to create a zero-length key so that we don't
5994 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5995 if( psa_get_key_bits( attributes ) == 0 )
5996 return( PSA_ERROR_INVALID_ARGUMENT );
5997
Gilles Peskinedf179142019-07-15 22:02:14 +02005998 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5999 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006000 if( status != PSA_SUCCESS )
6001 goto exit;
6002
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006003#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6004 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00006005 {
Gilles Peskine11792082019-08-06 18:36:36 +02006006 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
6007 size_t pubkey_length = 0; /* We don't support this feature yet */
6008 if( drv->key_management == NULL ||
6009 drv->key_management->p_generate == NULL )
6010 {
6011 status = PSA_ERROR_NOT_SUPPORTED;
6012 goto exit;
6013 }
6014 status = drv->key_management->p_generate(
6015 psa_get_se_driver_context( driver ),
6016 slot->data.se.slot_number, attributes,
6017 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00006018 }
Gilles Peskine11792082019-08-06 18:36:36 +02006019 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02006020#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006021 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006022 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02006023 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02006024 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006025 }
Gilles Peskine11792082019-08-06 18:36:36 +02006026
6027exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006028 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006029 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006030 if( status != PSA_SUCCESS )
6031 {
Gilles Peskine011e4282019-06-26 18:34:38 +02006032 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006033 *handle = 0;
6034 }
Darryl Green0c6575a2018-11-07 16:05:30 +00006035 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006036}
6037
6038
Gilles Peskinee59236f2018-01-27 23:32:46 +01006039
6040/****************************************************************/
6041/* Module setup */
6042/****************************************************************/
6043
Gilles Peskine5e769522018-11-20 21:59:56 +01006044psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6045 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6046 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6047{
6048 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6049 return( PSA_ERROR_BAD_STATE );
6050 global_data.entropy_init = entropy_init;
6051 global_data.entropy_free = entropy_free;
6052 return( PSA_SUCCESS );
6053}
6054
Gilles Peskinee59236f2018-01-27 23:32:46 +01006055void mbedtls_psa_crypto_free( void )
6056{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006057 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006058 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6059 {
6060 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01006061 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006062 }
6063 /* Wipe all remaining data, including configuration.
6064 * In particular, this sets all state indicator to the value
6065 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006066 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006067#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006068 /* Unregister all secure element drivers, so that we restart from
6069 * a pristine state. */
6070 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006071#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006072}
6073
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006074#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6075/** Recover a transaction that was interrupted by a power failure.
6076 *
6077 * This function is called during initialization, before psa_crypto_init()
6078 * returns. If this function returns a failure status, the initialization
6079 * fails.
6080 */
6081static psa_status_t psa_crypto_recover_transaction(
6082 const psa_crypto_transaction_t *transaction )
6083{
6084 switch( transaction->unknown.type )
6085 {
6086 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6087 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006088 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006089 * is implemented.
6090 * https://github.com/ARMmbed/mbed-crypto/issues/218
6091 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006092 default:
6093 /* We found an unsupported transaction in the storage.
6094 * We don't know what state the storage is in. Give up. */
6095 return( PSA_ERROR_STORAGE_FAILURE );
6096 }
6097}
6098#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6099
Gilles Peskinee59236f2018-01-27 23:32:46 +01006100psa_status_t psa_crypto_init( void )
6101{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006102 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006103 const unsigned char drbg_seed[] = "PSA";
6104
Gilles Peskinec6b69072018-11-20 21:42:52 +01006105 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006106 if( global_data.initialized != 0 )
6107 return( PSA_SUCCESS );
6108
Gilles Peskine5e769522018-11-20 21:59:56 +01006109 /* Set default configuration if
6110 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6111 if( global_data.entropy_init == NULL )
6112 global_data.entropy_init = mbedtls_entropy_init;
6113 if( global_data.entropy_free == NULL )
6114 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006115
Gilles Peskinec6b69072018-11-20 21:42:52 +01006116 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01006117 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01006118#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6119 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6120 /* The PSA entropy injection feature depends on using NV seed as an entropy
6121 * source. Add NV seed as an entropy source for PSA entropy injection. */
6122 mbedtls_entropy_add_source( &global_data.entropy,
6123 mbedtls_nv_seed_poll, NULL,
6124 MBEDTLS_ENTROPY_BLOCK_SIZE,
6125 MBEDTLS_ENTROPY_SOURCE_STRONG );
6126#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01006127 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006128 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01006129 status = mbedtls_to_psa_error(
6130 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
6131 mbedtls_entropy_func,
6132 &global_data.entropy,
6133 drbg_seed, sizeof( drbg_seed ) - 1 ) );
6134 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006135 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006136 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006137
Gilles Peskine66fb1262018-12-10 16:29:04 +01006138 status = psa_initialize_key_slots( );
6139 if( status != PSA_SUCCESS )
6140 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006141
Gilles Peskined9348f22019-10-01 15:22:29 +02006142#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6143 status = psa_init_all_se_drivers( );
6144 if( status != PSA_SUCCESS )
6145 goto exit;
6146#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6147
Gilles Peskine4b734222019-07-24 15:56:31 +02006148#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006149 status = psa_crypto_load_transaction( );
6150 if( status == PSA_SUCCESS )
6151 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006152 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6153 if( status != PSA_SUCCESS )
6154 goto exit;
6155 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006156 }
6157 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6158 {
6159 /* There's no transaction to complete. It's all good. */
6160 status = PSA_SUCCESS;
6161 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006162#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006163
Gilles Peskinec6b69072018-11-20 21:42:52 +01006164 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006165 global_data.initialized = 1;
6166
6167exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006168 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006169 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006170 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006171}
6172
6173#endif /* MBEDTLS_PSA_CRYPTO_C */