blob: 554da4bb4722c839a5069b1d00c58c829031d83b [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030029/*
30 * In case MBEDTLS_PSA_CRYPTO_SPM is defined the code is built for SPM (Secure
31 * Partition Manager) integration which separate the code into two parts
32 * NSPE (Non-Secure Process Environment) and SPE (Secure Process Environment).
33 * In this mode an additional header file should be included.
34 */
mohammad160327010052018-07-03 13:16:15 +030035#if defined(MBEDTLS_PSA_CRYPTO_SPM)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030036/*
37 * PSA_CRYPTO_SECURE means that this file is compiled to the SPE side.
38 * some headers will be affected by this flag.
39 */
mohammad160327010052018-07-03 13:16:15 +030040#define PSA_CRYPTO_SECURE 1
41#include "crypto_spe.h"
42#endif
43
Gilles Peskinee59236f2018-01-27 23:32:46 +010044#include "psa/crypto.h"
45
Gilles Peskine039b90c2018-12-07 18:24:41 +010046#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010047#include "psa_crypto_invasive.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010048#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010049/* Include internal declarations that are useful for implementing persistently
50 * stored keys. */
51#include "psa_crypto_storage.h"
52
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#include <stdlib.h>
54#include <string.h>
55#if defined(MBEDTLS_PLATFORM_C)
56#include "mbedtls/platform.h"
57#else
58#define mbedtls_calloc calloc
59#define mbedtls_free free
60#endif
61
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020063#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020064#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010065#include "mbedtls/blowfish.h"
66#include "mbedtls/camellia.h"
67#include "mbedtls/cipher.h"
68#include "mbedtls/ccm.h"
69#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010070#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010071#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020072#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010073#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010074#include "mbedtls/entropy.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020075#include "mbedtls/entropy_poll.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/error.h"
77#include "mbedtls/gcm.h"
78#include "mbedtls/md2.h"
79#include "mbedtls/md4.h"
80#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010081#include "mbedtls/md.h"
82#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010083#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010084#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010085#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010087#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010088#include "mbedtls/sha1.h"
89#include "mbedtls/sha256.h"
90#include "mbedtls/sha512.h"
91#include "mbedtls/xtea.h"
92
Netanel Gonen2bcd3122018-11-19 11:53:02 +020093#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
94#include "psa_prot_internal_storage.h"
95#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +010096
Gilles Peskine996deb12018-08-01 15:45:45 +020097#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
98
Gilles Peskine9ef733f2018-02-07 21:05:37 +010099/* constant-time buffer comparison */
100static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
101{
102 size_t i;
103 unsigned char diff = 0;
104
105 for( i = 0; i < n; i++ )
106 diff |= a[i] ^ b[i];
107
108 return( diff );
109}
110
111
112
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100113/****************************************************************/
114/* Global data, support functions and library management */
115/****************************************************************/
116
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200117static int key_type_is_raw_bytes( psa_key_type_t type )
118{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200119 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200120}
121
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122/* Values for psa_global_data_t::rng_state */
123#define RNG_NOT_INITIALIZED 0
124#define RNG_INITIALIZED 1
125#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100126
Gilles Peskine2d277862018-06-18 15:41:12 +0200127typedef struct
128{
Gilles Peskine5e769522018-11-20 21:59:56 +0100129 void (* entropy_init )( mbedtls_entropy_context *ctx );
130 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100131 mbedtls_entropy_context entropy;
132 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100133 psa_key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinec6b69072018-11-20 21:42:52 +0100134 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100135 unsigned rng_state : 2;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100136 unsigned key_slots_initialized : 1;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100137} psa_global_data_t;
138
139static psa_global_data_t global_data;
140
itayzafrir0adf0fc2018-09-06 16:24:41 +0300141#define GUARD_MODULE_INITIALIZED \
142 if( global_data.initialized == 0 ) \
143 return( PSA_ERROR_BAD_STATE );
144
Gilles Peskinee59236f2018-01-27 23:32:46 +0100145static psa_status_t mbedtls_to_psa_error( int ret )
146{
Gilles Peskinea5905292018-02-07 20:59:33 +0100147 /* If there's both a high-level code and low-level code, dispatch on
148 * the high-level code. */
149 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100150 {
151 case 0:
152 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100153
154 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
155 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
156 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
157 return( PSA_ERROR_NOT_SUPPORTED );
158 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
159 return( PSA_ERROR_HARDWARE_FAILURE );
160
161 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
162 return( PSA_ERROR_HARDWARE_FAILURE );
163
Gilles Peskine9a944802018-06-21 09:35:35 +0200164 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
165 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
166 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
167 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
168 case MBEDTLS_ERR_ASN1_INVALID_DATA:
169 return( PSA_ERROR_INVALID_ARGUMENT );
170 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
171 return( PSA_ERROR_INSUFFICIENT_MEMORY );
172 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
173 return( PSA_ERROR_BUFFER_TOO_SMALL );
174
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
176 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
181 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
182 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
183 return( PSA_ERROR_NOT_SUPPORTED );
184 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
185 return( PSA_ERROR_HARDWARE_FAILURE );
186
187 case MBEDTLS_ERR_CCM_BAD_INPUT:
188 return( PSA_ERROR_INVALID_ARGUMENT );
189 case MBEDTLS_ERR_CCM_AUTH_FAILED:
190 return( PSA_ERROR_INVALID_SIGNATURE );
191 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
192 return( PSA_ERROR_HARDWARE_FAILURE );
193
194 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:
207 return( PSA_ERROR_TAMPERING_DETECTED );
208 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
298 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
299 return( PSA_ERROR_HARDWARE_FAILURE );
300
301 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
302 return( PSA_ERROR_INVALID_ARGUMENT );
303 case MBEDTLS_ERR_RSA_INVALID_PADDING:
304 return( PSA_ERROR_INVALID_PADDING );
305 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
306 return( PSA_ERROR_HARDWARE_FAILURE );
307 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
310 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
311 return( PSA_ERROR_TAMPERING_DETECTED );
312 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
313 return( PSA_ERROR_INVALID_SIGNATURE );
314 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
315 return( PSA_ERROR_BUFFER_TOO_SMALL );
316 case MBEDTLS_ERR_RSA_RNG_FAILED:
317 return( PSA_ERROR_INSUFFICIENT_MEMORY );
318 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
319 return( PSA_ERROR_NOT_SUPPORTED );
320 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
321 return( PSA_ERROR_HARDWARE_FAILURE );
322
323 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
324 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
325 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327
328 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
329 return( PSA_ERROR_INVALID_ARGUMENT );
330 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
331 return( PSA_ERROR_HARDWARE_FAILURE );
332
itayzafrir5c753392018-05-08 11:18:38 +0300333 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300334 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300335 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300336 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
337 return( PSA_ERROR_BUFFER_TOO_SMALL );
338 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
339 return( PSA_ERROR_NOT_SUPPORTED );
340 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
341 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
342 return( PSA_ERROR_INVALID_SIGNATURE );
343 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
344 return( PSA_ERROR_INSUFFICIENT_MEMORY );
345 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
346 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300347
Gilles Peskinee59236f2018-01-27 23:32:46 +0100348 default:
349 return( PSA_ERROR_UNKNOWN_ERROR );
350 }
351}
352
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200353
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200354
355
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100356/****************************************************************/
357/* Key management */
358/****************************************************************/
359
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100360#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200361static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
362{
363 switch( grpid )
364 {
365 case MBEDTLS_ECP_DP_SECP192R1:
366 return( PSA_ECC_CURVE_SECP192R1 );
367 case MBEDTLS_ECP_DP_SECP224R1:
368 return( PSA_ECC_CURVE_SECP224R1 );
369 case MBEDTLS_ECP_DP_SECP256R1:
370 return( PSA_ECC_CURVE_SECP256R1 );
371 case MBEDTLS_ECP_DP_SECP384R1:
372 return( PSA_ECC_CURVE_SECP384R1 );
373 case MBEDTLS_ECP_DP_SECP521R1:
374 return( PSA_ECC_CURVE_SECP521R1 );
375 case MBEDTLS_ECP_DP_BP256R1:
376 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
377 case MBEDTLS_ECP_DP_BP384R1:
378 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
379 case MBEDTLS_ECP_DP_BP512R1:
380 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
381 case MBEDTLS_ECP_DP_CURVE25519:
382 return( PSA_ECC_CURVE_CURVE25519 );
383 case MBEDTLS_ECP_DP_SECP192K1:
384 return( PSA_ECC_CURVE_SECP192K1 );
385 case MBEDTLS_ECP_DP_SECP224K1:
386 return( PSA_ECC_CURVE_SECP224K1 );
387 case MBEDTLS_ECP_DP_SECP256K1:
388 return( PSA_ECC_CURVE_SECP256K1 );
389 case MBEDTLS_ECP_DP_CURVE448:
390 return( PSA_ECC_CURVE_CURVE448 );
391 default:
392 return( 0 );
393 }
394}
395
Gilles Peskine12313cd2018-06-20 00:20:32 +0200396static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
397{
398 switch( curve )
399 {
400 case PSA_ECC_CURVE_SECP192R1:
401 return( MBEDTLS_ECP_DP_SECP192R1 );
402 case PSA_ECC_CURVE_SECP224R1:
403 return( MBEDTLS_ECP_DP_SECP224R1 );
404 case PSA_ECC_CURVE_SECP256R1:
405 return( MBEDTLS_ECP_DP_SECP256R1 );
406 case PSA_ECC_CURVE_SECP384R1:
407 return( MBEDTLS_ECP_DP_SECP384R1 );
408 case PSA_ECC_CURVE_SECP521R1:
409 return( MBEDTLS_ECP_DP_SECP521R1 );
410 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
411 return( MBEDTLS_ECP_DP_BP256R1 );
412 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
413 return( MBEDTLS_ECP_DP_BP384R1 );
414 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
415 return( MBEDTLS_ECP_DP_BP512R1 );
416 case PSA_ECC_CURVE_CURVE25519:
417 return( MBEDTLS_ECP_DP_CURVE25519 );
418 case PSA_ECC_CURVE_SECP192K1:
419 return( MBEDTLS_ECP_DP_SECP192K1 );
420 case PSA_ECC_CURVE_SECP224K1:
421 return( MBEDTLS_ECP_DP_SECP224K1 );
422 case PSA_ECC_CURVE_SECP256K1:
423 return( MBEDTLS_ECP_DP_SECP256K1 );
424 case PSA_ECC_CURVE_CURVE448:
425 return( MBEDTLS_ECP_DP_CURVE448 );
426 default:
427 return( MBEDTLS_ECP_DP_NONE );
428 }
429}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100430#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200431
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200432static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
433 size_t bits,
434 struct raw_data *raw )
435{
436 /* Check that the bit size is acceptable for the key type */
437 switch( type )
438 {
439 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200440 if( bits == 0 )
441 {
442 raw->bytes = 0;
443 raw->data = NULL;
444 return( PSA_SUCCESS );
445 }
446 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200447#if defined(MBEDTLS_MD_C)
448 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200449#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200450 case PSA_KEY_TYPE_DERIVE:
451 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200452#if defined(MBEDTLS_AES_C)
453 case PSA_KEY_TYPE_AES:
454 if( bits != 128 && bits != 192 && bits != 256 )
455 return( PSA_ERROR_INVALID_ARGUMENT );
456 break;
457#endif
458#if defined(MBEDTLS_CAMELLIA_C)
459 case PSA_KEY_TYPE_CAMELLIA:
460 if( bits != 128 && bits != 192 && bits != 256 )
461 return( PSA_ERROR_INVALID_ARGUMENT );
462 break;
463#endif
464#if defined(MBEDTLS_DES_C)
465 case PSA_KEY_TYPE_DES:
466 if( bits != 64 && bits != 128 && bits != 192 )
467 return( PSA_ERROR_INVALID_ARGUMENT );
468 break;
469#endif
470#if defined(MBEDTLS_ARC4_C)
471 case PSA_KEY_TYPE_ARC4:
472 if( bits < 8 || bits > 2048 )
473 return( PSA_ERROR_INVALID_ARGUMENT );
474 break;
475#endif
476 default:
477 return( PSA_ERROR_NOT_SUPPORTED );
478 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200479 if( bits % 8 != 0 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200481
482 /* Allocate memory for the key */
483 raw->bytes = PSA_BITS_TO_BYTES( bits );
484 raw->data = mbedtls_calloc( 1, raw->bytes );
485 if( raw->data == NULL )
486 {
487 raw->bytes = 0;
488 return( PSA_ERROR_INSUFFICIENT_MEMORY );
489 }
490 return( PSA_SUCCESS );
491}
492
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200493#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100494/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
495 * that are not a multiple of 8) well. For example, there is only
496 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
497 * way to return the exact bit size of a key.
498 * To keep things simple, reject non-byte-aligned key sizes. */
499static psa_status_t psa_check_rsa_key_byte_aligned(
500 const mbedtls_rsa_context *rsa )
501{
502 mbedtls_mpi n;
503 psa_status_t status;
504 mbedtls_mpi_init( &n );
505 status = mbedtls_to_psa_error(
506 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
507 if( status == PSA_SUCCESS )
508 {
509 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
510 status = PSA_ERROR_NOT_SUPPORTED;
511 }
512 mbedtls_mpi_free( &n );
513 return( status );
514}
515
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200516static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
517 mbedtls_rsa_context **p_rsa )
518{
519 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
520 return( PSA_ERROR_INVALID_ARGUMENT );
521 else
522 {
523 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
Gilles Peskineaac64a22018-11-12 18:37:42 +0100524 /* The size of an RSA key doesn't have to be a multiple of 8.
525 * Mbed TLS supports non-byte-aligned key sizes, but not well.
526 * For example, mbedtls_rsa_get_len() returns the key size in
527 * bytes, not in bits. */
528 size_t bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100529 psa_status_t status;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200530 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
531 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100532 status = psa_check_rsa_key_byte_aligned( rsa );
533 if( status != PSA_SUCCESS )
534 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200535 *p_rsa = rsa;
536 return( PSA_SUCCESS );
537 }
538}
539#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
540
541#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100542/* Import an elliptic curve parsed by the mbedtls pk module. */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200543static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
544 mbedtls_pk_context *pk,
545 mbedtls_ecp_keypair **p_ecp )
546{
547 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
548 return( PSA_ERROR_INVALID_ARGUMENT );
549 else
550 {
551 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
552 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
553 if( actual_curve != expected_curve )
554 return( PSA_ERROR_INVALID_ARGUMENT );
555 *p_ecp = ecp;
556 return( PSA_SUCCESS );
557 }
558}
559#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
560
Gilles Peskinef76aa772018-10-29 19:24:33 +0100561#if defined(MBEDTLS_ECP_C)
562/* Import a private key given as a byte string which is the private value
563 * in big-endian order. */
564static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
565 const uint8_t *data,
566 size_t data_length,
567 mbedtls_ecp_keypair **p_ecp )
568{
569 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
570 mbedtls_ecp_keypair *ecp = NULL;
571 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
572
573 *p_ecp = NULL;
574 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
575 if( ecp == NULL )
576 return( PSA_ERROR_INSUFFICIENT_MEMORY );
577
578 /* Load the group. */
579 status = mbedtls_to_psa_error(
580 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
581 if( status != PSA_SUCCESS )
582 goto exit;
583 /* Load the secret value. */
584 status = mbedtls_to_psa_error(
585 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
586 if( status != PSA_SUCCESS )
587 goto exit;
588 /* Validate the private key. */
589 status = mbedtls_to_psa_error(
590 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
591 if( status != PSA_SUCCESS )
592 goto exit;
593 /* Calculate the public key from the private key. */
594 status = mbedtls_to_psa_error(
595 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
596 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
597 if( status != PSA_SUCCESS )
598 goto exit;
599
600 *p_ecp = ecp;
601 return( PSA_SUCCESS );
602
603exit:
604 if( ecp != NULL )
605 {
606 mbedtls_ecp_keypair_free( ecp );
607 mbedtls_free( ecp );
608 }
609 return( status );
610}
611#endif /* defined(MBEDTLS_ECP_C) */
612
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100613/** Import key data into a slot. `slot->type` must have been set
614 * previously. This function assumes that the slot does not contain
615 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100616static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
Darryl Green940d72c2018-07-13 13:18:51 +0100617 const uint8_t *data,
618 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100619{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200620 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100621
Darryl Green940d72c2018-07-13 13:18:51 +0100622 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100623 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100624 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100625 if( data_length > SIZE_MAX / 8 )
626 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100627 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200628 PSA_BYTES_TO_BITS( data_length ),
629 &slot->data.raw );
630 if( status != PSA_SUCCESS )
631 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200632 if( data_length != 0 )
633 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100634 }
635 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100636#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100637 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100638 {
Darryl Green940d72c2018-07-13 13:18:51 +0100639 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100640 data, data_length,
641 &slot->data.ecp );
642 if( status != PSA_SUCCESS )
643 return( status );
644 }
645 else
646#endif /* MBEDTLS_ECP_C */
Gilles Peskine969ac722018-01-28 18:16:59 +0100647#if defined(MBEDTLS_PK_PARSE_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100648 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
649 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100650 {
651 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100652 mbedtls_pk_context pk;
653 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200654
655 /* Parse the data. */
Darryl Green940d72c2018-07-13 13:18:51 +0100656 if( PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100657 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
658 else
659 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100660 if( ret != 0 )
661 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200662
663 /* We have something that the pkparse module recognizes.
664 * If it has the expected type and passes any type-specific
665 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100666#if defined(MBEDTLS_RSA_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100667 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200668 status = psa_import_rsa_key( &pk, &slot->data.rsa );
669 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100670#endif /* MBEDTLS_RSA_C */
671#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100672 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
673 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200674 &pk, &slot->data.ecp );
675 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100676#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200677 {
678 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200679 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200680
Gilles Peskinec648d692018-06-28 08:46:13 +0200681 /* Free the content of the pk object only on error. On success,
682 * the content of the object has been stored in the slot. */
683 if( status != PSA_SUCCESS )
684 {
685 mbedtls_pk_free( &pk );
686 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100687 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100688 }
689 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100690#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100691 {
692 return( PSA_ERROR_NOT_SUPPORTED );
693 }
Darryl Green940d72c2018-07-13 13:18:51 +0100694 return( PSA_SUCCESS );
695}
696
Darryl Greend49a4992018-06-18 17:27:26 +0100697#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +0100698static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *p_slot )
Darryl Greend49a4992018-06-18 17:27:26 +0100699{
700 psa_status_t status = PSA_SUCCESS;
701 uint8_t *key_data = NULL;
702 size_t key_data_length = 0;
703
Gilles Peskine69f976b2018-11-30 18:46:56 +0100704 status = psa_load_persistent_key( p_slot->persistent_storage_id,
705 &( p_slot )->type,
Darryl Greend49a4992018-06-18 17:27:26 +0100706 &( p_slot )->policy, &key_data,
707 &key_data_length );
708 if( status != PSA_SUCCESS )
709 goto exit;
710 status = psa_import_key_into_slot( p_slot,
711 key_data, key_data_length );
712exit:
713 psa_free_persistent_key_data( key_data, key_data_length );
714 return( status );
715}
716#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
717
Gilles Peskinec5487a82018-12-03 18:08:14 +0100718/* Access a key slot at the given handle. The handle of a key slot is
719 * the index of the slot in the global slot array, plus one so that handles
720 * start at 1 and not 0. */
721static psa_status_t psa_get_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100722 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100723{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100724 psa_key_slot_t *slot = NULL;
Gilles Peskine961849f2018-11-30 18:54:54 +0100725
Darryl Green06fd18d2018-07-16 11:21:11 +0100726 GUARD_MODULE_INITIALIZED;
727
Gilles Peskinec5487a82018-12-03 18:08:14 +0100728 /* 0 is not a valid handle under any circumstance. This
Darryl Green06fd18d2018-07-16 11:21:11 +0100729 * implementation provides slots number 1 to N where N is the
730 * number of available slots. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100731 if( handle == 0 || handle > ARRAY_LENGTH( global_data.key_slots ) )
732 return( PSA_ERROR_INVALID_HANDLE );
733 slot = &global_data.key_slots[handle - 1];
Darryl Green06fd18d2018-07-16 11:21:11 +0100734
Gilles Peskinec5487a82018-12-03 18:08:14 +0100735 /* If the slot hasn't been allocated, the handle is invalid. */
736 if( ! slot->allocated )
737 return( PSA_ERROR_INVALID_HANDLE );
Darryl Greend49a4992018-06-18 17:27:26 +0100738
Gilles Peskinec5487a82018-12-03 18:08:14 +0100739 *p_slot = slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100740 return( PSA_SUCCESS );
741}
742
743/* Retrieve an empty key slot (slot with no key data, but possibly
744 * with some metadata such as a policy). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100745static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100746 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100747{
748 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100749 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100750
751 *p_slot = NULL;
752
Gilles Peskinec5487a82018-12-03 18:08:14 +0100753 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100754 if( status != PSA_SUCCESS )
755 return( status );
756
757 if( slot->type != PSA_KEY_TYPE_NONE )
758 return( PSA_ERROR_OCCUPIED_SLOT );
759
760 *p_slot = slot;
761 return( status );
762}
763
764/** Retrieve a slot which must contain a key. The key must have allow all the
765 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
766 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100767static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100768 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100769 psa_key_usage_t usage,
770 psa_algorithm_t alg )
771{
772 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100773 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100774
775 *p_slot = NULL;
776
Gilles Peskinec5487a82018-12-03 18:08:14 +0100777 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100778 if( status != PSA_SUCCESS )
779 return( status );
780 if( slot->type == PSA_KEY_TYPE_NONE )
781 return( PSA_ERROR_EMPTY_SLOT );
782
783 /* Enforce that usage policy for the key slot contains all the flags
784 * required by the usage parameter. There is one exception: public
785 * keys can always be exported, so we treat public key objects as
786 * if they had the export flag. */
787 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
788 usage &= ~PSA_KEY_USAGE_EXPORT;
789 if( ( slot->policy.usage & usage ) != usage )
790 return( PSA_ERROR_NOT_PERMITTED );
791 if( alg != 0 && ( alg != slot->policy.alg ) )
792 return( PSA_ERROR_NOT_PERMITTED );
793
794 *p_slot = slot;
795 return( PSA_SUCCESS );
796}
Darryl Green940d72c2018-07-13 13:18:51 +0100797
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100798/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100799static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000800{
801 if( slot->type == PSA_KEY_TYPE_NONE )
802 {
803 /* No key material to clean. */
804 }
805 else if( key_type_is_raw_bytes( slot->type ) )
806 {
807 mbedtls_free( slot->data.raw.data );
808 }
809 else
810#if defined(MBEDTLS_RSA_C)
811 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
812 {
813 mbedtls_rsa_free( slot->data.rsa );
814 mbedtls_free( slot->data.rsa );
815 }
816 else
817#endif /* defined(MBEDTLS_RSA_C) */
818#if defined(MBEDTLS_ECP_C)
819 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
820 {
821 mbedtls_ecp_keypair_free( slot->data.ecp );
822 mbedtls_free( slot->data.ecp );
823 }
824 else
825#endif /* defined(MBEDTLS_ECP_C) */
826 {
827 /* Shouldn't happen: the key type is not any type that we
828 * put in. */
829 return( PSA_ERROR_TAMPERING_DETECTED );
830 }
831
832 return( PSA_SUCCESS );
833}
834
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100835/** Completely wipe a slot in memory, including its policy.
836 * Persistent storage is not affected. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100837static psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100838{
839 psa_status_t status = psa_remove_key_data_from_memory( slot );
840 /* At this point, key material and other type-specific content has
841 * been wiped. Clear remaining metadata. We can call memset and not
842 * zeroize because the metadata is not particularly sensitive. */
843 memset( slot, 0, sizeof( *slot ) );
844 return( status );
845}
846
Gilles Peskine961849f2018-11-30 18:54:54 +0100847psa_status_t psa_internal_allocate_key_slot( psa_key_handle_t *handle )
848{
Gilles Peskinec5487a82018-12-03 18:08:14 +0100849 for( *handle = PSA_KEY_SLOT_COUNT; *handle != 0; --( *handle ) )
Gilles Peskine961849f2018-11-30 18:54:54 +0100850 {
Gilles Peskine2f060a82018-12-04 17:12:32 +0100851 psa_key_slot_t *slot = &global_data.key_slots[*handle - 1];
Gilles Peskinec5487a82018-12-03 18:08:14 +0100852 if( ! slot->allocated )
Gilles Peskine961849f2018-11-30 18:54:54 +0100853 {
854 slot->allocated = 1;
Gilles Peskine961849f2018-11-30 18:54:54 +0100855 return( PSA_SUCCESS );
856 }
857 }
858 return( PSA_ERROR_INSUFFICIENT_MEMORY );
859}
860
861psa_status_t psa_internal_make_key_persistent( psa_key_handle_t handle,
862 psa_key_id_t id )
863{
Gilles Peskine4a044732018-12-03 18:19:39 +0100864#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +0100865 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100866 psa_status_t status;
867
868 /* Reject id=0 because by general library conventions, 0 is an invalid
869 * value wherever possible. */
870 if( id == 0 )
871 return( PSA_ERROR_INVALID_ARGUMENT );
872 /* Reject high values because the file names are reserved for the
873 * library's internal use. */
Gilles Peskine48868122018-12-10 17:30:29 +0100874 if( id >= PSA_MAX_PERSISTENT_KEY_IDENTIFIER )
Gilles Peskine961849f2018-11-30 18:54:54 +0100875 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine961849f2018-11-30 18:54:54 +0100876
877 status = psa_get_key_slot( handle, &slot );
878 if( status != PSA_SUCCESS )
879 return( status );
880
881 slot->lifetime = PSA_KEY_LIFETIME_PERSISTENT;
882 slot->persistent_storage_id = id;
883 status = psa_load_persistent_key_into_slot( slot );
884
885 return( status );
Gilles Peskine4a044732018-12-03 18:19:39 +0100886
887#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
888 (void) handle;
889 (void) id;
890 return( PSA_ERROR_NOT_SUPPORTED );
891#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine961849f2018-11-30 18:54:54 +0100892}
893
894psa_status_t psa_internal_release_key_slot( psa_key_handle_t handle )
895{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100896 psa_key_slot_t *slot;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100897 psa_status_t status;
898
899 status = psa_get_key_slot( handle, &slot );
900 if( status != PSA_SUCCESS )
901 return( status );
Gilles Peskinec5487a82018-12-03 18:08:14 +0100902
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100903 return( psa_wipe_key_slot( slot ) );
Gilles Peskine961849f2018-11-30 18:54:54 +0100904}
905
Gilles Peskinec5487a82018-12-03 18:08:14 +0100906psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100907 psa_key_type_t type,
908 const uint8_t *data,
909 size_t data_length )
910{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100911 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100912 psa_status_t status;
913
Gilles Peskinec5487a82018-12-03 18:08:14 +0100914 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100915 if( status != PSA_SUCCESS )
916 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100917
918 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100919
920 status = psa_import_key_into_slot( slot, data, data_length );
921 if( status != PSA_SUCCESS )
922 {
923 slot->type = PSA_KEY_TYPE_NONE;
924 return( status );
925 }
926
Darryl Greend49a4992018-06-18 17:27:26 +0100927#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
928 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
929 {
930 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100931 status = psa_save_persistent_key( slot->persistent_storage_id,
932 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100933 data_length );
934 if( status != PSA_SUCCESS )
935 {
936 (void) psa_remove_key_data_from_memory( slot );
937 slot->type = PSA_KEY_TYPE_NONE;
938 }
939 }
940#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
941
942 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100943}
944
Gilles Peskinec5487a82018-12-03 18:08:14 +0100945psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100946{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100947 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100948 psa_status_t status = PSA_SUCCESS;
949 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100950
Gilles Peskinec5487a82018-12-03 18:08:14 +0100951 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200952 if( status != PSA_SUCCESS )
953 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100954#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
955 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
956 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100957 storage_status =
958 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100959 }
960#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100961 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100962 if( status != PSA_SUCCESS )
963 return( status );
964 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100965}
966
Gilles Peskineb870b182018-07-06 16:02:09 +0200967/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100968static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200969{
970 if( key_type_is_raw_bytes( slot->type ) )
971 return( slot->data.raw.bytes * 8 );
972#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200973 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100974 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200975#endif /* defined(MBEDTLS_RSA_C) */
976#if defined(MBEDTLS_ECP_C)
977 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
978 return( slot->data.ecp->grp.pbits );
979#endif /* defined(MBEDTLS_ECP_C) */
980 /* Shouldn't happen except on an empty slot. */
981 return( 0 );
982}
983
Gilles Peskinec5487a82018-12-03 18:08:14 +0100984psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200985 psa_key_type_t *type,
986 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100987{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100988 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200989 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100990
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100991 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200992 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100993 if( bits != NULL )
994 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100995 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200996 if( status != PSA_SUCCESS )
997 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200998
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100999 if( slot->type == PSA_KEY_TYPE_NONE )
1000 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001001 if( type != NULL )
1002 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001003 if( bits != NULL )
1004 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001005 return( PSA_SUCCESS );
1006}
1007
Gilles Peskine2f060a82018-12-04 17:12:32 +01001008static psa_status_t psa_internal_export_key( psa_key_slot_t *slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001009 uint8_t *data,
1010 size_t data_size,
1011 size_t *data_length,
1012 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001013{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001014 *data_length = 0;
1015
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001016 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001017 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001018
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001019 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001020 {
1021 if( slot->data.raw.bytes > data_size )
1022 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001023 if( data_size != 0 )
1024 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001025 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001026 memset( data + slot->data.raw.bytes, 0,
1027 data_size - slot->data.raw.bytes );
1028 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001029 *data_length = slot->data.raw.bytes;
1030 return( PSA_SUCCESS );
1031 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001032#if defined(MBEDTLS_ECP_C)
1033 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1034 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001035 psa_status_t status;
1036
Gilles Peskine188c71e2018-10-29 19:26:02 +01001037 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1038 if( bytes > data_size )
1039 return( PSA_ERROR_BUFFER_TOO_SMALL );
1040 status = mbedtls_to_psa_error(
1041 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1042 if( status != PSA_SUCCESS )
1043 return( status );
1044 memset( data + bytes, 0, data_size - bytes );
1045 *data_length = bytes;
1046 return( PSA_SUCCESS );
1047 }
1048#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001049 else
Moran Peker17e36e12018-05-02 12:55:20 +03001050 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001051#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001052 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001053 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001054 {
Moran Pekera998bc62018-04-16 18:16:20 +03001055 mbedtls_pk_context pk;
1056 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001057 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001058 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001059#if defined(MBEDTLS_RSA_C)
1060 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001061 pk.pk_info = &mbedtls_rsa_info;
1062 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001063#else
1064 return( PSA_ERROR_NOT_SUPPORTED );
1065#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001066 }
1067 else
1068 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001069#if defined(MBEDTLS_ECP_C)
1070 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001071 pk.pk_info = &mbedtls_eckey_info;
1072 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001073#else
1074 return( PSA_ERROR_NOT_SUPPORTED );
1075#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001076 }
Moran Pekerd7326592018-05-29 16:56:39 +03001077 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001078 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +03001079 else
1080 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +03001081 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001082 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001083 /* If data_size is 0 then data may be NULL and then the
1084 * call to memset would have undefined behavior. */
1085 if( data_size != 0 )
1086 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001087 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001088 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001089 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1090 * Move the data to the beginning and erase remaining data
1091 * at the original location. */
1092 if( 2 * (size_t) ret <= data_size )
1093 {
1094 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001095 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001096 }
1097 else if( (size_t) ret < data_size )
1098 {
1099 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001100 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001101 }
Moran Pekera998bc62018-04-16 18:16:20 +03001102 *data_length = ret;
1103 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001104 }
1105 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001106#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001107 {
1108 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001109 it is valid for a special-purpose implementation to omit
1110 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001111 return( PSA_ERROR_NOT_SUPPORTED );
1112 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001113 }
1114}
1115
Gilles Peskinec5487a82018-12-03 18:08:14 +01001116psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001117 uint8_t *data,
1118 size_t data_size,
1119 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001120{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001121 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001122 psa_status_t status;
1123
1124 /* Set the key to empty now, so that even when there are errors, we always
1125 * set data_length to a value between 0 and data_size. On error, setting
1126 * the key to empty is a good choice because an empty key representation is
1127 * unlikely to be accepted anywhere. */
1128 *data_length = 0;
1129
1130 /* Export requires the EXPORT flag. There is an exception for public keys,
1131 * which don't require any flag, but psa_get_key_from_slot takes
1132 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001133 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001134 if( status != PSA_SUCCESS )
1135 return( status );
1136 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001137 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001138}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001139
Gilles Peskinec5487a82018-12-03 18:08:14 +01001140psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001141 uint8_t *data,
1142 size_t data_size,
1143 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001144{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001145 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001146 psa_status_t status;
1147
1148 /* Set the key to empty now, so that even when there are errors, we always
1149 * set data_length to a value between 0 and data_size. On error, setting
1150 * the key to empty is a good choice because an empty key representation is
1151 * unlikely to be accepted anywhere. */
1152 *data_length = 0;
1153
1154 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001155 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001156 if( status != PSA_SUCCESS )
1157 return( status );
1158 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001159 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001160}
1161
Darryl Green0c6575a2018-11-07 16:05:30 +00001162#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001163static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001164 size_t bits )
1165{
1166 psa_status_t status;
1167 uint8_t *data;
1168 size_t key_length;
1169 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1170 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001171 if( data == NULL )
1172 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001173 /* Get key data in export format */
1174 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1175 if( status != PSA_SUCCESS )
1176 {
1177 slot->type = PSA_KEY_TYPE_NONE;
1178 goto exit;
1179 }
1180 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001181 status = psa_save_persistent_key( slot->persistent_storage_id,
1182 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001183 data, key_length );
1184 if( status != PSA_SUCCESS )
1185 {
1186 slot->type = PSA_KEY_TYPE_NONE;
1187 }
1188exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001189 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001190 mbedtls_free( data );
1191 return( status );
1192}
1193#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1194
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001195
1196
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001197/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001198/* Message digests */
1199/****************************************************************/
1200
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001201static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001202{
1203 switch( alg )
1204 {
1205#if defined(MBEDTLS_MD2_C)
1206 case PSA_ALG_MD2:
1207 return( &mbedtls_md2_info );
1208#endif
1209#if defined(MBEDTLS_MD4_C)
1210 case PSA_ALG_MD4:
1211 return( &mbedtls_md4_info );
1212#endif
1213#if defined(MBEDTLS_MD5_C)
1214 case PSA_ALG_MD5:
1215 return( &mbedtls_md5_info );
1216#endif
1217#if defined(MBEDTLS_RIPEMD160_C)
1218 case PSA_ALG_RIPEMD160:
1219 return( &mbedtls_ripemd160_info );
1220#endif
1221#if defined(MBEDTLS_SHA1_C)
1222 case PSA_ALG_SHA_1:
1223 return( &mbedtls_sha1_info );
1224#endif
1225#if defined(MBEDTLS_SHA256_C)
1226 case PSA_ALG_SHA_224:
1227 return( &mbedtls_sha224_info );
1228 case PSA_ALG_SHA_256:
1229 return( &mbedtls_sha256_info );
1230#endif
1231#if defined(MBEDTLS_SHA512_C)
1232 case PSA_ALG_SHA_384:
1233 return( &mbedtls_sha384_info );
1234 case PSA_ALG_SHA_512:
1235 return( &mbedtls_sha512_info );
1236#endif
1237 default:
1238 return( NULL );
1239 }
1240}
1241
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001242psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1243{
1244 switch( operation->alg )
1245 {
Gilles Peskine81736312018-06-26 15:04:31 +02001246 case 0:
1247 /* The object has (apparently) been initialized but it is not
1248 * in use. It's ok to call abort on such an object, and there's
1249 * nothing to do. */
1250 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001251#if defined(MBEDTLS_MD2_C)
1252 case PSA_ALG_MD2:
1253 mbedtls_md2_free( &operation->ctx.md2 );
1254 break;
1255#endif
1256#if defined(MBEDTLS_MD4_C)
1257 case PSA_ALG_MD4:
1258 mbedtls_md4_free( &operation->ctx.md4 );
1259 break;
1260#endif
1261#if defined(MBEDTLS_MD5_C)
1262 case PSA_ALG_MD5:
1263 mbedtls_md5_free( &operation->ctx.md5 );
1264 break;
1265#endif
1266#if defined(MBEDTLS_RIPEMD160_C)
1267 case PSA_ALG_RIPEMD160:
1268 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1269 break;
1270#endif
1271#if defined(MBEDTLS_SHA1_C)
1272 case PSA_ALG_SHA_1:
1273 mbedtls_sha1_free( &operation->ctx.sha1 );
1274 break;
1275#endif
1276#if defined(MBEDTLS_SHA256_C)
1277 case PSA_ALG_SHA_224:
1278 case PSA_ALG_SHA_256:
1279 mbedtls_sha256_free( &operation->ctx.sha256 );
1280 break;
1281#endif
1282#if defined(MBEDTLS_SHA512_C)
1283 case PSA_ALG_SHA_384:
1284 case PSA_ALG_SHA_512:
1285 mbedtls_sha512_free( &operation->ctx.sha512 );
1286 break;
1287#endif
1288 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001289 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001290 }
1291 operation->alg = 0;
1292 return( PSA_SUCCESS );
1293}
1294
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001295psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001296 psa_algorithm_t alg )
1297{
1298 int ret;
1299 operation->alg = 0;
1300 switch( alg )
1301 {
1302#if defined(MBEDTLS_MD2_C)
1303 case PSA_ALG_MD2:
1304 mbedtls_md2_init( &operation->ctx.md2 );
1305 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1306 break;
1307#endif
1308#if defined(MBEDTLS_MD4_C)
1309 case PSA_ALG_MD4:
1310 mbedtls_md4_init( &operation->ctx.md4 );
1311 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1312 break;
1313#endif
1314#if defined(MBEDTLS_MD5_C)
1315 case PSA_ALG_MD5:
1316 mbedtls_md5_init( &operation->ctx.md5 );
1317 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1318 break;
1319#endif
1320#if defined(MBEDTLS_RIPEMD160_C)
1321 case PSA_ALG_RIPEMD160:
1322 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1323 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1324 break;
1325#endif
1326#if defined(MBEDTLS_SHA1_C)
1327 case PSA_ALG_SHA_1:
1328 mbedtls_sha1_init( &operation->ctx.sha1 );
1329 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1330 break;
1331#endif
1332#if defined(MBEDTLS_SHA256_C)
1333 case PSA_ALG_SHA_224:
1334 mbedtls_sha256_init( &operation->ctx.sha256 );
1335 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1336 break;
1337 case PSA_ALG_SHA_256:
1338 mbedtls_sha256_init( &operation->ctx.sha256 );
1339 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1340 break;
1341#endif
1342#if defined(MBEDTLS_SHA512_C)
1343 case PSA_ALG_SHA_384:
1344 mbedtls_sha512_init( &operation->ctx.sha512 );
1345 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1346 break;
1347 case PSA_ALG_SHA_512:
1348 mbedtls_sha512_init( &operation->ctx.sha512 );
1349 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1350 break;
1351#endif
1352 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001353 return( PSA_ALG_IS_HASH( alg ) ?
1354 PSA_ERROR_NOT_SUPPORTED :
1355 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001356 }
1357 if( ret == 0 )
1358 operation->alg = alg;
1359 else
1360 psa_hash_abort( operation );
1361 return( mbedtls_to_psa_error( ret ) );
1362}
1363
1364psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1365 const uint8_t *input,
1366 size_t input_length )
1367{
1368 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001369
1370 /* Don't require hash implementations to behave correctly on a
1371 * zero-length input, which may have an invalid pointer. */
1372 if( input_length == 0 )
1373 return( PSA_SUCCESS );
1374
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001375 switch( operation->alg )
1376 {
1377#if defined(MBEDTLS_MD2_C)
1378 case PSA_ALG_MD2:
1379 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1380 input, input_length );
1381 break;
1382#endif
1383#if defined(MBEDTLS_MD4_C)
1384 case PSA_ALG_MD4:
1385 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1386 input, input_length );
1387 break;
1388#endif
1389#if defined(MBEDTLS_MD5_C)
1390 case PSA_ALG_MD5:
1391 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1392 input, input_length );
1393 break;
1394#endif
1395#if defined(MBEDTLS_RIPEMD160_C)
1396 case PSA_ALG_RIPEMD160:
1397 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1398 input, input_length );
1399 break;
1400#endif
1401#if defined(MBEDTLS_SHA1_C)
1402 case PSA_ALG_SHA_1:
1403 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1404 input, input_length );
1405 break;
1406#endif
1407#if defined(MBEDTLS_SHA256_C)
1408 case PSA_ALG_SHA_224:
1409 case PSA_ALG_SHA_256:
1410 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1411 input, input_length );
1412 break;
1413#endif
1414#if defined(MBEDTLS_SHA512_C)
1415 case PSA_ALG_SHA_384:
1416 case PSA_ALG_SHA_512:
1417 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1418 input, input_length );
1419 break;
1420#endif
1421 default:
1422 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1423 break;
1424 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001425
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001426 if( ret != 0 )
1427 psa_hash_abort( operation );
1428 return( mbedtls_to_psa_error( ret ) );
1429}
1430
1431psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1432 uint8_t *hash,
1433 size_t hash_size,
1434 size_t *hash_length )
1435{
itayzafrir40835d42018-08-02 13:14:17 +03001436 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001437 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001438 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001439
1440 /* Fill the output buffer with something that isn't a valid hash
1441 * (barring an attack on the hash and deliberately-crafted input),
1442 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001443 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001444 /* If hash_size is 0 then hash may be NULL and then the
1445 * call to memset would have undefined behavior. */
1446 if( hash_size != 0 )
1447 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001448
1449 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001450 {
1451 status = PSA_ERROR_BUFFER_TOO_SMALL;
1452 goto exit;
1453 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001454
1455 switch( operation->alg )
1456 {
1457#if defined(MBEDTLS_MD2_C)
1458 case PSA_ALG_MD2:
1459 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1460 break;
1461#endif
1462#if defined(MBEDTLS_MD4_C)
1463 case PSA_ALG_MD4:
1464 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1465 break;
1466#endif
1467#if defined(MBEDTLS_MD5_C)
1468 case PSA_ALG_MD5:
1469 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1470 break;
1471#endif
1472#if defined(MBEDTLS_RIPEMD160_C)
1473 case PSA_ALG_RIPEMD160:
1474 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1475 break;
1476#endif
1477#if defined(MBEDTLS_SHA1_C)
1478 case PSA_ALG_SHA_1:
1479 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1480 break;
1481#endif
1482#if defined(MBEDTLS_SHA256_C)
1483 case PSA_ALG_SHA_224:
1484 case PSA_ALG_SHA_256:
1485 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1486 break;
1487#endif
1488#if defined(MBEDTLS_SHA512_C)
1489 case PSA_ALG_SHA_384:
1490 case PSA_ALG_SHA_512:
1491 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1492 break;
1493#endif
1494 default:
1495 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1496 break;
1497 }
itayzafrir40835d42018-08-02 13:14:17 +03001498 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001499
itayzafrir40835d42018-08-02 13:14:17 +03001500exit:
1501 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001502 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001503 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001504 return( psa_hash_abort( operation ) );
1505 }
1506 else
1507 {
1508 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001509 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001510 }
1511}
1512
Gilles Peskine2d277862018-06-18 15:41:12 +02001513psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1514 const uint8_t *hash,
1515 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001516{
1517 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1518 size_t actual_hash_length;
1519 psa_status_t status = psa_hash_finish( operation,
1520 actual_hash, sizeof( actual_hash ),
1521 &actual_hash_length );
1522 if( status != PSA_SUCCESS )
1523 return( status );
1524 if( actual_hash_length != hash_length )
1525 return( PSA_ERROR_INVALID_SIGNATURE );
1526 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1527 return( PSA_ERROR_INVALID_SIGNATURE );
1528 return( PSA_SUCCESS );
1529}
1530
1531
1532
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001533/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001534/* MAC */
1535/****************************************************************/
1536
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001537static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001538 psa_algorithm_t alg,
1539 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001540 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001541 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001542{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001543 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001544 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001545
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001546 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001547 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001548
Gilles Peskine8c9def32018-02-08 10:02:12 +01001549 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1550 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001551 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001552 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001553 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001554 mode = MBEDTLS_MODE_STREAM;
1555 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001556 case PSA_ALG_CTR:
1557 mode = MBEDTLS_MODE_CTR;
1558 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001559 case PSA_ALG_CFB:
1560 mode = MBEDTLS_MODE_CFB;
1561 break;
1562 case PSA_ALG_OFB:
1563 mode = MBEDTLS_MODE_OFB;
1564 break;
1565 case PSA_ALG_CBC_NO_PADDING:
1566 mode = MBEDTLS_MODE_CBC;
1567 break;
1568 case PSA_ALG_CBC_PKCS7:
1569 mode = MBEDTLS_MODE_CBC;
1570 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001571 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001572 mode = MBEDTLS_MODE_CCM;
1573 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001574 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001575 mode = MBEDTLS_MODE_GCM;
1576 break;
1577 default:
1578 return( NULL );
1579 }
1580 }
1581 else if( alg == PSA_ALG_CMAC )
1582 mode = MBEDTLS_MODE_ECB;
1583 else if( alg == PSA_ALG_GMAC )
1584 mode = MBEDTLS_MODE_GCM;
1585 else
1586 return( NULL );
1587
1588 switch( key_type )
1589 {
1590 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001591 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001592 break;
1593 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001594 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1595 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001596 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001597 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001598 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001599 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001600 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1601 * but two-key Triple-DES is functionally three-key Triple-DES
1602 * with K1=K3, so that's how we present it to mbedtls. */
1603 if( key_bits == 128 )
1604 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001605 break;
1606 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001607 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001608 break;
1609 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001610 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001611 break;
1612 default:
1613 return( NULL );
1614 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001615 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001616 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001617
Jaeden Amero23bbb752018-06-26 14:16:54 +01001618 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1619 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001620}
1621
Gilles Peskinea05219c2018-11-16 16:02:56 +01001622#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001623static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001624{
Gilles Peskine2d277862018-06-18 15:41:12 +02001625 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001626 {
1627 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001628 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001629 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001630 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001631 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001632 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001633 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001634 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001635 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001636 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001637 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001638 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001639 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001640 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001641 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001642 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001643 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001644 return( 128 );
1645 default:
1646 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001647 }
1648}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001649#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001650
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001651/* Initialize the MAC operation structure. Once this function has been
1652 * called, psa_mac_abort can run and will do the right thing. */
1653static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1654 psa_algorithm_t alg )
1655{
1656 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1657
1658 operation->alg = alg;
1659 operation->key_set = 0;
1660 operation->iv_set = 0;
1661 operation->iv_required = 0;
1662 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001663 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001664
1665#if defined(MBEDTLS_CMAC_C)
1666 if( alg == PSA_ALG_CMAC )
1667 {
1668 operation->iv_required = 0;
1669 mbedtls_cipher_init( &operation->ctx.cmac );
1670 status = PSA_SUCCESS;
1671 }
1672 else
1673#endif /* MBEDTLS_CMAC_C */
1674#if defined(MBEDTLS_MD_C)
1675 if( PSA_ALG_IS_HMAC( operation->alg ) )
1676 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001677 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1678 operation->ctx.hmac.hash_ctx.alg = 0;
1679 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001680 }
1681 else
1682#endif /* MBEDTLS_MD_C */
1683 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001684 if( ! PSA_ALG_IS_MAC( alg ) )
1685 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001686 }
1687
1688 if( status != PSA_SUCCESS )
1689 memset( operation, 0, sizeof( *operation ) );
1690 return( status );
1691}
1692
Gilles Peskine01126fa2018-07-12 17:04:55 +02001693#if defined(MBEDTLS_MD_C)
1694static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1695{
Gilles Peskine3f108122018-12-07 18:14:53 +01001696 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001697 return( psa_hash_abort( &hmac->hash_ctx ) );
1698}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001699
1700static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1701{
1702 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1703 memset( hmac, 0, sizeof( *hmac ) );
1704}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001705#endif /* MBEDTLS_MD_C */
1706
Gilles Peskine8c9def32018-02-08 10:02:12 +01001707psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1708{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001709 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001710 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001711 /* The object has (apparently) been initialized but it is not
1712 * in use. It's ok to call abort on such an object, and there's
1713 * nothing to do. */
1714 return( PSA_SUCCESS );
1715 }
1716 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001717#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001718 if( operation->alg == PSA_ALG_CMAC )
1719 {
1720 mbedtls_cipher_free( &operation->ctx.cmac );
1721 }
1722 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001723#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001724#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001725 if( PSA_ALG_IS_HMAC( operation->alg ) )
1726 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001727 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001728 }
1729 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001730#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001731 {
1732 /* Sanity check (shouldn't happen: operation->alg should
1733 * always have been initialized to a valid value). */
1734 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001735 }
Moran Peker41deec42018-04-04 15:43:05 +03001736
Gilles Peskine8c9def32018-02-08 10:02:12 +01001737 operation->alg = 0;
1738 operation->key_set = 0;
1739 operation->iv_set = 0;
1740 operation->iv_required = 0;
1741 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001742 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001743
Gilles Peskine8c9def32018-02-08 10:02:12 +01001744 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001745
1746bad_state:
1747 /* If abort is called on an uninitialized object, we can't trust
1748 * anything. Wipe the object in case it contains confidential data.
1749 * This may result in a memory leak if a pointer gets overwritten,
1750 * but it's too late to do anything about this. */
1751 memset( operation, 0, sizeof( *operation ) );
1752 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001753}
1754
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001755#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001756static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001757 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001758 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001759 const mbedtls_cipher_info_t *cipher_info )
1760{
1761 int ret;
1762
1763 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001764
1765 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1766 if( ret != 0 )
1767 return( ret );
1768
1769 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1770 slot->data.raw.data,
1771 key_bits );
1772 return( ret );
1773}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001774#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001775
Gilles Peskine248051a2018-06-20 16:09:38 +02001776#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001777static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1778 const uint8_t *key,
1779 size_t key_length,
1780 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001781{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001782 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001783 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001784 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001785 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001786 psa_status_t status;
1787
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001788 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1789 * overflow below. This should never trigger if the hash algorithm
1790 * is implemented correctly. */
1791 /* The size checks against the ipad and opad buffers cannot be written
1792 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1793 * because that triggers -Wlogical-op on GCC 7.3. */
1794 if( block_size > sizeof( ipad ) )
1795 return( PSA_ERROR_NOT_SUPPORTED );
1796 if( block_size > sizeof( hmac->opad ) )
1797 return( PSA_ERROR_NOT_SUPPORTED );
1798 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001799 return( PSA_ERROR_NOT_SUPPORTED );
1800
Gilles Peskined223b522018-06-11 18:12:58 +02001801 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001802 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001803 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001804 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001805 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001806 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001807 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001808 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001809 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001810 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001811 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001812 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001813 }
Gilles Peskine96889972018-07-12 17:07:03 +02001814 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1815 * but it is permitted. It is common when HMAC is used in HKDF, for
1816 * example. Don't call `memcpy` in the 0-length because `key` could be
1817 * an invalid pointer which would make the behavior undefined. */
1818 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001819 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001820
Gilles Peskined223b522018-06-11 18:12:58 +02001821 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1822 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001823 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001824 ipad[i] ^= 0x36;
1825 memset( ipad + key_length, 0x36, block_size - key_length );
1826
1827 /* Copy the key material from ipad to opad, flipping the requisite bits,
1828 * and filling the rest of opad with the requisite constant. */
1829 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001830 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1831 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001832
Gilles Peskine01126fa2018-07-12 17:04:55 +02001833 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001834 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001835 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001836
Gilles Peskine01126fa2018-07-12 17:04:55 +02001837 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001838
1839cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001840 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001841
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001842 return( status );
1843}
Gilles Peskine248051a2018-06-20 16:09:38 +02001844#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001845
Gilles Peskine89167cb2018-07-08 20:12:23 +02001846static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001847 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001848 psa_algorithm_t alg,
1849 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001850{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001851 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001852 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001853 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001854 psa_key_usage_t usage =
1855 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001856 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001857 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001858
Gilles Peskined911eb72018-08-14 15:18:45 +02001859 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001860 if( status != PSA_SUCCESS )
1861 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001862 if( is_sign )
1863 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001864
Gilles Peskinec5487a82018-12-03 18:08:14 +01001865 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001866 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001867 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001868 key_bits = psa_get_key_bits( slot );
1869
Gilles Peskine8c9def32018-02-08 10:02:12 +01001870#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001871 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001872 {
1873 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001874 mbedtls_cipher_info_from_psa( full_length_alg,
1875 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001876 int ret;
1877 if( cipher_info == NULL )
1878 {
1879 status = PSA_ERROR_NOT_SUPPORTED;
1880 goto exit;
1881 }
1882 operation->mac_size = cipher_info->block_size;
1883 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1884 status = mbedtls_to_psa_error( ret );
1885 }
1886 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001887#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001888#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001889 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001890 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001891 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001892 if( hash_alg == 0 )
1893 {
1894 status = PSA_ERROR_NOT_SUPPORTED;
1895 goto exit;
1896 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001897
1898 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1899 /* Sanity check. This shouldn't fail on a valid configuration. */
1900 if( operation->mac_size == 0 ||
1901 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1902 {
1903 status = PSA_ERROR_NOT_SUPPORTED;
1904 goto exit;
1905 }
1906
Gilles Peskine01126fa2018-07-12 17:04:55 +02001907 if( slot->type != PSA_KEY_TYPE_HMAC )
1908 {
1909 status = PSA_ERROR_INVALID_ARGUMENT;
1910 goto exit;
1911 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001912
Gilles Peskine01126fa2018-07-12 17:04:55 +02001913 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1914 slot->data.raw.data,
1915 slot->data.raw.bytes,
1916 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001917 }
1918 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001919#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001920 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00001921 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02001922 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001923 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001924
Gilles Peskined911eb72018-08-14 15:18:45 +02001925 if( truncated == 0 )
1926 {
1927 /* The "normal" case: untruncated algorithm. Nothing to do. */
1928 }
1929 else if( truncated < 4 )
1930 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001931 /* A very short MAC is too short for security since it can be
1932 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1933 * so we make this our minimum, even though 32 bits is still
1934 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001935 status = PSA_ERROR_NOT_SUPPORTED;
1936 }
1937 else if( truncated > operation->mac_size )
1938 {
1939 /* It's impossible to "truncate" to a larger length. */
1940 status = PSA_ERROR_INVALID_ARGUMENT;
1941 }
1942 else
1943 operation->mac_size = truncated;
1944
Gilles Peskinefbfac682018-07-08 20:51:54 +02001945exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001946 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001947 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001948 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001949 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001950 else
1951 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001952 operation->key_set = 1;
1953 }
1954 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001955}
1956
Gilles Peskine89167cb2018-07-08 20:12:23 +02001957psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001958 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001959 psa_algorithm_t alg )
1960{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001961 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001962}
1963
1964psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001965 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001966 psa_algorithm_t alg )
1967{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001968 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001969}
1970
Gilles Peskine8c9def32018-02-08 10:02:12 +01001971psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1972 const uint8_t *input,
1973 size_t input_length )
1974{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001975 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001976 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001977 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001978 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001979 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001980 operation->has_input = 1;
1981
Gilles Peskine8c9def32018-02-08 10:02:12 +01001982#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001983 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001984 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001985 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1986 input, input_length );
1987 status = mbedtls_to_psa_error( ret );
1988 }
1989 else
1990#endif /* MBEDTLS_CMAC_C */
1991#if defined(MBEDTLS_MD_C)
1992 if( PSA_ALG_IS_HMAC( operation->alg ) )
1993 {
1994 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1995 input_length );
1996 }
1997 else
1998#endif /* MBEDTLS_MD_C */
1999 {
2000 /* This shouldn't happen if `operation` was initialized by
2001 * a setup function. */
2002 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002003 }
2004
Gilles Peskinefbfac682018-07-08 20:51:54 +02002005cleanup:
2006 if( status != PSA_SUCCESS )
2007 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002008 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002009}
2010
Gilles Peskine01126fa2018-07-12 17:04:55 +02002011#if defined(MBEDTLS_MD_C)
2012static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2013 uint8_t *mac,
2014 size_t mac_size )
2015{
2016 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2017 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2018 size_t hash_size = 0;
2019 size_t block_size = psa_get_hash_block_size( hash_alg );
2020 psa_status_t status;
2021
Gilles Peskine01126fa2018-07-12 17:04:55 +02002022 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2023 if( status != PSA_SUCCESS )
2024 return( status );
2025 /* From here on, tmp needs to be wiped. */
2026
2027 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2028 if( status != PSA_SUCCESS )
2029 goto exit;
2030
2031 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2032 if( status != PSA_SUCCESS )
2033 goto exit;
2034
2035 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2036 if( status != PSA_SUCCESS )
2037 goto exit;
2038
Gilles Peskined911eb72018-08-14 15:18:45 +02002039 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2040 if( status != PSA_SUCCESS )
2041 goto exit;
2042
2043 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002044
2045exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002046 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002047 return( status );
2048}
2049#endif /* MBEDTLS_MD_C */
2050
mohammad16036df908f2018-04-02 08:34:15 -07002051static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002052 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002053 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002054{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002055 if( ! operation->key_set )
2056 return( PSA_ERROR_BAD_STATE );
2057 if( operation->iv_required && ! operation->iv_set )
2058 return( PSA_ERROR_BAD_STATE );
2059
Gilles Peskine8c9def32018-02-08 10:02:12 +01002060 if( mac_size < operation->mac_size )
2061 return( PSA_ERROR_BUFFER_TOO_SMALL );
2062
Gilles Peskine8c9def32018-02-08 10:02:12 +01002063#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002064 if( operation->alg == PSA_ALG_CMAC )
2065 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002066 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2067 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2068 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002069 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002070 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002071 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002072 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002073 else
2074#endif /* MBEDTLS_CMAC_C */
2075#if defined(MBEDTLS_MD_C)
2076 if( PSA_ALG_IS_HMAC( operation->alg ) )
2077 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002078 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002079 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002080 }
2081 else
2082#endif /* MBEDTLS_MD_C */
2083 {
2084 /* This shouldn't happen if `operation` was initialized by
2085 * a setup function. */
2086 return( PSA_ERROR_BAD_STATE );
2087 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002088}
2089
Gilles Peskineacd4be32018-07-08 19:56:25 +02002090psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2091 uint8_t *mac,
2092 size_t mac_size,
2093 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002094{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002095 psa_status_t status;
2096
2097 /* Fill the output buffer with something that isn't a valid mac
2098 * (barring an attack on the mac and deliberately-crafted input),
2099 * in case the caller doesn't check the return status properly. */
2100 *mac_length = mac_size;
2101 /* If mac_size is 0 then mac may be NULL and then the
2102 * call to memset would have undefined behavior. */
2103 if( mac_size != 0 )
2104 memset( mac, '!', mac_size );
2105
Gilles Peskine89167cb2018-07-08 20:12:23 +02002106 if( ! operation->is_sign )
2107 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002108 status = PSA_ERROR_BAD_STATE;
2109 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002110 }
mohammad16036df908f2018-04-02 08:34:15 -07002111
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002112 status = psa_mac_finish_internal( operation, mac, mac_size );
2113
2114cleanup:
2115 if( status == PSA_SUCCESS )
2116 {
2117 status = psa_mac_abort( operation );
2118 if( status == PSA_SUCCESS )
2119 *mac_length = operation->mac_size;
2120 else
2121 memset( mac, '!', mac_size );
2122 }
2123 else
2124 psa_mac_abort( operation );
2125 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002126}
2127
Gilles Peskineacd4be32018-07-08 19:56:25 +02002128psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2129 const uint8_t *mac,
2130 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002131{
Gilles Peskine828ed142018-06-18 23:25:51 +02002132 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002133 psa_status_t status;
2134
Gilles Peskine89167cb2018-07-08 20:12:23 +02002135 if( operation->is_sign )
2136 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002137 status = PSA_ERROR_BAD_STATE;
2138 goto cleanup;
2139 }
2140 if( operation->mac_size != mac_length )
2141 {
2142 status = PSA_ERROR_INVALID_SIGNATURE;
2143 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002144 }
mohammad16036df908f2018-04-02 08:34:15 -07002145
2146 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002147 actual_mac, sizeof( actual_mac ) );
2148
2149 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2150 status = PSA_ERROR_INVALID_SIGNATURE;
2151
2152cleanup:
2153 if( status == PSA_SUCCESS )
2154 status = psa_mac_abort( operation );
2155 else
2156 psa_mac_abort( operation );
2157
Gilles Peskine3f108122018-12-07 18:14:53 +01002158 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002159
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002160 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002161}
2162
2163
Gilles Peskine20035e32018-02-03 22:44:14 +01002164
Gilles Peskine20035e32018-02-03 22:44:14 +01002165/****************************************************************/
2166/* Asymmetric cryptography */
2167/****************************************************************/
2168
Gilles Peskine2b450e32018-06-27 15:42:46 +02002169#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002170/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002171 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002172static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2173 size_t hash_length,
2174 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002175{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002176 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002177 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002178 *md_alg = mbedtls_md_get_type( md_info );
2179
2180 /* The Mbed TLS RSA module uses an unsigned int for hash length
2181 * parameters. Validate that it fits so that we don't risk an
2182 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002183#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002184 if( hash_length > UINT_MAX )
2185 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002186#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002187
2188#if defined(MBEDTLS_PKCS1_V15)
2189 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2190 * must be correct. */
2191 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2192 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002193 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002194 if( md_info == NULL )
2195 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002196 if( mbedtls_md_get_size( md_info ) != hash_length )
2197 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002198 }
2199#endif /* MBEDTLS_PKCS1_V15 */
2200
2201#if defined(MBEDTLS_PKCS1_V21)
2202 /* PSS requires a hash internally. */
2203 if( PSA_ALG_IS_RSA_PSS( alg ) )
2204 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002205 if( md_info == NULL )
2206 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002207 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002208#endif /* MBEDTLS_PKCS1_V21 */
2209
Gilles Peskine61b91d42018-06-08 16:09:36 +02002210 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002211}
2212
Gilles Peskine2b450e32018-06-27 15:42:46 +02002213static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2214 psa_algorithm_t alg,
2215 const uint8_t *hash,
2216 size_t hash_length,
2217 uint8_t *signature,
2218 size_t signature_size,
2219 size_t *signature_length )
2220{
2221 psa_status_t status;
2222 int ret;
2223 mbedtls_md_type_t md_alg;
2224
2225 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2226 if( status != PSA_SUCCESS )
2227 return( status );
2228
Gilles Peskine630a18a2018-06-29 17:49:35 +02002229 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002230 return( PSA_ERROR_BUFFER_TOO_SMALL );
2231
2232#if defined(MBEDTLS_PKCS1_V15)
2233 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2234 {
2235 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2236 MBEDTLS_MD_NONE );
2237 ret = mbedtls_rsa_pkcs1_sign( rsa,
2238 mbedtls_ctr_drbg_random,
2239 &global_data.ctr_drbg,
2240 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002241 md_alg,
2242 (unsigned int) hash_length,
2243 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002244 signature );
2245 }
2246 else
2247#endif /* MBEDTLS_PKCS1_V15 */
2248#if defined(MBEDTLS_PKCS1_V21)
2249 if( PSA_ALG_IS_RSA_PSS( alg ) )
2250 {
2251 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2252 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2253 mbedtls_ctr_drbg_random,
2254 &global_data.ctr_drbg,
2255 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002256 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002257 (unsigned int) hash_length,
2258 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002259 signature );
2260 }
2261 else
2262#endif /* MBEDTLS_PKCS1_V21 */
2263 {
2264 return( PSA_ERROR_INVALID_ARGUMENT );
2265 }
2266
2267 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002268 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002269 return( mbedtls_to_psa_error( ret ) );
2270}
2271
2272static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2273 psa_algorithm_t alg,
2274 const uint8_t *hash,
2275 size_t hash_length,
2276 const uint8_t *signature,
2277 size_t signature_length )
2278{
2279 psa_status_t status;
2280 int ret;
2281 mbedtls_md_type_t md_alg;
2282
2283 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2284 if( status != PSA_SUCCESS )
2285 return( status );
2286
Gilles Peskine630a18a2018-06-29 17:49:35 +02002287 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002288 return( PSA_ERROR_BUFFER_TOO_SMALL );
2289
2290#if defined(MBEDTLS_PKCS1_V15)
2291 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2292 {
2293 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2294 MBEDTLS_MD_NONE );
2295 ret = mbedtls_rsa_pkcs1_verify( rsa,
2296 mbedtls_ctr_drbg_random,
2297 &global_data.ctr_drbg,
2298 MBEDTLS_RSA_PUBLIC,
2299 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002300 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002301 hash,
2302 signature );
2303 }
2304 else
2305#endif /* MBEDTLS_PKCS1_V15 */
2306#if defined(MBEDTLS_PKCS1_V21)
2307 if( PSA_ALG_IS_RSA_PSS( alg ) )
2308 {
2309 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2310 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2311 mbedtls_ctr_drbg_random,
2312 &global_data.ctr_drbg,
2313 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002314 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002315 (unsigned int) hash_length,
2316 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002317 signature );
2318 }
2319 else
2320#endif /* MBEDTLS_PKCS1_V21 */
2321 {
2322 return( PSA_ERROR_INVALID_ARGUMENT );
2323 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002324
2325 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2326 * the rest of the signature is invalid". This has little use in
2327 * practice and PSA doesn't report this distinction. */
2328 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2329 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002330 return( mbedtls_to_psa_error( ret ) );
2331}
2332#endif /* MBEDTLS_RSA_C */
2333
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002334#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002335/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2336 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2337 * (even though these functions don't modify it). */
2338static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2339 psa_algorithm_t alg,
2340 const uint8_t *hash,
2341 size_t hash_length,
2342 uint8_t *signature,
2343 size_t signature_size,
2344 size_t *signature_length )
2345{
2346 int ret;
2347 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002348 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002349 mbedtls_mpi_init( &r );
2350 mbedtls_mpi_init( &s );
2351
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002352 if( signature_size < 2 * curve_bytes )
2353 {
2354 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2355 goto cleanup;
2356 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002357
Gilles Peskinea05219c2018-11-16 16:02:56 +01002358#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002359 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2360 {
2361 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2362 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2363 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2364 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2365 hash, hash_length,
2366 md_alg ) );
2367 }
2368 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002369#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002370 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002371 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002372 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2373 hash, hash_length,
2374 mbedtls_ctr_drbg_random,
2375 &global_data.ctr_drbg ) );
2376 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002377
2378 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2379 signature,
2380 curve_bytes ) );
2381 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2382 signature + curve_bytes,
2383 curve_bytes ) );
2384
2385cleanup:
2386 mbedtls_mpi_free( &r );
2387 mbedtls_mpi_free( &s );
2388 if( ret == 0 )
2389 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002390 return( mbedtls_to_psa_error( ret ) );
2391}
2392
2393static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2394 const uint8_t *hash,
2395 size_t hash_length,
2396 const uint8_t *signature,
2397 size_t signature_length )
2398{
2399 int ret;
2400 mbedtls_mpi r, s;
2401 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2402 mbedtls_mpi_init( &r );
2403 mbedtls_mpi_init( &s );
2404
2405 if( signature_length != 2 * curve_bytes )
2406 return( PSA_ERROR_INVALID_SIGNATURE );
2407
2408 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2409 signature,
2410 curve_bytes ) );
2411 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2412 signature + curve_bytes,
2413 curve_bytes ) );
2414
2415 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2416 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002417
2418cleanup:
2419 mbedtls_mpi_free( &r );
2420 mbedtls_mpi_free( &s );
2421 return( mbedtls_to_psa_error( ret ) );
2422}
2423#endif /* MBEDTLS_ECDSA_C */
2424
Gilles Peskinec5487a82018-12-03 18:08:14 +01002425psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002426 psa_algorithm_t alg,
2427 const uint8_t *hash,
2428 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002429 uint8_t *signature,
2430 size_t signature_size,
2431 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002432{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002433 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002434 psa_status_t status;
2435
2436 *signature_length = signature_size;
2437
Gilles Peskinec5487a82018-12-03 18:08:14 +01002438 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002439 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002440 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002441 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002442 {
2443 status = PSA_ERROR_INVALID_ARGUMENT;
2444 goto exit;
2445 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002446
Gilles Peskine20035e32018-02-03 22:44:14 +01002447#if defined(MBEDTLS_RSA_C)
2448 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2449 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002450 status = psa_rsa_sign( slot->data.rsa,
2451 alg,
2452 hash, hash_length,
2453 signature, signature_size,
2454 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002455 }
2456 else
2457#endif /* defined(MBEDTLS_RSA_C) */
2458#if defined(MBEDTLS_ECP_C)
2459 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2460 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002461#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002462 if(
2463#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2464 PSA_ALG_IS_ECDSA( alg )
2465#else
2466 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2467#endif
2468 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002469 status = psa_ecdsa_sign( slot->data.ecp,
2470 alg,
2471 hash, hash_length,
2472 signature, signature_size,
2473 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002474 else
2475#endif /* defined(MBEDTLS_ECDSA_C) */
2476 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002477 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002478 }
itayzafrir5c753392018-05-08 11:18:38 +03002479 }
2480 else
2481#endif /* defined(MBEDTLS_ECP_C) */
2482 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002483 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002484 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002485
2486exit:
2487 /* Fill the unused part of the output buffer (the whole buffer on error,
2488 * the trailing part on success) with something that isn't a valid mac
2489 * (barring an attack on the mac and deliberately-crafted input),
2490 * in case the caller doesn't check the return status properly. */
2491 if( status == PSA_SUCCESS )
2492 memset( signature + *signature_length, '!',
2493 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002494 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002495 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002496 /* If signature_size is 0 then we have nothing to do. We must not call
2497 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002498 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002499}
2500
Gilles Peskinec5487a82018-12-03 18:08:14 +01002501psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002502 psa_algorithm_t alg,
2503 const uint8_t *hash,
2504 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002505 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002506 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002507{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002508 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002509 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002510
Gilles Peskinec5487a82018-12-03 18:08:14 +01002511 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002512 if( status != PSA_SUCCESS )
2513 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002514
Gilles Peskine61b91d42018-06-08 16:09:36 +02002515#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002516 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002517 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002518 return( psa_rsa_verify( slot->data.rsa,
2519 alg,
2520 hash, hash_length,
2521 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002522 }
2523 else
2524#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002525#if defined(MBEDTLS_ECP_C)
2526 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2527 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002528#if defined(MBEDTLS_ECDSA_C)
2529 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002530 return( psa_ecdsa_verify( slot->data.ecp,
2531 hash, hash_length,
2532 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002533 else
2534#endif /* defined(MBEDTLS_ECDSA_C) */
2535 {
2536 return( PSA_ERROR_INVALID_ARGUMENT );
2537 }
itayzafrir5c753392018-05-08 11:18:38 +03002538 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002539 else
2540#endif /* defined(MBEDTLS_ECP_C) */
2541 {
2542 return( PSA_ERROR_NOT_SUPPORTED );
2543 }
2544}
2545
Gilles Peskine072ac562018-06-30 00:21:29 +02002546#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2547static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2548 mbedtls_rsa_context *rsa )
2549{
2550 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2551 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2552 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2553 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2554}
2555#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2556
Gilles Peskinec5487a82018-12-03 18:08:14 +01002557psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002558 psa_algorithm_t alg,
2559 const uint8_t *input,
2560 size_t input_length,
2561 const uint8_t *salt,
2562 size_t salt_length,
2563 uint8_t *output,
2564 size_t output_size,
2565 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002566{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002567 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002568 psa_status_t status;
2569
Darryl Green5cc689a2018-07-24 15:34:10 +01002570 (void) input;
2571 (void) input_length;
2572 (void) salt;
2573 (void) output;
2574 (void) output_size;
2575
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002576 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002577
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002578 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2579 return( PSA_ERROR_INVALID_ARGUMENT );
2580
Gilles Peskinec5487a82018-12-03 18:08:14 +01002581 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002582 if( status != PSA_SUCCESS )
2583 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002584 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2585 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002586 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002587
2588#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002589 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002590 {
2591 mbedtls_rsa_context *rsa = slot->data.rsa;
2592 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002593 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002594 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002595#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002596 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002597 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002598 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2599 mbedtls_ctr_drbg_random,
2600 &global_data.ctr_drbg,
2601 MBEDTLS_RSA_PUBLIC,
2602 input_length,
2603 input,
2604 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002605 }
2606 else
2607#endif /* MBEDTLS_PKCS1_V15 */
2608#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002609 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002610 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002611 psa_rsa_oaep_set_padding_mode( alg, rsa );
2612 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2613 mbedtls_ctr_drbg_random,
2614 &global_data.ctr_drbg,
2615 MBEDTLS_RSA_PUBLIC,
2616 salt, salt_length,
2617 input_length,
2618 input,
2619 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002620 }
2621 else
2622#endif /* MBEDTLS_PKCS1_V21 */
2623 {
2624 return( PSA_ERROR_INVALID_ARGUMENT );
2625 }
2626 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002627 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002628 return( mbedtls_to_psa_error( ret ) );
2629 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002630 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002631#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002632 {
2633 return( PSA_ERROR_NOT_SUPPORTED );
2634 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002635}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002636
Gilles Peskinec5487a82018-12-03 18:08:14 +01002637psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002638 psa_algorithm_t alg,
2639 const uint8_t *input,
2640 size_t input_length,
2641 const uint8_t *salt,
2642 size_t salt_length,
2643 uint8_t *output,
2644 size_t output_size,
2645 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002646{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002647 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002648 psa_status_t status;
2649
Darryl Green5cc689a2018-07-24 15:34:10 +01002650 (void) input;
2651 (void) input_length;
2652 (void) salt;
2653 (void) output;
2654 (void) output_size;
2655
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002656 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002657
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002658 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2659 return( PSA_ERROR_INVALID_ARGUMENT );
2660
Gilles Peskinec5487a82018-12-03 18:08:14 +01002661 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002662 if( status != PSA_SUCCESS )
2663 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002664 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2665 return( PSA_ERROR_INVALID_ARGUMENT );
2666
2667#if defined(MBEDTLS_RSA_C)
2668 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2669 {
2670 mbedtls_rsa_context *rsa = slot->data.rsa;
2671 int ret;
2672
Gilles Peskine630a18a2018-06-29 17:49:35 +02002673 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002674 return( PSA_ERROR_INVALID_ARGUMENT );
2675
2676#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002677 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002678 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002679 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2680 mbedtls_ctr_drbg_random,
2681 &global_data.ctr_drbg,
2682 MBEDTLS_RSA_PRIVATE,
2683 output_length,
2684 input,
2685 output,
2686 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002687 }
2688 else
2689#endif /* MBEDTLS_PKCS1_V15 */
2690#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002691 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002692 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002693 psa_rsa_oaep_set_padding_mode( alg, rsa );
2694 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2695 mbedtls_ctr_drbg_random,
2696 &global_data.ctr_drbg,
2697 MBEDTLS_RSA_PRIVATE,
2698 salt, salt_length,
2699 output_length,
2700 input,
2701 output,
2702 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002703 }
2704 else
2705#endif /* MBEDTLS_PKCS1_V21 */
2706 {
2707 return( PSA_ERROR_INVALID_ARGUMENT );
2708 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002709
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002710 return( mbedtls_to_psa_error( ret ) );
2711 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002712 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002713#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002714 {
2715 return( PSA_ERROR_NOT_SUPPORTED );
2716 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002717}
Gilles Peskine20035e32018-02-03 22:44:14 +01002718
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002719
2720
mohammad1603503973b2018-03-12 15:59:30 +02002721/****************************************************************/
2722/* Symmetric cryptography */
2723/****************************************************************/
2724
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002725/* Initialize the cipher operation structure. Once this function has been
2726 * called, psa_cipher_abort can run and will do the right thing. */
2727static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2728 psa_algorithm_t alg )
2729{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002730 if( ! PSA_ALG_IS_CIPHER( alg ) )
2731 {
2732 memset( operation, 0, sizeof( *operation ) );
2733 return( PSA_ERROR_INVALID_ARGUMENT );
2734 }
2735
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002736 operation->alg = alg;
2737 operation->key_set = 0;
2738 operation->iv_set = 0;
2739 operation->iv_required = 1;
2740 operation->iv_size = 0;
2741 operation->block_size = 0;
2742 mbedtls_cipher_init( &operation->ctx.cipher );
2743 return( PSA_SUCCESS );
2744}
2745
Gilles Peskinee553c652018-06-04 16:22:46 +02002746static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002747 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002748 psa_algorithm_t alg,
2749 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002750{
2751 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2752 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002753 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002754 size_t key_bits;
2755 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002756 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2757 PSA_KEY_USAGE_ENCRYPT :
2758 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002759
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002760 status = psa_cipher_init( operation, alg );
2761 if( status != PSA_SUCCESS )
2762 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002763
Gilles Peskinec5487a82018-12-03 18:08:14 +01002764 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002765 if( status != PSA_SUCCESS )
2766 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002767 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002768
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002769 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002770 if( cipher_info == NULL )
2771 return( PSA_ERROR_NOT_SUPPORTED );
2772
mohammad1603503973b2018-03-12 15:59:30 +02002773 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002774 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002775 {
2776 psa_cipher_abort( operation );
2777 return( mbedtls_to_psa_error( ret ) );
2778 }
2779
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002780#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002781 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002782 {
2783 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2784 unsigned char keys[24];
2785 memcpy( keys, slot->data.raw.data, 16 );
2786 memcpy( keys + 16, slot->data.raw.data, 8 );
2787 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2788 keys,
2789 192, cipher_operation );
2790 }
2791 else
2792#endif
2793 {
2794 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2795 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002796 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002797 }
Moran Peker41deec42018-04-04 15:43:05 +03002798 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002799 {
2800 psa_cipher_abort( operation );
2801 return( mbedtls_to_psa_error( ret ) );
2802 }
2803
mohammad16038481e742018-03-18 13:57:31 +02002804#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002805 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002806 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002807 case PSA_ALG_CBC_NO_PADDING:
2808 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2809 MBEDTLS_PADDING_NONE );
2810 break;
2811 case PSA_ALG_CBC_PKCS7:
2812 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2813 MBEDTLS_PADDING_PKCS7 );
2814 break;
2815 default:
2816 /* The algorithm doesn't involve padding. */
2817 ret = 0;
2818 break;
2819 }
2820 if( ret != 0 )
2821 {
2822 psa_cipher_abort( operation );
2823 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002824 }
2825#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2826
mohammad1603503973b2018-03-12 15:59:30 +02002827 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002828 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2829 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2830 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002831 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002832 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002833 }
mohammad1603503973b2018-03-12 15:59:30 +02002834
Moran Peker395db872018-05-31 14:07:14 +03002835 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002836}
2837
Gilles Peskinefe119512018-07-08 21:39:34 +02002838psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002839 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002840 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002841{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002842 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002843}
2844
Gilles Peskinefe119512018-07-08 21:39:34 +02002845psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002846 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002847 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002848{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002849 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002850}
2851
Gilles Peskinefe119512018-07-08 21:39:34 +02002852psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2853 unsigned char *iv,
2854 size_t iv_size,
2855 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002856{
itayzafrir534bd7c2018-08-02 13:56:32 +03002857 psa_status_t status;
2858 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002859 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002860 {
2861 status = PSA_ERROR_BAD_STATE;
2862 goto exit;
2863 }
Moran Peker41deec42018-04-04 15:43:05 +03002864 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002865 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002866 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002867 goto exit;
2868 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002869 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2870 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002871 if( ret != 0 )
2872 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002873 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002874 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002875 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002876
mohammad16038481e742018-03-18 13:57:31 +02002877 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002878 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002879
Moran Peker395db872018-05-31 14:07:14 +03002880exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002881 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002882 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002883 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002884}
2885
Gilles Peskinefe119512018-07-08 21:39:34 +02002886psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2887 const unsigned char *iv,
2888 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002889{
itayzafrir534bd7c2018-08-02 13:56:32 +03002890 psa_status_t status;
2891 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002892 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002893 {
2894 status = PSA_ERROR_BAD_STATE;
2895 goto exit;
2896 }
Moran Pekera28258c2018-05-29 16:25:04 +03002897 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002898 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002899 status = PSA_ERROR_INVALID_ARGUMENT;
2900 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002901 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002902 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2903 status = mbedtls_to_psa_error( ret );
2904exit:
2905 if( status == PSA_SUCCESS )
2906 operation->iv_set = 1;
2907 else
mohammad1603503973b2018-03-12 15:59:30 +02002908 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002909 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002910}
2911
Gilles Peskinee553c652018-06-04 16:22:46 +02002912psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2913 const uint8_t *input,
2914 size_t input_length,
2915 unsigned char *output,
2916 size_t output_size,
2917 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002918{
itayzafrir534bd7c2018-08-02 13:56:32 +03002919 psa_status_t status;
2920 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002921 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002922 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002923 {
2924 /* Take the unprocessed partial block left over from previous
2925 * update calls, if any, plus the input to this call. Remove
2926 * the last partial block, if any. You get the data that will be
2927 * output in this call. */
2928 expected_output_size =
2929 ( operation->ctx.cipher.unprocessed_len + input_length )
2930 / operation->block_size * operation->block_size;
2931 }
2932 else
2933 {
2934 expected_output_size = input_length;
2935 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002936
Gilles Peskine89d789c2018-06-04 17:17:16 +02002937 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002938 {
2939 status = PSA_ERROR_BUFFER_TOO_SMALL;
2940 goto exit;
2941 }
mohammad160382759612018-03-12 18:16:40 +02002942
mohammad1603503973b2018-03-12 15:59:30 +02002943 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002944 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002945 status = mbedtls_to_psa_error( ret );
2946exit:
2947 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002948 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002949 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002950}
2951
Gilles Peskinee553c652018-06-04 16:22:46 +02002952psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2953 uint8_t *output,
2954 size_t output_size,
2955 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002956{
Janos Follath315b51c2018-07-09 16:04:51 +01002957 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2958 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002959 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002960
mohammad1603503973b2018-03-12 15:59:30 +02002961 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002962 {
Janos Follath315b51c2018-07-09 16:04:51 +01002963 status = PSA_ERROR_BAD_STATE;
2964 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002965 }
2966 if( operation->iv_required && ! operation->iv_set )
2967 {
Janos Follath315b51c2018-07-09 16:04:51 +01002968 status = PSA_ERROR_BAD_STATE;
2969 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002970 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002971
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002972 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002973 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2974 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002975 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002976 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002977 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002978 }
2979
Janos Follath315b51c2018-07-09 16:04:51 +01002980 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2981 temp_output_buffer,
2982 output_length );
2983 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002984 {
Janos Follath315b51c2018-07-09 16:04:51 +01002985 status = mbedtls_to_psa_error( cipher_ret );
2986 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002987 }
Janos Follath315b51c2018-07-09 16:04:51 +01002988
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002989 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002990 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002991 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002992 memcpy( output, temp_output_buffer, *output_length );
2993 else
2994 {
Janos Follath315b51c2018-07-09 16:04:51 +01002995 status = PSA_ERROR_BUFFER_TOO_SMALL;
2996 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002997 }
mohammad1603503973b2018-03-12 15:59:30 +02002998
Gilles Peskine3f108122018-12-07 18:14:53 +01002999 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003000 status = psa_cipher_abort( operation );
3001
3002 return( status );
3003
3004error:
3005
3006 *output_length = 0;
3007
Gilles Peskine3f108122018-12-07 18:14:53 +01003008 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003009 (void) psa_cipher_abort( operation );
3010
3011 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003012}
3013
Gilles Peskinee553c652018-06-04 16:22:46 +02003014psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3015{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003016 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003017 {
3018 /* The object has (apparently) been initialized but it is not
3019 * in use. It's ok to call abort on such an object, and there's
3020 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003021 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003022 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003023
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003024 /* Sanity check (shouldn't happen: operation->alg should
3025 * always have been initialized to a valid value). */
3026 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3027 return( PSA_ERROR_BAD_STATE );
3028
mohammad1603503973b2018-03-12 15:59:30 +02003029 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003030
Moran Peker41deec42018-04-04 15:43:05 +03003031 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003032 operation->key_set = 0;
3033 operation->iv_set = 0;
3034 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003035 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003036 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003037
Moran Peker395db872018-05-31 14:07:14 +03003038 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003039}
3040
Gilles Peskinea0655c32018-04-30 17:06:50 +02003041
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003042
mohammad16038cc1cee2018-03-28 01:21:33 +03003043/****************************************************************/
3044/* Key Policy */
3045/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003046
mohammad160327010052018-07-03 13:16:15 +03003047#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003048void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003049{
Gilles Peskine803ce742018-06-18 16:07:14 +02003050 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003051}
3052
Gilles Peskine2d277862018-06-18 15:41:12 +02003053void psa_key_policy_set_usage( psa_key_policy_t *policy,
3054 psa_key_usage_t usage,
3055 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003056{
mohammad16034eed7572018-03-28 05:14:59 -07003057 policy->usage = usage;
3058 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003059}
3060
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003061psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003062{
mohammad16036df908f2018-04-02 08:34:15 -07003063 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003064}
3065
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003066psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003067{
mohammad16036df908f2018-04-02 08:34:15 -07003068 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003069}
mohammad160327010052018-07-03 13:16:15 +03003070#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003071
Gilles Peskinec5487a82018-12-03 18:08:14 +01003072psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003073 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003074{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003075 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003076 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003077
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003078 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003079 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003080
Gilles Peskinec5487a82018-12-03 18:08:14 +01003081 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003082 if( status != PSA_SUCCESS )
3083 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003084
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003085 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3086 PSA_KEY_USAGE_ENCRYPT |
3087 PSA_KEY_USAGE_DECRYPT |
3088 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003089 PSA_KEY_USAGE_VERIFY |
3090 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003091 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003092
mohammad16036df908f2018-04-02 08:34:15 -07003093 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003094
3095 return( PSA_SUCCESS );
3096}
3097
Gilles Peskinec5487a82018-12-03 18:08:14 +01003098psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003099 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003100{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003101 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003102 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003103
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003104 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003105 return( PSA_ERROR_INVALID_ARGUMENT );
3106
Gilles Peskinec5487a82018-12-03 18:08:14 +01003107 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003108 if( status != PSA_SUCCESS )
3109 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003110
mohammad16036df908f2018-04-02 08:34:15 -07003111 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003112
3113 return( PSA_SUCCESS );
3114}
Gilles Peskine20035e32018-02-03 22:44:14 +01003115
Gilles Peskinea0655c32018-04-30 17:06:50 +02003116
3117
mohammad1603804cd712018-03-20 22:44:08 +02003118/****************************************************************/
3119/* Key Lifetime */
3120/****************************************************************/
3121
Gilles Peskinec5487a82018-12-03 18:08:14 +01003122psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003123 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003124{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003125 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003126 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003127
Gilles Peskinec5487a82018-12-03 18:08:14 +01003128 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003129 if( status != PSA_SUCCESS )
3130 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003131
mohammad1603804cd712018-03-20 22:44:08 +02003132 *lifetime = slot->lifetime;
3133
3134 return( PSA_SUCCESS );
3135}
3136
Gilles Peskine20035e32018-02-03 22:44:14 +01003137
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003138
mohammad16035955c982018-04-26 00:53:03 +03003139/****************************************************************/
3140/* AEAD */
3141/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003142
Gilles Peskineedf9a652018-08-17 18:11:56 +02003143typedef struct
3144{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003145 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003146 const mbedtls_cipher_info_t *cipher_info;
3147 union
3148 {
3149#if defined(MBEDTLS_CCM_C)
3150 mbedtls_ccm_context ccm;
3151#endif /* MBEDTLS_CCM_C */
3152#if defined(MBEDTLS_GCM_C)
3153 mbedtls_gcm_context gcm;
3154#endif /* MBEDTLS_GCM_C */
3155 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003156 psa_algorithm_t core_alg;
3157 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003158 uint8_t tag_length;
3159} aead_operation_t;
3160
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003161static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003162{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003163 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003164 {
3165#if defined(MBEDTLS_CCM_C)
3166 case PSA_ALG_CCM:
3167 mbedtls_ccm_free( &operation->ctx.ccm );
3168 break;
3169#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003170#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003171 case PSA_ALG_GCM:
3172 mbedtls_gcm_free( &operation->ctx.gcm );
3173 break;
3174#endif /* MBEDTLS_GCM_C */
3175 }
3176}
3177
3178static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003179 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003180 psa_key_usage_t usage,
3181 psa_algorithm_t alg )
3182{
3183 psa_status_t status;
3184 size_t key_bits;
3185 mbedtls_cipher_id_t cipher_id;
3186
Gilles Peskinec5487a82018-12-03 18:08:14 +01003187 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003188 if( status != PSA_SUCCESS )
3189 return( status );
3190
3191 key_bits = psa_get_key_bits( operation->slot );
3192
3193 operation->cipher_info =
3194 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3195 &cipher_id );
3196 if( operation->cipher_info == NULL )
3197 return( PSA_ERROR_NOT_SUPPORTED );
3198
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003199 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003200 {
3201#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003202 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3203 operation->core_alg = PSA_ALG_CCM;
3204 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003205 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3206 return( PSA_ERROR_INVALID_ARGUMENT );
3207 mbedtls_ccm_init( &operation->ctx.ccm );
3208 status = mbedtls_to_psa_error(
3209 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3210 operation->slot->data.raw.data,
3211 (unsigned int) key_bits ) );
3212 if( status != 0 )
3213 goto cleanup;
3214 break;
3215#endif /* MBEDTLS_CCM_C */
3216
3217#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003218 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3219 operation->core_alg = PSA_ALG_GCM;
3220 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003221 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3222 return( PSA_ERROR_INVALID_ARGUMENT );
3223 mbedtls_gcm_init( &operation->ctx.gcm );
3224 status = mbedtls_to_psa_error(
3225 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3226 operation->slot->data.raw.data,
3227 (unsigned int) key_bits ) );
3228 break;
3229#endif /* MBEDTLS_GCM_C */
3230
3231 default:
3232 return( PSA_ERROR_NOT_SUPPORTED );
3233 }
3234
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003235 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3236 {
3237 status = PSA_ERROR_INVALID_ARGUMENT;
3238 goto cleanup;
3239 }
3240 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3241 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3242 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3243 * In both cases, mbedtls_xxx will validate the tag length below. */
3244
Gilles Peskineedf9a652018-08-17 18:11:56 +02003245 return( PSA_SUCCESS );
3246
3247cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003248 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003249 return( status );
3250}
3251
Gilles Peskinec5487a82018-12-03 18:08:14 +01003252psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003253 psa_algorithm_t alg,
3254 const uint8_t *nonce,
3255 size_t nonce_length,
3256 const uint8_t *additional_data,
3257 size_t additional_data_length,
3258 const uint8_t *plaintext,
3259 size_t plaintext_length,
3260 uint8_t *ciphertext,
3261 size_t ciphertext_size,
3262 size_t *ciphertext_length )
3263{
mohammad16035955c982018-04-26 00:53:03 +03003264 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003265 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003266 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003267
mohammad1603f08a5502018-06-03 15:05:47 +03003268 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003269
Gilles Peskinec5487a82018-12-03 18:08:14 +01003270 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003271 if( status != PSA_SUCCESS )
3272 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003273
Gilles Peskineedf9a652018-08-17 18:11:56 +02003274 /* For all currently supported modes, the tag is at the end of the
3275 * ciphertext. */
3276 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3277 {
3278 status = PSA_ERROR_BUFFER_TOO_SMALL;
3279 goto exit;
3280 }
3281 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003282
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003283#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003284 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003285 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003286 status = mbedtls_to_psa_error(
3287 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3288 MBEDTLS_GCM_ENCRYPT,
3289 plaintext_length,
3290 nonce, nonce_length,
3291 additional_data, additional_data_length,
3292 plaintext, ciphertext,
3293 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003294 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003295 else
3296#endif /* MBEDTLS_GCM_C */
3297#if defined(MBEDTLS_CCM_C)
3298 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003299 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003300 status = mbedtls_to_psa_error(
3301 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3302 plaintext_length,
3303 nonce, nonce_length,
3304 additional_data,
3305 additional_data_length,
3306 plaintext, ciphertext,
3307 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003308 }
mohammad16035c8845f2018-05-09 05:40:09 -07003309 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003310#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003311 {
mohammad1603554faad2018-06-03 15:07:38 +03003312 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003313 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003314
Gilles Peskineedf9a652018-08-17 18:11:56 +02003315 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3316 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003317
Gilles Peskineedf9a652018-08-17 18:11:56 +02003318exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003319 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003320 if( status == PSA_SUCCESS )
3321 *ciphertext_length = plaintext_length + operation.tag_length;
3322 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003323}
3324
Gilles Peskineee652a32018-06-01 19:23:52 +02003325/* Locate the tag in a ciphertext buffer containing the encrypted data
3326 * followed by the tag. Return the length of the part preceding the tag in
3327 * *plaintext_length. This is the size of the plaintext in modes where
3328 * the encrypted data has the same size as the plaintext, such as
3329 * CCM and GCM. */
3330static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3331 const uint8_t *ciphertext,
3332 size_t ciphertext_length,
3333 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003334 const uint8_t **p_tag )
3335{
3336 size_t payload_length;
3337 if( tag_length > ciphertext_length )
3338 return( PSA_ERROR_INVALID_ARGUMENT );
3339 payload_length = ciphertext_length - tag_length;
3340 if( payload_length > plaintext_size )
3341 return( PSA_ERROR_BUFFER_TOO_SMALL );
3342 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003343 return( PSA_SUCCESS );
3344}
3345
Gilles Peskinec5487a82018-12-03 18:08:14 +01003346psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003347 psa_algorithm_t alg,
3348 const uint8_t *nonce,
3349 size_t nonce_length,
3350 const uint8_t *additional_data,
3351 size_t additional_data_length,
3352 const uint8_t *ciphertext,
3353 size_t ciphertext_length,
3354 uint8_t *plaintext,
3355 size_t plaintext_size,
3356 size_t *plaintext_length )
3357{
mohammad16035955c982018-04-26 00:53:03 +03003358 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003359 aead_operation_t operation;
3360 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003361
Gilles Peskineee652a32018-06-01 19:23:52 +02003362 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003363
Gilles Peskinec5487a82018-12-03 18:08:14 +01003364 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003365 if( status != PSA_SUCCESS )
3366 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003367
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003368#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003369 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003370 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003371 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003372 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003373 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003374 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003375 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003376
Gilles Peskineedf9a652018-08-17 18:11:56 +02003377 status = mbedtls_to_psa_error(
3378 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3379 ciphertext_length - operation.tag_length,
3380 nonce, nonce_length,
3381 additional_data,
3382 additional_data_length,
3383 tag, operation.tag_length,
3384 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003385 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003386 else
3387#endif /* MBEDTLS_GCM_C */
3388#if defined(MBEDTLS_CCM_C)
3389 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003390 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003391 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003392 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003393 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003394 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003395 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003396
Gilles Peskineedf9a652018-08-17 18:11:56 +02003397 status = mbedtls_to_psa_error(
3398 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3399 ciphertext_length - operation.tag_length,
3400 nonce, nonce_length,
3401 additional_data,
3402 additional_data_length,
3403 ciphertext, plaintext,
3404 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003405 }
mohammad160339574652018-06-01 04:39:53 -07003406 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003407#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003408 {
mohammad1603554faad2018-06-03 15:07:38 +03003409 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003410 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003411
Gilles Peskineedf9a652018-08-17 18:11:56 +02003412 if( status != PSA_SUCCESS && plaintext_size != 0 )
3413 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003414
Gilles Peskineedf9a652018-08-17 18:11:56 +02003415exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003416 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003417 if( status == PSA_SUCCESS )
3418 *plaintext_length = ciphertext_length - operation.tag_length;
3419 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003420}
3421
Gilles Peskinea0655c32018-04-30 17:06:50 +02003422
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003423
Gilles Peskine20035e32018-02-03 22:44:14 +01003424/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003425/* Generators */
3426/****************************************************************/
3427
3428psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3429{
3430 psa_status_t status = PSA_SUCCESS;
3431 if( generator->alg == 0 )
3432 {
3433 /* The object has (apparently) been initialized but it is not
3434 * in use. It's ok to call abort on such an object, and there's
3435 * nothing to do. */
3436 }
3437 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003438 if( generator->alg == PSA_ALG_SELECT_RAW )
3439 {
3440 if( generator->ctx.buffer.data != NULL )
3441 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003442 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003443 generator->ctx.buffer.size );
3444 mbedtls_free( generator->ctx.buffer.data );
3445 }
3446 }
3447 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003448#if defined(MBEDTLS_MD_C)
3449 if( PSA_ALG_IS_HKDF( generator->alg ) )
3450 {
3451 mbedtls_free( generator->ctx.hkdf.info );
3452 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3453 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003454 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3455 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3456 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003457 {
3458 if( generator->ctx.tls12_prf.key != NULL )
3459 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003460 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003461 generator->ctx.tls12_prf.key_len );
3462 mbedtls_free( generator->ctx.tls12_prf.key );
3463 }
Hanno Becker580fba12018-11-13 20:50:45 +00003464
3465 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3466 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003467 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003468 generator->ctx.tls12_prf.Ai_with_seed_len );
3469 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3470 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003471 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003472 else
3473#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003474 {
3475 status = PSA_ERROR_BAD_STATE;
3476 }
3477 memset( generator, 0, sizeof( *generator ) );
3478 return( status );
3479}
3480
3481
3482psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3483 size_t *capacity)
3484{
3485 *capacity = generator->capacity;
3486 return( PSA_SUCCESS );
3487}
3488
Gilles Peskinebef7f142018-07-12 17:22:21 +02003489#if defined(MBEDTLS_MD_C)
3490/* Read some bytes from an HKDF-based generator. This performs a chunk
3491 * of the expand phase of the HKDF algorithm. */
3492static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3493 psa_algorithm_t hash_alg,
3494 uint8_t *output,
3495 size_t output_length )
3496{
3497 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3498 psa_status_t status;
3499
3500 while( output_length != 0 )
3501 {
3502 /* Copy what remains of the current block */
3503 uint8_t n = hash_length - hkdf->offset_in_block;
3504 if( n > output_length )
3505 n = (uint8_t) output_length;
3506 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3507 output += n;
3508 output_length -= n;
3509 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003510 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003511 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003512 /* We can't be wanting more output after block 0xff, otherwise
3513 * the capacity check in psa_generator_read() would have
3514 * prevented this call. It could happen only if the generator
3515 * object was corrupted or if this function is called directly
3516 * inside the library. */
3517 if( hkdf->block_number == 0xff )
3518 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003519
3520 /* We need a new block */
3521 ++hkdf->block_number;
3522 hkdf->offset_in_block = 0;
3523 status = psa_hmac_setup_internal( &hkdf->hmac,
3524 hkdf->prk, hash_length,
3525 hash_alg );
3526 if( status != PSA_SUCCESS )
3527 return( status );
3528 if( hkdf->block_number != 1 )
3529 {
3530 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3531 hkdf->output_block,
3532 hash_length );
3533 if( status != PSA_SUCCESS )
3534 return( status );
3535 }
3536 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3537 hkdf->info,
3538 hkdf->info_length );
3539 if( status != PSA_SUCCESS )
3540 return( status );
3541 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3542 &hkdf->block_number, 1 );
3543 if( status != PSA_SUCCESS )
3544 return( status );
3545 status = psa_hmac_finish_internal( &hkdf->hmac,
3546 hkdf->output_block,
3547 sizeof( hkdf->output_block ) );
3548 if( status != PSA_SUCCESS )
3549 return( status );
3550 }
3551
3552 return( PSA_SUCCESS );
3553}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003554
3555static psa_status_t psa_generator_tls12_prf_generate_next_block(
3556 psa_tls12_prf_generator_t *tls12_prf,
3557 psa_algorithm_t alg )
3558{
3559 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3560 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3561 psa_hmac_internal_data hmac;
3562 psa_status_t status, cleanup_status;
3563
Hanno Becker3b339e22018-11-13 20:56:14 +00003564 unsigned char *Ai;
3565 size_t Ai_len;
3566
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003567 /* We can't be wanting more output after block 0xff, otherwise
3568 * the capacity check in psa_generator_read() would have
3569 * prevented this call. It could happen only if the generator
3570 * object was corrupted or if this function is called directly
3571 * inside the library. */
3572 if( tls12_prf->block_number == 0xff )
3573 return( PSA_ERROR_BAD_STATE );
3574
3575 /* We need a new block */
3576 ++tls12_prf->block_number;
3577 tls12_prf->offset_in_block = 0;
3578
3579 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3580 *
3581 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3582 *
3583 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3584 * HMAC_hash(secret, A(2) + seed) +
3585 * HMAC_hash(secret, A(3) + seed) + ...
3586 *
3587 * A(0) = seed
3588 * A(i) = HMAC_hash( secret, A(i-1) )
3589 *
3590 * The `psa_tls12_prf_generator` structures saves the block
3591 * `HMAC_hash(secret, A(i) + seed)` from which the output
3592 * is currently extracted as `output_block`, while
3593 * `A(i) + seed` is stored in `Ai_with_seed`.
3594 *
3595 * Generating a new block means recalculating `Ai_with_seed`
3596 * from the A(i)-part of it, and afterwards recalculating
3597 * `output_block`.
3598 *
3599 * A(0) is computed at setup time.
3600 *
3601 */
3602
3603 psa_hmac_init_internal( &hmac );
3604
3605 /* We must distinguish the calculation of A(1) from those
3606 * of A(2) and higher, because A(0)=seed has a different
3607 * length than the other A(i). */
3608 if( tls12_prf->block_number == 1 )
3609 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003610 Ai = tls12_prf->Ai_with_seed + hash_length;
3611 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003612 }
3613 else
3614 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003615 Ai = tls12_prf->Ai_with_seed;
3616 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003617 }
3618
Hanno Becker3b339e22018-11-13 20:56:14 +00003619 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3620 status = psa_hmac_setup_internal( &hmac,
3621 tls12_prf->key,
3622 tls12_prf->key_len,
3623 hash_alg );
3624 if( status != PSA_SUCCESS )
3625 goto cleanup;
3626
3627 status = psa_hash_update( &hmac.hash_ctx,
3628 Ai, Ai_len );
3629 if( status != PSA_SUCCESS )
3630 goto cleanup;
3631
3632 status = psa_hmac_finish_internal( &hmac,
3633 tls12_prf->Ai_with_seed,
3634 hash_length );
3635 if( status != PSA_SUCCESS )
3636 goto cleanup;
3637
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003638 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3639 status = psa_hmac_setup_internal( &hmac,
3640 tls12_prf->key,
3641 tls12_prf->key_len,
3642 hash_alg );
3643 if( status != PSA_SUCCESS )
3644 goto cleanup;
3645
3646 status = psa_hash_update( &hmac.hash_ctx,
3647 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003648 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003649 if( status != PSA_SUCCESS )
3650 goto cleanup;
3651
3652 status = psa_hmac_finish_internal( &hmac,
3653 tls12_prf->output_block,
3654 hash_length );
3655 if( status != PSA_SUCCESS )
3656 goto cleanup;
3657
3658cleanup:
3659
3660 cleanup_status = psa_hmac_abort_internal( &hmac );
3661 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3662 status = cleanup_status;
3663
3664 return( status );
3665}
3666
3667/* Read some bytes from an TLS-1.2-PRF-based generator.
3668 * See Section 5 of RFC 5246. */
3669static psa_status_t psa_generator_tls12_prf_read(
3670 psa_tls12_prf_generator_t *tls12_prf,
3671 psa_algorithm_t alg,
3672 uint8_t *output,
3673 size_t output_length )
3674{
3675 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3676 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3677 psa_status_t status;
3678
3679 while( output_length != 0 )
3680 {
3681 /* Copy what remains of the current block */
3682 uint8_t n = hash_length - tls12_prf->offset_in_block;
3683
3684 /* Check if we have fully processed the current block. */
3685 if( n == 0 )
3686 {
3687 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3688 alg );
3689 if( status != PSA_SUCCESS )
3690 return( status );
3691
3692 continue;
3693 }
3694
3695 if( n > output_length )
3696 n = (uint8_t) output_length;
3697 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3698 n );
3699 output += n;
3700 output_length -= n;
3701 tls12_prf->offset_in_block += n;
3702 }
3703
3704 return( PSA_SUCCESS );
3705}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003706#endif /* MBEDTLS_MD_C */
3707
Gilles Peskineeab56e42018-07-12 17:12:33 +02003708psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3709 uint8_t *output,
3710 size_t output_length )
3711{
3712 psa_status_t status;
3713
3714 if( output_length > generator->capacity )
3715 {
3716 generator->capacity = 0;
3717 /* Go through the error path to wipe all confidential data now
3718 * that the generator object is useless. */
3719 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3720 goto exit;
3721 }
3722 if( output_length == 0 &&
3723 generator->capacity == 0 && generator->alg == 0 )
3724 {
3725 /* Edge case: this is a blank or finished generator, and 0
3726 * bytes were requested. The right error in this case could
3727 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3728 * INSUFFICIENT_CAPACITY, which is right for a finished
3729 * generator, for consistency with the case when
3730 * output_length > 0. */
3731 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3732 }
3733 generator->capacity -= output_length;
3734
Gilles Peskine751d9652018-09-18 12:05:44 +02003735 if( generator->alg == PSA_ALG_SELECT_RAW )
3736 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003737 /* Initially, the capacity of a selection generator is always
3738 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3739 * abbreviated in this comment as `size`. When the remaining
3740 * capacity is `c`, the next bytes to serve start `c` bytes
3741 * from the end of the buffer, i.e. `size - c` from the
3742 * beginning of the buffer. Since `generator->capacity` was just
3743 * decremented above, we need to serve the bytes from
3744 * `size - generator->capacity - output_length` to
3745 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003746 size_t offset =
3747 generator->ctx.buffer.size - generator->capacity - output_length;
3748 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3749 status = PSA_SUCCESS;
3750 }
3751 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003752#if defined(MBEDTLS_MD_C)
3753 if( PSA_ALG_IS_HKDF( generator->alg ) )
3754 {
3755 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3756 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3757 output, output_length );
3758 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003759 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3760 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003761 {
3762 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3763 generator->alg, output,
3764 output_length );
3765 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003766 else
3767#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003768 {
3769 return( PSA_ERROR_BAD_STATE );
3770 }
3771
3772exit:
3773 if( status != PSA_SUCCESS )
3774 {
3775 psa_generator_abort( generator );
3776 memset( output, '!', output_length );
3777 }
3778 return( status );
3779}
3780
Gilles Peskine08542d82018-07-19 17:05:42 +02003781#if defined(MBEDTLS_DES_C)
3782static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3783{
3784 if( data_size >= 8 )
3785 mbedtls_des_key_set_parity( data );
3786 if( data_size >= 16 )
3787 mbedtls_des_key_set_parity( data + 8 );
3788 if( data_size >= 24 )
3789 mbedtls_des_key_set_parity( data + 16 );
3790}
3791#endif /* MBEDTLS_DES_C */
3792
Gilles Peskinec5487a82018-12-03 18:08:14 +01003793psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003794 psa_key_type_t type,
3795 size_t bits,
3796 psa_crypto_generator_t *generator )
3797{
3798 uint8_t *data = NULL;
3799 size_t bytes = PSA_BITS_TO_BYTES( bits );
3800 psa_status_t status;
3801
3802 if( ! key_type_is_raw_bytes( type ) )
3803 return( PSA_ERROR_INVALID_ARGUMENT );
3804 if( bits % 8 != 0 )
3805 return( PSA_ERROR_INVALID_ARGUMENT );
3806 data = mbedtls_calloc( 1, bytes );
3807 if( data == NULL )
3808 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3809
3810 status = psa_generator_read( generator, data, bytes );
3811 if( status != PSA_SUCCESS )
3812 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003813#if defined(MBEDTLS_DES_C)
3814 if( type == PSA_KEY_TYPE_DES )
3815 psa_des_set_key_parity( data, bytes );
3816#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003817 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003818
3819exit:
3820 mbedtls_free( data );
3821 return( status );
3822}
3823
3824
3825
3826/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003827/* Key derivation */
3828/****************************************************************/
3829
Gilles Peskinea05219c2018-11-16 16:02:56 +01003830#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003831/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003832 * of the HKDF algorithm.
3833 *
3834 * Note that if this function fails, you must call psa_generator_abort()
3835 * to potentially free embedded data structures and wipe confidential data.
3836 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003837static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003838 const uint8_t *secret,
3839 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003840 psa_algorithm_t hash_alg,
3841 const uint8_t *salt,
3842 size_t salt_length,
3843 const uint8_t *label,
3844 size_t label_length )
3845{
3846 psa_status_t status;
3847 status = psa_hmac_setup_internal( &hkdf->hmac,
3848 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003849 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003850 if( status != PSA_SUCCESS )
3851 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003852 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003853 if( status != PSA_SUCCESS )
3854 return( status );
3855 status = psa_hmac_finish_internal( &hkdf->hmac,
3856 hkdf->prk,
3857 sizeof( hkdf->prk ) );
3858 if( status != PSA_SUCCESS )
3859 return( status );
3860 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3861 hkdf->block_number = 0;
3862 hkdf->info_length = label_length;
3863 if( label_length != 0 )
3864 {
3865 hkdf->info = mbedtls_calloc( 1, label_length );
3866 if( hkdf->info == NULL )
3867 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3868 memcpy( hkdf->info, label, label_length );
3869 }
3870 return( PSA_SUCCESS );
3871}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003872#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003873
Gilles Peskinea05219c2018-11-16 16:02:56 +01003874#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003875/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3876 *
3877 * Note that if this function fails, you must call psa_generator_abort()
3878 * to potentially free embedded data structures and wipe confidential data.
3879 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003880static psa_status_t psa_generator_tls12_prf_setup(
3881 psa_tls12_prf_generator_t *tls12_prf,
3882 const unsigned char *key,
3883 size_t key_len,
3884 psa_algorithm_t hash_alg,
3885 const uint8_t *salt,
3886 size_t salt_length,
3887 const uint8_t *label,
3888 size_t label_length )
3889{
3890 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003891 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3892 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003893
3894 tls12_prf->key = mbedtls_calloc( 1, key_len );
3895 if( tls12_prf->key == NULL )
3896 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3897 tls12_prf->key_len = key_len;
3898 memcpy( tls12_prf->key, key, key_len );
3899
Hanno Becker580fba12018-11-13 20:50:45 +00003900 overflow = ( salt_length + label_length < salt_length ) ||
3901 ( salt_length + label_length + hash_length < hash_length );
3902 if( overflow )
3903 return( PSA_ERROR_INVALID_ARGUMENT );
3904
3905 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3906 if( tls12_prf->Ai_with_seed == NULL )
3907 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3908 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3909
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003910 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3911 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003912 if( label_length != 0 )
3913 {
3914 memcpy( tls12_prf->Ai_with_seed + hash_length,
3915 label, label_length );
3916 }
3917
3918 if( salt_length != 0 )
3919 {
3920 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3921 salt, salt_length );
3922 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003923
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003924 /* The first block gets generated when
3925 * psa_generator_read() is called. */
3926 tls12_prf->block_number = 0;
3927 tls12_prf->offset_in_block = hash_length;
3928
3929 return( PSA_SUCCESS );
3930}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003931
3932/* Set up a TLS-1.2-PSK-to-MS-based generator. */
3933static psa_status_t psa_generator_tls12_psk_to_ms_setup(
3934 psa_tls12_prf_generator_t *tls12_prf,
3935 const unsigned char *psk,
3936 size_t psk_len,
3937 psa_algorithm_t hash_alg,
3938 const uint8_t *salt,
3939 size_t salt_length,
3940 const uint8_t *label,
3941 size_t label_length )
3942{
3943 psa_status_t status;
3944 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
3945
3946 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
3947 return( PSA_ERROR_INVALID_ARGUMENT );
3948
3949 /* Quoting RFC 4279, Section 2:
3950 *
3951 * The premaster secret is formed as follows: if the PSK is N octets
3952 * long, concatenate a uint16 with the value N, N zero octets, a second
3953 * uint16 with the value N, and the PSK itself.
3954 */
3955
3956 pms[0] = ( psk_len >> 8 ) & 0xff;
3957 pms[1] = ( psk_len >> 0 ) & 0xff;
3958 memset( pms + 2, 0, psk_len );
3959 pms[2 + psk_len + 0] = pms[0];
3960 pms[2 + psk_len + 1] = pms[1];
3961 memcpy( pms + 4 + psk_len, psk, psk_len );
3962
3963 status = psa_generator_tls12_prf_setup( tls12_prf,
3964 pms, 4 + 2 * psk_len,
3965 hash_alg,
3966 salt, salt_length,
3967 label, label_length );
3968
Gilles Peskine3f108122018-12-07 18:14:53 +01003969 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00003970 return( status );
3971}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003972#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003973
Gilles Peskine346797d2018-11-16 16:05:06 +01003974/* Note that if this function fails, you must call psa_generator_abort()
3975 * to potentially free embedded data structures and wipe confidential data.
3976 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003977static psa_status_t psa_key_derivation_internal(
3978 psa_crypto_generator_t *generator,
3979 const uint8_t *secret, size_t secret_length,
3980 psa_algorithm_t alg,
3981 const uint8_t *salt, size_t salt_length,
3982 const uint8_t *label, size_t label_length,
3983 size_t capacity )
3984{
3985 psa_status_t status;
3986 size_t max_capacity;
3987
3988 /* Set generator->alg even on failure so that abort knows what to do. */
3989 generator->alg = alg;
3990
Gilles Peskine751d9652018-09-18 12:05:44 +02003991 if( alg == PSA_ALG_SELECT_RAW )
3992 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003993 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02003994 if( salt_length != 0 )
3995 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01003996 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02003997 if( label_length != 0 )
3998 return( PSA_ERROR_INVALID_ARGUMENT );
3999 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4000 if( generator->ctx.buffer.data == NULL )
4001 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4002 memcpy( generator->ctx.buffer.data, secret, secret_length );
4003 generator->ctx.buffer.size = secret_length;
4004 max_capacity = secret_length;
4005 status = PSA_SUCCESS;
4006 }
4007 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004008#if defined(MBEDTLS_MD_C)
4009 if( PSA_ALG_IS_HKDF( alg ) )
4010 {
4011 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4012 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4013 if( hash_size == 0 )
4014 return( PSA_ERROR_NOT_SUPPORTED );
4015 max_capacity = 255 * hash_size;
4016 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4017 secret, secret_length,
4018 hash_alg,
4019 salt, salt_length,
4020 label, label_length );
4021 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004022 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4023 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4024 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004025 {
4026 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4027 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4028
4029 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4030 if( hash_alg != PSA_ALG_SHA_256 &&
4031 hash_alg != PSA_ALG_SHA_384 )
4032 {
4033 return( PSA_ERROR_NOT_SUPPORTED );
4034 }
4035
4036 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004037
4038 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4039 {
4040 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4041 secret, secret_length,
4042 hash_alg, salt, salt_length,
4043 label, label_length );
4044 }
4045 else
4046 {
4047 status = psa_generator_tls12_psk_to_ms_setup(
4048 &generator->ctx.tls12_prf,
4049 secret, secret_length,
4050 hash_alg, salt, salt_length,
4051 label, label_length );
4052 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004053 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004054 else
4055#endif
4056 {
4057 return( PSA_ERROR_NOT_SUPPORTED );
4058 }
4059
4060 if( status != PSA_SUCCESS )
4061 return( status );
4062
4063 if( capacity <= max_capacity )
4064 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004065 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4066 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004067 else
4068 return( PSA_ERROR_INVALID_ARGUMENT );
4069
4070 return( PSA_SUCCESS );
4071}
4072
Gilles Peskineea0fb492018-07-12 17:17:20 +02004073psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004074 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004075 psa_algorithm_t alg,
4076 const uint8_t *salt,
4077 size_t salt_length,
4078 const uint8_t *label,
4079 size_t label_length,
4080 size_t capacity )
4081{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004082 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004083 psa_status_t status;
4084
4085 if( generator->alg != 0 )
4086 return( PSA_ERROR_BAD_STATE );
4087
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004088 /* Make sure that alg is a key derivation algorithm. This prevents
4089 * key selection algorithms, which psa_key_derivation_internal
4090 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004091 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4092 return( PSA_ERROR_INVALID_ARGUMENT );
4093
Gilles Peskinec5487a82018-12-03 18:08:14 +01004094 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004095 if( status != PSA_SUCCESS )
4096 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004097
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004098 if( slot->type != PSA_KEY_TYPE_DERIVE )
4099 return( PSA_ERROR_INVALID_ARGUMENT );
4100
4101 status = psa_key_derivation_internal( generator,
4102 slot->data.raw.data,
4103 slot->data.raw.bytes,
4104 alg,
4105 salt, salt_length,
4106 label, label_length,
4107 capacity );
4108 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004109 psa_generator_abort( generator );
4110 return( status );
4111}
4112
4113
4114
4115/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004116/* Key agreement */
4117/****************************************************************/
4118
Gilles Peskinea05219c2018-11-16 16:02:56 +01004119#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004120static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4121 size_t peer_key_length,
4122 const mbedtls_ecp_keypair *our_key,
4123 uint8_t *shared_secret,
4124 size_t shared_secret_size,
4125 size_t *shared_secret_length )
4126{
4127 mbedtls_pk_context pk;
4128 mbedtls_ecp_keypair *their_key = NULL;
4129 mbedtls_ecdh_context ecdh;
4130 int ret;
4131 mbedtls_ecdh_init( &ecdh );
4132 mbedtls_pk_init( &pk );
4133
4134 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
4135 if( ret != 0 )
4136 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02004137 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004138 {
Gilles Peskine88714d72018-10-25 23:07:25 +02004139 case MBEDTLS_PK_ECKEY:
4140 case MBEDTLS_PK_ECKEY_DH:
4141 break;
4142 default:
4143 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4144 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004145 }
4146 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01004147 if( their_key->grp.id != our_key->grp.id )
4148 {
4149 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4150 goto exit;
4151 }
4152
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004153 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
4154 if( ret != 0 )
4155 goto exit;
4156 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
4157 if( ret != 0 )
4158 goto exit;
4159
4160 ret = mbedtls_ecdh_calc_secret( &ecdh,
4161 shared_secret_length,
4162 shared_secret, shared_secret_size,
4163 mbedtls_ctr_drbg_random,
4164 &global_data.ctr_drbg );
4165
4166exit:
4167 mbedtls_pk_free( &pk );
4168 mbedtls_ecdh_free( &ecdh );
4169 return( mbedtls_to_psa_error( ret ) );
4170}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004171#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004172
Gilles Peskine01d718c2018-09-18 12:01:02 +02004173#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4174
Gilles Peskine346797d2018-11-16 16:05:06 +01004175/* Note that if this function fails, you must call psa_generator_abort()
4176 * to potentially free embedded data structures and wipe confidential data.
4177 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004178static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004179 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004180 const uint8_t *peer_key,
4181 size_t peer_key_length,
4182 psa_algorithm_t alg )
4183{
4184 psa_status_t status;
4185 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4186 size_t shared_secret_length = 0;
4187
4188 /* Step 1: run the secret agreement algorithm to generate the shared
4189 * secret. */
4190 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4191 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004192#if defined(MBEDTLS_ECDH_C)
4193 case PSA_ALG_ECDH_BASE:
4194 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4195 return( PSA_ERROR_INVALID_ARGUMENT );
4196 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4197 private_key->data.ecp,
4198 shared_secret,
4199 sizeof( shared_secret ),
4200 &shared_secret_length );
4201 break;
4202#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004203 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004204 (void) private_key;
4205 (void) peer_key;
4206 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004207 return( PSA_ERROR_NOT_SUPPORTED );
4208 }
4209 if( status != PSA_SUCCESS )
4210 goto exit;
4211
4212 /* Step 2: set up the key derivation to generate key material from
4213 * the shared secret. */
4214 status = psa_key_derivation_internal( generator,
4215 shared_secret, shared_secret_length,
4216 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4217 NULL, 0, NULL, 0,
4218 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4219exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004220 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004221 return( status );
4222}
4223
4224psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004225 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004226 const uint8_t *peer_key,
4227 size_t peer_key_length,
4228 psa_algorithm_t alg )
4229{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004230 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004231 psa_status_t status;
4232 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4233 return( PSA_ERROR_INVALID_ARGUMENT );
4234 status = psa_get_key_from_slot( private_key, &slot,
4235 PSA_KEY_USAGE_DERIVE, alg );
4236 if( status != PSA_SUCCESS )
4237 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004238 status = psa_key_agreement_internal( generator,
4239 slot,
4240 peer_key, peer_key_length,
4241 alg );
4242 if( status != PSA_SUCCESS )
4243 psa_generator_abort( generator );
4244 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004245}
4246
4247
4248
4249/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004250/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004251/****************************************************************/
4252
4253psa_status_t psa_generate_random( uint8_t *output,
4254 size_t output_size )
4255{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004256 int ret;
4257 GUARD_MODULE_INITIALIZED;
4258
4259 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004260 return( mbedtls_to_psa_error( ret ) );
4261}
4262
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004263#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004264
4265/* Support function for error conversion between psa_its error codes to psa crypto */
4266static psa_status_t its_to_psa_error( psa_its_status_t ret )
4267{
4268 switch( ret )
4269 {
4270 case PSA_ITS_SUCCESS:
4271 return( PSA_SUCCESS );
4272
4273 case PSA_ITS_ERROR_KEY_NOT_FOUND:
4274 return( PSA_ERROR_EMPTY_SLOT );
4275
4276 case PSA_ITS_ERROR_STORAGE_FAILURE:
4277 return( PSA_ERROR_STORAGE_FAILURE );
4278
4279 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4280 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4281
4282 case PSA_ITS_ERROR_INVALID_KEY:
4283 case PSA_PS_ERROR_OFFSET_INVALID:
4284 case PSA_ITS_ERROR_INCORRECT_SIZE:
4285 case PSA_ITS_ERROR_BAD_POINTER:
4286 return( PSA_ERROR_INVALID_ARGUMENT );
4287
4288 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4289 return( PSA_ERROR_NOT_SUPPORTED );
4290
4291 case PSA_ITS_ERROR_WRITE_ONCE:
4292 return( PSA_ERROR_OCCUPIED_SLOT );
4293
4294 default:
4295 return( PSA_ERROR_UNKNOWN_ERROR );
4296 }
4297}
4298
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004299psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4300 size_t seed_size )
4301{
4302 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004303 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004304 struct psa_its_info_t p_info;
4305 if( global_data.initialized )
4306 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004307
4308 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4309 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4310 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4311 return( PSA_ERROR_INVALID_ARGUMENT );
4312
avolinski0d2c2662018-11-21 17:31:07 +02004313 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004314 status = its_to_psa_error( its_status );
4315
4316 if( PSA_ITS_ERROR_KEY_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004317 {
avolinski0d2c2662018-11-21 17:31:07 +02004318 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004319 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004320 }
avolinski13beb102018-11-20 16:51:49 +02004321 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004322 {
4323 /* You should not be here. Seed needs to be injected only once */
4324 status = PSA_ERROR_NOT_PERMITTED;
4325 }
4326 return( status );
4327}
4328#endif
4329
Gilles Peskinec5487a82018-12-03 18:08:14 +01004330psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004331 psa_key_type_t type,
4332 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004333 const void *extra,
4334 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004335{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004336 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004337 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004338
Gilles Peskine53d991e2018-07-12 01:14:59 +02004339 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004340 return( PSA_ERROR_INVALID_ARGUMENT );
4341
Gilles Peskinec5487a82018-12-03 18:08:14 +01004342 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004343 if( status != PSA_SUCCESS )
4344 return( status );
4345
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004346 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004347 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004348 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004349 if( status != PSA_SUCCESS )
4350 return( status );
4351 status = psa_generate_random( slot->data.raw.data,
4352 slot->data.raw.bytes );
4353 if( status != PSA_SUCCESS )
4354 {
4355 mbedtls_free( slot->data.raw.data );
4356 return( status );
4357 }
4358#if defined(MBEDTLS_DES_C)
4359 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004360 psa_des_set_key_parity( slot->data.raw.data,
4361 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004362#endif /* MBEDTLS_DES_C */
4363 }
4364 else
4365
4366#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4367 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4368 {
4369 mbedtls_rsa_context *rsa;
4370 int ret;
4371 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004372 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4373 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004374 /* Accept only byte-aligned keys, for the same reasons as
4375 * in psa_import_rsa_key(). */
4376 if( bits % 8 != 0 )
4377 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004378 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004379 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004380 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004381 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004382 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004383#if INT_MAX < 0xffffffff
4384 /* Check that the uint32_t value passed by the caller fits
4385 * in the range supported by this implementation. */
4386 if( p->e > INT_MAX )
4387 return( PSA_ERROR_NOT_SUPPORTED );
4388#endif
4389 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004390 }
4391 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4392 if( rsa == NULL )
4393 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4394 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4395 ret = mbedtls_rsa_gen_key( rsa,
4396 mbedtls_ctr_drbg_random,
4397 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004398 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004399 exponent );
4400 if( ret != 0 )
4401 {
4402 mbedtls_rsa_free( rsa );
4403 mbedtls_free( rsa );
4404 return( mbedtls_to_psa_error( ret ) );
4405 }
4406 slot->data.rsa = rsa;
4407 }
4408 else
4409#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4410
4411#if defined(MBEDTLS_ECP_C)
4412 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4413 {
4414 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4415 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4416 const mbedtls_ecp_curve_info *curve_info =
4417 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4418 mbedtls_ecp_keypair *ecp;
4419 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004420 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004421 return( PSA_ERROR_NOT_SUPPORTED );
4422 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4423 return( PSA_ERROR_NOT_SUPPORTED );
4424 if( curve_info->bit_size != bits )
4425 return( PSA_ERROR_INVALID_ARGUMENT );
4426 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4427 if( ecp == NULL )
4428 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4429 mbedtls_ecp_keypair_init( ecp );
4430 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4431 mbedtls_ctr_drbg_random,
4432 &global_data.ctr_drbg );
4433 if( ret != 0 )
4434 {
4435 mbedtls_ecp_keypair_free( ecp );
4436 mbedtls_free( ecp );
4437 return( mbedtls_to_psa_error( ret ) );
4438 }
4439 slot->data.ecp = ecp;
4440 }
4441 else
4442#endif /* MBEDTLS_ECP_C */
4443
4444 return( PSA_ERROR_NOT_SUPPORTED );
4445
4446 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004447
4448#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4449 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4450 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004451 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004452 }
4453#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4454
4455 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004456}
4457
4458
4459/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004460/* Module setup */
4461/****************************************************************/
4462
Gilles Peskine5e769522018-11-20 21:59:56 +01004463psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4464 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4465 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4466{
4467 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4468 return( PSA_ERROR_BAD_STATE );
4469 global_data.entropy_init = entropy_init;
4470 global_data.entropy_free = entropy_free;
4471 return( PSA_SUCCESS );
4472}
4473
Gilles Peskinee59236f2018-01-27 23:32:46 +01004474void mbedtls_psa_crypto_free( void )
4475{
Gilles Peskinec6b69072018-11-20 21:42:52 +01004476 if( global_data.key_slots_initialized )
Darryl Green40225ba2018-11-15 14:48:15 +00004477 {
Gilles Peskinec5487a82018-12-03 18:08:14 +01004478 psa_key_handle_t key;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004479 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
4480 {
Gilles Peskine2f060a82018-12-04 17:12:32 +01004481 psa_key_slot_t *slot = &global_data.key_slots[key - 1];
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01004482 (void) psa_wipe_key_slot( slot );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004483 }
Darryl Green40225ba2018-11-15 14:48:15 +00004484 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01004485 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4486 {
4487 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004488 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004489 }
4490 /* Wipe all remaining data, including configuration.
4491 * In particular, this sets all state indicator to the value
4492 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004493 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004494}
4495
4496psa_status_t psa_crypto_init( void )
4497{
4498 int ret;
4499 const unsigned char drbg_seed[] = "PSA";
4500
Gilles Peskinec6b69072018-11-20 21:42:52 +01004501 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004502 if( global_data.initialized != 0 )
4503 return( PSA_SUCCESS );
4504
Gilles Peskine5e769522018-11-20 21:59:56 +01004505 /* Set default configuration if
4506 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4507 if( global_data.entropy_init == NULL )
4508 global_data.entropy_init = mbedtls_entropy_init;
4509 if( global_data.entropy_free == NULL )
4510 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004511
Gilles Peskinec6b69072018-11-20 21:42:52 +01004512 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004513 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004514 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004515 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004516 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4517 mbedtls_entropy_func,
4518 &global_data.entropy,
4519 drbg_seed, sizeof( drbg_seed ) - 1 );
4520 if( ret != 0 )
4521 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004522 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004523
Gilles Peskinec6b69072018-11-20 21:42:52 +01004524 /* Initialize the key slots. Zero-initialization has made all key
Gilles Peskinefe9756b2018-12-07 18:12:28 +01004525 * slots empty, so there is nothing to do. */
Gilles Peskinec6b69072018-11-20 21:42:52 +01004526 global_data.key_slots_initialized = 1;
4527
4528 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004529 global_data.initialized = 1;
4530
Gilles Peskinee59236f2018-01-27 23:32:46 +01004531exit:
4532 if( ret != 0 )
4533 mbedtls_psa_crypto_free( );
4534 return( mbedtls_to_psa_error( ret ) );
4535}
4536
4537#endif /* MBEDTLS_PSA_CRYPTO_C */