blob: 4c0ac1213a1d99cff3391f90106868393e3c19b6 [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 Peskinec6b69072018-11-20 21:42:52 +0100149enum rng_state
150{
151 RNG_NOT_INITIALIZED = 0,
152 RNG_INITIALIZED,
153 RNG_SEEDED,
154};
155
Gilles Peskine2d277862018-06-18 15:41:12 +0200156typedef struct
157{
Gilles Peskinee59236f2018-01-27 23:32:46 +0100158 mbedtls_entropy_context entropy;
159 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200160 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinec6b69072018-11-20 21:42:52 +0100161 unsigned initialized : 1;
162 enum rng_state rng_state : 2;
163 unsigned key_slots_initialized : 1;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100164} psa_global_data_t;
165
166static psa_global_data_t global_data;
167
itayzafrir0adf0fc2018-09-06 16:24:41 +0300168#define GUARD_MODULE_INITIALIZED \
169 if( global_data.initialized == 0 ) \
170 return( PSA_ERROR_BAD_STATE );
171
Gilles Peskinee59236f2018-01-27 23:32:46 +0100172static psa_status_t mbedtls_to_psa_error( int ret )
173{
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 /* If there's both a high-level code and low-level code, dispatch on
175 * the high-level code. */
176 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100177 {
178 case 0:
179 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100180
181 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
182 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
183 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
184 return( PSA_ERROR_NOT_SUPPORTED );
185 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
188 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
189 return( PSA_ERROR_HARDWARE_FAILURE );
190
Gilles Peskine9a944802018-06-21 09:35:35 +0200191 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
192 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
193 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
194 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
195 case MBEDTLS_ERR_ASN1_INVALID_DATA:
196 return( PSA_ERROR_INVALID_ARGUMENT );
197 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
198 return( PSA_ERROR_INSUFFICIENT_MEMORY );
199 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
200 return( PSA_ERROR_BUFFER_TOO_SMALL );
201
Gilles Peskinea5905292018-02-07 20:59:33 +0100202 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
203 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
204 return( PSA_ERROR_NOT_SUPPORTED );
205 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
206 return( PSA_ERROR_HARDWARE_FAILURE );
207
208 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
209 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
210 return( PSA_ERROR_NOT_SUPPORTED );
211 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
214 case MBEDTLS_ERR_CCM_BAD_INPUT:
215 return( PSA_ERROR_INVALID_ARGUMENT );
216 case MBEDTLS_ERR_CCM_AUTH_FAILED:
217 return( PSA_ERROR_INVALID_SIGNATURE );
218 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
219 return( PSA_ERROR_HARDWARE_FAILURE );
220
221 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
222 return( PSA_ERROR_NOT_SUPPORTED );
223 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
224 return( PSA_ERROR_INVALID_ARGUMENT );
225 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
226 return( PSA_ERROR_INSUFFICIENT_MEMORY );
227 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
228 return( PSA_ERROR_INVALID_PADDING );
229 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
230 return( PSA_ERROR_BAD_STATE );
231 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
232 return( PSA_ERROR_INVALID_SIGNATURE );
233 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
234 return( PSA_ERROR_TAMPERING_DETECTED );
235 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
236 return( PSA_ERROR_HARDWARE_FAILURE );
237
238 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
239 return( PSA_ERROR_HARDWARE_FAILURE );
240
241 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
242 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
243 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
244 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
245 return( PSA_ERROR_NOT_SUPPORTED );
246 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
247 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
248
249 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
250 return( PSA_ERROR_NOT_SUPPORTED );
251 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
Gilles Peskinee59236f2018-01-27 23:32:46 +0100254 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
255 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
256 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
257 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100258
259 case MBEDTLS_ERR_GCM_AUTH_FAILED:
260 return( PSA_ERROR_INVALID_SIGNATURE );
261 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200262 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100263 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
264 return( PSA_ERROR_HARDWARE_FAILURE );
265
266 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
267 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
268 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
269 return( PSA_ERROR_HARDWARE_FAILURE );
270
271 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
272 return( PSA_ERROR_NOT_SUPPORTED );
273 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_MD_ALLOC_FAILED:
276 return( PSA_ERROR_INSUFFICIENT_MEMORY );
277 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
278 return( PSA_ERROR_STORAGE_FAILURE );
279 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
280 return( PSA_ERROR_HARDWARE_FAILURE );
281
Gilles Peskinef76aa772018-10-29 19:24:33 +0100282 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
283 return( PSA_ERROR_STORAGE_FAILURE );
284 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
285 return( PSA_ERROR_INVALID_ARGUMENT );
286 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
289 return( PSA_ERROR_BUFFER_TOO_SMALL );
290 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
293 return( PSA_ERROR_INVALID_ARGUMENT );
294 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
295 return( PSA_ERROR_INVALID_ARGUMENT );
296 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
297 return( PSA_ERROR_INSUFFICIENT_MEMORY );
298
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100299 case MBEDTLS_ERR_PK_ALLOC_FAILED:
300 return( PSA_ERROR_INSUFFICIENT_MEMORY );
301 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
302 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
303 return( PSA_ERROR_INVALID_ARGUMENT );
304 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100305 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100306 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
307 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
310 return( PSA_ERROR_NOT_SUPPORTED );
311 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
312 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
313 return( PSA_ERROR_NOT_PERMITTED );
314 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
315 return( PSA_ERROR_INVALID_ARGUMENT );
316 case MBEDTLS_ERR_PK_INVALID_ALG:
317 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
318 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
319 return( PSA_ERROR_NOT_SUPPORTED );
320 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
321 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100322 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
323 return( PSA_ERROR_HARDWARE_FAILURE );
324
325 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327
328 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
329 return( PSA_ERROR_INVALID_ARGUMENT );
330 case MBEDTLS_ERR_RSA_INVALID_PADDING:
331 return( PSA_ERROR_INVALID_PADDING );
332 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
333 return( PSA_ERROR_HARDWARE_FAILURE );
334 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
335 return( PSA_ERROR_INVALID_ARGUMENT );
336 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
337 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
338 return( PSA_ERROR_TAMPERING_DETECTED );
339 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
340 return( PSA_ERROR_INVALID_SIGNATURE );
341 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
342 return( PSA_ERROR_BUFFER_TOO_SMALL );
343 case MBEDTLS_ERR_RSA_RNG_FAILED:
344 return( PSA_ERROR_INSUFFICIENT_MEMORY );
345 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
346 return( PSA_ERROR_NOT_SUPPORTED );
347 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
348 return( PSA_ERROR_HARDWARE_FAILURE );
349
350 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
351 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
352 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
353 return( PSA_ERROR_HARDWARE_FAILURE );
354
355 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
356 return( PSA_ERROR_INVALID_ARGUMENT );
357 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
358 return( PSA_ERROR_HARDWARE_FAILURE );
359
itayzafrir5c753392018-05-08 11:18:38 +0300360 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300361 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300362 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300363 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
364 return( PSA_ERROR_BUFFER_TOO_SMALL );
365 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
366 return( PSA_ERROR_NOT_SUPPORTED );
367 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
368 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
369 return( PSA_ERROR_INVALID_SIGNATURE );
370 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
371 return( PSA_ERROR_INSUFFICIENT_MEMORY );
372 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
373 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300374
Gilles Peskinee59236f2018-01-27 23:32:46 +0100375 default:
376 return( PSA_ERROR_UNKNOWN_ERROR );
377 }
378}
379
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200380
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200381
382
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100383/****************************************************************/
384/* Key management */
385/****************************************************************/
386
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100387#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200388static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
389{
390 switch( grpid )
391 {
392 case MBEDTLS_ECP_DP_SECP192R1:
393 return( PSA_ECC_CURVE_SECP192R1 );
394 case MBEDTLS_ECP_DP_SECP224R1:
395 return( PSA_ECC_CURVE_SECP224R1 );
396 case MBEDTLS_ECP_DP_SECP256R1:
397 return( PSA_ECC_CURVE_SECP256R1 );
398 case MBEDTLS_ECP_DP_SECP384R1:
399 return( PSA_ECC_CURVE_SECP384R1 );
400 case MBEDTLS_ECP_DP_SECP521R1:
401 return( PSA_ECC_CURVE_SECP521R1 );
402 case MBEDTLS_ECP_DP_BP256R1:
403 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
404 case MBEDTLS_ECP_DP_BP384R1:
405 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
406 case MBEDTLS_ECP_DP_BP512R1:
407 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
408 case MBEDTLS_ECP_DP_CURVE25519:
409 return( PSA_ECC_CURVE_CURVE25519 );
410 case MBEDTLS_ECP_DP_SECP192K1:
411 return( PSA_ECC_CURVE_SECP192K1 );
412 case MBEDTLS_ECP_DP_SECP224K1:
413 return( PSA_ECC_CURVE_SECP224K1 );
414 case MBEDTLS_ECP_DP_SECP256K1:
415 return( PSA_ECC_CURVE_SECP256K1 );
416 case MBEDTLS_ECP_DP_CURVE448:
417 return( PSA_ECC_CURVE_CURVE448 );
418 default:
419 return( 0 );
420 }
421}
422
Gilles Peskine12313cd2018-06-20 00:20:32 +0200423static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
424{
425 switch( curve )
426 {
427 case PSA_ECC_CURVE_SECP192R1:
428 return( MBEDTLS_ECP_DP_SECP192R1 );
429 case PSA_ECC_CURVE_SECP224R1:
430 return( MBEDTLS_ECP_DP_SECP224R1 );
431 case PSA_ECC_CURVE_SECP256R1:
432 return( MBEDTLS_ECP_DP_SECP256R1 );
433 case PSA_ECC_CURVE_SECP384R1:
434 return( MBEDTLS_ECP_DP_SECP384R1 );
435 case PSA_ECC_CURVE_SECP521R1:
436 return( MBEDTLS_ECP_DP_SECP521R1 );
437 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
438 return( MBEDTLS_ECP_DP_BP256R1 );
439 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
440 return( MBEDTLS_ECP_DP_BP384R1 );
441 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
442 return( MBEDTLS_ECP_DP_BP512R1 );
443 case PSA_ECC_CURVE_CURVE25519:
444 return( MBEDTLS_ECP_DP_CURVE25519 );
445 case PSA_ECC_CURVE_SECP192K1:
446 return( MBEDTLS_ECP_DP_SECP192K1 );
447 case PSA_ECC_CURVE_SECP224K1:
448 return( MBEDTLS_ECP_DP_SECP224K1 );
449 case PSA_ECC_CURVE_SECP256K1:
450 return( MBEDTLS_ECP_DP_SECP256K1 );
451 case PSA_ECC_CURVE_CURVE448:
452 return( MBEDTLS_ECP_DP_CURVE448 );
453 default:
454 return( MBEDTLS_ECP_DP_NONE );
455 }
456}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100457#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200458
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200459static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
460 size_t bits,
461 struct raw_data *raw )
462{
463 /* Check that the bit size is acceptable for the key type */
464 switch( type )
465 {
466 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200467 if( bits == 0 )
468 {
469 raw->bytes = 0;
470 raw->data = NULL;
471 return( PSA_SUCCESS );
472 }
473 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200474#if defined(MBEDTLS_MD_C)
475 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200476#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200477 case PSA_KEY_TYPE_DERIVE:
478 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200479#if defined(MBEDTLS_AES_C)
480 case PSA_KEY_TYPE_AES:
481 if( bits != 128 && bits != 192 && bits != 256 )
482 return( PSA_ERROR_INVALID_ARGUMENT );
483 break;
484#endif
485#if defined(MBEDTLS_CAMELLIA_C)
486 case PSA_KEY_TYPE_CAMELLIA:
487 if( bits != 128 && bits != 192 && bits != 256 )
488 return( PSA_ERROR_INVALID_ARGUMENT );
489 break;
490#endif
491#if defined(MBEDTLS_DES_C)
492 case PSA_KEY_TYPE_DES:
493 if( bits != 64 && bits != 128 && bits != 192 )
494 return( PSA_ERROR_INVALID_ARGUMENT );
495 break;
496#endif
497#if defined(MBEDTLS_ARC4_C)
498 case PSA_KEY_TYPE_ARC4:
499 if( bits < 8 || bits > 2048 )
500 return( PSA_ERROR_INVALID_ARGUMENT );
501 break;
502#endif
503 default:
504 return( PSA_ERROR_NOT_SUPPORTED );
505 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200506 if( bits % 8 != 0 )
507 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200508
509 /* Allocate memory for the key */
510 raw->bytes = PSA_BITS_TO_BYTES( bits );
511 raw->data = mbedtls_calloc( 1, raw->bytes );
512 if( raw->data == NULL )
513 {
514 raw->bytes = 0;
515 return( PSA_ERROR_INSUFFICIENT_MEMORY );
516 }
517 return( PSA_SUCCESS );
518}
519
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200520#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100521/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
522 * that are not a multiple of 8) well. For example, there is only
523 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
524 * way to return the exact bit size of a key.
525 * To keep things simple, reject non-byte-aligned key sizes. */
526static psa_status_t psa_check_rsa_key_byte_aligned(
527 const mbedtls_rsa_context *rsa )
528{
529 mbedtls_mpi n;
530 psa_status_t status;
531 mbedtls_mpi_init( &n );
532 status = mbedtls_to_psa_error(
533 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
534 if( status == PSA_SUCCESS )
535 {
536 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
537 status = PSA_ERROR_NOT_SUPPORTED;
538 }
539 mbedtls_mpi_free( &n );
540 return( status );
541}
542
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200543static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
544 mbedtls_rsa_context **p_rsa )
545{
546 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
547 return( PSA_ERROR_INVALID_ARGUMENT );
548 else
549 {
550 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
Gilles Peskineaac64a22018-11-12 18:37:42 +0100551 /* The size of an RSA key doesn't have to be a multiple of 8.
552 * Mbed TLS supports non-byte-aligned key sizes, but not well.
553 * For example, mbedtls_rsa_get_len() returns the key size in
554 * bytes, not in bits. */
555 size_t bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100556 psa_status_t status;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200557 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
558 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100559 status = psa_check_rsa_key_byte_aligned( rsa );
560 if( status != PSA_SUCCESS )
561 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200562 *p_rsa = rsa;
563 return( PSA_SUCCESS );
564 }
565}
566#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
567
568#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100569/* Import an elliptic curve parsed by the mbedtls pk module. */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200570static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
571 mbedtls_pk_context *pk,
572 mbedtls_ecp_keypair **p_ecp )
573{
574 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
575 return( PSA_ERROR_INVALID_ARGUMENT );
576 else
577 {
578 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
579 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
580 if( actual_curve != expected_curve )
581 return( PSA_ERROR_INVALID_ARGUMENT );
582 *p_ecp = ecp;
583 return( PSA_SUCCESS );
584 }
585}
586#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
587
Gilles Peskinef76aa772018-10-29 19:24:33 +0100588#if defined(MBEDTLS_ECP_C)
589/* Import a private key given as a byte string which is the private value
590 * in big-endian order. */
591static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
592 const uint8_t *data,
593 size_t data_length,
594 mbedtls_ecp_keypair **p_ecp )
595{
596 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
597 mbedtls_ecp_keypair *ecp = NULL;
598 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
599
600 *p_ecp = NULL;
601 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
602 if( ecp == NULL )
603 return( PSA_ERROR_INSUFFICIENT_MEMORY );
604
605 /* Load the group. */
606 status = mbedtls_to_psa_error(
607 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
608 if( status != PSA_SUCCESS )
609 goto exit;
610 /* Load the secret value. */
611 status = mbedtls_to_psa_error(
612 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
613 if( status != PSA_SUCCESS )
614 goto exit;
615 /* Validate the private key. */
616 status = mbedtls_to_psa_error(
617 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
618 if( status != PSA_SUCCESS )
619 goto exit;
620 /* Calculate the public key from the private key. */
621 status = mbedtls_to_psa_error(
622 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
623 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
624 if( status != PSA_SUCCESS )
625 goto exit;
626
627 *p_ecp = ecp;
628 return( PSA_SUCCESS );
629
630exit:
631 if( ecp != NULL )
632 {
633 mbedtls_ecp_keypair_free( ecp );
634 mbedtls_free( ecp );
635 }
636 return( status );
637}
638#endif /* defined(MBEDTLS_ECP_C) */
639
Darryl Green940d72c2018-07-13 13:18:51 +0100640static psa_status_t psa_import_key_into_slot( key_slot_t *slot,
641 const uint8_t *data,
642 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100643{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200644 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100645
Darryl Green940d72c2018-07-13 13:18:51 +0100646 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100647 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100648 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100649 if( data_length > SIZE_MAX / 8 )
650 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100651 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200652 PSA_BYTES_TO_BITS( data_length ),
653 &slot->data.raw );
654 if( status != PSA_SUCCESS )
655 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200656 if( data_length != 0 )
657 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100658 }
659 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100660#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100661 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100662 {
Darryl Green940d72c2018-07-13 13:18:51 +0100663 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100664 data, data_length,
665 &slot->data.ecp );
666 if( status != PSA_SUCCESS )
667 return( status );
668 }
669 else
670#endif /* MBEDTLS_ECP_C */
Gilles Peskine969ac722018-01-28 18:16:59 +0100671#if defined(MBEDTLS_PK_PARSE_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100672 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
673 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100674 {
675 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100676 mbedtls_pk_context pk;
677 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200678
679 /* Parse the data. */
Darryl Green940d72c2018-07-13 13:18:51 +0100680 if( PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100681 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
682 else
683 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100684 if( ret != 0 )
685 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200686
687 /* We have something that the pkparse module recognizes.
688 * If it has the expected type and passes any type-specific
689 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100690#if defined(MBEDTLS_RSA_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100691 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200692 status = psa_import_rsa_key( &pk, &slot->data.rsa );
693 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100694#endif /* MBEDTLS_RSA_C */
695#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100696 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
697 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200698 &pk, &slot->data.ecp );
699 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100700#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200701 {
702 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200703 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200704
Gilles Peskinec648d692018-06-28 08:46:13 +0200705 /* Free the content of the pk object only on error. On success,
706 * the content of the object has been stored in the slot. */
707 if( status != PSA_SUCCESS )
708 {
709 mbedtls_pk_free( &pk );
710 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100711 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100712 }
713 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100714#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100715 {
716 return( PSA_ERROR_NOT_SUPPORTED );
717 }
Darryl Green940d72c2018-07-13 13:18:51 +0100718 return( PSA_SUCCESS );
719}
720
Darryl Greend49a4992018-06-18 17:27:26 +0100721#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
722static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t key,
723 key_slot_t *p_slot )
724{
725 psa_status_t status = PSA_SUCCESS;
726 uint8_t *key_data = NULL;
727 size_t key_data_length = 0;
728
729 status = psa_load_persistent_key( key, &( p_slot )->type,
730 &( p_slot )->policy, &key_data,
731 &key_data_length );
732 if( status != PSA_SUCCESS )
733 goto exit;
734 status = psa_import_key_into_slot( p_slot,
735 key_data, key_data_length );
736exit:
737 psa_free_persistent_key_data( key_data, key_data_length );
738 return( status );
739}
740#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
741
Darryl Green06fd18d2018-07-16 11:21:11 +0100742/* Retrieve a key slot, occupied or not. */
743static psa_status_t psa_get_key_slot( psa_key_slot_t key,
744 key_slot_t **p_slot )
745{
746 GUARD_MODULE_INITIALIZED;
747
748 /* 0 is not a valid slot number under any circumstance. This
749 * implementation provides slots number 1 to N where N is the
750 * number of available slots. */
751 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
752 return( PSA_ERROR_INVALID_ARGUMENT );
753
754 *p_slot = &global_data.key_slots[key - 1];
Darryl Greend49a4992018-06-18 17:27:26 +0100755
756#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
757 if( ( *p_slot )->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
758 {
759 /* There are two circumstances this can occur: the key material has
760 * not yet been created, or the key exists in storage but has not yet
761 * been loaded into memory. */
762 if( ( *p_slot )->type == PSA_KEY_TYPE_NONE )
763 {
764 psa_status_t status = PSA_SUCCESS;
765 status = psa_load_persistent_key_into_slot( key, *p_slot );
766 if( status != PSA_ERROR_EMPTY_SLOT )
767 return( status );
768 }
769 }
770#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
771
Darryl Green06fd18d2018-07-16 11:21:11 +0100772 return( PSA_SUCCESS );
773}
774
775/* Retrieve an empty key slot (slot with no key data, but possibly
776 * with some metadata such as a policy). */
777static psa_status_t psa_get_empty_key_slot( psa_key_slot_t key,
778 key_slot_t **p_slot )
779{
780 psa_status_t status;
781 key_slot_t *slot = NULL;
782
783 *p_slot = NULL;
784
785 status = psa_get_key_slot( key, &slot );
786 if( status != PSA_SUCCESS )
787 return( status );
788
789 if( slot->type != PSA_KEY_TYPE_NONE )
790 return( PSA_ERROR_OCCUPIED_SLOT );
791
792 *p_slot = slot;
793 return( status );
794}
795
796/** Retrieve a slot which must contain a key. The key must have allow all the
797 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
798 * operations with this algorithm. */
799static psa_status_t psa_get_key_from_slot( psa_key_slot_t key,
800 key_slot_t **p_slot,
801 psa_key_usage_t usage,
802 psa_algorithm_t alg )
803{
804 psa_status_t status;
805 key_slot_t *slot = NULL;
806
807 *p_slot = NULL;
808
809 status = psa_get_key_slot( key, &slot );
810 if( status != PSA_SUCCESS )
811 return( status );
812 if( slot->type == PSA_KEY_TYPE_NONE )
813 return( PSA_ERROR_EMPTY_SLOT );
814
815 /* Enforce that usage policy for the key slot contains all the flags
816 * required by the usage parameter. There is one exception: public
817 * keys can always be exported, so we treat public key objects as
818 * if they had the export flag. */
819 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
820 usage &= ~PSA_KEY_USAGE_EXPORT;
821 if( ( slot->policy.usage & usage ) != usage )
822 return( PSA_ERROR_NOT_PERMITTED );
823 if( alg != 0 && ( alg != slot->policy.alg ) )
824 return( PSA_ERROR_NOT_PERMITTED );
825
826 *p_slot = slot;
827 return( PSA_SUCCESS );
828}
Darryl Green940d72c2018-07-13 13:18:51 +0100829
Darryl Green40225ba2018-11-15 14:48:15 +0000830static psa_status_t psa_remove_key_data_from_memory( key_slot_t *slot )
831{
832 if( slot->type == PSA_KEY_TYPE_NONE )
833 {
834 /* No key material to clean. */
835 }
836 else if( key_type_is_raw_bytes( slot->type ) )
837 {
838 mbedtls_free( slot->data.raw.data );
839 }
840 else
841#if defined(MBEDTLS_RSA_C)
842 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
843 {
844 mbedtls_rsa_free( slot->data.rsa );
845 mbedtls_free( slot->data.rsa );
846 }
847 else
848#endif /* defined(MBEDTLS_RSA_C) */
849#if defined(MBEDTLS_ECP_C)
850 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
851 {
852 mbedtls_ecp_keypair_free( slot->data.ecp );
853 mbedtls_free( slot->data.ecp );
854 }
855 else
856#endif /* defined(MBEDTLS_ECP_C) */
857 {
858 /* Shouldn't happen: the key type is not any type that we
859 * put in. */
860 return( PSA_ERROR_TAMPERING_DETECTED );
861 }
862
863 return( PSA_SUCCESS );
864}
865
Darryl Green940d72c2018-07-13 13:18:51 +0100866psa_status_t psa_import_key( psa_key_slot_t key,
867 psa_key_type_t type,
868 const uint8_t *data,
869 size_t data_length )
870{
871 key_slot_t *slot;
872 psa_status_t status;
873
874 status = psa_get_empty_key_slot( key, &slot );
875 if( status != PSA_SUCCESS )
876 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100877
878 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100879
880 status = psa_import_key_into_slot( slot, data, data_length );
881 if( status != PSA_SUCCESS )
882 {
883 slot->type = PSA_KEY_TYPE_NONE;
884 return( status );
885 }
886
Darryl Greend49a4992018-06-18 17:27:26 +0100887#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
888 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
889 {
890 /* Store in file location */
891 status = psa_save_persistent_key( key, slot->type, &slot->policy, data,
892 data_length );
893 if( status != PSA_SUCCESS )
894 {
895 (void) psa_remove_key_data_from_memory( slot );
896 slot->type = PSA_KEY_TYPE_NONE;
897 }
898 }
899#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
900
901 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100902}
903
Gilles Peskine2d277862018-06-18 15:41:12 +0200904psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100905{
906 key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100907 psa_status_t status = PSA_SUCCESS;
908 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100909
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200910 status = psa_get_key_slot( key, &slot );
911 if( status != PSA_SUCCESS )
912 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100913#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
914 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
915 {
916 storage_status = psa_destroy_persistent_key( key );
917 }
918#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
919 status = psa_remove_key_data_from_memory( slot );
920 /* Zeroize the slot to wipe metadata such as policies. */
921 mbedtls_zeroize( slot, sizeof( *slot ) );
922 if( status != PSA_SUCCESS )
923 return( status );
924 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100925}
926
Gilles Peskineb870b182018-07-06 16:02:09 +0200927/* Return the size of the key in the given slot, in bits. */
928static size_t psa_get_key_bits( const key_slot_t *slot )
929{
930 if( key_type_is_raw_bytes( slot->type ) )
931 return( slot->data.raw.bytes * 8 );
932#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200933 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100934 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200935#endif /* defined(MBEDTLS_RSA_C) */
936#if defined(MBEDTLS_ECP_C)
937 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
938 return( slot->data.ecp->grp.pbits );
939#endif /* defined(MBEDTLS_ECP_C) */
940 /* Shouldn't happen except on an empty slot. */
941 return( 0 );
942}
943
Gilles Peskine2d277862018-06-18 15:41:12 +0200944psa_status_t psa_get_key_information( psa_key_slot_t key,
945 psa_key_type_t *type,
946 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100947{
948 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200949 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100950
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100951 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200952 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100953 if( bits != NULL )
954 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200955 status = psa_get_key_slot( key, &slot );
956 if( status != PSA_SUCCESS )
957 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200958
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100959 if( slot->type == PSA_KEY_TYPE_NONE )
960 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200961 if( type != NULL )
962 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200963 if( bits != NULL )
964 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100965 return( PSA_SUCCESS );
966}
967
Darryl Greendd8fb772018-11-07 16:00:44 +0000968static psa_status_t psa_internal_export_key( key_slot_t *slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200969 uint8_t *data,
970 size_t data_size,
971 size_t *data_length,
972 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100973{
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100974 *data_length = 0;
975
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200976 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300977 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300978
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200979 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100980 {
981 if( slot->data.raw.bytes > data_size )
982 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +0100983 if( data_size != 0 )
984 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200985 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +0100986 memset( data + slot->data.raw.bytes, 0,
987 data_size - slot->data.raw.bytes );
988 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100989 *data_length = slot->data.raw.bytes;
990 return( PSA_SUCCESS );
991 }
Gilles Peskine188c71e2018-10-29 19:26:02 +0100992#if defined(MBEDTLS_ECP_C)
993 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
994 {
Darryl Greendd8fb772018-11-07 16:00:44 +0000995 psa_status_t status;
996
Gilles Peskine188c71e2018-10-29 19:26:02 +0100997 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
998 if( bytes > data_size )
999 return( PSA_ERROR_BUFFER_TOO_SMALL );
1000 status = mbedtls_to_psa_error(
1001 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1002 if( status != PSA_SUCCESS )
1003 return( status );
1004 memset( data + bytes, 0, data_size - bytes );
1005 *data_length = bytes;
1006 return( PSA_SUCCESS );
1007 }
1008#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001009 else
Moran Peker17e36e12018-05-02 12:55:20 +03001010 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001011#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001012 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001013 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001014 {
Moran Pekera998bc62018-04-16 18:16:20 +03001015 mbedtls_pk_context pk;
1016 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001017 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001018 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001019#if defined(MBEDTLS_RSA_C)
1020 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001021 pk.pk_info = &mbedtls_rsa_info;
1022 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001023#else
1024 return( PSA_ERROR_NOT_SUPPORTED );
1025#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001026 }
1027 else
1028 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001029#if defined(MBEDTLS_ECP_C)
1030 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001031 pk.pk_info = &mbedtls_eckey_info;
1032 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001033#else
1034 return( PSA_ERROR_NOT_SUPPORTED );
1035#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001036 }
Moran Pekerd7326592018-05-29 16:56:39 +03001037 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001038 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +03001039 else
1040 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +03001041 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001042 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001043 /* If data_size is 0 then data may be NULL and then the
1044 * call to memset would have undefined behavior. */
1045 if( data_size != 0 )
1046 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001047 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001048 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001049 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1050 * Move the data to the beginning and erase remaining data
1051 * at the original location. */
1052 if( 2 * (size_t) ret <= data_size )
1053 {
1054 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001055 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001056 }
1057 else if( (size_t) ret < data_size )
1058 {
1059 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001060 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001061 }
Moran Pekera998bc62018-04-16 18:16:20 +03001062 *data_length = ret;
1063 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001064 }
1065 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001066#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001067 {
1068 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001069 it is valid for a special-purpose implementation to omit
1070 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001071 return( PSA_ERROR_NOT_SUPPORTED );
1072 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001073 }
1074}
1075
Gilles Peskine2d277862018-06-18 15:41:12 +02001076psa_status_t psa_export_key( psa_key_slot_t key,
1077 uint8_t *data,
1078 size_t data_size,
1079 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001080{
Darryl Greendd8fb772018-11-07 16:00:44 +00001081 key_slot_t *slot;
1082 psa_status_t status;
1083
1084 /* Set the key to empty now, so that even when there are errors, we always
1085 * set data_length to a value between 0 and data_size. On error, setting
1086 * the key to empty is a good choice because an empty key representation is
1087 * unlikely to be accepted anywhere. */
1088 *data_length = 0;
1089
1090 /* Export requires the EXPORT flag. There is an exception for public keys,
1091 * which don't require any flag, but psa_get_key_from_slot takes
1092 * care of this. */
1093 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_EXPORT, 0 );
1094 if( status != PSA_SUCCESS )
1095 return( status );
1096 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001097 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001098}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001099
Gilles Peskine2d277862018-06-18 15:41:12 +02001100psa_status_t psa_export_public_key( psa_key_slot_t key,
1101 uint8_t *data,
1102 size_t data_size,
1103 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001104{
Darryl Greendd8fb772018-11-07 16:00:44 +00001105 key_slot_t *slot;
1106 psa_status_t status;
1107
1108 /* Set the key to empty now, so that even when there are errors, we always
1109 * set data_length to a value between 0 and data_size. On error, setting
1110 * the key to empty is a good choice because an empty key representation is
1111 * unlikely to be accepted anywhere. */
1112 *data_length = 0;
1113
1114 /* Exporting a public key doesn't require a usage flag. */
1115 status = psa_get_key_from_slot( key, &slot, 0, 0 );
1116 if( status != PSA_SUCCESS )
1117 return( status );
1118 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001119 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001120}
1121
Darryl Green0c6575a2018-11-07 16:05:30 +00001122#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1123static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t key,
1124 key_slot_t *slot,
1125 size_t bits )
1126{
1127 psa_status_t status;
1128 uint8_t *data;
1129 size_t key_length;
1130 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1131 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001132 if( data == NULL )
1133 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001134 /* Get key data in export format */
1135 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1136 if( status != PSA_SUCCESS )
1137 {
1138 slot->type = PSA_KEY_TYPE_NONE;
1139 goto exit;
1140 }
1141 /* Store in file location */
1142 status = psa_save_persistent_key( key, slot->type, &slot->policy,
1143 data, key_length );
1144 if( status != PSA_SUCCESS )
1145 {
1146 slot->type = PSA_KEY_TYPE_NONE;
1147 }
1148exit:
1149 mbedtls_zeroize( data, key_length );
1150 mbedtls_free( data );
1151 return( status );
1152}
1153#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1154
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001155
1156
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001157/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001158/* Message digests */
1159/****************************************************************/
1160
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001161static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001162{
1163 switch( alg )
1164 {
1165#if defined(MBEDTLS_MD2_C)
1166 case PSA_ALG_MD2:
1167 return( &mbedtls_md2_info );
1168#endif
1169#if defined(MBEDTLS_MD4_C)
1170 case PSA_ALG_MD4:
1171 return( &mbedtls_md4_info );
1172#endif
1173#if defined(MBEDTLS_MD5_C)
1174 case PSA_ALG_MD5:
1175 return( &mbedtls_md5_info );
1176#endif
1177#if defined(MBEDTLS_RIPEMD160_C)
1178 case PSA_ALG_RIPEMD160:
1179 return( &mbedtls_ripemd160_info );
1180#endif
1181#if defined(MBEDTLS_SHA1_C)
1182 case PSA_ALG_SHA_1:
1183 return( &mbedtls_sha1_info );
1184#endif
1185#if defined(MBEDTLS_SHA256_C)
1186 case PSA_ALG_SHA_224:
1187 return( &mbedtls_sha224_info );
1188 case PSA_ALG_SHA_256:
1189 return( &mbedtls_sha256_info );
1190#endif
1191#if defined(MBEDTLS_SHA512_C)
1192 case PSA_ALG_SHA_384:
1193 return( &mbedtls_sha384_info );
1194 case PSA_ALG_SHA_512:
1195 return( &mbedtls_sha512_info );
1196#endif
1197 default:
1198 return( NULL );
1199 }
1200}
1201
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001202psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1203{
1204 switch( operation->alg )
1205 {
Gilles Peskine81736312018-06-26 15:04:31 +02001206 case 0:
1207 /* The object has (apparently) been initialized but it is not
1208 * in use. It's ok to call abort on such an object, and there's
1209 * nothing to do. */
1210 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001211#if defined(MBEDTLS_MD2_C)
1212 case PSA_ALG_MD2:
1213 mbedtls_md2_free( &operation->ctx.md2 );
1214 break;
1215#endif
1216#if defined(MBEDTLS_MD4_C)
1217 case PSA_ALG_MD4:
1218 mbedtls_md4_free( &operation->ctx.md4 );
1219 break;
1220#endif
1221#if defined(MBEDTLS_MD5_C)
1222 case PSA_ALG_MD5:
1223 mbedtls_md5_free( &operation->ctx.md5 );
1224 break;
1225#endif
1226#if defined(MBEDTLS_RIPEMD160_C)
1227 case PSA_ALG_RIPEMD160:
1228 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1229 break;
1230#endif
1231#if defined(MBEDTLS_SHA1_C)
1232 case PSA_ALG_SHA_1:
1233 mbedtls_sha1_free( &operation->ctx.sha1 );
1234 break;
1235#endif
1236#if defined(MBEDTLS_SHA256_C)
1237 case PSA_ALG_SHA_224:
1238 case PSA_ALG_SHA_256:
1239 mbedtls_sha256_free( &operation->ctx.sha256 );
1240 break;
1241#endif
1242#if defined(MBEDTLS_SHA512_C)
1243 case PSA_ALG_SHA_384:
1244 case PSA_ALG_SHA_512:
1245 mbedtls_sha512_free( &operation->ctx.sha512 );
1246 break;
1247#endif
1248 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001249 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001250 }
1251 operation->alg = 0;
1252 return( PSA_SUCCESS );
1253}
1254
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001255psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001256 psa_algorithm_t alg )
1257{
1258 int ret;
1259 operation->alg = 0;
1260 switch( alg )
1261 {
1262#if defined(MBEDTLS_MD2_C)
1263 case PSA_ALG_MD2:
1264 mbedtls_md2_init( &operation->ctx.md2 );
1265 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1266 break;
1267#endif
1268#if defined(MBEDTLS_MD4_C)
1269 case PSA_ALG_MD4:
1270 mbedtls_md4_init( &operation->ctx.md4 );
1271 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1272 break;
1273#endif
1274#if defined(MBEDTLS_MD5_C)
1275 case PSA_ALG_MD5:
1276 mbedtls_md5_init( &operation->ctx.md5 );
1277 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1278 break;
1279#endif
1280#if defined(MBEDTLS_RIPEMD160_C)
1281 case PSA_ALG_RIPEMD160:
1282 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1283 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1284 break;
1285#endif
1286#if defined(MBEDTLS_SHA1_C)
1287 case PSA_ALG_SHA_1:
1288 mbedtls_sha1_init( &operation->ctx.sha1 );
1289 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1290 break;
1291#endif
1292#if defined(MBEDTLS_SHA256_C)
1293 case PSA_ALG_SHA_224:
1294 mbedtls_sha256_init( &operation->ctx.sha256 );
1295 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1296 break;
1297 case PSA_ALG_SHA_256:
1298 mbedtls_sha256_init( &operation->ctx.sha256 );
1299 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1300 break;
1301#endif
1302#if defined(MBEDTLS_SHA512_C)
1303 case PSA_ALG_SHA_384:
1304 mbedtls_sha512_init( &operation->ctx.sha512 );
1305 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1306 break;
1307 case PSA_ALG_SHA_512:
1308 mbedtls_sha512_init( &operation->ctx.sha512 );
1309 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1310 break;
1311#endif
1312 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001313 return( PSA_ALG_IS_HASH( alg ) ?
1314 PSA_ERROR_NOT_SUPPORTED :
1315 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001316 }
1317 if( ret == 0 )
1318 operation->alg = alg;
1319 else
1320 psa_hash_abort( operation );
1321 return( mbedtls_to_psa_error( ret ) );
1322}
1323
1324psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1325 const uint8_t *input,
1326 size_t input_length )
1327{
1328 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001329
1330 /* Don't require hash implementations to behave correctly on a
1331 * zero-length input, which may have an invalid pointer. */
1332 if( input_length == 0 )
1333 return( PSA_SUCCESS );
1334
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001335 switch( operation->alg )
1336 {
1337#if defined(MBEDTLS_MD2_C)
1338 case PSA_ALG_MD2:
1339 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1340 input, input_length );
1341 break;
1342#endif
1343#if defined(MBEDTLS_MD4_C)
1344 case PSA_ALG_MD4:
1345 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1346 input, input_length );
1347 break;
1348#endif
1349#if defined(MBEDTLS_MD5_C)
1350 case PSA_ALG_MD5:
1351 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1352 input, input_length );
1353 break;
1354#endif
1355#if defined(MBEDTLS_RIPEMD160_C)
1356 case PSA_ALG_RIPEMD160:
1357 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1358 input, input_length );
1359 break;
1360#endif
1361#if defined(MBEDTLS_SHA1_C)
1362 case PSA_ALG_SHA_1:
1363 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1364 input, input_length );
1365 break;
1366#endif
1367#if defined(MBEDTLS_SHA256_C)
1368 case PSA_ALG_SHA_224:
1369 case PSA_ALG_SHA_256:
1370 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1371 input, input_length );
1372 break;
1373#endif
1374#if defined(MBEDTLS_SHA512_C)
1375 case PSA_ALG_SHA_384:
1376 case PSA_ALG_SHA_512:
1377 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1378 input, input_length );
1379 break;
1380#endif
1381 default:
1382 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1383 break;
1384 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001385
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001386 if( ret != 0 )
1387 psa_hash_abort( operation );
1388 return( mbedtls_to_psa_error( ret ) );
1389}
1390
1391psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1392 uint8_t *hash,
1393 size_t hash_size,
1394 size_t *hash_length )
1395{
itayzafrir40835d42018-08-02 13:14:17 +03001396 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001397 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001398 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001399
1400 /* Fill the output buffer with something that isn't a valid hash
1401 * (barring an attack on the hash and deliberately-crafted input),
1402 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001403 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001404 /* If hash_size is 0 then hash may be NULL and then the
1405 * call to memset would have undefined behavior. */
1406 if( hash_size != 0 )
1407 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001408
1409 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001410 {
1411 status = PSA_ERROR_BUFFER_TOO_SMALL;
1412 goto exit;
1413 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001414
1415 switch( operation->alg )
1416 {
1417#if defined(MBEDTLS_MD2_C)
1418 case PSA_ALG_MD2:
1419 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1420 break;
1421#endif
1422#if defined(MBEDTLS_MD4_C)
1423 case PSA_ALG_MD4:
1424 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1425 break;
1426#endif
1427#if defined(MBEDTLS_MD5_C)
1428 case PSA_ALG_MD5:
1429 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1430 break;
1431#endif
1432#if defined(MBEDTLS_RIPEMD160_C)
1433 case PSA_ALG_RIPEMD160:
1434 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1435 break;
1436#endif
1437#if defined(MBEDTLS_SHA1_C)
1438 case PSA_ALG_SHA_1:
1439 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1440 break;
1441#endif
1442#if defined(MBEDTLS_SHA256_C)
1443 case PSA_ALG_SHA_224:
1444 case PSA_ALG_SHA_256:
1445 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1446 break;
1447#endif
1448#if defined(MBEDTLS_SHA512_C)
1449 case PSA_ALG_SHA_384:
1450 case PSA_ALG_SHA_512:
1451 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1452 break;
1453#endif
1454 default:
1455 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1456 break;
1457 }
itayzafrir40835d42018-08-02 13:14:17 +03001458 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001459
itayzafrir40835d42018-08-02 13:14:17 +03001460exit:
1461 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001462 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001463 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001464 return( psa_hash_abort( operation ) );
1465 }
1466 else
1467 {
1468 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001469 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001470 }
1471}
1472
Gilles Peskine2d277862018-06-18 15:41:12 +02001473psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1474 const uint8_t *hash,
1475 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001476{
1477 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1478 size_t actual_hash_length;
1479 psa_status_t status = psa_hash_finish( operation,
1480 actual_hash, sizeof( actual_hash ),
1481 &actual_hash_length );
1482 if( status != PSA_SUCCESS )
1483 return( status );
1484 if( actual_hash_length != hash_length )
1485 return( PSA_ERROR_INVALID_SIGNATURE );
1486 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1487 return( PSA_ERROR_INVALID_SIGNATURE );
1488 return( PSA_SUCCESS );
1489}
1490
1491
1492
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001493/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001494/* MAC */
1495/****************************************************************/
1496
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001497static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001498 psa_algorithm_t alg,
1499 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001500 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001501 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001502{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001503 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001504 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001505
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001506 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001507 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001508
Gilles Peskine8c9def32018-02-08 10:02:12 +01001509 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1510 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001511 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001512 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001513 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001514 mode = MBEDTLS_MODE_STREAM;
1515 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001516 case PSA_ALG_CTR:
1517 mode = MBEDTLS_MODE_CTR;
1518 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001519 case PSA_ALG_CFB:
1520 mode = MBEDTLS_MODE_CFB;
1521 break;
1522 case PSA_ALG_OFB:
1523 mode = MBEDTLS_MODE_OFB;
1524 break;
1525 case PSA_ALG_CBC_NO_PADDING:
1526 mode = MBEDTLS_MODE_CBC;
1527 break;
1528 case PSA_ALG_CBC_PKCS7:
1529 mode = MBEDTLS_MODE_CBC;
1530 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001531 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001532 mode = MBEDTLS_MODE_CCM;
1533 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001534 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001535 mode = MBEDTLS_MODE_GCM;
1536 break;
1537 default:
1538 return( NULL );
1539 }
1540 }
1541 else if( alg == PSA_ALG_CMAC )
1542 mode = MBEDTLS_MODE_ECB;
1543 else if( alg == PSA_ALG_GMAC )
1544 mode = MBEDTLS_MODE_GCM;
1545 else
1546 return( NULL );
1547
1548 switch( key_type )
1549 {
1550 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001551 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001552 break;
1553 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001554 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1555 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001556 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001557 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001558 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001559 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001560 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1561 * but two-key Triple-DES is functionally three-key Triple-DES
1562 * with K1=K3, so that's how we present it to mbedtls. */
1563 if( key_bits == 128 )
1564 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001565 break;
1566 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001567 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001568 break;
1569 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001570 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001571 break;
1572 default:
1573 return( NULL );
1574 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001575 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001576 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001577
Jaeden Amero23bbb752018-06-26 14:16:54 +01001578 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1579 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001580}
1581
Gilles Peskinea05219c2018-11-16 16:02:56 +01001582#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001583static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001584{
Gilles Peskine2d277862018-06-18 15:41:12 +02001585 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001586 {
1587 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001588 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001589 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001590 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001591 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001592 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001593 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001594 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001595 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001596 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001597 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001598 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001599 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001600 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001601 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001602 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001603 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001604 return( 128 );
1605 default:
1606 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001607 }
1608}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001609#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001610
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001611/* Initialize the MAC operation structure. Once this function has been
1612 * called, psa_mac_abort can run and will do the right thing. */
1613static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1614 psa_algorithm_t alg )
1615{
1616 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1617
1618 operation->alg = alg;
1619 operation->key_set = 0;
1620 operation->iv_set = 0;
1621 operation->iv_required = 0;
1622 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001623 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001624
1625#if defined(MBEDTLS_CMAC_C)
1626 if( alg == PSA_ALG_CMAC )
1627 {
1628 operation->iv_required = 0;
1629 mbedtls_cipher_init( &operation->ctx.cmac );
1630 status = PSA_SUCCESS;
1631 }
1632 else
1633#endif /* MBEDTLS_CMAC_C */
1634#if defined(MBEDTLS_MD_C)
1635 if( PSA_ALG_IS_HMAC( operation->alg ) )
1636 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001637 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1638 operation->ctx.hmac.hash_ctx.alg = 0;
1639 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001640 }
1641 else
1642#endif /* MBEDTLS_MD_C */
1643 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001644 if( ! PSA_ALG_IS_MAC( alg ) )
1645 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001646 }
1647
1648 if( status != PSA_SUCCESS )
1649 memset( operation, 0, sizeof( *operation ) );
1650 return( status );
1651}
1652
Gilles Peskine01126fa2018-07-12 17:04:55 +02001653#if defined(MBEDTLS_MD_C)
1654static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1655{
1656 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1657 return( psa_hash_abort( &hmac->hash_ctx ) );
1658}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001659
1660static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1661{
1662 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1663 memset( hmac, 0, sizeof( *hmac ) );
1664}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001665#endif /* MBEDTLS_MD_C */
1666
Gilles Peskine8c9def32018-02-08 10:02:12 +01001667psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1668{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001669 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001670 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001671 /* The object has (apparently) been initialized but it is not
1672 * in use. It's ok to call abort on such an object, and there's
1673 * nothing to do. */
1674 return( PSA_SUCCESS );
1675 }
1676 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001677#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001678 if( operation->alg == PSA_ALG_CMAC )
1679 {
1680 mbedtls_cipher_free( &operation->ctx.cmac );
1681 }
1682 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001683#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001684#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001685 if( PSA_ALG_IS_HMAC( operation->alg ) )
1686 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001687 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001688 }
1689 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001690#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001691 {
1692 /* Sanity check (shouldn't happen: operation->alg should
1693 * always have been initialized to a valid value). */
1694 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001695 }
Moran Peker41deec42018-04-04 15:43:05 +03001696
Gilles Peskine8c9def32018-02-08 10:02:12 +01001697 operation->alg = 0;
1698 operation->key_set = 0;
1699 operation->iv_set = 0;
1700 operation->iv_required = 0;
1701 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001702 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001703
Gilles Peskine8c9def32018-02-08 10:02:12 +01001704 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001705
1706bad_state:
1707 /* If abort is called on an uninitialized object, we can't trust
1708 * anything. Wipe the object in case it contains confidential data.
1709 * This may result in a memory leak if a pointer gets overwritten,
1710 * but it's too late to do anything about this. */
1711 memset( operation, 0, sizeof( *operation ) );
1712 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001713}
1714
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001715#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001716static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001717 size_t key_bits,
1718 key_slot_t *slot,
1719 const mbedtls_cipher_info_t *cipher_info )
1720{
1721 int ret;
1722
1723 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001724
1725 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1726 if( ret != 0 )
1727 return( ret );
1728
1729 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1730 slot->data.raw.data,
1731 key_bits );
1732 return( ret );
1733}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001734#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001735
Gilles Peskine248051a2018-06-20 16:09:38 +02001736#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001737static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1738 const uint8_t *key,
1739 size_t key_length,
1740 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001741{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001742 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001743 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001744 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001745 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001746 psa_status_t status;
1747
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001748 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1749 * overflow below. This should never trigger if the hash algorithm
1750 * is implemented correctly. */
1751 /* The size checks against the ipad and opad buffers cannot be written
1752 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1753 * because that triggers -Wlogical-op on GCC 7.3. */
1754 if( block_size > sizeof( ipad ) )
1755 return( PSA_ERROR_NOT_SUPPORTED );
1756 if( block_size > sizeof( hmac->opad ) )
1757 return( PSA_ERROR_NOT_SUPPORTED );
1758 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001759 return( PSA_ERROR_NOT_SUPPORTED );
1760
Gilles Peskined223b522018-06-11 18:12:58 +02001761 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001762 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001763 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001764 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001765 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001766 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001767 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001768 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001769 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001770 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001771 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001772 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001773 }
Gilles Peskine96889972018-07-12 17:07:03 +02001774 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1775 * but it is permitted. It is common when HMAC is used in HKDF, for
1776 * example. Don't call `memcpy` in the 0-length because `key` could be
1777 * an invalid pointer which would make the behavior undefined. */
1778 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001779 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001780
Gilles Peskined223b522018-06-11 18:12:58 +02001781 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1782 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001783 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001784 ipad[i] ^= 0x36;
1785 memset( ipad + key_length, 0x36, block_size - key_length );
1786
1787 /* Copy the key material from ipad to opad, flipping the requisite bits,
1788 * and filling the rest of opad with the requisite constant. */
1789 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001790 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1791 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001792
Gilles Peskine01126fa2018-07-12 17:04:55 +02001793 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001794 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001795 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001796
Gilles Peskine01126fa2018-07-12 17:04:55 +02001797 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001798
1799cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001800 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001801
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001802 return( status );
1803}
Gilles Peskine248051a2018-06-20 16:09:38 +02001804#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001805
Gilles Peskine89167cb2018-07-08 20:12:23 +02001806static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1807 psa_key_slot_t key,
1808 psa_algorithm_t alg,
1809 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001810{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001811 psa_status_t status;
1812 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001813 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001814 psa_key_usage_t usage =
1815 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001816 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001817 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001818
Gilles Peskined911eb72018-08-14 15:18:45 +02001819 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001820 if( status != PSA_SUCCESS )
1821 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001822 if( is_sign )
1823 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001824
Gilles Peskine89167cb2018-07-08 20:12:23 +02001825 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001826 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001827 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001828 key_bits = psa_get_key_bits( slot );
1829
Gilles Peskine8c9def32018-02-08 10:02:12 +01001830#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001831 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001832 {
1833 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001834 mbedtls_cipher_info_from_psa( full_length_alg,
1835 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001836 int ret;
1837 if( cipher_info == NULL )
1838 {
1839 status = PSA_ERROR_NOT_SUPPORTED;
1840 goto exit;
1841 }
1842 operation->mac_size = cipher_info->block_size;
1843 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1844 status = mbedtls_to_psa_error( ret );
1845 }
1846 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001847#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001848#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001849 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001850 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001851 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001852 if( hash_alg == 0 )
1853 {
1854 status = PSA_ERROR_NOT_SUPPORTED;
1855 goto exit;
1856 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001857
1858 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1859 /* Sanity check. This shouldn't fail on a valid configuration. */
1860 if( operation->mac_size == 0 ||
1861 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1862 {
1863 status = PSA_ERROR_NOT_SUPPORTED;
1864 goto exit;
1865 }
1866
Gilles Peskine01126fa2018-07-12 17:04:55 +02001867 if( slot->type != PSA_KEY_TYPE_HMAC )
1868 {
1869 status = PSA_ERROR_INVALID_ARGUMENT;
1870 goto exit;
1871 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001872
Gilles Peskine01126fa2018-07-12 17:04:55 +02001873 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1874 slot->data.raw.data,
1875 slot->data.raw.bytes,
1876 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001877 }
1878 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001879#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001880 {
1881 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001882 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001883
Gilles Peskined911eb72018-08-14 15:18:45 +02001884 if( truncated == 0 )
1885 {
1886 /* The "normal" case: untruncated algorithm. Nothing to do. */
1887 }
1888 else if( truncated < 4 )
1889 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001890 /* A very short MAC is too short for security since it can be
1891 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1892 * so we make this our minimum, even though 32 bits is still
1893 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001894 status = PSA_ERROR_NOT_SUPPORTED;
1895 }
1896 else if( truncated > operation->mac_size )
1897 {
1898 /* It's impossible to "truncate" to a larger length. */
1899 status = PSA_ERROR_INVALID_ARGUMENT;
1900 }
1901 else
1902 operation->mac_size = truncated;
1903
Gilles Peskinefbfac682018-07-08 20:51:54 +02001904exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001905 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001906 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001907 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001908 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001909 else
1910 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001911 operation->key_set = 1;
1912 }
1913 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001914}
1915
Gilles Peskine89167cb2018-07-08 20:12:23 +02001916psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1917 psa_key_slot_t key,
1918 psa_algorithm_t alg )
1919{
1920 return( psa_mac_setup( operation, key, alg, 1 ) );
1921}
1922
1923psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1924 psa_key_slot_t key,
1925 psa_algorithm_t alg )
1926{
1927 return( psa_mac_setup( operation, key, alg, 0 ) );
1928}
1929
Gilles Peskine8c9def32018-02-08 10:02:12 +01001930psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1931 const uint8_t *input,
1932 size_t input_length )
1933{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001934 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001935 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001936 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001937 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001938 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001939 operation->has_input = 1;
1940
Gilles Peskine8c9def32018-02-08 10:02:12 +01001941#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001942 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001943 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001944 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1945 input, input_length );
1946 status = mbedtls_to_psa_error( ret );
1947 }
1948 else
1949#endif /* MBEDTLS_CMAC_C */
1950#if defined(MBEDTLS_MD_C)
1951 if( PSA_ALG_IS_HMAC( operation->alg ) )
1952 {
1953 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1954 input_length );
1955 }
1956 else
1957#endif /* MBEDTLS_MD_C */
1958 {
1959 /* This shouldn't happen if `operation` was initialized by
1960 * a setup function. */
1961 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001962 }
1963
Gilles Peskinefbfac682018-07-08 20:51:54 +02001964cleanup:
1965 if( status != PSA_SUCCESS )
1966 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001967 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001968}
1969
Gilles Peskine01126fa2018-07-12 17:04:55 +02001970#if defined(MBEDTLS_MD_C)
1971static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1972 uint8_t *mac,
1973 size_t mac_size )
1974{
1975 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1976 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1977 size_t hash_size = 0;
1978 size_t block_size = psa_get_hash_block_size( hash_alg );
1979 psa_status_t status;
1980
Gilles Peskine01126fa2018-07-12 17:04:55 +02001981 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1982 if( status != PSA_SUCCESS )
1983 return( status );
1984 /* From here on, tmp needs to be wiped. */
1985
1986 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1987 if( status != PSA_SUCCESS )
1988 goto exit;
1989
1990 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1991 if( status != PSA_SUCCESS )
1992 goto exit;
1993
1994 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1995 if( status != PSA_SUCCESS )
1996 goto exit;
1997
Gilles Peskined911eb72018-08-14 15:18:45 +02001998 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1999 if( status != PSA_SUCCESS )
2000 goto exit;
2001
2002 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002003
2004exit:
2005 mbedtls_zeroize( tmp, hash_size );
2006 return( status );
2007}
2008#endif /* MBEDTLS_MD_C */
2009
mohammad16036df908f2018-04-02 08:34:15 -07002010static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002011 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002012 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002013{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002014 if( ! operation->key_set )
2015 return( PSA_ERROR_BAD_STATE );
2016 if( operation->iv_required && ! operation->iv_set )
2017 return( PSA_ERROR_BAD_STATE );
2018
Gilles Peskine8c9def32018-02-08 10:02:12 +01002019 if( mac_size < operation->mac_size )
2020 return( PSA_ERROR_BUFFER_TOO_SMALL );
2021
Gilles Peskine8c9def32018-02-08 10:02:12 +01002022#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002023 if( operation->alg == PSA_ALG_CMAC )
2024 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002025 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2026 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2027 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002028 memcpy( mac, tmp, operation->mac_size );
Gilles Peskined911eb72018-08-14 15:18:45 +02002029 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002030 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002031 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002032 else
2033#endif /* MBEDTLS_CMAC_C */
2034#if defined(MBEDTLS_MD_C)
2035 if( PSA_ALG_IS_HMAC( operation->alg ) )
2036 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002037 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002038 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002039 }
2040 else
2041#endif /* MBEDTLS_MD_C */
2042 {
2043 /* This shouldn't happen if `operation` was initialized by
2044 * a setup function. */
2045 return( PSA_ERROR_BAD_STATE );
2046 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002047}
2048
Gilles Peskineacd4be32018-07-08 19:56:25 +02002049psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2050 uint8_t *mac,
2051 size_t mac_size,
2052 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002053{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002054 psa_status_t status;
2055
2056 /* Fill the output buffer with something that isn't a valid mac
2057 * (barring an attack on the mac and deliberately-crafted input),
2058 * in case the caller doesn't check the return status properly. */
2059 *mac_length = mac_size;
2060 /* If mac_size is 0 then mac may be NULL and then the
2061 * call to memset would have undefined behavior. */
2062 if( mac_size != 0 )
2063 memset( mac, '!', mac_size );
2064
Gilles Peskine89167cb2018-07-08 20:12:23 +02002065 if( ! operation->is_sign )
2066 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002067 status = PSA_ERROR_BAD_STATE;
2068 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002069 }
mohammad16036df908f2018-04-02 08:34:15 -07002070
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002071 status = psa_mac_finish_internal( operation, mac, mac_size );
2072
2073cleanup:
2074 if( status == PSA_SUCCESS )
2075 {
2076 status = psa_mac_abort( operation );
2077 if( status == PSA_SUCCESS )
2078 *mac_length = operation->mac_size;
2079 else
2080 memset( mac, '!', mac_size );
2081 }
2082 else
2083 psa_mac_abort( operation );
2084 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002085}
2086
Gilles Peskineacd4be32018-07-08 19:56:25 +02002087psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2088 const uint8_t *mac,
2089 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002090{
Gilles Peskine828ed142018-06-18 23:25:51 +02002091 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002092 psa_status_t status;
2093
Gilles Peskine89167cb2018-07-08 20:12:23 +02002094 if( operation->is_sign )
2095 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002096 status = PSA_ERROR_BAD_STATE;
2097 goto cleanup;
2098 }
2099 if( operation->mac_size != mac_length )
2100 {
2101 status = PSA_ERROR_INVALID_SIGNATURE;
2102 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002103 }
mohammad16036df908f2018-04-02 08:34:15 -07002104
2105 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002106 actual_mac, sizeof( actual_mac ) );
2107
2108 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2109 status = PSA_ERROR_INVALID_SIGNATURE;
2110
2111cleanup:
2112 if( status == PSA_SUCCESS )
2113 status = psa_mac_abort( operation );
2114 else
2115 psa_mac_abort( operation );
2116
Gilles Peskine99b7d6b2018-08-21 14:56:19 +02002117 mbedtls_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002118
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002119 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002120}
2121
2122
Gilles Peskine20035e32018-02-03 22:44:14 +01002123
Gilles Peskine20035e32018-02-03 22:44:14 +01002124/****************************************************************/
2125/* Asymmetric cryptography */
2126/****************************************************************/
2127
Gilles Peskine2b450e32018-06-27 15:42:46 +02002128#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002129/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002130 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002131static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2132 size_t hash_length,
2133 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002134{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002135 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002136 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002137 *md_alg = mbedtls_md_get_type( md_info );
2138
2139 /* The Mbed TLS RSA module uses an unsigned int for hash length
2140 * parameters. Validate that it fits so that we don't risk an
2141 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002142#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002143 if( hash_length > UINT_MAX )
2144 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002145#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002146
2147#if defined(MBEDTLS_PKCS1_V15)
2148 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2149 * must be correct. */
2150 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2151 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002152 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002153 if( md_info == NULL )
2154 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002155 if( mbedtls_md_get_size( md_info ) != hash_length )
2156 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002157 }
2158#endif /* MBEDTLS_PKCS1_V15 */
2159
2160#if defined(MBEDTLS_PKCS1_V21)
2161 /* PSS requires a hash internally. */
2162 if( PSA_ALG_IS_RSA_PSS( alg ) )
2163 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002164 if( md_info == NULL )
2165 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002166 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002167#endif /* MBEDTLS_PKCS1_V21 */
2168
Gilles Peskine61b91d42018-06-08 16:09:36 +02002169 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002170}
2171
Gilles Peskine2b450e32018-06-27 15:42:46 +02002172static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2173 psa_algorithm_t alg,
2174 const uint8_t *hash,
2175 size_t hash_length,
2176 uint8_t *signature,
2177 size_t signature_size,
2178 size_t *signature_length )
2179{
2180 psa_status_t status;
2181 int ret;
2182 mbedtls_md_type_t md_alg;
2183
2184 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2185 if( status != PSA_SUCCESS )
2186 return( status );
2187
Gilles Peskine630a18a2018-06-29 17:49:35 +02002188 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002189 return( PSA_ERROR_BUFFER_TOO_SMALL );
2190
2191#if defined(MBEDTLS_PKCS1_V15)
2192 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2193 {
2194 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2195 MBEDTLS_MD_NONE );
2196 ret = mbedtls_rsa_pkcs1_sign( rsa,
2197 mbedtls_ctr_drbg_random,
2198 &global_data.ctr_drbg,
2199 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002200 md_alg,
2201 (unsigned int) hash_length,
2202 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002203 signature );
2204 }
2205 else
2206#endif /* MBEDTLS_PKCS1_V15 */
2207#if defined(MBEDTLS_PKCS1_V21)
2208 if( PSA_ALG_IS_RSA_PSS( alg ) )
2209 {
2210 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2211 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2212 mbedtls_ctr_drbg_random,
2213 &global_data.ctr_drbg,
2214 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002215 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002216 (unsigned int) hash_length,
2217 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002218 signature );
2219 }
2220 else
2221#endif /* MBEDTLS_PKCS1_V21 */
2222 {
2223 return( PSA_ERROR_INVALID_ARGUMENT );
2224 }
2225
2226 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002227 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002228 return( mbedtls_to_psa_error( ret ) );
2229}
2230
2231static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2232 psa_algorithm_t alg,
2233 const uint8_t *hash,
2234 size_t hash_length,
2235 const uint8_t *signature,
2236 size_t signature_length )
2237{
2238 psa_status_t status;
2239 int ret;
2240 mbedtls_md_type_t md_alg;
2241
2242 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2243 if( status != PSA_SUCCESS )
2244 return( status );
2245
Gilles Peskine630a18a2018-06-29 17:49:35 +02002246 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002247 return( PSA_ERROR_BUFFER_TOO_SMALL );
2248
2249#if defined(MBEDTLS_PKCS1_V15)
2250 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2251 {
2252 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2253 MBEDTLS_MD_NONE );
2254 ret = mbedtls_rsa_pkcs1_verify( rsa,
2255 mbedtls_ctr_drbg_random,
2256 &global_data.ctr_drbg,
2257 MBEDTLS_RSA_PUBLIC,
2258 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002259 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002260 hash,
2261 signature );
2262 }
2263 else
2264#endif /* MBEDTLS_PKCS1_V15 */
2265#if defined(MBEDTLS_PKCS1_V21)
2266 if( PSA_ALG_IS_RSA_PSS( alg ) )
2267 {
2268 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2269 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2270 mbedtls_ctr_drbg_random,
2271 &global_data.ctr_drbg,
2272 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002273 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002274 (unsigned int) hash_length,
2275 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002276 signature );
2277 }
2278 else
2279#endif /* MBEDTLS_PKCS1_V21 */
2280 {
2281 return( PSA_ERROR_INVALID_ARGUMENT );
2282 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002283
2284 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2285 * the rest of the signature is invalid". This has little use in
2286 * practice and PSA doesn't report this distinction. */
2287 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2288 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002289 return( mbedtls_to_psa_error( ret ) );
2290}
2291#endif /* MBEDTLS_RSA_C */
2292
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002293#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002294/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2295 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2296 * (even though these functions don't modify it). */
2297static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2298 psa_algorithm_t alg,
2299 const uint8_t *hash,
2300 size_t hash_length,
2301 uint8_t *signature,
2302 size_t signature_size,
2303 size_t *signature_length )
2304{
2305 int ret;
2306 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002307 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002308 mbedtls_mpi_init( &r );
2309 mbedtls_mpi_init( &s );
2310
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002311 if( signature_size < 2 * curve_bytes )
2312 {
2313 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2314 goto cleanup;
2315 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002316
Gilles Peskinea05219c2018-11-16 16:02:56 +01002317#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002318 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2319 {
2320 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2321 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2322 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2323 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2324 hash, hash_length,
2325 md_alg ) );
2326 }
2327 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002328#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002329 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002330 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002331 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2332 hash, hash_length,
2333 mbedtls_ctr_drbg_random,
2334 &global_data.ctr_drbg ) );
2335 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002336
2337 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2338 signature,
2339 curve_bytes ) );
2340 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2341 signature + curve_bytes,
2342 curve_bytes ) );
2343
2344cleanup:
2345 mbedtls_mpi_free( &r );
2346 mbedtls_mpi_free( &s );
2347 if( ret == 0 )
2348 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002349 return( mbedtls_to_psa_error( ret ) );
2350}
2351
2352static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2353 const uint8_t *hash,
2354 size_t hash_length,
2355 const uint8_t *signature,
2356 size_t signature_length )
2357{
2358 int ret;
2359 mbedtls_mpi r, s;
2360 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2361 mbedtls_mpi_init( &r );
2362 mbedtls_mpi_init( &s );
2363
2364 if( signature_length != 2 * curve_bytes )
2365 return( PSA_ERROR_INVALID_SIGNATURE );
2366
2367 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2368 signature,
2369 curve_bytes ) );
2370 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2371 signature + curve_bytes,
2372 curve_bytes ) );
2373
2374 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2375 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002376
2377cleanup:
2378 mbedtls_mpi_free( &r );
2379 mbedtls_mpi_free( &s );
2380 return( mbedtls_to_psa_error( ret ) );
2381}
2382#endif /* MBEDTLS_ECDSA_C */
2383
Gilles Peskine61b91d42018-06-08 16:09:36 +02002384psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2385 psa_algorithm_t alg,
2386 const uint8_t *hash,
2387 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002388 uint8_t *signature,
2389 size_t signature_size,
2390 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002391{
2392 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002393 psa_status_t status;
2394
2395 *signature_length = signature_size;
2396
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002397 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2398 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002399 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002400 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002401 {
2402 status = PSA_ERROR_INVALID_ARGUMENT;
2403 goto exit;
2404 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002405
Gilles Peskine20035e32018-02-03 22:44:14 +01002406#if defined(MBEDTLS_RSA_C)
2407 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2408 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002409 status = psa_rsa_sign( slot->data.rsa,
2410 alg,
2411 hash, hash_length,
2412 signature, signature_size,
2413 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002414 }
2415 else
2416#endif /* defined(MBEDTLS_RSA_C) */
2417#if defined(MBEDTLS_ECP_C)
2418 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2419 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002420#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002421 if(
2422#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2423 PSA_ALG_IS_ECDSA( alg )
2424#else
2425 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2426#endif
2427 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002428 status = psa_ecdsa_sign( slot->data.ecp,
2429 alg,
2430 hash, hash_length,
2431 signature, signature_size,
2432 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002433 else
2434#endif /* defined(MBEDTLS_ECDSA_C) */
2435 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002436 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002437 }
itayzafrir5c753392018-05-08 11:18:38 +03002438 }
2439 else
2440#endif /* defined(MBEDTLS_ECP_C) */
2441 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002442 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002443 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002444
2445exit:
2446 /* Fill the unused part of the output buffer (the whole buffer on error,
2447 * the trailing part on success) with something that isn't a valid mac
2448 * (barring an attack on the mac and deliberately-crafted input),
2449 * in case the caller doesn't check the return status properly. */
2450 if( status == PSA_SUCCESS )
2451 memset( signature + *signature_length, '!',
2452 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002453 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002454 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002455 /* If signature_size is 0 then we have nothing to do. We must not call
2456 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002457 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002458}
2459
2460psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2461 psa_algorithm_t alg,
2462 const uint8_t *hash,
2463 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002464 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002465 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002466{
2467 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002468 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002469
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002470 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2471 if( status != PSA_SUCCESS )
2472 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002473
Gilles Peskine61b91d42018-06-08 16:09:36 +02002474#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002475 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002476 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002477 return( psa_rsa_verify( slot->data.rsa,
2478 alg,
2479 hash, hash_length,
2480 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002481 }
2482 else
2483#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002484#if defined(MBEDTLS_ECP_C)
2485 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2486 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002487#if defined(MBEDTLS_ECDSA_C)
2488 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002489 return( psa_ecdsa_verify( slot->data.ecp,
2490 hash, hash_length,
2491 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002492 else
2493#endif /* defined(MBEDTLS_ECDSA_C) */
2494 {
2495 return( PSA_ERROR_INVALID_ARGUMENT );
2496 }
itayzafrir5c753392018-05-08 11:18:38 +03002497 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002498 else
2499#endif /* defined(MBEDTLS_ECP_C) */
2500 {
2501 return( PSA_ERROR_NOT_SUPPORTED );
2502 }
2503}
2504
Gilles Peskine072ac562018-06-30 00:21:29 +02002505#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2506static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2507 mbedtls_rsa_context *rsa )
2508{
2509 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2510 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2511 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2512 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2513}
2514#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2515
Gilles Peskine61b91d42018-06-08 16:09:36 +02002516psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2517 psa_algorithm_t alg,
2518 const uint8_t *input,
2519 size_t input_length,
2520 const uint8_t *salt,
2521 size_t salt_length,
2522 uint8_t *output,
2523 size_t output_size,
2524 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002525{
2526 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002527 psa_status_t status;
2528
Darryl Green5cc689a2018-07-24 15:34:10 +01002529 (void) input;
2530 (void) input_length;
2531 (void) salt;
2532 (void) output;
2533 (void) output_size;
2534
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002535 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002536
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002537 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2538 return( PSA_ERROR_INVALID_ARGUMENT );
2539
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002540 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2541 if( status != PSA_SUCCESS )
2542 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002543 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2544 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002545 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002546
2547#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002548 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002549 {
2550 mbedtls_rsa_context *rsa = slot->data.rsa;
2551 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002552 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002553 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002554#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002555 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002556 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002557 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2558 mbedtls_ctr_drbg_random,
2559 &global_data.ctr_drbg,
2560 MBEDTLS_RSA_PUBLIC,
2561 input_length,
2562 input,
2563 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002564 }
2565 else
2566#endif /* MBEDTLS_PKCS1_V15 */
2567#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002568 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002569 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002570 psa_rsa_oaep_set_padding_mode( alg, rsa );
2571 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2572 mbedtls_ctr_drbg_random,
2573 &global_data.ctr_drbg,
2574 MBEDTLS_RSA_PUBLIC,
2575 salt, salt_length,
2576 input_length,
2577 input,
2578 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002579 }
2580 else
2581#endif /* MBEDTLS_PKCS1_V21 */
2582 {
2583 return( PSA_ERROR_INVALID_ARGUMENT );
2584 }
2585 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002586 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002587 return( mbedtls_to_psa_error( ret ) );
2588 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002589 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002590#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002591 {
2592 return( PSA_ERROR_NOT_SUPPORTED );
2593 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002594}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002595
Gilles Peskine61b91d42018-06-08 16:09:36 +02002596psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2597 psa_algorithm_t alg,
2598 const uint8_t *input,
2599 size_t input_length,
2600 const uint8_t *salt,
2601 size_t salt_length,
2602 uint8_t *output,
2603 size_t output_size,
2604 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002605{
2606 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002607 psa_status_t status;
2608
Darryl Green5cc689a2018-07-24 15:34:10 +01002609 (void) input;
2610 (void) input_length;
2611 (void) salt;
2612 (void) output;
2613 (void) output_size;
2614
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002615 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002616
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002617 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2618 return( PSA_ERROR_INVALID_ARGUMENT );
2619
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002620 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2621 if( status != PSA_SUCCESS )
2622 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002623 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2624 return( PSA_ERROR_INVALID_ARGUMENT );
2625
2626#if defined(MBEDTLS_RSA_C)
2627 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2628 {
2629 mbedtls_rsa_context *rsa = slot->data.rsa;
2630 int ret;
2631
Gilles Peskine630a18a2018-06-29 17:49:35 +02002632 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002633 return( PSA_ERROR_INVALID_ARGUMENT );
2634
2635#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002636 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002637 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002638 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2639 mbedtls_ctr_drbg_random,
2640 &global_data.ctr_drbg,
2641 MBEDTLS_RSA_PRIVATE,
2642 output_length,
2643 input,
2644 output,
2645 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002646 }
2647 else
2648#endif /* MBEDTLS_PKCS1_V15 */
2649#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002650 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002651 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002652 psa_rsa_oaep_set_padding_mode( alg, rsa );
2653 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2654 mbedtls_ctr_drbg_random,
2655 &global_data.ctr_drbg,
2656 MBEDTLS_RSA_PRIVATE,
2657 salt, salt_length,
2658 output_length,
2659 input,
2660 output,
2661 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002662 }
2663 else
2664#endif /* MBEDTLS_PKCS1_V21 */
2665 {
2666 return( PSA_ERROR_INVALID_ARGUMENT );
2667 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002668
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002669 return( mbedtls_to_psa_error( ret ) );
2670 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002671 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002672#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002673 {
2674 return( PSA_ERROR_NOT_SUPPORTED );
2675 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002676}
Gilles Peskine20035e32018-02-03 22:44:14 +01002677
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002678
2679
mohammad1603503973b2018-03-12 15:59:30 +02002680/****************************************************************/
2681/* Symmetric cryptography */
2682/****************************************************************/
2683
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002684/* Initialize the cipher operation structure. Once this function has been
2685 * called, psa_cipher_abort can run and will do the right thing. */
2686static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2687 psa_algorithm_t alg )
2688{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002689 if( ! PSA_ALG_IS_CIPHER( alg ) )
2690 {
2691 memset( operation, 0, sizeof( *operation ) );
2692 return( PSA_ERROR_INVALID_ARGUMENT );
2693 }
2694
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002695 operation->alg = alg;
2696 operation->key_set = 0;
2697 operation->iv_set = 0;
2698 operation->iv_required = 1;
2699 operation->iv_size = 0;
2700 operation->block_size = 0;
2701 mbedtls_cipher_init( &operation->ctx.cipher );
2702 return( PSA_SUCCESS );
2703}
2704
Gilles Peskinee553c652018-06-04 16:22:46 +02002705static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2706 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002707 psa_algorithm_t alg,
2708 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002709{
2710 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2711 psa_status_t status;
2712 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002713 size_t key_bits;
2714 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002715 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2716 PSA_KEY_USAGE_ENCRYPT :
2717 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002718
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002719 status = psa_cipher_init( operation, alg );
2720 if( status != PSA_SUCCESS )
2721 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002722
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002723 status = psa_get_key_from_slot( key, &slot, usage, alg);
2724 if( status != PSA_SUCCESS )
2725 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002726 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002727
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002728 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002729 if( cipher_info == NULL )
2730 return( PSA_ERROR_NOT_SUPPORTED );
2731
mohammad1603503973b2018-03-12 15:59:30 +02002732 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002733 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002734 {
2735 psa_cipher_abort( operation );
2736 return( mbedtls_to_psa_error( ret ) );
2737 }
2738
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002739#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002740 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002741 {
2742 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2743 unsigned char keys[24];
2744 memcpy( keys, slot->data.raw.data, 16 );
2745 memcpy( keys + 16, slot->data.raw.data, 8 );
2746 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2747 keys,
2748 192, cipher_operation );
2749 }
2750 else
2751#endif
2752 {
2753 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2754 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002755 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002756 }
Moran Peker41deec42018-04-04 15:43:05 +03002757 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002758 {
2759 psa_cipher_abort( operation );
2760 return( mbedtls_to_psa_error( ret ) );
2761 }
2762
mohammad16038481e742018-03-18 13:57:31 +02002763#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002764 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002765 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002766 case PSA_ALG_CBC_NO_PADDING:
2767 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2768 MBEDTLS_PADDING_NONE );
2769 break;
2770 case PSA_ALG_CBC_PKCS7:
2771 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2772 MBEDTLS_PADDING_PKCS7 );
2773 break;
2774 default:
2775 /* The algorithm doesn't involve padding. */
2776 ret = 0;
2777 break;
2778 }
2779 if( ret != 0 )
2780 {
2781 psa_cipher_abort( operation );
2782 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002783 }
2784#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2785
mohammad1603503973b2018-03-12 15:59:30 +02002786 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002787 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2788 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2789 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002790 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002791 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002792 }
mohammad1603503973b2018-03-12 15:59:30 +02002793
Moran Peker395db872018-05-31 14:07:14 +03002794 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002795}
2796
Gilles Peskinefe119512018-07-08 21:39:34 +02002797psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2798 psa_key_slot_t key,
2799 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002800{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002801 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002802}
2803
Gilles Peskinefe119512018-07-08 21:39:34 +02002804psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2805 psa_key_slot_t key,
2806 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002807{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002808 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002809}
2810
Gilles Peskinefe119512018-07-08 21:39:34 +02002811psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2812 unsigned char *iv,
2813 size_t iv_size,
2814 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002815{
itayzafrir534bd7c2018-08-02 13:56:32 +03002816 psa_status_t status;
2817 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002818 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002819 {
2820 status = PSA_ERROR_BAD_STATE;
2821 goto exit;
2822 }
Moran Peker41deec42018-04-04 15:43:05 +03002823 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002824 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002825 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002826 goto exit;
2827 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002828 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2829 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002830 if( ret != 0 )
2831 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002832 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002833 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002834 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002835
mohammad16038481e742018-03-18 13:57:31 +02002836 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002837 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002838
Moran Peker395db872018-05-31 14:07:14 +03002839exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002840 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002841 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002842 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002843}
2844
Gilles Peskinefe119512018-07-08 21:39:34 +02002845psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2846 const unsigned char *iv,
2847 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002848{
itayzafrir534bd7c2018-08-02 13:56:32 +03002849 psa_status_t status;
2850 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002851 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002852 {
2853 status = PSA_ERROR_BAD_STATE;
2854 goto exit;
2855 }
Moran Pekera28258c2018-05-29 16:25:04 +03002856 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002857 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002858 status = PSA_ERROR_INVALID_ARGUMENT;
2859 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002860 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002861 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2862 status = mbedtls_to_psa_error( ret );
2863exit:
2864 if( status == PSA_SUCCESS )
2865 operation->iv_set = 1;
2866 else
mohammad1603503973b2018-03-12 15:59:30 +02002867 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002868 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002869}
2870
Gilles Peskinee553c652018-06-04 16:22:46 +02002871psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2872 const uint8_t *input,
2873 size_t input_length,
2874 unsigned char *output,
2875 size_t output_size,
2876 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002877{
itayzafrir534bd7c2018-08-02 13:56:32 +03002878 psa_status_t status;
2879 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002880 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002881 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002882 {
2883 /* Take the unprocessed partial block left over from previous
2884 * update calls, if any, plus the input to this call. Remove
2885 * the last partial block, if any. You get the data that will be
2886 * output in this call. */
2887 expected_output_size =
2888 ( operation->ctx.cipher.unprocessed_len + input_length )
2889 / operation->block_size * operation->block_size;
2890 }
2891 else
2892 {
2893 expected_output_size = input_length;
2894 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002895
Gilles Peskine89d789c2018-06-04 17:17:16 +02002896 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002897 {
2898 status = PSA_ERROR_BUFFER_TOO_SMALL;
2899 goto exit;
2900 }
mohammad160382759612018-03-12 18:16:40 +02002901
mohammad1603503973b2018-03-12 15:59:30 +02002902 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002903 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002904 status = mbedtls_to_psa_error( ret );
2905exit:
2906 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002907 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002908 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002909}
2910
Gilles Peskinee553c652018-06-04 16:22:46 +02002911psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2912 uint8_t *output,
2913 size_t output_size,
2914 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002915{
Janos Follath315b51c2018-07-09 16:04:51 +01002916 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2917 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002918 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002919
mohammad1603503973b2018-03-12 15:59:30 +02002920 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002921 {
Janos Follath315b51c2018-07-09 16:04:51 +01002922 status = PSA_ERROR_BAD_STATE;
2923 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002924 }
2925 if( operation->iv_required && ! operation->iv_set )
2926 {
Janos Follath315b51c2018-07-09 16:04:51 +01002927 status = PSA_ERROR_BAD_STATE;
2928 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002929 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002930
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002931 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002932 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2933 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002934 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002935 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002936 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002937 }
2938
Janos Follath315b51c2018-07-09 16:04:51 +01002939 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2940 temp_output_buffer,
2941 output_length );
2942 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002943 {
Janos Follath315b51c2018-07-09 16:04:51 +01002944 status = mbedtls_to_psa_error( cipher_ret );
2945 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002946 }
Janos Follath315b51c2018-07-09 16:04:51 +01002947
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002948 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002949 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002950 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002951 memcpy( output, temp_output_buffer, *output_length );
2952 else
2953 {
Janos Follath315b51c2018-07-09 16:04:51 +01002954 status = PSA_ERROR_BUFFER_TOO_SMALL;
2955 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002956 }
mohammad1603503973b2018-03-12 15:59:30 +02002957
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 status = psa_cipher_abort( operation );
2960
2961 return( status );
2962
2963error:
2964
2965 *output_length = 0;
2966
Janos Follath279ab8e2018-07-09 16:13:21 +01002967 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002968 (void) psa_cipher_abort( operation );
2969
2970 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002971}
2972
Gilles Peskinee553c652018-06-04 16:22:46 +02002973psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2974{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002975 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002976 {
2977 /* The object has (apparently) been initialized but it is not
2978 * in use. It's ok to call abort on such an object, and there's
2979 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002980 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002981 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002982
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002983 /* Sanity check (shouldn't happen: operation->alg should
2984 * always have been initialized to a valid value). */
2985 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2986 return( PSA_ERROR_BAD_STATE );
2987
mohammad1603503973b2018-03-12 15:59:30 +02002988 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002989
Moran Peker41deec42018-04-04 15:43:05 +03002990 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002991 operation->key_set = 0;
2992 operation->iv_set = 0;
2993 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002994 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002995 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002996
Moran Peker395db872018-05-31 14:07:14 +03002997 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002998}
2999
Gilles Peskinea0655c32018-04-30 17:06:50 +02003000
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003001
mohammad16038cc1cee2018-03-28 01:21:33 +03003002/****************************************************************/
3003/* Key Policy */
3004/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003005
mohammad160327010052018-07-03 13:16:15 +03003006#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003007void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003008{
Gilles Peskine803ce742018-06-18 16:07:14 +02003009 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03003010}
3011
Gilles Peskine2d277862018-06-18 15:41:12 +02003012void psa_key_policy_set_usage( psa_key_policy_t *policy,
3013 psa_key_usage_t usage,
3014 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003015{
mohammad16034eed7572018-03-28 05:14:59 -07003016 policy->usage = usage;
3017 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003018}
3019
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003020psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003021{
mohammad16036df908f2018-04-02 08:34:15 -07003022 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003023}
3024
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003025psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003026{
mohammad16036df908f2018-04-02 08:34:15 -07003027 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003028}
mohammad160327010052018-07-03 13:16:15 +03003029#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003030
Gilles Peskine2d277862018-06-18 15:41:12 +02003031psa_status_t psa_set_key_policy( psa_key_slot_t key,
3032 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003033{
3034 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003035 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003036
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003037 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003038 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003039
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003040 status = psa_get_empty_key_slot( key, &slot );
3041 if( status != PSA_SUCCESS )
3042 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003043
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003044 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3045 PSA_KEY_USAGE_ENCRYPT |
3046 PSA_KEY_USAGE_DECRYPT |
3047 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003048 PSA_KEY_USAGE_VERIFY |
3049 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003050 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003051
mohammad16036df908f2018-04-02 08:34:15 -07003052 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003053
3054 return( PSA_SUCCESS );
3055}
3056
Gilles Peskine2d277862018-06-18 15:41:12 +02003057psa_status_t psa_get_key_policy( psa_key_slot_t key,
3058 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003059{
3060 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003061 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003062
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003063 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003064 return( PSA_ERROR_INVALID_ARGUMENT );
3065
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003066 status = psa_get_key_slot( key, &slot );
3067 if( status != PSA_SUCCESS )
3068 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003069
mohammad16036df908f2018-04-02 08:34:15 -07003070 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003071
3072 return( PSA_SUCCESS );
3073}
Gilles Peskine20035e32018-02-03 22:44:14 +01003074
Gilles Peskinea0655c32018-04-30 17:06:50 +02003075
3076
mohammad1603804cd712018-03-20 22:44:08 +02003077/****************************************************************/
3078/* Key Lifetime */
3079/****************************************************************/
3080
Gilles Peskine2d277862018-06-18 15:41:12 +02003081psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
3082 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003083{
3084 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003085 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003086
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003087 status = psa_get_key_slot( key, &slot );
3088 if( status != PSA_SUCCESS )
3089 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003090
mohammad1603804cd712018-03-20 22:44:08 +02003091 *lifetime = slot->lifetime;
3092
3093 return( PSA_SUCCESS );
3094}
3095
Gilles Peskine2d277862018-06-18 15:41:12 +02003096psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01003097 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003098{
3099 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003100 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003101
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003102 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
3103 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
Darryl Greend49a4992018-06-18 17:27:26 +01003104 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003105 return( PSA_ERROR_INVALID_ARGUMENT );
3106
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003107 status = psa_get_empty_key_slot( key, &slot );
3108 if( status != PSA_SUCCESS )
3109 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02003110
Darryl Greend49a4992018-06-18 17:27:26 +01003111 if( lifetime == PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003112 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003113
Darryl Greend49a4992018-06-18 17:27:26 +01003114#if !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
3115 if( lifetime == PSA_KEY_LIFETIME_PERSISTENT )
3116 return( PSA_ERROR_NOT_SUPPORTED );
3117#endif
3118
mohammad1603060ad8a2018-03-20 14:28:38 -07003119 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02003120
3121 return( PSA_SUCCESS );
3122}
3123
Gilles Peskine20035e32018-02-03 22:44:14 +01003124
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003125
mohammad16035955c982018-04-26 00:53:03 +03003126/****************************************************************/
3127/* AEAD */
3128/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003129
Gilles Peskineedf9a652018-08-17 18:11:56 +02003130typedef struct
3131{
3132 key_slot_t *slot;
3133 const mbedtls_cipher_info_t *cipher_info;
3134 union
3135 {
3136#if defined(MBEDTLS_CCM_C)
3137 mbedtls_ccm_context ccm;
3138#endif /* MBEDTLS_CCM_C */
3139#if defined(MBEDTLS_GCM_C)
3140 mbedtls_gcm_context gcm;
3141#endif /* MBEDTLS_GCM_C */
3142 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003143 psa_algorithm_t core_alg;
3144 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003145 uint8_t tag_length;
3146} aead_operation_t;
3147
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003148static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003149{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003150 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003151 {
3152#if defined(MBEDTLS_CCM_C)
3153 case PSA_ALG_CCM:
3154 mbedtls_ccm_free( &operation->ctx.ccm );
3155 break;
3156#endif /* MBEDTLS_CCM_C */
3157#if defined(MBEDTLS_CCM_C)
3158 case PSA_ALG_GCM:
3159 mbedtls_gcm_free( &operation->ctx.gcm );
3160 break;
3161#endif /* MBEDTLS_GCM_C */
3162 }
3163}
3164
3165static psa_status_t psa_aead_setup( aead_operation_t *operation,
3166 psa_key_slot_t key,
3167 psa_key_usage_t usage,
3168 psa_algorithm_t alg )
3169{
3170 psa_status_t status;
3171 size_t key_bits;
3172 mbedtls_cipher_id_t cipher_id;
3173
3174 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
3175 if( status != PSA_SUCCESS )
3176 return( status );
3177
3178 key_bits = psa_get_key_bits( operation->slot );
3179
3180 operation->cipher_info =
3181 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3182 &cipher_id );
3183 if( operation->cipher_info == NULL )
3184 return( PSA_ERROR_NOT_SUPPORTED );
3185
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003186 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003187 {
3188#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003189 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3190 operation->core_alg = PSA_ALG_CCM;
3191 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003192 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3193 return( PSA_ERROR_INVALID_ARGUMENT );
3194 mbedtls_ccm_init( &operation->ctx.ccm );
3195 status = mbedtls_to_psa_error(
3196 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3197 operation->slot->data.raw.data,
3198 (unsigned int) key_bits ) );
3199 if( status != 0 )
3200 goto cleanup;
3201 break;
3202#endif /* MBEDTLS_CCM_C */
3203
3204#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003205 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3206 operation->core_alg = PSA_ALG_GCM;
3207 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003208 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3209 return( PSA_ERROR_INVALID_ARGUMENT );
3210 mbedtls_gcm_init( &operation->ctx.gcm );
3211 status = mbedtls_to_psa_error(
3212 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3213 operation->slot->data.raw.data,
3214 (unsigned int) key_bits ) );
3215 break;
3216#endif /* MBEDTLS_GCM_C */
3217
3218 default:
3219 return( PSA_ERROR_NOT_SUPPORTED );
3220 }
3221
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003222 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3223 {
3224 status = PSA_ERROR_INVALID_ARGUMENT;
3225 goto cleanup;
3226 }
3227 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3228 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3229 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3230 * In both cases, mbedtls_xxx will validate the tag length below. */
3231
Gilles Peskineedf9a652018-08-17 18:11:56 +02003232 return( PSA_SUCCESS );
3233
3234cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003235 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003236 return( status );
3237}
3238
mohammad16035955c982018-04-26 00:53:03 +03003239psa_status_t psa_aead_encrypt( psa_key_slot_t key,
3240 psa_algorithm_t alg,
3241 const uint8_t *nonce,
3242 size_t nonce_length,
3243 const uint8_t *additional_data,
3244 size_t additional_data_length,
3245 const uint8_t *plaintext,
3246 size_t plaintext_length,
3247 uint8_t *ciphertext,
3248 size_t ciphertext_size,
3249 size_t *ciphertext_length )
3250{
mohammad16035955c982018-04-26 00:53:03 +03003251 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003252 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003253 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003254
mohammad1603f08a5502018-06-03 15:05:47 +03003255 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003256
Gilles Peskineedf9a652018-08-17 18:11:56 +02003257 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003258 if( status != PSA_SUCCESS )
3259 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003260
Gilles Peskineedf9a652018-08-17 18:11:56 +02003261 /* For all currently supported modes, the tag is at the end of the
3262 * ciphertext. */
3263 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3264 {
3265 status = PSA_ERROR_BUFFER_TOO_SMALL;
3266 goto exit;
3267 }
3268 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003269
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003270 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003271 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003272 status = mbedtls_to_psa_error(
3273 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3274 MBEDTLS_GCM_ENCRYPT,
3275 plaintext_length,
3276 nonce, nonce_length,
3277 additional_data, additional_data_length,
3278 plaintext, ciphertext,
3279 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003280 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003281 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003282 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003283 status = mbedtls_to_psa_error(
3284 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3285 plaintext_length,
3286 nonce, nonce_length,
3287 additional_data,
3288 additional_data_length,
3289 plaintext, ciphertext,
3290 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003291 }
mohammad16035c8845f2018-05-09 05:40:09 -07003292 else
3293 {
mohammad1603554faad2018-06-03 15:07:38 +03003294 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003295 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003296
Gilles Peskineedf9a652018-08-17 18:11:56 +02003297 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3298 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003299
Gilles Peskineedf9a652018-08-17 18:11:56 +02003300exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003301 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003302 if( status == PSA_SUCCESS )
3303 *ciphertext_length = plaintext_length + operation.tag_length;
3304 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003305}
3306
Gilles Peskineee652a32018-06-01 19:23:52 +02003307/* Locate the tag in a ciphertext buffer containing the encrypted data
3308 * followed by the tag. Return the length of the part preceding the tag in
3309 * *plaintext_length. This is the size of the plaintext in modes where
3310 * the encrypted data has the same size as the plaintext, such as
3311 * CCM and GCM. */
3312static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3313 const uint8_t *ciphertext,
3314 size_t ciphertext_length,
3315 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003316 const uint8_t **p_tag )
3317{
3318 size_t payload_length;
3319 if( tag_length > ciphertext_length )
3320 return( PSA_ERROR_INVALID_ARGUMENT );
3321 payload_length = ciphertext_length - tag_length;
3322 if( payload_length > plaintext_size )
3323 return( PSA_ERROR_BUFFER_TOO_SMALL );
3324 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003325 return( PSA_SUCCESS );
3326}
3327
mohammad16035955c982018-04-26 00:53:03 +03003328psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3329 psa_algorithm_t alg,
3330 const uint8_t *nonce,
3331 size_t nonce_length,
3332 const uint8_t *additional_data,
3333 size_t additional_data_length,
3334 const uint8_t *ciphertext,
3335 size_t ciphertext_length,
3336 uint8_t *plaintext,
3337 size_t plaintext_size,
3338 size_t *plaintext_length )
3339{
mohammad16035955c982018-04-26 00:53:03 +03003340 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003341 aead_operation_t operation;
3342 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003343
Gilles Peskineee652a32018-06-01 19:23:52 +02003344 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003345
Gilles Peskineedf9a652018-08-17 18:11:56 +02003346 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003347 if( status != PSA_SUCCESS )
3348 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003349
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003350 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003351 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003352 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003353 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003354 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003355 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003356 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003357
Gilles Peskineedf9a652018-08-17 18:11:56 +02003358 status = mbedtls_to_psa_error(
3359 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3360 ciphertext_length - operation.tag_length,
3361 nonce, nonce_length,
3362 additional_data,
3363 additional_data_length,
3364 tag, operation.tag_length,
3365 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003366 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003367 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003368 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003369 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003370 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003371 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003372 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003373 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003374
Gilles Peskineedf9a652018-08-17 18:11:56 +02003375 status = mbedtls_to_psa_error(
3376 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3377 ciphertext_length - operation.tag_length,
3378 nonce, nonce_length,
3379 additional_data,
3380 additional_data_length,
3381 ciphertext, plaintext,
3382 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003383 }
mohammad160339574652018-06-01 04:39:53 -07003384 else
3385 {
mohammad1603554faad2018-06-03 15:07:38 +03003386 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003387 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003388
Gilles Peskineedf9a652018-08-17 18:11:56 +02003389 if( status != PSA_SUCCESS && plaintext_size != 0 )
3390 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003391
Gilles Peskineedf9a652018-08-17 18:11:56 +02003392exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003393 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003394 if( status == PSA_SUCCESS )
3395 *plaintext_length = ciphertext_length - operation.tag_length;
3396 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003397}
3398
Gilles Peskinea0655c32018-04-30 17:06:50 +02003399
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003400
Gilles Peskine20035e32018-02-03 22:44:14 +01003401/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003402/* Generators */
3403/****************************************************************/
3404
3405psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3406{
3407 psa_status_t status = PSA_SUCCESS;
3408 if( generator->alg == 0 )
3409 {
3410 /* The object has (apparently) been initialized but it is not
3411 * in use. It's ok to call abort on such an object, and there's
3412 * nothing to do. */
3413 }
3414 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003415 if( generator->alg == PSA_ALG_SELECT_RAW )
3416 {
3417 if( generator->ctx.buffer.data != NULL )
3418 {
3419 mbedtls_zeroize( generator->ctx.buffer.data,
3420 generator->ctx.buffer.size );
3421 mbedtls_free( generator->ctx.buffer.data );
3422 }
3423 }
3424 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003425#if defined(MBEDTLS_MD_C)
3426 if( PSA_ALG_IS_HKDF( generator->alg ) )
3427 {
3428 mbedtls_free( generator->ctx.hkdf.info );
3429 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3430 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003431 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3432 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3433 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003434 {
3435 if( generator->ctx.tls12_prf.key != NULL )
3436 {
3437 mbedtls_zeroize( generator->ctx.tls12_prf.key,
3438 generator->ctx.tls12_prf.key_len );
3439 mbedtls_free( generator->ctx.tls12_prf.key );
3440 }
Hanno Becker580fba12018-11-13 20:50:45 +00003441
3442 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3443 {
3444 mbedtls_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
3445 generator->ctx.tls12_prf.Ai_with_seed_len );
3446 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3447 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003448 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003449 else
3450#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003451 {
3452 status = PSA_ERROR_BAD_STATE;
3453 }
3454 memset( generator, 0, sizeof( *generator ) );
3455 return( status );
3456}
3457
3458
3459psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3460 size_t *capacity)
3461{
3462 *capacity = generator->capacity;
3463 return( PSA_SUCCESS );
3464}
3465
Gilles Peskinebef7f142018-07-12 17:22:21 +02003466#if defined(MBEDTLS_MD_C)
3467/* Read some bytes from an HKDF-based generator. This performs a chunk
3468 * of the expand phase of the HKDF algorithm. */
3469static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3470 psa_algorithm_t hash_alg,
3471 uint8_t *output,
3472 size_t output_length )
3473{
3474 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3475 psa_status_t status;
3476
3477 while( output_length != 0 )
3478 {
3479 /* Copy what remains of the current block */
3480 uint8_t n = hash_length - hkdf->offset_in_block;
3481 if( n > output_length )
3482 n = (uint8_t) output_length;
3483 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3484 output += n;
3485 output_length -= n;
3486 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003487 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003488 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003489 /* We can't be wanting more output after block 0xff, otherwise
3490 * the capacity check in psa_generator_read() would have
3491 * prevented this call. It could happen only if the generator
3492 * object was corrupted or if this function is called directly
3493 * inside the library. */
3494 if( hkdf->block_number == 0xff )
3495 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003496
3497 /* We need a new block */
3498 ++hkdf->block_number;
3499 hkdf->offset_in_block = 0;
3500 status = psa_hmac_setup_internal( &hkdf->hmac,
3501 hkdf->prk, hash_length,
3502 hash_alg );
3503 if( status != PSA_SUCCESS )
3504 return( status );
3505 if( hkdf->block_number != 1 )
3506 {
3507 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3508 hkdf->output_block,
3509 hash_length );
3510 if( status != PSA_SUCCESS )
3511 return( status );
3512 }
3513 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3514 hkdf->info,
3515 hkdf->info_length );
3516 if( status != PSA_SUCCESS )
3517 return( status );
3518 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3519 &hkdf->block_number, 1 );
3520 if( status != PSA_SUCCESS )
3521 return( status );
3522 status = psa_hmac_finish_internal( &hkdf->hmac,
3523 hkdf->output_block,
3524 sizeof( hkdf->output_block ) );
3525 if( status != PSA_SUCCESS )
3526 return( status );
3527 }
3528
3529 return( PSA_SUCCESS );
3530}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003531
3532static psa_status_t psa_generator_tls12_prf_generate_next_block(
3533 psa_tls12_prf_generator_t *tls12_prf,
3534 psa_algorithm_t alg )
3535{
3536 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3537 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3538 psa_hmac_internal_data hmac;
3539 psa_status_t status, cleanup_status;
3540
Hanno Becker3b339e22018-11-13 20:56:14 +00003541 unsigned char *Ai;
3542 size_t Ai_len;
3543
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003544 /* We can't be wanting more output after block 0xff, otherwise
3545 * the capacity check in psa_generator_read() would have
3546 * prevented this call. It could happen only if the generator
3547 * object was corrupted or if this function is called directly
3548 * inside the library. */
3549 if( tls12_prf->block_number == 0xff )
3550 return( PSA_ERROR_BAD_STATE );
3551
3552 /* We need a new block */
3553 ++tls12_prf->block_number;
3554 tls12_prf->offset_in_block = 0;
3555
3556 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3557 *
3558 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3559 *
3560 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3561 * HMAC_hash(secret, A(2) + seed) +
3562 * HMAC_hash(secret, A(3) + seed) + ...
3563 *
3564 * A(0) = seed
3565 * A(i) = HMAC_hash( secret, A(i-1) )
3566 *
3567 * The `psa_tls12_prf_generator` structures saves the block
3568 * `HMAC_hash(secret, A(i) + seed)` from which the output
3569 * is currently extracted as `output_block`, while
3570 * `A(i) + seed` is stored in `Ai_with_seed`.
3571 *
3572 * Generating a new block means recalculating `Ai_with_seed`
3573 * from the A(i)-part of it, and afterwards recalculating
3574 * `output_block`.
3575 *
3576 * A(0) is computed at setup time.
3577 *
3578 */
3579
3580 psa_hmac_init_internal( &hmac );
3581
3582 /* We must distinguish the calculation of A(1) from those
3583 * of A(2) and higher, because A(0)=seed has a different
3584 * length than the other A(i). */
3585 if( tls12_prf->block_number == 1 )
3586 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003587 Ai = tls12_prf->Ai_with_seed + hash_length;
3588 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003589 }
3590 else
3591 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003592 Ai = tls12_prf->Ai_with_seed;
3593 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003594 }
3595
Hanno Becker3b339e22018-11-13 20:56:14 +00003596 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3597 status = psa_hmac_setup_internal( &hmac,
3598 tls12_prf->key,
3599 tls12_prf->key_len,
3600 hash_alg );
3601 if( status != PSA_SUCCESS )
3602 goto cleanup;
3603
3604 status = psa_hash_update( &hmac.hash_ctx,
3605 Ai, Ai_len );
3606 if( status != PSA_SUCCESS )
3607 goto cleanup;
3608
3609 status = psa_hmac_finish_internal( &hmac,
3610 tls12_prf->Ai_with_seed,
3611 hash_length );
3612 if( status != PSA_SUCCESS )
3613 goto cleanup;
3614
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003615 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3616 status = psa_hmac_setup_internal( &hmac,
3617 tls12_prf->key,
3618 tls12_prf->key_len,
3619 hash_alg );
3620 if( status != PSA_SUCCESS )
3621 goto cleanup;
3622
3623 status = psa_hash_update( &hmac.hash_ctx,
3624 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003625 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003626 if( status != PSA_SUCCESS )
3627 goto cleanup;
3628
3629 status = psa_hmac_finish_internal( &hmac,
3630 tls12_prf->output_block,
3631 hash_length );
3632 if( status != PSA_SUCCESS )
3633 goto cleanup;
3634
3635cleanup:
3636
3637 cleanup_status = psa_hmac_abort_internal( &hmac );
3638 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3639 status = cleanup_status;
3640
3641 return( status );
3642}
3643
3644/* Read some bytes from an TLS-1.2-PRF-based generator.
3645 * See Section 5 of RFC 5246. */
3646static psa_status_t psa_generator_tls12_prf_read(
3647 psa_tls12_prf_generator_t *tls12_prf,
3648 psa_algorithm_t alg,
3649 uint8_t *output,
3650 size_t output_length )
3651{
3652 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3653 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3654 psa_status_t status;
3655
3656 while( output_length != 0 )
3657 {
3658 /* Copy what remains of the current block */
3659 uint8_t n = hash_length - tls12_prf->offset_in_block;
3660
3661 /* Check if we have fully processed the current block. */
3662 if( n == 0 )
3663 {
3664 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3665 alg );
3666 if( status != PSA_SUCCESS )
3667 return( status );
3668
3669 continue;
3670 }
3671
3672 if( n > output_length )
3673 n = (uint8_t) output_length;
3674 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3675 n );
3676 output += n;
3677 output_length -= n;
3678 tls12_prf->offset_in_block += n;
3679 }
3680
3681 return( PSA_SUCCESS );
3682}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003683#endif /* MBEDTLS_MD_C */
3684
Gilles Peskineeab56e42018-07-12 17:12:33 +02003685psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3686 uint8_t *output,
3687 size_t output_length )
3688{
3689 psa_status_t status;
3690
3691 if( output_length > generator->capacity )
3692 {
3693 generator->capacity = 0;
3694 /* Go through the error path to wipe all confidential data now
3695 * that the generator object is useless. */
3696 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3697 goto exit;
3698 }
3699 if( output_length == 0 &&
3700 generator->capacity == 0 && generator->alg == 0 )
3701 {
3702 /* Edge case: this is a blank or finished generator, and 0
3703 * bytes were requested. The right error in this case could
3704 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3705 * INSUFFICIENT_CAPACITY, which is right for a finished
3706 * generator, for consistency with the case when
3707 * output_length > 0. */
3708 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3709 }
3710 generator->capacity -= output_length;
3711
Gilles Peskine751d9652018-09-18 12:05:44 +02003712 if( generator->alg == PSA_ALG_SELECT_RAW )
3713 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003714 /* Initially, the capacity of a selection generator is always
3715 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3716 * abbreviated in this comment as `size`. When the remaining
3717 * capacity is `c`, the next bytes to serve start `c` bytes
3718 * from the end of the buffer, i.e. `size - c` from the
3719 * beginning of the buffer. Since `generator->capacity` was just
3720 * decremented above, we need to serve the bytes from
3721 * `size - generator->capacity - output_length` to
3722 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003723 size_t offset =
3724 generator->ctx.buffer.size - generator->capacity - output_length;
3725 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3726 status = PSA_SUCCESS;
3727 }
3728 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003729#if defined(MBEDTLS_MD_C)
3730 if( PSA_ALG_IS_HKDF( generator->alg ) )
3731 {
3732 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3733 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3734 output, output_length );
3735 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003736 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3737 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003738 {
3739 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3740 generator->alg, output,
3741 output_length );
3742 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003743 else
3744#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003745 {
3746 return( PSA_ERROR_BAD_STATE );
3747 }
3748
3749exit:
3750 if( status != PSA_SUCCESS )
3751 {
3752 psa_generator_abort( generator );
3753 memset( output, '!', output_length );
3754 }
3755 return( status );
3756}
3757
Gilles Peskine08542d82018-07-19 17:05:42 +02003758#if defined(MBEDTLS_DES_C)
3759static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3760{
3761 if( data_size >= 8 )
3762 mbedtls_des_key_set_parity( data );
3763 if( data_size >= 16 )
3764 mbedtls_des_key_set_parity( data + 8 );
3765 if( data_size >= 24 )
3766 mbedtls_des_key_set_parity( data + 16 );
3767}
3768#endif /* MBEDTLS_DES_C */
3769
Gilles Peskineeab56e42018-07-12 17:12:33 +02003770psa_status_t psa_generator_import_key( psa_key_slot_t key,
3771 psa_key_type_t type,
3772 size_t bits,
3773 psa_crypto_generator_t *generator )
3774{
3775 uint8_t *data = NULL;
3776 size_t bytes = PSA_BITS_TO_BYTES( bits );
3777 psa_status_t status;
3778
3779 if( ! key_type_is_raw_bytes( type ) )
3780 return( PSA_ERROR_INVALID_ARGUMENT );
3781 if( bits % 8 != 0 )
3782 return( PSA_ERROR_INVALID_ARGUMENT );
3783 data = mbedtls_calloc( 1, bytes );
3784 if( data == NULL )
3785 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3786
3787 status = psa_generator_read( generator, data, bytes );
3788 if( status != PSA_SUCCESS )
3789 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003790#if defined(MBEDTLS_DES_C)
3791 if( type == PSA_KEY_TYPE_DES )
3792 psa_des_set_key_parity( data, bytes );
3793#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003794 status = psa_import_key( key, type, data, bytes );
3795
3796exit:
3797 mbedtls_free( data );
3798 return( status );
3799}
3800
3801
3802
3803/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003804/* Key derivation */
3805/****************************************************************/
3806
Gilles Peskinea05219c2018-11-16 16:02:56 +01003807#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003808/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003809 * of the HKDF algorithm.
3810 *
3811 * Note that if this function fails, you must call psa_generator_abort()
3812 * to potentially free embedded data structures and wipe confidential data.
3813 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003814static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003815 const uint8_t *secret,
3816 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003817 psa_algorithm_t hash_alg,
3818 const uint8_t *salt,
3819 size_t salt_length,
3820 const uint8_t *label,
3821 size_t label_length )
3822{
3823 psa_status_t status;
3824 status = psa_hmac_setup_internal( &hkdf->hmac,
3825 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003826 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003827 if( status != PSA_SUCCESS )
3828 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003829 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003830 if( status != PSA_SUCCESS )
3831 return( status );
3832 status = psa_hmac_finish_internal( &hkdf->hmac,
3833 hkdf->prk,
3834 sizeof( hkdf->prk ) );
3835 if( status != PSA_SUCCESS )
3836 return( status );
3837 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3838 hkdf->block_number = 0;
3839 hkdf->info_length = label_length;
3840 if( label_length != 0 )
3841 {
3842 hkdf->info = mbedtls_calloc( 1, label_length );
3843 if( hkdf->info == NULL )
3844 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3845 memcpy( hkdf->info, label, label_length );
3846 }
3847 return( PSA_SUCCESS );
3848}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003849#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003850
Gilles Peskinea05219c2018-11-16 16:02:56 +01003851#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003852/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3853 *
3854 * Note that if this function fails, you must call psa_generator_abort()
3855 * to potentially free embedded data structures and wipe confidential data.
3856 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003857static psa_status_t psa_generator_tls12_prf_setup(
3858 psa_tls12_prf_generator_t *tls12_prf,
3859 const unsigned char *key,
3860 size_t key_len,
3861 psa_algorithm_t hash_alg,
3862 const uint8_t *salt,
3863 size_t salt_length,
3864 const uint8_t *label,
3865 size_t label_length )
3866{
3867 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003868 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3869 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003870
3871 tls12_prf->key = mbedtls_calloc( 1, key_len );
3872 if( tls12_prf->key == NULL )
3873 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3874 tls12_prf->key_len = key_len;
3875 memcpy( tls12_prf->key, key, key_len );
3876
Hanno Becker580fba12018-11-13 20:50:45 +00003877 overflow = ( salt_length + label_length < salt_length ) ||
3878 ( salt_length + label_length + hash_length < hash_length );
3879 if( overflow )
3880 return( PSA_ERROR_INVALID_ARGUMENT );
3881
3882 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3883 if( tls12_prf->Ai_with_seed == NULL )
3884 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3885 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3886
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003887 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3888 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003889 if( label_length != 0 )
3890 {
3891 memcpy( tls12_prf->Ai_with_seed + hash_length,
3892 label, label_length );
3893 }
3894
3895 if( salt_length != 0 )
3896 {
3897 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3898 salt, salt_length );
3899 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003900
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003901 /* The first block gets generated when
3902 * psa_generator_read() is called. */
3903 tls12_prf->block_number = 0;
3904 tls12_prf->offset_in_block = hash_length;
3905
3906 return( PSA_SUCCESS );
3907}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003908
3909/* Set up a TLS-1.2-PSK-to-MS-based generator. */
3910static psa_status_t psa_generator_tls12_psk_to_ms_setup(
3911 psa_tls12_prf_generator_t *tls12_prf,
3912 const unsigned char *psk,
3913 size_t psk_len,
3914 psa_algorithm_t hash_alg,
3915 const uint8_t *salt,
3916 size_t salt_length,
3917 const uint8_t *label,
3918 size_t label_length )
3919{
3920 psa_status_t status;
3921 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
3922
3923 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
3924 return( PSA_ERROR_INVALID_ARGUMENT );
3925
3926 /* Quoting RFC 4279, Section 2:
3927 *
3928 * The premaster secret is formed as follows: if the PSK is N octets
3929 * long, concatenate a uint16 with the value N, N zero octets, a second
3930 * uint16 with the value N, and the PSK itself.
3931 */
3932
3933 pms[0] = ( psk_len >> 8 ) & 0xff;
3934 pms[1] = ( psk_len >> 0 ) & 0xff;
3935 memset( pms + 2, 0, psk_len );
3936 pms[2 + psk_len + 0] = pms[0];
3937 pms[2 + psk_len + 1] = pms[1];
3938 memcpy( pms + 4 + psk_len, psk, psk_len );
3939
3940 status = psa_generator_tls12_prf_setup( tls12_prf,
3941 pms, 4 + 2 * psk_len,
3942 hash_alg,
3943 salt, salt_length,
3944 label, label_length );
3945
3946 mbedtls_zeroize( pms, sizeof( pms ) );
3947 return( status );
3948}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003949#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003950
Gilles Peskine346797d2018-11-16 16:05:06 +01003951/* Note that if this function fails, you must call psa_generator_abort()
3952 * to potentially free embedded data structures and wipe confidential data.
3953 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003954static psa_status_t psa_key_derivation_internal(
3955 psa_crypto_generator_t *generator,
3956 const uint8_t *secret, size_t secret_length,
3957 psa_algorithm_t alg,
3958 const uint8_t *salt, size_t salt_length,
3959 const uint8_t *label, size_t label_length,
3960 size_t capacity )
3961{
3962 psa_status_t status;
3963 size_t max_capacity;
3964
3965 /* Set generator->alg even on failure so that abort knows what to do. */
3966 generator->alg = alg;
3967
Gilles Peskine751d9652018-09-18 12:05:44 +02003968 if( alg == PSA_ALG_SELECT_RAW )
3969 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003970 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02003971 if( salt_length != 0 )
3972 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01003973 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02003974 if( label_length != 0 )
3975 return( PSA_ERROR_INVALID_ARGUMENT );
3976 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
3977 if( generator->ctx.buffer.data == NULL )
3978 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3979 memcpy( generator->ctx.buffer.data, secret, secret_length );
3980 generator->ctx.buffer.size = secret_length;
3981 max_capacity = secret_length;
3982 status = PSA_SUCCESS;
3983 }
3984 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003985#if defined(MBEDTLS_MD_C)
3986 if( PSA_ALG_IS_HKDF( alg ) )
3987 {
3988 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3989 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3990 if( hash_size == 0 )
3991 return( PSA_ERROR_NOT_SUPPORTED );
3992 max_capacity = 255 * hash_size;
3993 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3994 secret, secret_length,
3995 hash_alg,
3996 salt, salt_length,
3997 label, label_length );
3998 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003999 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4000 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4001 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004002 {
4003 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4004 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4005
4006 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4007 if( hash_alg != PSA_ALG_SHA_256 &&
4008 hash_alg != PSA_ALG_SHA_384 )
4009 {
4010 return( PSA_ERROR_NOT_SUPPORTED );
4011 }
4012
4013 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004014
4015 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4016 {
4017 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4018 secret, secret_length,
4019 hash_alg, salt, salt_length,
4020 label, label_length );
4021 }
4022 else
4023 {
4024 status = psa_generator_tls12_psk_to_ms_setup(
4025 &generator->ctx.tls12_prf,
4026 secret, secret_length,
4027 hash_alg, salt, salt_length,
4028 label, label_length );
4029 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004030 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004031 else
4032#endif
4033 {
4034 return( PSA_ERROR_NOT_SUPPORTED );
4035 }
4036
4037 if( status != PSA_SUCCESS )
4038 return( status );
4039
4040 if( capacity <= max_capacity )
4041 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004042 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4043 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004044 else
4045 return( PSA_ERROR_INVALID_ARGUMENT );
4046
4047 return( PSA_SUCCESS );
4048}
4049
Gilles Peskineea0fb492018-07-12 17:17:20 +02004050psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01004051 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004052 psa_algorithm_t alg,
4053 const uint8_t *salt,
4054 size_t salt_length,
4055 const uint8_t *label,
4056 size_t label_length,
4057 size_t capacity )
4058{
4059 key_slot_t *slot;
4060 psa_status_t status;
4061
4062 if( generator->alg != 0 )
4063 return( PSA_ERROR_BAD_STATE );
4064
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004065 /* Make sure that alg is a key derivation algorithm. This prevents
4066 * key selection algorithms, which psa_key_derivation_internal
4067 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004068 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4069 return( PSA_ERROR_INVALID_ARGUMENT );
4070
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004071 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
4072 if( status != PSA_SUCCESS )
4073 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004074
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004075 if( slot->type != PSA_KEY_TYPE_DERIVE )
4076 return( PSA_ERROR_INVALID_ARGUMENT );
4077
4078 status = psa_key_derivation_internal( generator,
4079 slot->data.raw.data,
4080 slot->data.raw.bytes,
4081 alg,
4082 salt, salt_length,
4083 label, label_length,
4084 capacity );
4085 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004086 psa_generator_abort( generator );
4087 return( status );
4088}
4089
4090
4091
4092/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004093/* Key agreement */
4094/****************************************************************/
4095
Gilles Peskinea05219c2018-11-16 16:02:56 +01004096#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004097static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4098 size_t peer_key_length,
4099 const mbedtls_ecp_keypair *our_key,
4100 uint8_t *shared_secret,
4101 size_t shared_secret_size,
4102 size_t *shared_secret_length )
4103{
4104 mbedtls_pk_context pk;
4105 mbedtls_ecp_keypair *their_key = NULL;
4106 mbedtls_ecdh_context ecdh;
4107 int ret;
4108 mbedtls_ecdh_init( &ecdh );
4109 mbedtls_pk_init( &pk );
4110
4111 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
4112 if( ret != 0 )
4113 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02004114 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004115 {
Gilles Peskine88714d72018-10-25 23:07:25 +02004116 case MBEDTLS_PK_ECKEY:
4117 case MBEDTLS_PK_ECKEY_DH:
4118 break;
4119 default:
4120 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4121 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004122 }
4123 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01004124 if( their_key->grp.id != our_key->grp.id )
4125 {
4126 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4127 goto exit;
4128 }
4129
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004130 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
4131 if( ret != 0 )
4132 goto exit;
4133 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
4134 if( ret != 0 )
4135 goto exit;
4136
4137 ret = mbedtls_ecdh_calc_secret( &ecdh,
4138 shared_secret_length,
4139 shared_secret, shared_secret_size,
4140 mbedtls_ctr_drbg_random,
4141 &global_data.ctr_drbg );
4142
4143exit:
4144 mbedtls_pk_free( &pk );
4145 mbedtls_ecdh_free( &ecdh );
4146 return( mbedtls_to_psa_error( ret ) );
4147}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004148#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004149
Gilles Peskine01d718c2018-09-18 12:01:02 +02004150#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4151
Gilles Peskine346797d2018-11-16 16:05:06 +01004152/* Note that if this function fails, you must call psa_generator_abort()
4153 * to potentially free embedded data structures and wipe confidential data.
4154 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004155static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
4156 key_slot_t *private_key,
4157 const uint8_t *peer_key,
4158 size_t peer_key_length,
4159 psa_algorithm_t alg )
4160{
4161 psa_status_t status;
4162 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4163 size_t shared_secret_length = 0;
4164
4165 /* Step 1: run the secret agreement algorithm to generate the shared
4166 * secret. */
4167 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4168 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004169#if defined(MBEDTLS_ECDH_C)
4170 case PSA_ALG_ECDH_BASE:
4171 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4172 return( PSA_ERROR_INVALID_ARGUMENT );
4173 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4174 private_key->data.ecp,
4175 shared_secret,
4176 sizeof( shared_secret ),
4177 &shared_secret_length );
4178 break;
4179#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004180 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004181 (void) private_key;
4182 (void) peer_key;
4183 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004184 return( PSA_ERROR_NOT_SUPPORTED );
4185 }
4186 if( status != PSA_SUCCESS )
4187 goto exit;
4188
4189 /* Step 2: set up the key derivation to generate key material from
4190 * the shared secret. */
4191 status = psa_key_derivation_internal( generator,
4192 shared_secret, shared_secret_length,
4193 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4194 NULL, 0, NULL, 0,
4195 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4196exit:
4197 mbedtls_zeroize( shared_secret, shared_secret_length );
4198 return( status );
4199}
4200
4201psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
4202 psa_key_slot_t private_key,
4203 const uint8_t *peer_key,
4204 size_t peer_key_length,
4205 psa_algorithm_t alg )
4206{
4207 key_slot_t *slot;
4208 psa_status_t status;
4209 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4210 return( PSA_ERROR_INVALID_ARGUMENT );
4211 status = psa_get_key_from_slot( private_key, &slot,
4212 PSA_KEY_USAGE_DERIVE, alg );
4213 if( status != PSA_SUCCESS )
4214 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004215 status = psa_key_agreement_internal( generator,
4216 slot,
4217 peer_key, peer_key_length,
4218 alg );
4219 if( status != PSA_SUCCESS )
4220 psa_generator_abort( generator );
4221 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004222}
4223
4224
4225
4226/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004227/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004228/****************************************************************/
4229
4230psa_status_t psa_generate_random( uint8_t *output,
4231 size_t output_size )
4232{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004233 int ret;
4234 GUARD_MODULE_INITIALIZED;
4235
4236 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004237 return( mbedtls_to_psa_error( ret ) );
4238}
4239
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004240#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004241
4242/* Support function for error conversion between psa_its error codes to psa crypto */
4243static psa_status_t its_to_psa_error( psa_its_status_t ret )
4244{
4245 switch( ret )
4246 {
4247 case PSA_ITS_SUCCESS:
4248 return( PSA_SUCCESS );
4249
4250 case PSA_ITS_ERROR_KEY_NOT_FOUND:
4251 return( PSA_ERROR_EMPTY_SLOT );
4252
4253 case PSA_ITS_ERROR_STORAGE_FAILURE:
4254 return( PSA_ERROR_STORAGE_FAILURE );
4255
4256 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4257 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4258
4259 case PSA_ITS_ERROR_INVALID_KEY:
4260 case PSA_PS_ERROR_OFFSET_INVALID:
4261 case PSA_ITS_ERROR_INCORRECT_SIZE:
4262 case PSA_ITS_ERROR_BAD_POINTER:
4263 return( PSA_ERROR_INVALID_ARGUMENT );
4264
4265 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4266 return( PSA_ERROR_NOT_SUPPORTED );
4267
4268 case PSA_ITS_ERROR_WRITE_ONCE:
4269 return( PSA_ERROR_OCCUPIED_SLOT );
4270
4271 default:
4272 return( PSA_ERROR_UNKNOWN_ERROR );
4273 }
4274}
4275
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004276psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4277 size_t seed_size )
4278{
4279 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004280 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004281 struct psa_its_info_t p_info;
4282 if( global_data.initialized )
4283 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004284
4285 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4286 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4287 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4288 return( PSA_ERROR_INVALID_ARGUMENT );
4289
avolinski0d2c2662018-11-21 17:31:07 +02004290 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004291 status = its_to_psa_error( its_status );
4292
4293 if( PSA_ITS_ERROR_KEY_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004294 {
avolinski0d2c2662018-11-21 17:31:07 +02004295 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004296 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004297 }
avolinski13beb102018-11-20 16:51:49 +02004298 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004299 {
4300 /* You should not be here. Seed needs to be injected only once */
4301 status = PSA_ERROR_NOT_PERMITTED;
4302 }
4303 return( status );
4304}
4305#endif
4306
Gilles Peskine05d69892018-06-19 22:00:52 +02004307psa_status_t psa_generate_key( psa_key_slot_t key,
4308 psa_key_type_t type,
4309 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004310 const void *extra,
4311 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004312{
Gilles Peskine12313cd2018-06-20 00:20:32 +02004313 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004314 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004315
Gilles Peskine53d991e2018-07-12 01:14:59 +02004316 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004317 return( PSA_ERROR_INVALID_ARGUMENT );
4318
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004319 status = psa_get_empty_key_slot( key, &slot );
4320 if( status != PSA_SUCCESS )
4321 return( status );
4322
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004323 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004324 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004325 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004326 if( status != PSA_SUCCESS )
4327 return( status );
4328 status = psa_generate_random( slot->data.raw.data,
4329 slot->data.raw.bytes );
4330 if( status != PSA_SUCCESS )
4331 {
4332 mbedtls_free( slot->data.raw.data );
4333 return( status );
4334 }
4335#if defined(MBEDTLS_DES_C)
4336 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004337 psa_des_set_key_parity( slot->data.raw.data,
4338 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004339#endif /* MBEDTLS_DES_C */
4340 }
4341 else
4342
4343#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4344 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4345 {
4346 mbedtls_rsa_context *rsa;
4347 int ret;
4348 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004349 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4350 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004351 /* Accept only byte-aligned keys, for the same reasons as
4352 * in psa_import_rsa_key(). */
4353 if( bits % 8 != 0 )
4354 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004355 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004356 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004357 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004358 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004359 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004360#if INT_MAX < 0xffffffff
4361 /* Check that the uint32_t value passed by the caller fits
4362 * in the range supported by this implementation. */
4363 if( p->e > INT_MAX )
4364 return( PSA_ERROR_NOT_SUPPORTED );
4365#endif
4366 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004367 }
4368 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4369 if( rsa == NULL )
4370 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4371 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4372 ret = mbedtls_rsa_gen_key( rsa,
4373 mbedtls_ctr_drbg_random,
4374 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004375 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004376 exponent );
4377 if( ret != 0 )
4378 {
4379 mbedtls_rsa_free( rsa );
4380 mbedtls_free( rsa );
4381 return( mbedtls_to_psa_error( ret ) );
4382 }
4383 slot->data.rsa = rsa;
4384 }
4385 else
4386#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4387
4388#if defined(MBEDTLS_ECP_C)
4389 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4390 {
4391 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4392 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4393 const mbedtls_ecp_curve_info *curve_info =
4394 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4395 mbedtls_ecp_keypair *ecp;
4396 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004397 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004398 return( PSA_ERROR_NOT_SUPPORTED );
4399 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4400 return( PSA_ERROR_NOT_SUPPORTED );
4401 if( curve_info->bit_size != bits )
4402 return( PSA_ERROR_INVALID_ARGUMENT );
4403 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4404 if( ecp == NULL )
4405 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4406 mbedtls_ecp_keypair_init( ecp );
4407 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4408 mbedtls_ctr_drbg_random,
4409 &global_data.ctr_drbg );
4410 if( ret != 0 )
4411 {
4412 mbedtls_ecp_keypair_free( ecp );
4413 mbedtls_free( ecp );
4414 return( mbedtls_to_psa_error( ret ) );
4415 }
4416 slot->data.ecp = ecp;
4417 }
4418 else
4419#endif /* MBEDTLS_ECP_C */
4420
4421 return( PSA_ERROR_NOT_SUPPORTED );
4422
4423 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004424
4425#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4426 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4427 {
4428 return( psa_save_generated_persistent_key( key, slot, bits ) );
4429 }
4430#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4431
4432 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004433}
4434
4435
4436/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004437/* Module setup */
4438/****************************************************************/
4439
Gilles Peskinee59236f2018-01-27 23:32:46 +01004440void mbedtls_psa_crypto_free( void )
4441{
Jaeden Amero045bd502018-06-26 14:00:08 +01004442 psa_key_slot_t key;
Darryl Green40225ba2018-11-15 14:48:15 +00004443 key_slot_t *slot;
4444 psa_status_t status;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004445 if( global_data.key_slots_initialized )
Darryl Green40225ba2018-11-15 14:48:15 +00004446 {
Gilles Peskinec6b69072018-11-20 21:42:52 +01004447 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
4448 {
4449 status = psa_get_key_slot( key, &slot );
4450 if( status != PSA_SUCCESS )
4451 continue;
4452 psa_remove_key_data_from_memory( slot );
4453 /* Zeroize the slot to wipe metadata such as policies. */
4454 mbedtls_zeroize( slot, sizeof( *slot ) );
4455 }
Darryl Green40225ba2018-11-15 14:48:15 +00004456 }
Gilles Peskinec6b69072018-11-20 21:42:52 +01004457 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4458 {
4459 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
4460 mbedtls_entropy_free( &global_data.entropy );
4461 }
4462 /* Wipe all remaining data, including configuration.
4463 * In particular, this sets all state indicator to the value
4464 * indicating "uninitialized". */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004465 mbedtls_zeroize( &global_data, sizeof( global_data ) );
4466}
4467
4468psa_status_t psa_crypto_init( void )
4469{
4470 int ret;
4471 const unsigned char drbg_seed[] = "PSA";
4472
Gilles Peskinec6b69072018-11-20 21:42:52 +01004473 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004474 if( global_data.initialized != 0 )
4475 return( PSA_SUCCESS );
4476
4477 mbedtls_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004478
4479 /* Initialize the random generator. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004480 mbedtls_entropy_init( &global_data.entropy );
4481 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004482 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004483 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4484 mbedtls_entropy_func,
4485 &global_data.entropy,
4486 drbg_seed, sizeof( drbg_seed ) - 1 );
4487 if( ret != 0 )
4488 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004489 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004490
Gilles Peskinec6b69072018-11-20 21:42:52 +01004491 /* Initialize the key slots. Zero-initialization has made all key
4492 * slots empty, so there is nothing to do. In a future version we will
4493 * load data from storage. */
4494 global_data.key_slots_initialized = 1;
4495
4496 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004497 global_data.initialized = 1;
4498
Gilles Peskinee59236f2018-01-27 23:32:46 +01004499exit:
4500 if( ret != 0 )
4501 mbedtls_psa_crypto_free( );
4502 return( mbedtls_to_psa_error( ret ) );
4503}
4504
4505#endif /* MBEDTLS_PSA_CRYPTO_C */