blob: a00c7d6ad51b11bf716f548aa2f31e0c46e51aef [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 Peskine30f77cd2019-01-14 16:06:39 +0100750/** Test whether a policy permits an algorithm.
751 *
752 * The caller must test usage flags separately.
753 */
754static int psa_key_policy_permits( const psa_key_policy_t *policy,
755 psa_algorithm_t alg )
756{
757 /* Common case: the policy only allows alg. */
758 if( alg == policy->alg )
759 return( 1 );
760 /* If policy->alg is a hash-and-sign with a wildcard for the hash,
761 * and alg is the same hash-and-sign family with any hash,
762 * then alg is compliant with policy->alg. */
763 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) &&
764 PSA_ALG_SIGN_GET_HASH( policy->alg ) == PSA_ALG_ANY_HASH )
765 {
766 return( ( policy->alg & ~PSA_ALG_HASH_MASK ) ==
767 ( alg & ~PSA_ALG_HASH_MASK ) );
768 }
769 /* If it isn't permitted, it's forbidden. */
770 return( 0 );
771}
772
Darryl Green06fd18d2018-07-16 11:21:11 +0100773/** Retrieve a slot which must contain a key. The key must have allow all the
774 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
775 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100776static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100777 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100778 psa_key_usage_t usage,
779 psa_algorithm_t alg )
780{
781 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100782 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100783
784 *p_slot = NULL;
785
Gilles Peskinec5487a82018-12-03 18:08:14 +0100786 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100787 if( status != PSA_SUCCESS )
788 return( status );
789 if( slot->type == PSA_KEY_TYPE_NONE )
790 return( PSA_ERROR_EMPTY_SLOT );
791
792 /* Enforce that usage policy for the key slot contains all the flags
793 * required by the usage parameter. There is one exception: public
794 * keys can always be exported, so we treat public key objects as
795 * if they had the export flag. */
796 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
797 usage &= ~PSA_KEY_USAGE_EXPORT;
798 if( ( slot->policy.usage & usage ) != usage )
799 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100800
801 /* Enforce that the usage policy permits the requested algortihm. */
802 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100803 return( PSA_ERROR_NOT_PERMITTED );
804
805 *p_slot = slot;
806 return( PSA_SUCCESS );
807}
Darryl Green940d72c2018-07-13 13:18:51 +0100808
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100809/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100810static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000811{
812 if( slot->type == PSA_KEY_TYPE_NONE )
813 {
814 /* No key material to clean. */
815 }
816 else if( key_type_is_raw_bytes( slot->type ) )
817 {
818 mbedtls_free( slot->data.raw.data );
819 }
820 else
821#if defined(MBEDTLS_RSA_C)
822 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
823 {
824 mbedtls_rsa_free( slot->data.rsa );
825 mbedtls_free( slot->data.rsa );
826 }
827 else
828#endif /* defined(MBEDTLS_RSA_C) */
829#if defined(MBEDTLS_ECP_C)
830 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
831 {
832 mbedtls_ecp_keypair_free( slot->data.ecp );
833 mbedtls_free( slot->data.ecp );
834 }
835 else
836#endif /* defined(MBEDTLS_ECP_C) */
837 {
838 /* Shouldn't happen: the key type is not any type that we
839 * put in. */
840 return( PSA_ERROR_TAMPERING_DETECTED );
841 }
842
843 return( PSA_SUCCESS );
844}
845
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100846static void psa_abort_operations_using_key( psa_key_slot_t *slot )
847{
848 /*TODO*/
849 (void) slot;
850}
851
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100852/** Completely wipe a slot in memory, including its policy.
853 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100854psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100855{
856 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine5f25dd02019-01-14 18:24:53 +0100857 psa_abort_operations_using_key( slot );
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100858 /* At this point, key material and other type-specific content has
859 * been wiped. Clear remaining metadata. We can call memset and not
860 * zeroize because the metadata is not particularly sensitive. */
861 memset( slot, 0, sizeof( *slot ) );
862 return( status );
863}
864
Gilles Peskinec5487a82018-12-03 18:08:14 +0100865psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100866 psa_key_type_t type,
867 const uint8_t *data,
868 size_t data_length )
869{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100870 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100871 psa_status_t status;
872
Gilles Peskinec5487a82018-12-03 18:08:14 +0100873 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100874 if( status != PSA_SUCCESS )
875 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100876
877 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100878
879 status = psa_import_key_into_slot( slot, data, data_length );
880 if( status != PSA_SUCCESS )
881 {
882 slot->type = PSA_KEY_TYPE_NONE;
883 return( status );
884 }
885
Darryl Greend49a4992018-06-18 17:27:26 +0100886#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
887 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
888 {
889 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100890 status = psa_save_persistent_key( slot->persistent_storage_id,
891 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100892 data_length );
893 if( status != PSA_SUCCESS )
894 {
895 (void) psa_remove_key_data_from_memory( slot );
896 slot->type = PSA_KEY_TYPE_NONE;
897 }
898 }
899#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
900
901 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100902}
903
Gilles Peskinec5487a82018-12-03 18:08:14 +0100904psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100905{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100906 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100907 psa_status_t status = PSA_SUCCESS;
908 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100909
Gilles Peskinec5487a82018-12-03 18:08:14 +0100910 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200911 if( status != PSA_SUCCESS )
912 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100913#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
914 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
915 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100916 storage_status =
917 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100918 }
919#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100920 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100921 if( status != PSA_SUCCESS )
922 return( status );
923 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100924}
925
Gilles Peskineb870b182018-07-06 16:02:09 +0200926/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100927static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200928{
929 if( key_type_is_raw_bytes( slot->type ) )
930 return( slot->data.raw.bytes * 8 );
931#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200932 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100933 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200934#endif /* defined(MBEDTLS_RSA_C) */
935#if defined(MBEDTLS_ECP_C)
936 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
937 return( slot->data.ecp->grp.pbits );
938#endif /* defined(MBEDTLS_ECP_C) */
939 /* Shouldn't happen except on an empty slot. */
940 return( 0 );
941}
942
Gilles Peskinec5487a82018-12-03 18:08:14 +0100943psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200944 psa_key_type_t *type,
945 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100946{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100947 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200948 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100949
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100950 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200951 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100952 if( bits != NULL )
953 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100954 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200955 if( status != PSA_SUCCESS )
956 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200957
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100958 if( slot->type == PSA_KEY_TYPE_NONE )
959 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200960 if( type != NULL )
961 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200962 if( bits != NULL )
963 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100964 return( PSA_SUCCESS );
965}
966
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000967#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +0000968static int pk_write_pubkey_simple( mbedtls_pk_context *key,
969 unsigned char *buf, size_t size )
970{
971 int ret;
972 unsigned char *c;
973 size_t len = 0;
974
975 c = buf + size;
976
977 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
978
979 return( (int) len );
980}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000981#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +0000982
Gilles Peskine2f060a82018-12-04 17:12:32 +0100983static psa_status_t psa_internal_export_key( psa_key_slot_t *slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200984 uint8_t *data,
985 size_t data_size,
986 size_t *data_length,
987 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100988{
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100989 *data_length = 0;
990
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200991 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300992 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300993
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200994 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995 {
996 if( slot->data.raw.bytes > data_size )
997 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +0100998 if( data_size != 0 )
999 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001000 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001001 memset( data + slot->data.raw.bytes, 0,
1002 data_size - slot->data.raw.bytes );
1003 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001004 *data_length = slot->data.raw.bytes;
1005 return( PSA_SUCCESS );
1006 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001007#if defined(MBEDTLS_ECP_C)
1008 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1009 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001010 psa_status_t status;
1011
Gilles Peskine188c71e2018-10-29 19:26:02 +01001012 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1013 if( bytes > data_size )
1014 return( PSA_ERROR_BUFFER_TOO_SMALL );
1015 status = mbedtls_to_psa_error(
1016 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1017 if( status != PSA_SUCCESS )
1018 return( status );
1019 memset( data + bytes, 0, data_size - bytes );
1020 *data_length = bytes;
1021 return( PSA_SUCCESS );
1022 }
1023#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001024 else
Moran Peker17e36e12018-05-02 12:55:20 +03001025 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001026#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001027 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001028 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001029 {
Moran Pekera998bc62018-04-16 18:16:20 +03001030 mbedtls_pk_context pk;
1031 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001032 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001033 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001034#if defined(MBEDTLS_RSA_C)
1035 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001036 pk.pk_info = &mbedtls_rsa_info;
1037 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001038#else
1039 return( PSA_ERROR_NOT_SUPPORTED );
1040#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001041 }
1042 else
1043 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001044#if defined(MBEDTLS_ECP_C)
1045 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001046 pk.pk_info = &mbedtls_eckey_info;
1047 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001048#else
1049 return( PSA_ERROR_NOT_SUPPORTED );
1050#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001051 }
Moran Pekerd7326592018-05-29 16:56:39 +03001052 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001053 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001054 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001055 }
Moran Peker17e36e12018-05-02 12:55:20 +03001056 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001057 {
Moran Peker17e36e12018-05-02 12:55:20 +03001058 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001059 }
Moran Peker60364322018-04-29 11:34:58 +03001060 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001061 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001062 /* If data_size is 0 then data may be NULL and then the
1063 * call to memset would have undefined behavior. */
1064 if( data_size != 0 )
1065 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001066 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001067 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001068 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1069 * Move the data to the beginning and erase remaining data
1070 * at the original location. */
1071 if( 2 * (size_t) ret <= data_size )
1072 {
1073 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001074 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001075 }
1076 else if( (size_t) ret < data_size )
1077 {
1078 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001079 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001080 }
Moran Pekera998bc62018-04-16 18:16:20 +03001081 *data_length = ret;
1082 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001083 }
1084 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001085#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001086 {
1087 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001088 it is valid for a special-purpose implementation to omit
1089 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001090 return( PSA_ERROR_NOT_SUPPORTED );
1091 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001092 }
1093}
1094
Gilles Peskinec5487a82018-12-03 18:08:14 +01001095psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001096 uint8_t *data,
1097 size_t data_size,
1098 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001099{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001100 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001101 psa_status_t status;
1102
1103 /* Set the key to empty now, so that even when there are errors, we always
1104 * set data_length to a value between 0 and data_size. On error, setting
1105 * the key to empty is a good choice because an empty key representation is
1106 * unlikely to be accepted anywhere. */
1107 *data_length = 0;
1108
1109 /* Export requires the EXPORT flag. There is an exception for public keys,
1110 * which don't require any flag, but psa_get_key_from_slot takes
1111 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001112 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001113 if( status != PSA_SUCCESS )
1114 return( status );
1115 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001116 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001117}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001118
Gilles Peskinec5487a82018-12-03 18:08:14 +01001119psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001120 uint8_t *data,
1121 size_t data_size,
1122 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001123{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001124 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001125 psa_status_t status;
1126
1127 /* Set the key to empty now, so that even when there are errors, we always
1128 * set data_length to a value between 0 and data_size. On error, setting
1129 * the key to empty is a good choice because an empty key representation is
1130 * unlikely to be accepted anywhere. */
1131 *data_length = 0;
1132
1133 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001134 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001135 if( status != PSA_SUCCESS )
1136 return( status );
1137 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001138 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001139}
1140
Darryl Green0c6575a2018-11-07 16:05:30 +00001141#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001142static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001143 size_t bits )
1144{
1145 psa_status_t status;
1146 uint8_t *data;
1147 size_t key_length;
1148 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1149 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001150 if( data == NULL )
1151 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001152 /* Get key data in export format */
1153 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1154 if( status != PSA_SUCCESS )
1155 {
1156 slot->type = PSA_KEY_TYPE_NONE;
1157 goto exit;
1158 }
1159 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001160 status = psa_save_persistent_key( slot->persistent_storage_id,
1161 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001162 data, key_length );
1163 if( status != PSA_SUCCESS )
1164 {
1165 slot->type = PSA_KEY_TYPE_NONE;
1166 }
1167exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001168 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001169 mbedtls_free( data );
1170 return( status );
1171}
1172#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1173
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001174
1175
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001176/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001177/* Message digests */
1178/****************************************************************/
1179
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001180static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001181{
1182 switch( alg )
1183 {
1184#if defined(MBEDTLS_MD2_C)
1185 case PSA_ALG_MD2:
1186 return( &mbedtls_md2_info );
1187#endif
1188#if defined(MBEDTLS_MD4_C)
1189 case PSA_ALG_MD4:
1190 return( &mbedtls_md4_info );
1191#endif
1192#if defined(MBEDTLS_MD5_C)
1193 case PSA_ALG_MD5:
1194 return( &mbedtls_md5_info );
1195#endif
1196#if defined(MBEDTLS_RIPEMD160_C)
1197 case PSA_ALG_RIPEMD160:
1198 return( &mbedtls_ripemd160_info );
1199#endif
1200#if defined(MBEDTLS_SHA1_C)
1201 case PSA_ALG_SHA_1:
1202 return( &mbedtls_sha1_info );
1203#endif
1204#if defined(MBEDTLS_SHA256_C)
1205 case PSA_ALG_SHA_224:
1206 return( &mbedtls_sha224_info );
1207 case PSA_ALG_SHA_256:
1208 return( &mbedtls_sha256_info );
1209#endif
1210#if defined(MBEDTLS_SHA512_C)
1211 case PSA_ALG_SHA_384:
1212 return( &mbedtls_sha384_info );
1213 case PSA_ALG_SHA_512:
1214 return( &mbedtls_sha512_info );
1215#endif
1216 default:
1217 return( NULL );
1218 }
1219}
1220
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001221psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1222{
1223 switch( operation->alg )
1224 {
Gilles Peskine81736312018-06-26 15:04:31 +02001225 case 0:
1226 /* The object has (apparently) been initialized but it is not
1227 * in use. It's ok to call abort on such an object, and there's
1228 * nothing to do. */
1229 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001230#if defined(MBEDTLS_MD2_C)
1231 case PSA_ALG_MD2:
1232 mbedtls_md2_free( &operation->ctx.md2 );
1233 break;
1234#endif
1235#if defined(MBEDTLS_MD4_C)
1236 case PSA_ALG_MD4:
1237 mbedtls_md4_free( &operation->ctx.md4 );
1238 break;
1239#endif
1240#if defined(MBEDTLS_MD5_C)
1241 case PSA_ALG_MD5:
1242 mbedtls_md5_free( &operation->ctx.md5 );
1243 break;
1244#endif
1245#if defined(MBEDTLS_RIPEMD160_C)
1246 case PSA_ALG_RIPEMD160:
1247 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1248 break;
1249#endif
1250#if defined(MBEDTLS_SHA1_C)
1251 case PSA_ALG_SHA_1:
1252 mbedtls_sha1_free( &operation->ctx.sha1 );
1253 break;
1254#endif
1255#if defined(MBEDTLS_SHA256_C)
1256 case PSA_ALG_SHA_224:
1257 case PSA_ALG_SHA_256:
1258 mbedtls_sha256_free( &operation->ctx.sha256 );
1259 break;
1260#endif
1261#if defined(MBEDTLS_SHA512_C)
1262 case PSA_ALG_SHA_384:
1263 case PSA_ALG_SHA_512:
1264 mbedtls_sha512_free( &operation->ctx.sha512 );
1265 break;
1266#endif
1267 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001268 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001269 }
1270 operation->alg = 0;
1271 return( PSA_SUCCESS );
1272}
1273
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001274psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001275 psa_algorithm_t alg )
1276{
1277 int ret;
1278 operation->alg = 0;
1279 switch( alg )
1280 {
1281#if defined(MBEDTLS_MD2_C)
1282 case PSA_ALG_MD2:
1283 mbedtls_md2_init( &operation->ctx.md2 );
1284 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1285 break;
1286#endif
1287#if defined(MBEDTLS_MD4_C)
1288 case PSA_ALG_MD4:
1289 mbedtls_md4_init( &operation->ctx.md4 );
1290 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1291 break;
1292#endif
1293#if defined(MBEDTLS_MD5_C)
1294 case PSA_ALG_MD5:
1295 mbedtls_md5_init( &operation->ctx.md5 );
1296 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1297 break;
1298#endif
1299#if defined(MBEDTLS_RIPEMD160_C)
1300 case PSA_ALG_RIPEMD160:
1301 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1302 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1303 break;
1304#endif
1305#if defined(MBEDTLS_SHA1_C)
1306 case PSA_ALG_SHA_1:
1307 mbedtls_sha1_init( &operation->ctx.sha1 );
1308 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1309 break;
1310#endif
1311#if defined(MBEDTLS_SHA256_C)
1312 case PSA_ALG_SHA_224:
1313 mbedtls_sha256_init( &operation->ctx.sha256 );
1314 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1315 break;
1316 case PSA_ALG_SHA_256:
1317 mbedtls_sha256_init( &operation->ctx.sha256 );
1318 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1319 break;
1320#endif
1321#if defined(MBEDTLS_SHA512_C)
1322 case PSA_ALG_SHA_384:
1323 mbedtls_sha512_init( &operation->ctx.sha512 );
1324 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1325 break;
1326 case PSA_ALG_SHA_512:
1327 mbedtls_sha512_init( &operation->ctx.sha512 );
1328 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1329 break;
1330#endif
1331 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001332 return( PSA_ALG_IS_HASH( alg ) ?
1333 PSA_ERROR_NOT_SUPPORTED :
1334 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001335 }
1336 if( ret == 0 )
1337 operation->alg = alg;
1338 else
1339 psa_hash_abort( operation );
1340 return( mbedtls_to_psa_error( ret ) );
1341}
1342
1343psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1344 const uint8_t *input,
1345 size_t input_length )
1346{
1347 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001348
1349 /* Don't require hash implementations to behave correctly on a
1350 * zero-length input, which may have an invalid pointer. */
1351 if( input_length == 0 )
1352 return( PSA_SUCCESS );
1353
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001354 switch( operation->alg )
1355 {
1356#if defined(MBEDTLS_MD2_C)
1357 case PSA_ALG_MD2:
1358 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1359 input, input_length );
1360 break;
1361#endif
1362#if defined(MBEDTLS_MD4_C)
1363 case PSA_ALG_MD4:
1364 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1365 input, input_length );
1366 break;
1367#endif
1368#if defined(MBEDTLS_MD5_C)
1369 case PSA_ALG_MD5:
1370 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1371 input, input_length );
1372 break;
1373#endif
1374#if defined(MBEDTLS_RIPEMD160_C)
1375 case PSA_ALG_RIPEMD160:
1376 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1377 input, input_length );
1378 break;
1379#endif
1380#if defined(MBEDTLS_SHA1_C)
1381 case PSA_ALG_SHA_1:
1382 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1383 input, input_length );
1384 break;
1385#endif
1386#if defined(MBEDTLS_SHA256_C)
1387 case PSA_ALG_SHA_224:
1388 case PSA_ALG_SHA_256:
1389 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1390 input, input_length );
1391 break;
1392#endif
1393#if defined(MBEDTLS_SHA512_C)
1394 case PSA_ALG_SHA_384:
1395 case PSA_ALG_SHA_512:
1396 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1397 input, input_length );
1398 break;
1399#endif
1400 default:
1401 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1402 break;
1403 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001404
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001405 if( ret != 0 )
1406 psa_hash_abort( operation );
1407 return( mbedtls_to_psa_error( ret ) );
1408}
1409
1410psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1411 uint8_t *hash,
1412 size_t hash_size,
1413 size_t *hash_length )
1414{
itayzafrir40835d42018-08-02 13:14:17 +03001415 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001416 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001417 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001418
1419 /* Fill the output buffer with something that isn't a valid hash
1420 * (barring an attack on the hash and deliberately-crafted input),
1421 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001422 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001423 /* If hash_size is 0 then hash may be NULL and then the
1424 * call to memset would have undefined behavior. */
1425 if( hash_size != 0 )
1426 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001427
1428 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001429 {
1430 status = PSA_ERROR_BUFFER_TOO_SMALL;
1431 goto exit;
1432 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001433
1434 switch( operation->alg )
1435 {
1436#if defined(MBEDTLS_MD2_C)
1437 case PSA_ALG_MD2:
1438 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1439 break;
1440#endif
1441#if defined(MBEDTLS_MD4_C)
1442 case PSA_ALG_MD4:
1443 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1444 break;
1445#endif
1446#if defined(MBEDTLS_MD5_C)
1447 case PSA_ALG_MD5:
1448 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1449 break;
1450#endif
1451#if defined(MBEDTLS_RIPEMD160_C)
1452 case PSA_ALG_RIPEMD160:
1453 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1454 break;
1455#endif
1456#if defined(MBEDTLS_SHA1_C)
1457 case PSA_ALG_SHA_1:
1458 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1459 break;
1460#endif
1461#if defined(MBEDTLS_SHA256_C)
1462 case PSA_ALG_SHA_224:
1463 case PSA_ALG_SHA_256:
1464 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1465 break;
1466#endif
1467#if defined(MBEDTLS_SHA512_C)
1468 case PSA_ALG_SHA_384:
1469 case PSA_ALG_SHA_512:
1470 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1471 break;
1472#endif
1473 default:
1474 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1475 break;
1476 }
itayzafrir40835d42018-08-02 13:14:17 +03001477 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001478
itayzafrir40835d42018-08-02 13:14:17 +03001479exit:
1480 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001481 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001482 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001483 return( psa_hash_abort( operation ) );
1484 }
1485 else
1486 {
1487 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001488 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001489 }
1490}
1491
Gilles Peskine2d277862018-06-18 15:41:12 +02001492psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1493 const uint8_t *hash,
1494 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001495{
1496 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1497 size_t actual_hash_length;
1498 psa_status_t status = psa_hash_finish( operation,
1499 actual_hash, sizeof( actual_hash ),
1500 &actual_hash_length );
1501 if( status != PSA_SUCCESS )
1502 return( status );
1503 if( actual_hash_length != hash_length )
1504 return( PSA_ERROR_INVALID_SIGNATURE );
1505 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1506 return( PSA_ERROR_INVALID_SIGNATURE );
1507 return( PSA_SUCCESS );
1508}
1509
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001510psa_status_t psa_hash_clone(const psa_hash_operation_t *source_operation,
1511 psa_hash_operation_t *target_operation)
1512{
1513 if( target_operation->alg != 0 )
1514 return( PSA_ERROR_BAD_STATE );
1515
1516 switch( source_operation->alg )
1517 {
1518 case 0:
1519 return( PSA_ERROR_BAD_STATE );
1520#if defined(MBEDTLS_MD2_C)
1521 case PSA_ALG_MD2:
1522 mbedtls_md2_clone( &target_operation->ctx.md2,
1523 &source_operation->ctx.md2 );
1524 break;
1525#endif
1526#if defined(MBEDTLS_MD4_C)
1527 case PSA_ALG_MD4:
1528 mbedtls_md4_clone( &target_operation->ctx.md4,
1529 &source_operation->ctx.md4 );
1530 break;
1531#endif
1532#if defined(MBEDTLS_MD5_C)
1533 case PSA_ALG_MD5:
1534 mbedtls_md5_clone( &target_operation->ctx.md5,
1535 &source_operation->ctx.md5 );
1536 break;
1537#endif
1538#if defined(MBEDTLS_RIPEMD160_C)
1539 case PSA_ALG_RIPEMD160:
1540 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1541 &source_operation->ctx.ripemd160 );
1542 break;
1543#endif
1544#if defined(MBEDTLS_SHA1_C)
1545 case PSA_ALG_SHA_1:
1546 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1547 &source_operation->ctx.sha1 );
1548 break;
1549#endif
1550#if defined(MBEDTLS_SHA256_C)
1551 case PSA_ALG_SHA_224:
1552 case PSA_ALG_SHA_256:
1553 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1554 &source_operation->ctx.sha256 );
1555 break;
1556#endif
1557#if defined(MBEDTLS_SHA512_C)
1558 case PSA_ALG_SHA_384:
1559 case PSA_ALG_SHA_512:
1560 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1561 &source_operation->ctx.sha512 );
1562 break;
1563#endif
1564 default:
1565 return( PSA_ERROR_NOT_SUPPORTED );
1566 }
1567
1568 target_operation->alg = source_operation->alg;
1569 return( PSA_SUCCESS );
1570}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001571
1572
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001573/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001574/* MAC */
1575/****************************************************************/
1576
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001577static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001578 psa_algorithm_t alg,
1579 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001580 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001581 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001582{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001583 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001584 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001585
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001586 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001587 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001588
Gilles Peskine8c9def32018-02-08 10:02:12 +01001589 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1590 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001591 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001592 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001593 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001594 mode = MBEDTLS_MODE_STREAM;
1595 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001596 case PSA_ALG_CTR:
1597 mode = MBEDTLS_MODE_CTR;
1598 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001599 case PSA_ALG_CFB:
1600 mode = MBEDTLS_MODE_CFB;
1601 break;
1602 case PSA_ALG_OFB:
1603 mode = MBEDTLS_MODE_OFB;
1604 break;
1605 case PSA_ALG_CBC_NO_PADDING:
1606 mode = MBEDTLS_MODE_CBC;
1607 break;
1608 case PSA_ALG_CBC_PKCS7:
1609 mode = MBEDTLS_MODE_CBC;
1610 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001611 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001612 mode = MBEDTLS_MODE_CCM;
1613 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001614 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001615 mode = MBEDTLS_MODE_GCM;
1616 break;
1617 default:
1618 return( NULL );
1619 }
1620 }
1621 else if( alg == PSA_ALG_CMAC )
1622 mode = MBEDTLS_MODE_ECB;
1623 else if( alg == PSA_ALG_GMAC )
1624 mode = MBEDTLS_MODE_GCM;
1625 else
1626 return( NULL );
1627
1628 switch( key_type )
1629 {
1630 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001631 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001632 break;
1633 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001634 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1635 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001636 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001637 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001638 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001639 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001640 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1641 * but two-key Triple-DES is functionally three-key Triple-DES
1642 * with K1=K3, so that's how we present it to mbedtls. */
1643 if( key_bits == 128 )
1644 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001645 break;
1646 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001647 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001648 break;
1649 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001650 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001651 break;
1652 default:
1653 return( NULL );
1654 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001655 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001656 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001657
Jaeden Amero23bbb752018-06-26 14:16:54 +01001658 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1659 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001660}
1661
Gilles Peskinea05219c2018-11-16 16:02:56 +01001662#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001663static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001664{
Gilles Peskine2d277862018-06-18 15:41:12 +02001665 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001666 {
1667 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001668 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001669 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001670 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001671 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001672 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001673 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001674 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001675 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001676 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001677 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001678 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001679 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001680 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001681 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001682 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001683 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001684 return( 128 );
1685 default:
1686 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001687 }
1688}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001689#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001690
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001691/* Initialize the MAC operation structure. Once this function has been
1692 * called, psa_mac_abort can run and will do the right thing. */
1693static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1694 psa_algorithm_t alg )
1695{
1696 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1697
1698 operation->alg = alg;
1699 operation->key_set = 0;
1700 operation->iv_set = 0;
1701 operation->iv_required = 0;
1702 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001703 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001704
1705#if defined(MBEDTLS_CMAC_C)
1706 if( alg == PSA_ALG_CMAC )
1707 {
1708 operation->iv_required = 0;
1709 mbedtls_cipher_init( &operation->ctx.cmac );
1710 status = PSA_SUCCESS;
1711 }
1712 else
1713#endif /* MBEDTLS_CMAC_C */
1714#if defined(MBEDTLS_MD_C)
1715 if( PSA_ALG_IS_HMAC( operation->alg ) )
1716 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001717 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1718 operation->ctx.hmac.hash_ctx.alg = 0;
1719 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001720 }
1721 else
1722#endif /* MBEDTLS_MD_C */
1723 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001724 if( ! PSA_ALG_IS_MAC( alg ) )
1725 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001726 }
1727
1728 if( status != PSA_SUCCESS )
1729 memset( operation, 0, sizeof( *operation ) );
1730 return( status );
1731}
1732
Gilles Peskine01126fa2018-07-12 17:04:55 +02001733#if defined(MBEDTLS_MD_C)
1734static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1735{
Gilles Peskine3f108122018-12-07 18:14:53 +01001736 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001737 return( psa_hash_abort( &hmac->hash_ctx ) );
1738}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001739
1740static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1741{
1742 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1743 memset( hmac, 0, sizeof( *hmac ) );
1744}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001745#endif /* MBEDTLS_MD_C */
1746
Gilles Peskine8c9def32018-02-08 10:02:12 +01001747psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1748{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001749 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001750 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001751 /* The object has (apparently) been initialized but it is not
1752 * in use. It's ok to call abort on such an object, and there's
1753 * nothing to do. */
1754 return( PSA_SUCCESS );
1755 }
1756 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001757#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001758 if( operation->alg == PSA_ALG_CMAC )
1759 {
1760 mbedtls_cipher_free( &operation->ctx.cmac );
1761 }
1762 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001763#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001764#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001765 if( PSA_ALG_IS_HMAC( operation->alg ) )
1766 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001767 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001768 }
1769 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001770#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001771 {
1772 /* Sanity check (shouldn't happen: operation->alg should
1773 * always have been initialized to a valid value). */
1774 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001775 }
Moran Peker41deec42018-04-04 15:43:05 +03001776
Gilles Peskine8c9def32018-02-08 10:02:12 +01001777 operation->alg = 0;
1778 operation->key_set = 0;
1779 operation->iv_set = 0;
1780 operation->iv_required = 0;
1781 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001782 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001783
Gilles Peskine8c9def32018-02-08 10:02:12 +01001784 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001785
1786bad_state:
1787 /* If abort is called on an uninitialized object, we can't trust
1788 * anything. Wipe the object in case it contains confidential data.
1789 * This may result in a memory leak if a pointer gets overwritten,
1790 * but it's too late to do anything about this. */
1791 memset( operation, 0, sizeof( *operation ) );
1792 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001793}
1794
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001795#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001796static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001797 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001798 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001799 const mbedtls_cipher_info_t *cipher_info )
1800{
1801 int ret;
1802
1803 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001804
1805 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1806 if( ret != 0 )
1807 return( ret );
1808
1809 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1810 slot->data.raw.data,
1811 key_bits );
1812 return( ret );
1813}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001814#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001815
Gilles Peskine248051a2018-06-20 16:09:38 +02001816#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001817static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1818 const uint8_t *key,
1819 size_t key_length,
1820 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001821{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001822 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001823 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001824 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001825 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001826 psa_status_t status;
1827
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001828 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1829 * overflow below. This should never trigger if the hash algorithm
1830 * is implemented correctly. */
1831 /* The size checks against the ipad and opad buffers cannot be written
1832 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1833 * because that triggers -Wlogical-op on GCC 7.3. */
1834 if( block_size > sizeof( ipad ) )
1835 return( PSA_ERROR_NOT_SUPPORTED );
1836 if( block_size > sizeof( hmac->opad ) )
1837 return( PSA_ERROR_NOT_SUPPORTED );
1838 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001839 return( PSA_ERROR_NOT_SUPPORTED );
1840
Gilles Peskined223b522018-06-11 18:12:58 +02001841 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001842 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001843 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001844 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001845 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001846 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001847 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001848 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001849 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001850 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001851 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001852 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001853 }
Gilles Peskine96889972018-07-12 17:07:03 +02001854 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1855 * but it is permitted. It is common when HMAC is used in HKDF, for
1856 * example. Don't call `memcpy` in the 0-length because `key` could be
1857 * an invalid pointer which would make the behavior undefined. */
1858 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001859 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001860
Gilles Peskined223b522018-06-11 18:12:58 +02001861 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1862 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001863 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001864 ipad[i] ^= 0x36;
1865 memset( ipad + key_length, 0x36, block_size - key_length );
1866
1867 /* Copy the key material from ipad to opad, flipping the requisite bits,
1868 * and filling the rest of opad with the requisite constant. */
1869 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001870 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1871 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001872
Gilles Peskine01126fa2018-07-12 17:04:55 +02001873 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001874 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001875 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001876
Gilles Peskine01126fa2018-07-12 17:04:55 +02001877 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001878
1879cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001880 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001881
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001882 return( status );
1883}
Gilles Peskine248051a2018-06-20 16:09:38 +02001884#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001885
Gilles Peskine89167cb2018-07-08 20:12:23 +02001886static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001887 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001888 psa_algorithm_t alg,
1889 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001890{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001891 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001892 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001893 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001894 psa_key_usage_t usage =
1895 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001896 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001897 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001898
Gilles Peskined911eb72018-08-14 15:18:45 +02001899 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001900 if( status != PSA_SUCCESS )
1901 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001902 if( is_sign )
1903 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001904
Gilles Peskinec5487a82018-12-03 18:08:14 +01001905 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001906 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001907 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001908 key_bits = psa_get_key_bits( slot );
1909
Gilles Peskine8c9def32018-02-08 10:02:12 +01001910#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001911 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001912 {
1913 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001914 mbedtls_cipher_info_from_psa( full_length_alg,
1915 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001916 int ret;
1917 if( cipher_info == NULL )
1918 {
1919 status = PSA_ERROR_NOT_SUPPORTED;
1920 goto exit;
1921 }
1922 operation->mac_size = cipher_info->block_size;
1923 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1924 status = mbedtls_to_psa_error( ret );
1925 }
1926 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001927#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001928#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001929 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001930 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001931 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001932 if( hash_alg == 0 )
1933 {
1934 status = PSA_ERROR_NOT_SUPPORTED;
1935 goto exit;
1936 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001937
1938 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1939 /* Sanity check. This shouldn't fail on a valid configuration. */
1940 if( operation->mac_size == 0 ||
1941 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1942 {
1943 status = PSA_ERROR_NOT_SUPPORTED;
1944 goto exit;
1945 }
1946
Gilles Peskine01126fa2018-07-12 17:04:55 +02001947 if( slot->type != PSA_KEY_TYPE_HMAC )
1948 {
1949 status = PSA_ERROR_INVALID_ARGUMENT;
1950 goto exit;
1951 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001952
Gilles Peskine01126fa2018-07-12 17:04:55 +02001953 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1954 slot->data.raw.data,
1955 slot->data.raw.bytes,
1956 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001957 }
1958 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001959#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001960 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00001961 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02001962 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001963 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001964
Gilles Peskined911eb72018-08-14 15:18:45 +02001965 if( truncated == 0 )
1966 {
1967 /* The "normal" case: untruncated algorithm. Nothing to do. */
1968 }
1969 else if( truncated < 4 )
1970 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001971 /* A very short MAC is too short for security since it can be
1972 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1973 * so we make this our minimum, even though 32 bits is still
1974 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001975 status = PSA_ERROR_NOT_SUPPORTED;
1976 }
1977 else if( truncated > operation->mac_size )
1978 {
1979 /* It's impossible to "truncate" to a larger length. */
1980 status = PSA_ERROR_INVALID_ARGUMENT;
1981 }
1982 else
1983 operation->mac_size = truncated;
1984
Gilles Peskinefbfac682018-07-08 20:51:54 +02001985exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001986 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001987 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001988 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001989 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001990 else
1991 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001992 operation->key_set = 1;
1993 }
1994 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001995}
1996
Gilles Peskine89167cb2018-07-08 20:12:23 +02001997psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001998 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001999 psa_algorithm_t alg )
2000{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002001 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002002}
2003
2004psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002005 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002006 psa_algorithm_t alg )
2007{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002008 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002009}
2010
Gilles Peskine8c9def32018-02-08 10:02:12 +01002011psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2012 const uint8_t *input,
2013 size_t input_length )
2014{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002015 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002016 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002017 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002018 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002019 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002020 operation->has_input = 1;
2021
Gilles Peskine8c9def32018-02-08 10:02:12 +01002022#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002023 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002024 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002025 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2026 input, input_length );
2027 status = mbedtls_to_psa_error( ret );
2028 }
2029 else
2030#endif /* MBEDTLS_CMAC_C */
2031#if defined(MBEDTLS_MD_C)
2032 if( PSA_ALG_IS_HMAC( operation->alg ) )
2033 {
2034 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2035 input_length );
2036 }
2037 else
2038#endif /* MBEDTLS_MD_C */
2039 {
2040 /* This shouldn't happen if `operation` was initialized by
2041 * a setup function. */
2042 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002043 }
2044
Gilles Peskinefbfac682018-07-08 20:51:54 +02002045cleanup:
2046 if( status != PSA_SUCCESS )
2047 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002048 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002049}
2050
Gilles Peskine01126fa2018-07-12 17:04:55 +02002051#if defined(MBEDTLS_MD_C)
2052static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2053 uint8_t *mac,
2054 size_t mac_size )
2055{
2056 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2057 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2058 size_t hash_size = 0;
2059 size_t block_size = psa_get_hash_block_size( hash_alg );
2060 psa_status_t status;
2061
Gilles Peskine01126fa2018-07-12 17:04:55 +02002062 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2063 if( status != PSA_SUCCESS )
2064 return( status );
2065 /* From here on, tmp needs to be wiped. */
2066
2067 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2068 if( status != PSA_SUCCESS )
2069 goto exit;
2070
2071 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2072 if( status != PSA_SUCCESS )
2073 goto exit;
2074
2075 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2076 if( status != PSA_SUCCESS )
2077 goto exit;
2078
Gilles Peskined911eb72018-08-14 15:18:45 +02002079 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2080 if( status != PSA_SUCCESS )
2081 goto exit;
2082
2083 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002084
2085exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002086 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002087 return( status );
2088}
2089#endif /* MBEDTLS_MD_C */
2090
mohammad16036df908f2018-04-02 08:34:15 -07002091static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002092 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002093 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002094{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002095 if( ! operation->key_set )
2096 return( PSA_ERROR_BAD_STATE );
2097 if( operation->iv_required && ! operation->iv_set )
2098 return( PSA_ERROR_BAD_STATE );
2099
Gilles Peskine8c9def32018-02-08 10:02:12 +01002100 if( mac_size < operation->mac_size )
2101 return( PSA_ERROR_BUFFER_TOO_SMALL );
2102
Gilles Peskine8c9def32018-02-08 10:02:12 +01002103#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002104 if( operation->alg == PSA_ALG_CMAC )
2105 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002106 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2107 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2108 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002109 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002110 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002111 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002112 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002113 else
2114#endif /* MBEDTLS_CMAC_C */
2115#if defined(MBEDTLS_MD_C)
2116 if( PSA_ALG_IS_HMAC( operation->alg ) )
2117 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002118 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002119 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002120 }
2121 else
2122#endif /* MBEDTLS_MD_C */
2123 {
2124 /* This shouldn't happen if `operation` was initialized by
2125 * a setup function. */
2126 return( PSA_ERROR_BAD_STATE );
2127 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002128}
2129
Gilles Peskineacd4be32018-07-08 19:56:25 +02002130psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2131 uint8_t *mac,
2132 size_t mac_size,
2133 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002134{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002135 psa_status_t status;
2136
2137 /* Fill the output buffer with something that isn't a valid mac
2138 * (barring an attack on the mac and deliberately-crafted input),
2139 * in case the caller doesn't check the return status properly. */
2140 *mac_length = mac_size;
2141 /* If mac_size is 0 then mac may be NULL and then the
2142 * call to memset would have undefined behavior. */
2143 if( mac_size != 0 )
2144 memset( mac, '!', mac_size );
2145
Gilles Peskine89167cb2018-07-08 20:12:23 +02002146 if( ! operation->is_sign )
2147 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002148 status = PSA_ERROR_BAD_STATE;
2149 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002150 }
mohammad16036df908f2018-04-02 08:34:15 -07002151
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002152 status = psa_mac_finish_internal( operation, mac, mac_size );
2153
2154cleanup:
2155 if( status == PSA_SUCCESS )
2156 {
2157 status = psa_mac_abort( operation );
2158 if( status == PSA_SUCCESS )
2159 *mac_length = operation->mac_size;
2160 else
2161 memset( mac, '!', mac_size );
2162 }
2163 else
2164 psa_mac_abort( operation );
2165 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002166}
2167
Gilles Peskineacd4be32018-07-08 19:56:25 +02002168psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2169 const uint8_t *mac,
2170 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002171{
Gilles Peskine828ed142018-06-18 23:25:51 +02002172 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002173 psa_status_t status;
2174
Gilles Peskine89167cb2018-07-08 20:12:23 +02002175 if( operation->is_sign )
2176 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002177 status = PSA_ERROR_BAD_STATE;
2178 goto cleanup;
2179 }
2180 if( operation->mac_size != mac_length )
2181 {
2182 status = PSA_ERROR_INVALID_SIGNATURE;
2183 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002184 }
mohammad16036df908f2018-04-02 08:34:15 -07002185
2186 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002187 actual_mac, sizeof( actual_mac ) );
2188
2189 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2190 status = PSA_ERROR_INVALID_SIGNATURE;
2191
2192cleanup:
2193 if( status == PSA_SUCCESS )
2194 status = psa_mac_abort( operation );
2195 else
2196 psa_mac_abort( operation );
2197
Gilles Peskine3f108122018-12-07 18:14:53 +01002198 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002199
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002200 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002201}
2202
2203
Gilles Peskine20035e32018-02-03 22:44:14 +01002204
Gilles Peskine20035e32018-02-03 22:44:14 +01002205/****************************************************************/
2206/* Asymmetric cryptography */
2207/****************************************************************/
2208
Gilles Peskine2b450e32018-06-27 15:42:46 +02002209#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002210/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002211 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002212static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2213 size_t hash_length,
2214 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002215{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002216 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002217 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002218 *md_alg = mbedtls_md_get_type( md_info );
2219
2220 /* The Mbed TLS RSA module uses an unsigned int for hash length
2221 * parameters. Validate that it fits so that we don't risk an
2222 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002223#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002224 if( hash_length > UINT_MAX )
2225 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002226#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002227
2228#if defined(MBEDTLS_PKCS1_V15)
2229 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2230 * must be correct. */
2231 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2232 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002233 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002234 if( md_info == NULL )
2235 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002236 if( mbedtls_md_get_size( md_info ) != hash_length )
2237 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002238 }
2239#endif /* MBEDTLS_PKCS1_V15 */
2240
2241#if defined(MBEDTLS_PKCS1_V21)
2242 /* PSS requires a hash internally. */
2243 if( PSA_ALG_IS_RSA_PSS( alg ) )
2244 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002245 if( md_info == NULL )
2246 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002247 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002248#endif /* MBEDTLS_PKCS1_V21 */
2249
Gilles Peskine61b91d42018-06-08 16:09:36 +02002250 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002251}
2252
Gilles Peskine2b450e32018-06-27 15:42:46 +02002253static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2254 psa_algorithm_t alg,
2255 const uint8_t *hash,
2256 size_t hash_length,
2257 uint8_t *signature,
2258 size_t signature_size,
2259 size_t *signature_length )
2260{
2261 psa_status_t status;
2262 int ret;
2263 mbedtls_md_type_t md_alg;
2264
2265 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2266 if( status != PSA_SUCCESS )
2267 return( status );
2268
Gilles Peskine630a18a2018-06-29 17:49:35 +02002269 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002270 return( PSA_ERROR_BUFFER_TOO_SMALL );
2271
2272#if defined(MBEDTLS_PKCS1_V15)
2273 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2274 {
2275 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2276 MBEDTLS_MD_NONE );
2277 ret = mbedtls_rsa_pkcs1_sign( rsa,
2278 mbedtls_ctr_drbg_random,
2279 &global_data.ctr_drbg,
2280 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002281 md_alg,
2282 (unsigned int) hash_length,
2283 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002284 signature );
2285 }
2286 else
2287#endif /* MBEDTLS_PKCS1_V15 */
2288#if defined(MBEDTLS_PKCS1_V21)
2289 if( PSA_ALG_IS_RSA_PSS( alg ) )
2290 {
2291 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2292 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2293 mbedtls_ctr_drbg_random,
2294 &global_data.ctr_drbg,
2295 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002296 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002297 (unsigned int) hash_length,
2298 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002299 signature );
2300 }
2301 else
2302#endif /* MBEDTLS_PKCS1_V21 */
2303 {
2304 return( PSA_ERROR_INVALID_ARGUMENT );
2305 }
2306
2307 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002308 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002309 return( mbedtls_to_psa_error( ret ) );
2310}
2311
2312static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2313 psa_algorithm_t alg,
2314 const uint8_t *hash,
2315 size_t hash_length,
2316 const uint8_t *signature,
2317 size_t signature_length )
2318{
2319 psa_status_t status;
2320 int ret;
2321 mbedtls_md_type_t md_alg;
2322
2323 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2324 if( status != PSA_SUCCESS )
2325 return( status );
2326
Gilles Peskine630a18a2018-06-29 17:49:35 +02002327 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002328 return( PSA_ERROR_BUFFER_TOO_SMALL );
2329
2330#if defined(MBEDTLS_PKCS1_V15)
2331 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2332 {
2333 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2334 MBEDTLS_MD_NONE );
2335 ret = mbedtls_rsa_pkcs1_verify( rsa,
2336 mbedtls_ctr_drbg_random,
2337 &global_data.ctr_drbg,
2338 MBEDTLS_RSA_PUBLIC,
2339 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002340 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002341 hash,
2342 signature );
2343 }
2344 else
2345#endif /* MBEDTLS_PKCS1_V15 */
2346#if defined(MBEDTLS_PKCS1_V21)
2347 if( PSA_ALG_IS_RSA_PSS( alg ) )
2348 {
2349 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2350 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2351 mbedtls_ctr_drbg_random,
2352 &global_data.ctr_drbg,
2353 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002354 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002355 (unsigned int) hash_length,
2356 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002357 signature );
2358 }
2359 else
2360#endif /* MBEDTLS_PKCS1_V21 */
2361 {
2362 return( PSA_ERROR_INVALID_ARGUMENT );
2363 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002364
2365 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2366 * the rest of the signature is invalid". This has little use in
2367 * practice and PSA doesn't report this distinction. */
2368 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2369 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002370 return( mbedtls_to_psa_error( ret ) );
2371}
2372#endif /* MBEDTLS_RSA_C */
2373
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002374#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002375/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2376 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2377 * (even though these functions don't modify it). */
2378static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2379 psa_algorithm_t alg,
2380 const uint8_t *hash,
2381 size_t hash_length,
2382 uint8_t *signature,
2383 size_t signature_size,
2384 size_t *signature_length )
2385{
2386 int ret;
2387 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002388 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002389 mbedtls_mpi_init( &r );
2390 mbedtls_mpi_init( &s );
2391
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002392 if( signature_size < 2 * curve_bytes )
2393 {
2394 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2395 goto cleanup;
2396 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002397
Gilles Peskinea05219c2018-11-16 16:02:56 +01002398#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002399 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2400 {
2401 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2402 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2403 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2404 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2405 hash, hash_length,
2406 md_alg ) );
2407 }
2408 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002409#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002410 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002411 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002412 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2413 hash, hash_length,
2414 mbedtls_ctr_drbg_random,
2415 &global_data.ctr_drbg ) );
2416 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002417
2418 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2419 signature,
2420 curve_bytes ) );
2421 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2422 signature + curve_bytes,
2423 curve_bytes ) );
2424
2425cleanup:
2426 mbedtls_mpi_free( &r );
2427 mbedtls_mpi_free( &s );
2428 if( ret == 0 )
2429 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002430 return( mbedtls_to_psa_error( ret ) );
2431}
2432
2433static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2434 const uint8_t *hash,
2435 size_t hash_length,
2436 const uint8_t *signature,
2437 size_t signature_length )
2438{
2439 int ret;
2440 mbedtls_mpi r, s;
2441 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2442 mbedtls_mpi_init( &r );
2443 mbedtls_mpi_init( &s );
2444
2445 if( signature_length != 2 * curve_bytes )
2446 return( PSA_ERROR_INVALID_SIGNATURE );
2447
2448 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2449 signature,
2450 curve_bytes ) );
2451 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2452 signature + curve_bytes,
2453 curve_bytes ) );
2454
2455 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2456 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002457
2458cleanup:
2459 mbedtls_mpi_free( &r );
2460 mbedtls_mpi_free( &s );
2461 return( mbedtls_to_psa_error( ret ) );
2462}
2463#endif /* MBEDTLS_ECDSA_C */
2464
Gilles Peskinec5487a82018-12-03 18:08:14 +01002465psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002466 psa_algorithm_t alg,
2467 const uint8_t *hash,
2468 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002469 uint8_t *signature,
2470 size_t signature_size,
2471 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002472{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002473 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002474 psa_status_t status;
2475
2476 *signature_length = signature_size;
2477
Gilles Peskinec5487a82018-12-03 18:08:14 +01002478 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002479 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002480 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002481 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002482 {
2483 status = PSA_ERROR_INVALID_ARGUMENT;
2484 goto exit;
2485 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002486
Gilles Peskine20035e32018-02-03 22:44:14 +01002487#if defined(MBEDTLS_RSA_C)
2488 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2489 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002490 status = psa_rsa_sign( slot->data.rsa,
2491 alg,
2492 hash, hash_length,
2493 signature, signature_size,
2494 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002495 }
2496 else
2497#endif /* defined(MBEDTLS_RSA_C) */
2498#if defined(MBEDTLS_ECP_C)
2499 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2500 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002501#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002502 if(
2503#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2504 PSA_ALG_IS_ECDSA( alg )
2505#else
2506 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2507#endif
2508 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002509 status = psa_ecdsa_sign( slot->data.ecp,
2510 alg,
2511 hash, hash_length,
2512 signature, signature_size,
2513 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002514 else
2515#endif /* defined(MBEDTLS_ECDSA_C) */
2516 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002517 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002518 }
itayzafrir5c753392018-05-08 11:18:38 +03002519 }
2520 else
2521#endif /* defined(MBEDTLS_ECP_C) */
2522 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002523 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002524 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002525
2526exit:
2527 /* Fill the unused part of the output buffer (the whole buffer on error,
2528 * the trailing part on success) with something that isn't a valid mac
2529 * (barring an attack on the mac and deliberately-crafted input),
2530 * in case the caller doesn't check the return status properly. */
2531 if( status == PSA_SUCCESS )
2532 memset( signature + *signature_length, '!',
2533 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002534 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002535 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002536 /* If signature_size is 0 then we have nothing to do. We must not call
2537 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002538 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002539}
2540
Gilles Peskinec5487a82018-12-03 18:08:14 +01002541psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002542 psa_algorithm_t alg,
2543 const uint8_t *hash,
2544 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002545 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002546 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002547{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002548 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002549 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002550
Gilles Peskinec5487a82018-12-03 18:08:14 +01002551 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002552 if( status != PSA_SUCCESS )
2553 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002554
Gilles Peskine61b91d42018-06-08 16:09:36 +02002555#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002556 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002557 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002558 return( psa_rsa_verify( slot->data.rsa,
2559 alg,
2560 hash, hash_length,
2561 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002562 }
2563 else
2564#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002565#if defined(MBEDTLS_ECP_C)
2566 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2567 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002568#if defined(MBEDTLS_ECDSA_C)
2569 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002570 return( psa_ecdsa_verify( slot->data.ecp,
2571 hash, hash_length,
2572 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002573 else
2574#endif /* defined(MBEDTLS_ECDSA_C) */
2575 {
2576 return( PSA_ERROR_INVALID_ARGUMENT );
2577 }
itayzafrir5c753392018-05-08 11:18:38 +03002578 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002579 else
2580#endif /* defined(MBEDTLS_ECP_C) */
2581 {
2582 return( PSA_ERROR_NOT_SUPPORTED );
2583 }
2584}
2585
Gilles Peskine072ac562018-06-30 00:21:29 +02002586#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2587static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2588 mbedtls_rsa_context *rsa )
2589{
2590 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2591 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2592 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2593 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2594}
2595#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2596
Gilles Peskinec5487a82018-12-03 18:08:14 +01002597psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002598 psa_algorithm_t alg,
2599 const uint8_t *input,
2600 size_t input_length,
2601 const uint8_t *salt,
2602 size_t salt_length,
2603 uint8_t *output,
2604 size_t output_size,
2605 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002606{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002607 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002608 psa_status_t status;
2609
Darryl Green5cc689a2018-07-24 15:34:10 +01002610 (void) input;
2611 (void) input_length;
2612 (void) salt;
2613 (void) output;
2614 (void) output_size;
2615
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002616 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002617
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002618 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2619 return( PSA_ERROR_INVALID_ARGUMENT );
2620
Gilles Peskinec5487a82018-12-03 18:08:14 +01002621 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002622 if( status != PSA_SUCCESS )
2623 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002624 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2625 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002626 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002627
2628#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002629 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002630 {
2631 mbedtls_rsa_context *rsa = slot->data.rsa;
2632 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002633 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002634 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002635#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002636 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002637 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002638 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2639 mbedtls_ctr_drbg_random,
2640 &global_data.ctr_drbg,
2641 MBEDTLS_RSA_PUBLIC,
2642 input_length,
2643 input,
2644 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002645 }
2646 else
2647#endif /* MBEDTLS_PKCS1_V15 */
2648#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002649 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002650 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002651 psa_rsa_oaep_set_padding_mode( alg, rsa );
2652 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2653 mbedtls_ctr_drbg_random,
2654 &global_data.ctr_drbg,
2655 MBEDTLS_RSA_PUBLIC,
2656 salt, salt_length,
2657 input_length,
2658 input,
2659 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002660 }
2661 else
2662#endif /* MBEDTLS_PKCS1_V21 */
2663 {
2664 return( PSA_ERROR_INVALID_ARGUMENT );
2665 }
2666 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002667 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002668 return( mbedtls_to_psa_error( ret ) );
2669 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002670 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002671#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002672 {
2673 return( PSA_ERROR_NOT_SUPPORTED );
2674 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002675}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002676
Gilles Peskinec5487a82018-12-03 18:08:14 +01002677psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002678 psa_algorithm_t alg,
2679 const uint8_t *input,
2680 size_t input_length,
2681 const uint8_t *salt,
2682 size_t salt_length,
2683 uint8_t *output,
2684 size_t output_size,
2685 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002686{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002687 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002688 psa_status_t status;
2689
Darryl Green5cc689a2018-07-24 15:34:10 +01002690 (void) input;
2691 (void) input_length;
2692 (void) salt;
2693 (void) output;
2694 (void) output_size;
2695
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002696 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002697
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002698 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2699 return( PSA_ERROR_INVALID_ARGUMENT );
2700
Gilles Peskinec5487a82018-12-03 18:08:14 +01002701 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002702 if( status != PSA_SUCCESS )
2703 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002704 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2705 return( PSA_ERROR_INVALID_ARGUMENT );
2706
2707#if defined(MBEDTLS_RSA_C)
2708 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2709 {
2710 mbedtls_rsa_context *rsa = slot->data.rsa;
2711 int ret;
2712
Gilles Peskine630a18a2018-06-29 17:49:35 +02002713 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002714 return( PSA_ERROR_INVALID_ARGUMENT );
2715
2716#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002717 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002718 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002719 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2720 mbedtls_ctr_drbg_random,
2721 &global_data.ctr_drbg,
2722 MBEDTLS_RSA_PRIVATE,
2723 output_length,
2724 input,
2725 output,
2726 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002727 }
2728 else
2729#endif /* MBEDTLS_PKCS1_V15 */
2730#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002731 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002732 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002733 psa_rsa_oaep_set_padding_mode( alg, rsa );
2734 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2735 mbedtls_ctr_drbg_random,
2736 &global_data.ctr_drbg,
2737 MBEDTLS_RSA_PRIVATE,
2738 salt, salt_length,
2739 output_length,
2740 input,
2741 output,
2742 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002743 }
2744 else
2745#endif /* MBEDTLS_PKCS1_V21 */
2746 {
2747 return( PSA_ERROR_INVALID_ARGUMENT );
2748 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002749
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002750 return( mbedtls_to_psa_error( ret ) );
2751 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002752 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002753#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002754 {
2755 return( PSA_ERROR_NOT_SUPPORTED );
2756 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002757}
Gilles Peskine20035e32018-02-03 22:44:14 +01002758
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002759
2760
mohammad1603503973b2018-03-12 15:59:30 +02002761/****************************************************************/
2762/* Symmetric cryptography */
2763/****************************************************************/
2764
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002765/* Initialize the cipher operation structure. Once this function has been
2766 * called, psa_cipher_abort can run and will do the right thing. */
2767static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2768 psa_algorithm_t alg )
2769{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002770 if( ! PSA_ALG_IS_CIPHER( alg ) )
2771 {
2772 memset( operation, 0, sizeof( *operation ) );
2773 return( PSA_ERROR_INVALID_ARGUMENT );
2774 }
2775
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002776 operation->alg = alg;
2777 operation->key_set = 0;
2778 operation->iv_set = 0;
2779 operation->iv_required = 1;
2780 operation->iv_size = 0;
2781 operation->block_size = 0;
2782 mbedtls_cipher_init( &operation->ctx.cipher );
2783 return( PSA_SUCCESS );
2784}
2785
Gilles Peskinee553c652018-06-04 16:22:46 +02002786static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002787 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002788 psa_algorithm_t alg,
2789 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002790{
2791 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2792 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002793 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002794 size_t key_bits;
2795 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002796 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2797 PSA_KEY_USAGE_ENCRYPT :
2798 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002799
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002800 status = psa_cipher_init( operation, alg );
2801 if( status != PSA_SUCCESS )
2802 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002803
Gilles Peskinec5487a82018-12-03 18:08:14 +01002804 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002805 if( status != PSA_SUCCESS )
2806 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002807 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002808
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002809 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002810 if( cipher_info == NULL )
2811 return( PSA_ERROR_NOT_SUPPORTED );
2812
mohammad1603503973b2018-03-12 15:59:30 +02002813 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002814 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002815 {
2816 psa_cipher_abort( operation );
2817 return( mbedtls_to_psa_error( ret ) );
2818 }
2819
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002820#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002821 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002822 {
2823 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2824 unsigned char keys[24];
2825 memcpy( keys, slot->data.raw.data, 16 );
2826 memcpy( keys + 16, slot->data.raw.data, 8 );
2827 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2828 keys,
2829 192, cipher_operation );
2830 }
2831 else
2832#endif
2833 {
2834 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2835 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002836 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002837 }
Moran Peker41deec42018-04-04 15:43:05 +03002838 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002839 {
2840 psa_cipher_abort( operation );
2841 return( mbedtls_to_psa_error( ret ) );
2842 }
2843
mohammad16038481e742018-03-18 13:57:31 +02002844#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002845 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002846 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002847 case PSA_ALG_CBC_NO_PADDING:
2848 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2849 MBEDTLS_PADDING_NONE );
2850 break;
2851 case PSA_ALG_CBC_PKCS7:
2852 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2853 MBEDTLS_PADDING_PKCS7 );
2854 break;
2855 default:
2856 /* The algorithm doesn't involve padding. */
2857 ret = 0;
2858 break;
2859 }
2860 if( ret != 0 )
2861 {
2862 psa_cipher_abort( operation );
2863 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002864 }
2865#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2866
mohammad1603503973b2018-03-12 15:59:30 +02002867 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002868 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2869 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2870 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002871 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002872 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002873 }
mohammad1603503973b2018-03-12 15:59:30 +02002874
Moran Peker395db872018-05-31 14:07:14 +03002875 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002876}
2877
Gilles Peskinefe119512018-07-08 21:39:34 +02002878psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002879 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002880 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002881{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002882 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002883}
2884
Gilles Peskinefe119512018-07-08 21:39:34 +02002885psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002886 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002887 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002888{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002889 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002890}
2891
Gilles Peskinefe119512018-07-08 21:39:34 +02002892psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2893 unsigned char *iv,
2894 size_t iv_size,
2895 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002896{
itayzafrir534bd7c2018-08-02 13:56:32 +03002897 psa_status_t status;
2898 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002899 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002900 {
2901 status = PSA_ERROR_BAD_STATE;
2902 goto exit;
2903 }
Moran Peker41deec42018-04-04 15:43:05 +03002904 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002905 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002906 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002907 goto exit;
2908 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002909 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2910 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002911 if( ret != 0 )
2912 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002913 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002914 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002915 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002916
mohammad16038481e742018-03-18 13:57:31 +02002917 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002918 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002919
Moran Peker395db872018-05-31 14:07:14 +03002920exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002921 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002922 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002923 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002924}
2925
Gilles Peskinefe119512018-07-08 21:39:34 +02002926psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2927 const unsigned char *iv,
2928 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002929{
itayzafrir534bd7c2018-08-02 13:56:32 +03002930 psa_status_t status;
2931 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002932 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002933 {
2934 status = PSA_ERROR_BAD_STATE;
2935 goto exit;
2936 }
Moran Pekera28258c2018-05-29 16:25:04 +03002937 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002938 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002939 status = PSA_ERROR_INVALID_ARGUMENT;
2940 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002941 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002942 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2943 status = mbedtls_to_psa_error( ret );
2944exit:
2945 if( status == PSA_SUCCESS )
2946 operation->iv_set = 1;
2947 else
mohammad1603503973b2018-03-12 15:59:30 +02002948 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002949 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002950}
2951
Gilles Peskinee553c652018-06-04 16:22:46 +02002952psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2953 const uint8_t *input,
2954 size_t input_length,
2955 unsigned char *output,
2956 size_t output_size,
2957 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002958{
itayzafrir534bd7c2018-08-02 13:56:32 +03002959 psa_status_t status;
2960 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002961 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002962 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002963 {
2964 /* Take the unprocessed partial block left over from previous
2965 * update calls, if any, plus the input to this call. Remove
2966 * the last partial block, if any. You get the data that will be
2967 * output in this call. */
2968 expected_output_size =
2969 ( operation->ctx.cipher.unprocessed_len + input_length )
2970 / operation->block_size * operation->block_size;
2971 }
2972 else
2973 {
2974 expected_output_size = input_length;
2975 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002976
Gilles Peskine89d789c2018-06-04 17:17:16 +02002977 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002978 {
2979 status = PSA_ERROR_BUFFER_TOO_SMALL;
2980 goto exit;
2981 }
mohammad160382759612018-03-12 18:16:40 +02002982
mohammad1603503973b2018-03-12 15:59:30 +02002983 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002984 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002985 status = mbedtls_to_psa_error( ret );
2986exit:
2987 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002988 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002989 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002990}
2991
Gilles Peskinee553c652018-06-04 16:22:46 +02002992psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2993 uint8_t *output,
2994 size_t output_size,
2995 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002996{
Janos Follath315b51c2018-07-09 16:04:51 +01002997 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2998 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002999 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003000
mohammad1603503973b2018-03-12 15:59:30 +02003001 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003002 {
Janos Follath315b51c2018-07-09 16:04:51 +01003003 status = PSA_ERROR_BAD_STATE;
3004 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03003005 }
3006 if( operation->iv_required && ! operation->iv_set )
3007 {
Janos Follath315b51c2018-07-09 16:04:51 +01003008 status = PSA_ERROR_BAD_STATE;
3009 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03003010 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003011
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003012 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003013 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3014 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003015 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003016 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003017 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003018 }
3019
Janos Follath315b51c2018-07-09 16:04:51 +01003020 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3021 temp_output_buffer,
3022 output_length );
3023 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003024 {
Janos Follath315b51c2018-07-09 16:04:51 +01003025 status = mbedtls_to_psa_error( cipher_ret );
3026 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003027 }
Janos Follath315b51c2018-07-09 16:04:51 +01003028
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003029 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003030 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003031 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003032 memcpy( output, temp_output_buffer, *output_length );
3033 else
3034 {
Janos Follath315b51c2018-07-09 16:04:51 +01003035 status = PSA_ERROR_BUFFER_TOO_SMALL;
3036 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003037 }
mohammad1603503973b2018-03-12 15:59:30 +02003038
Gilles Peskine3f108122018-12-07 18:14:53 +01003039 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003040 status = psa_cipher_abort( operation );
3041
3042 return( status );
3043
3044error:
3045
3046 *output_length = 0;
3047
Gilles Peskine3f108122018-12-07 18:14:53 +01003048 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003049 (void) psa_cipher_abort( operation );
3050
3051 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003052}
3053
Gilles Peskinee553c652018-06-04 16:22:46 +02003054psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3055{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003056 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003057 {
3058 /* The object has (apparently) been initialized but it is not
3059 * in use. It's ok to call abort on such an object, and there's
3060 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003061 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003062 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003063
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003064 /* Sanity check (shouldn't happen: operation->alg should
3065 * always have been initialized to a valid value). */
3066 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3067 return( PSA_ERROR_BAD_STATE );
3068
mohammad1603503973b2018-03-12 15:59:30 +02003069 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003070
Moran Peker41deec42018-04-04 15:43:05 +03003071 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003072 operation->key_set = 0;
3073 operation->iv_set = 0;
3074 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003075 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003076 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003077
Moran Peker395db872018-05-31 14:07:14 +03003078 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003079}
3080
Gilles Peskinea0655c32018-04-30 17:06:50 +02003081
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003082
mohammad16038cc1cee2018-03-28 01:21:33 +03003083/****************************************************************/
3084/* Key Policy */
3085/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003086
mohammad160327010052018-07-03 13:16:15 +03003087#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003088void psa_key_policy_set_usage( psa_key_policy_t *policy,
3089 psa_key_usage_t usage,
3090 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003091{
mohammad16034eed7572018-03-28 05:14:59 -07003092 policy->usage = usage;
3093 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003094}
3095
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003096psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003097{
mohammad16036df908f2018-04-02 08:34:15 -07003098 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003099}
3100
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003101psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003102{
mohammad16036df908f2018-04-02 08:34:15 -07003103 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003104}
mohammad160327010052018-07-03 13:16:15 +03003105#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003106
Gilles Peskinec5487a82018-12-03 18:08:14 +01003107psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003108 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003109{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003110 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003111 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003112
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003113 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003114 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003115
Gilles Peskinec5487a82018-12-03 18:08:14 +01003116 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003117 if( status != PSA_SUCCESS )
3118 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003119
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003120 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3121 PSA_KEY_USAGE_ENCRYPT |
3122 PSA_KEY_USAGE_DECRYPT |
3123 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003124 PSA_KEY_USAGE_VERIFY |
3125 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003126 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003127
mohammad16036df908f2018-04-02 08:34:15 -07003128 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003129
3130 return( PSA_SUCCESS );
3131}
3132
Gilles Peskinec5487a82018-12-03 18:08:14 +01003133psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003134 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003135{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003136 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003137 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003138
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003139 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003140 return( PSA_ERROR_INVALID_ARGUMENT );
3141
Gilles Peskinec5487a82018-12-03 18:08:14 +01003142 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003143 if( status != PSA_SUCCESS )
3144 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003145
mohammad16036df908f2018-04-02 08:34:15 -07003146 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003147
3148 return( PSA_SUCCESS );
3149}
Gilles Peskine20035e32018-02-03 22:44:14 +01003150
Gilles Peskinea0655c32018-04-30 17:06:50 +02003151
3152
mohammad1603804cd712018-03-20 22:44:08 +02003153/****************************************************************/
3154/* Key Lifetime */
3155/****************************************************************/
3156
Gilles Peskinec5487a82018-12-03 18:08:14 +01003157psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003158 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003159{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003160 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003161 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003162
Gilles Peskinec5487a82018-12-03 18:08:14 +01003163 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003164 if( status != PSA_SUCCESS )
3165 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003166
mohammad1603804cd712018-03-20 22:44:08 +02003167 *lifetime = slot->lifetime;
3168
3169 return( PSA_SUCCESS );
3170}
3171
Gilles Peskine20035e32018-02-03 22:44:14 +01003172
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003173
mohammad16035955c982018-04-26 00:53:03 +03003174/****************************************************************/
3175/* AEAD */
3176/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003177
Gilles Peskineedf9a652018-08-17 18:11:56 +02003178typedef struct
3179{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003180 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003181 const mbedtls_cipher_info_t *cipher_info;
3182 union
3183 {
3184#if defined(MBEDTLS_CCM_C)
3185 mbedtls_ccm_context ccm;
3186#endif /* MBEDTLS_CCM_C */
3187#if defined(MBEDTLS_GCM_C)
3188 mbedtls_gcm_context gcm;
3189#endif /* MBEDTLS_GCM_C */
3190 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003191 psa_algorithm_t core_alg;
3192 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003193 uint8_t tag_length;
3194} aead_operation_t;
3195
Gilles Peskine30a9e412019-01-14 18:36:12 +01003196static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003197{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003198 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003199 {
3200#if defined(MBEDTLS_CCM_C)
3201 case PSA_ALG_CCM:
3202 mbedtls_ccm_free( &operation->ctx.ccm );
3203 break;
3204#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003205#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003206 case PSA_ALG_GCM:
3207 mbedtls_gcm_free( &operation->ctx.gcm );
3208 break;
3209#endif /* MBEDTLS_GCM_C */
3210 }
3211}
3212
3213static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003214 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003215 psa_key_usage_t usage,
3216 psa_algorithm_t alg )
3217{
3218 psa_status_t status;
3219 size_t key_bits;
3220 mbedtls_cipher_id_t cipher_id;
3221
Gilles Peskinec5487a82018-12-03 18:08:14 +01003222 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003223 if( status != PSA_SUCCESS )
3224 return( status );
3225
3226 key_bits = psa_get_key_bits( operation->slot );
3227
3228 operation->cipher_info =
3229 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3230 &cipher_id );
3231 if( operation->cipher_info == NULL )
3232 return( PSA_ERROR_NOT_SUPPORTED );
3233
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003234 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003235 {
3236#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003237 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3238 operation->core_alg = PSA_ALG_CCM;
3239 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003240 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3241 return( PSA_ERROR_INVALID_ARGUMENT );
3242 mbedtls_ccm_init( &operation->ctx.ccm );
3243 status = mbedtls_to_psa_error(
3244 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3245 operation->slot->data.raw.data,
3246 (unsigned int) key_bits ) );
3247 if( status != 0 )
3248 goto cleanup;
3249 break;
3250#endif /* MBEDTLS_CCM_C */
3251
3252#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003253 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3254 operation->core_alg = PSA_ALG_GCM;
3255 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003256 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3257 return( PSA_ERROR_INVALID_ARGUMENT );
3258 mbedtls_gcm_init( &operation->ctx.gcm );
3259 status = mbedtls_to_psa_error(
3260 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3261 operation->slot->data.raw.data,
3262 (unsigned int) key_bits ) );
3263 break;
3264#endif /* MBEDTLS_GCM_C */
3265
3266 default:
3267 return( PSA_ERROR_NOT_SUPPORTED );
3268 }
3269
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003270 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3271 {
3272 status = PSA_ERROR_INVALID_ARGUMENT;
3273 goto cleanup;
3274 }
3275 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3276 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3277 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3278 * In both cases, mbedtls_xxx will validate the tag length below. */
3279
Gilles Peskineedf9a652018-08-17 18:11:56 +02003280 return( PSA_SUCCESS );
3281
3282cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003283 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003284 return( status );
3285}
3286
Gilles Peskinec5487a82018-12-03 18:08:14 +01003287psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003288 psa_algorithm_t alg,
3289 const uint8_t *nonce,
3290 size_t nonce_length,
3291 const uint8_t *additional_data,
3292 size_t additional_data_length,
3293 const uint8_t *plaintext,
3294 size_t plaintext_length,
3295 uint8_t *ciphertext,
3296 size_t ciphertext_size,
3297 size_t *ciphertext_length )
3298{
mohammad16035955c982018-04-26 00:53:03 +03003299 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003300 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003301 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003302
mohammad1603f08a5502018-06-03 15:05:47 +03003303 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003304
Gilles Peskinec5487a82018-12-03 18:08:14 +01003305 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003306 if( status != PSA_SUCCESS )
3307 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003308
Gilles Peskineedf9a652018-08-17 18:11:56 +02003309 /* For all currently supported modes, the tag is at the end of the
3310 * ciphertext. */
3311 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3312 {
3313 status = PSA_ERROR_BUFFER_TOO_SMALL;
3314 goto exit;
3315 }
3316 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003317
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003318#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003319 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003320 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003321 status = mbedtls_to_psa_error(
3322 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3323 MBEDTLS_GCM_ENCRYPT,
3324 plaintext_length,
3325 nonce, nonce_length,
3326 additional_data, additional_data_length,
3327 plaintext, ciphertext,
3328 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003329 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003330 else
3331#endif /* MBEDTLS_GCM_C */
3332#if defined(MBEDTLS_CCM_C)
3333 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003334 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003335 status = mbedtls_to_psa_error(
3336 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3337 plaintext_length,
3338 nonce, nonce_length,
3339 additional_data,
3340 additional_data_length,
3341 plaintext, ciphertext,
3342 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003343 }
mohammad16035c8845f2018-05-09 05:40:09 -07003344 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003345#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003346 {
mohammad1603554faad2018-06-03 15:07:38 +03003347 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003348 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003349
Gilles Peskineedf9a652018-08-17 18:11:56 +02003350 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3351 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003352
Gilles Peskineedf9a652018-08-17 18:11:56 +02003353exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01003354 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003355 if( status == PSA_SUCCESS )
3356 *ciphertext_length = plaintext_length + operation.tag_length;
3357 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003358}
3359
Gilles Peskineee652a32018-06-01 19:23:52 +02003360/* Locate the tag in a ciphertext buffer containing the encrypted data
3361 * followed by the tag. Return the length of the part preceding the tag in
3362 * *plaintext_length. This is the size of the plaintext in modes where
3363 * the encrypted data has the same size as the plaintext, such as
3364 * CCM and GCM. */
3365static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3366 const uint8_t *ciphertext,
3367 size_t ciphertext_length,
3368 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003369 const uint8_t **p_tag )
3370{
3371 size_t payload_length;
3372 if( tag_length > ciphertext_length )
3373 return( PSA_ERROR_INVALID_ARGUMENT );
3374 payload_length = ciphertext_length - tag_length;
3375 if( payload_length > plaintext_size )
3376 return( PSA_ERROR_BUFFER_TOO_SMALL );
3377 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003378 return( PSA_SUCCESS );
3379}
3380
Gilles Peskinec5487a82018-12-03 18:08:14 +01003381psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003382 psa_algorithm_t alg,
3383 const uint8_t *nonce,
3384 size_t nonce_length,
3385 const uint8_t *additional_data,
3386 size_t additional_data_length,
3387 const uint8_t *ciphertext,
3388 size_t ciphertext_length,
3389 uint8_t *plaintext,
3390 size_t plaintext_size,
3391 size_t *plaintext_length )
3392{
mohammad16035955c982018-04-26 00:53:03 +03003393 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003394 aead_operation_t operation;
3395 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003396
Gilles Peskineee652a32018-06-01 19:23:52 +02003397 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003398
Gilles Peskinec5487a82018-12-03 18:08:14 +01003399 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003400 if( status != PSA_SUCCESS )
3401 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003402
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003403#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003404 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003405 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003406 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003407 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003408 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003409 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003410 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003411
Gilles Peskineedf9a652018-08-17 18:11:56 +02003412 status = mbedtls_to_psa_error(
3413 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3414 ciphertext_length - operation.tag_length,
3415 nonce, nonce_length,
3416 additional_data,
3417 additional_data_length,
3418 tag, operation.tag_length,
3419 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003420 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003421 else
3422#endif /* MBEDTLS_GCM_C */
3423#if defined(MBEDTLS_CCM_C)
3424 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003425 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003426 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003427 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003428 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003429 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003430 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003431
Gilles Peskineedf9a652018-08-17 18:11:56 +02003432 status = mbedtls_to_psa_error(
3433 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3434 ciphertext_length - operation.tag_length,
3435 nonce, nonce_length,
3436 additional_data,
3437 additional_data_length,
3438 ciphertext, plaintext,
3439 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003440 }
mohammad160339574652018-06-01 04:39:53 -07003441 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003442#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003443 {
mohammad1603554faad2018-06-03 15:07:38 +03003444 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003445 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003446
Gilles Peskineedf9a652018-08-17 18:11:56 +02003447 if( status != PSA_SUCCESS && plaintext_size != 0 )
3448 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003449
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 *plaintext_length = ciphertext_length - operation.tag_length;
3454 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003455}
3456
Gilles Peskinea0655c32018-04-30 17:06:50 +02003457
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003458
Gilles Peskine20035e32018-02-03 22:44:14 +01003459/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003460/* Generators */
3461/****************************************************************/
3462
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003463#define HKDF_STATE_INIT 0 /* no input yet */
3464#define HKDF_STATE_STARTED 1 /* got salt */
3465#define HKDF_STATE_KEYED 2 /* got key */
3466#define HKDF_STATE_OUTPUT 3 /* output started */
3467
Gilles Peskine969c5d62019-01-16 15:53:06 +01003468static psa_algorithm_t psa_generator_get_kdf_alg(
3469 const psa_crypto_generator_t *generator )
3470{
3471 if ( PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
3472 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( generator->alg ) );
3473 else
3474 return( generator->alg );
3475}
3476
3477
Gilles Peskineeab56e42018-07-12 17:12:33 +02003478psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3479{
3480 psa_status_t status = PSA_SUCCESS;
Gilles Peskine969c5d62019-01-16 15:53:06 +01003481 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
3482 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003483 {
3484 /* The object has (apparently) been initialized but it is not
3485 * in use. It's ok to call abort on such an object, and there's
3486 * nothing to do. */
3487 }
3488 else
Gilles Peskine969c5d62019-01-16 15:53:06 +01003489 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02003490 {
3491 if( generator->ctx.buffer.data != NULL )
3492 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003493 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003494 generator->ctx.buffer.size );
3495 mbedtls_free( generator->ctx.buffer.data );
3496 }
3497 }
3498 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003499#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003500 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003501 {
3502 mbedtls_free( generator->ctx.hkdf.info );
3503 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3504 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003505 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Hanno Becker1aaedc02018-11-16 11:35:34 +00003506 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01003507 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003508 {
3509 if( generator->ctx.tls12_prf.key != NULL )
3510 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003511 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003512 generator->ctx.tls12_prf.key_len );
3513 mbedtls_free( generator->ctx.tls12_prf.key );
3514 }
Hanno Becker580fba12018-11-13 20:50:45 +00003515
3516 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3517 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003518 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003519 generator->ctx.tls12_prf.Ai_with_seed_len );
3520 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3521 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003522 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003523 else
3524#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003525 {
3526 status = PSA_ERROR_BAD_STATE;
3527 }
3528 memset( generator, 0, sizeof( *generator ) );
3529 return( status );
3530}
3531
Gilles Peskineeab56e42018-07-12 17:12:33 +02003532psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3533 size_t *capacity)
3534{
3535 *capacity = generator->capacity;
3536 return( PSA_SUCCESS );
3537}
3538
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003539psa_status_t psa_set_generator_capacity( psa_crypto_generator_t *generator,
3540 size_t capacity )
3541{
3542 if( generator->alg == 0 )
3543 return( PSA_ERROR_BAD_STATE );
3544 if( capacity > generator->capacity )
3545 return( PSA_ERROR_INVALID_ARGUMENT );
3546 generator->capacity = capacity;
3547 return( PSA_SUCCESS );
3548}
3549
Gilles Peskinebef7f142018-07-12 17:22:21 +02003550#if defined(MBEDTLS_MD_C)
3551/* Read some bytes from an HKDF-based generator. This performs a chunk
3552 * of the expand phase of the HKDF algorithm. */
3553static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3554 psa_algorithm_t hash_alg,
3555 uint8_t *output,
3556 size_t output_length )
3557{
3558 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3559 psa_status_t status;
3560
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003561 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
3562 return( PSA_ERROR_BAD_STATE );
3563 hkdf->state = HKDF_STATE_OUTPUT;
3564
Gilles Peskinebef7f142018-07-12 17:22:21 +02003565 while( output_length != 0 )
3566 {
3567 /* Copy what remains of the current block */
3568 uint8_t n = hash_length - hkdf->offset_in_block;
3569 if( n > output_length )
3570 n = (uint8_t) output_length;
3571 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3572 output += n;
3573 output_length -= n;
3574 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003575 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003576 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003577 /* We can't be wanting more output after block 0xff, otherwise
3578 * the capacity check in psa_generator_read() would have
3579 * prevented this call. It could happen only if the generator
3580 * object was corrupted or if this function is called directly
3581 * inside the library. */
3582 if( hkdf->block_number == 0xff )
3583 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003584
3585 /* We need a new block */
3586 ++hkdf->block_number;
3587 hkdf->offset_in_block = 0;
3588 status = psa_hmac_setup_internal( &hkdf->hmac,
3589 hkdf->prk, hash_length,
3590 hash_alg );
3591 if( status != PSA_SUCCESS )
3592 return( status );
3593 if( hkdf->block_number != 1 )
3594 {
3595 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3596 hkdf->output_block,
3597 hash_length );
3598 if( status != PSA_SUCCESS )
3599 return( status );
3600 }
3601 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3602 hkdf->info,
3603 hkdf->info_length );
3604 if( status != PSA_SUCCESS )
3605 return( status );
3606 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3607 &hkdf->block_number, 1 );
3608 if( status != PSA_SUCCESS )
3609 return( status );
3610 status = psa_hmac_finish_internal( &hkdf->hmac,
3611 hkdf->output_block,
3612 sizeof( hkdf->output_block ) );
3613 if( status != PSA_SUCCESS )
3614 return( status );
3615 }
3616
3617 return( PSA_SUCCESS );
3618}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003619
3620static psa_status_t psa_generator_tls12_prf_generate_next_block(
3621 psa_tls12_prf_generator_t *tls12_prf,
3622 psa_algorithm_t alg )
3623{
3624 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3625 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3626 psa_hmac_internal_data hmac;
3627 psa_status_t status, cleanup_status;
3628
Hanno Becker3b339e22018-11-13 20:56:14 +00003629 unsigned char *Ai;
3630 size_t Ai_len;
3631
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003632 /* We can't be wanting more output after block 0xff, otherwise
3633 * the capacity check in psa_generator_read() would have
3634 * prevented this call. It could happen only if the generator
3635 * object was corrupted or if this function is called directly
3636 * inside the library. */
3637 if( tls12_prf->block_number == 0xff )
3638 return( PSA_ERROR_BAD_STATE );
3639
3640 /* We need a new block */
3641 ++tls12_prf->block_number;
3642 tls12_prf->offset_in_block = 0;
3643
3644 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3645 *
3646 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3647 *
3648 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3649 * HMAC_hash(secret, A(2) + seed) +
3650 * HMAC_hash(secret, A(3) + seed) + ...
3651 *
3652 * A(0) = seed
3653 * A(i) = HMAC_hash( secret, A(i-1) )
3654 *
3655 * The `psa_tls12_prf_generator` structures saves the block
3656 * `HMAC_hash(secret, A(i) + seed)` from which the output
3657 * is currently extracted as `output_block`, while
3658 * `A(i) + seed` is stored in `Ai_with_seed`.
3659 *
3660 * Generating a new block means recalculating `Ai_with_seed`
3661 * from the A(i)-part of it, and afterwards recalculating
3662 * `output_block`.
3663 *
3664 * A(0) is computed at setup time.
3665 *
3666 */
3667
3668 psa_hmac_init_internal( &hmac );
3669
3670 /* We must distinguish the calculation of A(1) from those
3671 * of A(2) and higher, because A(0)=seed has a different
3672 * length than the other A(i). */
3673 if( tls12_prf->block_number == 1 )
3674 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003675 Ai = tls12_prf->Ai_with_seed + hash_length;
3676 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003677 }
3678 else
3679 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003680 Ai = tls12_prf->Ai_with_seed;
3681 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003682 }
3683
Hanno Becker3b339e22018-11-13 20:56:14 +00003684 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3685 status = psa_hmac_setup_internal( &hmac,
3686 tls12_prf->key,
3687 tls12_prf->key_len,
3688 hash_alg );
3689 if( status != PSA_SUCCESS )
3690 goto cleanup;
3691
3692 status = psa_hash_update( &hmac.hash_ctx,
3693 Ai, Ai_len );
3694 if( status != PSA_SUCCESS )
3695 goto cleanup;
3696
3697 status = psa_hmac_finish_internal( &hmac,
3698 tls12_prf->Ai_with_seed,
3699 hash_length );
3700 if( status != PSA_SUCCESS )
3701 goto cleanup;
3702
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003703 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3704 status = psa_hmac_setup_internal( &hmac,
3705 tls12_prf->key,
3706 tls12_prf->key_len,
3707 hash_alg );
3708 if( status != PSA_SUCCESS )
3709 goto cleanup;
3710
3711 status = psa_hash_update( &hmac.hash_ctx,
3712 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003713 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003714 if( status != PSA_SUCCESS )
3715 goto cleanup;
3716
3717 status = psa_hmac_finish_internal( &hmac,
3718 tls12_prf->output_block,
3719 hash_length );
3720 if( status != PSA_SUCCESS )
3721 goto cleanup;
3722
3723cleanup:
3724
3725 cleanup_status = psa_hmac_abort_internal( &hmac );
3726 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3727 status = cleanup_status;
3728
3729 return( status );
3730}
3731
3732/* Read some bytes from an TLS-1.2-PRF-based generator.
3733 * See Section 5 of RFC 5246. */
3734static psa_status_t psa_generator_tls12_prf_read(
3735 psa_tls12_prf_generator_t *tls12_prf,
3736 psa_algorithm_t alg,
3737 uint8_t *output,
3738 size_t output_length )
3739{
3740 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3741 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3742 psa_status_t status;
3743
3744 while( output_length != 0 )
3745 {
3746 /* Copy what remains of the current block */
3747 uint8_t n = hash_length - tls12_prf->offset_in_block;
3748
3749 /* Check if we have fully processed the current block. */
3750 if( n == 0 )
3751 {
3752 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3753 alg );
3754 if( status != PSA_SUCCESS )
3755 return( status );
3756
3757 continue;
3758 }
3759
3760 if( n > output_length )
3761 n = (uint8_t) output_length;
3762 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3763 n );
3764 output += n;
3765 output_length -= n;
3766 tls12_prf->offset_in_block += n;
3767 }
3768
3769 return( PSA_SUCCESS );
3770}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003771#endif /* MBEDTLS_MD_C */
3772
Gilles Peskineeab56e42018-07-12 17:12:33 +02003773psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3774 uint8_t *output,
3775 size_t output_length )
3776{
3777 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01003778 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003779
3780 if( output_length > generator->capacity )
3781 {
3782 generator->capacity = 0;
3783 /* Go through the error path to wipe all confidential data now
3784 * that the generator object is useless. */
3785 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3786 goto exit;
3787 }
3788 if( output_length == 0 &&
Gilles Peskine969c5d62019-01-16 15:53:06 +01003789 generator->capacity == 0 && kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02003790 {
3791 /* Edge case: this is a blank or finished generator, and 0
3792 * bytes were requested. The right error in this case could
3793 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3794 * INSUFFICIENT_CAPACITY, which is right for a finished
3795 * generator, for consistency with the case when
3796 * output_length > 0. */
3797 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3798 }
3799 generator->capacity -= output_length;
3800
Gilles Peskine969c5d62019-01-16 15:53:06 +01003801 if( kdf_alg == PSA_ALG_SELECT_RAW )
Gilles Peskine751d9652018-09-18 12:05:44 +02003802 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003803 /* Initially, the capacity of a selection generator is always
3804 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3805 * abbreviated in this comment as `size`. When the remaining
3806 * capacity is `c`, the next bytes to serve start `c` bytes
3807 * from the end of the buffer, i.e. `size - c` from the
3808 * beginning of the buffer. Since `generator->capacity` was just
3809 * decremented above, we need to serve the bytes from
3810 * `size - generator->capacity - output_length` to
3811 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003812 size_t offset =
3813 generator->ctx.buffer.size - generator->capacity - output_length;
3814 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3815 status = PSA_SUCCESS;
3816 }
3817 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003818#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01003819 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003820 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01003821 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003822 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3823 output, output_length );
3824 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01003825 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
3826 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003827 {
3828 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01003829 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003830 output_length );
3831 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003832 else
3833#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003834 {
3835 return( PSA_ERROR_BAD_STATE );
3836 }
3837
3838exit:
3839 if( status != PSA_SUCCESS )
3840 {
3841 psa_generator_abort( generator );
3842 memset( output, '!', output_length );
3843 }
3844 return( status );
3845}
3846
Gilles Peskine08542d82018-07-19 17:05:42 +02003847#if defined(MBEDTLS_DES_C)
3848static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3849{
3850 if( data_size >= 8 )
3851 mbedtls_des_key_set_parity( data );
3852 if( data_size >= 16 )
3853 mbedtls_des_key_set_parity( data + 8 );
3854 if( data_size >= 24 )
3855 mbedtls_des_key_set_parity( data + 16 );
3856}
3857#endif /* MBEDTLS_DES_C */
3858
Gilles Peskinec5487a82018-12-03 18:08:14 +01003859psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003860 psa_key_type_t type,
3861 size_t bits,
3862 psa_crypto_generator_t *generator )
3863{
3864 uint8_t *data = NULL;
3865 size_t bytes = PSA_BITS_TO_BYTES( bits );
3866 psa_status_t status;
3867
3868 if( ! key_type_is_raw_bytes( type ) )
3869 return( PSA_ERROR_INVALID_ARGUMENT );
3870 if( bits % 8 != 0 )
3871 return( PSA_ERROR_INVALID_ARGUMENT );
3872 data = mbedtls_calloc( 1, bytes );
3873 if( data == NULL )
3874 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3875
3876 status = psa_generator_read( generator, data, bytes );
3877 if( status != PSA_SUCCESS )
3878 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003879#if defined(MBEDTLS_DES_C)
3880 if( type == PSA_KEY_TYPE_DES )
3881 psa_des_set_key_parity( data, bytes );
3882#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003883 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003884
3885exit:
3886 mbedtls_free( data );
3887 return( status );
3888}
3889
3890
3891
3892/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003893/* Key derivation */
3894/****************************************************************/
3895
Gilles Peskinea05219c2018-11-16 16:02:56 +01003896#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003897/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003898 * of the HKDF algorithm.
3899 *
3900 * Note that if this function fails, you must call psa_generator_abort()
3901 * to potentially free embedded data structures and wipe confidential data.
3902 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003903static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003904 const uint8_t *secret,
3905 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003906 psa_algorithm_t hash_alg,
3907 const uint8_t *salt,
3908 size_t salt_length,
3909 const uint8_t *label,
3910 size_t label_length )
3911{
3912 psa_status_t status;
3913 status = psa_hmac_setup_internal( &hkdf->hmac,
3914 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003915 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003916 if( status != PSA_SUCCESS )
3917 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003918 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003919 if( status != PSA_SUCCESS )
3920 return( status );
3921 status = psa_hmac_finish_internal( &hkdf->hmac,
3922 hkdf->prk,
3923 sizeof( hkdf->prk ) );
3924 if( status != PSA_SUCCESS )
3925 return( status );
3926 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3927 hkdf->block_number = 0;
3928 hkdf->info_length = label_length;
3929 if( label_length != 0 )
3930 {
3931 hkdf->info = mbedtls_calloc( 1, label_length );
3932 if( hkdf->info == NULL )
3933 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3934 memcpy( hkdf->info, label, label_length );
3935 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01003936 hkdf->state = HKDF_STATE_KEYED;
3937 hkdf->info_set = 1;
Gilles Peskinebef7f142018-07-12 17:22:21 +02003938 return( PSA_SUCCESS );
3939}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003940#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003941
Gilles Peskinea05219c2018-11-16 16:02:56 +01003942#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003943/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3944 *
3945 * Note that if this function fails, you must call psa_generator_abort()
3946 * to potentially free embedded data structures and wipe confidential data.
3947 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003948static psa_status_t psa_generator_tls12_prf_setup(
3949 psa_tls12_prf_generator_t *tls12_prf,
3950 const unsigned char *key,
3951 size_t key_len,
3952 psa_algorithm_t hash_alg,
3953 const uint8_t *salt,
3954 size_t salt_length,
3955 const uint8_t *label,
3956 size_t label_length )
3957{
3958 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003959 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3960 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003961
3962 tls12_prf->key = mbedtls_calloc( 1, key_len );
3963 if( tls12_prf->key == NULL )
3964 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3965 tls12_prf->key_len = key_len;
3966 memcpy( tls12_prf->key, key, key_len );
3967
Hanno Becker580fba12018-11-13 20:50:45 +00003968 overflow = ( salt_length + label_length < salt_length ) ||
3969 ( salt_length + label_length + hash_length < hash_length );
3970 if( overflow )
3971 return( PSA_ERROR_INVALID_ARGUMENT );
3972
3973 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3974 if( tls12_prf->Ai_with_seed == NULL )
3975 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3976 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3977
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003978 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3979 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003980 if( label_length != 0 )
3981 {
3982 memcpy( tls12_prf->Ai_with_seed + hash_length,
3983 label, label_length );
3984 }
3985
3986 if( salt_length != 0 )
3987 {
3988 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3989 salt, salt_length );
3990 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003991
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003992 /* The first block gets generated when
3993 * psa_generator_read() is called. */
3994 tls12_prf->block_number = 0;
3995 tls12_prf->offset_in_block = hash_length;
3996
3997 return( PSA_SUCCESS );
3998}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003999
4000/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4001static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4002 psa_tls12_prf_generator_t *tls12_prf,
4003 const unsigned char *psk,
4004 size_t psk_len,
4005 psa_algorithm_t hash_alg,
4006 const uint8_t *salt,
4007 size_t salt_length,
4008 const uint8_t *label,
4009 size_t label_length )
4010{
4011 psa_status_t status;
4012 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4013
4014 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4015 return( PSA_ERROR_INVALID_ARGUMENT );
4016
4017 /* Quoting RFC 4279, Section 2:
4018 *
4019 * The premaster secret is formed as follows: if the PSK is N octets
4020 * long, concatenate a uint16 with the value N, N zero octets, a second
4021 * uint16 with the value N, and the PSK itself.
4022 */
4023
4024 pms[0] = ( psk_len >> 8 ) & 0xff;
4025 pms[1] = ( psk_len >> 0 ) & 0xff;
4026 memset( pms + 2, 0, psk_len );
4027 pms[2 + psk_len + 0] = pms[0];
4028 pms[2 + psk_len + 1] = pms[1];
4029 memcpy( pms + 4 + psk_len, psk, psk_len );
4030
4031 status = psa_generator_tls12_prf_setup( tls12_prf,
4032 pms, 4 + 2 * psk_len,
4033 hash_alg,
4034 salt, salt_length,
4035 label, label_length );
4036
Gilles Peskine3f108122018-12-07 18:14:53 +01004037 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004038 return( status );
4039}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004040#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004041
Gilles Peskine346797d2018-11-16 16:05:06 +01004042/* Note that if this function fails, you must call psa_generator_abort()
4043 * to potentially free embedded data structures and wipe confidential data.
4044 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004045static psa_status_t psa_key_derivation_internal(
4046 psa_crypto_generator_t *generator,
4047 const uint8_t *secret, size_t secret_length,
4048 psa_algorithm_t alg,
4049 const uint8_t *salt, size_t salt_length,
4050 const uint8_t *label, size_t label_length,
4051 size_t capacity )
4052{
4053 psa_status_t status;
4054 size_t max_capacity;
4055
4056 /* Set generator->alg even on failure so that abort knows what to do. */
4057 generator->alg = alg;
4058
Gilles Peskine751d9652018-09-18 12:05:44 +02004059 if( alg == PSA_ALG_SELECT_RAW )
4060 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004061 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004062 if( salt_length != 0 )
4063 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004064 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004065 if( label_length != 0 )
4066 return( PSA_ERROR_INVALID_ARGUMENT );
4067 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4068 if( generator->ctx.buffer.data == NULL )
4069 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4070 memcpy( generator->ctx.buffer.data, secret, secret_length );
4071 generator->ctx.buffer.size = secret_length;
4072 max_capacity = secret_length;
4073 status = PSA_SUCCESS;
4074 }
4075 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004076#if defined(MBEDTLS_MD_C)
4077 if( PSA_ALG_IS_HKDF( alg ) )
4078 {
4079 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4080 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4081 if( hash_size == 0 )
4082 return( PSA_ERROR_NOT_SUPPORTED );
4083 max_capacity = 255 * hash_size;
4084 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4085 secret, secret_length,
4086 hash_alg,
4087 salt, salt_length,
4088 label, label_length );
4089 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004090 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4091 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4092 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004093 {
4094 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4095 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4096
4097 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4098 if( hash_alg != PSA_ALG_SHA_256 &&
4099 hash_alg != PSA_ALG_SHA_384 )
4100 {
4101 return( PSA_ERROR_NOT_SUPPORTED );
4102 }
4103
4104 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004105
4106 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4107 {
4108 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4109 secret, secret_length,
4110 hash_alg, salt, salt_length,
4111 label, label_length );
4112 }
4113 else
4114 {
4115 status = psa_generator_tls12_psk_to_ms_setup(
4116 &generator->ctx.tls12_prf,
4117 secret, secret_length,
4118 hash_alg, salt, salt_length,
4119 label, label_length );
4120 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004121 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004122 else
4123#endif
4124 {
4125 return( PSA_ERROR_NOT_SUPPORTED );
4126 }
4127
4128 if( status != PSA_SUCCESS )
4129 return( status );
4130
4131 if( capacity <= max_capacity )
4132 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004133 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4134 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004135 else
4136 return( PSA_ERROR_INVALID_ARGUMENT );
4137
4138 return( PSA_SUCCESS );
4139}
4140
Gilles Peskineea0fb492018-07-12 17:17:20 +02004141psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004142 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004143 psa_algorithm_t alg,
4144 const uint8_t *salt,
4145 size_t salt_length,
4146 const uint8_t *label,
4147 size_t label_length,
4148 size_t capacity )
4149{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004150 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004151 psa_status_t status;
4152
4153 if( generator->alg != 0 )
4154 return( PSA_ERROR_BAD_STATE );
4155
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004156 /* Make sure that alg is a key derivation algorithm. This prevents
4157 * key selection algorithms, which psa_key_derivation_internal
4158 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004159 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4160 return( PSA_ERROR_INVALID_ARGUMENT );
4161
Gilles Peskinec5487a82018-12-03 18:08:14 +01004162 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004163 if( status != PSA_SUCCESS )
4164 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004165
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004166 if( slot->type != PSA_KEY_TYPE_DERIVE )
4167 return( PSA_ERROR_INVALID_ARGUMENT );
4168
4169 status = psa_key_derivation_internal( generator,
4170 slot->data.raw.data,
4171 slot->data.raw.bytes,
4172 alg,
4173 salt, salt_length,
4174 label, label_length,
4175 capacity );
4176 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004177 psa_generator_abort( generator );
4178 return( status );
4179}
4180
Gilles Peskine969c5d62019-01-16 15:53:06 +01004181static psa_status_t psa_key_derivation_setup_kdf(
4182 psa_crypto_generator_t *generator,
4183 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004184{
Gilles Peskine969c5d62019-01-16 15:53:06 +01004185 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004186#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004187 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4188 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4189 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004190 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004191 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004192 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4193 if( hash_size == 0 )
4194 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004195 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4196 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004197 ! ( hash_alg == PSA_ALG_SHA_256 && hash_alg == PSA_ALG_SHA_384 ) )
4198 {
4199 return( PSA_ERROR_NOT_SUPPORTED );
4200 }
4201 generator->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004202 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004203 }
4204#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004205 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004206 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004207}
4208
4209psa_status_t psa_key_derivation_setup( psa_crypto_generator_t *generator,
4210 psa_algorithm_t alg )
4211{
4212 psa_status_t status;
4213
4214 if( generator->alg != 0 )
4215 return( PSA_ERROR_BAD_STATE );
4216
Gilles Peskine6843c292019-01-18 16:44:49 +01004217 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4218 return( PSA_ERROR_INVALID_ARGUMENT );
4219 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004220 {
4221 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine6843c292019-01-18 16:44:49 +01004222 status = psa_key_derivation_setup_kdf( generator, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004223 }
4224 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4225 {
4226 status = psa_key_derivation_setup_kdf( generator, alg );
4227 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004228 else
4229 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004230
4231 if( status == PSA_SUCCESS )
4232 generator->alg = alg;
4233 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004234}
4235
4236#if defined(MBEDTLS_MD_C)
4237static psa_status_t psa_hkdf_input( psa_hkdf_generator_t *hkdf,
4238 psa_algorithm_t hash_alg,
4239 psa_key_derivation_step_t step,
4240 const uint8_t *data,
4241 size_t data_length )
4242{
4243 psa_status_t status;
4244 switch( step )
4245 {
4246 case PSA_KDF_STEP_SALT:
4247 if( hkdf->state == HKDF_STATE_INIT )
4248 {
4249 status = psa_hmac_setup_internal( &hkdf->hmac,
4250 data, data_length,
4251 hash_alg );
4252 if( status != PSA_SUCCESS )
4253 return( status );
4254 hkdf->state = HKDF_STATE_STARTED;
4255 return( PSA_SUCCESS );
4256 }
4257 else
4258 return( PSA_ERROR_BAD_STATE );
4259 break;
4260 case PSA_KDF_STEP_SECRET:
4261 /* If no salt was provided, use an empty salt. */
4262 if( hkdf->state == HKDF_STATE_INIT )
4263 {
4264 status = psa_hmac_setup_internal( &hkdf->hmac,
4265 NULL, 0,
4266 PSA_ALG_HMAC( hash_alg ) );
4267 if( status != PSA_SUCCESS )
4268 return( status );
4269 hkdf->state = HKDF_STATE_STARTED;
4270 }
4271 if( hkdf->state == HKDF_STATE_STARTED )
4272 {
4273 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4274 data, data_length );
4275 if( status != PSA_SUCCESS )
4276 return( status );
4277 status = psa_hmac_finish_internal( &hkdf->hmac,
4278 hkdf->prk,
4279 sizeof( hkdf->prk ) );
4280 if( status != PSA_SUCCESS )
4281 return( status );
4282 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4283 hkdf->block_number = 0;
4284 hkdf->state = HKDF_STATE_KEYED;
4285 return( PSA_SUCCESS );
4286 }
4287 else
4288 return( PSA_ERROR_BAD_STATE );
4289 break;
4290 case PSA_KDF_STEP_INFO:
4291 if( hkdf->state == HKDF_STATE_OUTPUT )
4292 return( PSA_ERROR_BAD_STATE );
4293 if( hkdf->info_set )
4294 return( PSA_ERROR_BAD_STATE );
4295 hkdf->info_length = data_length;
4296 if( data_length != 0 )
4297 {
4298 hkdf->info = mbedtls_calloc( 1, data_length );
4299 if( hkdf->info == NULL )
4300 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4301 memcpy( hkdf->info, data, data_length );
4302 }
4303 hkdf->info_set = 1;
4304 return( PSA_SUCCESS );
4305 default:
4306 return( PSA_ERROR_INVALID_ARGUMENT );
4307 }
4308}
4309#endif /* MBEDTLS_MD_C */
4310
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004311static psa_status_t psa_key_derivation_input_raw(
4312 psa_crypto_generator_t *generator,
4313 psa_key_derivation_step_t step,
4314 const uint8_t *data,
4315 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004316{
4317 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004318 psa_algorithm_t kdf_alg = psa_generator_get_kdf_alg( generator );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004319
Gilles Peskine969c5d62019-01-16 15:53:06 +01004320 if( kdf_alg == PSA_ALG_SELECT_RAW )
4321 {
4322 if( generator->capacity != 0 )
4323 return( PSA_ERROR_INVALID_ARGUMENT );
4324 generator->ctx.buffer.data = mbedtls_calloc( 1, data_length );
4325 if( generator->ctx.buffer.data == NULL )
4326 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4327 memcpy( generator->ctx.buffer.data, data, data_length );
4328 generator->ctx.buffer.size = data_length;
4329 generator->capacity = data_length;
4330 status = PSA_SUCCESS;
4331 }
4332 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004333#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004334 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004335 {
4336 status = psa_hkdf_input( &generator->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004337 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004338 step, data, data_length );
4339 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004340 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004341#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004342#if defined(MBEDTLS_MD_C)
4343 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004344 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4345 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004346 {
4347 // TODO
4348 status = PSA_ERROR_NOT_SUPPORTED;
4349 }
4350 else
4351#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004352 {
4353 /* This can't happen unless the generator object was not initialized */
4354 return( PSA_ERROR_BAD_STATE );
4355 }
4356
4357 if( status != PSA_SUCCESS )
4358 psa_generator_abort( generator );
4359 return( status );
4360}
4361
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004362psa_status_t psa_key_derivation_input_bytes( psa_crypto_generator_t *generator,
4363 psa_key_derivation_step_t step,
4364 const uint8_t *data,
4365 size_t data_length )
4366{
4367 switch( step )
4368 {
4369 case PSA_KDF_STEP_LABEL:
4370 case PSA_KDF_STEP_SALT:
4371 case PSA_KDF_STEP_INFO:
4372 return( psa_key_derivation_input_raw( generator, step,
4373 data, data_length ) );
4374 default:
4375 return( PSA_ERROR_INVALID_ARGUMENT );
4376 }
4377}
4378
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004379psa_status_t psa_key_derivation_input_key( psa_crypto_generator_t *generator,
4380 psa_key_derivation_step_t step,
4381 psa_key_handle_t handle )
4382{
4383 psa_key_slot_t *slot;
4384 psa_status_t status;
4385 status = psa_get_key_from_slot( handle, &slot,
4386 PSA_KEY_USAGE_DERIVE,
4387 generator->alg );
4388 if( status != PSA_SUCCESS )
4389 return( status );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004390 // TODO: for a key agreement algorithm, allow the corresponding key type and step
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004391 if( slot->type != PSA_KEY_TYPE_DERIVE )
4392 return( PSA_ERROR_INVALID_ARGUMENT );
4393 /* Don't allow a key to be used as an input that is usually public.
4394 * This is debatable. It's ok from a cryptographic perspective to
4395 * use secret material as an input that is usually public. However
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004396 * the material should be dedicated to a particular input step,
4397 * otherwise this may allow the key to be used in an unintended way
4398 * and leak values derived from the key. So be conservative. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004399 if( step != PSA_KDF_STEP_SECRET )
4400 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01004401 return( psa_key_derivation_input_raw( generator,
4402 step,
4403 slot->data.raw.data,
4404 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004405}
4406
Gilles Peskineea0fb492018-07-12 17:17:20 +02004407
4408
4409/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004410/* Key agreement */
4411/****************************************************************/
4412
Gilles Peskinea05219c2018-11-16 16:02:56 +01004413#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004414static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4415 size_t peer_key_length,
4416 const mbedtls_ecp_keypair *our_key,
4417 uint8_t *shared_secret,
4418 size_t shared_secret_size,
4419 size_t *shared_secret_length )
4420{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004421 mbedtls_ecp_keypair *their_key = NULL;
4422 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004423 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004424 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004425
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004426 status = psa_import_ec_public_key(
4427 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4428 peer_key, peer_key_length,
4429 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004430 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004431 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01004432
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004433 status = mbedtls_to_psa_error(
4434 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4435 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004436 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004437 status = mbedtls_to_psa_error(
4438 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4439 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004440 goto exit;
4441
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004442 status = mbedtls_to_psa_error(
4443 mbedtls_ecdh_calc_secret( &ecdh,
4444 shared_secret_length,
4445 shared_secret, shared_secret_size,
4446 mbedtls_ctr_drbg_random,
4447 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004448
4449exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004450 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004451 mbedtls_ecp_keypair_free( their_key );
4452 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004453 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004454}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004455#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004456
Gilles Peskine01d718c2018-09-18 12:01:02 +02004457#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4458
Gilles Peskine346797d2018-11-16 16:05:06 +01004459/* Note that if this function fails, you must call psa_generator_abort()
4460 * to potentially free embedded data structures and wipe confidential data.
4461 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004462static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004463 psa_key_derivation_step_t step,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004464 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004465 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004466 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004467{
4468 psa_status_t status;
4469 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4470 size_t shared_secret_length = 0;
4471
4472 /* Step 1: run the secret agreement algorithm to generate the shared
4473 * secret. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004474 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( generator->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004475 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004476#if defined(MBEDTLS_ECDH_C)
Gilles Peskine6843c292019-01-18 16:44:49 +01004477 case PSA_ALG_ECDH:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004478 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4479 return( PSA_ERROR_INVALID_ARGUMENT );
4480 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4481 private_key->data.ecp,
4482 shared_secret,
4483 sizeof( shared_secret ),
4484 &shared_secret_length );
4485 break;
4486#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004487 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004488 (void) private_key;
4489 (void) peer_key;
4490 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004491 return( PSA_ERROR_NOT_SUPPORTED );
4492 }
4493 if( status != PSA_SUCCESS )
4494 goto exit;
4495
4496 /* Step 2: set up the key derivation to generate key material from
4497 * the shared secret. */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004498 status = psa_key_derivation_input_raw( generator, step,
4499 shared_secret, shared_secret_length );
4500
Gilles Peskine01d718c2018-09-18 12:01:02 +02004501exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004502 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004503 return( status );
4504}
4505
4506psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004507 psa_key_derivation_step_t step,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004508 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004509 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004510 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004511{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004512 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004513 psa_status_t status;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004514 if( ! PSA_ALG_IS_KEY_AGREEMENT( generator->alg ) )
Gilles Peskine01d718c2018-09-18 12:01:02 +02004515 return( PSA_ERROR_INVALID_ARGUMENT );
4516 status = psa_get_key_from_slot( private_key, &slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004517 PSA_KEY_USAGE_DERIVE, generator->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004518 if( status != PSA_SUCCESS )
4519 return( status );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004520 status = psa_key_agreement_internal( generator, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01004521 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004522 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01004523 if( status != PSA_SUCCESS )
4524 psa_generator_abort( generator );
4525 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004526}
4527
4528
4529
4530/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004531/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004532/****************************************************************/
4533
4534psa_status_t psa_generate_random( uint8_t *output,
4535 size_t output_size )
4536{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004537 int ret;
4538 GUARD_MODULE_INITIALIZED;
4539
4540 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004541 return( mbedtls_to_psa_error( ret ) );
4542}
4543
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004544#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004545
4546/* Support function for error conversion between psa_its error codes to psa crypto */
4547static psa_status_t its_to_psa_error( psa_its_status_t ret )
4548{
4549 switch( ret )
4550 {
4551 case PSA_ITS_SUCCESS:
4552 return( PSA_SUCCESS );
4553
4554 case PSA_ITS_ERROR_KEY_NOT_FOUND:
4555 return( PSA_ERROR_EMPTY_SLOT );
4556
4557 case PSA_ITS_ERROR_STORAGE_FAILURE:
4558 return( PSA_ERROR_STORAGE_FAILURE );
4559
4560 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4561 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4562
4563 case PSA_ITS_ERROR_INVALID_KEY:
Jaeden Amero58600552018-11-30 12:04:38 +00004564 case PSA_ITS_ERROR_OFFSET_INVALID:
avolinski13beb102018-11-20 16:51:49 +02004565 case PSA_ITS_ERROR_INCORRECT_SIZE:
4566 case PSA_ITS_ERROR_BAD_POINTER:
4567 return( PSA_ERROR_INVALID_ARGUMENT );
4568
4569 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4570 return( PSA_ERROR_NOT_SUPPORTED );
4571
4572 case PSA_ITS_ERROR_WRITE_ONCE:
4573 return( PSA_ERROR_OCCUPIED_SLOT );
4574
4575 default:
4576 return( PSA_ERROR_UNKNOWN_ERROR );
4577 }
4578}
4579
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004580psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4581 size_t seed_size )
4582{
4583 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004584 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004585 struct psa_its_info_t p_info;
4586 if( global_data.initialized )
4587 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004588
4589 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4590 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4591 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4592 return( PSA_ERROR_INVALID_ARGUMENT );
4593
avolinski0d2c2662018-11-21 17:31:07 +02004594 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004595 status = its_to_psa_error( its_status );
4596
4597 if( PSA_ITS_ERROR_KEY_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004598 {
avolinski0d2c2662018-11-21 17:31:07 +02004599 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004600 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004601 }
avolinski13beb102018-11-20 16:51:49 +02004602 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004603 {
4604 /* You should not be here. Seed needs to be injected only once */
4605 status = PSA_ERROR_NOT_PERMITTED;
4606 }
4607 return( status );
4608}
4609#endif
4610
Gilles Peskinec5487a82018-12-03 18:08:14 +01004611psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004612 psa_key_type_t type,
4613 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004614 const void *extra,
4615 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004616{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004617 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004618 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004619
Gilles Peskine53d991e2018-07-12 01:14:59 +02004620 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004621 return( PSA_ERROR_INVALID_ARGUMENT );
4622
Gilles Peskinec5487a82018-12-03 18:08:14 +01004623 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004624 if( status != PSA_SUCCESS )
4625 return( status );
4626
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004627 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004628 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004629 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004630 if( status != PSA_SUCCESS )
4631 return( status );
4632 status = psa_generate_random( slot->data.raw.data,
4633 slot->data.raw.bytes );
4634 if( status != PSA_SUCCESS )
4635 {
4636 mbedtls_free( slot->data.raw.data );
4637 return( status );
4638 }
4639#if defined(MBEDTLS_DES_C)
4640 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004641 psa_des_set_key_parity( slot->data.raw.data,
4642 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004643#endif /* MBEDTLS_DES_C */
4644 }
4645 else
4646
4647#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4648 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4649 {
4650 mbedtls_rsa_context *rsa;
4651 int ret;
4652 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004653 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4654 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004655 /* Accept only byte-aligned keys, for the same reasons as
4656 * in psa_import_rsa_key(). */
4657 if( bits % 8 != 0 )
4658 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004659 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004660 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004661 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004662 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004663 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004664#if INT_MAX < 0xffffffff
4665 /* Check that the uint32_t value passed by the caller fits
4666 * in the range supported by this implementation. */
4667 if( p->e > INT_MAX )
4668 return( PSA_ERROR_NOT_SUPPORTED );
4669#endif
4670 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004671 }
4672 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4673 if( rsa == NULL )
4674 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4675 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4676 ret = mbedtls_rsa_gen_key( rsa,
4677 mbedtls_ctr_drbg_random,
4678 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004679 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004680 exponent );
4681 if( ret != 0 )
4682 {
4683 mbedtls_rsa_free( rsa );
4684 mbedtls_free( rsa );
4685 return( mbedtls_to_psa_error( ret ) );
4686 }
4687 slot->data.rsa = rsa;
4688 }
4689 else
4690#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4691
4692#if defined(MBEDTLS_ECP_C)
4693 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4694 {
4695 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4696 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4697 const mbedtls_ecp_curve_info *curve_info =
4698 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4699 mbedtls_ecp_keypair *ecp;
4700 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004701 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004702 return( PSA_ERROR_NOT_SUPPORTED );
4703 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4704 return( PSA_ERROR_NOT_SUPPORTED );
4705 if( curve_info->bit_size != bits )
4706 return( PSA_ERROR_INVALID_ARGUMENT );
4707 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4708 if( ecp == NULL )
4709 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4710 mbedtls_ecp_keypair_init( ecp );
4711 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4712 mbedtls_ctr_drbg_random,
4713 &global_data.ctr_drbg );
4714 if( ret != 0 )
4715 {
4716 mbedtls_ecp_keypair_free( ecp );
4717 mbedtls_free( ecp );
4718 return( mbedtls_to_psa_error( ret ) );
4719 }
4720 slot->data.ecp = ecp;
4721 }
4722 else
4723#endif /* MBEDTLS_ECP_C */
4724
4725 return( PSA_ERROR_NOT_SUPPORTED );
4726
4727 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004728
4729#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4730 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4731 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004732 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004733 }
4734#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4735
4736 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004737}
4738
4739
4740/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004741/* Module setup */
4742/****************************************************************/
4743
Gilles Peskine5e769522018-11-20 21:59:56 +01004744psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4745 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4746 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4747{
4748 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4749 return( PSA_ERROR_BAD_STATE );
4750 global_data.entropy_init = entropy_init;
4751 global_data.entropy_free = entropy_free;
4752 return( PSA_SUCCESS );
4753}
4754
Gilles Peskinee59236f2018-01-27 23:32:46 +01004755void mbedtls_psa_crypto_free( void )
4756{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004757 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004758 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4759 {
4760 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004761 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004762 }
4763 /* Wipe all remaining data, including configuration.
4764 * In particular, this sets all state indicator to the value
4765 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004766 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004767}
4768
4769psa_status_t psa_crypto_init( void )
4770{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004771 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004772 const unsigned char drbg_seed[] = "PSA";
4773
Gilles Peskinec6b69072018-11-20 21:42:52 +01004774 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004775 if( global_data.initialized != 0 )
4776 return( PSA_SUCCESS );
4777
Gilles Peskine5e769522018-11-20 21:59:56 +01004778 /* Set default configuration if
4779 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4780 if( global_data.entropy_init == NULL )
4781 global_data.entropy_init = mbedtls_entropy_init;
4782 if( global_data.entropy_free == NULL )
4783 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004784
Gilles Peskinec6b69072018-11-20 21:42:52 +01004785 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004786 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004787 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004788 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004789 status = mbedtls_to_psa_error(
4790 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4791 mbedtls_entropy_func,
4792 &global_data.entropy,
4793 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4794 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004795 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004796 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004797
Gilles Peskine66fb1262018-12-10 16:29:04 +01004798 status = psa_initialize_key_slots( );
4799 if( status != PSA_SUCCESS )
4800 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004801
4802 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004803 global_data.initialized = 1;
4804
Gilles Peskinee59236f2018-01-27 23:32:46 +01004805exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004806 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004807 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004808 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004809}
4810
4811#endif /* MBEDTLS_PSA_CRYPTO_C */