blob: 7415a9a4fe174450b26367cb03ebe6d2c13044c8 [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 Peskine5a3c50e2018-12-04 12:27:09 +0100150/* Values for psa_global_data_t::rng_state */
151#define RNG_NOT_INITIALIZED 0
152#define RNG_INITIALIZED 1
153#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100154
Gilles Peskine2d277862018-06-18 15:41:12 +0200155typedef struct
156{
Gilles Peskine5e769522018-11-20 21:59:56 +0100157 void (* entropy_init )( mbedtls_entropy_context *ctx );
158 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100159 mbedtls_entropy_context entropy;
160 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200161 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinec6b69072018-11-20 21:42:52 +0100162 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100163 unsigned rng_state : 2;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100164 unsigned key_slots_initialized : 1;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100165} psa_global_data_t;
166
167static psa_global_data_t global_data;
168
itayzafrir0adf0fc2018-09-06 16:24:41 +0300169#define GUARD_MODULE_INITIALIZED \
170 if( global_data.initialized == 0 ) \
171 return( PSA_ERROR_BAD_STATE );
172
Gilles Peskinee59236f2018-01-27 23:32:46 +0100173static psa_status_t mbedtls_to_psa_error( int ret )
174{
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 /* If there's both a high-level code and low-level code, dispatch on
176 * the high-level code. */
177 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100178 {
179 case 0:
180 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100181
182 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
183 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
184 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
185 return( PSA_ERROR_NOT_SUPPORTED );
186 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
187 return( PSA_ERROR_HARDWARE_FAILURE );
188
189 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
190 return( PSA_ERROR_HARDWARE_FAILURE );
191
Gilles Peskine9a944802018-06-21 09:35:35 +0200192 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
193 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
194 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
195 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
196 case MBEDTLS_ERR_ASN1_INVALID_DATA:
197 return( PSA_ERROR_INVALID_ARGUMENT );
198 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
199 return( PSA_ERROR_INSUFFICIENT_MEMORY );
200 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
201 return( PSA_ERROR_BUFFER_TOO_SMALL );
202
Gilles Peskinea5905292018-02-07 20:59:33 +0100203 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
204 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
205 return( PSA_ERROR_NOT_SUPPORTED );
206 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
207 return( PSA_ERROR_HARDWARE_FAILURE );
208
209 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
210 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
211 return( PSA_ERROR_NOT_SUPPORTED );
212 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
213 return( PSA_ERROR_HARDWARE_FAILURE );
214
215 case MBEDTLS_ERR_CCM_BAD_INPUT:
216 return( PSA_ERROR_INVALID_ARGUMENT );
217 case MBEDTLS_ERR_CCM_AUTH_FAILED:
218 return( PSA_ERROR_INVALID_SIGNATURE );
219 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
220 return( PSA_ERROR_HARDWARE_FAILURE );
221
222 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
223 return( PSA_ERROR_NOT_SUPPORTED );
224 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
225 return( PSA_ERROR_INVALID_ARGUMENT );
226 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
227 return( PSA_ERROR_INSUFFICIENT_MEMORY );
228 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
229 return( PSA_ERROR_INVALID_PADDING );
230 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
231 return( PSA_ERROR_BAD_STATE );
232 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
233 return( PSA_ERROR_INVALID_SIGNATURE );
234 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
235 return( PSA_ERROR_TAMPERING_DETECTED );
236 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
237 return( PSA_ERROR_HARDWARE_FAILURE );
238
239 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
240 return( PSA_ERROR_HARDWARE_FAILURE );
241
242 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
243 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
244 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
245 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
246 return( PSA_ERROR_NOT_SUPPORTED );
247 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
248 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
249
250 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
251 return( PSA_ERROR_NOT_SUPPORTED );
252 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
253 return( PSA_ERROR_HARDWARE_FAILURE );
254
Gilles Peskinee59236f2018-01-27 23:32:46 +0100255 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
256 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
257 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
258 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100259
260 case MBEDTLS_ERR_GCM_AUTH_FAILED:
261 return( PSA_ERROR_INVALID_SIGNATURE );
262 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200263 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100264 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
265 return( PSA_ERROR_HARDWARE_FAILURE );
266
267 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
268 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
269 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
270 return( PSA_ERROR_HARDWARE_FAILURE );
271
272 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
273 return( PSA_ERROR_NOT_SUPPORTED );
274 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
275 return( PSA_ERROR_INVALID_ARGUMENT );
276 case MBEDTLS_ERR_MD_ALLOC_FAILED:
277 return( PSA_ERROR_INSUFFICIENT_MEMORY );
278 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
279 return( PSA_ERROR_STORAGE_FAILURE );
280 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
281 return( PSA_ERROR_HARDWARE_FAILURE );
282
Gilles Peskinef76aa772018-10-29 19:24:33 +0100283 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
284 return( PSA_ERROR_STORAGE_FAILURE );
285 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
286 return( PSA_ERROR_INVALID_ARGUMENT );
287 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
288 return( PSA_ERROR_INVALID_ARGUMENT );
289 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
290 return( PSA_ERROR_BUFFER_TOO_SMALL );
291 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
292 return( PSA_ERROR_INVALID_ARGUMENT );
293 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
294 return( PSA_ERROR_INVALID_ARGUMENT );
295 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
296 return( PSA_ERROR_INVALID_ARGUMENT );
297 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
298 return( PSA_ERROR_INSUFFICIENT_MEMORY );
299
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100300 case MBEDTLS_ERR_PK_ALLOC_FAILED:
301 return( PSA_ERROR_INSUFFICIENT_MEMORY );
302 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
303 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
304 return( PSA_ERROR_INVALID_ARGUMENT );
305 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100307 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
308 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
309 return( PSA_ERROR_INVALID_ARGUMENT );
310 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
311 return( PSA_ERROR_NOT_SUPPORTED );
312 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
313 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
314 return( PSA_ERROR_NOT_PERMITTED );
315 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
316 return( PSA_ERROR_INVALID_ARGUMENT );
317 case MBEDTLS_ERR_PK_INVALID_ALG:
318 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
319 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
320 return( PSA_ERROR_NOT_SUPPORTED );
321 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
322 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100323 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
324 return( PSA_ERROR_HARDWARE_FAILURE );
325
326 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
327 return( PSA_ERROR_HARDWARE_FAILURE );
328
329 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
330 return( PSA_ERROR_INVALID_ARGUMENT );
331 case MBEDTLS_ERR_RSA_INVALID_PADDING:
332 return( PSA_ERROR_INVALID_PADDING );
333 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
334 return( PSA_ERROR_HARDWARE_FAILURE );
335 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
336 return( PSA_ERROR_INVALID_ARGUMENT );
337 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
338 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
339 return( PSA_ERROR_TAMPERING_DETECTED );
340 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
341 return( PSA_ERROR_INVALID_SIGNATURE );
342 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
343 return( PSA_ERROR_BUFFER_TOO_SMALL );
344 case MBEDTLS_ERR_RSA_RNG_FAILED:
345 return( PSA_ERROR_INSUFFICIENT_MEMORY );
346 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
347 return( PSA_ERROR_NOT_SUPPORTED );
348 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
349 return( PSA_ERROR_HARDWARE_FAILURE );
350
351 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
352 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
353 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
354 return( PSA_ERROR_HARDWARE_FAILURE );
355
356 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
357 return( PSA_ERROR_INVALID_ARGUMENT );
358 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
359 return( PSA_ERROR_HARDWARE_FAILURE );
360
itayzafrir5c753392018-05-08 11:18:38 +0300361 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300362 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300363 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300364 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
365 return( PSA_ERROR_BUFFER_TOO_SMALL );
366 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
367 return( PSA_ERROR_NOT_SUPPORTED );
368 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
369 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
370 return( PSA_ERROR_INVALID_SIGNATURE );
371 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
372 return( PSA_ERROR_INSUFFICIENT_MEMORY );
373 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
374 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300375
Gilles Peskinee59236f2018-01-27 23:32:46 +0100376 default:
377 return( PSA_ERROR_UNKNOWN_ERROR );
378 }
379}
380
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200381
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200382
383
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100384/****************************************************************/
385/* Key management */
386/****************************************************************/
387
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100388#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200389static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
390{
391 switch( grpid )
392 {
393 case MBEDTLS_ECP_DP_SECP192R1:
394 return( PSA_ECC_CURVE_SECP192R1 );
395 case MBEDTLS_ECP_DP_SECP224R1:
396 return( PSA_ECC_CURVE_SECP224R1 );
397 case MBEDTLS_ECP_DP_SECP256R1:
398 return( PSA_ECC_CURVE_SECP256R1 );
399 case MBEDTLS_ECP_DP_SECP384R1:
400 return( PSA_ECC_CURVE_SECP384R1 );
401 case MBEDTLS_ECP_DP_SECP521R1:
402 return( PSA_ECC_CURVE_SECP521R1 );
403 case MBEDTLS_ECP_DP_BP256R1:
404 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
405 case MBEDTLS_ECP_DP_BP384R1:
406 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
407 case MBEDTLS_ECP_DP_BP512R1:
408 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
409 case MBEDTLS_ECP_DP_CURVE25519:
410 return( PSA_ECC_CURVE_CURVE25519 );
411 case MBEDTLS_ECP_DP_SECP192K1:
412 return( PSA_ECC_CURVE_SECP192K1 );
413 case MBEDTLS_ECP_DP_SECP224K1:
414 return( PSA_ECC_CURVE_SECP224K1 );
415 case MBEDTLS_ECP_DP_SECP256K1:
416 return( PSA_ECC_CURVE_SECP256K1 );
417 case MBEDTLS_ECP_DP_CURVE448:
418 return( PSA_ECC_CURVE_CURVE448 );
419 default:
420 return( 0 );
421 }
422}
423
Gilles Peskine12313cd2018-06-20 00:20:32 +0200424static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
425{
426 switch( curve )
427 {
428 case PSA_ECC_CURVE_SECP192R1:
429 return( MBEDTLS_ECP_DP_SECP192R1 );
430 case PSA_ECC_CURVE_SECP224R1:
431 return( MBEDTLS_ECP_DP_SECP224R1 );
432 case PSA_ECC_CURVE_SECP256R1:
433 return( MBEDTLS_ECP_DP_SECP256R1 );
434 case PSA_ECC_CURVE_SECP384R1:
435 return( MBEDTLS_ECP_DP_SECP384R1 );
436 case PSA_ECC_CURVE_SECP521R1:
437 return( MBEDTLS_ECP_DP_SECP521R1 );
438 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
439 return( MBEDTLS_ECP_DP_BP256R1 );
440 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
441 return( MBEDTLS_ECP_DP_BP384R1 );
442 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
443 return( MBEDTLS_ECP_DP_BP512R1 );
444 case PSA_ECC_CURVE_CURVE25519:
445 return( MBEDTLS_ECP_DP_CURVE25519 );
446 case PSA_ECC_CURVE_SECP192K1:
447 return( MBEDTLS_ECP_DP_SECP192K1 );
448 case PSA_ECC_CURVE_SECP224K1:
449 return( MBEDTLS_ECP_DP_SECP224K1 );
450 case PSA_ECC_CURVE_SECP256K1:
451 return( MBEDTLS_ECP_DP_SECP256K1 );
452 case PSA_ECC_CURVE_CURVE448:
453 return( MBEDTLS_ECP_DP_CURVE448 );
454 default:
455 return( MBEDTLS_ECP_DP_NONE );
456 }
457}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100458#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200459
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200460static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
461 size_t bits,
462 struct raw_data *raw )
463{
464 /* Check that the bit size is acceptable for the key type */
465 switch( type )
466 {
467 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200468 if( bits == 0 )
469 {
470 raw->bytes = 0;
471 raw->data = NULL;
472 return( PSA_SUCCESS );
473 }
474 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200475#if defined(MBEDTLS_MD_C)
476 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200477#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200478 case PSA_KEY_TYPE_DERIVE:
479 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200480#if defined(MBEDTLS_AES_C)
481 case PSA_KEY_TYPE_AES:
482 if( bits != 128 && bits != 192 && bits != 256 )
483 return( PSA_ERROR_INVALID_ARGUMENT );
484 break;
485#endif
486#if defined(MBEDTLS_CAMELLIA_C)
487 case PSA_KEY_TYPE_CAMELLIA:
488 if( bits != 128 && bits != 192 && bits != 256 )
489 return( PSA_ERROR_INVALID_ARGUMENT );
490 break;
491#endif
492#if defined(MBEDTLS_DES_C)
493 case PSA_KEY_TYPE_DES:
494 if( bits != 64 && bits != 128 && bits != 192 )
495 return( PSA_ERROR_INVALID_ARGUMENT );
496 break;
497#endif
498#if defined(MBEDTLS_ARC4_C)
499 case PSA_KEY_TYPE_ARC4:
500 if( bits < 8 || bits > 2048 )
501 return( PSA_ERROR_INVALID_ARGUMENT );
502 break;
503#endif
504 default:
505 return( PSA_ERROR_NOT_SUPPORTED );
506 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200507 if( bits % 8 != 0 )
508 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200509
510 /* Allocate memory for the key */
511 raw->bytes = PSA_BITS_TO_BYTES( bits );
512 raw->data = mbedtls_calloc( 1, raw->bytes );
513 if( raw->data == NULL )
514 {
515 raw->bytes = 0;
516 return( PSA_ERROR_INSUFFICIENT_MEMORY );
517 }
518 return( PSA_SUCCESS );
519}
520
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200521#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100522/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
523 * that are not a multiple of 8) well. For example, there is only
524 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
525 * way to return the exact bit size of a key.
526 * To keep things simple, reject non-byte-aligned key sizes. */
527static psa_status_t psa_check_rsa_key_byte_aligned(
528 const mbedtls_rsa_context *rsa )
529{
530 mbedtls_mpi n;
531 psa_status_t status;
532 mbedtls_mpi_init( &n );
533 status = mbedtls_to_psa_error(
534 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
535 if( status == PSA_SUCCESS )
536 {
537 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
538 status = PSA_ERROR_NOT_SUPPORTED;
539 }
540 mbedtls_mpi_free( &n );
541 return( status );
542}
543
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200544static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
545 mbedtls_rsa_context **p_rsa )
546{
547 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
548 return( PSA_ERROR_INVALID_ARGUMENT );
549 else
550 {
551 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
Gilles Peskineaac64a22018-11-12 18:37:42 +0100552 /* The size of an RSA key doesn't have to be a multiple of 8.
553 * Mbed TLS supports non-byte-aligned key sizes, but not well.
554 * For example, mbedtls_rsa_get_len() returns the key size in
555 * bytes, not in bits. */
556 size_t bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100557 psa_status_t status;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200558 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
559 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100560 status = psa_check_rsa_key_byte_aligned( rsa );
561 if( status != PSA_SUCCESS )
562 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200563 *p_rsa = rsa;
564 return( PSA_SUCCESS );
565 }
566}
567#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
568
569#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100570/* Import an elliptic curve parsed by the mbedtls pk module. */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200571static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
572 mbedtls_pk_context *pk,
573 mbedtls_ecp_keypair **p_ecp )
574{
575 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
576 return( PSA_ERROR_INVALID_ARGUMENT );
577 else
578 {
579 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
580 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
581 if( actual_curve != expected_curve )
582 return( PSA_ERROR_INVALID_ARGUMENT );
583 *p_ecp = ecp;
584 return( PSA_SUCCESS );
585 }
586}
587#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
588
Gilles Peskinef76aa772018-10-29 19:24:33 +0100589#if defined(MBEDTLS_ECP_C)
590/* Import a private key given as a byte string which is the private value
591 * in big-endian order. */
592static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
593 const uint8_t *data,
594 size_t data_length,
595 mbedtls_ecp_keypair **p_ecp )
596{
597 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
598 mbedtls_ecp_keypair *ecp = NULL;
599 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
600
601 *p_ecp = NULL;
602 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
603 if( ecp == NULL )
604 return( PSA_ERROR_INSUFFICIENT_MEMORY );
605
606 /* Load the group. */
607 status = mbedtls_to_psa_error(
608 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
609 if( status != PSA_SUCCESS )
610 goto exit;
611 /* Load the secret value. */
612 status = mbedtls_to_psa_error(
613 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
614 if( status != PSA_SUCCESS )
615 goto exit;
616 /* Validate the private key. */
617 status = mbedtls_to_psa_error(
618 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
619 if( status != PSA_SUCCESS )
620 goto exit;
621 /* Calculate the public key from the private key. */
622 status = mbedtls_to_psa_error(
623 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
624 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
625 if( status != PSA_SUCCESS )
626 goto exit;
627
628 *p_ecp = ecp;
629 return( PSA_SUCCESS );
630
631exit:
632 if( ecp != NULL )
633 {
634 mbedtls_ecp_keypair_free( ecp );
635 mbedtls_free( ecp );
636 }
637 return( status );
638}
639#endif /* defined(MBEDTLS_ECP_C) */
640
Darryl Green940d72c2018-07-13 13:18:51 +0100641static psa_status_t psa_import_key_into_slot( key_slot_t *slot,
642 const uint8_t *data,
643 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100644{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200645 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100646
Darryl Green940d72c2018-07-13 13:18:51 +0100647 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100648 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100649 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100650 if( data_length > SIZE_MAX / 8 )
651 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100652 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200653 PSA_BYTES_TO_BITS( data_length ),
654 &slot->data.raw );
655 if( status != PSA_SUCCESS )
656 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200657 if( data_length != 0 )
658 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100659 }
660 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100661#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100662 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100663 {
Darryl Green940d72c2018-07-13 13:18:51 +0100664 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100665 data, data_length,
666 &slot->data.ecp );
667 if( status != PSA_SUCCESS )
668 return( status );
669 }
670 else
671#endif /* MBEDTLS_ECP_C */
Gilles Peskine969ac722018-01-28 18:16:59 +0100672#if defined(MBEDTLS_PK_PARSE_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100673 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
674 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675 {
676 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100677 mbedtls_pk_context pk;
678 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200679
680 /* Parse the data. */
Darryl Green940d72c2018-07-13 13:18:51 +0100681 if( PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100682 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
683 else
684 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100685 if( ret != 0 )
686 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200687
688 /* We have something that the pkparse module recognizes.
689 * If it has the expected type and passes any type-specific
690 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100691#if defined(MBEDTLS_RSA_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100692 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200693 status = psa_import_rsa_key( &pk, &slot->data.rsa );
694 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100695#endif /* MBEDTLS_RSA_C */
696#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100697 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
698 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200699 &pk, &slot->data.ecp );
700 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100701#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200702 {
703 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200704 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200705
Gilles Peskinec648d692018-06-28 08:46:13 +0200706 /* Free the content of the pk object only on error. On success,
707 * the content of the object has been stored in the slot. */
708 if( status != PSA_SUCCESS )
709 {
710 mbedtls_pk_free( &pk );
711 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100712 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100713 }
714 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100715#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100716 {
717 return( PSA_ERROR_NOT_SUPPORTED );
718 }
Darryl Green940d72c2018-07-13 13:18:51 +0100719 return( PSA_SUCCESS );
720}
721
Darryl Greend49a4992018-06-18 17:27:26 +0100722#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
723static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t key,
724 key_slot_t *p_slot )
725{
726 psa_status_t status = PSA_SUCCESS;
727 uint8_t *key_data = NULL;
728 size_t key_data_length = 0;
729
730 status = psa_load_persistent_key( key, &( p_slot )->type,
731 &( p_slot )->policy, &key_data,
732 &key_data_length );
733 if( status != PSA_SUCCESS )
734 goto exit;
735 status = psa_import_key_into_slot( p_slot,
736 key_data, key_data_length );
737exit:
738 psa_free_persistent_key_data( key_data, key_data_length );
739 return( status );
740}
741#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
742
Darryl Green06fd18d2018-07-16 11:21:11 +0100743/* Retrieve a key slot, occupied or not. */
744static psa_status_t psa_get_key_slot( psa_key_slot_t key,
745 key_slot_t **p_slot )
746{
747 GUARD_MODULE_INITIALIZED;
748
749 /* 0 is not a valid slot number under any circumstance. This
750 * implementation provides slots number 1 to N where N is the
751 * number of available slots. */
752 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
753 return( PSA_ERROR_INVALID_ARGUMENT );
754
755 *p_slot = &global_data.key_slots[key - 1];
Darryl Greend49a4992018-06-18 17:27:26 +0100756
757#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
758 if( ( *p_slot )->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
759 {
760 /* There are two circumstances this can occur: the key material has
761 * not yet been created, or the key exists in storage but has not yet
762 * been loaded into memory. */
763 if( ( *p_slot )->type == PSA_KEY_TYPE_NONE )
764 {
765 psa_status_t status = PSA_SUCCESS;
766 status = psa_load_persistent_key_into_slot( key, *p_slot );
767 if( status != PSA_ERROR_EMPTY_SLOT )
768 return( status );
769 }
770 }
771#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
772
Darryl Green06fd18d2018-07-16 11:21:11 +0100773 return( PSA_SUCCESS );
774}
775
776/* Retrieve an empty key slot (slot with no key data, but possibly
777 * with some metadata such as a policy). */
778static psa_status_t psa_get_empty_key_slot( psa_key_slot_t key,
779 key_slot_t **p_slot )
780{
781 psa_status_t status;
782 key_slot_t *slot = NULL;
783
784 *p_slot = NULL;
785
786 status = psa_get_key_slot( key, &slot );
787 if( status != PSA_SUCCESS )
788 return( status );
789
790 if( slot->type != PSA_KEY_TYPE_NONE )
791 return( PSA_ERROR_OCCUPIED_SLOT );
792
793 *p_slot = slot;
794 return( status );
795}
796
797/** Retrieve a slot which must contain a key. The key must have allow all the
798 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
799 * operations with this algorithm. */
800static psa_status_t psa_get_key_from_slot( psa_key_slot_t key,
801 key_slot_t **p_slot,
802 psa_key_usage_t usage,
803 psa_algorithm_t alg )
804{
805 psa_status_t status;
806 key_slot_t *slot = NULL;
807
808 *p_slot = NULL;
809
810 status = psa_get_key_slot( key, &slot );
811 if( status != PSA_SUCCESS )
812 return( status );
813 if( slot->type == PSA_KEY_TYPE_NONE )
814 return( PSA_ERROR_EMPTY_SLOT );
815
816 /* Enforce that usage policy for the key slot contains all the flags
817 * required by the usage parameter. There is one exception: public
818 * keys can always be exported, so we treat public key objects as
819 * if they had the export flag. */
820 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
821 usage &= ~PSA_KEY_USAGE_EXPORT;
822 if( ( slot->policy.usage & usage ) != usage )
823 return( PSA_ERROR_NOT_PERMITTED );
824 if( alg != 0 && ( alg != slot->policy.alg ) )
825 return( PSA_ERROR_NOT_PERMITTED );
826
827 *p_slot = slot;
828 return( PSA_SUCCESS );
829}
Darryl Green940d72c2018-07-13 13:18:51 +0100830
Darryl Green40225ba2018-11-15 14:48:15 +0000831static psa_status_t psa_remove_key_data_from_memory( key_slot_t *slot )
832{
833 if( slot->type == PSA_KEY_TYPE_NONE )
834 {
835 /* No key material to clean. */
836 }
837 else if( key_type_is_raw_bytes( slot->type ) )
838 {
839 mbedtls_free( slot->data.raw.data );
840 }
841 else
842#if defined(MBEDTLS_RSA_C)
843 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
844 {
845 mbedtls_rsa_free( slot->data.rsa );
846 mbedtls_free( slot->data.rsa );
847 }
848 else
849#endif /* defined(MBEDTLS_RSA_C) */
850#if defined(MBEDTLS_ECP_C)
851 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
852 {
853 mbedtls_ecp_keypair_free( slot->data.ecp );
854 mbedtls_free( slot->data.ecp );
855 }
856 else
857#endif /* defined(MBEDTLS_ECP_C) */
858 {
859 /* Shouldn't happen: the key type is not any type that we
860 * put in. */
861 return( PSA_ERROR_TAMPERING_DETECTED );
862 }
863
864 return( PSA_SUCCESS );
865}
866
Darryl Green940d72c2018-07-13 13:18:51 +0100867psa_status_t psa_import_key( psa_key_slot_t key,
868 psa_key_type_t type,
869 const uint8_t *data,
870 size_t data_length )
871{
872 key_slot_t *slot;
873 psa_status_t status;
874
875 status = psa_get_empty_key_slot( key, &slot );
876 if( status != PSA_SUCCESS )
877 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100878
879 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100880
881 status = psa_import_key_into_slot( slot, data, data_length );
882 if( status != PSA_SUCCESS )
883 {
884 slot->type = PSA_KEY_TYPE_NONE;
885 return( status );
886 }
887
Darryl Greend49a4992018-06-18 17:27:26 +0100888#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
889 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
890 {
891 /* Store in file location */
892 status = psa_save_persistent_key( key, slot->type, &slot->policy, data,
893 data_length );
894 if( status != PSA_SUCCESS )
895 {
896 (void) psa_remove_key_data_from_memory( slot );
897 slot->type = PSA_KEY_TYPE_NONE;
898 }
899 }
900#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
901
902 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100903}
904
Gilles Peskine2d277862018-06-18 15:41:12 +0200905psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100906{
907 key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100908 psa_status_t status = PSA_SUCCESS;
909 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100910
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200911 status = psa_get_key_slot( key, &slot );
912 if( status != PSA_SUCCESS )
913 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100914#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
915 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
916 {
917 storage_status = psa_destroy_persistent_key( key );
918 }
919#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
920 status = psa_remove_key_data_from_memory( slot );
921 /* Zeroize the slot to wipe metadata such as policies. */
922 mbedtls_zeroize( slot, sizeof( *slot ) );
923 if( status != PSA_SUCCESS )
924 return( status );
925 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100926}
927
Gilles Peskineb870b182018-07-06 16:02:09 +0200928/* Return the size of the key in the given slot, in bits. */
929static size_t psa_get_key_bits( const key_slot_t *slot )
930{
931 if( key_type_is_raw_bytes( slot->type ) )
932 return( slot->data.raw.bytes * 8 );
933#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200934 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100935 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200936#endif /* defined(MBEDTLS_RSA_C) */
937#if defined(MBEDTLS_ECP_C)
938 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
939 return( slot->data.ecp->grp.pbits );
940#endif /* defined(MBEDTLS_ECP_C) */
941 /* Shouldn't happen except on an empty slot. */
942 return( 0 );
943}
944
Gilles Peskine2d277862018-06-18 15:41:12 +0200945psa_status_t psa_get_key_information( psa_key_slot_t key,
946 psa_key_type_t *type,
947 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100948{
949 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200950 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100951
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100952 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200953 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100954 if( bits != NULL )
955 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200956 status = psa_get_key_slot( key, &slot );
957 if( status != PSA_SUCCESS )
958 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200959
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100960 if( slot->type == PSA_KEY_TYPE_NONE )
961 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200962 if( type != NULL )
963 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200964 if( bits != NULL )
965 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100966 return( PSA_SUCCESS );
967}
968
Darryl Greendd8fb772018-11-07 16:00:44 +0000969static psa_status_t psa_internal_export_key( key_slot_t *slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200970 uint8_t *data,
971 size_t data_size,
972 size_t *data_length,
973 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100974{
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100975 *data_length = 0;
976
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200977 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300978 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300979
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200980 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100981 {
982 if( slot->data.raw.bytes > data_size )
983 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +0100984 if( data_size != 0 )
985 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200986 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +0100987 memset( data + slot->data.raw.bytes, 0,
988 data_size - slot->data.raw.bytes );
989 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100990 *data_length = slot->data.raw.bytes;
991 return( PSA_SUCCESS );
992 }
Gilles Peskine188c71e2018-10-29 19:26:02 +0100993#if defined(MBEDTLS_ECP_C)
994 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
995 {
Darryl Greendd8fb772018-11-07 16:00:44 +0000996 psa_status_t status;
997
Gilles Peskine188c71e2018-10-29 19:26:02 +0100998 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
999 if( bytes > data_size )
1000 return( PSA_ERROR_BUFFER_TOO_SMALL );
1001 status = mbedtls_to_psa_error(
1002 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1003 if( status != PSA_SUCCESS )
1004 return( status );
1005 memset( data + bytes, 0, data_size - bytes );
1006 *data_length = bytes;
1007 return( PSA_SUCCESS );
1008 }
1009#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001010 else
Moran Peker17e36e12018-05-02 12:55:20 +03001011 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001012#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001013 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001014 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001015 {
Moran Pekera998bc62018-04-16 18:16:20 +03001016 mbedtls_pk_context pk;
1017 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001018 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001019 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001020#if defined(MBEDTLS_RSA_C)
1021 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001022 pk.pk_info = &mbedtls_rsa_info;
1023 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001024#else
1025 return( PSA_ERROR_NOT_SUPPORTED );
1026#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001027 }
1028 else
1029 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001030#if defined(MBEDTLS_ECP_C)
1031 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001032 pk.pk_info = &mbedtls_eckey_info;
1033 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001034#else
1035 return( PSA_ERROR_NOT_SUPPORTED );
1036#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001037 }
Moran Pekerd7326592018-05-29 16:56:39 +03001038 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001039 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +03001040 else
1041 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +03001042 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001043 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001044 /* If data_size is 0 then data may be NULL and then the
1045 * call to memset would have undefined behavior. */
1046 if( data_size != 0 )
1047 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001048 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001049 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001050 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1051 * Move the data to the beginning and erase remaining data
1052 * at the original location. */
1053 if( 2 * (size_t) ret <= data_size )
1054 {
1055 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001056 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001057 }
1058 else if( (size_t) ret < data_size )
1059 {
1060 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001061 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001062 }
Moran Pekera998bc62018-04-16 18:16:20 +03001063 *data_length = ret;
1064 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001065 }
1066 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001067#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001068 {
1069 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001070 it is valid for a special-purpose implementation to omit
1071 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001072 return( PSA_ERROR_NOT_SUPPORTED );
1073 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001074 }
1075}
1076
Gilles Peskine2d277862018-06-18 15:41:12 +02001077psa_status_t psa_export_key( psa_key_slot_t key,
1078 uint8_t *data,
1079 size_t data_size,
1080 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001081{
Darryl Greendd8fb772018-11-07 16:00:44 +00001082 key_slot_t *slot;
1083 psa_status_t status;
1084
1085 /* Set the key to empty now, so that even when there are errors, we always
1086 * set data_length to a value between 0 and data_size. On error, setting
1087 * the key to empty is a good choice because an empty key representation is
1088 * unlikely to be accepted anywhere. */
1089 *data_length = 0;
1090
1091 /* Export requires the EXPORT flag. There is an exception for public keys,
1092 * which don't require any flag, but psa_get_key_from_slot takes
1093 * care of this. */
1094 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_EXPORT, 0 );
1095 if( status != PSA_SUCCESS )
1096 return( status );
1097 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001098 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001099}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001100
Gilles Peskine2d277862018-06-18 15:41:12 +02001101psa_status_t psa_export_public_key( psa_key_slot_t key,
1102 uint8_t *data,
1103 size_t data_size,
1104 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001105{
Darryl Greendd8fb772018-11-07 16:00:44 +00001106 key_slot_t *slot;
1107 psa_status_t status;
1108
1109 /* Set the key to empty now, so that even when there are errors, we always
1110 * set data_length to a value between 0 and data_size. On error, setting
1111 * the key to empty is a good choice because an empty key representation is
1112 * unlikely to be accepted anywhere. */
1113 *data_length = 0;
1114
1115 /* Exporting a public key doesn't require a usage flag. */
1116 status = psa_get_key_from_slot( key, &slot, 0, 0 );
1117 if( status != PSA_SUCCESS )
1118 return( status );
1119 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001120 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001121}
1122
Darryl Green0c6575a2018-11-07 16:05:30 +00001123#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1124static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t key,
1125 key_slot_t *slot,
1126 size_t bits )
1127{
1128 psa_status_t status;
1129 uint8_t *data;
1130 size_t key_length;
1131 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1132 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001133 if( data == NULL )
1134 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001135 /* Get key data in export format */
1136 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1137 if( status != PSA_SUCCESS )
1138 {
1139 slot->type = PSA_KEY_TYPE_NONE;
1140 goto exit;
1141 }
1142 /* Store in file location */
1143 status = psa_save_persistent_key( key, slot->type, &slot->policy,
1144 data, key_length );
1145 if( status != PSA_SUCCESS )
1146 {
1147 slot->type = PSA_KEY_TYPE_NONE;
1148 }
1149exit:
1150 mbedtls_zeroize( data, key_length );
1151 mbedtls_free( data );
1152 return( status );
1153}
1154#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1155
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001156
1157
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001158/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001159/* Message digests */
1160/****************************************************************/
1161
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001162static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001163{
1164 switch( alg )
1165 {
1166#if defined(MBEDTLS_MD2_C)
1167 case PSA_ALG_MD2:
1168 return( &mbedtls_md2_info );
1169#endif
1170#if defined(MBEDTLS_MD4_C)
1171 case PSA_ALG_MD4:
1172 return( &mbedtls_md4_info );
1173#endif
1174#if defined(MBEDTLS_MD5_C)
1175 case PSA_ALG_MD5:
1176 return( &mbedtls_md5_info );
1177#endif
1178#if defined(MBEDTLS_RIPEMD160_C)
1179 case PSA_ALG_RIPEMD160:
1180 return( &mbedtls_ripemd160_info );
1181#endif
1182#if defined(MBEDTLS_SHA1_C)
1183 case PSA_ALG_SHA_1:
1184 return( &mbedtls_sha1_info );
1185#endif
1186#if defined(MBEDTLS_SHA256_C)
1187 case PSA_ALG_SHA_224:
1188 return( &mbedtls_sha224_info );
1189 case PSA_ALG_SHA_256:
1190 return( &mbedtls_sha256_info );
1191#endif
1192#if defined(MBEDTLS_SHA512_C)
1193 case PSA_ALG_SHA_384:
1194 return( &mbedtls_sha384_info );
1195 case PSA_ALG_SHA_512:
1196 return( &mbedtls_sha512_info );
1197#endif
1198 default:
1199 return( NULL );
1200 }
1201}
1202
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001203psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1204{
1205 switch( operation->alg )
1206 {
Gilles Peskine81736312018-06-26 15:04:31 +02001207 case 0:
1208 /* The object has (apparently) been initialized but it is not
1209 * in use. It's ok to call abort on such an object, and there's
1210 * nothing to do. */
1211 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001212#if defined(MBEDTLS_MD2_C)
1213 case PSA_ALG_MD2:
1214 mbedtls_md2_free( &operation->ctx.md2 );
1215 break;
1216#endif
1217#if defined(MBEDTLS_MD4_C)
1218 case PSA_ALG_MD4:
1219 mbedtls_md4_free( &operation->ctx.md4 );
1220 break;
1221#endif
1222#if defined(MBEDTLS_MD5_C)
1223 case PSA_ALG_MD5:
1224 mbedtls_md5_free( &operation->ctx.md5 );
1225 break;
1226#endif
1227#if defined(MBEDTLS_RIPEMD160_C)
1228 case PSA_ALG_RIPEMD160:
1229 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1230 break;
1231#endif
1232#if defined(MBEDTLS_SHA1_C)
1233 case PSA_ALG_SHA_1:
1234 mbedtls_sha1_free( &operation->ctx.sha1 );
1235 break;
1236#endif
1237#if defined(MBEDTLS_SHA256_C)
1238 case PSA_ALG_SHA_224:
1239 case PSA_ALG_SHA_256:
1240 mbedtls_sha256_free( &operation->ctx.sha256 );
1241 break;
1242#endif
1243#if defined(MBEDTLS_SHA512_C)
1244 case PSA_ALG_SHA_384:
1245 case PSA_ALG_SHA_512:
1246 mbedtls_sha512_free( &operation->ctx.sha512 );
1247 break;
1248#endif
1249 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001250 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001251 }
1252 operation->alg = 0;
1253 return( PSA_SUCCESS );
1254}
1255
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001256psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001257 psa_algorithm_t alg )
1258{
1259 int ret;
1260 operation->alg = 0;
1261 switch( alg )
1262 {
1263#if defined(MBEDTLS_MD2_C)
1264 case PSA_ALG_MD2:
1265 mbedtls_md2_init( &operation->ctx.md2 );
1266 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1267 break;
1268#endif
1269#if defined(MBEDTLS_MD4_C)
1270 case PSA_ALG_MD4:
1271 mbedtls_md4_init( &operation->ctx.md4 );
1272 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1273 break;
1274#endif
1275#if defined(MBEDTLS_MD5_C)
1276 case PSA_ALG_MD5:
1277 mbedtls_md5_init( &operation->ctx.md5 );
1278 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1279 break;
1280#endif
1281#if defined(MBEDTLS_RIPEMD160_C)
1282 case PSA_ALG_RIPEMD160:
1283 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1284 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1285 break;
1286#endif
1287#if defined(MBEDTLS_SHA1_C)
1288 case PSA_ALG_SHA_1:
1289 mbedtls_sha1_init( &operation->ctx.sha1 );
1290 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1291 break;
1292#endif
1293#if defined(MBEDTLS_SHA256_C)
1294 case PSA_ALG_SHA_224:
1295 mbedtls_sha256_init( &operation->ctx.sha256 );
1296 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1297 break;
1298 case PSA_ALG_SHA_256:
1299 mbedtls_sha256_init( &operation->ctx.sha256 );
1300 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1301 break;
1302#endif
1303#if defined(MBEDTLS_SHA512_C)
1304 case PSA_ALG_SHA_384:
1305 mbedtls_sha512_init( &operation->ctx.sha512 );
1306 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1307 break;
1308 case PSA_ALG_SHA_512:
1309 mbedtls_sha512_init( &operation->ctx.sha512 );
1310 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1311 break;
1312#endif
1313 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001314 return( PSA_ALG_IS_HASH( alg ) ?
1315 PSA_ERROR_NOT_SUPPORTED :
1316 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001317 }
1318 if( ret == 0 )
1319 operation->alg = alg;
1320 else
1321 psa_hash_abort( operation );
1322 return( mbedtls_to_psa_error( ret ) );
1323}
1324
1325psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1326 const uint8_t *input,
1327 size_t input_length )
1328{
1329 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001330
1331 /* Don't require hash implementations to behave correctly on a
1332 * zero-length input, which may have an invalid pointer. */
1333 if( input_length == 0 )
1334 return( PSA_SUCCESS );
1335
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001336 switch( operation->alg )
1337 {
1338#if defined(MBEDTLS_MD2_C)
1339 case PSA_ALG_MD2:
1340 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1341 input, input_length );
1342 break;
1343#endif
1344#if defined(MBEDTLS_MD4_C)
1345 case PSA_ALG_MD4:
1346 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1347 input, input_length );
1348 break;
1349#endif
1350#if defined(MBEDTLS_MD5_C)
1351 case PSA_ALG_MD5:
1352 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1353 input, input_length );
1354 break;
1355#endif
1356#if defined(MBEDTLS_RIPEMD160_C)
1357 case PSA_ALG_RIPEMD160:
1358 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1359 input, input_length );
1360 break;
1361#endif
1362#if defined(MBEDTLS_SHA1_C)
1363 case PSA_ALG_SHA_1:
1364 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1365 input, input_length );
1366 break;
1367#endif
1368#if defined(MBEDTLS_SHA256_C)
1369 case PSA_ALG_SHA_224:
1370 case PSA_ALG_SHA_256:
1371 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1372 input, input_length );
1373 break;
1374#endif
1375#if defined(MBEDTLS_SHA512_C)
1376 case PSA_ALG_SHA_384:
1377 case PSA_ALG_SHA_512:
1378 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1379 input, input_length );
1380 break;
1381#endif
1382 default:
1383 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1384 break;
1385 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001386
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001387 if( ret != 0 )
1388 psa_hash_abort( operation );
1389 return( mbedtls_to_psa_error( ret ) );
1390}
1391
1392psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1393 uint8_t *hash,
1394 size_t hash_size,
1395 size_t *hash_length )
1396{
itayzafrir40835d42018-08-02 13:14:17 +03001397 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001398 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001399 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001400
1401 /* Fill the output buffer with something that isn't a valid hash
1402 * (barring an attack on the hash and deliberately-crafted input),
1403 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001404 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001405 /* If hash_size is 0 then hash may be NULL and then the
1406 * call to memset would have undefined behavior. */
1407 if( hash_size != 0 )
1408 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001409
1410 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001411 {
1412 status = PSA_ERROR_BUFFER_TOO_SMALL;
1413 goto exit;
1414 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001415
1416 switch( operation->alg )
1417 {
1418#if defined(MBEDTLS_MD2_C)
1419 case PSA_ALG_MD2:
1420 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1421 break;
1422#endif
1423#if defined(MBEDTLS_MD4_C)
1424 case PSA_ALG_MD4:
1425 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1426 break;
1427#endif
1428#if defined(MBEDTLS_MD5_C)
1429 case PSA_ALG_MD5:
1430 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1431 break;
1432#endif
1433#if defined(MBEDTLS_RIPEMD160_C)
1434 case PSA_ALG_RIPEMD160:
1435 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1436 break;
1437#endif
1438#if defined(MBEDTLS_SHA1_C)
1439 case PSA_ALG_SHA_1:
1440 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1441 break;
1442#endif
1443#if defined(MBEDTLS_SHA256_C)
1444 case PSA_ALG_SHA_224:
1445 case PSA_ALG_SHA_256:
1446 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1447 break;
1448#endif
1449#if defined(MBEDTLS_SHA512_C)
1450 case PSA_ALG_SHA_384:
1451 case PSA_ALG_SHA_512:
1452 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1453 break;
1454#endif
1455 default:
1456 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1457 break;
1458 }
itayzafrir40835d42018-08-02 13:14:17 +03001459 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001460
itayzafrir40835d42018-08-02 13:14:17 +03001461exit:
1462 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001463 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001464 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001465 return( psa_hash_abort( operation ) );
1466 }
1467 else
1468 {
1469 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001470 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001471 }
1472}
1473
Gilles Peskine2d277862018-06-18 15:41:12 +02001474psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1475 const uint8_t *hash,
1476 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001477{
1478 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1479 size_t actual_hash_length;
1480 psa_status_t status = psa_hash_finish( operation,
1481 actual_hash, sizeof( actual_hash ),
1482 &actual_hash_length );
1483 if( status != PSA_SUCCESS )
1484 return( status );
1485 if( actual_hash_length != hash_length )
1486 return( PSA_ERROR_INVALID_SIGNATURE );
1487 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1488 return( PSA_ERROR_INVALID_SIGNATURE );
1489 return( PSA_SUCCESS );
1490}
1491
1492
1493
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001494/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001495/* MAC */
1496/****************************************************************/
1497
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001498static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001499 psa_algorithm_t alg,
1500 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001501 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001502 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001503{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001504 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001505 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001506
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001507 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001508 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001509
Gilles Peskine8c9def32018-02-08 10:02:12 +01001510 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1511 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001512 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001513 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001514 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001515 mode = MBEDTLS_MODE_STREAM;
1516 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001517 case PSA_ALG_CTR:
1518 mode = MBEDTLS_MODE_CTR;
1519 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001520 case PSA_ALG_CFB:
1521 mode = MBEDTLS_MODE_CFB;
1522 break;
1523 case PSA_ALG_OFB:
1524 mode = MBEDTLS_MODE_OFB;
1525 break;
1526 case PSA_ALG_CBC_NO_PADDING:
1527 mode = MBEDTLS_MODE_CBC;
1528 break;
1529 case PSA_ALG_CBC_PKCS7:
1530 mode = MBEDTLS_MODE_CBC;
1531 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001532 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001533 mode = MBEDTLS_MODE_CCM;
1534 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001535 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001536 mode = MBEDTLS_MODE_GCM;
1537 break;
1538 default:
1539 return( NULL );
1540 }
1541 }
1542 else if( alg == PSA_ALG_CMAC )
1543 mode = MBEDTLS_MODE_ECB;
1544 else if( alg == PSA_ALG_GMAC )
1545 mode = MBEDTLS_MODE_GCM;
1546 else
1547 return( NULL );
1548
1549 switch( key_type )
1550 {
1551 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001552 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001553 break;
1554 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001555 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1556 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001557 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001558 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001559 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001560 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001561 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1562 * but two-key Triple-DES is functionally three-key Triple-DES
1563 * with K1=K3, so that's how we present it to mbedtls. */
1564 if( key_bits == 128 )
1565 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001566 break;
1567 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001568 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001569 break;
1570 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001571 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001572 break;
1573 default:
1574 return( NULL );
1575 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001576 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001577 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001578
Jaeden Amero23bbb752018-06-26 14:16:54 +01001579 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1580 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001581}
1582
Gilles Peskinea05219c2018-11-16 16:02:56 +01001583#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001584static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001585{
Gilles Peskine2d277862018-06-18 15:41:12 +02001586 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001587 {
1588 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001589 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001590 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001591 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001592 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001593 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001594 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001595 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001596 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001597 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001598 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001599 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001600 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001601 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001602 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001603 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001604 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001605 return( 128 );
1606 default:
1607 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001608 }
1609}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001610#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001611
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001612/* Initialize the MAC operation structure. Once this function has been
1613 * called, psa_mac_abort can run and will do the right thing. */
1614static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1615 psa_algorithm_t alg )
1616{
1617 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1618
1619 operation->alg = alg;
1620 operation->key_set = 0;
1621 operation->iv_set = 0;
1622 operation->iv_required = 0;
1623 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001624 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001625
1626#if defined(MBEDTLS_CMAC_C)
1627 if( alg == PSA_ALG_CMAC )
1628 {
1629 operation->iv_required = 0;
1630 mbedtls_cipher_init( &operation->ctx.cmac );
1631 status = PSA_SUCCESS;
1632 }
1633 else
1634#endif /* MBEDTLS_CMAC_C */
1635#if defined(MBEDTLS_MD_C)
1636 if( PSA_ALG_IS_HMAC( operation->alg ) )
1637 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001638 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1639 operation->ctx.hmac.hash_ctx.alg = 0;
1640 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001641 }
1642 else
1643#endif /* MBEDTLS_MD_C */
1644 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001645 if( ! PSA_ALG_IS_MAC( alg ) )
1646 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001647 }
1648
1649 if( status != PSA_SUCCESS )
1650 memset( operation, 0, sizeof( *operation ) );
1651 return( status );
1652}
1653
Gilles Peskine01126fa2018-07-12 17:04:55 +02001654#if defined(MBEDTLS_MD_C)
1655static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1656{
1657 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1658 return( psa_hash_abort( &hmac->hash_ctx ) );
1659}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001660
1661static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1662{
1663 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1664 memset( hmac, 0, sizeof( *hmac ) );
1665}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001666#endif /* MBEDTLS_MD_C */
1667
Gilles Peskine8c9def32018-02-08 10:02:12 +01001668psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1669{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001670 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001671 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001672 /* The object has (apparently) been initialized but it is not
1673 * in use. It's ok to call abort on such an object, and there's
1674 * nothing to do. */
1675 return( PSA_SUCCESS );
1676 }
1677 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001678#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001679 if( operation->alg == PSA_ALG_CMAC )
1680 {
1681 mbedtls_cipher_free( &operation->ctx.cmac );
1682 }
1683 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001684#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001685#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001686 if( PSA_ALG_IS_HMAC( operation->alg ) )
1687 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001688 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001689 }
1690 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001691#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001692 {
1693 /* Sanity check (shouldn't happen: operation->alg should
1694 * always have been initialized to a valid value). */
1695 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001696 }
Moran Peker41deec42018-04-04 15:43:05 +03001697
Gilles Peskine8c9def32018-02-08 10:02:12 +01001698 operation->alg = 0;
1699 operation->key_set = 0;
1700 operation->iv_set = 0;
1701 operation->iv_required = 0;
1702 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001703 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001704
Gilles Peskine8c9def32018-02-08 10:02:12 +01001705 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001706
1707bad_state:
1708 /* If abort is called on an uninitialized object, we can't trust
1709 * anything. Wipe the object in case it contains confidential data.
1710 * This may result in a memory leak if a pointer gets overwritten,
1711 * but it's too late to do anything about this. */
1712 memset( operation, 0, sizeof( *operation ) );
1713 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001714}
1715
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001716#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001717static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001718 size_t key_bits,
1719 key_slot_t *slot,
1720 const mbedtls_cipher_info_t *cipher_info )
1721{
1722 int ret;
1723
1724 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001725
1726 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1727 if( ret != 0 )
1728 return( ret );
1729
1730 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1731 slot->data.raw.data,
1732 key_bits );
1733 return( ret );
1734}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001735#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001736
Gilles Peskine248051a2018-06-20 16:09:38 +02001737#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001738static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1739 const uint8_t *key,
1740 size_t key_length,
1741 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001742{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001743 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001744 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001745 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001746 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001747 psa_status_t status;
1748
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001749 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1750 * overflow below. This should never trigger if the hash algorithm
1751 * is implemented correctly. */
1752 /* The size checks against the ipad and opad buffers cannot be written
1753 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1754 * because that triggers -Wlogical-op on GCC 7.3. */
1755 if( block_size > sizeof( ipad ) )
1756 return( PSA_ERROR_NOT_SUPPORTED );
1757 if( block_size > sizeof( hmac->opad ) )
1758 return( PSA_ERROR_NOT_SUPPORTED );
1759 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001760 return( PSA_ERROR_NOT_SUPPORTED );
1761
Gilles Peskined223b522018-06-11 18:12:58 +02001762 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001763 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001764 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001765 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001766 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001767 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001768 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001769 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001770 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001771 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001772 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001773 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001774 }
Gilles Peskine96889972018-07-12 17:07:03 +02001775 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1776 * but it is permitted. It is common when HMAC is used in HKDF, for
1777 * example. Don't call `memcpy` in the 0-length because `key` could be
1778 * an invalid pointer which would make the behavior undefined. */
1779 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001780 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001781
Gilles Peskined223b522018-06-11 18:12:58 +02001782 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1783 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001784 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001785 ipad[i] ^= 0x36;
1786 memset( ipad + key_length, 0x36, block_size - key_length );
1787
1788 /* Copy the key material from ipad to opad, flipping the requisite bits,
1789 * and filling the rest of opad with the requisite constant. */
1790 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001791 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1792 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001793
Gilles Peskine01126fa2018-07-12 17:04:55 +02001794 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001795 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001796 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001797
Gilles Peskine01126fa2018-07-12 17:04:55 +02001798 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001799
1800cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001801 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001802
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001803 return( status );
1804}
Gilles Peskine248051a2018-06-20 16:09:38 +02001805#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001806
Gilles Peskine89167cb2018-07-08 20:12:23 +02001807static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1808 psa_key_slot_t key,
1809 psa_algorithm_t alg,
1810 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001811{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001812 psa_status_t status;
1813 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001814 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001815 psa_key_usage_t usage =
1816 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001817 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001818 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001819
Gilles Peskined911eb72018-08-14 15:18:45 +02001820 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001821 if( status != PSA_SUCCESS )
1822 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001823 if( is_sign )
1824 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001825
Gilles Peskine89167cb2018-07-08 20:12:23 +02001826 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001827 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001828 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001829 key_bits = psa_get_key_bits( slot );
1830
Gilles Peskine8c9def32018-02-08 10:02:12 +01001831#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001832 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001833 {
1834 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001835 mbedtls_cipher_info_from_psa( full_length_alg,
1836 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001837 int ret;
1838 if( cipher_info == NULL )
1839 {
1840 status = PSA_ERROR_NOT_SUPPORTED;
1841 goto exit;
1842 }
1843 operation->mac_size = cipher_info->block_size;
1844 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1845 status = mbedtls_to_psa_error( ret );
1846 }
1847 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001848#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001849#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001850 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001851 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001852 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001853 if( hash_alg == 0 )
1854 {
1855 status = PSA_ERROR_NOT_SUPPORTED;
1856 goto exit;
1857 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001858
1859 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1860 /* Sanity check. This shouldn't fail on a valid configuration. */
1861 if( operation->mac_size == 0 ||
1862 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1863 {
1864 status = PSA_ERROR_NOT_SUPPORTED;
1865 goto exit;
1866 }
1867
Gilles Peskine01126fa2018-07-12 17:04:55 +02001868 if( slot->type != PSA_KEY_TYPE_HMAC )
1869 {
1870 status = PSA_ERROR_INVALID_ARGUMENT;
1871 goto exit;
1872 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001873
Gilles Peskine01126fa2018-07-12 17:04:55 +02001874 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1875 slot->data.raw.data,
1876 slot->data.raw.bytes,
1877 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001878 }
1879 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001880#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001881 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00001882 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02001883 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001884 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001885
Gilles Peskined911eb72018-08-14 15:18:45 +02001886 if( truncated == 0 )
1887 {
1888 /* The "normal" case: untruncated algorithm. Nothing to do. */
1889 }
1890 else if( truncated < 4 )
1891 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001892 /* A very short MAC is too short for security since it can be
1893 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1894 * so we make this our minimum, even though 32 bits is still
1895 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001896 status = PSA_ERROR_NOT_SUPPORTED;
1897 }
1898 else if( truncated > operation->mac_size )
1899 {
1900 /* It's impossible to "truncate" to a larger length. */
1901 status = PSA_ERROR_INVALID_ARGUMENT;
1902 }
1903 else
1904 operation->mac_size = truncated;
1905
Gilles Peskinefbfac682018-07-08 20:51:54 +02001906exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001907 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001908 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001909 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001910 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001911 else
1912 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001913 operation->key_set = 1;
1914 }
1915 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001916}
1917
Gilles Peskine89167cb2018-07-08 20:12:23 +02001918psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1919 psa_key_slot_t key,
1920 psa_algorithm_t alg )
1921{
1922 return( psa_mac_setup( operation, key, alg, 1 ) );
1923}
1924
1925psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1926 psa_key_slot_t key,
1927 psa_algorithm_t alg )
1928{
1929 return( psa_mac_setup( operation, key, alg, 0 ) );
1930}
1931
Gilles Peskine8c9def32018-02-08 10:02:12 +01001932psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1933 const uint8_t *input,
1934 size_t input_length )
1935{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001936 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001937 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001938 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001939 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001940 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001941 operation->has_input = 1;
1942
Gilles Peskine8c9def32018-02-08 10:02:12 +01001943#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001944 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001945 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001946 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1947 input, input_length );
1948 status = mbedtls_to_psa_error( ret );
1949 }
1950 else
1951#endif /* MBEDTLS_CMAC_C */
1952#if defined(MBEDTLS_MD_C)
1953 if( PSA_ALG_IS_HMAC( operation->alg ) )
1954 {
1955 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1956 input_length );
1957 }
1958 else
1959#endif /* MBEDTLS_MD_C */
1960 {
1961 /* This shouldn't happen if `operation` was initialized by
1962 * a setup function. */
1963 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001964 }
1965
Gilles Peskinefbfac682018-07-08 20:51:54 +02001966cleanup:
1967 if( status != PSA_SUCCESS )
1968 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001969 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001970}
1971
Gilles Peskine01126fa2018-07-12 17:04:55 +02001972#if defined(MBEDTLS_MD_C)
1973static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1974 uint8_t *mac,
1975 size_t mac_size )
1976{
1977 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1978 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1979 size_t hash_size = 0;
1980 size_t block_size = psa_get_hash_block_size( hash_alg );
1981 psa_status_t status;
1982
Gilles Peskine01126fa2018-07-12 17:04:55 +02001983 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1984 if( status != PSA_SUCCESS )
1985 return( status );
1986 /* From here on, tmp needs to be wiped. */
1987
1988 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1989 if( status != PSA_SUCCESS )
1990 goto exit;
1991
1992 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1993 if( status != PSA_SUCCESS )
1994 goto exit;
1995
1996 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1997 if( status != PSA_SUCCESS )
1998 goto exit;
1999
Gilles Peskined911eb72018-08-14 15:18:45 +02002000 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2001 if( status != PSA_SUCCESS )
2002 goto exit;
2003
2004 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002005
2006exit:
2007 mbedtls_zeroize( tmp, hash_size );
2008 return( status );
2009}
2010#endif /* MBEDTLS_MD_C */
2011
mohammad16036df908f2018-04-02 08:34:15 -07002012static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002013 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002014 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002015{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002016 if( ! operation->key_set )
2017 return( PSA_ERROR_BAD_STATE );
2018 if( operation->iv_required && ! operation->iv_set )
2019 return( PSA_ERROR_BAD_STATE );
2020
Gilles Peskine8c9def32018-02-08 10:02:12 +01002021 if( mac_size < operation->mac_size )
2022 return( PSA_ERROR_BUFFER_TOO_SMALL );
2023
Gilles Peskine8c9def32018-02-08 10:02:12 +01002024#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002025 if( operation->alg == PSA_ALG_CMAC )
2026 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002027 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2028 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2029 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002030 memcpy( mac, tmp, operation->mac_size );
Gilles Peskined911eb72018-08-14 15:18:45 +02002031 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002032 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002033 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002034 else
2035#endif /* MBEDTLS_CMAC_C */
2036#if defined(MBEDTLS_MD_C)
2037 if( PSA_ALG_IS_HMAC( operation->alg ) )
2038 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002039 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002040 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002041 }
2042 else
2043#endif /* MBEDTLS_MD_C */
2044 {
2045 /* This shouldn't happen if `operation` was initialized by
2046 * a setup function. */
2047 return( PSA_ERROR_BAD_STATE );
2048 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002049}
2050
Gilles Peskineacd4be32018-07-08 19:56:25 +02002051psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2052 uint8_t *mac,
2053 size_t mac_size,
2054 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002055{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002056 psa_status_t status;
2057
2058 /* Fill the output buffer with something that isn't a valid mac
2059 * (barring an attack on the mac and deliberately-crafted input),
2060 * in case the caller doesn't check the return status properly. */
2061 *mac_length = mac_size;
2062 /* If mac_size is 0 then mac may be NULL and then the
2063 * call to memset would have undefined behavior. */
2064 if( mac_size != 0 )
2065 memset( mac, '!', mac_size );
2066
Gilles Peskine89167cb2018-07-08 20:12:23 +02002067 if( ! operation->is_sign )
2068 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002069 status = PSA_ERROR_BAD_STATE;
2070 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002071 }
mohammad16036df908f2018-04-02 08:34:15 -07002072
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002073 status = psa_mac_finish_internal( operation, mac, mac_size );
2074
2075cleanup:
2076 if( status == PSA_SUCCESS )
2077 {
2078 status = psa_mac_abort( operation );
2079 if( status == PSA_SUCCESS )
2080 *mac_length = operation->mac_size;
2081 else
2082 memset( mac, '!', mac_size );
2083 }
2084 else
2085 psa_mac_abort( operation );
2086 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002087}
2088
Gilles Peskineacd4be32018-07-08 19:56:25 +02002089psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2090 const uint8_t *mac,
2091 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002092{
Gilles Peskine828ed142018-06-18 23:25:51 +02002093 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002094 psa_status_t status;
2095
Gilles Peskine89167cb2018-07-08 20:12:23 +02002096 if( operation->is_sign )
2097 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002098 status = PSA_ERROR_BAD_STATE;
2099 goto cleanup;
2100 }
2101 if( operation->mac_size != mac_length )
2102 {
2103 status = PSA_ERROR_INVALID_SIGNATURE;
2104 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002105 }
mohammad16036df908f2018-04-02 08:34:15 -07002106
2107 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002108 actual_mac, sizeof( actual_mac ) );
2109
2110 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2111 status = PSA_ERROR_INVALID_SIGNATURE;
2112
2113cleanup:
2114 if( status == PSA_SUCCESS )
2115 status = psa_mac_abort( operation );
2116 else
2117 psa_mac_abort( operation );
2118
Gilles Peskine99b7d6b2018-08-21 14:56:19 +02002119 mbedtls_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002120
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002121 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002122}
2123
2124
Gilles Peskine20035e32018-02-03 22:44:14 +01002125
Gilles Peskine20035e32018-02-03 22:44:14 +01002126/****************************************************************/
2127/* Asymmetric cryptography */
2128/****************************************************************/
2129
Gilles Peskine2b450e32018-06-27 15:42:46 +02002130#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002131/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002132 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002133static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2134 size_t hash_length,
2135 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002136{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002137 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002138 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002139 *md_alg = mbedtls_md_get_type( md_info );
2140
2141 /* The Mbed TLS RSA module uses an unsigned int for hash length
2142 * parameters. Validate that it fits so that we don't risk an
2143 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002144#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002145 if( hash_length > UINT_MAX )
2146 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002147#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002148
2149#if defined(MBEDTLS_PKCS1_V15)
2150 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2151 * must be correct. */
2152 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2153 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002154 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002155 if( md_info == NULL )
2156 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002157 if( mbedtls_md_get_size( md_info ) != hash_length )
2158 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002159 }
2160#endif /* MBEDTLS_PKCS1_V15 */
2161
2162#if defined(MBEDTLS_PKCS1_V21)
2163 /* PSS requires a hash internally. */
2164 if( PSA_ALG_IS_RSA_PSS( alg ) )
2165 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002166 if( md_info == NULL )
2167 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002168 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002169#endif /* MBEDTLS_PKCS1_V21 */
2170
Gilles Peskine61b91d42018-06-08 16:09:36 +02002171 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002172}
2173
Gilles Peskine2b450e32018-06-27 15:42:46 +02002174static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2175 psa_algorithm_t alg,
2176 const uint8_t *hash,
2177 size_t hash_length,
2178 uint8_t *signature,
2179 size_t signature_size,
2180 size_t *signature_length )
2181{
2182 psa_status_t status;
2183 int ret;
2184 mbedtls_md_type_t md_alg;
2185
2186 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2187 if( status != PSA_SUCCESS )
2188 return( status );
2189
Gilles Peskine630a18a2018-06-29 17:49:35 +02002190 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002191 return( PSA_ERROR_BUFFER_TOO_SMALL );
2192
2193#if defined(MBEDTLS_PKCS1_V15)
2194 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2195 {
2196 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2197 MBEDTLS_MD_NONE );
2198 ret = mbedtls_rsa_pkcs1_sign( rsa,
2199 mbedtls_ctr_drbg_random,
2200 &global_data.ctr_drbg,
2201 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002202 md_alg,
2203 (unsigned int) hash_length,
2204 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002205 signature );
2206 }
2207 else
2208#endif /* MBEDTLS_PKCS1_V15 */
2209#if defined(MBEDTLS_PKCS1_V21)
2210 if( PSA_ALG_IS_RSA_PSS( alg ) )
2211 {
2212 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2213 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2214 mbedtls_ctr_drbg_random,
2215 &global_data.ctr_drbg,
2216 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002217 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002218 (unsigned int) hash_length,
2219 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002220 signature );
2221 }
2222 else
2223#endif /* MBEDTLS_PKCS1_V21 */
2224 {
2225 return( PSA_ERROR_INVALID_ARGUMENT );
2226 }
2227
2228 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002229 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002230 return( mbedtls_to_psa_error( ret ) );
2231}
2232
2233static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2234 psa_algorithm_t alg,
2235 const uint8_t *hash,
2236 size_t hash_length,
2237 const uint8_t *signature,
2238 size_t signature_length )
2239{
2240 psa_status_t status;
2241 int ret;
2242 mbedtls_md_type_t md_alg;
2243
2244 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2245 if( status != PSA_SUCCESS )
2246 return( status );
2247
Gilles Peskine630a18a2018-06-29 17:49:35 +02002248 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002249 return( PSA_ERROR_BUFFER_TOO_SMALL );
2250
2251#if defined(MBEDTLS_PKCS1_V15)
2252 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2253 {
2254 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2255 MBEDTLS_MD_NONE );
2256 ret = mbedtls_rsa_pkcs1_verify( rsa,
2257 mbedtls_ctr_drbg_random,
2258 &global_data.ctr_drbg,
2259 MBEDTLS_RSA_PUBLIC,
2260 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002261 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002262 hash,
2263 signature );
2264 }
2265 else
2266#endif /* MBEDTLS_PKCS1_V15 */
2267#if defined(MBEDTLS_PKCS1_V21)
2268 if( PSA_ALG_IS_RSA_PSS( alg ) )
2269 {
2270 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2271 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2272 mbedtls_ctr_drbg_random,
2273 &global_data.ctr_drbg,
2274 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002275 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002276 (unsigned int) hash_length,
2277 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002278 signature );
2279 }
2280 else
2281#endif /* MBEDTLS_PKCS1_V21 */
2282 {
2283 return( PSA_ERROR_INVALID_ARGUMENT );
2284 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002285
2286 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2287 * the rest of the signature is invalid". This has little use in
2288 * practice and PSA doesn't report this distinction. */
2289 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2290 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002291 return( mbedtls_to_psa_error( ret ) );
2292}
2293#endif /* MBEDTLS_RSA_C */
2294
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002295#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002296/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2297 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2298 * (even though these functions don't modify it). */
2299static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2300 psa_algorithm_t alg,
2301 const uint8_t *hash,
2302 size_t hash_length,
2303 uint8_t *signature,
2304 size_t signature_size,
2305 size_t *signature_length )
2306{
2307 int ret;
2308 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002309 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002310 mbedtls_mpi_init( &r );
2311 mbedtls_mpi_init( &s );
2312
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002313 if( signature_size < 2 * curve_bytes )
2314 {
2315 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2316 goto cleanup;
2317 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002318
Gilles Peskinea05219c2018-11-16 16:02:56 +01002319#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002320 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2321 {
2322 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2323 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2324 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2325 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2326 hash, hash_length,
2327 md_alg ) );
2328 }
2329 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002330#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002331 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002332 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002333 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2334 hash, hash_length,
2335 mbedtls_ctr_drbg_random,
2336 &global_data.ctr_drbg ) );
2337 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002338
2339 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2340 signature,
2341 curve_bytes ) );
2342 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2343 signature + curve_bytes,
2344 curve_bytes ) );
2345
2346cleanup:
2347 mbedtls_mpi_free( &r );
2348 mbedtls_mpi_free( &s );
2349 if( ret == 0 )
2350 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002351 return( mbedtls_to_psa_error( ret ) );
2352}
2353
2354static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2355 const uint8_t *hash,
2356 size_t hash_length,
2357 const uint8_t *signature,
2358 size_t signature_length )
2359{
2360 int ret;
2361 mbedtls_mpi r, s;
2362 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2363 mbedtls_mpi_init( &r );
2364 mbedtls_mpi_init( &s );
2365
2366 if( signature_length != 2 * curve_bytes )
2367 return( PSA_ERROR_INVALID_SIGNATURE );
2368
2369 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2370 signature,
2371 curve_bytes ) );
2372 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2373 signature + curve_bytes,
2374 curve_bytes ) );
2375
2376 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2377 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002378
2379cleanup:
2380 mbedtls_mpi_free( &r );
2381 mbedtls_mpi_free( &s );
2382 return( mbedtls_to_psa_error( ret ) );
2383}
2384#endif /* MBEDTLS_ECDSA_C */
2385
Gilles Peskine61b91d42018-06-08 16:09:36 +02002386psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2387 psa_algorithm_t alg,
2388 const uint8_t *hash,
2389 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002390 uint8_t *signature,
2391 size_t signature_size,
2392 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002393{
2394 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002395 psa_status_t status;
2396
2397 *signature_length = signature_size;
2398
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002399 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2400 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002401 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002402 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002403 {
2404 status = PSA_ERROR_INVALID_ARGUMENT;
2405 goto exit;
2406 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002407
Gilles Peskine20035e32018-02-03 22:44:14 +01002408#if defined(MBEDTLS_RSA_C)
2409 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2410 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002411 status = psa_rsa_sign( slot->data.rsa,
2412 alg,
2413 hash, hash_length,
2414 signature, signature_size,
2415 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002416 }
2417 else
2418#endif /* defined(MBEDTLS_RSA_C) */
2419#if defined(MBEDTLS_ECP_C)
2420 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2421 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002422#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002423 if(
2424#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2425 PSA_ALG_IS_ECDSA( alg )
2426#else
2427 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2428#endif
2429 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002430 status = psa_ecdsa_sign( slot->data.ecp,
2431 alg,
2432 hash, hash_length,
2433 signature, signature_size,
2434 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002435 else
2436#endif /* defined(MBEDTLS_ECDSA_C) */
2437 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002438 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002439 }
itayzafrir5c753392018-05-08 11:18:38 +03002440 }
2441 else
2442#endif /* defined(MBEDTLS_ECP_C) */
2443 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002444 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002445 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002446
2447exit:
2448 /* Fill the unused part of the output buffer (the whole buffer on error,
2449 * the trailing part on success) with something that isn't a valid mac
2450 * (barring an attack on the mac and deliberately-crafted input),
2451 * in case the caller doesn't check the return status properly. */
2452 if( status == PSA_SUCCESS )
2453 memset( signature + *signature_length, '!',
2454 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002455 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002456 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002457 /* If signature_size is 0 then we have nothing to do. We must not call
2458 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002459 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002460}
2461
2462psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2463 psa_algorithm_t alg,
2464 const uint8_t *hash,
2465 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002466 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002467 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002468{
2469 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002470 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002471
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002472 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2473 if( status != PSA_SUCCESS )
2474 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002475
Gilles Peskine61b91d42018-06-08 16:09:36 +02002476#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002477 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002478 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002479 return( psa_rsa_verify( slot->data.rsa,
2480 alg,
2481 hash, hash_length,
2482 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002483 }
2484 else
2485#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002486#if defined(MBEDTLS_ECP_C)
2487 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2488 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002489#if defined(MBEDTLS_ECDSA_C)
2490 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002491 return( psa_ecdsa_verify( slot->data.ecp,
2492 hash, hash_length,
2493 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002494 else
2495#endif /* defined(MBEDTLS_ECDSA_C) */
2496 {
2497 return( PSA_ERROR_INVALID_ARGUMENT );
2498 }
itayzafrir5c753392018-05-08 11:18:38 +03002499 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002500 else
2501#endif /* defined(MBEDTLS_ECP_C) */
2502 {
2503 return( PSA_ERROR_NOT_SUPPORTED );
2504 }
2505}
2506
Gilles Peskine072ac562018-06-30 00:21:29 +02002507#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2508static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2509 mbedtls_rsa_context *rsa )
2510{
2511 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2512 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2513 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2514 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2515}
2516#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2517
Gilles Peskine61b91d42018-06-08 16:09:36 +02002518psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2519 psa_algorithm_t alg,
2520 const uint8_t *input,
2521 size_t input_length,
2522 const uint8_t *salt,
2523 size_t salt_length,
2524 uint8_t *output,
2525 size_t output_size,
2526 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002527{
2528 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002529 psa_status_t status;
2530
Darryl Green5cc689a2018-07-24 15:34:10 +01002531 (void) input;
2532 (void) input_length;
2533 (void) salt;
2534 (void) output;
2535 (void) output_size;
2536
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002537 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002538
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002539 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2540 return( PSA_ERROR_INVALID_ARGUMENT );
2541
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002542 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2543 if( status != PSA_SUCCESS )
2544 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002545 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2546 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002547 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002548
2549#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002550 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002551 {
2552 mbedtls_rsa_context *rsa = slot->data.rsa;
2553 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002554 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002555 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002556#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002557 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002558 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002559 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2560 mbedtls_ctr_drbg_random,
2561 &global_data.ctr_drbg,
2562 MBEDTLS_RSA_PUBLIC,
2563 input_length,
2564 input,
2565 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002566 }
2567 else
2568#endif /* MBEDTLS_PKCS1_V15 */
2569#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002570 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002571 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002572 psa_rsa_oaep_set_padding_mode( alg, rsa );
2573 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2574 mbedtls_ctr_drbg_random,
2575 &global_data.ctr_drbg,
2576 MBEDTLS_RSA_PUBLIC,
2577 salt, salt_length,
2578 input_length,
2579 input,
2580 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002581 }
2582 else
2583#endif /* MBEDTLS_PKCS1_V21 */
2584 {
2585 return( PSA_ERROR_INVALID_ARGUMENT );
2586 }
2587 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002588 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002589 return( mbedtls_to_psa_error( ret ) );
2590 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002591 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002592#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002593 {
2594 return( PSA_ERROR_NOT_SUPPORTED );
2595 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002596}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002597
Gilles Peskine61b91d42018-06-08 16:09:36 +02002598psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2599 psa_algorithm_t alg,
2600 const uint8_t *input,
2601 size_t input_length,
2602 const uint8_t *salt,
2603 size_t salt_length,
2604 uint8_t *output,
2605 size_t output_size,
2606 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002607{
2608 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002609 psa_status_t status;
2610
Darryl Green5cc689a2018-07-24 15:34:10 +01002611 (void) input;
2612 (void) input_length;
2613 (void) salt;
2614 (void) output;
2615 (void) output_size;
2616
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002617 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002618
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002619 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2620 return( PSA_ERROR_INVALID_ARGUMENT );
2621
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002622 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2623 if( status != PSA_SUCCESS )
2624 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002625 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2626 return( PSA_ERROR_INVALID_ARGUMENT );
2627
2628#if defined(MBEDTLS_RSA_C)
2629 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2630 {
2631 mbedtls_rsa_context *rsa = slot->data.rsa;
2632 int ret;
2633
Gilles Peskine630a18a2018-06-29 17:49:35 +02002634 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002635 return( PSA_ERROR_INVALID_ARGUMENT );
2636
2637#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002638 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002639 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002640 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2641 mbedtls_ctr_drbg_random,
2642 &global_data.ctr_drbg,
2643 MBEDTLS_RSA_PRIVATE,
2644 output_length,
2645 input,
2646 output,
2647 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002648 }
2649 else
2650#endif /* MBEDTLS_PKCS1_V15 */
2651#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002652 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002653 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002654 psa_rsa_oaep_set_padding_mode( alg, rsa );
2655 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2656 mbedtls_ctr_drbg_random,
2657 &global_data.ctr_drbg,
2658 MBEDTLS_RSA_PRIVATE,
2659 salt, salt_length,
2660 output_length,
2661 input,
2662 output,
2663 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002664 }
2665 else
2666#endif /* MBEDTLS_PKCS1_V21 */
2667 {
2668 return( PSA_ERROR_INVALID_ARGUMENT );
2669 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002670
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002671 return( mbedtls_to_psa_error( ret ) );
2672 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002673 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002674#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002675 {
2676 return( PSA_ERROR_NOT_SUPPORTED );
2677 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002678}
Gilles Peskine20035e32018-02-03 22:44:14 +01002679
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002680
2681
mohammad1603503973b2018-03-12 15:59:30 +02002682/****************************************************************/
2683/* Symmetric cryptography */
2684/****************************************************************/
2685
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002686/* Initialize the cipher operation structure. Once this function has been
2687 * called, psa_cipher_abort can run and will do the right thing. */
2688static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2689 psa_algorithm_t alg )
2690{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002691 if( ! PSA_ALG_IS_CIPHER( alg ) )
2692 {
2693 memset( operation, 0, sizeof( *operation ) );
2694 return( PSA_ERROR_INVALID_ARGUMENT );
2695 }
2696
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002697 operation->alg = alg;
2698 operation->key_set = 0;
2699 operation->iv_set = 0;
2700 operation->iv_required = 1;
2701 operation->iv_size = 0;
2702 operation->block_size = 0;
2703 mbedtls_cipher_init( &operation->ctx.cipher );
2704 return( PSA_SUCCESS );
2705}
2706
Gilles Peskinee553c652018-06-04 16:22:46 +02002707static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2708 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002709 psa_algorithm_t alg,
2710 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002711{
2712 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2713 psa_status_t status;
2714 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002715 size_t key_bits;
2716 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002717 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2718 PSA_KEY_USAGE_ENCRYPT :
2719 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002720
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002721 status = psa_cipher_init( operation, alg );
2722 if( status != PSA_SUCCESS )
2723 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002724
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002725 status = psa_get_key_from_slot( key, &slot, usage, alg);
2726 if( status != PSA_SUCCESS )
2727 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002728 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002729
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002730 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002731 if( cipher_info == NULL )
2732 return( PSA_ERROR_NOT_SUPPORTED );
2733
mohammad1603503973b2018-03-12 15:59:30 +02002734 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002735 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002736 {
2737 psa_cipher_abort( operation );
2738 return( mbedtls_to_psa_error( ret ) );
2739 }
2740
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002741#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002742 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002743 {
2744 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2745 unsigned char keys[24];
2746 memcpy( keys, slot->data.raw.data, 16 );
2747 memcpy( keys + 16, slot->data.raw.data, 8 );
2748 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2749 keys,
2750 192, cipher_operation );
2751 }
2752 else
2753#endif
2754 {
2755 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2756 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002757 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002758 }
Moran Peker41deec42018-04-04 15:43:05 +03002759 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002760 {
2761 psa_cipher_abort( operation );
2762 return( mbedtls_to_psa_error( ret ) );
2763 }
2764
mohammad16038481e742018-03-18 13:57:31 +02002765#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002766 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002767 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002768 case PSA_ALG_CBC_NO_PADDING:
2769 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2770 MBEDTLS_PADDING_NONE );
2771 break;
2772 case PSA_ALG_CBC_PKCS7:
2773 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2774 MBEDTLS_PADDING_PKCS7 );
2775 break;
2776 default:
2777 /* The algorithm doesn't involve padding. */
2778 ret = 0;
2779 break;
2780 }
2781 if( ret != 0 )
2782 {
2783 psa_cipher_abort( operation );
2784 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002785 }
2786#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2787
mohammad1603503973b2018-03-12 15:59:30 +02002788 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002789 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2790 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2791 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002792 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002793 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002794 }
mohammad1603503973b2018-03-12 15:59:30 +02002795
Moran Peker395db872018-05-31 14:07:14 +03002796 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002797}
2798
Gilles Peskinefe119512018-07-08 21:39:34 +02002799psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2800 psa_key_slot_t key,
2801 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002802{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002803 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002804}
2805
Gilles Peskinefe119512018-07-08 21:39:34 +02002806psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2807 psa_key_slot_t key,
2808 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002809{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002810 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002811}
2812
Gilles Peskinefe119512018-07-08 21:39:34 +02002813psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2814 unsigned char *iv,
2815 size_t iv_size,
2816 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002817{
itayzafrir534bd7c2018-08-02 13:56:32 +03002818 psa_status_t status;
2819 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002820 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002821 {
2822 status = PSA_ERROR_BAD_STATE;
2823 goto exit;
2824 }
Moran Peker41deec42018-04-04 15:43:05 +03002825 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002826 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002827 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002828 goto exit;
2829 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002830 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2831 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002832 if( ret != 0 )
2833 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002834 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002835 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002836 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002837
mohammad16038481e742018-03-18 13:57:31 +02002838 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002839 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002840
Moran Peker395db872018-05-31 14:07:14 +03002841exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002842 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002843 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002844 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002845}
2846
Gilles Peskinefe119512018-07-08 21:39:34 +02002847psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2848 const unsigned char *iv,
2849 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002850{
itayzafrir534bd7c2018-08-02 13:56:32 +03002851 psa_status_t status;
2852 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002853 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002854 {
2855 status = PSA_ERROR_BAD_STATE;
2856 goto exit;
2857 }
Moran Pekera28258c2018-05-29 16:25:04 +03002858 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002859 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002860 status = PSA_ERROR_INVALID_ARGUMENT;
2861 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002862 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002863 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2864 status = mbedtls_to_psa_error( ret );
2865exit:
2866 if( status == PSA_SUCCESS )
2867 operation->iv_set = 1;
2868 else
mohammad1603503973b2018-03-12 15:59:30 +02002869 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002870 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002871}
2872
Gilles Peskinee553c652018-06-04 16:22:46 +02002873psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2874 const uint8_t *input,
2875 size_t input_length,
2876 unsigned char *output,
2877 size_t output_size,
2878 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002879{
itayzafrir534bd7c2018-08-02 13:56:32 +03002880 psa_status_t status;
2881 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002882 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002883 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002884 {
2885 /* Take the unprocessed partial block left over from previous
2886 * update calls, if any, plus the input to this call. Remove
2887 * the last partial block, if any. You get the data that will be
2888 * output in this call. */
2889 expected_output_size =
2890 ( operation->ctx.cipher.unprocessed_len + input_length )
2891 / operation->block_size * operation->block_size;
2892 }
2893 else
2894 {
2895 expected_output_size = input_length;
2896 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002897
Gilles Peskine89d789c2018-06-04 17:17:16 +02002898 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002899 {
2900 status = PSA_ERROR_BUFFER_TOO_SMALL;
2901 goto exit;
2902 }
mohammad160382759612018-03-12 18:16:40 +02002903
mohammad1603503973b2018-03-12 15:59:30 +02002904 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002905 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002906 status = mbedtls_to_psa_error( ret );
2907exit:
2908 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002909 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002910 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002911}
2912
Gilles Peskinee553c652018-06-04 16:22:46 +02002913psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2914 uint8_t *output,
2915 size_t output_size,
2916 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002917{
Janos Follath315b51c2018-07-09 16:04:51 +01002918 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2919 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002920 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002921
mohammad1603503973b2018-03-12 15:59:30 +02002922 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002923 {
Janos Follath315b51c2018-07-09 16:04:51 +01002924 status = PSA_ERROR_BAD_STATE;
2925 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002926 }
2927 if( operation->iv_required && ! operation->iv_set )
2928 {
Janos Follath315b51c2018-07-09 16:04:51 +01002929 status = PSA_ERROR_BAD_STATE;
2930 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002931 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002932
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002933 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002934 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2935 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002936 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002937 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002938 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002939 }
2940
Janos Follath315b51c2018-07-09 16:04:51 +01002941 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2942 temp_output_buffer,
2943 output_length );
2944 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002945 {
Janos Follath315b51c2018-07-09 16:04:51 +01002946 status = mbedtls_to_psa_error( cipher_ret );
2947 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002948 }
Janos Follath315b51c2018-07-09 16:04:51 +01002949
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002950 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002951 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002952 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002953 memcpy( output, temp_output_buffer, *output_length );
2954 else
2955 {
Janos Follath315b51c2018-07-09 16:04:51 +01002956 status = PSA_ERROR_BUFFER_TOO_SMALL;
2957 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002958 }
mohammad1603503973b2018-03-12 15:59:30 +02002959
Janos Follath279ab8e2018-07-09 16:13:21 +01002960 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002961 status = psa_cipher_abort( operation );
2962
2963 return( status );
2964
2965error:
2966
2967 *output_length = 0;
2968
Janos Follath279ab8e2018-07-09 16:13:21 +01002969 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002970 (void) psa_cipher_abort( operation );
2971
2972 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002973}
2974
Gilles Peskinee553c652018-06-04 16:22:46 +02002975psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2976{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002977 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002978 {
2979 /* The object has (apparently) been initialized but it is not
2980 * in use. It's ok to call abort on such an object, and there's
2981 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002982 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002983 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002984
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002985 /* Sanity check (shouldn't happen: operation->alg should
2986 * always have been initialized to a valid value). */
2987 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2988 return( PSA_ERROR_BAD_STATE );
2989
mohammad1603503973b2018-03-12 15:59:30 +02002990 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002991
Moran Peker41deec42018-04-04 15:43:05 +03002992 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002993 operation->key_set = 0;
2994 operation->iv_set = 0;
2995 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002996 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002997 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002998
Moran Peker395db872018-05-31 14:07:14 +03002999 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003000}
3001
Gilles Peskinea0655c32018-04-30 17:06:50 +02003002
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003003
mohammad16038cc1cee2018-03-28 01:21:33 +03003004/****************************************************************/
3005/* Key Policy */
3006/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003007
mohammad160327010052018-07-03 13:16:15 +03003008#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003009void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003010{
Gilles Peskine803ce742018-06-18 16:07:14 +02003011 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003012}
3013
Gilles Peskine2d277862018-06-18 15:41:12 +02003014void psa_key_policy_set_usage( psa_key_policy_t *policy,
3015 psa_key_usage_t usage,
3016 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003017{
mohammad16034eed7572018-03-28 05:14:59 -07003018 policy->usage = usage;
3019 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003020}
3021
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003022psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003023{
mohammad16036df908f2018-04-02 08:34:15 -07003024 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003025}
3026
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003027psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003028{
mohammad16036df908f2018-04-02 08:34:15 -07003029 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003030}
mohammad160327010052018-07-03 13:16:15 +03003031#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003032
Gilles Peskine2d277862018-06-18 15:41:12 +02003033psa_status_t psa_set_key_policy( psa_key_slot_t key,
3034 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003035{
3036 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003037 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003038
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003039 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003040 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003041
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003042 status = psa_get_empty_key_slot( key, &slot );
3043 if( status != PSA_SUCCESS )
3044 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003045
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003046 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3047 PSA_KEY_USAGE_ENCRYPT |
3048 PSA_KEY_USAGE_DECRYPT |
3049 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003050 PSA_KEY_USAGE_VERIFY |
3051 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003052 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003053
mohammad16036df908f2018-04-02 08:34:15 -07003054 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003055
3056 return( PSA_SUCCESS );
3057}
3058
Gilles Peskine2d277862018-06-18 15:41:12 +02003059psa_status_t psa_get_key_policy( psa_key_slot_t key,
3060 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003061{
3062 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003063 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003064
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003065 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003066 return( PSA_ERROR_INVALID_ARGUMENT );
3067
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003068 status = psa_get_key_slot( key, &slot );
3069 if( status != PSA_SUCCESS )
3070 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003071
mohammad16036df908f2018-04-02 08:34:15 -07003072 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003073
3074 return( PSA_SUCCESS );
3075}
Gilles Peskine20035e32018-02-03 22:44:14 +01003076
Gilles Peskinea0655c32018-04-30 17:06:50 +02003077
3078
mohammad1603804cd712018-03-20 22:44:08 +02003079/****************************************************************/
3080/* Key Lifetime */
3081/****************************************************************/
3082
Gilles Peskine2d277862018-06-18 15:41:12 +02003083psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
3084 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003085{
3086 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003087 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003088
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003089 status = psa_get_key_slot( key, &slot );
3090 if( status != PSA_SUCCESS )
3091 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003092
mohammad1603804cd712018-03-20 22:44:08 +02003093 *lifetime = slot->lifetime;
3094
3095 return( PSA_SUCCESS );
3096}
3097
Gilles Peskine2d277862018-06-18 15:41:12 +02003098psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01003099 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003100{
3101 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003102 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003103
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003104 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
3105 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
Darryl Greend49a4992018-06-18 17:27:26 +01003106 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003107 return( PSA_ERROR_INVALID_ARGUMENT );
3108
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003109 status = psa_get_empty_key_slot( key, &slot );
3110 if( status != PSA_SUCCESS )
3111 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02003112
Darryl Greend49a4992018-06-18 17:27:26 +01003113 if( lifetime == PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003114 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003115
Darryl Greend49a4992018-06-18 17:27:26 +01003116#if !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
3117 if( lifetime == PSA_KEY_LIFETIME_PERSISTENT )
3118 return( PSA_ERROR_NOT_SUPPORTED );
3119#endif
3120
mohammad1603060ad8a2018-03-20 14:28:38 -07003121 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02003122
3123 return( PSA_SUCCESS );
3124}
3125
Gilles Peskine20035e32018-02-03 22:44:14 +01003126
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003127
mohammad16035955c982018-04-26 00:53:03 +03003128/****************************************************************/
3129/* AEAD */
3130/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003131
Gilles Peskineedf9a652018-08-17 18:11:56 +02003132typedef struct
3133{
3134 key_slot_t *slot;
3135 const mbedtls_cipher_info_t *cipher_info;
3136 union
3137 {
3138#if defined(MBEDTLS_CCM_C)
3139 mbedtls_ccm_context ccm;
3140#endif /* MBEDTLS_CCM_C */
3141#if defined(MBEDTLS_GCM_C)
3142 mbedtls_gcm_context gcm;
3143#endif /* MBEDTLS_GCM_C */
3144 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003145 psa_algorithm_t core_alg;
3146 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003147 uint8_t tag_length;
3148} aead_operation_t;
3149
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003150static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003151{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003152 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003153 {
3154#if defined(MBEDTLS_CCM_C)
3155 case PSA_ALG_CCM:
3156 mbedtls_ccm_free( &operation->ctx.ccm );
3157 break;
3158#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003159#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003160 case PSA_ALG_GCM:
3161 mbedtls_gcm_free( &operation->ctx.gcm );
3162 break;
3163#endif /* MBEDTLS_GCM_C */
3164 }
3165}
3166
3167static psa_status_t psa_aead_setup( aead_operation_t *operation,
3168 psa_key_slot_t key,
3169 psa_key_usage_t usage,
3170 psa_algorithm_t alg )
3171{
3172 psa_status_t status;
3173 size_t key_bits;
3174 mbedtls_cipher_id_t cipher_id;
3175
3176 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
3177 if( status != PSA_SUCCESS )
3178 return( status );
3179
3180 key_bits = psa_get_key_bits( operation->slot );
3181
3182 operation->cipher_info =
3183 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3184 &cipher_id );
3185 if( operation->cipher_info == NULL )
3186 return( PSA_ERROR_NOT_SUPPORTED );
3187
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003188 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003189 {
3190#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003191 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3192 operation->core_alg = PSA_ALG_CCM;
3193 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003194 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3195 return( PSA_ERROR_INVALID_ARGUMENT );
3196 mbedtls_ccm_init( &operation->ctx.ccm );
3197 status = mbedtls_to_psa_error(
3198 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3199 operation->slot->data.raw.data,
3200 (unsigned int) key_bits ) );
3201 if( status != 0 )
3202 goto cleanup;
3203 break;
3204#endif /* MBEDTLS_CCM_C */
3205
3206#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003207 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3208 operation->core_alg = PSA_ALG_GCM;
3209 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003210 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3211 return( PSA_ERROR_INVALID_ARGUMENT );
3212 mbedtls_gcm_init( &operation->ctx.gcm );
3213 status = mbedtls_to_psa_error(
3214 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3215 operation->slot->data.raw.data,
3216 (unsigned int) key_bits ) );
3217 break;
3218#endif /* MBEDTLS_GCM_C */
3219
3220 default:
3221 return( PSA_ERROR_NOT_SUPPORTED );
3222 }
3223
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003224 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3225 {
3226 status = PSA_ERROR_INVALID_ARGUMENT;
3227 goto cleanup;
3228 }
3229 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3230 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3231 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3232 * In both cases, mbedtls_xxx will validate the tag length below. */
3233
Gilles Peskineedf9a652018-08-17 18:11:56 +02003234 return( PSA_SUCCESS );
3235
3236cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003237 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003238 return( status );
3239}
3240
mohammad16035955c982018-04-26 00:53:03 +03003241psa_status_t psa_aead_encrypt( psa_key_slot_t key,
3242 psa_algorithm_t alg,
3243 const uint8_t *nonce,
3244 size_t nonce_length,
3245 const uint8_t *additional_data,
3246 size_t additional_data_length,
3247 const uint8_t *plaintext,
3248 size_t plaintext_length,
3249 uint8_t *ciphertext,
3250 size_t ciphertext_size,
3251 size_t *ciphertext_length )
3252{
mohammad16035955c982018-04-26 00:53:03 +03003253 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003254 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003255 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003256
mohammad1603f08a5502018-06-03 15:05:47 +03003257 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003258
Gilles Peskineedf9a652018-08-17 18:11:56 +02003259 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003260 if( status != PSA_SUCCESS )
3261 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003262
Gilles Peskineedf9a652018-08-17 18:11:56 +02003263 /* For all currently supported modes, the tag is at the end of the
3264 * ciphertext. */
3265 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3266 {
3267 status = PSA_ERROR_BUFFER_TOO_SMALL;
3268 goto exit;
3269 }
3270 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003271
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003272#if defined(MBEDTLS_GCM_C)
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 Peskineb0b189f2018-11-28 17:30:58 +01003284 else
3285#endif /* MBEDTLS_GCM_C */
3286#if defined(MBEDTLS_CCM_C)
3287 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003288 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003289 status = mbedtls_to_psa_error(
3290 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3291 plaintext_length,
3292 nonce, nonce_length,
3293 additional_data,
3294 additional_data_length,
3295 plaintext, ciphertext,
3296 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003297 }
mohammad16035c8845f2018-05-09 05:40:09 -07003298 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003299#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003300 {
mohammad1603554faad2018-06-03 15:07:38 +03003301 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003302 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003303
Gilles Peskineedf9a652018-08-17 18:11:56 +02003304 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3305 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003306
Gilles Peskineedf9a652018-08-17 18:11:56 +02003307exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003308 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003309 if( status == PSA_SUCCESS )
3310 *ciphertext_length = plaintext_length + operation.tag_length;
3311 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003312}
3313
Gilles Peskineee652a32018-06-01 19:23:52 +02003314/* Locate the tag in a ciphertext buffer containing the encrypted data
3315 * followed by the tag. Return the length of the part preceding the tag in
3316 * *plaintext_length. This is the size of the plaintext in modes where
3317 * the encrypted data has the same size as the plaintext, such as
3318 * CCM and GCM. */
3319static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3320 const uint8_t *ciphertext,
3321 size_t ciphertext_length,
3322 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003323 const uint8_t **p_tag )
3324{
3325 size_t payload_length;
3326 if( tag_length > ciphertext_length )
3327 return( PSA_ERROR_INVALID_ARGUMENT );
3328 payload_length = ciphertext_length - tag_length;
3329 if( payload_length > plaintext_size )
3330 return( PSA_ERROR_BUFFER_TOO_SMALL );
3331 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003332 return( PSA_SUCCESS );
3333}
3334
mohammad16035955c982018-04-26 00:53:03 +03003335psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3336 psa_algorithm_t alg,
3337 const uint8_t *nonce,
3338 size_t nonce_length,
3339 const uint8_t *additional_data,
3340 size_t additional_data_length,
3341 const uint8_t *ciphertext,
3342 size_t ciphertext_length,
3343 uint8_t *plaintext,
3344 size_t plaintext_size,
3345 size_t *plaintext_length )
3346{
mohammad16035955c982018-04-26 00:53:03 +03003347 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003348 aead_operation_t operation;
3349 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003350
Gilles Peskineee652a32018-06-01 19:23:52 +02003351 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003352
Gilles Peskineedf9a652018-08-17 18:11:56 +02003353 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003354 if( status != PSA_SUCCESS )
3355 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003356
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003357#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003358 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003359 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003360 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003361 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003362 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003363 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003364 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003365
Gilles Peskineedf9a652018-08-17 18:11:56 +02003366 status = mbedtls_to_psa_error(
3367 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3368 ciphertext_length - operation.tag_length,
3369 nonce, nonce_length,
3370 additional_data,
3371 additional_data_length,
3372 tag, operation.tag_length,
3373 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003374 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003375 else
3376#endif /* MBEDTLS_GCM_C */
3377#if defined(MBEDTLS_CCM_C)
3378 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003379 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003380 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003381 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003382 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003383 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003384 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003385
Gilles Peskineedf9a652018-08-17 18:11:56 +02003386 status = mbedtls_to_psa_error(
3387 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3388 ciphertext_length - operation.tag_length,
3389 nonce, nonce_length,
3390 additional_data,
3391 additional_data_length,
3392 ciphertext, plaintext,
3393 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003394 }
mohammad160339574652018-06-01 04:39:53 -07003395 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003396#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003397 {
mohammad1603554faad2018-06-03 15:07:38 +03003398 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003399 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003400
Gilles Peskineedf9a652018-08-17 18:11:56 +02003401 if( status != PSA_SUCCESS && plaintext_size != 0 )
3402 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003403
Gilles Peskineedf9a652018-08-17 18:11:56 +02003404exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003405 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003406 if( status == PSA_SUCCESS )
3407 *plaintext_length = ciphertext_length - operation.tag_length;
3408 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003409}
3410
Gilles Peskinea0655c32018-04-30 17:06:50 +02003411
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003412
Gilles Peskine20035e32018-02-03 22:44:14 +01003413/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003414/* Generators */
3415/****************************************************************/
3416
3417psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3418{
3419 psa_status_t status = PSA_SUCCESS;
3420 if( generator->alg == 0 )
3421 {
3422 /* The object has (apparently) been initialized but it is not
3423 * in use. It's ok to call abort on such an object, and there's
3424 * nothing to do. */
3425 }
3426 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003427 if( generator->alg == PSA_ALG_SELECT_RAW )
3428 {
3429 if( generator->ctx.buffer.data != NULL )
3430 {
3431 mbedtls_zeroize( generator->ctx.buffer.data,
3432 generator->ctx.buffer.size );
3433 mbedtls_free( generator->ctx.buffer.data );
3434 }
3435 }
3436 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003437#if defined(MBEDTLS_MD_C)
3438 if( PSA_ALG_IS_HKDF( generator->alg ) )
3439 {
3440 mbedtls_free( generator->ctx.hkdf.info );
3441 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3442 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003443 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3444 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3445 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003446 {
3447 if( generator->ctx.tls12_prf.key != NULL )
3448 {
3449 mbedtls_zeroize( generator->ctx.tls12_prf.key,
3450 generator->ctx.tls12_prf.key_len );
3451 mbedtls_free( generator->ctx.tls12_prf.key );
3452 }
Hanno Becker580fba12018-11-13 20:50:45 +00003453
3454 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3455 {
3456 mbedtls_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
3457 generator->ctx.tls12_prf.Ai_with_seed_len );
3458 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3459 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003460 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003461 else
3462#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003463 {
3464 status = PSA_ERROR_BAD_STATE;
3465 }
3466 memset( generator, 0, sizeof( *generator ) );
3467 return( status );
3468}
3469
3470
3471psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3472 size_t *capacity)
3473{
3474 *capacity = generator->capacity;
3475 return( PSA_SUCCESS );
3476}
3477
Gilles Peskinebef7f142018-07-12 17:22:21 +02003478#if defined(MBEDTLS_MD_C)
3479/* Read some bytes from an HKDF-based generator. This performs a chunk
3480 * of the expand phase of the HKDF algorithm. */
3481static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3482 psa_algorithm_t hash_alg,
3483 uint8_t *output,
3484 size_t output_length )
3485{
3486 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3487 psa_status_t status;
3488
3489 while( output_length != 0 )
3490 {
3491 /* Copy what remains of the current block */
3492 uint8_t n = hash_length - hkdf->offset_in_block;
3493 if( n > output_length )
3494 n = (uint8_t) output_length;
3495 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3496 output += n;
3497 output_length -= n;
3498 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003499 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003500 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003501 /* We can't be wanting more output after block 0xff, otherwise
3502 * the capacity check in psa_generator_read() would have
3503 * prevented this call. It could happen only if the generator
3504 * object was corrupted or if this function is called directly
3505 * inside the library. */
3506 if( hkdf->block_number == 0xff )
3507 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003508
3509 /* We need a new block */
3510 ++hkdf->block_number;
3511 hkdf->offset_in_block = 0;
3512 status = psa_hmac_setup_internal( &hkdf->hmac,
3513 hkdf->prk, hash_length,
3514 hash_alg );
3515 if( status != PSA_SUCCESS )
3516 return( status );
3517 if( hkdf->block_number != 1 )
3518 {
3519 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3520 hkdf->output_block,
3521 hash_length );
3522 if( status != PSA_SUCCESS )
3523 return( status );
3524 }
3525 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3526 hkdf->info,
3527 hkdf->info_length );
3528 if( status != PSA_SUCCESS )
3529 return( status );
3530 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3531 &hkdf->block_number, 1 );
3532 if( status != PSA_SUCCESS )
3533 return( status );
3534 status = psa_hmac_finish_internal( &hkdf->hmac,
3535 hkdf->output_block,
3536 sizeof( hkdf->output_block ) );
3537 if( status != PSA_SUCCESS )
3538 return( status );
3539 }
3540
3541 return( PSA_SUCCESS );
3542}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003543
3544static psa_status_t psa_generator_tls12_prf_generate_next_block(
3545 psa_tls12_prf_generator_t *tls12_prf,
3546 psa_algorithm_t alg )
3547{
3548 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3549 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3550 psa_hmac_internal_data hmac;
3551 psa_status_t status, cleanup_status;
3552
Hanno Becker3b339e22018-11-13 20:56:14 +00003553 unsigned char *Ai;
3554 size_t Ai_len;
3555
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003556 /* We can't be wanting more output after block 0xff, otherwise
3557 * the capacity check in psa_generator_read() would have
3558 * prevented this call. It could happen only if the generator
3559 * object was corrupted or if this function is called directly
3560 * inside the library. */
3561 if( tls12_prf->block_number == 0xff )
3562 return( PSA_ERROR_BAD_STATE );
3563
3564 /* We need a new block */
3565 ++tls12_prf->block_number;
3566 tls12_prf->offset_in_block = 0;
3567
3568 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3569 *
3570 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3571 *
3572 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3573 * HMAC_hash(secret, A(2) + seed) +
3574 * HMAC_hash(secret, A(3) + seed) + ...
3575 *
3576 * A(0) = seed
3577 * A(i) = HMAC_hash( secret, A(i-1) )
3578 *
3579 * The `psa_tls12_prf_generator` structures saves the block
3580 * `HMAC_hash(secret, A(i) + seed)` from which the output
3581 * is currently extracted as `output_block`, while
3582 * `A(i) + seed` is stored in `Ai_with_seed`.
3583 *
3584 * Generating a new block means recalculating `Ai_with_seed`
3585 * from the A(i)-part of it, and afterwards recalculating
3586 * `output_block`.
3587 *
3588 * A(0) is computed at setup time.
3589 *
3590 */
3591
3592 psa_hmac_init_internal( &hmac );
3593
3594 /* We must distinguish the calculation of A(1) from those
3595 * of A(2) and higher, because A(0)=seed has a different
3596 * length than the other A(i). */
3597 if( tls12_prf->block_number == 1 )
3598 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003599 Ai = tls12_prf->Ai_with_seed + hash_length;
3600 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003601 }
3602 else
3603 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003604 Ai = tls12_prf->Ai_with_seed;
3605 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003606 }
3607
Hanno Becker3b339e22018-11-13 20:56:14 +00003608 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3609 status = psa_hmac_setup_internal( &hmac,
3610 tls12_prf->key,
3611 tls12_prf->key_len,
3612 hash_alg );
3613 if( status != PSA_SUCCESS )
3614 goto cleanup;
3615
3616 status = psa_hash_update( &hmac.hash_ctx,
3617 Ai, Ai_len );
3618 if( status != PSA_SUCCESS )
3619 goto cleanup;
3620
3621 status = psa_hmac_finish_internal( &hmac,
3622 tls12_prf->Ai_with_seed,
3623 hash_length );
3624 if( status != PSA_SUCCESS )
3625 goto cleanup;
3626
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003627 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3628 status = psa_hmac_setup_internal( &hmac,
3629 tls12_prf->key,
3630 tls12_prf->key_len,
3631 hash_alg );
3632 if( status != PSA_SUCCESS )
3633 goto cleanup;
3634
3635 status = psa_hash_update( &hmac.hash_ctx,
3636 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003637 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003638 if( status != PSA_SUCCESS )
3639 goto cleanup;
3640
3641 status = psa_hmac_finish_internal( &hmac,
3642 tls12_prf->output_block,
3643 hash_length );
3644 if( status != PSA_SUCCESS )
3645 goto cleanup;
3646
3647cleanup:
3648
3649 cleanup_status = psa_hmac_abort_internal( &hmac );
3650 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3651 status = cleanup_status;
3652
3653 return( status );
3654}
3655
3656/* Read some bytes from an TLS-1.2-PRF-based generator.
3657 * See Section 5 of RFC 5246. */
3658static psa_status_t psa_generator_tls12_prf_read(
3659 psa_tls12_prf_generator_t *tls12_prf,
3660 psa_algorithm_t alg,
3661 uint8_t *output,
3662 size_t output_length )
3663{
3664 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3665 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3666 psa_status_t status;
3667
3668 while( output_length != 0 )
3669 {
3670 /* Copy what remains of the current block */
3671 uint8_t n = hash_length - tls12_prf->offset_in_block;
3672
3673 /* Check if we have fully processed the current block. */
3674 if( n == 0 )
3675 {
3676 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3677 alg );
3678 if( status != PSA_SUCCESS )
3679 return( status );
3680
3681 continue;
3682 }
3683
3684 if( n > output_length )
3685 n = (uint8_t) output_length;
3686 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3687 n );
3688 output += n;
3689 output_length -= n;
3690 tls12_prf->offset_in_block += n;
3691 }
3692
3693 return( PSA_SUCCESS );
3694}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003695#endif /* MBEDTLS_MD_C */
3696
Gilles Peskineeab56e42018-07-12 17:12:33 +02003697psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3698 uint8_t *output,
3699 size_t output_length )
3700{
3701 psa_status_t status;
3702
3703 if( output_length > generator->capacity )
3704 {
3705 generator->capacity = 0;
3706 /* Go through the error path to wipe all confidential data now
3707 * that the generator object is useless. */
3708 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3709 goto exit;
3710 }
3711 if( output_length == 0 &&
3712 generator->capacity == 0 && generator->alg == 0 )
3713 {
3714 /* Edge case: this is a blank or finished generator, and 0
3715 * bytes were requested. The right error in this case could
3716 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3717 * INSUFFICIENT_CAPACITY, which is right for a finished
3718 * generator, for consistency with the case when
3719 * output_length > 0. */
3720 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3721 }
3722 generator->capacity -= output_length;
3723
Gilles Peskine751d9652018-09-18 12:05:44 +02003724 if( generator->alg == PSA_ALG_SELECT_RAW )
3725 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003726 /* Initially, the capacity of a selection generator is always
3727 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3728 * abbreviated in this comment as `size`. When the remaining
3729 * capacity is `c`, the next bytes to serve start `c` bytes
3730 * from the end of the buffer, i.e. `size - c` from the
3731 * beginning of the buffer. Since `generator->capacity` was just
3732 * decremented above, we need to serve the bytes from
3733 * `size - generator->capacity - output_length` to
3734 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003735 size_t offset =
3736 generator->ctx.buffer.size - generator->capacity - output_length;
3737 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3738 status = PSA_SUCCESS;
3739 }
3740 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003741#if defined(MBEDTLS_MD_C)
3742 if( PSA_ALG_IS_HKDF( generator->alg ) )
3743 {
3744 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3745 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3746 output, output_length );
3747 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003748 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3749 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003750 {
3751 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3752 generator->alg, output,
3753 output_length );
3754 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003755 else
3756#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003757 {
3758 return( PSA_ERROR_BAD_STATE );
3759 }
3760
3761exit:
3762 if( status != PSA_SUCCESS )
3763 {
3764 psa_generator_abort( generator );
3765 memset( output, '!', output_length );
3766 }
3767 return( status );
3768}
3769
Gilles Peskine08542d82018-07-19 17:05:42 +02003770#if defined(MBEDTLS_DES_C)
3771static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3772{
3773 if( data_size >= 8 )
3774 mbedtls_des_key_set_parity( data );
3775 if( data_size >= 16 )
3776 mbedtls_des_key_set_parity( data + 8 );
3777 if( data_size >= 24 )
3778 mbedtls_des_key_set_parity( data + 16 );
3779}
3780#endif /* MBEDTLS_DES_C */
3781
Gilles Peskineeab56e42018-07-12 17:12:33 +02003782psa_status_t psa_generator_import_key( psa_key_slot_t key,
3783 psa_key_type_t type,
3784 size_t bits,
3785 psa_crypto_generator_t *generator )
3786{
3787 uint8_t *data = NULL;
3788 size_t bytes = PSA_BITS_TO_BYTES( bits );
3789 psa_status_t status;
3790
3791 if( ! key_type_is_raw_bytes( type ) )
3792 return( PSA_ERROR_INVALID_ARGUMENT );
3793 if( bits % 8 != 0 )
3794 return( PSA_ERROR_INVALID_ARGUMENT );
3795 data = mbedtls_calloc( 1, bytes );
3796 if( data == NULL )
3797 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3798
3799 status = psa_generator_read( generator, data, bytes );
3800 if( status != PSA_SUCCESS )
3801 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003802#if defined(MBEDTLS_DES_C)
3803 if( type == PSA_KEY_TYPE_DES )
3804 psa_des_set_key_parity( data, bytes );
3805#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003806 status = psa_import_key( key, type, data, bytes );
3807
3808exit:
3809 mbedtls_free( data );
3810 return( status );
3811}
3812
3813
3814
3815/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003816/* Key derivation */
3817/****************************************************************/
3818
Gilles Peskinea05219c2018-11-16 16:02:56 +01003819#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003820/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003821 * of the HKDF algorithm.
3822 *
3823 * Note that if this function fails, you must call psa_generator_abort()
3824 * to potentially free embedded data structures and wipe confidential data.
3825 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003826static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003827 const uint8_t *secret,
3828 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003829 psa_algorithm_t hash_alg,
3830 const uint8_t *salt,
3831 size_t salt_length,
3832 const uint8_t *label,
3833 size_t label_length )
3834{
3835 psa_status_t status;
3836 status = psa_hmac_setup_internal( &hkdf->hmac,
3837 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003838 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003839 if( status != PSA_SUCCESS )
3840 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003841 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003842 if( status != PSA_SUCCESS )
3843 return( status );
3844 status = psa_hmac_finish_internal( &hkdf->hmac,
3845 hkdf->prk,
3846 sizeof( hkdf->prk ) );
3847 if( status != PSA_SUCCESS )
3848 return( status );
3849 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3850 hkdf->block_number = 0;
3851 hkdf->info_length = label_length;
3852 if( label_length != 0 )
3853 {
3854 hkdf->info = mbedtls_calloc( 1, label_length );
3855 if( hkdf->info == NULL )
3856 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3857 memcpy( hkdf->info, label, label_length );
3858 }
3859 return( PSA_SUCCESS );
3860}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003861#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003862
Gilles Peskinea05219c2018-11-16 16:02:56 +01003863#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003864/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3865 *
3866 * Note that if this function fails, you must call psa_generator_abort()
3867 * to potentially free embedded data structures and wipe confidential data.
3868 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003869static psa_status_t psa_generator_tls12_prf_setup(
3870 psa_tls12_prf_generator_t *tls12_prf,
3871 const unsigned char *key,
3872 size_t key_len,
3873 psa_algorithm_t hash_alg,
3874 const uint8_t *salt,
3875 size_t salt_length,
3876 const uint8_t *label,
3877 size_t label_length )
3878{
3879 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003880 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3881 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003882
3883 tls12_prf->key = mbedtls_calloc( 1, key_len );
3884 if( tls12_prf->key == NULL )
3885 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3886 tls12_prf->key_len = key_len;
3887 memcpy( tls12_prf->key, key, key_len );
3888
Hanno Becker580fba12018-11-13 20:50:45 +00003889 overflow = ( salt_length + label_length < salt_length ) ||
3890 ( salt_length + label_length + hash_length < hash_length );
3891 if( overflow )
3892 return( PSA_ERROR_INVALID_ARGUMENT );
3893
3894 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3895 if( tls12_prf->Ai_with_seed == NULL )
3896 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3897 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3898
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003899 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3900 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003901 if( label_length != 0 )
3902 {
3903 memcpy( tls12_prf->Ai_with_seed + hash_length,
3904 label, label_length );
3905 }
3906
3907 if( salt_length != 0 )
3908 {
3909 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3910 salt, salt_length );
3911 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003912
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003913 /* The first block gets generated when
3914 * psa_generator_read() is called. */
3915 tls12_prf->block_number = 0;
3916 tls12_prf->offset_in_block = hash_length;
3917
3918 return( PSA_SUCCESS );
3919}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003920
3921/* Set up a TLS-1.2-PSK-to-MS-based generator. */
3922static psa_status_t psa_generator_tls12_psk_to_ms_setup(
3923 psa_tls12_prf_generator_t *tls12_prf,
3924 const unsigned char *psk,
3925 size_t psk_len,
3926 psa_algorithm_t hash_alg,
3927 const uint8_t *salt,
3928 size_t salt_length,
3929 const uint8_t *label,
3930 size_t label_length )
3931{
3932 psa_status_t status;
3933 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
3934
3935 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
3936 return( PSA_ERROR_INVALID_ARGUMENT );
3937
3938 /* Quoting RFC 4279, Section 2:
3939 *
3940 * The premaster secret is formed as follows: if the PSK is N octets
3941 * long, concatenate a uint16 with the value N, N zero octets, a second
3942 * uint16 with the value N, and the PSK itself.
3943 */
3944
3945 pms[0] = ( psk_len >> 8 ) & 0xff;
3946 pms[1] = ( psk_len >> 0 ) & 0xff;
3947 memset( pms + 2, 0, psk_len );
3948 pms[2 + psk_len + 0] = pms[0];
3949 pms[2 + psk_len + 1] = pms[1];
3950 memcpy( pms + 4 + psk_len, psk, psk_len );
3951
3952 status = psa_generator_tls12_prf_setup( tls12_prf,
3953 pms, 4 + 2 * psk_len,
3954 hash_alg,
3955 salt, salt_length,
3956 label, label_length );
3957
3958 mbedtls_zeroize( pms, sizeof( pms ) );
3959 return( status );
3960}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003961#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003962
Gilles Peskine346797d2018-11-16 16:05:06 +01003963/* Note that if this function fails, you must call psa_generator_abort()
3964 * to potentially free embedded data structures and wipe confidential data.
3965 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003966static psa_status_t psa_key_derivation_internal(
3967 psa_crypto_generator_t *generator,
3968 const uint8_t *secret, size_t secret_length,
3969 psa_algorithm_t alg,
3970 const uint8_t *salt, size_t salt_length,
3971 const uint8_t *label, size_t label_length,
3972 size_t capacity )
3973{
3974 psa_status_t status;
3975 size_t max_capacity;
3976
3977 /* Set generator->alg even on failure so that abort knows what to do. */
3978 generator->alg = alg;
3979
Gilles Peskine751d9652018-09-18 12:05:44 +02003980 if( alg == PSA_ALG_SELECT_RAW )
3981 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003982 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02003983 if( salt_length != 0 )
3984 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01003985 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02003986 if( label_length != 0 )
3987 return( PSA_ERROR_INVALID_ARGUMENT );
3988 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
3989 if( generator->ctx.buffer.data == NULL )
3990 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3991 memcpy( generator->ctx.buffer.data, secret, secret_length );
3992 generator->ctx.buffer.size = secret_length;
3993 max_capacity = secret_length;
3994 status = PSA_SUCCESS;
3995 }
3996 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003997#if defined(MBEDTLS_MD_C)
3998 if( PSA_ALG_IS_HKDF( alg ) )
3999 {
4000 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4001 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4002 if( hash_size == 0 )
4003 return( PSA_ERROR_NOT_SUPPORTED );
4004 max_capacity = 255 * hash_size;
4005 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4006 secret, secret_length,
4007 hash_alg,
4008 salt, salt_length,
4009 label, label_length );
4010 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004011 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4012 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4013 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004014 {
4015 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4016 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4017
4018 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4019 if( hash_alg != PSA_ALG_SHA_256 &&
4020 hash_alg != PSA_ALG_SHA_384 )
4021 {
4022 return( PSA_ERROR_NOT_SUPPORTED );
4023 }
4024
4025 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004026
4027 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4028 {
4029 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4030 secret, secret_length,
4031 hash_alg, salt, salt_length,
4032 label, label_length );
4033 }
4034 else
4035 {
4036 status = psa_generator_tls12_psk_to_ms_setup(
4037 &generator->ctx.tls12_prf,
4038 secret, secret_length,
4039 hash_alg, salt, salt_length,
4040 label, label_length );
4041 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004042 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004043 else
4044#endif
4045 {
4046 return( PSA_ERROR_NOT_SUPPORTED );
4047 }
4048
4049 if( status != PSA_SUCCESS )
4050 return( status );
4051
4052 if( capacity <= max_capacity )
4053 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004054 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4055 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004056 else
4057 return( PSA_ERROR_INVALID_ARGUMENT );
4058
4059 return( PSA_SUCCESS );
4060}
4061
Gilles Peskineea0fb492018-07-12 17:17:20 +02004062psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01004063 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004064 psa_algorithm_t alg,
4065 const uint8_t *salt,
4066 size_t salt_length,
4067 const uint8_t *label,
4068 size_t label_length,
4069 size_t capacity )
4070{
4071 key_slot_t *slot;
4072 psa_status_t status;
4073
4074 if( generator->alg != 0 )
4075 return( PSA_ERROR_BAD_STATE );
4076
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004077 /* Make sure that alg is a key derivation algorithm. This prevents
4078 * key selection algorithms, which psa_key_derivation_internal
4079 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004080 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4081 return( PSA_ERROR_INVALID_ARGUMENT );
4082
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004083 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
4084 if( status != PSA_SUCCESS )
4085 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004086
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004087 if( slot->type != PSA_KEY_TYPE_DERIVE )
4088 return( PSA_ERROR_INVALID_ARGUMENT );
4089
4090 status = psa_key_derivation_internal( generator,
4091 slot->data.raw.data,
4092 slot->data.raw.bytes,
4093 alg,
4094 salt, salt_length,
4095 label, label_length,
4096 capacity );
4097 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004098 psa_generator_abort( generator );
4099 return( status );
4100}
4101
4102
4103
4104/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004105/* Key agreement */
4106/****************************************************************/
4107
Gilles Peskinea05219c2018-11-16 16:02:56 +01004108#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004109static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4110 size_t peer_key_length,
4111 const mbedtls_ecp_keypair *our_key,
4112 uint8_t *shared_secret,
4113 size_t shared_secret_size,
4114 size_t *shared_secret_length )
4115{
4116 mbedtls_pk_context pk;
4117 mbedtls_ecp_keypair *their_key = NULL;
4118 mbedtls_ecdh_context ecdh;
4119 int ret;
4120 mbedtls_ecdh_init( &ecdh );
4121 mbedtls_pk_init( &pk );
4122
4123 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
4124 if( ret != 0 )
4125 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02004126 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004127 {
Gilles Peskine88714d72018-10-25 23:07:25 +02004128 case MBEDTLS_PK_ECKEY:
4129 case MBEDTLS_PK_ECKEY_DH:
4130 break;
4131 default:
4132 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4133 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004134 }
4135 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01004136 if( their_key->grp.id != our_key->grp.id )
4137 {
4138 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4139 goto exit;
4140 }
4141
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004142 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
4143 if( ret != 0 )
4144 goto exit;
4145 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
4146 if( ret != 0 )
4147 goto exit;
4148
4149 ret = mbedtls_ecdh_calc_secret( &ecdh,
4150 shared_secret_length,
4151 shared_secret, shared_secret_size,
4152 mbedtls_ctr_drbg_random,
4153 &global_data.ctr_drbg );
4154
4155exit:
4156 mbedtls_pk_free( &pk );
4157 mbedtls_ecdh_free( &ecdh );
4158 return( mbedtls_to_psa_error( ret ) );
4159}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004160#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004161
Gilles Peskine01d718c2018-09-18 12:01:02 +02004162#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4163
Gilles Peskine346797d2018-11-16 16:05:06 +01004164/* Note that if this function fails, you must call psa_generator_abort()
4165 * to potentially free embedded data structures and wipe confidential data.
4166 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004167static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
4168 key_slot_t *private_key,
4169 const uint8_t *peer_key,
4170 size_t peer_key_length,
4171 psa_algorithm_t alg )
4172{
4173 psa_status_t status;
4174 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4175 size_t shared_secret_length = 0;
4176
4177 /* Step 1: run the secret agreement algorithm to generate the shared
4178 * secret. */
4179 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4180 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004181#if defined(MBEDTLS_ECDH_C)
4182 case PSA_ALG_ECDH_BASE:
4183 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4184 return( PSA_ERROR_INVALID_ARGUMENT );
4185 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4186 private_key->data.ecp,
4187 shared_secret,
4188 sizeof( shared_secret ),
4189 &shared_secret_length );
4190 break;
4191#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004192 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004193 (void) private_key;
4194 (void) peer_key;
4195 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004196 return( PSA_ERROR_NOT_SUPPORTED );
4197 }
4198 if( status != PSA_SUCCESS )
4199 goto exit;
4200
4201 /* Step 2: set up the key derivation to generate key material from
4202 * the shared secret. */
4203 status = psa_key_derivation_internal( generator,
4204 shared_secret, shared_secret_length,
4205 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4206 NULL, 0, NULL, 0,
4207 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4208exit:
4209 mbedtls_zeroize( shared_secret, shared_secret_length );
4210 return( status );
4211}
4212
4213psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
4214 psa_key_slot_t private_key,
4215 const uint8_t *peer_key,
4216 size_t peer_key_length,
4217 psa_algorithm_t alg )
4218{
4219 key_slot_t *slot;
4220 psa_status_t status;
4221 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4222 return( PSA_ERROR_INVALID_ARGUMENT );
4223 status = psa_get_key_from_slot( private_key, &slot,
4224 PSA_KEY_USAGE_DERIVE, alg );
4225 if( status != PSA_SUCCESS )
4226 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004227 status = psa_key_agreement_internal( generator,
4228 slot,
4229 peer_key, peer_key_length,
4230 alg );
4231 if( status != PSA_SUCCESS )
4232 psa_generator_abort( generator );
4233 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004234}
4235
4236
4237
4238/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004239/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004240/****************************************************************/
4241
4242psa_status_t psa_generate_random( uint8_t *output,
4243 size_t output_size )
4244{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004245 int ret;
4246 GUARD_MODULE_INITIALIZED;
4247
4248 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004249 return( mbedtls_to_psa_error( ret ) );
4250}
4251
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004252#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004253
4254/* Support function for error conversion between psa_its error codes to psa crypto */
4255static psa_status_t its_to_psa_error( psa_its_status_t ret )
4256{
4257 switch( ret )
4258 {
4259 case PSA_ITS_SUCCESS:
4260 return( PSA_SUCCESS );
4261
4262 case PSA_ITS_ERROR_KEY_NOT_FOUND:
4263 return( PSA_ERROR_EMPTY_SLOT );
4264
4265 case PSA_ITS_ERROR_STORAGE_FAILURE:
4266 return( PSA_ERROR_STORAGE_FAILURE );
4267
4268 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4269 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4270
4271 case PSA_ITS_ERROR_INVALID_KEY:
4272 case PSA_PS_ERROR_OFFSET_INVALID:
4273 case PSA_ITS_ERROR_INCORRECT_SIZE:
4274 case PSA_ITS_ERROR_BAD_POINTER:
4275 return( PSA_ERROR_INVALID_ARGUMENT );
4276
4277 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4278 return( PSA_ERROR_NOT_SUPPORTED );
4279
4280 case PSA_ITS_ERROR_WRITE_ONCE:
4281 return( PSA_ERROR_OCCUPIED_SLOT );
4282
4283 default:
4284 return( PSA_ERROR_UNKNOWN_ERROR );
4285 }
4286}
4287
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004288psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4289 size_t seed_size )
4290{
4291 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004292 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004293 struct psa_its_info_t p_info;
4294 if( global_data.initialized )
4295 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004296
4297 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4298 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4299 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4300 return( PSA_ERROR_INVALID_ARGUMENT );
4301
avolinski0d2c2662018-11-21 17:31:07 +02004302 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004303 status = its_to_psa_error( its_status );
4304
4305 if( PSA_ITS_ERROR_KEY_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004306 {
avolinski0d2c2662018-11-21 17:31:07 +02004307 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004308 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004309 }
avolinski13beb102018-11-20 16:51:49 +02004310 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004311 {
4312 /* You should not be here. Seed needs to be injected only once */
4313 status = PSA_ERROR_NOT_PERMITTED;
4314 }
4315 return( status );
4316}
4317#endif
4318
Gilles Peskine05d69892018-06-19 22:00:52 +02004319psa_status_t psa_generate_key( psa_key_slot_t key,
4320 psa_key_type_t type,
4321 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004322 const void *extra,
4323 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004324{
Gilles Peskine12313cd2018-06-20 00:20:32 +02004325 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004326 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004327
Gilles Peskine53d991e2018-07-12 01:14:59 +02004328 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004329 return( PSA_ERROR_INVALID_ARGUMENT );
4330
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004331 status = psa_get_empty_key_slot( key, &slot );
4332 if( status != PSA_SUCCESS )
4333 return( status );
4334
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004335 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004336 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004337 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004338 if( status != PSA_SUCCESS )
4339 return( status );
4340 status = psa_generate_random( slot->data.raw.data,
4341 slot->data.raw.bytes );
4342 if( status != PSA_SUCCESS )
4343 {
4344 mbedtls_free( slot->data.raw.data );
4345 return( status );
4346 }
4347#if defined(MBEDTLS_DES_C)
4348 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004349 psa_des_set_key_parity( slot->data.raw.data,
4350 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004351#endif /* MBEDTLS_DES_C */
4352 }
4353 else
4354
4355#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4356 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4357 {
4358 mbedtls_rsa_context *rsa;
4359 int ret;
4360 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004361 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4362 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004363 /* Accept only byte-aligned keys, for the same reasons as
4364 * in psa_import_rsa_key(). */
4365 if( bits % 8 != 0 )
4366 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004367 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004368 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004369 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004370 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004371 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004372#if INT_MAX < 0xffffffff
4373 /* Check that the uint32_t value passed by the caller fits
4374 * in the range supported by this implementation. */
4375 if( p->e > INT_MAX )
4376 return( PSA_ERROR_NOT_SUPPORTED );
4377#endif
4378 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004379 }
4380 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4381 if( rsa == NULL )
4382 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4383 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4384 ret = mbedtls_rsa_gen_key( rsa,
4385 mbedtls_ctr_drbg_random,
4386 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004387 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004388 exponent );
4389 if( ret != 0 )
4390 {
4391 mbedtls_rsa_free( rsa );
4392 mbedtls_free( rsa );
4393 return( mbedtls_to_psa_error( ret ) );
4394 }
4395 slot->data.rsa = rsa;
4396 }
4397 else
4398#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4399
4400#if defined(MBEDTLS_ECP_C)
4401 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4402 {
4403 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4404 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4405 const mbedtls_ecp_curve_info *curve_info =
4406 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4407 mbedtls_ecp_keypair *ecp;
4408 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004409 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004410 return( PSA_ERROR_NOT_SUPPORTED );
4411 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4412 return( PSA_ERROR_NOT_SUPPORTED );
4413 if( curve_info->bit_size != bits )
4414 return( PSA_ERROR_INVALID_ARGUMENT );
4415 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4416 if( ecp == NULL )
4417 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4418 mbedtls_ecp_keypair_init( ecp );
4419 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4420 mbedtls_ctr_drbg_random,
4421 &global_data.ctr_drbg );
4422 if( ret != 0 )
4423 {
4424 mbedtls_ecp_keypair_free( ecp );
4425 mbedtls_free( ecp );
4426 return( mbedtls_to_psa_error( ret ) );
4427 }
4428 slot->data.ecp = ecp;
4429 }
4430 else
4431#endif /* MBEDTLS_ECP_C */
4432
4433 return( PSA_ERROR_NOT_SUPPORTED );
4434
4435 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004436
4437#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4438 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4439 {
4440 return( psa_save_generated_persistent_key( key, slot, bits ) );
4441 }
4442#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4443
4444 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004445}
4446
4447
4448/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004449/* Module setup */
4450/****************************************************************/
4451
Gilles Peskine5e769522018-11-20 21:59:56 +01004452psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4453 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4454 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4455{
4456 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4457 return( PSA_ERROR_BAD_STATE );
4458 global_data.entropy_init = entropy_init;
4459 global_data.entropy_free = entropy_free;
4460 return( PSA_SUCCESS );
4461}
4462
Gilles Peskinee59236f2018-01-27 23:32:46 +01004463void mbedtls_psa_crypto_free( void )
4464{
Jaeden Amero045bd502018-06-26 14:00:08 +01004465 psa_key_slot_t key;
Darryl Green40225ba2018-11-15 14:48:15 +00004466 key_slot_t *slot;
4467 psa_status_t status;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004468 if( global_data.key_slots_initialized )
Darryl Green40225ba2018-11-15 14:48:15 +00004469 {
Gilles Peskinec6b69072018-11-20 21:42:52 +01004470 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
4471 {
4472 status = psa_get_key_slot( key, &slot );
4473 if( status != PSA_SUCCESS )
4474 continue;
4475 psa_remove_key_data_from_memory( slot );
4476 /* Zeroize the slot to wipe metadata such as policies. */
4477 mbedtls_zeroize( slot, sizeof( *slot ) );
4478 }
Darryl Green40225ba2018-11-15 14:48:15 +00004479 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01004480 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4481 {
4482 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004483 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004484 }
4485 /* Wipe all remaining data, including configuration.
4486 * In particular, this sets all state indicator to the value
4487 * indicating "uninitialized". */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004488 mbedtls_zeroize( &global_data, sizeof( global_data ) );
4489}
4490
4491psa_status_t psa_crypto_init( void )
4492{
4493 int ret;
4494 const unsigned char drbg_seed[] = "PSA";
4495
Gilles Peskinec6b69072018-11-20 21:42:52 +01004496 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004497 if( global_data.initialized != 0 )
4498 return( PSA_SUCCESS );
4499
Gilles Peskine5e769522018-11-20 21:59:56 +01004500 /* Set default configuration if
4501 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4502 if( global_data.entropy_init == NULL )
4503 global_data.entropy_init = mbedtls_entropy_init;
4504 if( global_data.entropy_free == NULL )
4505 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004506
Gilles Peskinec6b69072018-11-20 21:42:52 +01004507 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004508 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004509 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004510 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004511 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4512 mbedtls_entropy_func,
4513 &global_data.entropy,
4514 drbg_seed, sizeof( drbg_seed ) - 1 );
4515 if( ret != 0 )
4516 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004517 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004518
Gilles Peskinec6b69072018-11-20 21:42:52 +01004519 /* Initialize the key slots. Zero-initialization has made all key
4520 * slots empty, so there is nothing to do. In a future version we will
4521 * load data from storage. */
4522 global_data.key_slots_initialized = 1;
4523
4524 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004525 global_data.initialized = 1;
4526
Gilles Peskinee59236f2018-01-27 23:32:46 +01004527exit:
4528 if( ret != 0 )
4529 mbedtls_psa_crypto_free( );
4530 return( mbedtls_to_psa_error( ret ) );
4531}
4532
4533#endif /* MBEDTLS_PSA_CRYPTO_C */