blob: 233a19edeb6a21929b9d6651f5fd72156e9e7d5d [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"
Darryl Greend49a4992018-06-18 17:27:26 +010047/* Include internal declarations that are useful for implementing persistently
48 * stored keys. */
49#include "psa_crypto_storage.h"
50
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010051#include <stdlib.h>
52#include <string.h>
53#if defined(MBEDTLS_PLATFORM_C)
54#include "mbedtls/platform.h"
55#else
56#define mbedtls_calloc calloc
57#define mbedtls_free free
58#endif
59
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020061#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020062#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/blowfish.h"
64#include "mbedtls/camellia.h"
65#include "mbedtls/cipher.h"
66#include "mbedtls/ccm.h"
67#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010068#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010069#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020070#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010071#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010072#include "mbedtls/entropy.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020073#include "mbedtls/entropy_poll.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074#include "mbedtls/error.h"
75#include "mbedtls/gcm.h"
76#include "mbedtls/md2.h"
77#include "mbedtls/md4.h"
78#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010079#include "mbedtls/md.h"
80#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010081#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010082#include "mbedtls/pk_internal.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010083#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010084#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010085#include "mbedtls/sha1.h"
86#include "mbedtls/sha256.h"
87#include "mbedtls/sha512.h"
88#include "mbedtls/xtea.h"
89
Netanel Gonen2bcd3122018-11-19 11:53:02 +020090#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
91#include "psa_prot_internal_storage.h"
92#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +010093
Gilles Peskine996deb12018-08-01 15:45:45 +020094#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
95
Gilles Peskinee59236f2018-01-27 23:32:46 +010096/* Implementation that should never be optimized out by the compiler */
97static void mbedtls_zeroize( void *v, size_t n )
98{
99 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
100}
101
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100102/* constant-time buffer comparison */
103static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
104{
105 size_t i;
106 unsigned char diff = 0;
107
108 for( i = 0; i < n; i++ )
109 diff |= a[i] ^ b[i];
110
111 return( diff );
112}
113
114
115
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100116/****************************************************************/
117/* Global data, support functions and library management */
118/****************************************************************/
119
120/* Number of key slots (plus one because 0 is not used).
121 * The value is a compile-time constant for now, for simplicity. */
Gilles Peskine828ed142018-06-18 23:25:51 +0200122#define PSA_KEY_SLOT_COUNT 32
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100123
Gilles Peskine2d277862018-06-18 15:41:12 +0200124typedef struct
125{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100126 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +0300127 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200128 psa_key_lifetime_t lifetime;
Gilles Peskine2d277862018-06-18 15:41:12 +0200129 union
130 {
131 struct raw_data
132 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100133 uint8_t *data;
134 size_t bytes;
135 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100136#if defined(MBEDTLS_RSA_C)
137 mbedtls_rsa_context *rsa;
138#endif /* MBEDTLS_RSA_C */
139#if defined(MBEDTLS_ECP_C)
140 mbedtls_ecp_keypair *ecp;
141#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100142 } data;
143} key_slot_t;
144
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200145static int key_type_is_raw_bytes( psa_key_type_t type )
146{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200147 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200148}
149
Gilles Peskinec6b69072018-11-20 21:42:52 +0100150enum rng_state
151{
152 RNG_NOT_INITIALIZED = 0,
153 RNG_INITIALIZED,
154 RNG_SEEDED,
155};
156
Gilles Peskine2d277862018-06-18 15:41:12 +0200157typedef struct
158{
Gilles Peskine5e769522018-11-20 21:59:56 +0100159 void (* entropy_init )( mbedtls_entropy_context *ctx );
160 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100161 mbedtls_entropy_context entropy;
162 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200163 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinec6b69072018-11-20 21:42:52 +0100164 unsigned initialized : 1;
165 enum rng_state rng_state : 2;
166 unsigned key_slots_initialized : 1;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100167} psa_global_data_t;
168
169static psa_global_data_t global_data;
170
itayzafrir0adf0fc2018-09-06 16:24:41 +0300171#define GUARD_MODULE_INITIALIZED \
172 if( global_data.initialized == 0 ) \
173 return( PSA_ERROR_BAD_STATE );
174
Gilles Peskinee59236f2018-01-27 23:32:46 +0100175static psa_status_t mbedtls_to_psa_error( int ret )
176{
Gilles Peskinea5905292018-02-07 20:59:33 +0100177 /* If there's both a high-level code and low-level code, dispatch on
178 * the high-level code. */
179 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100180 {
181 case 0:
182 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100183
184 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
185 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
186 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
187 return( PSA_ERROR_NOT_SUPPORTED );
188 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
189 return( PSA_ERROR_HARDWARE_FAILURE );
190
191 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
192 return( PSA_ERROR_HARDWARE_FAILURE );
193
Gilles Peskine9a944802018-06-21 09:35:35 +0200194 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
195 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
196 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
197 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
198 case MBEDTLS_ERR_ASN1_INVALID_DATA:
199 return( PSA_ERROR_INVALID_ARGUMENT );
200 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
201 return( PSA_ERROR_INSUFFICIENT_MEMORY );
202 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
203 return( PSA_ERROR_BUFFER_TOO_SMALL );
204
Gilles Peskinea5905292018-02-07 20:59:33 +0100205 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
206 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
207 return( PSA_ERROR_NOT_SUPPORTED );
208 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
209 return( PSA_ERROR_HARDWARE_FAILURE );
210
211 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
212 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
213 return( PSA_ERROR_NOT_SUPPORTED );
214 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
215 return( PSA_ERROR_HARDWARE_FAILURE );
216
217 case MBEDTLS_ERR_CCM_BAD_INPUT:
218 return( PSA_ERROR_INVALID_ARGUMENT );
219 case MBEDTLS_ERR_CCM_AUTH_FAILED:
220 return( PSA_ERROR_INVALID_SIGNATURE );
221 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
222 return( PSA_ERROR_HARDWARE_FAILURE );
223
224 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
225 return( PSA_ERROR_NOT_SUPPORTED );
226 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
227 return( PSA_ERROR_INVALID_ARGUMENT );
228 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
229 return( PSA_ERROR_INSUFFICIENT_MEMORY );
230 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
231 return( PSA_ERROR_INVALID_PADDING );
232 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
233 return( PSA_ERROR_BAD_STATE );
234 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
235 return( PSA_ERROR_INVALID_SIGNATURE );
236 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
237 return( PSA_ERROR_TAMPERING_DETECTED );
238 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
241 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
244 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
245 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
246 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
247 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
248 return( PSA_ERROR_NOT_SUPPORTED );
249 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
250 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
251
252 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
253 return( PSA_ERROR_NOT_SUPPORTED );
254 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
255 return( PSA_ERROR_HARDWARE_FAILURE );
256
Gilles Peskinee59236f2018-01-27 23:32:46 +0100257 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
258 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
259 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
260 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100261
262 case MBEDTLS_ERR_GCM_AUTH_FAILED:
263 return( PSA_ERROR_INVALID_SIGNATURE );
264 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200265 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100266 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
267 return( PSA_ERROR_HARDWARE_FAILURE );
268
269 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
270 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
271 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
272 return( PSA_ERROR_HARDWARE_FAILURE );
273
274 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
275 return( PSA_ERROR_NOT_SUPPORTED );
276 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_MD_ALLOC_FAILED:
279 return( PSA_ERROR_INSUFFICIENT_MEMORY );
280 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
281 return( PSA_ERROR_STORAGE_FAILURE );
282 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
283 return( PSA_ERROR_HARDWARE_FAILURE );
284
Gilles Peskinef76aa772018-10-29 19:24:33 +0100285 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
286 return( PSA_ERROR_STORAGE_FAILURE );
287 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
288 return( PSA_ERROR_INVALID_ARGUMENT );
289 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
290 return( PSA_ERROR_INVALID_ARGUMENT );
291 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
292 return( PSA_ERROR_BUFFER_TOO_SMALL );
293 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
294 return( PSA_ERROR_INVALID_ARGUMENT );
295 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
296 return( PSA_ERROR_INVALID_ARGUMENT );
297 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
298 return( PSA_ERROR_INVALID_ARGUMENT );
299 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
300 return( PSA_ERROR_INSUFFICIENT_MEMORY );
301
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100302 case MBEDTLS_ERR_PK_ALLOC_FAILED:
303 return( PSA_ERROR_INSUFFICIENT_MEMORY );
304 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
305 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100308 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100309 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
310 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
311 return( PSA_ERROR_INVALID_ARGUMENT );
312 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
313 return( PSA_ERROR_NOT_SUPPORTED );
314 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
315 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
316 return( PSA_ERROR_NOT_PERMITTED );
317 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
318 return( PSA_ERROR_INVALID_ARGUMENT );
319 case MBEDTLS_ERR_PK_INVALID_ALG:
320 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
321 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
322 return( PSA_ERROR_NOT_SUPPORTED );
323 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
324 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100325 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327
328 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330
331 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
332 return( PSA_ERROR_INVALID_ARGUMENT );
333 case MBEDTLS_ERR_RSA_INVALID_PADDING:
334 return( PSA_ERROR_INVALID_PADDING );
335 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
336 return( PSA_ERROR_HARDWARE_FAILURE );
337 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
338 return( PSA_ERROR_INVALID_ARGUMENT );
339 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
340 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
341 return( PSA_ERROR_TAMPERING_DETECTED );
342 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
343 return( PSA_ERROR_INVALID_SIGNATURE );
344 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
345 return( PSA_ERROR_BUFFER_TOO_SMALL );
346 case MBEDTLS_ERR_RSA_RNG_FAILED:
347 return( PSA_ERROR_INSUFFICIENT_MEMORY );
348 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
349 return( PSA_ERROR_NOT_SUPPORTED );
350 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
351 return( PSA_ERROR_HARDWARE_FAILURE );
352
353 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
354 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
355 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
356 return( PSA_ERROR_HARDWARE_FAILURE );
357
358 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
359 return( PSA_ERROR_INVALID_ARGUMENT );
360 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
361 return( PSA_ERROR_HARDWARE_FAILURE );
362
itayzafrir5c753392018-05-08 11:18:38 +0300363 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300364 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300365 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300366 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
367 return( PSA_ERROR_BUFFER_TOO_SMALL );
368 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
369 return( PSA_ERROR_NOT_SUPPORTED );
370 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
371 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
372 return( PSA_ERROR_INVALID_SIGNATURE );
373 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
374 return( PSA_ERROR_INSUFFICIENT_MEMORY );
375 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
376 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300377
Gilles Peskinee59236f2018-01-27 23:32:46 +0100378 default:
379 return( PSA_ERROR_UNKNOWN_ERROR );
380 }
381}
382
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200383
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200384
385
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100386/****************************************************************/
387/* Key management */
388/****************************************************************/
389
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100390#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200391static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
392{
393 switch( grpid )
394 {
395 case MBEDTLS_ECP_DP_SECP192R1:
396 return( PSA_ECC_CURVE_SECP192R1 );
397 case MBEDTLS_ECP_DP_SECP224R1:
398 return( PSA_ECC_CURVE_SECP224R1 );
399 case MBEDTLS_ECP_DP_SECP256R1:
400 return( PSA_ECC_CURVE_SECP256R1 );
401 case MBEDTLS_ECP_DP_SECP384R1:
402 return( PSA_ECC_CURVE_SECP384R1 );
403 case MBEDTLS_ECP_DP_SECP521R1:
404 return( PSA_ECC_CURVE_SECP521R1 );
405 case MBEDTLS_ECP_DP_BP256R1:
406 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
407 case MBEDTLS_ECP_DP_BP384R1:
408 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
409 case MBEDTLS_ECP_DP_BP512R1:
410 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
411 case MBEDTLS_ECP_DP_CURVE25519:
412 return( PSA_ECC_CURVE_CURVE25519 );
413 case MBEDTLS_ECP_DP_SECP192K1:
414 return( PSA_ECC_CURVE_SECP192K1 );
415 case MBEDTLS_ECP_DP_SECP224K1:
416 return( PSA_ECC_CURVE_SECP224K1 );
417 case MBEDTLS_ECP_DP_SECP256K1:
418 return( PSA_ECC_CURVE_SECP256K1 );
419 case MBEDTLS_ECP_DP_CURVE448:
420 return( PSA_ECC_CURVE_CURVE448 );
421 default:
422 return( 0 );
423 }
424}
425
Gilles Peskine12313cd2018-06-20 00:20:32 +0200426static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
427{
428 switch( curve )
429 {
430 case PSA_ECC_CURVE_SECP192R1:
431 return( MBEDTLS_ECP_DP_SECP192R1 );
432 case PSA_ECC_CURVE_SECP224R1:
433 return( MBEDTLS_ECP_DP_SECP224R1 );
434 case PSA_ECC_CURVE_SECP256R1:
435 return( MBEDTLS_ECP_DP_SECP256R1 );
436 case PSA_ECC_CURVE_SECP384R1:
437 return( MBEDTLS_ECP_DP_SECP384R1 );
438 case PSA_ECC_CURVE_SECP521R1:
439 return( MBEDTLS_ECP_DP_SECP521R1 );
440 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
441 return( MBEDTLS_ECP_DP_BP256R1 );
442 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
443 return( MBEDTLS_ECP_DP_BP384R1 );
444 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
445 return( MBEDTLS_ECP_DP_BP512R1 );
446 case PSA_ECC_CURVE_CURVE25519:
447 return( MBEDTLS_ECP_DP_CURVE25519 );
448 case PSA_ECC_CURVE_SECP192K1:
449 return( MBEDTLS_ECP_DP_SECP192K1 );
450 case PSA_ECC_CURVE_SECP224K1:
451 return( MBEDTLS_ECP_DP_SECP224K1 );
452 case PSA_ECC_CURVE_SECP256K1:
453 return( MBEDTLS_ECP_DP_SECP256K1 );
454 case PSA_ECC_CURVE_CURVE448:
455 return( MBEDTLS_ECP_DP_CURVE448 );
456 default:
457 return( MBEDTLS_ECP_DP_NONE );
458 }
459}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100460#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200461
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200462static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
463 size_t bits,
464 struct raw_data *raw )
465{
466 /* Check that the bit size is acceptable for the key type */
467 switch( type )
468 {
469 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200470 if( bits == 0 )
471 {
472 raw->bytes = 0;
473 raw->data = NULL;
474 return( PSA_SUCCESS );
475 }
476 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200477#if defined(MBEDTLS_MD_C)
478 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200479#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200480 case PSA_KEY_TYPE_DERIVE:
481 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200482#if defined(MBEDTLS_AES_C)
483 case PSA_KEY_TYPE_AES:
484 if( bits != 128 && bits != 192 && bits != 256 )
485 return( PSA_ERROR_INVALID_ARGUMENT );
486 break;
487#endif
488#if defined(MBEDTLS_CAMELLIA_C)
489 case PSA_KEY_TYPE_CAMELLIA:
490 if( bits != 128 && bits != 192 && bits != 256 )
491 return( PSA_ERROR_INVALID_ARGUMENT );
492 break;
493#endif
494#if defined(MBEDTLS_DES_C)
495 case PSA_KEY_TYPE_DES:
496 if( bits != 64 && bits != 128 && bits != 192 )
497 return( PSA_ERROR_INVALID_ARGUMENT );
498 break;
499#endif
500#if defined(MBEDTLS_ARC4_C)
501 case PSA_KEY_TYPE_ARC4:
502 if( bits < 8 || bits > 2048 )
503 return( PSA_ERROR_INVALID_ARGUMENT );
504 break;
505#endif
506 default:
507 return( PSA_ERROR_NOT_SUPPORTED );
508 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200509 if( bits % 8 != 0 )
510 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200511
512 /* Allocate memory for the key */
513 raw->bytes = PSA_BITS_TO_BYTES( bits );
514 raw->data = mbedtls_calloc( 1, raw->bytes );
515 if( raw->data == NULL )
516 {
517 raw->bytes = 0;
518 return( PSA_ERROR_INSUFFICIENT_MEMORY );
519 }
520 return( PSA_SUCCESS );
521}
522
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200523#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100524/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
525 * that are not a multiple of 8) well. For example, there is only
526 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
527 * way to return the exact bit size of a key.
528 * To keep things simple, reject non-byte-aligned key sizes. */
529static psa_status_t psa_check_rsa_key_byte_aligned(
530 const mbedtls_rsa_context *rsa )
531{
532 mbedtls_mpi n;
533 psa_status_t status;
534 mbedtls_mpi_init( &n );
535 status = mbedtls_to_psa_error(
536 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
537 if( status == PSA_SUCCESS )
538 {
539 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
540 status = PSA_ERROR_NOT_SUPPORTED;
541 }
542 mbedtls_mpi_free( &n );
543 return( status );
544}
545
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200546static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
547 mbedtls_rsa_context **p_rsa )
548{
549 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
550 return( PSA_ERROR_INVALID_ARGUMENT );
551 else
552 {
553 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
Gilles Peskineaac64a22018-11-12 18:37:42 +0100554 /* The size of an RSA key doesn't have to be a multiple of 8.
555 * Mbed TLS supports non-byte-aligned key sizes, but not well.
556 * For example, mbedtls_rsa_get_len() returns the key size in
557 * bytes, not in bits. */
558 size_t bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100559 psa_status_t status;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200560 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
561 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100562 status = psa_check_rsa_key_byte_aligned( rsa );
563 if( status != PSA_SUCCESS )
564 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200565 *p_rsa = rsa;
566 return( PSA_SUCCESS );
567 }
568}
569#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
570
571#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100572/* Import an elliptic curve parsed by the mbedtls pk module. */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200573static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
574 mbedtls_pk_context *pk,
575 mbedtls_ecp_keypair **p_ecp )
576{
577 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
578 return( PSA_ERROR_INVALID_ARGUMENT );
579 else
580 {
581 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
582 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
583 if( actual_curve != expected_curve )
584 return( PSA_ERROR_INVALID_ARGUMENT );
585 *p_ecp = ecp;
586 return( PSA_SUCCESS );
587 }
588}
589#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
590
Gilles Peskinef76aa772018-10-29 19:24:33 +0100591#if defined(MBEDTLS_ECP_C)
592/* Import a private key given as a byte string which is the private value
593 * in big-endian order. */
594static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
595 const uint8_t *data,
596 size_t data_length,
597 mbedtls_ecp_keypair **p_ecp )
598{
599 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
600 mbedtls_ecp_keypair *ecp = NULL;
601 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
602
603 *p_ecp = NULL;
604 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
605 if( ecp == NULL )
606 return( PSA_ERROR_INSUFFICIENT_MEMORY );
607
608 /* Load the group. */
609 status = mbedtls_to_psa_error(
610 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
611 if( status != PSA_SUCCESS )
612 goto exit;
613 /* Load the secret value. */
614 status = mbedtls_to_psa_error(
615 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
616 if( status != PSA_SUCCESS )
617 goto exit;
618 /* Validate the private key. */
619 status = mbedtls_to_psa_error(
620 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
621 if( status != PSA_SUCCESS )
622 goto exit;
623 /* Calculate the public key from the private key. */
624 status = mbedtls_to_psa_error(
625 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
626 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
627 if( status != PSA_SUCCESS )
628 goto exit;
629
630 *p_ecp = ecp;
631 return( PSA_SUCCESS );
632
633exit:
634 if( ecp != NULL )
635 {
636 mbedtls_ecp_keypair_free( ecp );
637 mbedtls_free( ecp );
638 }
639 return( status );
640}
641#endif /* defined(MBEDTLS_ECP_C) */
642
Darryl Green940d72c2018-07-13 13:18:51 +0100643static psa_status_t psa_import_key_into_slot( key_slot_t *slot,
644 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)
725static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t key,
726 key_slot_t *p_slot )
727{
728 psa_status_t status = PSA_SUCCESS;
729 uint8_t *key_data = NULL;
730 size_t key_data_length = 0;
731
732 status = psa_load_persistent_key( key, &( p_slot )->type,
733 &( 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
Darryl Green06fd18d2018-07-16 11:21:11 +0100745/* Retrieve a key slot, occupied or not. */
746static psa_status_t psa_get_key_slot( psa_key_slot_t key,
747 key_slot_t **p_slot )
748{
749 GUARD_MODULE_INITIALIZED;
750
751 /* 0 is not a valid slot number under any circumstance. This
752 * implementation provides slots number 1 to N where N is the
753 * number of available slots. */
754 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
755 return( PSA_ERROR_INVALID_ARGUMENT );
756
757 *p_slot = &global_data.key_slots[key - 1];
Darryl Greend49a4992018-06-18 17:27:26 +0100758
759#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
760 if( ( *p_slot )->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
761 {
762 /* There are two circumstances this can occur: the key material has
763 * not yet been created, or the key exists in storage but has not yet
764 * been loaded into memory. */
765 if( ( *p_slot )->type == PSA_KEY_TYPE_NONE )
766 {
767 psa_status_t status = PSA_SUCCESS;
768 status = psa_load_persistent_key_into_slot( key, *p_slot );
769 if( status != PSA_ERROR_EMPTY_SLOT )
770 return( status );
771 }
772 }
773#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
774
Darryl Green06fd18d2018-07-16 11:21:11 +0100775 return( PSA_SUCCESS );
776}
777
778/* Retrieve an empty key slot (slot with no key data, but possibly
779 * with some metadata such as a policy). */
780static psa_status_t psa_get_empty_key_slot( psa_key_slot_t key,
781 key_slot_t **p_slot )
782{
783 psa_status_t status;
784 key_slot_t *slot = NULL;
785
786 *p_slot = NULL;
787
788 status = psa_get_key_slot( key, &slot );
789 if( status != PSA_SUCCESS )
790 return( status );
791
792 if( slot->type != PSA_KEY_TYPE_NONE )
793 return( PSA_ERROR_OCCUPIED_SLOT );
794
795 *p_slot = slot;
796 return( status );
797}
798
799/** Retrieve a slot which must contain a key. The key must have allow all the
800 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
801 * operations with this algorithm. */
802static psa_status_t psa_get_key_from_slot( psa_key_slot_t key,
803 key_slot_t **p_slot,
804 psa_key_usage_t usage,
805 psa_algorithm_t alg )
806{
807 psa_status_t status;
808 key_slot_t *slot = NULL;
809
810 *p_slot = NULL;
811
812 status = psa_get_key_slot( key, &slot );
813 if( status != PSA_SUCCESS )
814 return( status );
815 if( slot->type == PSA_KEY_TYPE_NONE )
816 return( PSA_ERROR_EMPTY_SLOT );
817
818 /* Enforce that usage policy for the key slot contains all the flags
819 * required by the usage parameter. There is one exception: public
820 * keys can always be exported, so we treat public key objects as
821 * if they had the export flag. */
822 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
823 usage &= ~PSA_KEY_USAGE_EXPORT;
824 if( ( slot->policy.usage & usage ) != usage )
825 return( PSA_ERROR_NOT_PERMITTED );
826 if( alg != 0 && ( alg != slot->policy.alg ) )
827 return( PSA_ERROR_NOT_PERMITTED );
828
829 *p_slot = slot;
830 return( PSA_SUCCESS );
831}
Darryl Green940d72c2018-07-13 13:18:51 +0100832
Darryl Green40225ba2018-11-15 14:48:15 +0000833static psa_status_t psa_remove_key_data_from_memory( key_slot_t *slot )
834{
835 if( slot->type == PSA_KEY_TYPE_NONE )
836 {
837 /* No key material to clean. */
838 }
839 else if( key_type_is_raw_bytes( slot->type ) )
840 {
841 mbedtls_free( slot->data.raw.data );
842 }
843 else
844#if defined(MBEDTLS_RSA_C)
845 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
846 {
847 mbedtls_rsa_free( slot->data.rsa );
848 mbedtls_free( slot->data.rsa );
849 }
850 else
851#endif /* defined(MBEDTLS_RSA_C) */
852#if defined(MBEDTLS_ECP_C)
853 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
854 {
855 mbedtls_ecp_keypair_free( slot->data.ecp );
856 mbedtls_free( slot->data.ecp );
857 }
858 else
859#endif /* defined(MBEDTLS_ECP_C) */
860 {
861 /* Shouldn't happen: the key type is not any type that we
862 * put in. */
863 return( PSA_ERROR_TAMPERING_DETECTED );
864 }
865
866 return( PSA_SUCCESS );
867}
868
Darryl Green940d72c2018-07-13 13:18:51 +0100869psa_status_t psa_import_key( psa_key_slot_t key,
870 psa_key_type_t type,
871 const uint8_t *data,
872 size_t data_length )
873{
874 key_slot_t *slot;
875 psa_status_t status;
876
877 status = psa_get_empty_key_slot( key, &slot );
878 if( status != PSA_SUCCESS )
879 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100880
881 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100882
883 status = psa_import_key_into_slot( slot, data, data_length );
884 if( status != PSA_SUCCESS )
885 {
886 slot->type = PSA_KEY_TYPE_NONE;
887 return( status );
888 }
889
Darryl Greend49a4992018-06-18 17:27:26 +0100890#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
891 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
892 {
893 /* Store in file location */
894 status = psa_save_persistent_key( key, slot->type, &slot->policy, data,
895 data_length );
896 if( status != PSA_SUCCESS )
897 {
898 (void) psa_remove_key_data_from_memory( slot );
899 slot->type = PSA_KEY_TYPE_NONE;
900 }
901 }
902#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
903
904 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100905}
906
Gilles Peskine2d277862018-06-18 15:41:12 +0200907psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100908{
909 key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100910 psa_status_t status = PSA_SUCCESS;
911 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100912
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200913 status = psa_get_key_slot( key, &slot );
914 if( status != PSA_SUCCESS )
915 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100916#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
917 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
918 {
919 storage_status = psa_destroy_persistent_key( key );
920 }
921#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
922 status = psa_remove_key_data_from_memory( slot );
923 /* Zeroize the slot to wipe metadata such as policies. */
924 mbedtls_zeroize( slot, sizeof( *slot ) );
925 if( status != PSA_SUCCESS )
926 return( status );
927 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100928}
929
Gilles Peskineb870b182018-07-06 16:02:09 +0200930/* Return the size of the key in the given slot, in bits. */
931static size_t psa_get_key_bits( const key_slot_t *slot )
932{
933 if( key_type_is_raw_bytes( slot->type ) )
934 return( slot->data.raw.bytes * 8 );
935#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200936 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100937 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200938#endif /* defined(MBEDTLS_RSA_C) */
939#if defined(MBEDTLS_ECP_C)
940 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
941 return( slot->data.ecp->grp.pbits );
942#endif /* defined(MBEDTLS_ECP_C) */
943 /* Shouldn't happen except on an empty slot. */
944 return( 0 );
945}
946
Gilles Peskine2d277862018-06-18 15:41:12 +0200947psa_status_t psa_get_key_information( psa_key_slot_t key,
948 psa_key_type_t *type,
949 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100950{
951 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200952 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100953
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100954 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200955 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100956 if( bits != NULL )
957 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200958 status = psa_get_key_slot( key, &slot );
959 if( status != PSA_SUCCESS )
960 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200961
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100962 if( slot->type == PSA_KEY_TYPE_NONE )
963 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200964 if( type != NULL )
965 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200966 if( bits != NULL )
967 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100968 return( PSA_SUCCESS );
969}
970
Darryl Greendd8fb772018-11-07 16:00:44 +0000971static psa_status_t psa_internal_export_key( key_slot_t *slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200972 uint8_t *data,
973 size_t data_size,
974 size_t *data_length,
975 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100976{
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100977 *data_length = 0;
978
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200979 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300980 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300981
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200982 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100983 {
984 if( slot->data.raw.bytes > data_size )
985 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +0100986 if( data_size != 0 )
987 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200988 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +0100989 memset( data + slot->data.raw.bytes, 0,
990 data_size - slot->data.raw.bytes );
991 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100992 *data_length = slot->data.raw.bytes;
993 return( PSA_SUCCESS );
994 }
Gilles Peskine188c71e2018-10-29 19:26:02 +0100995#if defined(MBEDTLS_ECP_C)
996 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
997 {
Darryl Greendd8fb772018-11-07 16:00:44 +0000998 psa_status_t status;
999
Gilles Peskine188c71e2018-10-29 19:26:02 +01001000 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1001 if( bytes > data_size )
1002 return( PSA_ERROR_BUFFER_TOO_SMALL );
1003 status = mbedtls_to_psa_error(
1004 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1005 if( status != PSA_SUCCESS )
1006 return( status );
1007 memset( data + bytes, 0, data_size - bytes );
1008 *data_length = bytes;
1009 return( PSA_SUCCESS );
1010 }
1011#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001012 else
Moran Peker17e36e12018-05-02 12:55:20 +03001013 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001014#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001015 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001016 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001017 {
Moran Pekera998bc62018-04-16 18:16:20 +03001018 mbedtls_pk_context pk;
1019 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001020 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001021 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001022#if defined(MBEDTLS_RSA_C)
1023 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001024 pk.pk_info = &mbedtls_rsa_info;
1025 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001026#else
1027 return( PSA_ERROR_NOT_SUPPORTED );
1028#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001029 }
1030 else
1031 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001032#if defined(MBEDTLS_ECP_C)
1033 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001034 pk.pk_info = &mbedtls_eckey_info;
1035 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001036#else
1037 return( PSA_ERROR_NOT_SUPPORTED );
1038#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001039 }
Moran Pekerd7326592018-05-29 16:56:39 +03001040 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001041 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +03001042 else
1043 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +03001044 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001045 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001046 /* If data_size is 0 then data may be NULL and then the
1047 * call to memset would have undefined behavior. */
1048 if( data_size != 0 )
1049 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001050 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001051 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001052 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1053 * Move the data to the beginning and erase remaining data
1054 * at the original location. */
1055 if( 2 * (size_t) ret <= data_size )
1056 {
1057 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001058 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001059 }
1060 else if( (size_t) ret < data_size )
1061 {
1062 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001063 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001064 }
Moran Pekera998bc62018-04-16 18:16:20 +03001065 *data_length = ret;
1066 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001067 }
1068 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001069#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001070 {
1071 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001072 it is valid for a special-purpose implementation to omit
1073 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001074 return( PSA_ERROR_NOT_SUPPORTED );
1075 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001076 }
1077}
1078
Gilles Peskine2d277862018-06-18 15:41:12 +02001079psa_status_t psa_export_key( psa_key_slot_t key,
1080 uint8_t *data,
1081 size_t data_size,
1082 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001083{
Darryl Greendd8fb772018-11-07 16:00:44 +00001084 key_slot_t *slot;
1085 psa_status_t status;
1086
1087 /* Set the key to empty now, so that even when there are errors, we always
1088 * set data_length to a value between 0 and data_size. On error, setting
1089 * the key to empty is a good choice because an empty key representation is
1090 * unlikely to be accepted anywhere. */
1091 *data_length = 0;
1092
1093 /* Export requires the EXPORT flag. There is an exception for public keys,
1094 * which don't require any flag, but psa_get_key_from_slot takes
1095 * care of this. */
1096 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_EXPORT, 0 );
1097 if( status != PSA_SUCCESS )
1098 return( status );
1099 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001100 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001101}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001102
Gilles Peskine2d277862018-06-18 15:41:12 +02001103psa_status_t psa_export_public_key( psa_key_slot_t key,
1104 uint8_t *data,
1105 size_t data_size,
1106 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001107{
Darryl Greendd8fb772018-11-07 16:00:44 +00001108 key_slot_t *slot;
1109 psa_status_t status;
1110
1111 /* Set the key to empty now, so that even when there are errors, we always
1112 * set data_length to a value between 0 and data_size. On error, setting
1113 * the key to empty is a good choice because an empty key representation is
1114 * unlikely to be accepted anywhere. */
1115 *data_length = 0;
1116
1117 /* Exporting a public key doesn't require a usage flag. */
1118 status = psa_get_key_from_slot( key, &slot, 0, 0 );
1119 if( status != PSA_SUCCESS )
1120 return( status );
1121 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001122 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001123}
1124
Darryl Green0c6575a2018-11-07 16:05:30 +00001125#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1126static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t key,
1127 key_slot_t *slot,
1128 size_t bits )
1129{
1130 psa_status_t status;
1131 uint8_t *data;
1132 size_t key_length;
1133 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1134 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001135 if( data == NULL )
1136 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001137 /* Get key data in export format */
1138 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1139 if( status != PSA_SUCCESS )
1140 {
1141 slot->type = PSA_KEY_TYPE_NONE;
1142 goto exit;
1143 }
1144 /* Store in file location */
1145 status = psa_save_persistent_key( key, slot->type, &slot->policy,
1146 data, key_length );
1147 if( status != PSA_SUCCESS )
1148 {
1149 slot->type = PSA_KEY_TYPE_NONE;
1150 }
1151exit:
1152 mbedtls_zeroize( data, key_length );
1153 mbedtls_free( data );
1154 return( status );
1155}
1156#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1157
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001158
1159
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001160/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001161/* Message digests */
1162/****************************************************************/
1163
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001164static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001165{
1166 switch( alg )
1167 {
1168#if defined(MBEDTLS_MD2_C)
1169 case PSA_ALG_MD2:
1170 return( &mbedtls_md2_info );
1171#endif
1172#if defined(MBEDTLS_MD4_C)
1173 case PSA_ALG_MD4:
1174 return( &mbedtls_md4_info );
1175#endif
1176#if defined(MBEDTLS_MD5_C)
1177 case PSA_ALG_MD5:
1178 return( &mbedtls_md5_info );
1179#endif
1180#if defined(MBEDTLS_RIPEMD160_C)
1181 case PSA_ALG_RIPEMD160:
1182 return( &mbedtls_ripemd160_info );
1183#endif
1184#if defined(MBEDTLS_SHA1_C)
1185 case PSA_ALG_SHA_1:
1186 return( &mbedtls_sha1_info );
1187#endif
1188#if defined(MBEDTLS_SHA256_C)
1189 case PSA_ALG_SHA_224:
1190 return( &mbedtls_sha224_info );
1191 case PSA_ALG_SHA_256:
1192 return( &mbedtls_sha256_info );
1193#endif
1194#if defined(MBEDTLS_SHA512_C)
1195 case PSA_ALG_SHA_384:
1196 return( &mbedtls_sha384_info );
1197 case PSA_ALG_SHA_512:
1198 return( &mbedtls_sha512_info );
1199#endif
1200 default:
1201 return( NULL );
1202 }
1203}
1204
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001205psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1206{
1207 switch( operation->alg )
1208 {
Gilles Peskine81736312018-06-26 15:04:31 +02001209 case 0:
1210 /* The object has (apparently) been initialized but it is not
1211 * in use. It's ok to call abort on such an object, and there's
1212 * nothing to do. */
1213 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001214#if defined(MBEDTLS_MD2_C)
1215 case PSA_ALG_MD2:
1216 mbedtls_md2_free( &operation->ctx.md2 );
1217 break;
1218#endif
1219#if defined(MBEDTLS_MD4_C)
1220 case PSA_ALG_MD4:
1221 mbedtls_md4_free( &operation->ctx.md4 );
1222 break;
1223#endif
1224#if defined(MBEDTLS_MD5_C)
1225 case PSA_ALG_MD5:
1226 mbedtls_md5_free( &operation->ctx.md5 );
1227 break;
1228#endif
1229#if defined(MBEDTLS_RIPEMD160_C)
1230 case PSA_ALG_RIPEMD160:
1231 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1232 break;
1233#endif
1234#if defined(MBEDTLS_SHA1_C)
1235 case PSA_ALG_SHA_1:
1236 mbedtls_sha1_free( &operation->ctx.sha1 );
1237 break;
1238#endif
1239#if defined(MBEDTLS_SHA256_C)
1240 case PSA_ALG_SHA_224:
1241 case PSA_ALG_SHA_256:
1242 mbedtls_sha256_free( &operation->ctx.sha256 );
1243 break;
1244#endif
1245#if defined(MBEDTLS_SHA512_C)
1246 case PSA_ALG_SHA_384:
1247 case PSA_ALG_SHA_512:
1248 mbedtls_sha512_free( &operation->ctx.sha512 );
1249 break;
1250#endif
1251 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001252 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001253 }
1254 operation->alg = 0;
1255 return( PSA_SUCCESS );
1256}
1257
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001258psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001259 psa_algorithm_t alg )
1260{
1261 int ret;
1262 operation->alg = 0;
1263 switch( alg )
1264 {
1265#if defined(MBEDTLS_MD2_C)
1266 case PSA_ALG_MD2:
1267 mbedtls_md2_init( &operation->ctx.md2 );
1268 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1269 break;
1270#endif
1271#if defined(MBEDTLS_MD4_C)
1272 case PSA_ALG_MD4:
1273 mbedtls_md4_init( &operation->ctx.md4 );
1274 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1275 break;
1276#endif
1277#if defined(MBEDTLS_MD5_C)
1278 case PSA_ALG_MD5:
1279 mbedtls_md5_init( &operation->ctx.md5 );
1280 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1281 break;
1282#endif
1283#if defined(MBEDTLS_RIPEMD160_C)
1284 case PSA_ALG_RIPEMD160:
1285 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1286 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1287 break;
1288#endif
1289#if defined(MBEDTLS_SHA1_C)
1290 case PSA_ALG_SHA_1:
1291 mbedtls_sha1_init( &operation->ctx.sha1 );
1292 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1293 break;
1294#endif
1295#if defined(MBEDTLS_SHA256_C)
1296 case PSA_ALG_SHA_224:
1297 mbedtls_sha256_init( &operation->ctx.sha256 );
1298 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1299 break;
1300 case PSA_ALG_SHA_256:
1301 mbedtls_sha256_init( &operation->ctx.sha256 );
1302 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1303 break;
1304#endif
1305#if defined(MBEDTLS_SHA512_C)
1306 case PSA_ALG_SHA_384:
1307 mbedtls_sha512_init( &operation->ctx.sha512 );
1308 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1309 break;
1310 case PSA_ALG_SHA_512:
1311 mbedtls_sha512_init( &operation->ctx.sha512 );
1312 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1313 break;
1314#endif
1315 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001316 return( PSA_ALG_IS_HASH( alg ) ?
1317 PSA_ERROR_NOT_SUPPORTED :
1318 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001319 }
1320 if( ret == 0 )
1321 operation->alg = alg;
1322 else
1323 psa_hash_abort( operation );
1324 return( mbedtls_to_psa_error( ret ) );
1325}
1326
1327psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1328 const uint8_t *input,
1329 size_t input_length )
1330{
1331 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001332
1333 /* Don't require hash implementations to behave correctly on a
1334 * zero-length input, which may have an invalid pointer. */
1335 if( input_length == 0 )
1336 return( PSA_SUCCESS );
1337
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001338 switch( operation->alg )
1339 {
1340#if defined(MBEDTLS_MD2_C)
1341 case PSA_ALG_MD2:
1342 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1343 input, input_length );
1344 break;
1345#endif
1346#if defined(MBEDTLS_MD4_C)
1347 case PSA_ALG_MD4:
1348 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1349 input, input_length );
1350 break;
1351#endif
1352#if defined(MBEDTLS_MD5_C)
1353 case PSA_ALG_MD5:
1354 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1355 input, input_length );
1356 break;
1357#endif
1358#if defined(MBEDTLS_RIPEMD160_C)
1359 case PSA_ALG_RIPEMD160:
1360 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1361 input, input_length );
1362 break;
1363#endif
1364#if defined(MBEDTLS_SHA1_C)
1365 case PSA_ALG_SHA_1:
1366 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1367 input, input_length );
1368 break;
1369#endif
1370#if defined(MBEDTLS_SHA256_C)
1371 case PSA_ALG_SHA_224:
1372 case PSA_ALG_SHA_256:
1373 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1374 input, input_length );
1375 break;
1376#endif
1377#if defined(MBEDTLS_SHA512_C)
1378 case PSA_ALG_SHA_384:
1379 case PSA_ALG_SHA_512:
1380 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1381 input, input_length );
1382 break;
1383#endif
1384 default:
1385 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1386 break;
1387 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001388
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001389 if( ret != 0 )
1390 psa_hash_abort( operation );
1391 return( mbedtls_to_psa_error( ret ) );
1392}
1393
1394psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1395 uint8_t *hash,
1396 size_t hash_size,
1397 size_t *hash_length )
1398{
itayzafrir40835d42018-08-02 13:14:17 +03001399 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001400 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001401 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001402
1403 /* Fill the output buffer with something that isn't a valid hash
1404 * (barring an attack on the hash and deliberately-crafted input),
1405 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001406 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001407 /* If hash_size is 0 then hash may be NULL and then the
1408 * call to memset would have undefined behavior. */
1409 if( hash_size != 0 )
1410 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001411
1412 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001413 {
1414 status = PSA_ERROR_BUFFER_TOO_SMALL;
1415 goto exit;
1416 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001417
1418 switch( operation->alg )
1419 {
1420#if defined(MBEDTLS_MD2_C)
1421 case PSA_ALG_MD2:
1422 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1423 break;
1424#endif
1425#if defined(MBEDTLS_MD4_C)
1426 case PSA_ALG_MD4:
1427 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1428 break;
1429#endif
1430#if defined(MBEDTLS_MD5_C)
1431 case PSA_ALG_MD5:
1432 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1433 break;
1434#endif
1435#if defined(MBEDTLS_RIPEMD160_C)
1436 case PSA_ALG_RIPEMD160:
1437 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1438 break;
1439#endif
1440#if defined(MBEDTLS_SHA1_C)
1441 case PSA_ALG_SHA_1:
1442 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1443 break;
1444#endif
1445#if defined(MBEDTLS_SHA256_C)
1446 case PSA_ALG_SHA_224:
1447 case PSA_ALG_SHA_256:
1448 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1449 break;
1450#endif
1451#if defined(MBEDTLS_SHA512_C)
1452 case PSA_ALG_SHA_384:
1453 case PSA_ALG_SHA_512:
1454 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1455 break;
1456#endif
1457 default:
1458 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1459 break;
1460 }
itayzafrir40835d42018-08-02 13:14:17 +03001461 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001462
itayzafrir40835d42018-08-02 13:14:17 +03001463exit:
1464 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001465 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001466 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001467 return( psa_hash_abort( operation ) );
1468 }
1469 else
1470 {
1471 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001472 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001473 }
1474}
1475
Gilles Peskine2d277862018-06-18 15:41:12 +02001476psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1477 const uint8_t *hash,
1478 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001479{
1480 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1481 size_t actual_hash_length;
1482 psa_status_t status = psa_hash_finish( operation,
1483 actual_hash, sizeof( actual_hash ),
1484 &actual_hash_length );
1485 if( status != PSA_SUCCESS )
1486 return( status );
1487 if( actual_hash_length != hash_length )
1488 return( PSA_ERROR_INVALID_SIGNATURE );
1489 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1490 return( PSA_ERROR_INVALID_SIGNATURE );
1491 return( PSA_SUCCESS );
1492}
1493
1494
1495
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001496/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001497/* MAC */
1498/****************************************************************/
1499
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001500static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001501 psa_algorithm_t alg,
1502 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001503 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001504 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001505{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001506 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001507 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001508
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001509 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001510 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001511
Gilles Peskine8c9def32018-02-08 10:02:12 +01001512 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1513 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001514 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001515 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001516 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001517 mode = MBEDTLS_MODE_STREAM;
1518 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001519 case PSA_ALG_CTR:
1520 mode = MBEDTLS_MODE_CTR;
1521 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001522 case PSA_ALG_CFB:
1523 mode = MBEDTLS_MODE_CFB;
1524 break;
1525 case PSA_ALG_OFB:
1526 mode = MBEDTLS_MODE_OFB;
1527 break;
1528 case PSA_ALG_CBC_NO_PADDING:
1529 mode = MBEDTLS_MODE_CBC;
1530 break;
1531 case PSA_ALG_CBC_PKCS7:
1532 mode = MBEDTLS_MODE_CBC;
1533 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001534 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001535 mode = MBEDTLS_MODE_CCM;
1536 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001537 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001538 mode = MBEDTLS_MODE_GCM;
1539 break;
1540 default:
1541 return( NULL );
1542 }
1543 }
1544 else if( alg == PSA_ALG_CMAC )
1545 mode = MBEDTLS_MODE_ECB;
1546 else if( alg == PSA_ALG_GMAC )
1547 mode = MBEDTLS_MODE_GCM;
1548 else
1549 return( NULL );
1550
1551 switch( key_type )
1552 {
1553 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001554 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001555 break;
1556 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001557 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1558 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001559 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001560 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001561 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001562 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001563 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1564 * but two-key Triple-DES is functionally three-key Triple-DES
1565 * with K1=K3, so that's how we present it to mbedtls. */
1566 if( key_bits == 128 )
1567 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001568 break;
1569 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001570 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001571 break;
1572 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001573 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001574 break;
1575 default:
1576 return( NULL );
1577 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001578 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001579 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001580
Jaeden Amero23bbb752018-06-26 14:16:54 +01001581 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1582 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001583}
1584
Gilles Peskinea05219c2018-11-16 16:02:56 +01001585#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001586static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001587{
Gilles Peskine2d277862018-06-18 15:41:12 +02001588 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001589 {
1590 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001591 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001592 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001593 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001594 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001595 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001596 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001597 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001598 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001599 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001600 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001601 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001602 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001603 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001604 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001605 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001606 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001607 return( 128 );
1608 default:
1609 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001610 }
1611}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001612#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001613
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001614/* Initialize the MAC operation structure. Once this function has been
1615 * called, psa_mac_abort can run and will do the right thing. */
1616static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1617 psa_algorithm_t alg )
1618{
1619 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1620
1621 operation->alg = alg;
1622 operation->key_set = 0;
1623 operation->iv_set = 0;
1624 operation->iv_required = 0;
1625 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001626 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001627
1628#if defined(MBEDTLS_CMAC_C)
1629 if( alg == PSA_ALG_CMAC )
1630 {
1631 operation->iv_required = 0;
1632 mbedtls_cipher_init( &operation->ctx.cmac );
1633 status = PSA_SUCCESS;
1634 }
1635 else
1636#endif /* MBEDTLS_CMAC_C */
1637#if defined(MBEDTLS_MD_C)
1638 if( PSA_ALG_IS_HMAC( operation->alg ) )
1639 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001640 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1641 operation->ctx.hmac.hash_ctx.alg = 0;
1642 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001643 }
1644 else
1645#endif /* MBEDTLS_MD_C */
1646 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001647 if( ! PSA_ALG_IS_MAC( alg ) )
1648 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001649 }
1650
1651 if( status != PSA_SUCCESS )
1652 memset( operation, 0, sizeof( *operation ) );
1653 return( status );
1654}
1655
Gilles Peskine01126fa2018-07-12 17:04:55 +02001656#if defined(MBEDTLS_MD_C)
1657static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1658{
1659 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1660 return( psa_hash_abort( &hmac->hash_ctx ) );
1661}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001662
1663static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1664{
1665 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1666 memset( hmac, 0, sizeof( *hmac ) );
1667}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001668#endif /* MBEDTLS_MD_C */
1669
Gilles Peskine8c9def32018-02-08 10:02:12 +01001670psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1671{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001672 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001673 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001674 /* The object has (apparently) been initialized but it is not
1675 * in use. It's ok to call abort on such an object, and there's
1676 * nothing to do. */
1677 return( PSA_SUCCESS );
1678 }
1679 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001680#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001681 if( operation->alg == PSA_ALG_CMAC )
1682 {
1683 mbedtls_cipher_free( &operation->ctx.cmac );
1684 }
1685 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001686#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001687#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001688 if( PSA_ALG_IS_HMAC( operation->alg ) )
1689 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001690 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001691 }
1692 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001693#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001694 {
1695 /* Sanity check (shouldn't happen: operation->alg should
1696 * always have been initialized to a valid value). */
1697 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001698 }
Moran Peker41deec42018-04-04 15:43:05 +03001699
Gilles Peskine8c9def32018-02-08 10:02:12 +01001700 operation->alg = 0;
1701 operation->key_set = 0;
1702 operation->iv_set = 0;
1703 operation->iv_required = 0;
1704 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001705 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001706
Gilles Peskine8c9def32018-02-08 10:02:12 +01001707 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001708
1709bad_state:
1710 /* If abort is called on an uninitialized object, we can't trust
1711 * anything. Wipe the object in case it contains confidential data.
1712 * This may result in a memory leak if a pointer gets overwritten,
1713 * but it's too late to do anything about this. */
1714 memset( operation, 0, sizeof( *operation ) );
1715 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001716}
1717
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001718#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001719static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001720 size_t key_bits,
1721 key_slot_t *slot,
1722 const mbedtls_cipher_info_t *cipher_info )
1723{
1724 int ret;
1725
1726 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001727
1728 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1729 if( ret != 0 )
1730 return( ret );
1731
1732 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1733 slot->data.raw.data,
1734 key_bits );
1735 return( ret );
1736}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001737#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001738
Gilles Peskine248051a2018-06-20 16:09:38 +02001739#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001740static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1741 const uint8_t *key,
1742 size_t key_length,
1743 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001744{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001745 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001746 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001747 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001748 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001749 psa_status_t status;
1750
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001751 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1752 * overflow below. This should never trigger if the hash algorithm
1753 * is implemented correctly. */
1754 /* The size checks against the ipad and opad buffers cannot be written
1755 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1756 * because that triggers -Wlogical-op on GCC 7.3. */
1757 if( block_size > sizeof( ipad ) )
1758 return( PSA_ERROR_NOT_SUPPORTED );
1759 if( block_size > sizeof( hmac->opad ) )
1760 return( PSA_ERROR_NOT_SUPPORTED );
1761 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001762 return( PSA_ERROR_NOT_SUPPORTED );
1763
Gilles Peskined223b522018-06-11 18:12:58 +02001764 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001765 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001766 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001767 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001768 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001769 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001770 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001771 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001772 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001773 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001774 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001775 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001776 }
Gilles Peskine96889972018-07-12 17:07:03 +02001777 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1778 * but it is permitted. It is common when HMAC is used in HKDF, for
1779 * example. Don't call `memcpy` in the 0-length because `key` could be
1780 * an invalid pointer which would make the behavior undefined. */
1781 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001782 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001783
Gilles Peskined223b522018-06-11 18:12:58 +02001784 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1785 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001786 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001787 ipad[i] ^= 0x36;
1788 memset( ipad + key_length, 0x36, block_size - key_length );
1789
1790 /* Copy the key material from ipad to opad, flipping the requisite bits,
1791 * and filling the rest of opad with the requisite constant. */
1792 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001793 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1794 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001795
Gilles Peskine01126fa2018-07-12 17:04:55 +02001796 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001797 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001798 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001799
Gilles Peskine01126fa2018-07-12 17:04:55 +02001800 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001801
1802cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001803 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001804
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001805 return( status );
1806}
Gilles Peskine248051a2018-06-20 16:09:38 +02001807#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001808
Gilles Peskine89167cb2018-07-08 20:12:23 +02001809static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1810 psa_key_slot_t key,
1811 psa_algorithm_t alg,
1812 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001813{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001814 psa_status_t status;
1815 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001816 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001817 psa_key_usage_t usage =
1818 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001819 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001820 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001821
Gilles Peskined911eb72018-08-14 15:18:45 +02001822 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001823 if( status != PSA_SUCCESS )
1824 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001825 if( is_sign )
1826 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001827
Gilles Peskine89167cb2018-07-08 20:12:23 +02001828 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001829 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001830 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001831 key_bits = psa_get_key_bits( slot );
1832
Gilles Peskine8c9def32018-02-08 10:02:12 +01001833#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001834 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001835 {
1836 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001837 mbedtls_cipher_info_from_psa( full_length_alg,
1838 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001839 int ret;
1840 if( cipher_info == NULL )
1841 {
1842 status = PSA_ERROR_NOT_SUPPORTED;
1843 goto exit;
1844 }
1845 operation->mac_size = cipher_info->block_size;
1846 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1847 status = mbedtls_to_psa_error( ret );
1848 }
1849 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001850#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001851#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001852 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001853 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001854 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001855 if( hash_alg == 0 )
1856 {
1857 status = PSA_ERROR_NOT_SUPPORTED;
1858 goto exit;
1859 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001860
1861 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1862 /* Sanity check. This shouldn't fail on a valid configuration. */
1863 if( operation->mac_size == 0 ||
1864 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1865 {
1866 status = PSA_ERROR_NOT_SUPPORTED;
1867 goto exit;
1868 }
1869
Gilles Peskine01126fa2018-07-12 17:04:55 +02001870 if( slot->type != PSA_KEY_TYPE_HMAC )
1871 {
1872 status = PSA_ERROR_INVALID_ARGUMENT;
1873 goto exit;
1874 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001875
Gilles Peskine01126fa2018-07-12 17:04:55 +02001876 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1877 slot->data.raw.data,
1878 slot->data.raw.bytes,
1879 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001880 }
1881 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001882#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001883 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00001884 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02001885 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001886 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001887
Gilles Peskined911eb72018-08-14 15:18:45 +02001888 if( truncated == 0 )
1889 {
1890 /* The "normal" case: untruncated algorithm. Nothing to do. */
1891 }
1892 else if( truncated < 4 )
1893 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001894 /* A very short MAC is too short for security since it can be
1895 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1896 * so we make this our minimum, even though 32 bits is still
1897 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001898 status = PSA_ERROR_NOT_SUPPORTED;
1899 }
1900 else if( truncated > operation->mac_size )
1901 {
1902 /* It's impossible to "truncate" to a larger length. */
1903 status = PSA_ERROR_INVALID_ARGUMENT;
1904 }
1905 else
1906 operation->mac_size = truncated;
1907
Gilles Peskinefbfac682018-07-08 20:51:54 +02001908exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001909 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001910 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001911 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001912 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001913 else
1914 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001915 operation->key_set = 1;
1916 }
1917 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001918}
1919
Gilles Peskine89167cb2018-07-08 20:12:23 +02001920psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1921 psa_key_slot_t key,
1922 psa_algorithm_t alg )
1923{
1924 return( psa_mac_setup( operation, key, alg, 1 ) );
1925}
1926
1927psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1928 psa_key_slot_t key,
1929 psa_algorithm_t alg )
1930{
1931 return( psa_mac_setup( operation, key, alg, 0 ) );
1932}
1933
Gilles Peskine8c9def32018-02-08 10:02:12 +01001934psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1935 const uint8_t *input,
1936 size_t input_length )
1937{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001938 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001939 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001940 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001941 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001942 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001943 operation->has_input = 1;
1944
Gilles Peskine8c9def32018-02-08 10:02:12 +01001945#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001946 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001947 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001948 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1949 input, input_length );
1950 status = mbedtls_to_psa_error( ret );
1951 }
1952 else
1953#endif /* MBEDTLS_CMAC_C */
1954#if defined(MBEDTLS_MD_C)
1955 if( PSA_ALG_IS_HMAC( operation->alg ) )
1956 {
1957 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1958 input_length );
1959 }
1960 else
1961#endif /* MBEDTLS_MD_C */
1962 {
1963 /* This shouldn't happen if `operation` was initialized by
1964 * a setup function. */
1965 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001966 }
1967
Gilles Peskinefbfac682018-07-08 20:51:54 +02001968cleanup:
1969 if( status != PSA_SUCCESS )
1970 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001971 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001972}
1973
Gilles Peskine01126fa2018-07-12 17:04:55 +02001974#if defined(MBEDTLS_MD_C)
1975static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1976 uint8_t *mac,
1977 size_t mac_size )
1978{
1979 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1980 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1981 size_t hash_size = 0;
1982 size_t block_size = psa_get_hash_block_size( hash_alg );
1983 psa_status_t status;
1984
Gilles Peskine01126fa2018-07-12 17:04:55 +02001985 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1986 if( status != PSA_SUCCESS )
1987 return( status );
1988 /* From here on, tmp needs to be wiped. */
1989
1990 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1991 if( status != PSA_SUCCESS )
1992 goto exit;
1993
1994 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1995 if( status != PSA_SUCCESS )
1996 goto exit;
1997
1998 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1999 if( status != PSA_SUCCESS )
2000 goto exit;
2001
Gilles Peskined911eb72018-08-14 15:18:45 +02002002 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2003 if( status != PSA_SUCCESS )
2004 goto exit;
2005
2006 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002007
2008exit:
2009 mbedtls_zeroize( tmp, hash_size );
2010 return( status );
2011}
2012#endif /* MBEDTLS_MD_C */
2013
mohammad16036df908f2018-04-02 08:34:15 -07002014static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002015 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002016 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002017{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002018 if( ! operation->key_set )
2019 return( PSA_ERROR_BAD_STATE );
2020 if( operation->iv_required && ! operation->iv_set )
2021 return( PSA_ERROR_BAD_STATE );
2022
Gilles Peskine8c9def32018-02-08 10:02:12 +01002023 if( mac_size < operation->mac_size )
2024 return( PSA_ERROR_BUFFER_TOO_SMALL );
2025
Gilles Peskine8c9def32018-02-08 10:02:12 +01002026#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002027 if( operation->alg == PSA_ALG_CMAC )
2028 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002029 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2030 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2031 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002032 memcpy( mac, tmp, operation->mac_size );
Gilles Peskined911eb72018-08-14 15:18:45 +02002033 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002034 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002035 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002036 else
2037#endif /* MBEDTLS_CMAC_C */
2038#if defined(MBEDTLS_MD_C)
2039 if( PSA_ALG_IS_HMAC( operation->alg ) )
2040 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002041 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002042 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002043 }
2044 else
2045#endif /* MBEDTLS_MD_C */
2046 {
2047 /* This shouldn't happen if `operation` was initialized by
2048 * a setup function. */
2049 return( PSA_ERROR_BAD_STATE );
2050 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002051}
2052
Gilles Peskineacd4be32018-07-08 19:56:25 +02002053psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2054 uint8_t *mac,
2055 size_t mac_size,
2056 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002057{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002058 psa_status_t status;
2059
2060 /* Fill the output buffer with something that isn't a valid mac
2061 * (barring an attack on the mac and deliberately-crafted input),
2062 * in case the caller doesn't check the return status properly. */
2063 *mac_length = mac_size;
2064 /* If mac_size is 0 then mac may be NULL and then the
2065 * call to memset would have undefined behavior. */
2066 if( mac_size != 0 )
2067 memset( mac, '!', mac_size );
2068
Gilles Peskine89167cb2018-07-08 20:12:23 +02002069 if( ! operation->is_sign )
2070 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002071 status = PSA_ERROR_BAD_STATE;
2072 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002073 }
mohammad16036df908f2018-04-02 08:34:15 -07002074
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002075 status = psa_mac_finish_internal( operation, mac, mac_size );
2076
2077cleanup:
2078 if( status == PSA_SUCCESS )
2079 {
2080 status = psa_mac_abort( operation );
2081 if( status == PSA_SUCCESS )
2082 *mac_length = operation->mac_size;
2083 else
2084 memset( mac, '!', mac_size );
2085 }
2086 else
2087 psa_mac_abort( operation );
2088 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002089}
2090
Gilles Peskineacd4be32018-07-08 19:56:25 +02002091psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2092 const uint8_t *mac,
2093 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002094{
Gilles Peskine828ed142018-06-18 23:25:51 +02002095 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002096 psa_status_t status;
2097
Gilles Peskine89167cb2018-07-08 20:12:23 +02002098 if( operation->is_sign )
2099 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002100 status = PSA_ERROR_BAD_STATE;
2101 goto cleanup;
2102 }
2103 if( operation->mac_size != mac_length )
2104 {
2105 status = PSA_ERROR_INVALID_SIGNATURE;
2106 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002107 }
mohammad16036df908f2018-04-02 08:34:15 -07002108
2109 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002110 actual_mac, sizeof( actual_mac ) );
2111
2112 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2113 status = PSA_ERROR_INVALID_SIGNATURE;
2114
2115cleanup:
2116 if( status == PSA_SUCCESS )
2117 status = psa_mac_abort( operation );
2118 else
2119 psa_mac_abort( operation );
2120
Gilles Peskine99b7d6b2018-08-21 14:56:19 +02002121 mbedtls_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002122
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002123 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002124}
2125
2126
Gilles Peskine20035e32018-02-03 22:44:14 +01002127
Gilles Peskine20035e32018-02-03 22:44:14 +01002128/****************************************************************/
2129/* Asymmetric cryptography */
2130/****************************************************************/
2131
Gilles Peskine2b450e32018-06-27 15:42:46 +02002132#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002133/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002134 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002135static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2136 size_t hash_length,
2137 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002138{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002139 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002140 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002141 *md_alg = mbedtls_md_get_type( md_info );
2142
2143 /* The Mbed TLS RSA module uses an unsigned int for hash length
2144 * parameters. Validate that it fits so that we don't risk an
2145 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002146#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002147 if( hash_length > UINT_MAX )
2148 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002149#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002150
2151#if defined(MBEDTLS_PKCS1_V15)
2152 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2153 * must be correct. */
2154 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2155 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002156 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002157 if( md_info == NULL )
2158 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002159 if( mbedtls_md_get_size( md_info ) != hash_length )
2160 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002161 }
2162#endif /* MBEDTLS_PKCS1_V15 */
2163
2164#if defined(MBEDTLS_PKCS1_V21)
2165 /* PSS requires a hash internally. */
2166 if( PSA_ALG_IS_RSA_PSS( alg ) )
2167 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002168 if( md_info == NULL )
2169 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002170 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002171#endif /* MBEDTLS_PKCS1_V21 */
2172
Gilles Peskine61b91d42018-06-08 16:09:36 +02002173 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002174}
2175
Gilles Peskine2b450e32018-06-27 15:42:46 +02002176static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2177 psa_algorithm_t alg,
2178 const uint8_t *hash,
2179 size_t hash_length,
2180 uint8_t *signature,
2181 size_t signature_size,
2182 size_t *signature_length )
2183{
2184 psa_status_t status;
2185 int ret;
2186 mbedtls_md_type_t md_alg;
2187
2188 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2189 if( status != PSA_SUCCESS )
2190 return( status );
2191
Gilles Peskine630a18a2018-06-29 17:49:35 +02002192 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002193 return( PSA_ERROR_BUFFER_TOO_SMALL );
2194
2195#if defined(MBEDTLS_PKCS1_V15)
2196 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2197 {
2198 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2199 MBEDTLS_MD_NONE );
2200 ret = mbedtls_rsa_pkcs1_sign( rsa,
2201 mbedtls_ctr_drbg_random,
2202 &global_data.ctr_drbg,
2203 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002204 md_alg,
2205 (unsigned int) hash_length,
2206 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002207 signature );
2208 }
2209 else
2210#endif /* MBEDTLS_PKCS1_V15 */
2211#if defined(MBEDTLS_PKCS1_V21)
2212 if( PSA_ALG_IS_RSA_PSS( alg ) )
2213 {
2214 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2215 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2216 mbedtls_ctr_drbg_random,
2217 &global_data.ctr_drbg,
2218 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002219 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002220 (unsigned int) hash_length,
2221 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002222 signature );
2223 }
2224 else
2225#endif /* MBEDTLS_PKCS1_V21 */
2226 {
2227 return( PSA_ERROR_INVALID_ARGUMENT );
2228 }
2229
2230 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002231 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002232 return( mbedtls_to_psa_error( ret ) );
2233}
2234
2235static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2236 psa_algorithm_t alg,
2237 const uint8_t *hash,
2238 size_t hash_length,
2239 const uint8_t *signature,
2240 size_t signature_length )
2241{
2242 psa_status_t status;
2243 int ret;
2244 mbedtls_md_type_t md_alg;
2245
2246 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2247 if( status != PSA_SUCCESS )
2248 return( status );
2249
Gilles Peskine630a18a2018-06-29 17:49:35 +02002250 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002251 return( PSA_ERROR_BUFFER_TOO_SMALL );
2252
2253#if defined(MBEDTLS_PKCS1_V15)
2254 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2255 {
2256 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2257 MBEDTLS_MD_NONE );
2258 ret = mbedtls_rsa_pkcs1_verify( rsa,
2259 mbedtls_ctr_drbg_random,
2260 &global_data.ctr_drbg,
2261 MBEDTLS_RSA_PUBLIC,
2262 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002263 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002264 hash,
2265 signature );
2266 }
2267 else
2268#endif /* MBEDTLS_PKCS1_V15 */
2269#if defined(MBEDTLS_PKCS1_V21)
2270 if( PSA_ALG_IS_RSA_PSS( alg ) )
2271 {
2272 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2273 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2274 mbedtls_ctr_drbg_random,
2275 &global_data.ctr_drbg,
2276 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002277 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002278 (unsigned int) hash_length,
2279 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002280 signature );
2281 }
2282 else
2283#endif /* MBEDTLS_PKCS1_V21 */
2284 {
2285 return( PSA_ERROR_INVALID_ARGUMENT );
2286 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002287
2288 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2289 * the rest of the signature is invalid". This has little use in
2290 * practice and PSA doesn't report this distinction. */
2291 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2292 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002293 return( mbedtls_to_psa_error( ret ) );
2294}
2295#endif /* MBEDTLS_RSA_C */
2296
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002297#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002298/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2299 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2300 * (even though these functions don't modify it). */
2301static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2302 psa_algorithm_t alg,
2303 const uint8_t *hash,
2304 size_t hash_length,
2305 uint8_t *signature,
2306 size_t signature_size,
2307 size_t *signature_length )
2308{
2309 int ret;
2310 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002311 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002312 mbedtls_mpi_init( &r );
2313 mbedtls_mpi_init( &s );
2314
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002315 if( signature_size < 2 * curve_bytes )
2316 {
2317 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2318 goto cleanup;
2319 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002320
Gilles Peskinea05219c2018-11-16 16:02:56 +01002321#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002322 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2323 {
2324 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2325 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2326 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2327 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2328 hash, hash_length,
2329 md_alg ) );
2330 }
2331 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002332#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002333 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002334 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002335 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2336 hash, hash_length,
2337 mbedtls_ctr_drbg_random,
2338 &global_data.ctr_drbg ) );
2339 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002340
2341 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2342 signature,
2343 curve_bytes ) );
2344 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2345 signature + curve_bytes,
2346 curve_bytes ) );
2347
2348cleanup:
2349 mbedtls_mpi_free( &r );
2350 mbedtls_mpi_free( &s );
2351 if( ret == 0 )
2352 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002353 return( mbedtls_to_psa_error( ret ) );
2354}
2355
2356static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2357 const uint8_t *hash,
2358 size_t hash_length,
2359 const uint8_t *signature,
2360 size_t signature_length )
2361{
2362 int ret;
2363 mbedtls_mpi r, s;
2364 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2365 mbedtls_mpi_init( &r );
2366 mbedtls_mpi_init( &s );
2367
2368 if( signature_length != 2 * curve_bytes )
2369 return( PSA_ERROR_INVALID_SIGNATURE );
2370
2371 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2372 signature,
2373 curve_bytes ) );
2374 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2375 signature + curve_bytes,
2376 curve_bytes ) );
2377
2378 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2379 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002380
2381cleanup:
2382 mbedtls_mpi_free( &r );
2383 mbedtls_mpi_free( &s );
2384 return( mbedtls_to_psa_error( ret ) );
2385}
2386#endif /* MBEDTLS_ECDSA_C */
2387
Gilles Peskine61b91d42018-06-08 16:09:36 +02002388psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2389 psa_algorithm_t alg,
2390 const uint8_t *hash,
2391 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002392 uint8_t *signature,
2393 size_t signature_size,
2394 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002395{
2396 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002397 psa_status_t status;
2398
2399 *signature_length = signature_size;
2400
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002401 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2402 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002403 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002404 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002405 {
2406 status = PSA_ERROR_INVALID_ARGUMENT;
2407 goto exit;
2408 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002409
Gilles Peskine20035e32018-02-03 22:44:14 +01002410#if defined(MBEDTLS_RSA_C)
2411 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2412 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002413 status = psa_rsa_sign( slot->data.rsa,
2414 alg,
2415 hash, hash_length,
2416 signature, signature_size,
2417 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002418 }
2419 else
2420#endif /* defined(MBEDTLS_RSA_C) */
2421#if defined(MBEDTLS_ECP_C)
2422 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2423 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002424#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002425 if(
2426#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2427 PSA_ALG_IS_ECDSA( alg )
2428#else
2429 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2430#endif
2431 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002432 status = psa_ecdsa_sign( slot->data.ecp,
2433 alg,
2434 hash, hash_length,
2435 signature, signature_size,
2436 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002437 else
2438#endif /* defined(MBEDTLS_ECDSA_C) */
2439 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002440 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002441 }
itayzafrir5c753392018-05-08 11:18:38 +03002442 }
2443 else
2444#endif /* defined(MBEDTLS_ECP_C) */
2445 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002446 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002447 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002448
2449exit:
2450 /* Fill the unused part of the output buffer (the whole buffer on error,
2451 * the trailing part on success) with something that isn't a valid mac
2452 * (barring an attack on the mac and deliberately-crafted input),
2453 * in case the caller doesn't check the return status properly. */
2454 if( status == PSA_SUCCESS )
2455 memset( signature + *signature_length, '!',
2456 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002457 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002458 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002459 /* If signature_size is 0 then we have nothing to do. We must not call
2460 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002461 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002462}
2463
2464psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2465 psa_algorithm_t alg,
2466 const uint8_t *hash,
2467 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002468 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002469 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002470{
2471 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002472 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002473
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002474 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2475 if( status != PSA_SUCCESS )
2476 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002477
Gilles Peskine61b91d42018-06-08 16:09:36 +02002478#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002479 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002480 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002481 return( psa_rsa_verify( slot->data.rsa,
2482 alg,
2483 hash, hash_length,
2484 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002485 }
2486 else
2487#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002488#if defined(MBEDTLS_ECP_C)
2489 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2490 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002491#if defined(MBEDTLS_ECDSA_C)
2492 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002493 return( psa_ecdsa_verify( slot->data.ecp,
2494 hash, hash_length,
2495 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002496 else
2497#endif /* defined(MBEDTLS_ECDSA_C) */
2498 {
2499 return( PSA_ERROR_INVALID_ARGUMENT );
2500 }
itayzafrir5c753392018-05-08 11:18:38 +03002501 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002502 else
2503#endif /* defined(MBEDTLS_ECP_C) */
2504 {
2505 return( PSA_ERROR_NOT_SUPPORTED );
2506 }
2507}
2508
Gilles Peskine072ac562018-06-30 00:21:29 +02002509#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2510static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2511 mbedtls_rsa_context *rsa )
2512{
2513 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2514 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2515 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2516 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2517}
2518#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2519
Gilles Peskine61b91d42018-06-08 16:09:36 +02002520psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2521 psa_algorithm_t alg,
2522 const uint8_t *input,
2523 size_t input_length,
2524 const uint8_t *salt,
2525 size_t salt_length,
2526 uint8_t *output,
2527 size_t output_size,
2528 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002529{
2530 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002531 psa_status_t status;
2532
Darryl Green5cc689a2018-07-24 15:34:10 +01002533 (void) input;
2534 (void) input_length;
2535 (void) salt;
2536 (void) output;
2537 (void) output_size;
2538
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002539 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002540
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002541 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2542 return( PSA_ERROR_INVALID_ARGUMENT );
2543
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002544 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2545 if( status != PSA_SUCCESS )
2546 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002547 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2548 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002549 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002550
2551#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002552 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002553 {
2554 mbedtls_rsa_context *rsa = slot->data.rsa;
2555 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002556 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002557 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002558#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002559 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002560 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002561 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2562 mbedtls_ctr_drbg_random,
2563 &global_data.ctr_drbg,
2564 MBEDTLS_RSA_PUBLIC,
2565 input_length,
2566 input,
2567 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002568 }
2569 else
2570#endif /* MBEDTLS_PKCS1_V15 */
2571#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002572 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002573 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002574 psa_rsa_oaep_set_padding_mode( alg, rsa );
2575 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2576 mbedtls_ctr_drbg_random,
2577 &global_data.ctr_drbg,
2578 MBEDTLS_RSA_PUBLIC,
2579 salt, salt_length,
2580 input_length,
2581 input,
2582 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002583 }
2584 else
2585#endif /* MBEDTLS_PKCS1_V21 */
2586 {
2587 return( PSA_ERROR_INVALID_ARGUMENT );
2588 }
2589 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002590 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002591 return( mbedtls_to_psa_error( ret ) );
2592 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002593 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002594#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002595 {
2596 return( PSA_ERROR_NOT_SUPPORTED );
2597 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002598}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002599
Gilles Peskine61b91d42018-06-08 16:09:36 +02002600psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2601 psa_algorithm_t alg,
2602 const uint8_t *input,
2603 size_t input_length,
2604 const uint8_t *salt,
2605 size_t salt_length,
2606 uint8_t *output,
2607 size_t output_size,
2608 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002609{
2610 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002611 psa_status_t status;
2612
Darryl Green5cc689a2018-07-24 15:34:10 +01002613 (void) input;
2614 (void) input_length;
2615 (void) salt;
2616 (void) output;
2617 (void) output_size;
2618
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002619 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002620
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002621 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2622 return( PSA_ERROR_INVALID_ARGUMENT );
2623
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002624 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2625 if( status != PSA_SUCCESS )
2626 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002627 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2628 return( PSA_ERROR_INVALID_ARGUMENT );
2629
2630#if defined(MBEDTLS_RSA_C)
2631 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2632 {
2633 mbedtls_rsa_context *rsa = slot->data.rsa;
2634 int ret;
2635
Gilles Peskine630a18a2018-06-29 17:49:35 +02002636 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002637 return( PSA_ERROR_INVALID_ARGUMENT );
2638
2639#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002640 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002641 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002642 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2643 mbedtls_ctr_drbg_random,
2644 &global_data.ctr_drbg,
2645 MBEDTLS_RSA_PRIVATE,
2646 output_length,
2647 input,
2648 output,
2649 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002650 }
2651 else
2652#endif /* MBEDTLS_PKCS1_V15 */
2653#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002654 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002655 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002656 psa_rsa_oaep_set_padding_mode( alg, rsa );
2657 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2658 mbedtls_ctr_drbg_random,
2659 &global_data.ctr_drbg,
2660 MBEDTLS_RSA_PRIVATE,
2661 salt, salt_length,
2662 output_length,
2663 input,
2664 output,
2665 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002666 }
2667 else
2668#endif /* MBEDTLS_PKCS1_V21 */
2669 {
2670 return( PSA_ERROR_INVALID_ARGUMENT );
2671 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002672
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002673 return( mbedtls_to_psa_error( ret ) );
2674 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002675 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002676#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002677 {
2678 return( PSA_ERROR_NOT_SUPPORTED );
2679 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002680}
Gilles Peskine20035e32018-02-03 22:44:14 +01002681
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002682
2683
mohammad1603503973b2018-03-12 15:59:30 +02002684/****************************************************************/
2685/* Symmetric cryptography */
2686/****************************************************************/
2687
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002688/* Initialize the cipher operation structure. Once this function has been
2689 * called, psa_cipher_abort can run and will do the right thing. */
2690static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2691 psa_algorithm_t alg )
2692{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002693 if( ! PSA_ALG_IS_CIPHER( alg ) )
2694 {
2695 memset( operation, 0, sizeof( *operation ) );
2696 return( PSA_ERROR_INVALID_ARGUMENT );
2697 }
2698
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002699 operation->alg = alg;
2700 operation->key_set = 0;
2701 operation->iv_set = 0;
2702 operation->iv_required = 1;
2703 operation->iv_size = 0;
2704 operation->block_size = 0;
2705 mbedtls_cipher_init( &operation->ctx.cipher );
2706 return( PSA_SUCCESS );
2707}
2708
Gilles Peskinee553c652018-06-04 16:22:46 +02002709static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2710 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002711 psa_algorithm_t alg,
2712 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002713{
2714 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2715 psa_status_t status;
2716 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002717 size_t key_bits;
2718 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002719 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2720 PSA_KEY_USAGE_ENCRYPT :
2721 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002722
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002723 status = psa_cipher_init( operation, alg );
2724 if( status != PSA_SUCCESS )
2725 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002726
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002727 status = psa_get_key_from_slot( key, &slot, usage, alg);
2728 if( status != PSA_SUCCESS )
2729 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002730 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002731
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002732 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002733 if( cipher_info == NULL )
2734 return( PSA_ERROR_NOT_SUPPORTED );
2735
mohammad1603503973b2018-03-12 15:59:30 +02002736 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002737 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002738 {
2739 psa_cipher_abort( operation );
2740 return( mbedtls_to_psa_error( ret ) );
2741 }
2742
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002743#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002744 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002745 {
2746 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2747 unsigned char keys[24];
2748 memcpy( keys, slot->data.raw.data, 16 );
2749 memcpy( keys + 16, slot->data.raw.data, 8 );
2750 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2751 keys,
2752 192, cipher_operation );
2753 }
2754 else
2755#endif
2756 {
2757 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2758 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002759 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002760 }
Moran Peker41deec42018-04-04 15:43:05 +03002761 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002762 {
2763 psa_cipher_abort( operation );
2764 return( mbedtls_to_psa_error( ret ) );
2765 }
2766
mohammad16038481e742018-03-18 13:57:31 +02002767#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002768 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002769 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002770 case PSA_ALG_CBC_NO_PADDING:
2771 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2772 MBEDTLS_PADDING_NONE );
2773 break;
2774 case PSA_ALG_CBC_PKCS7:
2775 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2776 MBEDTLS_PADDING_PKCS7 );
2777 break;
2778 default:
2779 /* The algorithm doesn't involve padding. */
2780 ret = 0;
2781 break;
2782 }
2783 if( ret != 0 )
2784 {
2785 psa_cipher_abort( operation );
2786 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002787 }
2788#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2789
mohammad1603503973b2018-03-12 15:59:30 +02002790 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002791 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2792 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2793 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002794 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002795 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002796 }
mohammad1603503973b2018-03-12 15:59:30 +02002797
Moran Peker395db872018-05-31 14:07:14 +03002798 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002799}
2800
Gilles Peskinefe119512018-07-08 21:39:34 +02002801psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2802 psa_key_slot_t key,
2803 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002804{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002805 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002806}
2807
Gilles Peskinefe119512018-07-08 21:39:34 +02002808psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2809 psa_key_slot_t key,
2810 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002811{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002812 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002813}
2814
Gilles Peskinefe119512018-07-08 21:39:34 +02002815psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2816 unsigned char *iv,
2817 size_t iv_size,
2818 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002819{
itayzafrir534bd7c2018-08-02 13:56:32 +03002820 psa_status_t status;
2821 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002822 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002823 {
2824 status = PSA_ERROR_BAD_STATE;
2825 goto exit;
2826 }
Moran Peker41deec42018-04-04 15:43:05 +03002827 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002828 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002829 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002830 goto exit;
2831 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002832 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2833 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002834 if( ret != 0 )
2835 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002836 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002837 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002838 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002839
mohammad16038481e742018-03-18 13:57:31 +02002840 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002841 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002842
Moran Peker395db872018-05-31 14:07:14 +03002843exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002844 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002845 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002846 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002847}
2848
Gilles Peskinefe119512018-07-08 21:39:34 +02002849psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2850 const unsigned char *iv,
2851 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002852{
itayzafrir534bd7c2018-08-02 13:56:32 +03002853 psa_status_t status;
2854 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002855 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002856 {
2857 status = PSA_ERROR_BAD_STATE;
2858 goto exit;
2859 }
Moran Pekera28258c2018-05-29 16:25:04 +03002860 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002861 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002862 status = PSA_ERROR_INVALID_ARGUMENT;
2863 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002864 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002865 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2866 status = mbedtls_to_psa_error( ret );
2867exit:
2868 if( status == PSA_SUCCESS )
2869 operation->iv_set = 1;
2870 else
mohammad1603503973b2018-03-12 15:59:30 +02002871 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002872 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002873}
2874
Gilles Peskinee553c652018-06-04 16:22:46 +02002875psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2876 const uint8_t *input,
2877 size_t input_length,
2878 unsigned char *output,
2879 size_t output_size,
2880 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002881{
itayzafrir534bd7c2018-08-02 13:56:32 +03002882 psa_status_t status;
2883 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002884 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002885 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002886 {
2887 /* Take the unprocessed partial block left over from previous
2888 * update calls, if any, plus the input to this call. Remove
2889 * the last partial block, if any. You get the data that will be
2890 * output in this call. */
2891 expected_output_size =
2892 ( operation->ctx.cipher.unprocessed_len + input_length )
2893 / operation->block_size * operation->block_size;
2894 }
2895 else
2896 {
2897 expected_output_size = input_length;
2898 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002899
Gilles Peskine89d789c2018-06-04 17:17:16 +02002900 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002901 {
2902 status = PSA_ERROR_BUFFER_TOO_SMALL;
2903 goto exit;
2904 }
mohammad160382759612018-03-12 18:16:40 +02002905
mohammad1603503973b2018-03-12 15:59:30 +02002906 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002907 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002908 status = mbedtls_to_psa_error( ret );
2909exit:
2910 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002911 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002912 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002913}
2914
Gilles Peskinee553c652018-06-04 16:22:46 +02002915psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2916 uint8_t *output,
2917 size_t output_size,
2918 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002919{
Janos Follath315b51c2018-07-09 16:04:51 +01002920 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2921 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002922 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002923
mohammad1603503973b2018-03-12 15:59:30 +02002924 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002925 {
Janos Follath315b51c2018-07-09 16:04:51 +01002926 status = PSA_ERROR_BAD_STATE;
2927 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002928 }
2929 if( operation->iv_required && ! operation->iv_set )
2930 {
Janos Follath315b51c2018-07-09 16:04:51 +01002931 status = PSA_ERROR_BAD_STATE;
2932 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002933 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002934
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002935 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002936 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2937 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002938 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002939 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002940 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002941 }
2942
Janos Follath315b51c2018-07-09 16:04:51 +01002943 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2944 temp_output_buffer,
2945 output_length );
2946 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002947 {
Janos Follath315b51c2018-07-09 16:04:51 +01002948 status = mbedtls_to_psa_error( cipher_ret );
2949 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002950 }
Janos Follath315b51c2018-07-09 16:04:51 +01002951
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002952 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002953 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002954 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002955 memcpy( output, temp_output_buffer, *output_length );
2956 else
2957 {
Janos Follath315b51c2018-07-09 16:04:51 +01002958 status = PSA_ERROR_BUFFER_TOO_SMALL;
2959 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002960 }
mohammad1603503973b2018-03-12 15:59:30 +02002961
Janos Follath279ab8e2018-07-09 16:13:21 +01002962 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002963 status = psa_cipher_abort( operation );
2964
2965 return( status );
2966
2967error:
2968
2969 *output_length = 0;
2970
Janos Follath279ab8e2018-07-09 16:13:21 +01002971 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002972 (void) psa_cipher_abort( operation );
2973
2974 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002975}
2976
Gilles Peskinee553c652018-06-04 16:22:46 +02002977psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2978{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002979 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002980 {
2981 /* The object has (apparently) been initialized but it is not
2982 * in use. It's ok to call abort on such an object, and there's
2983 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002984 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002985 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002986
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002987 /* Sanity check (shouldn't happen: operation->alg should
2988 * always have been initialized to a valid value). */
2989 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2990 return( PSA_ERROR_BAD_STATE );
2991
mohammad1603503973b2018-03-12 15:59:30 +02002992 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002993
Moran Peker41deec42018-04-04 15:43:05 +03002994 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002995 operation->key_set = 0;
2996 operation->iv_set = 0;
2997 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002998 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002999 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003000
Moran Peker395db872018-05-31 14:07:14 +03003001 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003002}
3003
Gilles Peskinea0655c32018-04-30 17:06:50 +02003004
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003005
mohammad16038cc1cee2018-03-28 01:21:33 +03003006/****************************************************************/
3007/* Key Policy */
3008/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003009
mohammad160327010052018-07-03 13:16:15 +03003010#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003011void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003012{
Gilles Peskine803ce742018-06-18 16:07:14 +02003013 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003014}
3015
Gilles Peskine2d277862018-06-18 15:41:12 +02003016void psa_key_policy_set_usage( psa_key_policy_t *policy,
3017 psa_key_usage_t usage,
3018 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003019{
mohammad16034eed7572018-03-28 05:14:59 -07003020 policy->usage = usage;
3021 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003022}
3023
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003024psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003025{
mohammad16036df908f2018-04-02 08:34:15 -07003026 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003027}
3028
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003029psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003030{
mohammad16036df908f2018-04-02 08:34:15 -07003031 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003032}
mohammad160327010052018-07-03 13:16:15 +03003033#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003034
Gilles Peskine2d277862018-06-18 15:41:12 +02003035psa_status_t psa_set_key_policy( psa_key_slot_t key,
3036 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003037{
3038 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003039 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003040
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003041 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003042 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003043
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003044 status = psa_get_empty_key_slot( key, &slot );
3045 if( status != PSA_SUCCESS )
3046 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003047
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003048 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3049 PSA_KEY_USAGE_ENCRYPT |
3050 PSA_KEY_USAGE_DECRYPT |
3051 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003052 PSA_KEY_USAGE_VERIFY |
3053 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003054 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003055
mohammad16036df908f2018-04-02 08:34:15 -07003056 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003057
3058 return( PSA_SUCCESS );
3059}
3060
Gilles Peskine2d277862018-06-18 15:41:12 +02003061psa_status_t psa_get_key_policy( psa_key_slot_t key,
3062 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003063{
3064 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003065 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003066
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003067 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003068 return( PSA_ERROR_INVALID_ARGUMENT );
3069
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003070 status = psa_get_key_slot( key, &slot );
3071 if( status != PSA_SUCCESS )
3072 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003073
mohammad16036df908f2018-04-02 08:34:15 -07003074 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003075
3076 return( PSA_SUCCESS );
3077}
Gilles Peskine20035e32018-02-03 22:44:14 +01003078
Gilles Peskinea0655c32018-04-30 17:06:50 +02003079
3080
mohammad1603804cd712018-03-20 22:44:08 +02003081/****************************************************************/
3082/* Key Lifetime */
3083/****************************************************************/
3084
Gilles Peskine2d277862018-06-18 15:41:12 +02003085psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
3086 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003087{
3088 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003089 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003090
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003091 status = psa_get_key_slot( key, &slot );
3092 if( status != PSA_SUCCESS )
3093 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003094
mohammad1603804cd712018-03-20 22:44:08 +02003095 *lifetime = slot->lifetime;
3096
3097 return( PSA_SUCCESS );
3098}
3099
Gilles Peskine2d277862018-06-18 15:41:12 +02003100psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01003101 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003102{
3103 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003104 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003105
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003106 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
3107 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
Darryl Greend49a4992018-06-18 17:27:26 +01003108 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003109 return( PSA_ERROR_INVALID_ARGUMENT );
3110
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003111 status = psa_get_empty_key_slot( key, &slot );
3112 if( status != PSA_SUCCESS )
3113 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02003114
Darryl Greend49a4992018-06-18 17:27:26 +01003115 if( lifetime == PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003116 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003117
Darryl Greend49a4992018-06-18 17:27:26 +01003118#if !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
3119 if( lifetime == PSA_KEY_LIFETIME_PERSISTENT )
3120 return( PSA_ERROR_NOT_SUPPORTED );
3121#endif
3122
mohammad1603060ad8a2018-03-20 14:28:38 -07003123 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02003124
3125 return( PSA_SUCCESS );
3126}
3127
Gilles Peskine20035e32018-02-03 22:44:14 +01003128
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003129
mohammad16035955c982018-04-26 00:53:03 +03003130/****************************************************************/
3131/* AEAD */
3132/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003133
Gilles Peskineedf9a652018-08-17 18:11:56 +02003134typedef struct
3135{
3136 key_slot_t *slot;
3137 const mbedtls_cipher_info_t *cipher_info;
3138 union
3139 {
3140#if defined(MBEDTLS_CCM_C)
3141 mbedtls_ccm_context ccm;
3142#endif /* MBEDTLS_CCM_C */
3143#if defined(MBEDTLS_GCM_C)
3144 mbedtls_gcm_context gcm;
3145#endif /* MBEDTLS_GCM_C */
3146 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003147 psa_algorithm_t core_alg;
3148 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003149 uint8_t tag_length;
3150} aead_operation_t;
3151
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003152static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003153{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003154 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003155 {
3156#if defined(MBEDTLS_CCM_C)
3157 case PSA_ALG_CCM:
3158 mbedtls_ccm_free( &operation->ctx.ccm );
3159 break;
3160#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003161#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003162 case PSA_ALG_GCM:
3163 mbedtls_gcm_free( &operation->ctx.gcm );
3164 break;
3165#endif /* MBEDTLS_GCM_C */
3166 }
3167}
3168
3169static psa_status_t psa_aead_setup( aead_operation_t *operation,
3170 psa_key_slot_t key,
3171 psa_key_usage_t usage,
3172 psa_algorithm_t alg )
3173{
3174 psa_status_t status;
3175 size_t key_bits;
3176 mbedtls_cipher_id_t cipher_id;
3177
3178 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
3179 if( status != PSA_SUCCESS )
3180 return( status );
3181
3182 key_bits = psa_get_key_bits( operation->slot );
3183
3184 operation->cipher_info =
3185 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3186 &cipher_id );
3187 if( operation->cipher_info == NULL )
3188 return( PSA_ERROR_NOT_SUPPORTED );
3189
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003190 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003191 {
3192#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003193 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3194 operation->core_alg = PSA_ALG_CCM;
3195 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003196 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3197 return( PSA_ERROR_INVALID_ARGUMENT );
3198 mbedtls_ccm_init( &operation->ctx.ccm );
3199 status = mbedtls_to_psa_error(
3200 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3201 operation->slot->data.raw.data,
3202 (unsigned int) key_bits ) );
3203 if( status != 0 )
3204 goto cleanup;
3205 break;
3206#endif /* MBEDTLS_CCM_C */
3207
3208#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003209 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3210 operation->core_alg = PSA_ALG_GCM;
3211 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003212 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3213 return( PSA_ERROR_INVALID_ARGUMENT );
3214 mbedtls_gcm_init( &operation->ctx.gcm );
3215 status = mbedtls_to_psa_error(
3216 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3217 operation->slot->data.raw.data,
3218 (unsigned int) key_bits ) );
3219 break;
3220#endif /* MBEDTLS_GCM_C */
3221
3222 default:
3223 return( PSA_ERROR_NOT_SUPPORTED );
3224 }
3225
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003226 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3227 {
3228 status = PSA_ERROR_INVALID_ARGUMENT;
3229 goto cleanup;
3230 }
3231 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3232 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3233 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3234 * In both cases, mbedtls_xxx will validate the tag length below. */
3235
Gilles Peskineedf9a652018-08-17 18:11:56 +02003236 return( PSA_SUCCESS );
3237
3238cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003239 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003240 return( status );
3241}
3242
mohammad16035955c982018-04-26 00:53:03 +03003243psa_status_t psa_aead_encrypt( psa_key_slot_t key,
3244 psa_algorithm_t alg,
3245 const uint8_t *nonce,
3246 size_t nonce_length,
3247 const uint8_t *additional_data,
3248 size_t additional_data_length,
3249 const uint8_t *plaintext,
3250 size_t plaintext_length,
3251 uint8_t *ciphertext,
3252 size_t ciphertext_size,
3253 size_t *ciphertext_length )
3254{
mohammad16035955c982018-04-26 00:53:03 +03003255 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003256 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003257 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003258
mohammad1603f08a5502018-06-03 15:05:47 +03003259 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003260
Gilles Peskineedf9a652018-08-17 18:11:56 +02003261 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003262 if( status != PSA_SUCCESS )
3263 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003264
Gilles Peskineedf9a652018-08-17 18:11:56 +02003265 /* For all currently supported modes, the tag is at the end of the
3266 * ciphertext. */
3267 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3268 {
3269 status = PSA_ERROR_BUFFER_TOO_SMALL;
3270 goto exit;
3271 }
3272 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003273
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003274#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003275 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003276 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003277 status = mbedtls_to_psa_error(
3278 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3279 MBEDTLS_GCM_ENCRYPT,
3280 plaintext_length,
3281 nonce, nonce_length,
3282 additional_data, additional_data_length,
3283 plaintext, ciphertext,
3284 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003285 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003286 else
3287#endif /* MBEDTLS_GCM_C */
3288#if defined(MBEDTLS_CCM_C)
3289 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003290 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003291 status = mbedtls_to_psa_error(
3292 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3293 plaintext_length,
3294 nonce, nonce_length,
3295 additional_data,
3296 additional_data_length,
3297 plaintext, ciphertext,
3298 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003299 }
mohammad16035c8845f2018-05-09 05:40:09 -07003300 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003301#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003302 {
mohammad1603554faad2018-06-03 15:07:38 +03003303 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003304 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003305
Gilles Peskineedf9a652018-08-17 18:11:56 +02003306 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3307 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003308
Gilles Peskineedf9a652018-08-17 18:11:56 +02003309exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003310 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003311 if( status == PSA_SUCCESS )
3312 *ciphertext_length = plaintext_length + operation.tag_length;
3313 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003314}
3315
Gilles Peskineee652a32018-06-01 19:23:52 +02003316/* Locate the tag in a ciphertext buffer containing the encrypted data
3317 * followed by the tag. Return the length of the part preceding the tag in
3318 * *plaintext_length. This is the size of the plaintext in modes where
3319 * the encrypted data has the same size as the plaintext, such as
3320 * CCM and GCM. */
3321static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3322 const uint8_t *ciphertext,
3323 size_t ciphertext_length,
3324 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003325 const uint8_t **p_tag )
3326{
3327 size_t payload_length;
3328 if( tag_length > ciphertext_length )
3329 return( PSA_ERROR_INVALID_ARGUMENT );
3330 payload_length = ciphertext_length - tag_length;
3331 if( payload_length > plaintext_size )
3332 return( PSA_ERROR_BUFFER_TOO_SMALL );
3333 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003334 return( PSA_SUCCESS );
3335}
3336
mohammad16035955c982018-04-26 00:53:03 +03003337psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3338 psa_algorithm_t alg,
3339 const uint8_t *nonce,
3340 size_t nonce_length,
3341 const uint8_t *additional_data,
3342 size_t additional_data_length,
3343 const uint8_t *ciphertext,
3344 size_t ciphertext_length,
3345 uint8_t *plaintext,
3346 size_t plaintext_size,
3347 size_t *plaintext_length )
3348{
mohammad16035955c982018-04-26 00:53:03 +03003349 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003350 aead_operation_t operation;
3351 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003352
Gilles Peskineee652a32018-06-01 19:23:52 +02003353 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003354
Gilles Peskineedf9a652018-08-17 18:11:56 +02003355 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003356 if( status != PSA_SUCCESS )
3357 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003358
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003359#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003360 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003361 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003362 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003363 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003364 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003365 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003366 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003367
Gilles Peskineedf9a652018-08-17 18:11:56 +02003368 status = mbedtls_to_psa_error(
3369 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3370 ciphertext_length - operation.tag_length,
3371 nonce, nonce_length,
3372 additional_data,
3373 additional_data_length,
3374 tag, operation.tag_length,
3375 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003376 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003377 else
3378#endif /* MBEDTLS_GCM_C */
3379#if defined(MBEDTLS_CCM_C)
3380 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003381 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003382 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003383 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003384 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003385 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003386 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003387
Gilles Peskineedf9a652018-08-17 18:11:56 +02003388 status = mbedtls_to_psa_error(
3389 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3390 ciphertext_length - operation.tag_length,
3391 nonce, nonce_length,
3392 additional_data,
3393 additional_data_length,
3394 ciphertext, plaintext,
3395 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003396 }
mohammad160339574652018-06-01 04:39:53 -07003397 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003398#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003399 {
mohammad1603554faad2018-06-03 15:07:38 +03003400 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003401 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003402
Gilles Peskineedf9a652018-08-17 18:11:56 +02003403 if( status != PSA_SUCCESS && plaintext_size != 0 )
3404 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003405
Gilles Peskineedf9a652018-08-17 18:11:56 +02003406exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003407 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003408 if( status == PSA_SUCCESS )
3409 *plaintext_length = ciphertext_length - operation.tag_length;
3410 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003411}
3412
Gilles Peskinea0655c32018-04-30 17:06:50 +02003413
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003414
Gilles Peskine20035e32018-02-03 22:44:14 +01003415/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003416/* Generators */
3417/****************************************************************/
3418
3419psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3420{
3421 psa_status_t status = PSA_SUCCESS;
3422 if( generator->alg == 0 )
3423 {
3424 /* The object has (apparently) been initialized but it is not
3425 * in use. It's ok to call abort on such an object, and there's
3426 * nothing to do. */
3427 }
3428 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003429 if( generator->alg == PSA_ALG_SELECT_RAW )
3430 {
3431 if( generator->ctx.buffer.data != NULL )
3432 {
3433 mbedtls_zeroize( generator->ctx.buffer.data,
3434 generator->ctx.buffer.size );
3435 mbedtls_free( generator->ctx.buffer.data );
3436 }
3437 }
3438 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003439#if defined(MBEDTLS_MD_C)
3440 if( PSA_ALG_IS_HKDF( generator->alg ) )
3441 {
3442 mbedtls_free( generator->ctx.hkdf.info );
3443 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3444 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003445 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3446 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3447 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003448 {
3449 if( generator->ctx.tls12_prf.key != NULL )
3450 {
3451 mbedtls_zeroize( generator->ctx.tls12_prf.key,
3452 generator->ctx.tls12_prf.key_len );
3453 mbedtls_free( generator->ctx.tls12_prf.key );
3454 }
Hanno Becker580fba12018-11-13 20:50:45 +00003455
3456 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3457 {
3458 mbedtls_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
3459 generator->ctx.tls12_prf.Ai_with_seed_len );
3460 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3461 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003462 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003463 else
3464#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003465 {
3466 status = PSA_ERROR_BAD_STATE;
3467 }
3468 memset( generator, 0, sizeof( *generator ) );
3469 return( status );
3470}
3471
3472
3473psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3474 size_t *capacity)
3475{
3476 *capacity = generator->capacity;
3477 return( PSA_SUCCESS );
3478}
3479
Gilles Peskinebef7f142018-07-12 17:22:21 +02003480#if defined(MBEDTLS_MD_C)
3481/* Read some bytes from an HKDF-based generator. This performs a chunk
3482 * of the expand phase of the HKDF algorithm. */
3483static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3484 psa_algorithm_t hash_alg,
3485 uint8_t *output,
3486 size_t output_length )
3487{
3488 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3489 psa_status_t status;
3490
3491 while( output_length != 0 )
3492 {
3493 /* Copy what remains of the current block */
3494 uint8_t n = hash_length - hkdf->offset_in_block;
3495 if( n > output_length )
3496 n = (uint8_t) output_length;
3497 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3498 output += n;
3499 output_length -= n;
3500 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003501 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003502 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003503 /* We can't be wanting more output after block 0xff, otherwise
3504 * the capacity check in psa_generator_read() would have
3505 * prevented this call. It could happen only if the generator
3506 * object was corrupted or if this function is called directly
3507 * inside the library. */
3508 if( hkdf->block_number == 0xff )
3509 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003510
3511 /* We need a new block */
3512 ++hkdf->block_number;
3513 hkdf->offset_in_block = 0;
3514 status = psa_hmac_setup_internal( &hkdf->hmac,
3515 hkdf->prk, hash_length,
3516 hash_alg );
3517 if( status != PSA_SUCCESS )
3518 return( status );
3519 if( hkdf->block_number != 1 )
3520 {
3521 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3522 hkdf->output_block,
3523 hash_length );
3524 if( status != PSA_SUCCESS )
3525 return( status );
3526 }
3527 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3528 hkdf->info,
3529 hkdf->info_length );
3530 if( status != PSA_SUCCESS )
3531 return( status );
3532 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3533 &hkdf->block_number, 1 );
3534 if( status != PSA_SUCCESS )
3535 return( status );
3536 status = psa_hmac_finish_internal( &hkdf->hmac,
3537 hkdf->output_block,
3538 sizeof( hkdf->output_block ) );
3539 if( status != PSA_SUCCESS )
3540 return( status );
3541 }
3542
3543 return( PSA_SUCCESS );
3544}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003545
3546static psa_status_t psa_generator_tls12_prf_generate_next_block(
3547 psa_tls12_prf_generator_t *tls12_prf,
3548 psa_algorithm_t alg )
3549{
3550 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3551 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3552 psa_hmac_internal_data hmac;
3553 psa_status_t status, cleanup_status;
3554
Hanno Becker3b339e22018-11-13 20:56:14 +00003555 unsigned char *Ai;
3556 size_t Ai_len;
3557
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003558 /* We can't be wanting more output after block 0xff, otherwise
3559 * the capacity check in psa_generator_read() would have
3560 * prevented this call. It could happen only if the generator
3561 * object was corrupted or if this function is called directly
3562 * inside the library. */
3563 if( tls12_prf->block_number == 0xff )
3564 return( PSA_ERROR_BAD_STATE );
3565
3566 /* We need a new block */
3567 ++tls12_prf->block_number;
3568 tls12_prf->offset_in_block = 0;
3569
3570 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3571 *
3572 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3573 *
3574 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3575 * HMAC_hash(secret, A(2) + seed) +
3576 * HMAC_hash(secret, A(3) + seed) + ...
3577 *
3578 * A(0) = seed
3579 * A(i) = HMAC_hash( secret, A(i-1) )
3580 *
3581 * The `psa_tls12_prf_generator` structures saves the block
3582 * `HMAC_hash(secret, A(i) + seed)` from which the output
3583 * is currently extracted as `output_block`, while
3584 * `A(i) + seed` is stored in `Ai_with_seed`.
3585 *
3586 * Generating a new block means recalculating `Ai_with_seed`
3587 * from the A(i)-part of it, and afterwards recalculating
3588 * `output_block`.
3589 *
3590 * A(0) is computed at setup time.
3591 *
3592 */
3593
3594 psa_hmac_init_internal( &hmac );
3595
3596 /* We must distinguish the calculation of A(1) from those
3597 * of A(2) and higher, because A(0)=seed has a different
3598 * length than the other A(i). */
3599 if( tls12_prf->block_number == 1 )
3600 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003601 Ai = tls12_prf->Ai_with_seed + hash_length;
3602 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003603 }
3604 else
3605 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003606 Ai = tls12_prf->Ai_with_seed;
3607 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003608 }
3609
Hanno Becker3b339e22018-11-13 20:56:14 +00003610 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3611 status = psa_hmac_setup_internal( &hmac,
3612 tls12_prf->key,
3613 tls12_prf->key_len,
3614 hash_alg );
3615 if( status != PSA_SUCCESS )
3616 goto cleanup;
3617
3618 status = psa_hash_update( &hmac.hash_ctx,
3619 Ai, Ai_len );
3620 if( status != PSA_SUCCESS )
3621 goto cleanup;
3622
3623 status = psa_hmac_finish_internal( &hmac,
3624 tls12_prf->Ai_with_seed,
3625 hash_length );
3626 if( status != PSA_SUCCESS )
3627 goto cleanup;
3628
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003629 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3630 status = psa_hmac_setup_internal( &hmac,
3631 tls12_prf->key,
3632 tls12_prf->key_len,
3633 hash_alg );
3634 if( status != PSA_SUCCESS )
3635 goto cleanup;
3636
3637 status = psa_hash_update( &hmac.hash_ctx,
3638 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003639 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003640 if( status != PSA_SUCCESS )
3641 goto cleanup;
3642
3643 status = psa_hmac_finish_internal( &hmac,
3644 tls12_prf->output_block,
3645 hash_length );
3646 if( status != PSA_SUCCESS )
3647 goto cleanup;
3648
3649cleanup:
3650
3651 cleanup_status = psa_hmac_abort_internal( &hmac );
3652 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3653 status = cleanup_status;
3654
3655 return( status );
3656}
3657
3658/* Read some bytes from an TLS-1.2-PRF-based generator.
3659 * See Section 5 of RFC 5246. */
3660static psa_status_t psa_generator_tls12_prf_read(
3661 psa_tls12_prf_generator_t *tls12_prf,
3662 psa_algorithm_t alg,
3663 uint8_t *output,
3664 size_t output_length )
3665{
3666 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3667 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3668 psa_status_t status;
3669
3670 while( output_length != 0 )
3671 {
3672 /* Copy what remains of the current block */
3673 uint8_t n = hash_length - tls12_prf->offset_in_block;
3674
3675 /* Check if we have fully processed the current block. */
3676 if( n == 0 )
3677 {
3678 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3679 alg );
3680 if( status != PSA_SUCCESS )
3681 return( status );
3682
3683 continue;
3684 }
3685
3686 if( n > output_length )
3687 n = (uint8_t) output_length;
3688 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3689 n );
3690 output += n;
3691 output_length -= n;
3692 tls12_prf->offset_in_block += n;
3693 }
3694
3695 return( PSA_SUCCESS );
3696}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003697#endif /* MBEDTLS_MD_C */
3698
Gilles Peskineeab56e42018-07-12 17:12:33 +02003699psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3700 uint8_t *output,
3701 size_t output_length )
3702{
3703 psa_status_t status;
3704
3705 if( output_length > generator->capacity )
3706 {
3707 generator->capacity = 0;
3708 /* Go through the error path to wipe all confidential data now
3709 * that the generator object is useless. */
3710 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3711 goto exit;
3712 }
3713 if( output_length == 0 &&
3714 generator->capacity == 0 && generator->alg == 0 )
3715 {
3716 /* Edge case: this is a blank or finished generator, and 0
3717 * bytes were requested. The right error in this case could
3718 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3719 * INSUFFICIENT_CAPACITY, which is right for a finished
3720 * generator, for consistency with the case when
3721 * output_length > 0. */
3722 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3723 }
3724 generator->capacity -= output_length;
3725
Gilles Peskine751d9652018-09-18 12:05:44 +02003726 if( generator->alg == PSA_ALG_SELECT_RAW )
3727 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003728 /* Initially, the capacity of a selection generator is always
3729 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3730 * abbreviated in this comment as `size`. When the remaining
3731 * capacity is `c`, the next bytes to serve start `c` bytes
3732 * from the end of the buffer, i.e. `size - c` from the
3733 * beginning of the buffer. Since `generator->capacity` was just
3734 * decremented above, we need to serve the bytes from
3735 * `size - generator->capacity - output_length` to
3736 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003737 size_t offset =
3738 generator->ctx.buffer.size - generator->capacity - output_length;
3739 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3740 status = PSA_SUCCESS;
3741 }
3742 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003743#if defined(MBEDTLS_MD_C)
3744 if( PSA_ALG_IS_HKDF( generator->alg ) )
3745 {
3746 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3747 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3748 output, output_length );
3749 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003750 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3751 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003752 {
3753 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3754 generator->alg, output,
3755 output_length );
3756 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003757 else
3758#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003759 {
3760 return( PSA_ERROR_BAD_STATE );
3761 }
3762
3763exit:
3764 if( status != PSA_SUCCESS )
3765 {
3766 psa_generator_abort( generator );
3767 memset( output, '!', output_length );
3768 }
3769 return( status );
3770}
3771
Gilles Peskine08542d82018-07-19 17:05:42 +02003772#if defined(MBEDTLS_DES_C)
3773static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3774{
3775 if( data_size >= 8 )
3776 mbedtls_des_key_set_parity( data );
3777 if( data_size >= 16 )
3778 mbedtls_des_key_set_parity( data + 8 );
3779 if( data_size >= 24 )
3780 mbedtls_des_key_set_parity( data + 16 );
3781}
3782#endif /* MBEDTLS_DES_C */
3783
Gilles Peskineeab56e42018-07-12 17:12:33 +02003784psa_status_t psa_generator_import_key( psa_key_slot_t key,
3785 psa_key_type_t type,
3786 size_t bits,
3787 psa_crypto_generator_t *generator )
3788{
3789 uint8_t *data = NULL;
3790 size_t bytes = PSA_BITS_TO_BYTES( bits );
3791 psa_status_t status;
3792
3793 if( ! key_type_is_raw_bytes( type ) )
3794 return( PSA_ERROR_INVALID_ARGUMENT );
3795 if( bits % 8 != 0 )
3796 return( PSA_ERROR_INVALID_ARGUMENT );
3797 data = mbedtls_calloc( 1, bytes );
3798 if( data == NULL )
3799 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3800
3801 status = psa_generator_read( generator, data, bytes );
3802 if( status != PSA_SUCCESS )
3803 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003804#if defined(MBEDTLS_DES_C)
3805 if( type == PSA_KEY_TYPE_DES )
3806 psa_des_set_key_parity( data, bytes );
3807#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003808 status = psa_import_key( key, type, data, bytes );
3809
3810exit:
3811 mbedtls_free( data );
3812 return( status );
3813}
3814
3815
3816
3817/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003818/* Key derivation */
3819/****************************************************************/
3820
Gilles Peskinea05219c2018-11-16 16:02:56 +01003821#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003822/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003823 * of the HKDF algorithm.
3824 *
3825 * Note that if this function fails, you must call psa_generator_abort()
3826 * to potentially free embedded data structures and wipe confidential data.
3827 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003828static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003829 const uint8_t *secret,
3830 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003831 psa_algorithm_t hash_alg,
3832 const uint8_t *salt,
3833 size_t salt_length,
3834 const uint8_t *label,
3835 size_t label_length )
3836{
3837 psa_status_t status;
3838 status = psa_hmac_setup_internal( &hkdf->hmac,
3839 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003840 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003841 if( status != PSA_SUCCESS )
3842 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003843 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003844 if( status != PSA_SUCCESS )
3845 return( status );
3846 status = psa_hmac_finish_internal( &hkdf->hmac,
3847 hkdf->prk,
3848 sizeof( hkdf->prk ) );
3849 if( status != PSA_SUCCESS )
3850 return( status );
3851 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3852 hkdf->block_number = 0;
3853 hkdf->info_length = label_length;
3854 if( label_length != 0 )
3855 {
3856 hkdf->info = mbedtls_calloc( 1, label_length );
3857 if( hkdf->info == NULL )
3858 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3859 memcpy( hkdf->info, label, label_length );
3860 }
3861 return( PSA_SUCCESS );
3862}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003863#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003864
Gilles Peskinea05219c2018-11-16 16:02:56 +01003865#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003866/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3867 *
3868 * Note that if this function fails, you must call psa_generator_abort()
3869 * to potentially free embedded data structures and wipe confidential data.
3870 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003871static psa_status_t psa_generator_tls12_prf_setup(
3872 psa_tls12_prf_generator_t *tls12_prf,
3873 const unsigned char *key,
3874 size_t key_len,
3875 psa_algorithm_t hash_alg,
3876 const uint8_t *salt,
3877 size_t salt_length,
3878 const uint8_t *label,
3879 size_t label_length )
3880{
3881 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003882 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3883 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003884
3885 tls12_prf->key = mbedtls_calloc( 1, key_len );
3886 if( tls12_prf->key == NULL )
3887 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3888 tls12_prf->key_len = key_len;
3889 memcpy( tls12_prf->key, key, key_len );
3890
Hanno Becker580fba12018-11-13 20:50:45 +00003891 overflow = ( salt_length + label_length < salt_length ) ||
3892 ( salt_length + label_length + hash_length < hash_length );
3893 if( overflow )
3894 return( PSA_ERROR_INVALID_ARGUMENT );
3895
3896 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3897 if( tls12_prf->Ai_with_seed == NULL )
3898 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3899 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3900
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003901 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3902 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003903 if( label_length != 0 )
3904 {
3905 memcpy( tls12_prf->Ai_with_seed + hash_length,
3906 label, label_length );
3907 }
3908
3909 if( salt_length != 0 )
3910 {
3911 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3912 salt, salt_length );
3913 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003914
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003915 /* The first block gets generated when
3916 * psa_generator_read() is called. */
3917 tls12_prf->block_number = 0;
3918 tls12_prf->offset_in_block = hash_length;
3919
3920 return( PSA_SUCCESS );
3921}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003922
3923/* Set up a TLS-1.2-PSK-to-MS-based generator. */
3924static psa_status_t psa_generator_tls12_psk_to_ms_setup(
3925 psa_tls12_prf_generator_t *tls12_prf,
3926 const unsigned char *psk,
3927 size_t psk_len,
3928 psa_algorithm_t hash_alg,
3929 const uint8_t *salt,
3930 size_t salt_length,
3931 const uint8_t *label,
3932 size_t label_length )
3933{
3934 psa_status_t status;
3935 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
3936
3937 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
3938 return( PSA_ERROR_INVALID_ARGUMENT );
3939
3940 /* Quoting RFC 4279, Section 2:
3941 *
3942 * The premaster secret is formed as follows: if the PSK is N octets
3943 * long, concatenate a uint16 with the value N, N zero octets, a second
3944 * uint16 with the value N, and the PSK itself.
3945 */
3946
3947 pms[0] = ( psk_len >> 8 ) & 0xff;
3948 pms[1] = ( psk_len >> 0 ) & 0xff;
3949 memset( pms + 2, 0, psk_len );
3950 pms[2 + psk_len + 0] = pms[0];
3951 pms[2 + psk_len + 1] = pms[1];
3952 memcpy( pms + 4 + psk_len, psk, psk_len );
3953
3954 status = psa_generator_tls12_prf_setup( tls12_prf,
3955 pms, 4 + 2 * psk_len,
3956 hash_alg,
3957 salt, salt_length,
3958 label, label_length );
3959
3960 mbedtls_zeroize( pms, sizeof( pms ) );
3961 return( status );
3962}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003963#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003964
Gilles Peskine346797d2018-11-16 16:05:06 +01003965/* Note that if this function fails, you must call psa_generator_abort()
3966 * to potentially free embedded data structures and wipe confidential data.
3967 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003968static psa_status_t psa_key_derivation_internal(
3969 psa_crypto_generator_t *generator,
3970 const uint8_t *secret, size_t secret_length,
3971 psa_algorithm_t alg,
3972 const uint8_t *salt, size_t salt_length,
3973 const uint8_t *label, size_t label_length,
3974 size_t capacity )
3975{
3976 psa_status_t status;
3977 size_t max_capacity;
3978
3979 /* Set generator->alg even on failure so that abort knows what to do. */
3980 generator->alg = alg;
3981
Gilles Peskine751d9652018-09-18 12:05:44 +02003982 if( alg == PSA_ALG_SELECT_RAW )
3983 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003984 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02003985 if( salt_length != 0 )
3986 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01003987 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02003988 if( label_length != 0 )
3989 return( PSA_ERROR_INVALID_ARGUMENT );
3990 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
3991 if( generator->ctx.buffer.data == NULL )
3992 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3993 memcpy( generator->ctx.buffer.data, secret, secret_length );
3994 generator->ctx.buffer.size = secret_length;
3995 max_capacity = secret_length;
3996 status = PSA_SUCCESS;
3997 }
3998 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003999#if defined(MBEDTLS_MD_C)
4000 if( PSA_ALG_IS_HKDF( alg ) )
4001 {
4002 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4003 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4004 if( hash_size == 0 )
4005 return( PSA_ERROR_NOT_SUPPORTED );
4006 max_capacity = 255 * hash_size;
4007 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4008 secret, secret_length,
4009 hash_alg,
4010 salt, salt_length,
4011 label, label_length );
4012 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004013 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4014 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4015 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004016 {
4017 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4018 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4019
4020 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4021 if( hash_alg != PSA_ALG_SHA_256 &&
4022 hash_alg != PSA_ALG_SHA_384 )
4023 {
4024 return( PSA_ERROR_NOT_SUPPORTED );
4025 }
4026
4027 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004028
4029 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4030 {
4031 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4032 secret, secret_length,
4033 hash_alg, salt, salt_length,
4034 label, label_length );
4035 }
4036 else
4037 {
4038 status = psa_generator_tls12_psk_to_ms_setup(
4039 &generator->ctx.tls12_prf,
4040 secret, secret_length,
4041 hash_alg, salt, salt_length,
4042 label, label_length );
4043 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004044 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004045 else
4046#endif
4047 {
4048 return( PSA_ERROR_NOT_SUPPORTED );
4049 }
4050
4051 if( status != PSA_SUCCESS )
4052 return( status );
4053
4054 if( capacity <= max_capacity )
4055 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004056 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4057 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004058 else
4059 return( PSA_ERROR_INVALID_ARGUMENT );
4060
4061 return( PSA_SUCCESS );
4062}
4063
Gilles Peskineea0fb492018-07-12 17:17:20 +02004064psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01004065 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004066 psa_algorithm_t alg,
4067 const uint8_t *salt,
4068 size_t salt_length,
4069 const uint8_t *label,
4070 size_t label_length,
4071 size_t capacity )
4072{
4073 key_slot_t *slot;
4074 psa_status_t status;
4075
4076 if( generator->alg != 0 )
4077 return( PSA_ERROR_BAD_STATE );
4078
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004079 /* Make sure that alg is a key derivation algorithm. This prevents
4080 * key selection algorithms, which psa_key_derivation_internal
4081 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004082 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4083 return( PSA_ERROR_INVALID_ARGUMENT );
4084
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004085 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
4086 if( status != PSA_SUCCESS )
4087 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004088
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004089 if( slot->type != PSA_KEY_TYPE_DERIVE )
4090 return( PSA_ERROR_INVALID_ARGUMENT );
4091
4092 status = psa_key_derivation_internal( generator,
4093 slot->data.raw.data,
4094 slot->data.raw.bytes,
4095 alg,
4096 salt, salt_length,
4097 label, label_length,
4098 capacity );
4099 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004100 psa_generator_abort( generator );
4101 return( status );
4102}
4103
4104
4105
4106/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004107/* Key agreement */
4108/****************************************************************/
4109
Gilles Peskinea05219c2018-11-16 16:02:56 +01004110#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004111static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4112 size_t peer_key_length,
4113 const mbedtls_ecp_keypair *our_key,
4114 uint8_t *shared_secret,
4115 size_t shared_secret_size,
4116 size_t *shared_secret_length )
4117{
4118 mbedtls_pk_context pk;
4119 mbedtls_ecp_keypair *their_key = NULL;
4120 mbedtls_ecdh_context ecdh;
4121 int ret;
4122 mbedtls_ecdh_init( &ecdh );
4123 mbedtls_pk_init( &pk );
4124
4125 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
4126 if( ret != 0 )
4127 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02004128 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004129 {
Gilles Peskine88714d72018-10-25 23:07:25 +02004130 case MBEDTLS_PK_ECKEY:
4131 case MBEDTLS_PK_ECKEY_DH:
4132 break;
4133 default:
4134 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4135 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004136 }
4137 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01004138 if( their_key->grp.id != our_key->grp.id )
4139 {
4140 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4141 goto exit;
4142 }
4143
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004144 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
4145 if( ret != 0 )
4146 goto exit;
4147 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
4148 if( ret != 0 )
4149 goto exit;
4150
4151 ret = mbedtls_ecdh_calc_secret( &ecdh,
4152 shared_secret_length,
4153 shared_secret, shared_secret_size,
4154 mbedtls_ctr_drbg_random,
4155 &global_data.ctr_drbg );
4156
4157exit:
4158 mbedtls_pk_free( &pk );
4159 mbedtls_ecdh_free( &ecdh );
4160 return( mbedtls_to_psa_error( ret ) );
4161}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004162#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004163
Gilles Peskine01d718c2018-09-18 12:01:02 +02004164#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4165
Gilles Peskine346797d2018-11-16 16:05:06 +01004166/* Note that if this function fails, you must call psa_generator_abort()
4167 * to potentially free embedded data structures and wipe confidential data.
4168 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004169static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
4170 key_slot_t *private_key,
4171 const uint8_t *peer_key,
4172 size_t peer_key_length,
4173 psa_algorithm_t alg )
4174{
4175 psa_status_t status;
4176 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4177 size_t shared_secret_length = 0;
4178
4179 /* Step 1: run the secret agreement algorithm to generate the shared
4180 * secret. */
4181 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4182 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004183#if defined(MBEDTLS_ECDH_C)
4184 case PSA_ALG_ECDH_BASE:
4185 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4186 return( PSA_ERROR_INVALID_ARGUMENT );
4187 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4188 private_key->data.ecp,
4189 shared_secret,
4190 sizeof( shared_secret ),
4191 &shared_secret_length );
4192 break;
4193#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004194 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004195 (void) private_key;
4196 (void) peer_key;
4197 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004198 return( PSA_ERROR_NOT_SUPPORTED );
4199 }
4200 if( status != PSA_SUCCESS )
4201 goto exit;
4202
4203 /* Step 2: set up the key derivation to generate key material from
4204 * the shared secret. */
4205 status = psa_key_derivation_internal( generator,
4206 shared_secret, shared_secret_length,
4207 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4208 NULL, 0, NULL, 0,
4209 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4210exit:
4211 mbedtls_zeroize( shared_secret, shared_secret_length );
4212 return( status );
4213}
4214
4215psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
4216 psa_key_slot_t private_key,
4217 const uint8_t *peer_key,
4218 size_t peer_key_length,
4219 psa_algorithm_t alg )
4220{
4221 key_slot_t *slot;
4222 psa_status_t status;
4223 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4224 return( PSA_ERROR_INVALID_ARGUMENT );
4225 status = psa_get_key_from_slot( private_key, &slot,
4226 PSA_KEY_USAGE_DERIVE, alg );
4227 if( status != PSA_SUCCESS )
4228 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004229 status = psa_key_agreement_internal( generator,
4230 slot,
4231 peer_key, peer_key_length,
4232 alg );
4233 if( status != PSA_SUCCESS )
4234 psa_generator_abort( generator );
4235 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004236}
4237
4238
4239
4240/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004241/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004242/****************************************************************/
4243
4244psa_status_t psa_generate_random( uint8_t *output,
4245 size_t output_size )
4246{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004247 int ret;
4248 GUARD_MODULE_INITIALIZED;
4249
4250 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004251 return( mbedtls_to_psa_error( ret ) );
4252}
4253
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004254#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004255
4256/* Support function for error conversion between psa_its error codes to psa crypto */
4257static psa_status_t its_to_psa_error( psa_its_status_t ret )
4258{
4259 switch( ret )
4260 {
4261 case PSA_ITS_SUCCESS:
4262 return( PSA_SUCCESS );
4263
4264 case PSA_ITS_ERROR_KEY_NOT_FOUND:
4265 return( PSA_ERROR_EMPTY_SLOT );
4266
4267 case PSA_ITS_ERROR_STORAGE_FAILURE:
4268 return( PSA_ERROR_STORAGE_FAILURE );
4269
4270 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4271 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4272
4273 case PSA_ITS_ERROR_INVALID_KEY:
4274 case PSA_PS_ERROR_OFFSET_INVALID:
4275 case PSA_ITS_ERROR_INCORRECT_SIZE:
4276 case PSA_ITS_ERROR_BAD_POINTER:
4277 return( PSA_ERROR_INVALID_ARGUMENT );
4278
4279 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4280 return( PSA_ERROR_NOT_SUPPORTED );
4281
4282 case PSA_ITS_ERROR_WRITE_ONCE:
4283 return( PSA_ERROR_OCCUPIED_SLOT );
4284
4285 default:
4286 return( PSA_ERROR_UNKNOWN_ERROR );
4287 }
4288}
4289
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004290psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4291 size_t seed_size )
4292{
4293 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004294 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004295 struct psa_its_info_t p_info;
4296 if( global_data.initialized )
4297 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004298
4299 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4300 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4301 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4302 return( PSA_ERROR_INVALID_ARGUMENT );
4303
avolinski0d2c2662018-11-21 17:31:07 +02004304 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004305 status = its_to_psa_error( its_status );
4306
4307 if( PSA_ITS_ERROR_KEY_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004308 {
avolinski0d2c2662018-11-21 17:31:07 +02004309 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004310 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004311 }
avolinski13beb102018-11-20 16:51:49 +02004312 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004313 {
4314 /* You should not be here. Seed needs to be injected only once */
4315 status = PSA_ERROR_NOT_PERMITTED;
4316 }
4317 return( status );
4318}
4319#endif
4320
Gilles Peskine05d69892018-06-19 22:00:52 +02004321psa_status_t psa_generate_key( psa_key_slot_t key,
4322 psa_key_type_t type,
4323 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004324 const void *extra,
4325 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004326{
Gilles Peskine12313cd2018-06-20 00:20:32 +02004327 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004328 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004329
Gilles Peskine53d991e2018-07-12 01:14:59 +02004330 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004331 return( PSA_ERROR_INVALID_ARGUMENT );
4332
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004333 status = psa_get_empty_key_slot( key, &slot );
4334 if( status != PSA_SUCCESS )
4335 return( status );
4336
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004337 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004338 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004339 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004340 if( status != PSA_SUCCESS )
4341 return( status );
4342 status = psa_generate_random( slot->data.raw.data,
4343 slot->data.raw.bytes );
4344 if( status != PSA_SUCCESS )
4345 {
4346 mbedtls_free( slot->data.raw.data );
4347 return( status );
4348 }
4349#if defined(MBEDTLS_DES_C)
4350 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004351 psa_des_set_key_parity( slot->data.raw.data,
4352 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004353#endif /* MBEDTLS_DES_C */
4354 }
4355 else
4356
4357#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4358 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4359 {
4360 mbedtls_rsa_context *rsa;
4361 int ret;
4362 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004363 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4364 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004365 /* Accept only byte-aligned keys, for the same reasons as
4366 * in psa_import_rsa_key(). */
4367 if( bits % 8 != 0 )
4368 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004369 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004370 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004371 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004372 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004373 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004374#if INT_MAX < 0xffffffff
4375 /* Check that the uint32_t value passed by the caller fits
4376 * in the range supported by this implementation. */
4377 if( p->e > INT_MAX )
4378 return( PSA_ERROR_NOT_SUPPORTED );
4379#endif
4380 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004381 }
4382 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4383 if( rsa == NULL )
4384 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4385 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4386 ret = mbedtls_rsa_gen_key( rsa,
4387 mbedtls_ctr_drbg_random,
4388 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004389 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004390 exponent );
4391 if( ret != 0 )
4392 {
4393 mbedtls_rsa_free( rsa );
4394 mbedtls_free( rsa );
4395 return( mbedtls_to_psa_error( ret ) );
4396 }
4397 slot->data.rsa = rsa;
4398 }
4399 else
4400#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4401
4402#if defined(MBEDTLS_ECP_C)
4403 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4404 {
4405 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4406 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4407 const mbedtls_ecp_curve_info *curve_info =
4408 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4409 mbedtls_ecp_keypair *ecp;
4410 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004411 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004412 return( PSA_ERROR_NOT_SUPPORTED );
4413 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4414 return( PSA_ERROR_NOT_SUPPORTED );
4415 if( curve_info->bit_size != bits )
4416 return( PSA_ERROR_INVALID_ARGUMENT );
4417 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4418 if( ecp == NULL )
4419 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4420 mbedtls_ecp_keypair_init( ecp );
4421 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4422 mbedtls_ctr_drbg_random,
4423 &global_data.ctr_drbg );
4424 if( ret != 0 )
4425 {
4426 mbedtls_ecp_keypair_free( ecp );
4427 mbedtls_free( ecp );
4428 return( mbedtls_to_psa_error( ret ) );
4429 }
4430 slot->data.ecp = ecp;
4431 }
4432 else
4433#endif /* MBEDTLS_ECP_C */
4434
4435 return( PSA_ERROR_NOT_SUPPORTED );
4436
4437 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004438
4439#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4440 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4441 {
4442 return( psa_save_generated_persistent_key( key, slot, bits ) );
4443 }
4444#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4445
4446 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004447}
4448
4449
4450/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004451/* Module setup */
4452/****************************************************************/
4453
Gilles Peskine5e769522018-11-20 21:59:56 +01004454psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4455 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4456 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4457{
4458 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4459 return( PSA_ERROR_BAD_STATE );
4460 global_data.entropy_init = entropy_init;
4461 global_data.entropy_free = entropy_free;
4462 return( PSA_SUCCESS );
4463}
4464
Gilles Peskinee59236f2018-01-27 23:32:46 +01004465void mbedtls_psa_crypto_free( void )
4466{
Jaeden Amero045bd502018-06-26 14:00:08 +01004467 psa_key_slot_t key;
Darryl Green40225ba2018-11-15 14:48:15 +00004468 key_slot_t *slot;
4469 psa_status_t status;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004470 if( global_data.key_slots_initialized )
Darryl Green40225ba2018-11-15 14:48:15 +00004471 {
Gilles Peskinec6b69072018-11-20 21:42:52 +01004472 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
4473 {
4474 status = psa_get_key_slot( key, &slot );
4475 if( status != PSA_SUCCESS )
4476 continue;
4477 psa_remove_key_data_from_memory( slot );
4478 /* Zeroize the slot to wipe metadata such as policies. */
4479 mbedtls_zeroize( slot, sizeof( *slot ) );
4480 }
Darryl Green40225ba2018-11-15 14:48:15 +00004481 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01004482 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4483 {
4484 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004485 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004486 }
4487 /* Wipe all remaining data, including configuration.
4488 * In particular, this sets all state indicator to the value
4489 * indicating "uninitialized". */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004490 mbedtls_zeroize( &global_data, sizeof( global_data ) );
4491}
4492
4493psa_status_t psa_crypto_init( void )
4494{
4495 int ret;
4496 const unsigned char drbg_seed[] = "PSA";
4497
Gilles Peskinec6b69072018-11-20 21:42:52 +01004498 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004499 if( global_data.initialized != 0 )
4500 return( PSA_SUCCESS );
4501
Gilles Peskine5e769522018-11-20 21:59:56 +01004502 /* Set default configuration if
4503 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4504 if( global_data.entropy_init == NULL )
4505 global_data.entropy_init = mbedtls_entropy_init;
4506 if( global_data.entropy_free == NULL )
4507 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004508
Gilles Peskinec6b69072018-11-20 21:42:52 +01004509 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004510 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004511 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004512 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004513 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4514 mbedtls_entropy_func,
4515 &global_data.entropy,
4516 drbg_seed, sizeof( drbg_seed ) - 1 );
4517 if( ret != 0 )
4518 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004519 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004520
Gilles Peskinec6b69072018-11-20 21:42:52 +01004521 /* Initialize the key slots. Zero-initialization has made all key
4522 * slots empty, so there is nothing to do. In a future version we will
4523 * load data from storage. */
4524 global_data.key_slots_initialized = 1;
4525
4526 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004527 global_data.initialized = 1;
4528
Gilles Peskinee59236f2018-01-27 23:32:46 +01004529exit:
4530 if( ret != 0 )
4531 mbedtls_psa_crypto_free( );
4532 return( mbedtls_to_psa_error( ret ) );
4533}
4534
4535#endif /* MBEDTLS_PSA_CRYPTO_C */