blob: 4c3966ca7a089c5c4028dce7eed10b370af0b3b2 [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)
Gilles Peskine5055b232019-12-12 17:49:31 +0100375mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve,
376 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200377{
378 switch( curve )
379 {
Gilles Peskine228abc52019-12-03 17:24:19 +0100380 case PSA_ECC_CURVE_SECP_R1:
381 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
398 case PSA_ECC_CURVE_BRAINPOOL_P_R1:
399 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
412 case PSA_ECC_CURVE_MONTGOMERY:
413 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
424 case PSA_ECC_CURVE_SECP_K1:
425 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
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200444static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
445 size_t bits,
446 struct raw_data *raw )
447{
448 /* Check that the bit size is acceptable for the key type */
449 switch( type )
450 {
451 case PSA_KEY_TYPE_RAW_DATA:
452#if defined(MBEDTLS_MD_C)
453 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200454#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200455 case PSA_KEY_TYPE_DERIVE:
456 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200457#if defined(MBEDTLS_AES_C)
458 case PSA_KEY_TYPE_AES:
459 if( bits != 128 && bits != 192 && bits != 256 )
460 return( PSA_ERROR_INVALID_ARGUMENT );
461 break;
462#endif
463#if defined(MBEDTLS_CAMELLIA_C)
464 case PSA_KEY_TYPE_CAMELLIA:
465 if( bits != 128 && bits != 192 && bits != 256 )
466 return( PSA_ERROR_INVALID_ARGUMENT );
467 break;
468#endif
469#if defined(MBEDTLS_DES_C)
470 case PSA_KEY_TYPE_DES:
471 if( bits != 64 && bits != 128 && bits != 192 )
472 return( PSA_ERROR_INVALID_ARGUMENT );
473 break;
474#endif
475#if defined(MBEDTLS_ARC4_C)
476 case PSA_KEY_TYPE_ARC4:
477 if( bits < 8 || bits > 2048 )
478 return( PSA_ERROR_INVALID_ARGUMENT );
479 break;
480#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200481#if defined(MBEDTLS_CHACHA20_C)
482 case PSA_KEY_TYPE_CHACHA20:
483 if( bits != 256 )
484 return( PSA_ERROR_INVALID_ARGUMENT );
485 break;
486#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200487 default:
488 return( PSA_ERROR_NOT_SUPPORTED );
489 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200490 if( bits % 8 != 0 )
491 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200492
493 /* Allocate memory for the key */
494 raw->bytes = PSA_BITS_TO_BYTES( bits );
495 raw->data = mbedtls_calloc( 1, raw->bytes );
496 if( raw->data == NULL )
497 {
498 raw->bytes = 0;
499 return( PSA_ERROR_INSUFFICIENT_MEMORY );
500 }
501 return( PSA_SUCCESS );
502}
503
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200504#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100505/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
506 * that are not a multiple of 8) well. For example, there is only
507 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
508 * way to return the exact bit size of a key.
509 * To keep things simple, reject non-byte-aligned key sizes. */
510static psa_status_t psa_check_rsa_key_byte_aligned(
511 const mbedtls_rsa_context *rsa )
512{
513 mbedtls_mpi n;
514 psa_status_t status;
515 mbedtls_mpi_init( &n );
516 status = mbedtls_to_psa_error(
517 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
518 if( status == PSA_SUCCESS )
519 {
520 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
521 status = PSA_ERROR_NOT_SUPPORTED;
522 }
523 mbedtls_mpi_free( &n );
524 return( status );
525}
526
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000527static psa_status_t psa_import_rsa_key( psa_key_type_t type,
528 const uint8_t *data,
529 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200530 mbedtls_rsa_context **p_rsa )
531{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000532 psa_status_t status;
533 mbedtls_pk_context pk;
534 mbedtls_rsa_context *rsa;
535 size_t bits;
536
537 mbedtls_pk_init( &pk );
538
539 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200540 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000541 status = mbedtls_to_psa_error(
542 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200543 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000544 status = mbedtls_to_psa_error(
545 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
546 if( status != PSA_SUCCESS )
547 goto exit;
548
549 /* We have something that the pkparse module recognizes. If it is a
550 * valid RSA key, store it. */
551 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200552 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000553 status = PSA_ERROR_INVALID_ARGUMENT;
554 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200555 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000556
557 rsa = mbedtls_pk_rsa( pk );
558 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
559 * supports non-byte-aligned key sizes, but not well. For example,
560 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
561 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
562 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
563 {
564 status = PSA_ERROR_NOT_SUPPORTED;
565 goto exit;
566 }
567 status = psa_check_rsa_key_byte_aligned( rsa );
568
569exit:
570 /* Free the content of the pk object only on error. */
571 if( status != PSA_SUCCESS )
572 {
573 mbedtls_pk_free( &pk );
574 return( status );
575 }
576
577 /* On success, store the content of the object in the RSA context. */
578 *p_rsa = rsa;
579
580 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200581}
582#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
583
Jaeden Ameroccdce902019-01-10 11:42:27 +0000584#if defined(MBEDTLS_ECP_C)
Gilles Peskine4cd32772019-12-02 20:49:42 +0100585static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve,
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100586 size_t data_length,
587 int is_public,
Gilles Peskine4cd32772019-12-02 20:49:42 +0100588 mbedtls_ecp_keypair **p_ecp )
589{
590 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
591 *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
592 if( *p_ecp == NULL )
593 return( PSA_ERROR_INSUFFICIENT_MEMORY );
594 mbedtls_ecp_keypair_init( *p_ecp );
595
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100596 if( is_public )
597 {
598 /* A public key is represented as:
599 * - The byte 0x04;
600 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
601 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
602 * So its data length is 2m+1 where n is the key size in bits.
603 */
604 if( ( data_length & 1 ) == 0 )
605 return( PSA_ERROR_INVALID_ARGUMENT );
606 data_length = data_length / 2;
607 }
608
Gilles Peskine4cd32772019-12-02 20:49:42 +0100609 /* Load the group. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100610 grp_id = mbedtls_ecc_group_of_psa( curve, data_length );
611 if( grp_id == MBEDTLS_ECP_DP_NONE )
612 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100613 return( mbedtls_to_psa_error(
614 mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) );
615}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000616
617/* Import a public key given as the uncompressed representation defined by SEC1
618 * 2.3.3 as the content of an ECPoint. */
619static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
620 const uint8_t *data,
621 size_t data_length,
622 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200623{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200624 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000625 mbedtls_ecp_keypair *ecp = NULL;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000626
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100627 status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp );
Jaeden Ameroccdce902019-01-10 11:42:27 +0000628 if( status != PSA_SUCCESS )
629 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100630
Jaeden Ameroccdce902019-01-10 11:42:27 +0000631 /* Load the public value. */
632 status = mbedtls_to_psa_error(
633 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
634 data, data_length ) );
635 if( status != PSA_SUCCESS )
636 goto exit;
637
638 /* Check that the point is on the curve. */
639 status = mbedtls_to_psa_error(
640 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
641 if( status != PSA_SUCCESS )
642 goto exit;
643
644 *p_ecp = ecp;
645 return( PSA_SUCCESS );
646
647exit:
648 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200649 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000650 mbedtls_ecp_keypair_free( ecp );
651 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200652 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000653 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200654}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200655
Gilles Peskinef76aa772018-10-29 19:24:33 +0100656/* Import a private key given as a byte string which is the private value
657 * in big-endian order. */
658static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
659 const uint8_t *data,
660 size_t data_length,
661 mbedtls_ecp_keypair **p_ecp )
662{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200663 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100664 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100665
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100666 status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100667 if( status != PSA_SUCCESS )
668 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100669
Gilles Peskinef76aa772018-10-29 19:24:33 +0100670 /* Load the secret value. */
671 status = mbedtls_to_psa_error(
672 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
673 if( status != PSA_SUCCESS )
674 goto exit;
675 /* Validate the private key. */
676 status = mbedtls_to_psa_error(
677 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
678 if( status != PSA_SUCCESS )
679 goto exit;
680 /* Calculate the public key from the private key. */
681 status = mbedtls_to_psa_error(
682 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
683 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
684 if( status != PSA_SUCCESS )
685 goto exit;
686
687 *p_ecp = ecp;
688 return( PSA_SUCCESS );
689
690exit:
691 if( ecp != NULL )
692 {
693 mbedtls_ecp_keypair_free( ecp );
694 mbedtls_free( ecp );
695 }
696 return( status );
697}
698#endif /* defined(MBEDTLS_ECP_C) */
699
Gilles Peskineb46bef22019-07-30 21:32:04 +0200700
701/** Return the size of the key in the given slot, in bits.
702 *
703 * \param[in] slot A key slot.
704 *
705 * \return The key size in bits, read from the metadata in the slot.
706 */
707static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
708{
709 return( slot->attr.bits );
710}
711
712/** Calculate the size of the key in the given slot, in bits.
713 *
714 * \param[in] slot A key slot containing a transparent key.
715 *
716 * \return The key size in bits, calculated from the key data.
717 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200718static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200719{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200720 size_t bits = 0; /* return 0 on an empty slot */
721
Gilles Peskineb46bef22019-07-30 21:32:04 +0200722 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200723 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200724#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200725 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
726 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200727#endif /* defined(MBEDTLS_RSA_C) */
728#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200729 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
730 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200731#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200732
733 /* We know that the size fits in psa_key_bits_t thanks to checks
734 * when the key was created. */
735 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200736}
737
Gilles Peskine8e338702019-07-30 20:06:31 +0200738/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100739 * previously. This function assumes that the slot does not contain
740 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100741psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
742 const uint8_t *data,
743 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100744{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200745 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746
Gilles Peskine8e338702019-07-30 20:06:31 +0200747 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100748 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200749 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200750 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100751 if( data_length > SIZE_MAX / 8 )
752 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200753 /* Enforce a size limit, and in particular ensure that the bit
754 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200755 if( bit_size > PSA_MAX_KEY_BITS )
756 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200757 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200758 &slot->data.raw );
759 if( status != PSA_SUCCESS )
760 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200761 if( data_length != 0 )
762 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100763 }
764 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100765#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200766 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100767 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200768 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100769 data, data_length,
770 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100771 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200772 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100773 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000774 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200775 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000776 data, data_length,
777 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100778 }
779 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100780#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000781#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200782 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100783 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200784 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000785 data, data_length,
786 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100787 }
788 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000789#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100790 {
791 return( PSA_ERROR_NOT_SUPPORTED );
792 }
Darryl Green940d72c2018-07-13 13:18:51 +0100793
Gilles Peskineb46bef22019-07-30 21:32:04 +0200794 if( status == PSA_SUCCESS )
795 {
796 /* Write the actual key size to the slot.
797 * psa_start_key_creation() wrote the size declared by the
798 * caller, which may be 0 (meaning unspecified) or wrong. */
799 slot->attr.bits = psa_calculate_key_bits( slot );
800 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100801 return( status );
802}
803
Gilles Peskinef603c712019-01-19 13:40:11 +0100804/** Calculate the intersection of two algorithm usage policies.
805 *
806 * Return 0 (which allows no operation) on incompatibility.
807 */
808static psa_algorithm_t psa_key_policy_algorithm_intersection(
809 psa_algorithm_t alg1,
810 psa_algorithm_t alg2 )
811{
Gilles Peskine549ea862019-05-22 11:45:59 +0200812 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100813 if( alg1 == alg2 )
814 return( alg1 );
815 /* If the policies are from the same hash-and-sign family, check
816 * if one is a wildcard. If so the other has the specific algorithm. */
817 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
818 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
819 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
820 {
821 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
822 return( alg2 );
823 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
824 return( alg1 );
825 }
826 /* If the policies are incompatible, allow nothing. */
827 return( 0 );
828}
829
Gilles Peskined6f371b2019-05-10 19:33:38 +0200830static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
831 psa_algorithm_t requested_alg )
832{
Gilles Peskine549ea862019-05-22 11:45:59 +0200833 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200834 if( requested_alg == policy_alg )
835 return( 1 );
836 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200837 * and requested_alg is the same hash-and-sign family with any hash,
838 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200839 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
840 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
841 {
842 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
843 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
844 }
845 /* If it isn't permitted, it's forbidden. */
846 return( 0 );
847}
848
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100849/** Test whether a policy permits an algorithm.
850 *
851 * The caller must test usage flags separately.
852 */
853static int psa_key_policy_permits( const psa_key_policy_t *policy,
854 psa_algorithm_t alg )
855{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200856 return( psa_key_algorithm_permits( policy->alg, alg ) ||
857 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100858}
859
Gilles Peskinef603c712019-01-19 13:40:11 +0100860/** Restrict a key policy based on a constraint.
861 *
862 * \param[in,out] policy The policy to restrict.
863 * \param[in] constraint The policy constraint to apply.
864 *
865 * \retval #PSA_SUCCESS
866 * \c *policy contains the intersection of the original value of
867 * \c *policy and \c *constraint.
868 * \retval #PSA_ERROR_INVALID_ARGUMENT
869 * \c *policy and \c *constraint are incompatible.
870 * \c *policy is unchanged.
871 */
872static psa_status_t psa_restrict_key_policy(
873 psa_key_policy_t *policy,
874 const psa_key_policy_t *constraint )
875{
876 psa_algorithm_t intersection_alg =
877 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200878 psa_algorithm_t intersection_alg2 =
879 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100880 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
881 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200882 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
883 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100884 policy->usage &= constraint->usage;
885 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200886 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100887 return( PSA_SUCCESS );
888}
889
Darryl Green06fd18d2018-07-16 11:21:11 +0100890/** Retrieve a slot which must contain a key. The key must have allow all the
891 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
892 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100893static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100894 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100895 psa_key_usage_t usage,
896 psa_algorithm_t alg )
897{
898 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100899 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100900
901 *p_slot = NULL;
902
Gilles Peskinec5487a82018-12-03 18:08:14 +0100903 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100904 if( status != PSA_SUCCESS )
905 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100906
907 /* Enforce that usage policy for the key slot contains all the flags
908 * required by the usage parameter. There is one exception: public
909 * keys can always be exported, so we treat public key objects as
910 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200911 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100912 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200913 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100914 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100915
916 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200917 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100918 return( PSA_ERROR_NOT_PERMITTED );
919
920 *p_slot = slot;
921 return( PSA_SUCCESS );
922}
Darryl Green940d72c2018-07-13 13:18:51 +0100923
Gilles Peskine28f8f302019-07-24 13:30:31 +0200924/** Retrieve a slot which must contain a transparent key.
925 *
926 * A transparent key is a key for which the key material is directly
927 * available, as opposed to a key in a secure element.
928 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200929 * This is a temporary function to use instead of psa_get_key_from_slot()
930 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200931 */
932#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
933static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
934 psa_key_slot_t **p_slot,
935 psa_key_usage_t usage,
936 psa_algorithm_t alg )
937{
938 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
939 if( status != PSA_SUCCESS )
940 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200941 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200942 {
943 *p_slot = NULL;
944 return( PSA_ERROR_NOT_SUPPORTED );
945 }
946 return( PSA_SUCCESS );
947}
948#else /* MBEDTLS_PSA_CRYPTO_SE_C */
949/* With no secure element support, all keys are transparent. */
950#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
951 psa_get_key_from_slot( handle, p_slot, usage, alg )
952#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
953
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100954/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100955static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000956{
Gilles Peskine73167e12019-07-12 23:44:37 +0200957#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
958 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000959 {
960 /* No key material to clean. */
961 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200962 else
963#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200964 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000965 {
966 /* No key material to clean. */
967 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200968 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000969 {
970 mbedtls_free( slot->data.raw.data );
971 }
972 else
973#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200974 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000975 {
976 mbedtls_rsa_free( slot->data.rsa );
977 mbedtls_free( slot->data.rsa );
978 }
979 else
980#endif /* defined(MBEDTLS_RSA_C) */
981#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200982 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000983 {
984 mbedtls_ecp_keypair_free( slot->data.ecp );
985 mbedtls_free( slot->data.ecp );
986 }
987 else
988#endif /* defined(MBEDTLS_ECP_C) */
989 {
990 /* Shouldn't happen: the key type is not any type that we
991 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200992 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000993 }
994
995 return( PSA_SUCCESS );
996}
997
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100998/** Completely wipe a slot in memory, including its policy.
999 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001000psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001001{
1002 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001003 /* Multipart operations may still be using the key. This is safe
1004 * because all multipart operation objects are independent from
1005 * the key slot: if they need to access the key after the setup
1006 * phase, they have a copy of the key. Note that this means that
1007 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001008 /* At this point, key material and other type-specific content has
1009 * been wiped. Clear remaining metadata. We can call memset and not
1010 * zeroize because the metadata is not particularly sensitive. */
1011 memset( slot, 0, sizeof( *slot ) );
1012 return( status );
1013}
1014
Gilles Peskinec5487a82018-12-03 18:08:14 +01001015psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001016{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001017 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001018 psa_status_t status; /* status of the last operation */
1019 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001020#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1021 psa_se_drv_table_entry_t *driver;
1022#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001023
Gilles Peskine1841cf42019-10-08 15:48:25 +02001024 if( handle == 0 )
1025 return( PSA_SUCCESS );
1026
Gilles Peskinec5487a82018-12-03 18:08:14 +01001027 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001028 if( status != PSA_SUCCESS )
1029 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001030
1031#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001032 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001033 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001034 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001035 /* For a key in a secure element, we need to do three things:
1036 * remove the key file in internal storage, destroy the
1037 * key inside the secure element, and update the driver's
1038 * persistent data. Start a transaction that will encompass these
1039 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001040 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001041 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001042 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001043 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001044 status = psa_crypto_save_transaction( );
1045 if( status != PSA_SUCCESS )
1046 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001047 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001048 /* We should still try to destroy the key in the secure
1049 * element and the key metadata in storage. This is especially
1050 * important if the error is that the storage is full.
1051 * But how to do it exactly without risking an inconsistent
1052 * state after a reset?
1053 * https://github.com/ARMmbed/mbed-crypto/issues/215
1054 */
1055 overall_status = status;
1056 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001057 }
1058
Gilles Peskine354f7672019-07-12 23:46:38 +02001059 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001060 if( overall_status == PSA_SUCCESS )
1061 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001062 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001063#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1064
Darryl Greend49a4992018-06-18 17:27:26 +01001065#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001066 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001067 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001068 status = psa_destroy_persistent_key( slot->attr.id );
1069 if( overall_status == PSA_SUCCESS )
1070 overall_status = status;
1071
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001072 /* TODO: other slots may have a copy of the same key. We should
1073 * invalidate them.
1074 * https://github.com/ARMmbed/mbed-crypto/issues/214
1075 */
Darryl Greend49a4992018-06-18 17:27:26 +01001076 }
1077#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001078
Gilles Peskinefc762652019-07-22 19:30:34 +02001079#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1080 if( driver != NULL )
1081 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001082 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001083 if( overall_status == PSA_SUCCESS )
1084 overall_status = status;
1085 status = psa_crypto_stop_transaction( );
1086 if( overall_status == PSA_SUCCESS )
1087 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001088 }
1089#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1090
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001091#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1092exit:
1093#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001094 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001095 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1096 if( overall_status == PSA_SUCCESS )
1097 overall_status = status;
1098 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001099}
1100
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001101void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001102{
Gilles Peskineb699f072019-04-26 16:06:02 +02001103 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001104 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001105}
1106
Gilles Peskineb699f072019-04-26 16:06:02 +02001107psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1108 psa_key_type_t type,
1109 const uint8_t *data,
1110 size_t data_length )
1111{
1112 uint8_t *copy = NULL;
1113
1114 if( data_length != 0 )
1115 {
1116 copy = mbedtls_calloc( 1, data_length );
1117 if( copy == NULL )
1118 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1119 memcpy( copy, data, data_length );
1120 }
1121 /* After this point, this function is guaranteed to succeed, so it
1122 * can start modifying `*attributes`. */
1123
1124 if( attributes->domain_parameters != NULL )
1125 {
1126 mbedtls_free( attributes->domain_parameters );
1127 attributes->domain_parameters = NULL;
1128 attributes->domain_parameters_size = 0;
1129 }
1130
1131 attributes->domain_parameters = copy;
1132 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001133 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001134 return( PSA_SUCCESS );
1135}
1136
1137psa_status_t psa_get_key_domain_parameters(
1138 const psa_key_attributes_t *attributes,
1139 uint8_t *data, size_t data_size, size_t *data_length )
1140{
1141 if( attributes->domain_parameters_size > data_size )
1142 return( PSA_ERROR_BUFFER_TOO_SMALL );
1143 *data_length = attributes->domain_parameters_size;
1144 if( attributes->domain_parameters_size != 0 )
1145 memcpy( data, attributes->domain_parameters,
1146 attributes->domain_parameters_size );
1147 return( PSA_SUCCESS );
1148}
1149
1150#if defined(MBEDTLS_RSA_C)
1151static psa_status_t psa_get_rsa_public_exponent(
1152 const mbedtls_rsa_context *rsa,
1153 psa_key_attributes_t *attributes )
1154{
1155 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001156 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001157 uint8_t *buffer = NULL;
1158 size_t buflen;
1159 mbedtls_mpi_init( &mpi );
1160
1161 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1162 if( ret != 0 )
1163 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001164 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1165 {
1166 /* It's the default value, which is reported as an empty string,
1167 * so there's nothing to do. */
1168 goto exit;
1169 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001170
1171 buflen = mbedtls_mpi_size( &mpi );
1172 buffer = mbedtls_calloc( 1, buflen );
1173 if( buffer == NULL )
1174 {
1175 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1176 goto exit;
1177 }
1178 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1179 if( ret != 0 )
1180 goto exit;
1181 attributes->domain_parameters = buffer;
1182 attributes->domain_parameters_size = buflen;
1183
1184exit:
1185 mbedtls_mpi_free( &mpi );
1186 if( ret != 0 )
1187 mbedtls_free( buffer );
1188 return( mbedtls_to_psa_error( ret ) );
1189}
1190#endif /* MBEDTLS_RSA_C */
1191
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001192/** Retrieve all the publicly-accessible attributes of a key.
1193 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001194psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1195 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001196{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001197 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001198 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001199
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001200 psa_reset_key_attributes( attributes );
1201
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001202 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001203 if( status != PSA_SUCCESS )
1204 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001205
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001206 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001207 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1208 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1209
1210#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1211 if( psa_key_slot_is_external( slot ) )
1212 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1213#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001214
Gilles Peskine8e338702019-07-30 20:06:31 +02001215 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001216 {
1217#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001218 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001219 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001220#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001221 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001222 * is not yet implemented.
1223 * https://github.com/ARMmbed/mbed-crypto/issues/216
1224 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001225 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001226 break;
1227#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001228 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1229 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001230#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001231 default:
1232 /* Nothing else to do. */
1233 break;
1234 }
1235
1236 if( status != PSA_SUCCESS )
1237 psa_reset_key_attributes( attributes );
1238 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001239}
1240
Gilles Peskinec8000c02019-08-02 20:15:51 +02001241#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1242psa_status_t psa_get_key_slot_number(
1243 const psa_key_attributes_t *attributes,
1244 psa_key_slot_number_t *slot_number )
1245{
1246 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1247 {
1248 *slot_number = attributes->slot_number;
1249 return( PSA_SUCCESS );
1250 }
1251 else
1252 return( PSA_ERROR_INVALID_ARGUMENT );
1253}
1254#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1255
Jaeden Ameroccdce902019-01-10 11:42:27 +00001256#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001257static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1258 unsigned char *buf, size_t size )
1259{
Janos Follath24eed8d2019-11-22 13:21:35 +00001260 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001261 unsigned char *c;
1262 size_t len = 0;
1263
1264 c = buf + size;
1265
1266 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1267
1268 return( (int) len );
1269}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001270#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001271
Gilles Peskinef603c712019-01-19 13:40:11 +01001272static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1273 uint8_t *data,
1274 size_t data_size,
1275 size_t *data_length,
1276 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001277{
Gilles Peskine5d309672019-07-12 23:47:28 +02001278#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1279 const psa_drv_se_t *drv;
1280 psa_drv_se_context_t *drv_context;
1281#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1282
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001283 *data_length = 0;
1284
Gilles Peskine8e338702019-07-30 20:06:31 +02001285 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001286 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001287
Gilles Peskinef9168942019-09-12 19:20:29 +02001288 /* Reject a zero-length output buffer now, since this can never be a
1289 * valid key representation. This way we know that data must be a valid
1290 * pointer and we can do things like memset(data, ..., data_size). */
1291 if( data_size == 0 )
1292 return( PSA_ERROR_BUFFER_TOO_SMALL );
1293
Gilles Peskine5d309672019-07-12 23:47:28 +02001294#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001295 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001296 {
1297 psa_drv_se_export_key_t method;
1298 if( drv->key_management == NULL )
1299 return( PSA_ERROR_NOT_SUPPORTED );
1300 method = ( export_public_key ?
1301 drv->key_management->p_export_public :
1302 drv->key_management->p_export );
1303 if( method == NULL )
1304 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001305 return( method( drv_context,
1306 slot->data.se.slot_number,
1307 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001308 }
1309#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1310
Gilles Peskine8e338702019-07-30 20:06:31 +02001311 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001312 {
1313 if( slot->data.raw.bytes > data_size )
1314 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001315 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1316 memset( data + slot->data.raw.bytes, 0,
1317 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001318 *data_length = slot->data.raw.bytes;
1319 return( PSA_SUCCESS );
1320 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001321#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001322 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001323 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001324 psa_status_t status;
1325
Gilles Peskineb46bef22019-07-30 21:32:04 +02001326 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001327 if( bytes > data_size )
1328 return( PSA_ERROR_BUFFER_TOO_SMALL );
1329 status = mbedtls_to_psa_error(
1330 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1331 if( status != PSA_SUCCESS )
1332 return( status );
1333 memset( data + bytes, 0, data_size - bytes );
1334 *data_length = bytes;
1335 return( PSA_SUCCESS );
1336 }
1337#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001338 else
Moran Peker17e36e12018-05-02 12:55:20 +03001339 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001340#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001341 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1342 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001343 {
Moran Pekera998bc62018-04-16 18:16:20 +03001344 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001345 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001346 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001347 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001348#if defined(MBEDTLS_RSA_C)
1349 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001350 pk.pk_info = &mbedtls_rsa_info;
1351 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001352#else
1353 return( PSA_ERROR_NOT_SUPPORTED );
1354#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001355 }
1356 else
1357 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001358#if defined(MBEDTLS_ECP_C)
1359 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001360 pk.pk_info = &mbedtls_eckey_info;
1361 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001362#else
1363 return( PSA_ERROR_NOT_SUPPORTED );
1364#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001365 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001366 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001367 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001368 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001369 }
Moran Peker17e36e12018-05-02 12:55:20 +03001370 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001371 {
Moran Peker17e36e12018-05-02 12:55:20 +03001372 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001373 }
Moran Peker60364322018-04-29 11:34:58 +03001374 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001375 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001376 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001377 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001378 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001379 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1380 * Move the data to the beginning and erase remaining data
1381 * at the original location. */
1382 if( 2 * (size_t) ret <= data_size )
1383 {
1384 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001385 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001386 }
1387 else if( (size_t) ret < data_size )
1388 {
1389 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001390 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001391 }
Moran Pekera998bc62018-04-16 18:16:20 +03001392 *data_length = ret;
1393 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001394 }
1395 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001396#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001397 {
1398 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001399 it is valid for a special-purpose implementation to omit
1400 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001401 return( PSA_ERROR_NOT_SUPPORTED );
1402 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001403 }
1404}
1405
Gilles Peskinec5487a82018-12-03 18:08:14 +01001406psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001407 uint8_t *data,
1408 size_t data_size,
1409 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001410{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001411 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001412 psa_status_t status;
1413
1414 /* Set the key to empty now, so that even when there are errors, we always
1415 * set data_length to a value between 0 and data_size. On error, setting
1416 * the key to empty is a good choice because an empty key representation is
1417 * unlikely to be accepted anywhere. */
1418 *data_length = 0;
1419
1420 /* Export requires the EXPORT flag. There is an exception for public keys,
1421 * which don't require any flag, but psa_get_key_from_slot takes
1422 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001423 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001424 if( status != PSA_SUCCESS )
1425 return( status );
1426 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001427 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001428}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001429
Gilles Peskinec5487a82018-12-03 18:08:14 +01001430psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001431 uint8_t *data,
1432 size_t data_size,
1433 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001434{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001435 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001436 psa_status_t status;
1437
1438 /* Set the key to empty now, so that even when there are errors, we always
1439 * set data_length to a value between 0 and data_size. On error, setting
1440 * the key to empty is a good choice because an empty key representation is
1441 * unlikely to be accepted anywhere. */
1442 *data_length = 0;
1443
1444 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001445 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001446 if( status != PSA_SUCCESS )
1447 return( status );
1448 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001449 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001450}
1451
Gilles Peskine91e8c332019-08-02 19:19:39 +02001452#if defined(static_assert)
1453static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1454 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001455static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001456 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001457static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001458 "One or more key attribute flag is listed as both internal-only and external-only" );
1459#endif
1460
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001461/** Validate that a key policy is internally well-formed.
1462 *
1463 * This function only rejects invalid policies. It does not validate the
1464 * consistency of the policy with respect to other attributes of the key
1465 * such as the key type.
1466 */
1467static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001468{
1469 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001470 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001471 PSA_KEY_USAGE_ENCRYPT |
1472 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001473 PSA_KEY_USAGE_SIGN_HASH |
1474 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001475 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1476 return( PSA_ERROR_INVALID_ARGUMENT );
1477
Gilles Peskine4747d192019-04-17 15:05:45 +02001478 return( PSA_SUCCESS );
1479}
1480
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001481/** Validate the internal consistency of key attributes.
1482 *
1483 * This function only rejects invalid attribute values. If does not
1484 * validate the consistency of the attributes with any key data that may
1485 * be involved in the creation of the key.
1486 *
1487 * Call this function early in the key creation process.
1488 *
1489 * \param[in] attributes Key attributes for the new key.
1490 * \param[out] p_drv On any return, the driver for the key, if any.
1491 * NULL for a transparent key.
1492 *
1493 */
1494static psa_status_t psa_validate_key_attributes(
1495 const psa_key_attributes_t *attributes,
1496 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001497{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001498 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001499
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001500 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1501 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001502 if( status != PSA_SUCCESS )
1503 return( status );
1504
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001505 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1506 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001507 if( status != PSA_SUCCESS )
1508 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001509
1510 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001511 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001512 return( status );
1513
1514 /* Refuse to create overly large keys.
1515 * Note that this doesn't trigger on import if the attributes don't
1516 * explicitly specify a size (so psa_get_key_bits returns 0), so
1517 * psa_import_key() needs its own checks. */
1518 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1519 return( PSA_ERROR_NOT_SUPPORTED );
1520
Gilles Peskine91e8c332019-08-02 19:19:39 +02001521 /* Reject invalid flags. These should not be reachable through the API. */
1522 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1523 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1524 return( PSA_ERROR_INVALID_ARGUMENT );
1525
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001526 return( PSA_SUCCESS );
1527}
1528
Gilles Peskine4747d192019-04-17 15:05:45 +02001529/** Prepare a key slot to receive key material.
1530 *
1531 * This function allocates a key slot and sets its metadata.
1532 *
1533 * If this function fails, call psa_fail_key_creation().
1534 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001535 * This function is intended to be used as follows:
1536 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1537 * it with the specified attributes, and assign it a handle.
1538 * -# Populate the slot with the key material.
1539 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1540 * In case of failure at any step, stop the sequence and call
1541 * psa_fail_key_creation().
1542 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001543 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001544 * \param[in] attributes Key attributes for the new key.
1545 * \param[out] handle On success, a handle for the allocated slot.
1546 * \param[out] p_slot On success, a pointer to the prepared slot.
1547 * \param[out] p_drv On any return, the driver for the key, if any.
1548 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001549 *
1550 * \retval #PSA_SUCCESS
1551 * The key slot is ready to receive key material.
1552 * \return If this function fails, the key slot is an invalid state.
1553 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001554 */
1555static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001556 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001557 const psa_key_attributes_t *attributes,
1558 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001559 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001560 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001561{
1562 psa_status_t status;
1563 psa_key_slot_t *slot;
1564
Gilles Peskinedf179142019-07-15 22:02:14 +02001565 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001566 *p_drv = NULL;
1567
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001568 status = psa_validate_key_attributes( attributes, p_drv );
1569 if( status != PSA_SUCCESS )
1570 return( status );
1571
Gilles Peskineedbed562019-08-07 18:19:59 +02001572 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001573 if( status != PSA_SUCCESS )
1574 return( status );
1575 slot = *p_slot;
1576
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001577 /* We're storing the declared bit-size of the key. It's up to each
1578 * creation mechanism to verify that this information is correct.
1579 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001580 * an input (generate, device) but not for those where the bit-size
1581 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001582
1583 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001584
Gilles Peskine91e8c332019-08-02 19:19:39 +02001585 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001586 * external-only flags, query `attributes`. Thanks to the check
1587 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001588 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001589 * may have set. */
1590 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001591
Gilles Peskinecbaff462019-07-12 23:46:04 +02001592#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001593 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001594 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001595 * create the key file in internal storage, create the
1596 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001597 * persistent data. This is done by starting a transaction that will
1598 * encompass these three actions.
1599 * For registering a volatile key, we just need to find an appropriate
1600 * slot number inside the SE. Since the key is designated volatile, creating
1601 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001602 /* The first thing to do is to find a slot number for the new key.
1603 * We save the slot number in persistent storage as part of the
1604 * transaction data. It will be needed to recover if the power
1605 * fails during the key creation process, to clean up on the secure
1606 * element side after restarting. Obtaining a slot number from the
1607 * secure element driver updates its persistent state, but we do not yet
1608 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001609 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001610 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001611 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001612 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001613 &slot->data.se.slot_number );
1614 if( status != PSA_SUCCESS )
1615 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001616
1617 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001618 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001619 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1620 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1621 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1622 psa_crypto_transaction.key.id = slot->attr.id;
1623 status = psa_crypto_save_transaction( );
1624 if( status != PSA_SUCCESS )
1625 {
1626 (void) psa_crypto_stop_transaction( );
1627 return( status );
1628 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001629 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001630 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001631
1632 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1633 {
1634 /* Key registration only makes sense with a secure element. */
1635 return( PSA_ERROR_INVALID_ARGUMENT );
1636 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001637#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1638
Darryl Green0c6575a2018-11-07 16:05:30 +00001639 return( status );
1640}
Gilles Peskine4747d192019-04-17 15:05:45 +02001641
1642/** Finalize the creation of a key once its key material has been set.
1643 *
1644 * This entails writing the key to persistent storage.
1645 *
1646 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001647 * See the documentation of psa_start_key_creation() for the intended use
1648 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001649 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001650 * \param[in,out] slot Pointer to the slot with key material.
1651 * \param[in] driver The secure element driver for the key,
1652 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001653 *
1654 * \retval #PSA_SUCCESS
1655 * The key was successfully created. The handle is now valid.
1656 * \return If this function fails, the key slot is an invalid state.
1657 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001658 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001659static psa_status_t psa_finish_key_creation(
1660 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001661 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001662{
1663 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001664 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001665 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001666
1667#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001668 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001669 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001670#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1671 if( driver != NULL )
1672 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001673 psa_se_key_data_storage_t data;
1674#if defined(static_assert)
1675 static_assert( sizeof( slot->data.se.slot_number ) ==
1676 sizeof( data.slot_number ),
1677 "Slot number size does not match psa_se_key_data_storage_t" );
1678 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1679 "Bit-size size does not match psa_se_key_data_storage_t" );
1680#endif
1681 memcpy( &data.slot_number, &slot->data.se.slot_number,
1682 sizeof( slot->data.se.slot_number ) );
1683 memcpy( &data.bits, &slot->attr.bits,
1684 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001685 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001686 (uint8_t*) &data,
1687 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001688 }
1689 else
1690#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1691 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001692 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001693 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001694 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001695 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1696 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001697 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001698 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1699 status = psa_internal_export_key( slot,
1700 buffer, buffer_size, &length,
1701 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001702 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001703 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001704 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001705
Gilles Peskinef9168942019-09-12 19:20:29 +02001706 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001707 mbedtls_free( buffer );
1708 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001709 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001710#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1711
Gilles Peskinecbaff462019-07-12 23:46:04 +02001712#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001713 /* Finish the transaction for a key creation. This does not
1714 * happen when registering an existing key. Detect this case
1715 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001716 * creation of a persistent key in a secure element requires a transaction,
1717 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001718 if( driver != NULL &&
1719 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001720 {
1721 status = psa_save_se_persistent_data( driver );
1722 if( status != PSA_SUCCESS )
1723 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001724 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001725 return( status );
1726 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001727 status = psa_crypto_stop_transaction( );
1728 if( status != PSA_SUCCESS )
1729 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001730 }
1731#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1732
Gilles Peskine4747d192019-04-17 15:05:45 +02001733 return( status );
1734}
1735
1736/** Abort the creation of a key.
1737 *
1738 * You may call this function after calling psa_start_key_creation(),
1739 * or after psa_finish_key_creation() fails. In other circumstances, this
1740 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001741 * See the documentation of psa_start_key_creation() for the intended use
1742 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001743 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001744 * \param[in,out] slot Pointer to the slot with key material.
1745 * \param[in] driver The secure element driver for the key,
1746 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001747 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001748static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001749 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001750{
Gilles Peskine011e4282019-06-26 18:34:38 +02001751 (void) driver;
1752
Gilles Peskine4747d192019-04-17 15:05:45 +02001753 if( slot == NULL )
1754 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001755
1756#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001757 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001758 * element, and the failure happened later (when saving metadata
1759 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001760 * element.
1761 * https://github.com/ARMmbed/mbed-crypto/issues/217
1762 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001763
Gilles Peskined7729582019-08-05 15:55:54 +02001764 /* Abort the ongoing transaction if any (there may not be one if
1765 * the creation process failed before starting one, or if the
1766 * key creation is a registration of a key in a secure element).
1767 * Earlier functions must already have done what it takes to undo any
1768 * partial creation. All that's left is to update the transaction data
1769 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001770 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001771#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1772
Gilles Peskine4747d192019-04-17 15:05:45 +02001773 psa_wipe_key_slot( slot );
1774}
1775
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001776/** Validate optional attributes during key creation.
1777 *
1778 * Some key attributes are optional during key creation. If they are
1779 * specified in the attributes structure, check that they are consistent
1780 * with the data in the slot.
1781 *
1782 * This function should be called near the end of key creation, after
1783 * the slot in memory is fully populated but before saving persistent data.
1784 */
1785static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001786 const psa_key_slot_t *slot,
1787 const psa_key_attributes_t *attributes )
1788{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001789 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001790 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001791 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001792 return( PSA_ERROR_INVALID_ARGUMENT );
1793 }
1794
1795 if( attributes->domain_parameters_size != 0 )
1796 {
1797#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001798 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001799 {
1800 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001801 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001802 mbedtls_mpi_init( &actual );
1803 mbedtls_mpi_init( &required );
1804 ret = mbedtls_rsa_export( slot->data.rsa,
1805 NULL, NULL, NULL, NULL, &actual );
1806 if( ret != 0 )
1807 goto rsa_exit;
1808 ret = mbedtls_mpi_read_binary( &required,
1809 attributes->domain_parameters,
1810 attributes->domain_parameters_size );
1811 if( ret != 0 )
1812 goto rsa_exit;
1813 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1814 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1815 rsa_exit:
1816 mbedtls_mpi_free( &actual );
1817 mbedtls_mpi_free( &required );
1818 if( ret != 0)
1819 return( mbedtls_to_psa_error( ret ) );
1820 }
1821 else
1822#endif
1823 {
1824 return( PSA_ERROR_INVALID_ARGUMENT );
1825 }
1826 }
1827
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001828 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001829 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001830 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001831 return( PSA_ERROR_INVALID_ARGUMENT );
1832 }
1833
1834 return( PSA_SUCCESS );
1835}
1836
Gilles Peskine4747d192019-04-17 15:05:45 +02001837psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001838 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001839 size_t data_length,
1840 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001841{
1842 psa_status_t status;
1843 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001844 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001845
Gilles Peskine0f84d622019-09-12 19:03:13 +02001846 /* Reject zero-length symmetric keys (including raw data key objects).
1847 * This also rejects any key which might be encoded as an empty string,
1848 * which is never valid. */
1849 if( data_length == 0 )
1850 return( PSA_ERROR_INVALID_ARGUMENT );
1851
Gilles Peskinedf179142019-07-15 22:02:14 +02001852 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1853 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001854 if( status != PSA_SUCCESS )
1855 goto exit;
1856
Gilles Peskine5d309672019-07-12 23:47:28 +02001857#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1858 if( driver != NULL )
1859 {
1860 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001861 /* The driver should set the number of key bits, however in
1862 * case it doesn't, we initialize bits to an invalid value. */
1863 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001864 if( drv->key_management == NULL ||
1865 drv->key_management->p_import == NULL )
1866 {
1867 status = PSA_ERROR_NOT_SUPPORTED;
1868 goto exit;
1869 }
1870 status = drv->key_management->p_import(
1871 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001872 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001873 &bits );
1874 if( status != PSA_SUCCESS )
1875 goto exit;
1876 if( bits > PSA_MAX_KEY_BITS )
1877 {
1878 status = PSA_ERROR_NOT_SUPPORTED;
1879 goto exit;
1880 }
1881 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001882 }
1883 else
1884#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1885 {
1886 status = psa_import_key_into_slot( slot, data, data_length );
1887 if( status != PSA_SUCCESS )
1888 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001889 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001890 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001891 if( status != PSA_SUCCESS )
1892 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001893
Gilles Peskine011e4282019-06-26 18:34:38 +02001894 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001895exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001896 if( status != PSA_SUCCESS )
1897 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001898 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001899 *handle = 0;
1900 }
1901 return( status );
1902}
1903
Gilles Peskined7729582019-08-05 15:55:54 +02001904#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1905psa_status_t mbedtls_psa_register_se_key(
1906 const psa_key_attributes_t *attributes )
1907{
1908 psa_status_t status;
1909 psa_key_slot_t *slot = NULL;
1910 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001911 psa_key_handle_t handle = 0;
1912
1913 /* Leaving attributes unspecified is not currently supported.
1914 * It could make sense to query the key type and size from the
1915 * secure element, but not all secure elements support this
1916 * and the driver HAL doesn't currently support it. */
1917 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1918 return( PSA_ERROR_NOT_SUPPORTED );
1919 if( psa_get_key_bits( attributes ) == 0 )
1920 return( PSA_ERROR_NOT_SUPPORTED );
1921
1922 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1923 &handle, &slot, &driver );
1924 if( status != PSA_SUCCESS )
1925 goto exit;
1926
Gilles Peskined7729582019-08-05 15:55:54 +02001927 status = psa_finish_key_creation( slot, driver );
1928
1929exit:
1930 if( status != PSA_SUCCESS )
1931 {
1932 psa_fail_key_creation( slot, driver );
1933 }
1934 /* Registration doesn't keep the key in RAM. */
1935 psa_close_key( handle );
1936 return( status );
1937}
1938#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1939
Gilles Peskinef603c712019-01-19 13:40:11 +01001940static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001941 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001942{
1943 psa_status_t status;
1944 uint8_t *buffer = NULL;
1945 size_t buffer_size = 0;
1946 size_t length;
1947
Gilles Peskine8e338702019-07-30 20:06:31 +02001948 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001949 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001950 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001951 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001952 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001953 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1954 if( status != PSA_SUCCESS )
1955 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001956 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001957 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001958
1959exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001960 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001961 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001962 return( status );
1963}
1964
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001965psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1966 const psa_key_attributes_t *specified_attributes,
1967 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001968{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001969 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001970 psa_key_slot_t *source_slot = NULL;
1971 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001972 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001973 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001974
Gilles Peskine28f8f302019-07-24 13:30:31 +02001975 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001976 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001977 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001978 goto exit;
1979
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001980 status = psa_validate_optional_attributes( source_slot,
1981 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001982 if( status != PSA_SUCCESS )
1983 goto exit;
1984
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001985 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001986 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001987 if( status != PSA_SUCCESS )
1988 goto exit;
1989
Gilles Peskinedf179142019-07-15 22:02:14 +02001990 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1991 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001992 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001993 if( status != PSA_SUCCESS )
1994 goto exit;
1995
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001996#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1997 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01001998 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001999 /* Copying to a secure element is not implemented yet. */
2000 status = PSA_ERROR_NOT_SUPPORTED;
2001 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002002 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002003#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002004
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002005 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002006 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002007 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002008
Gilles Peskine011e4282019-06-26 18:34:38 +02002009 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002010exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002011 if( status != PSA_SUCCESS )
2012 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002013 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002014 *target_handle = 0;
2015 }
2016 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002017}
2018
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002019
2020
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002021/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002022/* Message digests */
2023/****************************************************************/
2024
Gilles Peskineb16841e2019-10-10 20:36:12 +02002025#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002026static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002027{
2028 switch( alg )
2029 {
2030#if defined(MBEDTLS_MD2_C)
2031 case PSA_ALG_MD2:
2032 return( &mbedtls_md2_info );
2033#endif
2034#if defined(MBEDTLS_MD4_C)
2035 case PSA_ALG_MD4:
2036 return( &mbedtls_md4_info );
2037#endif
2038#if defined(MBEDTLS_MD5_C)
2039 case PSA_ALG_MD5:
2040 return( &mbedtls_md5_info );
2041#endif
2042#if defined(MBEDTLS_RIPEMD160_C)
2043 case PSA_ALG_RIPEMD160:
2044 return( &mbedtls_ripemd160_info );
2045#endif
2046#if defined(MBEDTLS_SHA1_C)
2047 case PSA_ALG_SHA_1:
2048 return( &mbedtls_sha1_info );
2049#endif
2050#if defined(MBEDTLS_SHA256_C)
2051 case PSA_ALG_SHA_224:
2052 return( &mbedtls_sha224_info );
2053 case PSA_ALG_SHA_256:
2054 return( &mbedtls_sha256_info );
2055#endif
2056#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002057#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002058 case PSA_ALG_SHA_384:
2059 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002060#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002061 case PSA_ALG_SHA_512:
2062 return( &mbedtls_sha512_info );
2063#endif
2064 default:
2065 return( NULL );
2066 }
2067}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002068#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002069
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002070psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2071{
2072 switch( operation->alg )
2073 {
Gilles Peskine81736312018-06-26 15:04:31 +02002074 case 0:
2075 /* The object has (apparently) been initialized but it is not
2076 * in use. It's ok to call abort on such an object, and there's
2077 * nothing to do. */
2078 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002079#if defined(MBEDTLS_MD2_C)
2080 case PSA_ALG_MD2:
2081 mbedtls_md2_free( &operation->ctx.md2 );
2082 break;
2083#endif
2084#if defined(MBEDTLS_MD4_C)
2085 case PSA_ALG_MD4:
2086 mbedtls_md4_free( &operation->ctx.md4 );
2087 break;
2088#endif
2089#if defined(MBEDTLS_MD5_C)
2090 case PSA_ALG_MD5:
2091 mbedtls_md5_free( &operation->ctx.md5 );
2092 break;
2093#endif
2094#if defined(MBEDTLS_RIPEMD160_C)
2095 case PSA_ALG_RIPEMD160:
2096 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2097 break;
2098#endif
2099#if defined(MBEDTLS_SHA1_C)
2100 case PSA_ALG_SHA_1:
2101 mbedtls_sha1_free( &operation->ctx.sha1 );
2102 break;
2103#endif
2104#if defined(MBEDTLS_SHA256_C)
2105 case PSA_ALG_SHA_224:
2106 case PSA_ALG_SHA_256:
2107 mbedtls_sha256_free( &operation->ctx.sha256 );
2108 break;
2109#endif
2110#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002111#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002112 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002113#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002114 case PSA_ALG_SHA_512:
2115 mbedtls_sha512_free( &operation->ctx.sha512 );
2116 break;
2117#endif
2118 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002119 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002120 }
2121 operation->alg = 0;
2122 return( PSA_SUCCESS );
2123}
2124
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002125psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002126 psa_algorithm_t alg )
2127{
Janos Follath24eed8d2019-11-22 13:21:35 +00002128 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002129
2130 /* A context must be freshly initialized before it can be set up. */
2131 if( operation->alg != 0 )
2132 {
2133 return( PSA_ERROR_BAD_STATE );
2134 }
2135
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002136 switch( alg )
2137 {
2138#if defined(MBEDTLS_MD2_C)
2139 case PSA_ALG_MD2:
2140 mbedtls_md2_init( &operation->ctx.md2 );
2141 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2142 break;
2143#endif
2144#if defined(MBEDTLS_MD4_C)
2145 case PSA_ALG_MD4:
2146 mbedtls_md4_init( &operation->ctx.md4 );
2147 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2148 break;
2149#endif
2150#if defined(MBEDTLS_MD5_C)
2151 case PSA_ALG_MD5:
2152 mbedtls_md5_init( &operation->ctx.md5 );
2153 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2154 break;
2155#endif
2156#if defined(MBEDTLS_RIPEMD160_C)
2157 case PSA_ALG_RIPEMD160:
2158 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2159 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2160 break;
2161#endif
2162#if defined(MBEDTLS_SHA1_C)
2163 case PSA_ALG_SHA_1:
2164 mbedtls_sha1_init( &operation->ctx.sha1 );
2165 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2166 break;
2167#endif
2168#if defined(MBEDTLS_SHA256_C)
2169 case PSA_ALG_SHA_224:
2170 mbedtls_sha256_init( &operation->ctx.sha256 );
2171 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2172 break;
2173 case PSA_ALG_SHA_256:
2174 mbedtls_sha256_init( &operation->ctx.sha256 );
2175 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2176 break;
2177#endif
2178#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002179#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002180 case PSA_ALG_SHA_384:
2181 mbedtls_sha512_init( &operation->ctx.sha512 );
2182 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2183 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002184#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002185 case PSA_ALG_SHA_512:
2186 mbedtls_sha512_init( &operation->ctx.sha512 );
2187 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2188 break;
2189#endif
2190 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002191 return( PSA_ALG_IS_HASH( alg ) ?
2192 PSA_ERROR_NOT_SUPPORTED :
2193 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002194 }
2195 if( ret == 0 )
2196 operation->alg = alg;
2197 else
2198 psa_hash_abort( operation );
2199 return( mbedtls_to_psa_error( ret ) );
2200}
2201
2202psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2203 const uint8_t *input,
2204 size_t input_length )
2205{
Janos Follath24eed8d2019-11-22 13:21:35 +00002206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002207
2208 /* Don't require hash implementations to behave correctly on a
2209 * zero-length input, which may have an invalid pointer. */
2210 if( input_length == 0 )
2211 return( PSA_SUCCESS );
2212
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002213 switch( operation->alg )
2214 {
2215#if defined(MBEDTLS_MD2_C)
2216 case PSA_ALG_MD2:
2217 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2218 input, input_length );
2219 break;
2220#endif
2221#if defined(MBEDTLS_MD4_C)
2222 case PSA_ALG_MD4:
2223 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2224 input, input_length );
2225 break;
2226#endif
2227#if defined(MBEDTLS_MD5_C)
2228 case PSA_ALG_MD5:
2229 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2230 input, input_length );
2231 break;
2232#endif
2233#if defined(MBEDTLS_RIPEMD160_C)
2234 case PSA_ALG_RIPEMD160:
2235 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2236 input, input_length );
2237 break;
2238#endif
2239#if defined(MBEDTLS_SHA1_C)
2240 case PSA_ALG_SHA_1:
2241 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2242 input, input_length );
2243 break;
2244#endif
2245#if defined(MBEDTLS_SHA256_C)
2246 case PSA_ALG_SHA_224:
2247 case PSA_ALG_SHA_256:
2248 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2249 input, input_length );
2250 break;
2251#endif
2252#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002253#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002254 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002255#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002256 case PSA_ALG_SHA_512:
2257 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2258 input, input_length );
2259 break;
2260#endif
2261 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002262 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002263 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002264
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002265 if( ret != 0 )
2266 psa_hash_abort( operation );
2267 return( mbedtls_to_psa_error( ret ) );
2268}
2269
2270psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2271 uint8_t *hash,
2272 size_t hash_size,
2273 size_t *hash_length )
2274{
itayzafrir40835d42018-08-02 13:14:17 +03002275 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002276 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002277 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002278
2279 /* Fill the output buffer with something that isn't a valid hash
2280 * (barring an attack on the hash and deliberately-crafted input),
2281 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002282 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002283 /* If hash_size is 0 then hash may be NULL and then the
2284 * call to memset would have undefined behavior. */
2285 if( hash_size != 0 )
2286 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002287
2288 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002289 {
2290 status = PSA_ERROR_BUFFER_TOO_SMALL;
2291 goto exit;
2292 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002293
2294 switch( operation->alg )
2295 {
2296#if defined(MBEDTLS_MD2_C)
2297 case PSA_ALG_MD2:
2298 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2299 break;
2300#endif
2301#if defined(MBEDTLS_MD4_C)
2302 case PSA_ALG_MD4:
2303 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2304 break;
2305#endif
2306#if defined(MBEDTLS_MD5_C)
2307 case PSA_ALG_MD5:
2308 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2309 break;
2310#endif
2311#if defined(MBEDTLS_RIPEMD160_C)
2312 case PSA_ALG_RIPEMD160:
2313 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2314 break;
2315#endif
2316#if defined(MBEDTLS_SHA1_C)
2317 case PSA_ALG_SHA_1:
2318 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2319 break;
2320#endif
2321#if defined(MBEDTLS_SHA256_C)
2322 case PSA_ALG_SHA_224:
2323 case PSA_ALG_SHA_256:
2324 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2325 break;
2326#endif
2327#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002328#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002329 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002330#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002331 case PSA_ALG_SHA_512:
2332 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2333 break;
2334#endif
2335 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002336 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002337 }
itayzafrir40835d42018-08-02 13:14:17 +03002338 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002339
itayzafrir40835d42018-08-02 13:14:17 +03002340exit:
2341 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002342 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002343 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002344 return( psa_hash_abort( operation ) );
2345 }
2346 else
2347 {
2348 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002349 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002350 }
2351}
2352
Gilles Peskine2d277862018-06-18 15:41:12 +02002353psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2354 const uint8_t *hash,
2355 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002356{
2357 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2358 size_t actual_hash_length;
2359 psa_status_t status = psa_hash_finish( operation,
2360 actual_hash, sizeof( actual_hash ),
2361 &actual_hash_length );
2362 if( status != PSA_SUCCESS )
2363 return( status );
2364 if( actual_hash_length != hash_length )
2365 return( PSA_ERROR_INVALID_SIGNATURE );
2366 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2367 return( PSA_ERROR_INVALID_SIGNATURE );
2368 return( PSA_SUCCESS );
2369}
2370
Gilles Peskine0a749c82019-11-28 19:33:58 +01002371psa_status_t psa_hash_compute( psa_algorithm_t alg,
2372 const uint8_t *input, size_t input_length,
2373 uint8_t *hash, size_t hash_size,
2374 size_t *hash_length )
2375{
2376 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2377 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2378
2379 *hash_length = hash_size;
2380 status = psa_hash_setup( &operation, alg );
2381 if( status != PSA_SUCCESS )
2382 goto exit;
2383 status = psa_hash_update( &operation, input, input_length );
2384 if( status != PSA_SUCCESS )
2385 goto exit;
2386 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2387 if( status != PSA_SUCCESS )
2388 goto exit;
2389
2390exit:
2391 if( status == PSA_SUCCESS )
2392 status = psa_hash_abort( &operation );
2393 else
2394 psa_hash_abort( &operation );
2395 return( status );
2396}
2397
2398psa_status_t psa_hash_compare( psa_algorithm_t alg,
2399 const uint8_t *input, size_t input_length,
2400 const uint8_t *hash, size_t hash_length )
2401{
2402 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2403 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2404
2405 status = psa_hash_setup( &operation, alg );
2406 if( status != PSA_SUCCESS )
2407 goto exit;
2408 status = psa_hash_update( &operation, input, input_length );
2409 if( status != PSA_SUCCESS )
2410 goto exit;
2411 status = psa_hash_verify( &operation, hash, hash_length );
2412 if( status != PSA_SUCCESS )
2413 goto exit;
2414
2415exit:
2416 if( status == PSA_SUCCESS )
2417 status = psa_hash_abort( &operation );
2418 else
2419 psa_hash_abort( &operation );
2420 return( status );
2421}
2422
Gilles Peskineeb35d782019-01-22 17:56:16 +01002423psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2424 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002425{
2426 if( target_operation->alg != 0 )
2427 return( PSA_ERROR_BAD_STATE );
2428
2429 switch( source_operation->alg )
2430 {
2431 case 0:
2432 return( PSA_ERROR_BAD_STATE );
2433#if defined(MBEDTLS_MD2_C)
2434 case PSA_ALG_MD2:
2435 mbedtls_md2_clone( &target_operation->ctx.md2,
2436 &source_operation->ctx.md2 );
2437 break;
2438#endif
2439#if defined(MBEDTLS_MD4_C)
2440 case PSA_ALG_MD4:
2441 mbedtls_md4_clone( &target_operation->ctx.md4,
2442 &source_operation->ctx.md4 );
2443 break;
2444#endif
2445#if defined(MBEDTLS_MD5_C)
2446 case PSA_ALG_MD5:
2447 mbedtls_md5_clone( &target_operation->ctx.md5,
2448 &source_operation->ctx.md5 );
2449 break;
2450#endif
2451#if defined(MBEDTLS_RIPEMD160_C)
2452 case PSA_ALG_RIPEMD160:
2453 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2454 &source_operation->ctx.ripemd160 );
2455 break;
2456#endif
2457#if defined(MBEDTLS_SHA1_C)
2458 case PSA_ALG_SHA_1:
2459 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2460 &source_operation->ctx.sha1 );
2461 break;
2462#endif
2463#if defined(MBEDTLS_SHA256_C)
2464 case PSA_ALG_SHA_224:
2465 case PSA_ALG_SHA_256:
2466 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2467 &source_operation->ctx.sha256 );
2468 break;
2469#endif
2470#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002471#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002472 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002473#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002474 case PSA_ALG_SHA_512:
2475 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2476 &source_operation->ctx.sha512 );
2477 break;
2478#endif
2479 default:
2480 return( PSA_ERROR_NOT_SUPPORTED );
2481 }
2482
2483 target_operation->alg = source_operation->alg;
2484 return( PSA_SUCCESS );
2485}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002486
2487
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002488/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002489/* MAC */
2490/****************************************************************/
2491
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002492static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002493 psa_algorithm_t alg,
2494 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002495 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002496 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002497{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002498 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002499 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002500
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002501 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002502 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002503
Gilles Peskine8c9def32018-02-08 10:02:12 +01002504 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2505 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002506 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002507 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002508 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002509 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002510 mode = MBEDTLS_MODE_STREAM;
2511 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002512 case PSA_ALG_CTR:
2513 mode = MBEDTLS_MODE_CTR;
2514 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002515 case PSA_ALG_CFB:
2516 mode = MBEDTLS_MODE_CFB;
2517 break;
2518 case PSA_ALG_OFB:
2519 mode = MBEDTLS_MODE_OFB;
2520 break;
2521 case PSA_ALG_CBC_NO_PADDING:
2522 mode = MBEDTLS_MODE_CBC;
2523 break;
2524 case PSA_ALG_CBC_PKCS7:
2525 mode = MBEDTLS_MODE_CBC;
2526 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002527 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002528 mode = MBEDTLS_MODE_CCM;
2529 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002530 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002531 mode = MBEDTLS_MODE_GCM;
2532 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002533 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2534 mode = MBEDTLS_MODE_CHACHAPOLY;
2535 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002536 default:
2537 return( NULL );
2538 }
2539 }
2540 else if( alg == PSA_ALG_CMAC )
2541 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002542 else
2543 return( NULL );
2544
2545 switch( key_type )
2546 {
2547 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002548 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002549 break;
2550 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002551 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2552 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002553 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002554 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002555 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002556 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002557 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2558 * but two-key Triple-DES is functionally three-key Triple-DES
2559 * with K1=K3, so that's how we present it to mbedtls. */
2560 if( key_bits == 128 )
2561 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002562 break;
2563 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002564 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002565 break;
2566 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002567 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002568 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002569 case PSA_KEY_TYPE_CHACHA20:
2570 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2571 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002572 default:
2573 return( NULL );
2574 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002575 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002576 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002577
Jaeden Amero23bbb752018-06-26 14:16:54 +01002578 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2579 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002580}
2581
Gilles Peskinea05219c2018-11-16 16:02:56 +01002582#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002583static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002584{
Gilles Peskine2d277862018-06-18 15:41:12 +02002585 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002586 {
2587 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002588 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002589 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002590 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002591 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002592 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002593 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002594 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002595 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002596 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002597 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002598 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002599 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002600 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002601 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002602 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002603 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002604 return( 128 );
2605 default:
2606 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002607 }
2608}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002609#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002610
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002611/* Initialize the MAC operation structure. Once this function has been
2612 * called, psa_mac_abort can run and will do the right thing. */
2613static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2614 psa_algorithm_t alg )
2615{
2616 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2617
2618 operation->alg = alg;
2619 operation->key_set = 0;
2620 operation->iv_set = 0;
2621 operation->iv_required = 0;
2622 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002623 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002624
2625#if defined(MBEDTLS_CMAC_C)
2626 if( alg == PSA_ALG_CMAC )
2627 {
2628 operation->iv_required = 0;
2629 mbedtls_cipher_init( &operation->ctx.cmac );
2630 status = PSA_SUCCESS;
2631 }
2632 else
2633#endif /* MBEDTLS_CMAC_C */
2634#if defined(MBEDTLS_MD_C)
2635 if( PSA_ALG_IS_HMAC( operation->alg ) )
2636 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002637 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2638 operation->ctx.hmac.hash_ctx.alg = 0;
2639 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002640 }
2641 else
2642#endif /* MBEDTLS_MD_C */
2643 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002644 if( ! PSA_ALG_IS_MAC( alg ) )
2645 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002646 }
2647
2648 if( status != PSA_SUCCESS )
2649 memset( operation, 0, sizeof( *operation ) );
2650 return( status );
2651}
2652
Gilles Peskine01126fa2018-07-12 17:04:55 +02002653#if defined(MBEDTLS_MD_C)
2654static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2655{
Gilles Peskine3f108122018-12-07 18:14:53 +01002656 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002657 return( psa_hash_abort( &hmac->hash_ctx ) );
2658}
2659#endif /* MBEDTLS_MD_C */
2660
Gilles Peskine8c9def32018-02-08 10:02:12 +01002661psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2662{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002663 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002664 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002665 /* The object has (apparently) been initialized but it is not
2666 * in use. It's ok to call abort on such an object, and there's
2667 * nothing to do. */
2668 return( PSA_SUCCESS );
2669 }
2670 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002671#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002672 if( operation->alg == PSA_ALG_CMAC )
2673 {
2674 mbedtls_cipher_free( &operation->ctx.cmac );
2675 }
2676 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002677#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002678#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002679 if( PSA_ALG_IS_HMAC( operation->alg ) )
2680 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002681 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002682 }
2683 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002684#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002685 {
2686 /* Sanity check (shouldn't happen: operation->alg should
2687 * always have been initialized to a valid value). */
2688 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002689 }
Moran Peker41deec42018-04-04 15:43:05 +03002690
Gilles Peskine8c9def32018-02-08 10:02:12 +01002691 operation->alg = 0;
2692 operation->key_set = 0;
2693 operation->iv_set = 0;
2694 operation->iv_required = 0;
2695 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002696 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002697
Gilles Peskine8c9def32018-02-08 10:02:12 +01002698 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002699
2700bad_state:
2701 /* If abort is called on an uninitialized object, we can't trust
2702 * anything. Wipe the object in case it contains confidential data.
2703 * This may result in a memory leak if a pointer gets overwritten,
2704 * but it's too late to do anything about this. */
2705 memset( operation, 0, sizeof( *operation ) );
2706 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002707}
2708
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002709#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002710static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002711 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002712 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002713 const mbedtls_cipher_info_t *cipher_info )
2714{
Janos Follath24eed8d2019-11-22 13:21:35 +00002715 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002716
2717 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002718
2719 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2720 if( ret != 0 )
2721 return( ret );
2722
2723 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2724 slot->data.raw.data,
2725 key_bits );
2726 return( ret );
2727}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002728#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002729
Gilles Peskine248051a2018-06-20 16:09:38 +02002730#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002731static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2732 const uint8_t *key,
2733 size_t key_length,
2734 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002735{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002736 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002737 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002738 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002739 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002740 psa_status_t status;
2741
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002742 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2743 * overflow below. This should never trigger if the hash algorithm
2744 * is implemented correctly. */
2745 /* The size checks against the ipad and opad buffers cannot be written
2746 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2747 * because that triggers -Wlogical-op on GCC 7.3. */
2748 if( block_size > sizeof( ipad ) )
2749 return( PSA_ERROR_NOT_SUPPORTED );
2750 if( block_size > sizeof( hmac->opad ) )
2751 return( PSA_ERROR_NOT_SUPPORTED );
2752 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002753 return( PSA_ERROR_NOT_SUPPORTED );
2754
Gilles Peskined223b522018-06-11 18:12:58 +02002755 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002756 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002757 status = psa_hash_compute( hash_alg, key, key_length,
2758 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002759 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002760 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002761 }
Gilles Peskine96889972018-07-12 17:07:03 +02002762 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2763 * but it is permitted. It is common when HMAC is used in HKDF, for
2764 * example. Don't call `memcpy` in the 0-length because `key` could be
2765 * an invalid pointer which would make the behavior undefined. */
2766 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002767 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002768
Gilles Peskined223b522018-06-11 18:12:58 +02002769 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2770 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002771 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002772 ipad[i] ^= 0x36;
2773 memset( ipad + key_length, 0x36, block_size - key_length );
2774
2775 /* Copy the key material from ipad to opad, flipping the requisite bits,
2776 * and filling the rest of opad with the requisite constant. */
2777 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002778 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2779 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002780
Gilles Peskine01126fa2018-07-12 17:04:55 +02002781 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002782 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002783 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002784
Gilles Peskine01126fa2018-07-12 17:04:55 +02002785 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002786
2787cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002788 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002789
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002790 return( status );
2791}
Gilles Peskine248051a2018-06-20 16:09:38 +02002792#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002793
Gilles Peskine89167cb2018-07-08 20:12:23 +02002794static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002795 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002796 psa_algorithm_t alg,
2797 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002798{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002799 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002800 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002801 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002802 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002803 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002804 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002805 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002806
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002807 /* A context must be freshly initialized before it can be set up. */
2808 if( operation->alg != 0 )
2809 {
2810 return( PSA_ERROR_BAD_STATE );
2811 }
2812
Gilles Peskined911eb72018-08-14 15:18:45 +02002813 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002814 if( status != PSA_SUCCESS )
2815 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002816 if( is_sign )
2817 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002818
Gilles Peskine28f8f302019-07-24 13:30:31 +02002819 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002820 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002821 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002822 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002823
Gilles Peskine8c9def32018-02-08 10:02:12 +01002824#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002825 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002826 {
2827 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002828 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002829 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002830 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002831 if( cipher_info == NULL )
2832 {
2833 status = PSA_ERROR_NOT_SUPPORTED;
2834 goto exit;
2835 }
2836 operation->mac_size = cipher_info->block_size;
2837 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2838 status = mbedtls_to_psa_error( ret );
2839 }
2840 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002841#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002842#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002843 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002844 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002845 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002846 if( hash_alg == 0 )
2847 {
2848 status = PSA_ERROR_NOT_SUPPORTED;
2849 goto exit;
2850 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002851
2852 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2853 /* Sanity check. This shouldn't fail on a valid configuration. */
2854 if( operation->mac_size == 0 ||
2855 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2856 {
2857 status = PSA_ERROR_NOT_SUPPORTED;
2858 goto exit;
2859 }
2860
Gilles Peskine8e338702019-07-30 20:06:31 +02002861 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002862 {
2863 status = PSA_ERROR_INVALID_ARGUMENT;
2864 goto exit;
2865 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002866
Gilles Peskine01126fa2018-07-12 17:04:55 +02002867 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2868 slot->data.raw.data,
2869 slot->data.raw.bytes,
2870 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002871 }
2872 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002873#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002874 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002875 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002876 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002877 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002878
Gilles Peskined911eb72018-08-14 15:18:45 +02002879 if( truncated == 0 )
2880 {
2881 /* The "normal" case: untruncated algorithm. Nothing to do. */
2882 }
2883 else if( truncated < 4 )
2884 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002885 /* A very short MAC is too short for security since it can be
2886 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2887 * so we make this our minimum, even though 32 bits is still
2888 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002889 status = PSA_ERROR_NOT_SUPPORTED;
2890 }
2891 else if( truncated > operation->mac_size )
2892 {
2893 /* It's impossible to "truncate" to a larger length. */
2894 status = PSA_ERROR_INVALID_ARGUMENT;
2895 }
2896 else
2897 operation->mac_size = truncated;
2898
Gilles Peskinefbfac682018-07-08 20:51:54 +02002899exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002900 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002901 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002902 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002903 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002904 else
2905 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002906 operation->key_set = 1;
2907 }
2908 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002909}
2910
Gilles Peskine89167cb2018-07-08 20:12:23 +02002911psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002912 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002913 psa_algorithm_t alg )
2914{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002915 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002916}
2917
2918psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002919 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002920 psa_algorithm_t alg )
2921{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002922 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002923}
2924
Gilles Peskine8c9def32018-02-08 10:02:12 +01002925psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2926 const uint8_t *input,
2927 size_t input_length )
2928{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002929 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002930 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002931 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002932 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002933 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002934 operation->has_input = 1;
2935
Gilles Peskine8c9def32018-02-08 10:02:12 +01002936#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002937 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002938 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002939 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2940 input, input_length );
2941 status = mbedtls_to_psa_error( ret );
2942 }
2943 else
2944#endif /* MBEDTLS_CMAC_C */
2945#if defined(MBEDTLS_MD_C)
2946 if( PSA_ALG_IS_HMAC( operation->alg ) )
2947 {
2948 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2949 input_length );
2950 }
2951 else
2952#endif /* MBEDTLS_MD_C */
2953 {
2954 /* This shouldn't happen if `operation` was initialized by
2955 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002956 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002957 }
2958
Gilles Peskinefbfac682018-07-08 20:51:54 +02002959 if( status != PSA_SUCCESS )
2960 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002961 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002962}
2963
Gilles Peskine01126fa2018-07-12 17:04:55 +02002964#if defined(MBEDTLS_MD_C)
2965static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2966 uint8_t *mac,
2967 size_t mac_size )
2968{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002969 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002970 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2971 size_t hash_size = 0;
2972 size_t block_size = psa_get_hash_block_size( hash_alg );
2973 psa_status_t status;
2974
Gilles Peskine01126fa2018-07-12 17:04:55 +02002975 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2976 if( status != PSA_SUCCESS )
2977 return( status );
2978 /* From here on, tmp needs to be wiped. */
2979
2980 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2981 if( status != PSA_SUCCESS )
2982 goto exit;
2983
2984 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2985 if( status != PSA_SUCCESS )
2986 goto exit;
2987
2988 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2989 if( status != PSA_SUCCESS )
2990 goto exit;
2991
Gilles Peskined911eb72018-08-14 15:18:45 +02002992 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2993 if( status != PSA_SUCCESS )
2994 goto exit;
2995
2996 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002997
2998exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002999 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003000 return( status );
3001}
3002#endif /* MBEDTLS_MD_C */
3003
mohammad16036df908f2018-04-02 08:34:15 -07003004static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003005 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003006 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003007{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003008 if( ! operation->key_set )
3009 return( PSA_ERROR_BAD_STATE );
3010 if( operation->iv_required && ! operation->iv_set )
3011 return( PSA_ERROR_BAD_STATE );
3012
Gilles Peskine8c9def32018-02-08 10:02:12 +01003013 if( mac_size < operation->mac_size )
3014 return( PSA_ERROR_BUFFER_TOO_SMALL );
3015
Gilles Peskine8c9def32018-02-08 10:02:12 +01003016#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003017 if( operation->alg == PSA_ALG_CMAC )
3018 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003019 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3020 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3021 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003022 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003023 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003024 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003025 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003026 else
3027#endif /* MBEDTLS_CMAC_C */
3028#if defined(MBEDTLS_MD_C)
3029 if( PSA_ALG_IS_HMAC( operation->alg ) )
3030 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003031 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003032 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003033 }
3034 else
3035#endif /* MBEDTLS_MD_C */
3036 {
3037 /* This shouldn't happen if `operation` was initialized by
3038 * a setup function. */
3039 return( PSA_ERROR_BAD_STATE );
3040 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003041}
3042
Gilles Peskineacd4be32018-07-08 19:56:25 +02003043psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3044 uint8_t *mac,
3045 size_t mac_size,
3046 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003047{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003048 psa_status_t status;
3049
Jaeden Amero252ef282019-02-15 14:05:35 +00003050 if( operation->alg == 0 )
3051 {
3052 return( PSA_ERROR_BAD_STATE );
3053 }
3054
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003055 /* Fill the output buffer with something that isn't a valid mac
3056 * (barring an attack on the mac and deliberately-crafted input),
3057 * in case the caller doesn't check the return status properly. */
3058 *mac_length = mac_size;
3059 /* If mac_size is 0 then mac may be NULL and then the
3060 * call to memset would have undefined behavior. */
3061 if( mac_size != 0 )
3062 memset( mac, '!', mac_size );
3063
Gilles Peskine89167cb2018-07-08 20:12:23 +02003064 if( ! operation->is_sign )
3065 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003066 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003067 }
mohammad16036df908f2018-04-02 08:34:15 -07003068
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003069 status = psa_mac_finish_internal( operation, mac, mac_size );
3070
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003071 if( status == PSA_SUCCESS )
3072 {
3073 status = psa_mac_abort( operation );
3074 if( status == PSA_SUCCESS )
3075 *mac_length = operation->mac_size;
3076 else
3077 memset( mac, '!', mac_size );
3078 }
3079 else
3080 psa_mac_abort( operation );
3081 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003082}
3083
Gilles Peskineacd4be32018-07-08 19:56:25 +02003084psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3085 const uint8_t *mac,
3086 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003087{
Gilles Peskine828ed142018-06-18 23:25:51 +02003088 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003089 psa_status_t status;
3090
Jaeden Amero252ef282019-02-15 14:05:35 +00003091 if( operation->alg == 0 )
3092 {
3093 return( PSA_ERROR_BAD_STATE );
3094 }
3095
Gilles Peskine89167cb2018-07-08 20:12:23 +02003096 if( operation->is_sign )
3097 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003098 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003099 }
3100 if( operation->mac_size != mac_length )
3101 {
3102 status = PSA_ERROR_INVALID_SIGNATURE;
3103 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003104 }
mohammad16036df908f2018-04-02 08:34:15 -07003105
3106 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003107 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003108 if( status != PSA_SUCCESS )
3109 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003110
3111 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3112 status = PSA_ERROR_INVALID_SIGNATURE;
3113
3114cleanup:
3115 if( status == PSA_SUCCESS )
3116 status = psa_mac_abort( operation );
3117 else
3118 psa_mac_abort( operation );
3119
Gilles Peskine3f108122018-12-07 18:14:53 +01003120 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003121
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003122 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003123}
3124
3125
Gilles Peskine20035e32018-02-03 22:44:14 +01003126
Gilles Peskine20035e32018-02-03 22:44:14 +01003127/****************************************************************/
3128/* Asymmetric cryptography */
3129/****************************************************************/
3130
Gilles Peskine2b450e32018-06-27 15:42:46 +02003131#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003132/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003133 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003134static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3135 size_t hash_length,
3136 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003137{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003138 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003139 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003140 *md_alg = mbedtls_md_get_type( md_info );
3141
3142 /* The Mbed TLS RSA module uses an unsigned int for hash length
3143 * parameters. Validate that it fits so that we don't risk an
3144 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003145#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003146 if( hash_length > UINT_MAX )
3147 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003148#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003149
3150#if defined(MBEDTLS_PKCS1_V15)
3151 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3152 * must be correct. */
3153 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3154 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003155 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003156 if( md_info == NULL )
3157 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003158 if( mbedtls_md_get_size( md_info ) != hash_length )
3159 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003160 }
3161#endif /* MBEDTLS_PKCS1_V15 */
3162
3163#if defined(MBEDTLS_PKCS1_V21)
3164 /* PSS requires a hash internally. */
3165 if( PSA_ALG_IS_RSA_PSS( alg ) )
3166 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003167 if( md_info == NULL )
3168 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003169 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003170#endif /* MBEDTLS_PKCS1_V21 */
3171
Gilles Peskine61b91d42018-06-08 16:09:36 +02003172 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003173}
3174
Gilles Peskine2b450e32018-06-27 15:42:46 +02003175static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3176 psa_algorithm_t alg,
3177 const uint8_t *hash,
3178 size_t hash_length,
3179 uint8_t *signature,
3180 size_t signature_size,
3181 size_t *signature_length )
3182{
3183 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003184 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003185 mbedtls_md_type_t md_alg;
3186
3187 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3188 if( status != PSA_SUCCESS )
3189 return( status );
3190
Gilles Peskine630a18a2018-06-29 17:49:35 +02003191 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003192 return( PSA_ERROR_BUFFER_TOO_SMALL );
3193
3194#if defined(MBEDTLS_PKCS1_V15)
3195 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3196 {
3197 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3198 MBEDTLS_MD_NONE );
3199 ret = mbedtls_rsa_pkcs1_sign( rsa,
3200 mbedtls_ctr_drbg_random,
3201 &global_data.ctr_drbg,
3202 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003203 md_alg,
3204 (unsigned int) hash_length,
3205 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003206 signature );
3207 }
3208 else
3209#endif /* MBEDTLS_PKCS1_V15 */
3210#if defined(MBEDTLS_PKCS1_V21)
3211 if( PSA_ALG_IS_RSA_PSS( alg ) )
3212 {
3213 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3214 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3215 mbedtls_ctr_drbg_random,
3216 &global_data.ctr_drbg,
3217 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003218 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003219 (unsigned int) hash_length,
3220 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003221 signature );
3222 }
3223 else
3224#endif /* MBEDTLS_PKCS1_V21 */
3225 {
3226 return( PSA_ERROR_INVALID_ARGUMENT );
3227 }
3228
3229 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003230 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003231 return( mbedtls_to_psa_error( ret ) );
3232}
3233
3234static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3235 psa_algorithm_t alg,
3236 const uint8_t *hash,
3237 size_t hash_length,
3238 const uint8_t *signature,
3239 size_t signature_length )
3240{
3241 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003242 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003243 mbedtls_md_type_t md_alg;
3244
3245 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3246 if( status != PSA_SUCCESS )
3247 return( status );
3248
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003249 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3250 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003251
3252#if defined(MBEDTLS_PKCS1_V15)
3253 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3254 {
3255 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3256 MBEDTLS_MD_NONE );
3257 ret = mbedtls_rsa_pkcs1_verify( rsa,
3258 mbedtls_ctr_drbg_random,
3259 &global_data.ctr_drbg,
3260 MBEDTLS_RSA_PUBLIC,
3261 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003262 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003263 hash,
3264 signature );
3265 }
3266 else
3267#endif /* MBEDTLS_PKCS1_V15 */
3268#if defined(MBEDTLS_PKCS1_V21)
3269 if( PSA_ALG_IS_RSA_PSS( alg ) )
3270 {
3271 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3272 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3273 mbedtls_ctr_drbg_random,
3274 &global_data.ctr_drbg,
3275 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003276 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003277 (unsigned int) hash_length,
3278 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003279 signature );
3280 }
3281 else
3282#endif /* MBEDTLS_PKCS1_V21 */
3283 {
3284 return( PSA_ERROR_INVALID_ARGUMENT );
3285 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003286
3287 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3288 * the rest of the signature is invalid". This has little use in
3289 * practice and PSA doesn't report this distinction. */
3290 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3291 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003292 return( mbedtls_to_psa_error( ret ) );
3293}
3294#endif /* MBEDTLS_RSA_C */
3295
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003296#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003297/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3298 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3299 * (even though these functions don't modify it). */
3300static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3301 psa_algorithm_t alg,
3302 const uint8_t *hash,
3303 size_t hash_length,
3304 uint8_t *signature,
3305 size_t signature_size,
3306 size_t *signature_length )
3307{
Janos Follath24eed8d2019-11-22 13:21:35 +00003308 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003309 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003310 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003311 mbedtls_mpi_init( &r );
3312 mbedtls_mpi_init( &s );
3313
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003314 if( signature_size < 2 * curve_bytes )
3315 {
3316 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3317 goto cleanup;
3318 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003319
Gilles Peskinea05219c2018-11-16 16:02:56 +01003320#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003321 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3322 {
3323 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3324 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3325 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003326 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3327 &ecp->d, hash,
3328 hash_length, md_alg,
3329 mbedtls_ctr_drbg_random,
3330 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003331 }
3332 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003333#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003334 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003335 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003336 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3337 hash, hash_length,
3338 mbedtls_ctr_drbg_random,
3339 &global_data.ctr_drbg ) );
3340 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003341
3342 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3343 signature,
3344 curve_bytes ) );
3345 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3346 signature + curve_bytes,
3347 curve_bytes ) );
3348
3349cleanup:
3350 mbedtls_mpi_free( &r );
3351 mbedtls_mpi_free( &s );
3352 if( ret == 0 )
3353 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003354 return( mbedtls_to_psa_error( ret ) );
3355}
3356
3357static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3358 const uint8_t *hash,
3359 size_t hash_length,
3360 const uint8_t *signature,
3361 size_t signature_length )
3362{
Janos Follath24eed8d2019-11-22 13:21:35 +00003363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003364 mbedtls_mpi r, s;
3365 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3366 mbedtls_mpi_init( &r );
3367 mbedtls_mpi_init( &s );
3368
3369 if( signature_length != 2 * curve_bytes )
3370 return( PSA_ERROR_INVALID_SIGNATURE );
3371
3372 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3373 signature,
3374 curve_bytes ) );
3375 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3376 signature + curve_bytes,
3377 curve_bytes ) );
3378
3379 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3380 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003381
3382cleanup:
3383 mbedtls_mpi_free( &r );
3384 mbedtls_mpi_free( &s );
3385 return( mbedtls_to_psa_error( ret ) );
3386}
3387#endif /* MBEDTLS_ECDSA_C */
3388
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003389psa_status_t psa_sign_hash( psa_key_handle_t handle,
3390 psa_algorithm_t alg,
3391 const uint8_t *hash,
3392 size_t hash_length,
3393 uint8_t *signature,
3394 size_t signature_size,
3395 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003396{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003397 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003398 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003399#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3400 const psa_drv_se_t *drv;
3401 psa_drv_se_context_t *drv_context;
3402#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003403
3404 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003405 /* Immediately reject a zero-length signature buffer. This guarantees
3406 * that signature must be a valid pointer. (On the other hand, the hash
3407 * buffer can in principle be empty since it doesn't actually have
3408 * to be a hash.) */
3409 if( signature_size == 0 )
3410 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003411
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003412 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003413 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003414 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003415 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003416 {
3417 status = PSA_ERROR_INVALID_ARGUMENT;
3418 goto exit;
3419 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003420
Gilles Peskineedc64242019-08-07 21:05:07 +02003421#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3422 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3423 {
3424 if( drv->asymmetric == NULL ||
3425 drv->asymmetric->p_sign == NULL )
3426 {
3427 status = PSA_ERROR_NOT_SUPPORTED;
3428 goto exit;
3429 }
3430 status = drv->asymmetric->p_sign( drv_context,
3431 slot->data.se.slot_number,
3432 alg,
3433 hash, hash_length,
3434 signature, signature_size,
3435 signature_length );
3436 }
3437 else
3438#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003439#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003440 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003441 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003442 status = psa_rsa_sign( slot->data.rsa,
3443 alg,
3444 hash, hash_length,
3445 signature, signature_size,
3446 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003447 }
3448 else
3449#endif /* defined(MBEDTLS_RSA_C) */
3450#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003451 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003452 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003453#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003454 if(
3455#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3456 PSA_ALG_IS_ECDSA( alg )
3457#else
3458 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3459#endif
3460 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003461 status = psa_ecdsa_sign( slot->data.ecp,
3462 alg,
3463 hash, hash_length,
3464 signature, signature_size,
3465 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003466 else
3467#endif /* defined(MBEDTLS_ECDSA_C) */
3468 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003469 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003470 }
itayzafrir5c753392018-05-08 11:18:38 +03003471 }
3472 else
3473#endif /* defined(MBEDTLS_ECP_C) */
3474 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003475 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003476 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003477
3478exit:
3479 /* Fill the unused part of the output buffer (the whole buffer on error,
3480 * the trailing part on success) with something that isn't a valid mac
3481 * (barring an attack on the mac and deliberately-crafted input),
3482 * in case the caller doesn't check the return status properly. */
3483 if( status == PSA_SUCCESS )
3484 memset( signature + *signature_length, '!',
3485 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003486 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003487 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003488 /* If signature_size is 0 then we have nothing to do. We must not call
3489 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003490 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003491}
3492
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003493psa_status_t psa_verify_hash( psa_key_handle_t handle,
3494 psa_algorithm_t alg,
3495 const uint8_t *hash,
3496 size_t hash_length,
3497 const uint8_t *signature,
3498 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003499{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003500 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003501 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003502#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3503 const psa_drv_se_t *drv;
3504 psa_drv_se_context_t *drv_context;
3505#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003506
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003507 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003508 if( status != PSA_SUCCESS )
3509 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003510
Gilles Peskineedc64242019-08-07 21:05:07 +02003511#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3512 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3513 {
3514 if( drv->asymmetric == NULL ||
3515 drv->asymmetric->p_verify == NULL )
3516 return( PSA_ERROR_NOT_SUPPORTED );
3517 return( drv->asymmetric->p_verify( drv_context,
3518 slot->data.se.slot_number,
3519 alg,
3520 hash, hash_length,
3521 signature, signature_length ) );
3522 }
3523 else
3524#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003525#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003526 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003527 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003528 return( psa_rsa_verify( slot->data.rsa,
3529 alg,
3530 hash, hash_length,
3531 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003532 }
3533 else
3534#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003535#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003536 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003537 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003538#if defined(MBEDTLS_ECDSA_C)
3539 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003540 return( psa_ecdsa_verify( slot->data.ecp,
3541 hash, hash_length,
3542 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003543 else
3544#endif /* defined(MBEDTLS_ECDSA_C) */
3545 {
3546 return( PSA_ERROR_INVALID_ARGUMENT );
3547 }
itayzafrir5c753392018-05-08 11:18:38 +03003548 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003549 else
3550#endif /* defined(MBEDTLS_ECP_C) */
3551 {
3552 return( PSA_ERROR_NOT_SUPPORTED );
3553 }
3554}
3555
Gilles Peskine072ac562018-06-30 00:21:29 +02003556#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3557static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3558 mbedtls_rsa_context *rsa )
3559{
3560 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3561 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3562 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3563 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3564}
3565#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3566
Gilles Peskinec5487a82018-12-03 18:08:14 +01003567psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003568 psa_algorithm_t alg,
3569 const uint8_t *input,
3570 size_t input_length,
3571 const uint8_t *salt,
3572 size_t salt_length,
3573 uint8_t *output,
3574 size_t output_size,
3575 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003576{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003577 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003578 psa_status_t status;
3579
Darryl Green5cc689a2018-07-24 15:34:10 +01003580 (void) input;
3581 (void) input_length;
3582 (void) salt;
3583 (void) output;
3584 (void) output_size;
3585
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003586 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003587
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003588 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3589 return( PSA_ERROR_INVALID_ARGUMENT );
3590
Gilles Peskine28f8f302019-07-24 13:30:31 +02003591 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003592 if( status != PSA_SUCCESS )
3593 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003594 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3595 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003596 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003597
3598#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003599 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003600 {
3601 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003602 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003603 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003604 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003605#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003606 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003607 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003608 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3609 mbedtls_ctr_drbg_random,
3610 &global_data.ctr_drbg,
3611 MBEDTLS_RSA_PUBLIC,
3612 input_length,
3613 input,
3614 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003615 }
3616 else
3617#endif /* MBEDTLS_PKCS1_V15 */
3618#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003619 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003620 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003621 psa_rsa_oaep_set_padding_mode( alg, rsa );
3622 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3623 mbedtls_ctr_drbg_random,
3624 &global_data.ctr_drbg,
3625 MBEDTLS_RSA_PUBLIC,
3626 salt, salt_length,
3627 input_length,
3628 input,
3629 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003630 }
3631 else
3632#endif /* MBEDTLS_PKCS1_V21 */
3633 {
3634 return( PSA_ERROR_INVALID_ARGUMENT );
3635 }
3636 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003637 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003638 return( mbedtls_to_psa_error( ret ) );
3639 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003640 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003641#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003642 {
3643 return( PSA_ERROR_NOT_SUPPORTED );
3644 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003645}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003646
Gilles Peskinec5487a82018-12-03 18:08:14 +01003647psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003648 psa_algorithm_t alg,
3649 const uint8_t *input,
3650 size_t input_length,
3651 const uint8_t *salt,
3652 size_t salt_length,
3653 uint8_t *output,
3654 size_t output_size,
3655 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003656{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003657 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003658 psa_status_t status;
3659
Darryl Green5cc689a2018-07-24 15:34:10 +01003660 (void) input;
3661 (void) input_length;
3662 (void) salt;
3663 (void) output;
3664 (void) output_size;
3665
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003666 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003667
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003668 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3669 return( PSA_ERROR_INVALID_ARGUMENT );
3670
Gilles Peskine28f8f302019-07-24 13:30:31 +02003671 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003672 if( status != PSA_SUCCESS )
3673 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003674 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003675 return( PSA_ERROR_INVALID_ARGUMENT );
3676
3677#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003678 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003679 {
3680 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003681 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003682
Gilles Peskine630a18a2018-06-29 17:49:35 +02003683 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003684 return( PSA_ERROR_INVALID_ARGUMENT );
3685
3686#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003687 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003688 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003689 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3690 mbedtls_ctr_drbg_random,
3691 &global_data.ctr_drbg,
3692 MBEDTLS_RSA_PRIVATE,
3693 output_length,
3694 input,
3695 output,
3696 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003697 }
3698 else
3699#endif /* MBEDTLS_PKCS1_V15 */
3700#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003701 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003702 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003703 psa_rsa_oaep_set_padding_mode( alg, rsa );
3704 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3705 mbedtls_ctr_drbg_random,
3706 &global_data.ctr_drbg,
3707 MBEDTLS_RSA_PRIVATE,
3708 salt, salt_length,
3709 output_length,
3710 input,
3711 output,
3712 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003713 }
3714 else
3715#endif /* MBEDTLS_PKCS1_V21 */
3716 {
3717 return( PSA_ERROR_INVALID_ARGUMENT );
3718 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003719
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003720 return( mbedtls_to_psa_error( ret ) );
3721 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003722 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003723#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003724 {
3725 return( PSA_ERROR_NOT_SUPPORTED );
3726 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003727}
Gilles Peskine20035e32018-02-03 22:44:14 +01003728
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003729
3730
mohammad1603503973b2018-03-12 15:59:30 +02003731/****************************************************************/
3732/* Symmetric cryptography */
3733/****************************************************************/
3734
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003735/* Initialize the cipher operation structure. Once this function has been
3736 * called, psa_cipher_abort can run and will do the right thing. */
3737static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3738 psa_algorithm_t alg )
3739{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003740 if( ! PSA_ALG_IS_CIPHER( alg ) )
3741 {
3742 memset( operation, 0, sizeof( *operation ) );
3743 return( PSA_ERROR_INVALID_ARGUMENT );
3744 }
3745
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003746 operation->alg = alg;
3747 operation->key_set = 0;
3748 operation->iv_set = 0;
3749 operation->iv_required = 1;
3750 operation->iv_size = 0;
3751 operation->block_size = 0;
3752 mbedtls_cipher_init( &operation->ctx.cipher );
3753 return( PSA_SUCCESS );
3754}
3755
Gilles Peskinee553c652018-06-04 16:22:46 +02003756static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003757 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003758 psa_algorithm_t alg,
3759 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003760{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003761 int ret = 0;
3762 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003763 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003764 size_t key_bits;
3765 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003766 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3767 PSA_KEY_USAGE_ENCRYPT :
3768 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003769
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003770 /* A context must be freshly initialized before it can be set up. */
3771 if( operation->alg != 0 )
3772 {
3773 return( PSA_ERROR_BAD_STATE );
3774 }
3775
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003776 status = psa_cipher_init( operation, alg );
3777 if( status != PSA_SUCCESS )
3778 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003779
Gilles Peskine28f8f302019-07-24 13:30:31 +02003780 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003781 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003782 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003783 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003784
Gilles Peskine8e338702019-07-30 20:06:31 +02003785 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003786 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003787 {
3788 status = PSA_ERROR_NOT_SUPPORTED;
3789 goto exit;
3790 }
mohammad1603503973b2018-03-12 15:59:30 +02003791
mohammad1603503973b2018-03-12 15:59:30 +02003792 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003793 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003794 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003795
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003796#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003797 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003798 {
3799 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003800 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003801 memcpy( keys, slot->data.raw.data, 16 );
3802 memcpy( keys + 16, slot->data.raw.data, 8 );
3803 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3804 keys,
3805 192, cipher_operation );
3806 }
3807 else
3808#endif
3809 {
3810 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3811 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003812 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003813 }
Moran Peker41deec42018-04-04 15:43:05 +03003814 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003815 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003816
mohammad16038481e742018-03-18 13:57:31 +02003817#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003818 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003819 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003820 case PSA_ALG_CBC_NO_PADDING:
3821 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3822 MBEDTLS_PADDING_NONE );
3823 break;
3824 case PSA_ALG_CBC_PKCS7:
3825 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3826 MBEDTLS_PADDING_PKCS7 );
3827 break;
3828 default:
3829 /* The algorithm doesn't involve padding. */
3830 ret = 0;
3831 break;
3832 }
3833 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003834 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003835#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3836
mohammad1603503973b2018-03-12 15:59:30 +02003837 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003838 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003839 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003840 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003841 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003842 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003843 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003844#if defined(MBEDTLS_CHACHA20_C)
3845 else
3846 if( alg == PSA_ALG_CHACHA20 )
3847 operation->iv_size = 12;
3848#endif
mohammad1603503973b2018-03-12 15:59:30 +02003849
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003850exit:
3851 if( status == 0 )
3852 status = mbedtls_to_psa_error( ret );
3853 if( status != 0 )
3854 psa_cipher_abort( operation );
3855 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003856}
3857
Gilles Peskinefe119512018-07-08 21:39:34 +02003858psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003859 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003860 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003861{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003862 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003863}
3864
Gilles Peskinefe119512018-07-08 21:39:34 +02003865psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003866 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003867 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003868{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003869 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003870}
3871
Gilles Peskinefe119512018-07-08 21:39:34 +02003872psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003873 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003874 size_t iv_size,
3875 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003876{
itayzafrir534bd7c2018-08-02 13:56:32 +03003877 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003878 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003879 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003880 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003881 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003882 }
Moran Peker41deec42018-04-04 15:43:05 +03003883 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003884 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003885 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003886 goto exit;
3887 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003888 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3889 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003890 if( ret != 0 )
3891 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003892 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003893 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003894 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003895
mohammad16038481e742018-03-18 13:57:31 +02003896 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003897 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003898
Moran Peker395db872018-05-31 14:07:14 +03003899exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003900 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003901 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003902 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003903}
3904
Gilles Peskinefe119512018-07-08 21:39:34 +02003905psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003906 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003907 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003908{
itayzafrir534bd7c2018-08-02 13:56:32 +03003909 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003910 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003911 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003912 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003913 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003914 }
Moran Pekera28258c2018-05-29 16:25:04 +03003915 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003916 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003917 status = PSA_ERROR_INVALID_ARGUMENT;
3918 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003919 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003920 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3921 status = mbedtls_to_psa_error( ret );
3922exit:
3923 if( status == PSA_SUCCESS )
3924 operation->iv_set = 1;
3925 else
mohammad1603503973b2018-03-12 15:59:30 +02003926 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003927 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003928}
3929
Gilles Peskinee553c652018-06-04 16:22:46 +02003930psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3931 const uint8_t *input,
3932 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003933 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003934 size_t output_size,
3935 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003936{
itayzafrir534bd7c2018-08-02 13:56:32 +03003937 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003938 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003939 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003940
3941 if( operation->alg == 0 )
3942 {
3943 return( PSA_ERROR_BAD_STATE );
3944 }
3945
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003946 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003947 {
3948 /* Take the unprocessed partial block left over from previous
3949 * update calls, if any, plus the input to this call. Remove
3950 * the last partial block, if any. You get the data that will be
3951 * output in this call. */
3952 expected_output_size =
3953 ( operation->ctx.cipher.unprocessed_len + input_length )
3954 / operation->block_size * operation->block_size;
3955 }
3956 else
3957 {
3958 expected_output_size = input_length;
3959 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003960
Gilles Peskine89d789c2018-06-04 17:17:16 +02003961 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003962 {
3963 status = PSA_ERROR_BUFFER_TOO_SMALL;
3964 goto exit;
3965 }
mohammad160382759612018-03-12 18:16:40 +02003966
mohammad1603503973b2018-03-12 15:59:30 +02003967 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003968 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003969 status = mbedtls_to_psa_error( ret );
3970exit:
3971 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003972 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003973 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003974}
3975
Gilles Peskinee553c652018-06-04 16:22:46 +02003976psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3977 uint8_t *output,
3978 size_t output_size,
3979 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003980{
David Saadab4ecc272019-02-14 13:48:10 +02003981 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003982 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003983 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003984
mohammad1603503973b2018-03-12 15:59:30 +02003985 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003986 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003987 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003988 }
3989 if( operation->iv_required && ! operation->iv_set )
3990 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003991 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003992 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003993
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003994 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003995 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3996 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003997 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003998 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003999 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004000 }
4001
Janos Follath315b51c2018-07-09 16:04:51 +01004002 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4003 temp_output_buffer,
4004 output_length );
4005 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004006 {
Janos Follath315b51c2018-07-09 16:04:51 +01004007 status = mbedtls_to_psa_error( cipher_ret );
4008 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004009 }
Janos Follath315b51c2018-07-09 16:04:51 +01004010
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004011 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004012 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004013 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004014 memcpy( output, temp_output_buffer, *output_length );
4015 else
4016 {
Janos Follath315b51c2018-07-09 16:04:51 +01004017 status = PSA_ERROR_BUFFER_TOO_SMALL;
4018 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004019 }
mohammad1603503973b2018-03-12 15:59:30 +02004020
Gilles Peskine3f108122018-12-07 18:14:53 +01004021 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004022 status = psa_cipher_abort( operation );
4023
4024 return( status );
4025
4026error:
4027
4028 *output_length = 0;
4029
Gilles Peskine3f108122018-12-07 18:14:53 +01004030 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004031 (void) psa_cipher_abort( operation );
4032
4033 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004034}
4035
Gilles Peskinee553c652018-06-04 16:22:46 +02004036psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4037{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004038 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004039 {
4040 /* The object has (apparently) been initialized but it is not
4041 * in use. It's ok to call abort on such an object, and there's
4042 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004043 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004044 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004045
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004046 /* Sanity check (shouldn't happen: operation->alg should
4047 * always have been initialized to a valid value). */
4048 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4049 return( PSA_ERROR_BAD_STATE );
4050
mohammad1603503973b2018-03-12 15:59:30 +02004051 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004052
Moran Peker41deec42018-04-04 15:43:05 +03004053 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004054 operation->key_set = 0;
4055 operation->iv_set = 0;
4056 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004057 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004058 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004059
Moran Peker395db872018-05-31 14:07:14 +03004060 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004061}
4062
Gilles Peskinea0655c32018-04-30 17:06:50 +02004063
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004064
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004065
mohammad16035955c982018-04-26 00:53:03 +03004066/****************************************************************/
4067/* AEAD */
4068/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004069
Gilles Peskineedf9a652018-08-17 18:11:56 +02004070typedef struct
4071{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004072 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004073 const mbedtls_cipher_info_t *cipher_info;
4074 union
4075 {
4076#if defined(MBEDTLS_CCM_C)
4077 mbedtls_ccm_context ccm;
4078#endif /* MBEDTLS_CCM_C */
4079#if defined(MBEDTLS_GCM_C)
4080 mbedtls_gcm_context gcm;
4081#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004082#if defined(MBEDTLS_CHACHAPOLY_C)
4083 mbedtls_chachapoly_context chachapoly;
4084#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004085 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004086 psa_algorithm_t core_alg;
4087 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004088 uint8_t tag_length;
4089} aead_operation_t;
4090
Gilles Peskine30a9e412019-01-14 18:36:12 +01004091static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004092{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004093 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004094 {
4095#if defined(MBEDTLS_CCM_C)
4096 case PSA_ALG_CCM:
4097 mbedtls_ccm_free( &operation->ctx.ccm );
4098 break;
4099#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004100#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004101 case PSA_ALG_GCM:
4102 mbedtls_gcm_free( &operation->ctx.gcm );
4103 break;
4104#endif /* MBEDTLS_GCM_C */
4105 }
4106}
4107
4108static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004109 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004110 psa_key_usage_t usage,
4111 psa_algorithm_t alg )
4112{
4113 psa_status_t status;
4114 size_t key_bits;
4115 mbedtls_cipher_id_t cipher_id;
4116
Gilles Peskine28f8f302019-07-24 13:30:31 +02004117 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004118 if( status != PSA_SUCCESS )
4119 return( status );
4120
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004121 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004122
4123 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004124 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004125 &cipher_id );
4126 if( operation->cipher_info == NULL )
4127 return( PSA_ERROR_NOT_SUPPORTED );
4128
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004129 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004130 {
4131#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004132 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4133 operation->core_alg = PSA_ALG_CCM;
4134 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004135 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4136 * The call to mbedtls_ccm_encrypt_and_tag or
4137 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004138 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004139 return( PSA_ERROR_INVALID_ARGUMENT );
4140 mbedtls_ccm_init( &operation->ctx.ccm );
4141 status = mbedtls_to_psa_error(
4142 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4143 operation->slot->data.raw.data,
4144 (unsigned int) key_bits ) );
4145 if( status != 0 )
4146 goto cleanup;
4147 break;
4148#endif /* MBEDTLS_CCM_C */
4149
4150#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004151 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4152 operation->core_alg = PSA_ALG_GCM;
4153 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004154 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4155 * The call to mbedtls_gcm_crypt_and_tag or
4156 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004157 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004158 return( PSA_ERROR_INVALID_ARGUMENT );
4159 mbedtls_gcm_init( &operation->ctx.gcm );
4160 status = mbedtls_to_psa_error(
4161 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4162 operation->slot->data.raw.data,
4163 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004164 if( status != 0 )
4165 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004166 break;
4167#endif /* MBEDTLS_GCM_C */
4168
Gilles Peskine26869f22019-05-06 15:25:00 +02004169#if defined(MBEDTLS_CHACHAPOLY_C)
4170 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4171 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4172 operation->full_tag_length = 16;
4173 /* We only support the default tag length. */
4174 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4175 return( PSA_ERROR_NOT_SUPPORTED );
4176 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4177 status = mbedtls_to_psa_error(
4178 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4179 operation->slot->data.raw.data ) );
4180 if( status != 0 )
4181 goto cleanup;
4182 break;
4183#endif /* MBEDTLS_CHACHAPOLY_C */
4184
Gilles Peskineedf9a652018-08-17 18:11:56 +02004185 default:
4186 return( PSA_ERROR_NOT_SUPPORTED );
4187 }
4188
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004189 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4190 {
4191 status = PSA_ERROR_INVALID_ARGUMENT;
4192 goto cleanup;
4193 }
4194 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004195
Gilles Peskineedf9a652018-08-17 18:11:56 +02004196 return( PSA_SUCCESS );
4197
4198cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004199 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004200 return( status );
4201}
4202
Gilles Peskinec5487a82018-12-03 18:08:14 +01004203psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004204 psa_algorithm_t alg,
4205 const uint8_t *nonce,
4206 size_t nonce_length,
4207 const uint8_t *additional_data,
4208 size_t additional_data_length,
4209 const uint8_t *plaintext,
4210 size_t plaintext_length,
4211 uint8_t *ciphertext,
4212 size_t ciphertext_size,
4213 size_t *ciphertext_length )
4214{
mohammad16035955c982018-04-26 00:53:03 +03004215 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004216 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004217 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004218
mohammad1603f08a5502018-06-03 15:05:47 +03004219 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004220
Gilles Peskinec5487a82018-12-03 18:08:14 +01004221 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004222 if( status != PSA_SUCCESS )
4223 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004224
Gilles Peskineedf9a652018-08-17 18:11:56 +02004225 /* For all currently supported modes, the tag is at the end of the
4226 * ciphertext. */
4227 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4228 {
4229 status = PSA_ERROR_BUFFER_TOO_SMALL;
4230 goto exit;
4231 }
4232 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004233
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004234#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004235 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004236 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004237 status = mbedtls_to_psa_error(
4238 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4239 MBEDTLS_GCM_ENCRYPT,
4240 plaintext_length,
4241 nonce, nonce_length,
4242 additional_data, additional_data_length,
4243 plaintext, ciphertext,
4244 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004245 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004246 else
4247#endif /* MBEDTLS_GCM_C */
4248#if defined(MBEDTLS_CCM_C)
4249 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004250 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004251 status = mbedtls_to_psa_error(
4252 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4253 plaintext_length,
4254 nonce, nonce_length,
4255 additional_data,
4256 additional_data_length,
4257 plaintext, ciphertext,
4258 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004259 }
mohammad16035c8845f2018-05-09 05:40:09 -07004260 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004261#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004262#if defined(MBEDTLS_CHACHAPOLY_C)
4263 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4264 {
4265 if( nonce_length != 12 || operation.tag_length != 16 )
4266 {
4267 status = PSA_ERROR_NOT_SUPPORTED;
4268 goto exit;
4269 }
4270 status = mbedtls_to_psa_error(
4271 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4272 plaintext_length,
4273 nonce,
4274 additional_data,
4275 additional_data_length,
4276 plaintext,
4277 ciphertext,
4278 tag ) );
4279 }
4280 else
4281#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004282 {
mohammad1603554faad2018-06-03 15:07:38 +03004283 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004284 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004285
Gilles Peskineedf9a652018-08-17 18:11:56 +02004286 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4287 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004288
Gilles Peskineedf9a652018-08-17 18:11:56 +02004289exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004290 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004291 if( status == PSA_SUCCESS )
4292 *ciphertext_length = plaintext_length + operation.tag_length;
4293 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004294}
4295
Gilles Peskineee652a32018-06-01 19:23:52 +02004296/* Locate the tag in a ciphertext buffer containing the encrypted data
4297 * followed by the tag. Return the length of the part preceding the tag in
4298 * *plaintext_length. This is the size of the plaintext in modes where
4299 * the encrypted data has the same size as the plaintext, such as
4300 * CCM and GCM. */
4301static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4302 const uint8_t *ciphertext,
4303 size_t ciphertext_length,
4304 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004305 const uint8_t **p_tag )
4306{
4307 size_t payload_length;
4308 if( tag_length > ciphertext_length )
4309 return( PSA_ERROR_INVALID_ARGUMENT );
4310 payload_length = ciphertext_length - tag_length;
4311 if( payload_length > plaintext_size )
4312 return( PSA_ERROR_BUFFER_TOO_SMALL );
4313 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004314 return( PSA_SUCCESS );
4315}
4316
Gilles Peskinec5487a82018-12-03 18:08:14 +01004317psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004318 psa_algorithm_t alg,
4319 const uint8_t *nonce,
4320 size_t nonce_length,
4321 const uint8_t *additional_data,
4322 size_t additional_data_length,
4323 const uint8_t *ciphertext,
4324 size_t ciphertext_length,
4325 uint8_t *plaintext,
4326 size_t plaintext_size,
4327 size_t *plaintext_length )
4328{
mohammad16035955c982018-04-26 00:53:03 +03004329 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004330 aead_operation_t operation;
4331 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004332
Gilles Peskineee652a32018-06-01 19:23:52 +02004333 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004334
Gilles Peskinec5487a82018-12-03 18:08:14 +01004335 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004336 if( status != PSA_SUCCESS )
4337 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004338
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004339 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4340 ciphertext, ciphertext_length,
4341 plaintext_size, &tag );
4342 if( status != PSA_SUCCESS )
4343 goto exit;
4344
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004345#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004346 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004347 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004348 status = mbedtls_to_psa_error(
4349 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4350 ciphertext_length - operation.tag_length,
4351 nonce, nonce_length,
4352 additional_data,
4353 additional_data_length,
4354 tag, operation.tag_length,
4355 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004356 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004357 else
4358#endif /* MBEDTLS_GCM_C */
4359#if defined(MBEDTLS_CCM_C)
4360 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004361 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004362 status = mbedtls_to_psa_error(
4363 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4364 ciphertext_length - operation.tag_length,
4365 nonce, nonce_length,
4366 additional_data,
4367 additional_data_length,
4368 ciphertext, plaintext,
4369 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004370 }
mohammad160339574652018-06-01 04:39:53 -07004371 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004372#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004373#if defined(MBEDTLS_CHACHAPOLY_C)
4374 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4375 {
4376 if( nonce_length != 12 || operation.tag_length != 16 )
4377 {
4378 status = PSA_ERROR_NOT_SUPPORTED;
4379 goto exit;
4380 }
4381 status = mbedtls_to_psa_error(
4382 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4383 ciphertext_length - operation.tag_length,
4384 nonce,
4385 additional_data,
4386 additional_data_length,
4387 tag,
4388 ciphertext,
4389 plaintext ) );
4390 }
4391 else
4392#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004393 {
mohammad1603554faad2018-06-03 15:07:38 +03004394 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004395 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004396
Gilles Peskineedf9a652018-08-17 18:11:56 +02004397 if( status != PSA_SUCCESS && plaintext_size != 0 )
4398 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004399
Gilles Peskineedf9a652018-08-17 18:11:56 +02004400exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004401 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004402 if( status == PSA_SUCCESS )
4403 *plaintext_length = ciphertext_length - operation.tag_length;
4404 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004405}
4406
Gilles Peskinea0655c32018-04-30 17:06:50 +02004407
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004408
Gilles Peskine20035e32018-02-03 22:44:14 +01004409/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004410/* Generators */
4411/****************************************************************/
4412
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004413#define HKDF_STATE_INIT 0 /* no input yet */
4414#define HKDF_STATE_STARTED 1 /* got salt */
4415#define HKDF_STATE_KEYED 2 /* got key */
4416#define HKDF_STATE_OUTPUT 3 /* output started */
4417
Gilles Peskinecbe66502019-05-16 16:59:18 +02004418static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004419 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004420{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004421 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4422 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004423 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004424 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004425}
4426
4427
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004428psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004429{
4430 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004431 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004432 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004433 {
4434 /* The object has (apparently) been initialized but it is not
4435 * in use. It's ok to call abort on such an object, and there's
4436 * nothing to do. */
4437 }
4438 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004439#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004440 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004441 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004442 mbedtls_free( operation->ctx.hkdf.info );
4443 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004444 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004445 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004446 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004447 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004448 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004449 if( operation->ctx.tls12_prf.seed != NULL )
4450 {
4451 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4452 operation->ctx.tls12_prf.seed_length );
4453 mbedtls_free( operation->ctx.tls12_prf.seed );
4454 }
4455
4456 if( operation->ctx.tls12_prf.label != NULL )
4457 {
4458 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4459 operation->ctx.tls12_prf.label_length );
4460 mbedtls_free( operation->ctx.tls12_prf.label );
4461 }
4462
4463 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4464
4465 /* We leave the fields Ai and output_block to be erased safely by the
4466 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004467 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004468 else
4469#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004470 {
4471 status = PSA_ERROR_BAD_STATE;
4472 }
Janos Follath083036a2019-06-11 10:22:26 +01004473 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004474 return( status );
4475}
4476
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004477psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004478 size_t *capacity)
4479{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004480 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004481 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004482 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004483 return PSA_ERROR_BAD_STATE;
4484 }
4485
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004486 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004487 return( PSA_SUCCESS );
4488}
4489
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004490psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004491 size_t capacity )
4492{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004493 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004494 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004495 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004496 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004497 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004498 return( PSA_SUCCESS );
4499}
4500
Gilles Peskinebef7f142018-07-12 17:22:21 +02004501#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004502/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004503 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004504static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004505 psa_algorithm_t hash_alg,
4506 uint8_t *output,
4507 size_t output_length )
4508{
4509 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4510 psa_status_t status;
4511
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004512 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4513 return( PSA_ERROR_BAD_STATE );
4514 hkdf->state = HKDF_STATE_OUTPUT;
4515
Gilles Peskinebef7f142018-07-12 17:22:21 +02004516 while( output_length != 0 )
4517 {
4518 /* Copy what remains of the current block */
4519 uint8_t n = hash_length - hkdf->offset_in_block;
4520 if( n > output_length )
4521 n = (uint8_t) output_length;
4522 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4523 output += n;
4524 output_length -= n;
4525 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004526 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004527 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004528 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004529 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004530 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004531 * object was corrupted or if this function is called directly
4532 * inside the library. */
4533 if( hkdf->block_number == 0xff )
4534 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004535
4536 /* We need a new block */
4537 ++hkdf->block_number;
4538 hkdf->offset_in_block = 0;
4539 status = psa_hmac_setup_internal( &hkdf->hmac,
4540 hkdf->prk, hash_length,
4541 hash_alg );
4542 if( status != PSA_SUCCESS )
4543 return( status );
4544 if( hkdf->block_number != 1 )
4545 {
4546 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4547 hkdf->output_block,
4548 hash_length );
4549 if( status != PSA_SUCCESS )
4550 return( status );
4551 }
4552 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4553 hkdf->info,
4554 hkdf->info_length );
4555 if( status != PSA_SUCCESS )
4556 return( status );
4557 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4558 &hkdf->block_number, 1 );
4559 if( status != PSA_SUCCESS )
4560 return( status );
4561 status = psa_hmac_finish_internal( &hkdf->hmac,
4562 hkdf->output_block,
4563 sizeof( hkdf->output_block ) );
4564 if( status != PSA_SUCCESS )
4565 return( status );
4566 }
4567
4568 return( PSA_SUCCESS );
4569}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004570
Janos Follath7742fee2019-06-17 12:58:10 +01004571static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4572 psa_tls12_prf_key_derivation_t *tls12_prf,
4573 psa_algorithm_t alg )
4574{
4575 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4576 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004577 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4578 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004579
Janos Follath7742fee2019-06-17 12:58:10 +01004580 /* We can't be wanting more output after block 0xff, otherwise
4581 * the capacity check in psa_key_derivation_output_bytes() would have
4582 * prevented this call. It could happen only if the operation
4583 * object was corrupted or if this function is called directly
4584 * inside the library. */
4585 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004586 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004587
4588 /* We need a new block */
4589 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004590 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004591
4592 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4593 *
4594 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4595 *
4596 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4597 * HMAC_hash(secret, A(2) + seed) +
4598 * HMAC_hash(secret, A(3) + seed) + ...
4599 *
4600 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004601 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004602 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004603 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004604 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004605 * is currently extracted as `output_block` and where i is
4606 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004607 */
4608
Janos Follathea29bfb2019-06-19 12:21:20 +01004609 /* Save the hash context before using it, to preserve the hash state with
4610 * only the inner padding in it. We need this, because inner padding depends
4611 * on the key (secret in the RFC's terminology). */
4612 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4613 if( status != PSA_SUCCESS )
4614 goto cleanup;
4615
4616 /* Calculate A(i) where i = tls12_prf->block_number. */
4617 if( tls12_prf->block_number == 1 )
4618 {
4619 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4620 * the variable seed and in this instance means it in the context of the
4621 * P_hash function, where seed = label + seed.) */
4622 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4623 tls12_prf->label, tls12_prf->label_length );
4624 if( status != PSA_SUCCESS )
4625 goto cleanup;
4626 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4627 tls12_prf->seed, tls12_prf->seed_length );
4628 if( status != PSA_SUCCESS )
4629 goto cleanup;
4630 }
4631 else
4632 {
4633 /* A(i) = HMAC_hash(secret, A(i-1)) */
4634 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4635 tls12_prf->Ai, hash_length );
4636 if( status != PSA_SUCCESS )
4637 goto cleanup;
4638 }
4639
4640 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4641 tls12_prf->Ai, hash_length );
4642 if( status != PSA_SUCCESS )
4643 goto cleanup;
4644 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4645 if( status != PSA_SUCCESS )
4646 goto cleanup;
4647
4648 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4649 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4650 tls12_prf->Ai, hash_length );
4651 if( status != PSA_SUCCESS )
4652 goto cleanup;
4653 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4654 tls12_prf->label, tls12_prf->label_length );
4655 if( status != PSA_SUCCESS )
4656 goto cleanup;
4657 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4658 tls12_prf->seed, tls12_prf->seed_length );
4659 if( status != PSA_SUCCESS )
4660 goto cleanup;
4661 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4662 tls12_prf->output_block, hash_length );
4663 if( status != PSA_SUCCESS )
4664 goto cleanup;
4665 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4666 if( status != PSA_SUCCESS )
4667 goto cleanup;
4668
Janos Follath7742fee2019-06-17 12:58:10 +01004669
4670cleanup:
4671
Janos Follathea29bfb2019-06-19 12:21:20 +01004672 cleanup_status = psa_hash_abort( &backup );
4673 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4674 status = cleanup_status;
4675
4676 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004677}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004678
Janos Follath844eb0e2019-06-19 12:10:49 +01004679static psa_status_t psa_key_derivation_tls12_prf_read(
4680 psa_tls12_prf_key_derivation_t *tls12_prf,
4681 psa_algorithm_t alg,
4682 uint8_t *output,
4683 size_t output_length )
4684{
4685 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4686 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4687 psa_status_t status;
4688 uint8_t offset, length;
4689
4690 while( output_length != 0 )
4691 {
4692 /* Check if we have fully processed the current block. */
4693 if( tls12_prf->left_in_block == 0 )
4694 {
4695 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4696 alg );
4697 if( status != PSA_SUCCESS )
4698 return( status );
4699
4700 continue;
4701 }
4702
4703 if( tls12_prf->left_in_block > output_length )
4704 length = (uint8_t) output_length;
4705 else
4706 length = tls12_prf->left_in_block;
4707
4708 offset = hash_length - tls12_prf->left_in_block;
4709 memcpy( output, tls12_prf->output_block + offset, length );
4710 output += length;
4711 output_length -= length;
4712 tls12_prf->left_in_block -= length;
4713 }
4714
4715 return( PSA_SUCCESS );
4716}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004717#endif /* MBEDTLS_MD_C */
4718
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004719psa_status_t psa_key_derivation_output_bytes(
4720 psa_key_derivation_operation_t *operation,
4721 uint8_t *output,
4722 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004723{
4724 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004725 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004726
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004727 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004728 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004729 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004730 return PSA_ERROR_BAD_STATE;
4731 }
4732
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004733 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004734 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004735 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004736 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004737 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004738 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004739 goto exit;
4740 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004741 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004742 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004743 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004744 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004745 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4746 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004747 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004748 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004749 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004750 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004751 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004752
Gilles Peskinebef7f142018-07-12 17:22:21 +02004753#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004754 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004755 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004756 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004757 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004758 output, output_length );
4759 }
Janos Follathadbec812019-06-14 11:05:39 +01004760 else
Janos Follathadbec812019-06-14 11:05:39 +01004761 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004762 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004763 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004764 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004765 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004766 output_length );
4767 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004768 else
4769#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004770 {
4771 return( PSA_ERROR_BAD_STATE );
4772 }
4773
4774exit:
4775 if( status != PSA_SUCCESS )
4776 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004777 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004778 * This allows us to differentiate between exhausted operations and
4779 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4780 * operations. */
4781 psa_algorithm_t alg = operation->alg;
4782 psa_key_derivation_abort( operation );
4783 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004784 memset( output, '!', output_length );
4785 }
4786 return( status );
4787}
4788
Gilles Peskine08542d82018-07-19 17:05:42 +02004789#if defined(MBEDTLS_DES_C)
4790static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4791{
4792 if( data_size >= 8 )
4793 mbedtls_des_key_set_parity( data );
4794 if( data_size >= 16 )
4795 mbedtls_des_key_set_parity( data + 8 );
4796 if( data_size >= 24 )
4797 mbedtls_des_key_set_parity( data + 16 );
4798}
4799#endif /* MBEDTLS_DES_C */
4800
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004801static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004802 psa_key_slot_t *slot,
4803 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004804 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004805{
4806 uint8_t *data = NULL;
4807 size_t bytes = PSA_BITS_TO_BYTES( bits );
4808 psa_status_t status;
4809
Gilles Peskine8e338702019-07-30 20:06:31 +02004810 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004811 return( PSA_ERROR_INVALID_ARGUMENT );
4812 if( bits % 8 != 0 )
4813 return( PSA_ERROR_INVALID_ARGUMENT );
4814 data = mbedtls_calloc( 1, bytes );
4815 if( data == NULL )
4816 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4817
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004818 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004819 if( status != PSA_SUCCESS )
4820 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004821#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004822 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004823 psa_des_set_key_parity( data, bytes );
4824#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004825 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004826
4827exit:
4828 mbedtls_free( data );
4829 return( status );
4830}
4831
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004832psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004833 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004834 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004835{
4836 psa_status_t status;
4837 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004838 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004839
4840 /* Reject any attempt to create a zero-length key so that we don't
4841 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4842 if( psa_get_key_bits( attributes ) == 0 )
4843 return( PSA_ERROR_INVALID_ARGUMENT );
4844
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004845 if( ! operation->can_output_key )
4846 return( PSA_ERROR_NOT_PERMITTED );
4847
Gilles Peskinedf179142019-07-15 22:02:14 +02004848 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4849 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004850#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4851 if( driver != NULL )
4852 {
4853 /* Deriving a key in a secure element is not implemented yet. */
4854 status = PSA_ERROR_NOT_SUPPORTED;
4855 }
4856#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004857 if( status == PSA_SUCCESS )
4858 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004859 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004860 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004861 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004862 }
4863 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004864 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004865 if( status != PSA_SUCCESS )
4866 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004867 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004868 *handle = 0;
4869 }
4870 return( status );
4871}
4872
Gilles Peskineeab56e42018-07-12 17:12:33 +02004873
4874
4875/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004876/* Key derivation */
4877/****************************************************************/
4878
Gilles Peskine969c5d62019-01-16 15:53:06 +01004879static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004880 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004881 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004882{
Janos Follath5fe19732019-06-20 15:09:30 +01004883 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4884 * initialisers for this union leave some bytes unspecified.) */
4885 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4886
Gilles Peskine969c5d62019-01-16 15:53:06 +01004887 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004888#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004889 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4890 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4891 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004892 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004893 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004894 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4895 if( hash_size == 0 )
4896 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004897 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4898 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004899 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004900 {
4901 return( PSA_ERROR_NOT_SUPPORTED );
4902 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004903 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004904 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004905 }
4906#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004907 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004908 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004909}
4910
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004911psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004912 psa_algorithm_t alg )
4913{
4914 psa_status_t status;
4915
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004916 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004917 return( PSA_ERROR_BAD_STATE );
4918
Gilles Peskine6843c292019-01-18 16:44:49 +01004919 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4920 return( PSA_ERROR_INVALID_ARGUMENT );
4921 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004922 {
4923 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004924 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004925 }
4926 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4927 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004928 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004929 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004930 else
4931 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004932
4933 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004934 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004935 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004936}
4937
4938#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004939static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004940 psa_algorithm_t hash_alg,
4941 psa_key_derivation_step_t step,
4942 const uint8_t *data,
4943 size_t data_length )
4944{
4945 psa_status_t status;
4946 switch( step )
4947 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004948 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004949 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004950 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004951 status = psa_hmac_setup_internal( &hkdf->hmac,
4952 data, data_length,
4953 hash_alg );
4954 if( status != PSA_SUCCESS )
4955 return( status );
4956 hkdf->state = HKDF_STATE_STARTED;
4957 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004958 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004959 /* If no salt was provided, use an empty salt. */
4960 if( hkdf->state == HKDF_STATE_INIT )
4961 {
4962 status = psa_hmac_setup_internal( &hkdf->hmac,
4963 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004964 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004965 if( status != PSA_SUCCESS )
4966 return( status );
4967 hkdf->state = HKDF_STATE_STARTED;
4968 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004969 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004970 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004971 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4972 data, data_length );
4973 if( status != PSA_SUCCESS )
4974 return( status );
4975 status = psa_hmac_finish_internal( &hkdf->hmac,
4976 hkdf->prk,
4977 sizeof( hkdf->prk ) );
4978 if( status != PSA_SUCCESS )
4979 return( status );
4980 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4981 hkdf->block_number = 0;
4982 hkdf->state = HKDF_STATE_KEYED;
4983 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004984 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004985 if( hkdf->state == HKDF_STATE_OUTPUT )
4986 return( PSA_ERROR_BAD_STATE );
4987 if( hkdf->info_set )
4988 return( PSA_ERROR_BAD_STATE );
4989 hkdf->info_length = data_length;
4990 if( data_length != 0 )
4991 {
4992 hkdf->info = mbedtls_calloc( 1, data_length );
4993 if( hkdf->info == NULL )
4994 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4995 memcpy( hkdf->info, data, data_length );
4996 }
4997 hkdf->info_set = 1;
4998 return( PSA_SUCCESS );
4999 default:
5000 return( PSA_ERROR_INVALID_ARGUMENT );
5001 }
5002}
Janos Follathb03233e2019-06-11 15:30:30 +01005003
Janos Follathf08e2652019-06-13 09:05:41 +01005004static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5005 const uint8_t *data,
5006 size_t data_length )
5007{
5008 if( prf->state != TLS12_PRF_STATE_INIT )
5009 return( PSA_ERROR_BAD_STATE );
5010
Janos Follathd6dce9f2019-07-04 09:11:38 +01005011 if( data_length != 0 )
5012 {
5013 prf->seed = mbedtls_calloc( 1, data_length );
5014 if( prf->seed == NULL )
5015 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005016
Janos Follathd6dce9f2019-07-04 09:11:38 +01005017 memcpy( prf->seed, data, data_length );
5018 prf->seed_length = data_length;
5019 }
Janos Follathf08e2652019-06-13 09:05:41 +01005020
5021 prf->state = TLS12_PRF_STATE_SEED_SET;
5022
5023 return( PSA_SUCCESS );
5024}
5025
Janos Follath81550542019-06-13 14:26:34 +01005026static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5027 psa_algorithm_t hash_alg,
5028 const uint8_t *data,
5029 size_t data_length )
5030{
5031 psa_status_t status;
5032 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5033 return( PSA_ERROR_BAD_STATE );
5034
5035 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5036 if( status != PSA_SUCCESS )
5037 return( status );
5038
5039 prf->state = TLS12_PRF_STATE_KEY_SET;
5040
5041 return( PSA_SUCCESS );
5042}
5043
Janos Follath6660f0e2019-06-17 08:44:03 +01005044static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5045 psa_tls12_prf_key_derivation_t *prf,
5046 psa_algorithm_t hash_alg,
5047 const uint8_t *data,
5048 size_t data_length )
5049{
5050 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005051 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5052 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005053
5054 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5055 return( PSA_ERROR_INVALID_ARGUMENT );
5056
5057 /* Quoting RFC 4279, Section 2:
5058 *
5059 * The premaster secret is formed as follows: if the PSK is N octets
5060 * long, concatenate a uint16 with the value N, N zero octets, a second
5061 * uint16 with the value N, and the PSK itself.
5062 */
5063
Janos Follath40e13932019-06-26 13:22:29 +01005064 *cur++ = ( data_length >> 8 ) & 0xff;
5065 *cur++ = ( data_length >> 0 ) & 0xff;
5066 memset( cur, 0, data_length );
5067 cur += data_length;
5068 *cur++ = pms[0];
5069 *cur++ = pms[1];
5070 memcpy( cur, data, data_length );
5071 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005072
Janos Follath40e13932019-06-26 13:22:29 +01005073 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005074
5075 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5076 return( status );
5077}
5078
Janos Follath63028dd2019-06-13 09:15:47 +01005079static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5080 const uint8_t *data,
5081 size_t data_length )
5082{
5083 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5084 return( PSA_ERROR_BAD_STATE );
5085
Janos Follathd6dce9f2019-07-04 09:11:38 +01005086 if( data_length != 0 )
5087 {
5088 prf->label = mbedtls_calloc( 1, data_length );
5089 if( prf->label == NULL )
5090 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005091
Janos Follathd6dce9f2019-07-04 09:11:38 +01005092 memcpy( prf->label, data, data_length );
5093 prf->label_length = data_length;
5094 }
Janos Follath63028dd2019-06-13 09:15:47 +01005095
5096 prf->state = TLS12_PRF_STATE_LABEL_SET;
5097
5098 return( PSA_SUCCESS );
5099}
5100
Janos Follathb03233e2019-06-11 15:30:30 +01005101static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5102 psa_algorithm_t hash_alg,
5103 psa_key_derivation_step_t step,
5104 const uint8_t *data,
5105 size_t data_length )
5106{
Janos Follathb03233e2019-06-11 15:30:30 +01005107 switch( step )
5108 {
Janos Follathf08e2652019-06-13 09:05:41 +01005109 case PSA_KEY_DERIVATION_INPUT_SEED:
5110 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005111 case PSA_KEY_DERIVATION_INPUT_SECRET:
5112 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005113 case PSA_KEY_DERIVATION_INPUT_LABEL:
5114 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005115 default:
5116 return( PSA_ERROR_INVALID_ARGUMENT );
5117 }
5118}
Janos Follath6660f0e2019-06-17 08:44:03 +01005119
5120static psa_status_t psa_tls12_prf_psk_to_ms_input(
5121 psa_tls12_prf_key_derivation_t *prf,
5122 psa_algorithm_t hash_alg,
5123 psa_key_derivation_step_t step,
5124 const uint8_t *data,
5125 size_t data_length )
5126{
5127 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005128 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005129 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5130 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005131 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005132
5133 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5134}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005135#endif /* MBEDTLS_MD_C */
5136
Gilles Peskineb8965192019-09-24 16:21:10 +02005137/** Check whether the given key type is acceptable for the given
5138 * input step of a key derivation.
5139 *
5140 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5141 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5142 * Both secret and non-secret inputs can alternatively have the type
5143 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5144 * that the input was passed as a buffer rather than via a key object.
5145 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005146static int psa_key_derivation_check_input_type(
5147 psa_key_derivation_step_t step,
5148 psa_key_type_t key_type )
5149{
5150 switch( step )
5151 {
5152 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005153 if( key_type == PSA_KEY_TYPE_DERIVE )
5154 return( PSA_SUCCESS );
5155 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005156 return( PSA_SUCCESS );
5157 break;
5158 case PSA_KEY_DERIVATION_INPUT_LABEL:
5159 case PSA_KEY_DERIVATION_INPUT_SALT:
5160 case PSA_KEY_DERIVATION_INPUT_INFO:
5161 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005162 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5163 return( PSA_SUCCESS );
5164 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005165 return( PSA_SUCCESS );
5166 break;
5167 }
5168 return( PSA_ERROR_INVALID_ARGUMENT );
5169}
5170
Janos Follathb80a94e2019-06-12 15:54:46 +01005171static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005172 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005173 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005174 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005175 const uint8_t *data,
5176 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005177{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005178 psa_status_t status;
5179 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5180
5181 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005182 if( status != PSA_SUCCESS )
5183 goto exit;
5184
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005185#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005186 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005187 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005188 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005189 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005190 step, data, data_length );
5191 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005192 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005193 {
Janos Follathb03233e2019-06-11 15:30:30 +01005194 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5195 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5196 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005197 }
5198 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5199 {
5200 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5201 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5202 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005203 }
5204 else
5205#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005206 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005207 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005208 return( PSA_ERROR_BAD_STATE );
5209 }
5210
Gilles Peskine224b0d62019-09-23 18:13:17 +02005211exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005212 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005213 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005214 return( status );
5215}
5216
Janos Follath51f4a0f2019-06-14 11:35:55 +01005217psa_status_t psa_key_derivation_input_bytes(
5218 psa_key_derivation_operation_t *operation,
5219 psa_key_derivation_step_t step,
5220 const uint8_t *data,
5221 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005222{
Gilles Peskineb8965192019-09-24 16:21:10 +02005223 return( psa_key_derivation_input_internal( operation, step,
5224 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005225 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005226}
5227
Janos Follath51f4a0f2019-06-14 11:35:55 +01005228psa_status_t psa_key_derivation_input_key(
5229 psa_key_derivation_operation_t *operation,
5230 psa_key_derivation_step_t step,
5231 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005232{
5233 psa_key_slot_t *slot;
5234 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005235
Gilles Peskine28f8f302019-07-24 13:30:31 +02005236 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005237 PSA_KEY_USAGE_DERIVE,
5238 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005239 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005240 {
5241 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005242 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005243 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005244
5245 /* Passing a key object as a SECRET input unlocks the permission
5246 * to output to a key object. */
5247 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5248 operation->can_output_key = 1;
5249
Janos Follathb80a94e2019-06-12 15:54:46 +01005250 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005251 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005252 slot->data.raw.data,
5253 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005254}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005255
5256
5257
5258/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005259/* Key agreement */
5260/****************************************************************/
5261
Gilles Peskinea05219c2018-11-16 16:02:56 +01005262#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005263static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5264 size_t peer_key_length,
5265 const mbedtls_ecp_keypair *our_key,
5266 uint8_t *shared_secret,
5267 size_t shared_secret_size,
5268 size_t *shared_secret_length )
5269{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005270 mbedtls_ecp_keypair *their_key = NULL;
5271 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005272 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005273 size_t bits = 0;
5274 psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005275 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005276
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005277 status = psa_import_ec_public_key( curve,
5278 peer_key, peer_key_length,
5279 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005280 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005281 goto exit;
5282
Jaeden Amero97271b32019-01-10 19:38:51 +00005283 status = mbedtls_to_psa_error(
5284 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5285 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005286 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005287 status = mbedtls_to_psa_error(
5288 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5289 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005290 goto exit;
5291
Jaeden Amero97271b32019-01-10 19:38:51 +00005292 status = mbedtls_to_psa_error(
5293 mbedtls_ecdh_calc_secret( &ecdh,
5294 shared_secret_length,
5295 shared_secret, shared_secret_size,
5296 mbedtls_ctr_drbg_random,
5297 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005298 if( status != PSA_SUCCESS )
5299 goto exit;
5300 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5301 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005302
5303exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005304 if( status != PSA_SUCCESS )
5305 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005306 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005307 mbedtls_ecp_keypair_free( their_key );
5308 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005309 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005310}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005311#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005312
Gilles Peskine01d718c2018-09-18 12:01:02 +02005313#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5314
Gilles Peskine0216fe12019-04-11 21:23:21 +02005315static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5316 psa_key_slot_t *private_key,
5317 const uint8_t *peer_key,
5318 size_t peer_key_length,
5319 uint8_t *shared_secret,
5320 size_t shared_secret_size,
5321 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005322{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005323 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005324 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005325#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005326 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005327 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005328 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005329 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5330 private_key->data.ecp,
5331 shared_secret, shared_secret_size,
5332 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005333#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005334 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005335 (void) private_key;
5336 (void) peer_key;
5337 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005338 (void) shared_secret;
5339 (void) shared_secret_size;
5340 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005341 return( PSA_ERROR_NOT_SUPPORTED );
5342 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005343}
5344
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005345/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005346 * to potentially free embedded data structures and wipe confidential data.
5347 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005348static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005349 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005350 psa_key_slot_t *private_key,
5351 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005352 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005353{
5354 psa_status_t status;
5355 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5356 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005357 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005358
5359 /* Step 1: run the secret agreement algorithm to generate the shared
5360 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005361 status = psa_key_agreement_raw_internal( ka_alg,
5362 private_key,
5363 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005364 shared_secret,
5365 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005366 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005367 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005368 goto exit;
5369
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005370 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005371 * the shared secret. A shared secret is permitted wherever a key
5372 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005373 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005374 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005375 shared_secret,
5376 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005377
Gilles Peskine12313cd2018-06-20 00:20:32 +02005378exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005379 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005380 return( status );
5381}
5382
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005383psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005384 psa_key_derivation_step_t step,
5385 psa_key_handle_t private_key,
5386 const uint8_t *peer_key,
5387 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005388{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005389 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005390 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005391 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005392 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005393 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005394 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005395 if( status != PSA_SUCCESS )
5396 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005397 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005398 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005399 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005400 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005401 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005402 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005403}
5404
Gilles Peskinebe697d82019-05-16 18:00:41 +02005405psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5406 psa_key_handle_t private_key,
5407 const uint8_t *peer_key,
5408 size_t peer_key_length,
5409 uint8_t *output,
5410 size_t output_size,
5411 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005412{
5413 psa_key_slot_t *slot;
5414 psa_status_t status;
5415
5416 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5417 {
5418 status = PSA_ERROR_INVALID_ARGUMENT;
5419 goto exit;
5420 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005421 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005422 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005423 if( status != PSA_SUCCESS )
5424 goto exit;
5425
5426 status = psa_key_agreement_raw_internal( alg, slot,
5427 peer_key, peer_key_length,
5428 output, output_size,
5429 output_length );
5430
5431exit:
5432 if( status != PSA_SUCCESS )
5433 {
5434 /* If an error happens and is not handled properly, the output
5435 * may be used as a key to protect sensitive data. Arrange for such
5436 * a key to be random, which is likely to result in decryption or
5437 * verification errors. This is better than filling the buffer with
5438 * some constant data such as zeros, which would result in the data
5439 * being protected with a reproducible, easily knowable key.
5440 */
5441 psa_generate_random( output, output_size );
5442 *output_length = output_size;
5443 }
5444 return( status );
5445}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005446
5447
5448/****************************************************************/
5449/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005450/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005451
Gilles Peskine4c317f42018-07-12 01:24:09 +02005452psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005453 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005454{
Janos Follath24eed8d2019-11-22 13:21:35 +00005455 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005456 GUARD_MODULE_INITIALIZED;
5457
Gilles Peskinef181eca2019-08-07 13:49:00 +02005458 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5459 {
5460 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5461 output,
5462 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5463 if( ret != 0 )
5464 return( mbedtls_to_psa_error( ret ) );
5465 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5466 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5467 }
5468
Gilles Peskinee59236f2018-01-27 23:32:46 +01005469 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5470 return( mbedtls_to_psa_error( ret ) );
5471}
5472
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005473#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5474#include "mbedtls/entropy_poll.h"
5475
Gilles Peskine7228da22019-07-15 11:06:15 +02005476psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005477 size_t seed_size )
5478{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005479 if( global_data.initialized )
5480 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005481
5482 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5483 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5484 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5485 return( PSA_ERROR_INVALID_ARGUMENT );
5486
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005487 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005488}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005489#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005490
Gilles Peskinee56e8782019-04-26 17:34:02 +02005491#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5492static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5493 size_t domain_parameters_size,
5494 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005495{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005496 size_t i;
5497 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005498
Gilles Peskinee56e8782019-04-26 17:34:02 +02005499 if( domain_parameters_size == 0 )
5500 {
5501 *exponent = 65537;
5502 return( PSA_SUCCESS );
5503 }
5504
5505 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5506 * support values that fit in a 32-bit integer, which is larger than
5507 * int on just about every platform anyway. */
5508 if( domain_parameters_size > sizeof( acc ) )
5509 return( PSA_ERROR_NOT_SUPPORTED );
5510 for( i = 0; i < domain_parameters_size; i++ )
5511 acc = ( acc << 8 ) | domain_parameters[i];
5512 if( acc > INT_MAX )
5513 return( PSA_ERROR_NOT_SUPPORTED );
5514 *exponent = acc;
5515 return( PSA_SUCCESS );
5516}
5517#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5518
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005519static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005520 psa_key_slot_t *slot, size_t bits,
5521 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005522{
Gilles Peskine8e338702019-07-30 20:06:31 +02005523 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005524
Gilles Peskinee56e8782019-04-26 17:34:02 +02005525 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005526 return( PSA_ERROR_INVALID_ARGUMENT );
5527
Gilles Peskinee59236f2018-01-27 23:32:46 +01005528 if( key_type_is_raw_bytes( type ) )
5529 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005530 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005531 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5532 if( status != PSA_SUCCESS )
5533 return( status );
5534 status = psa_generate_random( slot->data.raw.data,
5535 slot->data.raw.bytes );
5536 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005537 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005538#if defined(MBEDTLS_DES_C)
5539 if( type == PSA_KEY_TYPE_DES )
5540 psa_des_set_key_parity( slot->data.raw.data,
5541 slot->data.raw.bytes );
5542#endif /* MBEDTLS_DES_C */
5543 }
5544 else
5545
5546#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005547 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005548 {
5549 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005550 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005551 int exponent;
5552 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005553 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5554 return( PSA_ERROR_NOT_SUPPORTED );
5555 /* Accept only byte-aligned keys, for the same reasons as
5556 * in psa_import_rsa_key(). */
5557 if( bits % 8 != 0 )
5558 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005559 status = psa_read_rsa_exponent( domain_parameters,
5560 domain_parameters_size,
5561 &exponent );
5562 if( status != PSA_SUCCESS )
5563 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005564 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5565 if( rsa == NULL )
5566 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5567 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5568 ret = mbedtls_rsa_gen_key( rsa,
5569 mbedtls_ctr_drbg_random,
5570 &global_data.ctr_drbg,
5571 (unsigned int) bits,
5572 exponent );
5573 if( ret != 0 )
5574 {
5575 mbedtls_rsa_free( rsa );
5576 mbedtls_free( rsa );
5577 return( mbedtls_to_psa_error( ret ) );
5578 }
5579 slot->data.rsa = rsa;
5580 }
5581 else
5582#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5583
5584#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005585 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005586 {
5587 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005588 mbedtls_ecp_group_id grp_id =
5589 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005590 const mbedtls_ecp_curve_info *curve_info =
5591 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5592 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005593 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005594 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005595 return( PSA_ERROR_NOT_SUPPORTED );
5596 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5597 return( PSA_ERROR_NOT_SUPPORTED );
5598 if( curve_info->bit_size != bits )
5599 return( PSA_ERROR_INVALID_ARGUMENT );
5600 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5601 if( ecp == NULL )
5602 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5603 mbedtls_ecp_keypair_init( ecp );
5604 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5605 mbedtls_ctr_drbg_random,
5606 &global_data.ctr_drbg );
5607 if( ret != 0 )
5608 {
5609 mbedtls_ecp_keypair_free( ecp );
5610 mbedtls_free( ecp );
5611 return( mbedtls_to_psa_error( ret ) );
5612 }
5613 slot->data.ecp = ecp;
5614 }
5615 else
5616#endif /* MBEDTLS_ECP_C */
5617
5618 return( PSA_ERROR_NOT_SUPPORTED );
5619
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005620 return( PSA_SUCCESS );
5621}
Darryl Green0c6575a2018-11-07 16:05:30 +00005622
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005623psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005624 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005625{
5626 psa_status_t status;
5627 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005628 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005629
Gilles Peskine0f84d622019-09-12 19:03:13 +02005630 /* Reject any attempt to create a zero-length key so that we don't
5631 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5632 if( psa_get_key_bits( attributes ) == 0 )
5633 return( PSA_ERROR_INVALID_ARGUMENT );
5634
Gilles Peskinedf179142019-07-15 22:02:14 +02005635 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5636 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005637 if( status != PSA_SUCCESS )
5638 goto exit;
5639
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005640#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5641 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005642 {
Gilles Peskine11792082019-08-06 18:36:36 +02005643 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5644 size_t pubkey_length = 0; /* We don't support this feature yet */
5645 if( drv->key_management == NULL ||
5646 drv->key_management->p_generate == NULL )
5647 {
5648 status = PSA_ERROR_NOT_SUPPORTED;
5649 goto exit;
5650 }
5651 status = drv->key_management->p_generate(
5652 psa_get_se_driver_context( driver ),
5653 slot->data.se.slot_number, attributes,
5654 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005655 }
Gilles Peskine11792082019-08-06 18:36:36 +02005656 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005657#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005658 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005659 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005660 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005661 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005662 }
Gilles Peskine11792082019-08-06 18:36:36 +02005663
5664exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005665 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005666 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005667 if( status != PSA_SUCCESS )
5668 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005669 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005670 *handle = 0;
5671 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005672 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005673}
5674
5675
Gilles Peskinee59236f2018-01-27 23:32:46 +01005676
5677/****************************************************************/
5678/* Module setup */
5679/****************************************************************/
5680
Gilles Peskine5e769522018-11-20 21:59:56 +01005681psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5682 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5683 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5684{
5685 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5686 return( PSA_ERROR_BAD_STATE );
5687 global_data.entropy_init = entropy_init;
5688 global_data.entropy_free = entropy_free;
5689 return( PSA_SUCCESS );
5690}
5691
Gilles Peskinee59236f2018-01-27 23:32:46 +01005692void mbedtls_psa_crypto_free( void )
5693{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005694 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005695 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5696 {
5697 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005698 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005699 }
5700 /* Wipe all remaining data, including configuration.
5701 * In particular, this sets all state indicator to the value
5702 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005703 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005704#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005705 /* Unregister all secure element drivers, so that we restart from
5706 * a pristine state. */
5707 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005708#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005709}
5710
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005711#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5712/** Recover a transaction that was interrupted by a power failure.
5713 *
5714 * This function is called during initialization, before psa_crypto_init()
5715 * returns. If this function returns a failure status, the initialization
5716 * fails.
5717 */
5718static psa_status_t psa_crypto_recover_transaction(
5719 const psa_crypto_transaction_t *transaction )
5720{
5721 switch( transaction->unknown.type )
5722 {
5723 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5724 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005725 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005726 * is implemented.
5727 * https://github.com/ARMmbed/mbed-crypto/issues/218
5728 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005729 default:
5730 /* We found an unsupported transaction in the storage.
5731 * We don't know what state the storage is in. Give up. */
5732 return( PSA_ERROR_STORAGE_FAILURE );
5733 }
5734}
5735#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5736
Gilles Peskinee59236f2018-01-27 23:32:46 +01005737psa_status_t psa_crypto_init( void )
5738{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005739 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005740 const unsigned char drbg_seed[] = "PSA";
5741
Gilles Peskinec6b69072018-11-20 21:42:52 +01005742 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005743 if( global_data.initialized != 0 )
5744 return( PSA_SUCCESS );
5745
Gilles Peskine5e769522018-11-20 21:59:56 +01005746 /* Set default configuration if
5747 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5748 if( global_data.entropy_init == NULL )
5749 global_data.entropy_init = mbedtls_entropy_init;
5750 if( global_data.entropy_free == NULL )
5751 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005752
Gilles Peskinec6b69072018-11-20 21:42:52 +01005753 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005754 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005755#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5756 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5757 /* The PSA entropy injection feature depends on using NV seed as an entropy
5758 * source. Add NV seed as an entropy source for PSA entropy injection. */
5759 mbedtls_entropy_add_source( &global_data.entropy,
5760 mbedtls_nv_seed_poll, NULL,
5761 MBEDTLS_ENTROPY_BLOCK_SIZE,
5762 MBEDTLS_ENTROPY_SOURCE_STRONG );
5763#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005764 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005765 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005766 status = mbedtls_to_psa_error(
5767 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5768 mbedtls_entropy_func,
5769 &global_data.entropy,
5770 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5771 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005772 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005773 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005774
Gilles Peskine66fb1262018-12-10 16:29:04 +01005775 status = psa_initialize_key_slots( );
5776 if( status != PSA_SUCCESS )
5777 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005778
Gilles Peskined9348f22019-10-01 15:22:29 +02005779#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5780 status = psa_init_all_se_drivers( );
5781 if( status != PSA_SUCCESS )
5782 goto exit;
5783#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5784
Gilles Peskine4b734222019-07-24 15:56:31 +02005785#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005786 status = psa_crypto_load_transaction( );
5787 if( status == PSA_SUCCESS )
5788 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005789 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5790 if( status != PSA_SUCCESS )
5791 goto exit;
5792 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005793 }
5794 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5795 {
5796 /* There's no transaction to complete. It's all good. */
5797 status = PSA_SUCCESS;
5798 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005799#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005800
Gilles Peskinec6b69072018-11-20 21:42:52 +01005801 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005802 global_data.initialized = 1;
5803
5804exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005805 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005806 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005807 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005808}
5809
5810#endif /* MBEDTLS_PSA_CRYPTO_C */