blob: 291dcdb0de541f54c73d310bf8a60c8383b3d45b [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 );
itayzafrir910c76b2018-11-21 16:03:21 +02001123 if( data == NULL )
1124 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001125 /* Get key data in export format */
1126 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1127 if( status != PSA_SUCCESS )
1128 {
1129 slot->type = PSA_KEY_TYPE_NONE;
1130 goto exit;
1131 }
1132 /* Store in file location */
1133 status = psa_save_persistent_key( key, slot->type, &slot->policy,
1134 data, key_length );
1135 if( status != PSA_SUCCESS )
1136 {
1137 slot->type = PSA_KEY_TYPE_NONE;
1138 }
1139exit:
1140 mbedtls_zeroize( data, key_length );
1141 mbedtls_free( data );
1142 return( status );
1143}
1144#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1145
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001146
1147
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001148/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001149/* Message digests */
1150/****************************************************************/
1151
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001152static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001153{
1154 switch( alg )
1155 {
1156#if defined(MBEDTLS_MD2_C)
1157 case PSA_ALG_MD2:
1158 return( &mbedtls_md2_info );
1159#endif
1160#if defined(MBEDTLS_MD4_C)
1161 case PSA_ALG_MD4:
1162 return( &mbedtls_md4_info );
1163#endif
1164#if defined(MBEDTLS_MD5_C)
1165 case PSA_ALG_MD5:
1166 return( &mbedtls_md5_info );
1167#endif
1168#if defined(MBEDTLS_RIPEMD160_C)
1169 case PSA_ALG_RIPEMD160:
1170 return( &mbedtls_ripemd160_info );
1171#endif
1172#if defined(MBEDTLS_SHA1_C)
1173 case PSA_ALG_SHA_1:
1174 return( &mbedtls_sha1_info );
1175#endif
1176#if defined(MBEDTLS_SHA256_C)
1177 case PSA_ALG_SHA_224:
1178 return( &mbedtls_sha224_info );
1179 case PSA_ALG_SHA_256:
1180 return( &mbedtls_sha256_info );
1181#endif
1182#if defined(MBEDTLS_SHA512_C)
1183 case PSA_ALG_SHA_384:
1184 return( &mbedtls_sha384_info );
1185 case PSA_ALG_SHA_512:
1186 return( &mbedtls_sha512_info );
1187#endif
1188 default:
1189 return( NULL );
1190 }
1191}
1192
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001193psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1194{
1195 switch( operation->alg )
1196 {
Gilles Peskine81736312018-06-26 15:04:31 +02001197 case 0:
1198 /* The object has (apparently) been initialized but it is not
1199 * in use. It's ok to call abort on such an object, and there's
1200 * nothing to do. */
1201 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001202#if defined(MBEDTLS_MD2_C)
1203 case PSA_ALG_MD2:
1204 mbedtls_md2_free( &operation->ctx.md2 );
1205 break;
1206#endif
1207#if defined(MBEDTLS_MD4_C)
1208 case PSA_ALG_MD4:
1209 mbedtls_md4_free( &operation->ctx.md4 );
1210 break;
1211#endif
1212#if defined(MBEDTLS_MD5_C)
1213 case PSA_ALG_MD5:
1214 mbedtls_md5_free( &operation->ctx.md5 );
1215 break;
1216#endif
1217#if defined(MBEDTLS_RIPEMD160_C)
1218 case PSA_ALG_RIPEMD160:
1219 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1220 break;
1221#endif
1222#if defined(MBEDTLS_SHA1_C)
1223 case PSA_ALG_SHA_1:
1224 mbedtls_sha1_free( &operation->ctx.sha1 );
1225 break;
1226#endif
1227#if defined(MBEDTLS_SHA256_C)
1228 case PSA_ALG_SHA_224:
1229 case PSA_ALG_SHA_256:
1230 mbedtls_sha256_free( &operation->ctx.sha256 );
1231 break;
1232#endif
1233#if defined(MBEDTLS_SHA512_C)
1234 case PSA_ALG_SHA_384:
1235 case PSA_ALG_SHA_512:
1236 mbedtls_sha512_free( &operation->ctx.sha512 );
1237 break;
1238#endif
1239 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001240 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001241 }
1242 operation->alg = 0;
1243 return( PSA_SUCCESS );
1244}
1245
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001246psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001247 psa_algorithm_t alg )
1248{
1249 int ret;
1250 operation->alg = 0;
1251 switch( alg )
1252 {
1253#if defined(MBEDTLS_MD2_C)
1254 case PSA_ALG_MD2:
1255 mbedtls_md2_init( &operation->ctx.md2 );
1256 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1257 break;
1258#endif
1259#if defined(MBEDTLS_MD4_C)
1260 case PSA_ALG_MD4:
1261 mbedtls_md4_init( &operation->ctx.md4 );
1262 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1263 break;
1264#endif
1265#if defined(MBEDTLS_MD5_C)
1266 case PSA_ALG_MD5:
1267 mbedtls_md5_init( &operation->ctx.md5 );
1268 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1269 break;
1270#endif
1271#if defined(MBEDTLS_RIPEMD160_C)
1272 case PSA_ALG_RIPEMD160:
1273 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1274 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1275 break;
1276#endif
1277#if defined(MBEDTLS_SHA1_C)
1278 case PSA_ALG_SHA_1:
1279 mbedtls_sha1_init( &operation->ctx.sha1 );
1280 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1281 break;
1282#endif
1283#if defined(MBEDTLS_SHA256_C)
1284 case PSA_ALG_SHA_224:
1285 mbedtls_sha256_init( &operation->ctx.sha256 );
1286 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1287 break;
1288 case PSA_ALG_SHA_256:
1289 mbedtls_sha256_init( &operation->ctx.sha256 );
1290 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1291 break;
1292#endif
1293#if defined(MBEDTLS_SHA512_C)
1294 case PSA_ALG_SHA_384:
1295 mbedtls_sha512_init( &operation->ctx.sha512 );
1296 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1297 break;
1298 case PSA_ALG_SHA_512:
1299 mbedtls_sha512_init( &operation->ctx.sha512 );
1300 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1301 break;
1302#endif
1303 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001304 return( PSA_ALG_IS_HASH( alg ) ?
1305 PSA_ERROR_NOT_SUPPORTED :
1306 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001307 }
1308 if( ret == 0 )
1309 operation->alg = alg;
1310 else
1311 psa_hash_abort( operation );
1312 return( mbedtls_to_psa_error( ret ) );
1313}
1314
1315psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1316 const uint8_t *input,
1317 size_t input_length )
1318{
1319 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001320
1321 /* Don't require hash implementations to behave correctly on a
1322 * zero-length input, which may have an invalid pointer. */
1323 if( input_length == 0 )
1324 return( PSA_SUCCESS );
1325
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001326 switch( operation->alg )
1327 {
1328#if defined(MBEDTLS_MD2_C)
1329 case PSA_ALG_MD2:
1330 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1331 input, input_length );
1332 break;
1333#endif
1334#if defined(MBEDTLS_MD4_C)
1335 case PSA_ALG_MD4:
1336 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1337 input, input_length );
1338 break;
1339#endif
1340#if defined(MBEDTLS_MD5_C)
1341 case PSA_ALG_MD5:
1342 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1343 input, input_length );
1344 break;
1345#endif
1346#if defined(MBEDTLS_RIPEMD160_C)
1347 case PSA_ALG_RIPEMD160:
1348 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1349 input, input_length );
1350 break;
1351#endif
1352#if defined(MBEDTLS_SHA1_C)
1353 case PSA_ALG_SHA_1:
1354 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1355 input, input_length );
1356 break;
1357#endif
1358#if defined(MBEDTLS_SHA256_C)
1359 case PSA_ALG_SHA_224:
1360 case PSA_ALG_SHA_256:
1361 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1362 input, input_length );
1363 break;
1364#endif
1365#if defined(MBEDTLS_SHA512_C)
1366 case PSA_ALG_SHA_384:
1367 case PSA_ALG_SHA_512:
1368 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1369 input, input_length );
1370 break;
1371#endif
1372 default:
1373 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1374 break;
1375 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001376
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001377 if( ret != 0 )
1378 psa_hash_abort( operation );
1379 return( mbedtls_to_psa_error( ret ) );
1380}
1381
1382psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1383 uint8_t *hash,
1384 size_t hash_size,
1385 size_t *hash_length )
1386{
itayzafrir40835d42018-08-02 13:14:17 +03001387 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001388 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001389 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001390
1391 /* Fill the output buffer with something that isn't a valid hash
1392 * (barring an attack on the hash and deliberately-crafted input),
1393 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001394 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001395 /* If hash_size is 0 then hash may be NULL and then the
1396 * call to memset would have undefined behavior. */
1397 if( hash_size != 0 )
1398 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001399
1400 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001401 {
1402 status = PSA_ERROR_BUFFER_TOO_SMALL;
1403 goto exit;
1404 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001405
1406 switch( operation->alg )
1407 {
1408#if defined(MBEDTLS_MD2_C)
1409 case PSA_ALG_MD2:
1410 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1411 break;
1412#endif
1413#if defined(MBEDTLS_MD4_C)
1414 case PSA_ALG_MD4:
1415 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1416 break;
1417#endif
1418#if defined(MBEDTLS_MD5_C)
1419 case PSA_ALG_MD5:
1420 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1421 break;
1422#endif
1423#if defined(MBEDTLS_RIPEMD160_C)
1424 case PSA_ALG_RIPEMD160:
1425 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1426 break;
1427#endif
1428#if defined(MBEDTLS_SHA1_C)
1429 case PSA_ALG_SHA_1:
1430 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1431 break;
1432#endif
1433#if defined(MBEDTLS_SHA256_C)
1434 case PSA_ALG_SHA_224:
1435 case PSA_ALG_SHA_256:
1436 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1437 break;
1438#endif
1439#if defined(MBEDTLS_SHA512_C)
1440 case PSA_ALG_SHA_384:
1441 case PSA_ALG_SHA_512:
1442 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1443 break;
1444#endif
1445 default:
1446 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1447 break;
1448 }
itayzafrir40835d42018-08-02 13:14:17 +03001449 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001450
itayzafrir40835d42018-08-02 13:14:17 +03001451exit:
1452 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001453 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001454 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001455 return( psa_hash_abort( operation ) );
1456 }
1457 else
1458 {
1459 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001460 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001461 }
1462}
1463
Gilles Peskine2d277862018-06-18 15:41:12 +02001464psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1465 const uint8_t *hash,
1466 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001467{
1468 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1469 size_t actual_hash_length;
1470 psa_status_t status = psa_hash_finish( operation,
1471 actual_hash, sizeof( actual_hash ),
1472 &actual_hash_length );
1473 if( status != PSA_SUCCESS )
1474 return( status );
1475 if( actual_hash_length != hash_length )
1476 return( PSA_ERROR_INVALID_SIGNATURE );
1477 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1478 return( PSA_ERROR_INVALID_SIGNATURE );
1479 return( PSA_SUCCESS );
1480}
1481
1482
1483
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001484/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001485/* MAC */
1486/****************************************************************/
1487
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001488static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001489 psa_algorithm_t alg,
1490 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001491 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001492 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001493{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001494 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001495 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001496
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001497 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001498 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001499
Gilles Peskine8c9def32018-02-08 10:02:12 +01001500 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1501 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001502 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001503 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001504 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001505 mode = MBEDTLS_MODE_STREAM;
1506 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001507 case PSA_ALG_CTR:
1508 mode = MBEDTLS_MODE_CTR;
1509 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001510 case PSA_ALG_CFB:
1511 mode = MBEDTLS_MODE_CFB;
1512 break;
1513 case PSA_ALG_OFB:
1514 mode = MBEDTLS_MODE_OFB;
1515 break;
1516 case PSA_ALG_CBC_NO_PADDING:
1517 mode = MBEDTLS_MODE_CBC;
1518 break;
1519 case PSA_ALG_CBC_PKCS7:
1520 mode = MBEDTLS_MODE_CBC;
1521 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001522 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001523 mode = MBEDTLS_MODE_CCM;
1524 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001525 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001526 mode = MBEDTLS_MODE_GCM;
1527 break;
1528 default:
1529 return( NULL );
1530 }
1531 }
1532 else if( alg == PSA_ALG_CMAC )
1533 mode = MBEDTLS_MODE_ECB;
1534 else if( alg == PSA_ALG_GMAC )
1535 mode = MBEDTLS_MODE_GCM;
1536 else
1537 return( NULL );
1538
1539 switch( key_type )
1540 {
1541 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001542 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001543 break;
1544 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001545 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1546 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001547 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001548 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001549 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001550 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001551 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1552 * but two-key Triple-DES is functionally three-key Triple-DES
1553 * with K1=K3, so that's how we present it to mbedtls. */
1554 if( key_bits == 128 )
1555 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001556 break;
1557 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001558 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001559 break;
1560 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001561 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001562 break;
1563 default:
1564 return( NULL );
1565 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001566 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001567 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001568
Jaeden Amero23bbb752018-06-26 14:16:54 +01001569 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1570 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001571}
1572
Gilles Peskinea05219c2018-11-16 16:02:56 +01001573#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001574static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001575{
Gilles Peskine2d277862018-06-18 15:41:12 +02001576 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001577 {
1578 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001579 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001580 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001581 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001582 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001583 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001584 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001585 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001586 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001587 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001588 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001589 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001590 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001591 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001592 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001593 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001594 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001595 return( 128 );
1596 default:
1597 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001598 }
1599}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001600#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001601
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001602/* Initialize the MAC operation structure. Once this function has been
1603 * called, psa_mac_abort can run and will do the right thing. */
1604static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1605 psa_algorithm_t alg )
1606{
1607 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1608
1609 operation->alg = alg;
1610 operation->key_set = 0;
1611 operation->iv_set = 0;
1612 operation->iv_required = 0;
1613 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001614 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001615
1616#if defined(MBEDTLS_CMAC_C)
1617 if( alg == PSA_ALG_CMAC )
1618 {
1619 operation->iv_required = 0;
1620 mbedtls_cipher_init( &operation->ctx.cmac );
1621 status = PSA_SUCCESS;
1622 }
1623 else
1624#endif /* MBEDTLS_CMAC_C */
1625#if defined(MBEDTLS_MD_C)
1626 if( PSA_ALG_IS_HMAC( operation->alg ) )
1627 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001628 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1629 operation->ctx.hmac.hash_ctx.alg = 0;
1630 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001631 }
1632 else
1633#endif /* MBEDTLS_MD_C */
1634 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001635 if( ! PSA_ALG_IS_MAC( alg ) )
1636 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001637 }
1638
1639 if( status != PSA_SUCCESS )
1640 memset( operation, 0, sizeof( *operation ) );
1641 return( status );
1642}
1643
Gilles Peskine01126fa2018-07-12 17:04:55 +02001644#if defined(MBEDTLS_MD_C)
1645static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1646{
1647 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1648 return( psa_hash_abort( &hmac->hash_ctx ) );
1649}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001650
1651static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1652{
1653 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1654 memset( hmac, 0, sizeof( *hmac ) );
1655}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001656#endif /* MBEDTLS_MD_C */
1657
Gilles Peskine8c9def32018-02-08 10:02:12 +01001658psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1659{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001660 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001661 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001662 /* The object has (apparently) been initialized but it is not
1663 * in use. It's ok to call abort on such an object, and there's
1664 * nothing to do. */
1665 return( PSA_SUCCESS );
1666 }
1667 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001668#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001669 if( operation->alg == PSA_ALG_CMAC )
1670 {
1671 mbedtls_cipher_free( &operation->ctx.cmac );
1672 }
1673 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001674#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001675#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001676 if( PSA_ALG_IS_HMAC( operation->alg ) )
1677 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001678 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001679 }
1680 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001681#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001682 {
1683 /* Sanity check (shouldn't happen: operation->alg should
1684 * always have been initialized to a valid value). */
1685 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001686 }
Moran Peker41deec42018-04-04 15:43:05 +03001687
Gilles Peskine8c9def32018-02-08 10:02:12 +01001688 operation->alg = 0;
1689 operation->key_set = 0;
1690 operation->iv_set = 0;
1691 operation->iv_required = 0;
1692 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001693 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001694
Gilles Peskine8c9def32018-02-08 10:02:12 +01001695 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001696
1697bad_state:
1698 /* If abort is called on an uninitialized object, we can't trust
1699 * anything. Wipe the object in case it contains confidential data.
1700 * This may result in a memory leak if a pointer gets overwritten,
1701 * but it's too late to do anything about this. */
1702 memset( operation, 0, sizeof( *operation ) );
1703 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001704}
1705
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001706#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001707static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001708 size_t key_bits,
1709 key_slot_t *slot,
1710 const mbedtls_cipher_info_t *cipher_info )
1711{
1712 int ret;
1713
1714 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001715
1716 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1717 if( ret != 0 )
1718 return( ret );
1719
1720 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1721 slot->data.raw.data,
1722 key_bits );
1723 return( ret );
1724}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001725#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001726
Gilles Peskine248051a2018-06-20 16:09:38 +02001727#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001728static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1729 const uint8_t *key,
1730 size_t key_length,
1731 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001732{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001733 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001734 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001735 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001736 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001737 psa_status_t status;
1738
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001739 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1740 * overflow below. This should never trigger if the hash algorithm
1741 * is implemented correctly. */
1742 /* The size checks against the ipad and opad buffers cannot be written
1743 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1744 * because that triggers -Wlogical-op on GCC 7.3. */
1745 if( block_size > sizeof( ipad ) )
1746 return( PSA_ERROR_NOT_SUPPORTED );
1747 if( block_size > sizeof( hmac->opad ) )
1748 return( PSA_ERROR_NOT_SUPPORTED );
1749 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001750 return( PSA_ERROR_NOT_SUPPORTED );
1751
Gilles Peskined223b522018-06-11 18:12:58 +02001752 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001753 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001754 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001755 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001756 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001757 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001758 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001759 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001760 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001761 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001762 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001763 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001764 }
Gilles Peskine96889972018-07-12 17:07:03 +02001765 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1766 * but it is permitted. It is common when HMAC is used in HKDF, for
1767 * example. Don't call `memcpy` in the 0-length because `key` could be
1768 * an invalid pointer which would make the behavior undefined. */
1769 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001770 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001771
Gilles Peskined223b522018-06-11 18:12:58 +02001772 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1773 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001774 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001775 ipad[i] ^= 0x36;
1776 memset( ipad + key_length, 0x36, block_size - key_length );
1777
1778 /* Copy the key material from ipad to opad, flipping the requisite bits,
1779 * and filling the rest of opad with the requisite constant. */
1780 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001781 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1782 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001783
Gilles Peskine01126fa2018-07-12 17:04:55 +02001784 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001785 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001786 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001787
Gilles Peskine01126fa2018-07-12 17:04:55 +02001788 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001789
1790cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001791 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001792
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001793 return( status );
1794}
Gilles Peskine248051a2018-06-20 16:09:38 +02001795#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001796
Gilles Peskine89167cb2018-07-08 20:12:23 +02001797static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1798 psa_key_slot_t key,
1799 psa_algorithm_t alg,
1800 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001801{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001802 psa_status_t status;
1803 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001804 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001805 psa_key_usage_t usage =
1806 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001807 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001808 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001809
Gilles Peskined911eb72018-08-14 15:18:45 +02001810 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001811 if( status != PSA_SUCCESS )
1812 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001813 if( is_sign )
1814 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001815
Gilles Peskine89167cb2018-07-08 20:12:23 +02001816 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001817 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001818 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001819 key_bits = psa_get_key_bits( slot );
1820
Gilles Peskine8c9def32018-02-08 10:02:12 +01001821#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001822 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001823 {
1824 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001825 mbedtls_cipher_info_from_psa( full_length_alg,
1826 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001827 int ret;
1828 if( cipher_info == NULL )
1829 {
1830 status = PSA_ERROR_NOT_SUPPORTED;
1831 goto exit;
1832 }
1833 operation->mac_size = cipher_info->block_size;
1834 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1835 status = mbedtls_to_psa_error( ret );
1836 }
1837 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001838#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001839#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001840 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001841 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001842 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001843 if( hash_alg == 0 )
1844 {
1845 status = PSA_ERROR_NOT_SUPPORTED;
1846 goto exit;
1847 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001848
1849 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1850 /* Sanity check. This shouldn't fail on a valid configuration. */
1851 if( operation->mac_size == 0 ||
1852 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1853 {
1854 status = PSA_ERROR_NOT_SUPPORTED;
1855 goto exit;
1856 }
1857
Gilles Peskine01126fa2018-07-12 17:04:55 +02001858 if( slot->type != PSA_KEY_TYPE_HMAC )
1859 {
1860 status = PSA_ERROR_INVALID_ARGUMENT;
1861 goto exit;
1862 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001863
Gilles Peskine01126fa2018-07-12 17:04:55 +02001864 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1865 slot->data.raw.data,
1866 slot->data.raw.bytes,
1867 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001868 }
1869 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001870#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001871 {
1872 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001873 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001874
Gilles Peskined911eb72018-08-14 15:18:45 +02001875 if( truncated == 0 )
1876 {
1877 /* The "normal" case: untruncated algorithm. Nothing to do. */
1878 }
1879 else if( truncated < 4 )
1880 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001881 /* A very short MAC is too short for security since it can be
1882 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1883 * so we make this our minimum, even though 32 bits is still
1884 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001885 status = PSA_ERROR_NOT_SUPPORTED;
1886 }
1887 else if( truncated > operation->mac_size )
1888 {
1889 /* It's impossible to "truncate" to a larger length. */
1890 status = PSA_ERROR_INVALID_ARGUMENT;
1891 }
1892 else
1893 operation->mac_size = truncated;
1894
Gilles Peskinefbfac682018-07-08 20:51:54 +02001895exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001896 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001897 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001898 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001899 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001900 else
1901 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001902 operation->key_set = 1;
1903 }
1904 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001905}
1906
Gilles Peskine89167cb2018-07-08 20:12:23 +02001907psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1908 psa_key_slot_t key,
1909 psa_algorithm_t alg )
1910{
1911 return( psa_mac_setup( operation, key, alg, 1 ) );
1912}
1913
1914psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1915 psa_key_slot_t key,
1916 psa_algorithm_t alg )
1917{
1918 return( psa_mac_setup( operation, key, alg, 0 ) );
1919}
1920
Gilles Peskine8c9def32018-02-08 10:02:12 +01001921psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1922 const uint8_t *input,
1923 size_t input_length )
1924{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001925 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001926 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001927 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001928 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001929 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001930 operation->has_input = 1;
1931
Gilles Peskine8c9def32018-02-08 10:02:12 +01001932#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001933 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001934 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001935 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1936 input, input_length );
1937 status = mbedtls_to_psa_error( ret );
1938 }
1939 else
1940#endif /* MBEDTLS_CMAC_C */
1941#if defined(MBEDTLS_MD_C)
1942 if( PSA_ALG_IS_HMAC( operation->alg ) )
1943 {
1944 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1945 input_length );
1946 }
1947 else
1948#endif /* MBEDTLS_MD_C */
1949 {
1950 /* This shouldn't happen if `operation` was initialized by
1951 * a setup function. */
1952 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001953 }
1954
Gilles Peskinefbfac682018-07-08 20:51:54 +02001955cleanup:
1956 if( status != PSA_SUCCESS )
1957 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001958 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001959}
1960
Gilles Peskine01126fa2018-07-12 17:04:55 +02001961#if defined(MBEDTLS_MD_C)
1962static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1963 uint8_t *mac,
1964 size_t mac_size )
1965{
1966 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1967 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1968 size_t hash_size = 0;
1969 size_t block_size = psa_get_hash_block_size( hash_alg );
1970 psa_status_t status;
1971
Gilles Peskine01126fa2018-07-12 17:04:55 +02001972 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1973 if( status != PSA_SUCCESS )
1974 return( status );
1975 /* From here on, tmp needs to be wiped. */
1976
1977 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1978 if( status != PSA_SUCCESS )
1979 goto exit;
1980
1981 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1982 if( status != PSA_SUCCESS )
1983 goto exit;
1984
1985 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1986 if( status != PSA_SUCCESS )
1987 goto exit;
1988
Gilles Peskined911eb72018-08-14 15:18:45 +02001989 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1990 if( status != PSA_SUCCESS )
1991 goto exit;
1992
1993 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001994
1995exit:
1996 mbedtls_zeroize( tmp, hash_size );
1997 return( status );
1998}
1999#endif /* MBEDTLS_MD_C */
2000
mohammad16036df908f2018-04-02 08:34:15 -07002001static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002002 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002003 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002004{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002005 if( ! operation->key_set )
2006 return( PSA_ERROR_BAD_STATE );
2007 if( operation->iv_required && ! operation->iv_set )
2008 return( PSA_ERROR_BAD_STATE );
2009
Gilles Peskine8c9def32018-02-08 10:02:12 +01002010 if( mac_size < operation->mac_size )
2011 return( PSA_ERROR_BUFFER_TOO_SMALL );
2012
Gilles Peskine8c9def32018-02-08 10:02:12 +01002013#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002014 if( operation->alg == PSA_ALG_CMAC )
2015 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002016 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2017 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2018 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002019 memcpy( mac, tmp, operation->mac_size );
Gilles Peskined911eb72018-08-14 15:18:45 +02002020 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002021 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002022 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002023 else
2024#endif /* MBEDTLS_CMAC_C */
2025#if defined(MBEDTLS_MD_C)
2026 if( PSA_ALG_IS_HMAC( operation->alg ) )
2027 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002028 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002029 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002030 }
2031 else
2032#endif /* MBEDTLS_MD_C */
2033 {
2034 /* This shouldn't happen if `operation` was initialized by
2035 * a setup function. */
2036 return( PSA_ERROR_BAD_STATE );
2037 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002038}
2039
Gilles Peskineacd4be32018-07-08 19:56:25 +02002040psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2041 uint8_t *mac,
2042 size_t mac_size,
2043 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002044{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002045 psa_status_t status;
2046
2047 /* Fill the output buffer with something that isn't a valid mac
2048 * (barring an attack on the mac and deliberately-crafted input),
2049 * in case the caller doesn't check the return status properly. */
2050 *mac_length = mac_size;
2051 /* If mac_size is 0 then mac may be NULL and then the
2052 * call to memset would have undefined behavior. */
2053 if( mac_size != 0 )
2054 memset( mac, '!', mac_size );
2055
Gilles Peskine89167cb2018-07-08 20:12:23 +02002056 if( ! operation->is_sign )
2057 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002058 status = PSA_ERROR_BAD_STATE;
2059 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002060 }
mohammad16036df908f2018-04-02 08:34:15 -07002061
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002062 status = psa_mac_finish_internal( operation, mac, mac_size );
2063
2064cleanup:
2065 if( status == PSA_SUCCESS )
2066 {
2067 status = psa_mac_abort( operation );
2068 if( status == PSA_SUCCESS )
2069 *mac_length = operation->mac_size;
2070 else
2071 memset( mac, '!', mac_size );
2072 }
2073 else
2074 psa_mac_abort( operation );
2075 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002076}
2077
Gilles Peskineacd4be32018-07-08 19:56:25 +02002078psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2079 const uint8_t *mac,
2080 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002081{
Gilles Peskine828ed142018-06-18 23:25:51 +02002082 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002083 psa_status_t status;
2084
Gilles Peskine89167cb2018-07-08 20:12:23 +02002085 if( operation->is_sign )
2086 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002087 status = PSA_ERROR_BAD_STATE;
2088 goto cleanup;
2089 }
2090 if( operation->mac_size != mac_length )
2091 {
2092 status = PSA_ERROR_INVALID_SIGNATURE;
2093 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002094 }
mohammad16036df908f2018-04-02 08:34:15 -07002095
2096 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002097 actual_mac, sizeof( actual_mac ) );
2098
2099 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2100 status = PSA_ERROR_INVALID_SIGNATURE;
2101
2102cleanup:
2103 if( status == PSA_SUCCESS )
2104 status = psa_mac_abort( operation );
2105 else
2106 psa_mac_abort( operation );
2107
Gilles Peskine99b7d6b2018-08-21 14:56:19 +02002108 mbedtls_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002109
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002110 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002111}
2112
2113
Gilles Peskine20035e32018-02-03 22:44:14 +01002114
Gilles Peskine20035e32018-02-03 22:44:14 +01002115/****************************************************************/
2116/* Asymmetric cryptography */
2117/****************************************************************/
2118
Gilles Peskine2b450e32018-06-27 15:42:46 +02002119#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002120/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002121 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002122static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2123 size_t hash_length,
2124 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002125{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002126 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002127 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002128 *md_alg = mbedtls_md_get_type( md_info );
2129
2130 /* The Mbed TLS RSA module uses an unsigned int for hash length
2131 * parameters. Validate that it fits so that we don't risk an
2132 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002133#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002134 if( hash_length > UINT_MAX )
2135 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002136#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002137
2138#if defined(MBEDTLS_PKCS1_V15)
2139 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2140 * must be correct. */
2141 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2142 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002143 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002144 if( md_info == NULL )
2145 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002146 if( mbedtls_md_get_size( md_info ) != hash_length )
2147 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002148 }
2149#endif /* MBEDTLS_PKCS1_V15 */
2150
2151#if defined(MBEDTLS_PKCS1_V21)
2152 /* PSS requires a hash internally. */
2153 if( PSA_ALG_IS_RSA_PSS( alg ) )
2154 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002155 if( md_info == NULL )
2156 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002157 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002158#endif /* MBEDTLS_PKCS1_V21 */
2159
Gilles Peskine61b91d42018-06-08 16:09:36 +02002160 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002161}
2162
Gilles Peskine2b450e32018-06-27 15:42:46 +02002163static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2164 psa_algorithm_t alg,
2165 const uint8_t *hash,
2166 size_t hash_length,
2167 uint8_t *signature,
2168 size_t signature_size,
2169 size_t *signature_length )
2170{
2171 psa_status_t status;
2172 int ret;
2173 mbedtls_md_type_t md_alg;
2174
2175 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2176 if( status != PSA_SUCCESS )
2177 return( status );
2178
Gilles Peskine630a18a2018-06-29 17:49:35 +02002179 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002180 return( PSA_ERROR_BUFFER_TOO_SMALL );
2181
2182#if defined(MBEDTLS_PKCS1_V15)
2183 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2184 {
2185 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2186 MBEDTLS_MD_NONE );
2187 ret = mbedtls_rsa_pkcs1_sign( rsa,
2188 mbedtls_ctr_drbg_random,
2189 &global_data.ctr_drbg,
2190 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002191 md_alg,
2192 (unsigned int) hash_length,
2193 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002194 signature );
2195 }
2196 else
2197#endif /* MBEDTLS_PKCS1_V15 */
2198#if defined(MBEDTLS_PKCS1_V21)
2199 if( PSA_ALG_IS_RSA_PSS( alg ) )
2200 {
2201 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2202 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2203 mbedtls_ctr_drbg_random,
2204 &global_data.ctr_drbg,
2205 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002206 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002207 (unsigned int) hash_length,
2208 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002209 signature );
2210 }
2211 else
2212#endif /* MBEDTLS_PKCS1_V21 */
2213 {
2214 return( PSA_ERROR_INVALID_ARGUMENT );
2215 }
2216
2217 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002218 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002219 return( mbedtls_to_psa_error( ret ) );
2220}
2221
2222static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2223 psa_algorithm_t alg,
2224 const uint8_t *hash,
2225 size_t hash_length,
2226 const uint8_t *signature,
2227 size_t signature_length )
2228{
2229 psa_status_t status;
2230 int ret;
2231 mbedtls_md_type_t md_alg;
2232
2233 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2234 if( status != PSA_SUCCESS )
2235 return( status );
2236
Gilles Peskine630a18a2018-06-29 17:49:35 +02002237 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002238 return( PSA_ERROR_BUFFER_TOO_SMALL );
2239
2240#if defined(MBEDTLS_PKCS1_V15)
2241 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2242 {
2243 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2244 MBEDTLS_MD_NONE );
2245 ret = mbedtls_rsa_pkcs1_verify( rsa,
2246 mbedtls_ctr_drbg_random,
2247 &global_data.ctr_drbg,
2248 MBEDTLS_RSA_PUBLIC,
2249 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002250 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002251 hash,
2252 signature );
2253 }
2254 else
2255#endif /* MBEDTLS_PKCS1_V15 */
2256#if defined(MBEDTLS_PKCS1_V21)
2257 if( PSA_ALG_IS_RSA_PSS( alg ) )
2258 {
2259 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2260 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2261 mbedtls_ctr_drbg_random,
2262 &global_data.ctr_drbg,
2263 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002264 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002265 (unsigned int) hash_length,
2266 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002267 signature );
2268 }
2269 else
2270#endif /* MBEDTLS_PKCS1_V21 */
2271 {
2272 return( PSA_ERROR_INVALID_ARGUMENT );
2273 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002274
2275 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2276 * the rest of the signature is invalid". This has little use in
2277 * practice and PSA doesn't report this distinction. */
2278 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2279 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002280 return( mbedtls_to_psa_error( ret ) );
2281}
2282#endif /* MBEDTLS_RSA_C */
2283
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002284#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002285/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2286 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2287 * (even though these functions don't modify it). */
2288static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2289 psa_algorithm_t alg,
2290 const uint8_t *hash,
2291 size_t hash_length,
2292 uint8_t *signature,
2293 size_t signature_size,
2294 size_t *signature_length )
2295{
2296 int ret;
2297 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002298 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002299 mbedtls_mpi_init( &r );
2300 mbedtls_mpi_init( &s );
2301
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002302 if( signature_size < 2 * curve_bytes )
2303 {
2304 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2305 goto cleanup;
2306 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002307
Gilles Peskinea05219c2018-11-16 16:02:56 +01002308#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002309 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2310 {
2311 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2312 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2313 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2314 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2315 hash, hash_length,
2316 md_alg ) );
2317 }
2318 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002319#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002320 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002321 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002322 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2323 hash, hash_length,
2324 mbedtls_ctr_drbg_random,
2325 &global_data.ctr_drbg ) );
2326 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002327
2328 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2329 signature,
2330 curve_bytes ) );
2331 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2332 signature + curve_bytes,
2333 curve_bytes ) );
2334
2335cleanup:
2336 mbedtls_mpi_free( &r );
2337 mbedtls_mpi_free( &s );
2338 if( ret == 0 )
2339 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002340 return( mbedtls_to_psa_error( ret ) );
2341}
2342
2343static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2344 const uint8_t *hash,
2345 size_t hash_length,
2346 const uint8_t *signature,
2347 size_t signature_length )
2348{
2349 int ret;
2350 mbedtls_mpi r, s;
2351 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2352 mbedtls_mpi_init( &r );
2353 mbedtls_mpi_init( &s );
2354
2355 if( signature_length != 2 * curve_bytes )
2356 return( PSA_ERROR_INVALID_SIGNATURE );
2357
2358 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2359 signature,
2360 curve_bytes ) );
2361 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2362 signature + curve_bytes,
2363 curve_bytes ) );
2364
2365 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2366 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002367
2368cleanup:
2369 mbedtls_mpi_free( &r );
2370 mbedtls_mpi_free( &s );
2371 return( mbedtls_to_psa_error( ret ) );
2372}
2373#endif /* MBEDTLS_ECDSA_C */
2374
Gilles Peskine61b91d42018-06-08 16:09:36 +02002375psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2376 psa_algorithm_t alg,
2377 const uint8_t *hash,
2378 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002379 uint8_t *signature,
2380 size_t signature_size,
2381 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002382{
2383 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002384 psa_status_t status;
2385
2386 *signature_length = signature_size;
2387
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002388 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2389 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002390 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002391 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002392 {
2393 status = PSA_ERROR_INVALID_ARGUMENT;
2394 goto exit;
2395 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002396
Gilles Peskine20035e32018-02-03 22:44:14 +01002397#if defined(MBEDTLS_RSA_C)
2398 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2399 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002400 status = psa_rsa_sign( slot->data.rsa,
2401 alg,
2402 hash, hash_length,
2403 signature, signature_size,
2404 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002405 }
2406 else
2407#endif /* defined(MBEDTLS_RSA_C) */
2408#if defined(MBEDTLS_ECP_C)
2409 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2410 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002411#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002412 if(
2413#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2414 PSA_ALG_IS_ECDSA( alg )
2415#else
2416 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2417#endif
2418 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002419 status = psa_ecdsa_sign( slot->data.ecp,
2420 alg,
2421 hash, hash_length,
2422 signature, signature_size,
2423 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002424 else
2425#endif /* defined(MBEDTLS_ECDSA_C) */
2426 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002427 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002428 }
itayzafrir5c753392018-05-08 11:18:38 +03002429 }
2430 else
2431#endif /* defined(MBEDTLS_ECP_C) */
2432 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002433 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002434 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002435
2436exit:
2437 /* Fill the unused part of the output buffer (the whole buffer on error,
2438 * the trailing part on success) with something that isn't a valid mac
2439 * (barring an attack on the mac and deliberately-crafted input),
2440 * in case the caller doesn't check the return status properly. */
2441 if( status == PSA_SUCCESS )
2442 memset( signature + *signature_length, '!',
2443 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002444 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002445 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002446 /* If signature_size is 0 then we have nothing to do. We must not call
2447 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002448 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002449}
2450
2451psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2452 psa_algorithm_t alg,
2453 const uint8_t *hash,
2454 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002455 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002456 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002457{
2458 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002459 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002460
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002461 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2462 if( status != PSA_SUCCESS )
2463 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002464
Gilles Peskine61b91d42018-06-08 16:09:36 +02002465#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002466 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002467 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002468 return( psa_rsa_verify( slot->data.rsa,
2469 alg,
2470 hash, hash_length,
2471 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002472 }
2473 else
2474#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002475#if defined(MBEDTLS_ECP_C)
2476 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2477 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002478#if defined(MBEDTLS_ECDSA_C)
2479 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002480 return( psa_ecdsa_verify( slot->data.ecp,
2481 hash, hash_length,
2482 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002483 else
2484#endif /* defined(MBEDTLS_ECDSA_C) */
2485 {
2486 return( PSA_ERROR_INVALID_ARGUMENT );
2487 }
itayzafrir5c753392018-05-08 11:18:38 +03002488 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002489 else
2490#endif /* defined(MBEDTLS_ECP_C) */
2491 {
2492 return( PSA_ERROR_NOT_SUPPORTED );
2493 }
2494}
2495
Gilles Peskine072ac562018-06-30 00:21:29 +02002496#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2497static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2498 mbedtls_rsa_context *rsa )
2499{
2500 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2501 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2502 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2503 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2504}
2505#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2506
Gilles Peskine61b91d42018-06-08 16:09:36 +02002507psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2508 psa_algorithm_t alg,
2509 const uint8_t *input,
2510 size_t input_length,
2511 const uint8_t *salt,
2512 size_t salt_length,
2513 uint8_t *output,
2514 size_t output_size,
2515 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002516{
2517 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002518 psa_status_t status;
2519
Darryl Green5cc689a2018-07-24 15:34:10 +01002520 (void) input;
2521 (void) input_length;
2522 (void) salt;
2523 (void) output;
2524 (void) output_size;
2525
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002526 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002527
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002528 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2529 return( PSA_ERROR_INVALID_ARGUMENT );
2530
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002531 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2532 if( status != PSA_SUCCESS )
2533 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002534 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2535 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002536 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002537
2538#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002539 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002540 {
2541 mbedtls_rsa_context *rsa = slot->data.rsa;
2542 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002543 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002544 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002545#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002546 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002547 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002548 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2549 mbedtls_ctr_drbg_random,
2550 &global_data.ctr_drbg,
2551 MBEDTLS_RSA_PUBLIC,
2552 input_length,
2553 input,
2554 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002555 }
2556 else
2557#endif /* MBEDTLS_PKCS1_V15 */
2558#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002559 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002560 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002561 psa_rsa_oaep_set_padding_mode( alg, rsa );
2562 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2563 mbedtls_ctr_drbg_random,
2564 &global_data.ctr_drbg,
2565 MBEDTLS_RSA_PUBLIC,
2566 salt, salt_length,
2567 input_length,
2568 input,
2569 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002570 }
2571 else
2572#endif /* MBEDTLS_PKCS1_V21 */
2573 {
2574 return( PSA_ERROR_INVALID_ARGUMENT );
2575 }
2576 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002577 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002578 return( mbedtls_to_psa_error( ret ) );
2579 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002580 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002581#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002582 {
2583 return( PSA_ERROR_NOT_SUPPORTED );
2584 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002585}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002586
Gilles Peskine61b91d42018-06-08 16:09:36 +02002587psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2588 psa_algorithm_t alg,
2589 const uint8_t *input,
2590 size_t input_length,
2591 const uint8_t *salt,
2592 size_t salt_length,
2593 uint8_t *output,
2594 size_t output_size,
2595 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002596{
2597 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002598 psa_status_t status;
2599
Darryl Green5cc689a2018-07-24 15:34:10 +01002600 (void) input;
2601 (void) input_length;
2602 (void) salt;
2603 (void) output;
2604 (void) output_size;
2605
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002606 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002607
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002608 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2609 return( PSA_ERROR_INVALID_ARGUMENT );
2610
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002611 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2612 if( status != PSA_SUCCESS )
2613 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002614 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2615 return( PSA_ERROR_INVALID_ARGUMENT );
2616
2617#if defined(MBEDTLS_RSA_C)
2618 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2619 {
2620 mbedtls_rsa_context *rsa = slot->data.rsa;
2621 int ret;
2622
Gilles Peskine630a18a2018-06-29 17:49:35 +02002623 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002624 return( PSA_ERROR_INVALID_ARGUMENT );
2625
2626#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002627 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002628 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002629 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2630 mbedtls_ctr_drbg_random,
2631 &global_data.ctr_drbg,
2632 MBEDTLS_RSA_PRIVATE,
2633 output_length,
2634 input,
2635 output,
2636 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002637 }
2638 else
2639#endif /* MBEDTLS_PKCS1_V15 */
2640#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002641 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002642 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002643 psa_rsa_oaep_set_padding_mode( alg, rsa );
2644 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2645 mbedtls_ctr_drbg_random,
2646 &global_data.ctr_drbg,
2647 MBEDTLS_RSA_PRIVATE,
2648 salt, salt_length,
2649 output_length,
2650 input,
2651 output,
2652 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002653 }
2654 else
2655#endif /* MBEDTLS_PKCS1_V21 */
2656 {
2657 return( PSA_ERROR_INVALID_ARGUMENT );
2658 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002659
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002660 return( mbedtls_to_psa_error( ret ) );
2661 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002662 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002663#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002664 {
2665 return( PSA_ERROR_NOT_SUPPORTED );
2666 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002667}
Gilles Peskine20035e32018-02-03 22:44:14 +01002668
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002669
2670
mohammad1603503973b2018-03-12 15:59:30 +02002671/****************************************************************/
2672/* Symmetric cryptography */
2673/****************************************************************/
2674
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002675/* Initialize the cipher operation structure. Once this function has been
2676 * called, psa_cipher_abort can run and will do the right thing. */
2677static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2678 psa_algorithm_t alg )
2679{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002680 if( ! PSA_ALG_IS_CIPHER( alg ) )
2681 {
2682 memset( operation, 0, sizeof( *operation ) );
2683 return( PSA_ERROR_INVALID_ARGUMENT );
2684 }
2685
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002686 operation->alg = alg;
2687 operation->key_set = 0;
2688 operation->iv_set = 0;
2689 operation->iv_required = 1;
2690 operation->iv_size = 0;
2691 operation->block_size = 0;
2692 mbedtls_cipher_init( &operation->ctx.cipher );
2693 return( PSA_SUCCESS );
2694}
2695
Gilles Peskinee553c652018-06-04 16:22:46 +02002696static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2697 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002698 psa_algorithm_t alg,
2699 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002700{
2701 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2702 psa_status_t status;
2703 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002704 size_t key_bits;
2705 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002706 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2707 PSA_KEY_USAGE_ENCRYPT :
2708 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002709
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002710 status = psa_cipher_init( operation, alg );
2711 if( status != PSA_SUCCESS )
2712 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002713
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002714 status = psa_get_key_from_slot( key, &slot, usage, alg);
2715 if( status != PSA_SUCCESS )
2716 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002717 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002718
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002719 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002720 if( cipher_info == NULL )
2721 return( PSA_ERROR_NOT_SUPPORTED );
2722
mohammad1603503973b2018-03-12 15:59:30 +02002723 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002724 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002725 {
2726 psa_cipher_abort( operation );
2727 return( mbedtls_to_psa_error( ret ) );
2728 }
2729
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002730#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002731 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002732 {
2733 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2734 unsigned char keys[24];
2735 memcpy( keys, slot->data.raw.data, 16 );
2736 memcpy( keys + 16, slot->data.raw.data, 8 );
2737 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2738 keys,
2739 192, cipher_operation );
2740 }
2741 else
2742#endif
2743 {
2744 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2745 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002746 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002747 }
Moran Peker41deec42018-04-04 15:43:05 +03002748 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002749 {
2750 psa_cipher_abort( operation );
2751 return( mbedtls_to_psa_error( ret ) );
2752 }
2753
mohammad16038481e742018-03-18 13:57:31 +02002754#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002755 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002756 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002757 case PSA_ALG_CBC_NO_PADDING:
2758 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2759 MBEDTLS_PADDING_NONE );
2760 break;
2761 case PSA_ALG_CBC_PKCS7:
2762 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2763 MBEDTLS_PADDING_PKCS7 );
2764 break;
2765 default:
2766 /* The algorithm doesn't involve padding. */
2767 ret = 0;
2768 break;
2769 }
2770 if( ret != 0 )
2771 {
2772 psa_cipher_abort( operation );
2773 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002774 }
2775#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2776
mohammad1603503973b2018-03-12 15:59:30 +02002777 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002778 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2779 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2780 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002781 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002782 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002783 }
mohammad1603503973b2018-03-12 15:59:30 +02002784
Moran Peker395db872018-05-31 14:07:14 +03002785 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002786}
2787
Gilles Peskinefe119512018-07-08 21:39:34 +02002788psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2789 psa_key_slot_t key,
2790 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002791{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002792 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002793}
2794
Gilles Peskinefe119512018-07-08 21:39:34 +02002795psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2796 psa_key_slot_t key,
2797 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002798{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002799 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002800}
2801
Gilles Peskinefe119512018-07-08 21:39:34 +02002802psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2803 unsigned char *iv,
2804 size_t iv_size,
2805 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002806{
itayzafrir534bd7c2018-08-02 13:56:32 +03002807 psa_status_t status;
2808 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002809 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002810 {
2811 status = PSA_ERROR_BAD_STATE;
2812 goto exit;
2813 }
Moran Peker41deec42018-04-04 15:43:05 +03002814 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002815 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002816 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002817 goto exit;
2818 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002819 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2820 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002821 if( ret != 0 )
2822 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002823 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002824 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002825 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002826
mohammad16038481e742018-03-18 13:57:31 +02002827 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002828 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002829
Moran Peker395db872018-05-31 14:07:14 +03002830exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002831 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002832 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002833 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002834}
2835
Gilles Peskinefe119512018-07-08 21:39:34 +02002836psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2837 const unsigned char *iv,
2838 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002839{
itayzafrir534bd7c2018-08-02 13:56:32 +03002840 psa_status_t status;
2841 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002842 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002843 {
2844 status = PSA_ERROR_BAD_STATE;
2845 goto exit;
2846 }
Moran Pekera28258c2018-05-29 16:25:04 +03002847 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002848 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002849 status = PSA_ERROR_INVALID_ARGUMENT;
2850 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002851 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002852 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2853 status = mbedtls_to_psa_error( ret );
2854exit:
2855 if( status == PSA_SUCCESS )
2856 operation->iv_set = 1;
2857 else
mohammad1603503973b2018-03-12 15:59:30 +02002858 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002859 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002860}
2861
Gilles Peskinee553c652018-06-04 16:22:46 +02002862psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2863 const uint8_t *input,
2864 size_t input_length,
2865 unsigned char *output,
2866 size_t output_size,
2867 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002868{
itayzafrir534bd7c2018-08-02 13:56:32 +03002869 psa_status_t status;
2870 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002871 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002872 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002873 {
2874 /* Take the unprocessed partial block left over from previous
2875 * update calls, if any, plus the input to this call. Remove
2876 * the last partial block, if any. You get the data that will be
2877 * output in this call. */
2878 expected_output_size =
2879 ( operation->ctx.cipher.unprocessed_len + input_length )
2880 / operation->block_size * operation->block_size;
2881 }
2882 else
2883 {
2884 expected_output_size = input_length;
2885 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002886
Gilles Peskine89d789c2018-06-04 17:17:16 +02002887 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002888 {
2889 status = PSA_ERROR_BUFFER_TOO_SMALL;
2890 goto exit;
2891 }
mohammad160382759612018-03-12 18:16:40 +02002892
mohammad1603503973b2018-03-12 15:59:30 +02002893 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002894 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002895 status = mbedtls_to_psa_error( ret );
2896exit:
2897 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002898 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002899 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002900}
2901
Gilles Peskinee553c652018-06-04 16:22:46 +02002902psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2903 uint8_t *output,
2904 size_t output_size,
2905 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002906{
Janos Follath315b51c2018-07-09 16:04:51 +01002907 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2908 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002909 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002910
mohammad1603503973b2018-03-12 15:59:30 +02002911 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002912 {
Janos Follath315b51c2018-07-09 16:04:51 +01002913 status = PSA_ERROR_BAD_STATE;
2914 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002915 }
2916 if( operation->iv_required && ! operation->iv_set )
2917 {
Janos Follath315b51c2018-07-09 16:04:51 +01002918 status = PSA_ERROR_BAD_STATE;
2919 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002920 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002921
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002922 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002923 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2924 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002925 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002926 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002927 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002928 }
2929
Janos Follath315b51c2018-07-09 16:04:51 +01002930 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2931 temp_output_buffer,
2932 output_length );
2933 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002934 {
Janos Follath315b51c2018-07-09 16:04:51 +01002935 status = mbedtls_to_psa_error( cipher_ret );
2936 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002937 }
Janos Follath315b51c2018-07-09 16:04:51 +01002938
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002939 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002940 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002941 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002942 memcpy( output, temp_output_buffer, *output_length );
2943 else
2944 {
Janos Follath315b51c2018-07-09 16:04:51 +01002945 status = PSA_ERROR_BUFFER_TOO_SMALL;
2946 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002947 }
mohammad1603503973b2018-03-12 15:59:30 +02002948
Janos Follath279ab8e2018-07-09 16:13:21 +01002949 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002950 status = psa_cipher_abort( operation );
2951
2952 return( status );
2953
2954error:
2955
2956 *output_length = 0;
2957
Janos Follath279ab8e2018-07-09 16:13:21 +01002958 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002959 (void) psa_cipher_abort( operation );
2960
2961 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002962}
2963
Gilles Peskinee553c652018-06-04 16:22:46 +02002964psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2965{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002966 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002967 {
2968 /* The object has (apparently) been initialized but it is not
2969 * in use. It's ok to call abort on such an object, and there's
2970 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002971 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002972 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002973
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002974 /* Sanity check (shouldn't happen: operation->alg should
2975 * always have been initialized to a valid value). */
2976 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2977 return( PSA_ERROR_BAD_STATE );
2978
mohammad1603503973b2018-03-12 15:59:30 +02002979 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002980
Moran Peker41deec42018-04-04 15:43:05 +03002981 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002982 operation->key_set = 0;
2983 operation->iv_set = 0;
2984 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002985 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002986 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002987
Moran Peker395db872018-05-31 14:07:14 +03002988 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002989}
2990
Gilles Peskinea0655c32018-04-30 17:06:50 +02002991
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002992
mohammad16038cc1cee2018-03-28 01:21:33 +03002993/****************************************************************/
2994/* Key Policy */
2995/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002996
mohammad160327010052018-07-03 13:16:15 +03002997#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002998void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002999{
Gilles Peskine803ce742018-06-18 16:07:14 +02003000 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003001}
3002
Gilles Peskine2d277862018-06-18 15:41:12 +02003003void psa_key_policy_set_usage( psa_key_policy_t *policy,
3004 psa_key_usage_t usage,
3005 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003006{
mohammad16034eed7572018-03-28 05:14:59 -07003007 policy->usage = usage;
3008 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003009}
3010
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003011psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003012{
mohammad16036df908f2018-04-02 08:34:15 -07003013 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003014}
3015
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003016psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003017{
mohammad16036df908f2018-04-02 08:34:15 -07003018 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003019}
mohammad160327010052018-07-03 13:16:15 +03003020#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003021
Gilles Peskine2d277862018-06-18 15:41:12 +02003022psa_status_t psa_set_key_policy( psa_key_slot_t key,
3023 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003024{
3025 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003026 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003027
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003028 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003029 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003030
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003031 status = psa_get_empty_key_slot( key, &slot );
3032 if( status != PSA_SUCCESS )
3033 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003034
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003035 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3036 PSA_KEY_USAGE_ENCRYPT |
3037 PSA_KEY_USAGE_DECRYPT |
3038 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003039 PSA_KEY_USAGE_VERIFY |
3040 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003041 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003042
mohammad16036df908f2018-04-02 08:34:15 -07003043 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003044
3045 return( PSA_SUCCESS );
3046}
3047
Gilles Peskine2d277862018-06-18 15:41:12 +02003048psa_status_t psa_get_key_policy( psa_key_slot_t key,
3049 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003050{
3051 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003052 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003053
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003054 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003055 return( PSA_ERROR_INVALID_ARGUMENT );
3056
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003057 status = psa_get_key_slot( key, &slot );
3058 if( status != PSA_SUCCESS )
3059 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003060
mohammad16036df908f2018-04-02 08:34:15 -07003061 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003062
3063 return( PSA_SUCCESS );
3064}
Gilles Peskine20035e32018-02-03 22:44:14 +01003065
Gilles Peskinea0655c32018-04-30 17:06:50 +02003066
3067
mohammad1603804cd712018-03-20 22:44:08 +02003068/****************************************************************/
3069/* Key Lifetime */
3070/****************************************************************/
3071
Gilles Peskine2d277862018-06-18 15:41:12 +02003072psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
3073 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003074{
3075 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003076 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003077
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003078 status = psa_get_key_slot( key, &slot );
3079 if( status != PSA_SUCCESS )
3080 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003081
mohammad1603804cd712018-03-20 22:44:08 +02003082 *lifetime = slot->lifetime;
3083
3084 return( PSA_SUCCESS );
3085}
3086
Gilles Peskine2d277862018-06-18 15:41:12 +02003087psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01003088 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003089{
3090 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003091 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003092
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003093 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
3094 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
Darryl Greend49a4992018-06-18 17:27:26 +01003095 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003096 return( PSA_ERROR_INVALID_ARGUMENT );
3097
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003098 status = psa_get_empty_key_slot( key, &slot );
3099 if( status != PSA_SUCCESS )
3100 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02003101
Darryl Greend49a4992018-06-18 17:27:26 +01003102 if( lifetime == PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003103 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003104
Darryl Greend49a4992018-06-18 17:27:26 +01003105#if !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
3106 if( lifetime == PSA_KEY_LIFETIME_PERSISTENT )
3107 return( PSA_ERROR_NOT_SUPPORTED );
3108#endif
3109
mohammad1603060ad8a2018-03-20 14:28:38 -07003110 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02003111
3112 return( PSA_SUCCESS );
3113}
3114
Gilles Peskine20035e32018-02-03 22:44:14 +01003115
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003116
mohammad16035955c982018-04-26 00:53:03 +03003117/****************************************************************/
3118/* AEAD */
3119/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003120
Gilles Peskineedf9a652018-08-17 18:11:56 +02003121typedef struct
3122{
3123 key_slot_t *slot;
3124 const mbedtls_cipher_info_t *cipher_info;
3125 union
3126 {
3127#if defined(MBEDTLS_CCM_C)
3128 mbedtls_ccm_context ccm;
3129#endif /* MBEDTLS_CCM_C */
3130#if defined(MBEDTLS_GCM_C)
3131 mbedtls_gcm_context gcm;
3132#endif /* MBEDTLS_GCM_C */
3133 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003134 psa_algorithm_t core_alg;
3135 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003136 uint8_t tag_length;
3137} aead_operation_t;
3138
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003139static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003140{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003141 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003142 {
3143#if defined(MBEDTLS_CCM_C)
3144 case PSA_ALG_CCM:
3145 mbedtls_ccm_free( &operation->ctx.ccm );
3146 break;
3147#endif /* MBEDTLS_CCM_C */
3148#if defined(MBEDTLS_CCM_C)
3149 case PSA_ALG_GCM:
3150 mbedtls_gcm_free( &operation->ctx.gcm );
3151 break;
3152#endif /* MBEDTLS_GCM_C */
3153 }
3154}
3155
3156static psa_status_t psa_aead_setup( aead_operation_t *operation,
3157 psa_key_slot_t key,
3158 psa_key_usage_t usage,
3159 psa_algorithm_t alg )
3160{
3161 psa_status_t status;
3162 size_t key_bits;
3163 mbedtls_cipher_id_t cipher_id;
3164
3165 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
3166 if( status != PSA_SUCCESS )
3167 return( status );
3168
3169 key_bits = psa_get_key_bits( operation->slot );
3170
3171 operation->cipher_info =
3172 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3173 &cipher_id );
3174 if( operation->cipher_info == NULL )
3175 return( PSA_ERROR_NOT_SUPPORTED );
3176
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003177 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003178 {
3179#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003180 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3181 operation->core_alg = PSA_ALG_CCM;
3182 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003183 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3184 return( PSA_ERROR_INVALID_ARGUMENT );
3185 mbedtls_ccm_init( &operation->ctx.ccm );
3186 status = mbedtls_to_psa_error(
3187 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3188 operation->slot->data.raw.data,
3189 (unsigned int) key_bits ) );
3190 if( status != 0 )
3191 goto cleanup;
3192 break;
3193#endif /* MBEDTLS_CCM_C */
3194
3195#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003196 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3197 operation->core_alg = PSA_ALG_GCM;
3198 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003199 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3200 return( PSA_ERROR_INVALID_ARGUMENT );
3201 mbedtls_gcm_init( &operation->ctx.gcm );
3202 status = mbedtls_to_psa_error(
3203 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3204 operation->slot->data.raw.data,
3205 (unsigned int) key_bits ) );
3206 break;
3207#endif /* MBEDTLS_GCM_C */
3208
3209 default:
3210 return( PSA_ERROR_NOT_SUPPORTED );
3211 }
3212
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003213 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3214 {
3215 status = PSA_ERROR_INVALID_ARGUMENT;
3216 goto cleanup;
3217 }
3218 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3219 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3220 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3221 * In both cases, mbedtls_xxx will validate the tag length below. */
3222
Gilles Peskineedf9a652018-08-17 18:11:56 +02003223 return( PSA_SUCCESS );
3224
3225cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003226 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003227 return( status );
3228}
3229
mohammad16035955c982018-04-26 00:53:03 +03003230psa_status_t psa_aead_encrypt( psa_key_slot_t key,
3231 psa_algorithm_t alg,
3232 const uint8_t *nonce,
3233 size_t nonce_length,
3234 const uint8_t *additional_data,
3235 size_t additional_data_length,
3236 const uint8_t *plaintext,
3237 size_t plaintext_length,
3238 uint8_t *ciphertext,
3239 size_t ciphertext_size,
3240 size_t *ciphertext_length )
3241{
mohammad16035955c982018-04-26 00:53:03 +03003242 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003243 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003244 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003245
mohammad1603f08a5502018-06-03 15:05:47 +03003246 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003247
Gilles Peskineedf9a652018-08-17 18:11:56 +02003248 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003249 if( status != PSA_SUCCESS )
3250 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003251
Gilles Peskineedf9a652018-08-17 18:11:56 +02003252 /* For all currently supported modes, the tag is at the end of the
3253 * ciphertext. */
3254 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3255 {
3256 status = PSA_ERROR_BUFFER_TOO_SMALL;
3257 goto exit;
3258 }
3259 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003260
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003261 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003262 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003263 status = mbedtls_to_psa_error(
3264 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3265 MBEDTLS_GCM_ENCRYPT,
3266 plaintext_length,
3267 nonce, nonce_length,
3268 additional_data, additional_data_length,
3269 plaintext, ciphertext,
3270 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003271 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003272 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003273 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003274 status = mbedtls_to_psa_error(
3275 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3276 plaintext_length,
3277 nonce, nonce_length,
3278 additional_data,
3279 additional_data_length,
3280 plaintext, ciphertext,
3281 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003282 }
mohammad16035c8845f2018-05-09 05:40:09 -07003283 else
3284 {
mohammad1603554faad2018-06-03 15:07:38 +03003285 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003286 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003287
Gilles Peskineedf9a652018-08-17 18:11:56 +02003288 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3289 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003290
Gilles Peskineedf9a652018-08-17 18:11:56 +02003291exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003292 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003293 if( status == PSA_SUCCESS )
3294 *ciphertext_length = plaintext_length + operation.tag_length;
3295 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003296}
3297
Gilles Peskineee652a32018-06-01 19:23:52 +02003298/* Locate the tag in a ciphertext buffer containing the encrypted data
3299 * followed by the tag. Return the length of the part preceding the tag in
3300 * *plaintext_length. This is the size of the plaintext in modes where
3301 * the encrypted data has the same size as the plaintext, such as
3302 * CCM and GCM. */
3303static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3304 const uint8_t *ciphertext,
3305 size_t ciphertext_length,
3306 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003307 const uint8_t **p_tag )
3308{
3309 size_t payload_length;
3310 if( tag_length > ciphertext_length )
3311 return( PSA_ERROR_INVALID_ARGUMENT );
3312 payload_length = ciphertext_length - tag_length;
3313 if( payload_length > plaintext_size )
3314 return( PSA_ERROR_BUFFER_TOO_SMALL );
3315 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003316 return( PSA_SUCCESS );
3317}
3318
mohammad16035955c982018-04-26 00:53:03 +03003319psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3320 psa_algorithm_t alg,
3321 const uint8_t *nonce,
3322 size_t nonce_length,
3323 const uint8_t *additional_data,
3324 size_t additional_data_length,
3325 const uint8_t *ciphertext,
3326 size_t ciphertext_length,
3327 uint8_t *plaintext,
3328 size_t plaintext_size,
3329 size_t *plaintext_length )
3330{
mohammad16035955c982018-04-26 00:53:03 +03003331 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003332 aead_operation_t operation;
3333 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003334
Gilles Peskineee652a32018-06-01 19:23:52 +02003335 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003336
Gilles Peskineedf9a652018-08-17 18:11:56 +02003337 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003338 if( status != PSA_SUCCESS )
3339 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003340
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003341 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003342 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003343 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003344 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003345 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003346 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003347 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003348
Gilles Peskineedf9a652018-08-17 18:11:56 +02003349 status = mbedtls_to_psa_error(
3350 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3351 ciphertext_length - operation.tag_length,
3352 nonce, nonce_length,
3353 additional_data,
3354 additional_data_length,
3355 tag, operation.tag_length,
3356 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003357 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003358 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003359 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003360 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003361 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003362 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003363 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003364 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003365
Gilles Peskineedf9a652018-08-17 18:11:56 +02003366 status = mbedtls_to_psa_error(
3367 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3368 ciphertext_length - operation.tag_length,
3369 nonce, nonce_length,
3370 additional_data,
3371 additional_data_length,
3372 ciphertext, plaintext,
3373 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003374 }
mohammad160339574652018-06-01 04:39:53 -07003375 else
3376 {
mohammad1603554faad2018-06-03 15:07:38 +03003377 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003378 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003379
Gilles Peskineedf9a652018-08-17 18:11:56 +02003380 if( status != PSA_SUCCESS && plaintext_size != 0 )
3381 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003382
Gilles Peskineedf9a652018-08-17 18:11:56 +02003383exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003384 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003385 if( status == PSA_SUCCESS )
3386 *plaintext_length = ciphertext_length - operation.tag_length;
3387 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003388}
3389
Gilles Peskinea0655c32018-04-30 17:06:50 +02003390
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003391
Gilles Peskine20035e32018-02-03 22:44:14 +01003392/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003393/* Generators */
3394/****************************************************************/
3395
3396psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3397{
3398 psa_status_t status = PSA_SUCCESS;
3399 if( generator->alg == 0 )
3400 {
3401 /* The object has (apparently) been initialized but it is not
3402 * in use. It's ok to call abort on such an object, and there's
3403 * nothing to do. */
3404 }
3405 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003406 if( generator->alg == PSA_ALG_SELECT_RAW )
3407 {
3408 if( generator->ctx.buffer.data != NULL )
3409 {
3410 mbedtls_zeroize( generator->ctx.buffer.data,
3411 generator->ctx.buffer.size );
3412 mbedtls_free( generator->ctx.buffer.data );
3413 }
3414 }
3415 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003416#if defined(MBEDTLS_MD_C)
3417 if( PSA_ALG_IS_HKDF( generator->alg ) )
3418 {
3419 mbedtls_free( generator->ctx.hkdf.info );
3420 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3421 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003422 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3423 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3424 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003425 {
3426 if( generator->ctx.tls12_prf.key != NULL )
3427 {
3428 mbedtls_zeroize( generator->ctx.tls12_prf.key,
3429 generator->ctx.tls12_prf.key_len );
3430 mbedtls_free( generator->ctx.tls12_prf.key );
3431 }
Hanno Becker580fba12018-11-13 20:50:45 +00003432
3433 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3434 {
3435 mbedtls_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
3436 generator->ctx.tls12_prf.Ai_with_seed_len );
3437 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3438 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003439 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003440 else
3441#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003442 {
3443 status = PSA_ERROR_BAD_STATE;
3444 }
3445 memset( generator, 0, sizeof( *generator ) );
3446 return( status );
3447}
3448
3449
3450psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3451 size_t *capacity)
3452{
3453 *capacity = generator->capacity;
3454 return( PSA_SUCCESS );
3455}
3456
Gilles Peskinebef7f142018-07-12 17:22:21 +02003457#if defined(MBEDTLS_MD_C)
3458/* Read some bytes from an HKDF-based generator. This performs a chunk
3459 * of the expand phase of the HKDF algorithm. */
3460static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3461 psa_algorithm_t hash_alg,
3462 uint8_t *output,
3463 size_t output_length )
3464{
3465 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3466 psa_status_t status;
3467
3468 while( output_length != 0 )
3469 {
3470 /* Copy what remains of the current block */
3471 uint8_t n = hash_length - hkdf->offset_in_block;
3472 if( n > output_length )
3473 n = (uint8_t) output_length;
3474 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3475 output += n;
3476 output_length -= n;
3477 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003478 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003479 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003480 /* We can't be wanting more output after block 0xff, otherwise
3481 * the capacity check in psa_generator_read() would have
3482 * prevented this call. It could happen only if the generator
3483 * object was corrupted or if this function is called directly
3484 * inside the library. */
3485 if( hkdf->block_number == 0xff )
3486 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003487
3488 /* We need a new block */
3489 ++hkdf->block_number;
3490 hkdf->offset_in_block = 0;
3491 status = psa_hmac_setup_internal( &hkdf->hmac,
3492 hkdf->prk, hash_length,
3493 hash_alg );
3494 if( status != PSA_SUCCESS )
3495 return( status );
3496 if( hkdf->block_number != 1 )
3497 {
3498 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3499 hkdf->output_block,
3500 hash_length );
3501 if( status != PSA_SUCCESS )
3502 return( status );
3503 }
3504 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3505 hkdf->info,
3506 hkdf->info_length );
3507 if( status != PSA_SUCCESS )
3508 return( status );
3509 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3510 &hkdf->block_number, 1 );
3511 if( status != PSA_SUCCESS )
3512 return( status );
3513 status = psa_hmac_finish_internal( &hkdf->hmac,
3514 hkdf->output_block,
3515 sizeof( hkdf->output_block ) );
3516 if( status != PSA_SUCCESS )
3517 return( status );
3518 }
3519
3520 return( PSA_SUCCESS );
3521}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003522
3523static psa_status_t psa_generator_tls12_prf_generate_next_block(
3524 psa_tls12_prf_generator_t *tls12_prf,
3525 psa_algorithm_t alg )
3526{
3527 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3528 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3529 psa_hmac_internal_data hmac;
3530 psa_status_t status, cleanup_status;
3531
Hanno Becker3b339e22018-11-13 20:56:14 +00003532 unsigned char *Ai;
3533 size_t Ai_len;
3534
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003535 /* We can't be wanting more output after block 0xff, otherwise
3536 * the capacity check in psa_generator_read() would have
3537 * prevented this call. It could happen only if the generator
3538 * object was corrupted or if this function is called directly
3539 * inside the library. */
3540 if( tls12_prf->block_number == 0xff )
3541 return( PSA_ERROR_BAD_STATE );
3542
3543 /* We need a new block */
3544 ++tls12_prf->block_number;
3545 tls12_prf->offset_in_block = 0;
3546
3547 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3548 *
3549 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3550 *
3551 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3552 * HMAC_hash(secret, A(2) + seed) +
3553 * HMAC_hash(secret, A(3) + seed) + ...
3554 *
3555 * A(0) = seed
3556 * A(i) = HMAC_hash( secret, A(i-1) )
3557 *
3558 * The `psa_tls12_prf_generator` structures saves the block
3559 * `HMAC_hash(secret, A(i) + seed)` from which the output
3560 * is currently extracted as `output_block`, while
3561 * `A(i) + seed` is stored in `Ai_with_seed`.
3562 *
3563 * Generating a new block means recalculating `Ai_with_seed`
3564 * from the A(i)-part of it, and afterwards recalculating
3565 * `output_block`.
3566 *
3567 * A(0) is computed at setup time.
3568 *
3569 */
3570
3571 psa_hmac_init_internal( &hmac );
3572
3573 /* We must distinguish the calculation of A(1) from those
3574 * of A(2) and higher, because A(0)=seed has a different
3575 * length than the other A(i). */
3576 if( tls12_prf->block_number == 1 )
3577 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003578 Ai = tls12_prf->Ai_with_seed + hash_length;
3579 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003580 }
3581 else
3582 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003583 Ai = tls12_prf->Ai_with_seed;
3584 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003585 }
3586
Hanno Becker3b339e22018-11-13 20:56:14 +00003587 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3588 status = psa_hmac_setup_internal( &hmac,
3589 tls12_prf->key,
3590 tls12_prf->key_len,
3591 hash_alg );
3592 if( status != PSA_SUCCESS )
3593 goto cleanup;
3594
3595 status = psa_hash_update( &hmac.hash_ctx,
3596 Ai, Ai_len );
3597 if( status != PSA_SUCCESS )
3598 goto cleanup;
3599
3600 status = psa_hmac_finish_internal( &hmac,
3601 tls12_prf->Ai_with_seed,
3602 hash_length );
3603 if( status != PSA_SUCCESS )
3604 goto cleanup;
3605
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003606 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3607 status = psa_hmac_setup_internal( &hmac,
3608 tls12_prf->key,
3609 tls12_prf->key_len,
3610 hash_alg );
3611 if( status != PSA_SUCCESS )
3612 goto cleanup;
3613
3614 status = psa_hash_update( &hmac.hash_ctx,
3615 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003616 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003617 if( status != PSA_SUCCESS )
3618 goto cleanup;
3619
3620 status = psa_hmac_finish_internal( &hmac,
3621 tls12_prf->output_block,
3622 hash_length );
3623 if( status != PSA_SUCCESS )
3624 goto cleanup;
3625
3626cleanup:
3627
3628 cleanup_status = psa_hmac_abort_internal( &hmac );
3629 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3630 status = cleanup_status;
3631
3632 return( status );
3633}
3634
3635/* Read some bytes from an TLS-1.2-PRF-based generator.
3636 * See Section 5 of RFC 5246. */
3637static psa_status_t psa_generator_tls12_prf_read(
3638 psa_tls12_prf_generator_t *tls12_prf,
3639 psa_algorithm_t alg,
3640 uint8_t *output,
3641 size_t output_length )
3642{
3643 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3644 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3645 psa_status_t status;
3646
3647 while( output_length != 0 )
3648 {
3649 /* Copy what remains of the current block */
3650 uint8_t n = hash_length - tls12_prf->offset_in_block;
3651
3652 /* Check if we have fully processed the current block. */
3653 if( n == 0 )
3654 {
3655 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3656 alg );
3657 if( status != PSA_SUCCESS )
3658 return( status );
3659
3660 continue;
3661 }
3662
3663 if( n > output_length )
3664 n = (uint8_t) output_length;
3665 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3666 n );
3667 output += n;
3668 output_length -= n;
3669 tls12_prf->offset_in_block += n;
3670 }
3671
3672 return( PSA_SUCCESS );
3673}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003674#endif /* MBEDTLS_MD_C */
3675
Gilles Peskineeab56e42018-07-12 17:12:33 +02003676psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3677 uint8_t *output,
3678 size_t output_length )
3679{
3680 psa_status_t status;
3681
3682 if( output_length > generator->capacity )
3683 {
3684 generator->capacity = 0;
3685 /* Go through the error path to wipe all confidential data now
3686 * that the generator object is useless. */
3687 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3688 goto exit;
3689 }
3690 if( output_length == 0 &&
3691 generator->capacity == 0 && generator->alg == 0 )
3692 {
3693 /* Edge case: this is a blank or finished generator, and 0
3694 * bytes were requested. The right error in this case could
3695 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3696 * INSUFFICIENT_CAPACITY, which is right for a finished
3697 * generator, for consistency with the case when
3698 * output_length > 0. */
3699 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3700 }
3701 generator->capacity -= output_length;
3702
Gilles Peskine751d9652018-09-18 12:05:44 +02003703 if( generator->alg == PSA_ALG_SELECT_RAW )
3704 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003705 /* Initially, the capacity of a selection generator is always
3706 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3707 * abbreviated in this comment as `size`. When the remaining
3708 * capacity is `c`, the next bytes to serve start `c` bytes
3709 * from the end of the buffer, i.e. `size - c` from the
3710 * beginning of the buffer. Since `generator->capacity` was just
3711 * decremented above, we need to serve the bytes from
3712 * `size - generator->capacity - output_length` to
3713 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003714 size_t offset =
3715 generator->ctx.buffer.size - generator->capacity - output_length;
3716 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3717 status = PSA_SUCCESS;
3718 }
3719 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003720#if defined(MBEDTLS_MD_C)
3721 if( PSA_ALG_IS_HKDF( generator->alg ) )
3722 {
3723 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3724 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3725 output, output_length );
3726 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003727 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3728 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003729 {
3730 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3731 generator->alg, output,
3732 output_length );
3733 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003734 else
3735#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003736 {
3737 return( PSA_ERROR_BAD_STATE );
3738 }
3739
3740exit:
3741 if( status != PSA_SUCCESS )
3742 {
3743 psa_generator_abort( generator );
3744 memset( output, '!', output_length );
3745 }
3746 return( status );
3747}
3748
Gilles Peskine08542d82018-07-19 17:05:42 +02003749#if defined(MBEDTLS_DES_C)
3750static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3751{
3752 if( data_size >= 8 )
3753 mbedtls_des_key_set_parity( data );
3754 if( data_size >= 16 )
3755 mbedtls_des_key_set_parity( data + 8 );
3756 if( data_size >= 24 )
3757 mbedtls_des_key_set_parity( data + 16 );
3758}
3759#endif /* MBEDTLS_DES_C */
3760
Gilles Peskineeab56e42018-07-12 17:12:33 +02003761psa_status_t psa_generator_import_key( psa_key_slot_t key,
3762 psa_key_type_t type,
3763 size_t bits,
3764 psa_crypto_generator_t *generator )
3765{
3766 uint8_t *data = NULL;
3767 size_t bytes = PSA_BITS_TO_BYTES( bits );
3768 psa_status_t status;
3769
3770 if( ! key_type_is_raw_bytes( type ) )
3771 return( PSA_ERROR_INVALID_ARGUMENT );
3772 if( bits % 8 != 0 )
3773 return( PSA_ERROR_INVALID_ARGUMENT );
3774 data = mbedtls_calloc( 1, bytes );
3775 if( data == NULL )
3776 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3777
3778 status = psa_generator_read( generator, data, bytes );
3779 if( status != PSA_SUCCESS )
3780 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003781#if defined(MBEDTLS_DES_C)
3782 if( type == PSA_KEY_TYPE_DES )
3783 psa_des_set_key_parity( data, bytes );
3784#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003785 status = psa_import_key( key, type, data, bytes );
3786
3787exit:
3788 mbedtls_free( data );
3789 return( status );
3790}
3791
3792
3793
3794/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003795/* Key derivation */
3796/****************************************************************/
3797
Gilles Peskinea05219c2018-11-16 16:02:56 +01003798#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003799/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003800 * of the HKDF algorithm.
3801 *
3802 * Note that if this function fails, you must call psa_generator_abort()
3803 * to potentially free embedded data structures and wipe confidential data.
3804 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003805static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003806 const uint8_t *secret,
3807 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003808 psa_algorithm_t hash_alg,
3809 const uint8_t *salt,
3810 size_t salt_length,
3811 const uint8_t *label,
3812 size_t label_length )
3813{
3814 psa_status_t status;
3815 status = psa_hmac_setup_internal( &hkdf->hmac,
3816 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003817 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003818 if( status != PSA_SUCCESS )
3819 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003820 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003821 if( status != PSA_SUCCESS )
3822 return( status );
3823 status = psa_hmac_finish_internal( &hkdf->hmac,
3824 hkdf->prk,
3825 sizeof( hkdf->prk ) );
3826 if( status != PSA_SUCCESS )
3827 return( status );
3828 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3829 hkdf->block_number = 0;
3830 hkdf->info_length = label_length;
3831 if( label_length != 0 )
3832 {
3833 hkdf->info = mbedtls_calloc( 1, label_length );
3834 if( hkdf->info == NULL )
3835 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3836 memcpy( hkdf->info, label, label_length );
3837 }
3838 return( PSA_SUCCESS );
3839}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003840#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003841
Gilles Peskinea05219c2018-11-16 16:02:56 +01003842#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003843/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3844 *
3845 * Note that if this function fails, you must call psa_generator_abort()
3846 * to potentially free embedded data structures and wipe confidential data.
3847 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003848static psa_status_t psa_generator_tls12_prf_setup(
3849 psa_tls12_prf_generator_t *tls12_prf,
3850 const unsigned char *key,
3851 size_t key_len,
3852 psa_algorithm_t hash_alg,
3853 const uint8_t *salt,
3854 size_t salt_length,
3855 const uint8_t *label,
3856 size_t label_length )
3857{
3858 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003859 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3860 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003861
3862 tls12_prf->key = mbedtls_calloc( 1, key_len );
3863 if( tls12_prf->key == NULL )
3864 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3865 tls12_prf->key_len = key_len;
3866 memcpy( tls12_prf->key, key, key_len );
3867
Hanno Becker580fba12018-11-13 20:50:45 +00003868 overflow = ( salt_length + label_length < salt_length ) ||
3869 ( salt_length + label_length + hash_length < hash_length );
3870 if( overflow )
3871 return( PSA_ERROR_INVALID_ARGUMENT );
3872
3873 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3874 if( tls12_prf->Ai_with_seed == NULL )
3875 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3876 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3877
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003878 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3879 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003880 if( label_length != 0 )
3881 {
3882 memcpy( tls12_prf->Ai_with_seed + hash_length,
3883 label, label_length );
3884 }
3885
3886 if( salt_length != 0 )
3887 {
3888 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3889 salt, salt_length );
3890 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003891
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003892 /* The first block gets generated when
3893 * psa_generator_read() is called. */
3894 tls12_prf->block_number = 0;
3895 tls12_prf->offset_in_block = hash_length;
3896
3897 return( PSA_SUCCESS );
3898}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003899
3900/* Set up a TLS-1.2-PSK-to-MS-based generator. */
3901static psa_status_t psa_generator_tls12_psk_to_ms_setup(
3902 psa_tls12_prf_generator_t *tls12_prf,
3903 const unsigned char *psk,
3904 size_t psk_len,
3905 psa_algorithm_t hash_alg,
3906 const uint8_t *salt,
3907 size_t salt_length,
3908 const uint8_t *label,
3909 size_t label_length )
3910{
3911 psa_status_t status;
3912 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
3913
3914 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
3915 return( PSA_ERROR_INVALID_ARGUMENT );
3916
3917 /* Quoting RFC 4279, Section 2:
3918 *
3919 * The premaster secret is formed as follows: if the PSK is N octets
3920 * long, concatenate a uint16 with the value N, N zero octets, a second
3921 * uint16 with the value N, and the PSK itself.
3922 */
3923
3924 pms[0] = ( psk_len >> 8 ) & 0xff;
3925 pms[1] = ( psk_len >> 0 ) & 0xff;
3926 memset( pms + 2, 0, psk_len );
3927 pms[2 + psk_len + 0] = pms[0];
3928 pms[2 + psk_len + 1] = pms[1];
3929 memcpy( pms + 4 + psk_len, psk, psk_len );
3930
3931 status = psa_generator_tls12_prf_setup( tls12_prf,
3932 pms, 4 + 2 * psk_len,
3933 hash_alg,
3934 salt, salt_length,
3935 label, label_length );
3936
3937 mbedtls_zeroize( pms, sizeof( pms ) );
3938 return( status );
3939}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003940#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003941
Gilles Peskine346797d2018-11-16 16:05:06 +01003942/* Note that if this function fails, you must call psa_generator_abort()
3943 * to potentially free embedded data structures and wipe confidential data.
3944 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003945static psa_status_t psa_key_derivation_internal(
3946 psa_crypto_generator_t *generator,
3947 const uint8_t *secret, size_t secret_length,
3948 psa_algorithm_t alg,
3949 const uint8_t *salt, size_t salt_length,
3950 const uint8_t *label, size_t label_length,
3951 size_t capacity )
3952{
3953 psa_status_t status;
3954 size_t max_capacity;
3955
3956 /* Set generator->alg even on failure so that abort knows what to do. */
3957 generator->alg = alg;
3958
Gilles Peskine751d9652018-09-18 12:05:44 +02003959 if( alg == PSA_ALG_SELECT_RAW )
3960 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003961 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02003962 if( salt_length != 0 )
3963 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01003964 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02003965 if( label_length != 0 )
3966 return( PSA_ERROR_INVALID_ARGUMENT );
3967 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
3968 if( generator->ctx.buffer.data == NULL )
3969 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3970 memcpy( generator->ctx.buffer.data, secret, secret_length );
3971 generator->ctx.buffer.size = secret_length;
3972 max_capacity = secret_length;
3973 status = PSA_SUCCESS;
3974 }
3975 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003976#if defined(MBEDTLS_MD_C)
3977 if( PSA_ALG_IS_HKDF( alg ) )
3978 {
3979 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3980 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3981 if( hash_size == 0 )
3982 return( PSA_ERROR_NOT_SUPPORTED );
3983 max_capacity = 255 * hash_size;
3984 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3985 secret, secret_length,
3986 hash_alg,
3987 salt, salt_length,
3988 label, label_length );
3989 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003990 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
3991 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
3992 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003993 {
3994 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3995 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3996
3997 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
3998 if( hash_alg != PSA_ALG_SHA_256 &&
3999 hash_alg != PSA_ALG_SHA_384 )
4000 {
4001 return( PSA_ERROR_NOT_SUPPORTED );
4002 }
4003
4004 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004005
4006 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4007 {
4008 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4009 secret, secret_length,
4010 hash_alg, salt, salt_length,
4011 label, label_length );
4012 }
4013 else
4014 {
4015 status = psa_generator_tls12_psk_to_ms_setup(
4016 &generator->ctx.tls12_prf,
4017 secret, secret_length,
4018 hash_alg, salt, salt_length,
4019 label, label_length );
4020 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004021 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004022 else
4023#endif
4024 {
4025 return( PSA_ERROR_NOT_SUPPORTED );
4026 }
4027
4028 if( status != PSA_SUCCESS )
4029 return( status );
4030
4031 if( capacity <= max_capacity )
4032 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004033 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4034 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004035 else
4036 return( PSA_ERROR_INVALID_ARGUMENT );
4037
4038 return( PSA_SUCCESS );
4039}
4040
Gilles Peskineea0fb492018-07-12 17:17:20 +02004041psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01004042 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004043 psa_algorithm_t alg,
4044 const uint8_t *salt,
4045 size_t salt_length,
4046 const uint8_t *label,
4047 size_t label_length,
4048 size_t capacity )
4049{
4050 key_slot_t *slot;
4051 psa_status_t status;
4052
4053 if( generator->alg != 0 )
4054 return( PSA_ERROR_BAD_STATE );
4055
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004056 /* Make sure that alg is a key derivation algorithm. This prevents
4057 * key selection algorithms, which psa_key_derivation_internal
4058 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004059 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4060 return( PSA_ERROR_INVALID_ARGUMENT );
4061
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004062 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
4063 if( status != PSA_SUCCESS )
4064 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004065
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004066 if( slot->type != PSA_KEY_TYPE_DERIVE )
4067 return( PSA_ERROR_INVALID_ARGUMENT );
4068
4069 status = psa_key_derivation_internal( generator,
4070 slot->data.raw.data,
4071 slot->data.raw.bytes,
4072 alg,
4073 salt, salt_length,
4074 label, label_length,
4075 capacity );
4076 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004077 psa_generator_abort( generator );
4078 return( status );
4079}
4080
4081
4082
4083/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004084/* Key agreement */
4085/****************************************************************/
4086
Gilles Peskinea05219c2018-11-16 16:02:56 +01004087#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004088static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4089 size_t peer_key_length,
4090 const mbedtls_ecp_keypair *our_key,
4091 uint8_t *shared_secret,
4092 size_t shared_secret_size,
4093 size_t *shared_secret_length )
4094{
4095 mbedtls_pk_context pk;
4096 mbedtls_ecp_keypair *their_key = NULL;
4097 mbedtls_ecdh_context ecdh;
4098 int ret;
4099 mbedtls_ecdh_init( &ecdh );
4100 mbedtls_pk_init( &pk );
4101
4102 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
4103 if( ret != 0 )
4104 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02004105 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004106 {
Gilles Peskine88714d72018-10-25 23:07:25 +02004107 case MBEDTLS_PK_ECKEY:
4108 case MBEDTLS_PK_ECKEY_DH:
4109 break;
4110 default:
4111 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4112 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004113 }
4114 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01004115 if( their_key->grp.id != our_key->grp.id )
4116 {
4117 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4118 goto exit;
4119 }
4120
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004121 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
4122 if( ret != 0 )
4123 goto exit;
4124 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
4125 if( ret != 0 )
4126 goto exit;
4127
4128 ret = mbedtls_ecdh_calc_secret( &ecdh,
4129 shared_secret_length,
4130 shared_secret, shared_secret_size,
4131 mbedtls_ctr_drbg_random,
4132 &global_data.ctr_drbg );
4133
4134exit:
4135 mbedtls_pk_free( &pk );
4136 mbedtls_ecdh_free( &ecdh );
4137 return( mbedtls_to_psa_error( ret ) );
4138}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004139#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004140
Gilles Peskine01d718c2018-09-18 12:01:02 +02004141#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4142
Gilles Peskine346797d2018-11-16 16:05:06 +01004143/* Note that if this function fails, you must call psa_generator_abort()
4144 * to potentially free embedded data structures and wipe confidential data.
4145 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004146static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
4147 key_slot_t *private_key,
4148 const uint8_t *peer_key,
4149 size_t peer_key_length,
4150 psa_algorithm_t alg )
4151{
4152 psa_status_t status;
4153 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4154 size_t shared_secret_length = 0;
4155
4156 /* Step 1: run the secret agreement algorithm to generate the shared
4157 * secret. */
4158 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4159 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004160#if defined(MBEDTLS_ECDH_C)
4161 case PSA_ALG_ECDH_BASE:
4162 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4163 return( PSA_ERROR_INVALID_ARGUMENT );
4164 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4165 private_key->data.ecp,
4166 shared_secret,
4167 sizeof( shared_secret ),
4168 &shared_secret_length );
4169 break;
4170#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004171 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004172 (void) private_key;
4173 (void) peer_key;
4174 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004175 return( PSA_ERROR_NOT_SUPPORTED );
4176 }
4177 if( status != PSA_SUCCESS )
4178 goto exit;
4179
4180 /* Step 2: set up the key derivation to generate key material from
4181 * the shared secret. */
4182 status = psa_key_derivation_internal( generator,
4183 shared_secret, shared_secret_length,
4184 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4185 NULL, 0, NULL, 0,
4186 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4187exit:
4188 mbedtls_zeroize( shared_secret, shared_secret_length );
4189 return( status );
4190}
4191
4192psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
4193 psa_key_slot_t private_key,
4194 const uint8_t *peer_key,
4195 size_t peer_key_length,
4196 psa_algorithm_t alg )
4197{
4198 key_slot_t *slot;
4199 psa_status_t status;
4200 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4201 return( PSA_ERROR_INVALID_ARGUMENT );
4202 status = psa_get_key_from_slot( private_key, &slot,
4203 PSA_KEY_USAGE_DERIVE, alg );
4204 if( status != PSA_SUCCESS )
4205 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004206 status = psa_key_agreement_internal( generator,
4207 slot,
4208 peer_key, peer_key_length,
4209 alg );
4210 if( status != PSA_SUCCESS )
4211 psa_generator_abort( generator );
4212 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004213}
4214
4215
4216
4217/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004218/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004219/****************************************************************/
4220
4221psa_status_t psa_generate_random( uint8_t *output,
4222 size_t output_size )
4223{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004224 int ret;
4225 GUARD_MODULE_INITIALIZED;
4226
4227 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004228 return( mbedtls_to_psa_error( ret ) );
4229}
4230
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004231#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004232
4233/* Support function for error conversion between psa_its error codes to psa crypto */
4234static psa_status_t its_to_psa_error( psa_its_status_t ret )
4235{
4236 switch( ret )
4237 {
4238 case PSA_ITS_SUCCESS:
4239 return( PSA_SUCCESS );
4240
4241 case PSA_ITS_ERROR_KEY_NOT_FOUND:
4242 return( PSA_ERROR_EMPTY_SLOT );
4243
4244 case PSA_ITS_ERROR_STORAGE_FAILURE:
4245 return( PSA_ERROR_STORAGE_FAILURE );
4246
4247 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4248 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4249
4250 case PSA_ITS_ERROR_INVALID_KEY:
4251 case PSA_PS_ERROR_OFFSET_INVALID:
4252 case PSA_ITS_ERROR_INCORRECT_SIZE:
4253 case PSA_ITS_ERROR_BAD_POINTER:
4254 return( PSA_ERROR_INVALID_ARGUMENT );
4255
4256 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4257 return( PSA_ERROR_NOT_SUPPORTED );
4258
4259 case PSA_ITS_ERROR_WRITE_ONCE:
4260 return( PSA_ERROR_OCCUPIED_SLOT );
4261
4262 default:
4263 return( PSA_ERROR_UNKNOWN_ERROR );
4264 }
4265}
4266
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004267psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4268 size_t seed_size )
4269{
4270 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004271 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004272 struct psa_its_info_t p_info;
4273 if( global_data.initialized )
4274 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004275
4276 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4277 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4278 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4279 return( PSA_ERROR_INVALID_ARGUMENT );
4280
avolinski0d2c2662018-11-21 17:31:07 +02004281 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004282 status = its_to_psa_error( its_status );
4283
4284 if( PSA_ITS_ERROR_KEY_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004285 {
avolinski0d2c2662018-11-21 17:31:07 +02004286 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004287 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004288 }
avolinski13beb102018-11-20 16:51:49 +02004289 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004290 {
4291 /* You should not be here. Seed needs to be injected only once */
4292 status = PSA_ERROR_NOT_PERMITTED;
4293 }
4294 return( status );
4295}
4296#endif
4297
Gilles Peskine05d69892018-06-19 22:00:52 +02004298psa_status_t psa_generate_key( psa_key_slot_t key,
4299 psa_key_type_t type,
4300 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004301 const void *extra,
4302 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004303{
Gilles Peskine12313cd2018-06-20 00:20:32 +02004304 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004305 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004306
Gilles Peskine53d991e2018-07-12 01:14:59 +02004307 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004308 return( PSA_ERROR_INVALID_ARGUMENT );
4309
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004310 status = psa_get_empty_key_slot( key, &slot );
4311 if( status != PSA_SUCCESS )
4312 return( status );
4313
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004314 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004315 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004316 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004317 if( status != PSA_SUCCESS )
4318 return( status );
4319 status = psa_generate_random( slot->data.raw.data,
4320 slot->data.raw.bytes );
4321 if( status != PSA_SUCCESS )
4322 {
4323 mbedtls_free( slot->data.raw.data );
4324 return( status );
4325 }
4326#if defined(MBEDTLS_DES_C)
4327 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004328 psa_des_set_key_parity( slot->data.raw.data,
4329 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004330#endif /* MBEDTLS_DES_C */
4331 }
4332 else
4333
4334#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4335 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4336 {
4337 mbedtls_rsa_context *rsa;
4338 int ret;
4339 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004340 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4341 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004342 /* Accept only byte-aligned keys, for the same reasons as
4343 * in psa_import_rsa_key(). */
4344 if( bits % 8 != 0 )
4345 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004346 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004347 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004348 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004349 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004350 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004351#if INT_MAX < 0xffffffff
4352 /* Check that the uint32_t value passed by the caller fits
4353 * in the range supported by this implementation. */
4354 if( p->e > INT_MAX )
4355 return( PSA_ERROR_NOT_SUPPORTED );
4356#endif
4357 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004358 }
4359 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4360 if( rsa == NULL )
4361 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4362 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4363 ret = mbedtls_rsa_gen_key( rsa,
4364 mbedtls_ctr_drbg_random,
4365 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004366 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004367 exponent );
4368 if( ret != 0 )
4369 {
4370 mbedtls_rsa_free( rsa );
4371 mbedtls_free( rsa );
4372 return( mbedtls_to_psa_error( ret ) );
4373 }
4374 slot->data.rsa = rsa;
4375 }
4376 else
4377#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4378
4379#if defined(MBEDTLS_ECP_C)
4380 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4381 {
4382 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4383 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4384 const mbedtls_ecp_curve_info *curve_info =
4385 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4386 mbedtls_ecp_keypair *ecp;
4387 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004388 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004389 return( PSA_ERROR_NOT_SUPPORTED );
4390 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4391 return( PSA_ERROR_NOT_SUPPORTED );
4392 if( curve_info->bit_size != bits )
4393 return( PSA_ERROR_INVALID_ARGUMENT );
4394 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4395 if( ecp == NULL )
4396 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4397 mbedtls_ecp_keypair_init( ecp );
4398 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4399 mbedtls_ctr_drbg_random,
4400 &global_data.ctr_drbg );
4401 if( ret != 0 )
4402 {
4403 mbedtls_ecp_keypair_free( ecp );
4404 mbedtls_free( ecp );
4405 return( mbedtls_to_psa_error( ret ) );
4406 }
4407 slot->data.ecp = ecp;
4408 }
4409 else
4410#endif /* MBEDTLS_ECP_C */
4411
4412 return( PSA_ERROR_NOT_SUPPORTED );
4413
4414 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004415
4416#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4417 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4418 {
4419 return( psa_save_generated_persistent_key( key, slot, bits ) );
4420 }
4421#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4422
4423 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004424}
4425
4426
4427/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004428/* Module setup */
4429/****************************************************************/
4430
Gilles Peskinee59236f2018-01-27 23:32:46 +01004431void mbedtls_psa_crypto_free( void )
4432{
Jaeden Amero045bd502018-06-26 14:00:08 +01004433 psa_key_slot_t key;
Darryl Green40225ba2018-11-15 14:48:15 +00004434 key_slot_t *slot;
4435 psa_status_t status;
4436
Gilles Peskine9a056342018-08-01 15:46:54 +02004437 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Darryl Green40225ba2018-11-15 14:48:15 +00004438 {
4439 status = psa_get_key_slot( key, &slot );
4440 if( status != PSA_SUCCESS )
4441 continue;
4442 psa_remove_key_data_from_memory( slot );
4443 /* Zeroize the slot to wipe metadata such as policies. */
4444 mbedtls_zeroize( slot, sizeof( *slot ) );
4445 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01004446 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
4447 mbedtls_entropy_free( &global_data.entropy );
4448 mbedtls_zeroize( &global_data, sizeof( global_data ) );
4449}
4450
4451psa_status_t psa_crypto_init( void )
4452{
4453 int ret;
4454 const unsigned char drbg_seed[] = "PSA";
4455
4456 if( global_data.initialized != 0 )
4457 return( PSA_SUCCESS );
4458
4459 mbedtls_zeroize( &global_data, sizeof( global_data ) );
4460 mbedtls_entropy_init( &global_data.entropy );
4461 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
4462
4463 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4464 mbedtls_entropy_func,
4465 &global_data.entropy,
4466 drbg_seed, sizeof( drbg_seed ) - 1 );
4467 if( ret != 0 )
4468 goto exit;
4469
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004470 global_data.initialized = 1;
4471
Gilles Peskinee59236f2018-01-27 23:32:46 +01004472exit:
4473 if( ret != 0 )
4474 mbedtls_psa_crypto_free( );
4475 return( mbedtls_to_psa_error( ret ) );
4476}
4477
4478#endif /* MBEDTLS_PSA_CRYPTO_C */