blob: 03d33717884d9a9b719bd6d383bd57a02001b6f4 [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 );
704 if( status != PSA_SUCCESS )
705 return( status );
706 }
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000707 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
708 {
709 status = psa_import_ec_public_key(
710 PSA_KEY_TYPE_GET_CURVE( slot->type ),
711 data, data_length,
712 &slot->data.ecp );
713
714 if( status != PSA_SUCCESS )
715 return( status );
716 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100717 else
718#endif /* MBEDTLS_ECP_C */
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000719#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
720 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100721 {
Jaeden Ameroec6ff862019-01-11 17:53:05 +0000722 status = psa_import_rsa_key( slot->type,
723 data, data_length,
724 &slot->data.rsa );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200725
Gilles Peskinec648d692018-06-28 08:46:13 +0200726 if( status != PSA_SUCCESS )
Gilles Peskinec648d692018-06-28 08:46:13 +0200727 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100728 }
729 else
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000730#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100731 {
732 return( PSA_ERROR_NOT_SUPPORTED );
733 }
Darryl Green940d72c2018-07-13 13:18:51 +0100734 return( PSA_SUCCESS );
735}
736
Darryl Green06fd18d2018-07-16 11:21:11 +0100737/* Retrieve an empty key slot (slot with no key data, but possibly
738 * with some metadata such as a policy). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100739static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100740 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100741{
742 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100743 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100744
745 *p_slot = NULL;
746
Gilles Peskinec5487a82018-12-03 18:08:14 +0100747 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100748 if( status != PSA_SUCCESS )
749 return( status );
750
751 if( slot->type != PSA_KEY_TYPE_NONE )
752 return( PSA_ERROR_OCCUPIED_SLOT );
753
754 *p_slot = slot;
755 return( status );
756}
757
758/** Retrieve a slot which must contain a key. The key must have allow all the
759 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
760 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100761static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100762 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100763 psa_key_usage_t usage,
764 psa_algorithm_t alg )
765{
766 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100767 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100768
769 *p_slot = NULL;
770
Gilles Peskinec5487a82018-12-03 18:08:14 +0100771 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100772 if( status != PSA_SUCCESS )
773 return( status );
774 if( slot->type == PSA_KEY_TYPE_NONE )
775 return( PSA_ERROR_EMPTY_SLOT );
776
777 /* Enforce that usage policy for the key slot contains all the flags
778 * required by the usage parameter. There is one exception: public
779 * keys can always be exported, so we treat public key objects as
780 * if they had the export flag. */
781 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
782 usage &= ~PSA_KEY_USAGE_EXPORT;
783 if( ( slot->policy.usage & usage ) != usage )
784 return( PSA_ERROR_NOT_PERMITTED );
785 if( alg != 0 && ( alg != slot->policy.alg ) )
786 return( PSA_ERROR_NOT_PERMITTED );
787
788 *p_slot = slot;
789 return( PSA_SUCCESS );
790}
Darryl Green940d72c2018-07-13 13:18:51 +0100791
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100792/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100793static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000794{
795 if( slot->type == PSA_KEY_TYPE_NONE )
796 {
797 /* No key material to clean. */
798 }
799 else if( key_type_is_raw_bytes( slot->type ) )
800 {
801 mbedtls_free( slot->data.raw.data );
802 }
803 else
804#if defined(MBEDTLS_RSA_C)
805 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
806 {
807 mbedtls_rsa_free( slot->data.rsa );
808 mbedtls_free( slot->data.rsa );
809 }
810 else
811#endif /* defined(MBEDTLS_RSA_C) */
812#if defined(MBEDTLS_ECP_C)
813 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
814 {
815 mbedtls_ecp_keypair_free( slot->data.ecp );
816 mbedtls_free( slot->data.ecp );
817 }
818 else
819#endif /* defined(MBEDTLS_ECP_C) */
820 {
821 /* Shouldn't happen: the key type is not any type that we
822 * put in. */
823 return( PSA_ERROR_TAMPERING_DETECTED );
824 }
825
826 return( PSA_SUCCESS );
827}
828
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100829/** Completely wipe a slot in memory, including its policy.
830 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100831psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100832{
833 psa_status_t status = psa_remove_key_data_from_memory( slot );
834 /* At this point, key material and other type-specific content has
835 * been wiped. Clear remaining metadata. We can call memset and not
836 * zeroize because the metadata is not particularly sensitive. */
837 memset( slot, 0, sizeof( *slot ) );
838 return( status );
839}
840
Gilles Peskinec5487a82018-12-03 18:08:14 +0100841psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100842 psa_key_type_t type,
843 const uint8_t *data,
844 size_t data_length )
845{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100846 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100847 psa_status_t status;
848
Gilles Peskinec5487a82018-12-03 18:08:14 +0100849 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100850 if( status != PSA_SUCCESS )
851 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100852
853 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100854
855 status = psa_import_key_into_slot( slot, data, data_length );
856 if( status != PSA_SUCCESS )
857 {
858 slot->type = PSA_KEY_TYPE_NONE;
859 return( status );
860 }
861
Darryl Greend49a4992018-06-18 17:27:26 +0100862#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
863 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
864 {
865 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100866 status = psa_save_persistent_key( slot->persistent_storage_id,
867 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100868 data_length );
869 if( status != PSA_SUCCESS )
870 {
871 (void) psa_remove_key_data_from_memory( slot );
872 slot->type = PSA_KEY_TYPE_NONE;
873 }
874 }
875#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
876
877 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100878}
879
Gilles Peskinec5487a82018-12-03 18:08:14 +0100880psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100881{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100882 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100883 psa_status_t status = PSA_SUCCESS;
884 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100885
Gilles Peskinec5487a82018-12-03 18:08:14 +0100886 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200887 if( status != PSA_SUCCESS )
888 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100889#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
890 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
891 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100892 storage_status =
893 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100894 }
895#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100896 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100897 if( status != PSA_SUCCESS )
898 return( status );
899 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100900}
901
Gilles Peskineb870b182018-07-06 16:02:09 +0200902/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100903static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200904{
905 if( key_type_is_raw_bytes( slot->type ) )
906 return( slot->data.raw.bytes * 8 );
907#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200908 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100909 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200910#endif /* defined(MBEDTLS_RSA_C) */
911#if defined(MBEDTLS_ECP_C)
912 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
913 return( slot->data.ecp->grp.pbits );
914#endif /* defined(MBEDTLS_ECP_C) */
915 /* Shouldn't happen except on an empty slot. */
916 return( 0 );
917}
918
Gilles Peskinec5487a82018-12-03 18:08:14 +0100919psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200920 psa_key_type_t *type,
921 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100922{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100923 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200924 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100925
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100926 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200927 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100928 if( bits != NULL )
929 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100930 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200931 if( status != PSA_SUCCESS )
932 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200933
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100934 if( slot->type == PSA_KEY_TYPE_NONE )
935 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200936 if( type != NULL )
937 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200938 if( bits != NULL )
939 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100940 return( PSA_SUCCESS );
941}
942
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000943#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero6b196002019-01-10 10:23:21 +0000944static int pk_write_pubkey_simple( mbedtls_pk_context *key,
945 unsigned char *buf, size_t size )
946{
947 int ret;
948 unsigned char *c;
949 size_t len = 0;
950
951 c = buf + size;
952
953 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
954
955 return( (int) len );
956}
Jaeden Amero0ae445f2019-01-10 11:42:27 +0000957#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero6b196002019-01-10 10:23:21 +0000958
Gilles Peskine2f060a82018-12-04 17:12:32 +0100959static psa_status_t psa_internal_export_key( psa_key_slot_t *slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200960 uint8_t *data,
961 size_t data_size,
962 size_t *data_length,
963 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100964{
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100965 *data_length = 0;
966
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200967 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300968 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300969
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200970 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100971 {
972 if( slot->data.raw.bytes > data_size )
973 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +0100974 if( data_size != 0 )
975 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200976 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +0100977 memset( data + slot->data.raw.bytes, 0,
978 data_size - slot->data.raw.bytes );
979 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100980 *data_length = slot->data.raw.bytes;
981 return( PSA_SUCCESS );
982 }
Gilles Peskine188c71e2018-10-29 19:26:02 +0100983#if defined(MBEDTLS_ECP_C)
984 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
985 {
Darryl Greendd8fb772018-11-07 16:00:44 +0000986 psa_status_t status;
987
Gilles Peskine188c71e2018-10-29 19:26:02 +0100988 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
989 if( bytes > data_size )
990 return( PSA_ERROR_BUFFER_TOO_SMALL );
991 status = mbedtls_to_psa_error(
992 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
993 if( status != PSA_SUCCESS )
994 return( status );
995 memset( data + bytes, 0, data_size - bytes );
996 *data_length = bytes;
997 return( PSA_SUCCESS );
998 }
999#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001000 else
Moran Peker17e36e12018-05-02 12:55:20 +03001001 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001002#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001003 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001004 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001005 {
Moran Pekera998bc62018-04-16 18:16:20 +03001006 mbedtls_pk_context pk;
1007 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001008 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001009 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001010#if defined(MBEDTLS_RSA_C)
1011 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001012 pk.pk_info = &mbedtls_rsa_info;
1013 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001014#else
1015 return( PSA_ERROR_NOT_SUPPORTED );
1016#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001017 }
1018 else
1019 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001020#if defined(MBEDTLS_ECP_C)
1021 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001022 pk.pk_info = &mbedtls_eckey_info;
1023 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001024#else
1025 return( PSA_ERROR_NOT_SUPPORTED );
1026#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001027 }
Moran Pekerd7326592018-05-29 16:56:39 +03001028 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero6b196002019-01-10 10:23:21 +00001029 {
Jaeden Amero0ae445f2019-01-10 11:42:27 +00001030 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001031 }
Moran Peker17e36e12018-05-02 12:55:20 +03001032 else
Jaeden Amero6b196002019-01-10 10:23:21 +00001033 {
Moran Peker17e36e12018-05-02 12:55:20 +03001034 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero6b196002019-01-10 10:23:21 +00001035 }
Moran Peker60364322018-04-29 11:34:58 +03001036 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001037 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001038 /* If data_size is 0 then data may be NULL and then the
1039 * call to memset would have undefined behavior. */
1040 if( data_size != 0 )
1041 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001042 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001043 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001044 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1045 * Move the data to the beginning and erase remaining data
1046 * at the original location. */
1047 if( 2 * (size_t) ret <= data_size )
1048 {
1049 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001050 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001051 }
1052 else if( (size_t) ret < data_size )
1053 {
1054 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001055 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001056 }
Moran Pekera998bc62018-04-16 18:16:20 +03001057 *data_length = ret;
1058 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001059 }
1060 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001061#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001062 {
1063 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001064 it is valid for a special-purpose implementation to omit
1065 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001066 return( PSA_ERROR_NOT_SUPPORTED );
1067 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001068 }
1069}
1070
Gilles Peskinec5487a82018-12-03 18:08:14 +01001071psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001072 uint8_t *data,
1073 size_t data_size,
1074 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001075{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001076 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001077 psa_status_t status;
1078
1079 /* Set the key to empty now, so that even when there are errors, we always
1080 * set data_length to a value between 0 and data_size. On error, setting
1081 * the key to empty is a good choice because an empty key representation is
1082 * unlikely to be accepted anywhere. */
1083 *data_length = 0;
1084
1085 /* Export requires the EXPORT flag. There is an exception for public keys,
1086 * which don't require any flag, but psa_get_key_from_slot takes
1087 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001088 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001089 if( status != PSA_SUCCESS )
1090 return( status );
1091 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001092 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001093}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001094
Gilles Peskinec5487a82018-12-03 18:08:14 +01001095psa_status_t psa_export_public_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 Pekerdd4ea382018-04-03 15:30:03 +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 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001110 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001111 if( status != PSA_SUCCESS )
1112 return( status );
1113 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001114 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001115}
1116
Darryl Green0c6575a2018-11-07 16:05:30 +00001117#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001118static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001119 size_t bits )
1120{
1121 psa_status_t status;
1122 uint8_t *data;
1123 size_t key_length;
1124 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1125 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001126 if( data == NULL )
1127 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001128 /* Get key data in export format */
1129 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1130 if( status != PSA_SUCCESS )
1131 {
1132 slot->type = PSA_KEY_TYPE_NONE;
1133 goto exit;
1134 }
1135 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001136 status = psa_save_persistent_key( slot->persistent_storage_id,
1137 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001138 data, key_length );
1139 if( status != PSA_SUCCESS )
1140 {
1141 slot->type = PSA_KEY_TYPE_NONE;
1142 }
1143exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001144 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001145 mbedtls_free( data );
1146 return( status );
1147}
1148#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1149
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001150
1151
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001152/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001153/* Message digests */
1154/****************************************************************/
1155
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001156static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001157{
1158 switch( alg )
1159 {
1160#if defined(MBEDTLS_MD2_C)
1161 case PSA_ALG_MD2:
1162 return( &mbedtls_md2_info );
1163#endif
1164#if defined(MBEDTLS_MD4_C)
1165 case PSA_ALG_MD4:
1166 return( &mbedtls_md4_info );
1167#endif
1168#if defined(MBEDTLS_MD5_C)
1169 case PSA_ALG_MD5:
1170 return( &mbedtls_md5_info );
1171#endif
1172#if defined(MBEDTLS_RIPEMD160_C)
1173 case PSA_ALG_RIPEMD160:
1174 return( &mbedtls_ripemd160_info );
1175#endif
1176#if defined(MBEDTLS_SHA1_C)
1177 case PSA_ALG_SHA_1:
1178 return( &mbedtls_sha1_info );
1179#endif
1180#if defined(MBEDTLS_SHA256_C)
1181 case PSA_ALG_SHA_224:
1182 return( &mbedtls_sha224_info );
1183 case PSA_ALG_SHA_256:
1184 return( &mbedtls_sha256_info );
1185#endif
1186#if defined(MBEDTLS_SHA512_C)
1187 case PSA_ALG_SHA_384:
1188 return( &mbedtls_sha384_info );
1189 case PSA_ALG_SHA_512:
1190 return( &mbedtls_sha512_info );
1191#endif
1192 default:
1193 return( NULL );
1194 }
1195}
1196
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001197psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1198{
1199 switch( operation->alg )
1200 {
Gilles Peskine81736312018-06-26 15:04:31 +02001201 case 0:
1202 /* The object has (apparently) been initialized but it is not
1203 * in use. It's ok to call abort on such an object, and there's
1204 * nothing to do. */
1205 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001206#if defined(MBEDTLS_MD2_C)
1207 case PSA_ALG_MD2:
1208 mbedtls_md2_free( &operation->ctx.md2 );
1209 break;
1210#endif
1211#if defined(MBEDTLS_MD4_C)
1212 case PSA_ALG_MD4:
1213 mbedtls_md4_free( &operation->ctx.md4 );
1214 break;
1215#endif
1216#if defined(MBEDTLS_MD5_C)
1217 case PSA_ALG_MD5:
1218 mbedtls_md5_free( &operation->ctx.md5 );
1219 break;
1220#endif
1221#if defined(MBEDTLS_RIPEMD160_C)
1222 case PSA_ALG_RIPEMD160:
1223 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1224 break;
1225#endif
1226#if defined(MBEDTLS_SHA1_C)
1227 case PSA_ALG_SHA_1:
1228 mbedtls_sha1_free( &operation->ctx.sha1 );
1229 break;
1230#endif
1231#if defined(MBEDTLS_SHA256_C)
1232 case PSA_ALG_SHA_224:
1233 case PSA_ALG_SHA_256:
1234 mbedtls_sha256_free( &operation->ctx.sha256 );
1235 break;
1236#endif
1237#if defined(MBEDTLS_SHA512_C)
1238 case PSA_ALG_SHA_384:
1239 case PSA_ALG_SHA_512:
1240 mbedtls_sha512_free( &operation->ctx.sha512 );
1241 break;
1242#endif
1243 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001244 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001245 }
1246 operation->alg = 0;
1247 return( PSA_SUCCESS );
1248}
1249
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001250psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001251 psa_algorithm_t alg )
1252{
1253 int ret;
1254 operation->alg = 0;
1255 switch( alg )
1256 {
1257#if defined(MBEDTLS_MD2_C)
1258 case PSA_ALG_MD2:
1259 mbedtls_md2_init( &operation->ctx.md2 );
1260 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1261 break;
1262#endif
1263#if defined(MBEDTLS_MD4_C)
1264 case PSA_ALG_MD4:
1265 mbedtls_md4_init( &operation->ctx.md4 );
1266 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1267 break;
1268#endif
1269#if defined(MBEDTLS_MD5_C)
1270 case PSA_ALG_MD5:
1271 mbedtls_md5_init( &operation->ctx.md5 );
1272 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1273 break;
1274#endif
1275#if defined(MBEDTLS_RIPEMD160_C)
1276 case PSA_ALG_RIPEMD160:
1277 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1278 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1279 break;
1280#endif
1281#if defined(MBEDTLS_SHA1_C)
1282 case PSA_ALG_SHA_1:
1283 mbedtls_sha1_init( &operation->ctx.sha1 );
1284 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1285 break;
1286#endif
1287#if defined(MBEDTLS_SHA256_C)
1288 case PSA_ALG_SHA_224:
1289 mbedtls_sha256_init( &operation->ctx.sha256 );
1290 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1291 break;
1292 case PSA_ALG_SHA_256:
1293 mbedtls_sha256_init( &operation->ctx.sha256 );
1294 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1295 break;
1296#endif
1297#if defined(MBEDTLS_SHA512_C)
1298 case PSA_ALG_SHA_384:
1299 mbedtls_sha512_init( &operation->ctx.sha512 );
1300 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1301 break;
1302 case PSA_ALG_SHA_512:
1303 mbedtls_sha512_init( &operation->ctx.sha512 );
1304 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1305 break;
1306#endif
1307 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001308 return( PSA_ALG_IS_HASH( alg ) ?
1309 PSA_ERROR_NOT_SUPPORTED :
1310 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001311 }
1312 if( ret == 0 )
1313 operation->alg = alg;
1314 else
1315 psa_hash_abort( operation );
1316 return( mbedtls_to_psa_error( ret ) );
1317}
1318
1319psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1320 const uint8_t *input,
1321 size_t input_length )
1322{
1323 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001324
1325 /* Don't require hash implementations to behave correctly on a
1326 * zero-length input, which may have an invalid pointer. */
1327 if( input_length == 0 )
1328 return( PSA_SUCCESS );
1329
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001330 switch( operation->alg )
1331 {
1332#if defined(MBEDTLS_MD2_C)
1333 case PSA_ALG_MD2:
1334 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1335 input, input_length );
1336 break;
1337#endif
1338#if defined(MBEDTLS_MD4_C)
1339 case PSA_ALG_MD4:
1340 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1341 input, input_length );
1342 break;
1343#endif
1344#if defined(MBEDTLS_MD5_C)
1345 case PSA_ALG_MD5:
1346 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1347 input, input_length );
1348 break;
1349#endif
1350#if defined(MBEDTLS_RIPEMD160_C)
1351 case PSA_ALG_RIPEMD160:
1352 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1353 input, input_length );
1354 break;
1355#endif
1356#if defined(MBEDTLS_SHA1_C)
1357 case PSA_ALG_SHA_1:
1358 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1359 input, input_length );
1360 break;
1361#endif
1362#if defined(MBEDTLS_SHA256_C)
1363 case PSA_ALG_SHA_224:
1364 case PSA_ALG_SHA_256:
1365 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1366 input, input_length );
1367 break;
1368#endif
1369#if defined(MBEDTLS_SHA512_C)
1370 case PSA_ALG_SHA_384:
1371 case PSA_ALG_SHA_512:
1372 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1373 input, input_length );
1374 break;
1375#endif
1376 default:
1377 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1378 break;
1379 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001380
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001381 if( ret != 0 )
1382 psa_hash_abort( operation );
1383 return( mbedtls_to_psa_error( ret ) );
1384}
1385
1386psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1387 uint8_t *hash,
1388 size_t hash_size,
1389 size_t *hash_length )
1390{
itayzafrir40835d42018-08-02 13:14:17 +03001391 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001392 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001393 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001394
1395 /* Fill the output buffer with something that isn't a valid hash
1396 * (barring an attack on the hash and deliberately-crafted input),
1397 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001398 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001399 /* If hash_size is 0 then hash may be NULL and then the
1400 * call to memset would have undefined behavior. */
1401 if( hash_size != 0 )
1402 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001403
1404 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001405 {
1406 status = PSA_ERROR_BUFFER_TOO_SMALL;
1407 goto exit;
1408 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001409
1410 switch( operation->alg )
1411 {
1412#if defined(MBEDTLS_MD2_C)
1413 case PSA_ALG_MD2:
1414 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1415 break;
1416#endif
1417#if defined(MBEDTLS_MD4_C)
1418 case PSA_ALG_MD4:
1419 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1420 break;
1421#endif
1422#if defined(MBEDTLS_MD5_C)
1423 case PSA_ALG_MD5:
1424 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1425 break;
1426#endif
1427#if defined(MBEDTLS_RIPEMD160_C)
1428 case PSA_ALG_RIPEMD160:
1429 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1430 break;
1431#endif
1432#if defined(MBEDTLS_SHA1_C)
1433 case PSA_ALG_SHA_1:
1434 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1435 break;
1436#endif
1437#if defined(MBEDTLS_SHA256_C)
1438 case PSA_ALG_SHA_224:
1439 case PSA_ALG_SHA_256:
1440 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1441 break;
1442#endif
1443#if defined(MBEDTLS_SHA512_C)
1444 case PSA_ALG_SHA_384:
1445 case PSA_ALG_SHA_512:
1446 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1447 break;
1448#endif
1449 default:
1450 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1451 break;
1452 }
itayzafrir40835d42018-08-02 13:14:17 +03001453 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001454
itayzafrir40835d42018-08-02 13:14:17 +03001455exit:
1456 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001457 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001458 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001459 return( psa_hash_abort( operation ) );
1460 }
1461 else
1462 {
1463 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001464 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001465 }
1466}
1467
Gilles Peskine2d277862018-06-18 15:41:12 +02001468psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1469 const uint8_t *hash,
1470 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001471{
1472 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1473 size_t actual_hash_length;
1474 psa_status_t status = psa_hash_finish( operation,
1475 actual_hash, sizeof( actual_hash ),
1476 &actual_hash_length );
1477 if( status != PSA_SUCCESS )
1478 return( status );
1479 if( actual_hash_length != hash_length )
1480 return( PSA_ERROR_INVALID_SIGNATURE );
1481 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1482 return( PSA_ERROR_INVALID_SIGNATURE );
1483 return( PSA_SUCCESS );
1484}
1485
1486
1487
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001488/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001489/* MAC */
1490/****************************************************************/
1491
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001492static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001493 psa_algorithm_t alg,
1494 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001495 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001496 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001497{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001498 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001499 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001500
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001501 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001502 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001503
Gilles Peskine8c9def32018-02-08 10:02:12 +01001504 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1505 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001506 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001507 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001508 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001509 mode = MBEDTLS_MODE_STREAM;
1510 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001511 case PSA_ALG_CTR:
1512 mode = MBEDTLS_MODE_CTR;
1513 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001514 case PSA_ALG_CFB:
1515 mode = MBEDTLS_MODE_CFB;
1516 break;
1517 case PSA_ALG_OFB:
1518 mode = MBEDTLS_MODE_OFB;
1519 break;
1520 case PSA_ALG_CBC_NO_PADDING:
1521 mode = MBEDTLS_MODE_CBC;
1522 break;
1523 case PSA_ALG_CBC_PKCS7:
1524 mode = MBEDTLS_MODE_CBC;
1525 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001526 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001527 mode = MBEDTLS_MODE_CCM;
1528 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001529 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001530 mode = MBEDTLS_MODE_GCM;
1531 break;
1532 default:
1533 return( NULL );
1534 }
1535 }
1536 else if( alg == PSA_ALG_CMAC )
1537 mode = MBEDTLS_MODE_ECB;
1538 else if( alg == PSA_ALG_GMAC )
1539 mode = MBEDTLS_MODE_GCM;
1540 else
1541 return( NULL );
1542
1543 switch( key_type )
1544 {
1545 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001546 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001547 break;
1548 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001549 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1550 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001551 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001552 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001553 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001554 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001555 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1556 * but two-key Triple-DES is functionally three-key Triple-DES
1557 * with K1=K3, so that's how we present it to mbedtls. */
1558 if( key_bits == 128 )
1559 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001560 break;
1561 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001562 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001563 break;
1564 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001565 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001566 break;
1567 default:
1568 return( NULL );
1569 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001570 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001571 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001572
Jaeden Amero23bbb752018-06-26 14:16:54 +01001573 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1574 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001575}
1576
Gilles Peskinea05219c2018-11-16 16:02:56 +01001577#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001578static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001579{
Gilles Peskine2d277862018-06-18 15:41:12 +02001580 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001581 {
1582 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001583 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001584 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001585 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001586 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001587 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001588 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001589 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001590 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001591 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001592 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001593 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001594 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001595 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001596 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001597 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001598 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001599 return( 128 );
1600 default:
1601 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001602 }
1603}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001604#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001605
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001606/* Initialize the MAC operation structure. Once this function has been
1607 * called, psa_mac_abort can run and will do the right thing. */
1608static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1609 psa_algorithm_t alg )
1610{
1611 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1612
1613 operation->alg = alg;
1614 operation->key_set = 0;
1615 operation->iv_set = 0;
1616 operation->iv_required = 0;
1617 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001618 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001619
1620#if defined(MBEDTLS_CMAC_C)
1621 if( alg == PSA_ALG_CMAC )
1622 {
1623 operation->iv_required = 0;
1624 mbedtls_cipher_init( &operation->ctx.cmac );
1625 status = PSA_SUCCESS;
1626 }
1627 else
1628#endif /* MBEDTLS_CMAC_C */
1629#if defined(MBEDTLS_MD_C)
1630 if( PSA_ALG_IS_HMAC( operation->alg ) )
1631 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001632 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1633 operation->ctx.hmac.hash_ctx.alg = 0;
1634 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001635 }
1636 else
1637#endif /* MBEDTLS_MD_C */
1638 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001639 if( ! PSA_ALG_IS_MAC( alg ) )
1640 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001641 }
1642
1643 if( status != PSA_SUCCESS )
1644 memset( operation, 0, sizeof( *operation ) );
1645 return( status );
1646}
1647
Gilles Peskine01126fa2018-07-12 17:04:55 +02001648#if defined(MBEDTLS_MD_C)
1649static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1650{
Gilles Peskine3f108122018-12-07 18:14:53 +01001651 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001652 return( psa_hash_abort( &hmac->hash_ctx ) );
1653}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001654
1655static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1656{
1657 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1658 memset( hmac, 0, sizeof( *hmac ) );
1659}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001660#endif /* MBEDTLS_MD_C */
1661
Gilles Peskine8c9def32018-02-08 10:02:12 +01001662psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1663{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001664 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001665 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001666 /* The object has (apparently) been initialized but it is not
1667 * in use. It's ok to call abort on such an object, and there's
1668 * nothing to do. */
1669 return( PSA_SUCCESS );
1670 }
1671 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001672#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001673 if( operation->alg == PSA_ALG_CMAC )
1674 {
1675 mbedtls_cipher_free( &operation->ctx.cmac );
1676 }
1677 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001678#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001679#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001680 if( PSA_ALG_IS_HMAC( operation->alg ) )
1681 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001682 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001683 }
1684 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001685#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001686 {
1687 /* Sanity check (shouldn't happen: operation->alg should
1688 * always have been initialized to a valid value). */
1689 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001690 }
Moran Peker41deec42018-04-04 15:43:05 +03001691
Gilles Peskine8c9def32018-02-08 10:02:12 +01001692 operation->alg = 0;
1693 operation->key_set = 0;
1694 operation->iv_set = 0;
1695 operation->iv_required = 0;
1696 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001697 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001698
Gilles Peskine8c9def32018-02-08 10:02:12 +01001699 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001700
1701bad_state:
1702 /* If abort is called on an uninitialized object, we can't trust
1703 * anything. Wipe the object in case it contains confidential data.
1704 * This may result in a memory leak if a pointer gets overwritten,
1705 * but it's too late to do anything about this. */
1706 memset( operation, 0, sizeof( *operation ) );
1707 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001708}
1709
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001710#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001711static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001712 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001713 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001714 const mbedtls_cipher_info_t *cipher_info )
1715{
1716 int ret;
1717
1718 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001719
1720 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1721 if( ret != 0 )
1722 return( ret );
1723
1724 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1725 slot->data.raw.data,
1726 key_bits );
1727 return( ret );
1728}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001729#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001730
Gilles Peskine248051a2018-06-20 16:09:38 +02001731#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001732static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1733 const uint8_t *key,
1734 size_t key_length,
1735 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001736{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001737 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001738 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001739 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001740 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001741 psa_status_t status;
1742
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001743 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1744 * overflow below. This should never trigger if the hash algorithm
1745 * is implemented correctly. */
1746 /* The size checks against the ipad and opad buffers cannot be written
1747 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1748 * because that triggers -Wlogical-op on GCC 7.3. */
1749 if( block_size > sizeof( ipad ) )
1750 return( PSA_ERROR_NOT_SUPPORTED );
1751 if( block_size > sizeof( hmac->opad ) )
1752 return( PSA_ERROR_NOT_SUPPORTED );
1753 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001754 return( PSA_ERROR_NOT_SUPPORTED );
1755
Gilles Peskined223b522018-06-11 18:12:58 +02001756 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001757 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001758 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001759 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001760 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001761 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001762 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001763 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001764 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001765 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001766 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001767 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001768 }
Gilles Peskine96889972018-07-12 17:07:03 +02001769 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1770 * but it is permitted. It is common when HMAC is used in HKDF, for
1771 * example. Don't call `memcpy` in the 0-length because `key` could be
1772 * an invalid pointer which would make the behavior undefined. */
1773 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001774 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001775
Gilles Peskined223b522018-06-11 18:12:58 +02001776 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1777 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001778 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001779 ipad[i] ^= 0x36;
1780 memset( ipad + key_length, 0x36, block_size - key_length );
1781
1782 /* Copy the key material from ipad to opad, flipping the requisite bits,
1783 * and filling the rest of opad with the requisite constant. */
1784 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001785 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1786 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001787
Gilles Peskine01126fa2018-07-12 17:04:55 +02001788 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001789 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001790 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001791
Gilles Peskine01126fa2018-07-12 17:04:55 +02001792 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001793
1794cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001795 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001796
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001797 return( status );
1798}
Gilles Peskine248051a2018-06-20 16:09:38 +02001799#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001800
Gilles Peskine89167cb2018-07-08 20:12:23 +02001801static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001802 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001803 psa_algorithm_t alg,
1804 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001805{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001806 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001807 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001808 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001809 psa_key_usage_t usage =
1810 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001811 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001812 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001813
Gilles Peskined911eb72018-08-14 15:18:45 +02001814 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001815 if( status != PSA_SUCCESS )
1816 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001817 if( is_sign )
1818 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001819
Gilles Peskinec5487a82018-12-03 18:08:14 +01001820 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001821 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001822 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001823 key_bits = psa_get_key_bits( slot );
1824
Gilles Peskine8c9def32018-02-08 10:02:12 +01001825#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001826 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001827 {
1828 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001829 mbedtls_cipher_info_from_psa( full_length_alg,
1830 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001831 int ret;
1832 if( cipher_info == NULL )
1833 {
1834 status = PSA_ERROR_NOT_SUPPORTED;
1835 goto exit;
1836 }
1837 operation->mac_size = cipher_info->block_size;
1838 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1839 status = mbedtls_to_psa_error( ret );
1840 }
1841 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001842#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001843#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001844 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001845 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001846 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001847 if( hash_alg == 0 )
1848 {
1849 status = PSA_ERROR_NOT_SUPPORTED;
1850 goto exit;
1851 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001852
1853 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1854 /* Sanity check. This shouldn't fail on a valid configuration. */
1855 if( operation->mac_size == 0 ||
1856 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1857 {
1858 status = PSA_ERROR_NOT_SUPPORTED;
1859 goto exit;
1860 }
1861
Gilles Peskine01126fa2018-07-12 17:04:55 +02001862 if( slot->type != PSA_KEY_TYPE_HMAC )
1863 {
1864 status = PSA_ERROR_INVALID_ARGUMENT;
1865 goto exit;
1866 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001867
Gilles Peskine01126fa2018-07-12 17:04:55 +02001868 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1869 slot->data.raw.data,
1870 slot->data.raw.bytes,
1871 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001872 }
1873 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001874#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001875 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00001876 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02001877 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001878 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001879
Gilles Peskined911eb72018-08-14 15:18:45 +02001880 if( truncated == 0 )
1881 {
1882 /* The "normal" case: untruncated algorithm. Nothing to do. */
1883 }
1884 else if( truncated < 4 )
1885 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001886 /* A very short MAC is too short for security since it can be
1887 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1888 * so we make this our minimum, even though 32 bits is still
1889 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001890 status = PSA_ERROR_NOT_SUPPORTED;
1891 }
1892 else if( truncated > operation->mac_size )
1893 {
1894 /* It's impossible to "truncate" to a larger length. */
1895 status = PSA_ERROR_INVALID_ARGUMENT;
1896 }
1897 else
1898 operation->mac_size = truncated;
1899
Gilles Peskinefbfac682018-07-08 20:51:54 +02001900exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001901 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001902 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001903 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001904 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001905 else
1906 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001907 operation->key_set = 1;
1908 }
1909 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001910}
1911
Gilles Peskine89167cb2018-07-08 20:12:23 +02001912psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001913 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001914 psa_algorithm_t alg )
1915{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001916 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001917}
1918
1919psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001920 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001921 psa_algorithm_t alg )
1922{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001923 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001924}
1925
Gilles Peskine8c9def32018-02-08 10:02:12 +01001926psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1927 const uint8_t *input,
1928 size_t input_length )
1929{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001930 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001931 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001932 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001933 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001934 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001935 operation->has_input = 1;
1936
Gilles Peskine8c9def32018-02-08 10:02:12 +01001937#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001938 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001939 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001940 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1941 input, input_length );
1942 status = mbedtls_to_psa_error( ret );
1943 }
1944 else
1945#endif /* MBEDTLS_CMAC_C */
1946#if defined(MBEDTLS_MD_C)
1947 if( PSA_ALG_IS_HMAC( operation->alg ) )
1948 {
1949 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1950 input_length );
1951 }
1952 else
1953#endif /* MBEDTLS_MD_C */
1954 {
1955 /* This shouldn't happen if `operation` was initialized by
1956 * a setup function. */
1957 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001958 }
1959
Gilles Peskinefbfac682018-07-08 20:51:54 +02001960cleanup:
1961 if( status != PSA_SUCCESS )
1962 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001963 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001964}
1965
Gilles Peskine01126fa2018-07-12 17:04:55 +02001966#if defined(MBEDTLS_MD_C)
1967static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1968 uint8_t *mac,
1969 size_t mac_size )
1970{
1971 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1972 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1973 size_t hash_size = 0;
1974 size_t block_size = psa_get_hash_block_size( hash_alg );
1975 psa_status_t status;
1976
Gilles Peskine01126fa2018-07-12 17:04:55 +02001977 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1978 if( status != PSA_SUCCESS )
1979 return( status );
1980 /* From here on, tmp needs to be wiped. */
1981
1982 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1983 if( status != PSA_SUCCESS )
1984 goto exit;
1985
1986 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1987 if( status != PSA_SUCCESS )
1988 goto exit;
1989
1990 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1991 if( status != PSA_SUCCESS )
1992 goto exit;
1993
Gilles Peskined911eb72018-08-14 15:18:45 +02001994 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1995 if( status != PSA_SUCCESS )
1996 goto exit;
1997
1998 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001999
2000exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002001 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002002 return( status );
2003}
2004#endif /* MBEDTLS_MD_C */
2005
mohammad16036df908f2018-04-02 08:34:15 -07002006static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002007 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002008 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002009{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002010 if( ! operation->key_set )
2011 return( PSA_ERROR_BAD_STATE );
2012 if( operation->iv_required && ! operation->iv_set )
2013 return( PSA_ERROR_BAD_STATE );
2014
Gilles Peskine8c9def32018-02-08 10:02:12 +01002015 if( mac_size < operation->mac_size )
2016 return( PSA_ERROR_BUFFER_TOO_SMALL );
2017
Gilles Peskine8c9def32018-02-08 10:02:12 +01002018#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002019 if( operation->alg == PSA_ALG_CMAC )
2020 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002021 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2022 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2023 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002024 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002025 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002026 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002027 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002028 else
2029#endif /* MBEDTLS_CMAC_C */
2030#if defined(MBEDTLS_MD_C)
2031 if( PSA_ALG_IS_HMAC( operation->alg ) )
2032 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002033 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002034 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002035 }
2036 else
2037#endif /* MBEDTLS_MD_C */
2038 {
2039 /* This shouldn't happen if `operation` was initialized by
2040 * a setup function. */
2041 return( PSA_ERROR_BAD_STATE );
2042 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002043}
2044
Gilles Peskineacd4be32018-07-08 19:56:25 +02002045psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2046 uint8_t *mac,
2047 size_t mac_size,
2048 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002049{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002050 psa_status_t status;
2051
2052 /* Fill the output buffer with something that isn't a valid mac
2053 * (barring an attack on the mac and deliberately-crafted input),
2054 * in case the caller doesn't check the return status properly. */
2055 *mac_length = mac_size;
2056 /* If mac_size is 0 then mac may be NULL and then the
2057 * call to memset would have undefined behavior. */
2058 if( mac_size != 0 )
2059 memset( mac, '!', mac_size );
2060
Gilles Peskine89167cb2018-07-08 20:12:23 +02002061 if( ! operation->is_sign )
2062 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002063 status = PSA_ERROR_BAD_STATE;
2064 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002065 }
mohammad16036df908f2018-04-02 08:34:15 -07002066
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002067 status = psa_mac_finish_internal( operation, mac, mac_size );
2068
2069cleanup:
2070 if( status == PSA_SUCCESS )
2071 {
2072 status = psa_mac_abort( operation );
2073 if( status == PSA_SUCCESS )
2074 *mac_length = operation->mac_size;
2075 else
2076 memset( mac, '!', mac_size );
2077 }
2078 else
2079 psa_mac_abort( operation );
2080 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002081}
2082
Gilles Peskineacd4be32018-07-08 19:56:25 +02002083psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2084 const uint8_t *mac,
2085 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002086{
Gilles Peskine828ed142018-06-18 23:25:51 +02002087 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002088 psa_status_t status;
2089
Gilles Peskine89167cb2018-07-08 20:12:23 +02002090 if( operation->is_sign )
2091 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002092 status = PSA_ERROR_BAD_STATE;
2093 goto cleanup;
2094 }
2095 if( operation->mac_size != mac_length )
2096 {
2097 status = PSA_ERROR_INVALID_SIGNATURE;
2098 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002099 }
mohammad16036df908f2018-04-02 08:34:15 -07002100
2101 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002102 actual_mac, sizeof( actual_mac ) );
2103
2104 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2105 status = PSA_ERROR_INVALID_SIGNATURE;
2106
2107cleanup:
2108 if( status == PSA_SUCCESS )
2109 status = psa_mac_abort( operation );
2110 else
2111 psa_mac_abort( operation );
2112
Gilles Peskine3f108122018-12-07 18:14:53 +01002113 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002114
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002115 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002116}
2117
2118
Gilles Peskine20035e32018-02-03 22:44:14 +01002119
Gilles Peskine20035e32018-02-03 22:44:14 +01002120/****************************************************************/
2121/* Asymmetric cryptography */
2122/****************************************************************/
2123
Gilles Peskine2b450e32018-06-27 15:42:46 +02002124#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002125/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002126 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002127static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2128 size_t hash_length,
2129 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002130{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002131 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002132 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002133 *md_alg = mbedtls_md_get_type( md_info );
2134
2135 /* The Mbed TLS RSA module uses an unsigned int for hash length
2136 * parameters. Validate that it fits so that we don't risk an
2137 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002138#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002139 if( hash_length > UINT_MAX )
2140 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002141#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002142
2143#if defined(MBEDTLS_PKCS1_V15)
2144 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2145 * must be correct. */
2146 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2147 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002148 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002149 if( md_info == NULL )
2150 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002151 if( mbedtls_md_get_size( md_info ) != hash_length )
2152 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002153 }
2154#endif /* MBEDTLS_PKCS1_V15 */
2155
2156#if defined(MBEDTLS_PKCS1_V21)
2157 /* PSS requires a hash internally. */
2158 if( PSA_ALG_IS_RSA_PSS( alg ) )
2159 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002160 if( md_info == NULL )
2161 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002162 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002163#endif /* MBEDTLS_PKCS1_V21 */
2164
Gilles Peskine61b91d42018-06-08 16:09:36 +02002165 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002166}
2167
Gilles Peskine2b450e32018-06-27 15:42:46 +02002168static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2169 psa_algorithm_t alg,
2170 const uint8_t *hash,
2171 size_t hash_length,
2172 uint8_t *signature,
2173 size_t signature_size,
2174 size_t *signature_length )
2175{
2176 psa_status_t status;
2177 int ret;
2178 mbedtls_md_type_t md_alg;
2179
2180 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2181 if( status != PSA_SUCCESS )
2182 return( status );
2183
Gilles Peskine630a18a2018-06-29 17:49:35 +02002184 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002185 return( PSA_ERROR_BUFFER_TOO_SMALL );
2186
2187#if defined(MBEDTLS_PKCS1_V15)
2188 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2189 {
2190 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2191 MBEDTLS_MD_NONE );
2192 ret = mbedtls_rsa_pkcs1_sign( rsa,
2193 mbedtls_ctr_drbg_random,
2194 &global_data.ctr_drbg,
2195 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002196 md_alg,
2197 (unsigned int) hash_length,
2198 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002199 signature );
2200 }
2201 else
2202#endif /* MBEDTLS_PKCS1_V15 */
2203#if defined(MBEDTLS_PKCS1_V21)
2204 if( PSA_ALG_IS_RSA_PSS( alg ) )
2205 {
2206 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2207 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2208 mbedtls_ctr_drbg_random,
2209 &global_data.ctr_drbg,
2210 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002211 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002212 (unsigned int) hash_length,
2213 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002214 signature );
2215 }
2216 else
2217#endif /* MBEDTLS_PKCS1_V21 */
2218 {
2219 return( PSA_ERROR_INVALID_ARGUMENT );
2220 }
2221
2222 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002223 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002224 return( mbedtls_to_psa_error( ret ) );
2225}
2226
2227static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2228 psa_algorithm_t alg,
2229 const uint8_t *hash,
2230 size_t hash_length,
2231 const uint8_t *signature,
2232 size_t signature_length )
2233{
2234 psa_status_t status;
2235 int ret;
2236 mbedtls_md_type_t md_alg;
2237
2238 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2239 if( status != PSA_SUCCESS )
2240 return( status );
2241
Gilles Peskine630a18a2018-06-29 17:49:35 +02002242 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002243 return( PSA_ERROR_BUFFER_TOO_SMALL );
2244
2245#if defined(MBEDTLS_PKCS1_V15)
2246 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2247 {
2248 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2249 MBEDTLS_MD_NONE );
2250 ret = mbedtls_rsa_pkcs1_verify( rsa,
2251 mbedtls_ctr_drbg_random,
2252 &global_data.ctr_drbg,
2253 MBEDTLS_RSA_PUBLIC,
2254 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002255 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002256 hash,
2257 signature );
2258 }
2259 else
2260#endif /* MBEDTLS_PKCS1_V15 */
2261#if defined(MBEDTLS_PKCS1_V21)
2262 if( PSA_ALG_IS_RSA_PSS( alg ) )
2263 {
2264 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2265 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2266 mbedtls_ctr_drbg_random,
2267 &global_data.ctr_drbg,
2268 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002269 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002270 (unsigned int) hash_length,
2271 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002272 signature );
2273 }
2274 else
2275#endif /* MBEDTLS_PKCS1_V21 */
2276 {
2277 return( PSA_ERROR_INVALID_ARGUMENT );
2278 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002279
2280 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2281 * the rest of the signature is invalid". This has little use in
2282 * practice and PSA doesn't report this distinction. */
2283 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2284 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002285 return( mbedtls_to_psa_error( ret ) );
2286}
2287#endif /* MBEDTLS_RSA_C */
2288
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002289#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002290/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2291 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2292 * (even though these functions don't modify it). */
2293static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2294 psa_algorithm_t alg,
2295 const uint8_t *hash,
2296 size_t hash_length,
2297 uint8_t *signature,
2298 size_t signature_size,
2299 size_t *signature_length )
2300{
2301 int ret;
2302 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002303 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002304 mbedtls_mpi_init( &r );
2305 mbedtls_mpi_init( &s );
2306
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002307 if( signature_size < 2 * curve_bytes )
2308 {
2309 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2310 goto cleanup;
2311 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002312
Gilles Peskinea05219c2018-11-16 16:02:56 +01002313#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002314 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2315 {
2316 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2317 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2318 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2319 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2320 hash, hash_length,
2321 md_alg ) );
2322 }
2323 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002324#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002325 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002326 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002327 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2328 hash, hash_length,
2329 mbedtls_ctr_drbg_random,
2330 &global_data.ctr_drbg ) );
2331 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002332
2333 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2334 signature,
2335 curve_bytes ) );
2336 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2337 signature + curve_bytes,
2338 curve_bytes ) );
2339
2340cleanup:
2341 mbedtls_mpi_free( &r );
2342 mbedtls_mpi_free( &s );
2343 if( ret == 0 )
2344 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002345 return( mbedtls_to_psa_error( ret ) );
2346}
2347
2348static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2349 const uint8_t *hash,
2350 size_t hash_length,
2351 const uint8_t *signature,
2352 size_t signature_length )
2353{
2354 int ret;
2355 mbedtls_mpi r, s;
2356 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2357 mbedtls_mpi_init( &r );
2358 mbedtls_mpi_init( &s );
2359
2360 if( signature_length != 2 * curve_bytes )
2361 return( PSA_ERROR_INVALID_SIGNATURE );
2362
2363 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2364 signature,
2365 curve_bytes ) );
2366 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2367 signature + curve_bytes,
2368 curve_bytes ) );
2369
2370 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2371 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002372
2373cleanup:
2374 mbedtls_mpi_free( &r );
2375 mbedtls_mpi_free( &s );
2376 return( mbedtls_to_psa_error( ret ) );
2377}
2378#endif /* MBEDTLS_ECDSA_C */
2379
Gilles Peskinec5487a82018-12-03 18:08:14 +01002380psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002381 psa_algorithm_t alg,
2382 const uint8_t *hash,
2383 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002384 uint8_t *signature,
2385 size_t signature_size,
2386 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002387{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002388 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002389 psa_status_t status;
2390
2391 *signature_length = signature_size;
2392
Gilles Peskinec5487a82018-12-03 18:08:14 +01002393 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002394 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002395 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002396 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002397 {
2398 status = PSA_ERROR_INVALID_ARGUMENT;
2399 goto exit;
2400 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002401
Gilles Peskine20035e32018-02-03 22:44:14 +01002402#if defined(MBEDTLS_RSA_C)
2403 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2404 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002405 status = psa_rsa_sign( slot->data.rsa,
2406 alg,
2407 hash, hash_length,
2408 signature, signature_size,
2409 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002410 }
2411 else
2412#endif /* defined(MBEDTLS_RSA_C) */
2413#if defined(MBEDTLS_ECP_C)
2414 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2415 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002416#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002417 if(
2418#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2419 PSA_ALG_IS_ECDSA( alg )
2420#else
2421 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2422#endif
2423 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002424 status = psa_ecdsa_sign( slot->data.ecp,
2425 alg,
2426 hash, hash_length,
2427 signature, signature_size,
2428 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002429 else
2430#endif /* defined(MBEDTLS_ECDSA_C) */
2431 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002432 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002433 }
itayzafrir5c753392018-05-08 11:18:38 +03002434 }
2435 else
2436#endif /* defined(MBEDTLS_ECP_C) */
2437 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002438 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002439 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002440
2441exit:
2442 /* Fill the unused part of the output buffer (the whole buffer on error,
2443 * the trailing part on success) with something that isn't a valid mac
2444 * (barring an attack on the mac and deliberately-crafted input),
2445 * in case the caller doesn't check the return status properly. */
2446 if( status == PSA_SUCCESS )
2447 memset( signature + *signature_length, '!',
2448 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002449 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002450 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002451 /* If signature_size is 0 then we have nothing to do. We must not call
2452 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002453 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002454}
2455
Gilles Peskinec5487a82018-12-03 18:08:14 +01002456psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002457 psa_algorithm_t alg,
2458 const uint8_t *hash,
2459 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002460 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002461 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002462{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002463 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002464 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002465
Gilles Peskinec5487a82018-12-03 18:08:14 +01002466 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002467 if( status != PSA_SUCCESS )
2468 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002469
Gilles Peskine61b91d42018-06-08 16:09:36 +02002470#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002471 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002472 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002473 return( psa_rsa_verify( slot->data.rsa,
2474 alg,
2475 hash, hash_length,
2476 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002477 }
2478 else
2479#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002480#if defined(MBEDTLS_ECP_C)
2481 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2482 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002483#if defined(MBEDTLS_ECDSA_C)
2484 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002485 return( psa_ecdsa_verify( slot->data.ecp,
2486 hash, hash_length,
2487 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002488 else
2489#endif /* defined(MBEDTLS_ECDSA_C) */
2490 {
2491 return( PSA_ERROR_INVALID_ARGUMENT );
2492 }
itayzafrir5c753392018-05-08 11:18:38 +03002493 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002494 else
2495#endif /* defined(MBEDTLS_ECP_C) */
2496 {
2497 return( PSA_ERROR_NOT_SUPPORTED );
2498 }
2499}
2500
Gilles Peskine072ac562018-06-30 00:21:29 +02002501#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2502static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2503 mbedtls_rsa_context *rsa )
2504{
2505 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2506 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2507 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2508 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2509}
2510#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2511
Gilles Peskinec5487a82018-12-03 18:08:14 +01002512psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002513 psa_algorithm_t alg,
2514 const uint8_t *input,
2515 size_t input_length,
2516 const uint8_t *salt,
2517 size_t salt_length,
2518 uint8_t *output,
2519 size_t output_size,
2520 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002521{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002522 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002523 psa_status_t status;
2524
Darryl Green5cc689a2018-07-24 15:34:10 +01002525 (void) input;
2526 (void) input_length;
2527 (void) salt;
2528 (void) output;
2529 (void) output_size;
2530
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002531 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002532
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002533 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2534 return( PSA_ERROR_INVALID_ARGUMENT );
2535
Gilles Peskinec5487a82018-12-03 18:08:14 +01002536 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002537 if( status != PSA_SUCCESS )
2538 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002539 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2540 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002541 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002542
2543#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002544 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002545 {
2546 mbedtls_rsa_context *rsa = slot->data.rsa;
2547 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002548 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002549 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002550#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002551 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002552 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002553 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2554 mbedtls_ctr_drbg_random,
2555 &global_data.ctr_drbg,
2556 MBEDTLS_RSA_PUBLIC,
2557 input_length,
2558 input,
2559 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002560 }
2561 else
2562#endif /* MBEDTLS_PKCS1_V15 */
2563#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002564 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002565 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002566 psa_rsa_oaep_set_padding_mode( alg, rsa );
2567 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2568 mbedtls_ctr_drbg_random,
2569 &global_data.ctr_drbg,
2570 MBEDTLS_RSA_PUBLIC,
2571 salt, salt_length,
2572 input_length,
2573 input,
2574 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002575 }
2576 else
2577#endif /* MBEDTLS_PKCS1_V21 */
2578 {
2579 return( PSA_ERROR_INVALID_ARGUMENT );
2580 }
2581 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002582 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002583 return( mbedtls_to_psa_error( ret ) );
2584 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002585 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002586#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002587 {
2588 return( PSA_ERROR_NOT_SUPPORTED );
2589 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002590}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002591
Gilles Peskinec5487a82018-12-03 18:08:14 +01002592psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002593 psa_algorithm_t alg,
2594 const uint8_t *input,
2595 size_t input_length,
2596 const uint8_t *salt,
2597 size_t salt_length,
2598 uint8_t *output,
2599 size_t output_size,
2600 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002601{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002602 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002603 psa_status_t status;
2604
Darryl Green5cc689a2018-07-24 15:34:10 +01002605 (void) input;
2606 (void) input_length;
2607 (void) salt;
2608 (void) output;
2609 (void) output_size;
2610
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002611 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002612
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002613 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2614 return( PSA_ERROR_INVALID_ARGUMENT );
2615
Gilles Peskinec5487a82018-12-03 18:08:14 +01002616 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002617 if( status != PSA_SUCCESS )
2618 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002619 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2620 return( PSA_ERROR_INVALID_ARGUMENT );
2621
2622#if defined(MBEDTLS_RSA_C)
2623 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2624 {
2625 mbedtls_rsa_context *rsa = slot->data.rsa;
2626 int ret;
2627
Gilles Peskine630a18a2018-06-29 17:49:35 +02002628 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002629 return( PSA_ERROR_INVALID_ARGUMENT );
2630
2631#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002632 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002633 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002634 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2635 mbedtls_ctr_drbg_random,
2636 &global_data.ctr_drbg,
2637 MBEDTLS_RSA_PRIVATE,
2638 output_length,
2639 input,
2640 output,
2641 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002642 }
2643 else
2644#endif /* MBEDTLS_PKCS1_V15 */
2645#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002646 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002647 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002648 psa_rsa_oaep_set_padding_mode( alg, rsa );
2649 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2650 mbedtls_ctr_drbg_random,
2651 &global_data.ctr_drbg,
2652 MBEDTLS_RSA_PRIVATE,
2653 salt, salt_length,
2654 output_length,
2655 input,
2656 output,
2657 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002658 }
2659 else
2660#endif /* MBEDTLS_PKCS1_V21 */
2661 {
2662 return( PSA_ERROR_INVALID_ARGUMENT );
2663 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002664
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002665 return( mbedtls_to_psa_error( ret ) );
2666 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002667 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002668#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002669 {
2670 return( PSA_ERROR_NOT_SUPPORTED );
2671 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002672}
Gilles Peskine20035e32018-02-03 22:44:14 +01002673
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002674
2675
mohammad1603503973b2018-03-12 15:59:30 +02002676/****************************************************************/
2677/* Symmetric cryptography */
2678/****************************************************************/
2679
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002680/* Initialize the cipher operation structure. Once this function has been
2681 * called, psa_cipher_abort can run and will do the right thing. */
2682static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2683 psa_algorithm_t alg )
2684{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002685 if( ! PSA_ALG_IS_CIPHER( alg ) )
2686 {
2687 memset( operation, 0, sizeof( *operation ) );
2688 return( PSA_ERROR_INVALID_ARGUMENT );
2689 }
2690
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002691 operation->alg = alg;
2692 operation->key_set = 0;
2693 operation->iv_set = 0;
2694 operation->iv_required = 1;
2695 operation->iv_size = 0;
2696 operation->block_size = 0;
2697 mbedtls_cipher_init( &operation->ctx.cipher );
2698 return( PSA_SUCCESS );
2699}
2700
Gilles Peskinee553c652018-06-04 16:22:46 +02002701static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002702 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002703 psa_algorithm_t alg,
2704 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002705{
2706 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2707 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002708 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002709 size_t key_bits;
2710 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002711 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2712 PSA_KEY_USAGE_ENCRYPT :
2713 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002714
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002715 status = psa_cipher_init( operation, alg );
2716 if( status != PSA_SUCCESS )
2717 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002718
Gilles Peskinec5487a82018-12-03 18:08:14 +01002719 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002720 if( status != PSA_SUCCESS )
2721 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002722 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002723
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002724 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002725 if( cipher_info == NULL )
2726 return( PSA_ERROR_NOT_SUPPORTED );
2727
mohammad1603503973b2018-03-12 15:59:30 +02002728 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002729 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002730 {
2731 psa_cipher_abort( operation );
2732 return( mbedtls_to_psa_error( ret ) );
2733 }
2734
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002735#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002736 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002737 {
2738 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2739 unsigned char keys[24];
2740 memcpy( keys, slot->data.raw.data, 16 );
2741 memcpy( keys + 16, slot->data.raw.data, 8 );
2742 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2743 keys,
2744 192, cipher_operation );
2745 }
2746 else
2747#endif
2748 {
2749 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2750 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002751 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002752 }
Moran Peker41deec42018-04-04 15:43:05 +03002753 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002754 {
2755 psa_cipher_abort( operation );
2756 return( mbedtls_to_psa_error( ret ) );
2757 }
2758
mohammad16038481e742018-03-18 13:57:31 +02002759#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002760 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002761 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002762 case PSA_ALG_CBC_NO_PADDING:
2763 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2764 MBEDTLS_PADDING_NONE );
2765 break;
2766 case PSA_ALG_CBC_PKCS7:
2767 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2768 MBEDTLS_PADDING_PKCS7 );
2769 break;
2770 default:
2771 /* The algorithm doesn't involve padding. */
2772 ret = 0;
2773 break;
2774 }
2775 if( ret != 0 )
2776 {
2777 psa_cipher_abort( operation );
2778 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002779 }
2780#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2781
mohammad1603503973b2018-03-12 15:59:30 +02002782 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002783 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2784 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2785 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002786 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002787 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002788 }
mohammad1603503973b2018-03-12 15:59:30 +02002789
Moran Peker395db872018-05-31 14:07:14 +03002790 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002791}
2792
Gilles Peskinefe119512018-07-08 21:39:34 +02002793psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002794 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002795 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002796{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002797 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002798}
2799
Gilles Peskinefe119512018-07-08 21:39:34 +02002800psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002801 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002802 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002803{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002804 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002805}
2806
Gilles Peskinefe119512018-07-08 21:39:34 +02002807psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2808 unsigned char *iv,
2809 size_t iv_size,
2810 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002811{
itayzafrir534bd7c2018-08-02 13:56:32 +03002812 psa_status_t status;
2813 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002814 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002815 {
2816 status = PSA_ERROR_BAD_STATE;
2817 goto exit;
2818 }
Moran Peker41deec42018-04-04 15:43:05 +03002819 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002820 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002821 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002822 goto exit;
2823 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002824 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2825 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002826 if( ret != 0 )
2827 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002828 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002829 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002830 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002831
mohammad16038481e742018-03-18 13:57:31 +02002832 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002833 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002834
Moran Peker395db872018-05-31 14:07:14 +03002835exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002836 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002837 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002838 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002839}
2840
Gilles Peskinefe119512018-07-08 21:39:34 +02002841psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2842 const unsigned char *iv,
2843 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002844{
itayzafrir534bd7c2018-08-02 13:56:32 +03002845 psa_status_t status;
2846 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002847 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002848 {
2849 status = PSA_ERROR_BAD_STATE;
2850 goto exit;
2851 }
Moran Pekera28258c2018-05-29 16:25:04 +03002852 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002853 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002854 status = PSA_ERROR_INVALID_ARGUMENT;
2855 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002856 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002857 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2858 status = mbedtls_to_psa_error( ret );
2859exit:
2860 if( status == PSA_SUCCESS )
2861 operation->iv_set = 1;
2862 else
mohammad1603503973b2018-03-12 15:59:30 +02002863 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002864 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002865}
2866
Gilles Peskinee553c652018-06-04 16:22:46 +02002867psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2868 const uint8_t *input,
2869 size_t input_length,
2870 unsigned char *output,
2871 size_t output_size,
2872 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002873{
itayzafrir534bd7c2018-08-02 13:56:32 +03002874 psa_status_t status;
2875 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002876 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002877 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002878 {
2879 /* Take the unprocessed partial block left over from previous
2880 * update calls, if any, plus the input to this call. Remove
2881 * the last partial block, if any. You get the data that will be
2882 * output in this call. */
2883 expected_output_size =
2884 ( operation->ctx.cipher.unprocessed_len + input_length )
2885 / operation->block_size * operation->block_size;
2886 }
2887 else
2888 {
2889 expected_output_size = input_length;
2890 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002891
Gilles Peskine89d789c2018-06-04 17:17:16 +02002892 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002893 {
2894 status = PSA_ERROR_BUFFER_TOO_SMALL;
2895 goto exit;
2896 }
mohammad160382759612018-03-12 18:16:40 +02002897
mohammad1603503973b2018-03-12 15:59:30 +02002898 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002899 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002900 status = mbedtls_to_psa_error( ret );
2901exit:
2902 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002903 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002904 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002905}
2906
Gilles Peskinee553c652018-06-04 16:22:46 +02002907psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2908 uint8_t *output,
2909 size_t output_size,
2910 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002911{
Janos Follath315b51c2018-07-09 16:04:51 +01002912 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2913 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002914 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002915
mohammad1603503973b2018-03-12 15:59:30 +02002916 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002917 {
Janos Follath315b51c2018-07-09 16:04:51 +01002918 status = PSA_ERROR_BAD_STATE;
2919 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002920 }
2921 if( operation->iv_required && ! operation->iv_set )
2922 {
Janos Follath315b51c2018-07-09 16:04:51 +01002923 status = PSA_ERROR_BAD_STATE;
2924 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002925 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002926
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002927 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002928 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2929 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002930 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002931 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002932 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002933 }
2934
Janos Follath315b51c2018-07-09 16:04:51 +01002935 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2936 temp_output_buffer,
2937 output_length );
2938 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002939 {
Janos Follath315b51c2018-07-09 16:04:51 +01002940 status = mbedtls_to_psa_error( cipher_ret );
2941 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002942 }
Janos Follath315b51c2018-07-09 16:04:51 +01002943
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002944 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002945 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002946 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002947 memcpy( output, temp_output_buffer, *output_length );
2948 else
2949 {
Janos Follath315b51c2018-07-09 16:04:51 +01002950 status = PSA_ERROR_BUFFER_TOO_SMALL;
2951 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002952 }
mohammad1603503973b2018-03-12 15:59:30 +02002953
Gilles Peskine3f108122018-12-07 18:14:53 +01002954 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002955 status = psa_cipher_abort( operation );
2956
2957 return( status );
2958
2959error:
2960
2961 *output_length = 0;
2962
Gilles Peskine3f108122018-12-07 18:14:53 +01002963 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002964 (void) psa_cipher_abort( operation );
2965
2966 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002967}
2968
Gilles Peskinee553c652018-06-04 16:22:46 +02002969psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2970{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002971 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002972 {
2973 /* The object has (apparently) been initialized but it is not
2974 * in use. It's ok to call abort on such an object, and there's
2975 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002976 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002977 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002978
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002979 /* Sanity check (shouldn't happen: operation->alg should
2980 * always have been initialized to a valid value). */
2981 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2982 return( PSA_ERROR_BAD_STATE );
2983
mohammad1603503973b2018-03-12 15:59:30 +02002984 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002985
Moran Peker41deec42018-04-04 15:43:05 +03002986 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002987 operation->key_set = 0;
2988 operation->iv_set = 0;
2989 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002990 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002991 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002992
Moran Peker395db872018-05-31 14:07:14 +03002993 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002994}
2995
Gilles Peskinea0655c32018-04-30 17:06:50 +02002996
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002997
mohammad16038cc1cee2018-03-28 01:21:33 +03002998/****************************************************************/
2999/* Key Policy */
3000/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003001
mohammad160327010052018-07-03 13:16:15 +03003002#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003003void psa_key_policy_set_usage( psa_key_policy_t *policy,
3004 psa_key_usage_t usage,
3005 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003006{
mohammad16034eed7572018-03-28 05:14:59 -07003007 policy->usage = usage;
3008 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003009}
3010
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003011psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003012{
mohammad16036df908f2018-04-02 08:34:15 -07003013 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003014}
3015
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003016psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003017{
mohammad16036df908f2018-04-02 08:34:15 -07003018 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003019}
mohammad160327010052018-07-03 13:16:15 +03003020#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003021
Gilles Peskinec5487a82018-12-03 18:08:14 +01003022psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003023 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003024{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003025 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003026 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003027
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003028 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003029 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003030
Gilles Peskinec5487a82018-12-03 18:08:14 +01003031 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003032 if( status != PSA_SUCCESS )
3033 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003034
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003035 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3036 PSA_KEY_USAGE_ENCRYPT |
3037 PSA_KEY_USAGE_DECRYPT |
3038 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003039 PSA_KEY_USAGE_VERIFY |
3040 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003041 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003042
mohammad16036df908f2018-04-02 08:34:15 -07003043 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003044
3045 return( PSA_SUCCESS );
3046}
3047
Gilles Peskinec5487a82018-12-03 18:08:14 +01003048psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003049 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003050{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003051 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003052 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003053
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003054 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003055 return( PSA_ERROR_INVALID_ARGUMENT );
3056
Gilles Peskinec5487a82018-12-03 18:08:14 +01003057 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003058 if( status != PSA_SUCCESS )
3059 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003060
mohammad16036df908f2018-04-02 08:34:15 -07003061 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003062
3063 return( PSA_SUCCESS );
3064}
Gilles Peskine20035e32018-02-03 22:44:14 +01003065
Gilles Peskinea0655c32018-04-30 17:06:50 +02003066
3067
mohammad1603804cd712018-03-20 22:44:08 +02003068/****************************************************************/
3069/* Key Lifetime */
3070/****************************************************************/
3071
Gilles Peskinec5487a82018-12-03 18:08:14 +01003072psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003073 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003074{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003075 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003076 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003077
Gilles Peskinec5487a82018-12-03 18:08:14 +01003078 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003079 if( status != PSA_SUCCESS )
3080 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003081
mohammad1603804cd712018-03-20 22:44:08 +02003082 *lifetime = slot->lifetime;
3083
3084 return( PSA_SUCCESS );
3085}
3086
Gilles Peskine20035e32018-02-03 22:44:14 +01003087
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003088
mohammad16035955c982018-04-26 00:53:03 +03003089/****************************************************************/
3090/* AEAD */
3091/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003092
Gilles Peskineedf9a652018-08-17 18:11:56 +02003093typedef struct
3094{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003095 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003096 const mbedtls_cipher_info_t *cipher_info;
3097 union
3098 {
3099#if defined(MBEDTLS_CCM_C)
3100 mbedtls_ccm_context ccm;
3101#endif /* MBEDTLS_CCM_C */
3102#if defined(MBEDTLS_GCM_C)
3103 mbedtls_gcm_context gcm;
3104#endif /* MBEDTLS_GCM_C */
3105 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003106 psa_algorithm_t core_alg;
3107 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003108 uint8_t tag_length;
3109} aead_operation_t;
3110
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003111static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003112{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003113 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003114 {
3115#if defined(MBEDTLS_CCM_C)
3116 case PSA_ALG_CCM:
3117 mbedtls_ccm_free( &operation->ctx.ccm );
3118 break;
3119#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003120#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003121 case PSA_ALG_GCM:
3122 mbedtls_gcm_free( &operation->ctx.gcm );
3123 break;
3124#endif /* MBEDTLS_GCM_C */
3125 }
3126}
3127
3128static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003129 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003130 psa_key_usage_t usage,
3131 psa_algorithm_t alg )
3132{
3133 psa_status_t status;
3134 size_t key_bits;
3135 mbedtls_cipher_id_t cipher_id;
3136
Gilles Peskinec5487a82018-12-03 18:08:14 +01003137 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003138 if( status != PSA_SUCCESS )
3139 return( status );
3140
3141 key_bits = psa_get_key_bits( operation->slot );
3142
3143 operation->cipher_info =
3144 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3145 &cipher_id );
3146 if( operation->cipher_info == NULL )
3147 return( PSA_ERROR_NOT_SUPPORTED );
3148
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003149 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003150 {
3151#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003152 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3153 operation->core_alg = PSA_ALG_CCM;
3154 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003155 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3156 return( PSA_ERROR_INVALID_ARGUMENT );
3157 mbedtls_ccm_init( &operation->ctx.ccm );
3158 status = mbedtls_to_psa_error(
3159 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3160 operation->slot->data.raw.data,
3161 (unsigned int) key_bits ) );
3162 if( status != 0 )
3163 goto cleanup;
3164 break;
3165#endif /* MBEDTLS_CCM_C */
3166
3167#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003168 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3169 operation->core_alg = PSA_ALG_GCM;
3170 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003171 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3172 return( PSA_ERROR_INVALID_ARGUMENT );
3173 mbedtls_gcm_init( &operation->ctx.gcm );
3174 status = mbedtls_to_psa_error(
3175 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3176 operation->slot->data.raw.data,
3177 (unsigned int) key_bits ) );
3178 break;
3179#endif /* MBEDTLS_GCM_C */
3180
3181 default:
3182 return( PSA_ERROR_NOT_SUPPORTED );
3183 }
3184
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003185 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3186 {
3187 status = PSA_ERROR_INVALID_ARGUMENT;
3188 goto cleanup;
3189 }
3190 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3191 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3192 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3193 * In both cases, mbedtls_xxx will validate the tag length below. */
3194
Gilles Peskineedf9a652018-08-17 18:11:56 +02003195 return( PSA_SUCCESS );
3196
3197cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003198 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003199 return( status );
3200}
3201
Gilles Peskinec5487a82018-12-03 18:08:14 +01003202psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003203 psa_algorithm_t alg,
3204 const uint8_t *nonce,
3205 size_t nonce_length,
3206 const uint8_t *additional_data,
3207 size_t additional_data_length,
3208 const uint8_t *plaintext,
3209 size_t plaintext_length,
3210 uint8_t *ciphertext,
3211 size_t ciphertext_size,
3212 size_t *ciphertext_length )
3213{
mohammad16035955c982018-04-26 00:53:03 +03003214 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003215 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003216 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003217
mohammad1603f08a5502018-06-03 15:05:47 +03003218 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003219
Gilles Peskinec5487a82018-12-03 18:08:14 +01003220 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003221 if( status != PSA_SUCCESS )
3222 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003223
Gilles Peskineedf9a652018-08-17 18:11:56 +02003224 /* For all currently supported modes, the tag is at the end of the
3225 * ciphertext. */
3226 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3227 {
3228 status = PSA_ERROR_BUFFER_TOO_SMALL;
3229 goto exit;
3230 }
3231 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003232
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003233#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003234 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003235 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003236 status = mbedtls_to_psa_error(
3237 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3238 MBEDTLS_GCM_ENCRYPT,
3239 plaintext_length,
3240 nonce, nonce_length,
3241 additional_data, additional_data_length,
3242 plaintext, ciphertext,
3243 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003244 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003245 else
3246#endif /* MBEDTLS_GCM_C */
3247#if defined(MBEDTLS_CCM_C)
3248 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003249 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003250 status = mbedtls_to_psa_error(
3251 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3252 plaintext_length,
3253 nonce, nonce_length,
3254 additional_data,
3255 additional_data_length,
3256 plaintext, ciphertext,
3257 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003258 }
mohammad16035c8845f2018-05-09 05:40:09 -07003259 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003260#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003261 {
mohammad1603554faad2018-06-03 15:07:38 +03003262 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003263 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003264
Gilles Peskineedf9a652018-08-17 18:11:56 +02003265 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3266 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003267
Gilles Peskineedf9a652018-08-17 18:11:56 +02003268exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003269 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003270 if( status == PSA_SUCCESS )
3271 *ciphertext_length = plaintext_length + operation.tag_length;
3272 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003273}
3274
Gilles Peskineee652a32018-06-01 19:23:52 +02003275/* Locate the tag in a ciphertext buffer containing the encrypted data
3276 * followed by the tag. Return the length of the part preceding the tag in
3277 * *plaintext_length. This is the size of the plaintext in modes where
3278 * the encrypted data has the same size as the plaintext, such as
3279 * CCM and GCM. */
3280static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3281 const uint8_t *ciphertext,
3282 size_t ciphertext_length,
3283 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003284 const uint8_t **p_tag )
3285{
3286 size_t payload_length;
3287 if( tag_length > ciphertext_length )
3288 return( PSA_ERROR_INVALID_ARGUMENT );
3289 payload_length = ciphertext_length - tag_length;
3290 if( payload_length > plaintext_size )
3291 return( PSA_ERROR_BUFFER_TOO_SMALL );
3292 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003293 return( PSA_SUCCESS );
3294}
3295
Gilles Peskinec5487a82018-12-03 18:08:14 +01003296psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003297 psa_algorithm_t alg,
3298 const uint8_t *nonce,
3299 size_t nonce_length,
3300 const uint8_t *additional_data,
3301 size_t additional_data_length,
3302 const uint8_t *ciphertext,
3303 size_t ciphertext_length,
3304 uint8_t *plaintext,
3305 size_t plaintext_size,
3306 size_t *plaintext_length )
3307{
mohammad16035955c982018-04-26 00:53:03 +03003308 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003309 aead_operation_t operation;
3310 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003311
Gilles Peskineee652a32018-06-01 19:23:52 +02003312 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003313
Gilles Peskinec5487a82018-12-03 18:08:14 +01003314 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003315 if( status != PSA_SUCCESS )
3316 return( status );
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 = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003322 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003323 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003324 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003325 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003326
Gilles Peskineedf9a652018-08-17 18:11:56 +02003327 status = mbedtls_to_psa_error(
3328 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3329 ciphertext_length - operation.tag_length,
3330 nonce, nonce_length,
3331 additional_data,
3332 additional_data_length,
3333 tag, operation.tag_length,
3334 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003335 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003336 else
3337#endif /* MBEDTLS_GCM_C */
3338#if defined(MBEDTLS_CCM_C)
3339 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003340 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003341 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003342 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003343 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003344 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003345 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003346
Gilles Peskineedf9a652018-08-17 18:11:56 +02003347 status = mbedtls_to_psa_error(
3348 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3349 ciphertext_length - operation.tag_length,
3350 nonce, nonce_length,
3351 additional_data,
3352 additional_data_length,
3353 ciphertext, plaintext,
3354 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003355 }
mohammad160339574652018-06-01 04:39:53 -07003356 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003357#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003358 {
mohammad1603554faad2018-06-03 15:07:38 +03003359 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003360 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003361
Gilles Peskineedf9a652018-08-17 18:11:56 +02003362 if( status != PSA_SUCCESS && plaintext_size != 0 )
3363 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003364
Gilles Peskineedf9a652018-08-17 18:11:56 +02003365exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003366 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003367 if( status == PSA_SUCCESS )
3368 *plaintext_length = ciphertext_length - operation.tag_length;
3369 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003370}
3371
Gilles Peskinea0655c32018-04-30 17:06:50 +02003372
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003373
Gilles Peskine20035e32018-02-03 22:44:14 +01003374/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003375/* Generators */
3376/****************************************************************/
3377
3378psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3379{
3380 psa_status_t status = PSA_SUCCESS;
3381 if( generator->alg == 0 )
3382 {
3383 /* The object has (apparently) been initialized but it is not
3384 * in use. It's ok to call abort on such an object, and there's
3385 * nothing to do. */
3386 }
3387 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003388 if( generator->alg == PSA_ALG_SELECT_RAW )
3389 {
3390 if( generator->ctx.buffer.data != NULL )
3391 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003392 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003393 generator->ctx.buffer.size );
3394 mbedtls_free( generator->ctx.buffer.data );
3395 }
3396 }
3397 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003398#if defined(MBEDTLS_MD_C)
3399 if( PSA_ALG_IS_HKDF( generator->alg ) )
3400 {
3401 mbedtls_free( generator->ctx.hkdf.info );
3402 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3403 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003404 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3405 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3406 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003407 {
3408 if( generator->ctx.tls12_prf.key != NULL )
3409 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003410 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003411 generator->ctx.tls12_prf.key_len );
3412 mbedtls_free( generator->ctx.tls12_prf.key );
3413 }
Hanno Becker580fba12018-11-13 20:50:45 +00003414
3415 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3416 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003417 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003418 generator->ctx.tls12_prf.Ai_with_seed_len );
3419 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3420 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003421 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003422 else
3423#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003424 {
3425 status = PSA_ERROR_BAD_STATE;
3426 }
3427 memset( generator, 0, sizeof( *generator ) );
3428 return( status );
3429}
3430
3431
3432psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3433 size_t *capacity)
3434{
3435 *capacity = generator->capacity;
3436 return( PSA_SUCCESS );
3437}
3438
Gilles Peskinebef7f142018-07-12 17:22:21 +02003439#if defined(MBEDTLS_MD_C)
3440/* Read some bytes from an HKDF-based generator. This performs a chunk
3441 * of the expand phase of the HKDF algorithm. */
3442static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3443 psa_algorithm_t hash_alg,
3444 uint8_t *output,
3445 size_t output_length )
3446{
3447 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3448 psa_status_t status;
3449
3450 while( output_length != 0 )
3451 {
3452 /* Copy what remains of the current block */
3453 uint8_t n = hash_length - hkdf->offset_in_block;
3454 if( n > output_length )
3455 n = (uint8_t) output_length;
3456 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3457 output += n;
3458 output_length -= n;
3459 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003460 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003461 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003462 /* We can't be wanting more output after block 0xff, otherwise
3463 * the capacity check in psa_generator_read() would have
3464 * prevented this call. It could happen only if the generator
3465 * object was corrupted or if this function is called directly
3466 * inside the library. */
3467 if( hkdf->block_number == 0xff )
3468 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003469
3470 /* We need a new block */
3471 ++hkdf->block_number;
3472 hkdf->offset_in_block = 0;
3473 status = psa_hmac_setup_internal( &hkdf->hmac,
3474 hkdf->prk, hash_length,
3475 hash_alg );
3476 if( status != PSA_SUCCESS )
3477 return( status );
3478 if( hkdf->block_number != 1 )
3479 {
3480 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3481 hkdf->output_block,
3482 hash_length );
3483 if( status != PSA_SUCCESS )
3484 return( status );
3485 }
3486 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3487 hkdf->info,
3488 hkdf->info_length );
3489 if( status != PSA_SUCCESS )
3490 return( status );
3491 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3492 &hkdf->block_number, 1 );
3493 if( status != PSA_SUCCESS )
3494 return( status );
3495 status = psa_hmac_finish_internal( &hkdf->hmac,
3496 hkdf->output_block,
3497 sizeof( hkdf->output_block ) );
3498 if( status != PSA_SUCCESS )
3499 return( status );
3500 }
3501
3502 return( PSA_SUCCESS );
3503}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003504
3505static psa_status_t psa_generator_tls12_prf_generate_next_block(
3506 psa_tls12_prf_generator_t *tls12_prf,
3507 psa_algorithm_t alg )
3508{
3509 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3510 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3511 psa_hmac_internal_data hmac;
3512 psa_status_t status, cleanup_status;
3513
Hanno Becker3b339e22018-11-13 20:56:14 +00003514 unsigned char *Ai;
3515 size_t Ai_len;
3516
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003517 /* We can't be wanting more output after block 0xff, otherwise
3518 * the capacity check in psa_generator_read() would have
3519 * prevented this call. It could happen only if the generator
3520 * object was corrupted or if this function is called directly
3521 * inside the library. */
3522 if( tls12_prf->block_number == 0xff )
3523 return( PSA_ERROR_BAD_STATE );
3524
3525 /* We need a new block */
3526 ++tls12_prf->block_number;
3527 tls12_prf->offset_in_block = 0;
3528
3529 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3530 *
3531 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3532 *
3533 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3534 * HMAC_hash(secret, A(2) + seed) +
3535 * HMAC_hash(secret, A(3) + seed) + ...
3536 *
3537 * A(0) = seed
3538 * A(i) = HMAC_hash( secret, A(i-1) )
3539 *
3540 * The `psa_tls12_prf_generator` structures saves the block
3541 * `HMAC_hash(secret, A(i) + seed)` from which the output
3542 * is currently extracted as `output_block`, while
3543 * `A(i) + seed` is stored in `Ai_with_seed`.
3544 *
3545 * Generating a new block means recalculating `Ai_with_seed`
3546 * from the A(i)-part of it, and afterwards recalculating
3547 * `output_block`.
3548 *
3549 * A(0) is computed at setup time.
3550 *
3551 */
3552
3553 psa_hmac_init_internal( &hmac );
3554
3555 /* We must distinguish the calculation of A(1) from those
3556 * of A(2) and higher, because A(0)=seed has a different
3557 * length than the other A(i). */
3558 if( tls12_prf->block_number == 1 )
3559 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003560 Ai = tls12_prf->Ai_with_seed + hash_length;
3561 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003562 }
3563 else
3564 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003565 Ai = tls12_prf->Ai_with_seed;
3566 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003567 }
3568
Hanno Becker3b339e22018-11-13 20:56:14 +00003569 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3570 status = psa_hmac_setup_internal( &hmac,
3571 tls12_prf->key,
3572 tls12_prf->key_len,
3573 hash_alg );
3574 if( status != PSA_SUCCESS )
3575 goto cleanup;
3576
3577 status = psa_hash_update( &hmac.hash_ctx,
3578 Ai, Ai_len );
3579 if( status != PSA_SUCCESS )
3580 goto cleanup;
3581
3582 status = psa_hmac_finish_internal( &hmac,
3583 tls12_prf->Ai_with_seed,
3584 hash_length );
3585 if( status != PSA_SUCCESS )
3586 goto cleanup;
3587
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003588 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3589 status = psa_hmac_setup_internal( &hmac,
3590 tls12_prf->key,
3591 tls12_prf->key_len,
3592 hash_alg );
3593 if( status != PSA_SUCCESS )
3594 goto cleanup;
3595
3596 status = psa_hash_update( &hmac.hash_ctx,
3597 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003598 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003599 if( status != PSA_SUCCESS )
3600 goto cleanup;
3601
3602 status = psa_hmac_finish_internal( &hmac,
3603 tls12_prf->output_block,
3604 hash_length );
3605 if( status != PSA_SUCCESS )
3606 goto cleanup;
3607
3608cleanup:
3609
3610 cleanup_status = psa_hmac_abort_internal( &hmac );
3611 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3612 status = cleanup_status;
3613
3614 return( status );
3615}
3616
3617/* Read some bytes from an TLS-1.2-PRF-based generator.
3618 * See Section 5 of RFC 5246. */
3619static psa_status_t psa_generator_tls12_prf_read(
3620 psa_tls12_prf_generator_t *tls12_prf,
3621 psa_algorithm_t alg,
3622 uint8_t *output,
3623 size_t output_length )
3624{
3625 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3626 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3627 psa_status_t status;
3628
3629 while( output_length != 0 )
3630 {
3631 /* Copy what remains of the current block */
3632 uint8_t n = hash_length - tls12_prf->offset_in_block;
3633
3634 /* Check if we have fully processed the current block. */
3635 if( n == 0 )
3636 {
3637 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3638 alg );
3639 if( status != PSA_SUCCESS )
3640 return( status );
3641
3642 continue;
3643 }
3644
3645 if( n > output_length )
3646 n = (uint8_t) output_length;
3647 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3648 n );
3649 output += n;
3650 output_length -= n;
3651 tls12_prf->offset_in_block += n;
3652 }
3653
3654 return( PSA_SUCCESS );
3655}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003656#endif /* MBEDTLS_MD_C */
3657
Gilles Peskineeab56e42018-07-12 17:12:33 +02003658psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3659 uint8_t *output,
3660 size_t output_length )
3661{
3662 psa_status_t status;
3663
3664 if( output_length > generator->capacity )
3665 {
3666 generator->capacity = 0;
3667 /* Go through the error path to wipe all confidential data now
3668 * that the generator object is useless. */
3669 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3670 goto exit;
3671 }
3672 if( output_length == 0 &&
3673 generator->capacity == 0 && generator->alg == 0 )
3674 {
3675 /* Edge case: this is a blank or finished generator, and 0
3676 * bytes were requested. The right error in this case could
3677 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3678 * INSUFFICIENT_CAPACITY, which is right for a finished
3679 * generator, for consistency with the case when
3680 * output_length > 0. */
3681 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3682 }
3683 generator->capacity -= output_length;
3684
Gilles Peskine751d9652018-09-18 12:05:44 +02003685 if( generator->alg == PSA_ALG_SELECT_RAW )
3686 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003687 /* Initially, the capacity of a selection generator is always
3688 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3689 * abbreviated in this comment as `size`. When the remaining
3690 * capacity is `c`, the next bytes to serve start `c` bytes
3691 * from the end of the buffer, i.e. `size - c` from the
3692 * beginning of the buffer. Since `generator->capacity` was just
3693 * decremented above, we need to serve the bytes from
3694 * `size - generator->capacity - output_length` to
3695 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003696 size_t offset =
3697 generator->ctx.buffer.size - generator->capacity - output_length;
3698 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3699 status = PSA_SUCCESS;
3700 }
3701 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003702#if defined(MBEDTLS_MD_C)
3703 if( PSA_ALG_IS_HKDF( generator->alg ) )
3704 {
3705 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3706 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3707 output, output_length );
3708 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003709 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3710 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003711 {
3712 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3713 generator->alg, output,
3714 output_length );
3715 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003716 else
3717#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003718 {
3719 return( PSA_ERROR_BAD_STATE );
3720 }
3721
3722exit:
3723 if( status != PSA_SUCCESS )
3724 {
3725 psa_generator_abort( generator );
3726 memset( output, '!', output_length );
3727 }
3728 return( status );
3729}
3730
Gilles Peskine08542d82018-07-19 17:05:42 +02003731#if defined(MBEDTLS_DES_C)
3732static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3733{
3734 if( data_size >= 8 )
3735 mbedtls_des_key_set_parity( data );
3736 if( data_size >= 16 )
3737 mbedtls_des_key_set_parity( data + 8 );
3738 if( data_size >= 24 )
3739 mbedtls_des_key_set_parity( data + 16 );
3740}
3741#endif /* MBEDTLS_DES_C */
3742
Gilles Peskinec5487a82018-12-03 18:08:14 +01003743psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003744 psa_key_type_t type,
3745 size_t bits,
3746 psa_crypto_generator_t *generator )
3747{
3748 uint8_t *data = NULL;
3749 size_t bytes = PSA_BITS_TO_BYTES( bits );
3750 psa_status_t status;
3751
3752 if( ! key_type_is_raw_bytes( type ) )
3753 return( PSA_ERROR_INVALID_ARGUMENT );
3754 if( bits % 8 != 0 )
3755 return( PSA_ERROR_INVALID_ARGUMENT );
3756 data = mbedtls_calloc( 1, bytes );
3757 if( data == NULL )
3758 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3759
3760 status = psa_generator_read( generator, data, bytes );
3761 if( status != PSA_SUCCESS )
3762 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003763#if defined(MBEDTLS_DES_C)
3764 if( type == PSA_KEY_TYPE_DES )
3765 psa_des_set_key_parity( data, bytes );
3766#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003767 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003768
3769exit:
3770 mbedtls_free( data );
3771 return( status );
3772}
3773
3774
3775
3776/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003777/* Key derivation */
3778/****************************************************************/
3779
Gilles Peskinea05219c2018-11-16 16:02:56 +01003780#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003781/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003782 * of the HKDF algorithm.
3783 *
3784 * Note that if this function fails, you must call psa_generator_abort()
3785 * to potentially free embedded data structures and wipe confidential data.
3786 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003787static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003788 const uint8_t *secret,
3789 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003790 psa_algorithm_t hash_alg,
3791 const uint8_t *salt,
3792 size_t salt_length,
3793 const uint8_t *label,
3794 size_t label_length )
3795{
3796 psa_status_t status;
3797 status = psa_hmac_setup_internal( &hkdf->hmac,
3798 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003799 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003800 if( status != PSA_SUCCESS )
3801 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003802 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003803 if( status != PSA_SUCCESS )
3804 return( status );
3805 status = psa_hmac_finish_internal( &hkdf->hmac,
3806 hkdf->prk,
3807 sizeof( hkdf->prk ) );
3808 if( status != PSA_SUCCESS )
3809 return( status );
3810 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3811 hkdf->block_number = 0;
3812 hkdf->info_length = label_length;
3813 if( label_length != 0 )
3814 {
3815 hkdf->info = mbedtls_calloc( 1, label_length );
3816 if( hkdf->info == NULL )
3817 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3818 memcpy( hkdf->info, label, label_length );
3819 }
3820 return( PSA_SUCCESS );
3821}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003822#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003823
Gilles Peskinea05219c2018-11-16 16:02:56 +01003824#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003825/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3826 *
3827 * Note that if this function fails, you must call psa_generator_abort()
3828 * to potentially free embedded data structures and wipe confidential data.
3829 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003830static psa_status_t psa_generator_tls12_prf_setup(
3831 psa_tls12_prf_generator_t *tls12_prf,
3832 const unsigned char *key,
3833 size_t key_len,
3834 psa_algorithm_t hash_alg,
3835 const uint8_t *salt,
3836 size_t salt_length,
3837 const uint8_t *label,
3838 size_t label_length )
3839{
3840 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003841 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3842 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003843
3844 tls12_prf->key = mbedtls_calloc( 1, key_len );
3845 if( tls12_prf->key == NULL )
3846 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3847 tls12_prf->key_len = key_len;
3848 memcpy( tls12_prf->key, key, key_len );
3849
Hanno Becker580fba12018-11-13 20:50:45 +00003850 overflow = ( salt_length + label_length < salt_length ) ||
3851 ( salt_length + label_length + hash_length < hash_length );
3852 if( overflow )
3853 return( PSA_ERROR_INVALID_ARGUMENT );
3854
3855 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3856 if( tls12_prf->Ai_with_seed == NULL )
3857 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3858 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3859
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003860 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3861 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003862 if( label_length != 0 )
3863 {
3864 memcpy( tls12_prf->Ai_with_seed + hash_length,
3865 label, label_length );
3866 }
3867
3868 if( salt_length != 0 )
3869 {
3870 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3871 salt, salt_length );
3872 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003873
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003874 /* The first block gets generated when
3875 * psa_generator_read() is called. */
3876 tls12_prf->block_number = 0;
3877 tls12_prf->offset_in_block = hash_length;
3878
3879 return( PSA_SUCCESS );
3880}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003881
3882/* Set up a TLS-1.2-PSK-to-MS-based generator. */
3883static psa_status_t psa_generator_tls12_psk_to_ms_setup(
3884 psa_tls12_prf_generator_t *tls12_prf,
3885 const unsigned char *psk,
3886 size_t psk_len,
3887 psa_algorithm_t hash_alg,
3888 const uint8_t *salt,
3889 size_t salt_length,
3890 const uint8_t *label,
3891 size_t label_length )
3892{
3893 psa_status_t status;
3894 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
3895
3896 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
3897 return( PSA_ERROR_INVALID_ARGUMENT );
3898
3899 /* Quoting RFC 4279, Section 2:
3900 *
3901 * The premaster secret is formed as follows: if the PSK is N octets
3902 * long, concatenate a uint16 with the value N, N zero octets, a second
3903 * uint16 with the value N, and the PSK itself.
3904 */
3905
3906 pms[0] = ( psk_len >> 8 ) & 0xff;
3907 pms[1] = ( psk_len >> 0 ) & 0xff;
3908 memset( pms + 2, 0, psk_len );
3909 pms[2 + psk_len + 0] = pms[0];
3910 pms[2 + psk_len + 1] = pms[1];
3911 memcpy( pms + 4 + psk_len, psk, psk_len );
3912
3913 status = psa_generator_tls12_prf_setup( tls12_prf,
3914 pms, 4 + 2 * psk_len,
3915 hash_alg,
3916 salt, salt_length,
3917 label, label_length );
3918
Gilles Peskine3f108122018-12-07 18:14:53 +01003919 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00003920 return( status );
3921}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003922#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003923
Gilles Peskine346797d2018-11-16 16:05:06 +01003924/* Note that if this function fails, you must call psa_generator_abort()
3925 * to potentially free embedded data structures and wipe confidential data.
3926 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003927static psa_status_t psa_key_derivation_internal(
3928 psa_crypto_generator_t *generator,
3929 const uint8_t *secret, size_t secret_length,
3930 psa_algorithm_t alg,
3931 const uint8_t *salt, size_t salt_length,
3932 const uint8_t *label, size_t label_length,
3933 size_t capacity )
3934{
3935 psa_status_t status;
3936 size_t max_capacity;
3937
3938 /* Set generator->alg even on failure so that abort knows what to do. */
3939 generator->alg = alg;
3940
Gilles Peskine751d9652018-09-18 12:05:44 +02003941 if( alg == PSA_ALG_SELECT_RAW )
3942 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003943 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02003944 if( salt_length != 0 )
3945 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01003946 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02003947 if( label_length != 0 )
3948 return( PSA_ERROR_INVALID_ARGUMENT );
3949 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
3950 if( generator->ctx.buffer.data == NULL )
3951 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3952 memcpy( generator->ctx.buffer.data, secret, secret_length );
3953 generator->ctx.buffer.size = secret_length;
3954 max_capacity = secret_length;
3955 status = PSA_SUCCESS;
3956 }
3957 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003958#if defined(MBEDTLS_MD_C)
3959 if( PSA_ALG_IS_HKDF( alg ) )
3960 {
3961 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3962 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3963 if( hash_size == 0 )
3964 return( PSA_ERROR_NOT_SUPPORTED );
3965 max_capacity = 255 * hash_size;
3966 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3967 secret, secret_length,
3968 hash_alg,
3969 salt, salt_length,
3970 label, label_length );
3971 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003972 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
3973 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
3974 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003975 {
3976 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3977 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3978
3979 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
3980 if( hash_alg != PSA_ALG_SHA_256 &&
3981 hash_alg != PSA_ALG_SHA_384 )
3982 {
3983 return( PSA_ERROR_NOT_SUPPORTED );
3984 }
3985
3986 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00003987
3988 if( PSA_ALG_IS_TLS12_PRF( alg ) )
3989 {
3990 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
3991 secret, secret_length,
3992 hash_alg, salt, salt_length,
3993 label, label_length );
3994 }
3995 else
3996 {
3997 status = psa_generator_tls12_psk_to_ms_setup(
3998 &generator->ctx.tls12_prf,
3999 secret, secret_length,
4000 hash_alg, salt, salt_length,
4001 label, label_length );
4002 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004003 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004004 else
4005#endif
4006 {
4007 return( PSA_ERROR_NOT_SUPPORTED );
4008 }
4009
4010 if( status != PSA_SUCCESS )
4011 return( status );
4012
4013 if( capacity <= max_capacity )
4014 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004015 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4016 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004017 else
4018 return( PSA_ERROR_INVALID_ARGUMENT );
4019
4020 return( PSA_SUCCESS );
4021}
4022
Gilles Peskineea0fb492018-07-12 17:17:20 +02004023psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004024 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004025 psa_algorithm_t alg,
4026 const uint8_t *salt,
4027 size_t salt_length,
4028 const uint8_t *label,
4029 size_t label_length,
4030 size_t capacity )
4031{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004032 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004033 psa_status_t status;
4034
4035 if( generator->alg != 0 )
4036 return( PSA_ERROR_BAD_STATE );
4037
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004038 /* Make sure that alg is a key derivation algorithm. This prevents
4039 * key selection algorithms, which psa_key_derivation_internal
4040 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004041 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4042 return( PSA_ERROR_INVALID_ARGUMENT );
4043
Gilles Peskinec5487a82018-12-03 18:08:14 +01004044 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004045 if( status != PSA_SUCCESS )
4046 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004047
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004048 if( slot->type != PSA_KEY_TYPE_DERIVE )
4049 return( PSA_ERROR_INVALID_ARGUMENT );
4050
4051 status = psa_key_derivation_internal( generator,
4052 slot->data.raw.data,
4053 slot->data.raw.bytes,
4054 alg,
4055 salt, salt_length,
4056 label, label_length,
4057 capacity );
4058 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004059 psa_generator_abort( generator );
4060 return( status );
4061}
4062
4063
4064
4065/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004066/* Key agreement */
4067/****************************************************************/
4068
Gilles Peskinea05219c2018-11-16 16:02:56 +01004069#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004070static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4071 size_t peer_key_length,
4072 const mbedtls_ecp_keypair *our_key,
4073 uint8_t *shared_secret,
4074 size_t shared_secret_size,
4075 size_t *shared_secret_length )
4076{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004077 mbedtls_ecp_keypair *their_key = NULL;
4078 mbedtls_ecdh_context ecdh;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004079 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004080 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004081
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004082 status = psa_import_ec_public_key(
4083 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4084 peer_key, peer_key_length,
4085 &their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004086 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004087 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01004088
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004089 status = mbedtls_to_psa_error(
4090 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4091 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004092 goto exit;
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004093 status = mbedtls_to_psa_error(
4094 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4095 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004096 goto exit;
4097
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004098 status = mbedtls_to_psa_error(
4099 mbedtls_ecdh_calc_secret( &ecdh,
4100 shared_secret_length,
4101 shared_secret, shared_secret_size,
4102 mbedtls_ctr_drbg_random,
4103 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004104
4105exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004106 mbedtls_ecdh_free( &ecdh );
Jaeden Amero0ae445f2019-01-10 11:42:27 +00004107 mbedtls_ecp_keypair_free( their_key );
4108 mbedtls_free( their_key );
Jaeden Amero1e5c2bd2019-01-10 19:38:51 +00004109 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004110}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004111#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004112
Gilles Peskine01d718c2018-09-18 12:01:02 +02004113#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4114
Gilles Peskine346797d2018-11-16 16:05:06 +01004115/* Note that if this function fails, you must call psa_generator_abort()
4116 * to potentially free embedded data structures and wipe confidential data.
4117 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004118static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004119 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004120 const uint8_t *peer_key,
4121 size_t peer_key_length,
4122 psa_algorithm_t alg )
4123{
4124 psa_status_t status;
4125 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4126 size_t shared_secret_length = 0;
4127
4128 /* Step 1: run the secret agreement algorithm to generate the shared
4129 * secret. */
4130 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4131 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004132#if defined(MBEDTLS_ECDH_C)
4133 case PSA_ALG_ECDH_BASE:
4134 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4135 return( PSA_ERROR_INVALID_ARGUMENT );
4136 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4137 private_key->data.ecp,
4138 shared_secret,
4139 sizeof( shared_secret ),
4140 &shared_secret_length );
4141 break;
4142#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004143 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004144 (void) private_key;
4145 (void) peer_key;
4146 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004147 return( PSA_ERROR_NOT_SUPPORTED );
4148 }
4149 if( status != PSA_SUCCESS )
4150 goto exit;
4151
4152 /* Step 2: set up the key derivation to generate key material from
4153 * the shared secret. */
4154 status = psa_key_derivation_internal( generator,
4155 shared_secret, shared_secret_length,
4156 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4157 NULL, 0, NULL, 0,
4158 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4159exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004160 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004161 return( status );
4162}
4163
4164psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004165 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004166 const uint8_t *peer_key,
4167 size_t peer_key_length,
4168 psa_algorithm_t alg )
4169{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004170 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004171 psa_status_t status;
4172 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4173 return( PSA_ERROR_INVALID_ARGUMENT );
4174 status = psa_get_key_from_slot( private_key, &slot,
4175 PSA_KEY_USAGE_DERIVE, alg );
4176 if( status != PSA_SUCCESS )
4177 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004178 status = psa_key_agreement_internal( generator,
4179 slot,
4180 peer_key, peer_key_length,
4181 alg );
4182 if( status != PSA_SUCCESS )
4183 psa_generator_abort( generator );
4184 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004185}
4186
4187
4188
4189/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004190/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004191/****************************************************************/
4192
4193psa_status_t psa_generate_random( uint8_t *output,
4194 size_t output_size )
4195{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004196 int ret;
4197 GUARD_MODULE_INITIALIZED;
4198
4199 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004200 return( mbedtls_to_psa_error( ret ) );
4201}
4202
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004203#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004204
4205/* Support function for error conversion between psa_its error codes to psa crypto */
4206static psa_status_t its_to_psa_error( psa_its_status_t ret )
4207{
4208 switch( ret )
4209 {
4210 case PSA_ITS_SUCCESS:
4211 return( PSA_SUCCESS );
4212
4213 case PSA_ITS_ERROR_KEY_NOT_FOUND:
4214 return( PSA_ERROR_EMPTY_SLOT );
4215
4216 case PSA_ITS_ERROR_STORAGE_FAILURE:
4217 return( PSA_ERROR_STORAGE_FAILURE );
4218
4219 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4220 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4221
4222 case PSA_ITS_ERROR_INVALID_KEY:
Jaeden Amero58600552018-11-30 12:04:38 +00004223 case PSA_ITS_ERROR_OFFSET_INVALID:
avolinski13beb102018-11-20 16:51:49 +02004224 case PSA_ITS_ERROR_INCORRECT_SIZE:
4225 case PSA_ITS_ERROR_BAD_POINTER:
4226 return( PSA_ERROR_INVALID_ARGUMENT );
4227
4228 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4229 return( PSA_ERROR_NOT_SUPPORTED );
4230
4231 case PSA_ITS_ERROR_WRITE_ONCE:
4232 return( PSA_ERROR_OCCUPIED_SLOT );
4233
4234 default:
4235 return( PSA_ERROR_UNKNOWN_ERROR );
4236 }
4237}
4238
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004239psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4240 size_t seed_size )
4241{
4242 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004243 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004244 struct psa_its_info_t p_info;
4245 if( global_data.initialized )
4246 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004247
4248 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4249 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4250 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4251 return( PSA_ERROR_INVALID_ARGUMENT );
4252
avolinski0d2c2662018-11-21 17:31:07 +02004253 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004254 status = its_to_psa_error( its_status );
4255
4256 if( PSA_ITS_ERROR_KEY_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004257 {
avolinski0d2c2662018-11-21 17:31:07 +02004258 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004259 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004260 }
avolinski13beb102018-11-20 16:51:49 +02004261 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004262 {
4263 /* You should not be here. Seed needs to be injected only once */
4264 status = PSA_ERROR_NOT_PERMITTED;
4265 }
4266 return( status );
4267}
4268#endif
4269
Gilles Peskinec5487a82018-12-03 18:08:14 +01004270psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004271 psa_key_type_t type,
4272 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004273 const void *extra,
4274 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004275{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004276 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004277 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004278
Gilles Peskine53d991e2018-07-12 01:14:59 +02004279 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004280 return( PSA_ERROR_INVALID_ARGUMENT );
4281
Gilles Peskinec5487a82018-12-03 18:08:14 +01004282 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004283 if( status != PSA_SUCCESS )
4284 return( status );
4285
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004286 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004287 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004288 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004289 if( status != PSA_SUCCESS )
4290 return( status );
4291 status = psa_generate_random( slot->data.raw.data,
4292 slot->data.raw.bytes );
4293 if( status != PSA_SUCCESS )
4294 {
4295 mbedtls_free( slot->data.raw.data );
4296 return( status );
4297 }
4298#if defined(MBEDTLS_DES_C)
4299 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004300 psa_des_set_key_parity( slot->data.raw.data,
4301 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004302#endif /* MBEDTLS_DES_C */
4303 }
4304 else
4305
4306#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4307 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4308 {
4309 mbedtls_rsa_context *rsa;
4310 int ret;
4311 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004312 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4313 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004314 /* Accept only byte-aligned keys, for the same reasons as
4315 * in psa_import_rsa_key(). */
4316 if( bits % 8 != 0 )
4317 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004318 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004319 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004320 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004321 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004322 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004323#if INT_MAX < 0xffffffff
4324 /* Check that the uint32_t value passed by the caller fits
4325 * in the range supported by this implementation. */
4326 if( p->e > INT_MAX )
4327 return( PSA_ERROR_NOT_SUPPORTED );
4328#endif
4329 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004330 }
4331 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4332 if( rsa == NULL )
4333 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4334 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4335 ret = mbedtls_rsa_gen_key( rsa,
4336 mbedtls_ctr_drbg_random,
4337 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004338 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004339 exponent );
4340 if( ret != 0 )
4341 {
4342 mbedtls_rsa_free( rsa );
4343 mbedtls_free( rsa );
4344 return( mbedtls_to_psa_error( ret ) );
4345 }
4346 slot->data.rsa = rsa;
4347 }
4348 else
4349#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4350
4351#if defined(MBEDTLS_ECP_C)
4352 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4353 {
4354 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4355 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4356 const mbedtls_ecp_curve_info *curve_info =
4357 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4358 mbedtls_ecp_keypair *ecp;
4359 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004360 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004361 return( PSA_ERROR_NOT_SUPPORTED );
4362 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4363 return( PSA_ERROR_NOT_SUPPORTED );
4364 if( curve_info->bit_size != bits )
4365 return( PSA_ERROR_INVALID_ARGUMENT );
4366 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4367 if( ecp == NULL )
4368 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4369 mbedtls_ecp_keypair_init( ecp );
4370 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4371 mbedtls_ctr_drbg_random,
4372 &global_data.ctr_drbg );
4373 if( ret != 0 )
4374 {
4375 mbedtls_ecp_keypair_free( ecp );
4376 mbedtls_free( ecp );
4377 return( mbedtls_to_psa_error( ret ) );
4378 }
4379 slot->data.ecp = ecp;
4380 }
4381 else
4382#endif /* MBEDTLS_ECP_C */
4383
4384 return( PSA_ERROR_NOT_SUPPORTED );
4385
4386 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004387
4388#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4389 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4390 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004391 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004392 }
4393#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4394
4395 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004396}
4397
4398
4399/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004400/* Module setup */
4401/****************************************************************/
4402
Gilles Peskine5e769522018-11-20 21:59:56 +01004403psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4404 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4405 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4406{
4407 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4408 return( PSA_ERROR_BAD_STATE );
4409 global_data.entropy_init = entropy_init;
4410 global_data.entropy_free = entropy_free;
4411 return( PSA_SUCCESS );
4412}
4413
Gilles Peskinee59236f2018-01-27 23:32:46 +01004414void mbedtls_psa_crypto_free( void )
4415{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004416 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004417 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4418 {
4419 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004420 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004421 }
4422 /* Wipe all remaining data, including configuration.
4423 * In particular, this sets all state indicator to the value
4424 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004425 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004426}
4427
4428psa_status_t psa_crypto_init( void )
4429{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004430 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004431 const unsigned char drbg_seed[] = "PSA";
4432
Gilles Peskinec6b69072018-11-20 21:42:52 +01004433 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004434 if( global_data.initialized != 0 )
4435 return( PSA_SUCCESS );
4436
Gilles Peskine5e769522018-11-20 21:59:56 +01004437 /* Set default configuration if
4438 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4439 if( global_data.entropy_init == NULL )
4440 global_data.entropy_init = mbedtls_entropy_init;
4441 if( global_data.entropy_free == NULL )
4442 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004443
Gilles Peskinec6b69072018-11-20 21:42:52 +01004444 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004445 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004446 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004447 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004448 status = mbedtls_to_psa_error(
4449 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4450 mbedtls_entropy_func,
4451 &global_data.entropy,
4452 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4453 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004454 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004455 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004456
Gilles Peskine66fb1262018-12-10 16:29:04 +01004457 status = psa_initialize_key_slots( );
4458 if( status != PSA_SUCCESS )
4459 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004460
4461 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004462 global_data.initialized = 1;
4463
Gilles Peskinee59236f2018-01-27 23:32:46 +01004464exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004465 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004466 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004467 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004468}
4469
4470#endif /* MBEDTLS_PSA_CRYPTO_C */