blob: 77314f2dd2168d4325884af55390b692c4d0131f [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
Darryl Greend49a4992018-06-18 17:27:26 +010046/* Include internal declarations that are useful for implementing persistently
47 * stored keys. */
48#include "psa_crypto_storage.h"
49
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010050#include <stdlib.h>
51#include <string.h>
52#if defined(MBEDTLS_PLATFORM_C)
53#include "mbedtls/platform.h"
54#else
55#define mbedtls_calloc calloc
56#define mbedtls_free free
57#endif
58
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020060#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020061#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/blowfish.h"
63#include "mbedtls/camellia.h"
64#include "mbedtls/cipher.h"
65#include "mbedtls/ccm.h"
66#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020069#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010070#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010071#include "mbedtls/entropy.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020072#include "mbedtls/entropy_poll.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/error.h"
74#include "mbedtls/gcm.h"
75#include "mbedtls/md2.h"
76#include "mbedtls/md4.h"
77#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010078#include "mbedtls/md.h"
79#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010080#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010081#include "mbedtls/pk_internal.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010082#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010083#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010084#include "mbedtls/sha1.h"
85#include "mbedtls/sha256.h"
86#include "mbedtls/sha512.h"
87#include "mbedtls/xtea.h"
88
Netanel Gonen2bcd3122018-11-19 11:53:02 +020089#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
90#include "psa_prot_internal_storage.h"
91#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +010092
Gilles Peskine996deb12018-08-01 15:45:45 +020093#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
94
Gilles Peskinee59236f2018-01-27 23:32:46 +010095/* Implementation that should never be optimized out by the compiler */
96static void mbedtls_zeroize( void *v, size_t n )
97{
98 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
99}
100
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100101/* constant-time buffer comparison */
102static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
103{
104 size_t i;
105 unsigned char diff = 0;
106
107 for( i = 0; i < n; i++ )
108 diff |= a[i] ^ b[i];
109
110 return( diff );
111}
112
113
114
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100115/****************************************************************/
116/* Global data, support functions and library management */
117/****************************************************************/
118
119/* Number of key slots (plus one because 0 is not used).
120 * The value is a compile-time constant for now, for simplicity. */
Gilles Peskine828ed142018-06-18 23:25:51 +0200121#define PSA_KEY_SLOT_COUNT 32
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100122
Gilles Peskine2d277862018-06-18 15:41:12 +0200123typedef struct
124{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100125 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +0300126 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200127 psa_key_lifetime_t lifetime;
Gilles Peskine2d277862018-06-18 15:41:12 +0200128 union
129 {
130 struct raw_data
131 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100132 uint8_t *data;
133 size_t bytes;
134 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100135#if defined(MBEDTLS_RSA_C)
136 mbedtls_rsa_context *rsa;
137#endif /* MBEDTLS_RSA_C */
138#if defined(MBEDTLS_ECP_C)
139 mbedtls_ecp_keypair *ecp;
140#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100141 } data;
142} key_slot_t;
143
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200144static int key_type_is_raw_bytes( psa_key_type_t type )
145{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200146 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200147}
148
Gilles Peskine2d277862018-06-18 15:41:12 +0200149typedef struct
150{
Gilles Peskinee59236f2018-01-27 23:32:46 +0100151 int initialized;
152 mbedtls_entropy_context entropy;
153 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200154 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinee59236f2018-01-27 23:32:46 +0100155} psa_global_data_t;
156
157static psa_global_data_t global_data;
158
itayzafrir0adf0fc2018-09-06 16:24:41 +0300159#define GUARD_MODULE_INITIALIZED \
160 if( global_data.initialized == 0 ) \
161 return( PSA_ERROR_BAD_STATE );
162
Gilles Peskinee59236f2018-01-27 23:32:46 +0100163static psa_status_t mbedtls_to_psa_error( int ret )
164{
Gilles Peskinea5905292018-02-07 20:59:33 +0100165 /* If there's both a high-level code and low-level code, dispatch on
166 * the high-level code. */
167 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100168 {
169 case 0:
170 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100171
172 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
173 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
174 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
175 return( PSA_ERROR_NOT_SUPPORTED );
176 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
177 return( PSA_ERROR_HARDWARE_FAILURE );
178
179 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
180 return( PSA_ERROR_HARDWARE_FAILURE );
181
Gilles Peskine9a944802018-06-21 09:35:35 +0200182 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
183 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
184 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
185 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
186 case MBEDTLS_ERR_ASN1_INVALID_DATA:
187 return( PSA_ERROR_INVALID_ARGUMENT );
188 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
189 return( PSA_ERROR_INSUFFICIENT_MEMORY );
190 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
191 return( PSA_ERROR_BUFFER_TOO_SMALL );
192
Gilles Peskinea5905292018-02-07 20:59:33 +0100193 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
194 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
195 return( PSA_ERROR_NOT_SUPPORTED );
196 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
197 return( PSA_ERROR_HARDWARE_FAILURE );
198
199 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
200 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
201 return( PSA_ERROR_NOT_SUPPORTED );
202 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
203 return( PSA_ERROR_HARDWARE_FAILURE );
204
205 case MBEDTLS_ERR_CCM_BAD_INPUT:
206 return( PSA_ERROR_INVALID_ARGUMENT );
207 case MBEDTLS_ERR_CCM_AUTH_FAILED:
208 return( PSA_ERROR_INVALID_SIGNATURE );
209 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
210 return( PSA_ERROR_HARDWARE_FAILURE );
211
212 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
213 return( PSA_ERROR_NOT_SUPPORTED );
214 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
215 return( PSA_ERROR_INVALID_ARGUMENT );
216 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
217 return( PSA_ERROR_INSUFFICIENT_MEMORY );
218 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
219 return( PSA_ERROR_INVALID_PADDING );
220 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
221 return( PSA_ERROR_BAD_STATE );
222 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
223 return( PSA_ERROR_INVALID_SIGNATURE );
224 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
225 return( PSA_ERROR_TAMPERING_DETECTED );
226 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
227 return( PSA_ERROR_HARDWARE_FAILURE );
228
229 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
230 return( PSA_ERROR_HARDWARE_FAILURE );
231
232 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
233 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
234 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
235 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
236 return( PSA_ERROR_NOT_SUPPORTED );
237 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
238 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
239
240 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
241 return( PSA_ERROR_NOT_SUPPORTED );
242 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
243 return( PSA_ERROR_HARDWARE_FAILURE );
244
Gilles Peskinee59236f2018-01-27 23:32:46 +0100245 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
246 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
247 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
248 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100249
250 case MBEDTLS_ERR_GCM_AUTH_FAILED:
251 return( PSA_ERROR_INVALID_SIGNATURE );
252 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200253 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100254 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
255 return( PSA_ERROR_HARDWARE_FAILURE );
256
257 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
258 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
259 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
260 return( PSA_ERROR_HARDWARE_FAILURE );
261
262 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
263 return( PSA_ERROR_NOT_SUPPORTED );
264 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MD_ALLOC_FAILED:
267 return( PSA_ERROR_INSUFFICIENT_MEMORY );
268 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
269 return( PSA_ERROR_STORAGE_FAILURE );
270 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
271 return( PSA_ERROR_HARDWARE_FAILURE );
272
Gilles Peskinef76aa772018-10-29 19:24:33 +0100273 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
274 return( PSA_ERROR_STORAGE_FAILURE );
275 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
276 return( PSA_ERROR_INVALID_ARGUMENT );
277 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
280 return( PSA_ERROR_BUFFER_TOO_SMALL );
281 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
282 return( PSA_ERROR_INVALID_ARGUMENT );
283 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
284 return( PSA_ERROR_INVALID_ARGUMENT );
285 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
286 return( PSA_ERROR_INVALID_ARGUMENT );
287 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
288 return( PSA_ERROR_INSUFFICIENT_MEMORY );
289
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100290 case MBEDTLS_ERR_PK_ALLOC_FAILED:
291 return( PSA_ERROR_INSUFFICIENT_MEMORY );
292 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
293 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
294 return( PSA_ERROR_INVALID_ARGUMENT );
295 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100296 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100297 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
298 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
299 return( PSA_ERROR_INVALID_ARGUMENT );
300 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
301 return( PSA_ERROR_NOT_SUPPORTED );
302 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
303 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
304 return( PSA_ERROR_NOT_PERMITTED );
305 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_PK_INVALID_ALG:
308 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
309 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
310 return( PSA_ERROR_NOT_SUPPORTED );
311 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
312 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100313 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
314 return( PSA_ERROR_HARDWARE_FAILURE );
315
316 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
317 return( PSA_ERROR_HARDWARE_FAILURE );
318
319 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
320 return( PSA_ERROR_INVALID_ARGUMENT );
321 case MBEDTLS_ERR_RSA_INVALID_PADDING:
322 return( PSA_ERROR_INVALID_PADDING );
323 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
324 return( PSA_ERROR_HARDWARE_FAILURE );
325 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
326 return( PSA_ERROR_INVALID_ARGUMENT );
327 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
328 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
329 return( PSA_ERROR_TAMPERING_DETECTED );
330 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
331 return( PSA_ERROR_INVALID_SIGNATURE );
332 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
333 return( PSA_ERROR_BUFFER_TOO_SMALL );
334 case MBEDTLS_ERR_RSA_RNG_FAILED:
335 return( PSA_ERROR_INSUFFICIENT_MEMORY );
336 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
337 return( PSA_ERROR_NOT_SUPPORTED );
338 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
339 return( PSA_ERROR_HARDWARE_FAILURE );
340
341 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
342 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
343 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
344 return( PSA_ERROR_HARDWARE_FAILURE );
345
346 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
347 return( PSA_ERROR_INVALID_ARGUMENT );
348 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
349 return( PSA_ERROR_HARDWARE_FAILURE );
350
itayzafrir5c753392018-05-08 11:18:38 +0300351 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300352 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300353 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300354 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
355 return( PSA_ERROR_BUFFER_TOO_SMALL );
356 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
357 return( PSA_ERROR_NOT_SUPPORTED );
358 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
359 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
360 return( PSA_ERROR_INVALID_SIGNATURE );
361 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
362 return( PSA_ERROR_INSUFFICIENT_MEMORY );
363 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
364 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300365
Gilles Peskinee59236f2018-01-27 23:32:46 +0100366 default:
367 return( PSA_ERROR_UNKNOWN_ERROR );
368 }
369}
370
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200371
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200372
373
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100374/****************************************************************/
375/* Key management */
376/****************************************************************/
377
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100378#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200379static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
380{
381 switch( grpid )
382 {
383 case MBEDTLS_ECP_DP_SECP192R1:
384 return( PSA_ECC_CURVE_SECP192R1 );
385 case MBEDTLS_ECP_DP_SECP224R1:
386 return( PSA_ECC_CURVE_SECP224R1 );
387 case MBEDTLS_ECP_DP_SECP256R1:
388 return( PSA_ECC_CURVE_SECP256R1 );
389 case MBEDTLS_ECP_DP_SECP384R1:
390 return( PSA_ECC_CURVE_SECP384R1 );
391 case MBEDTLS_ECP_DP_SECP521R1:
392 return( PSA_ECC_CURVE_SECP521R1 );
393 case MBEDTLS_ECP_DP_BP256R1:
394 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
395 case MBEDTLS_ECP_DP_BP384R1:
396 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
397 case MBEDTLS_ECP_DP_BP512R1:
398 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
399 case MBEDTLS_ECP_DP_CURVE25519:
400 return( PSA_ECC_CURVE_CURVE25519 );
401 case MBEDTLS_ECP_DP_SECP192K1:
402 return( PSA_ECC_CURVE_SECP192K1 );
403 case MBEDTLS_ECP_DP_SECP224K1:
404 return( PSA_ECC_CURVE_SECP224K1 );
405 case MBEDTLS_ECP_DP_SECP256K1:
406 return( PSA_ECC_CURVE_SECP256K1 );
407 case MBEDTLS_ECP_DP_CURVE448:
408 return( PSA_ECC_CURVE_CURVE448 );
409 default:
410 return( 0 );
411 }
412}
413
Gilles Peskine12313cd2018-06-20 00:20:32 +0200414static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
415{
416 switch( curve )
417 {
418 case PSA_ECC_CURVE_SECP192R1:
419 return( MBEDTLS_ECP_DP_SECP192R1 );
420 case PSA_ECC_CURVE_SECP224R1:
421 return( MBEDTLS_ECP_DP_SECP224R1 );
422 case PSA_ECC_CURVE_SECP256R1:
423 return( MBEDTLS_ECP_DP_SECP256R1 );
424 case PSA_ECC_CURVE_SECP384R1:
425 return( MBEDTLS_ECP_DP_SECP384R1 );
426 case PSA_ECC_CURVE_SECP521R1:
427 return( MBEDTLS_ECP_DP_SECP521R1 );
428 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
429 return( MBEDTLS_ECP_DP_BP256R1 );
430 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
431 return( MBEDTLS_ECP_DP_BP384R1 );
432 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
433 return( MBEDTLS_ECP_DP_BP512R1 );
434 case PSA_ECC_CURVE_CURVE25519:
435 return( MBEDTLS_ECP_DP_CURVE25519 );
436 case PSA_ECC_CURVE_SECP192K1:
437 return( MBEDTLS_ECP_DP_SECP192K1 );
438 case PSA_ECC_CURVE_SECP224K1:
439 return( MBEDTLS_ECP_DP_SECP224K1 );
440 case PSA_ECC_CURVE_SECP256K1:
441 return( MBEDTLS_ECP_DP_SECP256K1 );
442 case PSA_ECC_CURVE_CURVE448:
443 return( MBEDTLS_ECP_DP_CURVE448 );
444 default:
445 return( MBEDTLS_ECP_DP_NONE );
446 }
447}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100448#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200449
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200450static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
451 size_t bits,
452 struct raw_data *raw )
453{
454 /* Check that the bit size is acceptable for the key type */
455 switch( type )
456 {
457 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200458 if( bits == 0 )
459 {
460 raw->bytes = 0;
461 raw->data = NULL;
462 return( PSA_SUCCESS );
463 }
464 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200465#if defined(MBEDTLS_MD_C)
466 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200467#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200468 case PSA_KEY_TYPE_DERIVE:
469 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200470#if defined(MBEDTLS_AES_C)
471 case PSA_KEY_TYPE_AES:
472 if( bits != 128 && bits != 192 && bits != 256 )
473 return( PSA_ERROR_INVALID_ARGUMENT );
474 break;
475#endif
476#if defined(MBEDTLS_CAMELLIA_C)
477 case PSA_KEY_TYPE_CAMELLIA:
478 if( bits != 128 && bits != 192 && bits != 256 )
479 return( PSA_ERROR_INVALID_ARGUMENT );
480 break;
481#endif
482#if defined(MBEDTLS_DES_C)
483 case PSA_KEY_TYPE_DES:
484 if( bits != 64 && bits != 128 && bits != 192 )
485 return( PSA_ERROR_INVALID_ARGUMENT );
486 break;
487#endif
488#if defined(MBEDTLS_ARC4_C)
489 case PSA_KEY_TYPE_ARC4:
490 if( bits < 8 || bits > 2048 )
491 return( PSA_ERROR_INVALID_ARGUMENT );
492 break;
493#endif
494 default:
495 return( PSA_ERROR_NOT_SUPPORTED );
496 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200497 if( bits % 8 != 0 )
498 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200499
500 /* Allocate memory for the key */
501 raw->bytes = PSA_BITS_TO_BYTES( bits );
502 raw->data = mbedtls_calloc( 1, raw->bytes );
503 if( raw->data == NULL )
504 {
505 raw->bytes = 0;
506 return( PSA_ERROR_INSUFFICIENT_MEMORY );
507 }
508 return( PSA_SUCCESS );
509}
510
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200511#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100512/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
513 * that are not a multiple of 8) well. For example, there is only
514 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
515 * way to return the exact bit size of a key.
516 * To keep things simple, reject non-byte-aligned key sizes. */
517static psa_status_t psa_check_rsa_key_byte_aligned(
518 const mbedtls_rsa_context *rsa )
519{
520 mbedtls_mpi n;
521 psa_status_t status;
522 mbedtls_mpi_init( &n );
523 status = mbedtls_to_psa_error(
524 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
525 if( status == PSA_SUCCESS )
526 {
527 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
528 status = PSA_ERROR_NOT_SUPPORTED;
529 }
530 mbedtls_mpi_free( &n );
531 return( status );
532}
533
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200534static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
535 mbedtls_rsa_context **p_rsa )
536{
537 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
538 return( PSA_ERROR_INVALID_ARGUMENT );
539 else
540 {
541 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
Gilles Peskineaac64a22018-11-12 18:37:42 +0100542 /* The size of an RSA key doesn't have to be a multiple of 8.
543 * Mbed TLS supports non-byte-aligned key sizes, but not well.
544 * For example, mbedtls_rsa_get_len() returns the key size in
545 * bytes, not in bits. */
546 size_t bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100547 psa_status_t status;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200548 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
549 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100550 status = psa_check_rsa_key_byte_aligned( rsa );
551 if( status != PSA_SUCCESS )
552 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200553 *p_rsa = rsa;
554 return( PSA_SUCCESS );
555 }
556}
557#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
558
559#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100560/* Import an elliptic curve parsed by the mbedtls pk module. */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200561static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
562 mbedtls_pk_context *pk,
563 mbedtls_ecp_keypair **p_ecp )
564{
565 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
566 return( PSA_ERROR_INVALID_ARGUMENT );
567 else
568 {
569 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
570 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
571 if( actual_curve != expected_curve )
572 return( PSA_ERROR_INVALID_ARGUMENT );
573 *p_ecp = ecp;
574 return( PSA_SUCCESS );
575 }
576}
577#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
578
Gilles Peskinef76aa772018-10-29 19:24:33 +0100579#if defined(MBEDTLS_ECP_C)
580/* Import a private key given as a byte string which is the private value
581 * in big-endian order. */
582static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
583 const uint8_t *data,
584 size_t data_length,
585 mbedtls_ecp_keypair **p_ecp )
586{
587 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
588 mbedtls_ecp_keypair *ecp = NULL;
589 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
590
591 *p_ecp = NULL;
592 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
593 if( ecp == NULL )
594 return( PSA_ERROR_INSUFFICIENT_MEMORY );
595
596 /* Load the group. */
597 status = mbedtls_to_psa_error(
598 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
599 if( status != PSA_SUCCESS )
600 goto exit;
601 /* Load the secret value. */
602 status = mbedtls_to_psa_error(
603 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
604 if( status != PSA_SUCCESS )
605 goto exit;
606 /* Validate the private key. */
607 status = mbedtls_to_psa_error(
608 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
609 if( status != PSA_SUCCESS )
610 goto exit;
611 /* Calculate the public key from the private key. */
612 status = mbedtls_to_psa_error(
613 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
614 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
615 if( status != PSA_SUCCESS )
616 goto exit;
617
618 *p_ecp = ecp;
619 return( PSA_SUCCESS );
620
621exit:
622 if( ecp != NULL )
623 {
624 mbedtls_ecp_keypair_free( ecp );
625 mbedtls_free( ecp );
626 }
627 return( status );
628}
629#endif /* defined(MBEDTLS_ECP_C) */
630
Darryl Green940d72c2018-07-13 13:18:51 +0100631static psa_status_t psa_import_key_into_slot( key_slot_t *slot,
632 const uint8_t *data,
633 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100634{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200635 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100636
Darryl Green940d72c2018-07-13 13:18:51 +0100637 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100638 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100639 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100640 if( data_length > SIZE_MAX / 8 )
641 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100642 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200643 PSA_BYTES_TO_BITS( data_length ),
644 &slot->data.raw );
645 if( status != PSA_SUCCESS )
646 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200647 if( data_length != 0 )
648 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100649 }
650 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100651#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100652 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100653 {
Darryl Green940d72c2018-07-13 13:18:51 +0100654 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100655 data, data_length,
656 &slot->data.ecp );
657 if( status != PSA_SUCCESS )
658 return( status );
659 }
660 else
661#endif /* MBEDTLS_ECP_C */
Gilles Peskine969ac722018-01-28 18:16:59 +0100662#if defined(MBEDTLS_PK_PARSE_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100663 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
664 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100665 {
666 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100667 mbedtls_pk_context pk;
668 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200669
670 /* Parse the data. */
Darryl Green940d72c2018-07-13 13:18:51 +0100671 if( PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100672 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
673 else
674 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675 if( ret != 0 )
676 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200677
678 /* We have something that the pkparse module recognizes.
679 * If it has the expected type and passes any type-specific
680 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100681#if defined(MBEDTLS_RSA_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100682 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200683 status = psa_import_rsa_key( &pk, &slot->data.rsa );
684 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100685#endif /* MBEDTLS_RSA_C */
686#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100687 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
688 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200689 &pk, &slot->data.ecp );
690 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100691#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200692 {
693 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200694 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200695
Gilles Peskinec648d692018-06-28 08:46:13 +0200696 /* Free the content of the pk object only on error. On success,
697 * the content of the object has been stored in the slot. */
698 if( status != PSA_SUCCESS )
699 {
700 mbedtls_pk_free( &pk );
701 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100702 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100703 }
704 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100705#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100706 {
707 return( PSA_ERROR_NOT_SUPPORTED );
708 }
Darryl Green940d72c2018-07-13 13:18:51 +0100709 return( PSA_SUCCESS );
710}
711
Darryl Greend49a4992018-06-18 17:27:26 +0100712#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
713static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t key,
714 key_slot_t *p_slot )
715{
716 psa_status_t status = PSA_SUCCESS;
717 uint8_t *key_data = NULL;
718 size_t key_data_length = 0;
719
720 status = psa_load_persistent_key( key, &( p_slot )->type,
721 &( p_slot )->policy, &key_data,
722 &key_data_length );
723 if( status != PSA_SUCCESS )
724 goto exit;
725 status = psa_import_key_into_slot( p_slot,
726 key_data, key_data_length );
727exit:
728 psa_free_persistent_key_data( key_data, key_data_length );
729 return( status );
730}
731#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
732
Darryl Green06fd18d2018-07-16 11:21:11 +0100733/* Retrieve a key slot, occupied or not. */
734static psa_status_t psa_get_key_slot( psa_key_slot_t key,
735 key_slot_t **p_slot )
736{
737 GUARD_MODULE_INITIALIZED;
738
739 /* 0 is not a valid slot number under any circumstance. This
740 * implementation provides slots number 1 to N where N is the
741 * number of available slots. */
742 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
743 return( PSA_ERROR_INVALID_ARGUMENT );
744
745 *p_slot = &global_data.key_slots[key - 1];
Darryl Greend49a4992018-06-18 17:27:26 +0100746
747#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
748 if( ( *p_slot )->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
749 {
750 /* There are two circumstances this can occur: the key material has
751 * not yet been created, or the key exists in storage but has not yet
752 * been loaded into memory. */
753 if( ( *p_slot )->type == PSA_KEY_TYPE_NONE )
754 {
755 psa_status_t status = PSA_SUCCESS;
756 status = psa_load_persistent_key_into_slot( key, *p_slot );
757 if( status != PSA_ERROR_EMPTY_SLOT )
758 return( status );
759 }
760 }
761#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
762
Darryl Green06fd18d2018-07-16 11:21:11 +0100763 return( PSA_SUCCESS );
764}
765
766/* Retrieve an empty key slot (slot with no key data, but possibly
767 * with some metadata such as a policy). */
768static psa_status_t psa_get_empty_key_slot( psa_key_slot_t key,
769 key_slot_t **p_slot )
770{
771 psa_status_t status;
772 key_slot_t *slot = NULL;
773
774 *p_slot = NULL;
775
776 status = psa_get_key_slot( key, &slot );
777 if( status != PSA_SUCCESS )
778 return( status );
779
780 if( slot->type != PSA_KEY_TYPE_NONE )
781 return( PSA_ERROR_OCCUPIED_SLOT );
782
783 *p_slot = slot;
784 return( status );
785}
786
787/** Retrieve a slot which must contain a key. The key must have allow all the
788 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
789 * operations with this algorithm. */
790static psa_status_t psa_get_key_from_slot( psa_key_slot_t key,
791 key_slot_t **p_slot,
792 psa_key_usage_t usage,
793 psa_algorithm_t alg )
794{
795 psa_status_t status;
796 key_slot_t *slot = NULL;
797
798 *p_slot = NULL;
799
800 status = psa_get_key_slot( key, &slot );
801 if( status != PSA_SUCCESS )
802 return( status );
803 if( slot->type == PSA_KEY_TYPE_NONE )
804 return( PSA_ERROR_EMPTY_SLOT );
805
806 /* Enforce that usage policy for the key slot contains all the flags
807 * required by the usage parameter. There is one exception: public
808 * keys can always be exported, so we treat public key objects as
809 * if they had the export flag. */
810 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
811 usage &= ~PSA_KEY_USAGE_EXPORT;
812 if( ( slot->policy.usage & usage ) != usage )
813 return( PSA_ERROR_NOT_PERMITTED );
814 if( alg != 0 && ( alg != slot->policy.alg ) )
815 return( PSA_ERROR_NOT_PERMITTED );
816
817 *p_slot = slot;
818 return( PSA_SUCCESS );
819}
Darryl Green940d72c2018-07-13 13:18:51 +0100820
Darryl Green40225ba2018-11-15 14:48:15 +0000821static psa_status_t psa_remove_key_data_from_memory( key_slot_t *slot )
822{
823 if( slot->type == PSA_KEY_TYPE_NONE )
824 {
825 /* No key material to clean. */
826 }
827 else if( key_type_is_raw_bytes( slot->type ) )
828 {
829 mbedtls_free( slot->data.raw.data );
830 }
831 else
832#if defined(MBEDTLS_RSA_C)
833 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
834 {
835 mbedtls_rsa_free( slot->data.rsa );
836 mbedtls_free( slot->data.rsa );
837 }
838 else
839#endif /* defined(MBEDTLS_RSA_C) */
840#if defined(MBEDTLS_ECP_C)
841 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
842 {
843 mbedtls_ecp_keypair_free( slot->data.ecp );
844 mbedtls_free( slot->data.ecp );
845 }
846 else
847#endif /* defined(MBEDTLS_ECP_C) */
848 {
849 /* Shouldn't happen: the key type is not any type that we
850 * put in. */
851 return( PSA_ERROR_TAMPERING_DETECTED );
852 }
853
854 return( PSA_SUCCESS );
855}
856
Darryl Green940d72c2018-07-13 13:18:51 +0100857psa_status_t psa_import_key( psa_key_slot_t key,
858 psa_key_type_t type,
859 const uint8_t *data,
860 size_t data_length )
861{
862 key_slot_t *slot;
863 psa_status_t status;
864
865 status = psa_get_empty_key_slot( key, &slot );
866 if( status != PSA_SUCCESS )
867 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100868
869 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100870
871 status = psa_import_key_into_slot( slot, data, data_length );
872 if( status != PSA_SUCCESS )
873 {
874 slot->type = PSA_KEY_TYPE_NONE;
875 return( status );
876 }
877
Darryl Greend49a4992018-06-18 17:27:26 +0100878#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
879 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
880 {
881 /* Store in file location */
882 status = psa_save_persistent_key( key, slot->type, &slot->policy, data,
883 data_length );
884 if( status != PSA_SUCCESS )
885 {
886 (void) psa_remove_key_data_from_memory( slot );
887 slot->type = PSA_KEY_TYPE_NONE;
888 }
889 }
890#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
891
892 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100893}
894
Gilles Peskine2d277862018-06-18 15:41:12 +0200895psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100896{
897 key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100898 psa_status_t status = PSA_SUCCESS;
899 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100900
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200901 status = psa_get_key_slot( key, &slot );
902 if( status != PSA_SUCCESS )
903 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100904#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
905 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
906 {
907 storage_status = psa_destroy_persistent_key( key );
908 }
909#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
910 status = psa_remove_key_data_from_memory( slot );
911 /* Zeroize the slot to wipe metadata such as policies. */
912 mbedtls_zeroize( slot, sizeof( *slot ) );
913 if( status != PSA_SUCCESS )
914 return( status );
915 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100916}
917
Gilles Peskineb870b182018-07-06 16:02:09 +0200918/* Return the size of the key in the given slot, in bits. */
919static size_t psa_get_key_bits( const key_slot_t *slot )
920{
921 if( key_type_is_raw_bytes( slot->type ) )
922 return( slot->data.raw.bytes * 8 );
923#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200924 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100925 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200926#endif /* defined(MBEDTLS_RSA_C) */
927#if defined(MBEDTLS_ECP_C)
928 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
929 return( slot->data.ecp->grp.pbits );
930#endif /* defined(MBEDTLS_ECP_C) */
931 /* Shouldn't happen except on an empty slot. */
932 return( 0 );
933}
934
Gilles Peskine2d277862018-06-18 15:41:12 +0200935psa_status_t psa_get_key_information( psa_key_slot_t key,
936 psa_key_type_t *type,
937 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100938{
939 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200940 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100941
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100942 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200943 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100944 if( bits != NULL )
945 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200946 status = psa_get_key_slot( key, &slot );
947 if( status != PSA_SUCCESS )
948 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200949
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100950 if( slot->type == PSA_KEY_TYPE_NONE )
951 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200952 if( type != NULL )
953 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200954 if( bits != NULL )
955 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100956 return( PSA_SUCCESS );
957}
958
Darryl Greendd8fb772018-11-07 16:00:44 +0000959static psa_status_t psa_internal_export_key( key_slot_t *slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200960 uint8_t *data,
961 size_t data_size,
962 size_t *data_length,
963 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100964{
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100965 *data_length = 0;
966
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200967 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300968 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300969
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200970 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100971 {
972 if( slot->data.raw.bytes > data_size )
973 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +0100974 if( data_size != 0 )
975 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200976 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +0100977 memset( data + slot->data.raw.bytes, 0,
978 data_size - slot->data.raw.bytes );
979 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100980 *data_length = slot->data.raw.bytes;
981 return( PSA_SUCCESS );
982 }
Gilles Peskine188c71e2018-10-29 19:26:02 +0100983#if defined(MBEDTLS_ECP_C)
984 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
985 {
Darryl Greendd8fb772018-11-07 16:00:44 +0000986 psa_status_t status;
987
Gilles Peskine188c71e2018-10-29 19:26:02 +0100988 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
989 if( bytes > data_size )
990 return( PSA_ERROR_BUFFER_TOO_SMALL );
991 status = mbedtls_to_psa_error(
992 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
993 if( status != PSA_SUCCESS )
994 return( status );
995 memset( data + bytes, 0, data_size - bytes );
996 *data_length = bytes;
997 return( PSA_SUCCESS );
998 }
999#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001000 else
Moran Peker17e36e12018-05-02 12:55:20 +03001001 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001002#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001003 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001004 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001005 {
Moran Pekera998bc62018-04-16 18:16:20 +03001006 mbedtls_pk_context pk;
1007 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001008 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001009 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001010#if defined(MBEDTLS_RSA_C)
1011 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001012 pk.pk_info = &mbedtls_rsa_info;
1013 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001014#else
1015 return( PSA_ERROR_NOT_SUPPORTED );
1016#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001017 }
1018 else
1019 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001020#if defined(MBEDTLS_ECP_C)
1021 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001022 pk.pk_info = &mbedtls_eckey_info;
1023 pk.pk_ctx = slot->data.ecp;
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 }
Moran Pekerd7326592018-05-29 16:56:39 +03001028 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001029 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +03001030 else
1031 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +03001032 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001033 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001034 /* If data_size is 0 then data may be NULL and then the
1035 * call to memset would have undefined behavior. */
1036 if( data_size != 0 )
1037 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001038 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001039 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001040 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1041 * Move the data to the beginning and erase remaining data
1042 * at the original location. */
1043 if( 2 * (size_t) ret <= data_size )
1044 {
1045 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001046 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001047 }
1048 else if( (size_t) ret < data_size )
1049 {
1050 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001051 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001052 }
Moran Pekera998bc62018-04-16 18:16:20 +03001053 *data_length = ret;
1054 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001055 }
1056 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001057#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001058 {
1059 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001060 it is valid for a special-purpose implementation to omit
1061 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001062 return( PSA_ERROR_NOT_SUPPORTED );
1063 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001064 }
1065}
1066
Gilles Peskine2d277862018-06-18 15:41:12 +02001067psa_status_t psa_export_key( psa_key_slot_t key,
1068 uint8_t *data,
1069 size_t data_size,
1070 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001071{
Darryl Greendd8fb772018-11-07 16:00:44 +00001072 key_slot_t *slot;
1073 psa_status_t status;
1074
1075 /* Set the key to empty now, so that even when there are errors, we always
1076 * set data_length to a value between 0 and data_size. On error, setting
1077 * the key to empty is a good choice because an empty key representation is
1078 * unlikely to be accepted anywhere. */
1079 *data_length = 0;
1080
1081 /* Export requires the EXPORT flag. There is an exception for public keys,
1082 * which don't require any flag, but psa_get_key_from_slot takes
1083 * care of this. */
1084 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_EXPORT, 0 );
1085 if( status != PSA_SUCCESS )
1086 return( status );
1087 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001088 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001089}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001090
Gilles Peskine2d277862018-06-18 15:41:12 +02001091psa_status_t psa_export_public_key( psa_key_slot_t key,
1092 uint8_t *data,
1093 size_t data_size,
1094 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001095{
Darryl Greendd8fb772018-11-07 16:00:44 +00001096 key_slot_t *slot;
1097 psa_status_t status;
1098
1099 /* Set the key to empty now, so that even when there are errors, we always
1100 * set data_length to a value between 0 and data_size. On error, setting
1101 * the key to empty is a good choice because an empty key representation is
1102 * unlikely to be accepted anywhere. */
1103 *data_length = 0;
1104
1105 /* Exporting a public key doesn't require a usage flag. */
1106 status = psa_get_key_from_slot( key, &slot, 0, 0 );
1107 if( status != PSA_SUCCESS )
1108 return( status );
1109 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001110 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001111}
1112
Darryl Green0c6575a2018-11-07 16:05:30 +00001113#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1114static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t key,
1115 key_slot_t *slot,
1116 size_t bits )
1117{
1118 psa_status_t status;
1119 uint8_t *data;
1120 size_t key_length;
1121 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1122 data = mbedtls_calloc( 1, data_size );
1123 /* Get key data in export format */
1124 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1125 if( status != PSA_SUCCESS )
1126 {
1127 slot->type = PSA_KEY_TYPE_NONE;
1128 goto exit;
1129 }
1130 /* Store in file location */
1131 status = psa_save_persistent_key( key, slot->type, &slot->policy,
1132 data, key_length );
1133 if( status != PSA_SUCCESS )
1134 {
1135 slot->type = PSA_KEY_TYPE_NONE;
1136 }
1137exit:
1138 mbedtls_zeroize( data, key_length );
1139 mbedtls_free( data );
1140 return( status );
1141}
1142#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1143
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001144
1145
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001146/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001147/* Message digests */
1148/****************************************************************/
1149
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001150static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001151{
1152 switch( alg )
1153 {
1154#if defined(MBEDTLS_MD2_C)
1155 case PSA_ALG_MD2:
1156 return( &mbedtls_md2_info );
1157#endif
1158#if defined(MBEDTLS_MD4_C)
1159 case PSA_ALG_MD4:
1160 return( &mbedtls_md4_info );
1161#endif
1162#if defined(MBEDTLS_MD5_C)
1163 case PSA_ALG_MD5:
1164 return( &mbedtls_md5_info );
1165#endif
1166#if defined(MBEDTLS_RIPEMD160_C)
1167 case PSA_ALG_RIPEMD160:
1168 return( &mbedtls_ripemd160_info );
1169#endif
1170#if defined(MBEDTLS_SHA1_C)
1171 case PSA_ALG_SHA_1:
1172 return( &mbedtls_sha1_info );
1173#endif
1174#if defined(MBEDTLS_SHA256_C)
1175 case PSA_ALG_SHA_224:
1176 return( &mbedtls_sha224_info );
1177 case PSA_ALG_SHA_256:
1178 return( &mbedtls_sha256_info );
1179#endif
1180#if defined(MBEDTLS_SHA512_C)
1181 case PSA_ALG_SHA_384:
1182 return( &mbedtls_sha384_info );
1183 case PSA_ALG_SHA_512:
1184 return( &mbedtls_sha512_info );
1185#endif
1186 default:
1187 return( NULL );
1188 }
1189}
1190
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001191psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1192{
1193 switch( operation->alg )
1194 {
Gilles Peskine81736312018-06-26 15:04:31 +02001195 case 0:
1196 /* The object has (apparently) been initialized but it is not
1197 * in use. It's ok to call abort on such an object, and there's
1198 * nothing to do. */
1199 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001200#if defined(MBEDTLS_MD2_C)
1201 case PSA_ALG_MD2:
1202 mbedtls_md2_free( &operation->ctx.md2 );
1203 break;
1204#endif
1205#if defined(MBEDTLS_MD4_C)
1206 case PSA_ALG_MD4:
1207 mbedtls_md4_free( &operation->ctx.md4 );
1208 break;
1209#endif
1210#if defined(MBEDTLS_MD5_C)
1211 case PSA_ALG_MD5:
1212 mbedtls_md5_free( &operation->ctx.md5 );
1213 break;
1214#endif
1215#if defined(MBEDTLS_RIPEMD160_C)
1216 case PSA_ALG_RIPEMD160:
1217 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1218 break;
1219#endif
1220#if defined(MBEDTLS_SHA1_C)
1221 case PSA_ALG_SHA_1:
1222 mbedtls_sha1_free( &operation->ctx.sha1 );
1223 break;
1224#endif
1225#if defined(MBEDTLS_SHA256_C)
1226 case PSA_ALG_SHA_224:
1227 case PSA_ALG_SHA_256:
1228 mbedtls_sha256_free( &operation->ctx.sha256 );
1229 break;
1230#endif
1231#if defined(MBEDTLS_SHA512_C)
1232 case PSA_ALG_SHA_384:
1233 case PSA_ALG_SHA_512:
1234 mbedtls_sha512_free( &operation->ctx.sha512 );
1235 break;
1236#endif
1237 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001238 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001239 }
1240 operation->alg = 0;
1241 return( PSA_SUCCESS );
1242}
1243
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001244psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001245 psa_algorithm_t alg )
1246{
1247 int ret;
1248 operation->alg = 0;
1249 switch( alg )
1250 {
1251#if defined(MBEDTLS_MD2_C)
1252 case PSA_ALG_MD2:
1253 mbedtls_md2_init( &operation->ctx.md2 );
1254 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1255 break;
1256#endif
1257#if defined(MBEDTLS_MD4_C)
1258 case PSA_ALG_MD4:
1259 mbedtls_md4_init( &operation->ctx.md4 );
1260 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1261 break;
1262#endif
1263#if defined(MBEDTLS_MD5_C)
1264 case PSA_ALG_MD5:
1265 mbedtls_md5_init( &operation->ctx.md5 );
1266 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1267 break;
1268#endif
1269#if defined(MBEDTLS_RIPEMD160_C)
1270 case PSA_ALG_RIPEMD160:
1271 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1272 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1273 break;
1274#endif
1275#if defined(MBEDTLS_SHA1_C)
1276 case PSA_ALG_SHA_1:
1277 mbedtls_sha1_init( &operation->ctx.sha1 );
1278 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1279 break;
1280#endif
1281#if defined(MBEDTLS_SHA256_C)
1282 case PSA_ALG_SHA_224:
1283 mbedtls_sha256_init( &operation->ctx.sha256 );
1284 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1285 break;
1286 case PSA_ALG_SHA_256:
1287 mbedtls_sha256_init( &operation->ctx.sha256 );
1288 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1289 break;
1290#endif
1291#if defined(MBEDTLS_SHA512_C)
1292 case PSA_ALG_SHA_384:
1293 mbedtls_sha512_init( &operation->ctx.sha512 );
1294 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1295 break;
1296 case PSA_ALG_SHA_512:
1297 mbedtls_sha512_init( &operation->ctx.sha512 );
1298 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1299 break;
1300#endif
1301 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001302 return( PSA_ALG_IS_HASH( alg ) ?
1303 PSA_ERROR_NOT_SUPPORTED :
1304 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001305 }
1306 if( ret == 0 )
1307 operation->alg = alg;
1308 else
1309 psa_hash_abort( operation );
1310 return( mbedtls_to_psa_error( ret ) );
1311}
1312
1313psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1314 const uint8_t *input,
1315 size_t input_length )
1316{
1317 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001318
1319 /* Don't require hash implementations to behave correctly on a
1320 * zero-length input, which may have an invalid pointer. */
1321 if( input_length == 0 )
1322 return( PSA_SUCCESS );
1323
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001324 switch( operation->alg )
1325 {
1326#if defined(MBEDTLS_MD2_C)
1327 case PSA_ALG_MD2:
1328 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1329 input, input_length );
1330 break;
1331#endif
1332#if defined(MBEDTLS_MD4_C)
1333 case PSA_ALG_MD4:
1334 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1335 input, input_length );
1336 break;
1337#endif
1338#if defined(MBEDTLS_MD5_C)
1339 case PSA_ALG_MD5:
1340 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1341 input, input_length );
1342 break;
1343#endif
1344#if defined(MBEDTLS_RIPEMD160_C)
1345 case PSA_ALG_RIPEMD160:
1346 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1347 input, input_length );
1348 break;
1349#endif
1350#if defined(MBEDTLS_SHA1_C)
1351 case PSA_ALG_SHA_1:
1352 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1353 input, input_length );
1354 break;
1355#endif
1356#if defined(MBEDTLS_SHA256_C)
1357 case PSA_ALG_SHA_224:
1358 case PSA_ALG_SHA_256:
1359 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1360 input, input_length );
1361 break;
1362#endif
1363#if defined(MBEDTLS_SHA512_C)
1364 case PSA_ALG_SHA_384:
1365 case PSA_ALG_SHA_512:
1366 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1367 input, input_length );
1368 break;
1369#endif
1370 default:
1371 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1372 break;
1373 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001374
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001375 if( ret != 0 )
1376 psa_hash_abort( operation );
1377 return( mbedtls_to_psa_error( ret ) );
1378}
1379
1380psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1381 uint8_t *hash,
1382 size_t hash_size,
1383 size_t *hash_length )
1384{
itayzafrir40835d42018-08-02 13:14:17 +03001385 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001386 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001387 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001388
1389 /* Fill the output buffer with something that isn't a valid hash
1390 * (barring an attack on the hash and deliberately-crafted input),
1391 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001392 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001393 /* If hash_size is 0 then hash may be NULL and then the
1394 * call to memset would have undefined behavior. */
1395 if( hash_size != 0 )
1396 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001397
1398 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001399 {
1400 status = PSA_ERROR_BUFFER_TOO_SMALL;
1401 goto exit;
1402 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001403
1404 switch( operation->alg )
1405 {
1406#if defined(MBEDTLS_MD2_C)
1407 case PSA_ALG_MD2:
1408 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1409 break;
1410#endif
1411#if defined(MBEDTLS_MD4_C)
1412 case PSA_ALG_MD4:
1413 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1414 break;
1415#endif
1416#if defined(MBEDTLS_MD5_C)
1417 case PSA_ALG_MD5:
1418 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1419 break;
1420#endif
1421#if defined(MBEDTLS_RIPEMD160_C)
1422 case PSA_ALG_RIPEMD160:
1423 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1424 break;
1425#endif
1426#if defined(MBEDTLS_SHA1_C)
1427 case PSA_ALG_SHA_1:
1428 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1429 break;
1430#endif
1431#if defined(MBEDTLS_SHA256_C)
1432 case PSA_ALG_SHA_224:
1433 case PSA_ALG_SHA_256:
1434 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1435 break;
1436#endif
1437#if defined(MBEDTLS_SHA512_C)
1438 case PSA_ALG_SHA_384:
1439 case PSA_ALG_SHA_512:
1440 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1441 break;
1442#endif
1443 default:
1444 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1445 break;
1446 }
itayzafrir40835d42018-08-02 13:14:17 +03001447 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001448
itayzafrir40835d42018-08-02 13:14:17 +03001449exit:
1450 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001451 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001452 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001453 return( psa_hash_abort( operation ) );
1454 }
1455 else
1456 {
1457 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001458 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001459 }
1460}
1461
Gilles Peskine2d277862018-06-18 15:41:12 +02001462psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1463 const uint8_t *hash,
1464 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001465{
1466 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1467 size_t actual_hash_length;
1468 psa_status_t status = psa_hash_finish( operation,
1469 actual_hash, sizeof( actual_hash ),
1470 &actual_hash_length );
1471 if( status != PSA_SUCCESS )
1472 return( status );
1473 if( actual_hash_length != hash_length )
1474 return( PSA_ERROR_INVALID_SIGNATURE );
1475 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1476 return( PSA_ERROR_INVALID_SIGNATURE );
1477 return( PSA_SUCCESS );
1478}
1479
1480
1481
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001482/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001483/* MAC */
1484/****************************************************************/
1485
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001486static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001487 psa_algorithm_t alg,
1488 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001489 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001490 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001491{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001492 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001493 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001494
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001495 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001496 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001497
Gilles Peskine8c9def32018-02-08 10:02:12 +01001498 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1499 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001500 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001501 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001502 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001503 mode = MBEDTLS_MODE_STREAM;
1504 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001505 case PSA_ALG_CTR:
1506 mode = MBEDTLS_MODE_CTR;
1507 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001508 case PSA_ALG_CFB:
1509 mode = MBEDTLS_MODE_CFB;
1510 break;
1511 case PSA_ALG_OFB:
1512 mode = MBEDTLS_MODE_OFB;
1513 break;
1514 case PSA_ALG_CBC_NO_PADDING:
1515 mode = MBEDTLS_MODE_CBC;
1516 break;
1517 case PSA_ALG_CBC_PKCS7:
1518 mode = MBEDTLS_MODE_CBC;
1519 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001520 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001521 mode = MBEDTLS_MODE_CCM;
1522 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001523 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001524 mode = MBEDTLS_MODE_GCM;
1525 break;
1526 default:
1527 return( NULL );
1528 }
1529 }
1530 else if( alg == PSA_ALG_CMAC )
1531 mode = MBEDTLS_MODE_ECB;
1532 else if( alg == PSA_ALG_GMAC )
1533 mode = MBEDTLS_MODE_GCM;
1534 else
1535 return( NULL );
1536
1537 switch( key_type )
1538 {
1539 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001540 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001541 break;
1542 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001543 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1544 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001545 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001546 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001547 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001548 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001549 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1550 * but two-key Triple-DES is functionally three-key Triple-DES
1551 * with K1=K3, so that's how we present it to mbedtls. */
1552 if( key_bits == 128 )
1553 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001554 break;
1555 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001556 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001557 break;
1558 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001559 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001560 break;
1561 default:
1562 return( NULL );
1563 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001564 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001565 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001566
Jaeden Amero23bbb752018-06-26 14:16:54 +01001567 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1568 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001569}
1570
Gilles Peskinea05219c2018-11-16 16:02:56 +01001571#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001572static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001573{
Gilles Peskine2d277862018-06-18 15:41:12 +02001574 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001575 {
1576 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001577 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001578 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001579 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001580 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001581 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001582 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001583 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001584 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001585 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001586 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001587 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001588 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001589 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001590 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001591 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001592 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001593 return( 128 );
1594 default:
1595 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001596 }
1597}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001598#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001599
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001600/* Initialize the MAC operation structure. Once this function has been
1601 * called, psa_mac_abort can run and will do the right thing. */
1602static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1603 psa_algorithm_t alg )
1604{
1605 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1606
1607 operation->alg = alg;
1608 operation->key_set = 0;
1609 operation->iv_set = 0;
1610 operation->iv_required = 0;
1611 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001612 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001613
1614#if defined(MBEDTLS_CMAC_C)
1615 if( alg == PSA_ALG_CMAC )
1616 {
1617 operation->iv_required = 0;
1618 mbedtls_cipher_init( &operation->ctx.cmac );
1619 status = PSA_SUCCESS;
1620 }
1621 else
1622#endif /* MBEDTLS_CMAC_C */
1623#if defined(MBEDTLS_MD_C)
1624 if( PSA_ALG_IS_HMAC( operation->alg ) )
1625 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001626 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1627 operation->ctx.hmac.hash_ctx.alg = 0;
1628 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001629 }
1630 else
1631#endif /* MBEDTLS_MD_C */
1632 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001633 if( ! PSA_ALG_IS_MAC( alg ) )
1634 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001635 }
1636
1637 if( status != PSA_SUCCESS )
1638 memset( operation, 0, sizeof( *operation ) );
1639 return( status );
1640}
1641
Gilles Peskine01126fa2018-07-12 17:04:55 +02001642#if defined(MBEDTLS_MD_C)
1643static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1644{
1645 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1646 return( psa_hash_abort( &hmac->hash_ctx ) );
1647}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001648
1649static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1650{
1651 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1652 memset( hmac, 0, sizeof( *hmac ) );
1653}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001654#endif /* MBEDTLS_MD_C */
1655
Gilles Peskine8c9def32018-02-08 10:02:12 +01001656psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1657{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001658 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001659 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001660 /* The object has (apparently) been initialized but it is not
1661 * in use. It's ok to call abort on such an object, and there's
1662 * nothing to do. */
1663 return( PSA_SUCCESS );
1664 }
1665 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001666#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001667 if( operation->alg == PSA_ALG_CMAC )
1668 {
1669 mbedtls_cipher_free( &operation->ctx.cmac );
1670 }
1671 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001672#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001673#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001674 if( PSA_ALG_IS_HMAC( operation->alg ) )
1675 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001676 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001677 }
1678 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001679#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001680 {
1681 /* Sanity check (shouldn't happen: operation->alg should
1682 * always have been initialized to a valid value). */
1683 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001684 }
Moran Peker41deec42018-04-04 15:43:05 +03001685
Gilles Peskine8c9def32018-02-08 10:02:12 +01001686 operation->alg = 0;
1687 operation->key_set = 0;
1688 operation->iv_set = 0;
1689 operation->iv_required = 0;
1690 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001691 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001692
Gilles Peskine8c9def32018-02-08 10:02:12 +01001693 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001694
1695bad_state:
1696 /* If abort is called on an uninitialized object, we can't trust
1697 * anything. Wipe the object in case it contains confidential data.
1698 * This may result in a memory leak if a pointer gets overwritten,
1699 * but it's too late to do anything about this. */
1700 memset( operation, 0, sizeof( *operation ) );
1701 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001702}
1703
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001704#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001705static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001706 size_t key_bits,
1707 key_slot_t *slot,
1708 const mbedtls_cipher_info_t *cipher_info )
1709{
1710 int ret;
1711
1712 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001713
1714 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1715 if( ret != 0 )
1716 return( ret );
1717
1718 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1719 slot->data.raw.data,
1720 key_bits );
1721 return( ret );
1722}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001723#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001724
Gilles Peskine248051a2018-06-20 16:09:38 +02001725#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001726static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1727 const uint8_t *key,
1728 size_t key_length,
1729 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001730{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001731 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001732 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001733 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001734 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001735 psa_status_t status;
1736
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001737 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1738 * overflow below. This should never trigger if the hash algorithm
1739 * is implemented correctly. */
1740 /* The size checks against the ipad and opad buffers cannot be written
1741 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1742 * because that triggers -Wlogical-op on GCC 7.3. */
1743 if( block_size > sizeof( ipad ) )
1744 return( PSA_ERROR_NOT_SUPPORTED );
1745 if( block_size > sizeof( hmac->opad ) )
1746 return( PSA_ERROR_NOT_SUPPORTED );
1747 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001748 return( PSA_ERROR_NOT_SUPPORTED );
1749
Gilles Peskined223b522018-06-11 18:12:58 +02001750 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001751 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001752 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001753 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001754 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001755 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001756 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001757 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001758 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001759 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001760 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001761 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001762 }
Gilles Peskine96889972018-07-12 17:07:03 +02001763 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1764 * but it is permitted. It is common when HMAC is used in HKDF, for
1765 * example. Don't call `memcpy` in the 0-length because `key` could be
1766 * an invalid pointer which would make the behavior undefined. */
1767 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001768 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001769
Gilles Peskined223b522018-06-11 18:12:58 +02001770 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1771 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001772 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001773 ipad[i] ^= 0x36;
1774 memset( ipad + key_length, 0x36, block_size - key_length );
1775
1776 /* Copy the key material from ipad to opad, flipping the requisite bits,
1777 * and filling the rest of opad with the requisite constant. */
1778 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001779 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1780 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001781
Gilles Peskine01126fa2018-07-12 17:04:55 +02001782 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001783 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001784 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001785
Gilles Peskine01126fa2018-07-12 17:04:55 +02001786 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001787
1788cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001789 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001790
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001791 return( status );
1792}
Gilles Peskine248051a2018-06-20 16:09:38 +02001793#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001794
Gilles Peskine89167cb2018-07-08 20:12:23 +02001795static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1796 psa_key_slot_t key,
1797 psa_algorithm_t alg,
1798 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001799{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001800 psa_status_t status;
1801 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001802 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001803 psa_key_usage_t usage =
1804 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001805 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001806 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001807
Gilles Peskined911eb72018-08-14 15:18:45 +02001808 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001809 if( status != PSA_SUCCESS )
1810 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001811 if( is_sign )
1812 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001813
Gilles Peskine89167cb2018-07-08 20:12:23 +02001814 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001815 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001816 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001817 key_bits = psa_get_key_bits( slot );
1818
Gilles Peskine8c9def32018-02-08 10:02:12 +01001819#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001820 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001821 {
1822 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001823 mbedtls_cipher_info_from_psa( full_length_alg,
1824 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001825 int ret;
1826 if( cipher_info == NULL )
1827 {
1828 status = PSA_ERROR_NOT_SUPPORTED;
1829 goto exit;
1830 }
1831 operation->mac_size = cipher_info->block_size;
1832 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1833 status = mbedtls_to_psa_error( ret );
1834 }
1835 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001836#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001837#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001838 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001839 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001840 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001841 if( hash_alg == 0 )
1842 {
1843 status = PSA_ERROR_NOT_SUPPORTED;
1844 goto exit;
1845 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001846
1847 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1848 /* Sanity check. This shouldn't fail on a valid configuration. */
1849 if( operation->mac_size == 0 ||
1850 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1851 {
1852 status = PSA_ERROR_NOT_SUPPORTED;
1853 goto exit;
1854 }
1855
Gilles Peskine01126fa2018-07-12 17:04:55 +02001856 if( slot->type != PSA_KEY_TYPE_HMAC )
1857 {
1858 status = PSA_ERROR_INVALID_ARGUMENT;
1859 goto exit;
1860 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001861
Gilles Peskine01126fa2018-07-12 17:04:55 +02001862 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1863 slot->data.raw.data,
1864 slot->data.raw.bytes,
1865 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001866 }
1867 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001868#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001869 {
1870 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001871 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001872
Gilles Peskined911eb72018-08-14 15:18:45 +02001873 if( truncated == 0 )
1874 {
1875 /* The "normal" case: untruncated algorithm. Nothing to do. */
1876 }
1877 else if( truncated < 4 )
1878 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001879 /* A very short MAC is too short for security since it can be
1880 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1881 * so we make this our minimum, even though 32 bits is still
1882 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001883 status = PSA_ERROR_NOT_SUPPORTED;
1884 }
1885 else if( truncated > operation->mac_size )
1886 {
1887 /* It's impossible to "truncate" to a larger length. */
1888 status = PSA_ERROR_INVALID_ARGUMENT;
1889 }
1890 else
1891 operation->mac_size = truncated;
1892
Gilles Peskinefbfac682018-07-08 20:51:54 +02001893exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001894 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001895 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001896 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001897 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001898 else
1899 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001900 operation->key_set = 1;
1901 }
1902 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001903}
1904
Gilles Peskine89167cb2018-07-08 20:12:23 +02001905psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1906 psa_key_slot_t key,
1907 psa_algorithm_t alg )
1908{
1909 return( psa_mac_setup( operation, key, alg, 1 ) );
1910}
1911
1912psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1913 psa_key_slot_t key,
1914 psa_algorithm_t alg )
1915{
1916 return( psa_mac_setup( operation, key, alg, 0 ) );
1917}
1918
Gilles Peskine8c9def32018-02-08 10:02:12 +01001919psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1920 const uint8_t *input,
1921 size_t input_length )
1922{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001923 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001924 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001925 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001926 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001927 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001928 operation->has_input = 1;
1929
Gilles Peskine8c9def32018-02-08 10:02:12 +01001930#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001931 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001932 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001933 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1934 input, input_length );
1935 status = mbedtls_to_psa_error( ret );
1936 }
1937 else
1938#endif /* MBEDTLS_CMAC_C */
1939#if defined(MBEDTLS_MD_C)
1940 if( PSA_ALG_IS_HMAC( operation->alg ) )
1941 {
1942 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1943 input_length );
1944 }
1945 else
1946#endif /* MBEDTLS_MD_C */
1947 {
1948 /* This shouldn't happen if `operation` was initialized by
1949 * a setup function. */
1950 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001951 }
1952
Gilles Peskinefbfac682018-07-08 20:51:54 +02001953cleanup:
1954 if( status != PSA_SUCCESS )
1955 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001956 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001957}
1958
Gilles Peskine01126fa2018-07-12 17:04:55 +02001959#if defined(MBEDTLS_MD_C)
1960static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1961 uint8_t *mac,
1962 size_t mac_size )
1963{
1964 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1965 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1966 size_t hash_size = 0;
1967 size_t block_size = psa_get_hash_block_size( hash_alg );
1968 psa_status_t status;
1969
Gilles Peskine01126fa2018-07-12 17:04:55 +02001970 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1971 if( status != PSA_SUCCESS )
1972 return( status );
1973 /* From here on, tmp needs to be wiped. */
1974
1975 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1976 if( status != PSA_SUCCESS )
1977 goto exit;
1978
1979 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1980 if( status != PSA_SUCCESS )
1981 goto exit;
1982
1983 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1984 if( status != PSA_SUCCESS )
1985 goto exit;
1986
Gilles Peskined911eb72018-08-14 15:18:45 +02001987 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1988 if( status != PSA_SUCCESS )
1989 goto exit;
1990
1991 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001992
1993exit:
1994 mbedtls_zeroize( tmp, hash_size );
1995 return( status );
1996}
1997#endif /* MBEDTLS_MD_C */
1998
mohammad16036df908f2018-04-02 08:34:15 -07001999static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002000 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002001 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002002{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002003 if( ! operation->key_set )
2004 return( PSA_ERROR_BAD_STATE );
2005 if( operation->iv_required && ! operation->iv_set )
2006 return( PSA_ERROR_BAD_STATE );
2007
Gilles Peskine8c9def32018-02-08 10:02:12 +01002008 if( mac_size < operation->mac_size )
2009 return( PSA_ERROR_BUFFER_TOO_SMALL );
2010
Gilles Peskine8c9def32018-02-08 10:02:12 +01002011#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002012 if( operation->alg == PSA_ALG_CMAC )
2013 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002014 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2015 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2016 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002017 memcpy( mac, tmp, operation->mac_size );
Gilles Peskined911eb72018-08-14 15:18:45 +02002018 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002019 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002020 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002021 else
2022#endif /* MBEDTLS_CMAC_C */
2023#if defined(MBEDTLS_MD_C)
2024 if( PSA_ALG_IS_HMAC( operation->alg ) )
2025 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002026 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002027 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002028 }
2029 else
2030#endif /* MBEDTLS_MD_C */
2031 {
2032 /* This shouldn't happen if `operation` was initialized by
2033 * a setup function. */
2034 return( PSA_ERROR_BAD_STATE );
2035 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002036}
2037
Gilles Peskineacd4be32018-07-08 19:56:25 +02002038psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2039 uint8_t *mac,
2040 size_t mac_size,
2041 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002042{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002043 psa_status_t status;
2044
2045 /* Fill the output buffer with something that isn't a valid mac
2046 * (barring an attack on the mac and deliberately-crafted input),
2047 * in case the caller doesn't check the return status properly. */
2048 *mac_length = mac_size;
2049 /* If mac_size is 0 then mac may be NULL and then the
2050 * call to memset would have undefined behavior. */
2051 if( mac_size != 0 )
2052 memset( mac, '!', mac_size );
2053
Gilles Peskine89167cb2018-07-08 20:12:23 +02002054 if( ! operation->is_sign )
2055 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002056 status = PSA_ERROR_BAD_STATE;
2057 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002058 }
mohammad16036df908f2018-04-02 08:34:15 -07002059
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002060 status = psa_mac_finish_internal( operation, mac, mac_size );
2061
2062cleanup:
2063 if( status == PSA_SUCCESS )
2064 {
2065 status = psa_mac_abort( operation );
2066 if( status == PSA_SUCCESS )
2067 *mac_length = operation->mac_size;
2068 else
2069 memset( mac, '!', mac_size );
2070 }
2071 else
2072 psa_mac_abort( operation );
2073 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002074}
2075
Gilles Peskineacd4be32018-07-08 19:56:25 +02002076psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2077 const uint8_t *mac,
2078 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002079{
Gilles Peskine828ed142018-06-18 23:25:51 +02002080 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002081 psa_status_t status;
2082
Gilles Peskine89167cb2018-07-08 20:12:23 +02002083 if( operation->is_sign )
2084 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002085 status = PSA_ERROR_BAD_STATE;
2086 goto cleanup;
2087 }
2088 if( operation->mac_size != mac_length )
2089 {
2090 status = PSA_ERROR_INVALID_SIGNATURE;
2091 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002092 }
mohammad16036df908f2018-04-02 08:34:15 -07002093
2094 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002095 actual_mac, sizeof( actual_mac ) );
2096
2097 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2098 status = PSA_ERROR_INVALID_SIGNATURE;
2099
2100cleanup:
2101 if( status == PSA_SUCCESS )
2102 status = psa_mac_abort( operation );
2103 else
2104 psa_mac_abort( operation );
2105
Gilles Peskine99b7d6b2018-08-21 14:56:19 +02002106 mbedtls_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002107
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002108 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002109}
2110
2111
Gilles Peskine20035e32018-02-03 22:44:14 +01002112
Gilles Peskine20035e32018-02-03 22:44:14 +01002113/****************************************************************/
2114/* Asymmetric cryptography */
2115/****************************************************************/
2116
Gilles Peskine2b450e32018-06-27 15:42:46 +02002117#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002118/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002119 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002120static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2121 size_t hash_length,
2122 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002123{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002124 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002125 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002126 *md_alg = mbedtls_md_get_type( md_info );
2127
2128 /* The Mbed TLS RSA module uses an unsigned int for hash length
2129 * parameters. Validate that it fits so that we don't risk an
2130 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002131#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002132 if( hash_length > UINT_MAX )
2133 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002134#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002135
2136#if defined(MBEDTLS_PKCS1_V15)
2137 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2138 * must be correct. */
2139 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2140 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002141 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002142 if( md_info == NULL )
2143 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002144 if( mbedtls_md_get_size( md_info ) != hash_length )
2145 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002146 }
2147#endif /* MBEDTLS_PKCS1_V15 */
2148
2149#if defined(MBEDTLS_PKCS1_V21)
2150 /* PSS requires a hash internally. */
2151 if( PSA_ALG_IS_RSA_PSS( alg ) )
2152 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002153 if( md_info == NULL )
2154 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002155 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002156#endif /* MBEDTLS_PKCS1_V21 */
2157
Gilles Peskine61b91d42018-06-08 16:09:36 +02002158 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002159}
2160
Gilles Peskine2b450e32018-06-27 15:42:46 +02002161static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2162 psa_algorithm_t alg,
2163 const uint8_t *hash,
2164 size_t hash_length,
2165 uint8_t *signature,
2166 size_t signature_size,
2167 size_t *signature_length )
2168{
2169 psa_status_t status;
2170 int ret;
2171 mbedtls_md_type_t md_alg;
2172
2173 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2174 if( status != PSA_SUCCESS )
2175 return( status );
2176
Gilles Peskine630a18a2018-06-29 17:49:35 +02002177 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002178 return( PSA_ERROR_BUFFER_TOO_SMALL );
2179
2180#if defined(MBEDTLS_PKCS1_V15)
2181 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2182 {
2183 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2184 MBEDTLS_MD_NONE );
2185 ret = mbedtls_rsa_pkcs1_sign( rsa,
2186 mbedtls_ctr_drbg_random,
2187 &global_data.ctr_drbg,
2188 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002189 md_alg,
2190 (unsigned int) hash_length,
2191 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002192 signature );
2193 }
2194 else
2195#endif /* MBEDTLS_PKCS1_V15 */
2196#if defined(MBEDTLS_PKCS1_V21)
2197 if( PSA_ALG_IS_RSA_PSS( alg ) )
2198 {
2199 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2200 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2201 mbedtls_ctr_drbg_random,
2202 &global_data.ctr_drbg,
2203 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002204 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002205 (unsigned int) hash_length,
2206 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002207 signature );
2208 }
2209 else
2210#endif /* MBEDTLS_PKCS1_V21 */
2211 {
2212 return( PSA_ERROR_INVALID_ARGUMENT );
2213 }
2214
2215 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002216 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002217 return( mbedtls_to_psa_error( ret ) );
2218}
2219
2220static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2221 psa_algorithm_t alg,
2222 const uint8_t *hash,
2223 size_t hash_length,
2224 const uint8_t *signature,
2225 size_t signature_length )
2226{
2227 psa_status_t status;
2228 int ret;
2229 mbedtls_md_type_t md_alg;
2230
2231 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2232 if( status != PSA_SUCCESS )
2233 return( status );
2234
Gilles Peskine630a18a2018-06-29 17:49:35 +02002235 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002236 return( PSA_ERROR_BUFFER_TOO_SMALL );
2237
2238#if defined(MBEDTLS_PKCS1_V15)
2239 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2240 {
2241 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2242 MBEDTLS_MD_NONE );
2243 ret = mbedtls_rsa_pkcs1_verify( rsa,
2244 mbedtls_ctr_drbg_random,
2245 &global_data.ctr_drbg,
2246 MBEDTLS_RSA_PUBLIC,
2247 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002248 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002249 hash,
2250 signature );
2251 }
2252 else
2253#endif /* MBEDTLS_PKCS1_V15 */
2254#if defined(MBEDTLS_PKCS1_V21)
2255 if( PSA_ALG_IS_RSA_PSS( alg ) )
2256 {
2257 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2258 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2259 mbedtls_ctr_drbg_random,
2260 &global_data.ctr_drbg,
2261 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002262 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002263 (unsigned int) hash_length,
2264 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002265 signature );
2266 }
2267 else
2268#endif /* MBEDTLS_PKCS1_V21 */
2269 {
2270 return( PSA_ERROR_INVALID_ARGUMENT );
2271 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002272
2273 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2274 * the rest of the signature is invalid". This has little use in
2275 * practice and PSA doesn't report this distinction. */
2276 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2277 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002278 return( mbedtls_to_psa_error( ret ) );
2279}
2280#endif /* MBEDTLS_RSA_C */
2281
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002282#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002283/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2284 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2285 * (even though these functions don't modify it). */
2286static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2287 psa_algorithm_t alg,
2288 const uint8_t *hash,
2289 size_t hash_length,
2290 uint8_t *signature,
2291 size_t signature_size,
2292 size_t *signature_length )
2293{
2294 int ret;
2295 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002296 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002297 mbedtls_mpi_init( &r );
2298 mbedtls_mpi_init( &s );
2299
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002300 if( signature_size < 2 * curve_bytes )
2301 {
2302 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2303 goto cleanup;
2304 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002305
Gilles Peskinea05219c2018-11-16 16:02:56 +01002306#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002307 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2308 {
2309 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2310 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2311 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2312 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2313 hash, hash_length,
2314 md_alg ) );
2315 }
2316 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002317#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002318 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002319 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002320 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2321 hash, hash_length,
2322 mbedtls_ctr_drbg_random,
2323 &global_data.ctr_drbg ) );
2324 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002325
2326 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2327 signature,
2328 curve_bytes ) );
2329 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2330 signature + curve_bytes,
2331 curve_bytes ) );
2332
2333cleanup:
2334 mbedtls_mpi_free( &r );
2335 mbedtls_mpi_free( &s );
2336 if( ret == 0 )
2337 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002338 return( mbedtls_to_psa_error( ret ) );
2339}
2340
2341static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2342 const uint8_t *hash,
2343 size_t hash_length,
2344 const uint8_t *signature,
2345 size_t signature_length )
2346{
2347 int ret;
2348 mbedtls_mpi r, s;
2349 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2350 mbedtls_mpi_init( &r );
2351 mbedtls_mpi_init( &s );
2352
2353 if( signature_length != 2 * curve_bytes )
2354 return( PSA_ERROR_INVALID_SIGNATURE );
2355
2356 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2357 signature,
2358 curve_bytes ) );
2359 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2360 signature + curve_bytes,
2361 curve_bytes ) );
2362
2363 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2364 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002365
2366cleanup:
2367 mbedtls_mpi_free( &r );
2368 mbedtls_mpi_free( &s );
2369 return( mbedtls_to_psa_error( ret ) );
2370}
2371#endif /* MBEDTLS_ECDSA_C */
2372
Gilles Peskine61b91d42018-06-08 16:09:36 +02002373psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2374 psa_algorithm_t alg,
2375 const uint8_t *hash,
2376 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002377 uint8_t *signature,
2378 size_t signature_size,
2379 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002380{
2381 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002382 psa_status_t status;
2383
2384 *signature_length = signature_size;
2385
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002386 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2387 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002388 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002389 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002390 {
2391 status = PSA_ERROR_INVALID_ARGUMENT;
2392 goto exit;
2393 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002394
Gilles Peskine20035e32018-02-03 22:44:14 +01002395#if defined(MBEDTLS_RSA_C)
2396 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2397 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002398 status = psa_rsa_sign( slot->data.rsa,
2399 alg,
2400 hash, hash_length,
2401 signature, signature_size,
2402 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002403 }
2404 else
2405#endif /* defined(MBEDTLS_RSA_C) */
2406#if defined(MBEDTLS_ECP_C)
2407 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2408 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002409#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002410 if(
2411#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2412 PSA_ALG_IS_ECDSA( alg )
2413#else
2414 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2415#endif
2416 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002417 status = psa_ecdsa_sign( slot->data.ecp,
2418 alg,
2419 hash, hash_length,
2420 signature, signature_size,
2421 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002422 else
2423#endif /* defined(MBEDTLS_ECDSA_C) */
2424 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002425 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002426 }
itayzafrir5c753392018-05-08 11:18:38 +03002427 }
2428 else
2429#endif /* defined(MBEDTLS_ECP_C) */
2430 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002431 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002432 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002433
2434exit:
2435 /* Fill the unused part of the output buffer (the whole buffer on error,
2436 * the trailing part on success) with something that isn't a valid mac
2437 * (barring an attack on the mac and deliberately-crafted input),
2438 * in case the caller doesn't check the return status properly. */
2439 if( status == PSA_SUCCESS )
2440 memset( signature + *signature_length, '!',
2441 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002442 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002443 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002444 /* If signature_size is 0 then we have nothing to do. We must not call
2445 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002446 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002447}
2448
2449psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2450 psa_algorithm_t alg,
2451 const uint8_t *hash,
2452 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002453 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002454 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002455{
2456 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002457 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002458
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002459 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2460 if( status != PSA_SUCCESS )
2461 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002462
Gilles Peskine61b91d42018-06-08 16:09:36 +02002463#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002464 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002465 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002466 return( psa_rsa_verify( slot->data.rsa,
2467 alg,
2468 hash, hash_length,
2469 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002470 }
2471 else
2472#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002473#if defined(MBEDTLS_ECP_C)
2474 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2475 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002476#if defined(MBEDTLS_ECDSA_C)
2477 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002478 return( psa_ecdsa_verify( slot->data.ecp,
2479 hash, hash_length,
2480 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002481 else
2482#endif /* defined(MBEDTLS_ECDSA_C) */
2483 {
2484 return( PSA_ERROR_INVALID_ARGUMENT );
2485 }
itayzafrir5c753392018-05-08 11:18:38 +03002486 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002487 else
2488#endif /* defined(MBEDTLS_ECP_C) */
2489 {
2490 return( PSA_ERROR_NOT_SUPPORTED );
2491 }
2492}
2493
Gilles Peskine072ac562018-06-30 00:21:29 +02002494#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2495static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2496 mbedtls_rsa_context *rsa )
2497{
2498 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2499 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2500 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2501 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2502}
2503#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2504
Gilles Peskine61b91d42018-06-08 16:09:36 +02002505psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2506 psa_algorithm_t alg,
2507 const uint8_t *input,
2508 size_t input_length,
2509 const uint8_t *salt,
2510 size_t salt_length,
2511 uint8_t *output,
2512 size_t output_size,
2513 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002514{
2515 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002516 psa_status_t status;
2517
Darryl Green5cc689a2018-07-24 15:34:10 +01002518 (void) input;
2519 (void) input_length;
2520 (void) salt;
2521 (void) output;
2522 (void) output_size;
2523
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002524 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002525
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002526 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2527 return( PSA_ERROR_INVALID_ARGUMENT );
2528
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002529 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2530 if( status != PSA_SUCCESS )
2531 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002532 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2533 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002534 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002535
2536#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002537 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002538 {
2539 mbedtls_rsa_context *rsa = slot->data.rsa;
2540 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002541 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002542 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002543#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002544 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002545 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002546 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2547 mbedtls_ctr_drbg_random,
2548 &global_data.ctr_drbg,
2549 MBEDTLS_RSA_PUBLIC,
2550 input_length,
2551 input,
2552 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002553 }
2554 else
2555#endif /* MBEDTLS_PKCS1_V15 */
2556#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002557 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002558 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002559 psa_rsa_oaep_set_padding_mode( alg, rsa );
2560 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2561 mbedtls_ctr_drbg_random,
2562 &global_data.ctr_drbg,
2563 MBEDTLS_RSA_PUBLIC,
2564 salt, salt_length,
2565 input_length,
2566 input,
2567 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002568 }
2569 else
2570#endif /* MBEDTLS_PKCS1_V21 */
2571 {
2572 return( PSA_ERROR_INVALID_ARGUMENT );
2573 }
2574 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002575 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002576 return( mbedtls_to_psa_error( ret ) );
2577 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002578 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002579#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002580 {
2581 return( PSA_ERROR_NOT_SUPPORTED );
2582 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002583}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002584
Gilles Peskine61b91d42018-06-08 16:09:36 +02002585psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2586 psa_algorithm_t alg,
2587 const uint8_t *input,
2588 size_t input_length,
2589 const uint8_t *salt,
2590 size_t salt_length,
2591 uint8_t *output,
2592 size_t output_size,
2593 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002594{
2595 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002596 psa_status_t status;
2597
Darryl Green5cc689a2018-07-24 15:34:10 +01002598 (void) input;
2599 (void) input_length;
2600 (void) salt;
2601 (void) output;
2602 (void) output_size;
2603
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002604 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002605
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002606 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2607 return( PSA_ERROR_INVALID_ARGUMENT );
2608
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002609 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2610 if( status != PSA_SUCCESS )
2611 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002612 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2613 return( PSA_ERROR_INVALID_ARGUMENT );
2614
2615#if defined(MBEDTLS_RSA_C)
2616 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2617 {
2618 mbedtls_rsa_context *rsa = slot->data.rsa;
2619 int ret;
2620
Gilles Peskine630a18a2018-06-29 17:49:35 +02002621 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002622 return( PSA_ERROR_INVALID_ARGUMENT );
2623
2624#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002625 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002626 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002627 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2628 mbedtls_ctr_drbg_random,
2629 &global_data.ctr_drbg,
2630 MBEDTLS_RSA_PRIVATE,
2631 output_length,
2632 input,
2633 output,
2634 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002635 }
2636 else
2637#endif /* MBEDTLS_PKCS1_V15 */
2638#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002639 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002640 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002641 psa_rsa_oaep_set_padding_mode( alg, rsa );
2642 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2643 mbedtls_ctr_drbg_random,
2644 &global_data.ctr_drbg,
2645 MBEDTLS_RSA_PRIVATE,
2646 salt, salt_length,
2647 output_length,
2648 input,
2649 output,
2650 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002651 }
2652 else
2653#endif /* MBEDTLS_PKCS1_V21 */
2654 {
2655 return( PSA_ERROR_INVALID_ARGUMENT );
2656 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002657
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002658 return( mbedtls_to_psa_error( ret ) );
2659 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002660 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002661#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002662 {
2663 return( PSA_ERROR_NOT_SUPPORTED );
2664 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002665}
Gilles Peskine20035e32018-02-03 22:44:14 +01002666
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002667
2668
mohammad1603503973b2018-03-12 15:59:30 +02002669/****************************************************************/
2670/* Symmetric cryptography */
2671/****************************************************************/
2672
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002673/* Initialize the cipher operation structure. Once this function has been
2674 * called, psa_cipher_abort can run and will do the right thing. */
2675static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2676 psa_algorithm_t alg )
2677{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002678 if( ! PSA_ALG_IS_CIPHER( alg ) )
2679 {
2680 memset( operation, 0, sizeof( *operation ) );
2681 return( PSA_ERROR_INVALID_ARGUMENT );
2682 }
2683
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002684 operation->alg = alg;
2685 operation->key_set = 0;
2686 operation->iv_set = 0;
2687 operation->iv_required = 1;
2688 operation->iv_size = 0;
2689 operation->block_size = 0;
2690 mbedtls_cipher_init( &operation->ctx.cipher );
2691 return( PSA_SUCCESS );
2692}
2693
Gilles Peskinee553c652018-06-04 16:22:46 +02002694static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2695 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002696 psa_algorithm_t alg,
2697 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002698{
2699 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2700 psa_status_t status;
2701 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002702 size_t key_bits;
2703 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002704 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2705 PSA_KEY_USAGE_ENCRYPT :
2706 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002707
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002708 status = psa_cipher_init( operation, alg );
2709 if( status != PSA_SUCCESS )
2710 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002711
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002712 status = psa_get_key_from_slot( key, &slot, usage, alg);
2713 if( status != PSA_SUCCESS )
2714 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002715 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002716
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002717 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002718 if( cipher_info == NULL )
2719 return( PSA_ERROR_NOT_SUPPORTED );
2720
mohammad1603503973b2018-03-12 15:59:30 +02002721 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002722 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002723 {
2724 psa_cipher_abort( operation );
2725 return( mbedtls_to_psa_error( ret ) );
2726 }
2727
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002728#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002729 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002730 {
2731 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2732 unsigned char keys[24];
2733 memcpy( keys, slot->data.raw.data, 16 );
2734 memcpy( keys + 16, slot->data.raw.data, 8 );
2735 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2736 keys,
2737 192, cipher_operation );
2738 }
2739 else
2740#endif
2741 {
2742 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2743 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002744 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002745 }
Moran Peker41deec42018-04-04 15:43:05 +03002746 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002747 {
2748 psa_cipher_abort( operation );
2749 return( mbedtls_to_psa_error( ret ) );
2750 }
2751
mohammad16038481e742018-03-18 13:57:31 +02002752#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002753 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002754 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002755 case PSA_ALG_CBC_NO_PADDING:
2756 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2757 MBEDTLS_PADDING_NONE );
2758 break;
2759 case PSA_ALG_CBC_PKCS7:
2760 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2761 MBEDTLS_PADDING_PKCS7 );
2762 break;
2763 default:
2764 /* The algorithm doesn't involve padding. */
2765 ret = 0;
2766 break;
2767 }
2768 if( ret != 0 )
2769 {
2770 psa_cipher_abort( operation );
2771 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002772 }
2773#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2774
mohammad1603503973b2018-03-12 15:59:30 +02002775 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002776 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2777 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2778 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002779 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002780 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002781 }
mohammad1603503973b2018-03-12 15:59:30 +02002782
Moran Peker395db872018-05-31 14:07:14 +03002783 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002784}
2785
Gilles Peskinefe119512018-07-08 21:39:34 +02002786psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2787 psa_key_slot_t key,
2788 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002789{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002790 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002791}
2792
Gilles Peskinefe119512018-07-08 21:39:34 +02002793psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2794 psa_key_slot_t key,
2795 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002796{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002797 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002798}
2799
Gilles Peskinefe119512018-07-08 21:39:34 +02002800psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2801 unsigned char *iv,
2802 size_t iv_size,
2803 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002804{
itayzafrir534bd7c2018-08-02 13:56:32 +03002805 psa_status_t status;
2806 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002807 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002808 {
2809 status = PSA_ERROR_BAD_STATE;
2810 goto exit;
2811 }
Moran Peker41deec42018-04-04 15:43:05 +03002812 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002813 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002814 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002815 goto exit;
2816 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002817 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2818 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002819 if( ret != 0 )
2820 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002821 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002822 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002823 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002824
mohammad16038481e742018-03-18 13:57:31 +02002825 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002826 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002827
Moran Peker395db872018-05-31 14:07:14 +03002828exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002829 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002830 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002831 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002832}
2833
Gilles Peskinefe119512018-07-08 21:39:34 +02002834psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2835 const unsigned char *iv,
2836 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002837{
itayzafrir534bd7c2018-08-02 13:56:32 +03002838 psa_status_t status;
2839 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002840 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002841 {
2842 status = PSA_ERROR_BAD_STATE;
2843 goto exit;
2844 }
Moran Pekera28258c2018-05-29 16:25:04 +03002845 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002846 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002847 status = PSA_ERROR_INVALID_ARGUMENT;
2848 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002849 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002850 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2851 status = mbedtls_to_psa_error( ret );
2852exit:
2853 if( status == PSA_SUCCESS )
2854 operation->iv_set = 1;
2855 else
mohammad1603503973b2018-03-12 15:59:30 +02002856 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002857 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002858}
2859
Gilles Peskinee553c652018-06-04 16:22:46 +02002860psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2861 const uint8_t *input,
2862 size_t input_length,
2863 unsigned char *output,
2864 size_t output_size,
2865 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002866{
itayzafrir534bd7c2018-08-02 13:56:32 +03002867 psa_status_t status;
2868 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002869 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002870 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002871 {
2872 /* Take the unprocessed partial block left over from previous
2873 * update calls, if any, plus the input to this call. Remove
2874 * the last partial block, if any. You get the data that will be
2875 * output in this call. */
2876 expected_output_size =
2877 ( operation->ctx.cipher.unprocessed_len + input_length )
2878 / operation->block_size * operation->block_size;
2879 }
2880 else
2881 {
2882 expected_output_size = input_length;
2883 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002884
Gilles Peskine89d789c2018-06-04 17:17:16 +02002885 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002886 {
2887 status = PSA_ERROR_BUFFER_TOO_SMALL;
2888 goto exit;
2889 }
mohammad160382759612018-03-12 18:16:40 +02002890
mohammad1603503973b2018-03-12 15:59:30 +02002891 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002892 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002893 status = mbedtls_to_psa_error( ret );
2894exit:
2895 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002896 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002897 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002898}
2899
Gilles Peskinee553c652018-06-04 16:22:46 +02002900psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2901 uint8_t *output,
2902 size_t output_size,
2903 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002904{
Janos Follath315b51c2018-07-09 16:04:51 +01002905 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2906 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002907 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002908
mohammad1603503973b2018-03-12 15:59:30 +02002909 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002910 {
Janos Follath315b51c2018-07-09 16:04:51 +01002911 status = PSA_ERROR_BAD_STATE;
2912 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002913 }
2914 if( operation->iv_required && ! operation->iv_set )
2915 {
Janos Follath315b51c2018-07-09 16:04:51 +01002916 status = PSA_ERROR_BAD_STATE;
2917 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002918 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002919
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002920 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002921 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2922 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002923 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002924 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002925 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002926 }
2927
Janos Follath315b51c2018-07-09 16:04:51 +01002928 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2929 temp_output_buffer,
2930 output_length );
2931 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002932 {
Janos Follath315b51c2018-07-09 16:04:51 +01002933 status = mbedtls_to_psa_error( cipher_ret );
2934 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002935 }
Janos Follath315b51c2018-07-09 16:04:51 +01002936
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002937 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002938 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002939 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002940 memcpy( output, temp_output_buffer, *output_length );
2941 else
2942 {
Janos Follath315b51c2018-07-09 16:04:51 +01002943 status = PSA_ERROR_BUFFER_TOO_SMALL;
2944 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002945 }
mohammad1603503973b2018-03-12 15:59:30 +02002946
Janos Follath279ab8e2018-07-09 16:13:21 +01002947 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002948 status = psa_cipher_abort( operation );
2949
2950 return( status );
2951
2952error:
2953
2954 *output_length = 0;
2955
Janos Follath279ab8e2018-07-09 16:13:21 +01002956 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002957 (void) psa_cipher_abort( operation );
2958
2959 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002960}
2961
Gilles Peskinee553c652018-06-04 16:22:46 +02002962psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2963{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002964 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002965 {
2966 /* The object has (apparently) been initialized but it is not
2967 * in use. It's ok to call abort on such an object, and there's
2968 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002969 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002970 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002971
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002972 /* Sanity check (shouldn't happen: operation->alg should
2973 * always have been initialized to a valid value). */
2974 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2975 return( PSA_ERROR_BAD_STATE );
2976
mohammad1603503973b2018-03-12 15:59:30 +02002977 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002978
Moran Peker41deec42018-04-04 15:43:05 +03002979 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002980 operation->key_set = 0;
2981 operation->iv_set = 0;
2982 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002983 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002984 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002985
Moran Peker395db872018-05-31 14:07:14 +03002986 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002987}
2988
Gilles Peskinea0655c32018-04-30 17:06:50 +02002989
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002990
mohammad16038cc1cee2018-03-28 01:21:33 +03002991/****************************************************************/
2992/* Key Policy */
2993/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002994
mohammad160327010052018-07-03 13:16:15 +03002995#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002996void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002997{
Gilles Peskine803ce742018-06-18 16:07:14 +02002998 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002999}
3000
Gilles Peskine2d277862018-06-18 15:41:12 +02003001void psa_key_policy_set_usage( psa_key_policy_t *policy,
3002 psa_key_usage_t usage,
3003 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003004{
mohammad16034eed7572018-03-28 05:14:59 -07003005 policy->usage = usage;
3006 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003007}
3008
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003009psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003010{
mohammad16036df908f2018-04-02 08:34:15 -07003011 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003012}
3013
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003014psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003015{
mohammad16036df908f2018-04-02 08:34:15 -07003016 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003017}
mohammad160327010052018-07-03 13:16:15 +03003018#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003019
Gilles Peskine2d277862018-06-18 15:41:12 +02003020psa_status_t psa_set_key_policy( psa_key_slot_t key,
3021 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003022{
3023 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003024 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003025
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003026 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003027 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003028
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003029 status = psa_get_empty_key_slot( key, &slot );
3030 if( status != PSA_SUCCESS )
3031 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003032
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003033 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3034 PSA_KEY_USAGE_ENCRYPT |
3035 PSA_KEY_USAGE_DECRYPT |
3036 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003037 PSA_KEY_USAGE_VERIFY |
3038 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003039 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003040
mohammad16036df908f2018-04-02 08:34:15 -07003041 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003042
3043 return( PSA_SUCCESS );
3044}
3045
Gilles Peskine2d277862018-06-18 15:41:12 +02003046psa_status_t psa_get_key_policy( psa_key_slot_t key,
3047 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003048{
3049 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003050 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003051
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003052 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003053 return( PSA_ERROR_INVALID_ARGUMENT );
3054
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003055 status = psa_get_key_slot( key, &slot );
3056 if( status != PSA_SUCCESS )
3057 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003058
mohammad16036df908f2018-04-02 08:34:15 -07003059 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003060
3061 return( PSA_SUCCESS );
3062}
Gilles Peskine20035e32018-02-03 22:44:14 +01003063
Gilles Peskinea0655c32018-04-30 17:06:50 +02003064
3065
mohammad1603804cd712018-03-20 22:44:08 +02003066/****************************************************************/
3067/* Key Lifetime */
3068/****************************************************************/
3069
Gilles Peskine2d277862018-06-18 15:41:12 +02003070psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
3071 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003072{
3073 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003074 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003075
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003076 status = psa_get_key_slot( key, &slot );
3077 if( status != PSA_SUCCESS )
3078 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003079
mohammad1603804cd712018-03-20 22:44:08 +02003080 *lifetime = slot->lifetime;
3081
3082 return( PSA_SUCCESS );
3083}
3084
Gilles Peskine2d277862018-06-18 15:41:12 +02003085psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01003086 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003087{
3088 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003089 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003090
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003091 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
3092 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
Darryl Greend49a4992018-06-18 17:27:26 +01003093 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003094 return( PSA_ERROR_INVALID_ARGUMENT );
3095
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003096 status = psa_get_empty_key_slot( key, &slot );
3097 if( status != PSA_SUCCESS )
3098 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02003099
Darryl Greend49a4992018-06-18 17:27:26 +01003100 if( lifetime == PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003101 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003102
Darryl Greend49a4992018-06-18 17:27:26 +01003103#if !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
3104 if( lifetime == PSA_KEY_LIFETIME_PERSISTENT )
3105 return( PSA_ERROR_NOT_SUPPORTED );
3106#endif
3107
mohammad1603060ad8a2018-03-20 14:28:38 -07003108 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02003109
3110 return( PSA_SUCCESS );
3111}
3112
Gilles Peskine20035e32018-02-03 22:44:14 +01003113
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003114
mohammad16035955c982018-04-26 00:53:03 +03003115/****************************************************************/
3116/* AEAD */
3117/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003118
Gilles Peskineedf9a652018-08-17 18:11:56 +02003119typedef struct
3120{
3121 key_slot_t *slot;
3122 const mbedtls_cipher_info_t *cipher_info;
3123 union
3124 {
3125#if defined(MBEDTLS_CCM_C)
3126 mbedtls_ccm_context ccm;
3127#endif /* MBEDTLS_CCM_C */
3128#if defined(MBEDTLS_GCM_C)
3129 mbedtls_gcm_context gcm;
3130#endif /* MBEDTLS_GCM_C */
3131 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003132 psa_algorithm_t core_alg;
3133 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003134 uint8_t tag_length;
3135} aead_operation_t;
3136
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003137static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003138{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003139 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003140 {
3141#if defined(MBEDTLS_CCM_C)
3142 case PSA_ALG_CCM:
3143 mbedtls_ccm_free( &operation->ctx.ccm );
3144 break;
3145#endif /* MBEDTLS_CCM_C */
3146#if defined(MBEDTLS_CCM_C)
3147 case PSA_ALG_GCM:
3148 mbedtls_gcm_free( &operation->ctx.gcm );
3149 break;
3150#endif /* MBEDTLS_GCM_C */
3151 }
3152}
3153
3154static psa_status_t psa_aead_setup( aead_operation_t *operation,
3155 psa_key_slot_t key,
3156 psa_key_usage_t usage,
3157 psa_algorithm_t alg )
3158{
3159 psa_status_t status;
3160 size_t key_bits;
3161 mbedtls_cipher_id_t cipher_id;
3162
3163 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
3164 if( status != PSA_SUCCESS )
3165 return( status );
3166
3167 key_bits = psa_get_key_bits( operation->slot );
3168
3169 operation->cipher_info =
3170 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3171 &cipher_id );
3172 if( operation->cipher_info == NULL )
3173 return( PSA_ERROR_NOT_SUPPORTED );
3174
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003175 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003176 {
3177#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003178 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3179 operation->core_alg = PSA_ALG_CCM;
3180 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003181 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3182 return( PSA_ERROR_INVALID_ARGUMENT );
3183 mbedtls_ccm_init( &operation->ctx.ccm );
3184 status = mbedtls_to_psa_error(
3185 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3186 operation->slot->data.raw.data,
3187 (unsigned int) key_bits ) );
3188 if( status != 0 )
3189 goto cleanup;
3190 break;
3191#endif /* MBEDTLS_CCM_C */
3192
3193#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003194 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3195 operation->core_alg = PSA_ALG_GCM;
3196 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003197 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3198 return( PSA_ERROR_INVALID_ARGUMENT );
3199 mbedtls_gcm_init( &operation->ctx.gcm );
3200 status = mbedtls_to_psa_error(
3201 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3202 operation->slot->data.raw.data,
3203 (unsigned int) key_bits ) );
3204 break;
3205#endif /* MBEDTLS_GCM_C */
3206
3207 default:
3208 return( PSA_ERROR_NOT_SUPPORTED );
3209 }
3210
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003211 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3212 {
3213 status = PSA_ERROR_INVALID_ARGUMENT;
3214 goto cleanup;
3215 }
3216 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3217 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3218 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3219 * In both cases, mbedtls_xxx will validate the tag length below. */
3220
Gilles Peskineedf9a652018-08-17 18:11:56 +02003221 return( PSA_SUCCESS );
3222
3223cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003224 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003225 return( status );
3226}
3227
mohammad16035955c982018-04-26 00:53:03 +03003228psa_status_t psa_aead_encrypt( psa_key_slot_t key,
3229 psa_algorithm_t alg,
3230 const uint8_t *nonce,
3231 size_t nonce_length,
3232 const uint8_t *additional_data,
3233 size_t additional_data_length,
3234 const uint8_t *plaintext,
3235 size_t plaintext_length,
3236 uint8_t *ciphertext,
3237 size_t ciphertext_size,
3238 size_t *ciphertext_length )
3239{
mohammad16035955c982018-04-26 00:53:03 +03003240 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003241 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003242 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003243
mohammad1603f08a5502018-06-03 15:05:47 +03003244 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003245
Gilles Peskineedf9a652018-08-17 18:11:56 +02003246 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003247 if( status != PSA_SUCCESS )
3248 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003249
Gilles Peskineedf9a652018-08-17 18:11:56 +02003250 /* For all currently supported modes, the tag is at the end of the
3251 * ciphertext. */
3252 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3253 {
3254 status = PSA_ERROR_BUFFER_TOO_SMALL;
3255 goto exit;
3256 }
3257 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003258
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003259 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003260 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003261 status = mbedtls_to_psa_error(
3262 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3263 MBEDTLS_GCM_ENCRYPT,
3264 plaintext_length,
3265 nonce, nonce_length,
3266 additional_data, additional_data_length,
3267 plaintext, ciphertext,
3268 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003269 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003270 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003271 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003272 status = mbedtls_to_psa_error(
3273 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3274 plaintext_length,
3275 nonce, nonce_length,
3276 additional_data,
3277 additional_data_length,
3278 plaintext, ciphertext,
3279 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003280 }
mohammad16035c8845f2018-05-09 05:40:09 -07003281 else
3282 {
mohammad1603554faad2018-06-03 15:07:38 +03003283 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003284 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003285
Gilles Peskineedf9a652018-08-17 18:11:56 +02003286 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3287 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003288
Gilles Peskineedf9a652018-08-17 18:11:56 +02003289exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003290 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003291 if( status == PSA_SUCCESS )
3292 *ciphertext_length = plaintext_length + operation.tag_length;
3293 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003294}
3295
Gilles Peskineee652a32018-06-01 19:23:52 +02003296/* Locate the tag in a ciphertext buffer containing the encrypted data
3297 * followed by the tag. Return the length of the part preceding the tag in
3298 * *plaintext_length. This is the size of the plaintext in modes where
3299 * the encrypted data has the same size as the plaintext, such as
3300 * CCM and GCM. */
3301static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3302 const uint8_t *ciphertext,
3303 size_t ciphertext_length,
3304 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003305 const uint8_t **p_tag )
3306{
3307 size_t payload_length;
3308 if( tag_length > ciphertext_length )
3309 return( PSA_ERROR_INVALID_ARGUMENT );
3310 payload_length = ciphertext_length - tag_length;
3311 if( payload_length > plaintext_size )
3312 return( PSA_ERROR_BUFFER_TOO_SMALL );
3313 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003314 return( PSA_SUCCESS );
3315}
3316
mohammad16035955c982018-04-26 00:53:03 +03003317psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3318 psa_algorithm_t alg,
3319 const uint8_t *nonce,
3320 size_t nonce_length,
3321 const uint8_t *additional_data,
3322 size_t additional_data_length,
3323 const uint8_t *ciphertext,
3324 size_t ciphertext_length,
3325 uint8_t *plaintext,
3326 size_t plaintext_size,
3327 size_t *plaintext_length )
3328{
mohammad16035955c982018-04-26 00:53:03 +03003329 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003330 aead_operation_t operation;
3331 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003332
Gilles Peskineee652a32018-06-01 19:23:52 +02003333 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003334
Gilles Peskineedf9a652018-08-17 18:11:56 +02003335 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003336 if( status != PSA_SUCCESS )
3337 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003338
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003339 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003340 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003341 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003342 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003343 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003344 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003345 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003346
Gilles Peskineedf9a652018-08-17 18:11:56 +02003347 status = mbedtls_to_psa_error(
3348 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3349 ciphertext_length - operation.tag_length,
3350 nonce, nonce_length,
3351 additional_data,
3352 additional_data_length,
3353 tag, operation.tag_length,
3354 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003355 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003356 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003357 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003358 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003359 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003360 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003361 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003362 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003363
Gilles Peskineedf9a652018-08-17 18:11:56 +02003364 status = mbedtls_to_psa_error(
3365 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3366 ciphertext_length - operation.tag_length,
3367 nonce, nonce_length,
3368 additional_data,
3369 additional_data_length,
3370 ciphertext, plaintext,
3371 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003372 }
mohammad160339574652018-06-01 04:39:53 -07003373 else
3374 {
mohammad1603554faad2018-06-03 15:07:38 +03003375 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003376 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003377
Gilles Peskineedf9a652018-08-17 18:11:56 +02003378 if( status != PSA_SUCCESS && plaintext_size != 0 )
3379 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003380
Gilles Peskineedf9a652018-08-17 18:11:56 +02003381exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003382 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003383 if( status == PSA_SUCCESS )
3384 *plaintext_length = ciphertext_length - operation.tag_length;
3385 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003386}
3387
Gilles Peskinea0655c32018-04-30 17:06:50 +02003388
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003389
Gilles Peskine20035e32018-02-03 22:44:14 +01003390/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003391/* Generators */
3392/****************************************************************/
3393
3394psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3395{
3396 psa_status_t status = PSA_SUCCESS;
3397 if( generator->alg == 0 )
3398 {
3399 /* The object has (apparently) been initialized but it is not
3400 * in use. It's ok to call abort on such an object, and there's
3401 * nothing to do. */
3402 }
3403 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003404 if( generator->alg == PSA_ALG_SELECT_RAW )
3405 {
3406 if( generator->ctx.buffer.data != NULL )
3407 {
3408 mbedtls_zeroize( generator->ctx.buffer.data,
3409 generator->ctx.buffer.size );
3410 mbedtls_free( generator->ctx.buffer.data );
3411 }
3412 }
3413 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003414#if defined(MBEDTLS_MD_C)
3415 if( PSA_ALG_IS_HKDF( generator->alg ) )
3416 {
3417 mbedtls_free( generator->ctx.hkdf.info );
3418 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3419 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003420 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3421 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3422 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003423 {
3424 if( generator->ctx.tls12_prf.key != NULL )
3425 {
3426 mbedtls_zeroize( generator->ctx.tls12_prf.key,
3427 generator->ctx.tls12_prf.key_len );
3428 mbedtls_free( generator->ctx.tls12_prf.key );
3429 }
Hanno Becker580fba12018-11-13 20:50:45 +00003430
3431 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3432 {
3433 mbedtls_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
3434 generator->ctx.tls12_prf.Ai_with_seed_len );
3435 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3436 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003437 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003438 else
3439#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003440 {
3441 status = PSA_ERROR_BAD_STATE;
3442 }
3443 memset( generator, 0, sizeof( *generator ) );
3444 return( status );
3445}
3446
3447
3448psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3449 size_t *capacity)
3450{
3451 *capacity = generator->capacity;
3452 return( PSA_SUCCESS );
3453}
3454
Gilles Peskinebef7f142018-07-12 17:22:21 +02003455#if defined(MBEDTLS_MD_C)
3456/* Read some bytes from an HKDF-based generator. This performs a chunk
3457 * of the expand phase of the HKDF algorithm. */
3458static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3459 psa_algorithm_t hash_alg,
3460 uint8_t *output,
3461 size_t output_length )
3462{
3463 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3464 psa_status_t status;
3465
3466 while( output_length != 0 )
3467 {
3468 /* Copy what remains of the current block */
3469 uint8_t n = hash_length - hkdf->offset_in_block;
3470 if( n > output_length )
3471 n = (uint8_t) output_length;
3472 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3473 output += n;
3474 output_length -= n;
3475 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003476 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003477 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003478 /* We can't be wanting more output after block 0xff, otherwise
3479 * the capacity check in psa_generator_read() would have
3480 * prevented this call. It could happen only if the generator
3481 * object was corrupted or if this function is called directly
3482 * inside the library. */
3483 if( hkdf->block_number == 0xff )
3484 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003485
3486 /* We need a new block */
3487 ++hkdf->block_number;
3488 hkdf->offset_in_block = 0;
3489 status = psa_hmac_setup_internal( &hkdf->hmac,
3490 hkdf->prk, hash_length,
3491 hash_alg );
3492 if( status != PSA_SUCCESS )
3493 return( status );
3494 if( hkdf->block_number != 1 )
3495 {
3496 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3497 hkdf->output_block,
3498 hash_length );
3499 if( status != PSA_SUCCESS )
3500 return( status );
3501 }
3502 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3503 hkdf->info,
3504 hkdf->info_length );
3505 if( status != PSA_SUCCESS )
3506 return( status );
3507 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3508 &hkdf->block_number, 1 );
3509 if( status != PSA_SUCCESS )
3510 return( status );
3511 status = psa_hmac_finish_internal( &hkdf->hmac,
3512 hkdf->output_block,
3513 sizeof( hkdf->output_block ) );
3514 if( status != PSA_SUCCESS )
3515 return( status );
3516 }
3517
3518 return( PSA_SUCCESS );
3519}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003520
3521static psa_status_t psa_generator_tls12_prf_generate_next_block(
3522 psa_tls12_prf_generator_t *tls12_prf,
3523 psa_algorithm_t alg )
3524{
3525 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3526 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3527 psa_hmac_internal_data hmac;
3528 psa_status_t status, cleanup_status;
3529
Hanno Becker3b339e22018-11-13 20:56:14 +00003530 unsigned char *Ai;
3531 size_t Ai_len;
3532
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003533 /* We can't be wanting more output after block 0xff, otherwise
3534 * the capacity check in psa_generator_read() would have
3535 * prevented this call. It could happen only if the generator
3536 * object was corrupted or if this function is called directly
3537 * inside the library. */
3538 if( tls12_prf->block_number == 0xff )
3539 return( PSA_ERROR_BAD_STATE );
3540
3541 /* We need a new block */
3542 ++tls12_prf->block_number;
3543 tls12_prf->offset_in_block = 0;
3544
3545 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3546 *
3547 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3548 *
3549 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3550 * HMAC_hash(secret, A(2) + seed) +
3551 * HMAC_hash(secret, A(3) + seed) + ...
3552 *
3553 * A(0) = seed
3554 * A(i) = HMAC_hash( secret, A(i-1) )
3555 *
3556 * The `psa_tls12_prf_generator` structures saves the block
3557 * `HMAC_hash(secret, A(i) + seed)` from which the output
3558 * is currently extracted as `output_block`, while
3559 * `A(i) + seed` is stored in `Ai_with_seed`.
3560 *
3561 * Generating a new block means recalculating `Ai_with_seed`
3562 * from the A(i)-part of it, and afterwards recalculating
3563 * `output_block`.
3564 *
3565 * A(0) is computed at setup time.
3566 *
3567 */
3568
3569 psa_hmac_init_internal( &hmac );
3570
3571 /* We must distinguish the calculation of A(1) from those
3572 * of A(2) and higher, because A(0)=seed has a different
3573 * length than the other A(i). */
3574 if( tls12_prf->block_number == 1 )
3575 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003576 Ai = tls12_prf->Ai_with_seed + hash_length;
3577 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003578 }
3579 else
3580 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003581 Ai = tls12_prf->Ai_with_seed;
3582 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003583 }
3584
Hanno Becker3b339e22018-11-13 20:56:14 +00003585 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3586 status = psa_hmac_setup_internal( &hmac,
3587 tls12_prf->key,
3588 tls12_prf->key_len,
3589 hash_alg );
3590 if( status != PSA_SUCCESS )
3591 goto cleanup;
3592
3593 status = psa_hash_update( &hmac.hash_ctx,
3594 Ai, Ai_len );
3595 if( status != PSA_SUCCESS )
3596 goto cleanup;
3597
3598 status = psa_hmac_finish_internal( &hmac,
3599 tls12_prf->Ai_with_seed,
3600 hash_length );
3601 if( status != PSA_SUCCESS )
3602 goto cleanup;
3603
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003604 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3605 status = psa_hmac_setup_internal( &hmac,
3606 tls12_prf->key,
3607 tls12_prf->key_len,
3608 hash_alg );
3609 if( status != PSA_SUCCESS )
3610 goto cleanup;
3611
3612 status = psa_hash_update( &hmac.hash_ctx,
3613 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003614 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003615 if( status != PSA_SUCCESS )
3616 goto cleanup;
3617
3618 status = psa_hmac_finish_internal( &hmac,
3619 tls12_prf->output_block,
3620 hash_length );
3621 if( status != PSA_SUCCESS )
3622 goto cleanup;
3623
3624cleanup:
3625
3626 cleanup_status = psa_hmac_abort_internal( &hmac );
3627 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3628 status = cleanup_status;
3629
3630 return( status );
3631}
3632
3633/* Read some bytes from an TLS-1.2-PRF-based generator.
3634 * See Section 5 of RFC 5246. */
3635static psa_status_t psa_generator_tls12_prf_read(
3636 psa_tls12_prf_generator_t *tls12_prf,
3637 psa_algorithm_t alg,
3638 uint8_t *output,
3639 size_t output_length )
3640{
3641 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3642 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3643 psa_status_t status;
3644
3645 while( output_length != 0 )
3646 {
3647 /* Copy what remains of the current block */
3648 uint8_t n = hash_length - tls12_prf->offset_in_block;
3649
3650 /* Check if we have fully processed the current block. */
3651 if( n == 0 )
3652 {
3653 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3654 alg );
3655 if( status != PSA_SUCCESS )
3656 return( status );
3657
3658 continue;
3659 }
3660
3661 if( n > output_length )
3662 n = (uint8_t) output_length;
3663 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3664 n );
3665 output += n;
3666 output_length -= n;
3667 tls12_prf->offset_in_block += n;
3668 }
3669
3670 return( PSA_SUCCESS );
3671}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003672#endif /* MBEDTLS_MD_C */
3673
Gilles Peskineeab56e42018-07-12 17:12:33 +02003674psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3675 uint8_t *output,
3676 size_t output_length )
3677{
3678 psa_status_t status;
3679
3680 if( output_length > generator->capacity )
3681 {
3682 generator->capacity = 0;
3683 /* Go through the error path to wipe all confidential data now
3684 * that the generator object is useless. */
3685 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3686 goto exit;
3687 }
3688 if( output_length == 0 &&
3689 generator->capacity == 0 && generator->alg == 0 )
3690 {
3691 /* Edge case: this is a blank or finished generator, and 0
3692 * bytes were requested. The right error in this case could
3693 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3694 * INSUFFICIENT_CAPACITY, which is right for a finished
3695 * generator, for consistency with the case when
3696 * output_length > 0. */
3697 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3698 }
3699 generator->capacity -= output_length;
3700
Gilles Peskine751d9652018-09-18 12:05:44 +02003701 if( generator->alg == PSA_ALG_SELECT_RAW )
3702 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003703 /* Initially, the capacity of a selection generator is always
3704 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3705 * abbreviated in this comment as `size`. When the remaining
3706 * capacity is `c`, the next bytes to serve start `c` bytes
3707 * from the end of the buffer, i.e. `size - c` from the
3708 * beginning of the buffer. Since `generator->capacity` was just
3709 * decremented above, we need to serve the bytes from
3710 * `size - generator->capacity - output_length` to
3711 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003712 size_t offset =
3713 generator->ctx.buffer.size - generator->capacity - output_length;
3714 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3715 status = PSA_SUCCESS;
3716 }
3717 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003718#if defined(MBEDTLS_MD_C)
3719 if( PSA_ALG_IS_HKDF( generator->alg ) )
3720 {
3721 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3722 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3723 output, output_length );
3724 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003725 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3726 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003727 {
3728 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3729 generator->alg, output,
3730 output_length );
3731 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003732 else
3733#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003734 {
3735 return( PSA_ERROR_BAD_STATE );
3736 }
3737
3738exit:
3739 if( status != PSA_SUCCESS )
3740 {
3741 psa_generator_abort( generator );
3742 memset( output, '!', output_length );
3743 }
3744 return( status );
3745}
3746
Gilles Peskine08542d82018-07-19 17:05:42 +02003747#if defined(MBEDTLS_DES_C)
3748static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3749{
3750 if( data_size >= 8 )
3751 mbedtls_des_key_set_parity( data );
3752 if( data_size >= 16 )
3753 mbedtls_des_key_set_parity( data + 8 );
3754 if( data_size >= 24 )
3755 mbedtls_des_key_set_parity( data + 16 );
3756}
3757#endif /* MBEDTLS_DES_C */
3758
Gilles Peskineeab56e42018-07-12 17:12:33 +02003759psa_status_t psa_generator_import_key( psa_key_slot_t key,
3760 psa_key_type_t type,
3761 size_t bits,
3762 psa_crypto_generator_t *generator )
3763{
3764 uint8_t *data = NULL;
3765 size_t bytes = PSA_BITS_TO_BYTES( bits );
3766 psa_status_t status;
3767
3768 if( ! key_type_is_raw_bytes( type ) )
3769 return( PSA_ERROR_INVALID_ARGUMENT );
3770 if( bits % 8 != 0 )
3771 return( PSA_ERROR_INVALID_ARGUMENT );
3772 data = mbedtls_calloc( 1, bytes );
3773 if( data == NULL )
3774 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3775
3776 status = psa_generator_read( generator, data, bytes );
3777 if( status != PSA_SUCCESS )
3778 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003779#if defined(MBEDTLS_DES_C)
3780 if( type == PSA_KEY_TYPE_DES )
3781 psa_des_set_key_parity( data, bytes );
3782#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003783 status = psa_import_key( key, type, data, bytes );
3784
3785exit:
3786 mbedtls_free( data );
3787 return( status );
3788}
3789
3790
3791
3792/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003793/* Key derivation */
3794/****************************************************************/
3795
Gilles Peskinea05219c2018-11-16 16:02:56 +01003796#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003797/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003798 * of the HKDF algorithm.
3799 *
3800 * Note that if this function fails, you must call psa_generator_abort()
3801 * to potentially free embedded data structures and wipe confidential data.
3802 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003803static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003804 const uint8_t *secret,
3805 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003806 psa_algorithm_t hash_alg,
3807 const uint8_t *salt,
3808 size_t salt_length,
3809 const uint8_t *label,
3810 size_t label_length )
3811{
3812 psa_status_t status;
3813 status = psa_hmac_setup_internal( &hkdf->hmac,
3814 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003815 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003816 if( status != PSA_SUCCESS )
3817 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003818 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003819 if( status != PSA_SUCCESS )
3820 return( status );
3821 status = psa_hmac_finish_internal( &hkdf->hmac,
3822 hkdf->prk,
3823 sizeof( hkdf->prk ) );
3824 if( status != PSA_SUCCESS )
3825 return( status );
3826 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3827 hkdf->block_number = 0;
3828 hkdf->info_length = label_length;
3829 if( label_length != 0 )
3830 {
3831 hkdf->info = mbedtls_calloc( 1, label_length );
3832 if( hkdf->info == NULL )
3833 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3834 memcpy( hkdf->info, label, label_length );
3835 }
3836 return( PSA_SUCCESS );
3837}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003838#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003839
Gilles Peskinea05219c2018-11-16 16:02:56 +01003840#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003841/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3842 *
3843 * Note that if this function fails, you must call psa_generator_abort()
3844 * to potentially free embedded data structures and wipe confidential data.
3845 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003846static psa_status_t psa_generator_tls12_prf_setup(
3847 psa_tls12_prf_generator_t *tls12_prf,
3848 const unsigned char *key,
3849 size_t key_len,
3850 psa_algorithm_t hash_alg,
3851 const uint8_t *salt,
3852 size_t salt_length,
3853 const uint8_t *label,
3854 size_t label_length )
3855{
3856 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003857 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3858 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003859
3860 tls12_prf->key = mbedtls_calloc( 1, key_len );
3861 if( tls12_prf->key == NULL )
3862 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3863 tls12_prf->key_len = key_len;
3864 memcpy( tls12_prf->key, key, key_len );
3865
Hanno Becker580fba12018-11-13 20:50:45 +00003866 overflow = ( salt_length + label_length < salt_length ) ||
3867 ( salt_length + label_length + hash_length < hash_length );
3868 if( overflow )
3869 return( PSA_ERROR_INVALID_ARGUMENT );
3870
3871 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3872 if( tls12_prf->Ai_with_seed == NULL )
3873 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3874 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3875
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003876 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3877 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003878 if( label_length != 0 )
3879 {
3880 memcpy( tls12_prf->Ai_with_seed + hash_length,
3881 label, label_length );
3882 }
3883
3884 if( salt_length != 0 )
3885 {
3886 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3887 salt, salt_length );
3888 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003889
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003890 /* The first block gets generated when
3891 * psa_generator_read() is called. */
3892 tls12_prf->block_number = 0;
3893 tls12_prf->offset_in_block = hash_length;
3894
3895 return( PSA_SUCCESS );
3896}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003897
3898/* Set up a TLS-1.2-PSK-to-MS-based generator. */
3899static psa_status_t psa_generator_tls12_psk_to_ms_setup(
3900 psa_tls12_prf_generator_t *tls12_prf,
3901 const unsigned char *psk,
3902 size_t psk_len,
3903 psa_algorithm_t hash_alg,
3904 const uint8_t *salt,
3905 size_t salt_length,
3906 const uint8_t *label,
3907 size_t label_length )
3908{
3909 psa_status_t status;
3910 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
3911
3912 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
3913 return( PSA_ERROR_INVALID_ARGUMENT );
3914
3915 /* Quoting RFC 4279, Section 2:
3916 *
3917 * The premaster secret is formed as follows: if the PSK is N octets
3918 * long, concatenate a uint16 with the value N, N zero octets, a second
3919 * uint16 with the value N, and the PSK itself.
3920 */
3921
3922 pms[0] = ( psk_len >> 8 ) & 0xff;
3923 pms[1] = ( psk_len >> 0 ) & 0xff;
3924 memset( pms + 2, 0, psk_len );
3925 pms[2 + psk_len + 0] = pms[0];
3926 pms[2 + psk_len + 1] = pms[1];
3927 memcpy( pms + 4 + psk_len, psk, psk_len );
3928
3929 status = psa_generator_tls12_prf_setup( tls12_prf,
3930 pms, 4 + 2 * psk_len,
3931 hash_alg,
3932 salt, salt_length,
3933 label, label_length );
3934
3935 mbedtls_zeroize( pms, sizeof( pms ) );
3936 return( status );
3937}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003938#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003939
Gilles Peskine346797d2018-11-16 16:05:06 +01003940/* Note that if this function fails, you must call psa_generator_abort()
3941 * to potentially free embedded data structures and wipe confidential data.
3942 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003943static psa_status_t psa_key_derivation_internal(
3944 psa_crypto_generator_t *generator,
3945 const uint8_t *secret, size_t secret_length,
3946 psa_algorithm_t alg,
3947 const uint8_t *salt, size_t salt_length,
3948 const uint8_t *label, size_t label_length,
3949 size_t capacity )
3950{
3951 psa_status_t status;
3952 size_t max_capacity;
3953
3954 /* Set generator->alg even on failure so that abort knows what to do. */
3955 generator->alg = alg;
3956
Gilles Peskine751d9652018-09-18 12:05:44 +02003957 if( alg == PSA_ALG_SELECT_RAW )
3958 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003959 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02003960 if( salt_length != 0 )
3961 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01003962 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02003963 if( label_length != 0 )
3964 return( PSA_ERROR_INVALID_ARGUMENT );
3965 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
3966 if( generator->ctx.buffer.data == NULL )
3967 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3968 memcpy( generator->ctx.buffer.data, secret, secret_length );
3969 generator->ctx.buffer.size = secret_length;
3970 max_capacity = secret_length;
3971 status = PSA_SUCCESS;
3972 }
3973 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003974#if defined(MBEDTLS_MD_C)
3975 if( PSA_ALG_IS_HKDF( alg ) )
3976 {
3977 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3978 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3979 if( hash_size == 0 )
3980 return( PSA_ERROR_NOT_SUPPORTED );
3981 max_capacity = 255 * hash_size;
3982 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3983 secret, secret_length,
3984 hash_alg,
3985 salt, salt_length,
3986 label, label_length );
3987 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003988 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
3989 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
3990 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003991 {
3992 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3993 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3994
3995 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
3996 if( hash_alg != PSA_ALG_SHA_256 &&
3997 hash_alg != PSA_ALG_SHA_384 )
3998 {
3999 return( PSA_ERROR_NOT_SUPPORTED );
4000 }
4001
4002 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004003
4004 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4005 {
4006 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4007 secret, secret_length,
4008 hash_alg, salt, salt_length,
4009 label, label_length );
4010 }
4011 else
4012 {
4013 status = psa_generator_tls12_psk_to_ms_setup(
4014 &generator->ctx.tls12_prf,
4015 secret, secret_length,
4016 hash_alg, salt, salt_length,
4017 label, label_length );
4018 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004019 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004020 else
4021#endif
4022 {
4023 return( PSA_ERROR_NOT_SUPPORTED );
4024 }
4025
4026 if( status != PSA_SUCCESS )
4027 return( status );
4028
4029 if( capacity <= max_capacity )
4030 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004031 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4032 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004033 else
4034 return( PSA_ERROR_INVALID_ARGUMENT );
4035
4036 return( PSA_SUCCESS );
4037}
4038
Gilles Peskineea0fb492018-07-12 17:17:20 +02004039psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01004040 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004041 psa_algorithm_t alg,
4042 const uint8_t *salt,
4043 size_t salt_length,
4044 const uint8_t *label,
4045 size_t label_length,
4046 size_t capacity )
4047{
4048 key_slot_t *slot;
4049 psa_status_t status;
4050
4051 if( generator->alg != 0 )
4052 return( PSA_ERROR_BAD_STATE );
4053
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004054 /* Make sure that alg is a key derivation algorithm. This prevents
4055 * key selection algorithms, which psa_key_derivation_internal
4056 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004057 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4058 return( PSA_ERROR_INVALID_ARGUMENT );
4059
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004060 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
4061 if( status != PSA_SUCCESS )
4062 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004063
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004064 if( slot->type != PSA_KEY_TYPE_DERIVE )
4065 return( PSA_ERROR_INVALID_ARGUMENT );
4066
4067 status = psa_key_derivation_internal( generator,
4068 slot->data.raw.data,
4069 slot->data.raw.bytes,
4070 alg,
4071 salt, salt_length,
4072 label, label_length,
4073 capacity );
4074 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004075 psa_generator_abort( generator );
4076 return( status );
4077}
4078
4079
4080
4081/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004082/* Key agreement */
4083/****************************************************************/
4084
Gilles Peskinea05219c2018-11-16 16:02:56 +01004085#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004086static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4087 size_t peer_key_length,
4088 const mbedtls_ecp_keypair *our_key,
4089 uint8_t *shared_secret,
4090 size_t shared_secret_size,
4091 size_t *shared_secret_length )
4092{
4093 mbedtls_pk_context pk;
4094 mbedtls_ecp_keypair *their_key = NULL;
4095 mbedtls_ecdh_context ecdh;
4096 int ret;
4097 mbedtls_ecdh_init( &ecdh );
4098 mbedtls_pk_init( &pk );
4099
4100 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
4101 if( ret != 0 )
4102 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02004103 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004104 {
Gilles Peskine88714d72018-10-25 23:07:25 +02004105 case MBEDTLS_PK_ECKEY:
4106 case MBEDTLS_PK_ECKEY_DH:
4107 break;
4108 default:
4109 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4110 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004111 }
4112 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01004113 if( their_key->grp.id != our_key->grp.id )
4114 {
4115 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4116 goto exit;
4117 }
4118
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004119 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
4120 if( ret != 0 )
4121 goto exit;
4122 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
4123 if( ret != 0 )
4124 goto exit;
4125
4126 ret = mbedtls_ecdh_calc_secret( &ecdh,
4127 shared_secret_length,
4128 shared_secret, shared_secret_size,
4129 mbedtls_ctr_drbg_random,
4130 &global_data.ctr_drbg );
4131
4132exit:
4133 mbedtls_pk_free( &pk );
4134 mbedtls_ecdh_free( &ecdh );
4135 return( mbedtls_to_psa_error( ret ) );
4136}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004137#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004138
Gilles Peskine01d718c2018-09-18 12:01:02 +02004139#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4140
Gilles Peskine346797d2018-11-16 16:05:06 +01004141/* Note that if this function fails, you must call psa_generator_abort()
4142 * to potentially free embedded data structures and wipe confidential data.
4143 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004144static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
4145 key_slot_t *private_key,
4146 const uint8_t *peer_key,
4147 size_t peer_key_length,
4148 psa_algorithm_t alg )
4149{
4150 psa_status_t status;
4151 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4152 size_t shared_secret_length = 0;
4153
4154 /* Step 1: run the secret agreement algorithm to generate the shared
4155 * secret. */
4156 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4157 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004158#if defined(MBEDTLS_ECDH_C)
4159 case PSA_ALG_ECDH_BASE:
4160 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4161 return( PSA_ERROR_INVALID_ARGUMENT );
4162 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4163 private_key->data.ecp,
4164 shared_secret,
4165 sizeof( shared_secret ),
4166 &shared_secret_length );
4167 break;
4168#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004169 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004170 (void) private_key;
4171 (void) peer_key;
4172 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004173 return( PSA_ERROR_NOT_SUPPORTED );
4174 }
4175 if( status != PSA_SUCCESS )
4176 goto exit;
4177
4178 /* Step 2: set up the key derivation to generate key material from
4179 * the shared secret. */
4180 status = psa_key_derivation_internal( generator,
4181 shared_secret, shared_secret_length,
4182 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4183 NULL, 0, NULL, 0,
4184 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4185exit:
4186 mbedtls_zeroize( shared_secret, shared_secret_length );
4187 return( status );
4188}
4189
4190psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
4191 psa_key_slot_t private_key,
4192 const uint8_t *peer_key,
4193 size_t peer_key_length,
4194 psa_algorithm_t alg )
4195{
4196 key_slot_t *slot;
4197 psa_status_t status;
4198 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4199 return( PSA_ERROR_INVALID_ARGUMENT );
4200 status = psa_get_key_from_slot( private_key, &slot,
4201 PSA_KEY_USAGE_DERIVE, alg );
4202 if( status != PSA_SUCCESS )
4203 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004204 status = psa_key_agreement_internal( generator,
4205 slot,
4206 peer_key, peer_key_length,
4207 alg );
4208 if( status != PSA_SUCCESS )
4209 psa_generator_abort( generator );
4210 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004211}
4212
4213
4214
4215/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004216/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004217/****************************************************************/
4218
4219psa_status_t psa_generate_random( uint8_t *output,
4220 size_t output_size )
4221{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004222 int ret;
4223 GUARD_MODULE_INITIALIZED;
4224
4225 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004226 return( mbedtls_to_psa_error( ret ) );
4227}
4228
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004229#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
4230psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4231 size_t seed_size )
4232{
4233 psa_status_t status;
4234 struct psa_its_info_t p_info;
4235 if( global_data.initialized )
4236 return( PSA_ERROR_NOT_PERMITTED );
4237 if( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) || ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4238 return( PSA_ERROR_INVALID_ARGUMENT );
4239 status = psa_its_get_info( MBED_RANDOM_SEED_ITS_UID, &p_info );
4240 if( PSA_ITS_ERROR_KEY_NOT_FOUND == status ) /* No seed exists */
4241 {
4242 status = psa_its_set( MBED_RANDOM_SEED_ITS_UID, seed_size, seed, 0 );
4243 }
4244 else if( PSA_ITS_SUCCESS == status )
4245 {
4246 /* You should not be here. Seed needs to be injected only once */
4247 status = PSA_ERROR_NOT_PERMITTED;
4248 }
4249 return( status );
4250}
4251#endif
4252
Gilles Peskine05d69892018-06-19 22:00:52 +02004253psa_status_t psa_generate_key( psa_key_slot_t key,
4254 psa_key_type_t type,
4255 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004256 const void *extra,
4257 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004258{
Gilles Peskine12313cd2018-06-20 00:20:32 +02004259 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004260 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004261
Gilles Peskine53d991e2018-07-12 01:14:59 +02004262 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004263 return( PSA_ERROR_INVALID_ARGUMENT );
4264
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004265 status = psa_get_empty_key_slot( key, &slot );
4266 if( status != PSA_SUCCESS )
4267 return( status );
4268
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004269 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004270 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004271 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004272 if( status != PSA_SUCCESS )
4273 return( status );
4274 status = psa_generate_random( slot->data.raw.data,
4275 slot->data.raw.bytes );
4276 if( status != PSA_SUCCESS )
4277 {
4278 mbedtls_free( slot->data.raw.data );
4279 return( status );
4280 }
4281#if defined(MBEDTLS_DES_C)
4282 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004283 psa_des_set_key_parity( slot->data.raw.data,
4284 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004285#endif /* MBEDTLS_DES_C */
4286 }
4287 else
4288
4289#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4290 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4291 {
4292 mbedtls_rsa_context *rsa;
4293 int ret;
4294 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004295 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4296 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004297 /* Accept only byte-aligned keys, for the same reasons as
4298 * in psa_import_rsa_key(). */
4299 if( bits % 8 != 0 )
4300 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004301 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004302 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004303 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004304 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004305 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004306#if INT_MAX < 0xffffffff
4307 /* Check that the uint32_t value passed by the caller fits
4308 * in the range supported by this implementation. */
4309 if( p->e > INT_MAX )
4310 return( PSA_ERROR_NOT_SUPPORTED );
4311#endif
4312 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004313 }
4314 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4315 if( rsa == NULL )
4316 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4317 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4318 ret = mbedtls_rsa_gen_key( rsa,
4319 mbedtls_ctr_drbg_random,
4320 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004321 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004322 exponent );
4323 if( ret != 0 )
4324 {
4325 mbedtls_rsa_free( rsa );
4326 mbedtls_free( rsa );
4327 return( mbedtls_to_psa_error( ret ) );
4328 }
4329 slot->data.rsa = rsa;
4330 }
4331 else
4332#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4333
4334#if defined(MBEDTLS_ECP_C)
4335 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4336 {
4337 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4338 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4339 const mbedtls_ecp_curve_info *curve_info =
4340 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4341 mbedtls_ecp_keypair *ecp;
4342 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004343 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004344 return( PSA_ERROR_NOT_SUPPORTED );
4345 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4346 return( PSA_ERROR_NOT_SUPPORTED );
4347 if( curve_info->bit_size != bits )
4348 return( PSA_ERROR_INVALID_ARGUMENT );
4349 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4350 if( ecp == NULL )
4351 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4352 mbedtls_ecp_keypair_init( ecp );
4353 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4354 mbedtls_ctr_drbg_random,
4355 &global_data.ctr_drbg );
4356 if( ret != 0 )
4357 {
4358 mbedtls_ecp_keypair_free( ecp );
4359 mbedtls_free( ecp );
4360 return( mbedtls_to_psa_error( ret ) );
4361 }
4362 slot->data.ecp = ecp;
4363 }
4364 else
4365#endif /* MBEDTLS_ECP_C */
4366
4367 return( PSA_ERROR_NOT_SUPPORTED );
4368
4369 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004370
4371#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4372 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4373 {
4374 return( psa_save_generated_persistent_key( key, slot, bits ) );
4375 }
4376#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4377
4378 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004379}
4380
4381
4382/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004383/* Module setup */
4384/****************************************************************/
4385
Gilles Peskinee59236f2018-01-27 23:32:46 +01004386void mbedtls_psa_crypto_free( void )
4387{
Jaeden Amero045bd502018-06-26 14:00:08 +01004388 psa_key_slot_t key;
Darryl Green40225ba2018-11-15 14:48:15 +00004389 key_slot_t *slot;
4390 psa_status_t status;
4391
Gilles Peskine9a056342018-08-01 15:46:54 +02004392 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Darryl Green40225ba2018-11-15 14:48:15 +00004393 {
4394 status = psa_get_key_slot( key, &slot );
4395 if( status != PSA_SUCCESS )
4396 continue;
4397 psa_remove_key_data_from_memory( slot );
4398 /* Zeroize the slot to wipe metadata such as policies. */
4399 mbedtls_zeroize( slot, sizeof( *slot ) );
4400 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01004401 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
4402 mbedtls_entropy_free( &global_data.entropy );
4403 mbedtls_zeroize( &global_data, sizeof( global_data ) );
4404}
4405
4406psa_status_t psa_crypto_init( void )
4407{
4408 int ret;
4409 const unsigned char drbg_seed[] = "PSA";
4410
4411 if( global_data.initialized != 0 )
4412 return( PSA_SUCCESS );
4413
4414 mbedtls_zeroize( &global_data, sizeof( global_data ) );
4415 mbedtls_entropy_init( &global_data.entropy );
4416 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
4417
4418 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4419 mbedtls_entropy_func,
4420 &global_data.entropy,
4421 drbg_seed, sizeof( drbg_seed ) - 1 );
4422 if( ret != 0 )
4423 goto exit;
4424
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004425 global_data.initialized = 1;
4426
Gilles Peskinee59236f2018-01-27 23:32:46 +01004427exit:
4428 if( ret != 0 )
4429 mbedtls_psa_crypto_free( );
4430 return( mbedtls_to_psa_error( ret ) );
4431}
4432
4433#endif /* MBEDTLS_PSA_CRYPTO_C */