blob: f4c87d3e2f1ec2f4c96e2fa9e066aeb01e92c85b [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 {
1884 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001885 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001886
Gilles Peskined911eb72018-08-14 15:18:45 +02001887 if( truncated == 0 )
1888 {
1889 /* The "normal" case: untruncated algorithm. Nothing to do. */
1890 }
1891 else if( truncated < 4 )
1892 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001893 /* A very short MAC is too short for security since it can be
1894 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1895 * so we make this our minimum, even though 32 bits is still
1896 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001897 status = PSA_ERROR_NOT_SUPPORTED;
1898 }
1899 else if( truncated > operation->mac_size )
1900 {
1901 /* It's impossible to "truncate" to a larger length. */
1902 status = PSA_ERROR_INVALID_ARGUMENT;
1903 }
1904 else
1905 operation->mac_size = truncated;
1906
Gilles Peskinefbfac682018-07-08 20:51:54 +02001907exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001908 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001909 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001910 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001911 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001912 else
1913 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001914 operation->key_set = 1;
1915 }
1916 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001917}
1918
Gilles Peskine89167cb2018-07-08 20:12:23 +02001919psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1920 psa_key_slot_t key,
1921 psa_algorithm_t alg )
1922{
1923 return( psa_mac_setup( operation, key, alg, 1 ) );
1924}
1925
1926psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1927 psa_key_slot_t key,
1928 psa_algorithm_t alg )
1929{
1930 return( psa_mac_setup( operation, key, alg, 0 ) );
1931}
1932
Gilles Peskine8c9def32018-02-08 10:02:12 +01001933psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1934 const uint8_t *input,
1935 size_t input_length )
1936{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001937 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001938 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001939 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001940 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001941 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001942 operation->has_input = 1;
1943
Gilles Peskine8c9def32018-02-08 10:02:12 +01001944#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001945 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001946 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001947 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1948 input, input_length );
1949 status = mbedtls_to_psa_error( ret );
1950 }
1951 else
1952#endif /* MBEDTLS_CMAC_C */
1953#if defined(MBEDTLS_MD_C)
1954 if( PSA_ALG_IS_HMAC( operation->alg ) )
1955 {
1956 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1957 input_length );
1958 }
1959 else
1960#endif /* MBEDTLS_MD_C */
1961 {
1962 /* This shouldn't happen if `operation` was initialized by
1963 * a setup function. */
1964 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001965 }
1966
Gilles Peskinefbfac682018-07-08 20:51:54 +02001967cleanup:
1968 if( status != PSA_SUCCESS )
1969 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001970 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001971}
1972
Gilles Peskine01126fa2018-07-12 17:04:55 +02001973#if defined(MBEDTLS_MD_C)
1974static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1975 uint8_t *mac,
1976 size_t mac_size )
1977{
1978 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1979 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1980 size_t hash_size = 0;
1981 size_t block_size = psa_get_hash_block_size( hash_alg );
1982 psa_status_t status;
1983
Gilles Peskine01126fa2018-07-12 17:04:55 +02001984 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1985 if( status != PSA_SUCCESS )
1986 return( status );
1987 /* From here on, tmp needs to be wiped. */
1988
1989 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1990 if( status != PSA_SUCCESS )
1991 goto exit;
1992
1993 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1994 if( status != PSA_SUCCESS )
1995 goto exit;
1996
1997 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1998 if( status != PSA_SUCCESS )
1999 goto exit;
2000
Gilles Peskined911eb72018-08-14 15:18:45 +02002001 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2002 if( status != PSA_SUCCESS )
2003 goto exit;
2004
2005 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002006
2007exit:
2008 mbedtls_zeroize( tmp, hash_size );
2009 return( status );
2010}
2011#endif /* MBEDTLS_MD_C */
2012
mohammad16036df908f2018-04-02 08:34:15 -07002013static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002014 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002015 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002016{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002017 if( ! operation->key_set )
2018 return( PSA_ERROR_BAD_STATE );
2019 if( operation->iv_required && ! operation->iv_set )
2020 return( PSA_ERROR_BAD_STATE );
2021
Gilles Peskine8c9def32018-02-08 10:02:12 +01002022 if( mac_size < operation->mac_size )
2023 return( PSA_ERROR_BUFFER_TOO_SMALL );
2024
Gilles Peskine8c9def32018-02-08 10:02:12 +01002025#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002026 if( operation->alg == PSA_ALG_CMAC )
2027 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002028 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2029 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2030 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002031 memcpy( mac, tmp, operation->mac_size );
Gilles Peskined911eb72018-08-14 15:18:45 +02002032 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002033 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002034 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002035 else
2036#endif /* MBEDTLS_CMAC_C */
2037#if defined(MBEDTLS_MD_C)
2038 if( PSA_ALG_IS_HMAC( operation->alg ) )
2039 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002040 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002041 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002042 }
2043 else
2044#endif /* MBEDTLS_MD_C */
2045 {
2046 /* This shouldn't happen if `operation` was initialized by
2047 * a setup function. */
2048 return( PSA_ERROR_BAD_STATE );
2049 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002050}
2051
Gilles Peskineacd4be32018-07-08 19:56:25 +02002052psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2053 uint8_t *mac,
2054 size_t mac_size,
2055 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002056{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002057 psa_status_t status;
2058
2059 /* Fill the output buffer with something that isn't a valid mac
2060 * (barring an attack on the mac and deliberately-crafted input),
2061 * in case the caller doesn't check the return status properly. */
2062 *mac_length = mac_size;
2063 /* If mac_size is 0 then mac may be NULL and then the
2064 * call to memset would have undefined behavior. */
2065 if( mac_size != 0 )
2066 memset( mac, '!', mac_size );
2067
Gilles Peskine89167cb2018-07-08 20:12:23 +02002068 if( ! operation->is_sign )
2069 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002070 status = PSA_ERROR_BAD_STATE;
2071 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002072 }
mohammad16036df908f2018-04-02 08:34:15 -07002073
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002074 status = psa_mac_finish_internal( operation, mac, mac_size );
2075
2076cleanup:
2077 if( status == PSA_SUCCESS )
2078 {
2079 status = psa_mac_abort( operation );
2080 if( status == PSA_SUCCESS )
2081 *mac_length = operation->mac_size;
2082 else
2083 memset( mac, '!', mac_size );
2084 }
2085 else
2086 psa_mac_abort( operation );
2087 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002088}
2089
Gilles Peskineacd4be32018-07-08 19:56:25 +02002090psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2091 const uint8_t *mac,
2092 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002093{
Gilles Peskine828ed142018-06-18 23:25:51 +02002094 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002095 psa_status_t status;
2096
Gilles Peskine89167cb2018-07-08 20:12:23 +02002097 if( operation->is_sign )
2098 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002099 status = PSA_ERROR_BAD_STATE;
2100 goto cleanup;
2101 }
2102 if( operation->mac_size != mac_length )
2103 {
2104 status = PSA_ERROR_INVALID_SIGNATURE;
2105 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002106 }
mohammad16036df908f2018-04-02 08:34:15 -07002107
2108 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002109 actual_mac, sizeof( actual_mac ) );
2110
2111 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2112 status = PSA_ERROR_INVALID_SIGNATURE;
2113
2114cleanup:
2115 if( status == PSA_SUCCESS )
2116 status = psa_mac_abort( operation );
2117 else
2118 psa_mac_abort( operation );
2119
Gilles Peskine99b7d6b2018-08-21 14:56:19 +02002120 mbedtls_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002121
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002122 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002123}
2124
2125
Gilles Peskine20035e32018-02-03 22:44:14 +01002126
Gilles Peskine20035e32018-02-03 22:44:14 +01002127/****************************************************************/
2128/* Asymmetric cryptography */
2129/****************************************************************/
2130
Gilles Peskine2b450e32018-06-27 15:42:46 +02002131#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002132/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002133 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002134static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2135 size_t hash_length,
2136 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002137{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002138 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002139 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002140 *md_alg = mbedtls_md_get_type( md_info );
2141
2142 /* The Mbed TLS RSA module uses an unsigned int for hash length
2143 * parameters. Validate that it fits so that we don't risk an
2144 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002145#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002146 if( hash_length > UINT_MAX )
2147 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002148#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002149
2150#if defined(MBEDTLS_PKCS1_V15)
2151 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2152 * must be correct. */
2153 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2154 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002155 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002156 if( md_info == NULL )
2157 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002158 if( mbedtls_md_get_size( md_info ) != hash_length )
2159 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002160 }
2161#endif /* MBEDTLS_PKCS1_V15 */
2162
2163#if defined(MBEDTLS_PKCS1_V21)
2164 /* PSS requires a hash internally. */
2165 if( PSA_ALG_IS_RSA_PSS( alg ) )
2166 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002167 if( md_info == NULL )
2168 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002169 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002170#endif /* MBEDTLS_PKCS1_V21 */
2171
Gilles Peskine61b91d42018-06-08 16:09:36 +02002172 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002173}
2174
Gilles Peskine2b450e32018-06-27 15:42:46 +02002175static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2176 psa_algorithm_t alg,
2177 const uint8_t *hash,
2178 size_t hash_length,
2179 uint8_t *signature,
2180 size_t signature_size,
2181 size_t *signature_length )
2182{
2183 psa_status_t status;
2184 int ret;
2185 mbedtls_md_type_t md_alg;
2186
2187 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2188 if( status != PSA_SUCCESS )
2189 return( status );
2190
Gilles Peskine630a18a2018-06-29 17:49:35 +02002191 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002192 return( PSA_ERROR_BUFFER_TOO_SMALL );
2193
2194#if defined(MBEDTLS_PKCS1_V15)
2195 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2196 {
2197 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2198 MBEDTLS_MD_NONE );
2199 ret = mbedtls_rsa_pkcs1_sign( rsa,
2200 mbedtls_ctr_drbg_random,
2201 &global_data.ctr_drbg,
2202 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002203 md_alg,
2204 (unsigned int) hash_length,
2205 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002206 signature );
2207 }
2208 else
2209#endif /* MBEDTLS_PKCS1_V15 */
2210#if defined(MBEDTLS_PKCS1_V21)
2211 if( PSA_ALG_IS_RSA_PSS( alg ) )
2212 {
2213 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2214 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2215 mbedtls_ctr_drbg_random,
2216 &global_data.ctr_drbg,
2217 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002218 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002219 (unsigned int) hash_length,
2220 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002221 signature );
2222 }
2223 else
2224#endif /* MBEDTLS_PKCS1_V21 */
2225 {
2226 return( PSA_ERROR_INVALID_ARGUMENT );
2227 }
2228
2229 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002230 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002231 return( mbedtls_to_psa_error( ret ) );
2232}
2233
2234static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2235 psa_algorithm_t alg,
2236 const uint8_t *hash,
2237 size_t hash_length,
2238 const uint8_t *signature,
2239 size_t signature_length )
2240{
2241 psa_status_t status;
2242 int ret;
2243 mbedtls_md_type_t md_alg;
2244
2245 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2246 if( status != PSA_SUCCESS )
2247 return( status );
2248
Gilles Peskine630a18a2018-06-29 17:49:35 +02002249 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002250 return( PSA_ERROR_BUFFER_TOO_SMALL );
2251
2252#if defined(MBEDTLS_PKCS1_V15)
2253 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2254 {
2255 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2256 MBEDTLS_MD_NONE );
2257 ret = mbedtls_rsa_pkcs1_verify( rsa,
2258 mbedtls_ctr_drbg_random,
2259 &global_data.ctr_drbg,
2260 MBEDTLS_RSA_PUBLIC,
2261 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002262 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002263 hash,
2264 signature );
2265 }
2266 else
2267#endif /* MBEDTLS_PKCS1_V15 */
2268#if defined(MBEDTLS_PKCS1_V21)
2269 if( PSA_ALG_IS_RSA_PSS( alg ) )
2270 {
2271 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2272 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2273 mbedtls_ctr_drbg_random,
2274 &global_data.ctr_drbg,
2275 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002276 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002277 (unsigned int) hash_length,
2278 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002279 signature );
2280 }
2281 else
2282#endif /* MBEDTLS_PKCS1_V21 */
2283 {
2284 return( PSA_ERROR_INVALID_ARGUMENT );
2285 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002286
2287 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2288 * the rest of the signature is invalid". This has little use in
2289 * practice and PSA doesn't report this distinction. */
2290 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2291 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002292 return( mbedtls_to_psa_error( ret ) );
2293}
2294#endif /* MBEDTLS_RSA_C */
2295
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002296#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002297/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2298 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2299 * (even though these functions don't modify it). */
2300static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2301 psa_algorithm_t alg,
2302 const uint8_t *hash,
2303 size_t hash_length,
2304 uint8_t *signature,
2305 size_t signature_size,
2306 size_t *signature_length )
2307{
2308 int ret;
2309 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002310 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002311 mbedtls_mpi_init( &r );
2312 mbedtls_mpi_init( &s );
2313
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002314 if( signature_size < 2 * curve_bytes )
2315 {
2316 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2317 goto cleanup;
2318 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002319
Gilles Peskinea05219c2018-11-16 16:02:56 +01002320#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002321 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2322 {
2323 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2324 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2325 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2326 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2327 hash, hash_length,
2328 md_alg ) );
2329 }
2330 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002331#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002332 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002333 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002334 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2335 hash, hash_length,
2336 mbedtls_ctr_drbg_random,
2337 &global_data.ctr_drbg ) );
2338 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002339
2340 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2341 signature,
2342 curve_bytes ) );
2343 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2344 signature + curve_bytes,
2345 curve_bytes ) );
2346
2347cleanup:
2348 mbedtls_mpi_free( &r );
2349 mbedtls_mpi_free( &s );
2350 if( ret == 0 )
2351 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002352 return( mbedtls_to_psa_error( ret ) );
2353}
2354
2355static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2356 const uint8_t *hash,
2357 size_t hash_length,
2358 const uint8_t *signature,
2359 size_t signature_length )
2360{
2361 int ret;
2362 mbedtls_mpi r, s;
2363 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2364 mbedtls_mpi_init( &r );
2365 mbedtls_mpi_init( &s );
2366
2367 if( signature_length != 2 * curve_bytes )
2368 return( PSA_ERROR_INVALID_SIGNATURE );
2369
2370 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2371 signature,
2372 curve_bytes ) );
2373 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2374 signature + curve_bytes,
2375 curve_bytes ) );
2376
2377 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2378 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002379
2380cleanup:
2381 mbedtls_mpi_free( &r );
2382 mbedtls_mpi_free( &s );
2383 return( mbedtls_to_psa_error( ret ) );
2384}
2385#endif /* MBEDTLS_ECDSA_C */
2386
Gilles Peskine61b91d42018-06-08 16:09:36 +02002387psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2388 psa_algorithm_t alg,
2389 const uint8_t *hash,
2390 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002391 uint8_t *signature,
2392 size_t signature_size,
2393 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002394{
2395 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002396 psa_status_t status;
2397
2398 *signature_length = signature_size;
2399
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002400 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2401 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002402 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002403 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002404 {
2405 status = PSA_ERROR_INVALID_ARGUMENT;
2406 goto exit;
2407 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002408
Gilles Peskine20035e32018-02-03 22:44:14 +01002409#if defined(MBEDTLS_RSA_C)
2410 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2411 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002412 status = psa_rsa_sign( slot->data.rsa,
2413 alg,
2414 hash, hash_length,
2415 signature, signature_size,
2416 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002417 }
2418 else
2419#endif /* defined(MBEDTLS_RSA_C) */
2420#if defined(MBEDTLS_ECP_C)
2421 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2422 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002423#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002424 if(
2425#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2426 PSA_ALG_IS_ECDSA( alg )
2427#else
2428 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2429#endif
2430 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002431 status = psa_ecdsa_sign( slot->data.ecp,
2432 alg,
2433 hash, hash_length,
2434 signature, signature_size,
2435 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002436 else
2437#endif /* defined(MBEDTLS_ECDSA_C) */
2438 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002439 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002440 }
itayzafrir5c753392018-05-08 11:18:38 +03002441 }
2442 else
2443#endif /* defined(MBEDTLS_ECP_C) */
2444 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002445 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002446 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002447
2448exit:
2449 /* Fill the unused part of the output buffer (the whole buffer on error,
2450 * the trailing part on success) with something that isn't a valid mac
2451 * (barring an attack on the mac and deliberately-crafted input),
2452 * in case the caller doesn't check the return status properly. */
2453 if( status == PSA_SUCCESS )
2454 memset( signature + *signature_length, '!',
2455 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002456 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002457 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002458 /* If signature_size is 0 then we have nothing to do. We must not call
2459 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002460 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002461}
2462
2463psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2464 psa_algorithm_t alg,
2465 const uint8_t *hash,
2466 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002467 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002468 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002469{
2470 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002471 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002472
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002473 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2474 if( status != PSA_SUCCESS )
2475 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002476
Gilles Peskine61b91d42018-06-08 16:09:36 +02002477#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002478 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002479 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002480 return( psa_rsa_verify( slot->data.rsa,
2481 alg,
2482 hash, hash_length,
2483 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002484 }
2485 else
2486#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002487#if defined(MBEDTLS_ECP_C)
2488 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2489 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002490#if defined(MBEDTLS_ECDSA_C)
2491 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002492 return( psa_ecdsa_verify( slot->data.ecp,
2493 hash, hash_length,
2494 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002495 else
2496#endif /* defined(MBEDTLS_ECDSA_C) */
2497 {
2498 return( PSA_ERROR_INVALID_ARGUMENT );
2499 }
itayzafrir5c753392018-05-08 11:18:38 +03002500 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002501 else
2502#endif /* defined(MBEDTLS_ECP_C) */
2503 {
2504 return( PSA_ERROR_NOT_SUPPORTED );
2505 }
2506}
2507
Gilles Peskine072ac562018-06-30 00:21:29 +02002508#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2509static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2510 mbedtls_rsa_context *rsa )
2511{
2512 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2513 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2514 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2515 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2516}
2517#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2518
Gilles Peskine61b91d42018-06-08 16:09:36 +02002519psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2520 psa_algorithm_t alg,
2521 const uint8_t *input,
2522 size_t input_length,
2523 const uint8_t *salt,
2524 size_t salt_length,
2525 uint8_t *output,
2526 size_t output_size,
2527 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002528{
2529 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002530 psa_status_t status;
2531
Darryl Green5cc689a2018-07-24 15:34:10 +01002532 (void) input;
2533 (void) input_length;
2534 (void) salt;
2535 (void) output;
2536 (void) output_size;
2537
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002538 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002539
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002540 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2541 return( PSA_ERROR_INVALID_ARGUMENT );
2542
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002543 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2544 if( status != PSA_SUCCESS )
2545 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002546 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2547 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002548 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002549
2550#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002551 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002552 {
2553 mbedtls_rsa_context *rsa = slot->data.rsa;
2554 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002555 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002556 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002557#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002558 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002559 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002560 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2561 mbedtls_ctr_drbg_random,
2562 &global_data.ctr_drbg,
2563 MBEDTLS_RSA_PUBLIC,
2564 input_length,
2565 input,
2566 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002567 }
2568 else
2569#endif /* MBEDTLS_PKCS1_V15 */
2570#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002571 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002572 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002573 psa_rsa_oaep_set_padding_mode( alg, rsa );
2574 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2575 mbedtls_ctr_drbg_random,
2576 &global_data.ctr_drbg,
2577 MBEDTLS_RSA_PUBLIC,
2578 salt, salt_length,
2579 input_length,
2580 input,
2581 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002582 }
2583 else
2584#endif /* MBEDTLS_PKCS1_V21 */
2585 {
2586 return( PSA_ERROR_INVALID_ARGUMENT );
2587 }
2588 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002589 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002590 return( mbedtls_to_psa_error( ret ) );
2591 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002592 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002593#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002594 {
2595 return( PSA_ERROR_NOT_SUPPORTED );
2596 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002597}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002598
Gilles Peskine61b91d42018-06-08 16:09:36 +02002599psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2600 psa_algorithm_t alg,
2601 const uint8_t *input,
2602 size_t input_length,
2603 const uint8_t *salt,
2604 size_t salt_length,
2605 uint8_t *output,
2606 size_t output_size,
2607 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002608{
2609 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002610 psa_status_t status;
2611
Darryl Green5cc689a2018-07-24 15:34:10 +01002612 (void) input;
2613 (void) input_length;
2614 (void) salt;
2615 (void) output;
2616 (void) output_size;
2617
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002618 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002619
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002620 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2621 return( PSA_ERROR_INVALID_ARGUMENT );
2622
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002623 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2624 if( status != PSA_SUCCESS )
2625 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002626 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2627 return( PSA_ERROR_INVALID_ARGUMENT );
2628
2629#if defined(MBEDTLS_RSA_C)
2630 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2631 {
2632 mbedtls_rsa_context *rsa = slot->data.rsa;
2633 int ret;
2634
Gilles Peskine630a18a2018-06-29 17:49:35 +02002635 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002636 return( PSA_ERROR_INVALID_ARGUMENT );
2637
2638#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002639 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002640 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002641 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2642 mbedtls_ctr_drbg_random,
2643 &global_data.ctr_drbg,
2644 MBEDTLS_RSA_PRIVATE,
2645 output_length,
2646 input,
2647 output,
2648 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002649 }
2650 else
2651#endif /* MBEDTLS_PKCS1_V15 */
2652#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002653 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002654 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002655 psa_rsa_oaep_set_padding_mode( alg, rsa );
2656 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2657 mbedtls_ctr_drbg_random,
2658 &global_data.ctr_drbg,
2659 MBEDTLS_RSA_PRIVATE,
2660 salt, salt_length,
2661 output_length,
2662 input,
2663 output,
2664 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002665 }
2666 else
2667#endif /* MBEDTLS_PKCS1_V21 */
2668 {
2669 return( PSA_ERROR_INVALID_ARGUMENT );
2670 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002671
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002672 return( mbedtls_to_psa_error( ret ) );
2673 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002674 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002675#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002676 {
2677 return( PSA_ERROR_NOT_SUPPORTED );
2678 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002679}
Gilles Peskine20035e32018-02-03 22:44:14 +01002680
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002681
2682
mohammad1603503973b2018-03-12 15:59:30 +02002683/****************************************************************/
2684/* Symmetric cryptography */
2685/****************************************************************/
2686
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002687/* Initialize the cipher operation structure. Once this function has been
2688 * called, psa_cipher_abort can run and will do the right thing. */
2689static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2690 psa_algorithm_t alg )
2691{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002692 if( ! PSA_ALG_IS_CIPHER( alg ) )
2693 {
2694 memset( operation, 0, sizeof( *operation ) );
2695 return( PSA_ERROR_INVALID_ARGUMENT );
2696 }
2697
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002698 operation->alg = alg;
2699 operation->key_set = 0;
2700 operation->iv_set = 0;
2701 operation->iv_required = 1;
2702 operation->iv_size = 0;
2703 operation->block_size = 0;
2704 mbedtls_cipher_init( &operation->ctx.cipher );
2705 return( PSA_SUCCESS );
2706}
2707
Gilles Peskinee553c652018-06-04 16:22:46 +02002708static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2709 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002710 psa_algorithm_t alg,
2711 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002712{
2713 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2714 psa_status_t status;
2715 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002716 size_t key_bits;
2717 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002718 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2719 PSA_KEY_USAGE_ENCRYPT :
2720 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002721
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002722 status = psa_cipher_init( operation, alg );
2723 if( status != PSA_SUCCESS )
2724 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002725
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002726 status = psa_get_key_from_slot( key, &slot, usage, alg);
2727 if( status != PSA_SUCCESS )
2728 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002729 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002730
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002731 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002732 if( cipher_info == NULL )
2733 return( PSA_ERROR_NOT_SUPPORTED );
2734
mohammad1603503973b2018-03-12 15:59:30 +02002735 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002736 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002737 {
2738 psa_cipher_abort( operation );
2739 return( mbedtls_to_psa_error( ret ) );
2740 }
2741
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002742#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002743 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002744 {
2745 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2746 unsigned char keys[24];
2747 memcpy( keys, slot->data.raw.data, 16 );
2748 memcpy( keys + 16, slot->data.raw.data, 8 );
2749 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2750 keys,
2751 192, cipher_operation );
2752 }
2753 else
2754#endif
2755 {
2756 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2757 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002758 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002759 }
Moran Peker41deec42018-04-04 15:43:05 +03002760 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002761 {
2762 psa_cipher_abort( operation );
2763 return( mbedtls_to_psa_error( ret ) );
2764 }
2765
mohammad16038481e742018-03-18 13:57:31 +02002766#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002767 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002768 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002769 case PSA_ALG_CBC_NO_PADDING:
2770 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2771 MBEDTLS_PADDING_NONE );
2772 break;
2773 case PSA_ALG_CBC_PKCS7:
2774 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2775 MBEDTLS_PADDING_PKCS7 );
2776 break;
2777 default:
2778 /* The algorithm doesn't involve padding. */
2779 ret = 0;
2780 break;
2781 }
2782 if( ret != 0 )
2783 {
2784 psa_cipher_abort( operation );
2785 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002786 }
2787#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2788
mohammad1603503973b2018-03-12 15:59:30 +02002789 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002790 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2791 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2792 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002793 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002794 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002795 }
mohammad1603503973b2018-03-12 15:59:30 +02002796
Moran Peker395db872018-05-31 14:07:14 +03002797 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002798}
2799
Gilles Peskinefe119512018-07-08 21:39:34 +02002800psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2801 psa_key_slot_t key,
2802 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002803{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002804 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002805}
2806
Gilles Peskinefe119512018-07-08 21:39:34 +02002807psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2808 psa_key_slot_t key,
2809 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002810{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002811 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002812}
2813
Gilles Peskinefe119512018-07-08 21:39:34 +02002814psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2815 unsigned char *iv,
2816 size_t iv_size,
2817 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002818{
itayzafrir534bd7c2018-08-02 13:56:32 +03002819 psa_status_t status;
2820 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002821 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002822 {
2823 status = PSA_ERROR_BAD_STATE;
2824 goto exit;
2825 }
Moran Peker41deec42018-04-04 15:43:05 +03002826 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002827 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002828 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002829 goto exit;
2830 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002831 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2832 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002833 if( ret != 0 )
2834 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002835 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002836 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002837 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002838
mohammad16038481e742018-03-18 13:57:31 +02002839 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002840 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002841
Moran Peker395db872018-05-31 14:07:14 +03002842exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002843 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002844 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002845 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002846}
2847
Gilles Peskinefe119512018-07-08 21:39:34 +02002848psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2849 const unsigned char *iv,
2850 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002851{
itayzafrir534bd7c2018-08-02 13:56:32 +03002852 psa_status_t status;
2853 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002854 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002855 {
2856 status = PSA_ERROR_BAD_STATE;
2857 goto exit;
2858 }
Moran Pekera28258c2018-05-29 16:25:04 +03002859 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002860 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002861 status = PSA_ERROR_INVALID_ARGUMENT;
2862 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002863 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002864 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2865 status = mbedtls_to_psa_error( ret );
2866exit:
2867 if( status == PSA_SUCCESS )
2868 operation->iv_set = 1;
2869 else
mohammad1603503973b2018-03-12 15:59:30 +02002870 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002871 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002872}
2873
Gilles Peskinee553c652018-06-04 16:22:46 +02002874psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2875 const uint8_t *input,
2876 size_t input_length,
2877 unsigned char *output,
2878 size_t output_size,
2879 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002880{
itayzafrir534bd7c2018-08-02 13:56:32 +03002881 psa_status_t status;
2882 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002883 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002884 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002885 {
2886 /* Take the unprocessed partial block left over from previous
2887 * update calls, if any, plus the input to this call. Remove
2888 * the last partial block, if any. You get the data that will be
2889 * output in this call. */
2890 expected_output_size =
2891 ( operation->ctx.cipher.unprocessed_len + input_length )
2892 / operation->block_size * operation->block_size;
2893 }
2894 else
2895 {
2896 expected_output_size = input_length;
2897 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002898
Gilles Peskine89d789c2018-06-04 17:17:16 +02002899 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002900 {
2901 status = PSA_ERROR_BUFFER_TOO_SMALL;
2902 goto exit;
2903 }
mohammad160382759612018-03-12 18:16:40 +02002904
mohammad1603503973b2018-03-12 15:59:30 +02002905 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002906 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002907 status = mbedtls_to_psa_error( ret );
2908exit:
2909 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002910 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002911 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002912}
2913
Gilles Peskinee553c652018-06-04 16:22:46 +02002914psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2915 uint8_t *output,
2916 size_t output_size,
2917 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002918{
Janos Follath315b51c2018-07-09 16:04:51 +01002919 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2920 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002921 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002922
mohammad1603503973b2018-03-12 15:59:30 +02002923 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002924 {
Janos Follath315b51c2018-07-09 16:04:51 +01002925 status = PSA_ERROR_BAD_STATE;
2926 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002927 }
2928 if( operation->iv_required && ! operation->iv_set )
2929 {
Janos Follath315b51c2018-07-09 16:04:51 +01002930 status = PSA_ERROR_BAD_STATE;
2931 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002932 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002933
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002934 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002935 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2936 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002937 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002938 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002939 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002940 }
2941
Janos Follath315b51c2018-07-09 16:04:51 +01002942 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2943 temp_output_buffer,
2944 output_length );
2945 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002946 {
Janos Follath315b51c2018-07-09 16:04:51 +01002947 status = mbedtls_to_psa_error( cipher_ret );
2948 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002949 }
Janos Follath315b51c2018-07-09 16:04:51 +01002950
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002951 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002952 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002953 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002954 memcpy( output, temp_output_buffer, *output_length );
2955 else
2956 {
Janos Follath315b51c2018-07-09 16:04:51 +01002957 status = PSA_ERROR_BUFFER_TOO_SMALL;
2958 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002959 }
mohammad1603503973b2018-03-12 15:59:30 +02002960
Janos Follath279ab8e2018-07-09 16:13:21 +01002961 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002962 status = psa_cipher_abort( operation );
2963
2964 return( status );
2965
2966error:
2967
2968 *output_length = 0;
2969
Janos Follath279ab8e2018-07-09 16:13:21 +01002970 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002971 (void) psa_cipher_abort( operation );
2972
2973 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002974}
2975
Gilles Peskinee553c652018-06-04 16:22:46 +02002976psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2977{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002978 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002979 {
2980 /* The object has (apparently) been initialized but it is not
2981 * in use. It's ok to call abort on such an object, and there's
2982 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002983 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002984 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002985
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002986 /* Sanity check (shouldn't happen: operation->alg should
2987 * always have been initialized to a valid value). */
2988 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2989 return( PSA_ERROR_BAD_STATE );
2990
mohammad1603503973b2018-03-12 15:59:30 +02002991 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002992
Moran Peker41deec42018-04-04 15:43:05 +03002993 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002994 operation->key_set = 0;
2995 operation->iv_set = 0;
2996 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002997 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002998 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002999
Moran Peker395db872018-05-31 14:07:14 +03003000 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003001}
3002
Gilles Peskinea0655c32018-04-30 17:06:50 +02003003
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003004
mohammad16038cc1cee2018-03-28 01:21:33 +03003005/****************************************************************/
3006/* Key Policy */
3007/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003008
mohammad160327010052018-07-03 13:16:15 +03003009#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003010void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003011{
Gilles Peskine803ce742018-06-18 16:07:14 +02003012 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003013}
3014
Gilles Peskine2d277862018-06-18 15:41:12 +02003015void psa_key_policy_set_usage( psa_key_policy_t *policy,
3016 psa_key_usage_t usage,
3017 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003018{
mohammad16034eed7572018-03-28 05:14:59 -07003019 policy->usage = usage;
3020 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003021}
3022
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003023psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003024{
mohammad16036df908f2018-04-02 08:34:15 -07003025 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003026}
3027
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003028psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003029{
mohammad16036df908f2018-04-02 08:34:15 -07003030 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003031}
mohammad160327010052018-07-03 13:16:15 +03003032#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003033
Gilles Peskine2d277862018-06-18 15:41:12 +02003034psa_status_t psa_set_key_policy( psa_key_slot_t key,
3035 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003036{
3037 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003038 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003039
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003040 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003041 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003042
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003043 status = psa_get_empty_key_slot( key, &slot );
3044 if( status != PSA_SUCCESS )
3045 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003046
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003047 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3048 PSA_KEY_USAGE_ENCRYPT |
3049 PSA_KEY_USAGE_DECRYPT |
3050 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003051 PSA_KEY_USAGE_VERIFY |
3052 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003053 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003054
mohammad16036df908f2018-04-02 08:34:15 -07003055 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003056
3057 return( PSA_SUCCESS );
3058}
3059
Gilles Peskine2d277862018-06-18 15:41:12 +02003060psa_status_t psa_get_key_policy( psa_key_slot_t key,
3061 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003062{
3063 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003064 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003065
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003066 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003067 return( PSA_ERROR_INVALID_ARGUMENT );
3068
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003069 status = psa_get_key_slot( key, &slot );
3070 if( status != PSA_SUCCESS )
3071 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003072
mohammad16036df908f2018-04-02 08:34:15 -07003073 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003074
3075 return( PSA_SUCCESS );
3076}
Gilles Peskine20035e32018-02-03 22:44:14 +01003077
Gilles Peskinea0655c32018-04-30 17:06:50 +02003078
3079
mohammad1603804cd712018-03-20 22:44:08 +02003080/****************************************************************/
3081/* Key Lifetime */
3082/****************************************************************/
3083
Gilles Peskine2d277862018-06-18 15:41:12 +02003084psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
3085 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003086{
3087 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003088 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003089
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003090 status = psa_get_key_slot( key, &slot );
3091 if( status != PSA_SUCCESS )
3092 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003093
mohammad1603804cd712018-03-20 22:44:08 +02003094 *lifetime = slot->lifetime;
3095
3096 return( PSA_SUCCESS );
3097}
3098
Gilles Peskine2d277862018-06-18 15:41:12 +02003099psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01003100 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003101{
3102 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003103 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003104
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003105 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
3106 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
Darryl Greend49a4992018-06-18 17:27:26 +01003107 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003108 return( PSA_ERROR_INVALID_ARGUMENT );
3109
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003110 status = psa_get_empty_key_slot( key, &slot );
3111 if( status != PSA_SUCCESS )
3112 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02003113
Darryl Greend49a4992018-06-18 17:27:26 +01003114 if( lifetime == PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003115 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003116
Darryl Greend49a4992018-06-18 17:27:26 +01003117#if !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
3118 if( lifetime == PSA_KEY_LIFETIME_PERSISTENT )
3119 return( PSA_ERROR_NOT_SUPPORTED );
3120#endif
3121
mohammad1603060ad8a2018-03-20 14:28:38 -07003122 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02003123
3124 return( PSA_SUCCESS );
3125}
3126
Gilles Peskine20035e32018-02-03 22:44:14 +01003127
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003128
mohammad16035955c982018-04-26 00:53:03 +03003129/****************************************************************/
3130/* AEAD */
3131/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003132
Gilles Peskineedf9a652018-08-17 18:11:56 +02003133typedef struct
3134{
3135 key_slot_t *slot;
3136 const mbedtls_cipher_info_t *cipher_info;
3137 union
3138 {
3139#if defined(MBEDTLS_CCM_C)
3140 mbedtls_ccm_context ccm;
3141#endif /* MBEDTLS_CCM_C */
3142#if defined(MBEDTLS_GCM_C)
3143 mbedtls_gcm_context gcm;
3144#endif /* MBEDTLS_GCM_C */
3145 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003146 psa_algorithm_t core_alg;
3147 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003148 uint8_t tag_length;
3149} aead_operation_t;
3150
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003151static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003152{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003153 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003154 {
3155#if defined(MBEDTLS_CCM_C)
3156 case PSA_ALG_CCM:
3157 mbedtls_ccm_free( &operation->ctx.ccm );
3158 break;
3159#endif /* MBEDTLS_CCM_C */
3160#if defined(MBEDTLS_CCM_C)
3161 case PSA_ALG_GCM:
3162 mbedtls_gcm_free( &operation->ctx.gcm );
3163 break;
3164#endif /* MBEDTLS_GCM_C */
3165 }
3166}
3167
3168static psa_status_t psa_aead_setup( aead_operation_t *operation,
3169 psa_key_slot_t key,
3170 psa_key_usage_t usage,
3171 psa_algorithm_t alg )
3172{
3173 psa_status_t status;
3174 size_t key_bits;
3175 mbedtls_cipher_id_t cipher_id;
3176
3177 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
3178 if( status != PSA_SUCCESS )
3179 return( status );
3180
3181 key_bits = psa_get_key_bits( operation->slot );
3182
3183 operation->cipher_info =
3184 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3185 &cipher_id );
3186 if( operation->cipher_info == NULL )
3187 return( PSA_ERROR_NOT_SUPPORTED );
3188
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003189 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003190 {
3191#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003192 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3193 operation->core_alg = PSA_ALG_CCM;
3194 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003195 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3196 return( PSA_ERROR_INVALID_ARGUMENT );
3197 mbedtls_ccm_init( &operation->ctx.ccm );
3198 status = mbedtls_to_psa_error(
3199 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3200 operation->slot->data.raw.data,
3201 (unsigned int) key_bits ) );
3202 if( status != 0 )
3203 goto cleanup;
3204 break;
3205#endif /* MBEDTLS_CCM_C */
3206
3207#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003208 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3209 operation->core_alg = PSA_ALG_GCM;
3210 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003211 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3212 return( PSA_ERROR_INVALID_ARGUMENT );
3213 mbedtls_gcm_init( &operation->ctx.gcm );
3214 status = mbedtls_to_psa_error(
3215 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3216 operation->slot->data.raw.data,
3217 (unsigned int) key_bits ) );
3218 break;
3219#endif /* MBEDTLS_GCM_C */
3220
3221 default:
3222 return( PSA_ERROR_NOT_SUPPORTED );
3223 }
3224
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003225 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3226 {
3227 status = PSA_ERROR_INVALID_ARGUMENT;
3228 goto cleanup;
3229 }
3230 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3231 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3232 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3233 * In both cases, mbedtls_xxx will validate the tag length below. */
3234
Gilles Peskineedf9a652018-08-17 18:11:56 +02003235 return( PSA_SUCCESS );
3236
3237cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003238 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003239 return( status );
3240}
3241
mohammad16035955c982018-04-26 00:53:03 +03003242psa_status_t psa_aead_encrypt( psa_key_slot_t key,
3243 psa_algorithm_t alg,
3244 const uint8_t *nonce,
3245 size_t nonce_length,
3246 const uint8_t *additional_data,
3247 size_t additional_data_length,
3248 const uint8_t *plaintext,
3249 size_t plaintext_length,
3250 uint8_t *ciphertext,
3251 size_t ciphertext_size,
3252 size_t *ciphertext_length )
3253{
mohammad16035955c982018-04-26 00:53:03 +03003254 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003255 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003256 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003257
mohammad1603f08a5502018-06-03 15:05:47 +03003258 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003259
Gilles Peskineedf9a652018-08-17 18:11:56 +02003260 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003261 if( status != PSA_SUCCESS )
3262 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003263
Gilles Peskineedf9a652018-08-17 18:11:56 +02003264 /* For all currently supported modes, the tag is at the end of the
3265 * ciphertext. */
3266 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3267 {
3268 status = PSA_ERROR_BUFFER_TOO_SMALL;
3269 goto exit;
3270 }
3271 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003272
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003273 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003274 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003275 status = mbedtls_to_psa_error(
3276 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3277 MBEDTLS_GCM_ENCRYPT,
3278 plaintext_length,
3279 nonce, nonce_length,
3280 additional_data, additional_data_length,
3281 plaintext, ciphertext,
3282 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003283 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003284 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003285 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003286 status = mbedtls_to_psa_error(
3287 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3288 plaintext_length,
3289 nonce, nonce_length,
3290 additional_data,
3291 additional_data_length,
3292 plaintext, ciphertext,
3293 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003294 }
mohammad16035c8845f2018-05-09 05:40:09 -07003295 else
3296 {
mohammad1603554faad2018-06-03 15:07:38 +03003297 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003298 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003299
Gilles Peskineedf9a652018-08-17 18:11:56 +02003300 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3301 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003302
Gilles Peskineedf9a652018-08-17 18:11:56 +02003303exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003304 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003305 if( status == PSA_SUCCESS )
3306 *ciphertext_length = plaintext_length + operation.tag_length;
3307 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003308}
3309
Gilles Peskineee652a32018-06-01 19:23:52 +02003310/* Locate the tag in a ciphertext buffer containing the encrypted data
3311 * followed by the tag. Return the length of the part preceding the tag in
3312 * *plaintext_length. This is the size of the plaintext in modes where
3313 * the encrypted data has the same size as the plaintext, such as
3314 * CCM and GCM. */
3315static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3316 const uint8_t *ciphertext,
3317 size_t ciphertext_length,
3318 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003319 const uint8_t **p_tag )
3320{
3321 size_t payload_length;
3322 if( tag_length > ciphertext_length )
3323 return( PSA_ERROR_INVALID_ARGUMENT );
3324 payload_length = ciphertext_length - tag_length;
3325 if( payload_length > plaintext_size )
3326 return( PSA_ERROR_BUFFER_TOO_SMALL );
3327 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003328 return( PSA_SUCCESS );
3329}
3330
mohammad16035955c982018-04-26 00:53:03 +03003331psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3332 psa_algorithm_t alg,
3333 const uint8_t *nonce,
3334 size_t nonce_length,
3335 const uint8_t *additional_data,
3336 size_t additional_data_length,
3337 const uint8_t *ciphertext,
3338 size_t ciphertext_length,
3339 uint8_t *plaintext,
3340 size_t plaintext_size,
3341 size_t *plaintext_length )
3342{
mohammad16035955c982018-04-26 00:53:03 +03003343 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003344 aead_operation_t operation;
3345 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003346
Gilles Peskineee652a32018-06-01 19:23:52 +02003347 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003348
Gilles Peskineedf9a652018-08-17 18:11:56 +02003349 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003350 if( status != PSA_SUCCESS )
3351 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003352
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003353 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003354 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003355 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003356 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003357 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003358 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003359 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003360
Gilles Peskineedf9a652018-08-17 18:11:56 +02003361 status = mbedtls_to_psa_error(
3362 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3363 ciphertext_length - operation.tag_length,
3364 nonce, nonce_length,
3365 additional_data,
3366 additional_data_length,
3367 tag, operation.tag_length,
3368 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003369 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003370 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003371 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003372 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003373 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003374 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003375 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003376 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003377
Gilles Peskineedf9a652018-08-17 18:11:56 +02003378 status = mbedtls_to_psa_error(
3379 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3380 ciphertext_length - operation.tag_length,
3381 nonce, nonce_length,
3382 additional_data,
3383 additional_data_length,
3384 ciphertext, plaintext,
3385 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003386 }
mohammad160339574652018-06-01 04:39:53 -07003387 else
3388 {
mohammad1603554faad2018-06-03 15:07:38 +03003389 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003390 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003391
Gilles Peskineedf9a652018-08-17 18:11:56 +02003392 if( status != PSA_SUCCESS && plaintext_size != 0 )
3393 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003394
Gilles Peskineedf9a652018-08-17 18:11:56 +02003395exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003396 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003397 if( status == PSA_SUCCESS )
3398 *plaintext_length = ciphertext_length - operation.tag_length;
3399 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003400}
3401
Gilles Peskinea0655c32018-04-30 17:06:50 +02003402
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003403
Gilles Peskine20035e32018-02-03 22:44:14 +01003404/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003405/* Generators */
3406/****************************************************************/
3407
3408psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3409{
3410 psa_status_t status = PSA_SUCCESS;
3411 if( generator->alg == 0 )
3412 {
3413 /* The object has (apparently) been initialized but it is not
3414 * in use. It's ok to call abort on such an object, and there's
3415 * nothing to do. */
3416 }
3417 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003418 if( generator->alg == PSA_ALG_SELECT_RAW )
3419 {
3420 if( generator->ctx.buffer.data != NULL )
3421 {
3422 mbedtls_zeroize( generator->ctx.buffer.data,
3423 generator->ctx.buffer.size );
3424 mbedtls_free( generator->ctx.buffer.data );
3425 }
3426 }
3427 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003428#if defined(MBEDTLS_MD_C)
3429 if( PSA_ALG_IS_HKDF( generator->alg ) )
3430 {
3431 mbedtls_free( generator->ctx.hkdf.info );
3432 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3433 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003434 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3435 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3436 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003437 {
3438 if( generator->ctx.tls12_prf.key != NULL )
3439 {
3440 mbedtls_zeroize( generator->ctx.tls12_prf.key,
3441 generator->ctx.tls12_prf.key_len );
3442 mbedtls_free( generator->ctx.tls12_prf.key );
3443 }
Hanno Becker580fba12018-11-13 20:50:45 +00003444
3445 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3446 {
3447 mbedtls_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
3448 generator->ctx.tls12_prf.Ai_with_seed_len );
3449 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3450 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003451 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003452 else
3453#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003454 {
3455 status = PSA_ERROR_BAD_STATE;
3456 }
3457 memset( generator, 0, sizeof( *generator ) );
3458 return( status );
3459}
3460
3461
3462psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3463 size_t *capacity)
3464{
3465 *capacity = generator->capacity;
3466 return( PSA_SUCCESS );
3467}
3468
Gilles Peskinebef7f142018-07-12 17:22:21 +02003469#if defined(MBEDTLS_MD_C)
3470/* Read some bytes from an HKDF-based generator. This performs a chunk
3471 * of the expand phase of the HKDF algorithm. */
3472static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3473 psa_algorithm_t hash_alg,
3474 uint8_t *output,
3475 size_t output_length )
3476{
3477 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3478 psa_status_t status;
3479
3480 while( output_length != 0 )
3481 {
3482 /* Copy what remains of the current block */
3483 uint8_t n = hash_length - hkdf->offset_in_block;
3484 if( n > output_length )
3485 n = (uint8_t) output_length;
3486 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3487 output += n;
3488 output_length -= n;
3489 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003490 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003491 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003492 /* We can't be wanting more output after block 0xff, otherwise
3493 * the capacity check in psa_generator_read() would have
3494 * prevented this call. It could happen only if the generator
3495 * object was corrupted or if this function is called directly
3496 * inside the library. */
3497 if( hkdf->block_number == 0xff )
3498 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003499
3500 /* We need a new block */
3501 ++hkdf->block_number;
3502 hkdf->offset_in_block = 0;
3503 status = psa_hmac_setup_internal( &hkdf->hmac,
3504 hkdf->prk, hash_length,
3505 hash_alg );
3506 if( status != PSA_SUCCESS )
3507 return( status );
3508 if( hkdf->block_number != 1 )
3509 {
3510 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3511 hkdf->output_block,
3512 hash_length );
3513 if( status != PSA_SUCCESS )
3514 return( status );
3515 }
3516 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3517 hkdf->info,
3518 hkdf->info_length );
3519 if( status != PSA_SUCCESS )
3520 return( status );
3521 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3522 &hkdf->block_number, 1 );
3523 if( status != PSA_SUCCESS )
3524 return( status );
3525 status = psa_hmac_finish_internal( &hkdf->hmac,
3526 hkdf->output_block,
3527 sizeof( hkdf->output_block ) );
3528 if( status != PSA_SUCCESS )
3529 return( status );
3530 }
3531
3532 return( PSA_SUCCESS );
3533}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003534
3535static psa_status_t psa_generator_tls12_prf_generate_next_block(
3536 psa_tls12_prf_generator_t *tls12_prf,
3537 psa_algorithm_t alg )
3538{
3539 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3540 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3541 psa_hmac_internal_data hmac;
3542 psa_status_t status, cleanup_status;
3543
Hanno Becker3b339e22018-11-13 20:56:14 +00003544 unsigned char *Ai;
3545 size_t Ai_len;
3546
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003547 /* We can't be wanting more output after block 0xff, otherwise
3548 * the capacity check in psa_generator_read() would have
3549 * prevented this call. It could happen only if the generator
3550 * object was corrupted or if this function is called directly
3551 * inside the library. */
3552 if( tls12_prf->block_number == 0xff )
3553 return( PSA_ERROR_BAD_STATE );
3554
3555 /* We need a new block */
3556 ++tls12_prf->block_number;
3557 tls12_prf->offset_in_block = 0;
3558
3559 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3560 *
3561 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3562 *
3563 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3564 * HMAC_hash(secret, A(2) + seed) +
3565 * HMAC_hash(secret, A(3) + seed) + ...
3566 *
3567 * A(0) = seed
3568 * A(i) = HMAC_hash( secret, A(i-1) )
3569 *
3570 * The `psa_tls12_prf_generator` structures saves the block
3571 * `HMAC_hash(secret, A(i) + seed)` from which the output
3572 * is currently extracted as `output_block`, while
3573 * `A(i) + seed` is stored in `Ai_with_seed`.
3574 *
3575 * Generating a new block means recalculating `Ai_with_seed`
3576 * from the A(i)-part of it, and afterwards recalculating
3577 * `output_block`.
3578 *
3579 * A(0) is computed at setup time.
3580 *
3581 */
3582
3583 psa_hmac_init_internal( &hmac );
3584
3585 /* We must distinguish the calculation of A(1) from those
3586 * of A(2) and higher, because A(0)=seed has a different
3587 * length than the other A(i). */
3588 if( tls12_prf->block_number == 1 )
3589 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003590 Ai = tls12_prf->Ai_with_seed + hash_length;
3591 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003592 }
3593 else
3594 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003595 Ai = tls12_prf->Ai_with_seed;
3596 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003597 }
3598
Hanno Becker3b339e22018-11-13 20:56:14 +00003599 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3600 status = psa_hmac_setup_internal( &hmac,
3601 tls12_prf->key,
3602 tls12_prf->key_len,
3603 hash_alg );
3604 if( status != PSA_SUCCESS )
3605 goto cleanup;
3606
3607 status = psa_hash_update( &hmac.hash_ctx,
3608 Ai, Ai_len );
3609 if( status != PSA_SUCCESS )
3610 goto cleanup;
3611
3612 status = psa_hmac_finish_internal( &hmac,
3613 tls12_prf->Ai_with_seed,
3614 hash_length );
3615 if( status != PSA_SUCCESS )
3616 goto cleanup;
3617
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003618 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3619 status = psa_hmac_setup_internal( &hmac,
3620 tls12_prf->key,
3621 tls12_prf->key_len,
3622 hash_alg );
3623 if( status != PSA_SUCCESS )
3624 goto cleanup;
3625
3626 status = psa_hash_update( &hmac.hash_ctx,
3627 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003628 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003629 if( status != PSA_SUCCESS )
3630 goto cleanup;
3631
3632 status = psa_hmac_finish_internal( &hmac,
3633 tls12_prf->output_block,
3634 hash_length );
3635 if( status != PSA_SUCCESS )
3636 goto cleanup;
3637
3638cleanup:
3639
3640 cleanup_status = psa_hmac_abort_internal( &hmac );
3641 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3642 status = cleanup_status;
3643
3644 return( status );
3645}
3646
3647/* Read some bytes from an TLS-1.2-PRF-based generator.
3648 * See Section 5 of RFC 5246. */
3649static psa_status_t psa_generator_tls12_prf_read(
3650 psa_tls12_prf_generator_t *tls12_prf,
3651 psa_algorithm_t alg,
3652 uint8_t *output,
3653 size_t output_length )
3654{
3655 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3656 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3657 psa_status_t status;
3658
3659 while( output_length != 0 )
3660 {
3661 /* Copy what remains of the current block */
3662 uint8_t n = hash_length - tls12_prf->offset_in_block;
3663
3664 /* Check if we have fully processed the current block. */
3665 if( n == 0 )
3666 {
3667 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3668 alg );
3669 if( status != PSA_SUCCESS )
3670 return( status );
3671
3672 continue;
3673 }
3674
3675 if( n > output_length )
3676 n = (uint8_t) output_length;
3677 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3678 n );
3679 output += n;
3680 output_length -= n;
3681 tls12_prf->offset_in_block += n;
3682 }
3683
3684 return( PSA_SUCCESS );
3685}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003686#endif /* MBEDTLS_MD_C */
3687
Gilles Peskineeab56e42018-07-12 17:12:33 +02003688psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3689 uint8_t *output,
3690 size_t output_length )
3691{
3692 psa_status_t status;
3693
3694 if( output_length > generator->capacity )
3695 {
3696 generator->capacity = 0;
3697 /* Go through the error path to wipe all confidential data now
3698 * that the generator object is useless. */
3699 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3700 goto exit;
3701 }
3702 if( output_length == 0 &&
3703 generator->capacity == 0 && generator->alg == 0 )
3704 {
3705 /* Edge case: this is a blank or finished generator, and 0
3706 * bytes were requested. The right error in this case could
3707 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3708 * INSUFFICIENT_CAPACITY, which is right for a finished
3709 * generator, for consistency with the case when
3710 * output_length > 0. */
3711 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3712 }
3713 generator->capacity -= output_length;
3714
Gilles Peskine751d9652018-09-18 12:05:44 +02003715 if( generator->alg == PSA_ALG_SELECT_RAW )
3716 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003717 /* Initially, the capacity of a selection generator is always
3718 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3719 * abbreviated in this comment as `size`. When the remaining
3720 * capacity is `c`, the next bytes to serve start `c` bytes
3721 * from the end of the buffer, i.e. `size - c` from the
3722 * beginning of the buffer. Since `generator->capacity` was just
3723 * decremented above, we need to serve the bytes from
3724 * `size - generator->capacity - output_length` to
3725 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003726 size_t offset =
3727 generator->ctx.buffer.size - generator->capacity - output_length;
3728 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3729 status = PSA_SUCCESS;
3730 }
3731 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003732#if defined(MBEDTLS_MD_C)
3733 if( PSA_ALG_IS_HKDF( generator->alg ) )
3734 {
3735 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3736 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3737 output, output_length );
3738 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003739 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3740 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003741 {
3742 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3743 generator->alg, output,
3744 output_length );
3745 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003746 else
3747#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003748 {
3749 return( PSA_ERROR_BAD_STATE );
3750 }
3751
3752exit:
3753 if( status != PSA_SUCCESS )
3754 {
3755 psa_generator_abort( generator );
3756 memset( output, '!', output_length );
3757 }
3758 return( status );
3759}
3760
Gilles Peskine08542d82018-07-19 17:05:42 +02003761#if defined(MBEDTLS_DES_C)
3762static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3763{
3764 if( data_size >= 8 )
3765 mbedtls_des_key_set_parity( data );
3766 if( data_size >= 16 )
3767 mbedtls_des_key_set_parity( data + 8 );
3768 if( data_size >= 24 )
3769 mbedtls_des_key_set_parity( data + 16 );
3770}
3771#endif /* MBEDTLS_DES_C */
3772
Gilles Peskineeab56e42018-07-12 17:12:33 +02003773psa_status_t psa_generator_import_key( psa_key_slot_t key,
3774 psa_key_type_t type,
3775 size_t bits,
3776 psa_crypto_generator_t *generator )
3777{
3778 uint8_t *data = NULL;
3779 size_t bytes = PSA_BITS_TO_BYTES( bits );
3780 psa_status_t status;
3781
3782 if( ! key_type_is_raw_bytes( type ) )
3783 return( PSA_ERROR_INVALID_ARGUMENT );
3784 if( bits % 8 != 0 )
3785 return( PSA_ERROR_INVALID_ARGUMENT );
3786 data = mbedtls_calloc( 1, bytes );
3787 if( data == NULL )
3788 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3789
3790 status = psa_generator_read( generator, data, bytes );
3791 if( status != PSA_SUCCESS )
3792 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003793#if defined(MBEDTLS_DES_C)
3794 if( type == PSA_KEY_TYPE_DES )
3795 psa_des_set_key_parity( data, bytes );
3796#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003797 status = psa_import_key( key, type, data, bytes );
3798
3799exit:
3800 mbedtls_free( data );
3801 return( status );
3802}
3803
3804
3805
3806/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003807/* Key derivation */
3808/****************************************************************/
3809
Gilles Peskinea05219c2018-11-16 16:02:56 +01003810#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003811/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003812 * of the HKDF algorithm.
3813 *
3814 * Note that if this function fails, you must call psa_generator_abort()
3815 * to potentially free embedded data structures and wipe confidential data.
3816 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003817static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003818 const uint8_t *secret,
3819 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003820 psa_algorithm_t hash_alg,
3821 const uint8_t *salt,
3822 size_t salt_length,
3823 const uint8_t *label,
3824 size_t label_length )
3825{
3826 psa_status_t status;
3827 status = psa_hmac_setup_internal( &hkdf->hmac,
3828 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003829 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003830 if( status != PSA_SUCCESS )
3831 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003832 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003833 if( status != PSA_SUCCESS )
3834 return( status );
3835 status = psa_hmac_finish_internal( &hkdf->hmac,
3836 hkdf->prk,
3837 sizeof( hkdf->prk ) );
3838 if( status != PSA_SUCCESS )
3839 return( status );
3840 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3841 hkdf->block_number = 0;
3842 hkdf->info_length = label_length;
3843 if( label_length != 0 )
3844 {
3845 hkdf->info = mbedtls_calloc( 1, label_length );
3846 if( hkdf->info == NULL )
3847 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3848 memcpy( hkdf->info, label, label_length );
3849 }
3850 return( PSA_SUCCESS );
3851}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003852#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003853
Gilles Peskinea05219c2018-11-16 16:02:56 +01003854#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003855/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3856 *
3857 * Note that if this function fails, you must call psa_generator_abort()
3858 * to potentially free embedded data structures and wipe confidential data.
3859 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003860static psa_status_t psa_generator_tls12_prf_setup(
3861 psa_tls12_prf_generator_t *tls12_prf,
3862 const unsigned char *key,
3863 size_t key_len,
3864 psa_algorithm_t hash_alg,
3865 const uint8_t *salt,
3866 size_t salt_length,
3867 const uint8_t *label,
3868 size_t label_length )
3869{
3870 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003871 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3872 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003873
3874 tls12_prf->key = mbedtls_calloc( 1, key_len );
3875 if( tls12_prf->key == NULL )
3876 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3877 tls12_prf->key_len = key_len;
3878 memcpy( tls12_prf->key, key, key_len );
3879
Hanno Becker580fba12018-11-13 20:50:45 +00003880 overflow = ( salt_length + label_length < salt_length ) ||
3881 ( salt_length + label_length + hash_length < hash_length );
3882 if( overflow )
3883 return( PSA_ERROR_INVALID_ARGUMENT );
3884
3885 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3886 if( tls12_prf->Ai_with_seed == NULL )
3887 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3888 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3889
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003890 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3891 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003892 if( label_length != 0 )
3893 {
3894 memcpy( tls12_prf->Ai_with_seed + hash_length,
3895 label, label_length );
3896 }
3897
3898 if( salt_length != 0 )
3899 {
3900 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3901 salt, salt_length );
3902 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003903
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003904 /* The first block gets generated when
3905 * psa_generator_read() is called. */
3906 tls12_prf->block_number = 0;
3907 tls12_prf->offset_in_block = hash_length;
3908
3909 return( PSA_SUCCESS );
3910}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003911
3912/* Set up a TLS-1.2-PSK-to-MS-based generator. */
3913static psa_status_t psa_generator_tls12_psk_to_ms_setup(
3914 psa_tls12_prf_generator_t *tls12_prf,
3915 const unsigned char *psk,
3916 size_t psk_len,
3917 psa_algorithm_t hash_alg,
3918 const uint8_t *salt,
3919 size_t salt_length,
3920 const uint8_t *label,
3921 size_t label_length )
3922{
3923 psa_status_t status;
3924 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
3925
3926 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
3927 return( PSA_ERROR_INVALID_ARGUMENT );
3928
3929 /* Quoting RFC 4279, Section 2:
3930 *
3931 * The premaster secret is formed as follows: if the PSK is N octets
3932 * long, concatenate a uint16 with the value N, N zero octets, a second
3933 * uint16 with the value N, and the PSK itself.
3934 */
3935
3936 pms[0] = ( psk_len >> 8 ) & 0xff;
3937 pms[1] = ( psk_len >> 0 ) & 0xff;
3938 memset( pms + 2, 0, psk_len );
3939 pms[2 + psk_len + 0] = pms[0];
3940 pms[2 + psk_len + 1] = pms[1];
3941 memcpy( pms + 4 + psk_len, psk, psk_len );
3942
3943 status = psa_generator_tls12_prf_setup( tls12_prf,
3944 pms, 4 + 2 * psk_len,
3945 hash_alg,
3946 salt, salt_length,
3947 label, label_length );
3948
3949 mbedtls_zeroize( pms, sizeof( pms ) );
3950 return( status );
3951}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003952#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003953
Gilles Peskine346797d2018-11-16 16:05:06 +01003954/* Note that if this function fails, you must call psa_generator_abort()
3955 * to potentially free embedded data structures and wipe confidential data.
3956 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003957static psa_status_t psa_key_derivation_internal(
3958 psa_crypto_generator_t *generator,
3959 const uint8_t *secret, size_t secret_length,
3960 psa_algorithm_t alg,
3961 const uint8_t *salt, size_t salt_length,
3962 const uint8_t *label, size_t label_length,
3963 size_t capacity )
3964{
3965 psa_status_t status;
3966 size_t max_capacity;
3967
3968 /* Set generator->alg even on failure so that abort knows what to do. */
3969 generator->alg = alg;
3970
Gilles Peskine751d9652018-09-18 12:05:44 +02003971 if( alg == PSA_ALG_SELECT_RAW )
3972 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003973 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02003974 if( salt_length != 0 )
3975 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01003976 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02003977 if( label_length != 0 )
3978 return( PSA_ERROR_INVALID_ARGUMENT );
3979 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
3980 if( generator->ctx.buffer.data == NULL )
3981 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3982 memcpy( generator->ctx.buffer.data, secret, secret_length );
3983 generator->ctx.buffer.size = secret_length;
3984 max_capacity = secret_length;
3985 status = PSA_SUCCESS;
3986 }
3987 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003988#if defined(MBEDTLS_MD_C)
3989 if( PSA_ALG_IS_HKDF( alg ) )
3990 {
3991 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3992 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3993 if( hash_size == 0 )
3994 return( PSA_ERROR_NOT_SUPPORTED );
3995 max_capacity = 255 * hash_size;
3996 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3997 secret, secret_length,
3998 hash_alg,
3999 salt, salt_length,
4000 label, label_length );
4001 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004002 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4003 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4004 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004005 {
4006 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4007 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4008
4009 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4010 if( hash_alg != PSA_ALG_SHA_256 &&
4011 hash_alg != PSA_ALG_SHA_384 )
4012 {
4013 return( PSA_ERROR_NOT_SUPPORTED );
4014 }
4015
4016 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004017
4018 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4019 {
4020 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4021 secret, secret_length,
4022 hash_alg, salt, salt_length,
4023 label, label_length );
4024 }
4025 else
4026 {
4027 status = psa_generator_tls12_psk_to_ms_setup(
4028 &generator->ctx.tls12_prf,
4029 secret, secret_length,
4030 hash_alg, salt, salt_length,
4031 label, label_length );
4032 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004033 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004034 else
4035#endif
4036 {
4037 return( PSA_ERROR_NOT_SUPPORTED );
4038 }
4039
4040 if( status != PSA_SUCCESS )
4041 return( status );
4042
4043 if( capacity <= max_capacity )
4044 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004045 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4046 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004047 else
4048 return( PSA_ERROR_INVALID_ARGUMENT );
4049
4050 return( PSA_SUCCESS );
4051}
4052
Gilles Peskineea0fb492018-07-12 17:17:20 +02004053psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01004054 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004055 psa_algorithm_t alg,
4056 const uint8_t *salt,
4057 size_t salt_length,
4058 const uint8_t *label,
4059 size_t label_length,
4060 size_t capacity )
4061{
4062 key_slot_t *slot;
4063 psa_status_t status;
4064
4065 if( generator->alg != 0 )
4066 return( PSA_ERROR_BAD_STATE );
4067
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004068 /* Make sure that alg is a key derivation algorithm. This prevents
4069 * key selection algorithms, which psa_key_derivation_internal
4070 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004071 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4072 return( PSA_ERROR_INVALID_ARGUMENT );
4073
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004074 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
4075 if( status != PSA_SUCCESS )
4076 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004077
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004078 if( slot->type != PSA_KEY_TYPE_DERIVE )
4079 return( PSA_ERROR_INVALID_ARGUMENT );
4080
4081 status = psa_key_derivation_internal( generator,
4082 slot->data.raw.data,
4083 slot->data.raw.bytes,
4084 alg,
4085 salt, salt_length,
4086 label, label_length,
4087 capacity );
4088 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004089 psa_generator_abort( generator );
4090 return( status );
4091}
4092
4093
4094
4095/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004096/* Key agreement */
4097/****************************************************************/
4098
Gilles Peskinea05219c2018-11-16 16:02:56 +01004099#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004100static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4101 size_t peer_key_length,
4102 const mbedtls_ecp_keypair *our_key,
4103 uint8_t *shared_secret,
4104 size_t shared_secret_size,
4105 size_t *shared_secret_length )
4106{
4107 mbedtls_pk_context pk;
4108 mbedtls_ecp_keypair *their_key = NULL;
4109 mbedtls_ecdh_context ecdh;
4110 int ret;
4111 mbedtls_ecdh_init( &ecdh );
4112 mbedtls_pk_init( &pk );
4113
4114 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
4115 if( ret != 0 )
4116 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02004117 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004118 {
Gilles Peskine88714d72018-10-25 23:07:25 +02004119 case MBEDTLS_PK_ECKEY:
4120 case MBEDTLS_PK_ECKEY_DH:
4121 break;
4122 default:
4123 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4124 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004125 }
4126 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01004127 if( their_key->grp.id != our_key->grp.id )
4128 {
4129 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4130 goto exit;
4131 }
4132
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004133 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
4134 if( ret != 0 )
4135 goto exit;
4136 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
4137 if( ret != 0 )
4138 goto exit;
4139
4140 ret = mbedtls_ecdh_calc_secret( &ecdh,
4141 shared_secret_length,
4142 shared_secret, shared_secret_size,
4143 mbedtls_ctr_drbg_random,
4144 &global_data.ctr_drbg );
4145
4146exit:
4147 mbedtls_pk_free( &pk );
4148 mbedtls_ecdh_free( &ecdh );
4149 return( mbedtls_to_psa_error( ret ) );
4150}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004151#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004152
Gilles Peskine01d718c2018-09-18 12:01:02 +02004153#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4154
Gilles Peskine346797d2018-11-16 16:05:06 +01004155/* Note that if this function fails, you must call psa_generator_abort()
4156 * to potentially free embedded data structures and wipe confidential data.
4157 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004158static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
4159 key_slot_t *private_key,
4160 const uint8_t *peer_key,
4161 size_t peer_key_length,
4162 psa_algorithm_t alg )
4163{
4164 psa_status_t status;
4165 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4166 size_t shared_secret_length = 0;
4167
4168 /* Step 1: run the secret agreement algorithm to generate the shared
4169 * secret. */
4170 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4171 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004172#if defined(MBEDTLS_ECDH_C)
4173 case PSA_ALG_ECDH_BASE:
4174 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4175 return( PSA_ERROR_INVALID_ARGUMENT );
4176 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4177 private_key->data.ecp,
4178 shared_secret,
4179 sizeof( shared_secret ),
4180 &shared_secret_length );
4181 break;
4182#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004183 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004184 (void) private_key;
4185 (void) peer_key;
4186 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004187 return( PSA_ERROR_NOT_SUPPORTED );
4188 }
4189 if( status != PSA_SUCCESS )
4190 goto exit;
4191
4192 /* Step 2: set up the key derivation to generate key material from
4193 * the shared secret. */
4194 status = psa_key_derivation_internal( generator,
4195 shared_secret, shared_secret_length,
4196 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4197 NULL, 0, NULL, 0,
4198 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4199exit:
4200 mbedtls_zeroize( shared_secret, shared_secret_length );
4201 return( status );
4202}
4203
4204psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
4205 psa_key_slot_t private_key,
4206 const uint8_t *peer_key,
4207 size_t peer_key_length,
4208 psa_algorithm_t alg )
4209{
4210 key_slot_t *slot;
4211 psa_status_t status;
4212 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4213 return( PSA_ERROR_INVALID_ARGUMENT );
4214 status = psa_get_key_from_slot( private_key, &slot,
4215 PSA_KEY_USAGE_DERIVE, alg );
4216 if( status != PSA_SUCCESS )
4217 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004218 status = psa_key_agreement_internal( generator,
4219 slot,
4220 peer_key, peer_key_length,
4221 alg );
4222 if( status != PSA_SUCCESS )
4223 psa_generator_abort( generator );
4224 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004225}
4226
4227
4228
4229/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004230/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004231/****************************************************************/
4232
4233psa_status_t psa_generate_random( uint8_t *output,
4234 size_t output_size )
4235{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004236 int ret;
4237 GUARD_MODULE_INITIALIZED;
4238
4239 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004240 return( mbedtls_to_psa_error( ret ) );
4241}
4242
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004243#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004244
4245/* Support function for error conversion between psa_its error codes to psa crypto */
4246static psa_status_t its_to_psa_error( psa_its_status_t ret )
4247{
4248 switch( ret )
4249 {
4250 case PSA_ITS_SUCCESS:
4251 return( PSA_SUCCESS );
4252
4253 case PSA_ITS_ERROR_KEY_NOT_FOUND:
4254 return( PSA_ERROR_EMPTY_SLOT );
4255
4256 case PSA_ITS_ERROR_STORAGE_FAILURE:
4257 return( PSA_ERROR_STORAGE_FAILURE );
4258
4259 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4260 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4261
4262 case PSA_ITS_ERROR_INVALID_KEY:
4263 case PSA_PS_ERROR_OFFSET_INVALID:
4264 case PSA_ITS_ERROR_INCORRECT_SIZE:
4265 case PSA_ITS_ERROR_BAD_POINTER:
4266 return( PSA_ERROR_INVALID_ARGUMENT );
4267
4268 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4269 return( PSA_ERROR_NOT_SUPPORTED );
4270
4271 case PSA_ITS_ERROR_WRITE_ONCE:
4272 return( PSA_ERROR_OCCUPIED_SLOT );
4273
4274 default:
4275 return( PSA_ERROR_UNKNOWN_ERROR );
4276 }
4277}
4278
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004279psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4280 size_t seed_size )
4281{
4282 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004283 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004284 struct psa_its_info_t p_info;
4285 if( global_data.initialized )
4286 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004287
4288 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4289 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4290 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4291 return( PSA_ERROR_INVALID_ARGUMENT );
4292
avolinski0d2c2662018-11-21 17:31:07 +02004293 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004294 status = its_to_psa_error( its_status );
4295
4296 if( PSA_ITS_ERROR_KEY_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004297 {
avolinski0d2c2662018-11-21 17:31:07 +02004298 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004299 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004300 }
avolinski13beb102018-11-20 16:51:49 +02004301 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004302 {
4303 /* You should not be here. Seed needs to be injected only once */
4304 status = PSA_ERROR_NOT_PERMITTED;
4305 }
4306 return( status );
4307}
4308#endif
4309
Gilles Peskine05d69892018-06-19 22:00:52 +02004310psa_status_t psa_generate_key( psa_key_slot_t key,
4311 psa_key_type_t type,
4312 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004313 const void *extra,
4314 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004315{
Gilles Peskine12313cd2018-06-20 00:20:32 +02004316 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004317 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004318
Gilles Peskine53d991e2018-07-12 01:14:59 +02004319 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004320 return( PSA_ERROR_INVALID_ARGUMENT );
4321
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004322 status = psa_get_empty_key_slot( key, &slot );
4323 if( status != PSA_SUCCESS )
4324 return( status );
4325
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004326 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004327 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004328 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004329 if( status != PSA_SUCCESS )
4330 return( status );
4331 status = psa_generate_random( slot->data.raw.data,
4332 slot->data.raw.bytes );
4333 if( status != PSA_SUCCESS )
4334 {
4335 mbedtls_free( slot->data.raw.data );
4336 return( status );
4337 }
4338#if defined(MBEDTLS_DES_C)
4339 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004340 psa_des_set_key_parity( slot->data.raw.data,
4341 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004342#endif /* MBEDTLS_DES_C */
4343 }
4344 else
4345
4346#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4347 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4348 {
4349 mbedtls_rsa_context *rsa;
4350 int ret;
4351 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004352 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4353 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004354 /* Accept only byte-aligned keys, for the same reasons as
4355 * in psa_import_rsa_key(). */
4356 if( bits % 8 != 0 )
4357 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004358 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004359 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004360 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004361 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004362 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004363#if INT_MAX < 0xffffffff
4364 /* Check that the uint32_t value passed by the caller fits
4365 * in the range supported by this implementation. */
4366 if( p->e > INT_MAX )
4367 return( PSA_ERROR_NOT_SUPPORTED );
4368#endif
4369 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004370 }
4371 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4372 if( rsa == NULL )
4373 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4374 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4375 ret = mbedtls_rsa_gen_key( rsa,
4376 mbedtls_ctr_drbg_random,
4377 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004378 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004379 exponent );
4380 if( ret != 0 )
4381 {
4382 mbedtls_rsa_free( rsa );
4383 mbedtls_free( rsa );
4384 return( mbedtls_to_psa_error( ret ) );
4385 }
4386 slot->data.rsa = rsa;
4387 }
4388 else
4389#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4390
4391#if defined(MBEDTLS_ECP_C)
4392 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4393 {
4394 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4395 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4396 const mbedtls_ecp_curve_info *curve_info =
4397 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4398 mbedtls_ecp_keypair *ecp;
4399 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004400 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004401 return( PSA_ERROR_NOT_SUPPORTED );
4402 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4403 return( PSA_ERROR_NOT_SUPPORTED );
4404 if( curve_info->bit_size != bits )
4405 return( PSA_ERROR_INVALID_ARGUMENT );
4406 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4407 if( ecp == NULL )
4408 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4409 mbedtls_ecp_keypair_init( ecp );
4410 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4411 mbedtls_ctr_drbg_random,
4412 &global_data.ctr_drbg );
4413 if( ret != 0 )
4414 {
4415 mbedtls_ecp_keypair_free( ecp );
4416 mbedtls_free( ecp );
4417 return( mbedtls_to_psa_error( ret ) );
4418 }
4419 slot->data.ecp = ecp;
4420 }
4421 else
4422#endif /* MBEDTLS_ECP_C */
4423
4424 return( PSA_ERROR_NOT_SUPPORTED );
4425
4426 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004427
4428#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4429 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4430 {
4431 return( psa_save_generated_persistent_key( key, slot, bits ) );
4432 }
4433#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4434
4435 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004436}
4437
4438
4439/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004440/* Module setup */
4441/****************************************************************/
4442
Gilles Peskine5e769522018-11-20 21:59:56 +01004443psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4444 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4445 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4446{
4447 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4448 return( PSA_ERROR_BAD_STATE );
4449 global_data.entropy_init = entropy_init;
4450 global_data.entropy_free = entropy_free;
4451 return( PSA_SUCCESS );
4452}
4453
Gilles Peskinee59236f2018-01-27 23:32:46 +01004454void mbedtls_psa_crypto_free( void )
4455{
Jaeden Amero045bd502018-06-26 14:00:08 +01004456 psa_key_slot_t key;
Darryl Green40225ba2018-11-15 14:48:15 +00004457 key_slot_t *slot;
4458 psa_status_t status;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004459 if( global_data.key_slots_initialized )
Darryl Green40225ba2018-11-15 14:48:15 +00004460 {
Gilles Peskinec6b69072018-11-20 21:42:52 +01004461 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
4462 {
4463 status = psa_get_key_slot( key, &slot );
4464 if( status != PSA_SUCCESS )
4465 continue;
4466 psa_remove_key_data_from_memory( slot );
4467 /* Zeroize the slot to wipe metadata such as policies. */
4468 mbedtls_zeroize( slot, sizeof( *slot ) );
4469 }
Darryl Green40225ba2018-11-15 14:48:15 +00004470 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01004471 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4472 {
4473 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004474 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004475 }
4476 /* Wipe all remaining data, including configuration.
4477 * In particular, this sets all state indicator to the value
4478 * indicating "uninitialized". */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004479 mbedtls_zeroize( &global_data, sizeof( global_data ) );
4480}
4481
4482psa_status_t psa_crypto_init( void )
4483{
4484 int ret;
4485 const unsigned char drbg_seed[] = "PSA";
4486
Gilles Peskinec6b69072018-11-20 21:42:52 +01004487 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004488 if( global_data.initialized != 0 )
4489 return( PSA_SUCCESS );
4490
Gilles Peskine5e769522018-11-20 21:59:56 +01004491 /* Set default configuration if
4492 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4493 if( global_data.entropy_init == NULL )
4494 global_data.entropy_init = mbedtls_entropy_init;
4495 if( global_data.entropy_free == NULL )
4496 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004497
4498 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004499 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004500 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004501 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004502 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4503 mbedtls_entropy_func,
4504 &global_data.entropy,
4505 drbg_seed, sizeof( drbg_seed ) - 1 );
4506 if( ret != 0 )
4507 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004508 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004509
Gilles Peskinec6b69072018-11-20 21:42:52 +01004510 /* Initialize the key slots. Zero-initialization has made all key
4511 * slots empty, so there is nothing to do. In a future version we will
4512 * load data from storage. */
4513 global_data.key_slots_initialized = 1;
4514
4515 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004516 global_data.initialized = 1;
4517
Gilles Peskinee59236f2018-01-27 23:32:46 +01004518exit:
4519 if( ret != 0 )
4520 mbedtls_psa_crypto_free( );
4521 return( mbedtls_to_psa_error( ret ) );
4522}
4523
4524#endif /* MBEDTLS_PSA_CRYPTO_C */