blob: f3eeddeeb8a259a8a677faf3f884bfc208a0e8b8 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030029/*
30 * In case MBEDTLS_PSA_CRYPTO_SPM is defined the code is built for SPM (Secure
31 * Partition Manager) integration which separate the code into two parts
32 * NSPE (Non-Secure Process Environment) and SPE (Secure Process Environment).
33 * In this mode an additional header file should be included.
34 */
mohammad160327010052018-07-03 13:16:15 +030035#if defined(MBEDTLS_PSA_CRYPTO_SPM)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030036/*
37 * PSA_CRYPTO_SECURE means that this file is compiled to the SPE side.
38 * some headers will be affected by this flag.
39 */
mohammad160327010052018-07-03 13:16:15 +030040#define PSA_CRYPTO_SECURE 1
41#include "crypto_spe.h"
42#endif
43
Gilles Peskinee59236f2018-01-27 23:32:46 +010044#include "psa/crypto.h"
45
Gilles Peskine039b90c2018-12-07 18:24:41 +010046#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010047#include "psa_crypto_invasive.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010048#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010049/* Include internal declarations that are useful for implementing persistently
50 * stored keys. */
51#include "psa_crypto_storage.h"
52
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#include <stdlib.h>
54#include <string.h>
55#if defined(MBEDTLS_PLATFORM_C)
56#include "mbedtls/platform.h"
57#else
58#define mbedtls_calloc calloc
59#define mbedtls_free free
60#endif
61
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020063#include "mbedtls/asn1.h"
Jaeden Amero6b196002019-01-10 10:23:21 +000064#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020065#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/blowfish.h"
67#include "mbedtls/camellia.h"
68#include "mbedtls/cipher.h"
69#include "mbedtls/ccm.h"
70#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010071#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010072#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020073#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010074#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010075#include "mbedtls/entropy.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020076#include "mbedtls/entropy_poll.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010077#include "mbedtls/error.h"
78#include "mbedtls/gcm.h"
79#include "mbedtls/md2.h"
80#include "mbedtls/md4.h"
81#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010082#include "mbedtls/md.h"
83#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010084#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010085#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010086#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010087#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010088#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010089#include "mbedtls/sha1.h"
90#include "mbedtls/sha256.h"
91#include "mbedtls/sha512.h"
92#include "mbedtls/xtea.h"
93
Netanel Gonen2bcd3122018-11-19 11:53:02 +020094#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
95#include "psa_prot_internal_storage.h"
96#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +010097
Gilles Peskine996deb12018-08-01 15:45:45 +020098#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
99
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100100/* constant-time buffer comparison */
101static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
102{
103 size_t i;
104 unsigned char diff = 0;
105
106 for( i = 0; i < n; i++ )
107 diff |= a[i] ^ b[i];
108
109 return( diff );
110}
111
112
113
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100114/****************************************************************/
115/* Global data, support functions and library management */
116/****************************************************************/
117
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200118static int key_type_is_raw_bytes( psa_key_type_t type )
119{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200120 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200121}
122
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100123/* Values for psa_global_data_t::rng_state */
124#define RNG_NOT_INITIALIZED 0
125#define RNG_INITIALIZED 1
126#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100127
Gilles Peskine2d277862018-06-18 15:41:12 +0200128typedef struct
129{
Gilles Peskine5e769522018-11-20 21:59:56 +0100130 void (* entropy_init )( mbedtls_entropy_context *ctx );
131 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100132 mbedtls_entropy_context entropy;
133 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100134 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100135 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100136} psa_global_data_t;
137
138static psa_global_data_t global_data;
139
itayzafrir0adf0fc2018-09-06 16:24:41 +0300140#define GUARD_MODULE_INITIALIZED \
141 if( global_data.initialized == 0 ) \
142 return( PSA_ERROR_BAD_STATE );
143
Gilles Peskinee59236f2018-01-27 23:32:46 +0100144static psa_status_t mbedtls_to_psa_error( int ret )
145{
Gilles Peskinea5905292018-02-07 20:59:33 +0100146 /* If there's both a high-level code and low-level code, dispatch on
147 * the high-level code. */
148 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100149 {
150 case 0:
151 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100152
153 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
154 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
155 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
156 return( PSA_ERROR_NOT_SUPPORTED );
157 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
158 return( PSA_ERROR_HARDWARE_FAILURE );
159
160 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
161 return( PSA_ERROR_HARDWARE_FAILURE );
162
Gilles Peskine9a944802018-06-21 09:35:35 +0200163 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
164 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
165 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
166 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
167 case MBEDTLS_ERR_ASN1_INVALID_DATA:
168 return( PSA_ERROR_INVALID_ARGUMENT );
169 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
170 return( PSA_ERROR_INSUFFICIENT_MEMORY );
171 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
172 return( PSA_ERROR_BUFFER_TOO_SMALL );
173
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
175 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
176 return( PSA_ERROR_NOT_SUPPORTED );
177 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
178 return( PSA_ERROR_HARDWARE_FAILURE );
179
180 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
181 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
182 return( PSA_ERROR_NOT_SUPPORTED );
183 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
184 return( PSA_ERROR_HARDWARE_FAILURE );
185
186 case MBEDTLS_ERR_CCM_BAD_INPUT:
187 return( PSA_ERROR_INVALID_ARGUMENT );
188 case MBEDTLS_ERR_CCM_AUTH_FAILED:
189 return( PSA_ERROR_INVALID_SIGNATURE );
190 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
191 return( PSA_ERROR_HARDWARE_FAILURE );
192
193 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
194 return( PSA_ERROR_NOT_SUPPORTED );
195 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
196 return( PSA_ERROR_INVALID_ARGUMENT );
197 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
198 return( PSA_ERROR_INSUFFICIENT_MEMORY );
199 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
200 return( PSA_ERROR_INVALID_PADDING );
201 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
202 return( PSA_ERROR_BAD_STATE );
203 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
204 return( PSA_ERROR_INVALID_SIGNATURE );
205 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
206 return( PSA_ERROR_TAMPERING_DETECTED );
207 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
208 return( PSA_ERROR_HARDWARE_FAILURE );
209
210 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
214 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
215 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
216 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
217 return( PSA_ERROR_NOT_SUPPORTED );
218 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
219 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
220
221 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
222 return( PSA_ERROR_NOT_SUPPORTED );
223 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
224 return( PSA_ERROR_HARDWARE_FAILURE );
225
Gilles Peskinee59236f2018-01-27 23:32:46 +0100226 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
227 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
228 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
229 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100230
231 case MBEDTLS_ERR_GCM_AUTH_FAILED:
232 return( PSA_ERROR_INVALID_SIGNATURE );
233 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200234 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100235 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
236 return( PSA_ERROR_HARDWARE_FAILURE );
237
238 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
239 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
240 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
241 return( PSA_ERROR_HARDWARE_FAILURE );
242
243 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
244 return( PSA_ERROR_NOT_SUPPORTED );
245 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
246 return( PSA_ERROR_INVALID_ARGUMENT );
247 case MBEDTLS_ERR_MD_ALLOC_FAILED:
248 return( PSA_ERROR_INSUFFICIENT_MEMORY );
249 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
250 return( PSA_ERROR_STORAGE_FAILURE );
251 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
Gilles Peskinef76aa772018-10-29 19:24:33 +0100254 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
255 return( PSA_ERROR_STORAGE_FAILURE );
256 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
259 return( PSA_ERROR_INVALID_ARGUMENT );
260 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
261 return( PSA_ERROR_BUFFER_TOO_SMALL );
262 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
269 return( PSA_ERROR_INSUFFICIENT_MEMORY );
270
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100271 case MBEDTLS_ERR_PK_ALLOC_FAILED:
272 return( PSA_ERROR_INSUFFICIENT_MEMORY );
273 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
274 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
275 return( PSA_ERROR_INVALID_ARGUMENT );
276 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100277 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100278 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
279 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
280 return( PSA_ERROR_INVALID_ARGUMENT );
281 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
282 return( PSA_ERROR_NOT_SUPPORTED );
283 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
284 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
285 return( PSA_ERROR_NOT_PERMITTED );
286 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
287 return( PSA_ERROR_INVALID_ARGUMENT );
288 case MBEDTLS_ERR_PK_INVALID_ALG:
289 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
290 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
291 return( PSA_ERROR_NOT_SUPPORTED );
292 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
293 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296
297 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
298 return( PSA_ERROR_HARDWARE_FAILURE );
299
300 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
301 return( PSA_ERROR_INVALID_ARGUMENT );
302 case MBEDTLS_ERR_RSA_INVALID_PADDING:
303 return( PSA_ERROR_INVALID_PADDING );
304 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
305 return( PSA_ERROR_HARDWARE_FAILURE );
306 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
307 return( PSA_ERROR_INVALID_ARGUMENT );
308 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
309 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
310 return( PSA_ERROR_TAMPERING_DETECTED );
311 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
312 return( PSA_ERROR_INVALID_SIGNATURE );
313 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
314 return( PSA_ERROR_BUFFER_TOO_SMALL );
315 case MBEDTLS_ERR_RSA_RNG_FAILED:
316 return( PSA_ERROR_INSUFFICIENT_MEMORY );
317 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
318 return( PSA_ERROR_NOT_SUPPORTED );
319 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
320 return( PSA_ERROR_HARDWARE_FAILURE );
321
322 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
323 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
324 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
325 return( PSA_ERROR_HARDWARE_FAILURE );
326
327 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
328 return( PSA_ERROR_INVALID_ARGUMENT );
329 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
330 return( PSA_ERROR_HARDWARE_FAILURE );
331
itayzafrir5c753392018-05-08 11:18:38 +0300332 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300333 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300334 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300335 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
336 return( PSA_ERROR_BUFFER_TOO_SMALL );
337 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
338 return( PSA_ERROR_NOT_SUPPORTED );
339 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
340 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
341 return( PSA_ERROR_INVALID_SIGNATURE );
342 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
343 return( PSA_ERROR_INSUFFICIENT_MEMORY );
344 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
345 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300346
Gilles Peskinee59236f2018-01-27 23:32:46 +0100347 default:
348 return( PSA_ERROR_UNKNOWN_ERROR );
349 }
350}
351
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200352
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200353
354
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100355/****************************************************************/
356/* Key management */
357/****************************************************************/
358
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100359#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200360static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
361{
362 switch( grpid )
363 {
364 case MBEDTLS_ECP_DP_SECP192R1:
365 return( PSA_ECC_CURVE_SECP192R1 );
366 case MBEDTLS_ECP_DP_SECP224R1:
367 return( PSA_ECC_CURVE_SECP224R1 );
368 case MBEDTLS_ECP_DP_SECP256R1:
369 return( PSA_ECC_CURVE_SECP256R1 );
370 case MBEDTLS_ECP_DP_SECP384R1:
371 return( PSA_ECC_CURVE_SECP384R1 );
372 case MBEDTLS_ECP_DP_SECP521R1:
373 return( PSA_ECC_CURVE_SECP521R1 );
374 case MBEDTLS_ECP_DP_BP256R1:
375 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
376 case MBEDTLS_ECP_DP_BP384R1:
377 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
378 case MBEDTLS_ECP_DP_BP512R1:
379 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
380 case MBEDTLS_ECP_DP_CURVE25519:
381 return( PSA_ECC_CURVE_CURVE25519 );
382 case MBEDTLS_ECP_DP_SECP192K1:
383 return( PSA_ECC_CURVE_SECP192K1 );
384 case MBEDTLS_ECP_DP_SECP224K1:
385 return( PSA_ECC_CURVE_SECP224K1 );
386 case MBEDTLS_ECP_DP_SECP256K1:
387 return( PSA_ECC_CURVE_SECP256K1 );
388 case MBEDTLS_ECP_DP_CURVE448:
389 return( PSA_ECC_CURVE_CURVE448 );
390 default:
391 return( 0 );
392 }
393}
394
Gilles Peskine12313cd2018-06-20 00:20:32 +0200395static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
396{
397 switch( curve )
398 {
399 case PSA_ECC_CURVE_SECP192R1:
400 return( MBEDTLS_ECP_DP_SECP192R1 );
401 case PSA_ECC_CURVE_SECP224R1:
402 return( MBEDTLS_ECP_DP_SECP224R1 );
403 case PSA_ECC_CURVE_SECP256R1:
404 return( MBEDTLS_ECP_DP_SECP256R1 );
405 case PSA_ECC_CURVE_SECP384R1:
406 return( MBEDTLS_ECP_DP_SECP384R1 );
407 case PSA_ECC_CURVE_SECP521R1:
408 return( MBEDTLS_ECP_DP_SECP521R1 );
409 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
410 return( MBEDTLS_ECP_DP_BP256R1 );
411 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
412 return( MBEDTLS_ECP_DP_BP384R1 );
413 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
414 return( MBEDTLS_ECP_DP_BP512R1 );
415 case PSA_ECC_CURVE_CURVE25519:
416 return( MBEDTLS_ECP_DP_CURVE25519 );
417 case PSA_ECC_CURVE_SECP192K1:
418 return( MBEDTLS_ECP_DP_SECP192K1 );
419 case PSA_ECC_CURVE_SECP224K1:
420 return( MBEDTLS_ECP_DP_SECP224K1 );
421 case PSA_ECC_CURVE_SECP256K1:
422 return( MBEDTLS_ECP_DP_SECP256K1 );
423 case PSA_ECC_CURVE_CURVE448:
424 return( MBEDTLS_ECP_DP_CURVE448 );
425 default:
426 return( MBEDTLS_ECP_DP_NONE );
427 }
428}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100429#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200430
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200431static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
432 size_t bits,
433 struct raw_data *raw )
434{
435 /* Check that the bit size is acceptable for the key type */
436 switch( type )
437 {
438 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200439 if( bits == 0 )
440 {
441 raw->bytes = 0;
442 raw->data = NULL;
443 return( PSA_SUCCESS );
444 }
445 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200446#if defined(MBEDTLS_MD_C)
447 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200448#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200449 case PSA_KEY_TYPE_DERIVE:
450 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200451#if defined(MBEDTLS_AES_C)
452 case PSA_KEY_TYPE_AES:
453 if( bits != 128 && bits != 192 && bits != 256 )
454 return( PSA_ERROR_INVALID_ARGUMENT );
455 break;
456#endif
457#if defined(MBEDTLS_CAMELLIA_C)
458 case PSA_KEY_TYPE_CAMELLIA:
459 if( bits != 128 && bits != 192 && bits != 256 )
460 return( PSA_ERROR_INVALID_ARGUMENT );
461 break;
462#endif
463#if defined(MBEDTLS_DES_C)
464 case PSA_KEY_TYPE_DES:
465 if( bits != 64 && bits != 128 && bits != 192 )
466 return( PSA_ERROR_INVALID_ARGUMENT );
467 break;
468#endif
469#if defined(MBEDTLS_ARC4_C)
470 case PSA_KEY_TYPE_ARC4:
471 if( bits < 8 || bits > 2048 )
472 return( PSA_ERROR_INVALID_ARGUMENT );
473 break;
474#endif
475 default:
476 return( PSA_ERROR_NOT_SUPPORTED );
477 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200478 if( bits % 8 != 0 )
479 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200480
481 /* Allocate memory for the key */
482 raw->bytes = PSA_BITS_TO_BYTES( bits );
483 raw->data = mbedtls_calloc( 1, raw->bytes );
484 if( raw->data == NULL )
485 {
486 raw->bytes = 0;
487 return( PSA_ERROR_INSUFFICIENT_MEMORY );
488 }
489 return( PSA_SUCCESS );
490}
491
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200492#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100493/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
494 * that are not a multiple of 8) well. For example, there is only
495 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
496 * way to return the exact bit size of a key.
497 * To keep things simple, reject non-byte-aligned key sizes. */
498static psa_status_t psa_check_rsa_key_byte_aligned(
499 const mbedtls_rsa_context *rsa )
500{
501 mbedtls_mpi n;
502 psa_status_t status;
503 mbedtls_mpi_init( &n );
504 status = mbedtls_to_psa_error(
505 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
506 if( status == PSA_SUCCESS )
507 {
508 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
509 status = PSA_ERROR_NOT_SUPPORTED;
510 }
511 mbedtls_mpi_free( &n );
512 return( status );
513}
514
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000515static psa_status_t psa_import_rsa_key( psa_key_type_t type,
516 const uint8_t *data,
517 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200518 mbedtls_rsa_context **p_rsa )
519{
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000520 psa_status_t status;
521 mbedtls_pk_context pk;
522 mbedtls_rsa_context *rsa;
523 size_t bits;
524
525 mbedtls_pk_init( &pk );
526
527 /* Parse the data. */
528 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
529 status = mbedtls_to_psa_error(
530 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200531 else
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000532 status = mbedtls_to_psa_error(
533 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
534 if( status != PSA_SUCCESS )
535 goto exit;
536
537 /* We have something that the pkparse module recognizes. If it is a
538 * valid RSA key, store it. */
539 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200540 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000541 status = PSA_ERROR_INVALID_ARGUMENT;
542 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200543 }
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000544
545 rsa = mbedtls_pk_rsa( pk );
546 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
547 * supports non-byte-aligned key sizes, but not well. For example,
548 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
549 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
550 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
551 {
552 status = PSA_ERROR_NOT_SUPPORTED;
553 goto exit;
554 }
555 status = psa_check_rsa_key_byte_aligned( rsa );
556
557exit:
558 /* Free the content of the pk object only on error. */
559 if( status != PSA_SUCCESS )
560 {
561 mbedtls_pk_free( &pk );
562 return( status );
563 }
564
565 /* On success, store the content of the object in the RSA context. */
566 *p_rsa = rsa;
567
568 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200569}
570#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
571
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000572#if defined(MBEDTLS_ECP_C)
573
574/* Import a public key given as the uncompressed representation defined by SEC1
575 * 2.3.3 as the content of an ECPoint. */
576static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
577 const uint8_t *data,
578 size_t data_length,
579 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200580{
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000581 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
582 mbedtls_ecp_keypair *ecp = NULL;
583 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
584
585 *p_ecp = NULL;
586 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
587 if( ecp == NULL )
588 return( PSA_ERROR_INSUFFICIENT_MEMORY );
589 mbedtls_ecp_keypair_init( ecp );
590
591 /* Load the group. */
592 status = mbedtls_to_psa_error(
593 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
594 if( status != PSA_SUCCESS )
595 goto exit;
596 /* Load the public value. */
597 status = mbedtls_to_psa_error(
598 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
599 data, data_length ) );
600 if( status != PSA_SUCCESS )
601 goto exit;
602
603 /* Check that the point is on the curve. */
604 status = mbedtls_to_psa_error(
605 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
606 if( status != PSA_SUCCESS )
607 goto exit;
608
609 *p_ecp = ecp;
610 return( PSA_SUCCESS );
611
612exit:
613 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200614 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000615 mbedtls_ecp_keypair_free( ecp );
616 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200617 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000618 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200619}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000620#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200621
Gilles Peskinef76aa772018-10-29 19:24:33 +0100622#if defined(MBEDTLS_ECP_C)
623/* Import a private key given as a byte string which is the private value
624 * in big-endian order. */
625static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
626 const uint8_t *data,
627 size_t data_length,
628 mbedtls_ecp_keypair **p_ecp )
629{
630 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
631 mbedtls_ecp_keypair *ecp = NULL;
632 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
633
634 *p_ecp = NULL;
635 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
636 if( ecp == NULL )
637 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000638 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100639
640 /* Load the group. */
641 status = mbedtls_to_psa_error(
642 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
643 if( status != PSA_SUCCESS )
644 goto exit;
645 /* Load the secret value. */
646 status = mbedtls_to_psa_error(
647 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
648 if( status != PSA_SUCCESS )
649 goto exit;
650 /* Validate the private key. */
651 status = mbedtls_to_psa_error(
652 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
653 if( status != PSA_SUCCESS )
654 goto exit;
655 /* Calculate the public key from the private key. */
656 status = mbedtls_to_psa_error(
657 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
658 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
659 if( status != PSA_SUCCESS )
660 goto exit;
661
662 *p_ecp = ecp;
663 return( PSA_SUCCESS );
664
665exit:
666 if( ecp != NULL )
667 {
668 mbedtls_ecp_keypair_free( ecp );
669 mbedtls_free( ecp );
670 }
671 return( status );
672}
673#endif /* defined(MBEDTLS_ECP_C) */
674
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100675/** Import key data into a slot. `slot->type` must have been set
676 * previously. This function assumes that the slot does not contain
677 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100678psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
679 const uint8_t *data,
680 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100681{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200682 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100683
Darryl Green940d72c2018-07-13 13:18:51 +0100684 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100685 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100686 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100687 if( data_length > SIZE_MAX / 8 )
688 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100689 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200690 PSA_BYTES_TO_BITS( data_length ),
691 &slot->data.raw );
692 if( status != PSA_SUCCESS )
693 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200694 if( data_length != 0 )
695 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100696 }
697 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100698#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100699 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100700 {
Darryl Green940d72c2018-07-13 13:18:51 +0100701 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100702 data, data_length,
703 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100704 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000705 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
706 {
707 status = psa_import_ec_public_key(
708 PSA_KEY_TYPE_GET_CURVE( slot->type ),
709 data, data_length,
710 &slot->data.ecp );
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000711 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100712 else
713#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000714#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
715 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100716 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000717 status = psa_import_rsa_key( slot->type,
718 data, data_length,
719 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100720 }
721 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000722#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100723 {
724 return( PSA_ERROR_NOT_SUPPORTED );
725 }
Jaeden Amero08ad3272019-01-14 13:12:39 +0000726 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100727}
728
Darryl Green06fd18d2018-07-16 11:21:11 +0100729/* Retrieve an empty key slot (slot with no key data, but possibly
Jaeden Amero283dfd12019-01-11 12:06:22 +0000730 * with some metadata such as a policy or domain parameters). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100731static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100732 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100733{
734 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100735 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100736
737 *p_slot = NULL;
738
Gilles Peskinec5487a82018-12-03 18:08:14 +0100739 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100740 if( status != PSA_SUCCESS )
741 return( status );
742
743 if( slot->type != PSA_KEY_TYPE_NONE )
744 return( PSA_ERROR_OCCUPIED_SLOT );
745
746 *p_slot = slot;
747 return( status );
748}
749
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100750/** Calculate the intersection of two algorithm usage policies.
751 *
752 * Return 0 (which allows no operation) on incompatibility.
753 */
754static psa_algorithm_t psa_key_policy_algorithm_intersection(
755 psa_algorithm_t alg1,
756 psa_algorithm_t alg2 )
757{
758 /* Common case: the policy only allows alg. */
759 if( alg1 == alg2 )
760 return( alg1 );
761 /* If the policies are from the same hash-and-sign family, check
762 * if one is a wildcard. */
763 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
764 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
765 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
766 {
767 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
768 return( alg2 );
769 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
770 return( alg1 );
771 }
772 /* If the policies are incompatible, allow nothing. */
773 return( 0 );
774}
775
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100776/** Test whether a policy permits an algorithm.
777 *
778 * The caller must test usage flags separately.
779 */
780static int psa_key_policy_permits( const psa_key_policy_t *policy,
781 psa_algorithm_t alg )
782{
783 /* Common case: the policy only allows alg. */
784 if( alg == policy->alg )
785 return( 1 );
786 /* If policy->alg is a hash-and-sign with a wildcard for the hash,
787 * and alg is the same hash-and-sign family with any hash,
788 * then alg is compliant with policy->alg. */
789 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) &&
790 PSA_ALG_SIGN_GET_HASH( policy->alg ) == PSA_ALG_ANY_HASH )
791 {
792 return( ( policy->alg & ~PSA_ALG_HASH_MASK ) ==
793 ( alg & ~PSA_ALG_HASH_MASK ) );
794 }
795 /* If it isn't permitted, it's forbidden. */
796 return( 0 );
797}
798
Gilles Peskine4cb9dde2019-01-19 13:40:11 +0100799static psa_status_t psa_restrict_key_policy(
800 psa_key_policy_t *policy,
801 const psa_key_policy_t *constraint )
802{
803 psa_algorithm_t intersection_alg =
804 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
805 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
806 return( PSA_ERROR_INVALID_ARGUMENT );
807 policy->usage &= constraint->usage;
808 return( PSA_SUCCESS );
809}
810
Darryl Green06fd18d2018-07-16 11:21:11 +0100811/** Retrieve a slot which must contain a key. The key must have allow all the
812 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
813 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100814static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100815 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100816 psa_key_usage_t usage,
817 psa_algorithm_t alg )
818{
819 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100820 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100821
822 *p_slot = NULL;
823
Gilles Peskinec5487a82018-12-03 18:08:14 +0100824 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100825 if( status != PSA_SUCCESS )
826 return( status );
827 if( slot->type == PSA_KEY_TYPE_NONE )
828 return( PSA_ERROR_EMPTY_SLOT );
829
830 /* Enforce that usage policy for the key slot contains all the flags
831 * required by the usage parameter. There is one exception: public
832 * keys can always be exported, so we treat public key objects as
833 * if they had the export flag. */
834 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
835 usage &= ~PSA_KEY_USAGE_EXPORT;
836 if( ( slot->policy.usage & usage ) != usage )
837 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100838
839 /* Enforce that the usage policy permits the requested algortihm. */
840 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100841 return( PSA_ERROR_NOT_PERMITTED );
842
843 *p_slot = slot;
844 return( PSA_SUCCESS );
845}
Darryl Green940d72c2018-07-13 13:18:51 +0100846
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100847/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100848static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000849{
850 if( slot->type == PSA_KEY_TYPE_NONE )
851 {
852 /* No key material to clean. */
853 }
854 else if( key_type_is_raw_bytes( slot->type ) )
855 {
856 mbedtls_free( slot->data.raw.data );
857 }
858 else
859#if defined(MBEDTLS_RSA_C)
860 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
861 {
862 mbedtls_rsa_free( slot->data.rsa );
863 mbedtls_free( slot->data.rsa );
864 }
865 else
866#endif /* defined(MBEDTLS_RSA_C) */
867#if defined(MBEDTLS_ECP_C)
868 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
869 {
870 mbedtls_ecp_keypair_free( slot->data.ecp );
871 mbedtls_free( slot->data.ecp );
872 }
873 else
874#endif /* defined(MBEDTLS_ECP_C) */
875 {
876 /* Shouldn't happen: the key type is not any type that we
877 * put in. */
878 return( PSA_ERROR_TAMPERING_DETECTED );
879 }
880
881 return( PSA_SUCCESS );
882}
883
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100884static void psa_abort_operations_using_key( psa_key_slot_t *slot )
885{
886 /*TODO*/
887 (void) slot;
888}
889
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100890/** Completely wipe a slot in memory, including its policy.
891 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100892psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100893{
894 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100895 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100896 /* At this point, key material and other type-specific content has
897 * been wiped. Clear remaining metadata. We can call memset and not
898 * zeroize because the metadata is not particularly sensitive. */
899 memset( slot, 0, sizeof( *slot ) );
900 return( status );
901}
902
Gilles Peskinec5487a82018-12-03 18:08:14 +0100903psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100904 psa_key_type_t type,
905 const uint8_t *data,
906 size_t data_length )
907{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100908 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100909 psa_status_t status;
910
Gilles Peskinec5487a82018-12-03 18:08:14 +0100911 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100912 if( status != PSA_SUCCESS )
913 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100914
915 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100916
917 status = psa_import_key_into_slot( slot, data, data_length );
918 if( status != PSA_SUCCESS )
919 {
920 slot->type = PSA_KEY_TYPE_NONE;
921 return( status );
922 }
923
Darryl Greend49a4992018-06-18 17:27:26 +0100924#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
925 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
926 {
927 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100928 status = psa_save_persistent_key( slot->persistent_storage_id,
929 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100930 data_length );
931 if( status != PSA_SUCCESS )
932 {
933 (void) psa_remove_key_data_from_memory( slot );
934 slot->type = PSA_KEY_TYPE_NONE;
935 }
936 }
937#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
938
939 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100940}
941
Gilles Peskinec5487a82018-12-03 18:08:14 +0100942psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100943{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100944 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100945 psa_status_t status = PSA_SUCCESS;
946 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100947
Gilles Peskinec5487a82018-12-03 18:08:14 +0100948 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200949 if( status != PSA_SUCCESS )
950 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100951#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
952 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
953 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100954 storage_status =
955 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100956 }
957#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100958 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100959 if( status != PSA_SUCCESS )
960 return( status );
961 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100962}
963
Gilles Peskineb870b182018-07-06 16:02:09 +0200964/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100965static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200966{
967 if( key_type_is_raw_bytes( slot->type ) )
968 return( slot->data.raw.bytes * 8 );
969#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200970 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100971 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200972#endif /* defined(MBEDTLS_RSA_C) */
973#if defined(MBEDTLS_ECP_C)
974 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
975 return( slot->data.ecp->grp.pbits );
976#endif /* defined(MBEDTLS_ECP_C) */
977 /* Shouldn't happen except on an empty slot. */
978 return( 0 );
979}
980
Gilles Peskinec5487a82018-12-03 18:08:14 +0100981psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200982 psa_key_type_t *type,
983 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100984{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100985 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200986 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100987
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200989 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100990 if( bits != NULL )
991 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100992 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200993 if( status != PSA_SUCCESS )
994 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200995
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100996 if( slot->type == PSA_KEY_TYPE_NONE )
997 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200998 if( type != NULL )
999 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001000 if( bits != NULL )
1001 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001002 return( PSA_SUCCESS );
1003}
1004
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001005#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +00001006static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1007 unsigned char *buf, size_t size )
1008{
1009 int ret;
1010 unsigned char *c;
1011 size_t len = 0;
1012
1013 c = buf + size;
1014
1015 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1016
1017 return( (int) len );
1018}
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001019#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +00001020
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001021static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1022 uint8_t *data,
1023 size_t data_size,
1024 size_t *data_length,
1025 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001026{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001027 *data_length = 0;
1028
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001029 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001030 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001031
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001032 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001033 {
1034 if( slot->data.raw.bytes > data_size )
1035 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001036 if( data_size != 0 )
1037 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001038 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001039 memset( data + slot->data.raw.bytes, 0,
1040 data_size - slot->data.raw.bytes );
1041 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001042 *data_length = slot->data.raw.bytes;
1043 return( PSA_SUCCESS );
1044 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001045#if defined(MBEDTLS_ECP_C)
1046 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1047 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001048 psa_status_t status;
1049
Gilles Peskine188c71e2018-10-29 19:26:02 +01001050 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1051 if( bytes > data_size )
1052 return( PSA_ERROR_BUFFER_TOO_SMALL );
1053 status = mbedtls_to_psa_error(
1054 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1055 if( status != PSA_SUCCESS )
1056 return( status );
1057 memset( data + bytes, 0, data_size - bytes );
1058 *data_length = bytes;
1059 return( PSA_SUCCESS );
1060 }
1061#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001062 else
Moran Peker17e36e12018-05-02 12:55:20 +03001063 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001064#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001065 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001066 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001067 {
Moran Pekera998bc62018-04-16 18:16:20 +03001068 mbedtls_pk_context pk;
1069 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001070 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001071 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001072#if defined(MBEDTLS_RSA_C)
1073 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001074 pk.pk_info = &mbedtls_rsa_info;
1075 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001076#else
1077 return( PSA_ERROR_NOT_SUPPORTED );
1078#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001079 }
1080 else
1081 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001082#if defined(MBEDTLS_ECP_C)
1083 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001084 pk.pk_info = &mbedtls_eckey_info;
1085 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001086#else
1087 return( PSA_ERROR_NOT_SUPPORTED );
1088#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001089 }
Moran Pekerd7326592018-05-29 16:56:39 +03001090 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001091 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001092 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001093 }
Moran Peker17e36e12018-05-02 12:55:20 +03001094 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001095 {
Moran Peker17e36e12018-05-02 12:55:20 +03001096 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001097 }
Moran Peker60364322018-04-29 11:34:58 +03001098 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001099 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001100 /* If data_size is 0 then data may be NULL and then the
1101 * call to memset would have undefined behavior. */
1102 if( data_size != 0 )
1103 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001104 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001105 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001106 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1107 * Move the data to the beginning and erase remaining data
1108 * at the original location. */
1109 if( 2 * (size_t) ret <= data_size )
1110 {
1111 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001112 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001113 }
1114 else if( (size_t) ret < data_size )
1115 {
1116 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001117 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001118 }
Moran Pekera998bc62018-04-16 18:16:20 +03001119 *data_length = ret;
1120 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001121 }
1122 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001123#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001124 {
1125 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001126 it is valid for a special-purpose implementation to omit
1127 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001128 return( PSA_ERROR_NOT_SUPPORTED );
1129 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001130 }
1131}
1132
Gilles Peskinec5487a82018-12-03 18:08:14 +01001133psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001134 uint8_t *data,
1135 size_t data_size,
1136 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001137{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001138 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001139 psa_status_t status;
1140
1141 /* Set the key to empty now, so that even when there are errors, we always
1142 * set data_length to a value between 0 and data_size. On error, setting
1143 * the key to empty is a good choice because an empty key representation is
1144 * unlikely to be accepted anywhere. */
1145 *data_length = 0;
1146
1147 /* Export requires the EXPORT flag. There is an exception for public keys,
1148 * which don't require any flag, but psa_get_key_from_slot takes
1149 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001150 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001151 if( status != PSA_SUCCESS )
1152 return( status );
1153 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001154 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001155}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001156
Gilles Peskinec5487a82018-12-03 18:08:14 +01001157psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001158 uint8_t *data,
1159 size_t data_size,
1160 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001161{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001162 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001163 psa_status_t status;
1164
1165 /* Set the key to empty now, so that even when there are errors, we always
1166 * set data_length to a value between 0 and data_size. On error, setting
1167 * the key to empty is a good choice because an empty key representation is
1168 * unlikely to be accepted anywhere. */
1169 *data_length = 0;
1170
1171 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001172 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001173 if( status != PSA_SUCCESS )
1174 return( status );
1175 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001176 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001177}
1178
Darryl Green0c6575a2018-11-07 16:05:30 +00001179#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001180static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001181 size_t bits )
1182{
1183 psa_status_t status;
1184 uint8_t *data;
1185 size_t key_length;
1186 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1187 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001188 if( data == NULL )
1189 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001190 /* Get key data in export format */
1191 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1192 if( status != PSA_SUCCESS )
1193 {
1194 slot->type = PSA_KEY_TYPE_NONE;
1195 goto exit;
1196 }
1197 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001198 status = psa_save_persistent_key( slot->persistent_storage_id,
1199 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001200 data, key_length );
1201 if( status != PSA_SUCCESS )
1202 {
1203 slot->type = PSA_KEY_TYPE_NONE;
1204 }
1205exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001206 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001207 mbedtls_free( data );
1208 return( status );
1209}
1210#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1211
Gilles Peskine4cb9dde2019-01-19 13:40:11 +01001212static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
1213 psa_key_handle_t target )
1214{
1215 psa_status_t status;
1216 uint8_t *buffer = NULL;
1217 size_t buffer_size = 0;
1218 size_t length;
1219
1220 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
1221 psa_get_key_bits( source ) );
1222 buffer = mbedtls_calloc( 1, buffer_size );
1223 if( buffer == NULL )
1224 {
1225 status = PSA_ERROR_INSUFFICIENT_MEMORY;
1226 goto exit;
1227 }
1228 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1229 if( status != PSA_SUCCESS )
1230 goto exit;
1231 status = psa_import_key( target, source->type, buffer, length );
1232
1233exit:
1234 return( status );
1235}
1236
1237psa_status_t psa_copy_key(psa_key_handle_t source_handle,
1238 psa_key_handle_t target_handle,
1239 const psa_key_policy_t *constraint)
1240{
1241 psa_key_slot_t *source_slot = NULL;
1242 psa_key_slot_t *target_slot = NULL;
1243 psa_key_policy_t new_policy;
1244 psa_status_t status;
1245 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1246 if( status != PSA_SUCCESS )
1247 return( status );
1248 status = psa_get_empty_key_slot( target_handle, &target_slot );
1249 if( status != PSA_SUCCESS )
1250 return( status );
1251
1252 new_policy = target_slot->policy;
1253 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1254 if( status != PSA_SUCCESS )
1255 return( status );
1256 if( constraint != NULL )
1257 {
1258 status = psa_restrict_key_policy( &new_policy, constraint );
1259 if( status != PSA_SUCCESS )
1260 return( status );
1261 }
1262
1263 status = psa_copy_key_material( source_slot, target_handle );
1264 if( status != PSA_SUCCESS )
1265 return( status );
1266
1267 target_slot->policy = new_policy;
1268 return( PSA_SUCCESS );
1269}
1270
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001271
1272
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001273/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001274/* Message digests */
1275/****************************************************************/
1276
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001277static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001278{
1279 switch( alg )
1280 {
1281#if defined(MBEDTLS_MD2_C)
1282 case PSA_ALG_MD2:
1283 return( &mbedtls_md2_info );
1284#endif
1285#if defined(MBEDTLS_MD4_C)
1286 case PSA_ALG_MD4:
1287 return( &mbedtls_md4_info );
1288#endif
1289#if defined(MBEDTLS_MD5_C)
1290 case PSA_ALG_MD5:
1291 return( &mbedtls_md5_info );
1292#endif
1293#if defined(MBEDTLS_RIPEMD160_C)
1294 case PSA_ALG_RIPEMD160:
1295 return( &mbedtls_ripemd160_info );
1296#endif
1297#if defined(MBEDTLS_SHA1_C)
1298 case PSA_ALG_SHA_1:
1299 return( &mbedtls_sha1_info );
1300#endif
1301#if defined(MBEDTLS_SHA256_C)
1302 case PSA_ALG_SHA_224:
1303 return( &mbedtls_sha224_info );
1304 case PSA_ALG_SHA_256:
1305 return( &mbedtls_sha256_info );
1306#endif
1307#if defined(MBEDTLS_SHA512_C)
1308 case PSA_ALG_SHA_384:
1309 return( &mbedtls_sha384_info );
1310 case PSA_ALG_SHA_512:
1311 return( &mbedtls_sha512_info );
1312#endif
1313 default:
1314 return( NULL );
1315 }
1316}
1317
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001318psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1319{
1320 switch( operation->alg )
1321 {
Gilles Peskine81736312018-06-26 15:04:31 +02001322 case 0:
1323 /* The object has (apparently) been initialized but it is not
1324 * in use. It's ok to call abort on such an object, and there's
1325 * nothing to do. */
1326 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001327#if defined(MBEDTLS_MD2_C)
1328 case PSA_ALG_MD2:
1329 mbedtls_md2_free( &operation->ctx.md2 );
1330 break;
1331#endif
1332#if defined(MBEDTLS_MD4_C)
1333 case PSA_ALG_MD4:
1334 mbedtls_md4_free( &operation->ctx.md4 );
1335 break;
1336#endif
1337#if defined(MBEDTLS_MD5_C)
1338 case PSA_ALG_MD5:
1339 mbedtls_md5_free( &operation->ctx.md5 );
1340 break;
1341#endif
1342#if defined(MBEDTLS_RIPEMD160_C)
1343 case PSA_ALG_RIPEMD160:
1344 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1345 break;
1346#endif
1347#if defined(MBEDTLS_SHA1_C)
1348 case PSA_ALG_SHA_1:
1349 mbedtls_sha1_free( &operation->ctx.sha1 );
1350 break;
1351#endif
1352#if defined(MBEDTLS_SHA256_C)
1353 case PSA_ALG_SHA_224:
1354 case PSA_ALG_SHA_256:
1355 mbedtls_sha256_free( &operation->ctx.sha256 );
1356 break;
1357#endif
1358#if defined(MBEDTLS_SHA512_C)
1359 case PSA_ALG_SHA_384:
1360 case PSA_ALG_SHA_512:
1361 mbedtls_sha512_free( &operation->ctx.sha512 );
1362 break;
1363#endif
1364 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001365 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001366 }
1367 operation->alg = 0;
1368 return( PSA_SUCCESS );
1369}
1370
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001371psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001372 psa_algorithm_t alg )
1373{
1374 int ret;
1375 operation->alg = 0;
1376 switch( alg )
1377 {
1378#if defined(MBEDTLS_MD2_C)
1379 case PSA_ALG_MD2:
1380 mbedtls_md2_init( &operation->ctx.md2 );
1381 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1382 break;
1383#endif
1384#if defined(MBEDTLS_MD4_C)
1385 case PSA_ALG_MD4:
1386 mbedtls_md4_init( &operation->ctx.md4 );
1387 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1388 break;
1389#endif
1390#if defined(MBEDTLS_MD5_C)
1391 case PSA_ALG_MD5:
1392 mbedtls_md5_init( &operation->ctx.md5 );
1393 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1394 break;
1395#endif
1396#if defined(MBEDTLS_RIPEMD160_C)
1397 case PSA_ALG_RIPEMD160:
1398 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1399 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1400 break;
1401#endif
1402#if defined(MBEDTLS_SHA1_C)
1403 case PSA_ALG_SHA_1:
1404 mbedtls_sha1_init( &operation->ctx.sha1 );
1405 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1406 break;
1407#endif
1408#if defined(MBEDTLS_SHA256_C)
1409 case PSA_ALG_SHA_224:
1410 mbedtls_sha256_init( &operation->ctx.sha256 );
1411 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1412 break;
1413 case PSA_ALG_SHA_256:
1414 mbedtls_sha256_init( &operation->ctx.sha256 );
1415 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1416 break;
1417#endif
1418#if defined(MBEDTLS_SHA512_C)
1419 case PSA_ALG_SHA_384:
1420 mbedtls_sha512_init( &operation->ctx.sha512 );
1421 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1422 break;
1423 case PSA_ALG_SHA_512:
1424 mbedtls_sha512_init( &operation->ctx.sha512 );
1425 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1426 break;
1427#endif
1428 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001429 return( PSA_ALG_IS_HASH( alg ) ?
1430 PSA_ERROR_NOT_SUPPORTED :
1431 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001432 }
1433 if( ret == 0 )
1434 operation->alg = alg;
1435 else
1436 psa_hash_abort( operation );
1437 return( mbedtls_to_psa_error( ret ) );
1438}
1439
1440psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1441 const uint8_t *input,
1442 size_t input_length )
1443{
1444 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001445
1446 /* Don't require hash implementations to behave correctly on a
1447 * zero-length input, which may have an invalid pointer. */
1448 if( input_length == 0 )
1449 return( PSA_SUCCESS );
1450
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001451 switch( operation->alg )
1452 {
1453#if defined(MBEDTLS_MD2_C)
1454 case PSA_ALG_MD2:
1455 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1456 input, input_length );
1457 break;
1458#endif
1459#if defined(MBEDTLS_MD4_C)
1460 case PSA_ALG_MD4:
1461 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1462 input, input_length );
1463 break;
1464#endif
1465#if defined(MBEDTLS_MD5_C)
1466 case PSA_ALG_MD5:
1467 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1468 input, input_length );
1469 break;
1470#endif
1471#if defined(MBEDTLS_RIPEMD160_C)
1472 case PSA_ALG_RIPEMD160:
1473 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1474 input, input_length );
1475 break;
1476#endif
1477#if defined(MBEDTLS_SHA1_C)
1478 case PSA_ALG_SHA_1:
1479 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1480 input, input_length );
1481 break;
1482#endif
1483#if defined(MBEDTLS_SHA256_C)
1484 case PSA_ALG_SHA_224:
1485 case PSA_ALG_SHA_256:
1486 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1487 input, input_length );
1488 break;
1489#endif
1490#if defined(MBEDTLS_SHA512_C)
1491 case PSA_ALG_SHA_384:
1492 case PSA_ALG_SHA_512:
1493 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1494 input, input_length );
1495 break;
1496#endif
1497 default:
1498 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1499 break;
1500 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001501
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001502 if( ret != 0 )
1503 psa_hash_abort( operation );
1504 return( mbedtls_to_psa_error( ret ) );
1505}
1506
1507psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1508 uint8_t *hash,
1509 size_t hash_size,
1510 size_t *hash_length )
1511{
itayzafrir40835d42018-08-02 13:14:17 +03001512 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001513 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001514 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001515
1516 /* Fill the output buffer with something that isn't a valid hash
1517 * (barring an attack on the hash and deliberately-crafted input),
1518 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001519 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001520 /* If hash_size is 0 then hash may be NULL and then the
1521 * call to memset would have undefined behavior. */
1522 if( hash_size != 0 )
1523 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001524
1525 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001526 {
1527 status = PSA_ERROR_BUFFER_TOO_SMALL;
1528 goto exit;
1529 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001530
1531 switch( operation->alg )
1532 {
1533#if defined(MBEDTLS_MD2_C)
1534 case PSA_ALG_MD2:
1535 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1536 break;
1537#endif
1538#if defined(MBEDTLS_MD4_C)
1539 case PSA_ALG_MD4:
1540 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1541 break;
1542#endif
1543#if defined(MBEDTLS_MD5_C)
1544 case PSA_ALG_MD5:
1545 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1546 break;
1547#endif
1548#if defined(MBEDTLS_RIPEMD160_C)
1549 case PSA_ALG_RIPEMD160:
1550 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1551 break;
1552#endif
1553#if defined(MBEDTLS_SHA1_C)
1554 case PSA_ALG_SHA_1:
1555 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1556 break;
1557#endif
1558#if defined(MBEDTLS_SHA256_C)
1559 case PSA_ALG_SHA_224:
1560 case PSA_ALG_SHA_256:
1561 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1562 break;
1563#endif
1564#if defined(MBEDTLS_SHA512_C)
1565 case PSA_ALG_SHA_384:
1566 case PSA_ALG_SHA_512:
1567 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1568 break;
1569#endif
1570 default:
1571 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1572 break;
1573 }
itayzafrir40835d42018-08-02 13:14:17 +03001574 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001575
itayzafrir40835d42018-08-02 13:14:17 +03001576exit:
1577 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001578 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001579 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001580 return( psa_hash_abort( operation ) );
1581 }
1582 else
1583 {
1584 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001585 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001586 }
1587}
1588
Gilles Peskine2d277862018-06-18 15:41:12 +02001589psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1590 const uint8_t *hash,
1591 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001592{
1593 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1594 size_t actual_hash_length;
1595 psa_status_t status = psa_hash_finish( operation,
1596 actual_hash, sizeof( actual_hash ),
1597 &actual_hash_length );
1598 if( status != PSA_SUCCESS )
1599 return( status );
1600 if( actual_hash_length != hash_length )
1601 return( PSA_ERROR_INVALID_SIGNATURE );
1602 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1603 return( PSA_ERROR_INVALID_SIGNATURE );
1604 return( PSA_SUCCESS );
1605}
1606
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001607psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1608 psa_hash_operation_t *target_operation)
1609{
1610 if( target_operation->alg != 0 )
1611 return( PSA_ERROR_BAD_STATE );
1612
1613 switch( source_operation->alg )
1614 {
1615 case 0:
1616 return( PSA_ERROR_BAD_STATE );
1617#if defined(MBEDTLS_MD2_C)
1618 case PSA_ALG_MD2:
1619 mbedtls_md2_clone( &target_operation->ctx.md2,
1620 &source_operation->ctx.md2 );
1621 break;
1622#endif
1623#if defined(MBEDTLS_MD4_C)
1624 case PSA_ALG_MD4:
1625 mbedtls_md4_clone( &target_operation->ctx.md4,
1626 &source_operation->ctx.md4 );
1627 break;
1628#endif
1629#if defined(MBEDTLS_MD5_C)
1630 case PSA_ALG_MD5:
1631 mbedtls_md5_clone( &target_operation->ctx.md5,
1632 &source_operation->ctx.md5 );
1633 break;
1634#endif
1635#if defined(MBEDTLS_RIPEMD160_C)
1636 case PSA_ALG_RIPEMD160:
1637 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1638 &source_operation->ctx.ripemd160 );
1639 break;
1640#endif
1641#if defined(MBEDTLS_SHA1_C)
1642 case PSA_ALG_SHA_1:
1643 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1644 &source_operation->ctx.sha1 );
1645 break;
1646#endif
1647#if defined(MBEDTLS_SHA256_C)
1648 case PSA_ALG_SHA_224:
1649 case PSA_ALG_SHA_256:
1650 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1651 &source_operation->ctx.sha256 );
1652 break;
1653#endif
1654#if defined(MBEDTLS_SHA512_C)
1655 case PSA_ALG_SHA_384:
1656 case PSA_ALG_SHA_512:
1657 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1658 &source_operation->ctx.sha512 );
1659 break;
1660#endif
1661 default:
1662 return( PSA_ERROR_NOT_SUPPORTED );
1663 }
1664
1665 target_operation->alg = source_operation->alg;
1666 return( PSA_SUCCESS );
1667}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001668
1669
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001670/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001671/* MAC */
1672/****************************************************************/
1673
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001674static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001675 psa_algorithm_t alg,
1676 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001677 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001678 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001679{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001680 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001681 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001682
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001683 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001684 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001685
Gilles Peskine8c9def32018-02-08 10:02:12 +01001686 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1687 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001688 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001689 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001690 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001691 mode = MBEDTLS_MODE_STREAM;
1692 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001693 case PSA_ALG_CTR:
1694 mode = MBEDTLS_MODE_CTR;
1695 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001696 case PSA_ALG_CFB:
1697 mode = MBEDTLS_MODE_CFB;
1698 break;
1699 case PSA_ALG_OFB:
1700 mode = MBEDTLS_MODE_OFB;
1701 break;
1702 case PSA_ALG_CBC_NO_PADDING:
1703 mode = MBEDTLS_MODE_CBC;
1704 break;
1705 case PSA_ALG_CBC_PKCS7:
1706 mode = MBEDTLS_MODE_CBC;
1707 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001708 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001709 mode = MBEDTLS_MODE_CCM;
1710 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001711 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001712 mode = MBEDTLS_MODE_GCM;
1713 break;
1714 default:
1715 return( NULL );
1716 }
1717 }
1718 else if( alg == PSA_ALG_CMAC )
1719 mode = MBEDTLS_MODE_ECB;
1720 else if( alg == PSA_ALG_GMAC )
1721 mode = MBEDTLS_MODE_GCM;
1722 else
1723 return( NULL );
1724
1725 switch( key_type )
1726 {
1727 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001728 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001729 break;
1730 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001731 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1732 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001733 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001734 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001735 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001736 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001737 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1738 * but two-key Triple-DES is functionally three-key Triple-DES
1739 * with K1=K3, so that's how we present it to mbedtls. */
1740 if( key_bits == 128 )
1741 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001742 break;
1743 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001744 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001745 break;
1746 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001747 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001748 break;
1749 default:
1750 return( NULL );
1751 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001752 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001753 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001754
Jaeden Amero23bbb752018-06-26 14:16:54 +01001755 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1756 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001757}
1758
Gilles Peskinea05219c2018-11-16 16:02:56 +01001759#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001760static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001761{
Gilles Peskine2d277862018-06-18 15:41:12 +02001762 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001763 {
1764 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001765 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001766 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001767 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001768 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001769 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001770 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001771 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001772 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001773 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001774 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001775 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001776 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001777 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001778 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001779 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001780 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001781 return( 128 );
1782 default:
1783 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001784 }
1785}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001786#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001787
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001788/* Initialize the MAC operation structure. Once this function has been
1789 * called, psa_mac_abort can run and will do the right thing. */
1790static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1791 psa_algorithm_t alg )
1792{
1793 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1794
1795 operation->alg = alg;
1796 operation->key_set = 0;
1797 operation->iv_set = 0;
1798 operation->iv_required = 0;
1799 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001800 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001801
1802#if defined(MBEDTLS_CMAC_C)
1803 if( alg == PSA_ALG_CMAC )
1804 {
1805 operation->iv_required = 0;
1806 mbedtls_cipher_init( &operation->ctx.cmac );
1807 status = PSA_SUCCESS;
1808 }
1809 else
1810#endif /* MBEDTLS_CMAC_C */
1811#if defined(MBEDTLS_MD_C)
1812 if( PSA_ALG_IS_HMAC( operation->alg ) )
1813 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001814 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1815 operation->ctx.hmac.hash_ctx.alg = 0;
1816 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001817 }
1818 else
1819#endif /* MBEDTLS_MD_C */
1820 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001821 if( ! PSA_ALG_IS_MAC( alg ) )
1822 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001823 }
1824
1825 if( status != PSA_SUCCESS )
1826 memset( operation, 0, sizeof( *operation ) );
1827 return( status );
1828}
1829
Gilles Peskine01126fa2018-07-12 17:04:55 +02001830#if defined(MBEDTLS_MD_C)
1831static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1832{
Gilles Peskine3f108122018-12-07 18:14:53 +01001833 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001834 return( psa_hash_abort( &hmac->hash_ctx ) );
1835}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001836
1837static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1838{
1839 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1840 memset( hmac, 0, sizeof( *hmac ) );
1841}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001842#endif /* MBEDTLS_MD_C */
1843
Gilles Peskine8c9def32018-02-08 10:02:12 +01001844psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1845{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001846 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001847 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001848 /* The object has (apparently) been initialized but it is not
1849 * in use. It's ok to call abort on such an object, and there's
1850 * nothing to do. */
1851 return( PSA_SUCCESS );
1852 }
1853 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001854#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001855 if( operation->alg == PSA_ALG_CMAC )
1856 {
1857 mbedtls_cipher_free( &operation->ctx.cmac );
1858 }
1859 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001860#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001861#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001862 if( PSA_ALG_IS_HMAC( operation->alg ) )
1863 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001864 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001865 }
1866 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001867#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001868 {
1869 /* Sanity check (shouldn't happen: operation->alg should
1870 * always have been initialized to a valid value). */
1871 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001872 }
Moran Peker41deec42018-04-04 15:43:05 +03001873
Gilles Peskine8c9def32018-02-08 10:02:12 +01001874 operation->alg = 0;
1875 operation->key_set = 0;
1876 operation->iv_set = 0;
1877 operation->iv_required = 0;
1878 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001879 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001880
Gilles Peskine8c9def32018-02-08 10:02:12 +01001881 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001882
1883bad_state:
1884 /* If abort is called on an uninitialized object, we can't trust
1885 * anything. Wipe the object in case it contains confidential data.
1886 * This may result in a memory leak if a pointer gets overwritten,
1887 * but it's too late to do anything about this. */
1888 memset( operation, 0, sizeof( *operation ) );
1889 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001890}
1891
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001892#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001893static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001894 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001895 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001896 const mbedtls_cipher_info_t *cipher_info )
1897{
1898 int ret;
1899
1900 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001901
1902 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1903 if( ret != 0 )
1904 return( ret );
1905
1906 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1907 slot->data.raw.data,
1908 key_bits );
1909 return( ret );
1910}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001911#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001912
Gilles Peskine248051a2018-06-20 16:09:38 +02001913#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001914static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1915 const uint8_t *key,
1916 size_t key_length,
1917 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001918{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001919 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001920 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001921 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001922 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001923 psa_status_t status;
1924
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001925 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1926 * overflow below. This should never trigger if the hash algorithm
1927 * is implemented correctly. */
1928 /* The size checks against the ipad and opad buffers cannot be written
1929 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1930 * because that triggers -Wlogical-op on GCC 7.3. */
1931 if( block_size > sizeof( ipad ) )
1932 return( PSA_ERROR_NOT_SUPPORTED );
1933 if( block_size > sizeof( hmac->opad ) )
1934 return( PSA_ERROR_NOT_SUPPORTED );
1935 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001936 return( PSA_ERROR_NOT_SUPPORTED );
1937
Gilles Peskined223b522018-06-11 18:12:58 +02001938 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001939 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001940 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001941 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001942 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001943 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001944 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001945 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001946 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001947 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001948 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001949 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001950 }
Gilles Peskine96889972018-07-12 17:07:03 +02001951 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1952 * but it is permitted. It is common when HMAC is used in HKDF, for
1953 * example. Don't call `memcpy` in the 0-length because `key` could be
1954 * an invalid pointer which would make the behavior undefined. */
1955 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001956 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001957
Gilles Peskined223b522018-06-11 18:12:58 +02001958 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1959 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001960 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001961 ipad[i] ^= 0x36;
1962 memset( ipad + key_length, 0x36, block_size - key_length );
1963
1964 /* Copy the key material from ipad to opad, flipping the requisite bits,
1965 * and filling the rest of opad with the requisite constant. */
1966 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001967 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1968 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001969
Gilles Peskine01126fa2018-07-12 17:04:55 +02001970 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001971 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001972 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001973
Gilles Peskine01126fa2018-07-12 17:04:55 +02001974 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001975
1976cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001977 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001978
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001979 return( status );
1980}
Gilles Peskine248051a2018-06-20 16:09:38 +02001981#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001982
Gilles Peskine89167cb2018-07-08 20:12:23 +02001983static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001984 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001985 psa_algorithm_t alg,
1986 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001987{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001988 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001989 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001990 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001991 psa_key_usage_t usage =
1992 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001993 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001994 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001995
Gilles Peskined911eb72018-08-14 15:18:45 +02001996 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001997 if( status != PSA_SUCCESS )
1998 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001999 if( is_sign )
2000 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002001
Gilles Peskinec5487a82018-12-03 18:08:14 +01002002 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002003 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002004 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002005 key_bits = psa_get_key_bits( slot );
2006
Gilles Peskine8c9def32018-02-08 10:02:12 +01002007#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002008 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002009 {
2010 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002011 mbedtls_cipher_info_from_psa( full_length_alg,
2012 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002013 int ret;
2014 if( cipher_info == NULL )
2015 {
2016 status = PSA_ERROR_NOT_SUPPORTED;
2017 goto exit;
2018 }
2019 operation->mac_size = cipher_info->block_size;
2020 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2021 status = mbedtls_to_psa_error( ret );
2022 }
2023 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002024#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002025#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002026 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002027 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002028 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002029 if( hash_alg == 0 )
2030 {
2031 status = PSA_ERROR_NOT_SUPPORTED;
2032 goto exit;
2033 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002034
2035 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2036 /* Sanity check. This shouldn't fail on a valid configuration. */
2037 if( operation->mac_size == 0 ||
2038 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2039 {
2040 status = PSA_ERROR_NOT_SUPPORTED;
2041 goto exit;
2042 }
2043
Gilles Peskine01126fa2018-07-12 17:04:55 +02002044 if( slot->type != PSA_KEY_TYPE_HMAC )
2045 {
2046 status = PSA_ERROR_INVALID_ARGUMENT;
2047 goto exit;
2048 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002049
Gilles Peskine01126fa2018-07-12 17:04:55 +02002050 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2051 slot->data.raw.data,
2052 slot->data.raw.bytes,
2053 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002054 }
2055 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002056#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002057 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002058 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002059 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002060 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002061
Gilles Peskined911eb72018-08-14 15:18:45 +02002062 if( truncated == 0 )
2063 {
2064 /* The "normal" case: untruncated algorithm. Nothing to do. */
2065 }
2066 else if( truncated < 4 )
2067 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002068 /* A very short MAC is too short for security since it can be
2069 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2070 * so we make this our minimum, even though 32 bits is still
2071 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002072 status = PSA_ERROR_NOT_SUPPORTED;
2073 }
2074 else if( truncated > operation->mac_size )
2075 {
2076 /* It's impossible to "truncate" to a larger length. */
2077 status = PSA_ERROR_INVALID_ARGUMENT;
2078 }
2079 else
2080 operation->mac_size = truncated;
2081
Gilles Peskinefbfac682018-07-08 20:51:54 +02002082exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002083 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002084 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002085 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002086 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002087 else
2088 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002089 operation->key_set = 1;
2090 }
2091 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002092}
2093
Gilles Peskine89167cb2018-07-08 20:12:23 +02002094psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002095 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002096 psa_algorithm_t alg )
2097{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002098 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002099}
2100
2101psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002102 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002103 psa_algorithm_t alg )
2104{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002105 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002106}
2107
Gilles Peskine8c9def32018-02-08 10:02:12 +01002108psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2109 const uint8_t *input,
2110 size_t input_length )
2111{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002112 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002113 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002114 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002115 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002116 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002117 operation->has_input = 1;
2118
Gilles Peskine8c9def32018-02-08 10:02:12 +01002119#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002120 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002121 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002122 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2123 input, input_length );
2124 status = mbedtls_to_psa_error( ret );
2125 }
2126 else
2127#endif /* MBEDTLS_CMAC_C */
2128#if defined(MBEDTLS_MD_C)
2129 if( PSA_ALG_IS_HMAC( operation->alg ) )
2130 {
2131 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2132 input_length );
2133 }
2134 else
2135#endif /* MBEDTLS_MD_C */
2136 {
2137 /* This shouldn't happen if `operation` was initialized by
2138 * a setup function. */
2139 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002140 }
2141
Gilles Peskinefbfac682018-07-08 20:51:54 +02002142cleanup:
2143 if( status != PSA_SUCCESS )
2144 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002145 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002146}
2147
Gilles Peskine01126fa2018-07-12 17:04:55 +02002148#if defined(MBEDTLS_MD_C)
2149static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2150 uint8_t *mac,
2151 size_t mac_size )
2152{
2153 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2154 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2155 size_t hash_size = 0;
2156 size_t block_size = psa_get_hash_block_size( hash_alg );
2157 psa_status_t status;
2158
Gilles Peskine01126fa2018-07-12 17:04:55 +02002159 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2160 if( status != PSA_SUCCESS )
2161 return( status );
2162 /* From here on, tmp needs to be wiped. */
2163
2164 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2165 if( status != PSA_SUCCESS )
2166 goto exit;
2167
2168 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2169 if( status != PSA_SUCCESS )
2170 goto exit;
2171
2172 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2173 if( status != PSA_SUCCESS )
2174 goto exit;
2175
Gilles Peskined911eb72018-08-14 15:18:45 +02002176 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2177 if( status != PSA_SUCCESS )
2178 goto exit;
2179
2180 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002181
2182exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002183 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002184 return( status );
2185}
2186#endif /* MBEDTLS_MD_C */
2187
mohammad16036df908f2018-04-02 08:34:15 -07002188static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002189 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002190 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002191{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002192 if( ! operation->key_set )
2193 return( PSA_ERROR_BAD_STATE );
2194 if( operation->iv_required && ! operation->iv_set )
2195 return( PSA_ERROR_BAD_STATE );
2196
Gilles Peskine8c9def32018-02-08 10:02:12 +01002197 if( mac_size < operation->mac_size )
2198 return( PSA_ERROR_BUFFER_TOO_SMALL );
2199
Gilles Peskine8c9def32018-02-08 10:02:12 +01002200#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002201 if( operation->alg == PSA_ALG_CMAC )
2202 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002203 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2204 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2205 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002206 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002207 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002208 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002209 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002210 else
2211#endif /* MBEDTLS_CMAC_C */
2212#if defined(MBEDTLS_MD_C)
2213 if( PSA_ALG_IS_HMAC( operation->alg ) )
2214 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002215 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002216 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002217 }
2218 else
2219#endif /* MBEDTLS_MD_C */
2220 {
2221 /* This shouldn't happen if `operation` was initialized by
2222 * a setup function. */
2223 return( PSA_ERROR_BAD_STATE );
2224 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002225}
2226
Gilles Peskineacd4be32018-07-08 19:56:25 +02002227psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2228 uint8_t *mac,
2229 size_t mac_size,
2230 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002231{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002232 psa_status_t status;
2233
2234 /* Fill the output buffer with something that isn't a valid mac
2235 * (barring an attack on the mac and deliberately-crafted input),
2236 * in case the caller doesn't check the return status properly. */
2237 *mac_length = mac_size;
2238 /* If mac_size is 0 then mac may be NULL and then the
2239 * call to memset would have undefined behavior. */
2240 if( mac_size != 0 )
2241 memset( mac, '!', mac_size );
2242
Gilles Peskine89167cb2018-07-08 20:12:23 +02002243 if( ! operation->is_sign )
2244 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002245 status = PSA_ERROR_BAD_STATE;
2246 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002247 }
mohammad16036df908f2018-04-02 08:34:15 -07002248
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002249 status = psa_mac_finish_internal( operation, mac, mac_size );
2250
2251cleanup:
2252 if( status == PSA_SUCCESS )
2253 {
2254 status = psa_mac_abort( operation );
2255 if( status == PSA_SUCCESS )
2256 *mac_length = operation->mac_size;
2257 else
2258 memset( mac, '!', mac_size );
2259 }
2260 else
2261 psa_mac_abort( operation );
2262 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002263}
2264
Gilles Peskineacd4be32018-07-08 19:56:25 +02002265psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2266 const uint8_t *mac,
2267 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002268{
Gilles Peskine828ed142018-06-18 23:25:51 +02002269 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002270 psa_status_t status;
2271
Gilles Peskine89167cb2018-07-08 20:12:23 +02002272 if( operation->is_sign )
2273 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002274 status = PSA_ERROR_BAD_STATE;
2275 goto cleanup;
2276 }
2277 if( operation->mac_size != mac_length )
2278 {
2279 status = PSA_ERROR_INVALID_SIGNATURE;
2280 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002281 }
mohammad16036df908f2018-04-02 08:34:15 -07002282
2283 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002284 actual_mac, sizeof( actual_mac ) );
2285
2286 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2287 status = PSA_ERROR_INVALID_SIGNATURE;
2288
2289cleanup:
2290 if( status == PSA_SUCCESS )
2291 status = psa_mac_abort( operation );
2292 else
2293 psa_mac_abort( operation );
2294
Gilles Peskine3f108122018-12-07 18:14:53 +01002295 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002296
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002297 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002298}
2299
2300
Gilles Peskine20035e32018-02-03 22:44:14 +01002301
Gilles Peskine20035e32018-02-03 22:44:14 +01002302/****************************************************************/
2303/* Asymmetric cryptography */
2304/****************************************************************/
2305
Gilles Peskine2b450e32018-06-27 15:42:46 +02002306#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002307/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002308 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002309static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2310 size_t hash_length,
2311 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002312{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002313 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002314 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002315 *md_alg = mbedtls_md_get_type( md_info );
2316
2317 /* The Mbed TLS RSA module uses an unsigned int for hash length
2318 * parameters. Validate that it fits so that we don't risk an
2319 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002320#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002321 if( hash_length > UINT_MAX )
2322 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002323#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002324
2325#if defined(MBEDTLS_PKCS1_V15)
2326 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2327 * must be correct. */
2328 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2329 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002330 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002331 if( md_info == NULL )
2332 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002333 if( mbedtls_md_get_size( md_info ) != hash_length )
2334 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002335 }
2336#endif /* MBEDTLS_PKCS1_V15 */
2337
2338#if defined(MBEDTLS_PKCS1_V21)
2339 /* PSS requires a hash internally. */
2340 if( PSA_ALG_IS_RSA_PSS( alg ) )
2341 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002342 if( md_info == NULL )
2343 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002344 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002345#endif /* MBEDTLS_PKCS1_V21 */
2346
Gilles Peskine61b91d42018-06-08 16:09:36 +02002347 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002348}
2349
Gilles Peskine2b450e32018-06-27 15:42:46 +02002350static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2351 psa_algorithm_t alg,
2352 const uint8_t *hash,
2353 size_t hash_length,
2354 uint8_t *signature,
2355 size_t signature_size,
2356 size_t *signature_length )
2357{
2358 psa_status_t status;
2359 int ret;
2360 mbedtls_md_type_t md_alg;
2361
2362 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2363 if( status != PSA_SUCCESS )
2364 return( status );
2365
Gilles Peskine630a18a2018-06-29 17:49:35 +02002366 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002367 return( PSA_ERROR_BUFFER_TOO_SMALL );
2368
2369#if defined(MBEDTLS_PKCS1_V15)
2370 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2371 {
2372 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2373 MBEDTLS_MD_NONE );
2374 ret = mbedtls_rsa_pkcs1_sign( rsa,
2375 mbedtls_ctr_drbg_random,
2376 &global_data.ctr_drbg,
2377 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002378 md_alg,
2379 (unsigned int) hash_length,
2380 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002381 signature );
2382 }
2383 else
2384#endif /* MBEDTLS_PKCS1_V15 */
2385#if defined(MBEDTLS_PKCS1_V21)
2386 if( PSA_ALG_IS_RSA_PSS( alg ) )
2387 {
2388 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2389 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2390 mbedtls_ctr_drbg_random,
2391 &global_data.ctr_drbg,
2392 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002393 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002394 (unsigned int) hash_length,
2395 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002396 signature );
2397 }
2398 else
2399#endif /* MBEDTLS_PKCS1_V21 */
2400 {
2401 return( PSA_ERROR_INVALID_ARGUMENT );
2402 }
2403
2404 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002405 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002406 return( mbedtls_to_psa_error( ret ) );
2407}
2408
2409static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2410 psa_algorithm_t alg,
2411 const uint8_t *hash,
2412 size_t hash_length,
2413 const uint8_t *signature,
2414 size_t signature_length )
2415{
2416 psa_status_t status;
2417 int ret;
2418 mbedtls_md_type_t md_alg;
2419
2420 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2421 if( status != PSA_SUCCESS )
2422 return( status );
2423
Gilles Peskine630a18a2018-06-29 17:49:35 +02002424 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002425 return( PSA_ERROR_BUFFER_TOO_SMALL );
2426
2427#if defined(MBEDTLS_PKCS1_V15)
2428 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2429 {
2430 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2431 MBEDTLS_MD_NONE );
2432 ret = mbedtls_rsa_pkcs1_verify( rsa,
2433 mbedtls_ctr_drbg_random,
2434 &global_data.ctr_drbg,
2435 MBEDTLS_RSA_PUBLIC,
2436 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002437 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002438 hash,
2439 signature );
2440 }
2441 else
2442#endif /* MBEDTLS_PKCS1_V15 */
2443#if defined(MBEDTLS_PKCS1_V21)
2444 if( PSA_ALG_IS_RSA_PSS( alg ) )
2445 {
2446 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2447 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2448 mbedtls_ctr_drbg_random,
2449 &global_data.ctr_drbg,
2450 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002451 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002452 (unsigned int) hash_length,
2453 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002454 signature );
2455 }
2456 else
2457#endif /* MBEDTLS_PKCS1_V21 */
2458 {
2459 return( PSA_ERROR_INVALID_ARGUMENT );
2460 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002461
2462 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2463 * the rest of the signature is invalid". This has little use in
2464 * practice and PSA doesn't report this distinction. */
2465 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2466 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002467 return( mbedtls_to_psa_error( ret ) );
2468}
2469#endif /* MBEDTLS_RSA_C */
2470
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002471#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002472/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2473 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2474 * (even though these functions don't modify it). */
2475static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2476 psa_algorithm_t alg,
2477 const uint8_t *hash,
2478 size_t hash_length,
2479 uint8_t *signature,
2480 size_t signature_size,
2481 size_t *signature_length )
2482{
2483 int ret;
2484 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002485 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002486 mbedtls_mpi_init( &r );
2487 mbedtls_mpi_init( &s );
2488
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002489 if( signature_size < 2 * curve_bytes )
2490 {
2491 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2492 goto cleanup;
2493 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002494
Gilles Peskinea05219c2018-11-16 16:02:56 +01002495#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002496 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2497 {
2498 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2499 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2500 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2501 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2502 hash, hash_length,
2503 md_alg ) );
2504 }
2505 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002506#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002507 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002508 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002509 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2510 hash, hash_length,
2511 mbedtls_ctr_drbg_random,
2512 &global_data.ctr_drbg ) );
2513 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002514
2515 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2516 signature,
2517 curve_bytes ) );
2518 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2519 signature + curve_bytes,
2520 curve_bytes ) );
2521
2522cleanup:
2523 mbedtls_mpi_free( &r );
2524 mbedtls_mpi_free( &s );
2525 if( ret == 0 )
2526 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002527 return( mbedtls_to_psa_error( ret ) );
2528}
2529
2530static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2531 const uint8_t *hash,
2532 size_t hash_length,
2533 const uint8_t *signature,
2534 size_t signature_length )
2535{
2536 int ret;
2537 mbedtls_mpi r, s;
2538 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2539 mbedtls_mpi_init( &r );
2540 mbedtls_mpi_init( &s );
2541
2542 if( signature_length != 2 * curve_bytes )
2543 return( PSA_ERROR_INVALID_SIGNATURE );
2544
2545 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2546 signature,
2547 curve_bytes ) );
2548 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2549 signature + curve_bytes,
2550 curve_bytes ) );
2551
2552 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2553 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002554
2555cleanup:
2556 mbedtls_mpi_free( &r );
2557 mbedtls_mpi_free( &s );
2558 return( mbedtls_to_psa_error( ret ) );
2559}
2560#endif /* MBEDTLS_ECDSA_C */
2561
Gilles Peskinec5487a82018-12-03 18:08:14 +01002562psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002563 psa_algorithm_t alg,
2564 const uint8_t *hash,
2565 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002566 uint8_t *signature,
2567 size_t signature_size,
2568 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002569{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002570 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002571 psa_status_t status;
2572
2573 *signature_length = signature_size;
2574
Gilles Peskinec5487a82018-12-03 18:08:14 +01002575 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002576 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002577 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002578 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002579 {
2580 status = PSA_ERROR_INVALID_ARGUMENT;
2581 goto exit;
2582 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002583
Gilles Peskine20035e32018-02-03 22:44:14 +01002584#if defined(MBEDTLS_RSA_C)
2585 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2586 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002587 status = psa_rsa_sign( slot->data.rsa,
2588 alg,
2589 hash, hash_length,
2590 signature, signature_size,
2591 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002592 }
2593 else
2594#endif /* defined(MBEDTLS_RSA_C) */
2595#if defined(MBEDTLS_ECP_C)
2596 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2597 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002598#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002599 if(
2600#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2601 PSA_ALG_IS_ECDSA( alg )
2602#else
2603 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2604#endif
2605 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002606 status = psa_ecdsa_sign( slot->data.ecp,
2607 alg,
2608 hash, hash_length,
2609 signature, signature_size,
2610 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002611 else
2612#endif /* defined(MBEDTLS_ECDSA_C) */
2613 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002614 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002615 }
itayzafrir5c753392018-05-08 11:18:38 +03002616 }
2617 else
2618#endif /* defined(MBEDTLS_ECP_C) */
2619 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002620 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002621 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002622
2623exit:
2624 /* Fill the unused part of the output buffer (the whole buffer on error,
2625 * the trailing part on success) with something that isn't a valid mac
2626 * (barring an attack on the mac and deliberately-crafted input),
2627 * in case the caller doesn't check the return status properly. */
2628 if( status == PSA_SUCCESS )
2629 memset( signature + *signature_length, '!',
2630 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002631 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002632 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002633 /* If signature_size is 0 then we have nothing to do. We must not call
2634 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002635 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002636}
2637
Gilles Peskinec5487a82018-12-03 18:08:14 +01002638psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002639 psa_algorithm_t alg,
2640 const uint8_t *hash,
2641 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002642 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002643 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002644{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002645 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002646 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002647
Gilles Peskinec5487a82018-12-03 18:08:14 +01002648 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002649 if( status != PSA_SUCCESS )
2650 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002651
Gilles Peskine61b91d42018-06-08 16:09:36 +02002652#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002653 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002654 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002655 return( psa_rsa_verify( slot->data.rsa,
2656 alg,
2657 hash, hash_length,
2658 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002659 }
2660 else
2661#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002662#if defined(MBEDTLS_ECP_C)
2663 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2664 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002665#if defined(MBEDTLS_ECDSA_C)
2666 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002667 return( psa_ecdsa_verify( slot->data.ecp,
2668 hash, hash_length,
2669 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002670 else
2671#endif /* defined(MBEDTLS_ECDSA_C) */
2672 {
2673 return( PSA_ERROR_INVALID_ARGUMENT );
2674 }
itayzafrir5c753392018-05-08 11:18:38 +03002675 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002676 else
2677#endif /* defined(MBEDTLS_ECP_C) */
2678 {
2679 return( PSA_ERROR_NOT_SUPPORTED );
2680 }
2681}
2682
Gilles Peskine072ac562018-06-30 00:21:29 +02002683#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2684static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2685 mbedtls_rsa_context *rsa )
2686{
2687 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2688 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2689 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2690 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2691}
2692#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2693
Gilles Peskinec5487a82018-12-03 18:08:14 +01002694psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002695 psa_algorithm_t alg,
2696 const uint8_t *input,
2697 size_t input_length,
2698 const uint8_t *salt,
2699 size_t salt_length,
2700 uint8_t *output,
2701 size_t output_size,
2702 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002703{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002704 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002705 psa_status_t status;
2706
Darryl Green5cc689a2018-07-24 15:34:10 +01002707 (void) input;
2708 (void) input_length;
2709 (void) salt;
2710 (void) output;
2711 (void) output_size;
2712
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002713 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002714
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002715 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2716 return( PSA_ERROR_INVALID_ARGUMENT );
2717
Gilles Peskinec5487a82018-12-03 18:08:14 +01002718 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002719 if( status != PSA_SUCCESS )
2720 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002721 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2722 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002723 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002724
2725#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002726 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002727 {
2728 mbedtls_rsa_context *rsa = slot->data.rsa;
2729 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002730 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002731 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002732#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002733 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002734 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002735 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2736 mbedtls_ctr_drbg_random,
2737 &global_data.ctr_drbg,
2738 MBEDTLS_RSA_PUBLIC,
2739 input_length,
2740 input,
2741 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002742 }
2743 else
2744#endif /* MBEDTLS_PKCS1_V15 */
2745#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002746 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002747 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002748 psa_rsa_oaep_set_padding_mode( alg, rsa );
2749 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2750 mbedtls_ctr_drbg_random,
2751 &global_data.ctr_drbg,
2752 MBEDTLS_RSA_PUBLIC,
2753 salt, salt_length,
2754 input_length,
2755 input,
2756 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002757 }
2758 else
2759#endif /* MBEDTLS_PKCS1_V21 */
2760 {
2761 return( PSA_ERROR_INVALID_ARGUMENT );
2762 }
2763 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002764 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002765 return( mbedtls_to_psa_error( ret ) );
2766 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002767 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002768#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002769 {
2770 return( PSA_ERROR_NOT_SUPPORTED );
2771 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002772}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002773
Gilles Peskinec5487a82018-12-03 18:08:14 +01002774psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002775 psa_algorithm_t alg,
2776 const uint8_t *input,
2777 size_t input_length,
2778 const uint8_t *salt,
2779 size_t salt_length,
2780 uint8_t *output,
2781 size_t output_size,
2782 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002783{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002784 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002785 psa_status_t status;
2786
Darryl Green5cc689a2018-07-24 15:34:10 +01002787 (void) input;
2788 (void) input_length;
2789 (void) salt;
2790 (void) output;
2791 (void) output_size;
2792
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002793 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002794
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002795 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2796 return( PSA_ERROR_INVALID_ARGUMENT );
2797
Gilles Peskinec5487a82018-12-03 18:08:14 +01002798 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002799 if( status != PSA_SUCCESS )
2800 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002801 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2802 return( PSA_ERROR_INVALID_ARGUMENT );
2803
2804#if defined(MBEDTLS_RSA_C)
2805 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2806 {
2807 mbedtls_rsa_context *rsa = slot->data.rsa;
2808 int ret;
2809
Gilles Peskine630a18a2018-06-29 17:49:35 +02002810 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002811 return( PSA_ERROR_INVALID_ARGUMENT );
2812
2813#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002814 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002815 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002816 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2817 mbedtls_ctr_drbg_random,
2818 &global_data.ctr_drbg,
2819 MBEDTLS_RSA_PRIVATE,
2820 output_length,
2821 input,
2822 output,
2823 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002824 }
2825 else
2826#endif /* MBEDTLS_PKCS1_V15 */
2827#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002828 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002829 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002830 psa_rsa_oaep_set_padding_mode( alg, rsa );
2831 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2832 mbedtls_ctr_drbg_random,
2833 &global_data.ctr_drbg,
2834 MBEDTLS_RSA_PRIVATE,
2835 salt, salt_length,
2836 output_length,
2837 input,
2838 output,
2839 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002840 }
2841 else
2842#endif /* MBEDTLS_PKCS1_V21 */
2843 {
2844 return( PSA_ERROR_INVALID_ARGUMENT );
2845 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002846
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002847 return( mbedtls_to_psa_error( ret ) );
2848 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002849 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002850#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002851 {
2852 return( PSA_ERROR_NOT_SUPPORTED );
2853 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002854}
Gilles Peskine20035e32018-02-03 22:44:14 +01002855
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002856
2857
mohammad1603503973b2018-03-12 15:59:30 +02002858/****************************************************************/
2859/* Symmetric cryptography */
2860/****************************************************************/
2861
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002862/* Initialize the cipher operation structure. Once this function has been
2863 * called, psa_cipher_abort can run and will do the right thing. */
2864static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2865 psa_algorithm_t alg )
2866{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002867 if( ! PSA_ALG_IS_CIPHER( alg ) )
2868 {
2869 memset( operation, 0, sizeof( *operation ) );
2870 return( PSA_ERROR_INVALID_ARGUMENT );
2871 }
2872
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002873 operation->alg = alg;
2874 operation->key_set = 0;
2875 operation->iv_set = 0;
2876 operation->iv_required = 1;
2877 operation->iv_size = 0;
2878 operation->block_size = 0;
2879 mbedtls_cipher_init( &operation->ctx.cipher );
2880 return( PSA_SUCCESS );
2881}
2882
Gilles Peskinee553c652018-06-04 16:22:46 +02002883static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002884 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002885 psa_algorithm_t alg,
2886 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002887{
2888 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2889 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002890 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002891 size_t key_bits;
2892 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002893 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2894 PSA_KEY_USAGE_ENCRYPT :
2895 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002896
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002897 status = psa_cipher_init( operation, alg );
2898 if( status != PSA_SUCCESS )
2899 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002900
Gilles Peskinec5487a82018-12-03 18:08:14 +01002901 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002902 if( status != PSA_SUCCESS )
2903 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002904 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002905
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002906 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002907 if( cipher_info == NULL )
2908 return( PSA_ERROR_NOT_SUPPORTED );
2909
mohammad1603503973b2018-03-12 15:59:30 +02002910 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002911 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002912 {
2913 psa_cipher_abort( operation );
2914 return( mbedtls_to_psa_error( ret ) );
2915 }
2916
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002917#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002918 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002919 {
2920 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2921 unsigned char keys[24];
2922 memcpy( keys, slot->data.raw.data, 16 );
2923 memcpy( keys + 16, slot->data.raw.data, 8 );
2924 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2925 keys,
2926 192, cipher_operation );
2927 }
2928 else
2929#endif
2930 {
2931 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2932 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002933 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002934 }
Moran Peker41deec42018-04-04 15:43:05 +03002935 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002936 {
2937 psa_cipher_abort( operation );
2938 return( mbedtls_to_psa_error( ret ) );
2939 }
2940
mohammad16038481e742018-03-18 13:57:31 +02002941#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002942 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002943 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002944 case PSA_ALG_CBC_NO_PADDING:
2945 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2946 MBEDTLS_PADDING_NONE );
2947 break;
2948 case PSA_ALG_CBC_PKCS7:
2949 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2950 MBEDTLS_PADDING_PKCS7 );
2951 break;
2952 default:
2953 /* The algorithm doesn't involve padding. */
2954 ret = 0;
2955 break;
2956 }
2957 if( ret != 0 )
2958 {
2959 psa_cipher_abort( operation );
2960 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002961 }
2962#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2963
mohammad1603503973b2018-03-12 15:59:30 +02002964 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002965 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2966 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2967 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002968 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002969 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002970 }
mohammad1603503973b2018-03-12 15:59:30 +02002971
Moran Peker395db872018-05-31 14:07:14 +03002972 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002973}
2974
Gilles Peskinefe119512018-07-08 21:39:34 +02002975psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002976 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002977 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002978{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002979 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002980}
2981
Gilles Peskinefe119512018-07-08 21:39:34 +02002982psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002983 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002984 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002985{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002986 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002987}
2988
Gilles Peskinefe119512018-07-08 21:39:34 +02002989psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2990 unsigned char *iv,
2991 size_t iv_size,
2992 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002993{
itayzafrir534bd7c2018-08-02 13:56:32 +03002994 psa_status_t status;
2995 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002996 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002997 {
2998 status = PSA_ERROR_BAD_STATE;
2999 goto exit;
3000 }
Moran Peker41deec42018-04-04 15:43:05 +03003001 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003002 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003003 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003004 goto exit;
3005 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003006 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3007 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003008 if( ret != 0 )
3009 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003010 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003011 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003012 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003013
mohammad16038481e742018-03-18 13:57:31 +02003014 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003015 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003016
Moran Peker395db872018-05-31 14:07:14 +03003017exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003018 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003019 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003020 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003021}
3022
Gilles Peskinefe119512018-07-08 21:39:34 +02003023psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3024 const unsigned char *iv,
3025 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003026{
itayzafrir534bd7c2018-08-02 13:56:32 +03003027 psa_status_t status;
3028 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003029 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003030 {
3031 status = PSA_ERROR_BAD_STATE;
3032 goto exit;
3033 }
Moran Pekera28258c2018-05-29 16:25:04 +03003034 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003035 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003036 status = PSA_ERROR_INVALID_ARGUMENT;
3037 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003038 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003039 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3040 status = mbedtls_to_psa_error( ret );
3041exit:
3042 if( status == PSA_SUCCESS )
3043 operation->iv_set = 1;
3044 else
mohammad1603503973b2018-03-12 15:59:30 +02003045 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003046 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003047}
3048
Gilles Peskinee553c652018-06-04 16:22:46 +02003049psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3050 const uint8_t *input,
3051 size_t input_length,
3052 unsigned char *output,
3053 size_t output_size,
3054 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003055{
itayzafrir534bd7c2018-08-02 13:56:32 +03003056 psa_status_t status;
3057 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003058 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003059 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003060 {
3061 /* Take the unprocessed partial block left over from previous
3062 * update calls, if any, plus the input to this call. Remove
3063 * the last partial block, if any. You get the data that will be
3064 * output in this call. */
3065 expected_output_size =
3066 ( operation->ctx.cipher.unprocessed_len + input_length )
3067 / operation->block_size * operation->block_size;
3068 }
3069 else
3070 {
3071 expected_output_size = input_length;
3072 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003073
Gilles Peskine89d789c2018-06-04 17:17:16 +02003074 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003075 {
3076 status = PSA_ERROR_BUFFER_TOO_SMALL;
3077 goto exit;
3078 }
mohammad160382759612018-03-12 18:16:40 +02003079
mohammad1603503973b2018-03-12 15:59:30 +02003080 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003081 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003082 status = mbedtls_to_psa_error( ret );
3083exit:
3084 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003085 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003086 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003087}
3088
Gilles Peskinee553c652018-06-04 16:22:46 +02003089psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3090 uint8_t *output,
3091 size_t output_size,
3092 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003093{
Janos Follath315b51c2018-07-09 16:04:51 +01003094 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
3095 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003096 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003097
mohammad1603503973b2018-03-12 15:59:30 +02003098 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003099 {
Janos Follath315b51c2018-07-09 16:04:51 +01003100 status = PSA_ERROR_BAD_STATE;
3101 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03003102 }
3103 if( operation->iv_required && ! operation->iv_set )
3104 {
Janos Follath315b51c2018-07-09 16:04:51 +01003105 status = PSA_ERROR_BAD_STATE;
3106 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03003107 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003108
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003109 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003110 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3111 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003112 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003113 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003114 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003115 }
3116
Janos Follath315b51c2018-07-09 16:04:51 +01003117 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3118 temp_output_buffer,
3119 output_length );
3120 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003121 {
Janos Follath315b51c2018-07-09 16:04:51 +01003122 status = mbedtls_to_psa_error( cipher_ret );
3123 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003124 }
Janos Follath315b51c2018-07-09 16:04:51 +01003125
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003126 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003127 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003128 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003129 memcpy( output, temp_output_buffer, *output_length );
3130 else
3131 {
Janos Follath315b51c2018-07-09 16:04:51 +01003132 status = PSA_ERROR_BUFFER_TOO_SMALL;
3133 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003134 }
mohammad1603503973b2018-03-12 15:59:30 +02003135
Gilles Peskine3f108122018-12-07 18:14:53 +01003136 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003137 status = psa_cipher_abort( operation );
3138
3139 return( status );
3140
3141error:
3142
3143 *output_length = 0;
3144
Gilles Peskine3f108122018-12-07 18:14:53 +01003145 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003146 (void) psa_cipher_abort( operation );
3147
3148 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003149}
3150
Gilles Peskinee553c652018-06-04 16:22:46 +02003151psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3152{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003153 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003154 {
3155 /* The object has (apparently) been initialized but it is not
3156 * in use. It's ok to call abort on such an object, and there's
3157 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003158 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003159 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003160
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003161 /* Sanity check (shouldn't happen: operation->alg should
3162 * always have been initialized to a valid value). */
3163 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3164 return( PSA_ERROR_BAD_STATE );
3165
mohammad1603503973b2018-03-12 15:59:30 +02003166 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003167
Moran Peker41deec42018-04-04 15:43:05 +03003168 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003169 operation->key_set = 0;
3170 operation->iv_set = 0;
3171 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003172 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003173 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003174
Moran Peker395db872018-05-31 14:07:14 +03003175 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003176}
3177
Gilles Peskinea0655c32018-04-30 17:06:50 +02003178
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003179
mohammad16038cc1cee2018-03-28 01:21:33 +03003180/****************************************************************/
3181/* Key Policy */
3182/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003183
mohammad160327010052018-07-03 13:16:15 +03003184#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003185void psa_key_policy_set_usage( psa_key_policy_t *policy,
3186 psa_key_usage_t usage,
3187 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003188{
mohammad16034eed7572018-03-28 05:14:59 -07003189 policy->usage = usage;
3190 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003191}
3192
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003193psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003194{
mohammad16036df908f2018-04-02 08:34:15 -07003195 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003196}
3197
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003198psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003199{
mohammad16036df908f2018-04-02 08:34:15 -07003200 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003201}
mohammad160327010052018-07-03 13:16:15 +03003202#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003203
Gilles Peskinec5487a82018-12-03 18:08:14 +01003204psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003205 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003206{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003207 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003208 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003209
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003210 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003211 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003212
Gilles Peskinec5487a82018-12-03 18:08:14 +01003213 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003214 if( status != PSA_SUCCESS )
3215 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003216
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003217 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3218 PSA_KEY_USAGE_ENCRYPT |
3219 PSA_KEY_USAGE_DECRYPT |
3220 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003221 PSA_KEY_USAGE_VERIFY |
3222 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003223 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003224
mohammad16036df908f2018-04-02 08:34:15 -07003225 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003226
3227 return( PSA_SUCCESS );
3228}
3229
Gilles Peskinec5487a82018-12-03 18:08:14 +01003230psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003231 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003232{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003233 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003234 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003235
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003236 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003237 return( PSA_ERROR_INVALID_ARGUMENT );
3238
Gilles Peskinec5487a82018-12-03 18:08:14 +01003239 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003240 if( status != PSA_SUCCESS )
3241 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003242
mohammad16036df908f2018-04-02 08:34:15 -07003243 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003244
3245 return( PSA_SUCCESS );
3246}
Gilles Peskine20035e32018-02-03 22:44:14 +01003247
Gilles Peskinea0655c32018-04-30 17:06:50 +02003248
3249
mohammad1603804cd712018-03-20 22:44:08 +02003250/****************************************************************/
3251/* Key Lifetime */
3252/****************************************************************/
3253
Gilles Peskinec5487a82018-12-03 18:08:14 +01003254psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003255 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003256{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003257 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003258 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003259
Gilles Peskinec5487a82018-12-03 18:08:14 +01003260 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003261 if( status != PSA_SUCCESS )
3262 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003263
mohammad1603804cd712018-03-20 22:44:08 +02003264 *lifetime = slot->lifetime;
3265
3266 return( PSA_SUCCESS );
3267}
3268
Gilles Peskine20035e32018-02-03 22:44:14 +01003269
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003270
mohammad16035955c982018-04-26 00:53:03 +03003271/****************************************************************/
3272/* AEAD */
3273/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003274
Gilles Peskineedf9a652018-08-17 18:11:56 +02003275typedef struct
3276{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003277 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003278 const mbedtls_cipher_info_t *cipher_info;
3279 union
3280 {
3281#if defined(MBEDTLS_CCM_C)
3282 mbedtls_ccm_context ccm;
3283#endif /* MBEDTLS_CCM_C */
3284#if defined(MBEDTLS_GCM_C)
3285 mbedtls_gcm_context gcm;
3286#endif /* MBEDTLS_GCM_C */
3287 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003288 psa_algorithm_t core_alg;
3289 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003290 uint8_t tag_length;
3291} aead_operation_t;
3292
Gilles Peskine30a9e412019-01-14 18:36:12 +01003293static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003294{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003295 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003296 {
3297#if defined(MBEDTLS_CCM_C)
3298 case PSA_ALG_CCM:
3299 mbedtls_ccm_free( &operation->ctx.ccm );
3300 break;
3301#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003302#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003303 case PSA_ALG_GCM:
3304 mbedtls_gcm_free( &operation->ctx.gcm );
3305 break;
3306#endif /* MBEDTLS_GCM_C */
3307 }
3308}
3309
3310static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003311 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003312 psa_key_usage_t usage,
3313 psa_algorithm_t alg )
3314{
3315 psa_status_t status;
3316 size_t key_bits;
3317 mbedtls_cipher_id_t cipher_id;
3318
Gilles Peskinec5487a82018-12-03 18:08:14 +01003319 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003320 if( status != PSA_SUCCESS )
3321 return( status );
3322
3323 key_bits = psa_get_key_bits( operation->slot );
3324
3325 operation->cipher_info =
3326 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3327 &cipher_id );
3328 if( operation->cipher_info == NULL )
3329 return( PSA_ERROR_NOT_SUPPORTED );
3330
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003331 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003332 {
3333#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003334 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3335 operation->core_alg = PSA_ALG_CCM;
3336 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003337 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3338 return( PSA_ERROR_INVALID_ARGUMENT );
3339 mbedtls_ccm_init( &operation->ctx.ccm );
3340 status = mbedtls_to_psa_error(
3341 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3342 operation->slot->data.raw.data,
3343 (unsigned int) key_bits ) );
3344 if( status != 0 )
3345 goto cleanup;
3346 break;
3347#endif /* MBEDTLS_CCM_C */
3348
3349#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003350 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3351 operation->core_alg = PSA_ALG_GCM;
3352 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003353 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3354 return( PSA_ERROR_INVALID_ARGUMENT );
3355 mbedtls_gcm_init( &operation->ctx.gcm );
3356 status = mbedtls_to_psa_error(
3357 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3358 operation->slot->data.raw.data,
3359 (unsigned int) key_bits ) );
3360 break;
3361#endif /* MBEDTLS_GCM_C */
3362
3363 default:
3364 return( PSA_ERROR_NOT_SUPPORTED );
3365 }
3366
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003367 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3368 {
3369 status = PSA_ERROR_INVALID_ARGUMENT;
3370 goto cleanup;
3371 }
3372 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3373 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3374 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3375 * In both cases, mbedtls_xxx will validate the tag length below. */
3376
Gilles Peskineedf9a652018-08-17 18:11:56 +02003377 return( PSA_SUCCESS );
3378
3379cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003380 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003381 return( status );
3382}
3383
Gilles Peskinec5487a82018-12-03 18:08:14 +01003384psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003385 psa_algorithm_t alg,
3386 const uint8_t *nonce,
3387 size_t nonce_length,
3388 const uint8_t *additional_data,
3389 size_t additional_data_length,
3390 const uint8_t *plaintext,
3391 size_t plaintext_length,
3392 uint8_t *ciphertext,
3393 size_t ciphertext_size,
3394 size_t *ciphertext_length )
3395{
mohammad16035955c982018-04-26 00:53:03 +03003396 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003397 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003398 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003399
mohammad1603f08a5502018-06-03 15:05:47 +03003400 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003401
Gilles Peskinec5487a82018-12-03 18:08:14 +01003402 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003403 if( status != PSA_SUCCESS )
3404 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003405
Gilles Peskineedf9a652018-08-17 18:11:56 +02003406 /* For all currently supported modes, the tag is at the end of the
3407 * ciphertext. */
3408 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3409 {
3410 status = PSA_ERROR_BUFFER_TOO_SMALL;
3411 goto exit;
3412 }
3413 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003414
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003415#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003416 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003417 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003418 status = mbedtls_to_psa_error(
3419 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3420 MBEDTLS_GCM_ENCRYPT,
3421 plaintext_length,
3422 nonce, nonce_length,
3423 additional_data, additional_data_length,
3424 plaintext, ciphertext,
3425 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003426 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003427 else
3428#endif /* MBEDTLS_GCM_C */
3429#if defined(MBEDTLS_CCM_C)
3430 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003431 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003432 status = mbedtls_to_psa_error(
3433 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3434 plaintext_length,
3435 nonce, nonce_length,
3436 additional_data,
3437 additional_data_length,
3438 plaintext, ciphertext,
3439 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003440 }
mohammad16035c8845f2018-05-09 05:40:09 -07003441 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003442#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003443 {
mohammad1603554faad2018-06-03 15:07:38 +03003444 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003445 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003446
Gilles Peskineedf9a652018-08-17 18:11:56 +02003447 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3448 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003449
Gilles Peskineedf9a652018-08-17 18:11:56 +02003450exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003451 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003452 if( status == PSA_SUCCESS )
3453 *ciphertext_length = plaintext_length + operation.tag_length;
3454 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003455}
3456
Gilles Peskineee652a32018-06-01 19:23:52 +02003457/* Locate the tag in a ciphertext buffer containing the encrypted data
3458 * followed by the tag. Return the length of the part preceding the tag in
3459 * *plaintext_length. This is the size of the plaintext in modes where
3460 * the encrypted data has the same size as the plaintext, such as
3461 * CCM and GCM. */
3462static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3463 const uint8_t *ciphertext,
3464 size_t ciphertext_length,
3465 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003466 const uint8_t **p_tag )
3467{
3468 size_t payload_length;
3469 if( tag_length > ciphertext_length )
3470 return( PSA_ERROR_INVALID_ARGUMENT );
3471 payload_length = ciphertext_length - tag_length;
3472 if( payload_length > plaintext_size )
3473 return( PSA_ERROR_BUFFER_TOO_SMALL );
3474 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003475 return( PSA_SUCCESS );
3476}
3477
Gilles Peskinec5487a82018-12-03 18:08:14 +01003478psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003479 psa_algorithm_t alg,
3480 const uint8_t *nonce,
3481 size_t nonce_length,
3482 const uint8_t *additional_data,
3483 size_t additional_data_length,
3484 const uint8_t *ciphertext,
3485 size_t ciphertext_length,
3486 uint8_t *plaintext,
3487 size_t plaintext_size,
3488 size_t *plaintext_length )
3489{
mohammad16035955c982018-04-26 00:53:03 +03003490 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003491 aead_operation_t operation;
3492 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003493
Gilles Peskineee652a32018-06-01 19:23:52 +02003494 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003495
Gilles Peskinec5487a82018-12-03 18:08:14 +01003496 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003497 if( status != PSA_SUCCESS )
3498 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003499
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003500#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003501 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003502 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003503 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003504 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003505 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003506 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003507 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003508
Gilles Peskineedf9a652018-08-17 18:11:56 +02003509 status = mbedtls_to_psa_error(
3510 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3511 ciphertext_length - operation.tag_length,
3512 nonce, nonce_length,
3513 additional_data,
3514 additional_data_length,
3515 tag, operation.tag_length,
3516 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003517 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003518 else
3519#endif /* MBEDTLS_GCM_C */
3520#if defined(MBEDTLS_CCM_C)
3521 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003522 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003523 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003524 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003525 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003526 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003527 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003528
Gilles Peskineedf9a652018-08-17 18:11:56 +02003529 status = mbedtls_to_psa_error(
3530 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3531 ciphertext_length - operation.tag_length,
3532 nonce, nonce_length,
3533 additional_data,
3534 additional_data_length,
3535 ciphertext, plaintext,
3536 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003537 }
mohammad160339574652018-06-01 04:39:53 -07003538 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003539#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003540 {
mohammad1603554faad2018-06-03 15:07:38 +03003541 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003542 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003543
Gilles Peskineedf9a652018-08-17 18:11:56 +02003544 if( status != PSA_SUCCESS && plaintext_size != 0 )
3545 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003546
Gilles Peskineedf9a652018-08-17 18:11:56 +02003547exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003548 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003549 if( status == PSA_SUCCESS )
3550 *plaintext_length = ciphertext_length - operation.tag_length;
3551 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003552}
3553
Gilles Peskinea0655c32018-04-30 17:06:50 +02003554
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003555
Gilles Peskine20035e32018-02-03 22:44:14 +01003556/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003557/* Generators */
3558/****************************************************************/
3559
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003560#define HKDF_STATE_INIT 0 /* no input yet */
3561#define HKDF_STATE_STARTED 1 /* got salt */
3562#define HKDF_STATE_KEYED 2 /* got key */
3563#define HKDF_STATE_OUTPUT 3 /* output started */
3564
Gilles Peskine969c5d62019-01-16 15:53:06 +01003565static psa_algorithm_t psa_generator_get_kdf_alg(
3566 const psa_crypto_generator_t *generator )
3567{
3568 if ( PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
3569 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( generator->alg ) );
3570 else
3571 return( generator->alg );
3572}
3573
3574
Gilles Peskineeab56e42018-07-12 17:12:33 +02003575psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3576{
3577 psa_status_t status = PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01003578 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
3579 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003580 {
3581 /* The object has (apparently) been initialized but it is not
3582 * in use. It's ok to call abort on such an object, and there's
3583 * nothing to do. */
3584 }
3585 else
Gilles Peskine969c5d62019-01-16 15:53:06 +01003586 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02003587 {
3588 if( generator->ctx.buffer.data != NULL )
3589 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003590 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003591 generator->ctx.buffer.size );
3592 mbedtls_free( generator->ctx.buffer.data );
3593 }
3594 }
3595 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003596#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003597 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003598 {
3599 mbedtls_free( generator->ctx.hkdf.info );
3600 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3601 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003602 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Hanno Becker1aaedc02018-11-16 11:35:34 +00003603 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003604 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003605 {
3606 if( generator->ctx.tls12_prf.key != NULL )
3607 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003608 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003609 generator->ctx.tls12_prf.key_len );
3610 mbedtls_free( generator->ctx.tls12_prf.key );
3611 }
Hanno Becker580fba12018-11-13 20:50:45 +00003612
3613 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3614 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003615 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003616 generator->ctx.tls12_prf.Ai_with_seed_len );
3617 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3618 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003619 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003620 else
3621#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003622 {
3623 status = PSA_ERROR_BAD_STATE;
3624 }
3625 memset( generator, 0, sizeof( *generator ) );
3626 return( status );
3627}
3628
Gilles Peskineeab56e42018-07-12 17:12:33 +02003629psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3630 size_t *capacity)
3631{
3632 *capacity = generator->capacity;
3633 return( PSA_SUCCESS );
3634}
3635
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003636psa_status_t psa_set_generator_capacity( psa_crypto_generator_t *generator,
3637 size_t capacity )
3638{
3639 if( generator->alg == 0 )
3640 return( PSA_ERROR_BAD_STATE );
3641 if( capacity > generator->capacity )
3642 return( PSA_ERROR_INVALID_ARGUMENT );
3643 generator->capacity = capacity;
3644 return( PSA_SUCCESS );
3645}
3646
Gilles Peskinebef7f142018-07-12 17:22:21 +02003647#if defined(MBEDTLS_MD_C)
3648/* Read some bytes from an HKDF-based generator. This performs a chunk
3649 * of the expand phase of the HKDF algorithm. */
3650static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3651 psa_algorithm_t hash_alg,
3652 uint8_t *output,
3653 size_t output_length )
3654{
3655 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3656 psa_status_t status;
3657
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003658 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
3659 return( PSA_ERROR_BAD_STATE );
3660 hkdf->state = HKDF_STATE_OUTPUT;
3661
Gilles Peskinebef7f142018-07-12 17:22:21 +02003662 while( output_length != 0 )
3663 {
3664 /* Copy what remains of the current block */
3665 uint8_t n = hash_length - hkdf->offset_in_block;
3666 if( n > output_length )
3667 n = (uint8_t) output_length;
3668 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3669 output += n;
3670 output_length -= n;
3671 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003672 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003673 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003674 /* We can't be wanting more output after block 0xff, otherwise
3675 * the capacity check in psa_generator_read() would have
3676 * prevented this call. It could happen only if the generator
3677 * object was corrupted or if this function is called directly
3678 * inside the library. */
3679 if( hkdf->block_number == 0xff )
3680 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003681
3682 /* We need a new block */
3683 ++hkdf->block_number;
3684 hkdf->offset_in_block = 0;
3685 status = psa_hmac_setup_internal( &hkdf->hmac,
3686 hkdf->prk, hash_length,
3687 hash_alg );
3688 if( status != PSA_SUCCESS )
3689 return( status );
3690 if( hkdf->block_number != 1 )
3691 {
3692 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3693 hkdf->output_block,
3694 hash_length );
3695 if( status != PSA_SUCCESS )
3696 return( status );
3697 }
3698 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3699 hkdf->info,
3700 hkdf->info_length );
3701 if( status != PSA_SUCCESS )
3702 return( status );
3703 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3704 &hkdf->block_number, 1 );
3705 if( status != PSA_SUCCESS )
3706 return( status );
3707 status = psa_hmac_finish_internal( &hkdf->hmac,
3708 hkdf->output_block,
3709 sizeof( hkdf->output_block ) );
3710 if( status != PSA_SUCCESS )
3711 return( status );
3712 }
3713
3714 return( PSA_SUCCESS );
3715}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003716
3717static psa_status_t psa_generator_tls12_prf_generate_next_block(
3718 psa_tls12_prf_generator_t *tls12_prf,
3719 psa_algorithm_t alg )
3720{
3721 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3722 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3723 psa_hmac_internal_data hmac;
3724 psa_status_t status, cleanup_status;
3725
Hanno Becker3b339e22018-11-13 20:56:14 +00003726 unsigned char *Ai;
3727 size_t Ai_len;
3728
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003729 /* We can't be wanting more output after block 0xff, otherwise
3730 * the capacity check in psa_generator_read() would have
3731 * prevented this call. It could happen only if the generator
3732 * object was corrupted or if this function is called directly
3733 * inside the library. */
3734 if( tls12_prf->block_number == 0xff )
3735 return( PSA_ERROR_BAD_STATE );
3736
3737 /* We need a new block */
3738 ++tls12_prf->block_number;
3739 tls12_prf->offset_in_block = 0;
3740
3741 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3742 *
3743 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3744 *
3745 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3746 * HMAC_hash(secret, A(2) + seed) +
3747 * HMAC_hash(secret, A(3) + seed) + ...
3748 *
3749 * A(0) = seed
3750 * A(i) = HMAC_hash( secret, A(i-1) )
3751 *
3752 * The `psa_tls12_prf_generator` structures saves the block
3753 * `HMAC_hash(secret, A(i) + seed)` from which the output
3754 * is currently extracted as `output_block`, while
3755 * `A(i) + seed` is stored in `Ai_with_seed`.
3756 *
3757 * Generating a new block means recalculating `Ai_with_seed`
3758 * from the A(i)-part of it, and afterwards recalculating
3759 * `output_block`.
3760 *
3761 * A(0) is computed at setup time.
3762 *
3763 */
3764
3765 psa_hmac_init_internal( &hmac );
3766
3767 /* We must distinguish the calculation of A(1) from those
3768 * of A(2) and higher, because A(0)=seed has a different
3769 * length than the other A(i). */
3770 if( tls12_prf->block_number == 1 )
3771 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003772 Ai = tls12_prf->Ai_with_seed + hash_length;
3773 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003774 }
3775 else
3776 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003777 Ai = tls12_prf->Ai_with_seed;
3778 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003779 }
3780
Hanno Becker3b339e22018-11-13 20:56:14 +00003781 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3782 status = psa_hmac_setup_internal( &hmac,
3783 tls12_prf->key,
3784 tls12_prf->key_len,
3785 hash_alg );
3786 if( status != PSA_SUCCESS )
3787 goto cleanup;
3788
3789 status = psa_hash_update( &hmac.hash_ctx,
3790 Ai, Ai_len );
3791 if( status != PSA_SUCCESS )
3792 goto cleanup;
3793
3794 status = psa_hmac_finish_internal( &hmac,
3795 tls12_prf->Ai_with_seed,
3796 hash_length );
3797 if( status != PSA_SUCCESS )
3798 goto cleanup;
3799
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003800 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3801 status = psa_hmac_setup_internal( &hmac,
3802 tls12_prf->key,
3803 tls12_prf->key_len,
3804 hash_alg );
3805 if( status != PSA_SUCCESS )
3806 goto cleanup;
3807
3808 status = psa_hash_update( &hmac.hash_ctx,
3809 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003810 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003811 if( status != PSA_SUCCESS )
3812 goto cleanup;
3813
3814 status = psa_hmac_finish_internal( &hmac,
3815 tls12_prf->output_block,
3816 hash_length );
3817 if( status != PSA_SUCCESS )
3818 goto cleanup;
3819
3820cleanup:
3821
3822 cleanup_status = psa_hmac_abort_internal( &hmac );
3823 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3824 status = cleanup_status;
3825
3826 return( status );
3827}
3828
3829/* Read some bytes from an TLS-1.2-PRF-based generator.
3830 * See Section 5 of RFC 5246. */
3831static psa_status_t psa_generator_tls12_prf_read(
3832 psa_tls12_prf_generator_t *tls12_prf,
3833 psa_algorithm_t alg,
3834 uint8_t *output,
3835 size_t output_length )
3836{
3837 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3838 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3839 psa_status_t status;
3840
3841 while( output_length != 0 )
3842 {
3843 /* Copy what remains of the current block */
3844 uint8_t n = hash_length - tls12_prf->offset_in_block;
3845
3846 /* Check if we have fully processed the current block. */
3847 if( n == 0 )
3848 {
3849 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3850 alg );
3851 if( status != PSA_SUCCESS )
3852 return( status );
3853
3854 continue;
3855 }
3856
3857 if( n > output_length )
3858 n = (uint8_t) output_length;
3859 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3860 n );
3861 output += n;
3862 output_length -= n;
3863 tls12_prf->offset_in_block += n;
3864 }
3865
3866 return( PSA_SUCCESS );
3867}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003868#endif /* MBEDTLS_MD_C */
3869
Gilles Peskineeab56e42018-07-12 17:12:33 +02003870psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3871 uint8_t *output,
3872 size_t output_length )
3873{
3874 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01003875 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003876
3877 if( output_length > generator->capacity )
3878 {
3879 generator->capacity = 0;
3880 /* Go through the error path to wipe all confidential data now
3881 * that the generator object is useless. */
3882 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3883 goto exit;
3884 }
3885 if( output_length == 0 &&
Gilles Peskine969c5d62019-01-16 15:53:06 +01003886 generator->capacity == 0 && kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003887 {
3888 /* Edge case: this is a blank or finished generator, and 0
3889 * bytes were requested. The right error in this case could
3890 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3891 * INSUFFICIENT_CAPACITY, which is right for a finished
3892 * generator, for consistency with the case when
3893 * output_length > 0. */
3894 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3895 }
3896 generator->capacity -= output_length;
3897
Gilles Peskine969c5d62019-01-16 15:53:06 +01003898 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02003899 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003900 /* Initially, the capacity of a selection generator is always
3901 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3902 * abbreviated in this comment as `size`. When the remaining
3903 * capacity is `c`, the next bytes to serve start `c` bytes
3904 * from the end of the buffer, i.e. `size - c` from the
3905 * beginning of the buffer. Since `generator->capacity` was just
3906 * decremented above, we need to serve the bytes from
3907 * `size - generator->capacity - output_length` to
3908 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003909 size_t offset =
3910 generator->ctx.buffer.size - generator->capacity - output_length;
3911 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3912 status = PSA_SUCCESS;
3913 }
3914 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003915#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003916 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003917 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01003918 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003919 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3920 output, output_length );
3921 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003922 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
3923 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003924 {
3925 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003926 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003927 output_length );
3928 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003929 else
3930#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003931 {
3932 return( PSA_ERROR_BAD_STATE );
3933 }
3934
3935exit:
3936 if( status != PSA_SUCCESS )
3937 {
3938 psa_generator_abort( generator );
3939 memset( output, '!', output_length );
3940 }
3941 return( status );
3942}
3943
Gilles Peskine08542d82018-07-19 17:05:42 +02003944#if defined(MBEDTLS_DES_C)
3945static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3946{
3947 if( data_size >= 8 )
3948 mbedtls_des_key_set_parity( data );
3949 if( data_size >= 16 )
3950 mbedtls_des_key_set_parity( data + 8 );
3951 if( data_size >= 24 )
3952 mbedtls_des_key_set_parity( data + 16 );
3953}
3954#endif /* MBEDTLS_DES_C */
3955
Gilles Peskinec5487a82018-12-03 18:08:14 +01003956psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003957 psa_key_type_t type,
3958 size_t bits,
3959 psa_crypto_generator_t *generator )
3960{
3961 uint8_t *data = NULL;
3962 size_t bytes = PSA_BITS_TO_BYTES( bits );
3963 psa_status_t status;
3964
3965 if( ! key_type_is_raw_bytes( type ) )
3966 return( PSA_ERROR_INVALID_ARGUMENT );
3967 if( bits % 8 != 0 )
3968 return( PSA_ERROR_INVALID_ARGUMENT );
3969 data = mbedtls_calloc( 1, bytes );
3970 if( data == NULL )
3971 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3972
3973 status = psa_generator_read( generator, data, bytes );
3974 if( status != PSA_SUCCESS )
3975 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003976#if defined(MBEDTLS_DES_C)
3977 if( type == PSA_KEY_TYPE_DES )
3978 psa_des_set_key_parity( data, bytes );
3979#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003980 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003981
3982exit:
3983 mbedtls_free( data );
3984 return( status );
3985}
3986
3987
3988
3989/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003990/* Key derivation */
3991/****************************************************************/
3992
Gilles Peskinea05219c2018-11-16 16:02:56 +01003993#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003994/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003995 * of the HKDF algorithm.
3996 *
3997 * Note that if this function fails, you must call psa_generator_abort()
3998 * to potentially free embedded data structures and wipe confidential data.
3999 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004000static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004001 const uint8_t *secret,
4002 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004003 psa_algorithm_t hash_alg,
4004 const uint8_t *salt,
4005 size_t salt_length,
4006 const uint8_t *label,
4007 size_t label_length )
4008{
4009 psa_status_t status;
4010 status = psa_hmac_setup_internal( &hkdf->hmac,
4011 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02004012 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004013 if( status != PSA_SUCCESS )
4014 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004015 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004016 if( status != PSA_SUCCESS )
4017 return( status );
4018 status = psa_hmac_finish_internal( &hkdf->hmac,
4019 hkdf->prk,
4020 sizeof( hkdf->prk ) );
4021 if( status != PSA_SUCCESS )
4022 return( status );
4023 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4024 hkdf->block_number = 0;
4025 hkdf->info_length = label_length;
4026 if( label_length != 0 )
4027 {
4028 hkdf->info = mbedtls_calloc( 1, label_length );
4029 if( hkdf->info == NULL )
4030 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4031 memcpy( hkdf->info, label, label_length );
4032 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004033 hkdf->state = HKDF_STATE_KEYED;
4034 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02004035 return( PSA_SUCCESS );
4036}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004037#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004038
Gilles Peskinea05219c2018-11-16 16:02:56 +01004039#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004040/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4041 *
4042 * Note that if this function fails, you must call psa_generator_abort()
4043 * to potentially free embedded data structures and wipe confidential data.
4044 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004045static psa_status_t psa_generator_tls12_prf_setup(
4046 psa_tls12_prf_generator_t *tls12_prf,
4047 const unsigned char *key,
4048 size_t key_len,
4049 psa_algorithm_t hash_alg,
4050 const uint8_t *salt,
4051 size_t salt_length,
4052 const uint8_t *label,
4053 size_t label_length )
4054{
4055 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004056 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4057 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004058
4059 tls12_prf->key = mbedtls_calloc( 1, key_len );
4060 if( tls12_prf->key == NULL )
4061 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4062 tls12_prf->key_len = key_len;
4063 memcpy( tls12_prf->key, key, key_len );
4064
Hanno Becker580fba12018-11-13 20:50:45 +00004065 overflow = ( salt_length + label_length < salt_length ) ||
4066 ( salt_length + label_length + hash_length < hash_length );
4067 if( overflow )
4068 return( PSA_ERROR_INVALID_ARGUMENT );
4069
4070 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4071 if( tls12_prf->Ai_with_seed == NULL )
4072 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4073 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4074
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004075 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4076 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004077 if( label_length != 0 )
4078 {
4079 memcpy( tls12_prf->Ai_with_seed + hash_length,
4080 label, label_length );
4081 }
4082
4083 if( salt_length != 0 )
4084 {
4085 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4086 salt, salt_length );
4087 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004088
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004089 /* The first block gets generated when
4090 * psa_generator_read() is called. */
4091 tls12_prf->block_number = 0;
4092 tls12_prf->offset_in_block = hash_length;
4093
4094 return( PSA_SUCCESS );
4095}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004096
4097/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4098static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4099 psa_tls12_prf_generator_t *tls12_prf,
4100 const unsigned char *psk,
4101 size_t psk_len,
4102 psa_algorithm_t hash_alg,
4103 const uint8_t *salt,
4104 size_t salt_length,
4105 const uint8_t *label,
4106 size_t label_length )
4107{
4108 psa_status_t status;
4109 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4110
4111 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4112 return( PSA_ERROR_INVALID_ARGUMENT );
4113
4114 /* Quoting RFC 4279, Section 2:
4115 *
4116 * The premaster secret is formed as follows: if the PSK is N octets
4117 * long, concatenate a uint16 with the value N, N zero octets, a second
4118 * uint16 with the value N, and the PSK itself.
4119 */
4120
4121 pms[0] = ( psk_len >> 8 ) & 0xff;
4122 pms[1] = ( psk_len >> 0 ) & 0xff;
4123 memset( pms + 2, 0, psk_len );
4124 pms[2 + psk_len + 0] = pms[0];
4125 pms[2 + psk_len + 1] = pms[1];
4126 memcpy( pms + 4 + psk_len, psk, psk_len );
4127
4128 status = psa_generator_tls12_prf_setup( tls12_prf,
4129 pms, 4 + 2 * psk_len,
4130 hash_alg,
4131 salt, salt_length,
4132 label, label_length );
4133
Gilles Peskine3f108122018-12-07 18:14:53 +01004134 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004135 return( status );
4136}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004137#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004138
Gilles Peskine346797d2018-11-16 16:05:06 +01004139/* Note that if this function fails, you must call psa_generator_abort()
4140 * to potentially free embedded data structures and wipe confidential data.
4141 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004142static psa_status_t psa_key_derivation_internal(
4143 psa_crypto_generator_t *generator,
4144 const uint8_t *secret, size_t secret_length,
4145 psa_algorithm_t alg,
4146 const uint8_t *salt, size_t salt_length,
4147 const uint8_t *label, size_t label_length,
4148 size_t capacity )
4149{
4150 psa_status_t status;
4151 size_t max_capacity;
4152
4153 /* Set generator->alg even on failure so that abort knows what to do. */
4154 generator->alg = alg;
4155
Gilles Peskine751d9652018-09-18 12:05:44 +02004156 if( alg == PSA_ALG_SELECT_RAW )
4157 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004158 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004159 if( salt_length != 0 )
4160 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004161 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004162 if( label_length != 0 )
4163 return( PSA_ERROR_INVALID_ARGUMENT );
4164 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4165 if( generator->ctx.buffer.data == NULL )
4166 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4167 memcpy( generator->ctx.buffer.data, secret, secret_length );
4168 generator->ctx.buffer.size = secret_length;
4169 max_capacity = secret_length;
4170 status = PSA_SUCCESS;
4171 }
4172 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004173#if defined(MBEDTLS_MD_C)
4174 if( PSA_ALG_IS_HKDF( alg ) )
4175 {
4176 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4177 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4178 if( hash_size == 0 )
4179 return( PSA_ERROR_NOT_SUPPORTED );
4180 max_capacity = 255 * hash_size;
4181 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4182 secret, secret_length,
4183 hash_alg,
4184 salt, salt_length,
4185 label, label_length );
4186 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004187 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4188 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4189 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004190 {
4191 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4192 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4193
4194 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4195 if( hash_alg != PSA_ALG_SHA_256 &&
4196 hash_alg != PSA_ALG_SHA_384 )
4197 {
4198 return( PSA_ERROR_NOT_SUPPORTED );
4199 }
4200
4201 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004202
4203 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4204 {
4205 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4206 secret, secret_length,
4207 hash_alg, salt, salt_length,
4208 label, label_length );
4209 }
4210 else
4211 {
4212 status = psa_generator_tls12_psk_to_ms_setup(
4213 &generator->ctx.tls12_prf,
4214 secret, secret_length,
4215 hash_alg, salt, salt_length,
4216 label, label_length );
4217 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004218 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004219 else
4220#endif
4221 {
4222 return( PSA_ERROR_NOT_SUPPORTED );
4223 }
4224
4225 if( status != PSA_SUCCESS )
4226 return( status );
4227
4228 if( capacity <= max_capacity )
4229 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004230 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4231 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004232 else
4233 return( PSA_ERROR_INVALID_ARGUMENT );
4234
4235 return( PSA_SUCCESS );
4236}
4237
Gilles Peskineea0fb492018-07-12 17:17:20 +02004238psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004239 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004240 psa_algorithm_t alg,
4241 const uint8_t *salt,
4242 size_t salt_length,
4243 const uint8_t *label,
4244 size_t label_length,
4245 size_t capacity )
4246{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004247 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004248 psa_status_t status;
4249
4250 if( generator->alg != 0 )
4251 return( PSA_ERROR_BAD_STATE );
4252
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004253 /* Make sure that alg is a key derivation algorithm. This prevents
4254 * key selection algorithms, which psa_key_derivation_internal
4255 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004256 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4257 return( PSA_ERROR_INVALID_ARGUMENT );
4258
Gilles Peskinec5487a82018-12-03 18:08:14 +01004259 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004260 if( status != PSA_SUCCESS )
4261 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004262
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004263 if( slot->type != PSA_KEY_TYPE_DERIVE )
4264 return( PSA_ERROR_INVALID_ARGUMENT );
4265
4266 status = psa_key_derivation_internal( generator,
4267 slot->data.raw.data,
4268 slot->data.raw.bytes,
4269 alg,
4270 salt, salt_length,
4271 label, label_length,
4272 capacity );
4273 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004274 psa_generator_abort( generator );
4275 return( status );
4276}
4277
Gilles Peskine969c5d62019-01-16 15:53:06 +01004278static psa_status_t psa_key_derivation_setup_kdf(
4279 psa_crypto_generator_t *generator,
4280 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004281{
Gilles Peskine969c5d62019-01-16 15:53:06 +01004282 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004283#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004284 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4285 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4286 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004287 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004288 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004289 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4290 if( hash_size == 0 )
4291 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004292 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4293 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004294 ! ( hash_alg == PSA_ALG_SHA_256 && hash_alg == PSA_ALG_SHA_384 ) )
4295 {
4296 return( PSA_ERROR_NOT_SUPPORTED );
4297 }
4298 generator->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004299 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004300 }
4301#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004302 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004303 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004304}
4305
4306psa_status_t psa_key_derivation_setup( psa_crypto_generator_t *generator,
4307 psa_algorithm_t alg )
4308{
4309 psa_status_t status;
4310
4311 if( generator->alg != 0 )
4312 return( PSA_ERROR_BAD_STATE );
4313
Gilles Peskine6843c292019-01-18 16:44:49 +01004314 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4315 return( PSA_ERROR_INVALID_ARGUMENT );
4316 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004317 {
4318 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine6843c292019-01-18 16:44:49 +01004319 status = psa_key_derivation_setup_kdf( generator, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004320 }
4321 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4322 {
4323 status = psa_key_derivation_setup_kdf( generator, alg );
4324 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004325 else
4326 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004327
4328 if( status == PSA_SUCCESS )
4329 generator->alg = alg;
4330 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004331}
4332
4333#if defined(MBEDTLS_MD_C)
4334static psa_status_t psa_hkdf_input( psa_hkdf_generator_t *hkdf,
4335 psa_algorithm_t hash_alg,
4336 psa_key_derivation_step_t step,
4337 const uint8_t *data,
4338 size_t data_length )
4339{
4340 psa_status_t status;
4341 switch( step )
4342 {
4343 case PSA_KDF_STEP_SALT:
4344 if( hkdf->state == HKDF_STATE_INIT )
4345 {
4346 status = psa_hmac_setup_internal( &hkdf->hmac,
4347 data, data_length,
4348 hash_alg );
4349 if( status != PSA_SUCCESS )
4350 return( status );
4351 hkdf->state = HKDF_STATE_STARTED;
4352 return( PSA_SUCCESS );
4353 }
4354 else
4355 return( PSA_ERROR_BAD_STATE );
4356 break;
4357 case PSA_KDF_STEP_SECRET:
4358 /* If no salt was provided, use an empty salt. */
4359 if( hkdf->state == HKDF_STATE_INIT )
4360 {
4361 status = psa_hmac_setup_internal( &hkdf->hmac,
4362 NULL, 0,
4363 PSA_ALG_HMAC( hash_alg ) );
4364 if( status != PSA_SUCCESS )
4365 return( status );
4366 hkdf->state = HKDF_STATE_STARTED;
4367 }
4368 if( hkdf->state == HKDF_STATE_STARTED )
4369 {
4370 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4371 data, data_length );
4372 if( status != PSA_SUCCESS )
4373 return( status );
4374 status = psa_hmac_finish_internal( &hkdf->hmac,
4375 hkdf->prk,
4376 sizeof( hkdf->prk ) );
4377 if( status != PSA_SUCCESS )
4378 return( status );
4379 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4380 hkdf->block_number = 0;
4381 hkdf->state = HKDF_STATE_KEYED;
4382 return( PSA_SUCCESS );
4383 }
4384 else
4385 return( PSA_ERROR_BAD_STATE );
4386 break;
4387 case PSA_KDF_STEP_INFO:
4388 if( hkdf->state == HKDF_STATE_OUTPUT )
4389 return( PSA_ERROR_BAD_STATE );
4390 if( hkdf->info_set )
4391 return( PSA_ERROR_BAD_STATE );
4392 hkdf->info_length = data_length;
4393 if( data_length != 0 )
4394 {
4395 hkdf->info = mbedtls_calloc( 1, data_length );
4396 if( hkdf->info == NULL )
4397 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4398 memcpy( hkdf->info, data, data_length );
4399 }
4400 hkdf->info_set = 1;
4401 return( PSA_SUCCESS );
4402 default:
4403 return( PSA_ERROR_INVALID_ARGUMENT );
4404 }
4405}
4406#endif /* MBEDTLS_MD_C */
4407
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004408static psa_status_t psa_key_derivation_input_raw(
4409 psa_crypto_generator_t *generator,
4410 psa_key_derivation_step_t step,
4411 const uint8_t *data,
4412 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004413{
4414 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004415 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004416
Gilles Peskine969c5d62019-01-16 15:53:06 +01004417 if( kdf_alg == PSA_ALG_SELECT_RAW )
4418 {
4419 if( generator->capacity != 0 )
4420 return( PSA_ERROR_INVALID_ARGUMENT );
4421 generator->ctx.buffer.data = mbedtls_calloc( 1, data_length );
4422 if( generator->ctx.buffer.data == NULL )
4423 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4424 memcpy( generator->ctx.buffer.data, data, data_length );
4425 generator->ctx.buffer.size = data_length;
4426 generator->capacity = data_length;
4427 status = PSA_SUCCESS;
4428 }
4429 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004430#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004431 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004432 {
4433 status = psa_hkdf_input( &generator->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004434 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004435 step, data, data_length );
4436 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004437 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004438#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004439#if defined(MBEDTLS_MD_C)
4440 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004441 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4442 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004443 {
4444 // TODO
4445 status = PSA_ERROR_NOT_SUPPORTED;
4446 }
4447 else
4448#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004449 {
4450 /* This can't happen unless the generator object was not initialized */
4451 return( PSA_ERROR_BAD_STATE );
4452 }
4453
4454 if( status != PSA_SUCCESS )
4455 psa_generator_abort( generator );
4456 return( status );
4457}
4458
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004459psa_status_t psa_key_derivation_input_bytes( psa_crypto_generator_t *generator,
4460 psa_key_derivation_step_t step,
4461 const uint8_t *data,
4462 size_t data_length )
4463{
4464 switch( step )
4465 {
4466 case PSA_KDF_STEP_LABEL:
4467 case PSA_KDF_STEP_SALT:
4468 case PSA_KDF_STEP_INFO:
4469 return( psa_key_derivation_input_raw( generator, step,
4470 data, data_length ) );
4471 default:
4472 return( PSA_ERROR_INVALID_ARGUMENT );
4473 }
4474}
4475
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004476psa_status_t psa_key_derivation_input_key( psa_crypto_generator_t *generator,
4477 psa_key_derivation_step_t step,
4478 psa_key_handle_t handle )
4479{
4480 psa_key_slot_t *slot;
4481 psa_status_t status;
4482 status = psa_get_key_from_slot( handle, &slot,
4483 PSA_KEY_USAGE_DERIVE,
4484 generator->alg );
4485 if( status != PSA_SUCCESS )
4486 return( status );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004487 // TODO: for a key agreement algorithm, allow the corresponding key type and step
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004488 if( slot->type != PSA_KEY_TYPE_DERIVE )
4489 return( PSA_ERROR_INVALID_ARGUMENT );
4490 /* Don't allow a key to be used as an input that is usually public.
4491 * This is debatable. It's ok from a cryptographic perspective to
4492 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004493 * the material should be dedicated to a particular input step,
4494 * otherwise this may allow the key to be used in an unintended way
4495 * and leak values derived from the key. So be conservative. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004496 if( step != PSA_KDF_STEP_SECRET )
4497 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004498 return( psa_key_derivation_input_raw( generator,
4499 step,
4500 slot->data.raw.data,
4501 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004502}
4503
Gilles Peskineea0fb492018-07-12 17:17:20 +02004504
4505
4506/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004507/* Key agreement */
4508/****************************************************************/
4509
Gilles Peskinea05219c2018-11-16 16:02:56 +01004510#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004511static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4512 size_t peer_key_length,
4513 const mbedtls_ecp_keypair *our_key,
4514 uint8_t *shared_secret,
4515 size_t shared_secret_size,
4516 size_t *shared_secret_length )
4517{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004518 mbedtls_ecp_keypair *their_key = NULL;
4519 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004520 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004521 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004522
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004523 status = psa_import_ec_public_key(
4524 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4525 peer_key, peer_key_length,
4526 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004527 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004528 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01004529
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004530 status = mbedtls_to_psa_error(
4531 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4532 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004533 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004534 status = mbedtls_to_psa_error(
4535 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4536 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004537 goto exit;
4538
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004539 status = mbedtls_to_psa_error(
4540 mbedtls_ecdh_calc_secret( &ecdh,
4541 shared_secret_length,
4542 shared_secret, shared_secret_size,
4543 mbedtls_ctr_drbg_random,
4544 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004545
4546exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004547 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004548 mbedtls_ecp_keypair_free( their_key );
4549 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004550 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004551}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004552#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004553
Gilles Peskine01d718c2018-09-18 12:01:02 +02004554#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4555
Gilles Peskine346797d2018-11-16 16:05:06 +01004556/* Note that if this function fails, you must call psa_generator_abort()
4557 * to potentially free embedded data structures and wipe confidential data.
4558 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004559static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004560 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004561 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004562 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004563 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004564{
4565 psa_status_t status;
4566 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4567 size_t shared_secret_length = 0;
4568
4569 /* Step 1: run the secret agreement algorithm to generate the shared
4570 * secret. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004571 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( generator->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004572 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004573#if defined(MBEDTLS_ECDH_C)
Gilles Peskine6843c292019-01-18 16:44:49 +01004574 case PSA_ALG_ECDH:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004575 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4576 return( PSA_ERROR_INVALID_ARGUMENT );
4577 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4578 private_key->data.ecp,
4579 shared_secret,
4580 sizeof( shared_secret ),
4581 &shared_secret_length );
4582 break;
4583#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004584 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004585 (void) private_key;
4586 (void) peer_key;
4587 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004588 return( PSA_ERROR_NOT_SUPPORTED );
4589 }
4590 if( status != PSA_SUCCESS )
4591 goto exit;
4592
4593 /* Step 2: set up the key derivation to generate key material from
4594 * the shared secret. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004595 status = psa_key_derivation_input_raw( generator, step,
4596 shared_secret, shared_secret_length );
4597
Gilles Peskine01d718c2018-09-18 12:01:02 +02004598exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004599 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004600 return( status );
4601}
4602
4603psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004604 psa_key_derivation_step_t step,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004605 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004606 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004607 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004608{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004609 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004610 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004611 if( ! PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004612 return( PSA_ERROR_INVALID_ARGUMENT );
4613 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004614 PSA_KEY_USAGE_DERIVE, generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004615 if( status != PSA_SUCCESS )
4616 return( status );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004617 status = psa_key_agreement_internal( generator, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01004618 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004619 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01004620 if( status != PSA_SUCCESS )
4621 psa_generator_abort( generator );
4622 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004623}
4624
4625
4626
4627/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004628/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004629/****************************************************************/
4630
4631psa_status_t psa_generate_random( uint8_t *output,
4632 size_t output_size )
4633{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004634 int ret;
4635 GUARD_MODULE_INITIALIZED;
4636
4637 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004638 return( mbedtls_to_psa_error( ret ) );
4639}
4640
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004641#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004642
4643/* Support function for error conversion between psa_its error codes to psa crypto */
4644static psa_status_t its_to_psa_error( psa_its_status_t ret )
4645{
4646 switch( ret )
4647 {
4648 case PSA_ITS_SUCCESS:
4649 return( PSA_SUCCESS );
4650
4651 case PSA_ITS_ERROR_KEY_NOT_FOUND:
4652 return( PSA_ERROR_EMPTY_SLOT );
4653
4654 case PSA_ITS_ERROR_STORAGE_FAILURE:
4655 return( PSA_ERROR_STORAGE_FAILURE );
4656
4657 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4658 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4659
4660 case PSA_ITS_ERROR_INVALID_KEY:
Jaeden Amero58600552018-11-30 12:04:38 +00004661 case PSA_ITS_ERROR_OFFSET_INVALID:
avolinski13beb102018-11-20 16:51:49 +02004662 case PSA_ITS_ERROR_INCORRECT_SIZE:
4663 case PSA_ITS_ERROR_BAD_POINTER:
4664 return( PSA_ERROR_INVALID_ARGUMENT );
4665
4666 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4667 return( PSA_ERROR_NOT_SUPPORTED );
4668
4669 case PSA_ITS_ERROR_WRITE_ONCE:
4670 return( PSA_ERROR_OCCUPIED_SLOT );
4671
4672 default:
4673 return( PSA_ERROR_UNKNOWN_ERROR );
4674 }
4675}
4676
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004677psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4678 size_t seed_size )
4679{
4680 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004681 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004682 struct psa_its_info_t p_info;
4683 if( global_data.initialized )
4684 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004685
4686 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4687 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4688 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4689 return( PSA_ERROR_INVALID_ARGUMENT );
4690
avolinski0d2c2662018-11-21 17:31:07 +02004691 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004692 status = its_to_psa_error( its_status );
4693
4694 if( PSA_ITS_ERROR_KEY_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004695 {
avolinski0d2c2662018-11-21 17:31:07 +02004696 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004697 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004698 }
avolinski13beb102018-11-20 16:51:49 +02004699 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004700 {
4701 /* You should not be here. Seed needs to be injected only once */
4702 status = PSA_ERROR_NOT_PERMITTED;
4703 }
4704 return( status );
4705}
4706#endif
4707
Gilles Peskinec5487a82018-12-03 18:08:14 +01004708psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004709 psa_key_type_t type,
4710 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004711 const void *extra,
4712 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004713{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004714 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004715 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004716
Gilles Peskine53d991e2018-07-12 01:14:59 +02004717 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004718 return( PSA_ERROR_INVALID_ARGUMENT );
4719
Gilles Peskinec5487a82018-12-03 18:08:14 +01004720 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004721 if( status != PSA_SUCCESS )
4722 return( status );
4723
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004724 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004725 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004726 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004727 if( status != PSA_SUCCESS )
4728 return( status );
4729 status = psa_generate_random( slot->data.raw.data,
4730 slot->data.raw.bytes );
4731 if( status != PSA_SUCCESS )
4732 {
4733 mbedtls_free( slot->data.raw.data );
4734 return( status );
4735 }
4736#if defined(MBEDTLS_DES_C)
4737 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004738 psa_des_set_key_parity( slot->data.raw.data,
4739 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004740#endif /* MBEDTLS_DES_C */
4741 }
4742 else
4743
4744#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4745 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4746 {
4747 mbedtls_rsa_context *rsa;
4748 int ret;
4749 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004750 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4751 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004752 /* Accept only byte-aligned keys, for the same reasons as
4753 * in psa_import_rsa_key(). */
4754 if( bits % 8 != 0 )
4755 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004756 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004757 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004758 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004759 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004760 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004761#if INT_MAX < 0xffffffff
4762 /* Check that the uint32_t value passed by the caller fits
4763 * in the range supported by this implementation. */
4764 if( p->e > INT_MAX )
4765 return( PSA_ERROR_NOT_SUPPORTED );
4766#endif
4767 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004768 }
4769 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4770 if( rsa == NULL )
4771 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4772 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4773 ret = mbedtls_rsa_gen_key( rsa,
4774 mbedtls_ctr_drbg_random,
4775 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004776 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004777 exponent );
4778 if( ret != 0 )
4779 {
4780 mbedtls_rsa_free( rsa );
4781 mbedtls_free( rsa );
4782 return( mbedtls_to_psa_error( ret ) );
4783 }
4784 slot->data.rsa = rsa;
4785 }
4786 else
4787#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4788
4789#if defined(MBEDTLS_ECP_C)
4790 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4791 {
4792 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4793 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4794 const mbedtls_ecp_curve_info *curve_info =
4795 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4796 mbedtls_ecp_keypair *ecp;
4797 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004798 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004799 return( PSA_ERROR_NOT_SUPPORTED );
4800 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4801 return( PSA_ERROR_NOT_SUPPORTED );
4802 if( curve_info->bit_size != bits )
4803 return( PSA_ERROR_INVALID_ARGUMENT );
4804 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4805 if( ecp == NULL )
4806 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4807 mbedtls_ecp_keypair_init( ecp );
4808 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4809 mbedtls_ctr_drbg_random,
4810 &global_data.ctr_drbg );
4811 if( ret != 0 )
4812 {
4813 mbedtls_ecp_keypair_free( ecp );
4814 mbedtls_free( ecp );
4815 return( mbedtls_to_psa_error( ret ) );
4816 }
4817 slot->data.ecp = ecp;
4818 }
4819 else
4820#endif /* MBEDTLS_ECP_C */
4821
4822 return( PSA_ERROR_NOT_SUPPORTED );
4823
4824 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004825
4826#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4827 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4828 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004829 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004830 }
4831#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4832
4833 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004834}
4835
4836
4837/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004838/* Module setup */
4839/****************************************************************/
4840
Gilles Peskine5e769522018-11-20 21:59:56 +01004841psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4842 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4843 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4844{
4845 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4846 return( PSA_ERROR_BAD_STATE );
4847 global_data.entropy_init = entropy_init;
4848 global_data.entropy_free = entropy_free;
4849 return( PSA_SUCCESS );
4850}
4851
Gilles Peskinee59236f2018-01-27 23:32:46 +01004852void mbedtls_psa_crypto_free( void )
4853{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004854 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004855 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4856 {
4857 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004858 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004859 }
4860 /* Wipe all remaining data, including configuration.
4861 * In particular, this sets all state indicator to the value
4862 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004863 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004864}
4865
4866psa_status_t psa_crypto_init( void )
4867{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004868 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004869 const unsigned char drbg_seed[] = "PSA";
4870
Gilles Peskinec6b69072018-11-20 21:42:52 +01004871 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004872 if( global_data.initialized != 0 )
4873 return( PSA_SUCCESS );
4874
Gilles Peskine5e769522018-11-20 21:59:56 +01004875 /* Set default configuration if
4876 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4877 if( global_data.entropy_init == NULL )
4878 global_data.entropy_init = mbedtls_entropy_init;
4879 if( global_data.entropy_free == NULL )
4880 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004881
Gilles Peskinec6b69072018-11-20 21:42:52 +01004882 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004883 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004884 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004885 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004886 status = mbedtls_to_psa_error(
4887 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4888 mbedtls_entropy_func,
4889 &global_data.entropy,
4890 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4891 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004892 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004893 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004894
Gilles Peskine66fb1262018-12-10 16:29:04 +01004895 status = psa_initialize_key_slots( );
4896 if( status != PSA_SUCCESS )
4897 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004898
4899 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004900 global_data.initialized = 1;
4901
Gilles Peskinee59236f2018-01-27 23:32:46 +01004902exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004903 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004904 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004905 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004906}
4907
4908#endif /* MBEDTLS_PSA_CRYPTO_C */