blob: 01bd9574f21aa644a64a1139c2016d370215789c [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 Amero25384a22019-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
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200515static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
516 mbedtls_rsa_context **p_rsa )
517{
518 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
519 return( PSA_ERROR_INVALID_ARGUMENT );
520 else
521 {
522 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
Gilles Peskineaac64a22018-11-12 18:37:42 +0100523 /* The size of an RSA key doesn't have to be a multiple of 8.
524 * Mbed TLS supports non-byte-aligned key sizes, but not well.
525 * For example, mbedtls_rsa_get_len() returns the key size in
526 * bytes, not in bits. */
527 size_t bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100528 psa_status_t status;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200529 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
530 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100531 status = psa_check_rsa_key_byte_aligned( rsa );
532 if( status != PSA_SUCCESS )
533 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200534 *p_rsa = rsa;
535 return( PSA_SUCCESS );
536 }
537}
538#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
539
Jaeden Ameroccdce902019-01-10 11:42:27 +0000540#if defined(MBEDTLS_ECP_C)
541
542/* Import a public key given as the uncompressed representation defined by SEC1
543 * 2.3.3 as the content of an ECPoint. */
544static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
545 const uint8_t *data,
546 size_t data_length,
547 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200548{
Jaeden Ameroccdce902019-01-10 11:42:27 +0000549 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
550 mbedtls_ecp_keypair *ecp = NULL;
551 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
552
553 *p_ecp = NULL;
554 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
555 if( ecp == NULL )
556 return( PSA_ERROR_INSUFFICIENT_MEMORY );
557 mbedtls_ecp_keypair_init( ecp );
558
559 /* Load the group. */
560 status = mbedtls_to_psa_error(
561 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
562 if( status != PSA_SUCCESS )
563 goto exit;
564 /* Load the public value. */
565 status = mbedtls_to_psa_error(
566 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
567 data, data_length ) );
568 if( status != PSA_SUCCESS )
569 goto exit;
570
571 /* Check that the point is on the curve. */
572 status = mbedtls_to_psa_error(
573 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
574 if( status != PSA_SUCCESS )
575 goto exit;
576
577 *p_ecp = ecp;
578 return( PSA_SUCCESS );
579
580exit:
581 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200582 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000583 mbedtls_ecp_keypair_free( ecp );
584 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200585 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000586 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200587}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000588#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200589
Gilles Peskinef76aa772018-10-29 19:24:33 +0100590#if defined(MBEDTLS_ECP_C)
591/* Import a private key given as a byte string which is the private value
592 * in big-endian order. */
593static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
594 const uint8_t *data,
595 size_t data_length,
596 mbedtls_ecp_keypair **p_ecp )
597{
598 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
599 mbedtls_ecp_keypair *ecp = NULL;
600 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
601
602 *p_ecp = NULL;
603 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
604 if( ecp == NULL )
605 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000606 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100607
608 /* Load the group. */
609 status = mbedtls_to_psa_error(
610 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
611 if( status != PSA_SUCCESS )
612 goto exit;
613 /* Load the secret value. */
614 status = mbedtls_to_psa_error(
615 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
616 if( status != PSA_SUCCESS )
617 goto exit;
618 /* Validate the private key. */
619 status = mbedtls_to_psa_error(
620 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
621 if( status != PSA_SUCCESS )
622 goto exit;
623 /* Calculate the public key from the private key. */
624 status = mbedtls_to_psa_error(
625 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
626 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
627 if( status != PSA_SUCCESS )
628 goto exit;
629
630 *p_ecp = ecp;
631 return( PSA_SUCCESS );
632
633exit:
634 if( ecp != NULL )
635 {
636 mbedtls_ecp_keypair_free( ecp );
637 mbedtls_free( ecp );
638 }
639 return( status );
640}
641#endif /* defined(MBEDTLS_ECP_C) */
642
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100643/** Import key data into a slot. `slot->type` must have been set
644 * previously. This function assumes that the slot does not contain
645 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100646psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
647 const uint8_t *data,
648 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100649{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200650 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100651
Darryl Green940d72c2018-07-13 13:18:51 +0100652 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100653 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100654 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100655 if( data_length > SIZE_MAX / 8 )
656 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100657 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200658 PSA_BYTES_TO_BITS( data_length ),
659 &slot->data.raw );
660 if( status != PSA_SUCCESS )
661 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200662 if( data_length != 0 )
663 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100664 }
665 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100666#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100667 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100668 {
Darryl Green940d72c2018-07-13 13:18:51 +0100669 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100670 data, data_length,
671 &slot->data.ecp );
672 if( status != PSA_SUCCESS )
673 return( status );
674 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000675 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
676 {
677 status = psa_import_ec_public_key(
678 PSA_KEY_TYPE_GET_CURVE( slot->type ),
679 data, data_length,
680 &slot->data.ecp );
681
682 if( status != PSA_SUCCESS )
683 return( status );
684 }
Gilles Peskinef76aa772018-10-29 19:24:33 +0100685 else
686#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000687#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
688 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100689 {
690 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100691 mbedtls_pk_context pk;
692 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200693
694 /* Parse the data. */
Darryl Green940d72c2018-07-13 13:18:51 +0100695 if( PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100696 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
697 else
698 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100699 if( ret != 0 )
700 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200701
Jaeden Ameroccdce902019-01-10 11:42:27 +0000702 /* We have something that the pkparse module recognizes. If it is a
703 * valid RSA key, store it. */
704 status = psa_import_rsa_key( &pk, &slot->data.rsa );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200705
Gilles Peskinec648d692018-06-28 08:46:13 +0200706 /* Free the content of the pk object only on error. On success,
707 * the content of the object has been stored in the slot. */
708 if( status != PSA_SUCCESS )
709 {
710 mbedtls_pk_free( &pk );
711 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100712 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100713 }
714 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000715#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100716 {
717 return( PSA_ERROR_NOT_SUPPORTED );
718 }
Darryl Green940d72c2018-07-13 13:18:51 +0100719 return( PSA_SUCCESS );
720}
721
Darryl Green06fd18d2018-07-16 11:21:11 +0100722/* Retrieve an empty key slot (slot with no key data, but possibly
723 * with some metadata such as a policy). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100724static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100725 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100726{
727 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100728 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100729
730 *p_slot = NULL;
731
Gilles Peskinec5487a82018-12-03 18:08:14 +0100732 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100733 if( status != PSA_SUCCESS )
734 return( status );
735
736 if( slot->type != PSA_KEY_TYPE_NONE )
737 return( PSA_ERROR_OCCUPIED_SLOT );
738
739 *p_slot = slot;
740 return( status );
741}
742
743/** Retrieve a slot which must contain a key. The key must have allow all the
744 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
745 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100746static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100747 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100748 psa_key_usage_t usage,
749 psa_algorithm_t alg )
750{
751 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100752 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100753
754 *p_slot = NULL;
755
Gilles Peskinec5487a82018-12-03 18:08:14 +0100756 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100757 if( status != PSA_SUCCESS )
758 return( status );
759 if( slot->type == PSA_KEY_TYPE_NONE )
760 return( PSA_ERROR_EMPTY_SLOT );
761
762 /* Enforce that usage policy for the key slot contains all the flags
763 * required by the usage parameter. There is one exception: public
764 * keys can always be exported, so we treat public key objects as
765 * if they had the export flag. */
766 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
767 usage &= ~PSA_KEY_USAGE_EXPORT;
768 if( ( slot->policy.usage & usage ) != usage )
769 return( PSA_ERROR_NOT_PERMITTED );
770 if( alg != 0 && ( alg != slot->policy.alg ) )
771 return( PSA_ERROR_NOT_PERMITTED );
772
773 *p_slot = slot;
774 return( PSA_SUCCESS );
775}
Darryl Green940d72c2018-07-13 13:18:51 +0100776
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100777/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100778static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000779{
780 if( slot->type == PSA_KEY_TYPE_NONE )
781 {
782 /* No key material to clean. */
783 }
784 else if( key_type_is_raw_bytes( slot->type ) )
785 {
786 mbedtls_free( slot->data.raw.data );
787 }
788 else
789#if defined(MBEDTLS_RSA_C)
790 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
791 {
792 mbedtls_rsa_free( slot->data.rsa );
793 mbedtls_free( slot->data.rsa );
794 }
795 else
796#endif /* defined(MBEDTLS_RSA_C) */
797#if defined(MBEDTLS_ECP_C)
798 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
799 {
800 mbedtls_ecp_keypair_free( slot->data.ecp );
801 mbedtls_free( slot->data.ecp );
802 }
803 else
804#endif /* defined(MBEDTLS_ECP_C) */
805 {
806 /* Shouldn't happen: the key type is not any type that we
807 * put in. */
808 return( PSA_ERROR_TAMPERING_DETECTED );
809 }
810
811 return( PSA_SUCCESS );
812}
813
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100814/** Completely wipe a slot in memory, including its policy.
815 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100816psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100817{
818 psa_status_t status = psa_remove_key_data_from_memory( slot );
819 /* At this point, key material and other type-specific content has
820 * been wiped. Clear remaining metadata. We can call memset and not
821 * zeroize because the metadata is not particularly sensitive. */
822 memset( slot, 0, sizeof( *slot ) );
823 return( status );
824}
825
Gilles Peskinec5487a82018-12-03 18:08:14 +0100826psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100827 psa_key_type_t type,
828 const uint8_t *data,
829 size_t data_length )
830{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100831 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100832 psa_status_t status;
833
Gilles Peskinec5487a82018-12-03 18:08:14 +0100834 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100835 if( status != PSA_SUCCESS )
836 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837
838 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100839
840 status = psa_import_key_into_slot( slot, data, data_length );
841 if( status != PSA_SUCCESS )
842 {
843 slot->type = PSA_KEY_TYPE_NONE;
844 return( status );
845 }
846
Darryl Greend49a4992018-06-18 17:27:26 +0100847#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
848 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
849 {
850 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100851 status = psa_save_persistent_key( slot->persistent_storage_id,
852 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100853 data_length );
854 if( status != PSA_SUCCESS )
855 {
856 (void) psa_remove_key_data_from_memory( slot );
857 slot->type = PSA_KEY_TYPE_NONE;
858 }
859 }
860#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
861
862 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100863}
864
Gilles Peskinec5487a82018-12-03 18:08:14 +0100865psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100866{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100867 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100868 psa_status_t status = PSA_SUCCESS;
869 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100870
Gilles Peskinec5487a82018-12-03 18:08:14 +0100871 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200872 if( status != PSA_SUCCESS )
873 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100874#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
875 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
876 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100877 storage_status =
878 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100879 }
880#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100881 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100882 if( status != PSA_SUCCESS )
883 return( status );
884 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100885}
886
Gilles Peskineb870b182018-07-06 16:02:09 +0200887/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100888static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200889{
890 if( key_type_is_raw_bytes( slot->type ) )
891 return( slot->data.raw.bytes * 8 );
892#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200893 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100894 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200895#endif /* defined(MBEDTLS_RSA_C) */
896#if defined(MBEDTLS_ECP_C)
897 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
898 return( slot->data.ecp->grp.pbits );
899#endif /* defined(MBEDTLS_ECP_C) */
900 /* Shouldn't happen except on an empty slot. */
901 return( 0 );
902}
903
Gilles Peskinec5487a82018-12-03 18:08:14 +0100904psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200905 psa_key_type_t *type,
906 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100907{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100908 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200909 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100910
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100911 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200912 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100913 if( bits != NULL )
914 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100915 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200916 if( status != PSA_SUCCESS )
917 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200918
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100919 if( slot->type == PSA_KEY_TYPE_NONE )
920 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200921 if( type != NULL )
922 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200923 if( bits != NULL )
924 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100925 return( PSA_SUCCESS );
926}
927
Jaeden Ameroccdce902019-01-10 11:42:27 +0000928#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +0000929static int pk_write_pubkey_simple( mbedtls_pk_context *key,
930 unsigned char *buf, size_t size )
931{
932 int ret;
933 unsigned char *c;
934 size_t len = 0;
935
936 c = buf + size;
937
938 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
939
940 return( (int) len );
941}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000942#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +0000943
Gilles Peskine2f060a82018-12-04 17:12:32 +0100944static psa_status_t psa_internal_export_key( psa_key_slot_t *slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200945 uint8_t *data,
946 size_t data_size,
947 size_t *data_length,
948 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100949{
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100950 *data_length = 0;
951
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200952 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300953 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300954
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200955 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100956 {
957 if( slot->data.raw.bytes > data_size )
958 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +0100959 if( data_size != 0 )
960 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200961 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +0100962 memset( data + slot->data.raw.bytes, 0,
963 data_size - slot->data.raw.bytes );
964 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100965 *data_length = slot->data.raw.bytes;
966 return( PSA_SUCCESS );
967 }
Gilles Peskine188c71e2018-10-29 19:26:02 +0100968#if defined(MBEDTLS_ECP_C)
969 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
970 {
Darryl Greendd8fb772018-11-07 16:00:44 +0000971 psa_status_t status;
972
Gilles Peskine188c71e2018-10-29 19:26:02 +0100973 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
974 if( bytes > data_size )
975 return( PSA_ERROR_BUFFER_TOO_SMALL );
976 status = mbedtls_to_psa_error(
977 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
978 if( status != PSA_SUCCESS )
979 return( status );
980 memset( data + bytes, 0, data_size - bytes );
981 *data_length = bytes;
982 return( PSA_SUCCESS );
983 }
984#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100985 else
Moran Peker17e36e12018-05-02 12:55:20 +0300986 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100987#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200988 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300989 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100990 {
Moran Pekera998bc62018-04-16 18:16:20 +0300991 mbedtls_pk_context pk;
992 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +0200993 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300994 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100995#if defined(MBEDTLS_RSA_C)
996 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300997 pk.pk_info = &mbedtls_rsa_info;
998 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100999#else
1000 return( PSA_ERROR_NOT_SUPPORTED );
1001#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001002 }
1003 else
1004 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001005#if defined(MBEDTLS_ECP_C)
1006 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001007 pk.pk_info = &mbedtls_eckey_info;
1008 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001009#else
1010 return( PSA_ERROR_NOT_SUPPORTED );
1011#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001012 }
Moran Pekerd7326592018-05-29 16:56:39 +03001013 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001014 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001015 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001016 }
Moran Peker17e36e12018-05-02 12:55:20 +03001017 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001018 {
Moran Peker17e36e12018-05-02 12:55:20 +03001019 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001020 }
Moran Peker60364322018-04-29 11:34:58 +03001021 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001022 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001023 /* If data_size is 0 then data may be NULL and then the
1024 * call to memset would have undefined behavior. */
1025 if( data_size != 0 )
1026 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001027 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001028 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001029 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1030 * Move the data to the beginning and erase remaining data
1031 * at the original location. */
1032 if( 2 * (size_t) ret <= data_size )
1033 {
1034 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001035 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001036 }
1037 else if( (size_t) ret < data_size )
1038 {
1039 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001040 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001041 }
Moran Pekera998bc62018-04-16 18:16:20 +03001042 *data_length = ret;
1043 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001044 }
1045 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001046#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001047 {
1048 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001049 it is valid for a special-purpose implementation to omit
1050 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001051 return( PSA_ERROR_NOT_SUPPORTED );
1052 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001053 }
1054}
1055
Gilles Peskinec5487a82018-12-03 18:08:14 +01001056psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001057 uint8_t *data,
1058 size_t data_size,
1059 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001060{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001061 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001062 psa_status_t status;
1063
1064 /* Set the key to empty now, so that even when there are errors, we always
1065 * set data_length to a value between 0 and data_size. On error, setting
1066 * the key to empty is a good choice because an empty key representation is
1067 * unlikely to be accepted anywhere. */
1068 *data_length = 0;
1069
1070 /* Export requires the EXPORT flag. There is an exception for public keys,
1071 * which don't require any flag, but psa_get_key_from_slot takes
1072 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001073 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001074 if( status != PSA_SUCCESS )
1075 return( status );
1076 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001077 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001078}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001079
Gilles Peskinec5487a82018-12-03 18:08:14 +01001080psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001081 uint8_t *data,
1082 size_t data_size,
1083 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001084{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001085 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001086 psa_status_t status;
1087
1088 /* Set the key to empty now, so that even when there are errors, we always
1089 * set data_length to a value between 0 and data_size. On error, setting
1090 * the key to empty is a good choice because an empty key representation is
1091 * unlikely to be accepted anywhere. */
1092 *data_length = 0;
1093
1094 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001095 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001096 if( status != PSA_SUCCESS )
1097 return( status );
1098 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001099 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001100}
1101
Darryl Green0c6575a2018-11-07 16:05:30 +00001102#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001103static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001104 size_t bits )
1105{
1106 psa_status_t status;
1107 uint8_t *data;
1108 size_t key_length;
1109 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1110 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001111 if( data == NULL )
1112 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001113 /* Get key data in export format */
1114 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1115 if( status != PSA_SUCCESS )
1116 {
1117 slot->type = PSA_KEY_TYPE_NONE;
1118 goto exit;
1119 }
1120 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001121 status = psa_save_persistent_key( slot->persistent_storage_id,
1122 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001123 data, key_length );
1124 if( status != PSA_SUCCESS )
1125 {
1126 slot->type = PSA_KEY_TYPE_NONE;
1127 }
1128exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001129 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001130 mbedtls_free( data );
1131 return( status );
1132}
1133#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1134
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001135
1136
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001137/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001138/* Message digests */
1139/****************************************************************/
1140
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001141static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001142{
1143 switch( alg )
1144 {
1145#if defined(MBEDTLS_MD2_C)
1146 case PSA_ALG_MD2:
1147 return( &mbedtls_md2_info );
1148#endif
1149#if defined(MBEDTLS_MD4_C)
1150 case PSA_ALG_MD4:
1151 return( &mbedtls_md4_info );
1152#endif
1153#if defined(MBEDTLS_MD5_C)
1154 case PSA_ALG_MD5:
1155 return( &mbedtls_md5_info );
1156#endif
1157#if defined(MBEDTLS_RIPEMD160_C)
1158 case PSA_ALG_RIPEMD160:
1159 return( &mbedtls_ripemd160_info );
1160#endif
1161#if defined(MBEDTLS_SHA1_C)
1162 case PSA_ALG_SHA_1:
1163 return( &mbedtls_sha1_info );
1164#endif
1165#if defined(MBEDTLS_SHA256_C)
1166 case PSA_ALG_SHA_224:
1167 return( &mbedtls_sha224_info );
1168 case PSA_ALG_SHA_256:
1169 return( &mbedtls_sha256_info );
1170#endif
1171#if defined(MBEDTLS_SHA512_C)
1172 case PSA_ALG_SHA_384:
1173 return( &mbedtls_sha384_info );
1174 case PSA_ALG_SHA_512:
1175 return( &mbedtls_sha512_info );
1176#endif
1177 default:
1178 return( NULL );
1179 }
1180}
1181
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001182psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1183{
1184 switch( operation->alg )
1185 {
Gilles Peskine81736312018-06-26 15:04:31 +02001186 case 0:
1187 /* The object has (apparently) been initialized but it is not
1188 * in use. It's ok to call abort on such an object, and there's
1189 * nothing to do. */
1190 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001191#if defined(MBEDTLS_MD2_C)
1192 case PSA_ALG_MD2:
1193 mbedtls_md2_free( &operation->ctx.md2 );
1194 break;
1195#endif
1196#if defined(MBEDTLS_MD4_C)
1197 case PSA_ALG_MD4:
1198 mbedtls_md4_free( &operation->ctx.md4 );
1199 break;
1200#endif
1201#if defined(MBEDTLS_MD5_C)
1202 case PSA_ALG_MD5:
1203 mbedtls_md5_free( &operation->ctx.md5 );
1204 break;
1205#endif
1206#if defined(MBEDTLS_RIPEMD160_C)
1207 case PSA_ALG_RIPEMD160:
1208 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1209 break;
1210#endif
1211#if defined(MBEDTLS_SHA1_C)
1212 case PSA_ALG_SHA_1:
1213 mbedtls_sha1_free( &operation->ctx.sha1 );
1214 break;
1215#endif
1216#if defined(MBEDTLS_SHA256_C)
1217 case PSA_ALG_SHA_224:
1218 case PSA_ALG_SHA_256:
1219 mbedtls_sha256_free( &operation->ctx.sha256 );
1220 break;
1221#endif
1222#if defined(MBEDTLS_SHA512_C)
1223 case PSA_ALG_SHA_384:
1224 case PSA_ALG_SHA_512:
1225 mbedtls_sha512_free( &operation->ctx.sha512 );
1226 break;
1227#endif
1228 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001229 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001230 }
1231 operation->alg = 0;
1232 return( PSA_SUCCESS );
1233}
1234
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001235psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001236 psa_algorithm_t alg )
1237{
1238 int ret;
1239 operation->alg = 0;
1240 switch( alg )
1241 {
1242#if defined(MBEDTLS_MD2_C)
1243 case PSA_ALG_MD2:
1244 mbedtls_md2_init( &operation->ctx.md2 );
1245 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1246 break;
1247#endif
1248#if defined(MBEDTLS_MD4_C)
1249 case PSA_ALG_MD4:
1250 mbedtls_md4_init( &operation->ctx.md4 );
1251 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1252 break;
1253#endif
1254#if defined(MBEDTLS_MD5_C)
1255 case PSA_ALG_MD5:
1256 mbedtls_md5_init( &operation->ctx.md5 );
1257 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1258 break;
1259#endif
1260#if defined(MBEDTLS_RIPEMD160_C)
1261 case PSA_ALG_RIPEMD160:
1262 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1263 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1264 break;
1265#endif
1266#if defined(MBEDTLS_SHA1_C)
1267 case PSA_ALG_SHA_1:
1268 mbedtls_sha1_init( &operation->ctx.sha1 );
1269 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1270 break;
1271#endif
1272#if defined(MBEDTLS_SHA256_C)
1273 case PSA_ALG_SHA_224:
1274 mbedtls_sha256_init( &operation->ctx.sha256 );
1275 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1276 break;
1277 case PSA_ALG_SHA_256:
1278 mbedtls_sha256_init( &operation->ctx.sha256 );
1279 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1280 break;
1281#endif
1282#if defined(MBEDTLS_SHA512_C)
1283 case PSA_ALG_SHA_384:
1284 mbedtls_sha512_init( &operation->ctx.sha512 );
1285 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1286 break;
1287 case PSA_ALG_SHA_512:
1288 mbedtls_sha512_init( &operation->ctx.sha512 );
1289 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1290 break;
1291#endif
1292 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001293 return( PSA_ALG_IS_HASH( alg ) ?
1294 PSA_ERROR_NOT_SUPPORTED :
1295 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001296 }
1297 if( ret == 0 )
1298 operation->alg = alg;
1299 else
1300 psa_hash_abort( operation );
1301 return( mbedtls_to_psa_error( ret ) );
1302}
1303
1304psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1305 const uint8_t *input,
1306 size_t input_length )
1307{
1308 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001309
1310 /* Don't require hash implementations to behave correctly on a
1311 * zero-length input, which may have an invalid pointer. */
1312 if( input_length == 0 )
1313 return( PSA_SUCCESS );
1314
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001315 switch( operation->alg )
1316 {
1317#if defined(MBEDTLS_MD2_C)
1318 case PSA_ALG_MD2:
1319 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1320 input, input_length );
1321 break;
1322#endif
1323#if defined(MBEDTLS_MD4_C)
1324 case PSA_ALG_MD4:
1325 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1326 input, input_length );
1327 break;
1328#endif
1329#if defined(MBEDTLS_MD5_C)
1330 case PSA_ALG_MD5:
1331 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1332 input, input_length );
1333 break;
1334#endif
1335#if defined(MBEDTLS_RIPEMD160_C)
1336 case PSA_ALG_RIPEMD160:
1337 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1338 input, input_length );
1339 break;
1340#endif
1341#if defined(MBEDTLS_SHA1_C)
1342 case PSA_ALG_SHA_1:
1343 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1344 input, input_length );
1345 break;
1346#endif
1347#if defined(MBEDTLS_SHA256_C)
1348 case PSA_ALG_SHA_224:
1349 case PSA_ALG_SHA_256:
1350 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1351 input, input_length );
1352 break;
1353#endif
1354#if defined(MBEDTLS_SHA512_C)
1355 case PSA_ALG_SHA_384:
1356 case PSA_ALG_SHA_512:
1357 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1358 input, input_length );
1359 break;
1360#endif
1361 default:
1362 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1363 break;
1364 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001365
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001366 if( ret != 0 )
1367 psa_hash_abort( operation );
1368 return( mbedtls_to_psa_error( ret ) );
1369}
1370
1371psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1372 uint8_t *hash,
1373 size_t hash_size,
1374 size_t *hash_length )
1375{
itayzafrir40835d42018-08-02 13:14:17 +03001376 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001377 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001378 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001379
1380 /* Fill the output buffer with something that isn't a valid hash
1381 * (barring an attack on the hash and deliberately-crafted input),
1382 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001383 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001384 /* If hash_size is 0 then hash may be NULL and then the
1385 * call to memset would have undefined behavior. */
1386 if( hash_size != 0 )
1387 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001388
1389 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001390 {
1391 status = PSA_ERROR_BUFFER_TOO_SMALL;
1392 goto exit;
1393 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001394
1395 switch( operation->alg )
1396 {
1397#if defined(MBEDTLS_MD2_C)
1398 case PSA_ALG_MD2:
1399 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1400 break;
1401#endif
1402#if defined(MBEDTLS_MD4_C)
1403 case PSA_ALG_MD4:
1404 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1405 break;
1406#endif
1407#if defined(MBEDTLS_MD5_C)
1408 case PSA_ALG_MD5:
1409 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1410 break;
1411#endif
1412#if defined(MBEDTLS_RIPEMD160_C)
1413 case PSA_ALG_RIPEMD160:
1414 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1415 break;
1416#endif
1417#if defined(MBEDTLS_SHA1_C)
1418 case PSA_ALG_SHA_1:
1419 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1420 break;
1421#endif
1422#if defined(MBEDTLS_SHA256_C)
1423 case PSA_ALG_SHA_224:
1424 case PSA_ALG_SHA_256:
1425 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1426 break;
1427#endif
1428#if defined(MBEDTLS_SHA512_C)
1429 case PSA_ALG_SHA_384:
1430 case PSA_ALG_SHA_512:
1431 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1432 break;
1433#endif
1434 default:
1435 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1436 break;
1437 }
itayzafrir40835d42018-08-02 13:14:17 +03001438 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001439
itayzafrir40835d42018-08-02 13:14:17 +03001440exit:
1441 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001442 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001443 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001444 return( psa_hash_abort( operation ) );
1445 }
1446 else
1447 {
1448 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001449 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001450 }
1451}
1452
Gilles Peskine2d277862018-06-18 15:41:12 +02001453psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1454 const uint8_t *hash,
1455 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001456{
1457 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1458 size_t actual_hash_length;
1459 psa_status_t status = psa_hash_finish( operation,
1460 actual_hash, sizeof( actual_hash ),
1461 &actual_hash_length );
1462 if( status != PSA_SUCCESS )
1463 return( status );
1464 if( actual_hash_length != hash_length )
1465 return( PSA_ERROR_INVALID_SIGNATURE );
1466 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1467 return( PSA_ERROR_INVALID_SIGNATURE );
1468 return( PSA_SUCCESS );
1469}
1470
1471
1472
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001473/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001474/* MAC */
1475/****************************************************************/
1476
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001477static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001478 psa_algorithm_t alg,
1479 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001480 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001481 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001482{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001483 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001484 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001485
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001486 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001487 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001488
Gilles Peskine8c9def32018-02-08 10:02:12 +01001489 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1490 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001491 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001492 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001493 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001494 mode = MBEDTLS_MODE_STREAM;
1495 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001496 case PSA_ALG_CTR:
1497 mode = MBEDTLS_MODE_CTR;
1498 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001499 case PSA_ALG_CFB:
1500 mode = MBEDTLS_MODE_CFB;
1501 break;
1502 case PSA_ALG_OFB:
1503 mode = MBEDTLS_MODE_OFB;
1504 break;
1505 case PSA_ALG_CBC_NO_PADDING:
1506 mode = MBEDTLS_MODE_CBC;
1507 break;
1508 case PSA_ALG_CBC_PKCS7:
1509 mode = MBEDTLS_MODE_CBC;
1510 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001511 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001512 mode = MBEDTLS_MODE_CCM;
1513 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001514 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001515 mode = MBEDTLS_MODE_GCM;
1516 break;
1517 default:
1518 return( NULL );
1519 }
1520 }
1521 else if( alg == PSA_ALG_CMAC )
1522 mode = MBEDTLS_MODE_ECB;
1523 else if( alg == PSA_ALG_GMAC )
1524 mode = MBEDTLS_MODE_GCM;
1525 else
1526 return( NULL );
1527
1528 switch( key_type )
1529 {
1530 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001531 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001532 break;
1533 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001534 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1535 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001536 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001537 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001538 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001539 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001540 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1541 * but two-key Triple-DES is functionally three-key Triple-DES
1542 * with K1=K3, so that's how we present it to mbedtls. */
1543 if( key_bits == 128 )
1544 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001545 break;
1546 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001547 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001548 break;
1549 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001550 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001551 break;
1552 default:
1553 return( NULL );
1554 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001555 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001556 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001557
Jaeden Amero23bbb752018-06-26 14:16:54 +01001558 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1559 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001560}
1561
Gilles Peskinea05219c2018-11-16 16:02:56 +01001562#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001563static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001564{
Gilles Peskine2d277862018-06-18 15:41:12 +02001565 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001566 {
1567 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001568 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001569 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001570 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001571 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001572 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001573 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001574 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001575 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001576 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001577 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001578 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001579 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001580 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001581 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001582 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001583 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001584 return( 128 );
1585 default:
1586 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001587 }
1588}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001589#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001590
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001591/* Initialize the MAC operation structure. Once this function has been
1592 * called, psa_mac_abort can run and will do the right thing. */
1593static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1594 psa_algorithm_t alg )
1595{
1596 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1597
1598 operation->alg = alg;
1599 operation->key_set = 0;
1600 operation->iv_set = 0;
1601 operation->iv_required = 0;
1602 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001603 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001604
1605#if defined(MBEDTLS_CMAC_C)
1606 if( alg == PSA_ALG_CMAC )
1607 {
1608 operation->iv_required = 0;
1609 mbedtls_cipher_init( &operation->ctx.cmac );
1610 status = PSA_SUCCESS;
1611 }
1612 else
1613#endif /* MBEDTLS_CMAC_C */
1614#if defined(MBEDTLS_MD_C)
1615 if( PSA_ALG_IS_HMAC( operation->alg ) )
1616 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001617 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1618 operation->ctx.hmac.hash_ctx.alg = 0;
1619 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001620 }
1621 else
1622#endif /* MBEDTLS_MD_C */
1623 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001624 if( ! PSA_ALG_IS_MAC( alg ) )
1625 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001626 }
1627
1628 if( status != PSA_SUCCESS )
1629 memset( operation, 0, sizeof( *operation ) );
1630 return( status );
1631}
1632
Gilles Peskine01126fa2018-07-12 17:04:55 +02001633#if defined(MBEDTLS_MD_C)
1634static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1635{
Gilles Peskine3f108122018-12-07 18:14:53 +01001636 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001637 return( psa_hash_abort( &hmac->hash_ctx ) );
1638}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001639
1640static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1641{
1642 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1643 memset( hmac, 0, sizeof( *hmac ) );
1644}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001645#endif /* MBEDTLS_MD_C */
1646
Gilles Peskine8c9def32018-02-08 10:02:12 +01001647psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1648{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001649 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001650 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001651 /* The object has (apparently) been initialized but it is not
1652 * in use. It's ok to call abort on such an object, and there's
1653 * nothing to do. */
1654 return( PSA_SUCCESS );
1655 }
1656 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001657#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001658 if( operation->alg == PSA_ALG_CMAC )
1659 {
1660 mbedtls_cipher_free( &operation->ctx.cmac );
1661 }
1662 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001663#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001664#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001665 if( PSA_ALG_IS_HMAC( operation->alg ) )
1666 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001667 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001668 }
1669 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001670#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001671 {
1672 /* Sanity check (shouldn't happen: operation->alg should
1673 * always have been initialized to a valid value). */
1674 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001675 }
Moran Peker41deec42018-04-04 15:43:05 +03001676
Gilles Peskine8c9def32018-02-08 10:02:12 +01001677 operation->alg = 0;
1678 operation->key_set = 0;
1679 operation->iv_set = 0;
1680 operation->iv_required = 0;
1681 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001682 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001683
Gilles Peskine8c9def32018-02-08 10:02:12 +01001684 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001685
1686bad_state:
1687 /* If abort is called on an uninitialized object, we can't trust
1688 * anything. Wipe the object in case it contains confidential data.
1689 * This may result in a memory leak if a pointer gets overwritten,
1690 * but it's too late to do anything about this. */
1691 memset( operation, 0, sizeof( *operation ) );
1692 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001693}
1694
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001695#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001696static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001697 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001698 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001699 const mbedtls_cipher_info_t *cipher_info )
1700{
1701 int ret;
1702
1703 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001704
1705 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1706 if( ret != 0 )
1707 return( ret );
1708
1709 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1710 slot->data.raw.data,
1711 key_bits );
1712 return( ret );
1713}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001714#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001715
Gilles Peskine248051a2018-06-20 16:09:38 +02001716#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001717static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1718 const uint8_t *key,
1719 size_t key_length,
1720 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001721{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001722 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001723 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001724 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001725 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001726 psa_status_t status;
1727
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001728 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1729 * overflow below. This should never trigger if the hash algorithm
1730 * is implemented correctly. */
1731 /* The size checks against the ipad and opad buffers cannot be written
1732 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1733 * because that triggers -Wlogical-op on GCC 7.3. */
1734 if( block_size > sizeof( ipad ) )
1735 return( PSA_ERROR_NOT_SUPPORTED );
1736 if( block_size > sizeof( hmac->opad ) )
1737 return( PSA_ERROR_NOT_SUPPORTED );
1738 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001739 return( PSA_ERROR_NOT_SUPPORTED );
1740
Gilles Peskined223b522018-06-11 18:12:58 +02001741 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001742 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001743 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001744 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001745 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001746 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001747 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001748 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001749 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001750 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001751 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001752 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001753 }
Gilles Peskine96889972018-07-12 17:07:03 +02001754 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1755 * but it is permitted. It is common when HMAC is used in HKDF, for
1756 * example. Don't call `memcpy` in the 0-length because `key` could be
1757 * an invalid pointer which would make the behavior undefined. */
1758 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001759 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001760
Gilles Peskined223b522018-06-11 18:12:58 +02001761 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1762 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001763 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001764 ipad[i] ^= 0x36;
1765 memset( ipad + key_length, 0x36, block_size - key_length );
1766
1767 /* Copy the key material from ipad to opad, flipping the requisite bits,
1768 * and filling the rest of opad with the requisite constant. */
1769 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001770 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1771 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001772
Gilles Peskine01126fa2018-07-12 17:04:55 +02001773 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001774 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001775 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001776
Gilles Peskine01126fa2018-07-12 17:04:55 +02001777 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001778
1779cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001780 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001781
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001782 return( status );
1783}
Gilles Peskine248051a2018-06-20 16:09:38 +02001784#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001785
Gilles Peskine89167cb2018-07-08 20:12:23 +02001786static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001787 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001788 psa_algorithm_t alg,
1789 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001790{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001791 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001792 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001793 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001794 psa_key_usage_t usage =
1795 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001796 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001797 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001798
Gilles Peskined911eb72018-08-14 15:18:45 +02001799 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001800 if( status != PSA_SUCCESS )
1801 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001802 if( is_sign )
1803 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001804
Gilles Peskinec5487a82018-12-03 18:08:14 +01001805 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001806 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001807 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001808 key_bits = psa_get_key_bits( slot );
1809
Gilles Peskine8c9def32018-02-08 10:02:12 +01001810#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001811 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001812 {
1813 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001814 mbedtls_cipher_info_from_psa( full_length_alg,
1815 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001816 int ret;
1817 if( cipher_info == NULL )
1818 {
1819 status = PSA_ERROR_NOT_SUPPORTED;
1820 goto exit;
1821 }
1822 operation->mac_size = cipher_info->block_size;
1823 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1824 status = mbedtls_to_psa_error( ret );
1825 }
1826 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001827#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001828#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001829 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001830 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001831 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001832 if( hash_alg == 0 )
1833 {
1834 status = PSA_ERROR_NOT_SUPPORTED;
1835 goto exit;
1836 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001837
1838 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1839 /* Sanity check. This shouldn't fail on a valid configuration. */
1840 if( operation->mac_size == 0 ||
1841 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1842 {
1843 status = PSA_ERROR_NOT_SUPPORTED;
1844 goto exit;
1845 }
1846
Gilles Peskine01126fa2018-07-12 17:04:55 +02001847 if( slot->type != PSA_KEY_TYPE_HMAC )
1848 {
1849 status = PSA_ERROR_INVALID_ARGUMENT;
1850 goto exit;
1851 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001852
Gilles Peskine01126fa2018-07-12 17:04:55 +02001853 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1854 slot->data.raw.data,
1855 slot->data.raw.bytes,
1856 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001857 }
1858 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001859#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001860 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00001861 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02001862 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001863 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001864
Gilles Peskined911eb72018-08-14 15:18:45 +02001865 if( truncated == 0 )
1866 {
1867 /* The "normal" case: untruncated algorithm. Nothing to do. */
1868 }
1869 else if( truncated < 4 )
1870 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001871 /* A very short MAC is too short for security since it can be
1872 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1873 * so we make this our minimum, even though 32 bits is still
1874 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001875 status = PSA_ERROR_NOT_SUPPORTED;
1876 }
1877 else if( truncated > operation->mac_size )
1878 {
1879 /* It's impossible to "truncate" to a larger length. */
1880 status = PSA_ERROR_INVALID_ARGUMENT;
1881 }
1882 else
1883 operation->mac_size = truncated;
1884
Gilles Peskinefbfac682018-07-08 20:51:54 +02001885exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001886 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001887 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001888 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001889 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001890 else
1891 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001892 operation->key_set = 1;
1893 }
1894 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001895}
1896
Gilles Peskine89167cb2018-07-08 20:12:23 +02001897psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001898 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001899 psa_algorithm_t alg )
1900{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001901 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001902}
1903
1904psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001905 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001906 psa_algorithm_t alg )
1907{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001908 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001909}
1910
Gilles Peskine8c9def32018-02-08 10:02:12 +01001911psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1912 const uint8_t *input,
1913 size_t input_length )
1914{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001915 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001916 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001917 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001918 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001919 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001920 operation->has_input = 1;
1921
Gilles Peskine8c9def32018-02-08 10:02:12 +01001922#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001923 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001924 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001925 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1926 input, input_length );
1927 status = mbedtls_to_psa_error( ret );
1928 }
1929 else
1930#endif /* MBEDTLS_CMAC_C */
1931#if defined(MBEDTLS_MD_C)
1932 if( PSA_ALG_IS_HMAC( operation->alg ) )
1933 {
1934 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1935 input_length );
1936 }
1937 else
1938#endif /* MBEDTLS_MD_C */
1939 {
1940 /* This shouldn't happen if `operation` was initialized by
1941 * a setup function. */
1942 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001943 }
1944
Gilles Peskinefbfac682018-07-08 20:51:54 +02001945cleanup:
1946 if( status != PSA_SUCCESS )
1947 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001948 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001949}
1950
Gilles Peskine01126fa2018-07-12 17:04:55 +02001951#if defined(MBEDTLS_MD_C)
1952static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1953 uint8_t *mac,
1954 size_t mac_size )
1955{
1956 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1957 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1958 size_t hash_size = 0;
1959 size_t block_size = psa_get_hash_block_size( hash_alg );
1960 psa_status_t status;
1961
Gilles Peskine01126fa2018-07-12 17:04:55 +02001962 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1963 if( status != PSA_SUCCESS )
1964 return( status );
1965 /* From here on, tmp needs to be wiped. */
1966
1967 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1968 if( status != PSA_SUCCESS )
1969 goto exit;
1970
1971 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1972 if( status != PSA_SUCCESS )
1973 goto exit;
1974
1975 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1976 if( status != PSA_SUCCESS )
1977 goto exit;
1978
Gilles Peskined911eb72018-08-14 15:18:45 +02001979 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1980 if( status != PSA_SUCCESS )
1981 goto exit;
1982
1983 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001984
1985exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001986 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001987 return( status );
1988}
1989#endif /* MBEDTLS_MD_C */
1990
mohammad16036df908f2018-04-02 08:34:15 -07001991static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001992 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001993 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001994{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001995 if( ! operation->key_set )
1996 return( PSA_ERROR_BAD_STATE );
1997 if( operation->iv_required && ! operation->iv_set )
1998 return( PSA_ERROR_BAD_STATE );
1999
Gilles Peskine8c9def32018-02-08 10:02:12 +01002000 if( mac_size < operation->mac_size )
2001 return( PSA_ERROR_BUFFER_TOO_SMALL );
2002
Gilles Peskine8c9def32018-02-08 10:02:12 +01002003#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002004 if( operation->alg == PSA_ALG_CMAC )
2005 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002006 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2007 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2008 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002009 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002010 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002011 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002012 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002013 else
2014#endif /* MBEDTLS_CMAC_C */
2015#if defined(MBEDTLS_MD_C)
2016 if( PSA_ALG_IS_HMAC( operation->alg ) )
2017 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002018 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002019 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002020 }
2021 else
2022#endif /* MBEDTLS_MD_C */
2023 {
2024 /* This shouldn't happen if `operation` was initialized by
2025 * a setup function. */
2026 return( PSA_ERROR_BAD_STATE );
2027 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002028}
2029
Gilles Peskineacd4be32018-07-08 19:56:25 +02002030psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2031 uint8_t *mac,
2032 size_t mac_size,
2033 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002034{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002035 psa_status_t status;
2036
2037 /* Fill the output buffer with something that isn't a valid mac
2038 * (barring an attack on the mac and deliberately-crafted input),
2039 * in case the caller doesn't check the return status properly. */
2040 *mac_length = mac_size;
2041 /* If mac_size is 0 then mac may be NULL and then the
2042 * call to memset would have undefined behavior. */
2043 if( mac_size != 0 )
2044 memset( mac, '!', mac_size );
2045
Gilles Peskine89167cb2018-07-08 20:12:23 +02002046 if( ! operation->is_sign )
2047 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002048 status = PSA_ERROR_BAD_STATE;
2049 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002050 }
mohammad16036df908f2018-04-02 08:34:15 -07002051
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002052 status = psa_mac_finish_internal( operation, mac, mac_size );
2053
2054cleanup:
2055 if( status == PSA_SUCCESS )
2056 {
2057 status = psa_mac_abort( operation );
2058 if( status == PSA_SUCCESS )
2059 *mac_length = operation->mac_size;
2060 else
2061 memset( mac, '!', mac_size );
2062 }
2063 else
2064 psa_mac_abort( operation );
2065 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002066}
2067
Gilles Peskineacd4be32018-07-08 19:56:25 +02002068psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2069 const uint8_t *mac,
2070 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002071{
Gilles Peskine828ed142018-06-18 23:25:51 +02002072 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002073 psa_status_t status;
2074
Gilles Peskine89167cb2018-07-08 20:12:23 +02002075 if( operation->is_sign )
2076 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002077 status = PSA_ERROR_BAD_STATE;
2078 goto cleanup;
2079 }
2080 if( operation->mac_size != mac_length )
2081 {
2082 status = PSA_ERROR_INVALID_SIGNATURE;
2083 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002084 }
mohammad16036df908f2018-04-02 08:34:15 -07002085
2086 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002087 actual_mac, sizeof( actual_mac ) );
2088
2089 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2090 status = PSA_ERROR_INVALID_SIGNATURE;
2091
2092cleanup:
2093 if( status == PSA_SUCCESS )
2094 status = psa_mac_abort( operation );
2095 else
2096 psa_mac_abort( operation );
2097
Gilles Peskine3f108122018-12-07 18:14:53 +01002098 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002099
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002100 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002101}
2102
2103
Gilles Peskine20035e32018-02-03 22:44:14 +01002104
Gilles Peskine20035e32018-02-03 22:44:14 +01002105/****************************************************************/
2106/* Asymmetric cryptography */
2107/****************************************************************/
2108
Gilles Peskine2b450e32018-06-27 15:42:46 +02002109#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002110/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002111 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002112static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2113 size_t hash_length,
2114 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002115{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002116 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002117 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002118 *md_alg = mbedtls_md_get_type( md_info );
2119
2120 /* The Mbed TLS RSA module uses an unsigned int for hash length
2121 * parameters. Validate that it fits so that we don't risk an
2122 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002123#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002124 if( hash_length > UINT_MAX )
2125 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002126#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002127
2128#if defined(MBEDTLS_PKCS1_V15)
2129 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2130 * must be correct. */
2131 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2132 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002133 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002134 if( md_info == NULL )
2135 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002136 if( mbedtls_md_get_size( md_info ) != hash_length )
2137 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002138 }
2139#endif /* MBEDTLS_PKCS1_V15 */
2140
2141#if defined(MBEDTLS_PKCS1_V21)
2142 /* PSS requires a hash internally. */
2143 if( PSA_ALG_IS_RSA_PSS( alg ) )
2144 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002145 if( md_info == NULL )
2146 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002147 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002148#endif /* MBEDTLS_PKCS1_V21 */
2149
Gilles Peskine61b91d42018-06-08 16:09:36 +02002150 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002151}
2152
Gilles Peskine2b450e32018-06-27 15:42:46 +02002153static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2154 psa_algorithm_t alg,
2155 const uint8_t *hash,
2156 size_t hash_length,
2157 uint8_t *signature,
2158 size_t signature_size,
2159 size_t *signature_length )
2160{
2161 psa_status_t status;
2162 int ret;
2163 mbedtls_md_type_t md_alg;
2164
2165 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2166 if( status != PSA_SUCCESS )
2167 return( status );
2168
Gilles Peskine630a18a2018-06-29 17:49:35 +02002169 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002170 return( PSA_ERROR_BUFFER_TOO_SMALL );
2171
2172#if defined(MBEDTLS_PKCS1_V15)
2173 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2174 {
2175 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2176 MBEDTLS_MD_NONE );
2177 ret = mbedtls_rsa_pkcs1_sign( rsa,
2178 mbedtls_ctr_drbg_random,
2179 &global_data.ctr_drbg,
2180 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002181 md_alg,
2182 (unsigned int) hash_length,
2183 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002184 signature );
2185 }
2186 else
2187#endif /* MBEDTLS_PKCS1_V15 */
2188#if defined(MBEDTLS_PKCS1_V21)
2189 if( PSA_ALG_IS_RSA_PSS( alg ) )
2190 {
2191 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2192 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2193 mbedtls_ctr_drbg_random,
2194 &global_data.ctr_drbg,
2195 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002196 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002197 (unsigned int) hash_length,
2198 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002199 signature );
2200 }
2201 else
2202#endif /* MBEDTLS_PKCS1_V21 */
2203 {
2204 return( PSA_ERROR_INVALID_ARGUMENT );
2205 }
2206
2207 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002208 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002209 return( mbedtls_to_psa_error( ret ) );
2210}
2211
2212static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2213 psa_algorithm_t alg,
2214 const uint8_t *hash,
2215 size_t hash_length,
2216 const uint8_t *signature,
2217 size_t signature_length )
2218{
2219 psa_status_t status;
2220 int ret;
2221 mbedtls_md_type_t md_alg;
2222
2223 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2224 if( status != PSA_SUCCESS )
2225 return( status );
2226
Gilles Peskine630a18a2018-06-29 17:49:35 +02002227 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002228 return( PSA_ERROR_BUFFER_TOO_SMALL );
2229
2230#if defined(MBEDTLS_PKCS1_V15)
2231 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2232 {
2233 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2234 MBEDTLS_MD_NONE );
2235 ret = mbedtls_rsa_pkcs1_verify( rsa,
2236 mbedtls_ctr_drbg_random,
2237 &global_data.ctr_drbg,
2238 MBEDTLS_RSA_PUBLIC,
2239 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002240 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002241 hash,
2242 signature );
2243 }
2244 else
2245#endif /* MBEDTLS_PKCS1_V15 */
2246#if defined(MBEDTLS_PKCS1_V21)
2247 if( PSA_ALG_IS_RSA_PSS( alg ) )
2248 {
2249 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2250 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2251 mbedtls_ctr_drbg_random,
2252 &global_data.ctr_drbg,
2253 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002254 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002255 (unsigned int) hash_length,
2256 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002257 signature );
2258 }
2259 else
2260#endif /* MBEDTLS_PKCS1_V21 */
2261 {
2262 return( PSA_ERROR_INVALID_ARGUMENT );
2263 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002264
2265 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2266 * the rest of the signature is invalid". This has little use in
2267 * practice and PSA doesn't report this distinction. */
2268 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2269 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002270 return( mbedtls_to_psa_error( ret ) );
2271}
2272#endif /* MBEDTLS_RSA_C */
2273
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002274#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002275/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2276 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2277 * (even though these functions don't modify it). */
2278static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2279 psa_algorithm_t alg,
2280 const uint8_t *hash,
2281 size_t hash_length,
2282 uint8_t *signature,
2283 size_t signature_size,
2284 size_t *signature_length )
2285{
2286 int ret;
2287 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002288 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002289 mbedtls_mpi_init( &r );
2290 mbedtls_mpi_init( &s );
2291
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002292 if( signature_size < 2 * curve_bytes )
2293 {
2294 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2295 goto cleanup;
2296 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002297
Gilles Peskinea05219c2018-11-16 16:02:56 +01002298#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002299 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2300 {
2301 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2302 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2303 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2304 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2305 hash, hash_length,
2306 md_alg ) );
2307 }
2308 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002309#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002310 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002311 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002312 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2313 hash, hash_length,
2314 mbedtls_ctr_drbg_random,
2315 &global_data.ctr_drbg ) );
2316 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002317
2318 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2319 signature,
2320 curve_bytes ) );
2321 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2322 signature + curve_bytes,
2323 curve_bytes ) );
2324
2325cleanup:
2326 mbedtls_mpi_free( &r );
2327 mbedtls_mpi_free( &s );
2328 if( ret == 0 )
2329 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002330 return( mbedtls_to_psa_error( ret ) );
2331}
2332
2333static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2334 const uint8_t *hash,
2335 size_t hash_length,
2336 const uint8_t *signature,
2337 size_t signature_length )
2338{
2339 int ret;
2340 mbedtls_mpi r, s;
2341 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2342 mbedtls_mpi_init( &r );
2343 mbedtls_mpi_init( &s );
2344
2345 if( signature_length != 2 * curve_bytes )
2346 return( PSA_ERROR_INVALID_SIGNATURE );
2347
2348 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2349 signature,
2350 curve_bytes ) );
2351 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2352 signature + curve_bytes,
2353 curve_bytes ) );
2354
2355 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2356 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002357
2358cleanup:
2359 mbedtls_mpi_free( &r );
2360 mbedtls_mpi_free( &s );
2361 return( mbedtls_to_psa_error( ret ) );
2362}
2363#endif /* MBEDTLS_ECDSA_C */
2364
Gilles Peskinec5487a82018-12-03 18:08:14 +01002365psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002366 psa_algorithm_t alg,
2367 const uint8_t *hash,
2368 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002369 uint8_t *signature,
2370 size_t signature_size,
2371 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002372{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002373 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002374 psa_status_t status;
2375
2376 *signature_length = signature_size;
2377
Gilles Peskinec5487a82018-12-03 18:08:14 +01002378 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002379 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002380 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002381 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002382 {
2383 status = PSA_ERROR_INVALID_ARGUMENT;
2384 goto exit;
2385 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002386
Gilles Peskine20035e32018-02-03 22:44:14 +01002387#if defined(MBEDTLS_RSA_C)
2388 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2389 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002390 status = psa_rsa_sign( slot->data.rsa,
2391 alg,
2392 hash, hash_length,
2393 signature, signature_size,
2394 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002395 }
2396 else
2397#endif /* defined(MBEDTLS_RSA_C) */
2398#if defined(MBEDTLS_ECP_C)
2399 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2400 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002401#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002402 if(
2403#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2404 PSA_ALG_IS_ECDSA( alg )
2405#else
2406 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2407#endif
2408 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002409 status = psa_ecdsa_sign( slot->data.ecp,
2410 alg,
2411 hash, hash_length,
2412 signature, signature_size,
2413 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002414 else
2415#endif /* defined(MBEDTLS_ECDSA_C) */
2416 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002417 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002418 }
itayzafrir5c753392018-05-08 11:18:38 +03002419 }
2420 else
2421#endif /* defined(MBEDTLS_ECP_C) */
2422 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002423 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002424 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002425
2426exit:
2427 /* Fill the unused part of the output buffer (the whole buffer on error,
2428 * the trailing part on success) with something that isn't a valid mac
2429 * (barring an attack on the mac and deliberately-crafted input),
2430 * in case the caller doesn't check the return status properly. */
2431 if( status == PSA_SUCCESS )
2432 memset( signature + *signature_length, '!',
2433 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002434 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002435 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002436 /* If signature_size is 0 then we have nothing to do. We must not call
2437 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002438 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002439}
2440
Gilles Peskinec5487a82018-12-03 18:08:14 +01002441psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002442 psa_algorithm_t alg,
2443 const uint8_t *hash,
2444 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002445 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002446 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002447{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002448 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002449 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002450
Gilles Peskinec5487a82018-12-03 18:08:14 +01002451 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002452 if( status != PSA_SUCCESS )
2453 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002454
Gilles Peskine61b91d42018-06-08 16:09:36 +02002455#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002456 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002457 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002458 return( psa_rsa_verify( slot->data.rsa,
2459 alg,
2460 hash, hash_length,
2461 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002462 }
2463 else
2464#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002465#if defined(MBEDTLS_ECP_C)
2466 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2467 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002468#if defined(MBEDTLS_ECDSA_C)
2469 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002470 return( psa_ecdsa_verify( slot->data.ecp,
2471 hash, hash_length,
2472 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002473 else
2474#endif /* defined(MBEDTLS_ECDSA_C) */
2475 {
2476 return( PSA_ERROR_INVALID_ARGUMENT );
2477 }
itayzafrir5c753392018-05-08 11:18:38 +03002478 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002479 else
2480#endif /* defined(MBEDTLS_ECP_C) */
2481 {
2482 return( PSA_ERROR_NOT_SUPPORTED );
2483 }
2484}
2485
Gilles Peskine072ac562018-06-30 00:21:29 +02002486#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2487static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2488 mbedtls_rsa_context *rsa )
2489{
2490 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2491 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2492 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2493 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2494}
2495#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2496
Gilles Peskinec5487a82018-12-03 18:08:14 +01002497psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002498 psa_algorithm_t alg,
2499 const uint8_t *input,
2500 size_t input_length,
2501 const uint8_t *salt,
2502 size_t salt_length,
2503 uint8_t *output,
2504 size_t output_size,
2505 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002506{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002507 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002508 psa_status_t status;
2509
Darryl Green5cc689a2018-07-24 15:34:10 +01002510 (void) input;
2511 (void) input_length;
2512 (void) salt;
2513 (void) output;
2514 (void) output_size;
2515
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002516 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002517
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002518 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2519 return( PSA_ERROR_INVALID_ARGUMENT );
2520
Gilles Peskinec5487a82018-12-03 18:08:14 +01002521 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002522 if( status != PSA_SUCCESS )
2523 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002524 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2525 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002526 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002527
2528#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002529 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002530 {
2531 mbedtls_rsa_context *rsa = slot->data.rsa;
2532 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002533 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002534 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002535#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002536 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002537 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002538 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2539 mbedtls_ctr_drbg_random,
2540 &global_data.ctr_drbg,
2541 MBEDTLS_RSA_PUBLIC,
2542 input_length,
2543 input,
2544 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002545 }
2546 else
2547#endif /* MBEDTLS_PKCS1_V15 */
2548#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002549 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002550 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002551 psa_rsa_oaep_set_padding_mode( alg, rsa );
2552 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2553 mbedtls_ctr_drbg_random,
2554 &global_data.ctr_drbg,
2555 MBEDTLS_RSA_PUBLIC,
2556 salt, salt_length,
2557 input_length,
2558 input,
2559 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002560 }
2561 else
2562#endif /* MBEDTLS_PKCS1_V21 */
2563 {
2564 return( PSA_ERROR_INVALID_ARGUMENT );
2565 }
2566 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002567 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002568 return( mbedtls_to_psa_error( ret ) );
2569 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002570 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002571#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002572 {
2573 return( PSA_ERROR_NOT_SUPPORTED );
2574 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002575}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002576
Gilles Peskinec5487a82018-12-03 18:08:14 +01002577psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002578 psa_algorithm_t alg,
2579 const uint8_t *input,
2580 size_t input_length,
2581 const uint8_t *salt,
2582 size_t salt_length,
2583 uint8_t *output,
2584 size_t output_size,
2585 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002586{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002587 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002588 psa_status_t status;
2589
Darryl Green5cc689a2018-07-24 15:34:10 +01002590 (void) input;
2591 (void) input_length;
2592 (void) salt;
2593 (void) output;
2594 (void) output_size;
2595
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002596 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002597
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002598 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2599 return( PSA_ERROR_INVALID_ARGUMENT );
2600
Gilles Peskinec5487a82018-12-03 18:08:14 +01002601 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002602 if( status != PSA_SUCCESS )
2603 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002604 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2605 return( PSA_ERROR_INVALID_ARGUMENT );
2606
2607#if defined(MBEDTLS_RSA_C)
2608 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2609 {
2610 mbedtls_rsa_context *rsa = slot->data.rsa;
2611 int ret;
2612
Gilles Peskine630a18a2018-06-29 17:49:35 +02002613 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002614 return( PSA_ERROR_INVALID_ARGUMENT );
2615
2616#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002617 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002618 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002619 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2620 mbedtls_ctr_drbg_random,
2621 &global_data.ctr_drbg,
2622 MBEDTLS_RSA_PRIVATE,
2623 output_length,
2624 input,
2625 output,
2626 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002627 }
2628 else
2629#endif /* MBEDTLS_PKCS1_V15 */
2630#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002631 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002632 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002633 psa_rsa_oaep_set_padding_mode( alg, rsa );
2634 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2635 mbedtls_ctr_drbg_random,
2636 &global_data.ctr_drbg,
2637 MBEDTLS_RSA_PRIVATE,
2638 salt, salt_length,
2639 output_length,
2640 input,
2641 output,
2642 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002643 }
2644 else
2645#endif /* MBEDTLS_PKCS1_V21 */
2646 {
2647 return( PSA_ERROR_INVALID_ARGUMENT );
2648 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002649
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002650 return( mbedtls_to_psa_error( ret ) );
2651 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002652 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002653#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002654 {
2655 return( PSA_ERROR_NOT_SUPPORTED );
2656 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002657}
Gilles Peskine20035e32018-02-03 22:44:14 +01002658
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002659
2660
mohammad1603503973b2018-03-12 15:59:30 +02002661/****************************************************************/
2662/* Symmetric cryptography */
2663/****************************************************************/
2664
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002665/* Initialize the cipher operation structure. Once this function has been
2666 * called, psa_cipher_abort can run and will do the right thing. */
2667static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2668 psa_algorithm_t alg )
2669{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002670 if( ! PSA_ALG_IS_CIPHER( alg ) )
2671 {
2672 memset( operation, 0, sizeof( *operation ) );
2673 return( PSA_ERROR_INVALID_ARGUMENT );
2674 }
2675
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002676 operation->alg = alg;
2677 operation->key_set = 0;
2678 operation->iv_set = 0;
2679 operation->iv_required = 1;
2680 operation->iv_size = 0;
2681 operation->block_size = 0;
2682 mbedtls_cipher_init( &operation->ctx.cipher );
2683 return( PSA_SUCCESS );
2684}
2685
Gilles Peskinee553c652018-06-04 16:22:46 +02002686static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002687 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002688 psa_algorithm_t alg,
2689 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002690{
2691 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2692 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002693 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002694 size_t key_bits;
2695 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002696 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2697 PSA_KEY_USAGE_ENCRYPT :
2698 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002699
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002700 status = psa_cipher_init( operation, alg );
2701 if( status != PSA_SUCCESS )
2702 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002703
Gilles Peskinec5487a82018-12-03 18:08:14 +01002704 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002705 if( status != PSA_SUCCESS )
2706 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002707 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002708
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002709 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002710 if( cipher_info == NULL )
2711 return( PSA_ERROR_NOT_SUPPORTED );
2712
mohammad1603503973b2018-03-12 15:59:30 +02002713 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002714 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002715 {
2716 psa_cipher_abort( operation );
2717 return( mbedtls_to_psa_error( ret ) );
2718 }
2719
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002720#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002721 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002722 {
2723 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2724 unsigned char keys[24];
2725 memcpy( keys, slot->data.raw.data, 16 );
2726 memcpy( keys + 16, slot->data.raw.data, 8 );
2727 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2728 keys,
2729 192, cipher_operation );
2730 }
2731 else
2732#endif
2733 {
2734 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2735 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002736 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002737 }
Moran Peker41deec42018-04-04 15:43:05 +03002738 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002739 {
2740 psa_cipher_abort( operation );
2741 return( mbedtls_to_psa_error( ret ) );
2742 }
2743
mohammad16038481e742018-03-18 13:57:31 +02002744#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002745 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002746 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002747 case PSA_ALG_CBC_NO_PADDING:
2748 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2749 MBEDTLS_PADDING_NONE );
2750 break;
2751 case PSA_ALG_CBC_PKCS7:
2752 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2753 MBEDTLS_PADDING_PKCS7 );
2754 break;
2755 default:
2756 /* The algorithm doesn't involve padding. */
2757 ret = 0;
2758 break;
2759 }
2760 if( ret != 0 )
2761 {
2762 psa_cipher_abort( operation );
2763 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002764 }
2765#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2766
mohammad1603503973b2018-03-12 15:59:30 +02002767 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002768 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2769 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2770 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002771 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002772 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002773 }
mohammad1603503973b2018-03-12 15:59:30 +02002774
Moran Peker395db872018-05-31 14:07:14 +03002775 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002776}
2777
Gilles Peskinefe119512018-07-08 21:39:34 +02002778psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002779 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002780 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002781{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002782 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002783}
2784
Gilles Peskinefe119512018-07-08 21:39:34 +02002785psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002786 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002787 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002788{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002789 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002790}
2791
Gilles Peskinefe119512018-07-08 21:39:34 +02002792psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2793 unsigned char *iv,
2794 size_t iv_size,
2795 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002796{
itayzafrir534bd7c2018-08-02 13:56:32 +03002797 psa_status_t status;
2798 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002799 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002800 {
2801 status = PSA_ERROR_BAD_STATE;
2802 goto exit;
2803 }
Moran Peker41deec42018-04-04 15:43:05 +03002804 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002805 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002806 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002807 goto exit;
2808 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002809 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2810 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002811 if( ret != 0 )
2812 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002813 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002814 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002815 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002816
mohammad16038481e742018-03-18 13:57:31 +02002817 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002818 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002819
Moran Peker395db872018-05-31 14:07:14 +03002820exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002821 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002822 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002823 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002824}
2825
Gilles Peskinefe119512018-07-08 21:39:34 +02002826psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2827 const unsigned char *iv,
2828 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002829{
itayzafrir534bd7c2018-08-02 13:56:32 +03002830 psa_status_t status;
2831 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002832 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002833 {
2834 status = PSA_ERROR_BAD_STATE;
2835 goto exit;
2836 }
Moran Pekera28258c2018-05-29 16:25:04 +03002837 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002838 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002839 status = PSA_ERROR_INVALID_ARGUMENT;
2840 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002841 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002842 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2843 status = mbedtls_to_psa_error( ret );
2844exit:
2845 if( status == PSA_SUCCESS )
2846 operation->iv_set = 1;
2847 else
mohammad1603503973b2018-03-12 15:59:30 +02002848 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002849 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002850}
2851
Gilles Peskinee553c652018-06-04 16:22:46 +02002852psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2853 const uint8_t *input,
2854 size_t input_length,
2855 unsigned char *output,
2856 size_t output_size,
2857 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002858{
itayzafrir534bd7c2018-08-02 13:56:32 +03002859 psa_status_t status;
2860 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002861 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002862 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002863 {
2864 /* Take the unprocessed partial block left over from previous
2865 * update calls, if any, plus the input to this call. Remove
2866 * the last partial block, if any. You get the data that will be
2867 * output in this call. */
2868 expected_output_size =
2869 ( operation->ctx.cipher.unprocessed_len + input_length )
2870 / operation->block_size * operation->block_size;
2871 }
2872 else
2873 {
2874 expected_output_size = input_length;
2875 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002876
Gilles Peskine89d789c2018-06-04 17:17:16 +02002877 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002878 {
2879 status = PSA_ERROR_BUFFER_TOO_SMALL;
2880 goto exit;
2881 }
mohammad160382759612018-03-12 18:16:40 +02002882
mohammad1603503973b2018-03-12 15:59:30 +02002883 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002884 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002885 status = mbedtls_to_psa_error( ret );
2886exit:
2887 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002888 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002889 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002890}
2891
Gilles Peskinee553c652018-06-04 16:22:46 +02002892psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2893 uint8_t *output,
2894 size_t output_size,
2895 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002896{
Janos Follath315b51c2018-07-09 16:04:51 +01002897 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2898 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002899 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002900
mohammad1603503973b2018-03-12 15:59:30 +02002901 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002902 {
Janos Follath315b51c2018-07-09 16:04:51 +01002903 status = PSA_ERROR_BAD_STATE;
2904 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002905 }
2906 if( operation->iv_required && ! operation->iv_set )
2907 {
Janos Follath315b51c2018-07-09 16:04:51 +01002908 status = PSA_ERROR_BAD_STATE;
2909 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002910 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002911
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002912 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002913 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2914 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002915 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002916 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002917 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002918 }
2919
Janos Follath315b51c2018-07-09 16:04:51 +01002920 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2921 temp_output_buffer,
2922 output_length );
2923 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002924 {
Janos Follath315b51c2018-07-09 16:04:51 +01002925 status = mbedtls_to_psa_error( cipher_ret );
2926 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002927 }
Janos Follath315b51c2018-07-09 16:04:51 +01002928
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002929 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002930 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002931 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002932 memcpy( output, temp_output_buffer, *output_length );
2933 else
2934 {
Janos Follath315b51c2018-07-09 16:04:51 +01002935 status = PSA_ERROR_BUFFER_TOO_SMALL;
2936 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002937 }
mohammad1603503973b2018-03-12 15:59:30 +02002938
Gilles Peskine3f108122018-12-07 18:14:53 +01002939 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002940 status = psa_cipher_abort( operation );
2941
2942 return( status );
2943
2944error:
2945
2946 *output_length = 0;
2947
Gilles Peskine3f108122018-12-07 18:14:53 +01002948 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002949 (void) psa_cipher_abort( operation );
2950
2951 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002952}
2953
Gilles Peskinee553c652018-06-04 16:22:46 +02002954psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2955{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002956 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002957 {
2958 /* The object has (apparently) been initialized but it is not
2959 * in use. It's ok to call abort on such an object, and there's
2960 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002961 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002962 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002963
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002964 /* Sanity check (shouldn't happen: operation->alg should
2965 * always have been initialized to a valid value). */
2966 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2967 return( PSA_ERROR_BAD_STATE );
2968
mohammad1603503973b2018-03-12 15:59:30 +02002969 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002970
Moran Peker41deec42018-04-04 15:43:05 +03002971 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002972 operation->key_set = 0;
2973 operation->iv_set = 0;
2974 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002975 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002976 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002977
Moran Peker395db872018-05-31 14:07:14 +03002978 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002979}
2980
Gilles Peskinea0655c32018-04-30 17:06:50 +02002981
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002982
mohammad16038cc1cee2018-03-28 01:21:33 +03002983/****************************************************************/
2984/* Key Policy */
2985/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002986
mohammad160327010052018-07-03 13:16:15 +03002987#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002988void psa_key_policy_set_usage( psa_key_policy_t *policy,
2989 psa_key_usage_t usage,
2990 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002991{
mohammad16034eed7572018-03-28 05:14:59 -07002992 policy->usage = usage;
2993 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002994}
2995
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002996psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002997{
mohammad16036df908f2018-04-02 08:34:15 -07002998 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002999}
3000
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003001psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003002{
mohammad16036df908f2018-04-02 08:34:15 -07003003 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003004}
mohammad160327010052018-07-03 13:16:15 +03003005#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003006
Gilles Peskinec5487a82018-12-03 18:08:14 +01003007psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003008 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003009{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003010 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003011 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003012
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003013 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003014 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003015
Gilles Peskinec5487a82018-12-03 18:08:14 +01003016 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003017 if( status != PSA_SUCCESS )
3018 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003019
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003020 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3021 PSA_KEY_USAGE_ENCRYPT |
3022 PSA_KEY_USAGE_DECRYPT |
3023 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003024 PSA_KEY_USAGE_VERIFY |
3025 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003026 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003027
mohammad16036df908f2018-04-02 08:34:15 -07003028 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003029
3030 return( PSA_SUCCESS );
3031}
3032
Gilles Peskinec5487a82018-12-03 18:08:14 +01003033psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003034 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003035{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003036 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003037 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003038
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003039 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003040 return( PSA_ERROR_INVALID_ARGUMENT );
3041
Gilles Peskinec5487a82018-12-03 18:08:14 +01003042 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003043 if( status != PSA_SUCCESS )
3044 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003045
mohammad16036df908f2018-04-02 08:34:15 -07003046 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003047
3048 return( PSA_SUCCESS );
3049}
Gilles Peskine20035e32018-02-03 22:44:14 +01003050
Gilles Peskinea0655c32018-04-30 17:06:50 +02003051
3052
mohammad1603804cd712018-03-20 22:44:08 +02003053/****************************************************************/
3054/* Key Lifetime */
3055/****************************************************************/
3056
Gilles Peskinec5487a82018-12-03 18:08:14 +01003057psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003058 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003059{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003060 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003061 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003062
Gilles Peskinec5487a82018-12-03 18:08:14 +01003063 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003064 if( status != PSA_SUCCESS )
3065 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003066
mohammad1603804cd712018-03-20 22:44:08 +02003067 *lifetime = slot->lifetime;
3068
3069 return( PSA_SUCCESS );
3070}
3071
Gilles Peskine20035e32018-02-03 22:44:14 +01003072
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003073
mohammad16035955c982018-04-26 00:53:03 +03003074/****************************************************************/
3075/* AEAD */
3076/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003077
Gilles Peskineedf9a652018-08-17 18:11:56 +02003078typedef struct
3079{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003080 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003081 const mbedtls_cipher_info_t *cipher_info;
3082 union
3083 {
3084#if defined(MBEDTLS_CCM_C)
3085 mbedtls_ccm_context ccm;
3086#endif /* MBEDTLS_CCM_C */
3087#if defined(MBEDTLS_GCM_C)
3088 mbedtls_gcm_context gcm;
3089#endif /* MBEDTLS_GCM_C */
3090 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003091 psa_algorithm_t core_alg;
3092 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003093 uint8_t tag_length;
3094} aead_operation_t;
3095
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003096static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003097{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003098 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003099 {
3100#if defined(MBEDTLS_CCM_C)
3101 case PSA_ALG_CCM:
3102 mbedtls_ccm_free( &operation->ctx.ccm );
3103 break;
3104#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003105#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003106 case PSA_ALG_GCM:
3107 mbedtls_gcm_free( &operation->ctx.gcm );
3108 break;
3109#endif /* MBEDTLS_GCM_C */
3110 }
3111}
3112
3113static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003114 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003115 psa_key_usage_t usage,
3116 psa_algorithm_t alg )
3117{
3118 psa_status_t status;
3119 size_t key_bits;
3120 mbedtls_cipher_id_t cipher_id;
3121
Gilles Peskinec5487a82018-12-03 18:08:14 +01003122 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003123 if( status != PSA_SUCCESS )
3124 return( status );
3125
3126 key_bits = psa_get_key_bits( operation->slot );
3127
3128 operation->cipher_info =
3129 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3130 &cipher_id );
3131 if( operation->cipher_info == NULL )
3132 return( PSA_ERROR_NOT_SUPPORTED );
3133
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003134 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003135 {
3136#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003137 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3138 operation->core_alg = PSA_ALG_CCM;
3139 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003140 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3141 return( PSA_ERROR_INVALID_ARGUMENT );
3142 mbedtls_ccm_init( &operation->ctx.ccm );
3143 status = mbedtls_to_psa_error(
3144 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3145 operation->slot->data.raw.data,
3146 (unsigned int) key_bits ) );
3147 if( status != 0 )
3148 goto cleanup;
3149 break;
3150#endif /* MBEDTLS_CCM_C */
3151
3152#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003153 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3154 operation->core_alg = PSA_ALG_GCM;
3155 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003156 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3157 return( PSA_ERROR_INVALID_ARGUMENT );
3158 mbedtls_gcm_init( &operation->ctx.gcm );
3159 status = mbedtls_to_psa_error(
3160 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3161 operation->slot->data.raw.data,
3162 (unsigned int) key_bits ) );
3163 break;
3164#endif /* MBEDTLS_GCM_C */
3165
3166 default:
3167 return( PSA_ERROR_NOT_SUPPORTED );
3168 }
3169
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003170 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3171 {
3172 status = PSA_ERROR_INVALID_ARGUMENT;
3173 goto cleanup;
3174 }
3175 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3176 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3177 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3178 * In both cases, mbedtls_xxx will validate the tag length below. */
3179
Gilles Peskineedf9a652018-08-17 18:11:56 +02003180 return( PSA_SUCCESS );
3181
3182cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003183 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003184 return( status );
3185}
3186
Gilles Peskinec5487a82018-12-03 18:08:14 +01003187psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003188 psa_algorithm_t alg,
3189 const uint8_t *nonce,
3190 size_t nonce_length,
3191 const uint8_t *additional_data,
3192 size_t additional_data_length,
3193 const uint8_t *plaintext,
3194 size_t plaintext_length,
3195 uint8_t *ciphertext,
3196 size_t ciphertext_size,
3197 size_t *ciphertext_length )
3198{
mohammad16035955c982018-04-26 00:53:03 +03003199 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003200 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003201 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003202
mohammad1603f08a5502018-06-03 15:05:47 +03003203 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003204
Gilles Peskinec5487a82018-12-03 18:08:14 +01003205 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003206 if( status != PSA_SUCCESS )
3207 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003208
Gilles Peskineedf9a652018-08-17 18:11:56 +02003209 /* For all currently supported modes, the tag is at the end of the
3210 * ciphertext. */
3211 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3212 {
3213 status = PSA_ERROR_BUFFER_TOO_SMALL;
3214 goto exit;
3215 }
3216 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003217
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003218#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003219 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003220 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003221 status = mbedtls_to_psa_error(
3222 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3223 MBEDTLS_GCM_ENCRYPT,
3224 plaintext_length,
3225 nonce, nonce_length,
3226 additional_data, additional_data_length,
3227 plaintext, ciphertext,
3228 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003229 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003230 else
3231#endif /* MBEDTLS_GCM_C */
3232#if defined(MBEDTLS_CCM_C)
3233 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003234 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003235 status = mbedtls_to_psa_error(
3236 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3237 plaintext_length,
3238 nonce, nonce_length,
3239 additional_data,
3240 additional_data_length,
3241 plaintext, ciphertext,
3242 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003243 }
mohammad16035c8845f2018-05-09 05:40:09 -07003244 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003245#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003246 {
mohammad1603554faad2018-06-03 15:07:38 +03003247 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003248 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003249
Gilles Peskineedf9a652018-08-17 18:11:56 +02003250 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3251 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003252
Gilles Peskineedf9a652018-08-17 18:11:56 +02003253exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003254 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003255 if( status == PSA_SUCCESS )
3256 *ciphertext_length = plaintext_length + operation.tag_length;
3257 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003258}
3259
Gilles Peskineee652a32018-06-01 19:23:52 +02003260/* Locate the tag in a ciphertext buffer containing the encrypted data
3261 * followed by the tag. Return the length of the part preceding the tag in
3262 * *plaintext_length. This is the size of the plaintext in modes where
3263 * the encrypted data has the same size as the plaintext, such as
3264 * CCM and GCM. */
3265static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3266 const uint8_t *ciphertext,
3267 size_t ciphertext_length,
3268 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003269 const uint8_t **p_tag )
3270{
3271 size_t payload_length;
3272 if( tag_length > ciphertext_length )
3273 return( PSA_ERROR_INVALID_ARGUMENT );
3274 payload_length = ciphertext_length - tag_length;
3275 if( payload_length > plaintext_size )
3276 return( PSA_ERROR_BUFFER_TOO_SMALL );
3277 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003278 return( PSA_SUCCESS );
3279}
3280
Gilles Peskinec5487a82018-12-03 18:08:14 +01003281psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003282 psa_algorithm_t alg,
3283 const uint8_t *nonce,
3284 size_t nonce_length,
3285 const uint8_t *additional_data,
3286 size_t additional_data_length,
3287 const uint8_t *ciphertext,
3288 size_t ciphertext_length,
3289 uint8_t *plaintext,
3290 size_t plaintext_size,
3291 size_t *plaintext_length )
3292{
mohammad16035955c982018-04-26 00:53:03 +03003293 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003294 aead_operation_t operation;
3295 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003296
Gilles Peskineee652a32018-06-01 19:23:52 +02003297 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003298
Gilles Peskinec5487a82018-12-03 18:08:14 +01003299 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003300 if( status != PSA_SUCCESS )
3301 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003302
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003303#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003304 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003305 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003306 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003307 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003308 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003309 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003310 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003311
Gilles Peskineedf9a652018-08-17 18:11:56 +02003312 status = mbedtls_to_psa_error(
3313 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3314 ciphertext_length - operation.tag_length,
3315 nonce, nonce_length,
3316 additional_data,
3317 additional_data_length,
3318 tag, operation.tag_length,
3319 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003320 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003321 else
3322#endif /* MBEDTLS_GCM_C */
3323#if defined(MBEDTLS_CCM_C)
3324 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003325 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003326 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003327 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003328 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003329 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003330 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003331
Gilles Peskineedf9a652018-08-17 18:11:56 +02003332 status = mbedtls_to_psa_error(
3333 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3334 ciphertext_length - operation.tag_length,
3335 nonce, nonce_length,
3336 additional_data,
3337 additional_data_length,
3338 ciphertext, plaintext,
3339 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003340 }
mohammad160339574652018-06-01 04:39:53 -07003341 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003342#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003343 {
mohammad1603554faad2018-06-03 15:07:38 +03003344 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003345 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003346
Gilles Peskineedf9a652018-08-17 18:11:56 +02003347 if( status != PSA_SUCCESS && plaintext_size != 0 )
3348 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003349
Gilles Peskineedf9a652018-08-17 18:11:56 +02003350exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003351 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003352 if( status == PSA_SUCCESS )
3353 *plaintext_length = ciphertext_length - operation.tag_length;
3354 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003355}
3356
Gilles Peskinea0655c32018-04-30 17:06:50 +02003357
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003358
Gilles Peskine20035e32018-02-03 22:44:14 +01003359/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003360/* Generators */
3361/****************************************************************/
3362
3363psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3364{
3365 psa_status_t status = PSA_SUCCESS;
3366 if( generator->alg == 0 )
3367 {
3368 /* The object has (apparently) been initialized but it is not
3369 * in use. It's ok to call abort on such an object, and there's
3370 * nothing to do. */
3371 }
3372 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003373 if( generator->alg == PSA_ALG_SELECT_RAW )
3374 {
3375 if( generator->ctx.buffer.data != NULL )
3376 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003377 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003378 generator->ctx.buffer.size );
3379 mbedtls_free( generator->ctx.buffer.data );
3380 }
3381 }
3382 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003383#if defined(MBEDTLS_MD_C)
3384 if( PSA_ALG_IS_HKDF( generator->alg ) )
3385 {
3386 mbedtls_free( generator->ctx.hkdf.info );
3387 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3388 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003389 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3390 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3391 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003392 {
3393 if( generator->ctx.tls12_prf.key != NULL )
3394 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003395 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003396 generator->ctx.tls12_prf.key_len );
3397 mbedtls_free( generator->ctx.tls12_prf.key );
3398 }
Hanno Becker580fba12018-11-13 20:50:45 +00003399
3400 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3401 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003402 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003403 generator->ctx.tls12_prf.Ai_with_seed_len );
3404 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3405 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003406 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003407 else
3408#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003409 {
3410 status = PSA_ERROR_BAD_STATE;
3411 }
3412 memset( generator, 0, sizeof( *generator ) );
3413 return( status );
3414}
3415
3416
3417psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3418 size_t *capacity)
3419{
3420 *capacity = generator->capacity;
3421 return( PSA_SUCCESS );
3422}
3423
Gilles Peskinebef7f142018-07-12 17:22:21 +02003424#if defined(MBEDTLS_MD_C)
3425/* Read some bytes from an HKDF-based generator. This performs a chunk
3426 * of the expand phase of the HKDF algorithm. */
3427static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3428 psa_algorithm_t hash_alg,
3429 uint8_t *output,
3430 size_t output_length )
3431{
3432 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3433 psa_status_t status;
3434
3435 while( output_length != 0 )
3436 {
3437 /* Copy what remains of the current block */
3438 uint8_t n = hash_length - hkdf->offset_in_block;
3439 if( n > output_length )
3440 n = (uint8_t) output_length;
3441 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3442 output += n;
3443 output_length -= n;
3444 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003445 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003446 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003447 /* We can't be wanting more output after block 0xff, otherwise
3448 * the capacity check in psa_generator_read() would have
3449 * prevented this call. It could happen only if the generator
3450 * object was corrupted or if this function is called directly
3451 * inside the library. */
3452 if( hkdf->block_number == 0xff )
3453 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003454
3455 /* We need a new block */
3456 ++hkdf->block_number;
3457 hkdf->offset_in_block = 0;
3458 status = psa_hmac_setup_internal( &hkdf->hmac,
3459 hkdf->prk, hash_length,
3460 hash_alg );
3461 if( status != PSA_SUCCESS )
3462 return( status );
3463 if( hkdf->block_number != 1 )
3464 {
3465 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3466 hkdf->output_block,
3467 hash_length );
3468 if( status != PSA_SUCCESS )
3469 return( status );
3470 }
3471 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3472 hkdf->info,
3473 hkdf->info_length );
3474 if( status != PSA_SUCCESS )
3475 return( status );
3476 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3477 &hkdf->block_number, 1 );
3478 if( status != PSA_SUCCESS )
3479 return( status );
3480 status = psa_hmac_finish_internal( &hkdf->hmac,
3481 hkdf->output_block,
3482 sizeof( hkdf->output_block ) );
3483 if( status != PSA_SUCCESS )
3484 return( status );
3485 }
3486
3487 return( PSA_SUCCESS );
3488}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003489
3490static psa_status_t psa_generator_tls12_prf_generate_next_block(
3491 psa_tls12_prf_generator_t *tls12_prf,
3492 psa_algorithm_t alg )
3493{
3494 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3495 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3496 psa_hmac_internal_data hmac;
3497 psa_status_t status, cleanup_status;
3498
Hanno Becker3b339e22018-11-13 20:56:14 +00003499 unsigned char *Ai;
3500 size_t Ai_len;
3501
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003502 /* We can't be wanting more output after block 0xff, otherwise
3503 * the capacity check in psa_generator_read() would have
3504 * prevented this call. It could happen only if the generator
3505 * object was corrupted or if this function is called directly
3506 * inside the library. */
3507 if( tls12_prf->block_number == 0xff )
3508 return( PSA_ERROR_BAD_STATE );
3509
3510 /* We need a new block */
3511 ++tls12_prf->block_number;
3512 tls12_prf->offset_in_block = 0;
3513
3514 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3515 *
3516 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3517 *
3518 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3519 * HMAC_hash(secret, A(2) + seed) +
3520 * HMAC_hash(secret, A(3) + seed) + ...
3521 *
3522 * A(0) = seed
3523 * A(i) = HMAC_hash( secret, A(i-1) )
3524 *
3525 * The `psa_tls12_prf_generator` structures saves the block
3526 * `HMAC_hash(secret, A(i) + seed)` from which the output
3527 * is currently extracted as `output_block`, while
3528 * `A(i) + seed` is stored in `Ai_with_seed`.
3529 *
3530 * Generating a new block means recalculating `Ai_with_seed`
3531 * from the A(i)-part of it, and afterwards recalculating
3532 * `output_block`.
3533 *
3534 * A(0) is computed at setup time.
3535 *
3536 */
3537
3538 psa_hmac_init_internal( &hmac );
3539
3540 /* We must distinguish the calculation of A(1) from those
3541 * of A(2) and higher, because A(0)=seed has a different
3542 * length than the other A(i). */
3543 if( tls12_prf->block_number == 1 )
3544 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003545 Ai = tls12_prf->Ai_with_seed + hash_length;
3546 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003547 }
3548 else
3549 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003550 Ai = tls12_prf->Ai_with_seed;
3551 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003552 }
3553
Hanno Becker3b339e22018-11-13 20:56:14 +00003554 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3555 status = psa_hmac_setup_internal( &hmac,
3556 tls12_prf->key,
3557 tls12_prf->key_len,
3558 hash_alg );
3559 if( status != PSA_SUCCESS )
3560 goto cleanup;
3561
3562 status = psa_hash_update( &hmac.hash_ctx,
3563 Ai, Ai_len );
3564 if( status != PSA_SUCCESS )
3565 goto cleanup;
3566
3567 status = psa_hmac_finish_internal( &hmac,
3568 tls12_prf->Ai_with_seed,
3569 hash_length );
3570 if( status != PSA_SUCCESS )
3571 goto cleanup;
3572
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003573 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3574 status = psa_hmac_setup_internal( &hmac,
3575 tls12_prf->key,
3576 tls12_prf->key_len,
3577 hash_alg );
3578 if( status != PSA_SUCCESS )
3579 goto cleanup;
3580
3581 status = psa_hash_update( &hmac.hash_ctx,
3582 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003583 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003584 if( status != PSA_SUCCESS )
3585 goto cleanup;
3586
3587 status = psa_hmac_finish_internal( &hmac,
3588 tls12_prf->output_block,
3589 hash_length );
3590 if( status != PSA_SUCCESS )
3591 goto cleanup;
3592
3593cleanup:
3594
3595 cleanup_status = psa_hmac_abort_internal( &hmac );
3596 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3597 status = cleanup_status;
3598
3599 return( status );
3600}
3601
3602/* Read some bytes from an TLS-1.2-PRF-based generator.
3603 * See Section 5 of RFC 5246. */
3604static psa_status_t psa_generator_tls12_prf_read(
3605 psa_tls12_prf_generator_t *tls12_prf,
3606 psa_algorithm_t alg,
3607 uint8_t *output,
3608 size_t output_length )
3609{
3610 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3611 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3612 psa_status_t status;
3613
3614 while( output_length != 0 )
3615 {
3616 /* Copy what remains of the current block */
3617 uint8_t n = hash_length - tls12_prf->offset_in_block;
3618
3619 /* Check if we have fully processed the current block. */
3620 if( n == 0 )
3621 {
3622 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3623 alg );
3624 if( status != PSA_SUCCESS )
3625 return( status );
3626
3627 continue;
3628 }
3629
3630 if( n > output_length )
3631 n = (uint8_t) output_length;
3632 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3633 n );
3634 output += n;
3635 output_length -= n;
3636 tls12_prf->offset_in_block += n;
3637 }
3638
3639 return( PSA_SUCCESS );
3640}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003641#endif /* MBEDTLS_MD_C */
3642
Gilles Peskineeab56e42018-07-12 17:12:33 +02003643psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3644 uint8_t *output,
3645 size_t output_length )
3646{
3647 psa_status_t status;
3648
3649 if( output_length > generator->capacity )
3650 {
3651 generator->capacity = 0;
3652 /* Go through the error path to wipe all confidential data now
3653 * that the generator object is useless. */
3654 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3655 goto exit;
3656 }
3657 if( output_length == 0 &&
3658 generator->capacity == 0 && generator->alg == 0 )
3659 {
3660 /* Edge case: this is a blank or finished generator, and 0
3661 * bytes were requested. The right error in this case could
3662 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3663 * INSUFFICIENT_CAPACITY, which is right for a finished
3664 * generator, for consistency with the case when
3665 * output_length > 0. */
3666 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3667 }
3668 generator->capacity -= output_length;
3669
Gilles Peskine751d9652018-09-18 12:05:44 +02003670 if( generator->alg == PSA_ALG_SELECT_RAW )
3671 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003672 /* Initially, the capacity of a selection generator is always
3673 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3674 * abbreviated in this comment as `size`. When the remaining
3675 * capacity is `c`, the next bytes to serve start `c` bytes
3676 * from the end of the buffer, i.e. `size - c` from the
3677 * beginning of the buffer. Since `generator->capacity` was just
3678 * decremented above, we need to serve the bytes from
3679 * `size - generator->capacity - output_length` to
3680 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003681 size_t offset =
3682 generator->ctx.buffer.size - generator->capacity - output_length;
3683 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3684 status = PSA_SUCCESS;
3685 }
3686 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003687#if defined(MBEDTLS_MD_C)
3688 if( PSA_ALG_IS_HKDF( generator->alg ) )
3689 {
3690 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3691 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3692 output, output_length );
3693 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003694 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3695 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003696 {
3697 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3698 generator->alg, output,
3699 output_length );
3700 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003701 else
3702#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003703 {
3704 return( PSA_ERROR_BAD_STATE );
3705 }
3706
3707exit:
3708 if( status != PSA_SUCCESS )
3709 {
3710 psa_generator_abort( generator );
3711 memset( output, '!', output_length );
3712 }
3713 return( status );
3714}
3715
Gilles Peskine08542d82018-07-19 17:05:42 +02003716#if defined(MBEDTLS_DES_C)
3717static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3718{
3719 if( data_size >= 8 )
3720 mbedtls_des_key_set_parity( data );
3721 if( data_size >= 16 )
3722 mbedtls_des_key_set_parity( data + 8 );
3723 if( data_size >= 24 )
3724 mbedtls_des_key_set_parity( data + 16 );
3725}
3726#endif /* MBEDTLS_DES_C */
3727
Gilles Peskinec5487a82018-12-03 18:08:14 +01003728psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003729 psa_key_type_t type,
3730 size_t bits,
3731 psa_crypto_generator_t *generator )
3732{
3733 uint8_t *data = NULL;
3734 size_t bytes = PSA_BITS_TO_BYTES( bits );
3735 psa_status_t status;
3736
3737 if( ! key_type_is_raw_bytes( type ) )
3738 return( PSA_ERROR_INVALID_ARGUMENT );
3739 if( bits % 8 != 0 )
3740 return( PSA_ERROR_INVALID_ARGUMENT );
3741 data = mbedtls_calloc( 1, bytes );
3742 if( data == NULL )
3743 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3744
3745 status = psa_generator_read( generator, data, bytes );
3746 if( status != PSA_SUCCESS )
3747 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003748#if defined(MBEDTLS_DES_C)
3749 if( type == PSA_KEY_TYPE_DES )
3750 psa_des_set_key_parity( data, bytes );
3751#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003752 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003753
3754exit:
3755 mbedtls_free( data );
3756 return( status );
3757}
3758
3759
3760
3761/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003762/* Key derivation */
3763/****************************************************************/
3764
Gilles Peskinea05219c2018-11-16 16:02:56 +01003765#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003766/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003767 * of the HKDF algorithm.
3768 *
3769 * Note that if this function fails, you must call psa_generator_abort()
3770 * to potentially free embedded data structures and wipe confidential data.
3771 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003772static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003773 const uint8_t *secret,
3774 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003775 psa_algorithm_t hash_alg,
3776 const uint8_t *salt,
3777 size_t salt_length,
3778 const uint8_t *label,
3779 size_t label_length )
3780{
3781 psa_status_t status;
3782 status = psa_hmac_setup_internal( &hkdf->hmac,
3783 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003784 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003785 if( status != PSA_SUCCESS )
3786 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003787 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003788 if( status != PSA_SUCCESS )
3789 return( status );
3790 status = psa_hmac_finish_internal( &hkdf->hmac,
3791 hkdf->prk,
3792 sizeof( hkdf->prk ) );
3793 if( status != PSA_SUCCESS )
3794 return( status );
3795 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3796 hkdf->block_number = 0;
3797 hkdf->info_length = label_length;
3798 if( label_length != 0 )
3799 {
3800 hkdf->info = mbedtls_calloc( 1, label_length );
3801 if( hkdf->info == NULL )
3802 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3803 memcpy( hkdf->info, label, label_length );
3804 }
3805 return( PSA_SUCCESS );
3806}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003807#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003808
Gilles Peskinea05219c2018-11-16 16:02:56 +01003809#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003810/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3811 *
3812 * Note that if this function fails, you must call psa_generator_abort()
3813 * to potentially free embedded data structures and wipe confidential data.
3814 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003815static psa_status_t psa_generator_tls12_prf_setup(
3816 psa_tls12_prf_generator_t *tls12_prf,
3817 const unsigned char *key,
3818 size_t key_len,
3819 psa_algorithm_t hash_alg,
3820 const uint8_t *salt,
3821 size_t salt_length,
3822 const uint8_t *label,
3823 size_t label_length )
3824{
3825 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003826 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3827 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003828
3829 tls12_prf->key = mbedtls_calloc( 1, key_len );
3830 if( tls12_prf->key == NULL )
3831 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3832 tls12_prf->key_len = key_len;
3833 memcpy( tls12_prf->key, key, key_len );
3834
Hanno Becker580fba12018-11-13 20:50:45 +00003835 overflow = ( salt_length + label_length < salt_length ) ||
3836 ( salt_length + label_length + hash_length < hash_length );
3837 if( overflow )
3838 return( PSA_ERROR_INVALID_ARGUMENT );
3839
3840 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3841 if( tls12_prf->Ai_with_seed == NULL )
3842 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3843 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3844
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003845 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3846 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003847 if( label_length != 0 )
3848 {
3849 memcpy( tls12_prf->Ai_with_seed + hash_length,
3850 label, label_length );
3851 }
3852
3853 if( salt_length != 0 )
3854 {
3855 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3856 salt, salt_length );
3857 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003858
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003859 /* The first block gets generated when
3860 * psa_generator_read() is called. */
3861 tls12_prf->block_number = 0;
3862 tls12_prf->offset_in_block = hash_length;
3863
3864 return( PSA_SUCCESS );
3865}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003866
3867/* Set up a TLS-1.2-PSK-to-MS-based generator. */
3868static psa_status_t psa_generator_tls12_psk_to_ms_setup(
3869 psa_tls12_prf_generator_t *tls12_prf,
3870 const unsigned char *psk,
3871 size_t psk_len,
3872 psa_algorithm_t hash_alg,
3873 const uint8_t *salt,
3874 size_t salt_length,
3875 const uint8_t *label,
3876 size_t label_length )
3877{
3878 psa_status_t status;
3879 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
3880
3881 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
3882 return( PSA_ERROR_INVALID_ARGUMENT );
3883
3884 /* Quoting RFC 4279, Section 2:
3885 *
3886 * The premaster secret is formed as follows: if the PSK is N octets
3887 * long, concatenate a uint16 with the value N, N zero octets, a second
3888 * uint16 with the value N, and the PSK itself.
3889 */
3890
3891 pms[0] = ( psk_len >> 8 ) & 0xff;
3892 pms[1] = ( psk_len >> 0 ) & 0xff;
3893 memset( pms + 2, 0, psk_len );
3894 pms[2 + psk_len + 0] = pms[0];
3895 pms[2 + psk_len + 1] = pms[1];
3896 memcpy( pms + 4 + psk_len, psk, psk_len );
3897
3898 status = psa_generator_tls12_prf_setup( tls12_prf,
3899 pms, 4 + 2 * psk_len,
3900 hash_alg,
3901 salt, salt_length,
3902 label, label_length );
3903
Gilles Peskine3f108122018-12-07 18:14:53 +01003904 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00003905 return( status );
3906}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003907#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003908
Gilles Peskine346797d2018-11-16 16:05:06 +01003909/* Note that if this function fails, you must call psa_generator_abort()
3910 * to potentially free embedded data structures and wipe confidential data.
3911 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003912static psa_status_t psa_key_derivation_internal(
3913 psa_crypto_generator_t *generator,
3914 const uint8_t *secret, size_t secret_length,
3915 psa_algorithm_t alg,
3916 const uint8_t *salt, size_t salt_length,
3917 const uint8_t *label, size_t label_length,
3918 size_t capacity )
3919{
3920 psa_status_t status;
3921 size_t max_capacity;
3922
3923 /* Set generator->alg even on failure so that abort knows what to do. */
3924 generator->alg = alg;
3925
Gilles Peskine751d9652018-09-18 12:05:44 +02003926 if( alg == PSA_ALG_SELECT_RAW )
3927 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003928 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02003929 if( salt_length != 0 )
3930 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01003931 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02003932 if( label_length != 0 )
3933 return( PSA_ERROR_INVALID_ARGUMENT );
3934 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
3935 if( generator->ctx.buffer.data == NULL )
3936 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3937 memcpy( generator->ctx.buffer.data, secret, secret_length );
3938 generator->ctx.buffer.size = secret_length;
3939 max_capacity = secret_length;
3940 status = PSA_SUCCESS;
3941 }
3942 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003943#if defined(MBEDTLS_MD_C)
3944 if( PSA_ALG_IS_HKDF( alg ) )
3945 {
3946 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3947 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3948 if( hash_size == 0 )
3949 return( PSA_ERROR_NOT_SUPPORTED );
3950 max_capacity = 255 * hash_size;
3951 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3952 secret, secret_length,
3953 hash_alg,
3954 salt, salt_length,
3955 label, label_length );
3956 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003957 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
3958 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
3959 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003960 {
3961 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3962 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3963
3964 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
3965 if( hash_alg != PSA_ALG_SHA_256 &&
3966 hash_alg != PSA_ALG_SHA_384 )
3967 {
3968 return( PSA_ERROR_NOT_SUPPORTED );
3969 }
3970
3971 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00003972
3973 if( PSA_ALG_IS_TLS12_PRF( alg ) )
3974 {
3975 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
3976 secret, secret_length,
3977 hash_alg, salt, salt_length,
3978 label, label_length );
3979 }
3980 else
3981 {
3982 status = psa_generator_tls12_psk_to_ms_setup(
3983 &generator->ctx.tls12_prf,
3984 secret, secret_length,
3985 hash_alg, salt, salt_length,
3986 label, label_length );
3987 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003988 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003989 else
3990#endif
3991 {
3992 return( PSA_ERROR_NOT_SUPPORTED );
3993 }
3994
3995 if( status != PSA_SUCCESS )
3996 return( status );
3997
3998 if( capacity <= max_capacity )
3999 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004000 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4001 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004002 else
4003 return( PSA_ERROR_INVALID_ARGUMENT );
4004
4005 return( PSA_SUCCESS );
4006}
4007
Gilles Peskineea0fb492018-07-12 17:17:20 +02004008psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004009 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004010 psa_algorithm_t alg,
4011 const uint8_t *salt,
4012 size_t salt_length,
4013 const uint8_t *label,
4014 size_t label_length,
4015 size_t capacity )
4016{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004017 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004018 psa_status_t status;
4019
4020 if( generator->alg != 0 )
4021 return( PSA_ERROR_BAD_STATE );
4022
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004023 /* Make sure that alg is a key derivation algorithm. This prevents
4024 * key selection algorithms, which psa_key_derivation_internal
4025 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004026 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4027 return( PSA_ERROR_INVALID_ARGUMENT );
4028
Gilles Peskinec5487a82018-12-03 18:08:14 +01004029 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004030 if( status != PSA_SUCCESS )
4031 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004032
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004033 if( slot->type != PSA_KEY_TYPE_DERIVE )
4034 return( PSA_ERROR_INVALID_ARGUMENT );
4035
4036 status = psa_key_derivation_internal( generator,
4037 slot->data.raw.data,
4038 slot->data.raw.bytes,
4039 alg,
4040 salt, salt_length,
4041 label, label_length,
4042 capacity );
4043 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004044 psa_generator_abort( generator );
4045 return( status );
4046}
4047
4048
4049
4050/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004051/* Key agreement */
4052/****************************************************************/
4053
Gilles Peskinea05219c2018-11-16 16:02:56 +01004054#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004055static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4056 size_t peer_key_length,
4057 const mbedtls_ecp_keypair *our_key,
4058 uint8_t *shared_secret,
4059 size_t shared_secret_size,
4060 size_t *shared_secret_length )
4061{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004062 mbedtls_ecp_keypair *their_key = NULL;
4063 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004064 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004065 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004066
Jaeden Ameroccdce902019-01-10 11:42:27 +00004067 status = psa_import_ec_public_key(
4068 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4069 peer_key, peer_key_length,
4070 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004071 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004072 goto exit;
Gilles Peskineb4086612018-11-14 20:51:23 +01004073
Jaeden Amero97271b32019-01-10 19:38:51 +00004074 status = mbedtls_to_psa_error(
4075 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4076 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004077 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00004078 status = mbedtls_to_psa_error(
4079 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4080 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004081 goto exit;
4082
Jaeden Amero97271b32019-01-10 19:38:51 +00004083 status = mbedtls_to_psa_error(
4084 mbedtls_ecdh_calc_secret( &ecdh,
4085 shared_secret_length,
4086 shared_secret, shared_secret_size,
4087 mbedtls_ctr_drbg_random,
4088 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004089
4090exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004091 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00004092 mbedtls_ecp_keypair_free( their_key );
4093 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004094 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004095}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004096#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004097
Gilles Peskine01d718c2018-09-18 12:01:02 +02004098#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4099
Gilles Peskine346797d2018-11-16 16:05:06 +01004100/* Note that if this function fails, you must call psa_generator_abort()
4101 * to potentially free embedded data structures and wipe confidential data.
4102 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004103static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004104 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004105 const uint8_t *peer_key,
4106 size_t peer_key_length,
4107 psa_algorithm_t alg )
4108{
4109 psa_status_t status;
4110 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4111 size_t shared_secret_length = 0;
4112
4113 /* Step 1: run the secret agreement algorithm to generate the shared
4114 * secret. */
4115 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4116 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004117#if defined(MBEDTLS_ECDH_C)
4118 case PSA_ALG_ECDH_BASE:
4119 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4120 return( PSA_ERROR_INVALID_ARGUMENT );
4121 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4122 private_key->data.ecp,
4123 shared_secret,
4124 sizeof( shared_secret ),
4125 &shared_secret_length );
4126 break;
4127#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004128 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004129 (void) private_key;
4130 (void) peer_key;
4131 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004132 return( PSA_ERROR_NOT_SUPPORTED );
4133 }
4134 if( status != PSA_SUCCESS )
4135 goto exit;
4136
4137 /* Step 2: set up the key derivation to generate key material from
4138 * the shared secret. */
4139 status = psa_key_derivation_internal( generator,
4140 shared_secret, shared_secret_length,
4141 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4142 NULL, 0, NULL, 0,
4143 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4144exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004145 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004146 return( status );
4147}
4148
4149psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004150 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004151 const uint8_t *peer_key,
4152 size_t peer_key_length,
4153 psa_algorithm_t alg )
4154{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004155 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004156 psa_status_t status;
4157 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4158 return( PSA_ERROR_INVALID_ARGUMENT );
4159 status = psa_get_key_from_slot( private_key, &slot,
4160 PSA_KEY_USAGE_DERIVE, alg );
4161 if( status != PSA_SUCCESS )
4162 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004163 status = psa_key_agreement_internal( generator,
4164 slot,
4165 peer_key, peer_key_length,
4166 alg );
4167 if( status != PSA_SUCCESS )
4168 psa_generator_abort( generator );
4169 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004170}
4171
4172
4173
4174/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004175/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004176/****************************************************************/
4177
4178psa_status_t psa_generate_random( uint8_t *output,
4179 size_t output_size )
4180{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004181 int ret;
4182 GUARD_MODULE_INITIALIZED;
4183
4184 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004185 return( mbedtls_to_psa_error( ret ) );
4186}
4187
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004188#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004189
4190/* Support function for error conversion between psa_its error codes to psa crypto */
4191static psa_status_t its_to_psa_error( psa_its_status_t ret )
4192{
4193 switch( ret )
4194 {
4195 case PSA_ITS_SUCCESS:
4196 return( PSA_SUCCESS );
4197
4198 case PSA_ITS_ERROR_KEY_NOT_FOUND:
4199 return( PSA_ERROR_EMPTY_SLOT );
4200
4201 case PSA_ITS_ERROR_STORAGE_FAILURE:
4202 return( PSA_ERROR_STORAGE_FAILURE );
4203
4204 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4205 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4206
4207 case PSA_ITS_ERROR_INVALID_KEY:
Jaeden Amero58600552018-11-30 12:04:38 +00004208 case PSA_ITS_ERROR_OFFSET_INVALID:
avolinski13beb102018-11-20 16:51:49 +02004209 case PSA_ITS_ERROR_INCORRECT_SIZE:
4210 case PSA_ITS_ERROR_BAD_POINTER:
4211 return( PSA_ERROR_INVALID_ARGUMENT );
4212
4213 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4214 return( PSA_ERROR_NOT_SUPPORTED );
4215
4216 case PSA_ITS_ERROR_WRITE_ONCE:
4217 return( PSA_ERROR_OCCUPIED_SLOT );
4218
4219 default:
4220 return( PSA_ERROR_UNKNOWN_ERROR );
4221 }
4222}
4223
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004224psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4225 size_t seed_size )
4226{
4227 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004228 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004229 struct psa_its_info_t p_info;
4230 if( global_data.initialized )
4231 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004232
4233 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4234 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4235 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4236 return( PSA_ERROR_INVALID_ARGUMENT );
4237
avolinski0d2c2662018-11-21 17:31:07 +02004238 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004239 status = its_to_psa_error( its_status );
4240
4241 if( PSA_ITS_ERROR_KEY_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004242 {
avolinski0d2c2662018-11-21 17:31:07 +02004243 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004244 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004245 }
avolinski13beb102018-11-20 16:51:49 +02004246 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004247 {
4248 /* You should not be here. Seed needs to be injected only once */
4249 status = PSA_ERROR_NOT_PERMITTED;
4250 }
4251 return( status );
4252}
4253#endif
4254
Gilles Peskinec5487a82018-12-03 18:08:14 +01004255psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004256 psa_key_type_t type,
4257 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004258 const void *extra,
4259 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004260{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004261 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004262 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004263
Gilles Peskine53d991e2018-07-12 01:14:59 +02004264 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004265 return( PSA_ERROR_INVALID_ARGUMENT );
4266
Gilles Peskinec5487a82018-12-03 18:08:14 +01004267 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004268 if( status != PSA_SUCCESS )
4269 return( status );
4270
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004271 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004272 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004273 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004274 if( status != PSA_SUCCESS )
4275 return( status );
4276 status = psa_generate_random( slot->data.raw.data,
4277 slot->data.raw.bytes );
4278 if( status != PSA_SUCCESS )
4279 {
4280 mbedtls_free( slot->data.raw.data );
4281 return( status );
4282 }
4283#if defined(MBEDTLS_DES_C)
4284 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004285 psa_des_set_key_parity( slot->data.raw.data,
4286 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004287#endif /* MBEDTLS_DES_C */
4288 }
4289 else
4290
4291#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4292 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4293 {
4294 mbedtls_rsa_context *rsa;
4295 int ret;
4296 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004297 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4298 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004299 /* Accept only byte-aligned keys, for the same reasons as
4300 * in psa_import_rsa_key(). */
4301 if( bits % 8 != 0 )
4302 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004303 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004304 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004305 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004306 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004307 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004308#if INT_MAX < 0xffffffff
4309 /* Check that the uint32_t value passed by the caller fits
4310 * in the range supported by this implementation. */
4311 if( p->e > INT_MAX )
4312 return( PSA_ERROR_NOT_SUPPORTED );
4313#endif
4314 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004315 }
4316 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4317 if( rsa == NULL )
4318 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4319 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4320 ret = mbedtls_rsa_gen_key( rsa,
4321 mbedtls_ctr_drbg_random,
4322 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004323 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004324 exponent );
4325 if( ret != 0 )
4326 {
4327 mbedtls_rsa_free( rsa );
4328 mbedtls_free( rsa );
4329 return( mbedtls_to_psa_error( ret ) );
4330 }
4331 slot->data.rsa = rsa;
4332 }
4333 else
4334#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4335
4336#if defined(MBEDTLS_ECP_C)
4337 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4338 {
4339 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4340 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4341 const mbedtls_ecp_curve_info *curve_info =
4342 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4343 mbedtls_ecp_keypair *ecp;
4344 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004345 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004346 return( PSA_ERROR_NOT_SUPPORTED );
4347 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4348 return( PSA_ERROR_NOT_SUPPORTED );
4349 if( curve_info->bit_size != bits )
4350 return( PSA_ERROR_INVALID_ARGUMENT );
4351 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4352 if( ecp == NULL )
4353 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4354 mbedtls_ecp_keypair_init( ecp );
4355 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4356 mbedtls_ctr_drbg_random,
4357 &global_data.ctr_drbg );
4358 if( ret != 0 )
4359 {
4360 mbedtls_ecp_keypair_free( ecp );
4361 mbedtls_free( ecp );
4362 return( mbedtls_to_psa_error( ret ) );
4363 }
4364 slot->data.ecp = ecp;
4365 }
4366 else
4367#endif /* MBEDTLS_ECP_C */
4368
4369 return( PSA_ERROR_NOT_SUPPORTED );
4370
4371 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004372
4373#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4374 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4375 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004376 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004377 }
4378#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4379
4380 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004381}
4382
4383
4384/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004385/* Module setup */
4386/****************************************************************/
4387
Gilles Peskine5e769522018-11-20 21:59:56 +01004388psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4389 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4390 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4391{
4392 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4393 return( PSA_ERROR_BAD_STATE );
4394 global_data.entropy_init = entropy_init;
4395 global_data.entropy_free = entropy_free;
4396 return( PSA_SUCCESS );
4397}
4398
Gilles Peskinee59236f2018-01-27 23:32:46 +01004399void mbedtls_psa_crypto_free( void )
4400{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004401 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004402 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4403 {
4404 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004405 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004406 }
4407 /* Wipe all remaining data, including configuration.
4408 * In particular, this sets all state indicator to the value
4409 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004410 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004411}
4412
4413psa_status_t psa_crypto_init( void )
4414{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004415 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004416 const unsigned char drbg_seed[] = "PSA";
4417
Gilles Peskinec6b69072018-11-20 21:42:52 +01004418 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004419 if( global_data.initialized != 0 )
4420 return( PSA_SUCCESS );
4421
Gilles Peskine5e769522018-11-20 21:59:56 +01004422 /* Set default configuration if
4423 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4424 if( global_data.entropy_init == NULL )
4425 global_data.entropy_init = mbedtls_entropy_init;
4426 if( global_data.entropy_free == NULL )
4427 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004428
Gilles Peskinec6b69072018-11-20 21:42:52 +01004429 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004430 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004431 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004432 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004433 status = mbedtls_to_psa_error(
4434 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4435 mbedtls_entropy_func,
4436 &global_data.entropy,
4437 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4438 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004439 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004440 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004441
Gilles Peskine66fb1262018-12-10 16:29:04 +01004442 status = psa_initialize_key_slots( );
4443 if( status != PSA_SUCCESS )
4444 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004445
4446 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004447 global_data.initialized = 1;
4448
Gilles Peskinee59236f2018-01-27 23:32:46 +01004449exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004450 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004451 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004452 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004453}
4454
4455#endif /* MBEDTLS_PSA_CRYPTO_C */