blob: 2712c6778dca6784d42bbaae523e2fd18326314e [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. */
874 if( id >= 0xffff0000 )
875 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 Peskine961849f2018-11-30 18:54:54 +0100902 if( ! slot->allocated )
903 return( PSA_ERROR_INVALID_HANDLE );
Gilles Peskinec5487a82018-12-03 18:08:14 +0100904
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100905 return( psa_wipe_key_slot( slot ) );
Gilles Peskine961849f2018-11-30 18:54:54 +0100906}
907
Gilles Peskinec5487a82018-12-03 18:08:14 +0100908psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100909 psa_key_type_t type,
910 const uint8_t *data,
911 size_t data_length )
912{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100913 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100914 psa_status_t status;
915
Gilles Peskinec5487a82018-12-03 18:08:14 +0100916 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100917 if( status != PSA_SUCCESS )
918 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100919
920 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100921
922 status = psa_import_key_into_slot( slot, data, data_length );
923 if( status != PSA_SUCCESS )
924 {
925 slot->type = PSA_KEY_TYPE_NONE;
926 return( status );
927 }
928
Darryl Greend49a4992018-06-18 17:27:26 +0100929#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
930 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
931 {
932 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100933 status = psa_save_persistent_key( slot->persistent_storage_id,
934 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100935 data_length );
936 if( status != PSA_SUCCESS )
937 {
938 (void) psa_remove_key_data_from_memory( slot );
939 slot->type = PSA_KEY_TYPE_NONE;
940 }
941 }
942#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
943
944 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100945}
946
Gilles Peskinec5487a82018-12-03 18:08:14 +0100947psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100948{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100949 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100950 psa_status_t status = PSA_SUCCESS;
951 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100952
Gilles Peskinec5487a82018-12-03 18:08:14 +0100953 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200954 if( status != PSA_SUCCESS )
955 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100956#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
957 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
958 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100959 storage_status =
960 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100961 }
962#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100963 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100964 if( status != PSA_SUCCESS )
965 return( status );
966 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100967}
968
Gilles Peskineb870b182018-07-06 16:02:09 +0200969/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100970static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200971{
972 if( key_type_is_raw_bytes( slot->type ) )
973 return( slot->data.raw.bytes * 8 );
974#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200975 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100976 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200977#endif /* defined(MBEDTLS_RSA_C) */
978#if defined(MBEDTLS_ECP_C)
979 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
980 return( slot->data.ecp->grp.pbits );
981#endif /* defined(MBEDTLS_ECP_C) */
982 /* Shouldn't happen except on an empty slot. */
983 return( 0 );
984}
985
Gilles Peskinec5487a82018-12-03 18:08:14 +0100986psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200987 psa_key_type_t *type,
988 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100989{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100990 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200991 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100992
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100993 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200994 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995 if( bits != NULL )
996 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100997 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200998 if( status != PSA_SUCCESS )
999 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001000
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001001 if( slot->type == PSA_KEY_TYPE_NONE )
1002 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001003 if( type != NULL )
1004 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001005 if( bits != NULL )
1006 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001007 return( PSA_SUCCESS );
1008}
1009
Gilles Peskine2f060a82018-12-04 17:12:32 +01001010static psa_status_t psa_internal_export_key( psa_key_slot_t *slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001011 uint8_t *data,
1012 size_t data_size,
1013 size_t *data_length,
1014 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001015{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001016 *data_length = 0;
1017
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001018 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001019 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001020
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001021 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001022 {
1023 if( slot->data.raw.bytes > data_size )
1024 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001025 if( data_size != 0 )
1026 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001027 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001028 memset( data + slot->data.raw.bytes, 0,
1029 data_size - slot->data.raw.bytes );
1030 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001031 *data_length = slot->data.raw.bytes;
1032 return( PSA_SUCCESS );
1033 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001034#if defined(MBEDTLS_ECP_C)
1035 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1036 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001037 psa_status_t status;
1038
Gilles Peskine188c71e2018-10-29 19:26:02 +01001039 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1040 if( bytes > data_size )
1041 return( PSA_ERROR_BUFFER_TOO_SMALL );
1042 status = mbedtls_to_psa_error(
1043 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1044 if( status != PSA_SUCCESS )
1045 return( status );
1046 memset( data + bytes, 0, data_size - bytes );
1047 *data_length = bytes;
1048 return( PSA_SUCCESS );
1049 }
1050#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001051 else
Moran Peker17e36e12018-05-02 12:55:20 +03001052 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001053#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001054 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001055 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001056 {
Moran Pekera998bc62018-04-16 18:16:20 +03001057 mbedtls_pk_context pk;
1058 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001059 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001060 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001061#if defined(MBEDTLS_RSA_C)
1062 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001063 pk.pk_info = &mbedtls_rsa_info;
1064 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001065#else
1066 return( PSA_ERROR_NOT_SUPPORTED );
1067#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001068 }
1069 else
1070 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001071#if defined(MBEDTLS_ECP_C)
1072 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001073 pk.pk_info = &mbedtls_eckey_info;
1074 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001075#else
1076 return( PSA_ERROR_NOT_SUPPORTED );
1077#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001078 }
Moran Pekerd7326592018-05-29 16:56:39 +03001079 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001080 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +03001081 else
1082 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +03001083 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001084 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001085 /* If data_size is 0 then data may be NULL and then the
1086 * call to memset would have undefined behavior. */
1087 if( data_size != 0 )
1088 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001089 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001090 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001091 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1092 * Move the data to the beginning and erase remaining data
1093 * at the original location. */
1094 if( 2 * (size_t) ret <= data_size )
1095 {
1096 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001097 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001098 }
1099 else if( (size_t) ret < data_size )
1100 {
1101 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001102 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001103 }
Moran Pekera998bc62018-04-16 18:16:20 +03001104 *data_length = ret;
1105 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001106 }
1107 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001108#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001109 {
1110 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001111 it is valid for a special-purpose implementation to omit
1112 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001113 return( PSA_ERROR_NOT_SUPPORTED );
1114 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001115 }
1116}
1117
Gilles Peskinec5487a82018-12-03 18:08:14 +01001118psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001119 uint8_t *data,
1120 size_t data_size,
1121 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001122{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001123 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001124 psa_status_t status;
1125
1126 /* Set the key to empty now, so that even when there are errors, we always
1127 * set data_length to a value between 0 and data_size. On error, setting
1128 * the key to empty is a good choice because an empty key representation is
1129 * unlikely to be accepted anywhere. */
1130 *data_length = 0;
1131
1132 /* Export requires the EXPORT flag. There is an exception for public keys,
1133 * which don't require any flag, but psa_get_key_from_slot takes
1134 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001135 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001136 if( status != PSA_SUCCESS )
1137 return( status );
1138 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001139 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001140}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001141
Gilles Peskinec5487a82018-12-03 18:08:14 +01001142psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001143 uint8_t *data,
1144 size_t data_size,
1145 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001146{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001147 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001148 psa_status_t status;
1149
1150 /* Set the key to empty now, so that even when there are errors, we always
1151 * set data_length to a value between 0 and data_size. On error, setting
1152 * the key to empty is a good choice because an empty key representation is
1153 * unlikely to be accepted anywhere. */
1154 *data_length = 0;
1155
1156 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001157 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001158 if( status != PSA_SUCCESS )
1159 return( status );
1160 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001161 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001162}
1163
Darryl Green0c6575a2018-11-07 16:05:30 +00001164#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001165static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001166 size_t bits )
1167{
1168 psa_status_t status;
1169 uint8_t *data;
1170 size_t key_length;
1171 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1172 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001173 if( data == NULL )
1174 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001175 /* Get key data in export format */
1176 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1177 if( status != PSA_SUCCESS )
1178 {
1179 slot->type = PSA_KEY_TYPE_NONE;
1180 goto exit;
1181 }
1182 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001183 status = psa_save_persistent_key( slot->persistent_storage_id,
1184 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001185 data, key_length );
1186 if( status != PSA_SUCCESS )
1187 {
1188 slot->type = PSA_KEY_TYPE_NONE;
1189 }
1190exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001191 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001192 mbedtls_free( data );
1193 return( status );
1194}
1195#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1196
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001197
1198
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001199/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001200/* Message digests */
1201/****************************************************************/
1202
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001203static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001204{
1205 switch( alg )
1206 {
1207#if defined(MBEDTLS_MD2_C)
1208 case PSA_ALG_MD2:
1209 return( &mbedtls_md2_info );
1210#endif
1211#if defined(MBEDTLS_MD4_C)
1212 case PSA_ALG_MD4:
1213 return( &mbedtls_md4_info );
1214#endif
1215#if defined(MBEDTLS_MD5_C)
1216 case PSA_ALG_MD5:
1217 return( &mbedtls_md5_info );
1218#endif
1219#if defined(MBEDTLS_RIPEMD160_C)
1220 case PSA_ALG_RIPEMD160:
1221 return( &mbedtls_ripemd160_info );
1222#endif
1223#if defined(MBEDTLS_SHA1_C)
1224 case PSA_ALG_SHA_1:
1225 return( &mbedtls_sha1_info );
1226#endif
1227#if defined(MBEDTLS_SHA256_C)
1228 case PSA_ALG_SHA_224:
1229 return( &mbedtls_sha224_info );
1230 case PSA_ALG_SHA_256:
1231 return( &mbedtls_sha256_info );
1232#endif
1233#if defined(MBEDTLS_SHA512_C)
1234 case PSA_ALG_SHA_384:
1235 return( &mbedtls_sha384_info );
1236 case PSA_ALG_SHA_512:
1237 return( &mbedtls_sha512_info );
1238#endif
1239 default:
1240 return( NULL );
1241 }
1242}
1243
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001244psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1245{
1246 switch( operation->alg )
1247 {
Gilles Peskine81736312018-06-26 15:04:31 +02001248 case 0:
1249 /* The object has (apparently) been initialized but it is not
1250 * in use. It's ok to call abort on such an object, and there's
1251 * nothing to do. */
1252 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001253#if defined(MBEDTLS_MD2_C)
1254 case PSA_ALG_MD2:
1255 mbedtls_md2_free( &operation->ctx.md2 );
1256 break;
1257#endif
1258#if defined(MBEDTLS_MD4_C)
1259 case PSA_ALG_MD4:
1260 mbedtls_md4_free( &operation->ctx.md4 );
1261 break;
1262#endif
1263#if defined(MBEDTLS_MD5_C)
1264 case PSA_ALG_MD5:
1265 mbedtls_md5_free( &operation->ctx.md5 );
1266 break;
1267#endif
1268#if defined(MBEDTLS_RIPEMD160_C)
1269 case PSA_ALG_RIPEMD160:
1270 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1271 break;
1272#endif
1273#if defined(MBEDTLS_SHA1_C)
1274 case PSA_ALG_SHA_1:
1275 mbedtls_sha1_free( &operation->ctx.sha1 );
1276 break;
1277#endif
1278#if defined(MBEDTLS_SHA256_C)
1279 case PSA_ALG_SHA_224:
1280 case PSA_ALG_SHA_256:
1281 mbedtls_sha256_free( &operation->ctx.sha256 );
1282 break;
1283#endif
1284#if defined(MBEDTLS_SHA512_C)
1285 case PSA_ALG_SHA_384:
1286 case PSA_ALG_SHA_512:
1287 mbedtls_sha512_free( &operation->ctx.sha512 );
1288 break;
1289#endif
1290 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001291 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001292 }
1293 operation->alg = 0;
1294 return( PSA_SUCCESS );
1295}
1296
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001297psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001298 psa_algorithm_t alg )
1299{
1300 int ret;
1301 operation->alg = 0;
1302 switch( alg )
1303 {
1304#if defined(MBEDTLS_MD2_C)
1305 case PSA_ALG_MD2:
1306 mbedtls_md2_init( &operation->ctx.md2 );
1307 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1308 break;
1309#endif
1310#if defined(MBEDTLS_MD4_C)
1311 case PSA_ALG_MD4:
1312 mbedtls_md4_init( &operation->ctx.md4 );
1313 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1314 break;
1315#endif
1316#if defined(MBEDTLS_MD5_C)
1317 case PSA_ALG_MD5:
1318 mbedtls_md5_init( &operation->ctx.md5 );
1319 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1320 break;
1321#endif
1322#if defined(MBEDTLS_RIPEMD160_C)
1323 case PSA_ALG_RIPEMD160:
1324 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1325 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1326 break;
1327#endif
1328#if defined(MBEDTLS_SHA1_C)
1329 case PSA_ALG_SHA_1:
1330 mbedtls_sha1_init( &operation->ctx.sha1 );
1331 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1332 break;
1333#endif
1334#if defined(MBEDTLS_SHA256_C)
1335 case PSA_ALG_SHA_224:
1336 mbedtls_sha256_init( &operation->ctx.sha256 );
1337 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1338 break;
1339 case PSA_ALG_SHA_256:
1340 mbedtls_sha256_init( &operation->ctx.sha256 );
1341 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1342 break;
1343#endif
1344#if defined(MBEDTLS_SHA512_C)
1345 case PSA_ALG_SHA_384:
1346 mbedtls_sha512_init( &operation->ctx.sha512 );
1347 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1348 break;
1349 case PSA_ALG_SHA_512:
1350 mbedtls_sha512_init( &operation->ctx.sha512 );
1351 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1352 break;
1353#endif
1354 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001355 return( PSA_ALG_IS_HASH( alg ) ?
1356 PSA_ERROR_NOT_SUPPORTED :
1357 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001358 }
1359 if( ret == 0 )
1360 operation->alg = alg;
1361 else
1362 psa_hash_abort( operation );
1363 return( mbedtls_to_psa_error( ret ) );
1364}
1365
1366psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1367 const uint8_t *input,
1368 size_t input_length )
1369{
1370 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001371
1372 /* Don't require hash implementations to behave correctly on a
1373 * zero-length input, which may have an invalid pointer. */
1374 if( input_length == 0 )
1375 return( PSA_SUCCESS );
1376
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001377 switch( operation->alg )
1378 {
1379#if defined(MBEDTLS_MD2_C)
1380 case PSA_ALG_MD2:
1381 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1382 input, input_length );
1383 break;
1384#endif
1385#if defined(MBEDTLS_MD4_C)
1386 case PSA_ALG_MD4:
1387 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1388 input, input_length );
1389 break;
1390#endif
1391#if defined(MBEDTLS_MD5_C)
1392 case PSA_ALG_MD5:
1393 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1394 input, input_length );
1395 break;
1396#endif
1397#if defined(MBEDTLS_RIPEMD160_C)
1398 case PSA_ALG_RIPEMD160:
1399 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1400 input, input_length );
1401 break;
1402#endif
1403#if defined(MBEDTLS_SHA1_C)
1404 case PSA_ALG_SHA_1:
1405 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1406 input, input_length );
1407 break;
1408#endif
1409#if defined(MBEDTLS_SHA256_C)
1410 case PSA_ALG_SHA_224:
1411 case PSA_ALG_SHA_256:
1412 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1413 input, input_length );
1414 break;
1415#endif
1416#if defined(MBEDTLS_SHA512_C)
1417 case PSA_ALG_SHA_384:
1418 case PSA_ALG_SHA_512:
1419 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1420 input, input_length );
1421 break;
1422#endif
1423 default:
1424 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1425 break;
1426 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001427
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001428 if( ret != 0 )
1429 psa_hash_abort( operation );
1430 return( mbedtls_to_psa_error( ret ) );
1431}
1432
1433psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1434 uint8_t *hash,
1435 size_t hash_size,
1436 size_t *hash_length )
1437{
itayzafrir40835d42018-08-02 13:14:17 +03001438 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001439 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001440 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001441
1442 /* Fill the output buffer with something that isn't a valid hash
1443 * (barring an attack on the hash and deliberately-crafted input),
1444 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001445 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001446 /* If hash_size is 0 then hash may be NULL and then the
1447 * call to memset would have undefined behavior. */
1448 if( hash_size != 0 )
1449 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001450
1451 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001452 {
1453 status = PSA_ERROR_BUFFER_TOO_SMALL;
1454 goto exit;
1455 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001456
1457 switch( operation->alg )
1458 {
1459#if defined(MBEDTLS_MD2_C)
1460 case PSA_ALG_MD2:
1461 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1462 break;
1463#endif
1464#if defined(MBEDTLS_MD4_C)
1465 case PSA_ALG_MD4:
1466 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1467 break;
1468#endif
1469#if defined(MBEDTLS_MD5_C)
1470 case PSA_ALG_MD5:
1471 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1472 break;
1473#endif
1474#if defined(MBEDTLS_RIPEMD160_C)
1475 case PSA_ALG_RIPEMD160:
1476 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1477 break;
1478#endif
1479#if defined(MBEDTLS_SHA1_C)
1480 case PSA_ALG_SHA_1:
1481 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1482 break;
1483#endif
1484#if defined(MBEDTLS_SHA256_C)
1485 case PSA_ALG_SHA_224:
1486 case PSA_ALG_SHA_256:
1487 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1488 break;
1489#endif
1490#if defined(MBEDTLS_SHA512_C)
1491 case PSA_ALG_SHA_384:
1492 case PSA_ALG_SHA_512:
1493 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1494 break;
1495#endif
1496 default:
1497 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1498 break;
1499 }
itayzafrir40835d42018-08-02 13:14:17 +03001500 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001501
itayzafrir40835d42018-08-02 13:14:17 +03001502exit:
1503 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001504 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001505 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001506 return( psa_hash_abort( operation ) );
1507 }
1508 else
1509 {
1510 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001511 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001512 }
1513}
1514
Gilles Peskine2d277862018-06-18 15:41:12 +02001515psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1516 const uint8_t *hash,
1517 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001518{
1519 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1520 size_t actual_hash_length;
1521 psa_status_t status = psa_hash_finish( operation,
1522 actual_hash, sizeof( actual_hash ),
1523 &actual_hash_length );
1524 if( status != PSA_SUCCESS )
1525 return( status );
1526 if( actual_hash_length != hash_length )
1527 return( PSA_ERROR_INVALID_SIGNATURE );
1528 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1529 return( PSA_ERROR_INVALID_SIGNATURE );
1530 return( PSA_SUCCESS );
1531}
1532
1533
1534
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001535/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001536/* MAC */
1537/****************************************************************/
1538
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001539static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001540 psa_algorithm_t alg,
1541 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001542 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001543 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001544{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001545 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001546 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001547
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001548 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001549 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001550
Gilles Peskine8c9def32018-02-08 10:02:12 +01001551 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1552 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001553 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001554 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001555 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001556 mode = MBEDTLS_MODE_STREAM;
1557 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001558 case PSA_ALG_CTR:
1559 mode = MBEDTLS_MODE_CTR;
1560 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001561 case PSA_ALG_CFB:
1562 mode = MBEDTLS_MODE_CFB;
1563 break;
1564 case PSA_ALG_OFB:
1565 mode = MBEDTLS_MODE_OFB;
1566 break;
1567 case PSA_ALG_CBC_NO_PADDING:
1568 mode = MBEDTLS_MODE_CBC;
1569 break;
1570 case PSA_ALG_CBC_PKCS7:
1571 mode = MBEDTLS_MODE_CBC;
1572 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001573 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001574 mode = MBEDTLS_MODE_CCM;
1575 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001576 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001577 mode = MBEDTLS_MODE_GCM;
1578 break;
1579 default:
1580 return( NULL );
1581 }
1582 }
1583 else if( alg == PSA_ALG_CMAC )
1584 mode = MBEDTLS_MODE_ECB;
1585 else if( alg == PSA_ALG_GMAC )
1586 mode = MBEDTLS_MODE_GCM;
1587 else
1588 return( NULL );
1589
1590 switch( key_type )
1591 {
1592 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001593 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001594 break;
1595 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001596 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1597 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001598 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001599 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001600 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001601 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001602 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1603 * but two-key Triple-DES is functionally three-key Triple-DES
1604 * with K1=K3, so that's how we present it to mbedtls. */
1605 if( key_bits == 128 )
1606 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001607 break;
1608 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001609 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001610 break;
1611 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001612 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001613 break;
1614 default:
1615 return( NULL );
1616 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001617 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001618 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001619
Jaeden Amero23bbb752018-06-26 14:16:54 +01001620 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1621 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001622}
1623
Gilles Peskinea05219c2018-11-16 16:02:56 +01001624#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001625static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001626{
Gilles Peskine2d277862018-06-18 15:41:12 +02001627 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001628 {
1629 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001630 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001631 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001632 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001633 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001634 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001635 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001636 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001637 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001638 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001639 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001640 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001641 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001642 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001643 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001644 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001645 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001646 return( 128 );
1647 default:
1648 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001649 }
1650}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001651#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001652
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001653/* Initialize the MAC operation structure. Once this function has been
1654 * called, psa_mac_abort can run and will do the right thing. */
1655static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1656 psa_algorithm_t alg )
1657{
1658 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1659
1660 operation->alg = alg;
1661 operation->key_set = 0;
1662 operation->iv_set = 0;
1663 operation->iv_required = 0;
1664 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001665 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001666
1667#if defined(MBEDTLS_CMAC_C)
1668 if( alg == PSA_ALG_CMAC )
1669 {
1670 operation->iv_required = 0;
1671 mbedtls_cipher_init( &operation->ctx.cmac );
1672 status = PSA_SUCCESS;
1673 }
1674 else
1675#endif /* MBEDTLS_CMAC_C */
1676#if defined(MBEDTLS_MD_C)
1677 if( PSA_ALG_IS_HMAC( operation->alg ) )
1678 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001679 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1680 operation->ctx.hmac.hash_ctx.alg = 0;
1681 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001682 }
1683 else
1684#endif /* MBEDTLS_MD_C */
1685 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001686 if( ! PSA_ALG_IS_MAC( alg ) )
1687 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001688 }
1689
1690 if( status != PSA_SUCCESS )
1691 memset( operation, 0, sizeof( *operation ) );
1692 return( status );
1693}
1694
Gilles Peskine01126fa2018-07-12 17:04:55 +02001695#if defined(MBEDTLS_MD_C)
1696static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1697{
Gilles Peskine3f108122018-12-07 18:14:53 +01001698 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001699 return( psa_hash_abort( &hmac->hash_ctx ) );
1700}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001701
1702static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1703{
1704 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1705 memset( hmac, 0, sizeof( *hmac ) );
1706}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001707#endif /* MBEDTLS_MD_C */
1708
Gilles Peskine8c9def32018-02-08 10:02:12 +01001709psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1710{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001711 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001712 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001713 /* The object has (apparently) been initialized but it is not
1714 * in use. It's ok to call abort on such an object, and there's
1715 * nothing to do. */
1716 return( PSA_SUCCESS );
1717 }
1718 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001719#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001720 if( operation->alg == PSA_ALG_CMAC )
1721 {
1722 mbedtls_cipher_free( &operation->ctx.cmac );
1723 }
1724 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001725#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001726#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001727 if( PSA_ALG_IS_HMAC( operation->alg ) )
1728 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001729 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001730 }
1731 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001732#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001733 {
1734 /* Sanity check (shouldn't happen: operation->alg should
1735 * always have been initialized to a valid value). */
1736 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001737 }
Moran Peker41deec42018-04-04 15:43:05 +03001738
Gilles Peskine8c9def32018-02-08 10:02:12 +01001739 operation->alg = 0;
1740 operation->key_set = 0;
1741 operation->iv_set = 0;
1742 operation->iv_required = 0;
1743 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001744 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001745
Gilles Peskine8c9def32018-02-08 10:02:12 +01001746 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001747
1748bad_state:
1749 /* If abort is called on an uninitialized object, we can't trust
1750 * anything. Wipe the object in case it contains confidential data.
1751 * This may result in a memory leak if a pointer gets overwritten,
1752 * but it's too late to do anything about this. */
1753 memset( operation, 0, sizeof( *operation ) );
1754 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001755}
1756
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001757#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001758static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001759 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001760 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001761 const mbedtls_cipher_info_t *cipher_info )
1762{
1763 int ret;
1764
1765 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001766
1767 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1768 if( ret != 0 )
1769 return( ret );
1770
1771 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1772 slot->data.raw.data,
1773 key_bits );
1774 return( ret );
1775}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001776#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001777
Gilles Peskine248051a2018-06-20 16:09:38 +02001778#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001779static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1780 const uint8_t *key,
1781 size_t key_length,
1782 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001783{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001784 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001785 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001786 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001787 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001788 psa_status_t status;
1789
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001790 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1791 * overflow below. This should never trigger if the hash algorithm
1792 * is implemented correctly. */
1793 /* The size checks against the ipad and opad buffers cannot be written
1794 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1795 * because that triggers -Wlogical-op on GCC 7.3. */
1796 if( block_size > sizeof( ipad ) )
1797 return( PSA_ERROR_NOT_SUPPORTED );
1798 if( block_size > sizeof( hmac->opad ) )
1799 return( PSA_ERROR_NOT_SUPPORTED );
1800 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001801 return( PSA_ERROR_NOT_SUPPORTED );
1802
Gilles Peskined223b522018-06-11 18:12:58 +02001803 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001804 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001805 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001806 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001807 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001808 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001809 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001810 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001811 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001812 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001813 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001814 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001815 }
Gilles Peskine96889972018-07-12 17:07:03 +02001816 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1817 * but it is permitted. It is common when HMAC is used in HKDF, for
1818 * example. Don't call `memcpy` in the 0-length because `key` could be
1819 * an invalid pointer which would make the behavior undefined. */
1820 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001821 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001822
Gilles Peskined223b522018-06-11 18:12:58 +02001823 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1824 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001825 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001826 ipad[i] ^= 0x36;
1827 memset( ipad + key_length, 0x36, block_size - key_length );
1828
1829 /* Copy the key material from ipad to opad, flipping the requisite bits,
1830 * and filling the rest of opad with the requisite constant. */
1831 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001832 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1833 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001834
Gilles Peskine01126fa2018-07-12 17:04:55 +02001835 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001836 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001837 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001838
Gilles Peskine01126fa2018-07-12 17:04:55 +02001839 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001840
1841cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001842 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001843
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001844 return( status );
1845}
Gilles Peskine248051a2018-06-20 16:09:38 +02001846#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001847
Gilles Peskine89167cb2018-07-08 20:12:23 +02001848static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001849 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001850 psa_algorithm_t alg,
1851 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001852{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001853 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001854 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001855 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001856 psa_key_usage_t usage =
1857 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001858 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001859 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001860
Gilles Peskined911eb72018-08-14 15:18:45 +02001861 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001862 if( status != PSA_SUCCESS )
1863 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001864 if( is_sign )
1865 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001866
Gilles Peskinec5487a82018-12-03 18:08:14 +01001867 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001868 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001869 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001870 key_bits = psa_get_key_bits( slot );
1871
Gilles Peskine8c9def32018-02-08 10:02:12 +01001872#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001873 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001874 {
1875 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001876 mbedtls_cipher_info_from_psa( full_length_alg,
1877 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001878 int ret;
1879 if( cipher_info == NULL )
1880 {
1881 status = PSA_ERROR_NOT_SUPPORTED;
1882 goto exit;
1883 }
1884 operation->mac_size = cipher_info->block_size;
1885 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1886 status = mbedtls_to_psa_error( ret );
1887 }
1888 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001889#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001890#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001891 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001892 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001893 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001894 if( hash_alg == 0 )
1895 {
1896 status = PSA_ERROR_NOT_SUPPORTED;
1897 goto exit;
1898 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001899
1900 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1901 /* Sanity check. This shouldn't fail on a valid configuration. */
1902 if( operation->mac_size == 0 ||
1903 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1904 {
1905 status = PSA_ERROR_NOT_SUPPORTED;
1906 goto exit;
1907 }
1908
Gilles Peskine01126fa2018-07-12 17:04:55 +02001909 if( slot->type != PSA_KEY_TYPE_HMAC )
1910 {
1911 status = PSA_ERROR_INVALID_ARGUMENT;
1912 goto exit;
1913 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001914
Gilles Peskine01126fa2018-07-12 17:04:55 +02001915 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1916 slot->data.raw.data,
1917 slot->data.raw.bytes,
1918 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001919 }
1920 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001921#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001922 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00001923 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02001924 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001925 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001926
Gilles Peskined911eb72018-08-14 15:18:45 +02001927 if( truncated == 0 )
1928 {
1929 /* The "normal" case: untruncated algorithm. Nothing to do. */
1930 }
1931 else if( truncated < 4 )
1932 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001933 /* A very short MAC is too short for security since it can be
1934 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1935 * so we make this our minimum, even though 32 bits is still
1936 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001937 status = PSA_ERROR_NOT_SUPPORTED;
1938 }
1939 else if( truncated > operation->mac_size )
1940 {
1941 /* It's impossible to "truncate" to a larger length. */
1942 status = PSA_ERROR_INVALID_ARGUMENT;
1943 }
1944 else
1945 operation->mac_size = truncated;
1946
Gilles Peskinefbfac682018-07-08 20:51:54 +02001947exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001948 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001949 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001950 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001951 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001952 else
1953 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001954 operation->key_set = 1;
1955 }
1956 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001957}
1958
Gilles Peskine89167cb2018-07-08 20:12:23 +02001959psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001960 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001961 psa_algorithm_t alg )
1962{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001963 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001964}
1965
1966psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001967 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001968 psa_algorithm_t alg )
1969{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001970 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001971}
1972
Gilles Peskine8c9def32018-02-08 10:02:12 +01001973psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1974 const uint8_t *input,
1975 size_t input_length )
1976{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001977 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001978 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001979 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001980 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001981 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001982 operation->has_input = 1;
1983
Gilles Peskine8c9def32018-02-08 10:02:12 +01001984#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001985 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001986 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001987 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1988 input, input_length );
1989 status = mbedtls_to_psa_error( ret );
1990 }
1991 else
1992#endif /* MBEDTLS_CMAC_C */
1993#if defined(MBEDTLS_MD_C)
1994 if( PSA_ALG_IS_HMAC( operation->alg ) )
1995 {
1996 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1997 input_length );
1998 }
1999 else
2000#endif /* MBEDTLS_MD_C */
2001 {
2002 /* This shouldn't happen if `operation` was initialized by
2003 * a setup function. */
2004 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002005 }
2006
Gilles Peskinefbfac682018-07-08 20:51:54 +02002007cleanup:
2008 if( status != PSA_SUCCESS )
2009 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002010 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002011}
2012
Gilles Peskine01126fa2018-07-12 17:04:55 +02002013#if defined(MBEDTLS_MD_C)
2014static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2015 uint8_t *mac,
2016 size_t mac_size )
2017{
2018 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2019 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2020 size_t hash_size = 0;
2021 size_t block_size = psa_get_hash_block_size( hash_alg );
2022 psa_status_t status;
2023
Gilles Peskine01126fa2018-07-12 17:04:55 +02002024 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2025 if( status != PSA_SUCCESS )
2026 return( status );
2027 /* From here on, tmp needs to be wiped. */
2028
2029 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2030 if( status != PSA_SUCCESS )
2031 goto exit;
2032
2033 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2034 if( status != PSA_SUCCESS )
2035 goto exit;
2036
2037 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2038 if( status != PSA_SUCCESS )
2039 goto exit;
2040
Gilles Peskined911eb72018-08-14 15:18:45 +02002041 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2042 if( status != PSA_SUCCESS )
2043 goto exit;
2044
2045 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002046
2047exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002048 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002049 return( status );
2050}
2051#endif /* MBEDTLS_MD_C */
2052
mohammad16036df908f2018-04-02 08:34:15 -07002053static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002054 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002055 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002056{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002057 if( ! operation->key_set )
2058 return( PSA_ERROR_BAD_STATE );
2059 if( operation->iv_required && ! operation->iv_set )
2060 return( PSA_ERROR_BAD_STATE );
2061
Gilles Peskine8c9def32018-02-08 10:02:12 +01002062 if( mac_size < operation->mac_size )
2063 return( PSA_ERROR_BUFFER_TOO_SMALL );
2064
Gilles Peskine8c9def32018-02-08 10:02:12 +01002065#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002066 if( operation->alg == PSA_ALG_CMAC )
2067 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002068 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2069 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2070 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002071 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002072 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002073 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002074 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002075 else
2076#endif /* MBEDTLS_CMAC_C */
2077#if defined(MBEDTLS_MD_C)
2078 if( PSA_ALG_IS_HMAC( operation->alg ) )
2079 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002080 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002081 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002082 }
2083 else
2084#endif /* MBEDTLS_MD_C */
2085 {
2086 /* This shouldn't happen if `operation` was initialized by
2087 * a setup function. */
2088 return( PSA_ERROR_BAD_STATE );
2089 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002090}
2091
Gilles Peskineacd4be32018-07-08 19:56:25 +02002092psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2093 uint8_t *mac,
2094 size_t mac_size,
2095 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002096{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002097 psa_status_t status;
2098
2099 /* Fill the output buffer with something that isn't a valid mac
2100 * (barring an attack on the mac and deliberately-crafted input),
2101 * in case the caller doesn't check the return status properly. */
2102 *mac_length = mac_size;
2103 /* If mac_size is 0 then mac may be NULL and then the
2104 * call to memset would have undefined behavior. */
2105 if( mac_size != 0 )
2106 memset( mac, '!', mac_size );
2107
Gilles Peskine89167cb2018-07-08 20:12:23 +02002108 if( ! operation->is_sign )
2109 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002110 status = PSA_ERROR_BAD_STATE;
2111 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002112 }
mohammad16036df908f2018-04-02 08:34:15 -07002113
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002114 status = psa_mac_finish_internal( operation, mac, mac_size );
2115
2116cleanup:
2117 if( status == PSA_SUCCESS )
2118 {
2119 status = psa_mac_abort( operation );
2120 if( status == PSA_SUCCESS )
2121 *mac_length = operation->mac_size;
2122 else
2123 memset( mac, '!', mac_size );
2124 }
2125 else
2126 psa_mac_abort( operation );
2127 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002128}
2129
Gilles Peskineacd4be32018-07-08 19:56:25 +02002130psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2131 const uint8_t *mac,
2132 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002133{
Gilles Peskine828ed142018-06-18 23:25:51 +02002134 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002135 psa_status_t status;
2136
Gilles Peskine89167cb2018-07-08 20:12:23 +02002137 if( operation->is_sign )
2138 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002139 status = PSA_ERROR_BAD_STATE;
2140 goto cleanup;
2141 }
2142 if( operation->mac_size != mac_length )
2143 {
2144 status = PSA_ERROR_INVALID_SIGNATURE;
2145 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002146 }
mohammad16036df908f2018-04-02 08:34:15 -07002147
2148 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002149 actual_mac, sizeof( actual_mac ) );
2150
2151 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2152 status = PSA_ERROR_INVALID_SIGNATURE;
2153
2154cleanup:
2155 if( status == PSA_SUCCESS )
2156 status = psa_mac_abort( operation );
2157 else
2158 psa_mac_abort( operation );
2159
Gilles Peskine3f108122018-12-07 18:14:53 +01002160 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002161
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002162 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002163}
2164
2165
Gilles Peskine20035e32018-02-03 22:44:14 +01002166
Gilles Peskine20035e32018-02-03 22:44:14 +01002167/****************************************************************/
2168/* Asymmetric cryptography */
2169/****************************************************************/
2170
Gilles Peskine2b450e32018-06-27 15:42:46 +02002171#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002172/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002173 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002174static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2175 size_t hash_length,
2176 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002177{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002178 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002179 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002180 *md_alg = mbedtls_md_get_type( md_info );
2181
2182 /* The Mbed TLS RSA module uses an unsigned int for hash length
2183 * parameters. Validate that it fits so that we don't risk an
2184 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002185#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002186 if( hash_length > UINT_MAX )
2187 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002188#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002189
2190#if defined(MBEDTLS_PKCS1_V15)
2191 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2192 * must be correct. */
2193 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2194 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002195 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002196 if( md_info == NULL )
2197 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002198 if( mbedtls_md_get_size( md_info ) != hash_length )
2199 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002200 }
2201#endif /* MBEDTLS_PKCS1_V15 */
2202
2203#if defined(MBEDTLS_PKCS1_V21)
2204 /* PSS requires a hash internally. */
2205 if( PSA_ALG_IS_RSA_PSS( alg ) )
2206 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002207 if( md_info == NULL )
2208 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002209 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002210#endif /* MBEDTLS_PKCS1_V21 */
2211
Gilles Peskine61b91d42018-06-08 16:09:36 +02002212 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002213}
2214
Gilles Peskine2b450e32018-06-27 15:42:46 +02002215static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2216 psa_algorithm_t alg,
2217 const uint8_t *hash,
2218 size_t hash_length,
2219 uint8_t *signature,
2220 size_t signature_size,
2221 size_t *signature_length )
2222{
2223 psa_status_t status;
2224 int ret;
2225 mbedtls_md_type_t md_alg;
2226
2227 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2228 if( status != PSA_SUCCESS )
2229 return( status );
2230
Gilles Peskine630a18a2018-06-29 17:49:35 +02002231 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002232 return( PSA_ERROR_BUFFER_TOO_SMALL );
2233
2234#if defined(MBEDTLS_PKCS1_V15)
2235 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2236 {
2237 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2238 MBEDTLS_MD_NONE );
2239 ret = mbedtls_rsa_pkcs1_sign( rsa,
2240 mbedtls_ctr_drbg_random,
2241 &global_data.ctr_drbg,
2242 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002243 md_alg,
2244 (unsigned int) hash_length,
2245 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002246 signature );
2247 }
2248 else
2249#endif /* MBEDTLS_PKCS1_V15 */
2250#if defined(MBEDTLS_PKCS1_V21)
2251 if( PSA_ALG_IS_RSA_PSS( alg ) )
2252 {
2253 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2254 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2255 mbedtls_ctr_drbg_random,
2256 &global_data.ctr_drbg,
2257 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002258 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002259 (unsigned int) hash_length,
2260 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002261 signature );
2262 }
2263 else
2264#endif /* MBEDTLS_PKCS1_V21 */
2265 {
2266 return( PSA_ERROR_INVALID_ARGUMENT );
2267 }
2268
2269 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002270 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002271 return( mbedtls_to_psa_error( ret ) );
2272}
2273
2274static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2275 psa_algorithm_t alg,
2276 const uint8_t *hash,
2277 size_t hash_length,
2278 const uint8_t *signature,
2279 size_t signature_length )
2280{
2281 psa_status_t status;
2282 int ret;
2283 mbedtls_md_type_t md_alg;
2284
2285 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2286 if( status != PSA_SUCCESS )
2287 return( status );
2288
Gilles Peskine630a18a2018-06-29 17:49:35 +02002289 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002290 return( PSA_ERROR_BUFFER_TOO_SMALL );
2291
2292#if defined(MBEDTLS_PKCS1_V15)
2293 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2294 {
2295 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2296 MBEDTLS_MD_NONE );
2297 ret = mbedtls_rsa_pkcs1_verify( rsa,
2298 mbedtls_ctr_drbg_random,
2299 &global_data.ctr_drbg,
2300 MBEDTLS_RSA_PUBLIC,
2301 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002302 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002303 hash,
2304 signature );
2305 }
2306 else
2307#endif /* MBEDTLS_PKCS1_V15 */
2308#if defined(MBEDTLS_PKCS1_V21)
2309 if( PSA_ALG_IS_RSA_PSS( alg ) )
2310 {
2311 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2312 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2313 mbedtls_ctr_drbg_random,
2314 &global_data.ctr_drbg,
2315 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002316 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002317 (unsigned int) hash_length,
2318 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002319 signature );
2320 }
2321 else
2322#endif /* MBEDTLS_PKCS1_V21 */
2323 {
2324 return( PSA_ERROR_INVALID_ARGUMENT );
2325 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002326
2327 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2328 * the rest of the signature is invalid". This has little use in
2329 * practice and PSA doesn't report this distinction. */
2330 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2331 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002332 return( mbedtls_to_psa_error( ret ) );
2333}
2334#endif /* MBEDTLS_RSA_C */
2335
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002336#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002337/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2338 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2339 * (even though these functions don't modify it). */
2340static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2341 psa_algorithm_t alg,
2342 const uint8_t *hash,
2343 size_t hash_length,
2344 uint8_t *signature,
2345 size_t signature_size,
2346 size_t *signature_length )
2347{
2348 int ret;
2349 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002350 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002351 mbedtls_mpi_init( &r );
2352 mbedtls_mpi_init( &s );
2353
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002354 if( signature_size < 2 * curve_bytes )
2355 {
2356 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2357 goto cleanup;
2358 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002359
Gilles Peskinea05219c2018-11-16 16:02:56 +01002360#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002361 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2362 {
2363 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2364 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2365 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2366 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2367 hash, hash_length,
2368 md_alg ) );
2369 }
2370 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002371#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002372 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002373 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002374 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2375 hash, hash_length,
2376 mbedtls_ctr_drbg_random,
2377 &global_data.ctr_drbg ) );
2378 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002379
2380 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2381 signature,
2382 curve_bytes ) );
2383 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2384 signature + curve_bytes,
2385 curve_bytes ) );
2386
2387cleanup:
2388 mbedtls_mpi_free( &r );
2389 mbedtls_mpi_free( &s );
2390 if( ret == 0 )
2391 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002392 return( mbedtls_to_psa_error( ret ) );
2393}
2394
2395static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2396 const uint8_t *hash,
2397 size_t hash_length,
2398 const uint8_t *signature,
2399 size_t signature_length )
2400{
2401 int ret;
2402 mbedtls_mpi r, s;
2403 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2404 mbedtls_mpi_init( &r );
2405 mbedtls_mpi_init( &s );
2406
2407 if( signature_length != 2 * curve_bytes )
2408 return( PSA_ERROR_INVALID_SIGNATURE );
2409
2410 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2411 signature,
2412 curve_bytes ) );
2413 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2414 signature + curve_bytes,
2415 curve_bytes ) );
2416
2417 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2418 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002419
2420cleanup:
2421 mbedtls_mpi_free( &r );
2422 mbedtls_mpi_free( &s );
2423 return( mbedtls_to_psa_error( ret ) );
2424}
2425#endif /* MBEDTLS_ECDSA_C */
2426
Gilles Peskinec5487a82018-12-03 18:08:14 +01002427psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002428 psa_algorithm_t alg,
2429 const uint8_t *hash,
2430 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002431 uint8_t *signature,
2432 size_t signature_size,
2433 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002434{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002435 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002436 psa_status_t status;
2437
2438 *signature_length = signature_size;
2439
Gilles Peskinec5487a82018-12-03 18:08:14 +01002440 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002441 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002442 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002443 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002444 {
2445 status = PSA_ERROR_INVALID_ARGUMENT;
2446 goto exit;
2447 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002448
Gilles Peskine20035e32018-02-03 22:44:14 +01002449#if defined(MBEDTLS_RSA_C)
2450 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2451 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002452 status = psa_rsa_sign( slot->data.rsa,
2453 alg,
2454 hash, hash_length,
2455 signature, signature_size,
2456 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002457 }
2458 else
2459#endif /* defined(MBEDTLS_RSA_C) */
2460#if defined(MBEDTLS_ECP_C)
2461 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2462 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002463#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002464 if(
2465#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2466 PSA_ALG_IS_ECDSA( alg )
2467#else
2468 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2469#endif
2470 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002471 status = psa_ecdsa_sign( slot->data.ecp,
2472 alg,
2473 hash, hash_length,
2474 signature, signature_size,
2475 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002476 else
2477#endif /* defined(MBEDTLS_ECDSA_C) */
2478 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002479 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002480 }
itayzafrir5c753392018-05-08 11:18:38 +03002481 }
2482 else
2483#endif /* defined(MBEDTLS_ECP_C) */
2484 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002485 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002486 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002487
2488exit:
2489 /* Fill the unused part of the output buffer (the whole buffer on error,
2490 * the trailing part on success) with something that isn't a valid mac
2491 * (barring an attack on the mac and deliberately-crafted input),
2492 * in case the caller doesn't check the return status properly. */
2493 if( status == PSA_SUCCESS )
2494 memset( signature + *signature_length, '!',
2495 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002496 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002497 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002498 /* If signature_size is 0 then we have nothing to do. We must not call
2499 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002500 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002501}
2502
Gilles Peskinec5487a82018-12-03 18:08:14 +01002503psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002504 psa_algorithm_t alg,
2505 const uint8_t *hash,
2506 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002507 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002508 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002509{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002510 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002511 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002512
Gilles Peskinec5487a82018-12-03 18:08:14 +01002513 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002514 if( status != PSA_SUCCESS )
2515 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002516
Gilles Peskine61b91d42018-06-08 16:09:36 +02002517#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002518 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002519 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002520 return( psa_rsa_verify( slot->data.rsa,
2521 alg,
2522 hash, hash_length,
2523 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002524 }
2525 else
2526#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002527#if defined(MBEDTLS_ECP_C)
2528 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2529 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002530#if defined(MBEDTLS_ECDSA_C)
2531 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002532 return( psa_ecdsa_verify( slot->data.ecp,
2533 hash, hash_length,
2534 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002535 else
2536#endif /* defined(MBEDTLS_ECDSA_C) */
2537 {
2538 return( PSA_ERROR_INVALID_ARGUMENT );
2539 }
itayzafrir5c753392018-05-08 11:18:38 +03002540 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002541 else
2542#endif /* defined(MBEDTLS_ECP_C) */
2543 {
2544 return( PSA_ERROR_NOT_SUPPORTED );
2545 }
2546}
2547
Gilles Peskine072ac562018-06-30 00:21:29 +02002548#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2549static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2550 mbedtls_rsa_context *rsa )
2551{
2552 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2553 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2554 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2555 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2556}
2557#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2558
Gilles Peskinec5487a82018-12-03 18:08:14 +01002559psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002560 psa_algorithm_t alg,
2561 const uint8_t *input,
2562 size_t input_length,
2563 const uint8_t *salt,
2564 size_t salt_length,
2565 uint8_t *output,
2566 size_t output_size,
2567 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002568{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002569 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002570 psa_status_t status;
2571
Darryl Green5cc689a2018-07-24 15:34:10 +01002572 (void) input;
2573 (void) input_length;
2574 (void) salt;
2575 (void) output;
2576 (void) output_size;
2577
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002578 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002579
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002580 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2581 return( PSA_ERROR_INVALID_ARGUMENT );
2582
Gilles Peskinec5487a82018-12-03 18:08:14 +01002583 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002584 if( status != PSA_SUCCESS )
2585 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002586 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2587 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002588 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002589
2590#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002591 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002592 {
2593 mbedtls_rsa_context *rsa = slot->data.rsa;
2594 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002595 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002596 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002597#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002598 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002599 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002600 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2601 mbedtls_ctr_drbg_random,
2602 &global_data.ctr_drbg,
2603 MBEDTLS_RSA_PUBLIC,
2604 input_length,
2605 input,
2606 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002607 }
2608 else
2609#endif /* MBEDTLS_PKCS1_V15 */
2610#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002611 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002612 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002613 psa_rsa_oaep_set_padding_mode( alg, rsa );
2614 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2615 mbedtls_ctr_drbg_random,
2616 &global_data.ctr_drbg,
2617 MBEDTLS_RSA_PUBLIC,
2618 salt, salt_length,
2619 input_length,
2620 input,
2621 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002622 }
2623 else
2624#endif /* MBEDTLS_PKCS1_V21 */
2625 {
2626 return( PSA_ERROR_INVALID_ARGUMENT );
2627 }
2628 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002629 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002630 return( mbedtls_to_psa_error( ret ) );
2631 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002632 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002633#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002634 {
2635 return( PSA_ERROR_NOT_SUPPORTED );
2636 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002637}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002638
Gilles Peskinec5487a82018-12-03 18:08:14 +01002639psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002640 psa_algorithm_t alg,
2641 const uint8_t *input,
2642 size_t input_length,
2643 const uint8_t *salt,
2644 size_t salt_length,
2645 uint8_t *output,
2646 size_t output_size,
2647 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002648{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002649 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002650 psa_status_t status;
2651
Darryl Green5cc689a2018-07-24 15:34:10 +01002652 (void) input;
2653 (void) input_length;
2654 (void) salt;
2655 (void) output;
2656 (void) output_size;
2657
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002658 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002659
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002660 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2661 return( PSA_ERROR_INVALID_ARGUMENT );
2662
Gilles Peskinec5487a82018-12-03 18:08:14 +01002663 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002664 if( status != PSA_SUCCESS )
2665 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002666 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2667 return( PSA_ERROR_INVALID_ARGUMENT );
2668
2669#if defined(MBEDTLS_RSA_C)
2670 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2671 {
2672 mbedtls_rsa_context *rsa = slot->data.rsa;
2673 int ret;
2674
Gilles Peskine630a18a2018-06-29 17:49:35 +02002675 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002676 return( PSA_ERROR_INVALID_ARGUMENT );
2677
2678#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002679 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002680 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002681 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2682 mbedtls_ctr_drbg_random,
2683 &global_data.ctr_drbg,
2684 MBEDTLS_RSA_PRIVATE,
2685 output_length,
2686 input,
2687 output,
2688 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002689 }
2690 else
2691#endif /* MBEDTLS_PKCS1_V15 */
2692#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002693 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002694 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002695 psa_rsa_oaep_set_padding_mode( alg, rsa );
2696 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2697 mbedtls_ctr_drbg_random,
2698 &global_data.ctr_drbg,
2699 MBEDTLS_RSA_PRIVATE,
2700 salt, salt_length,
2701 output_length,
2702 input,
2703 output,
2704 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002705 }
2706 else
2707#endif /* MBEDTLS_PKCS1_V21 */
2708 {
2709 return( PSA_ERROR_INVALID_ARGUMENT );
2710 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002711
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002712 return( mbedtls_to_psa_error( ret ) );
2713 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002714 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002715#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002716 {
2717 return( PSA_ERROR_NOT_SUPPORTED );
2718 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002719}
Gilles Peskine20035e32018-02-03 22:44:14 +01002720
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002721
2722
mohammad1603503973b2018-03-12 15:59:30 +02002723/****************************************************************/
2724/* Symmetric cryptography */
2725/****************************************************************/
2726
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002727/* Initialize the cipher operation structure. Once this function has been
2728 * called, psa_cipher_abort can run and will do the right thing. */
2729static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2730 psa_algorithm_t alg )
2731{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002732 if( ! PSA_ALG_IS_CIPHER( alg ) )
2733 {
2734 memset( operation, 0, sizeof( *operation ) );
2735 return( PSA_ERROR_INVALID_ARGUMENT );
2736 }
2737
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002738 operation->alg = alg;
2739 operation->key_set = 0;
2740 operation->iv_set = 0;
2741 operation->iv_required = 1;
2742 operation->iv_size = 0;
2743 operation->block_size = 0;
2744 mbedtls_cipher_init( &operation->ctx.cipher );
2745 return( PSA_SUCCESS );
2746}
2747
Gilles Peskinee553c652018-06-04 16:22:46 +02002748static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002749 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002750 psa_algorithm_t alg,
2751 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002752{
2753 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2754 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002755 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002756 size_t key_bits;
2757 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002758 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2759 PSA_KEY_USAGE_ENCRYPT :
2760 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002761
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002762 status = psa_cipher_init( operation, alg );
2763 if( status != PSA_SUCCESS )
2764 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002765
Gilles Peskinec5487a82018-12-03 18:08:14 +01002766 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002767 if( status != PSA_SUCCESS )
2768 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002769 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002770
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002771 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002772 if( cipher_info == NULL )
2773 return( PSA_ERROR_NOT_SUPPORTED );
2774
mohammad1603503973b2018-03-12 15:59:30 +02002775 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002776 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002777 {
2778 psa_cipher_abort( operation );
2779 return( mbedtls_to_psa_error( ret ) );
2780 }
2781
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002782#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002783 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002784 {
2785 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2786 unsigned char keys[24];
2787 memcpy( keys, slot->data.raw.data, 16 );
2788 memcpy( keys + 16, slot->data.raw.data, 8 );
2789 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2790 keys,
2791 192, cipher_operation );
2792 }
2793 else
2794#endif
2795 {
2796 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2797 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002798 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002799 }
Moran Peker41deec42018-04-04 15:43:05 +03002800 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002801 {
2802 psa_cipher_abort( operation );
2803 return( mbedtls_to_psa_error( ret ) );
2804 }
2805
mohammad16038481e742018-03-18 13:57:31 +02002806#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002807 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002808 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002809 case PSA_ALG_CBC_NO_PADDING:
2810 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2811 MBEDTLS_PADDING_NONE );
2812 break;
2813 case PSA_ALG_CBC_PKCS7:
2814 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2815 MBEDTLS_PADDING_PKCS7 );
2816 break;
2817 default:
2818 /* The algorithm doesn't involve padding. */
2819 ret = 0;
2820 break;
2821 }
2822 if( ret != 0 )
2823 {
2824 psa_cipher_abort( operation );
2825 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002826 }
2827#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2828
mohammad1603503973b2018-03-12 15:59:30 +02002829 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002830 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2831 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2832 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002833 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002834 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002835 }
mohammad1603503973b2018-03-12 15:59:30 +02002836
Moran Peker395db872018-05-31 14:07:14 +03002837 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002838}
2839
Gilles Peskinefe119512018-07-08 21:39:34 +02002840psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002841 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002842 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002843{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002844 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002845}
2846
Gilles Peskinefe119512018-07-08 21:39:34 +02002847psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002848 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002849 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002850{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002851 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002852}
2853
Gilles Peskinefe119512018-07-08 21:39:34 +02002854psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2855 unsigned char *iv,
2856 size_t iv_size,
2857 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002858{
itayzafrir534bd7c2018-08-02 13:56:32 +03002859 psa_status_t status;
2860 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002861 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002862 {
2863 status = PSA_ERROR_BAD_STATE;
2864 goto exit;
2865 }
Moran Peker41deec42018-04-04 15:43:05 +03002866 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002867 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002868 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002869 goto exit;
2870 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002871 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2872 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002873 if( ret != 0 )
2874 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002875 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002876 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002877 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002878
mohammad16038481e742018-03-18 13:57:31 +02002879 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002880 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002881
Moran Peker395db872018-05-31 14:07:14 +03002882exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002883 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002884 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002885 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002886}
2887
Gilles Peskinefe119512018-07-08 21:39:34 +02002888psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2889 const unsigned char *iv,
2890 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002891{
itayzafrir534bd7c2018-08-02 13:56:32 +03002892 psa_status_t status;
2893 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002894 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002895 {
2896 status = PSA_ERROR_BAD_STATE;
2897 goto exit;
2898 }
Moran Pekera28258c2018-05-29 16:25:04 +03002899 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002900 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002901 status = PSA_ERROR_INVALID_ARGUMENT;
2902 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002903 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002904 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2905 status = mbedtls_to_psa_error( ret );
2906exit:
2907 if( status == PSA_SUCCESS )
2908 operation->iv_set = 1;
2909 else
mohammad1603503973b2018-03-12 15:59:30 +02002910 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002911 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002912}
2913
Gilles Peskinee553c652018-06-04 16:22:46 +02002914psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2915 const uint8_t *input,
2916 size_t input_length,
2917 unsigned char *output,
2918 size_t output_size,
2919 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002920{
itayzafrir534bd7c2018-08-02 13:56:32 +03002921 psa_status_t status;
2922 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002923 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002924 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002925 {
2926 /* Take the unprocessed partial block left over from previous
2927 * update calls, if any, plus the input to this call. Remove
2928 * the last partial block, if any. You get the data that will be
2929 * output in this call. */
2930 expected_output_size =
2931 ( operation->ctx.cipher.unprocessed_len + input_length )
2932 / operation->block_size * operation->block_size;
2933 }
2934 else
2935 {
2936 expected_output_size = input_length;
2937 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002938
Gilles Peskine89d789c2018-06-04 17:17:16 +02002939 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002940 {
2941 status = PSA_ERROR_BUFFER_TOO_SMALL;
2942 goto exit;
2943 }
mohammad160382759612018-03-12 18:16:40 +02002944
mohammad1603503973b2018-03-12 15:59:30 +02002945 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002946 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002947 status = mbedtls_to_psa_error( ret );
2948exit:
2949 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002950 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002951 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002952}
2953
Gilles Peskinee553c652018-06-04 16:22:46 +02002954psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2955 uint8_t *output,
2956 size_t output_size,
2957 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002958{
Janos Follath315b51c2018-07-09 16:04:51 +01002959 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2960 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002961 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002962
mohammad1603503973b2018-03-12 15:59:30 +02002963 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002964 {
Janos Follath315b51c2018-07-09 16:04:51 +01002965 status = PSA_ERROR_BAD_STATE;
2966 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002967 }
2968 if( operation->iv_required && ! operation->iv_set )
2969 {
Janos Follath315b51c2018-07-09 16:04:51 +01002970 status = PSA_ERROR_BAD_STATE;
2971 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002972 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002973
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002974 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002975 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2976 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002977 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002978 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002979 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002980 }
2981
Janos Follath315b51c2018-07-09 16:04:51 +01002982 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2983 temp_output_buffer,
2984 output_length );
2985 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002986 {
Janos Follath315b51c2018-07-09 16:04:51 +01002987 status = mbedtls_to_psa_error( cipher_ret );
2988 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002989 }
Janos Follath315b51c2018-07-09 16:04:51 +01002990
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002991 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002992 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002993 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002994 memcpy( output, temp_output_buffer, *output_length );
2995 else
2996 {
Janos Follath315b51c2018-07-09 16:04:51 +01002997 status = PSA_ERROR_BUFFER_TOO_SMALL;
2998 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002999 }
mohammad1603503973b2018-03-12 15:59:30 +02003000
Gilles Peskine3f108122018-12-07 18:14:53 +01003001 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003002 status = psa_cipher_abort( operation );
3003
3004 return( status );
3005
3006error:
3007
3008 *output_length = 0;
3009
Gilles Peskine3f108122018-12-07 18:14:53 +01003010 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003011 (void) psa_cipher_abort( operation );
3012
3013 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003014}
3015
Gilles Peskinee553c652018-06-04 16:22:46 +02003016psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3017{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003018 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003019 {
3020 /* The object has (apparently) been initialized but it is not
3021 * in use. It's ok to call abort on such an object, and there's
3022 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003023 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003024 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003025
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003026 /* Sanity check (shouldn't happen: operation->alg should
3027 * always have been initialized to a valid value). */
3028 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3029 return( PSA_ERROR_BAD_STATE );
3030
mohammad1603503973b2018-03-12 15:59:30 +02003031 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003032
Moran Peker41deec42018-04-04 15:43:05 +03003033 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003034 operation->key_set = 0;
3035 operation->iv_set = 0;
3036 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003037 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003038 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003039
Moran Peker395db872018-05-31 14:07:14 +03003040 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003041}
3042
Gilles Peskinea0655c32018-04-30 17:06:50 +02003043
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003044
mohammad16038cc1cee2018-03-28 01:21:33 +03003045/****************************************************************/
3046/* Key Policy */
3047/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003048
mohammad160327010052018-07-03 13:16:15 +03003049#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003050void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003051{
Gilles Peskine803ce742018-06-18 16:07:14 +02003052 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003053}
3054
Gilles Peskine2d277862018-06-18 15:41:12 +02003055void psa_key_policy_set_usage( psa_key_policy_t *policy,
3056 psa_key_usage_t usage,
3057 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003058{
mohammad16034eed7572018-03-28 05:14:59 -07003059 policy->usage = usage;
3060 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003061}
3062
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003063psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003064{
mohammad16036df908f2018-04-02 08:34:15 -07003065 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003066}
3067
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003068psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003069{
mohammad16036df908f2018-04-02 08:34:15 -07003070 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003071}
mohammad160327010052018-07-03 13:16:15 +03003072#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003073
Gilles Peskinec5487a82018-12-03 18:08:14 +01003074psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003075 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003076{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003077 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003078 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003079
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003080 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003081 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003082
Gilles Peskinec5487a82018-12-03 18:08:14 +01003083 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003084 if( status != PSA_SUCCESS )
3085 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003086
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003087 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3088 PSA_KEY_USAGE_ENCRYPT |
3089 PSA_KEY_USAGE_DECRYPT |
3090 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003091 PSA_KEY_USAGE_VERIFY |
3092 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003093 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003094
mohammad16036df908f2018-04-02 08:34:15 -07003095 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003096
3097 return( PSA_SUCCESS );
3098}
3099
Gilles Peskinec5487a82018-12-03 18:08:14 +01003100psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003101 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003102{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003103 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003104 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003105
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003106 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003107 return( PSA_ERROR_INVALID_ARGUMENT );
3108
Gilles Peskinec5487a82018-12-03 18:08:14 +01003109 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003110 if( status != PSA_SUCCESS )
3111 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003112
mohammad16036df908f2018-04-02 08:34:15 -07003113 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003114
3115 return( PSA_SUCCESS );
3116}
Gilles Peskine20035e32018-02-03 22:44:14 +01003117
Gilles Peskinea0655c32018-04-30 17:06:50 +02003118
3119
mohammad1603804cd712018-03-20 22:44:08 +02003120/****************************************************************/
3121/* Key Lifetime */
3122/****************************************************************/
3123
Gilles Peskinec5487a82018-12-03 18:08:14 +01003124psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003125 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003126{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003127 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003128 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003129
Gilles Peskinec5487a82018-12-03 18:08:14 +01003130 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003131 if( status != PSA_SUCCESS )
3132 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003133
mohammad1603804cd712018-03-20 22:44:08 +02003134 *lifetime = slot->lifetime;
3135
3136 return( PSA_SUCCESS );
3137}
3138
Gilles Peskine20035e32018-02-03 22:44:14 +01003139
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003140
mohammad16035955c982018-04-26 00:53:03 +03003141/****************************************************************/
3142/* AEAD */
3143/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003144
Gilles Peskineedf9a652018-08-17 18:11:56 +02003145typedef struct
3146{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003147 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003148 const mbedtls_cipher_info_t *cipher_info;
3149 union
3150 {
3151#if defined(MBEDTLS_CCM_C)
3152 mbedtls_ccm_context ccm;
3153#endif /* MBEDTLS_CCM_C */
3154#if defined(MBEDTLS_GCM_C)
3155 mbedtls_gcm_context gcm;
3156#endif /* MBEDTLS_GCM_C */
3157 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003158 psa_algorithm_t core_alg;
3159 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003160 uint8_t tag_length;
3161} aead_operation_t;
3162
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003163static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003164{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003165 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003166 {
3167#if defined(MBEDTLS_CCM_C)
3168 case PSA_ALG_CCM:
3169 mbedtls_ccm_free( &operation->ctx.ccm );
3170 break;
3171#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003172#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003173 case PSA_ALG_GCM:
3174 mbedtls_gcm_free( &operation->ctx.gcm );
3175 break;
3176#endif /* MBEDTLS_GCM_C */
3177 }
3178}
3179
3180static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003181 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003182 psa_key_usage_t usage,
3183 psa_algorithm_t alg )
3184{
3185 psa_status_t status;
3186 size_t key_bits;
3187 mbedtls_cipher_id_t cipher_id;
3188
Gilles Peskinec5487a82018-12-03 18:08:14 +01003189 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003190 if( status != PSA_SUCCESS )
3191 return( status );
3192
3193 key_bits = psa_get_key_bits( operation->slot );
3194
3195 operation->cipher_info =
3196 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3197 &cipher_id );
3198 if( operation->cipher_info == NULL )
3199 return( PSA_ERROR_NOT_SUPPORTED );
3200
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003201 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003202 {
3203#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003204 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3205 operation->core_alg = PSA_ALG_CCM;
3206 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003207 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3208 return( PSA_ERROR_INVALID_ARGUMENT );
3209 mbedtls_ccm_init( &operation->ctx.ccm );
3210 status = mbedtls_to_psa_error(
3211 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3212 operation->slot->data.raw.data,
3213 (unsigned int) key_bits ) );
3214 if( status != 0 )
3215 goto cleanup;
3216 break;
3217#endif /* MBEDTLS_CCM_C */
3218
3219#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003220 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3221 operation->core_alg = PSA_ALG_GCM;
3222 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003223 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3224 return( PSA_ERROR_INVALID_ARGUMENT );
3225 mbedtls_gcm_init( &operation->ctx.gcm );
3226 status = mbedtls_to_psa_error(
3227 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3228 operation->slot->data.raw.data,
3229 (unsigned int) key_bits ) );
3230 break;
3231#endif /* MBEDTLS_GCM_C */
3232
3233 default:
3234 return( PSA_ERROR_NOT_SUPPORTED );
3235 }
3236
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003237 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3238 {
3239 status = PSA_ERROR_INVALID_ARGUMENT;
3240 goto cleanup;
3241 }
3242 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3243 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3244 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3245 * In both cases, mbedtls_xxx will validate the tag length below. */
3246
Gilles Peskineedf9a652018-08-17 18:11:56 +02003247 return( PSA_SUCCESS );
3248
3249cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003250 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003251 return( status );
3252}
3253
Gilles Peskinec5487a82018-12-03 18:08:14 +01003254psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003255 psa_algorithm_t alg,
3256 const uint8_t *nonce,
3257 size_t nonce_length,
3258 const uint8_t *additional_data,
3259 size_t additional_data_length,
3260 const uint8_t *plaintext,
3261 size_t plaintext_length,
3262 uint8_t *ciphertext,
3263 size_t ciphertext_size,
3264 size_t *ciphertext_length )
3265{
mohammad16035955c982018-04-26 00:53:03 +03003266 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003267 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003268 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003269
mohammad1603f08a5502018-06-03 15:05:47 +03003270 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003271
Gilles Peskinec5487a82018-12-03 18:08:14 +01003272 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003273 if( status != PSA_SUCCESS )
3274 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003275
Gilles Peskineedf9a652018-08-17 18:11:56 +02003276 /* For all currently supported modes, the tag is at the end of the
3277 * ciphertext. */
3278 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3279 {
3280 status = PSA_ERROR_BUFFER_TOO_SMALL;
3281 goto exit;
3282 }
3283 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003284
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003285#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003286 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003287 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003288 status = mbedtls_to_psa_error(
3289 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3290 MBEDTLS_GCM_ENCRYPT,
3291 plaintext_length,
3292 nonce, nonce_length,
3293 additional_data, additional_data_length,
3294 plaintext, ciphertext,
3295 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003296 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003297 else
3298#endif /* MBEDTLS_GCM_C */
3299#if defined(MBEDTLS_CCM_C)
3300 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003301 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003302 status = mbedtls_to_psa_error(
3303 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3304 plaintext_length,
3305 nonce, nonce_length,
3306 additional_data,
3307 additional_data_length,
3308 plaintext, ciphertext,
3309 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003310 }
mohammad16035c8845f2018-05-09 05:40:09 -07003311 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003312#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003313 {
mohammad1603554faad2018-06-03 15:07:38 +03003314 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003315 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003316
Gilles Peskineedf9a652018-08-17 18:11:56 +02003317 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3318 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003319
Gilles Peskineedf9a652018-08-17 18:11:56 +02003320exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003321 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003322 if( status == PSA_SUCCESS )
3323 *ciphertext_length = plaintext_length + operation.tag_length;
3324 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003325}
3326
Gilles Peskineee652a32018-06-01 19:23:52 +02003327/* Locate the tag in a ciphertext buffer containing the encrypted data
3328 * followed by the tag. Return the length of the part preceding the tag in
3329 * *plaintext_length. This is the size of the plaintext in modes where
3330 * the encrypted data has the same size as the plaintext, such as
3331 * CCM and GCM. */
3332static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3333 const uint8_t *ciphertext,
3334 size_t ciphertext_length,
3335 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003336 const uint8_t **p_tag )
3337{
3338 size_t payload_length;
3339 if( tag_length > ciphertext_length )
3340 return( PSA_ERROR_INVALID_ARGUMENT );
3341 payload_length = ciphertext_length - tag_length;
3342 if( payload_length > plaintext_size )
3343 return( PSA_ERROR_BUFFER_TOO_SMALL );
3344 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003345 return( PSA_SUCCESS );
3346}
3347
Gilles Peskinec5487a82018-12-03 18:08:14 +01003348psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003349 psa_algorithm_t alg,
3350 const uint8_t *nonce,
3351 size_t nonce_length,
3352 const uint8_t *additional_data,
3353 size_t additional_data_length,
3354 const uint8_t *ciphertext,
3355 size_t ciphertext_length,
3356 uint8_t *plaintext,
3357 size_t plaintext_size,
3358 size_t *plaintext_length )
3359{
mohammad16035955c982018-04-26 00:53:03 +03003360 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003361 aead_operation_t operation;
3362 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003363
Gilles Peskineee652a32018-06-01 19:23:52 +02003364 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003365
Gilles Peskinec5487a82018-12-03 18:08:14 +01003366 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003367 if( status != PSA_SUCCESS )
3368 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003369
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003370#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003371 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003372 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003373 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003374 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003375 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003376 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003377 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003378
Gilles Peskineedf9a652018-08-17 18:11:56 +02003379 status = mbedtls_to_psa_error(
3380 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3381 ciphertext_length - operation.tag_length,
3382 nonce, nonce_length,
3383 additional_data,
3384 additional_data_length,
3385 tag, operation.tag_length,
3386 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003387 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003388 else
3389#endif /* MBEDTLS_GCM_C */
3390#if defined(MBEDTLS_CCM_C)
3391 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003392 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003393 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003394 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003395 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003396 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003397 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003398
Gilles Peskineedf9a652018-08-17 18:11:56 +02003399 status = mbedtls_to_psa_error(
3400 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3401 ciphertext_length - operation.tag_length,
3402 nonce, nonce_length,
3403 additional_data,
3404 additional_data_length,
3405 ciphertext, plaintext,
3406 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003407 }
mohammad160339574652018-06-01 04:39:53 -07003408 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003409#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003410 {
mohammad1603554faad2018-06-03 15:07:38 +03003411 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003412 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003413
Gilles Peskineedf9a652018-08-17 18:11:56 +02003414 if( status != PSA_SUCCESS && plaintext_size != 0 )
3415 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003416
Gilles Peskineedf9a652018-08-17 18:11:56 +02003417exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003418 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003419 if( status == PSA_SUCCESS )
3420 *plaintext_length = ciphertext_length - operation.tag_length;
3421 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003422}
3423
Gilles Peskinea0655c32018-04-30 17:06:50 +02003424
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003425
Gilles Peskine20035e32018-02-03 22:44:14 +01003426/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003427/* Generators */
3428/****************************************************************/
3429
3430psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3431{
3432 psa_status_t status = PSA_SUCCESS;
3433 if( generator->alg == 0 )
3434 {
3435 /* The object has (apparently) been initialized but it is not
3436 * in use. It's ok to call abort on such an object, and there's
3437 * nothing to do. */
3438 }
3439 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003440 if( generator->alg == PSA_ALG_SELECT_RAW )
3441 {
3442 if( generator->ctx.buffer.data != NULL )
3443 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003444 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003445 generator->ctx.buffer.size );
3446 mbedtls_free( generator->ctx.buffer.data );
3447 }
3448 }
3449 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003450#if defined(MBEDTLS_MD_C)
3451 if( PSA_ALG_IS_HKDF( generator->alg ) )
3452 {
3453 mbedtls_free( generator->ctx.hkdf.info );
3454 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3455 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003456 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3457 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3458 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003459 {
3460 if( generator->ctx.tls12_prf.key != NULL )
3461 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003462 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003463 generator->ctx.tls12_prf.key_len );
3464 mbedtls_free( generator->ctx.tls12_prf.key );
3465 }
Hanno Becker580fba12018-11-13 20:50:45 +00003466
3467 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3468 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003469 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003470 generator->ctx.tls12_prf.Ai_with_seed_len );
3471 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3472 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003473 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003474 else
3475#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003476 {
3477 status = PSA_ERROR_BAD_STATE;
3478 }
3479 memset( generator, 0, sizeof( *generator ) );
3480 return( status );
3481}
3482
3483
3484psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3485 size_t *capacity)
3486{
3487 *capacity = generator->capacity;
3488 return( PSA_SUCCESS );
3489}
3490
Gilles Peskinebef7f142018-07-12 17:22:21 +02003491#if defined(MBEDTLS_MD_C)
3492/* Read some bytes from an HKDF-based generator. This performs a chunk
3493 * of the expand phase of the HKDF algorithm. */
3494static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3495 psa_algorithm_t hash_alg,
3496 uint8_t *output,
3497 size_t output_length )
3498{
3499 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3500 psa_status_t status;
3501
3502 while( output_length != 0 )
3503 {
3504 /* Copy what remains of the current block */
3505 uint8_t n = hash_length - hkdf->offset_in_block;
3506 if( n > output_length )
3507 n = (uint8_t) output_length;
3508 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3509 output += n;
3510 output_length -= n;
3511 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003512 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003513 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003514 /* We can't be wanting more output after block 0xff, otherwise
3515 * the capacity check in psa_generator_read() would have
3516 * prevented this call. It could happen only if the generator
3517 * object was corrupted or if this function is called directly
3518 * inside the library. */
3519 if( hkdf->block_number == 0xff )
3520 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003521
3522 /* We need a new block */
3523 ++hkdf->block_number;
3524 hkdf->offset_in_block = 0;
3525 status = psa_hmac_setup_internal( &hkdf->hmac,
3526 hkdf->prk, hash_length,
3527 hash_alg );
3528 if( status != PSA_SUCCESS )
3529 return( status );
3530 if( hkdf->block_number != 1 )
3531 {
3532 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3533 hkdf->output_block,
3534 hash_length );
3535 if( status != PSA_SUCCESS )
3536 return( status );
3537 }
3538 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3539 hkdf->info,
3540 hkdf->info_length );
3541 if( status != PSA_SUCCESS )
3542 return( status );
3543 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3544 &hkdf->block_number, 1 );
3545 if( status != PSA_SUCCESS )
3546 return( status );
3547 status = psa_hmac_finish_internal( &hkdf->hmac,
3548 hkdf->output_block,
3549 sizeof( hkdf->output_block ) );
3550 if( status != PSA_SUCCESS )
3551 return( status );
3552 }
3553
3554 return( PSA_SUCCESS );
3555}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003556
3557static psa_status_t psa_generator_tls12_prf_generate_next_block(
3558 psa_tls12_prf_generator_t *tls12_prf,
3559 psa_algorithm_t alg )
3560{
3561 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3562 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3563 psa_hmac_internal_data hmac;
3564 psa_status_t status, cleanup_status;
3565
Hanno Becker3b339e22018-11-13 20:56:14 +00003566 unsigned char *Ai;
3567 size_t Ai_len;
3568
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003569 /* We can't be wanting more output after block 0xff, otherwise
3570 * the capacity check in psa_generator_read() would have
3571 * prevented this call. It could happen only if the generator
3572 * object was corrupted or if this function is called directly
3573 * inside the library. */
3574 if( tls12_prf->block_number == 0xff )
3575 return( PSA_ERROR_BAD_STATE );
3576
3577 /* We need a new block */
3578 ++tls12_prf->block_number;
3579 tls12_prf->offset_in_block = 0;
3580
3581 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3582 *
3583 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3584 *
3585 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3586 * HMAC_hash(secret, A(2) + seed) +
3587 * HMAC_hash(secret, A(3) + seed) + ...
3588 *
3589 * A(0) = seed
3590 * A(i) = HMAC_hash( secret, A(i-1) )
3591 *
3592 * The `psa_tls12_prf_generator` structures saves the block
3593 * `HMAC_hash(secret, A(i) + seed)` from which the output
3594 * is currently extracted as `output_block`, while
3595 * `A(i) + seed` is stored in `Ai_with_seed`.
3596 *
3597 * Generating a new block means recalculating `Ai_with_seed`
3598 * from the A(i)-part of it, and afterwards recalculating
3599 * `output_block`.
3600 *
3601 * A(0) is computed at setup time.
3602 *
3603 */
3604
3605 psa_hmac_init_internal( &hmac );
3606
3607 /* We must distinguish the calculation of A(1) from those
3608 * of A(2) and higher, because A(0)=seed has a different
3609 * length than the other A(i). */
3610 if( tls12_prf->block_number == 1 )
3611 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003612 Ai = tls12_prf->Ai_with_seed + hash_length;
3613 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003614 }
3615 else
3616 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003617 Ai = tls12_prf->Ai_with_seed;
3618 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003619 }
3620
Hanno Becker3b339e22018-11-13 20:56:14 +00003621 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3622 status = psa_hmac_setup_internal( &hmac,
3623 tls12_prf->key,
3624 tls12_prf->key_len,
3625 hash_alg );
3626 if( status != PSA_SUCCESS )
3627 goto cleanup;
3628
3629 status = psa_hash_update( &hmac.hash_ctx,
3630 Ai, Ai_len );
3631 if( status != PSA_SUCCESS )
3632 goto cleanup;
3633
3634 status = psa_hmac_finish_internal( &hmac,
3635 tls12_prf->Ai_with_seed,
3636 hash_length );
3637 if( status != PSA_SUCCESS )
3638 goto cleanup;
3639
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003640 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3641 status = psa_hmac_setup_internal( &hmac,
3642 tls12_prf->key,
3643 tls12_prf->key_len,
3644 hash_alg );
3645 if( status != PSA_SUCCESS )
3646 goto cleanup;
3647
3648 status = psa_hash_update( &hmac.hash_ctx,
3649 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003650 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003651 if( status != PSA_SUCCESS )
3652 goto cleanup;
3653
3654 status = psa_hmac_finish_internal( &hmac,
3655 tls12_prf->output_block,
3656 hash_length );
3657 if( status != PSA_SUCCESS )
3658 goto cleanup;
3659
3660cleanup:
3661
3662 cleanup_status = psa_hmac_abort_internal( &hmac );
3663 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3664 status = cleanup_status;
3665
3666 return( status );
3667}
3668
3669/* Read some bytes from an TLS-1.2-PRF-based generator.
3670 * See Section 5 of RFC 5246. */
3671static psa_status_t psa_generator_tls12_prf_read(
3672 psa_tls12_prf_generator_t *tls12_prf,
3673 psa_algorithm_t alg,
3674 uint8_t *output,
3675 size_t output_length )
3676{
3677 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3678 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3679 psa_status_t status;
3680
3681 while( output_length != 0 )
3682 {
3683 /* Copy what remains of the current block */
3684 uint8_t n = hash_length - tls12_prf->offset_in_block;
3685
3686 /* Check if we have fully processed the current block. */
3687 if( n == 0 )
3688 {
3689 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3690 alg );
3691 if( status != PSA_SUCCESS )
3692 return( status );
3693
3694 continue;
3695 }
3696
3697 if( n > output_length )
3698 n = (uint8_t) output_length;
3699 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3700 n );
3701 output += n;
3702 output_length -= n;
3703 tls12_prf->offset_in_block += n;
3704 }
3705
3706 return( PSA_SUCCESS );
3707}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003708#endif /* MBEDTLS_MD_C */
3709
Gilles Peskineeab56e42018-07-12 17:12:33 +02003710psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3711 uint8_t *output,
3712 size_t output_length )
3713{
3714 psa_status_t status;
3715
3716 if( output_length > generator->capacity )
3717 {
3718 generator->capacity = 0;
3719 /* Go through the error path to wipe all confidential data now
3720 * that the generator object is useless. */
3721 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3722 goto exit;
3723 }
3724 if( output_length == 0 &&
3725 generator->capacity == 0 && generator->alg == 0 )
3726 {
3727 /* Edge case: this is a blank or finished generator, and 0
3728 * bytes were requested. The right error in this case could
3729 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3730 * INSUFFICIENT_CAPACITY, which is right for a finished
3731 * generator, for consistency with the case when
3732 * output_length > 0. */
3733 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3734 }
3735 generator->capacity -= output_length;
3736
Gilles Peskine751d9652018-09-18 12:05:44 +02003737 if( generator->alg == PSA_ALG_SELECT_RAW )
3738 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003739 /* Initially, the capacity of a selection generator is always
3740 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3741 * abbreviated in this comment as `size`. When the remaining
3742 * capacity is `c`, the next bytes to serve start `c` bytes
3743 * from the end of the buffer, i.e. `size - c` from the
3744 * beginning of the buffer. Since `generator->capacity` was just
3745 * decremented above, we need to serve the bytes from
3746 * `size - generator->capacity - output_length` to
3747 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003748 size_t offset =
3749 generator->ctx.buffer.size - generator->capacity - output_length;
3750 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3751 status = PSA_SUCCESS;
3752 }
3753 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003754#if defined(MBEDTLS_MD_C)
3755 if( PSA_ALG_IS_HKDF( generator->alg ) )
3756 {
3757 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3758 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3759 output, output_length );
3760 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003761 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3762 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003763 {
3764 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3765 generator->alg, output,
3766 output_length );
3767 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003768 else
3769#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003770 {
3771 return( PSA_ERROR_BAD_STATE );
3772 }
3773
3774exit:
3775 if( status != PSA_SUCCESS )
3776 {
3777 psa_generator_abort( generator );
3778 memset( output, '!', output_length );
3779 }
3780 return( status );
3781}
3782
Gilles Peskine08542d82018-07-19 17:05:42 +02003783#if defined(MBEDTLS_DES_C)
3784static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3785{
3786 if( data_size >= 8 )
3787 mbedtls_des_key_set_parity( data );
3788 if( data_size >= 16 )
3789 mbedtls_des_key_set_parity( data + 8 );
3790 if( data_size >= 24 )
3791 mbedtls_des_key_set_parity( data + 16 );
3792}
3793#endif /* MBEDTLS_DES_C */
3794
Gilles Peskinec5487a82018-12-03 18:08:14 +01003795psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003796 psa_key_type_t type,
3797 size_t bits,
3798 psa_crypto_generator_t *generator )
3799{
3800 uint8_t *data = NULL;
3801 size_t bytes = PSA_BITS_TO_BYTES( bits );
3802 psa_status_t status;
3803
3804 if( ! key_type_is_raw_bytes( type ) )
3805 return( PSA_ERROR_INVALID_ARGUMENT );
3806 if( bits % 8 != 0 )
3807 return( PSA_ERROR_INVALID_ARGUMENT );
3808 data = mbedtls_calloc( 1, bytes );
3809 if( data == NULL )
3810 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3811
3812 status = psa_generator_read( generator, data, bytes );
3813 if( status != PSA_SUCCESS )
3814 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003815#if defined(MBEDTLS_DES_C)
3816 if( type == PSA_KEY_TYPE_DES )
3817 psa_des_set_key_parity( data, bytes );
3818#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003819 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003820
3821exit:
3822 mbedtls_free( data );
3823 return( status );
3824}
3825
3826
3827
3828/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003829/* Key derivation */
3830/****************************************************************/
3831
Gilles Peskinea05219c2018-11-16 16:02:56 +01003832#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003833/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003834 * of the HKDF algorithm.
3835 *
3836 * Note that if this function fails, you must call psa_generator_abort()
3837 * to potentially free embedded data structures and wipe confidential data.
3838 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003839static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003840 const uint8_t *secret,
3841 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003842 psa_algorithm_t hash_alg,
3843 const uint8_t *salt,
3844 size_t salt_length,
3845 const uint8_t *label,
3846 size_t label_length )
3847{
3848 psa_status_t status;
3849 status = psa_hmac_setup_internal( &hkdf->hmac,
3850 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003851 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003852 if( status != PSA_SUCCESS )
3853 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003854 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003855 if( status != PSA_SUCCESS )
3856 return( status );
3857 status = psa_hmac_finish_internal( &hkdf->hmac,
3858 hkdf->prk,
3859 sizeof( hkdf->prk ) );
3860 if( status != PSA_SUCCESS )
3861 return( status );
3862 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3863 hkdf->block_number = 0;
3864 hkdf->info_length = label_length;
3865 if( label_length != 0 )
3866 {
3867 hkdf->info = mbedtls_calloc( 1, label_length );
3868 if( hkdf->info == NULL )
3869 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3870 memcpy( hkdf->info, label, label_length );
3871 }
3872 return( PSA_SUCCESS );
3873}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003874#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003875
Gilles Peskinea05219c2018-11-16 16:02:56 +01003876#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003877/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3878 *
3879 * Note that if this function fails, you must call psa_generator_abort()
3880 * to potentially free embedded data structures and wipe confidential data.
3881 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003882static psa_status_t psa_generator_tls12_prf_setup(
3883 psa_tls12_prf_generator_t *tls12_prf,
3884 const unsigned char *key,
3885 size_t key_len,
3886 psa_algorithm_t hash_alg,
3887 const uint8_t *salt,
3888 size_t salt_length,
3889 const uint8_t *label,
3890 size_t label_length )
3891{
3892 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003893 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3894 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003895
3896 tls12_prf->key = mbedtls_calloc( 1, key_len );
3897 if( tls12_prf->key == NULL )
3898 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3899 tls12_prf->key_len = key_len;
3900 memcpy( tls12_prf->key, key, key_len );
3901
Hanno Becker580fba12018-11-13 20:50:45 +00003902 overflow = ( salt_length + label_length < salt_length ) ||
3903 ( salt_length + label_length + hash_length < hash_length );
3904 if( overflow )
3905 return( PSA_ERROR_INVALID_ARGUMENT );
3906
3907 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3908 if( tls12_prf->Ai_with_seed == NULL )
3909 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3910 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3911
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003912 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3913 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003914 if( label_length != 0 )
3915 {
3916 memcpy( tls12_prf->Ai_with_seed + hash_length,
3917 label, label_length );
3918 }
3919
3920 if( salt_length != 0 )
3921 {
3922 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3923 salt, salt_length );
3924 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003925
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003926 /* The first block gets generated when
3927 * psa_generator_read() is called. */
3928 tls12_prf->block_number = 0;
3929 tls12_prf->offset_in_block = hash_length;
3930
3931 return( PSA_SUCCESS );
3932}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003933
3934/* Set up a TLS-1.2-PSK-to-MS-based generator. */
3935static psa_status_t psa_generator_tls12_psk_to_ms_setup(
3936 psa_tls12_prf_generator_t *tls12_prf,
3937 const unsigned char *psk,
3938 size_t psk_len,
3939 psa_algorithm_t hash_alg,
3940 const uint8_t *salt,
3941 size_t salt_length,
3942 const uint8_t *label,
3943 size_t label_length )
3944{
3945 psa_status_t status;
3946 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
3947
3948 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
3949 return( PSA_ERROR_INVALID_ARGUMENT );
3950
3951 /* Quoting RFC 4279, Section 2:
3952 *
3953 * The premaster secret is formed as follows: if the PSK is N octets
3954 * long, concatenate a uint16 with the value N, N zero octets, a second
3955 * uint16 with the value N, and the PSK itself.
3956 */
3957
3958 pms[0] = ( psk_len >> 8 ) & 0xff;
3959 pms[1] = ( psk_len >> 0 ) & 0xff;
3960 memset( pms + 2, 0, psk_len );
3961 pms[2 + psk_len + 0] = pms[0];
3962 pms[2 + psk_len + 1] = pms[1];
3963 memcpy( pms + 4 + psk_len, psk, psk_len );
3964
3965 status = psa_generator_tls12_prf_setup( tls12_prf,
3966 pms, 4 + 2 * psk_len,
3967 hash_alg,
3968 salt, salt_length,
3969 label, label_length );
3970
Gilles Peskine3f108122018-12-07 18:14:53 +01003971 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00003972 return( status );
3973}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003974#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003975
Gilles Peskine346797d2018-11-16 16:05:06 +01003976/* Note that if this function fails, you must call psa_generator_abort()
3977 * to potentially free embedded data structures and wipe confidential data.
3978 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003979static psa_status_t psa_key_derivation_internal(
3980 psa_crypto_generator_t *generator,
3981 const uint8_t *secret, size_t secret_length,
3982 psa_algorithm_t alg,
3983 const uint8_t *salt, size_t salt_length,
3984 const uint8_t *label, size_t label_length,
3985 size_t capacity )
3986{
3987 psa_status_t status;
3988 size_t max_capacity;
3989
3990 /* Set generator->alg even on failure so that abort knows what to do. */
3991 generator->alg = alg;
3992
Gilles Peskine751d9652018-09-18 12:05:44 +02003993 if( alg == PSA_ALG_SELECT_RAW )
3994 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003995 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02003996 if( salt_length != 0 )
3997 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01003998 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02003999 if( label_length != 0 )
4000 return( PSA_ERROR_INVALID_ARGUMENT );
4001 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4002 if( generator->ctx.buffer.data == NULL )
4003 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4004 memcpy( generator->ctx.buffer.data, secret, secret_length );
4005 generator->ctx.buffer.size = secret_length;
4006 max_capacity = secret_length;
4007 status = PSA_SUCCESS;
4008 }
4009 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004010#if defined(MBEDTLS_MD_C)
4011 if( PSA_ALG_IS_HKDF( alg ) )
4012 {
4013 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4014 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4015 if( hash_size == 0 )
4016 return( PSA_ERROR_NOT_SUPPORTED );
4017 max_capacity = 255 * hash_size;
4018 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4019 secret, secret_length,
4020 hash_alg,
4021 salt, salt_length,
4022 label, label_length );
4023 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004024 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4025 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4026 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004027 {
4028 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4029 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4030
4031 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4032 if( hash_alg != PSA_ALG_SHA_256 &&
4033 hash_alg != PSA_ALG_SHA_384 )
4034 {
4035 return( PSA_ERROR_NOT_SUPPORTED );
4036 }
4037
4038 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004039
4040 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4041 {
4042 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4043 secret, secret_length,
4044 hash_alg, salt, salt_length,
4045 label, label_length );
4046 }
4047 else
4048 {
4049 status = psa_generator_tls12_psk_to_ms_setup(
4050 &generator->ctx.tls12_prf,
4051 secret, secret_length,
4052 hash_alg, salt, salt_length,
4053 label, label_length );
4054 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004055 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004056 else
4057#endif
4058 {
4059 return( PSA_ERROR_NOT_SUPPORTED );
4060 }
4061
4062 if( status != PSA_SUCCESS )
4063 return( status );
4064
4065 if( capacity <= max_capacity )
4066 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004067 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4068 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004069 else
4070 return( PSA_ERROR_INVALID_ARGUMENT );
4071
4072 return( PSA_SUCCESS );
4073}
4074
Gilles Peskineea0fb492018-07-12 17:17:20 +02004075psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004076 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004077 psa_algorithm_t alg,
4078 const uint8_t *salt,
4079 size_t salt_length,
4080 const uint8_t *label,
4081 size_t label_length,
4082 size_t capacity )
4083{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004084 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004085 psa_status_t status;
4086
4087 if( generator->alg != 0 )
4088 return( PSA_ERROR_BAD_STATE );
4089
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004090 /* Make sure that alg is a key derivation algorithm. This prevents
4091 * key selection algorithms, which psa_key_derivation_internal
4092 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004093 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4094 return( PSA_ERROR_INVALID_ARGUMENT );
4095
Gilles Peskinec5487a82018-12-03 18:08:14 +01004096 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004097 if( status != PSA_SUCCESS )
4098 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004099
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004100 if( slot->type != PSA_KEY_TYPE_DERIVE )
4101 return( PSA_ERROR_INVALID_ARGUMENT );
4102
4103 status = psa_key_derivation_internal( generator,
4104 slot->data.raw.data,
4105 slot->data.raw.bytes,
4106 alg,
4107 salt, salt_length,
4108 label, label_length,
4109 capacity );
4110 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004111 psa_generator_abort( generator );
4112 return( status );
4113}
4114
4115
4116
4117/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004118/* Key agreement */
4119/****************************************************************/
4120
Gilles Peskinea05219c2018-11-16 16:02:56 +01004121#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004122static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4123 size_t peer_key_length,
4124 const mbedtls_ecp_keypair *our_key,
4125 uint8_t *shared_secret,
4126 size_t shared_secret_size,
4127 size_t *shared_secret_length )
4128{
4129 mbedtls_pk_context pk;
4130 mbedtls_ecp_keypair *their_key = NULL;
4131 mbedtls_ecdh_context ecdh;
4132 int ret;
4133 mbedtls_ecdh_init( &ecdh );
4134 mbedtls_pk_init( &pk );
4135
4136 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
4137 if( ret != 0 )
4138 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02004139 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004140 {
Gilles Peskine88714d72018-10-25 23:07:25 +02004141 case MBEDTLS_PK_ECKEY:
4142 case MBEDTLS_PK_ECKEY_DH:
4143 break;
4144 default:
4145 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4146 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004147 }
4148 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01004149 if( their_key->grp.id != our_key->grp.id )
4150 {
4151 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4152 goto exit;
4153 }
4154
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004155 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
4156 if( ret != 0 )
4157 goto exit;
4158 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
4159 if( ret != 0 )
4160 goto exit;
4161
4162 ret = mbedtls_ecdh_calc_secret( &ecdh,
4163 shared_secret_length,
4164 shared_secret, shared_secret_size,
4165 mbedtls_ctr_drbg_random,
4166 &global_data.ctr_drbg );
4167
4168exit:
4169 mbedtls_pk_free( &pk );
4170 mbedtls_ecdh_free( &ecdh );
4171 return( mbedtls_to_psa_error( ret ) );
4172}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004173#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004174
Gilles Peskine01d718c2018-09-18 12:01:02 +02004175#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4176
Gilles Peskine346797d2018-11-16 16:05:06 +01004177/* Note that if this function fails, you must call psa_generator_abort()
4178 * to potentially free embedded data structures and wipe confidential data.
4179 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004180static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004181 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004182 const uint8_t *peer_key,
4183 size_t peer_key_length,
4184 psa_algorithm_t alg )
4185{
4186 psa_status_t status;
4187 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4188 size_t shared_secret_length = 0;
4189
4190 /* Step 1: run the secret agreement algorithm to generate the shared
4191 * secret. */
4192 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4193 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004194#if defined(MBEDTLS_ECDH_C)
4195 case PSA_ALG_ECDH_BASE:
4196 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4197 return( PSA_ERROR_INVALID_ARGUMENT );
4198 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4199 private_key->data.ecp,
4200 shared_secret,
4201 sizeof( shared_secret ),
4202 &shared_secret_length );
4203 break;
4204#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004205 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004206 (void) private_key;
4207 (void) peer_key;
4208 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004209 return( PSA_ERROR_NOT_SUPPORTED );
4210 }
4211 if( status != PSA_SUCCESS )
4212 goto exit;
4213
4214 /* Step 2: set up the key derivation to generate key material from
4215 * the shared secret. */
4216 status = psa_key_derivation_internal( generator,
4217 shared_secret, shared_secret_length,
4218 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4219 NULL, 0, NULL, 0,
4220 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4221exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004222 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004223 return( status );
4224}
4225
4226psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004227 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004228 const uint8_t *peer_key,
4229 size_t peer_key_length,
4230 psa_algorithm_t alg )
4231{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004232 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004233 psa_status_t status;
4234 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4235 return( PSA_ERROR_INVALID_ARGUMENT );
4236 status = psa_get_key_from_slot( private_key, &slot,
4237 PSA_KEY_USAGE_DERIVE, alg );
4238 if( status != PSA_SUCCESS )
4239 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004240 status = psa_key_agreement_internal( generator,
4241 slot,
4242 peer_key, peer_key_length,
4243 alg );
4244 if( status != PSA_SUCCESS )
4245 psa_generator_abort( generator );
4246 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004247}
4248
4249
4250
4251/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004252/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004253/****************************************************************/
4254
4255psa_status_t psa_generate_random( uint8_t *output,
4256 size_t output_size )
4257{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004258 int ret;
4259 GUARD_MODULE_INITIALIZED;
4260
4261 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004262 return( mbedtls_to_psa_error( ret ) );
4263}
4264
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004265#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004266
4267/* Support function for error conversion between psa_its error codes to psa crypto */
4268static psa_status_t its_to_psa_error( psa_its_status_t ret )
4269{
4270 switch( ret )
4271 {
4272 case PSA_ITS_SUCCESS:
4273 return( PSA_SUCCESS );
4274
4275 case PSA_ITS_ERROR_KEY_NOT_FOUND:
4276 return( PSA_ERROR_EMPTY_SLOT );
4277
4278 case PSA_ITS_ERROR_STORAGE_FAILURE:
4279 return( PSA_ERROR_STORAGE_FAILURE );
4280
4281 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4282 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4283
4284 case PSA_ITS_ERROR_INVALID_KEY:
4285 case PSA_PS_ERROR_OFFSET_INVALID:
4286 case PSA_ITS_ERROR_INCORRECT_SIZE:
4287 case PSA_ITS_ERROR_BAD_POINTER:
4288 return( PSA_ERROR_INVALID_ARGUMENT );
4289
4290 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4291 return( PSA_ERROR_NOT_SUPPORTED );
4292
4293 case PSA_ITS_ERROR_WRITE_ONCE:
4294 return( PSA_ERROR_OCCUPIED_SLOT );
4295
4296 default:
4297 return( PSA_ERROR_UNKNOWN_ERROR );
4298 }
4299}
4300
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004301psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4302 size_t seed_size )
4303{
4304 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004305 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004306 struct psa_its_info_t p_info;
4307 if( global_data.initialized )
4308 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004309
4310 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4311 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4312 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4313 return( PSA_ERROR_INVALID_ARGUMENT );
4314
avolinski0d2c2662018-11-21 17:31:07 +02004315 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004316 status = its_to_psa_error( its_status );
4317
4318 if( PSA_ITS_ERROR_KEY_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004319 {
avolinski0d2c2662018-11-21 17:31:07 +02004320 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004321 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004322 }
avolinski13beb102018-11-20 16:51:49 +02004323 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004324 {
4325 /* You should not be here. Seed needs to be injected only once */
4326 status = PSA_ERROR_NOT_PERMITTED;
4327 }
4328 return( status );
4329}
4330#endif
4331
Gilles Peskinec5487a82018-12-03 18:08:14 +01004332psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004333 psa_key_type_t type,
4334 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004335 const void *extra,
4336 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004337{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004338 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004339 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004340
Gilles Peskine53d991e2018-07-12 01:14:59 +02004341 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004342 return( PSA_ERROR_INVALID_ARGUMENT );
4343
Gilles Peskinec5487a82018-12-03 18:08:14 +01004344 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004345 if( status != PSA_SUCCESS )
4346 return( status );
4347
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004348 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004349 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004350 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004351 if( status != PSA_SUCCESS )
4352 return( status );
4353 status = psa_generate_random( slot->data.raw.data,
4354 slot->data.raw.bytes );
4355 if( status != PSA_SUCCESS )
4356 {
4357 mbedtls_free( slot->data.raw.data );
4358 return( status );
4359 }
4360#if defined(MBEDTLS_DES_C)
4361 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004362 psa_des_set_key_parity( slot->data.raw.data,
4363 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004364#endif /* MBEDTLS_DES_C */
4365 }
4366 else
4367
4368#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4369 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4370 {
4371 mbedtls_rsa_context *rsa;
4372 int ret;
4373 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004374 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4375 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004376 /* Accept only byte-aligned keys, for the same reasons as
4377 * in psa_import_rsa_key(). */
4378 if( bits % 8 != 0 )
4379 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004380 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004381 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004382 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004383 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004384 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004385#if INT_MAX < 0xffffffff
4386 /* Check that the uint32_t value passed by the caller fits
4387 * in the range supported by this implementation. */
4388 if( p->e > INT_MAX )
4389 return( PSA_ERROR_NOT_SUPPORTED );
4390#endif
4391 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004392 }
4393 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4394 if( rsa == NULL )
4395 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4396 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4397 ret = mbedtls_rsa_gen_key( rsa,
4398 mbedtls_ctr_drbg_random,
4399 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004400 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004401 exponent );
4402 if( ret != 0 )
4403 {
4404 mbedtls_rsa_free( rsa );
4405 mbedtls_free( rsa );
4406 return( mbedtls_to_psa_error( ret ) );
4407 }
4408 slot->data.rsa = rsa;
4409 }
4410 else
4411#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4412
4413#if defined(MBEDTLS_ECP_C)
4414 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4415 {
4416 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4417 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4418 const mbedtls_ecp_curve_info *curve_info =
4419 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4420 mbedtls_ecp_keypair *ecp;
4421 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004422 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004423 return( PSA_ERROR_NOT_SUPPORTED );
4424 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4425 return( PSA_ERROR_NOT_SUPPORTED );
4426 if( curve_info->bit_size != bits )
4427 return( PSA_ERROR_INVALID_ARGUMENT );
4428 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4429 if( ecp == NULL )
4430 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4431 mbedtls_ecp_keypair_init( ecp );
4432 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4433 mbedtls_ctr_drbg_random,
4434 &global_data.ctr_drbg );
4435 if( ret != 0 )
4436 {
4437 mbedtls_ecp_keypair_free( ecp );
4438 mbedtls_free( ecp );
4439 return( mbedtls_to_psa_error( ret ) );
4440 }
4441 slot->data.ecp = ecp;
4442 }
4443 else
4444#endif /* MBEDTLS_ECP_C */
4445
4446 return( PSA_ERROR_NOT_SUPPORTED );
4447
4448 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004449
4450#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4451 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4452 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004453 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004454 }
4455#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4456
4457 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004458}
4459
4460
4461/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004462/* Module setup */
4463/****************************************************************/
4464
Gilles Peskine5e769522018-11-20 21:59:56 +01004465psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4466 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4467 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4468{
4469 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4470 return( PSA_ERROR_BAD_STATE );
4471 global_data.entropy_init = entropy_init;
4472 global_data.entropy_free = entropy_free;
4473 return( PSA_SUCCESS );
4474}
4475
Gilles Peskinee59236f2018-01-27 23:32:46 +01004476void mbedtls_psa_crypto_free( void )
4477{
Gilles Peskinec6b69072018-11-20 21:42:52 +01004478 if( global_data.key_slots_initialized )
Darryl Green40225ba2018-11-15 14:48:15 +00004479 {
Gilles Peskinec5487a82018-12-03 18:08:14 +01004480 psa_key_handle_t key;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004481 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
4482 {
Gilles Peskine2f060a82018-12-04 17:12:32 +01004483 psa_key_slot_t *slot = &global_data.key_slots[key - 1];
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01004484 (void) psa_wipe_key_slot( slot );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004485 }
Darryl Green40225ba2018-11-15 14:48:15 +00004486 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01004487 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4488 {
4489 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004490 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004491 }
4492 /* Wipe all remaining data, including configuration.
4493 * In particular, this sets all state indicator to the value
4494 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004495 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004496}
4497
4498psa_status_t psa_crypto_init( void )
4499{
4500 int ret;
4501 const unsigned char drbg_seed[] = "PSA";
4502
Gilles Peskinec6b69072018-11-20 21:42:52 +01004503 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004504 if( global_data.initialized != 0 )
4505 return( PSA_SUCCESS );
4506
Gilles Peskine5e769522018-11-20 21:59:56 +01004507 /* Set default configuration if
4508 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4509 if( global_data.entropy_init == NULL )
4510 global_data.entropy_init = mbedtls_entropy_init;
4511 if( global_data.entropy_free == NULL )
4512 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004513
Gilles Peskinec6b69072018-11-20 21:42:52 +01004514 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004515 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004516 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004517 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004518 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4519 mbedtls_entropy_func,
4520 &global_data.entropy,
4521 drbg_seed, sizeof( drbg_seed ) - 1 );
4522 if( ret != 0 )
4523 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004524 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004525
Gilles Peskinec6b69072018-11-20 21:42:52 +01004526 /* Initialize the key slots. Zero-initialization has made all key
Gilles Peskinefe9756b2018-12-07 18:12:28 +01004527 * slots empty, so there is nothing to do. */
Gilles Peskinec6b69072018-11-20 21:42:52 +01004528 global_data.key_slots_initialized = 1;
4529
4530 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004531 global_data.initialized = 1;
4532
Gilles Peskinee59236f2018-01-27 23:32:46 +01004533exit:
4534 if( ret != 0 )
4535 mbedtls_psa_crypto_free( );
4536 return( mbedtls_to_psa_error( ret ) );
4537}
4538
4539#endif /* MBEDTLS_PSA_CRYPTO_C */