blob: 3e08657db21ae7e705229859bcc62d9c47a97b2c [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 Peskine5e769522018-11-20 21:59:56 +010046#include "psa_crypto_invasive.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010047#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010048/* Include internal declarations that are useful for implementing persistently
49 * stored keys. */
50#include "psa_crypto_storage.h"
51
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010052#include <stdlib.h>
53#include <string.h>
54#if defined(MBEDTLS_PLATFORM_C)
55#include "mbedtls/platform.h"
56#else
57#define mbedtls_calloc calloc
58#define mbedtls_free free
59#endif
60
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020062#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020063#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/blowfish.h"
65#include "mbedtls/camellia.h"
66#include "mbedtls/cipher.h"
67#include "mbedtls/ccm.h"
68#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010069#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020071#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010072#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010073#include "mbedtls/entropy.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020074#include "mbedtls/entropy_poll.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010075#include "mbedtls/error.h"
76#include "mbedtls/gcm.h"
77#include "mbedtls/md2.h"
78#include "mbedtls/md4.h"
79#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010080#include "mbedtls/md.h"
81#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010082#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010083#include "mbedtls/pk_internal.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010084#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010085#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086#include "mbedtls/sha1.h"
87#include "mbedtls/sha256.h"
88#include "mbedtls/sha512.h"
89#include "mbedtls/xtea.h"
90
Netanel Gonen2bcd3122018-11-19 11:53:02 +020091#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
92#include "psa_prot_internal_storage.h"
93#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +010094
Gilles Peskine996deb12018-08-01 15:45:45 +020095#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
96
Gilles Peskinee59236f2018-01-27 23:32:46 +010097/* Implementation that should never be optimized out by the compiler */
98static void mbedtls_zeroize( void *v, size_t n )
99{
100 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
101}
102
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100103/* constant-time buffer comparison */
104static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
105{
106 size_t i;
107 unsigned char diff = 0;
108
109 for( i = 0; i < n; i++ )
110 diff |= a[i] ^ b[i];
111
112 return( diff );
113}
114
115
116
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100117/****************************************************************/
118/* Global data, support functions and library management */
119/****************************************************************/
120
Gilles Peskine2d277862018-06-18 15:41:12 +0200121typedef struct
122{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100123 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +0300124 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200125 psa_key_lifetime_t lifetime;
Gilles Peskine69f976b2018-11-30 18:46:56 +0100126 psa_key_id_t persistent_storage_id;
Gilles Peskine961849f2018-11-30 18:54:54 +0100127 unsigned allocated : 1;
Gilles Peskine2d277862018-06-18 15:41:12 +0200128 union
129 {
130 struct raw_data
131 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100132 uint8_t *data;
133 size_t bytes;
134 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100135#if defined(MBEDTLS_RSA_C)
136 mbedtls_rsa_context *rsa;
137#endif /* MBEDTLS_RSA_C */
138#if defined(MBEDTLS_ECP_C)
139 mbedtls_ecp_keypair *ecp;
140#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100141 } data;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100142} psa_key_slot_t;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100143
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200144static int key_type_is_raw_bytes( psa_key_type_t type )
145{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200146 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200147}
148
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100149/* Values for psa_global_data_t::rng_state */
150#define RNG_NOT_INITIALIZED 0
151#define RNG_INITIALIZED 1
152#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100153
Gilles Peskine2d277862018-06-18 15:41:12 +0200154typedef struct
155{
Gilles Peskine5e769522018-11-20 21:59:56 +0100156 void (* entropy_init )( mbedtls_entropy_context *ctx );
157 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100158 mbedtls_entropy_context entropy;
159 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100160 psa_key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinec6b69072018-11-20 21:42:52 +0100161 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100162 unsigned rng_state : 2;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100163 unsigned key_slots_initialized : 1;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100164} psa_global_data_t;
165
166static psa_global_data_t global_data;
167
itayzafrir0adf0fc2018-09-06 16:24:41 +0300168#define GUARD_MODULE_INITIALIZED \
169 if( global_data.initialized == 0 ) \
170 return( PSA_ERROR_BAD_STATE );
171
Gilles Peskinee59236f2018-01-27 23:32:46 +0100172static psa_status_t mbedtls_to_psa_error( int ret )
173{
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 /* If there's both a high-level code and low-level code, dispatch on
175 * the high-level code. */
176 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100177 {
178 case 0:
179 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100180
181 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
182 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
183 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
184 return( PSA_ERROR_NOT_SUPPORTED );
185 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
188 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
189 return( PSA_ERROR_HARDWARE_FAILURE );
190
Gilles Peskine9a944802018-06-21 09:35:35 +0200191 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
192 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
193 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
194 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
195 case MBEDTLS_ERR_ASN1_INVALID_DATA:
196 return( PSA_ERROR_INVALID_ARGUMENT );
197 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
198 return( PSA_ERROR_INSUFFICIENT_MEMORY );
199 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
200 return( PSA_ERROR_BUFFER_TOO_SMALL );
201
Gilles Peskinea5905292018-02-07 20:59:33 +0100202 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
203 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
204 return( PSA_ERROR_NOT_SUPPORTED );
205 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
206 return( PSA_ERROR_HARDWARE_FAILURE );
207
208 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
209 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
210 return( PSA_ERROR_NOT_SUPPORTED );
211 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
214 case MBEDTLS_ERR_CCM_BAD_INPUT:
215 return( PSA_ERROR_INVALID_ARGUMENT );
216 case MBEDTLS_ERR_CCM_AUTH_FAILED:
217 return( PSA_ERROR_INVALID_SIGNATURE );
218 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
219 return( PSA_ERROR_HARDWARE_FAILURE );
220
221 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
222 return( PSA_ERROR_NOT_SUPPORTED );
223 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
224 return( PSA_ERROR_INVALID_ARGUMENT );
225 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
226 return( PSA_ERROR_INSUFFICIENT_MEMORY );
227 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
228 return( PSA_ERROR_INVALID_PADDING );
229 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
230 return( PSA_ERROR_BAD_STATE );
231 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
232 return( PSA_ERROR_INVALID_SIGNATURE );
233 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
234 return( PSA_ERROR_TAMPERING_DETECTED );
235 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
236 return( PSA_ERROR_HARDWARE_FAILURE );
237
238 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
241 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
242 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
243 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
244 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
245 return( PSA_ERROR_NOT_SUPPORTED );
246 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
247 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
248
249 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
250 return( PSA_ERROR_NOT_SUPPORTED );
251 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
Gilles Peskinee59236f2018-01-27 23:32:46 +0100254 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
255 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
256 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
257 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100258
259 case MBEDTLS_ERR_GCM_AUTH_FAILED:
260 return( PSA_ERROR_INVALID_SIGNATURE );
261 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200262 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100263 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
264 return( PSA_ERROR_HARDWARE_FAILURE );
265
266 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
267 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
268 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
269 return( PSA_ERROR_HARDWARE_FAILURE );
270
271 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
272 return( PSA_ERROR_NOT_SUPPORTED );
273 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_MD_ALLOC_FAILED:
276 return( PSA_ERROR_INSUFFICIENT_MEMORY );
277 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
278 return( PSA_ERROR_STORAGE_FAILURE );
279 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
280 return( PSA_ERROR_HARDWARE_FAILURE );
281
Gilles Peskinef76aa772018-10-29 19:24:33 +0100282 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
283 return( PSA_ERROR_STORAGE_FAILURE );
284 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
285 return( PSA_ERROR_INVALID_ARGUMENT );
286 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
289 return( PSA_ERROR_BUFFER_TOO_SMALL );
290 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
293 return( PSA_ERROR_INVALID_ARGUMENT );
294 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
295 return( PSA_ERROR_INVALID_ARGUMENT );
296 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
297 return( PSA_ERROR_INSUFFICIENT_MEMORY );
298
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100299 case MBEDTLS_ERR_PK_ALLOC_FAILED:
300 return( PSA_ERROR_INSUFFICIENT_MEMORY );
301 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
302 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
303 return( PSA_ERROR_INVALID_ARGUMENT );
304 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100306 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
307 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
310 return( PSA_ERROR_NOT_SUPPORTED );
311 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
312 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
313 return( PSA_ERROR_NOT_PERMITTED );
314 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
315 return( PSA_ERROR_INVALID_ARGUMENT );
316 case MBEDTLS_ERR_PK_INVALID_ALG:
317 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
318 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
319 return( PSA_ERROR_NOT_SUPPORTED );
320 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
321 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100322 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
323 return( PSA_ERROR_HARDWARE_FAILURE );
324
325 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327
328 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
329 return( PSA_ERROR_INVALID_ARGUMENT );
330 case MBEDTLS_ERR_RSA_INVALID_PADDING:
331 return( PSA_ERROR_INVALID_PADDING );
332 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
333 return( PSA_ERROR_HARDWARE_FAILURE );
334 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
335 return( PSA_ERROR_INVALID_ARGUMENT );
336 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
337 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
338 return( PSA_ERROR_TAMPERING_DETECTED );
339 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
340 return( PSA_ERROR_INVALID_SIGNATURE );
341 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
342 return( PSA_ERROR_BUFFER_TOO_SMALL );
343 case MBEDTLS_ERR_RSA_RNG_FAILED:
344 return( PSA_ERROR_INSUFFICIENT_MEMORY );
345 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
346 return( PSA_ERROR_NOT_SUPPORTED );
347 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
348 return( PSA_ERROR_HARDWARE_FAILURE );
349
350 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
351 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
352 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
353 return( PSA_ERROR_HARDWARE_FAILURE );
354
355 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
356 return( PSA_ERROR_INVALID_ARGUMENT );
357 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
358 return( PSA_ERROR_HARDWARE_FAILURE );
359
itayzafrir5c753392018-05-08 11:18:38 +0300360 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300361 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300362 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300363 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
364 return( PSA_ERROR_BUFFER_TOO_SMALL );
365 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
366 return( PSA_ERROR_NOT_SUPPORTED );
367 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
368 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
369 return( PSA_ERROR_INVALID_SIGNATURE );
370 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
371 return( PSA_ERROR_INSUFFICIENT_MEMORY );
372 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
373 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300374
Gilles Peskinee59236f2018-01-27 23:32:46 +0100375 default:
376 return( PSA_ERROR_UNKNOWN_ERROR );
377 }
378}
379
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200380
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200381
382
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100383/****************************************************************/
384/* Key management */
385/****************************************************************/
386
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100387#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200388static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
389{
390 switch( grpid )
391 {
392 case MBEDTLS_ECP_DP_SECP192R1:
393 return( PSA_ECC_CURVE_SECP192R1 );
394 case MBEDTLS_ECP_DP_SECP224R1:
395 return( PSA_ECC_CURVE_SECP224R1 );
396 case MBEDTLS_ECP_DP_SECP256R1:
397 return( PSA_ECC_CURVE_SECP256R1 );
398 case MBEDTLS_ECP_DP_SECP384R1:
399 return( PSA_ECC_CURVE_SECP384R1 );
400 case MBEDTLS_ECP_DP_SECP521R1:
401 return( PSA_ECC_CURVE_SECP521R1 );
402 case MBEDTLS_ECP_DP_BP256R1:
403 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
404 case MBEDTLS_ECP_DP_BP384R1:
405 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
406 case MBEDTLS_ECP_DP_BP512R1:
407 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
408 case MBEDTLS_ECP_DP_CURVE25519:
409 return( PSA_ECC_CURVE_CURVE25519 );
410 case MBEDTLS_ECP_DP_SECP192K1:
411 return( PSA_ECC_CURVE_SECP192K1 );
412 case MBEDTLS_ECP_DP_SECP224K1:
413 return( PSA_ECC_CURVE_SECP224K1 );
414 case MBEDTLS_ECP_DP_SECP256K1:
415 return( PSA_ECC_CURVE_SECP256K1 );
416 case MBEDTLS_ECP_DP_CURVE448:
417 return( PSA_ECC_CURVE_CURVE448 );
418 default:
419 return( 0 );
420 }
421}
422
Gilles Peskine12313cd2018-06-20 00:20:32 +0200423static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
424{
425 switch( curve )
426 {
427 case PSA_ECC_CURVE_SECP192R1:
428 return( MBEDTLS_ECP_DP_SECP192R1 );
429 case PSA_ECC_CURVE_SECP224R1:
430 return( MBEDTLS_ECP_DP_SECP224R1 );
431 case PSA_ECC_CURVE_SECP256R1:
432 return( MBEDTLS_ECP_DP_SECP256R1 );
433 case PSA_ECC_CURVE_SECP384R1:
434 return( MBEDTLS_ECP_DP_SECP384R1 );
435 case PSA_ECC_CURVE_SECP521R1:
436 return( MBEDTLS_ECP_DP_SECP521R1 );
437 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
438 return( MBEDTLS_ECP_DP_BP256R1 );
439 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
440 return( MBEDTLS_ECP_DP_BP384R1 );
441 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
442 return( MBEDTLS_ECP_DP_BP512R1 );
443 case PSA_ECC_CURVE_CURVE25519:
444 return( MBEDTLS_ECP_DP_CURVE25519 );
445 case PSA_ECC_CURVE_SECP192K1:
446 return( MBEDTLS_ECP_DP_SECP192K1 );
447 case PSA_ECC_CURVE_SECP224K1:
448 return( MBEDTLS_ECP_DP_SECP224K1 );
449 case PSA_ECC_CURVE_SECP256K1:
450 return( MBEDTLS_ECP_DP_SECP256K1 );
451 case PSA_ECC_CURVE_CURVE448:
452 return( MBEDTLS_ECP_DP_CURVE448 );
453 default:
454 return( MBEDTLS_ECP_DP_NONE );
455 }
456}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100457#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200458
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200459static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
460 size_t bits,
461 struct raw_data *raw )
462{
463 /* Check that the bit size is acceptable for the key type */
464 switch( type )
465 {
466 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200467 if( bits == 0 )
468 {
469 raw->bytes = 0;
470 raw->data = NULL;
471 return( PSA_SUCCESS );
472 }
473 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200474#if defined(MBEDTLS_MD_C)
475 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200476#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200477 case PSA_KEY_TYPE_DERIVE:
478 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200479#if defined(MBEDTLS_AES_C)
480 case PSA_KEY_TYPE_AES:
481 if( bits != 128 && bits != 192 && bits != 256 )
482 return( PSA_ERROR_INVALID_ARGUMENT );
483 break;
484#endif
485#if defined(MBEDTLS_CAMELLIA_C)
486 case PSA_KEY_TYPE_CAMELLIA:
487 if( bits != 128 && bits != 192 && bits != 256 )
488 return( PSA_ERROR_INVALID_ARGUMENT );
489 break;
490#endif
491#if defined(MBEDTLS_DES_C)
492 case PSA_KEY_TYPE_DES:
493 if( bits != 64 && bits != 128 && bits != 192 )
494 return( PSA_ERROR_INVALID_ARGUMENT );
495 break;
496#endif
497#if defined(MBEDTLS_ARC4_C)
498 case PSA_KEY_TYPE_ARC4:
499 if( bits < 8 || bits > 2048 )
500 return( PSA_ERROR_INVALID_ARGUMENT );
501 break;
502#endif
503 default:
504 return( PSA_ERROR_NOT_SUPPORTED );
505 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200506 if( bits % 8 != 0 )
507 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200508
509 /* Allocate memory for the key */
510 raw->bytes = PSA_BITS_TO_BYTES( bits );
511 raw->data = mbedtls_calloc( 1, raw->bytes );
512 if( raw->data == NULL )
513 {
514 raw->bytes = 0;
515 return( PSA_ERROR_INSUFFICIENT_MEMORY );
516 }
517 return( PSA_SUCCESS );
518}
519
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200520#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100521/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
522 * that are not a multiple of 8) well. For example, there is only
523 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
524 * way to return the exact bit size of a key.
525 * To keep things simple, reject non-byte-aligned key sizes. */
526static psa_status_t psa_check_rsa_key_byte_aligned(
527 const mbedtls_rsa_context *rsa )
528{
529 mbedtls_mpi n;
530 psa_status_t status;
531 mbedtls_mpi_init( &n );
532 status = mbedtls_to_psa_error(
533 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
534 if( status == PSA_SUCCESS )
535 {
536 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
537 status = PSA_ERROR_NOT_SUPPORTED;
538 }
539 mbedtls_mpi_free( &n );
540 return( status );
541}
542
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200543static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
544 mbedtls_rsa_context **p_rsa )
545{
546 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
547 return( PSA_ERROR_INVALID_ARGUMENT );
548 else
549 {
550 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
Gilles Peskineaac64a22018-11-12 18:37:42 +0100551 /* The size of an RSA key doesn't have to be a multiple of 8.
552 * Mbed TLS supports non-byte-aligned key sizes, but not well.
553 * For example, mbedtls_rsa_get_len() returns the key size in
554 * bytes, not in bits. */
555 size_t bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100556 psa_status_t status;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200557 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
558 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100559 status = psa_check_rsa_key_byte_aligned( rsa );
560 if( status != PSA_SUCCESS )
561 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200562 *p_rsa = rsa;
563 return( PSA_SUCCESS );
564 }
565}
566#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
567
568#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100569/* Import an elliptic curve parsed by the mbedtls pk module. */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200570static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
571 mbedtls_pk_context *pk,
572 mbedtls_ecp_keypair **p_ecp )
573{
574 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
575 return( PSA_ERROR_INVALID_ARGUMENT );
576 else
577 {
578 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
579 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
580 if( actual_curve != expected_curve )
581 return( PSA_ERROR_INVALID_ARGUMENT );
582 *p_ecp = ecp;
583 return( PSA_SUCCESS );
584 }
585}
586#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
587
Gilles Peskinef76aa772018-10-29 19:24:33 +0100588#if defined(MBEDTLS_ECP_C)
589/* Import a private key given as a byte string which is the private value
590 * in big-endian order. */
591static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
592 const uint8_t *data,
593 size_t data_length,
594 mbedtls_ecp_keypair **p_ecp )
595{
596 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
597 mbedtls_ecp_keypair *ecp = NULL;
598 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
599
600 *p_ecp = NULL;
601 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
602 if( ecp == NULL )
603 return( PSA_ERROR_INSUFFICIENT_MEMORY );
604
605 /* Load the group. */
606 status = mbedtls_to_psa_error(
607 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
608 if( status != PSA_SUCCESS )
609 goto exit;
610 /* Load the secret value. */
611 status = mbedtls_to_psa_error(
612 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
613 if( status != PSA_SUCCESS )
614 goto exit;
615 /* Validate the private key. */
616 status = mbedtls_to_psa_error(
617 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
618 if( status != PSA_SUCCESS )
619 goto exit;
620 /* Calculate the public key from the private key. */
621 status = mbedtls_to_psa_error(
622 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
623 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
624 if( status != PSA_SUCCESS )
625 goto exit;
626
627 *p_ecp = ecp;
628 return( PSA_SUCCESS );
629
630exit:
631 if( ecp != NULL )
632 {
633 mbedtls_ecp_keypair_free( ecp );
634 mbedtls_free( ecp );
635 }
636 return( status );
637}
638#endif /* defined(MBEDTLS_ECP_C) */
639
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100640/** Import key data into a slot. `slot->type` must have been set
641 * previously. This function assumes that the slot does not contain
642 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100643static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
Darryl Green940d72c2018-07-13 13:18:51 +0100644 const uint8_t *data,
645 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100646{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200647 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100648
Darryl Green940d72c2018-07-13 13:18:51 +0100649 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100650 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100651 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100652 if( data_length > SIZE_MAX / 8 )
653 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100654 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200655 PSA_BYTES_TO_BITS( data_length ),
656 &slot->data.raw );
657 if( status != PSA_SUCCESS )
658 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200659 if( data_length != 0 )
660 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100661 }
662 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100663#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100664 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100665 {
Darryl Green940d72c2018-07-13 13:18:51 +0100666 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100667 data, data_length,
668 &slot->data.ecp );
669 if( status != PSA_SUCCESS )
670 return( status );
671 }
672 else
673#endif /* MBEDTLS_ECP_C */
Gilles Peskine969ac722018-01-28 18:16:59 +0100674#if defined(MBEDTLS_PK_PARSE_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100675 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
676 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100677 {
678 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100679 mbedtls_pk_context pk;
680 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200681
682 /* Parse the data. */
Darryl Green940d72c2018-07-13 13:18:51 +0100683 if( PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100684 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
685 else
686 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100687 if( ret != 0 )
688 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200689
690 /* We have something that the pkparse module recognizes.
691 * If it has the expected type and passes any type-specific
692 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100693#if defined(MBEDTLS_RSA_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100694 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200695 status = psa_import_rsa_key( &pk, &slot->data.rsa );
696 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100697#endif /* MBEDTLS_RSA_C */
698#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100699 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
700 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200701 &pk, &slot->data.ecp );
702 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100703#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200704 {
705 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200706 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200707
Gilles Peskinec648d692018-06-28 08:46:13 +0200708 /* Free the content of the pk object only on error. On success,
709 * the content of the object has been stored in the slot. */
710 if( status != PSA_SUCCESS )
711 {
712 mbedtls_pk_free( &pk );
713 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100714 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100715 }
716 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100717#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100718 {
719 return( PSA_ERROR_NOT_SUPPORTED );
720 }
Darryl Green940d72c2018-07-13 13:18:51 +0100721 return( PSA_SUCCESS );
722}
723
Darryl Greend49a4992018-06-18 17:27:26 +0100724#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +0100725static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *p_slot )
Darryl Greend49a4992018-06-18 17:27:26 +0100726{
727 psa_status_t status = PSA_SUCCESS;
728 uint8_t *key_data = NULL;
729 size_t key_data_length = 0;
730
Gilles Peskine69f976b2018-11-30 18:46:56 +0100731 status = psa_load_persistent_key( p_slot->persistent_storage_id,
732 &( p_slot )->type,
Darryl Greend49a4992018-06-18 17:27:26 +0100733 &( p_slot )->policy, &key_data,
734 &key_data_length );
735 if( status != PSA_SUCCESS )
736 goto exit;
737 status = psa_import_key_into_slot( p_slot,
738 key_data, key_data_length );
739exit:
740 psa_free_persistent_key_data( key_data, key_data_length );
741 return( status );
742}
743#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
744
Gilles Peskinec5487a82018-12-03 18:08:14 +0100745/* Access a key slot at the given handle. The handle of a key slot is
746 * the index of the slot in the global slot array, plus one so that handles
747 * start at 1 and not 0. */
748static psa_status_t psa_get_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100749 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100750{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100751 psa_key_slot_t *slot = NULL;
Gilles Peskine961849f2018-11-30 18:54:54 +0100752
Darryl Green06fd18d2018-07-16 11:21:11 +0100753 GUARD_MODULE_INITIALIZED;
754
Gilles Peskinec5487a82018-12-03 18:08:14 +0100755 /* 0 is not a valid handle under any circumstance. This
Darryl Green06fd18d2018-07-16 11:21:11 +0100756 * implementation provides slots number 1 to N where N is the
757 * number of available slots. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100758 if( handle == 0 || handle > ARRAY_LENGTH( global_data.key_slots ) )
759 return( PSA_ERROR_INVALID_HANDLE );
760 slot = &global_data.key_slots[handle - 1];
Darryl Green06fd18d2018-07-16 11:21:11 +0100761
Gilles Peskinec5487a82018-12-03 18:08:14 +0100762 /* If the slot hasn't been allocated, the handle is invalid. */
763 if( ! slot->allocated )
764 return( PSA_ERROR_INVALID_HANDLE );
Darryl Greend49a4992018-06-18 17:27:26 +0100765
Gilles Peskinec5487a82018-12-03 18:08:14 +0100766 *p_slot = slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100767 return( PSA_SUCCESS );
768}
769
770/* Retrieve an empty key slot (slot with no key data, but possibly
771 * with some metadata such as a policy). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100772static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100773 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100774{
775 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100776 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100777
778 *p_slot = NULL;
779
Gilles Peskinec5487a82018-12-03 18:08:14 +0100780 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100781 if( status != PSA_SUCCESS )
782 return( status );
783
784 if( slot->type != PSA_KEY_TYPE_NONE )
785 return( PSA_ERROR_OCCUPIED_SLOT );
786
787 *p_slot = slot;
788 return( status );
789}
790
791/** Retrieve a slot which must contain a key. The key must have allow all the
792 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
793 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100794static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100795 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100796 psa_key_usage_t usage,
797 psa_algorithm_t alg )
798{
799 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100800 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100801
802 *p_slot = NULL;
803
Gilles Peskinec5487a82018-12-03 18:08:14 +0100804 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100805 if( status != PSA_SUCCESS )
806 return( status );
807 if( slot->type == PSA_KEY_TYPE_NONE )
808 return( PSA_ERROR_EMPTY_SLOT );
809
810 /* Enforce that usage policy for the key slot contains all the flags
811 * required by the usage parameter. There is one exception: public
812 * keys can always be exported, so we treat public key objects as
813 * if they had the export flag. */
814 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
815 usage &= ~PSA_KEY_USAGE_EXPORT;
816 if( ( slot->policy.usage & usage ) != usage )
817 return( PSA_ERROR_NOT_PERMITTED );
818 if( alg != 0 && ( alg != slot->policy.alg ) )
819 return( PSA_ERROR_NOT_PERMITTED );
820
821 *p_slot = slot;
822 return( PSA_SUCCESS );
823}
Darryl Green940d72c2018-07-13 13:18:51 +0100824
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100825/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100826static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000827{
828 if( slot->type == PSA_KEY_TYPE_NONE )
829 {
830 /* No key material to clean. */
831 }
832 else if( key_type_is_raw_bytes( slot->type ) )
833 {
834 mbedtls_free( slot->data.raw.data );
835 }
836 else
837#if defined(MBEDTLS_RSA_C)
838 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
839 {
840 mbedtls_rsa_free( slot->data.rsa );
841 mbedtls_free( slot->data.rsa );
842 }
843 else
844#endif /* defined(MBEDTLS_RSA_C) */
845#if defined(MBEDTLS_ECP_C)
846 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
847 {
848 mbedtls_ecp_keypair_free( slot->data.ecp );
849 mbedtls_free( slot->data.ecp );
850 }
851 else
852#endif /* defined(MBEDTLS_ECP_C) */
853 {
854 /* Shouldn't happen: the key type is not any type that we
855 * put in. */
856 return( PSA_ERROR_TAMPERING_DETECTED );
857 }
858
859 return( PSA_SUCCESS );
860}
861
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100862/** Completely wipe a slot in memory, including its policy.
863 * Persistent storage is not affected. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100864static psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100865{
866 psa_status_t status = psa_remove_key_data_from_memory( slot );
867 /* At this point, key material and other type-specific content has
868 * been wiped. Clear remaining metadata. We can call memset and not
869 * zeroize because the metadata is not particularly sensitive. */
870 memset( slot, 0, sizeof( *slot ) );
871 return( status );
872}
873
Gilles Peskine961849f2018-11-30 18:54:54 +0100874psa_status_t psa_internal_allocate_key_slot( psa_key_handle_t *handle )
875{
Gilles Peskinec5487a82018-12-03 18:08:14 +0100876 for( *handle = PSA_KEY_SLOT_COUNT; *handle != 0; --( *handle ) )
Gilles Peskine961849f2018-11-30 18:54:54 +0100877 {
Gilles Peskine2f060a82018-12-04 17:12:32 +0100878 psa_key_slot_t *slot = &global_data.key_slots[*handle - 1];
Gilles Peskinec5487a82018-12-03 18:08:14 +0100879 if( ! slot->allocated )
Gilles Peskine961849f2018-11-30 18:54:54 +0100880 {
881 slot->allocated = 1;
Gilles Peskine961849f2018-11-30 18:54:54 +0100882 return( PSA_SUCCESS );
883 }
884 }
885 return( PSA_ERROR_INSUFFICIENT_MEMORY );
886}
887
888psa_status_t psa_internal_make_key_persistent( psa_key_handle_t handle,
889 psa_key_id_t id )
890{
Gilles Peskine4a044732018-12-03 18:19:39 +0100891#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +0100892 psa_key_slot_t *slot;
Gilles Peskine961849f2018-11-30 18:54:54 +0100893 psa_status_t status;
894
895 /* Reject id=0 because by general library conventions, 0 is an invalid
896 * value wherever possible. */
897 if( id == 0 )
898 return( PSA_ERROR_INVALID_ARGUMENT );
899 /* Reject high values because the file names are reserved for the
900 * library's internal use. */
901 if( id >= 0xffff0000 )
902 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine961849f2018-11-30 18:54:54 +0100903
904 status = psa_get_key_slot( handle, &slot );
905 if( status != PSA_SUCCESS )
906 return( status );
907
908 slot->lifetime = PSA_KEY_LIFETIME_PERSISTENT;
909 slot->persistent_storage_id = id;
910 status = psa_load_persistent_key_into_slot( slot );
911
912 return( status );
Gilles Peskine4a044732018-12-03 18:19:39 +0100913
914#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
915 (void) handle;
916 (void) id;
917 return( PSA_ERROR_NOT_SUPPORTED );
918#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
Gilles Peskine961849f2018-11-30 18:54:54 +0100919}
920
921psa_status_t psa_internal_release_key_slot( psa_key_handle_t handle )
922{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100923 psa_key_slot_t *slot;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100924 psa_status_t status;
925
926 status = psa_get_key_slot( handle, &slot );
927 if( status != PSA_SUCCESS )
928 return( status );
Gilles Peskine961849f2018-11-30 18:54:54 +0100929 if( ! slot->allocated )
930 return( PSA_ERROR_INVALID_HANDLE );
Gilles Peskinec5487a82018-12-03 18:08:14 +0100931
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100932 return( psa_wipe_key_slot( slot ) );
Gilles Peskine961849f2018-11-30 18:54:54 +0100933}
934
Gilles Peskinec5487a82018-12-03 18:08:14 +0100935psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100936 psa_key_type_t type,
937 const uint8_t *data,
938 size_t data_length )
939{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100940 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100941 psa_status_t status;
942
Gilles Peskinec5487a82018-12-03 18:08:14 +0100943 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100944 if( status != PSA_SUCCESS )
945 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100946
947 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100948
949 status = psa_import_key_into_slot( slot, data, data_length );
950 if( status != PSA_SUCCESS )
951 {
952 slot->type = PSA_KEY_TYPE_NONE;
953 return( status );
954 }
955
Darryl Greend49a4992018-06-18 17:27:26 +0100956#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
957 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
958 {
959 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100960 status = psa_save_persistent_key( slot->persistent_storage_id,
961 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100962 data_length );
963 if( status != PSA_SUCCESS )
964 {
965 (void) psa_remove_key_data_from_memory( slot );
966 slot->type = PSA_KEY_TYPE_NONE;
967 }
968 }
969#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
970
971 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100972}
973
Gilles Peskinec5487a82018-12-03 18:08:14 +0100974psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100975{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100976 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100977 psa_status_t status = PSA_SUCCESS;
978 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100979
Gilles Peskinec5487a82018-12-03 18:08:14 +0100980 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200981 if( status != PSA_SUCCESS )
982 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100983#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
984 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
985 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100986 storage_status =
987 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100988 }
989#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100990 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100991 if( status != PSA_SUCCESS )
992 return( status );
993 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100994}
995
Gilles Peskineb870b182018-07-06 16:02:09 +0200996/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100997static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200998{
999 if( key_type_is_raw_bytes( slot->type ) )
1000 return( slot->data.raw.bytes * 8 );
1001#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001002 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +01001003 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001004#endif /* defined(MBEDTLS_RSA_C) */
1005#if defined(MBEDTLS_ECP_C)
1006 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1007 return( slot->data.ecp->grp.pbits );
1008#endif /* defined(MBEDTLS_ECP_C) */
1009 /* Shouldn't happen except on an empty slot. */
1010 return( 0 );
1011}
1012
Gilles Peskinec5487a82018-12-03 18:08:14 +01001013psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001014 psa_key_type_t *type,
1015 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001016{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001017 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001018 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001019
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001020 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001021 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001022 if( bits != NULL )
1023 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +01001024 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001025 if( status != PSA_SUCCESS )
1026 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001027
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001028 if( slot->type == PSA_KEY_TYPE_NONE )
1029 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001030 if( type != NULL )
1031 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001032 if( bits != NULL )
1033 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001034 return( PSA_SUCCESS );
1035}
1036
Gilles Peskine2f060a82018-12-04 17:12:32 +01001037static psa_status_t psa_internal_export_key( psa_key_slot_t *slot,
Gilles Peskine2d277862018-06-18 15:41:12 +02001038 uint8_t *data,
1039 size_t data_size,
1040 size_t *data_length,
1041 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001042{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001043 *data_length = 0;
1044
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001045 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001046 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001047
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001048 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001049 {
1050 if( slot->data.raw.bytes > data_size )
1051 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001052 if( data_size != 0 )
1053 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001054 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001055 memset( data + slot->data.raw.bytes, 0,
1056 data_size - slot->data.raw.bytes );
1057 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001058 *data_length = slot->data.raw.bytes;
1059 return( PSA_SUCCESS );
1060 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001061#if defined(MBEDTLS_ECP_C)
1062 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1063 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001064 psa_status_t status;
1065
Gilles Peskine188c71e2018-10-29 19:26:02 +01001066 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1067 if( bytes > data_size )
1068 return( PSA_ERROR_BUFFER_TOO_SMALL );
1069 status = mbedtls_to_psa_error(
1070 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1071 if( status != PSA_SUCCESS )
1072 return( status );
1073 memset( data + bytes, 0, data_size - bytes );
1074 *data_length = bytes;
1075 return( PSA_SUCCESS );
1076 }
1077#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001078 else
Moran Peker17e36e12018-05-02 12:55:20 +03001079 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001080#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001081 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001082 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001083 {
Moran Pekera998bc62018-04-16 18:16:20 +03001084 mbedtls_pk_context pk;
1085 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001086 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001087 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001088#if defined(MBEDTLS_RSA_C)
1089 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001090 pk.pk_info = &mbedtls_rsa_info;
1091 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001092#else
1093 return( PSA_ERROR_NOT_SUPPORTED );
1094#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001095 }
1096 else
1097 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001098#if defined(MBEDTLS_ECP_C)
1099 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001100 pk.pk_info = &mbedtls_eckey_info;
1101 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001102#else
1103 return( PSA_ERROR_NOT_SUPPORTED );
1104#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001105 }
Moran Pekerd7326592018-05-29 16:56:39 +03001106 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001107 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +03001108 else
1109 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +03001110 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001111 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001112 /* If data_size is 0 then data may be NULL and then the
1113 * call to memset would have undefined behavior. */
1114 if( data_size != 0 )
1115 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001116 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001117 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001118 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1119 * Move the data to the beginning and erase remaining data
1120 * at the original location. */
1121 if( 2 * (size_t) ret <= data_size )
1122 {
1123 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001124 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001125 }
1126 else if( (size_t) ret < data_size )
1127 {
1128 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001129 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001130 }
Moran Pekera998bc62018-04-16 18:16:20 +03001131 *data_length = ret;
1132 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001133 }
1134 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001135#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001136 {
1137 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001138 it is valid for a special-purpose implementation to omit
1139 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001140 return( PSA_ERROR_NOT_SUPPORTED );
1141 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001142 }
1143}
1144
Gilles Peskinec5487a82018-12-03 18:08:14 +01001145psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001146 uint8_t *data,
1147 size_t data_size,
1148 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001149{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001150 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001151 psa_status_t status;
1152
1153 /* Set the key to empty now, so that even when there are errors, we always
1154 * set data_length to a value between 0 and data_size. On error, setting
1155 * the key to empty is a good choice because an empty key representation is
1156 * unlikely to be accepted anywhere. */
1157 *data_length = 0;
1158
1159 /* Export requires the EXPORT flag. There is an exception for public keys,
1160 * which don't require any flag, but psa_get_key_from_slot takes
1161 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001162 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001163 if( status != PSA_SUCCESS )
1164 return( status );
1165 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001166 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001167}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001168
Gilles Peskinec5487a82018-12-03 18:08:14 +01001169psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001170 uint8_t *data,
1171 size_t data_size,
1172 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001173{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001174 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001175 psa_status_t status;
1176
1177 /* Set the key to empty now, so that even when there are errors, we always
1178 * set data_length to a value between 0 and data_size. On error, setting
1179 * the key to empty is a good choice because an empty key representation is
1180 * unlikely to be accepted anywhere. */
1181 *data_length = 0;
1182
1183 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001184 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001185 if( status != PSA_SUCCESS )
1186 return( status );
1187 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001188 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001189}
1190
Darryl Green0c6575a2018-11-07 16:05:30 +00001191#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001192static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001193 size_t bits )
1194{
1195 psa_status_t status;
1196 uint8_t *data;
1197 size_t key_length;
1198 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1199 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001200 if( data == NULL )
1201 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001202 /* Get key data in export format */
1203 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1204 if( status != PSA_SUCCESS )
1205 {
1206 slot->type = PSA_KEY_TYPE_NONE;
1207 goto exit;
1208 }
1209 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001210 status = psa_save_persistent_key( slot->persistent_storage_id,
1211 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001212 data, key_length );
1213 if( status != PSA_SUCCESS )
1214 {
1215 slot->type = PSA_KEY_TYPE_NONE;
1216 }
1217exit:
1218 mbedtls_zeroize( data, key_length );
1219 mbedtls_free( data );
1220 return( status );
1221}
1222#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1223
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001224
1225
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001226/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001227/* Message digests */
1228/****************************************************************/
1229
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001230static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001231{
1232 switch( alg )
1233 {
1234#if defined(MBEDTLS_MD2_C)
1235 case PSA_ALG_MD2:
1236 return( &mbedtls_md2_info );
1237#endif
1238#if defined(MBEDTLS_MD4_C)
1239 case PSA_ALG_MD4:
1240 return( &mbedtls_md4_info );
1241#endif
1242#if defined(MBEDTLS_MD5_C)
1243 case PSA_ALG_MD5:
1244 return( &mbedtls_md5_info );
1245#endif
1246#if defined(MBEDTLS_RIPEMD160_C)
1247 case PSA_ALG_RIPEMD160:
1248 return( &mbedtls_ripemd160_info );
1249#endif
1250#if defined(MBEDTLS_SHA1_C)
1251 case PSA_ALG_SHA_1:
1252 return( &mbedtls_sha1_info );
1253#endif
1254#if defined(MBEDTLS_SHA256_C)
1255 case PSA_ALG_SHA_224:
1256 return( &mbedtls_sha224_info );
1257 case PSA_ALG_SHA_256:
1258 return( &mbedtls_sha256_info );
1259#endif
1260#if defined(MBEDTLS_SHA512_C)
1261 case PSA_ALG_SHA_384:
1262 return( &mbedtls_sha384_info );
1263 case PSA_ALG_SHA_512:
1264 return( &mbedtls_sha512_info );
1265#endif
1266 default:
1267 return( NULL );
1268 }
1269}
1270
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001271psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1272{
1273 switch( operation->alg )
1274 {
Gilles Peskine81736312018-06-26 15:04:31 +02001275 case 0:
1276 /* The object has (apparently) been initialized but it is not
1277 * in use. It's ok to call abort on such an object, and there's
1278 * nothing to do. */
1279 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001280#if defined(MBEDTLS_MD2_C)
1281 case PSA_ALG_MD2:
1282 mbedtls_md2_free( &operation->ctx.md2 );
1283 break;
1284#endif
1285#if defined(MBEDTLS_MD4_C)
1286 case PSA_ALG_MD4:
1287 mbedtls_md4_free( &operation->ctx.md4 );
1288 break;
1289#endif
1290#if defined(MBEDTLS_MD5_C)
1291 case PSA_ALG_MD5:
1292 mbedtls_md5_free( &operation->ctx.md5 );
1293 break;
1294#endif
1295#if defined(MBEDTLS_RIPEMD160_C)
1296 case PSA_ALG_RIPEMD160:
1297 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1298 break;
1299#endif
1300#if defined(MBEDTLS_SHA1_C)
1301 case PSA_ALG_SHA_1:
1302 mbedtls_sha1_free( &operation->ctx.sha1 );
1303 break;
1304#endif
1305#if defined(MBEDTLS_SHA256_C)
1306 case PSA_ALG_SHA_224:
1307 case PSA_ALG_SHA_256:
1308 mbedtls_sha256_free( &operation->ctx.sha256 );
1309 break;
1310#endif
1311#if defined(MBEDTLS_SHA512_C)
1312 case PSA_ALG_SHA_384:
1313 case PSA_ALG_SHA_512:
1314 mbedtls_sha512_free( &operation->ctx.sha512 );
1315 break;
1316#endif
1317 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001318 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001319 }
1320 operation->alg = 0;
1321 return( PSA_SUCCESS );
1322}
1323
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001324psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001325 psa_algorithm_t alg )
1326{
1327 int ret;
1328 operation->alg = 0;
1329 switch( alg )
1330 {
1331#if defined(MBEDTLS_MD2_C)
1332 case PSA_ALG_MD2:
1333 mbedtls_md2_init( &operation->ctx.md2 );
1334 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1335 break;
1336#endif
1337#if defined(MBEDTLS_MD4_C)
1338 case PSA_ALG_MD4:
1339 mbedtls_md4_init( &operation->ctx.md4 );
1340 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1341 break;
1342#endif
1343#if defined(MBEDTLS_MD5_C)
1344 case PSA_ALG_MD5:
1345 mbedtls_md5_init( &operation->ctx.md5 );
1346 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1347 break;
1348#endif
1349#if defined(MBEDTLS_RIPEMD160_C)
1350 case PSA_ALG_RIPEMD160:
1351 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1352 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1353 break;
1354#endif
1355#if defined(MBEDTLS_SHA1_C)
1356 case PSA_ALG_SHA_1:
1357 mbedtls_sha1_init( &operation->ctx.sha1 );
1358 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1359 break;
1360#endif
1361#if defined(MBEDTLS_SHA256_C)
1362 case PSA_ALG_SHA_224:
1363 mbedtls_sha256_init( &operation->ctx.sha256 );
1364 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1365 break;
1366 case PSA_ALG_SHA_256:
1367 mbedtls_sha256_init( &operation->ctx.sha256 );
1368 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1369 break;
1370#endif
1371#if defined(MBEDTLS_SHA512_C)
1372 case PSA_ALG_SHA_384:
1373 mbedtls_sha512_init( &operation->ctx.sha512 );
1374 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1375 break;
1376 case PSA_ALG_SHA_512:
1377 mbedtls_sha512_init( &operation->ctx.sha512 );
1378 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1379 break;
1380#endif
1381 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001382 return( PSA_ALG_IS_HASH( alg ) ?
1383 PSA_ERROR_NOT_SUPPORTED :
1384 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001385 }
1386 if( ret == 0 )
1387 operation->alg = alg;
1388 else
1389 psa_hash_abort( operation );
1390 return( mbedtls_to_psa_error( ret ) );
1391}
1392
1393psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1394 const uint8_t *input,
1395 size_t input_length )
1396{
1397 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001398
1399 /* Don't require hash implementations to behave correctly on a
1400 * zero-length input, which may have an invalid pointer. */
1401 if( input_length == 0 )
1402 return( PSA_SUCCESS );
1403
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001404 switch( operation->alg )
1405 {
1406#if defined(MBEDTLS_MD2_C)
1407 case PSA_ALG_MD2:
1408 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1409 input, input_length );
1410 break;
1411#endif
1412#if defined(MBEDTLS_MD4_C)
1413 case PSA_ALG_MD4:
1414 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1415 input, input_length );
1416 break;
1417#endif
1418#if defined(MBEDTLS_MD5_C)
1419 case PSA_ALG_MD5:
1420 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1421 input, input_length );
1422 break;
1423#endif
1424#if defined(MBEDTLS_RIPEMD160_C)
1425 case PSA_ALG_RIPEMD160:
1426 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1427 input, input_length );
1428 break;
1429#endif
1430#if defined(MBEDTLS_SHA1_C)
1431 case PSA_ALG_SHA_1:
1432 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1433 input, input_length );
1434 break;
1435#endif
1436#if defined(MBEDTLS_SHA256_C)
1437 case PSA_ALG_SHA_224:
1438 case PSA_ALG_SHA_256:
1439 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1440 input, input_length );
1441 break;
1442#endif
1443#if defined(MBEDTLS_SHA512_C)
1444 case PSA_ALG_SHA_384:
1445 case PSA_ALG_SHA_512:
1446 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1447 input, input_length );
1448 break;
1449#endif
1450 default:
1451 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1452 break;
1453 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001454
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001455 if( ret != 0 )
1456 psa_hash_abort( operation );
1457 return( mbedtls_to_psa_error( ret ) );
1458}
1459
1460psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1461 uint8_t *hash,
1462 size_t hash_size,
1463 size_t *hash_length )
1464{
itayzafrir40835d42018-08-02 13:14:17 +03001465 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001466 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001467 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001468
1469 /* Fill the output buffer with something that isn't a valid hash
1470 * (barring an attack on the hash and deliberately-crafted input),
1471 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001472 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001473 /* If hash_size is 0 then hash may be NULL and then the
1474 * call to memset would have undefined behavior. */
1475 if( hash_size != 0 )
1476 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001477
1478 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001479 {
1480 status = PSA_ERROR_BUFFER_TOO_SMALL;
1481 goto exit;
1482 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001483
1484 switch( operation->alg )
1485 {
1486#if defined(MBEDTLS_MD2_C)
1487 case PSA_ALG_MD2:
1488 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1489 break;
1490#endif
1491#if defined(MBEDTLS_MD4_C)
1492 case PSA_ALG_MD4:
1493 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1494 break;
1495#endif
1496#if defined(MBEDTLS_MD5_C)
1497 case PSA_ALG_MD5:
1498 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1499 break;
1500#endif
1501#if defined(MBEDTLS_RIPEMD160_C)
1502 case PSA_ALG_RIPEMD160:
1503 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1504 break;
1505#endif
1506#if defined(MBEDTLS_SHA1_C)
1507 case PSA_ALG_SHA_1:
1508 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1509 break;
1510#endif
1511#if defined(MBEDTLS_SHA256_C)
1512 case PSA_ALG_SHA_224:
1513 case PSA_ALG_SHA_256:
1514 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1515 break;
1516#endif
1517#if defined(MBEDTLS_SHA512_C)
1518 case PSA_ALG_SHA_384:
1519 case PSA_ALG_SHA_512:
1520 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1521 break;
1522#endif
1523 default:
1524 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1525 break;
1526 }
itayzafrir40835d42018-08-02 13:14:17 +03001527 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001528
itayzafrir40835d42018-08-02 13:14:17 +03001529exit:
1530 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001531 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001532 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001533 return( psa_hash_abort( operation ) );
1534 }
1535 else
1536 {
1537 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001538 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001539 }
1540}
1541
Gilles Peskine2d277862018-06-18 15:41:12 +02001542psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1543 const uint8_t *hash,
1544 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001545{
1546 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1547 size_t actual_hash_length;
1548 psa_status_t status = psa_hash_finish( operation,
1549 actual_hash, sizeof( actual_hash ),
1550 &actual_hash_length );
1551 if( status != PSA_SUCCESS )
1552 return( status );
1553 if( actual_hash_length != hash_length )
1554 return( PSA_ERROR_INVALID_SIGNATURE );
1555 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1556 return( PSA_ERROR_INVALID_SIGNATURE );
1557 return( PSA_SUCCESS );
1558}
1559
1560
1561
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001562/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001563/* MAC */
1564/****************************************************************/
1565
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001566static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001567 psa_algorithm_t alg,
1568 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001569 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001570 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001571{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001572 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001573 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001574
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001575 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001576 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001577
Gilles Peskine8c9def32018-02-08 10:02:12 +01001578 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1579 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001580 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001581 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001582 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001583 mode = MBEDTLS_MODE_STREAM;
1584 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001585 case PSA_ALG_CTR:
1586 mode = MBEDTLS_MODE_CTR;
1587 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001588 case PSA_ALG_CFB:
1589 mode = MBEDTLS_MODE_CFB;
1590 break;
1591 case PSA_ALG_OFB:
1592 mode = MBEDTLS_MODE_OFB;
1593 break;
1594 case PSA_ALG_CBC_NO_PADDING:
1595 mode = MBEDTLS_MODE_CBC;
1596 break;
1597 case PSA_ALG_CBC_PKCS7:
1598 mode = MBEDTLS_MODE_CBC;
1599 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001600 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001601 mode = MBEDTLS_MODE_CCM;
1602 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001603 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001604 mode = MBEDTLS_MODE_GCM;
1605 break;
1606 default:
1607 return( NULL );
1608 }
1609 }
1610 else if( alg == PSA_ALG_CMAC )
1611 mode = MBEDTLS_MODE_ECB;
1612 else if( alg == PSA_ALG_GMAC )
1613 mode = MBEDTLS_MODE_GCM;
1614 else
1615 return( NULL );
1616
1617 switch( key_type )
1618 {
1619 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001620 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001621 break;
1622 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001623 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1624 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001625 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001626 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001627 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001628 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001629 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1630 * but two-key Triple-DES is functionally three-key Triple-DES
1631 * with K1=K3, so that's how we present it to mbedtls. */
1632 if( key_bits == 128 )
1633 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001634 break;
1635 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001636 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001637 break;
1638 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001639 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001640 break;
1641 default:
1642 return( NULL );
1643 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001644 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001645 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001646
Jaeden Amero23bbb752018-06-26 14:16:54 +01001647 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1648 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001649}
1650
Gilles Peskinea05219c2018-11-16 16:02:56 +01001651#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001652static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001653{
Gilles Peskine2d277862018-06-18 15:41:12 +02001654 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001655 {
1656 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001657 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001658 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001659 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001660 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001661 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001662 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001663 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001664 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001665 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001666 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001667 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001668 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001669 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001670 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001671 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001672 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001673 return( 128 );
1674 default:
1675 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001676 }
1677}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001678#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001679
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001680/* Initialize the MAC operation structure. Once this function has been
1681 * called, psa_mac_abort can run and will do the right thing. */
1682static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1683 psa_algorithm_t alg )
1684{
1685 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1686
1687 operation->alg = alg;
1688 operation->key_set = 0;
1689 operation->iv_set = 0;
1690 operation->iv_required = 0;
1691 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001692 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001693
1694#if defined(MBEDTLS_CMAC_C)
1695 if( alg == PSA_ALG_CMAC )
1696 {
1697 operation->iv_required = 0;
1698 mbedtls_cipher_init( &operation->ctx.cmac );
1699 status = PSA_SUCCESS;
1700 }
1701 else
1702#endif /* MBEDTLS_CMAC_C */
1703#if defined(MBEDTLS_MD_C)
1704 if( PSA_ALG_IS_HMAC( operation->alg ) )
1705 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001706 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1707 operation->ctx.hmac.hash_ctx.alg = 0;
1708 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001709 }
1710 else
1711#endif /* MBEDTLS_MD_C */
1712 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001713 if( ! PSA_ALG_IS_MAC( alg ) )
1714 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001715 }
1716
1717 if( status != PSA_SUCCESS )
1718 memset( operation, 0, sizeof( *operation ) );
1719 return( status );
1720}
1721
Gilles Peskine01126fa2018-07-12 17:04:55 +02001722#if defined(MBEDTLS_MD_C)
1723static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1724{
1725 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1726 return( psa_hash_abort( &hmac->hash_ctx ) );
1727}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001728
1729static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1730{
1731 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1732 memset( hmac, 0, sizeof( *hmac ) );
1733}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001734#endif /* MBEDTLS_MD_C */
1735
Gilles Peskine8c9def32018-02-08 10:02:12 +01001736psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1737{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001738 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001739 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001740 /* The object has (apparently) been initialized but it is not
1741 * in use. It's ok to call abort on such an object, and there's
1742 * nothing to do. */
1743 return( PSA_SUCCESS );
1744 }
1745 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001746#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001747 if( operation->alg == PSA_ALG_CMAC )
1748 {
1749 mbedtls_cipher_free( &operation->ctx.cmac );
1750 }
1751 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001752#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001753#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001754 if( PSA_ALG_IS_HMAC( operation->alg ) )
1755 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001756 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001757 }
1758 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001759#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001760 {
1761 /* Sanity check (shouldn't happen: operation->alg should
1762 * always have been initialized to a valid value). */
1763 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001764 }
Moran Peker41deec42018-04-04 15:43:05 +03001765
Gilles Peskine8c9def32018-02-08 10:02:12 +01001766 operation->alg = 0;
1767 operation->key_set = 0;
1768 operation->iv_set = 0;
1769 operation->iv_required = 0;
1770 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001771 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001772
Gilles Peskine8c9def32018-02-08 10:02:12 +01001773 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001774
1775bad_state:
1776 /* If abort is called on an uninitialized object, we can't trust
1777 * anything. Wipe the object in case it contains confidential data.
1778 * This may result in a memory leak if a pointer gets overwritten,
1779 * but it's too late to do anything about this. */
1780 memset( operation, 0, sizeof( *operation ) );
1781 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001782}
1783
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001784#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001785static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001786 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001787 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001788 const mbedtls_cipher_info_t *cipher_info )
1789{
1790 int ret;
1791
1792 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001793
1794 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1795 if( ret != 0 )
1796 return( ret );
1797
1798 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1799 slot->data.raw.data,
1800 key_bits );
1801 return( ret );
1802}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001803#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001804
Gilles Peskine248051a2018-06-20 16:09:38 +02001805#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001806static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1807 const uint8_t *key,
1808 size_t key_length,
1809 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001810{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001811 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001812 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001813 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001814 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001815 psa_status_t status;
1816
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001817 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1818 * overflow below. This should never trigger if the hash algorithm
1819 * is implemented correctly. */
1820 /* The size checks against the ipad and opad buffers cannot be written
1821 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1822 * because that triggers -Wlogical-op on GCC 7.3. */
1823 if( block_size > sizeof( ipad ) )
1824 return( PSA_ERROR_NOT_SUPPORTED );
1825 if( block_size > sizeof( hmac->opad ) )
1826 return( PSA_ERROR_NOT_SUPPORTED );
1827 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001828 return( PSA_ERROR_NOT_SUPPORTED );
1829
Gilles Peskined223b522018-06-11 18:12:58 +02001830 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001831 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001832 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001833 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001834 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001835 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001836 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001837 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001838 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001839 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001840 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001841 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001842 }
Gilles Peskine96889972018-07-12 17:07:03 +02001843 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1844 * but it is permitted. It is common when HMAC is used in HKDF, for
1845 * example. Don't call `memcpy` in the 0-length because `key` could be
1846 * an invalid pointer which would make the behavior undefined. */
1847 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001848 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001849
Gilles Peskined223b522018-06-11 18:12:58 +02001850 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1851 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001852 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001853 ipad[i] ^= 0x36;
1854 memset( ipad + key_length, 0x36, block_size - key_length );
1855
1856 /* Copy the key material from ipad to opad, flipping the requisite bits,
1857 * and filling the rest of opad with the requisite constant. */
1858 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001859 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1860 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001861
Gilles Peskine01126fa2018-07-12 17:04:55 +02001862 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001863 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001864 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001865
Gilles Peskine01126fa2018-07-12 17:04:55 +02001866 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001867
1868cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001869 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001870
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001871 return( status );
1872}
Gilles Peskine248051a2018-06-20 16:09:38 +02001873#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001874
Gilles Peskine89167cb2018-07-08 20:12:23 +02001875static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001876 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001877 psa_algorithm_t alg,
1878 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001879{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001880 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001881 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001882 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001883 psa_key_usage_t usage =
1884 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001885 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001886 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001887
Gilles Peskined911eb72018-08-14 15:18:45 +02001888 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001889 if( status != PSA_SUCCESS )
1890 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001891 if( is_sign )
1892 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001893
Gilles Peskinec5487a82018-12-03 18:08:14 +01001894 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001895 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001896 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001897 key_bits = psa_get_key_bits( slot );
1898
Gilles Peskine8c9def32018-02-08 10:02:12 +01001899#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001900 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001901 {
1902 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001903 mbedtls_cipher_info_from_psa( full_length_alg,
1904 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001905 int ret;
1906 if( cipher_info == NULL )
1907 {
1908 status = PSA_ERROR_NOT_SUPPORTED;
1909 goto exit;
1910 }
1911 operation->mac_size = cipher_info->block_size;
1912 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1913 status = mbedtls_to_psa_error( ret );
1914 }
1915 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001916#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001917#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001918 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001919 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001920 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001921 if( hash_alg == 0 )
1922 {
1923 status = PSA_ERROR_NOT_SUPPORTED;
1924 goto exit;
1925 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001926
1927 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1928 /* Sanity check. This shouldn't fail on a valid configuration. */
1929 if( operation->mac_size == 0 ||
1930 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1931 {
1932 status = PSA_ERROR_NOT_SUPPORTED;
1933 goto exit;
1934 }
1935
Gilles Peskine01126fa2018-07-12 17:04:55 +02001936 if( slot->type != PSA_KEY_TYPE_HMAC )
1937 {
1938 status = PSA_ERROR_INVALID_ARGUMENT;
1939 goto exit;
1940 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001941
Gilles Peskine01126fa2018-07-12 17:04:55 +02001942 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1943 slot->data.raw.data,
1944 slot->data.raw.bytes,
1945 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001946 }
1947 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001948#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001949 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00001950 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02001951 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001952 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001953
Gilles Peskined911eb72018-08-14 15:18:45 +02001954 if( truncated == 0 )
1955 {
1956 /* The "normal" case: untruncated algorithm. Nothing to do. */
1957 }
1958 else if( truncated < 4 )
1959 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001960 /* A very short MAC is too short for security since it can be
1961 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1962 * so we make this our minimum, even though 32 bits is still
1963 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001964 status = PSA_ERROR_NOT_SUPPORTED;
1965 }
1966 else if( truncated > operation->mac_size )
1967 {
1968 /* It's impossible to "truncate" to a larger length. */
1969 status = PSA_ERROR_INVALID_ARGUMENT;
1970 }
1971 else
1972 operation->mac_size = truncated;
1973
Gilles Peskinefbfac682018-07-08 20:51:54 +02001974exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001975 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001976 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001977 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001978 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001979 else
1980 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001981 operation->key_set = 1;
1982 }
1983 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001984}
1985
Gilles Peskine89167cb2018-07-08 20:12:23 +02001986psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001987 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001988 psa_algorithm_t alg )
1989{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001990 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001991}
1992
1993psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001994 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001995 psa_algorithm_t alg )
1996{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001997 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001998}
1999
Gilles Peskine8c9def32018-02-08 10:02:12 +01002000psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2001 const uint8_t *input,
2002 size_t input_length )
2003{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002004 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002005 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002006 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002007 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002008 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002009 operation->has_input = 1;
2010
Gilles Peskine8c9def32018-02-08 10:02:12 +01002011#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002012 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002013 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002014 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2015 input, input_length );
2016 status = mbedtls_to_psa_error( ret );
2017 }
2018 else
2019#endif /* MBEDTLS_CMAC_C */
2020#if defined(MBEDTLS_MD_C)
2021 if( PSA_ALG_IS_HMAC( operation->alg ) )
2022 {
2023 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2024 input_length );
2025 }
2026 else
2027#endif /* MBEDTLS_MD_C */
2028 {
2029 /* This shouldn't happen if `operation` was initialized by
2030 * a setup function. */
2031 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002032 }
2033
Gilles Peskinefbfac682018-07-08 20:51:54 +02002034cleanup:
2035 if( status != PSA_SUCCESS )
2036 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002037 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002038}
2039
Gilles Peskine01126fa2018-07-12 17:04:55 +02002040#if defined(MBEDTLS_MD_C)
2041static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2042 uint8_t *mac,
2043 size_t mac_size )
2044{
2045 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2046 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2047 size_t hash_size = 0;
2048 size_t block_size = psa_get_hash_block_size( hash_alg );
2049 psa_status_t status;
2050
Gilles Peskine01126fa2018-07-12 17:04:55 +02002051 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2052 if( status != PSA_SUCCESS )
2053 return( status );
2054 /* From here on, tmp needs to be wiped. */
2055
2056 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2057 if( status != PSA_SUCCESS )
2058 goto exit;
2059
2060 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2061 if( status != PSA_SUCCESS )
2062 goto exit;
2063
2064 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2065 if( status != PSA_SUCCESS )
2066 goto exit;
2067
Gilles Peskined911eb72018-08-14 15:18:45 +02002068 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2069 if( status != PSA_SUCCESS )
2070 goto exit;
2071
2072 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002073
2074exit:
2075 mbedtls_zeroize( tmp, hash_size );
2076 return( status );
2077}
2078#endif /* MBEDTLS_MD_C */
2079
mohammad16036df908f2018-04-02 08:34:15 -07002080static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002081 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002082 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002083{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002084 if( ! operation->key_set )
2085 return( PSA_ERROR_BAD_STATE );
2086 if( operation->iv_required && ! operation->iv_set )
2087 return( PSA_ERROR_BAD_STATE );
2088
Gilles Peskine8c9def32018-02-08 10:02:12 +01002089 if( mac_size < operation->mac_size )
2090 return( PSA_ERROR_BUFFER_TOO_SMALL );
2091
Gilles Peskine8c9def32018-02-08 10:02:12 +01002092#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002093 if( operation->alg == PSA_ALG_CMAC )
2094 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002095 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2096 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2097 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002098 memcpy( mac, tmp, operation->mac_size );
Gilles Peskined911eb72018-08-14 15:18:45 +02002099 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002100 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002101 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002102 else
2103#endif /* MBEDTLS_CMAC_C */
2104#if defined(MBEDTLS_MD_C)
2105 if( PSA_ALG_IS_HMAC( operation->alg ) )
2106 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002107 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002108 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002109 }
2110 else
2111#endif /* MBEDTLS_MD_C */
2112 {
2113 /* This shouldn't happen if `operation` was initialized by
2114 * a setup function. */
2115 return( PSA_ERROR_BAD_STATE );
2116 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002117}
2118
Gilles Peskineacd4be32018-07-08 19:56:25 +02002119psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2120 uint8_t *mac,
2121 size_t mac_size,
2122 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002123{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002124 psa_status_t status;
2125
2126 /* Fill the output buffer with something that isn't a valid mac
2127 * (barring an attack on the mac and deliberately-crafted input),
2128 * in case the caller doesn't check the return status properly. */
2129 *mac_length = mac_size;
2130 /* If mac_size is 0 then mac may be NULL and then the
2131 * call to memset would have undefined behavior. */
2132 if( mac_size != 0 )
2133 memset( mac, '!', mac_size );
2134
Gilles Peskine89167cb2018-07-08 20:12:23 +02002135 if( ! operation->is_sign )
2136 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002137 status = PSA_ERROR_BAD_STATE;
2138 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002139 }
mohammad16036df908f2018-04-02 08:34:15 -07002140
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002141 status = psa_mac_finish_internal( operation, mac, mac_size );
2142
2143cleanup:
2144 if( status == PSA_SUCCESS )
2145 {
2146 status = psa_mac_abort( operation );
2147 if( status == PSA_SUCCESS )
2148 *mac_length = operation->mac_size;
2149 else
2150 memset( mac, '!', mac_size );
2151 }
2152 else
2153 psa_mac_abort( operation );
2154 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002155}
2156
Gilles Peskineacd4be32018-07-08 19:56:25 +02002157psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2158 const uint8_t *mac,
2159 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002160{
Gilles Peskine828ed142018-06-18 23:25:51 +02002161 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002162 psa_status_t status;
2163
Gilles Peskine89167cb2018-07-08 20:12:23 +02002164 if( operation->is_sign )
2165 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002166 status = PSA_ERROR_BAD_STATE;
2167 goto cleanup;
2168 }
2169 if( operation->mac_size != mac_length )
2170 {
2171 status = PSA_ERROR_INVALID_SIGNATURE;
2172 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002173 }
mohammad16036df908f2018-04-02 08:34:15 -07002174
2175 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002176 actual_mac, sizeof( actual_mac ) );
2177
2178 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2179 status = PSA_ERROR_INVALID_SIGNATURE;
2180
2181cleanup:
2182 if( status == PSA_SUCCESS )
2183 status = psa_mac_abort( operation );
2184 else
2185 psa_mac_abort( operation );
2186
Gilles Peskine99b7d6b2018-08-21 14:56:19 +02002187 mbedtls_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002188
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002189 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002190}
2191
2192
Gilles Peskine20035e32018-02-03 22:44:14 +01002193
Gilles Peskine20035e32018-02-03 22:44:14 +01002194/****************************************************************/
2195/* Asymmetric cryptography */
2196/****************************************************************/
2197
Gilles Peskine2b450e32018-06-27 15:42:46 +02002198#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002199/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002200 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002201static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2202 size_t hash_length,
2203 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002204{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002205 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002206 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002207 *md_alg = mbedtls_md_get_type( md_info );
2208
2209 /* The Mbed TLS RSA module uses an unsigned int for hash length
2210 * parameters. Validate that it fits so that we don't risk an
2211 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002212#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002213 if( hash_length > UINT_MAX )
2214 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002215#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002216
2217#if defined(MBEDTLS_PKCS1_V15)
2218 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2219 * must be correct. */
2220 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2221 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002222 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002223 if( md_info == NULL )
2224 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002225 if( mbedtls_md_get_size( md_info ) != hash_length )
2226 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002227 }
2228#endif /* MBEDTLS_PKCS1_V15 */
2229
2230#if defined(MBEDTLS_PKCS1_V21)
2231 /* PSS requires a hash internally. */
2232 if( PSA_ALG_IS_RSA_PSS( alg ) )
2233 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002234 if( md_info == NULL )
2235 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002236 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002237#endif /* MBEDTLS_PKCS1_V21 */
2238
Gilles Peskine61b91d42018-06-08 16:09:36 +02002239 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002240}
2241
Gilles Peskine2b450e32018-06-27 15:42:46 +02002242static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2243 psa_algorithm_t alg,
2244 const uint8_t *hash,
2245 size_t hash_length,
2246 uint8_t *signature,
2247 size_t signature_size,
2248 size_t *signature_length )
2249{
2250 psa_status_t status;
2251 int ret;
2252 mbedtls_md_type_t md_alg;
2253
2254 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2255 if( status != PSA_SUCCESS )
2256 return( status );
2257
Gilles Peskine630a18a2018-06-29 17:49:35 +02002258 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002259 return( PSA_ERROR_BUFFER_TOO_SMALL );
2260
2261#if defined(MBEDTLS_PKCS1_V15)
2262 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2263 {
2264 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2265 MBEDTLS_MD_NONE );
2266 ret = mbedtls_rsa_pkcs1_sign( rsa,
2267 mbedtls_ctr_drbg_random,
2268 &global_data.ctr_drbg,
2269 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002270 md_alg,
2271 (unsigned int) hash_length,
2272 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002273 signature );
2274 }
2275 else
2276#endif /* MBEDTLS_PKCS1_V15 */
2277#if defined(MBEDTLS_PKCS1_V21)
2278 if( PSA_ALG_IS_RSA_PSS( alg ) )
2279 {
2280 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2281 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2282 mbedtls_ctr_drbg_random,
2283 &global_data.ctr_drbg,
2284 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002285 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002286 (unsigned int) hash_length,
2287 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002288 signature );
2289 }
2290 else
2291#endif /* MBEDTLS_PKCS1_V21 */
2292 {
2293 return( PSA_ERROR_INVALID_ARGUMENT );
2294 }
2295
2296 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002297 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002298 return( mbedtls_to_psa_error( ret ) );
2299}
2300
2301static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2302 psa_algorithm_t alg,
2303 const uint8_t *hash,
2304 size_t hash_length,
2305 const uint8_t *signature,
2306 size_t signature_length )
2307{
2308 psa_status_t status;
2309 int ret;
2310 mbedtls_md_type_t md_alg;
2311
2312 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2313 if( status != PSA_SUCCESS )
2314 return( status );
2315
Gilles Peskine630a18a2018-06-29 17:49:35 +02002316 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002317 return( PSA_ERROR_BUFFER_TOO_SMALL );
2318
2319#if defined(MBEDTLS_PKCS1_V15)
2320 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2321 {
2322 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2323 MBEDTLS_MD_NONE );
2324 ret = mbedtls_rsa_pkcs1_verify( rsa,
2325 mbedtls_ctr_drbg_random,
2326 &global_data.ctr_drbg,
2327 MBEDTLS_RSA_PUBLIC,
2328 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002329 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002330 hash,
2331 signature );
2332 }
2333 else
2334#endif /* MBEDTLS_PKCS1_V15 */
2335#if defined(MBEDTLS_PKCS1_V21)
2336 if( PSA_ALG_IS_RSA_PSS( alg ) )
2337 {
2338 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2339 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2340 mbedtls_ctr_drbg_random,
2341 &global_data.ctr_drbg,
2342 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002343 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002344 (unsigned int) hash_length,
2345 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002346 signature );
2347 }
2348 else
2349#endif /* MBEDTLS_PKCS1_V21 */
2350 {
2351 return( PSA_ERROR_INVALID_ARGUMENT );
2352 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002353
2354 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2355 * the rest of the signature is invalid". This has little use in
2356 * practice and PSA doesn't report this distinction. */
2357 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2358 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002359 return( mbedtls_to_psa_error( ret ) );
2360}
2361#endif /* MBEDTLS_RSA_C */
2362
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002363#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002364/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2365 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2366 * (even though these functions don't modify it). */
2367static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2368 psa_algorithm_t alg,
2369 const uint8_t *hash,
2370 size_t hash_length,
2371 uint8_t *signature,
2372 size_t signature_size,
2373 size_t *signature_length )
2374{
2375 int ret;
2376 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002377 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002378 mbedtls_mpi_init( &r );
2379 mbedtls_mpi_init( &s );
2380
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002381 if( signature_size < 2 * curve_bytes )
2382 {
2383 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2384 goto cleanup;
2385 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002386
Gilles Peskinea05219c2018-11-16 16:02:56 +01002387#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002388 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2389 {
2390 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2391 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2392 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2393 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2394 hash, hash_length,
2395 md_alg ) );
2396 }
2397 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002398#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002399 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002400 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002401 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2402 hash, hash_length,
2403 mbedtls_ctr_drbg_random,
2404 &global_data.ctr_drbg ) );
2405 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002406
2407 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2408 signature,
2409 curve_bytes ) );
2410 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2411 signature + curve_bytes,
2412 curve_bytes ) );
2413
2414cleanup:
2415 mbedtls_mpi_free( &r );
2416 mbedtls_mpi_free( &s );
2417 if( ret == 0 )
2418 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002419 return( mbedtls_to_psa_error( ret ) );
2420}
2421
2422static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2423 const uint8_t *hash,
2424 size_t hash_length,
2425 const uint8_t *signature,
2426 size_t signature_length )
2427{
2428 int ret;
2429 mbedtls_mpi r, s;
2430 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2431 mbedtls_mpi_init( &r );
2432 mbedtls_mpi_init( &s );
2433
2434 if( signature_length != 2 * curve_bytes )
2435 return( PSA_ERROR_INVALID_SIGNATURE );
2436
2437 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2438 signature,
2439 curve_bytes ) );
2440 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2441 signature + curve_bytes,
2442 curve_bytes ) );
2443
2444 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2445 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002446
2447cleanup:
2448 mbedtls_mpi_free( &r );
2449 mbedtls_mpi_free( &s );
2450 return( mbedtls_to_psa_error( ret ) );
2451}
2452#endif /* MBEDTLS_ECDSA_C */
2453
Gilles Peskinec5487a82018-12-03 18:08:14 +01002454psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002455 psa_algorithm_t alg,
2456 const uint8_t *hash,
2457 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002458 uint8_t *signature,
2459 size_t signature_size,
2460 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002461{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002462 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002463 psa_status_t status;
2464
2465 *signature_length = signature_size;
2466
Gilles Peskinec5487a82018-12-03 18:08:14 +01002467 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002468 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002469 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002470 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002471 {
2472 status = PSA_ERROR_INVALID_ARGUMENT;
2473 goto exit;
2474 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002475
Gilles Peskine20035e32018-02-03 22:44:14 +01002476#if defined(MBEDTLS_RSA_C)
2477 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2478 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002479 status = psa_rsa_sign( slot->data.rsa,
2480 alg,
2481 hash, hash_length,
2482 signature, signature_size,
2483 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002484 }
2485 else
2486#endif /* defined(MBEDTLS_RSA_C) */
2487#if defined(MBEDTLS_ECP_C)
2488 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2489 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002490#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002491 if(
2492#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2493 PSA_ALG_IS_ECDSA( alg )
2494#else
2495 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2496#endif
2497 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002498 status = psa_ecdsa_sign( slot->data.ecp,
2499 alg,
2500 hash, hash_length,
2501 signature, signature_size,
2502 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002503 else
2504#endif /* defined(MBEDTLS_ECDSA_C) */
2505 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002506 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002507 }
itayzafrir5c753392018-05-08 11:18:38 +03002508 }
2509 else
2510#endif /* defined(MBEDTLS_ECP_C) */
2511 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002512 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002513 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002514
2515exit:
2516 /* Fill the unused part of the output buffer (the whole buffer on error,
2517 * the trailing part on success) with something that isn't a valid mac
2518 * (barring an attack on the mac and deliberately-crafted input),
2519 * in case the caller doesn't check the return status properly. */
2520 if( status == PSA_SUCCESS )
2521 memset( signature + *signature_length, '!',
2522 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002523 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002524 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002525 /* If signature_size is 0 then we have nothing to do. We must not call
2526 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002527 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002528}
2529
Gilles Peskinec5487a82018-12-03 18:08:14 +01002530psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002531 psa_algorithm_t alg,
2532 const uint8_t *hash,
2533 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002534 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002535 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002536{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002537 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002538 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002539
Gilles Peskinec5487a82018-12-03 18:08:14 +01002540 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002541 if( status != PSA_SUCCESS )
2542 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002543
Gilles Peskine61b91d42018-06-08 16:09:36 +02002544#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002545 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002546 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002547 return( psa_rsa_verify( slot->data.rsa,
2548 alg,
2549 hash, hash_length,
2550 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002551 }
2552 else
2553#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002554#if defined(MBEDTLS_ECP_C)
2555 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2556 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002557#if defined(MBEDTLS_ECDSA_C)
2558 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002559 return( psa_ecdsa_verify( slot->data.ecp,
2560 hash, hash_length,
2561 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002562 else
2563#endif /* defined(MBEDTLS_ECDSA_C) */
2564 {
2565 return( PSA_ERROR_INVALID_ARGUMENT );
2566 }
itayzafrir5c753392018-05-08 11:18:38 +03002567 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002568 else
2569#endif /* defined(MBEDTLS_ECP_C) */
2570 {
2571 return( PSA_ERROR_NOT_SUPPORTED );
2572 }
2573}
2574
Gilles Peskine072ac562018-06-30 00:21:29 +02002575#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2576static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2577 mbedtls_rsa_context *rsa )
2578{
2579 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2580 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2581 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2582 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2583}
2584#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2585
Gilles Peskinec5487a82018-12-03 18:08:14 +01002586psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002587 psa_algorithm_t alg,
2588 const uint8_t *input,
2589 size_t input_length,
2590 const uint8_t *salt,
2591 size_t salt_length,
2592 uint8_t *output,
2593 size_t output_size,
2594 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002595{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002596 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002597 psa_status_t status;
2598
Darryl Green5cc689a2018-07-24 15:34:10 +01002599 (void) input;
2600 (void) input_length;
2601 (void) salt;
2602 (void) output;
2603 (void) output_size;
2604
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002605 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002606
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002607 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2608 return( PSA_ERROR_INVALID_ARGUMENT );
2609
Gilles Peskinec5487a82018-12-03 18:08:14 +01002610 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002611 if( status != PSA_SUCCESS )
2612 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002613 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2614 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002615 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002616
2617#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002618 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002619 {
2620 mbedtls_rsa_context *rsa = slot->data.rsa;
2621 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002622 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002623 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002624#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002625 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002626 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002627 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2628 mbedtls_ctr_drbg_random,
2629 &global_data.ctr_drbg,
2630 MBEDTLS_RSA_PUBLIC,
2631 input_length,
2632 input,
2633 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002634 }
2635 else
2636#endif /* MBEDTLS_PKCS1_V15 */
2637#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002638 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002639 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002640 psa_rsa_oaep_set_padding_mode( alg, rsa );
2641 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2642 mbedtls_ctr_drbg_random,
2643 &global_data.ctr_drbg,
2644 MBEDTLS_RSA_PUBLIC,
2645 salt, salt_length,
2646 input_length,
2647 input,
2648 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002649 }
2650 else
2651#endif /* MBEDTLS_PKCS1_V21 */
2652 {
2653 return( PSA_ERROR_INVALID_ARGUMENT );
2654 }
2655 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002656 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002657 return( mbedtls_to_psa_error( ret ) );
2658 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002659 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002660#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002661 {
2662 return( PSA_ERROR_NOT_SUPPORTED );
2663 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002664}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002665
Gilles Peskinec5487a82018-12-03 18:08:14 +01002666psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002667 psa_algorithm_t alg,
2668 const uint8_t *input,
2669 size_t input_length,
2670 const uint8_t *salt,
2671 size_t salt_length,
2672 uint8_t *output,
2673 size_t output_size,
2674 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002675{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002676 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002677 psa_status_t status;
2678
Darryl Green5cc689a2018-07-24 15:34:10 +01002679 (void) input;
2680 (void) input_length;
2681 (void) salt;
2682 (void) output;
2683 (void) output_size;
2684
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002685 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002686
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002687 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2688 return( PSA_ERROR_INVALID_ARGUMENT );
2689
Gilles Peskinec5487a82018-12-03 18:08:14 +01002690 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002691 if( status != PSA_SUCCESS )
2692 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002693 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2694 return( PSA_ERROR_INVALID_ARGUMENT );
2695
2696#if defined(MBEDTLS_RSA_C)
2697 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2698 {
2699 mbedtls_rsa_context *rsa = slot->data.rsa;
2700 int ret;
2701
Gilles Peskine630a18a2018-06-29 17:49:35 +02002702 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002703 return( PSA_ERROR_INVALID_ARGUMENT );
2704
2705#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002706 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002707 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002708 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2709 mbedtls_ctr_drbg_random,
2710 &global_data.ctr_drbg,
2711 MBEDTLS_RSA_PRIVATE,
2712 output_length,
2713 input,
2714 output,
2715 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002716 }
2717 else
2718#endif /* MBEDTLS_PKCS1_V15 */
2719#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002720 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002721 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002722 psa_rsa_oaep_set_padding_mode( alg, rsa );
2723 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2724 mbedtls_ctr_drbg_random,
2725 &global_data.ctr_drbg,
2726 MBEDTLS_RSA_PRIVATE,
2727 salt, salt_length,
2728 output_length,
2729 input,
2730 output,
2731 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002732 }
2733 else
2734#endif /* MBEDTLS_PKCS1_V21 */
2735 {
2736 return( PSA_ERROR_INVALID_ARGUMENT );
2737 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002738
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002739 return( mbedtls_to_psa_error( ret ) );
2740 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002741 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002742#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002743 {
2744 return( PSA_ERROR_NOT_SUPPORTED );
2745 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002746}
Gilles Peskine20035e32018-02-03 22:44:14 +01002747
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002748
2749
mohammad1603503973b2018-03-12 15:59:30 +02002750/****************************************************************/
2751/* Symmetric cryptography */
2752/****************************************************************/
2753
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002754/* Initialize the cipher operation structure. Once this function has been
2755 * called, psa_cipher_abort can run and will do the right thing. */
2756static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2757 psa_algorithm_t alg )
2758{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002759 if( ! PSA_ALG_IS_CIPHER( alg ) )
2760 {
2761 memset( operation, 0, sizeof( *operation ) );
2762 return( PSA_ERROR_INVALID_ARGUMENT );
2763 }
2764
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002765 operation->alg = alg;
2766 operation->key_set = 0;
2767 operation->iv_set = 0;
2768 operation->iv_required = 1;
2769 operation->iv_size = 0;
2770 operation->block_size = 0;
2771 mbedtls_cipher_init( &operation->ctx.cipher );
2772 return( PSA_SUCCESS );
2773}
2774
Gilles Peskinee553c652018-06-04 16:22:46 +02002775static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002776 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002777 psa_algorithm_t alg,
2778 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002779{
2780 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2781 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002782 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002783 size_t key_bits;
2784 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002785 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2786 PSA_KEY_USAGE_ENCRYPT :
2787 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002788
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002789 status = psa_cipher_init( operation, alg );
2790 if( status != PSA_SUCCESS )
2791 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002792
Gilles Peskinec5487a82018-12-03 18:08:14 +01002793 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002794 if( status != PSA_SUCCESS )
2795 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002796 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002797
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002798 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002799 if( cipher_info == NULL )
2800 return( PSA_ERROR_NOT_SUPPORTED );
2801
mohammad1603503973b2018-03-12 15:59:30 +02002802 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002803 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002804 {
2805 psa_cipher_abort( operation );
2806 return( mbedtls_to_psa_error( ret ) );
2807 }
2808
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002809#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002810 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002811 {
2812 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2813 unsigned char keys[24];
2814 memcpy( keys, slot->data.raw.data, 16 );
2815 memcpy( keys + 16, slot->data.raw.data, 8 );
2816 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2817 keys,
2818 192, cipher_operation );
2819 }
2820 else
2821#endif
2822 {
2823 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2824 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002825 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002826 }
Moran Peker41deec42018-04-04 15:43:05 +03002827 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002828 {
2829 psa_cipher_abort( operation );
2830 return( mbedtls_to_psa_error( ret ) );
2831 }
2832
mohammad16038481e742018-03-18 13:57:31 +02002833#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002834 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002835 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002836 case PSA_ALG_CBC_NO_PADDING:
2837 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2838 MBEDTLS_PADDING_NONE );
2839 break;
2840 case PSA_ALG_CBC_PKCS7:
2841 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2842 MBEDTLS_PADDING_PKCS7 );
2843 break;
2844 default:
2845 /* The algorithm doesn't involve padding. */
2846 ret = 0;
2847 break;
2848 }
2849 if( ret != 0 )
2850 {
2851 psa_cipher_abort( operation );
2852 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002853 }
2854#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2855
mohammad1603503973b2018-03-12 15:59:30 +02002856 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002857 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2858 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2859 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002860 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002861 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002862 }
mohammad1603503973b2018-03-12 15:59:30 +02002863
Moran Peker395db872018-05-31 14:07:14 +03002864 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002865}
2866
Gilles Peskinefe119512018-07-08 21:39:34 +02002867psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002868 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002869 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002870{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002871 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002872}
2873
Gilles Peskinefe119512018-07-08 21:39:34 +02002874psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002875 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002876 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002877{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002878 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002879}
2880
Gilles Peskinefe119512018-07-08 21:39:34 +02002881psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2882 unsigned char *iv,
2883 size_t iv_size,
2884 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002885{
itayzafrir534bd7c2018-08-02 13:56:32 +03002886 psa_status_t status;
2887 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002888 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002889 {
2890 status = PSA_ERROR_BAD_STATE;
2891 goto exit;
2892 }
Moran Peker41deec42018-04-04 15:43:05 +03002893 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002894 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002895 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002896 goto exit;
2897 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002898 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2899 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002900 if( ret != 0 )
2901 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002902 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002903 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002904 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002905
mohammad16038481e742018-03-18 13:57:31 +02002906 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002907 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002908
Moran Peker395db872018-05-31 14:07:14 +03002909exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002910 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002911 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002912 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002913}
2914
Gilles Peskinefe119512018-07-08 21:39:34 +02002915psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2916 const unsigned char *iv,
2917 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002918{
itayzafrir534bd7c2018-08-02 13:56:32 +03002919 psa_status_t status;
2920 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002921 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002922 {
2923 status = PSA_ERROR_BAD_STATE;
2924 goto exit;
2925 }
Moran Pekera28258c2018-05-29 16:25:04 +03002926 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002927 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002928 status = PSA_ERROR_INVALID_ARGUMENT;
2929 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002930 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002931 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2932 status = mbedtls_to_psa_error( ret );
2933exit:
2934 if( status == PSA_SUCCESS )
2935 operation->iv_set = 1;
2936 else
mohammad1603503973b2018-03-12 15:59:30 +02002937 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002938 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002939}
2940
Gilles Peskinee553c652018-06-04 16:22:46 +02002941psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2942 const uint8_t *input,
2943 size_t input_length,
2944 unsigned char *output,
2945 size_t output_size,
2946 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002947{
itayzafrir534bd7c2018-08-02 13:56:32 +03002948 psa_status_t status;
2949 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002950 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002951 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002952 {
2953 /* Take the unprocessed partial block left over from previous
2954 * update calls, if any, plus the input to this call. Remove
2955 * the last partial block, if any. You get the data that will be
2956 * output in this call. */
2957 expected_output_size =
2958 ( operation->ctx.cipher.unprocessed_len + input_length )
2959 / operation->block_size * operation->block_size;
2960 }
2961 else
2962 {
2963 expected_output_size = input_length;
2964 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002965
Gilles Peskine89d789c2018-06-04 17:17:16 +02002966 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002967 {
2968 status = PSA_ERROR_BUFFER_TOO_SMALL;
2969 goto exit;
2970 }
mohammad160382759612018-03-12 18:16:40 +02002971
mohammad1603503973b2018-03-12 15:59:30 +02002972 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002973 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002974 status = mbedtls_to_psa_error( ret );
2975exit:
2976 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002977 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002978 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002979}
2980
Gilles Peskinee553c652018-06-04 16:22:46 +02002981psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2982 uint8_t *output,
2983 size_t output_size,
2984 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002985{
Janos Follath315b51c2018-07-09 16:04:51 +01002986 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2987 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002988 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002989
mohammad1603503973b2018-03-12 15:59:30 +02002990 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002991 {
Janos Follath315b51c2018-07-09 16:04:51 +01002992 status = PSA_ERROR_BAD_STATE;
2993 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002994 }
2995 if( operation->iv_required && ! operation->iv_set )
2996 {
Janos Follath315b51c2018-07-09 16:04:51 +01002997 status = PSA_ERROR_BAD_STATE;
2998 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002999 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003000
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003001 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003002 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3003 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003004 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003005 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003006 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003007 }
3008
Janos Follath315b51c2018-07-09 16:04:51 +01003009 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3010 temp_output_buffer,
3011 output_length );
3012 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003013 {
Janos Follath315b51c2018-07-09 16:04:51 +01003014 status = mbedtls_to_psa_error( cipher_ret );
3015 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003016 }
Janos Follath315b51c2018-07-09 16:04:51 +01003017
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003018 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003019 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003020 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003021 memcpy( output, temp_output_buffer, *output_length );
3022 else
3023 {
Janos Follath315b51c2018-07-09 16:04:51 +01003024 status = PSA_ERROR_BUFFER_TOO_SMALL;
3025 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003026 }
mohammad1603503973b2018-03-12 15:59:30 +02003027
Janos Follath279ab8e2018-07-09 16:13:21 +01003028 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003029 status = psa_cipher_abort( operation );
3030
3031 return( status );
3032
3033error:
3034
3035 *output_length = 0;
3036
Janos Follath279ab8e2018-07-09 16:13:21 +01003037 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003038 (void) psa_cipher_abort( operation );
3039
3040 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003041}
3042
Gilles Peskinee553c652018-06-04 16:22:46 +02003043psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3044{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003045 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003046 {
3047 /* The object has (apparently) been initialized but it is not
3048 * in use. It's ok to call abort on such an object, and there's
3049 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003050 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003051 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003052
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003053 /* Sanity check (shouldn't happen: operation->alg should
3054 * always have been initialized to a valid value). */
3055 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3056 return( PSA_ERROR_BAD_STATE );
3057
mohammad1603503973b2018-03-12 15:59:30 +02003058 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003059
Moran Peker41deec42018-04-04 15:43:05 +03003060 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003061 operation->key_set = 0;
3062 operation->iv_set = 0;
3063 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003064 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003065 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003066
Moran Peker395db872018-05-31 14:07:14 +03003067 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003068}
3069
Gilles Peskinea0655c32018-04-30 17:06:50 +02003070
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003071
mohammad16038cc1cee2018-03-28 01:21:33 +03003072/****************************************************************/
3073/* Key Policy */
3074/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003075
mohammad160327010052018-07-03 13:16:15 +03003076#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003077void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003078{
Gilles Peskine803ce742018-06-18 16:07:14 +02003079 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003080}
3081
Gilles Peskine2d277862018-06-18 15:41:12 +02003082void psa_key_policy_set_usage( psa_key_policy_t *policy,
3083 psa_key_usage_t usage,
3084 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003085{
mohammad16034eed7572018-03-28 05:14:59 -07003086 policy->usage = usage;
3087 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003088}
3089
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003090psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003091{
mohammad16036df908f2018-04-02 08:34:15 -07003092 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003093}
3094
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003095psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003096{
mohammad16036df908f2018-04-02 08:34:15 -07003097 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003098}
mohammad160327010052018-07-03 13:16:15 +03003099#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003100
Gilles Peskinec5487a82018-12-03 18:08:14 +01003101psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003102 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003103{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003104 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003105 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003106
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003107 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003108 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003109
Gilles Peskinec5487a82018-12-03 18:08:14 +01003110 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003111 if( status != PSA_SUCCESS )
3112 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003113
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003114 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3115 PSA_KEY_USAGE_ENCRYPT |
3116 PSA_KEY_USAGE_DECRYPT |
3117 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003118 PSA_KEY_USAGE_VERIFY |
3119 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003120 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003121
mohammad16036df908f2018-04-02 08:34:15 -07003122 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003123
3124 return( PSA_SUCCESS );
3125}
3126
Gilles Peskinec5487a82018-12-03 18:08:14 +01003127psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003128 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003129{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003130 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003131 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003132
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003133 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003134 return( PSA_ERROR_INVALID_ARGUMENT );
3135
Gilles Peskinec5487a82018-12-03 18:08:14 +01003136 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003137 if( status != PSA_SUCCESS )
3138 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003139
mohammad16036df908f2018-04-02 08:34:15 -07003140 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003141
3142 return( PSA_SUCCESS );
3143}
Gilles Peskine20035e32018-02-03 22:44:14 +01003144
Gilles Peskinea0655c32018-04-30 17:06:50 +02003145
3146
mohammad1603804cd712018-03-20 22:44:08 +02003147/****************************************************************/
3148/* Key Lifetime */
3149/****************************************************************/
3150
Gilles Peskinec5487a82018-12-03 18:08:14 +01003151psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003152 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003153{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003154 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003155 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003156
Gilles Peskinec5487a82018-12-03 18:08:14 +01003157 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003158 if( status != PSA_SUCCESS )
3159 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003160
mohammad1603804cd712018-03-20 22:44:08 +02003161 *lifetime = slot->lifetime;
3162
3163 return( PSA_SUCCESS );
3164}
3165
Gilles Peskine20035e32018-02-03 22:44:14 +01003166
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003167
mohammad16035955c982018-04-26 00:53:03 +03003168/****************************************************************/
3169/* AEAD */
3170/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003171
Gilles Peskineedf9a652018-08-17 18:11:56 +02003172typedef struct
3173{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003174 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003175 const mbedtls_cipher_info_t *cipher_info;
3176 union
3177 {
3178#if defined(MBEDTLS_CCM_C)
3179 mbedtls_ccm_context ccm;
3180#endif /* MBEDTLS_CCM_C */
3181#if defined(MBEDTLS_GCM_C)
3182 mbedtls_gcm_context gcm;
3183#endif /* MBEDTLS_GCM_C */
3184 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003185 psa_algorithm_t core_alg;
3186 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003187 uint8_t tag_length;
3188} aead_operation_t;
3189
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003190static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003191{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003192 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003193 {
3194#if defined(MBEDTLS_CCM_C)
3195 case PSA_ALG_CCM:
3196 mbedtls_ccm_free( &operation->ctx.ccm );
3197 break;
3198#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003199#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003200 case PSA_ALG_GCM:
3201 mbedtls_gcm_free( &operation->ctx.gcm );
3202 break;
3203#endif /* MBEDTLS_GCM_C */
3204 }
3205}
3206
3207static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003208 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003209 psa_key_usage_t usage,
3210 psa_algorithm_t alg )
3211{
3212 psa_status_t status;
3213 size_t key_bits;
3214 mbedtls_cipher_id_t cipher_id;
3215
Gilles Peskinec5487a82018-12-03 18:08:14 +01003216 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003217 if( status != PSA_SUCCESS )
3218 return( status );
3219
3220 key_bits = psa_get_key_bits( operation->slot );
3221
3222 operation->cipher_info =
3223 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3224 &cipher_id );
3225 if( operation->cipher_info == NULL )
3226 return( PSA_ERROR_NOT_SUPPORTED );
3227
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003228 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003229 {
3230#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003231 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3232 operation->core_alg = PSA_ALG_CCM;
3233 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003234 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3235 return( PSA_ERROR_INVALID_ARGUMENT );
3236 mbedtls_ccm_init( &operation->ctx.ccm );
3237 status = mbedtls_to_psa_error(
3238 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3239 operation->slot->data.raw.data,
3240 (unsigned int) key_bits ) );
3241 if( status != 0 )
3242 goto cleanup;
3243 break;
3244#endif /* MBEDTLS_CCM_C */
3245
3246#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003247 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3248 operation->core_alg = PSA_ALG_GCM;
3249 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003250 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3251 return( PSA_ERROR_INVALID_ARGUMENT );
3252 mbedtls_gcm_init( &operation->ctx.gcm );
3253 status = mbedtls_to_psa_error(
3254 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3255 operation->slot->data.raw.data,
3256 (unsigned int) key_bits ) );
3257 break;
3258#endif /* MBEDTLS_GCM_C */
3259
3260 default:
3261 return( PSA_ERROR_NOT_SUPPORTED );
3262 }
3263
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003264 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3265 {
3266 status = PSA_ERROR_INVALID_ARGUMENT;
3267 goto cleanup;
3268 }
3269 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3270 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3271 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3272 * In both cases, mbedtls_xxx will validate the tag length below. */
3273
Gilles Peskineedf9a652018-08-17 18:11:56 +02003274 return( PSA_SUCCESS );
3275
3276cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003277 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003278 return( status );
3279}
3280
Gilles Peskinec5487a82018-12-03 18:08:14 +01003281psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003282 psa_algorithm_t alg,
3283 const uint8_t *nonce,
3284 size_t nonce_length,
3285 const uint8_t *additional_data,
3286 size_t additional_data_length,
3287 const uint8_t *plaintext,
3288 size_t plaintext_length,
3289 uint8_t *ciphertext,
3290 size_t ciphertext_size,
3291 size_t *ciphertext_length )
3292{
mohammad16035955c982018-04-26 00:53:03 +03003293 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003294 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003295 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003296
mohammad1603f08a5502018-06-03 15:05:47 +03003297 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003298
Gilles Peskinec5487a82018-12-03 18:08:14 +01003299 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003300 if( status != PSA_SUCCESS )
3301 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003302
Gilles Peskineedf9a652018-08-17 18:11:56 +02003303 /* For all currently supported modes, the tag is at the end of the
3304 * ciphertext. */
3305 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3306 {
3307 status = PSA_ERROR_BUFFER_TOO_SMALL;
3308 goto exit;
3309 }
3310 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003311
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003312#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003313 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003314 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003315 status = mbedtls_to_psa_error(
3316 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3317 MBEDTLS_GCM_ENCRYPT,
3318 plaintext_length,
3319 nonce, nonce_length,
3320 additional_data, additional_data_length,
3321 plaintext, ciphertext,
3322 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003323 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003324 else
3325#endif /* MBEDTLS_GCM_C */
3326#if defined(MBEDTLS_CCM_C)
3327 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003328 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003329 status = mbedtls_to_psa_error(
3330 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3331 plaintext_length,
3332 nonce, nonce_length,
3333 additional_data,
3334 additional_data_length,
3335 plaintext, ciphertext,
3336 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003337 }
mohammad16035c8845f2018-05-09 05:40:09 -07003338 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003339#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003340 {
mohammad1603554faad2018-06-03 15:07:38 +03003341 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003342 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003343
Gilles Peskineedf9a652018-08-17 18:11:56 +02003344 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3345 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003346
Gilles Peskineedf9a652018-08-17 18:11:56 +02003347exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003348 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003349 if( status == PSA_SUCCESS )
3350 *ciphertext_length = plaintext_length + operation.tag_length;
3351 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003352}
3353
Gilles Peskineee652a32018-06-01 19:23:52 +02003354/* Locate the tag in a ciphertext buffer containing the encrypted data
3355 * followed by the tag. Return the length of the part preceding the tag in
3356 * *plaintext_length. This is the size of the plaintext in modes where
3357 * the encrypted data has the same size as the plaintext, such as
3358 * CCM and GCM. */
3359static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3360 const uint8_t *ciphertext,
3361 size_t ciphertext_length,
3362 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003363 const uint8_t **p_tag )
3364{
3365 size_t payload_length;
3366 if( tag_length > ciphertext_length )
3367 return( PSA_ERROR_INVALID_ARGUMENT );
3368 payload_length = ciphertext_length - tag_length;
3369 if( payload_length > plaintext_size )
3370 return( PSA_ERROR_BUFFER_TOO_SMALL );
3371 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003372 return( PSA_SUCCESS );
3373}
3374
Gilles Peskinec5487a82018-12-03 18:08:14 +01003375psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003376 psa_algorithm_t alg,
3377 const uint8_t *nonce,
3378 size_t nonce_length,
3379 const uint8_t *additional_data,
3380 size_t additional_data_length,
3381 const uint8_t *ciphertext,
3382 size_t ciphertext_length,
3383 uint8_t *plaintext,
3384 size_t plaintext_size,
3385 size_t *plaintext_length )
3386{
mohammad16035955c982018-04-26 00:53:03 +03003387 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003388 aead_operation_t operation;
3389 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003390
Gilles Peskineee652a32018-06-01 19:23:52 +02003391 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003392
Gilles Peskinec5487a82018-12-03 18:08:14 +01003393 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003394 if( status != PSA_SUCCESS )
3395 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003396
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003397#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003398 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003399 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003400 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003401 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003402 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003403 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003404 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003405
Gilles Peskineedf9a652018-08-17 18:11:56 +02003406 status = mbedtls_to_psa_error(
3407 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3408 ciphertext_length - operation.tag_length,
3409 nonce, nonce_length,
3410 additional_data,
3411 additional_data_length,
3412 tag, operation.tag_length,
3413 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003414 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003415 else
3416#endif /* MBEDTLS_GCM_C */
3417#if defined(MBEDTLS_CCM_C)
3418 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003419 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003420 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003421 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003422 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003423 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003424 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003425
Gilles Peskineedf9a652018-08-17 18:11:56 +02003426 status = mbedtls_to_psa_error(
3427 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3428 ciphertext_length - operation.tag_length,
3429 nonce, nonce_length,
3430 additional_data,
3431 additional_data_length,
3432 ciphertext, plaintext,
3433 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003434 }
mohammad160339574652018-06-01 04:39:53 -07003435 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003436#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003437 {
mohammad1603554faad2018-06-03 15:07:38 +03003438 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003439 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003440
Gilles Peskineedf9a652018-08-17 18:11:56 +02003441 if( status != PSA_SUCCESS && plaintext_size != 0 )
3442 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003443
Gilles Peskineedf9a652018-08-17 18:11:56 +02003444exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003445 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003446 if( status == PSA_SUCCESS )
3447 *plaintext_length = ciphertext_length - operation.tag_length;
3448 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003449}
3450
Gilles Peskinea0655c32018-04-30 17:06:50 +02003451
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003452
Gilles Peskine20035e32018-02-03 22:44:14 +01003453/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003454/* Generators */
3455/****************************************************************/
3456
3457psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3458{
3459 psa_status_t status = PSA_SUCCESS;
3460 if( generator->alg == 0 )
3461 {
3462 /* The object has (apparently) been initialized but it is not
3463 * in use. It's ok to call abort on such an object, and there's
3464 * nothing to do. */
3465 }
3466 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003467 if( generator->alg == PSA_ALG_SELECT_RAW )
3468 {
3469 if( generator->ctx.buffer.data != NULL )
3470 {
3471 mbedtls_zeroize( generator->ctx.buffer.data,
3472 generator->ctx.buffer.size );
3473 mbedtls_free( generator->ctx.buffer.data );
3474 }
3475 }
3476 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003477#if defined(MBEDTLS_MD_C)
3478 if( PSA_ALG_IS_HKDF( generator->alg ) )
3479 {
3480 mbedtls_free( generator->ctx.hkdf.info );
3481 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3482 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003483 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3484 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3485 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003486 {
3487 if( generator->ctx.tls12_prf.key != NULL )
3488 {
3489 mbedtls_zeroize( generator->ctx.tls12_prf.key,
3490 generator->ctx.tls12_prf.key_len );
3491 mbedtls_free( generator->ctx.tls12_prf.key );
3492 }
Hanno Becker580fba12018-11-13 20:50:45 +00003493
3494 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3495 {
3496 mbedtls_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
3497 generator->ctx.tls12_prf.Ai_with_seed_len );
3498 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3499 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003500 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003501 else
3502#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003503 {
3504 status = PSA_ERROR_BAD_STATE;
3505 }
3506 memset( generator, 0, sizeof( *generator ) );
3507 return( status );
3508}
3509
3510
3511psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3512 size_t *capacity)
3513{
3514 *capacity = generator->capacity;
3515 return( PSA_SUCCESS );
3516}
3517
Gilles Peskinebef7f142018-07-12 17:22:21 +02003518#if defined(MBEDTLS_MD_C)
3519/* Read some bytes from an HKDF-based generator. This performs a chunk
3520 * of the expand phase of the HKDF algorithm. */
3521static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3522 psa_algorithm_t hash_alg,
3523 uint8_t *output,
3524 size_t output_length )
3525{
3526 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3527 psa_status_t status;
3528
3529 while( output_length != 0 )
3530 {
3531 /* Copy what remains of the current block */
3532 uint8_t n = hash_length - hkdf->offset_in_block;
3533 if( n > output_length )
3534 n = (uint8_t) output_length;
3535 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3536 output += n;
3537 output_length -= n;
3538 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003539 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003540 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003541 /* We can't be wanting more output after block 0xff, otherwise
3542 * the capacity check in psa_generator_read() would have
3543 * prevented this call. It could happen only if the generator
3544 * object was corrupted or if this function is called directly
3545 * inside the library. */
3546 if( hkdf->block_number == 0xff )
3547 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003548
3549 /* We need a new block */
3550 ++hkdf->block_number;
3551 hkdf->offset_in_block = 0;
3552 status = psa_hmac_setup_internal( &hkdf->hmac,
3553 hkdf->prk, hash_length,
3554 hash_alg );
3555 if( status != PSA_SUCCESS )
3556 return( status );
3557 if( hkdf->block_number != 1 )
3558 {
3559 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3560 hkdf->output_block,
3561 hash_length );
3562 if( status != PSA_SUCCESS )
3563 return( status );
3564 }
3565 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3566 hkdf->info,
3567 hkdf->info_length );
3568 if( status != PSA_SUCCESS )
3569 return( status );
3570 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3571 &hkdf->block_number, 1 );
3572 if( status != PSA_SUCCESS )
3573 return( status );
3574 status = psa_hmac_finish_internal( &hkdf->hmac,
3575 hkdf->output_block,
3576 sizeof( hkdf->output_block ) );
3577 if( status != PSA_SUCCESS )
3578 return( status );
3579 }
3580
3581 return( PSA_SUCCESS );
3582}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003583
3584static psa_status_t psa_generator_tls12_prf_generate_next_block(
3585 psa_tls12_prf_generator_t *tls12_prf,
3586 psa_algorithm_t alg )
3587{
3588 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3589 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3590 psa_hmac_internal_data hmac;
3591 psa_status_t status, cleanup_status;
3592
Hanno Becker3b339e22018-11-13 20:56:14 +00003593 unsigned char *Ai;
3594 size_t Ai_len;
3595
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003596 /* We can't be wanting more output after block 0xff, otherwise
3597 * the capacity check in psa_generator_read() would have
3598 * prevented this call. It could happen only if the generator
3599 * object was corrupted or if this function is called directly
3600 * inside the library. */
3601 if( tls12_prf->block_number == 0xff )
3602 return( PSA_ERROR_BAD_STATE );
3603
3604 /* We need a new block */
3605 ++tls12_prf->block_number;
3606 tls12_prf->offset_in_block = 0;
3607
3608 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3609 *
3610 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3611 *
3612 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3613 * HMAC_hash(secret, A(2) + seed) +
3614 * HMAC_hash(secret, A(3) + seed) + ...
3615 *
3616 * A(0) = seed
3617 * A(i) = HMAC_hash( secret, A(i-1) )
3618 *
3619 * The `psa_tls12_prf_generator` structures saves the block
3620 * `HMAC_hash(secret, A(i) + seed)` from which the output
3621 * is currently extracted as `output_block`, while
3622 * `A(i) + seed` is stored in `Ai_with_seed`.
3623 *
3624 * Generating a new block means recalculating `Ai_with_seed`
3625 * from the A(i)-part of it, and afterwards recalculating
3626 * `output_block`.
3627 *
3628 * A(0) is computed at setup time.
3629 *
3630 */
3631
3632 psa_hmac_init_internal( &hmac );
3633
3634 /* We must distinguish the calculation of A(1) from those
3635 * of A(2) and higher, because A(0)=seed has a different
3636 * length than the other A(i). */
3637 if( tls12_prf->block_number == 1 )
3638 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003639 Ai = tls12_prf->Ai_with_seed + hash_length;
3640 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003641 }
3642 else
3643 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003644 Ai = tls12_prf->Ai_with_seed;
3645 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003646 }
3647
Hanno Becker3b339e22018-11-13 20:56:14 +00003648 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3649 status = psa_hmac_setup_internal( &hmac,
3650 tls12_prf->key,
3651 tls12_prf->key_len,
3652 hash_alg );
3653 if( status != PSA_SUCCESS )
3654 goto cleanup;
3655
3656 status = psa_hash_update( &hmac.hash_ctx,
3657 Ai, Ai_len );
3658 if( status != PSA_SUCCESS )
3659 goto cleanup;
3660
3661 status = psa_hmac_finish_internal( &hmac,
3662 tls12_prf->Ai_with_seed,
3663 hash_length );
3664 if( status != PSA_SUCCESS )
3665 goto cleanup;
3666
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003667 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3668 status = psa_hmac_setup_internal( &hmac,
3669 tls12_prf->key,
3670 tls12_prf->key_len,
3671 hash_alg );
3672 if( status != PSA_SUCCESS )
3673 goto cleanup;
3674
3675 status = psa_hash_update( &hmac.hash_ctx,
3676 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003677 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003678 if( status != PSA_SUCCESS )
3679 goto cleanup;
3680
3681 status = psa_hmac_finish_internal( &hmac,
3682 tls12_prf->output_block,
3683 hash_length );
3684 if( status != PSA_SUCCESS )
3685 goto cleanup;
3686
3687cleanup:
3688
3689 cleanup_status = psa_hmac_abort_internal( &hmac );
3690 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3691 status = cleanup_status;
3692
3693 return( status );
3694}
3695
3696/* Read some bytes from an TLS-1.2-PRF-based generator.
3697 * See Section 5 of RFC 5246. */
3698static psa_status_t psa_generator_tls12_prf_read(
3699 psa_tls12_prf_generator_t *tls12_prf,
3700 psa_algorithm_t alg,
3701 uint8_t *output,
3702 size_t output_length )
3703{
3704 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3705 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3706 psa_status_t status;
3707
3708 while( output_length != 0 )
3709 {
3710 /* Copy what remains of the current block */
3711 uint8_t n = hash_length - tls12_prf->offset_in_block;
3712
3713 /* Check if we have fully processed the current block. */
3714 if( n == 0 )
3715 {
3716 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3717 alg );
3718 if( status != PSA_SUCCESS )
3719 return( status );
3720
3721 continue;
3722 }
3723
3724 if( n > output_length )
3725 n = (uint8_t) output_length;
3726 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3727 n );
3728 output += n;
3729 output_length -= n;
3730 tls12_prf->offset_in_block += n;
3731 }
3732
3733 return( PSA_SUCCESS );
3734}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003735#endif /* MBEDTLS_MD_C */
3736
Gilles Peskineeab56e42018-07-12 17:12:33 +02003737psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3738 uint8_t *output,
3739 size_t output_length )
3740{
3741 psa_status_t status;
3742
3743 if( output_length > generator->capacity )
3744 {
3745 generator->capacity = 0;
3746 /* Go through the error path to wipe all confidential data now
3747 * that the generator object is useless. */
3748 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3749 goto exit;
3750 }
3751 if( output_length == 0 &&
3752 generator->capacity == 0 && generator->alg == 0 )
3753 {
3754 /* Edge case: this is a blank or finished generator, and 0
3755 * bytes were requested. The right error in this case could
3756 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3757 * INSUFFICIENT_CAPACITY, which is right for a finished
3758 * generator, for consistency with the case when
3759 * output_length > 0. */
3760 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3761 }
3762 generator->capacity -= output_length;
3763
Gilles Peskine751d9652018-09-18 12:05:44 +02003764 if( generator->alg == PSA_ALG_SELECT_RAW )
3765 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003766 /* Initially, the capacity of a selection generator is always
3767 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3768 * abbreviated in this comment as `size`. When the remaining
3769 * capacity is `c`, the next bytes to serve start `c` bytes
3770 * from the end of the buffer, i.e. `size - c` from the
3771 * beginning of the buffer. Since `generator->capacity` was just
3772 * decremented above, we need to serve the bytes from
3773 * `size - generator->capacity - output_length` to
3774 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003775 size_t offset =
3776 generator->ctx.buffer.size - generator->capacity - output_length;
3777 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3778 status = PSA_SUCCESS;
3779 }
3780 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003781#if defined(MBEDTLS_MD_C)
3782 if( PSA_ALG_IS_HKDF( generator->alg ) )
3783 {
3784 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3785 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3786 output, output_length );
3787 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003788 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3789 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003790 {
3791 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3792 generator->alg, output,
3793 output_length );
3794 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003795 else
3796#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003797 {
3798 return( PSA_ERROR_BAD_STATE );
3799 }
3800
3801exit:
3802 if( status != PSA_SUCCESS )
3803 {
3804 psa_generator_abort( generator );
3805 memset( output, '!', output_length );
3806 }
3807 return( status );
3808}
3809
Gilles Peskine08542d82018-07-19 17:05:42 +02003810#if defined(MBEDTLS_DES_C)
3811static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3812{
3813 if( data_size >= 8 )
3814 mbedtls_des_key_set_parity( data );
3815 if( data_size >= 16 )
3816 mbedtls_des_key_set_parity( data + 8 );
3817 if( data_size >= 24 )
3818 mbedtls_des_key_set_parity( data + 16 );
3819}
3820#endif /* MBEDTLS_DES_C */
3821
Gilles Peskinec5487a82018-12-03 18:08:14 +01003822psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003823 psa_key_type_t type,
3824 size_t bits,
3825 psa_crypto_generator_t *generator )
3826{
3827 uint8_t *data = NULL;
3828 size_t bytes = PSA_BITS_TO_BYTES( bits );
3829 psa_status_t status;
3830
3831 if( ! key_type_is_raw_bytes( type ) )
3832 return( PSA_ERROR_INVALID_ARGUMENT );
3833 if( bits % 8 != 0 )
3834 return( PSA_ERROR_INVALID_ARGUMENT );
3835 data = mbedtls_calloc( 1, bytes );
3836 if( data == NULL )
3837 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3838
3839 status = psa_generator_read( generator, data, bytes );
3840 if( status != PSA_SUCCESS )
3841 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003842#if defined(MBEDTLS_DES_C)
3843 if( type == PSA_KEY_TYPE_DES )
3844 psa_des_set_key_parity( data, bytes );
3845#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003846 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003847
3848exit:
3849 mbedtls_free( data );
3850 return( status );
3851}
3852
3853
3854
3855/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003856/* Key derivation */
3857/****************************************************************/
3858
Gilles Peskinea05219c2018-11-16 16:02:56 +01003859#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003860/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003861 * of the HKDF algorithm.
3862 *
3863 * Note that if this function fails, you must call psa_generator_abort()
3864 * to potentially free embedded data structures and wipe confidential data.
3865 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003866static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003867 const uint8_t *secret,
3868 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003869 psa_algorithm_t hash_alg,
3870 const uint8_t *salt,
3871 size_t salt_length,
3872 const uint8_t *label,
3873 size_t label_length )
3874{
3875 psa_status_t status;
3876 status = psa_hmac_setup_internal( &hkdf->hmac,
3877 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003878 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003879 if( status != PSA_SUCCESS )
3880 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003881 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003882 if( status != PSA_SUCCESS )
3883 return( status );
3884 status = psa_hmac_finish_internal( &hkdf->hmac,
3885 hkdf->prk,
3886 sizeof( hkdf->prk ) );
3887 if( status != PSA_SUCCESS )
3888 return( status );
3889 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3890 hkdf->block_number = 0;
3891 hkdf->info_length = label_length;
3892 if( label_length != 0 )
3893 {
3894 hkdf->info = mbedtls_calloc( 1, label_length );
3895 if( hkdf->info == NULL )
3896 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3897 memcpy( hkdf->info, label, label_length );
3898 }
3899 return( PSA_SUCCESS );
3900}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003901#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003902
Gilles Peskinea05219c2018-11-16 16:02:56 +01003903#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003904/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3905 *
3906 * Note that if this function fails, you must call psa_generator_abort()
3907 * to potentially free embedded data structures and wipe confidential data.
3908 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003909static psa_status_t psa_generator_tls12_prf_setup(
3910 psa_tls12_prf_generator_t *tls12_prf,
3911 const unsigned char *key,
3912 size_t key_len,
3913 psa_algorithm_t hash_alg,
3914 const uint8_t *salt,
3915 size_t salt_length,
3916 const uint8_t *label,
3917 size_t label_length )
3918{
3919 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003920 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3921 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003922
3923 tls12_prf->key = mbedtls_calloc( 1, key_len );
3924 if( tls12_prf->key == NULL )
3925 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3926 tls12_prf->key_len = key_len;
3927 memcpy( tls12_prf->key, key, key_len );
3928
Hanno Becker580fba12018-11-13 20:50:45 +00003929 overflow = ( salt_length + label_length < salt_length ) ||
3930 ( salt_length + label_length + hash_length < hash_length );
3931 if( overflow )
3932 return( PSA_ERROR_INVALID_ARGUMENT );
3933
3934 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3935 if( tls12_prf->Ai_with_seed == NULL )
3936 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3937 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3938
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003939 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3940 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003941 if( label_length != 0 )
3942 {
3943 memcpy( tls12_prf->Ai_with_seed + hash_length,
3944 label, label_length );
3945 }
3946
3947 if( salt_length != 0 )
3948 {
3949 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3950 salt, salt_length );
3951 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003952
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003953 /* The first block gets generated when
3954 * psa_generator_read() is called. */
3955 tls12_prf->block_number = 0;
3956 tls12_prf->offset_in_block = hash_length;
3957
3958 return( PSA_SUCCESS );
3959}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003960
3961/* Set up a TLS-1.2-PSK-to-MS-based generator. */
3962static psa_status_t psa_generator_tls12_psk_to_ms_setup(
3963 psa_tls12_prf_generator_t *tls12_prf,
3964 const unsigned char *psk,
3965 size_t psk_len,
3966 psa_algorithm_t hash_alg,
3967 const uint8_t *salt,
3968 size_t salt_length,
3969 const uint8_t *label,
3970 size_t label_length )
3971{
3972 psa_status_t status;
3973 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
3974
3975 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
3976 return( PSA_ERROR_INVALID_ARGUMENT );
3977
3978 /* Quoting RFC 4279, Section 2:
3979 *
3980 * The premaster secret is formed as follows: if the PSK is N octets
3981 * long, concatenate a uint16 with the value N, N zero octets, a second
3982 * uint16 with the value N, and the PSK itself.
3983 */
3984
3985 pms[0] = ( psk_len >> 8 ) & 0xff;
3986 pms[1] = ( psk_len >> 0 ) & 0xff;
3987 memset( pms + 2, 0, psk_len );
3988 pms[2 + psk_len + 0] = pms[0];
3989 pms[2 + psk_len + 1] = pms[1];
3990 memcpy( pms + 4 + psk_len, psk, psk_len );
3991
3992 status = psa_generator_tls12_prf_setup( tls12_prf,
3993 pms, 4 + 2 * psk_len,
3994 hash_alg,
3995 salt, salt_length,
3996 label, label_length );
3997
3998 mbedtls_zeroize( pms, sizeof( pms ) );
3999 return( status );
4000}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004001#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004002
Gilles Peskine346797d2018-11-16 16:05:06 +01004003/* Note that if this function fails, you must call psa_generator_abort()
4004 * to potentially free embedded data structures and wipe confidential data.
4005 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004006static psa_status_t psa_key_derivation_internal(
4007 psa_crypto_generator_t *generator,
4008 const uint8_t *secret, size_t secret_length,
4009 psa_algorithm_t alg,
4010 const uint8_t *salt, size_t salt_length,
4011 const uint8_t *label, size_t label_length,
4012 size_t capacity )
4013{
4014 psa_status_t status;
4015 size_t max_capacity;
4016
4017 /* Set generator->alg even on failure so that abort knows what to do. */
4018 generator->alg = alg;
4019
Gilles Peskine751d9652018-09-18 12:05:44 +02004020 if( alg == PSA_ALG_SELECT_RAW )
4021 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004022 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004023 if( salt_length != 0 )
4024 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004025 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004026 if( label_length != 0 )
4027 return( PSA_ERROR_INVALID_ARGUMENT );
4028 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4029 if( generator->ctx.buffer.data == NULL )
4030 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4031 memcpy( generator->ctx.buffer.data, secret, secret_length );
4032 generator->ctx.buffer.size = secret_length;
4033 max_capacity = secret_length;
4034 status = PSA_SUCCESS;
4035 }
4036 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004037#if defined(MBEDTLS_MD_C)
4038 if( PSA_ALG_IS_HKDF( alg ) )
4039 {
4040 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4041 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4042 if( hash_size == 0 )
4043 return( PSA_ERROR_NOT_SUPPORTED );
4044 max_capacity = 255 * hash_size;
4045 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4046 secret, secret_length,
4047 hash_alg,
4048 salt, salt_length,
4049 label, label_length );
4050 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004051 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4052 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4053 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004054 {
4055 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4056 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4057
4058 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4059 if( hash_alg != PSA_ALG_SHA_256 &&
4060 hash_alg != PSA_ALG_SHA_384 )
4061 {
4062 return( PSA_ERROR_NOT_SUPPORTED );
4063 }
4064
4065 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004066
4067 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4068 {
4069 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4070 secret, secret_length,
4071 hash_alg, salt, salt_length,
4072 label, label_length );
4073 }
4074 else
4075 {
4076 status = psa_generator_tls12_psk_to_ms_setup(
4077 &generator->ctx.tls12_prf,
4078 secret, secret_length,
4079 hash_alg, salt, salt_length,
4080 label, label_length );
4081 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004082 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004083 else
4084#endif
4085 {
4086 return( PSA_ERROR_NOT_SUPPORTED );
4087 }
4088
4089 if( status != PSA_SUCCESS )
4090 return( status );
4091
4092 if( capacity <= max_capacity )
4093 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004094 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4095 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004096 else
4097 return( PSA_ERROR_INVALID_ARGUMENT );
4098
4099 return( PSA_SUCCESS );
4100}
4101
Gilles Peskineea0fb492018-07-12 17:17:20 +02004102psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004103 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004104 psa_algorithm_t alg,
4105 const uint8_t *salt,
4106 size_t salt_length,
4107 const uint8_t *label,
4108 size_t label_length,
4109 size_t capacity )
4110{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004111 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004112 psa_status_t status;
4113
4114 if( generator->alg != 0 )
4115 return( PSA_ERROR_BAD_STATE );
4116
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004117 /* Make sure that alg is a key derivation algorithm. This prevents
4118 * key selection algorithms, which psa_key_derivation_internal
4119 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004120 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4121 return( PSA_ERROR_INVALID_ARGUMENT );
4122
Gilles Peskinec5487a82018-12-03 18:08:14 +01004123 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004124 if( status != PSA_SUCCESS )
4125 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004126
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004127 if( slot->type != PSA_KEY_TYPE_DERIVE )
4128 return( PSA_ERROR_INVALID_ARGUMENT );
4129
4130 status = psa_key_derivation_internal( generator,
4131 slot->data.raw.data,
4132 slot->data.raw.bytes,
4133 alg,
4134 salt, salt_length,
4135 label, label_length,
4136 capacity );
4137 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004138 psa_generator_abort( generator );
4139 return( status );
4140}
4141
4142
4143
4144/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004145/* Key agreement */
4146/****************************************************************/
4147
Gilles Peskinea05219c2018-11-16 16:02:56 +01004148#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004149static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4150 size_t peer_key_length,
4151 const mbedtls_ecp_keypair *our_key,
4152 uint8_t *shared_secret,
4153 size_t shared_secret_size,
4154 size_t *shared_secret_length )
4155{
4156 mbedtls_pk_context pk;
4157 mbedtls_ecp_keypair *their_key = NULL;
4158 mbedtls_ecdh_context ecdh;
4159 int ret;
4160 mbedtls_ecdh_init( &ecdh );
4161 mbedtls_pk_init( &pk );
4162
4163 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
4164 if( ret != 0 )
4165 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02004166 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004167 {
Gilles Peskine88714d72018-10-25 23:07:25 +02004168 case MBEDTLS_PK_ECKEY:
4169 case MBEDTLS_PK_ECKEY_DH:
4170 break;
4171 default:
4172 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4173 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004174 }
4175 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01004176 if( their_key->grp.id != our_key->grp.id )
4177 {
4178 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4179 goto exit;
4180 }
4181
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004182 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
4183 if( ret != 0 )
4184 goto exit;
4185 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
4186 if( ret != 0 )
4187 goto exit;
4188
4189 ret = mbedtls_ecdh_calc_secret( &ecdh,
4190 shared_secret_length,
4191 shared_secret, shared_secret_size,
4192 mbedtls_ctr_drbg_random,
4193 &global_data.ctr_drbg );
4194
4195exit:
4196 mbedtls_pk_free( &pk );
4197 mbedtls_ecdh_free( &ecdh );
4198 return( mbedtls_to_psa_error( ret ) );
4199}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004200#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004201
Gilles Peskine01d718c2018-09-18 12:01:02 +02004202#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4203
Gilles Peskine346797d2018-11-16 16:05:06 +01004204/* Note that if this function fails, you must call psa_generator_abort()
4205 * to potentially free embedded data structures and wipe confidential data.
4206 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004207static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004208 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004209 const uint8_t *peer_key,
4210 size_t peer_key_length,
4211 psa_algorithm_t alg )
4212{
4213 psa_status_t status;
4214 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4215 size_t shared_secret_length = 0;
4216
4217 /* Step 1: run the secret agreement algorithm to generate the shared
4218 * secret. */
4219 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4220 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004221#if defined(MBEDTLS_ECDH_C)
4222 case PSA_ALG_ECDH_BASE:
4223 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4224 return( PSA_ERROR_INVALID_ARGUMENT );
4225 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4226 private_key->data.ecp,
4227 shared_secret,
4228 sizeof( shared_secret ),
4229 &shared_secret_length );
4230 break;
4231#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004232 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004233 (void) private_key;
4234 (void) peer_key;
4235 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004236 return( PSA_ERROR_NOT_SUPPORTED );
4237 }
4238 if( status != PSA_SUCCESS )
4239 goto exit;
4240
4241 /* Step 2: set up the key derivation to generate key material from
4242 * the shared secret. */
4243 status = psa_key_derivation_internal( generator,
4244 shared_secret, shared_secret_length,
4245 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4246 NULL, 0, NULL, 0,
4247 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4248exit:
4249 mbedtls_zeroize( shared_secret, shared_secret_length );
4250 return( status );
4251}
4252
4253psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004254 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004255 const uint8_t *peer_key,
4256 size_t peer_key_length,
4257 psa_algorithm_t alg )
4258{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004259 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004260 psa_status_t status;
4261 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4262 return( PSA_ERROR_INVALID_ARGUMENT );
4263 status = psa_get_key_from_slot( private_key, &slot,
4264 PSA_KEY_USAGE_DERIVE, alg );
4265 if( status != PSA_SUCCESS )
4266 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004267 status = psa_key_agreement_internal( generator,
4268 slot,
4269 peer_key, peer_key_length,
4270 alg );
4271 if( status != PSA_SUCCESS )
4272 psa_generator_abort( generator );
4273 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004274}
4275
4276
4277
4278/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004279/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004280/****************************************************************/
4281
4282psa_status_t psa_generate_random( uint8_t *output,
4283 size_t output_size )
4284{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004285 int ret;
4286 GUARD_MODULE_INITIALIZED;
4287
4288 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004289 return( mbedtls_to_psa_error( ret ) );
4290}
4291
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004292#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004293
4294/* Support function for error conversion between psa_its error codes to psa crypto */
4295static psa_status_t its_to_psa_error( psa_its_status_t ret )
4296{
4297 switch( ret )
4298 {
4299 case PSA_ITS_SUCCESS:
4300 return( PSA_SUCCESS );
4301
4302 case PSA_ITS_ERROR_KEY_NOT_FOUND:
4303 return( PSA_ERROR_EMPTY_SLOT );
4304
4305 case PSA_ITS_ERROR_STORAGE_FAILURE:
4306 return( PSA_ERROR_STORAGE_FAILURE );
4307
4308 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4309 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4310
4311 case PSA_ITS_ERROR_INVALID_KEY:
4312 case PSA_PS_ERROR_OFFSET_INVALID:
4313 case PSA_ITS_ERROR_INCORRECT_SIZE:
4314 case PSA_ITS_ERROR_BAD_POINTER:
4315 return( PSA_ERROR_INVALID_ARGUMENT );
4316
4317 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4318 return( PSA_ERROR_NOT_SUPPORTED );
4319
4320 case PSA_ITS_ERROR_WRITE_ONCE:
4321 return( PSA_ERROR_OCCUPIED_SLOT );
4322
4323 default:
4324 return( PSA_ERROR_UNKNOWN_ERROR );
4325 }
4326}
4327
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004328psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4329 size_t seed_size )
4330{
4331 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004332 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004333 struct psa_its_info_t p_info;
4334 if( global_data.initialized )
4335 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004336
4337 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4338 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4339 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4340 return( PSA_ERROR_INVALID_ARGUMENT );
4341
avolinski0d2c2662018-11-21 17:31:07 +02004342 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004343 status = its_to_psa_error( its_status );
4344
4345 if( PSA_ITS_ERROR_KEY_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004346 {
avolinski0d2c2662018-11-21 17:31:07 +02004347 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004348 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004349 }
avolinski13beb102018-11-20 16:51:49 +02004350 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004351 {
4352 /* You should not be here. Seed needs to be injected only once */
4353 status = PSA_ERROR_NOT_PERMITTED;
4354 }
4355 return( status );
4356}
4357#endif
4358
Gilles Peskinec5487a82018-12-03 18:08:14 +01004359psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004360 psa_key_type_t type,
4361 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004362 const void *extra,
4363 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004364{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004365 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004366 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004367
Gilles Peskine53d991e2018-07-12 01:14:59 +02004368 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004369 return( PSA_ERROR_INVALID_ARGUMENT );
4370
Gilles Peskinec5487a82018-12-03 18:08:14 +01004371 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004372 if( status != PSA_SUCCESS )
4373 return( status );
4374
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004375 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004376 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004377 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004378 if( status != PSA_SUCCESS )
4379 return( status );
4380 status = psa_generate_random( slot->data.raw.data,
4381 slot->data.raw.bytes );
4382 if( status != PSA_SUCCESS )
4383 {
4384 mbedtls_free( slot->data.raw.data );
4385 return( status );
4386 }
4387#if defined(MBEDTLS_DES_C)
4388 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004389 psa_des_set_key_parity( slot->data.raw.data,
4390 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004391#endif /* MBEDTLS_DES_C */
4392 }
4393 else
4394
4395#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4396 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4397 {
4398 mbedtls_rsa_context *rsa;
4399 int ret;
4400 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004401 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4402 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004403 /* Accept only byte-aligned keys, for the same reasons as
4404 * in psa_import_rsa_key(). */
4405 if( bits % 8 != 0 )
4406 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004407 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004408 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004409 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004410 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004411 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004412#if INT_MAX < 0xffffffff
4413 /* Check that the uint32_t value passed by the caller fits
4414 * in the range supported by this implementation. */
4415 if( p->e > INT_MAX )
4416 return( PSA_ERROR_NOT_SUPPORTED );
4417#endif
4418 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004419 }
4420 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4421 if( rsa == NULL )
4422 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4423 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4424 ret = mbedtls_rsa_gen_key( rsa,
4425 mbedtls_ctr_drbg_random,
4426 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004427 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004428 exponent );
4429 if( ret != 0 )
4430 {
4431 mbedtls_rsa_free( rsa );
4432 mbedtls_free( rsa );
4433 return( mbedtls_to_psa_error( ret ) );
4434 }
4435 slot->data.rsa = rsa;
4436 }
4437 else
4438#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4439
4440#if defined(MBEDTLS_ECP_C)
4441 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4442 {
4443 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4444 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4445 const mbedtls_ecp_curve_info *curve_info =
4446 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4447 mbedtls_ecp_keypair *ecp;
4448 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004449 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004450 return( PSA_ERROR_NOT_SUPPORTED );
4451 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4452 return( PSA_ERROR_NOT_SUPPORTED );
4453 if( curve_info->bit_size != bits )
4454 return( PSA_ERROR_INVALID_ARGUMENT );
4455 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4456 if( ecp == NULL )
4457 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4458 mbedtls_ecp_keypair_init( ecp );
4459 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4460 mbedtls_ctr_drbg_random,
4461 &global_data.ctr_drbg );
4462 if( ret != 0 )
4463 {
4464 mbedtls_ecp_keypair_free( ecp );
4465 mbedtls_free( ecp );
4466 return( mbedtls_to_psa_error( ret ) );
4467 }
4468 slot->data.ecp = ecp;
4469 }
4470 else
4471#endif /* MBEDTLS_ECP_C */
4472
4473 return( PSA_ERROR_NOT_SUPPORTED );
4474
4475 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004476
4477#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4478 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4479 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004480 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004481 }
4482#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4483
4484 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004485}
4486
4487
4488/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004489/* Module setup */
4490/****************************************************************/
4491
Gilles Peskine5e769522018-11-20 21:59:56 +01004492psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4493 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4494 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4495{
4496 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4497 return( PSA_ERROR_BAD_STATE );
4498 global_data.entropy_init = entropy_init;
4499 global_data.entropy_free = entropy_free;
4500 return( PSA_SUCCESS );
4501}
4502
Gilles Peskinee59236f2018-01-27 23:32:46 +01004503void mbedtls_psa_crypto_free( void )
4504{
Gilles Peskinec6b69072018-11-20 21:42:52 +01004505 if( global_data.key_slots_initialized )
Darryl Green40225ba2018-11-15 14:48:15 +00004506 {
Gilles Peskinec5487a82018-12-03 18:08:14 +01004507 psa_key_handle_t key;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004508 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
4509 {
Gilles Peskine2f060a82018-12-04 17:12:32 +01004510 psa_key_slot_t *slot = &global_data.key_slots[key - 1];
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01004511 (void) psa_wipe_key_slot( slot );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004512 }
Darryl Green40225ba2018-11-15 14:48:15 +00004513 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01004514 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4515 {
4516 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004517 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004518 }
4519 /* Wipe all remaining data, including configuration.
4520 * In particular, this sets all state indicator to the value
4521 * indicating "uninitialized". */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004522 mbedtls_zeroize( &global_data, sizeof( global_data ) );
4523}
4524
4525psa_status_t psa_crypto_init( void )
4526{
4527 int ret;
4528 const unsigned char drbg_seed[] = "PSA";
4529
Gilles Peskinec6b69072018-11-20 21:42:52 +01004530 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004531 if( global_data.initialized != 0 )
4532 return( PSA_SUCCESS );
4533
Gilles Peskine5e769522018-11-20 21:59:56 +01004534 /* Set default configuration if
4535 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4536 if( global_data.entropy_init == NULL )
4537 global_data.entropy_init = mbedtls_entropy_init;
4538 if( global_data.entropy_free == NULL )
4539 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004540
Gilles Peskinec6b69072018-11-20 21:42:52 +01004541 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004542 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004543 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004544 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004545 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4546 mbedtls_entropy_func,
4547 &global_data.entropy,
4548 drbg_seed, sizeof( drbg_seed ) - 1 );
4549 if( ret != 0 )
4550 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004551 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004552
Gilles Peskinec6b69072018-11-20 21:42:52 +01004553 /* Initialize the key slots. Zero-initialization has made all key
4554 * slots empty, so there is nothing to do. In a future version we will
4555 * load data from storage. */
4556 global_data.key_slots_initialized = 1;
4557
4558 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004559 global_data.initialized = 1;
4560
Gilles Peskinee59236f2018-01-27 23:32:46 +01004561exit:
4562 if( ret != 0 )
4563 mbedtls_psa_crypto_free( );
4564 return( mbedtls_to_psa_error( ret ) );
4565}
4566
4567#endif /* MBEDTLS_PSA_CRYPTO_C */