blob: 2d21bb079505eb287b6d4f180c7674d86d6992fe [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/*
itayzafrir14e76782019-01-16 11:16:39 +020030 * When MBEDTLS_PSA_CRYPTO_SPM is defined, the code is being built for SPM
31 * (Secure Partition Manager) integration which separates the code into two
32 * parts: NSPE (Non-Secure Processing Environment) and SPE (Secure Processing
33 * Environment). When building for the SPE, an additional header file should be
34 * included.
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030035 */
mohammad160327010052018-07-03 13:16:15 +030036#if defined(MBEDTLS_PSA_CRYPTO_SPM)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030037/*
itayzafrir14e76782019-01-16 11:16:39 +020038 * PSA_CRYPTO_SECURE means that this file is compiled for the SPE.
39 * Some headers will be affected by this flag.
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030040 */
mohammad160327010052018-07-03 13:16:15 +030041#define PSA_CRYPTO_SECURE 1
42#include "crypto_spe.h"
43#endif
44
Gilles Peskinee59236f2018-01-27 23:32:46 +010045#include "psa/crypto.h"
46
Gilles Peskine039b90c2018-12-07 18:24:41 +010047#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010048#include "psa_crypto_invasive.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010049#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010050/* Include internal declarations that are useful for implementing persistently
51 * stored keys. */
52#include "psa_crypto_storage.h"
53
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010054#include <stdlib.h>
55#include <string.h>
56#if defined(MBEDTLS_PLATFORM_C)
57#include "mbedtls/platform.h"
58#else
59#define mbedtls_calloc calloc
60#define mbedtls_free free
61#endif
62
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020064#include "mbedtls/asn1.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) )
Oren Cohen23a67842019-01-24 14:32:11 +020095#include "psa/internal_trusted_storage.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020096#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
540#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100541/* Import an elliptic curve parsed by the mbedtls pk module. */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200542static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
543 mbedtls_pk_context *pk,
544 mbedtls_ecp_keypair **p_ecp )
545{
546 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
547 return( PSA_ERROR_INVALID_ARGUMENT );
548 else
549 {
550 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
551 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
552 if( actual_curve != expected_curve )
553 return( PSA_ERROR_INVALID_ARGUMENT );
554 *p_ecp = ecp;
555 return( PSA_SUCCESS );
556 }
557}
558#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
559
Gilles Peskinef76aa772018-10-29 19:24:33 +0100560#if defined(MBEDTLS_ECP_C)
561/* Import a private key given as a byte string which is the private value
562 * in big-endian order. */
563static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
564 const uint8_t *data,
565 size_t data_length,
566 mbedtls_ecp_keypair **p_ecp )
567{
568 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
569 mbedtls_ecp_keypair *ecp = NULL;
570 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
571
572 *p_ecp = NULL;
573 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
574 if( ecp == NULL )
575 return( PSA_ERROR_INSUFFICIENT_MEMORY );
576
577 /* Load the group. */
578 status = mbedtls_to_psa_error(
579 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
580 if( status != PSA_SUCCESS )
581 goto exit;
582 /* Load the secret value. */
583 status = mbedtls_to_psa_error(
584 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
585 if( status != PSA_SUCCESS )
586 goto exit;
587 /* Validate the private key. */
588 status = mbedtls_to_psa_error(
589 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
590 if( status != PSA_SUCCESS )
591 goto exit;
592 /* Calculate the public key from the private key. */
593 status = mbedtls_to_psa_error(
594 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
595 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
596 if( status != PSA_SUCCESS )
597 goto exit;
598
599 *p_ecp = ecp;
600 return( PSA_SUCCESS );
601
602exit:
603 if( ecp != NULL )
604 {
605 mbedtls_ecp_keypair_free( ecp );
606 mbedtls_free( ecp );
607 }
608 return( status );
609}
610#endif /* defined(MBEDTLS_ECP_C) */
611
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100612/** Import key data into a slot. `slot->type` must have been set
613 * previously. This function assumes that the slot does not contain
614 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100615psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
616 const uint8_t *data,
617 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100618{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200619 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100620
Darryl Green940d72c2018-07-13 13:18:51 +0100621 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100622 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100623 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100624 if( data_length > SIZE_MAX / 8 )
625 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100626 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200627 PSA_BYTES_TO_BITS( data_length ),
628 &slot->data.raw );
629 if( status != PSA_SUCCESS )
630 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200631 if( data_length != 0 )
632 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100633 }
634 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100635#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100636 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100637 {
Darryl Green940d72c2018-07-13 13:18:51 +0100638 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100639 data, data_length,
640 &slot->data.ecp );
641 if( status != PSA_SUCCESS )
642 return( status );
643 }
644 else
645#endif /* MBEDTLS_ECP_C */
Gilles Peskine969ac722018-01-28 18:16:59 +0100646#if defined(MBEDTLS_PK_PARSE_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100647 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
648 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100649 {
650 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100651 mbedtls_pk_context pk;
652 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200653
654 /* Parse the data. */
Darryl Green940d72c2018-07-13 13:18:51 +0100655 if( PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100656 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
657 else
658 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100659 if( ret != 0 )
660 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200661
662 /* We have something that the pkparse module recognizes.
663 * If it has the expected type and passes any type-specific
664 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100665#if defined(MBEDTLS_RSA_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100666 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200667 status = psa_import_rsa_key( &pk, &slot->data.rsa );
668 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100669#endif /* MBEDTLS_RSA_C */
670#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100671 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
672 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200673 &pk, &slot->data.ecp );
674 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100675#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200676 {
677 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200678 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200679
Gilles Peskinec648d692018-06-28 08:46:13 +0200680 /* Free the content of the pk object only on error. On success,
681 * the content of the object has been stored in the slot. */
682 if( status != PSA_SUCCESS )
683 {
684 mbedtls_pk_free( &pk );
685 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100686 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100687 }
688 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100689#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100690 {
691 return( PSA_ERROR_NOT_SUPPORTED );
692 }
Darryl Green940d72c2018-07-13 13:18:51 +0100693 return( PSA_SUCCESS );
694}
695
Darryl Green06fd18d2018-07-16 11:21:11 +0100696/* Retrieve an empty key slot (slot with no key data, but possibly
697 * with some metadata such as a policy). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100698static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100699 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100700{
701 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100702 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100703
704 *p_slot = NULL;
705
Gilles Peskinec5487a82018-12-03 18:08:14 +0100706 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100707 if( status != PSA_SUCCESS )
708 return( status );
709
710 if( slot->type != PSA_KEY_TYPE_NONE )
711 return( PSA_ERROR_OCCUPIED_SLOT );
712
713 *p_slot = slot;
714 return( status );
715}
716
717/** Retrieve a slot which must contain a key. The key must have allow all the
718 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
719 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100720static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100721 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100722 psa_key_usage_t usage,
723 psa_algorithm_t alg )
724{
725 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100726 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100727
728 *p_slot = NULL;
729
Gilles Peskinec5487a82018-12-03 18:08:14 +0100730 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100731 if( status != PSA_SUCCESS )
732 return( status );
733 if( slot->type == PSA_KEY_TYPE_NONE )
734 return( PSA_ERROR_EMPTY_SLOT );
735
736 /* Enforce that usage policy for the key slot contains all the flags
737 * required by the usage parameter. There is one exception: public
738 * keys can always be exported, so we treat public key objects as
739 * if they had the export flag. */
740 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
741 usage &= ~PSA_KEY_USAGE_EXPORT;
742 if( ( slot->policy.usage & usage ) != usage )
743 return( PSA_ERROR_NOT_PERMITTED );
744 if( alg != 0 && ( alg != slot->policy.alg ) )
745 return( PSA_ERROR_NOT_PERMITTED );
746
747 *p_slot = slot;
748 return( PSA_SUCCESS );
749}
Darryl Green940d72c2018-07-13 13:18:51 +0100750
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100751/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100752static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000753{
754 if( slot->type == PSA_KEY_TYPE_NONE )
755 {
756 /* No key material to clean. */
757 }
758 else if( key_type_is_raw_bytes( slot->type ) )
759 {
760 mbedtls_free( slot->data.raw.data );
761 }
762 else
763#if defined(MBEDTLS_RSA_C)
764 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
765 {
766 mbedtls_rsa_free( slot->data.rsa );
767 mbedtls_free( slot->data.rsa );
768 }
769 else
770#endif /* defined(MBEDTLS_RSA_C) */
771#if defined(MBEDTLS_ECP_C)
772 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
773 {
774 mbedtls_ecp_keypair_free( slot->data.ecp );
775 mbedtls_free( slot->data.ecp );
776 }
777 else
778#endif /* defined(MBEDTLS_ECP_C) */
779 {
780 /* Shouldn't happen: the key type is not any type that we
781 * put in. */
782 return( PSA_ERROR_TAMPERING_DETECTED );
783 }
784
785 return( PSA_SUCCESS );
786}
787
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100788/** Completely wipe a slot in memory, including its policy.
789 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100790psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100791{
792 psa_status_t status = psa_remove_key_data_from_memory( slot );
793 /* At this point, key material and other type-specific content has
794 * been wiped. Clear remaining metadata. We can call memset and not
795 * zeroize because the metadata is not particularly sensitive. */
796 memset( slot, 0, sizeof( *slot ) );
797 return( status );
798}
799
Gilles Peskinec5487a82018-12-03 18:08:14 +0100800psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100801 psa_key_type_t type,
802 const uint8_t *data,
803 size_t data_length )
804{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100805 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100806 psa_status_t status;
807
Gilles Peskinec5487a82018-12-03 18:08:14 +0100808 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100809 if( status != PSA_SUCCESS )
810 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100811
812 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100813
814 status = psa_import_key_into_slot( slot, data, data_length );
815 if( status != PSA_SUCCESS )
816 {
817 slot->type = PSA_KEY_TYPE_NONE;
818 return( status );
819 }
820
Darryl Greend49a4992018-06-18 17:27:26 +0100821#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
822 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
823 {
824 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100825 status = psa_save_persistent_key( slot->persistent_storage_id,
826 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100827 data_length );
828 if( status != PSA_SUCCESS )
829 {
830 (void) psa_remove_key_data_from_memory( slot );
831 slot->type = PSA_KEY_TYPE_NONE;
832 }
833 }
834#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
835
836 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837}
838
Gilles Peskinec5487a82018-12-03 18:08:14 +0100839psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100840{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100841 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100842 psa_status_t status = PSA_SUCCESS;
843 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100844
Gilles Peskinec5487a82018-12-03 18:08:14 +0100845 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200846 if( status != PSA_SUCCESS )
847 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100848#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
849 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
850 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100851 storage_status =
852 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100853 }
854#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100855 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100856 if( status != PSA_SUCCESS )
857 return( status );
858 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100859}
860
Gilles Peskineb870b182018-07-06 16:02:09 +0200861/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100862static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200863{
864 if( key_type_is_raw_bytes( slot->type ) )
865 return( slot->data.raw.bytes * 8 );
866#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200867 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100868 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200869#endif /* defined(MBEDTLS_RSA_C) */
870#if defined(MBEDTLS_ECP_C)
871 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
872 return( slot->data.ecp->grp.pbits );
873#endif /* defined(MBEDTLS_ECP_C) */
874 /* Shouldn't happen except on an empty slot. */
875 return( 0 );
876}
877
Gilles Peskinec5487a82018-12-03 18:08:14 +0100878psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200879 psa_key_type_t *type,
880 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100881{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100882 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200883 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100884
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100885 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200886 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100887 if( bits != NULL )
888 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100889 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200890 if( status != PSA_SUCCESS )
891 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200892
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100893 if( slot->type == PSA_KEY_TYPE_NONE )
894 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200895 if( type != NULL )
896 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200897 if( bits != NULL )
898 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100899 return( PSA_SUCCESS );
900}
901
Gilles Peskine2f060a82018-12-04 17:12:32 +0100902static psa_status_t psa_internal_export_key( psa_key_slot_t *slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200903 uint8_t *data,
904 size_t data_size,
905 size_t *data_length,
906 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100907{
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100908 *data_length = 0;
909
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200910 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300911 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300912
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200913 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100914 {
915 if( slot->data.raw.bytes > data_size )
916 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +0100917 if( data_size != 0 )
918 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200919 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +0100920 memset( data + slot->data.raw.bytes, 0,
921 data_size - slot->data.raw.bytes );
922 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100923 *data_length = slot->data.raw.bytes;
924 return( PSA_SUCCESS );
925 }
Gilles Peskine188c71e2018-10-29 19:26:02 +0100926#if defined(MBEDTLS_ECP_C)
927 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
928 {
Darryl Greendd8fb772018-11-07 16:00:44 +0000929 psa_status_t status;
930
Gilles Peskine188c71e2018-10-29 19:26:02 +0100931 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
932 if( bytes > data_size )
933 return( PSA_ERROR_BUFFER_TOO_SMALL );
934 status = mbedtls_to_psa_error(
935 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
936 if( status != PSA_SUCCESS )
937 return( status );
938 memset( data + bytes, 0, data_size - bytes );
939 *data_length = bytes;
940 return( PSA_SUCCESS );
941 }
942#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100943 else
Moran Peker17e36e12018-05-02 12:55:20 +0300944 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100945#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200946 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300947 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100948 {
Moran Pekera998bc62018-04-16 18:16:20 +0300949 mbedtls_pk_context pk;
950 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +0200951 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300952 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100953#if defined(MBEDTLS_RSA_C)
954 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300955 pk.pk_info = &mbedtls_rsa_info;
956 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100957#else
958 return( PSA_ERROR_NOT_SUPPORTED );
959#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300960 }
961 else
962 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100963#if defined(MBEDTLS_ECP_C)
964 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300965 pk.pk_info = &mbedtls_eckey_info;
966 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100967#else
968 return( PSA_ERROR_NOT_SUPPORTED );
969#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300970 }
Moran Pekerd7326592018-05-29 16:56:39 +0300971 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300972 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300973 else
974 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300975 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200976 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200977 /* If data_size is 0 then data may be NULL and then the
978 * call to memset would have undefined behavior. */
979 if( data_size != 0 )
980 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300981 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200982 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200983 /* The mbedtls_pk_xxx functions write to the end of the buffer.
984 * Move the data to the beginning and erase remaining data
985 * at the original location. */
986 if( 2 * (size_t) ret <= data_size )
987 {
988 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200989 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200990 }
991 else if( (size_t) ret < data_size )
992 {
993 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200994 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200995 }
Moran Pekera998bc62018-04-16 18:16:20 +0300996 *data_length = ret;
997 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100998 }
999 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001000#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001001 {
1002 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001003 it is valid for a special-purpose implementation to omit
1004 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001005 return( PSA_ERROR_NOT_SUPPORTED );
1006 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001007 }
1008}
1009
Gilles Peskinec5487a82018-12-03 18:08:14 +01001010psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001011 uint8_t *data,
1012 size_t data_size,
1013 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001014{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001015 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001016 psa_status_t status;
1017
1018 /* Set the key to empty now, so that even when there are errors, we always
1019 * set data_length to a value between 0 and data_size. On error, setting
1020 * the key to empty is a good choice because an empty key representation is
1021 * unlikely to be accepted anywhere. */
1022 *data_length = 0;
1023
1024 /* Export requires the EXPORT flag. There is an exception for public keys,
1025 * which don't require any flag, but psa_get_key_from_slot takes
1026 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001027 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001028 if( status != PSA_SUCCESS )
1029 return( status );
1030 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001031 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001032}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001033
Gilles Peskinec5487a82018-12-03 18:08:14 +01001034psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001035 uint8_t *data,
1036 size_t data_size,
1037 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001038{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001039 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001040 psa_status_t status;
1041
1042 /* Set the key to empty now, so that even when there are errors, we always
1043 * set data_length to a value between 0 and data_size. On error, setting
1044 * the key to empty is a good choice because an empty key representation is
1045 * unlikely to be accepted anywhere. */
1046 *data_length = 0;
1047
1048 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001049 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001050 if( status != PSA_SUCCESS )
1051 return( status );
1052 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001053 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001054}
1055
Darryl Green0c6575a2018-11-07 16:05:30 +00001056#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001057static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001058 size_t bits )
1059{
1060 psa_status_t status;
1061 uint8_t *data;
1062 size_t key_length;
1063 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1064 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001065 if( data == NULL )
1066 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001067 /* Get key data in export format */
1068 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1069 if( status != PSA_SUCCESS )
1070 {
1071 slot->type = PSA_KEY_TYPE_NONE;
1072 goto exit;
1073 }
1074 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001075 status = psa_save_persistent_key( slot->persistent_storage_id,
1076 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001077 data, key_length );
1078 if( status != PSA_SUCCESS )
1079 {
1080 slot->type = PSA_KEY_TYPE_NONE;
1081 }
1082exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001083 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001084 mbedtls_free( data );
1085 return( status );
1086}
1087#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1088
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001089
1090
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001091/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001092/* Message digests */
1093/****************************************************************/
1094
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001095static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001096{
1097 switch( alg )
1098 {
1099#if defined(MBEDTLS_MD2_C)
1100 case PSA_ALG_MD2:
1101 return( &mbedtls_md2_info );
1102#endif
1103#if defined(MBEDTLS_MD4_C)
1104 case PSA_ALG_MD4:
1105 return( &mbedtls_md4_info );
1106#endif
1107#if defined(MBEDTLS_MD5_C)
1108 case PSA_ALG_MD5:
1109 return( &mbedtls_md5_info );
1110#endif
1111#if defined(MBEDTLS_RIPEMD160_C)
1112 case PSA_ALG_RIPEMD160:
1113 return( &mbedtls_ripemd160_info );
1114#endif
1115#if defined(MBEDTLS_SHA1_C)
1116 case PSA_ALG_SHA_1:
1117 return( &mbedtls_sha1_info );
1118#endif
1119#if defined(MBEDTLS_SHA256_C)
1120 case PSA_ALG_SHA_224:
1121 return( &mbedtls_sha224_info );
1122 case PSA_ALG_SHA_256:
1123 return( &mbedtls_sha256_info );
1124#endif
1125#if defined(MBEDTLS_SHA512_C)
1126 case PSA_ALG_SHA_384:
1127 return( &mbedtls_sha384_info );
1128 case PSA_ALG_SHA_512:
1129 return( &mbedtls_sha512_info );
1130#endif
1131 default:
1132 return( NULL );
1133 }
1134}
1135
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001136psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1137{
1138 switch( operation->alg )
1139 {
Gilles Peskine81736312018-06-26 15:04:31 +02001140 case 0:
1141 /* The object has (apparently) been initialized but it is not
1142 * in use. It's ok to call abort on such an object, and there's
1143 * nothing to do. */
1144 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001145#if defined(MBEDTLS_MD2_C)
1146 case PSA_ALG_MD2:
1147 mbedtls_md2_free( &operation->ctx.md2 );
1148 break;
1149#endif
1150#if defined(MBEDTLS_MD4_C)
1151 case PSA_ALG_MD4:
1152 mbedtls_md4_free( &operation->ctx.md4 );
1153 break;
1154#endif
1155#if defined(MBEDTLS_MD5_C)
1156 case PSA_ALG_MD5:
1157 mbedtls_md5_free( &operation->ctx.md5 );
1158 break;
1159#endif
1160#if defined(MBEDTLS_RIPEMD160_C)
1161 case PSA_ALG_RIPEMD160:
1162 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1163 break;
1164#endif
1165#if defined(MBEDTLS_SHA1_C)
1166 case PSA_ALG_SHA_1:
1167 mbedtls_sha1_free( &operation->ctx.sha1 );
1168 break;
1169#endif
1170#if defined(MBEDTLS_SHA256_C)
1171 case PSA_ALG_SHA_224:
1172 case PSA_ALG_SHA_256:
1173 mbedtls_sha256_free( &operation->ctx.sha256 );
1174 break;
1175#endif
1176#if defined(MBEDTLS_SHA512_C)
1177 case PSA_ALG_SHA_384:
1178 case PSA_ALG_SHA_512:
1179 mbedtls_sha512_free( &operation->ctx.sha512 );
1180 break;
1181#endif
1182 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001183 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001184 }
1185 operation->alg = 0;
1186 return( PSA_SUCCESS );
1187}
1188
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001189psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001190 psa_algorithm_t alg )
1191{
1192 int ret;
1193 operation->alg = 0;
1194 switch( alg )
1195 {
1196#if defined(MBEDTLS_MD2_C)
1197 case PSA_ALG_MD2:
1198 mbedtls_md2_init( &operation->ctx.md2 );
1199 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1200 break;
1201#endif
1202#if defined(MBEDTLS_MD4_C)
1203 case PSA_ALG_MD4:
1204 mbedtls_md4_init( &operation->ctx.md4 );
1205 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1206 break;
1207#endif
1208#if defined(MBEDTLS_MD5_C)
1209 case PSA_ALG_MD5:
1210 mbedtls_md5_init( &operation->ctx.md5 );
1211 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1212 break;
1213#endif
1214#if defined(MBEDTLS_RIPEMD160_C)
1215 case PSA_ALG_RIPEMD160:
1216 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1217 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1218 break;
1219#endif
1220#if defined(MBEDTLS_SHA1_C)
1221 case PSA_ALG_SHA_1:
1222 mbedtls_sha1_init( &operation->ctx.sha1 );
1223 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1224 break;
1225#endif
1226#if defined(MBEDTLS_SHA256_C)
1227 case PSA_ALG_SHA_224:
1228 mbedtls_sha256_init( &operation->ctx.sha256 );
1229 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1230 break;
1231 case PSA_ALG_SHA_256:
1232 mbedtls_sha256_init( &operation->ctx.sha256 );
1233 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1234 break;
1235#endif
1236#if defined(MBEDTLS_SHA512_C)
1237 case PSA_ALG_SHA_384:
1238 mbedtls_sha512_init( &operation->ctx.sha512 );
1239 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1240 break;
1241 case PSA_ALG_SHA_512:
1242 mbedtls_sha512_init( &operation->ctx.sha512 );
1243 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1244 break;
1245#endif
1246 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001247 return( PSA_ALG_IS_HASH( alg ) ?
1248 PSA_ERROR_NOT_SUPPORTED :
1249 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001250 }
1251 if( ret == 0 )
1252 operation->alg = alg;
1253 else
1254 psa_hash_abort( operation );
1255 return( mbedtls_to_psa_error( ret ) );
1256}
1257
1258psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1259 const uint8_t *input,
1260 size_t input_length )
1261{
1262 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001263
1264 /* Don't require hash implementations to behave correctly on a
1265 * zero-length input, which may have an invalid pointer. */
1266 if( input_length == 0 )
1267 return( PSA_SUCCESS );
1268
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001269 switch( operation->alg )
1270 {
1271#if defined(MBEDTLS_MD2_C)
1272 case PSA_ALG_MD2:
1273 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1274 input, input_length );
1275 break;
1276#endif
1277#if defined(MBEDTLS_MD4_C)
1278 case PSA_ALG_MD4:
1279 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1280 input, input_length );
1281 break;
1282#endif
1283#if defined(MBEDTLS_MD5_C)
1284 case PSA_ALG_MD5:
1285 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1286 input, input_length );
1287 break;
1288#endif
1289#if defined(MBEDTLS_RIPEMD160_C)
1290 case PSA_ALG_RIPEMD160:
1291 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1292 input, input_length );
1293 break;
1294#endif
1295#if defined(MBEDTLS_SHA1_C)
1296 case PSA_ALG_SHA_1:
1297 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1298 input, input_length );
1299 break;
1300#endif
1301#if defined(MBEDTLS_SHA256_C)
1302 case PSA_ALG_SHA_224:
1303 case PSA_ALG_SHA_256:
1304 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1305 input, input_length );
1306 break;
1307#endif
1308#if defined(MBEDTLS_SHA512_C)
1309 case PSA_ALG_SHA_384:
1310 case PSA_ALG_SHA_512:
1311 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1312 input, input_length );
1313 break;
1314#endif
1315 default:
1316 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1317 break;
1318 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001319
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001320 if( ret != 0 )
1321 psa_hash_abort( operation );
1322 return( mbedtls_to_psa_error( ret ) );
1323}
1324
1325psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1326 uint8_t *hash,
1327 size_t hash_size,
1328 size_t *hash_length )
1329{
itayzafrir40835d42018-08-02 13:14:17 +03001330 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001331 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001332 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001333
1334 /* Fill the output buffer with something that isn't a valid hash
1335 * (barring an attack on the hash and deliberately-crafted input),
1336 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001337 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001338 /* If hash_size is 0 then hash may be NULL and then the
1339 * call to memset would have undefined behavior. */
1340 if( hash_size != 0 )
1341 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001342
1343 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001344 {
1345 status = PSA_ERROR_BUFFER_TOO_SMALL;
1346 goto exit;
1347 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001348
1349 switch( operation->alg )
1350 {
1351#if defined(MBEDTLS_MD2_C)
1352 case PSA_ALG_MD2:
1353 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1354 break;
1355#endif
1356#if defined(MBEDTLS_MD4_C)
1357 case PSA_ALG_MD4:
1358 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1359 break;
1360#endif
1361#if defined(MBEDTLS_MD5_C)
1362 case PSA_ALG_MD5:
1363 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1364 break;
1365#endif
1366#if defined(MBEDTLS_RIPEMD160_C)
1367 case PSA_ALG_RIPEMD160:
1368 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1369 break;
1370#endif
1371#if defined(MBEDTLS_SHA1_C)
1372 case PSA_ALG_SHA_1:
1373 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1374 break;
1375#endif
1376#if defined(MBEDTLS_SHA256_C)
1377 case PSA_ALG_SHA_224:
1378 case PSA_ALG_SHA_256:
1379 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1380 break;
1381#endif
1382#if defined(MBEDTLS_SHA512_C)
1383 case PSA_ALG_SHA_384:
1384 case PSA_ALG_SHA_512:
1385 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1386 break;
1387#endif
1388 default:
1389 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1390 break;
1391 }
itayzafrir40835d42018-08-02 13:14:17 +03001392 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001393
itayzafrir40835d42018-08-02 13:14:17 +03001394exit:
1395 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001396 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001397 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001398 return( psa_hash_abort( operation ) );
1399 }
1400 else
1401 {
1402 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001403 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001404 }
1405}
1406
Gilles Peskine2d277862018-06-18 15:41:12 +02001407psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1408 const uint8_t *hash,
1409 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001410{
1411 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1412 size_t actual_hash_length;
1413 psa_status_t status = psa_hash_finish( operation,
1414 actual_hash, sizeof( actual_hash ),
1415 &actual_hash_length );
1416 if( status != PSA_SUCCESS )
1417 return( status );
1418 if( actual_hash_length != hash_length )
1419 return( PSA_ERROR_INVALID_SIGNATURE );
1420 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1421 return( PSA_ERROR_INVALID_SIGNATURE );
1422 return( PSA_SUCCESS );
1423}
1424
Gilles Peskineeb35d782019-01-22 17:56:16 +01001425psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1426 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001427{
1428 if( target_operation->alg != 0 )
1429 return( PSA_ERROR_BAD_STATE );
1430
1431 switch( source_operation->alg )
1432 {
1433 case 0:
1434 return( PSA_ERROR_BAD_STATE );
1435#if defined(MBEDTLS_MD2_C)
1436 case PSA_ALG_MD2:
1437 mbedtls_md2_clone( &target_operation->ctx.md2,
1438 &source_operation->ctx.md2 );
1439 break;
1440#endif
1441#if defined(MBEDTLS_MD4_C)
1442 case PSA_ALG_MD4:
1443 mbedtls_md4_clone( &target_operation->ctx.md4,
1444 &source_operation->ctx.md4 );
1445 break;
1446#endif
1447#if defined(MBEDTLS_MD5_C)
1448 case PSA_ALG_MD5:
1449 mbedtls_md5_clone( &target_operation->ctx.md5,
1450 &source_operation->ctx.md5 );
1451 break;
1452#endif
1453#if defined(MBEDTLS_RIPEMD160_C)
1454 case PSA_ALG_RIPEMD160:
1455 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1456 &source_operation->ctx.ripemd160 );
1457 break;
1458#endif
1459#if defined(MBEDTLS_SHA1_C)
1460 case PSA_ALG_SHA_1:
1461 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1462 &source_operation->ctx.sha1 );
1463 break;
1464#endif
1465#if defined(MBEDTLS_SHA256_C)
1466 case PSA_ALG_SHA_224:
1467 case PSA_ALG_SHA_256:
1468 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1469 &source_operation->ctx.sha256 );
1470 break;
1471#endif
1472#if defined(MBEDTLS_SHA512_C)
1473 case PSA_ALG_SHA_384:
1474 case PSA_ALG_SHA_512:
1475 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1476 &source_operation->ctx.sha512 );
1477 break;
1478#endif
1479 default:
1480 return( PSA_ERROR_NOT_SUPPORTED );
1481 }
1482
1483 target_operation->alg = source_operation->alg;
1484 return( PSA_SUCCESS );
1485}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001486
1487
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001488/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001489/* MAC */
1490/****************************************************************/
1491
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001492static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001493 psa_algorithm_t alg,
1494 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001495 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001496 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001497{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001498 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001499 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001500
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001501 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001502 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001503
Gilles Peskine8c9def32018-02-08 10:02:12 +01001504 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1505 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001506 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001507 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001508 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001509 mode = MBEDTLS_MODE_STREAM;
1510 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001511 case PSA_ALG_CTR:
1512 mode = MBEDTLS_MODE_CTR;
1513 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001514 case PSA_ALG_CFB:
1515 mode = MBEDTLS_MODE_CFB;
1516 break;
1517 case PSA_ALG_OFB:
1518 mode = MBEDTLS_MODE_OFB;
1519 break;
1520 case PSA_ALG_CBC_NO_PADDING:
1521 mode = MBEDTLS_MODE_CBC;
1522 break;
1523 case PSA_ALG_CBC_PKCS7:
1524 mode = MBEDTLS_MODE_CBC;
1525 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001526 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001527 mode = MBEDTLS_MODE_CCM;
1528 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001529 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001530 mode = MBEDTLS_MODE_GCM;
1531 break;
1532 default:
1533 return( NULL );
1534 }
1535 }
1536 else if( alg == PSA_ALG_CMAC )
1537 mode = MBEDTLS_MODE_ECB;
1538 else if( alg == PSA_ALG_GMAC )
1539 mode = MBEDTLS_MODE_GCM;
1540 else
1541 return( NULL );
1542
1543 switch( key_type )
1544 {
1545 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001546 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001547 break;
1548 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001549 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1550 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001551 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001552 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001553 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001554 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001555 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1556 * but two-key Triple-DES is functionally three-key Triple-DES
1557 * with K1=K3, so that's how we present it to mbedtls. */
1558 if( key_bits == 128 )
1559 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001560 break;
1561 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001562 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001563 break;
1564 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001565 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001566 break;
1567 default:
1568 return( NULL );
1569 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001570 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001571 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001572
Jaeden Amero23bbb752018-06-26 14:16:54 +01001573 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1574 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001575}
1576
Gilles Peskinea05219c2018-11-16 16:02:56 +01001577#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001578static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001579{
Gilles Peskine2d277862018-06-18 15:41:12 +02001580 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001581 {
1582 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001583 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001584 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001585 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001586 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001587 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001588 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001589 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001590 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001591 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001592 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001593 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001594 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001595 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001596 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001597 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001598 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001599 return( 128 );
1600 default:
1601 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001602 }
1603}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001604#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001605
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001606/* Initialize the MAC operation structure. Once this function has been
1607 * called, psa_mac_abort can run and will do the right thing. */
1608static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1609 psa_algorithm_t alg )
1610{
1611 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1612
1613 operation->alg = alg;
1614 operation->key_set = 0;
1615 operation->iv_set = 0;
1616 operation->iv_required = 0;
1617 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001618 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001619
1620#if defined(MBEDTLS_CMAC_C)
1621 if( alg == PSA_ALG_CMAC )
1622 {
1623 operation->iv_required = 0;
1624 mbedtls_cipher_init( &operation->ctx.cmac );
1625 status = PSA_SUCCESS;
1626 }
1627 else
1628#endif /* MBEDTLS_CMAC_C */
1629#if defined(MBEDTLS_MD_C)
1630 if( PSA_ALG_IS_HMAC( operation->alg ) )
1631 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001632 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1633 operation->ctx.hmac.hash_ctx.alg = 0;
1634 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001635 }
1636 else
1637#endif /* MBEDTLS_MD_C */
1638 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001639 if( ! PSA_ALG_IS_MAC( alg ) )
1640 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001641 }
1642
1643 if( status != PSA_SUCCESS )
1644 memset( operation, 0, sizeof( *operation ) );
1645 return( status );
1646}
1647
Gilles Peskine01126fa2018-07-12 17:04:55 +02001648#if defined(MBEDTLS_MD_C)
1649static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1650{
Gilles Peskine3f108122018-12-07 18:14:53 +01001651 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001652 return( psa_hash_abort( &hmac->hash_ctx ) );
1653}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001654
1655static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1656{
1657 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1658 memset( hmac, 0, sizeof( *hmac ) );
1659}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001660#endif /* MBEDTLS_MD_C */
1661
Gilles Peskine8c9def32018-02-08 10:02:12 +01001662psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1663{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001664 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001665 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001666 /* The object has (apparently) been initialized but it is not
1667 * in use. It's ok to call abort on such an object, and there's
1668 * nothing to do. */
1669 return( PSA_SUCCESS );
1670 }
1671 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001672#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001673 if( operation->alg == PSA_ALG_CMAC )
1674 {
1675 mbedtls_cipher_free( &operation->ctx.cmac );
1676 }
1677 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001678#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001679#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001680 if( PSA_ALG_IS_HMAC( operation->alg ) )
1681 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001682 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001683 }
1684 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001685#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001686 {
1687 /* Sanity check (shouldn't happen: operation->alg should
1688 * always have been initialized to a valid value). */
1689 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001690 }
Moran Peker41deec42018-04-04 15:43:05 +03001691
Gilles Peskine8c9def32018-02-08 10:02:12 +01001692 operation->alg = 0;
1693 operation->key_set = 0;
1694 operation->iv_set = 0;
1695 operation->iv_required = 0;
1696 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001697 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001698
Gilles Peskine8c9def32018-02-08 10:02:12 +01001699 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001700
1701bad_state:
1702 /* If abort is called on an uninitialized object, we can't trust
1703 * anything. Wipe the object in case it contains confidential data.
1704 * This may result in a memory leak if a pointer gets overwritten,
1705 * but it's too late to do anything about this. */
1706 memset( operation, 0, sizeof( *operation ) );
1707 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001708}
1709
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001710#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001711static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001712 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001713 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001714 const mbedtls_cipher_info_t *cipher_info )
1715{
1716 int ret;
1717
1718 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001719
1720 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1721 if( ret != 0 )
1722 return( ret );
1723
1724 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1725 slot->data.raw.data,
1726 key_bits );
1727 return( ret );
1728}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001729#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001730
Gilles Peskine248051a2018-06-20 16:09:38 +02001731#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001732static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1733 const uint8_t *key,
1734 size_t key_length,
1735 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001736{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001737 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001738 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001739 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001740 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001741 psa_status_t status;
1742
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001743 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1744 * overflow below. This should never trigger if the hash algorithm
1745 * is implemented correctly. */
1746 /* The size checks against the ipad and opad buffers cannot be written
1747 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1748 * because that triggers -Wlogical-op on GCC 7.3. */
1749 if( block_size > sizeof( ipad ) )
1750 return( PSA_ERROR_NOT_SUPPORTED );
1751 if( block_size > sizeof( hmac->opad ) )
1752 return( PSA_ERROR_NOT_SUPPORTED );
1753 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001754 return( PSA_ERROR_NOT_SUPPORTED );
1755
Gilles Peskined223b522018-06-11 18:12:58 +02001756 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001757 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001758 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001759 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001760 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001761 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001762 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001763 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001764 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001765 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001766 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001767 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001768 }
Gilles Peskine96889972018-07-12 17:07:03 +02001769 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1770 * but it is permitted. It is common when HMAC is used in HKDF, for
1771 * example. Don't call `memcpy` in the 0-length because `key` could be
1772 * an invalid pointer which would make the behavior undefined. */
1773 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001774 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001775
Gilles Peskined223b522018-06-11 18:12:58 +02001776 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1777 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001778 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001779 ipad[i] ^= 0x36;
1780 memset( ipad + key_length, 0x36, block_size - key_length );
1781
1782 /* Copy the key material from ipad to opad, flipping the requisite bits,
1783 * and filling the rest of opad with the requisite constant. */
1784 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001785 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1786 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001787
Gilles Peskine01126fa2018-07-12 17:04:55 +02001788 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001789 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001790 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001791
Gilles Peskine01126fa2018-07-12 17:04:55 +02001792 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001793
1794cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001795 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001796
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001797 return( status );
1798}
Gilles Peskine248051a2018-06-20 16:09:38 +02001799#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001800
Gilles Peskine89167cb2018-07-08 20:12:23 +02001801static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001802 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001803 psa_algorithm_t alg,
1804 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001805{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001806 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001807 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001808 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001809 psa_key_usage_t usage =
1810 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001811 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001812 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001813
Gilles Peskined911eb72018-08-14 15:18:45 +02001814 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001815 if( status != PSA_SUCCESS )
1816 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001817 if( is_sign )
1818 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001819
Gilles Peskinec5487a82018-12-03 18:08:14 +01001820 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001821 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001822 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001823 key_bits = psa_get_key_bits( slot );
1824
Gilles Peskine8c9def32018-02-08 10:02:12 +01001825#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001826 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001827 {
1828 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001829 mbedtls_cipher_info_from_psa( full_length_alg,
1830 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001831 int ret;
1832 if( cipher_info == NULL )
1833 {
1834 status = PSA_ERROR_NOT_SUPPORTED;
1835 goto exit;
1836 }
1837 operation->mac_size = cipher_info->block_size;
1838 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1839 status = mbedtls_to_psa_error( ret );
1840 }
1841 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001842#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001843#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001844 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001845 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001846 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001847 if( hash_alg == 0 )
1848 {
1849 status = PSA_ERROR_NOT_SUPPORTED;
1850 goto exit;
1851 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001852
1853 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1854 /* Sanity check. This shouldn't fail on a valid configuration. */
1855 if( operation->mac_size == 0 ||
1856 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1857 {
1858 status = PSA_ERROR_NOT_SUPPORTED;
1859 goto exit;
1860 }
1861
Gilles Peskine01126fa2018-07-12 17:04:55 +02001862 if( slot->type != PSA_KEY_TYPE_HMAC )
1863 {
1864 status = PSA_ERROR_INVALID_ARGUMENT;
1865 goto exit;
1866 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001867
Gilles Peskine01126fa2018-07-12 17:04:55 +02001868 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1869 slot->data.raw.data,
1870 slot->data.raw.bytes,
1871 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001872 }
1873 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001874#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001875 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00001876 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02001877 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001878 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001879
Gilles Peskined911eb72018-08-14 15:18:45 +02001880 if( truncated == 0 )
1881 {
1882 /* The "normal" case: untruncated algorithm. Nothing to do. */
1883 }
1884 else if( truncated < 4 )
1885 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001886 /* A very short MAC is too short for security since it can be
1887 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1888 * so we make this our minimum, even though 32 bits is still
1889 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001890 status = PSA_ERROR_NOT_SUPPORTED;
1891 }
1892 else if( truncated > operation->mac_size )
1893 {
1894 /* It's impossible to "truncate" to a larger length. */
1895 status = PSA_ERROR_INVALID_ARGUMENT;
1896 }
1897 else
1898 operation->mac_size = truncated;
1899
Gilles Peskinefbfac682018-07-08 20:51:54 +02001900exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001901 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001902 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001903 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001904 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001905 else
1906 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001907 operation->key_set = 1;
1908 }
1909 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001910}
1911
Gilles Peskine89167cb2018-07-08 20:12:23 +02001912psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001913 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001914 psa_algorithm_t alg )
1915{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001916 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001917}
1918
1919psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001920 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001921 psa_algorithm_t alg )
1922{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001923 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001924}
1925
Gilles Peskine8c9def32018-02-08 10:02:12 +01001926psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1927 const uint8_t *input,
1928 size_t input_length )
1929{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001930 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001931 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001932 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001933 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001934 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001935 operation->has_input = 1;
1936
Gilles Peskine8c9def32018-02-08 10:02:12 +01001937#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001938 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001939 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001940 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1941 input, input_length );
1942 status = mbedtls_to_psa_error( ret );
1943 }
1944 else
1945#endif /* MBEDTLS_CMAC_C */
1946#if defined(MBEDTLS_MD_C)
1947 if( PSA_ALG_IS_HMAC( operation->alg ) )
1948 {
1949 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1950 input_length );
1951 }
1952 else
1953#endif /* MBEDTLS_MD_C */
1954 {
1955 /* This shouldn't happen if `operation` was initialized by
1956 * a setup function. */
1957 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001958 }
1959
Gilles Peskinefbfac682018-07-08 20:51:54 +02001960cleanup:
1961 if( status != PSA_SUCCESS )
1962 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001963 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001964}
1965
Gilles Peskine01126fa2018-07-12 17:04:55 +02001966#if defined(MBEDTLS_MD_C)
1967static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1968 uint8_t *mac,
1969 size_t mac_size )
1970{
1971 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1972 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1973 size_t hash_size = 0;
1974 size_t block_size = psa_get_hash_block_size( hash_alg );
1975 psa_status_t status;
1976
Gilles Peskine01126fa2018-07-12 17:04:55 +02001977 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1978 if( status != PSA_SUCCESS )
1979 return( status );
1980 /* From here on, tmp needs to be wiped. */
1981
1982 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1983 if( status != PSA_SUCCESS )
1984 goto exit;
1985
1986 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1987 if( status != PSA_SUCCESS )
1988 goto exit;
1989
1990 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1991 if( status != PSA_SUCCESS )
1992 goto exit;
1993
Gilles Peskined911eb72018-08-14 15:18:45 +02001994 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1995 if( status != PSA_SUCCESS )
1996 goto exit;
1997
1998 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001999
2000exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002001 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002002 return( status );
2003}
2004#endif /* MBEDTLS_MD_C */
2005
mohammad16036df908f2018-04-02 08:34:15 -07002006static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002007 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002008 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002009{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002010 if( ! operation->key_set )
2011 return( PSA_ERROR_BAD_STATE );
2012 if( operation->iv_required && ! operation->iv_set )
2013 return( PSA_ERROR_BAD_STATE );
2014
Gilles Peskine8c9def32018-02-08 10:02:12 +01002015 if( mac_size < operation->mac_size )
2016 return( PSA_ERROR_BUFFER_TOO_SMALL );
2017
Gilles Peskine8c9def32018-02-08 10:02:12 +01002018#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002019 if( operation->alg == PSA_ALG_CMAC )
2020 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002021 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2022 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2023 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002024 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002025 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002026 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002027 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002028 else
2029#endif /* MBEDTLS_CMAC_C */
2030#if defined(MBEDTLS_MD_C)
2031 if( PSA_ALG_IS_HMAC( operation->alg ) )
2032 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002033 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002034 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002035 }
2036 else
2037#endif /* MBEDTLS_MD_C */
2038 {
2039 /* This shouldn't happen if `operation` was initialized by
2040 * a setup function. */
2041 return( PSA_ERROR_BAD_STATE );
2042 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002043}
2044
Gilles Peskineacd4be32018-07-08 19:56:25 +02002045psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2046 uint8_t *mac,
2047 size_t mac_size,
2048 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002049{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002050 psa_status_t status;
2051
2052 /* Fill the output buffer with something that isn't a valid mac
2053 * (barring an attack on the mac and deliberately-crafted input),
2054 * in case the caller doesn't check the return status properly. */
2055 *mac_length = mac_size;
2056 /* If mac_size is 0 then mac may be NULL and then the
2057 * call to memset would have undefined behavior. */
2058 if( mac_size != 0 )
2059 memset( mac, '!', mac_size );
2060
Gilles Peskine89167cb2018-07-08 20:12:23 +02002061 if( ! operation->is_sign )
2062 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002063 status = PSA_ERROR_BAD_STATE;
2064 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002065 }
mohammad16036df908f2018-04-02 08:34:15 -07002066
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002067 status = psa_mac_finish_internal( operation, mac, mac_size );
2068
2069cleanup:
2070 if( status == PSA_SUCCESS )
2071 {
2072 status = psa_mac_abort( operation );
2073 if( status == PSA_SUCCESS )
2074 *mac_length = operation->mac_size;
2075 else
2076 memset( mac, '!', mac_size );
2077 }
2078 else
2079 psa_mac_abort( operation );
2080 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002081}
2082
Gilles Peskineacd4be32018-07-08 19:56:25 +02002083psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2084 const uint8_t *mac,
2085 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002086{
Gilles Peskine828ed142018-06-18 23:25:51 +02002087 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002088 psa_status_t status;
2089
Gilles Peskine89167cb2018-07-08 20:12:23 +02002090 if( operation->is_sign )
2091 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002092 status = PSA_ERROR_BAD_STATE;
2093 goto cleanup;
2094 }
2095 if( operation->mac_size != mac_length )
2096 {
2097 status = PSA_ERROR_INVALID_SIGNATURE;
2098 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002099 }
mohammad16036df908f2018-04-02 08:34:15 -07002100
2101 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002102 actual_mac, sizeof( actual_mac ) );
2103
2104 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2105 status = PSA_ERROR_INVALID_SIGNATURE;
2106
2107cleanup:
2108 if( status == PSA_SUCCESS )
2109 status = psa_mac_abort( operation );
2110 else
2111 psa_mac_abort( operation );
2112
Gilles Peskine3f108122018-12-07 18:14:53 +01002113 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002114
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002115 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002116}
2117
2118
Gilles Peskine20035e32018-02-03 22:44:14 +01002119
Gilles Peskine20035e32018-02-03 22:44:14 +01002120/****************************************************************/
2121/* Asymmetric cryptography */
2122/****************************************************************/
2123
Gilles Peskine2b450e32018-06-27 15:42:46 +02002124#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002125/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002126 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002127static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2128 size_t hash_length,
2129 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002130{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002131 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002132 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002133 *md_alg = mbedtls_md_get_type( md_info );
2134
2135 /* The Mbed TLS RSA module uses an unsigned int for hash length
2136 * parameters. Validate that it fits so that we don't risk an
2137 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002138#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002139 if( hash_length > UINT_MAX )
2140 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002141#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002142
2143#if defined(MBEDTLS_PKCS1_V15)
2144 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2145 * must be correct. */
2146 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2147 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002148 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002149 if( md_info == NULL )
2150 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002151 if( mbedtls_md_get_size( md_info ) != hash_length )
2152 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002153 }
2154#endif /* MBEDTLS_PKCS1_V15 */
2155
2156#if defined(MBEDTLS_PKCS1_V21)
2157 /* PSS requires a hash internally. */
2158 if( PSA_ALG_IS_RSA_PSS( alg ) )
2159 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002160 if( md_info == NULL )
2161 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002162 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002163#endif /* MBEDTLS_PKCS1_V21 */
2164
Gilles Peskine61b91d42018-06-08 16:09:36 +02002165 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002166}
2167
Gilles Peskine2b450e32018-06-27 15:42:46 +02002168static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2169 psa_algorithm_t alg,
2170 const uint8_t *hash,
2171 size_t hash_length,
2172 uint8_t *signature,
2173 size_t signature_size,
2174 size_t *signature_length )
2175{
2176 psa_status_t status;
2177 int ret;
2178 mbedtls_md_type_t md_alg;
2179
2180 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2181 if( status != PSA_SUCCESS )
2182 return( status );
2183
Gilles Peskine630a18a2018-06-29 17:49:35 +02002184 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002185 return( PSA_ERROR_BUFFER_TOO_SMALL );
2186
2187#if defined(MBEDTLS_PKCS1_V15)
2188 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2189 {
2190 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2191 MBEDTLS_MD_NONE );
2192 ret = mbedtls_rsa_pkcs1_sign( rsa,
2193 mbedtls_ctr_drbg_random,
2194 &global_data.ctr_drbg,
2195 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002196 md_alg,
2197 (unsigned int) hash_length,
2198 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002199 signature );
2200 }
2201 else
2202#endif /* MBEDTLS_PKCS1_V15 */
2203#if defined(MBEDTLS_PKCS1_V21)
2204 if( PSA_ALG_IS_RSA_PSS( alg ) )
2205 {
2206 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2207 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2208 mbedtls_ctr_drbg_random,
2209 &global_data.ctr_drbg,
2210 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002211 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002212 (unsigned int) hash_length,
2213 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002214 signature );
2215 }
2216 else
2217#endif /* MBEDTLS_PKCS1_V21 */
2218 {
2219 return( PSA_ERROR_INVALID_ARGUMENT );
2220 }
2221
2222 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002223 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002224 return( mbedtls_to_psa_error( ret ) );
2225}
2226
2227static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2228 psa_algorithm_t alg,
2229 const uint8_t *hash,
2230 size_t hash_length,
2231 const uint8_t *signature,
2232 size_t signature_length )
2233{
2234 psa_status_t status;
2235 int ret;
2236 mbedtls_md_type_t md_alg;
2237
2238 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2239 if( status != PSA_SUCCESS )
2240 return( status );
2241
Gilles Peskine630a18a2018-06-29 17:49:35 +02002242 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002243 return( PSA_ERROR_BUFFER_TOO_SMALL );
2244
2245#if defined(MBEDTLS_PKCS1_V15)
2246 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2247 {
2248 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2249 MBEDTLS_MD_NONE );
2250 ret = mbedtls_rsa_pkcs1_verify( rsa,
2251 mbedtls_ctr_drbg_random,
2252 &global_data.ctr_drbg,
2253 MBEDTLS_RSA_PUBLIC,
2254 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002255 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002256 hash,
2257 signature );
2258 }
2259 else
2260#endif /* MBEDTLS_PKCS1_V15 */
2261#if defined(MBEDTLS_PKCS1_V21)
2262 if( PSA_ALG_IS_RSA_PSS( alg ) )
2263 {
2264 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2265 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2266 mbedtls_ctr_drbg_random,
2267 &global_data.ctr_drbg,
2268 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002269 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002270 (unsigned int) hash_length,
2271 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002272 signature );
2273 }
2274 else
2275#endif /* MBEDTLS_PKCS1_V21 */
2276 {
2277 return( PSA_ERROR_INVALID_ARGUMENT );
2278 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002279
2280 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2281 * the rest of the signature is invalid". This has little use in
2282 * practice and PSA doesn't report this distinction. */
2283 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2284 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002285 return( mbedtls_to_psa_error( ret ) );
2286}
2287#endif /* MBEDTLS_RSA_C */
2288
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002289#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002290/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2291 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2292 * (even though these functions don't modify it). */
2293static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2294 psa_algorithm_t alg,
2295 const uint8_t *hash,
2296 size_t hash_length,
2297 uint8_t *signature,
2298 size_t signature_size,
2299 size_t *signature_length )
2300{
2301 int ret;
2302 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002303 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002304 mbedtls_mpi_init( &r );
2305 mbedtls_mpi_init( &s );
2306
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002307 if( signature_size < 2 * curve_bytes )
2308 {
2309 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2310 goto cleanup;
2311 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002312
Gilles Peskinea05219c2018-11-16 16:02:56 +01002313#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002314 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2315 {
2316 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2317 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2318 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2319 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2320 hash, hash_length,
2321 md_alg ) );
2322 }
2323 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002324#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002325 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002326 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002327 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2328 hash, hash_length,
2329 mbedtls_ctr_drbg_random,
2330 &global_data.ctr_drbg ) );
2331 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002332
2333 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2334 signature,
2335 curve_bytes ) );
2336 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2337 signature + curve_bytes,
2338 curve_bytes ) );
2339
2340cleanup:
2341 mbedtls_mpi_free( &r );
2342 mbedtls_mpi_free( &s );
2343 if( ret == 0 )
2344 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002345 return( mbedtls_to_psa_error( ret ) );
2346}
2347
2348static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2349 const uint8_t *hash,
2350 size_t hash_length,
2351 const uint8_t *signature,
2352 size_t signature_length )
2353{
2354 int ret;
2355 mbedtls_mpi r, s;
2356 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2357 mbedtls_mpi_init( &r );
2358 mbedtls_mpi_init( &s );
2359
2360 if( signature_length != 2 * curve_bytes )
2361 return( PSA_ERROR_INVALID_SIGNATURE );
2362
2363 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2364 signature,
2365 curve_bytes ) );
2366 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2367 signature + curve_bytes,
2368 curve_bytes ) );
2369
2370 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2371 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002372
2373cleanup:
2374 mbedtls_mpi_free( &r );
2375 mbedtls_mpi_free( &s );
2376 return( mbedtls_to_psa_error( ret ) );
2377}
2378#endif /* MBEDTLS_ECDSA_C */
2379
Gilles Peskinec5487a82018-12-03 18:08:14 +01002380psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002381 psa_algorithm_t alg,
2382 const uint8_t *hash,
2383 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002384 uint8_t *signature,
2385 size_t signature_size,
2386 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002387{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002388 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002389 psa_status_t status;
2390
2391 *signature_length = signature_size;
2392
Gilles Peskinec5487a82018-12-03 18:08:14 +01002393 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002394 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002395 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002396 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002397 {
2398 status = PSA_ERROR_INVALID_ARGUMENT;
2399 goto exit;
2400 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002401
Gilles Peskine20035e32018-02-03 22:44:14 +01002402#if defined(MBEDTLS_RSA_C)
2403 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2404 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002405 status = psa_rsa_sign( slot->data.rsa,
2406 alg,
2407 hash, hash_length,
2408 signature, signature_size,
2409 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002410 }
2411 else
2412#endif /* defined(MBEDTLS_RSA_C) */
2413#if defined(MBEDTLS_ECP_C)
2414 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2415 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002416#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002417 if(
2418#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2419 PSA_ALG_IS_ECDSA( alg )
2420#else
2421 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2422#endif
2423 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002424 status = psa_ecdsa_sign( slot->data.ecp,
2425 alg,
2426 hash, hash_length,
2427 signature, signature_size,
2428 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002429 else
2430#endif /* defined(MBEDTLS_ECDSA_C) */
2431 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002432 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002433 }
itayzafrir5c753392018-05-08 11:18:38 +03002434 }
2435 else
2436#endif /* defined(MBEDTLS_ECP_C) */
2437 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002438 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002439 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002440
2441exit:
2442 /* Fill the unused part of the output buffer (the whole buffer on error,
2443 * the trailing part on success) with something that isn't a valid mac
2444 * (barring an attack on the mac and deliberately-crafted input),
2445 * in case the caller doesn't check the return status properly. */
2446 if( status == PSA_SUCCESS )
2447 memset( signature + *signature_length, '!',
2448 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002449 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002450 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002451 /* If signature_size is 0 then we have nothing to do. We must not call
2452 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002453 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002454}
2455
Gilles Peskinec5487a82018-12-03 18:08:14 +01002456psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002457 psa_algorithm_t alg,
2458 const uint8_t *hash,
2459 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002460 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002461 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002462{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002463 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002464 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002465
Gilles Peskinec5487a82018-12-03 18:08:14 +01002466 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002467 if( status != PSA_SUCCESS )
2468 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002469
Gilles Peskine61b91d42018-06-08 16:09:36 +02002470#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002471 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002472 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002473 return( psa_rsa_verify( slot->data.rsa,
2474 alg,
2475 hash, hash_length,
2476 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002477 }
2478 else
2479#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002480#if defined(MBEDTLS_ECP_C)
2481 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2482 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002483#if defined(MBEDTLS_ECDSA_C)
2484 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002485 return( psa_ecdsa_verify( slot->data.ecp,
2486 hash, hash_length,
2487 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002488 else
2489#endif /* defined(MBEDTLS_ECDSA_C) */
2490 {
2491 return( PSA_ERROR_INVALID_ARGUMENT );
2492 }
itayzafrir5c753392018-05-08 11:18:38 +03002493 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002494 else
2495#endif /* defined(MBEDTLS_ECP_C) */
2496 {
2497 return( PSA_ERROR_NOT_SUPPORTED );
2498 }
2499}
2500
Gilles Peskine072ac562018-06-30 00:21:29 +02002501#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2502static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2503 mbedtls_rsa_context *rsa )
2504{
2505 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2506 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2507 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2508 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2509}
2510#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2511
Gilles Peskinec5487a82018-12-03 18:08:14 +01002512psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002513 psa_algorithm_t alg,
2514 const uint8_t *input,
2515 size_t input_length,
2516 const uint8_t *salt,
2517 size_t salt_length,
2518 uint8_t *output,
2519 size_t output_size,
2520 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002521{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002522 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002523 psa_status_t status;
2524
Darryl Green5cc689a2018-07-24 15:34:10 +01002525 (void) input;
2526 (void) input_length;
2527 (void) salt;
2528 (void) output;
2529 (void) output_size;
2530
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002531 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002532
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002533 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2534 return( PSA_ERROR_INVALID_ARGUMENT );
2535
Gilles Peskinec5487a82018-12-03 18:08:14 +01002536 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002537 if( status != PSA_SUCCESS )
2538 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002539 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2540 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002541 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002542
2543#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002544 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002545 {
2546 mbedtls_rsa_context *rsa = slot->data.rsa;
2547 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002548 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002549 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002550#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002551 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002552 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002553 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2554 mbedtls_ctr_drbg_random,
2555 &global_data.ctr_drbg,
2556 MBEDTLS_RSA_PUBLIC,
2557 input_length,
2558 input,
2559 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002560 }
2561 else
2562#endif /* MBEDTLS_PKCS1_V15 */
2563#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002564 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002565 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002566 psa_rsa_oaep_set_padding_mode( alg, rsa );
2567 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2568 mbedtls_ctr_drbg_random,
2569 &global_data.ctr_drbg,
2570 MBEDTLS_RSA_PUBLIC,
2571 salt, salt_length,
2572 input_length,
2573 input,
2574 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002575 }
2576 else
2577#endif /* MBEDTLS_PKCS1_V21 */
2578 {
2579 return( PSA_ERROR_INVALID_ARGUMENT );
2580 }
2581 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002582 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002583 return( mbedtls_to_psa_error( ret ) );
2584 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002585 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002586#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002587 {
2588 return( PSA_ERROR_NOT_SUPPORTED );
2589 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002590}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002591
Gilles Peskinec5487a82018-12-03 18:08:14 +01002592psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002593 psa_algorithm_t alg,
2594 const uint8_t *input,
2595 size_t input_length,
2596 const uint8_t *salt,
2597 size_t salt_length,
2598 uint8_t *output,
2599 size_t output_size,
2600 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002601{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002602 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002603 psa_status_t status;
2604
Darryl Green5cc689a2018-07-24 15:34:10 +01002605 (void) input;
2606 (void) input_length;
2607 (void) salt;
2608 (void) output;
2609 (void) output_size;
2610
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002611 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002612
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002613 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2614 return( PSA_ERROR_INVALID_ARGUMENT );
2615
Gilles Peskinec5487a82018-12-03 18:08:14 +01002616 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002617 if( status != PSA_SUCCESS )
2618 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002619 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2620 return( PSA_ERROR_INVALID_ARGUMENT );
2621
2622#if defined(MBEDTLS_RSA_C)
2623 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2624 {
2625 mbedtls_rsa_context *rsa = slot->data.rsa;
2626 int ret;
2627
Gilles Peskine630a18a2018-06-29 17:49:35 +02002628 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002629 return( PSA_ERROR_INVALID_ARGUMENT );
2630
2631#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002632 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002633 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002634 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2635 mbedtls_ctr_drbg_random,
2636 &global_data.ctr_drbg,
2637 MBEDTLS_RSA_PRIVATE,
2638 output_length,
2639 input,
2640 output,
2641 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002642 }
2643 else
2644#endif /* MBEDTLS_PKCS1_V15 */
2645#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002646 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002647 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002648 psa_rsa_oaep_set_padding_mode( alg, rsa );
2649 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2650 mbedtls_ctr_drbg_random,
2651 &global_data.ctr_drbg,
2652 MBEDTLS_RSA_PRIVATE,
2653 salt, salt_length,
2654 output_length,
2655 input,
2656 output,
2657 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002658 }
2659 else
2660#endif /* MBEDTLS_PKCS1_V21 */
2661 {
2662 return( PSA_ERROR_INVALID_ARGUMENT );
2663 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002664
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002665 return( mbedtls_to_psa_error( ret ) );
2666 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002667 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002668#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002669 {
2670 return( PSA_ERROR_NOT_SUPPORTED );
2671 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002672}
Gilles Peskine20035e32018-02-03 22:44:14 +01002673
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002674
2675
mohammad1603503973b2018-03-12 15:59:30 +02002676/****************************************************************/
2677/* Symmetric cryptography */
2678/****************************************************************/
2679
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002680/* Initialize the cipher operation structure. Once this function has been
2681 * called, psa_cipher_abort can run and will do the right thing. */
2682static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2683 psa_algorithm_t alg )
2684{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002685 if( ! PSA_ALG_IS_CIPHER( alg ) )
2686 {
2687 memset( operation, 0, sizeof( *operation ) );
2688 return( PSA_ERROR_INVALID_ARGUMENT );
2689 }
2690
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002691 operation->alg = alg;
2692 operation->key_set = 0;
2693 operation->iv_set = 0;
2694 operation->iv_required = 1;
2695 operation->iv_size = 0;
2696 operation->block_size = 0;
2697 mbedtls_cipher_init( &operation->ctx.cipher );
2698 return( PSA_SUCCESS );
2699}
2700
Gilles Peskinee553c652018-06-04 16:22:46 +02002701static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002702 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002703 psa_algorithm_t alg,
2704 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002705{
2706 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2707 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002708 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002709 size_t key_bits;
2710 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002711 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2712 PSA_KEY_USAGE_ENCRYPT :
2713 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002714
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002715 status = psa_cipher_init( operation, alg );
2716 if( status != PSA_SUCCESS )
2717 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002718
Gilles Peskinec5487a82018-12-03 18:08:14 +01002719 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002720 if( status != PSA_SUCCESS )
2721 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002722 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002723
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002724 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002725 if( cipher_info == NULL )
2726 return( PSA_ERROR_NOT_SUPPORTED );
2727
mohammad1603503973b2018-03-12 15:59:30 +02002728 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002729 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002730 {
2731 psa_cipher_abort( operation );
2732 return( mbedtls_to_psa_error( ret ) );
2733 }
2734
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002735#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002736 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002737 {
2738 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2739 unsigned char keys[24];
2740 memcpy( keys, slot->data.raw.data, 16 );
2741 memcpy( keys + 16, slot->data.raw.data, 8 );
2742 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2743 keys,
2744 192, cipher_operation );
2745 }
2746 else
2747#endif
2748 {
2749 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2750 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002751 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002752 }
Moran Peker41deec42018-04-04 15:43:05 +03002753 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002754 {
2755 psa_cipher_abort( operation );
2756 return( mbedtls_to_psa_error( ret ) );
2757 }
2758
mohammad16038481e742018-03-18 13:57:31 +02002759#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002760 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002761 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002762 case PSA_ALG_CBC_NO_PADDING:
2763 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2764 MBEDTLS_PADDING_NONE );
2765 break;
2766 case PSA_ALG_CBC_PKCS7:
2767 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2768 MBEDTLS_PADDING_PKCS7 );
2769 break;
2770 default:
2771 /* The algorithm doesn't involve padding. */
2772 ret = 0;
2773 break;
2774 }
2775 if( ret != 0 )
2776 {
2777 psa_cipher_abort( operation );
2778 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002779 }
2780#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2781
mohammad1603503973b2018-03-12 15:59:30 +02002782 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002783 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2784 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2785 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002786 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002787 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002788 }
mohammad1603503973b2018-03-12 15:59:30 +02002789
Moran Peker395db872018-05-31 14:07:14 +03002790 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002791}
2792
Gilles Peskinefe119512018-07-08 21:39:34 +02002793psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002794 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002795 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002796{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002797 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002798}
2799
Gilles Peskinefe119512018-07-08 21:39:34 +02002800psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002801 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002802 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002803{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002804 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002805}
2806
Gilles Peskinefe119512018-07-08 21:39:34 +02002807psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2808 unsigned char *iv,
2809 size_t iv_size,
2810 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002811{
itayzafrir534bd7c2018-08-02 13:56:32 +03002812 psa_status_t status;
2813 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002814 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002815 {
2816 status = PSA_ERROR_BAD_STATE;
2817 goto exit;
2818 }
Moran Peker41deec42018-04-04 15:43:05 +03002819 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002820 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002821 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002822 goto exit;
2823 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002824 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2825 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002826 if( ret != 0 )
2827 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002828 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002829 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002830 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002831
mohammad16038481e742018-03-18 13:57:31 +02002832 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002833 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002834
Moran Peker395db872018-05-31 14:07:14 +03002835exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002836 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002837 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002838 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002839}
2840
Gilles Peskinefe119512018-07-08 21:39:34 +02002841psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2842 const unsigned char *iv,
2843 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002844{
itayzafrir534bd7c2018-08-02 13:56:32 +03002845 psa_status_t status;
2846 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002847 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002848 {
2849 status = PSA_ERROR_BAD_STATE;
2850 goto exit;
2851 }
Moran Pekera28258c2018-05-29 16:25:04 +03002852 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002853 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002854 status = PSA_ERROR_INVALID_ARGUMENT;
2855 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002856 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002857 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2858 status = mbedtls_to_psa_error( ret );
2859exit:
2860 if( status == PSA_SUCCESS )
2861 operation->iv_set = 1;
2862 else
mohammad1603503973b2018-03-12 15:59:30 +02002863 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002864 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002865}
2866
Gilles Peskinee553c652018-06-04 16:22:46 +02002867psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2868 const uint8_t *input,
2869 size_t input_length,
2870 unsigned char *output,
2871 size_t output_size,
2872 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002873{
itayzafrir534bd7c2018-08-02 13:56:32 +03002874 psa_status_t status;
2875 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002876 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002877 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002878 {
2879 /* Take the unprocessed partial block left over from previous
2880 * update calls, if any, plus the input to this call. Remove
2881 * the last partial block, if any. You get the data that will be
2882 * output in this call. */
2883 expected_output_size =
2884 ( operation->ctx.cipher.unprocessed_len + input_length )
2885 / operation->block_size * operation->block_size;
2886 }
2887 else
2888 {
2889 expected_output_size = input_length;
2890 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002891
Gilles Peskine89d789c2018-06-04 17:17:16 +02002892 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002893 {
2894 status = PSA_ERROR_BUFFER_TOO_SMALL;
2895 goto exit;
2896 }
mohammad160382759612018-03-12 18:16:40 +02002897
mohammad1603503973b2018-03-12 15:59:30 +02002898 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002899 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002900 status = mbedtls_to_psa_error( ret );
2901exit:
2902 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002903 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002904 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002905}
2906
Gilles Peskinee553c652018-06-04 16:22:46 +02002907psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2908 uint8_t *output,
2909 size_t output_size,
2910 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002911{
Janos Follath315b51c2018-07-09 16:04:51 +01002912 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2913 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002914 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002915
mohammad1603503973b2018-03-12 15:59:30 +02002916 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002917 {
Janos Follath315b51c2018-07-09 16:04:51 +01002918 status = PSA_ERROR_BAD_STATE;
2919 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002920 }
2921 if( operation->iv_required && ! operation->iv_set )
2922 {
Janos Follath315b51c2018-07-09 16:04:51 +01002923 status = PSA_ERROR_BAD_STATE;
2924 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002925 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002926
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002927 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002928 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2929 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002930 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002931 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002932 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002933 }
2934
Janos Follath315b51c2018-07-09 16:04:51 +01002935 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2936 temp_output_buffer,
2937 output_length );
2938 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002939 {
Janos Follath315b51c2018-07-09 16:04:51 +01002940 status = mbedtls_to_psa_error( cipher_ret );
2941 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002942 }
Janos Follath315b51c2018-07-09 16:04:51 +01002943
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002944 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002945 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002946 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002947 memcpy( output, temp_output_buffer, *output_length );
2948 else
2949 {
Janos Follath315b51c2018-07-09 16:04:51 +01002950 status = PSA_ERROR_BUFFER_TOO_SMALL;
2951 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002952 }
mohammad1603503973b2018-03-12 15:59:30 +02002953
Gilles Peskine3f108122018-12-07 18:14:53 +01002954 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002955 status = psa_cipher_abort( operation );
2956
2957 return( status );
2958
2959error:
2960
2961 *output_length = 0;
2962
Gilles Peskine3f108122018-12-07 18:14:53 +01002963 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002964 (void) psa_cipher_abort( operation );
2965
2966 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002967}
2968
Gilles Peskinee553c652018-06-04 16:22:46 +02002969psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2970{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002971 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002972 {
2973 /* The object has (apparently) been initialized but it is not
2974 * in use. It's ok to call abort on such an object, and there's
2975 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002976 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002977 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002978
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002979 /* Sanity check (shouldn't happen: operation->alg should
2980 * always have been initialized to a valid value). */
2981 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2982 return( PSA_ERROR_BAD_STATE );
2983
mohammad1603503973b2018-03-12 15:59:30 +02002984 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002985
Moran Peker41deec42018-04-04 15:43:05 +03002986 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002987 operation->key_set = 0;
2988 operation->iv_set = 0;
2989 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002990 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002991 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002992
Moran Peker395db872018-05-31 14:07:14 +03002993 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002994}
2995
Gilles Peskinea0655c32018-04-30 17:06:50 +02002996
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002997
mohammad16038cc1cee2018-03-28 01:21:33 +03002998/****************************************************************/
2999/* Key Policy */
3000/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003001
mohammad160327010052018-07-03 13:16:15 +03003002#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003003void psa_key_policy_set_usage( psa_key_policy_t *policy,
3004 psa_key_usage_t usage,
3005 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003006{
mohammad16034eed7572018-03-28 05:14:59 -07003007 policy->usage = usage;
3008 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003009}
3010
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003011psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003012{
mohammad16036df908f2018-04-02 08:34:15 -07003013 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003014}
3015
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003016psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003017{
mohammad16036df908f2018-04-02 08:34:15 -07003018 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003019}
mohammad160327010052018-07-03 13:16:15 +03003020#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003021
Gilles Peskinec5487a82018-12-03 18:08:14 +01003022psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003023 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003024{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003025 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003026 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003027
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003028 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003029 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003030
Gilles Peskinec5487a82018-12-03 18:08:14 +01003031 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003032 if( status != PSA_SUCCESS )
3033 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003034
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003035 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3036 PSA_KEY_USAGE_ENCRYPT |
3037 PSA_KEY_USAGE_DECRYPT |
3038 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003039 PSA_KEY_USAGE_VERIFY |
3040 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003041 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003042
mohammad16036df908f2018-04-02 08:34:15 -07003043 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003044
3045 return( PSA_SUCCESS );
3046}
3047
Gilles Peskinec5487a82018-12-03 18:08:14 +01003048psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003049 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003050{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003051 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003052 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003053
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003054 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003055 return( PSA_ERROR_INVALID_ARGUMENT );
3056
Gilles Peskinec5487a82018-12-03 18:08:14 +01003057 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003058 if( status != PSA_SUCCESS )
3059 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003060
mohammad16036df908f2018-04-02 08:34:15 -07003061 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003062
3063 return( PSA_SUCCESS );
3064}
Gilles Peskine20035e32018-02-03 22:44:14 +01003065
Gilles Peskinea0655c32018-04-30 17:06:50 +02003066
3067
mohammad1603804cd712018-03-20 22:44:08 +02003068/****************************************************************/
3069/* Key Lifetime */
3070/****************************************************************/
3071
Gilles Peskinec5487a82018-12-03 18:08:14 +01003072psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003073 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003074{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003075 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003076 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003077
Gilles Peskinec5487a82018-12-03 18:08:14 +01003078 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003079 if( status != PSA_SUCCESS )
3080 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003081
mohammad1603804cd712018-03-20 22:44:08 +02003082 *lifetime = slot->lifetime;
3083
3084 return( PSA_SUCCESS );
3085}
3086
Gilles Peskine20035e32018-02-03 22:44:14 +01003087
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003088
mohammad16035955c982018-04-26 00:53:03 +03003089/****************************************************************/
3090/* AEAD */
3091/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003092
Gilles Peskineedf9a652018-08-17 18:11:56 +02003093typedef struct
3094{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003095 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003096 const mbedtls_cipher_info_t *cipher_info;
3097 union
3098 {
3099#if defined(MBEDTLS_CCM_C)
3100 mbedtls_ccm_context ccm;
3101#endif /* MBEDTLS_CCM_C */
3102#if defined(MBEDTLS_GCM_C)
3103 mbedtls_gcm_context gcm;
3104#endif /* MBEDTLS_GCM_C */
3105 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003106 psa_algorithm_t core_alg;
3107 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003108 uint8_t tag_length;
3109} aead_operation_t;
3110
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003111static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003112{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003113 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003114 {
3115#if defined(MBEDTLS_CCM_C)
3116 case PSA_ALG_CCM:
3117 mbedtls_ccm_free( &operation->ctx.ccm );
3118 break;
3119#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003120#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003121 case PSA_ALG_GCM:
3122 mbedtls_gcm_free( &operation->ctx.gcm );
3123 break;
3124#endif /* MBEDTLS_GCM_C */
3125 }
3126}
3127
3128static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003129 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003130 psa_key_usage_t usage,
3131 psa_algorithm_t alg )
3132{
3133 psa_status_t status;
3134 size_t key_bits;
3135 mbedtls_cipher_id_t cipher_id;
3136
Gilles Peskinec5487a82018-12-03 18:08:14 +01003137 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003138 if( status != PSA_SUCCESS )
3139 return( status );
3140
3141 key_bits = psa_get_key_bits( operation->slot );
3142
3143 operation->cipher_info =
3144 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3145 &cipher_id );
3146 if( operation->cipher_info == NULL )
3147 return( PSA_ERROR_NOT_SUPPORTED );
3148
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003149 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003150 {
3151#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003152 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3153 operation->core_alg = PSA_ALG_CCM;
3154 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003155 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3156 return( PSA_ERROR_INVALID_ARGUMENT );
3157 mbedtls_ccm_init( &operation->ctx.ccm );
3158 status = mbedtls_to_psa_error(
3159 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3160 operation->slot->data.raw.data,
3161 (unsigned int) key_bits ) );
3162 if( status != 0 )
3163 goto cleanup;
3164 break;
3165#endif /* MBEDTLS_CCM_C */
3166
3167#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003168 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3169 operation->core_alg = PSA_ALG_GCM;
3170 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003171 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3172 return( PSA_ERROR_INVALID_ARGUMENT );
3173 mbedtls_gcm_init( &operation->ctx.gcm );
3174 status = mbedtls_to_psa_error(
3175 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3176 operation->slot->data.raw.data,
3177 (unsigned int) key_bits ) );
3178 break;
3179#endif /* MBEDTLS_GCM_C */
3180
3181 default:
3182 return( PSA_ERROR_NOT_SUPPORTED );
3183 }
3184
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003185 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3186 {
3187 status = PSA_ERROR_INVALID_ARGUMENT;
3188 goto cleanup;
3189 }
3190 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3191 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3192 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3193 * In both cases, mbedtls_xxx will validate the tag length below. */
3194
Gilles Peskineedf9a652018-08-17 18:11:56 +02003195 return( PSA_SUCCESS );
3196
3197cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003198 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003199 return( status );
3200}
3201
Gilles Peskinec5487a82018-12-03 18:08:14 +01003202psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003203 psa_algorithm_t alg,
3204 const uint8_t *nonce,
3205 size_t nonce_length,
3206 const uint8_t *additional_data,
3207 size_t additional_data_length,
3208 const uint8_t *plaintext,
3209 size_t plaintext_length,
3210 uint8_t *ciphertext,
3211 size_t ciphertext_size,
3212 size_t *ciphertext_length )
3213{
mohammad16035955c982018-04-26 00:53:03 +03003214 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003215 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003216 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003217
mohammad1603f08a5502018-06-03 15:05:47 +03003218 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003219
Gilles Peskinec5487a82018-12-03 18:08:14 +01003220 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003221 if( status != PSA_SUCCESS )
3222 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003223
Gilles Peskineedf9a652018-08-17 18:11:56 +02003224 /* For all currently supported modes, the tag is at the end of the
3225 * ciphertext. */
3226 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3227 {
3228 status = PSA_ERROR_BUFFER_TOO_SMALL;
3229 goto exit;
3230 }
3231 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003232
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003233#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003234 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003235 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003236 status = mbedtls_to_psa_error(
3237 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3238 MBEDTLS_GCM_ENCRYPT,
3239 plaintext_length,
3240 nonce, nonce_length,
3241 additional_data, additional_data_length,
3242 plaintext, ciphertext,
3243 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003244 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003245 else
3246#endif /* MBEDTLS_GCM_C */
3247#if defined(MBEDTLS_CCM_C)
3248 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003249 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003250 status = mbedtls_to_psa_error(
3251 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3252 plaintext_length,
3253 nonce, nonce_length,
3254 additional_data,
3255 additional_data_length,
3256 plaintext, ciphertext,
3257 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003258 }
mohammad16035c8845f2018-05-09 05:40:09 -07003259 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003260#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003261 {
mohammad1603554faad2018-06-03 15:07:38 +03003262 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003263 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003264
Gilles Peskineedf9a652018-08-17 18:11:56 +02003265 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3266 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003267
Gilles Peskineedf9a652018-08-17 18:11:56 +02003268exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003269 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003270 if( status == PSA_SUCCESS )
3271 *ciphertext_length = plaintext_length + operation.tag_length;
3272 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003273}
3274
Gilles Peskineee652a32018-06-01 19:23:52 +02003275/* Locate the tag in a ciphertext buffer containing the encrypted data
3276 * followed by the tag. Return the length of the part preceding the tag in
3277 * *plaintext_length. This is the size of the plaintext in modes where
3278 * the encrypted data has the same size as the plaintext, such as
3279 * CCM and GCM. */
3280static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3281 const uint8_t *ciphertext,
3282 size_t ciphertext_length,
3283 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003284 const uint8_t **p_tag )
3285{
3286 size_t payload_length;
3287 if( tag_length > ciphertext_length )
3288 return( PSA_ERROR_INVALID_ARGUMENT );
3289 payload_length = ciphertext_length - tag_length;
3290 if( payload_length > plaintext_size )
3291 return( PSA_ERROR_BUFFER_TOO_SMALL );
3292 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003293 return( PSA_SUCCESS );
3294}
3295
Gilles Peskinec5487a82018-12-03 18:08:14 +01003296psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003297 psa_algorithm_t alg,
3298 const uint8_t *nonce,
3299 size_t nonce_length,
3300 const uint8_t *additional_data,
3301 size_t additional_data_length,
3302 const uint8_t *ciphertext,
3303 size_t ciphertext_length,
3304 uint8_t *plaintext,
3305 size_t plaintext_size,
3306 size_t *plaintext_length )
3307{
mohammad16035955c982018-04-26 00:53:03 +03003308 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003309 aead_operation_t operation;
3310 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003311
Gilles Peskineee652a32018-06-01 19:23:52 +02003312 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003313
Gilles Peskinec5487a82018-12-03 18:08:14 +01003314 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003315 if( status != PSA_SUCCESS )
3316 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003317
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003318#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003319 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003320 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003321 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003322 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003323 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003324 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003325 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003326
Gilles Peskineedf9a652018-08-17 18:11:56 +02003327 status = mbedtls_to_psa_error(
3328 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3329 ciphertext_length - operation.tag_length,
3330 nonce, nonce_length,
3331 additional_data,
3332 additional_data_length,
3333 tag, operation.tag_length,
3334 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003335 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003336 else
3337#endif /* MBEDTLS_GCM_C */
3338#if defined(MBEDTLS_CCM_C)
3339 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003340 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003341 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003342 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003343 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003344 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003345 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003346
Gilles Peskineedf9a652018-08-17 18:11:56 +02003347 status = mbedtls_to_psa_error(
3348 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3349 ciphertext_length - operation.tag_length,
3350 nonce, nonce_length,
3351 additional_data,
3352 additional_data_length,
3353 ciphertext, plaintext,
3354 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003355 }
mohammad160339574652018-06-01 04:39:53 -07003356 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003357#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003358 {
mohammad1603554faad2018-06-03 15:07:38 +03003359 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003360 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003361
Gilles Peskineedf9a652018-08-17 18:11:56 +02003362 if( status != PSA_SUCCESS && plaintext_size != 0 )
3363 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003364
Gilles Peskineedf9a652018-08-17 18:11:56 +02003365exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003366 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003367 if( status == PSA_SUCCESS )
3368 *plaintext_length = ciphertext_length - operation.tag_length;
3369 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003370}
3371
Gilles Peskinea0655c32018-04-30 17:06:50 +02003372
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003373
Gilles Peskine20035e32018-02-03 22:44:14 +01003374/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003375/* Generators */
3376/****************************************************************/
3377
3378psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3379{
3380 psa_status_t status = PSA_SUCCESS;
3381 if( generator->alg == 0 )
3382 {
3383 /* The object has (apparently) been initialized but it is not
3384 * in use. It's ok to call abort on such an object, and there's
3385 * nothing to do. */
3386 }
3387 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003388 if( generator->alg == PSA_ALG_SELECT_RAW )
3389 {
3390 if( generator->ctx.buffer.data != NULL )
3391 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003392 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003393 generator->ctx.buffer.size );
3394 mbedtls_free( generator->ctx.buffer.data );
3395 }
3396 }
3397 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003398#if defined(MBEDTLS_MD_C)
3399 if( PSA_ALG_IS_HKDF( generator->alg ) )
3400 {
3401 mbedtls_free( generator->ctx.hkdf.info );
3402 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3403 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003404 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3405 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3406 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003407 {
3408 if( generator->ctx.tls12_prf.key != NULL )
3409 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003410 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003411 generator->ctx.tls12_prf.key_len );
3412 mbedtls_free( generator->ctx.tls12_prf.key );
3413 }
Hanno Becker580fba12018-11-13 20:50:45 +00003414
3415 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3416 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003417 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003418 generator->ctx.tls12_prf.Ai_with_seed_len );
3419 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3420 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003421 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003422 else
3423#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003424 {
3425 status = PSA_ERROR_BAD_STATE;
3426 }
3427 memset( generator, 0, sizeof( *generator ) );
3428 return( status );
3429}
3430
3431
3432psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3433 size_t *capacity)
3434{
3435 *capacity = generator->capacity;
3436 return( PSA_SUCCESS );
3437}
3438
Gilles Peskinebef7f142018-07-12 17:22:21 +02003439#if defined(MBEDTLS_MD_C)
3440/* Read some bytes from an HKDF-based generator. This performs a chunk
3441 * of the expand phase of the HKDF algorithm. */
3442static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3443 psa_algorithm_t hash_alg,
3444 uint8_t *output,
3445 size_t output_length )
3446{
3447 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3448 psa_status_t status;
3449
3450 while( output_length != 0 )
3451 {
3452 /* Copy what remains of the current block */
3453 uint8_t n = hash_length - hkdf->offset_in_block;
3454 if( n > output_length )
3455 n = (uint8_t) output_length;
3456 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3457 output += n;
3458 output_length -= n;
3459 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003460 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003461 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003462 /* We can't be wanting more output after block 0xff, otherwise
3463 * the capacity check in psa_generator_read() would have
3464 * prevented this call. It could happen only if the generator
3465 * object was corrupted or if this function is called directly
3466 * inside the library. */
3467 if( hkdf->block_number == 0xff )
3468 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003469
3470 /* We need a new block */
3471 ++hkdf->block_number;
3472 hkdf->offset_in_block = 0;
3473 status = psa_hmac_setup_internal( &hkdf->hmac,
3474 hkdf->prk, hash_length,
3475 hash_alg );
3476 if( status != PSA_SUCCESS )
3477 return( status );
3478 if( hkdf->block_number != 1 )
3479 {
3480 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3481 hkdf->output_block,
3482 hash_length );
3483 if( status != PSA_SUCCESS )
3484 return( status );
3485 }
3486 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3487 hkdf->info,
3488 hkdf->info_length );
3489 if( status != PSA_SUCCESS )
3490 return( status );
3491 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3492 &hkdf->block_number, 1 );
3493 if( status != PSA_SUCCESS )
3494 return( status );
3495 status = psa_hmac_finish_internal( &hkdf->hmac,
3496 hkdf->output_block,
3497 sizeof( hkdf->output_block ) );
3498 if( status != PSA_SUCCESS )
3499 return( status );
3500 }
3501
3502 return( PSA_SUCCESS );
3503}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003504
3505static psa_status_t psa_generator_tls12_prf_generate_next_block(
3506 psa_tls12_prf_generator_t *tls12_prf,
3507 psa_algorithm_t alg )
3508{
3509 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3510 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3511 psa_hmac_internal_data hmac;
3512 psa_status_t status, cleanup_status;
3513
Hanno Becker3b339e22018-11-13 20:56:14 +00003514 unsigned char *Ai;
3515 size_t Ai_len;
3516
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003517 /* We can't be wanting more output after block 0xff, otherwise
3518 * the capacity check in psa_generator_read() would have
3519 * prevented this call. It could happen only if the generator
3520 * object was corrupted or if this function is called directly
3521 * inside the library. */
3522 if( tls12_prf->block_number == 0xff )
3523 return( PSA_ERROR_BAD_STATE );
3524
3525 /* We need a new block */
3526 ++tls12_prf->block_number;
3527 tls12_prf->offset_in_block = 0;
3528
3529 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3530 *
3531 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3532 *
3533 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3534 * HMAC_hash(secret, A(2) + seed) +
3535 * HMAC_hash(secret, A(3) + seed) + ...
3536 *
3537 * A(0) = seed
3538 * A(i) = HMAC_hash( secret, A(i-1) )
3539 *
3540 * The `psa_tls12_prf_generator` structures saves the block
3541 * `HMAC_hash(secret, A(i) + seed)` from which the output
3542 * is currently extracted as `output_block`, while
3543 * `A(i) + seed` is stored in `Ai_with_seed`.
3544 *
3545 * Generating a new block means recalculating `Ai_with_seed`
3546 * from the A(i)-part of it, and afterwards recalculating
3547 * `output_block`.
3548 *
3549 * A(0) is computed at setup time.
3550 *
3551 */
3552
3553 psa_hmac_init_internal( &hmac );
3554
3555 /* We must distinguish the calculation of A(1) from those
3556 * of A(2) and higher, because A(0)=seed has a different
3557 * length than the other A(i). */
3558 if( tls12_prf->block_number == 1 )
3559 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003560 Ai = tls12_prf->Ai_with_seed + hash_length;
3561 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003562 }
3563 else
3564 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003565 Ai = tls12_prf->Ai_with_seed;
3566 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003567 }
3568
Hanno Becker3b339e22018-11-13 20:56:14 +00003569 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3570 status = psa_hmac_setup_internal( &hmac,
3571 tls12_prf->key,
3572 tls12_prf->key_len,
3573 hash_alg );
3574 if( status != PSA_SUCCESS )
3575 goto cleanup;
3576
3577 status = psa_hash_update( &hmac.hash_ctx,
3578 Ai, Ai_len );
3579 if( status != PSA_SUCCESS )
3580 goto cleanup;
3581
3582 status = psa_hmac_finish_internal( &hmac,
3583 tls12_prf->Ai_with_seed,
3584 hash_length );
3585 if( status != PSA_SUCCESS )
3586 goto cleanup;
3587
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003588 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3589 status = psa_hmac_setup_internal( &hmac,
3590 tls12_prf->key,
3591 tls12_prf->key_len,
3592 hash_alg );
3593 if( status != PSA_SUCCESS )
3594 goto cleanup;
3595
3596 status = psa_hash_update( &hmac.hash_ctx,
3597 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003598 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003599 if( status != PSA_SUCCESS )
3600 goto cleanup;
3601
3602 status = psa_hmac_finish_internal( &hmac,
3603 tls12_prf->output_block,
3604 hash_length );
3605 if( status != PSA_SUCCESS )
3606 goto cleanup;
3607
3608cleanup:
3609
3610 cleanup_status = psa_hmac_abort_internal( &hmac );
3611 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3612 status = cleanup_status;
3613
3614 return( status );
3615}
3616
3617/* Read some bytes from an TLS-1.2-PRF-based generator.
3618 * See Section 5 of RFC 5246. */
3619static psa_status_t psa_generator_tls12_prf_read(
3620 psa_tls12_prf_generator_t *tls12_prf,
3621 psa_algorithm_t alg,
3622 uint8_t *output,
3623 size_t output_length )
3624{
3625 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3626 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3627 psa_status_t status;
3628
3629 while( output_length != 0 )
3630 {
3631 /* Copy what remains of the current block */
3632 uint8_t n = hash_length - tls12_prf->offset_in_block;
3633
3634 /* Check if we have fully processed the current block. */
3635 if( n == 0 )
3636 {
3637 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3638 alg );
3639 if( status != PSA_SUCCESS )
3640 return( status );
3641
3642 continue;
3643 }
3644
3645 if( n > output_length )
3646 n = (uint8_t) output_length;
3647 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3648 n );
3649 output += n;
3650 output_length -= n;
3651 tls12_prf->offset_in_block += n;
3652 }
3653
3654 return( PSA_SUCCESS );
3655}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003656#endif /* MBEDTLS_MD_C */
3657
Gilles Peskineeab56e42018-07-12 17:12:33 +02003658psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3659 uint8_t *output,
3660 size_t output_length )
3661{
3662 psa_status_t status;
3663
3664 if( output_length > generator->capacity )
3665 {
3666 generator->capacity = 0;
3667 /* Go through the error path to wipe all confidential data now
3668 * that the generator object is useless. */
3669 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3670 goto exit;
3671 }
3672 if( output_length == 0 &&
3673 generator->capacity == 0 && generator->alg == 0 )
3674 {
3675 /* Edge case: this is a blank or finished generator, and 0
3676 * bytes were requested. The right error in this case could
3677 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3678 * INSUFFICIENT_CAPACITY, which is right for a finished
3679 * generator, for consistency with the case when
3680 * output_length > 0. */
3681 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3682 }
3683 generator->capacity -= output_length;
3684
Gilles Peskine751d9652018-09-18 12:05:44 +02003685 if( generator->alg == PSA_ALG_SELECT_RAW )
3686 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003687 /* Initially, the capacity of a selection generator is always
3688 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3689 * abbreviated in this comment as `size`. When the remaining
3690 * capacity is `c`, the next bytes to serve start `c` bytes
3691 * from the end of the buffer, i.e. `size - c` from the
3692 * beginning of the buffer. Since `generator->capacity` was just
3693 * decremented above, we need to serve the bytes from
3694 * `size - generator->capacity - output_length` to
3695 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003696 size_t offset =
3697 generator->ctx.buffer.size - generator->capacity - output_length;
3698 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3699 status = PSA_SUCCESS;
3700 }
3701 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003702#if defined(MBEDTLS_MD_C)
3703 if( PSA_ALG_IS_HKDF( generator->alg ) )
3704 {
3705 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3706 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3707 output, output_length );
3708 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003709 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3710 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003711 {
3712 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3713 generator->alg, output,
3714 output_length );
3715 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003716 else
3717#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003718 {
3719 return( PSA_ERROR_BAD_STATE );
3720 }
3721
3722exit:
3723 if( status != PSA_SUCCESS )
3724 {
3725 psa_generator_abort( generator );
3726 memset( output, '!', output_length );
3727 }
3728 return( status );
3729}
3730
Gilles Peskine08542d82018-07-19 17:05:42 +02003731#if defined(MBEDTLS_DES_C)
3732static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3733{
3734 if( data_size >= 8 )
3735 mbedtls_des_key_set_parity( data );
3736 if( data_size >= 16 )
3737 mbedtls_des_key_set_parity( data + 8 );
3738 if( data_size >= 24 )
3739 mbedtls_des_key_set_parity( data + 16 );
3740}
3741#endif /* MBEDTLS_DES_C */
3742
Gilles Peskinec5487a82018-12-03 18:08:14 +01003743psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003744 psa_key_type_t type,
3745 size_t bits,
3746 psa_crypto_generator_t *generator )
3747{
3748 uint8_t *data = NULL;
3749 size_t bytes = PSA_BITS_TO_BYTES( bits );
3750 psa_status_t status;
3751
3752 if( ! key_type_is_raw_bytes( type ) )
3753 return( PSA_ERROR_INVALID_ARGUMENT );
3754 if( bits % 8 != 0 )
3755 return( PSA_ERROR_INVALID_ARGUMENT );
3756 data = mbedtls_calloc( 1, bytes );
3757 if( data == NULL )
3758 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3759
3760 status = psa_generator_read( generator, data, bytes );
3761 if( status != PSA_SUCCESS )
3762 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003763#if defined(MBEDTLS_DES_C)
3764 if( type == PSA_KEY_TYPE_DES )
3765 psa_des_set_key_parity( data, bytes );
3766#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003767 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003768
3769exit:
3770 mbedtls_free( data );
3771 return( status );
3772}
3773
3774
3775
3776/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003777/* Key derivation */
3778/****************************************************************/
3779
Gilles Peskinea05219c2018-11-16 16:02:56 +01003780#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003781/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003782 * of the HKDF algorithm.
3783 *
3784 * Note that if this function fails, you must call psa_generator_abort()
3785 * to potentially free embedded data structures and wipe confidential data.
3786 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003787static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003788 const uint8_t *secret,
3789 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003790 psa_algorithm_t hash_alg,
3791 const uint8_t *salt,
3792 size_t salt_length,
3793 const uint8_t *label,
3794 size_t label_length )
3795{
3796 psa_status_t status;
3797 status = psa_hmac_setup_internal( &hkdf->hmac,
3798 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003799 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003800 if( status != PSA_SUCCESS )
3801 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003802 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003803 if( status != PSA_SUCCESS )
3804 return( status );
3805 status = psa_hmac_finish_internal( &hkdf->hmac,
3806 hkdf->prk,
3807 sizeof( hkdf->prk ) );
3808 if( status != PSA_SUCCESS )
3809 return( status );
3810 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3811 hkdf->block_number = 0;
3812 hkdf->info_length = label_length;
3813 if( label_length != 0 )
3814 {
3815 hkdf->info = mbedtls_calloc( 1, label_length );
3816 if( hkdf->info == NULL )
3817 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3818 memcpy( hkdf->info, label, label_length );
3819 }
3820 return( PSA_SUCCESS );
3821}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003822#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003823
Gilles Peskinea05219c2018-11-16 16:02:56 +01003824#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003825/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3826 *
3827 * Note that if this function fails, you must call psa_generator_abort()
3828 * to potentially free embedded data structures and wipe confidential data.
3829 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003830static psa_status_t psa_generator_tls12_prf_setup(
3831 psa_tls12_prf_generator_t *tls12_prf,
3832 const unsigned char *key,
3833 size_t key_len,
3834 psa_algorithm_t hash_alg,
3835 const uint8_t *salt,
3836 size_t salt_length,
3837 const uint8_t *label,
3838 size_t label_length )
3839{
3840 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003841 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3842 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003843
3844 tls12_prf->key = mbedtls_calloc( 1, key_len );
3845 if( tls12_prf->key == NULL )
3846 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3847 tls12_prf->key_len = key_len;
3848 memcpy( tls12_prf->key, key, key_len );
3849
Hanno Becker580fba12018-11-13 20:50:45 +00003850 overflow = ( salt_length + label_length < salt_length ) ||
3851 ( salt_length + label_length + hash_length < hash_length );
3852 if( overflow )
3853 return( PSA_ERROR_INVALID_ARGUMENT );
3854
3855 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3856 if( tls12_prf->Ai_with_seed == NULL )
3857 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3858 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3859
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003860 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3861 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003862 if( label_length != 0 )
3863 {
3864 memcpy( tls12_prf->Ai_with_seed + hash_length,
3865 label, label_length );
3866 }
3867
3868 if( salt_length != 0 )
3869 {
3870 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3871 salt, salt_length );
3872 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003873
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003874 /* The first block gets generated when
3875 * psa_generator_read() is called. */
3876 tls12_prf->block_number = 0;
3877 tls12_prf->offset_in_block = hash_length;
3878
3879 return( PSA_SUCCESS );
3880}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003881
3882/* Set up a TLS-1.2-PSK-to-MS-based generator. */
3883static psa_status_t psa_generator_tls12_psk_to_ms_setup(
3884 psa_tls12_prf_generator_t *tls12_prf,
3885 const unsigned char *psk,
3886 size_t psk_len,
3887 psa_algorithm_t hash_alg,
3888 const uint8_t *salt,
3889 size_t salt_length,
3890 const uint8_t *label,
3891 size_t label_length )
3892{
3893 psa_status_t status;
3894 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
3895
3896 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
3897 return( PSA_ERROR_INVALID_ARGUMENT );
3898
3899 /* Quoting RFC 4279, Section 2:
3900 *
3901 * The premaster secret is formed as follows: if the PSK is N octets
3902 * long, concatenate a uint16 with the value N, N zero octets, a second
3903 * uint16 with the value N, and the PSK itself.
3904 */
3905
3906 pms[0] = ( psk_len >> 8 ) & 0xff;
3907 pms[1] = ( psk_len >> 0 ) & 0xff;
3908 memset( pms + 2, 0, psk_len );
3909 pms[2 + psk_len + 0] = pms[0];
3910 pms[2 + psk_len + 1] = pms[1];
3911 memcpy( pms + 4 + psk_len, psk, psk_len );
3912
3913 status = psa_generator_tls12_prf_setup( tls12_prf,
3914 pms, 4 + 2 * psk_len,
3915 hash_alg,
3916 salt, salt_length,
3917 label, label_length );
3918
Gilles Peskine3f108122018-12-07 18:14:53 +01003919 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00003920 return( status );
3921}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003922#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003923
Gilles Peskine346797d2018-11-16 16:05:06 +01003924/* Note that if this function fails, you must call psa_generator_abort()
3925 * to potentially free embedded data structures and wipe confidential data.
3926 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003927static psa_status_t psa_key_derivation_internal(
3928 psa_crypto_generator_t *generator,
3929 const uint8_t *secret, size_t secret_length,
3930 psa_algorithm_t alg,
3931 const uint8_t *salt, size_t salt_length,
3932 const uint8_t *label, size_t label_length,
3933 size_t capacity )
3934{
3935 psa_status_t status;
3936 size_t max_capacity;
3937
3938 /* Set generator->alg even on failure so that abort knows what to do. */
3939 generator->alg = alg;
3940
Gilles Peskine751d9652018-09-18 12:05:44 +02003941 if( alg == PSA_ALG_SELECT_RAW )
3942 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003943 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02003944 if( salt_length != 0 )
3945 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01003946 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02003947 if( label_length != 0 )
3948 return( PSA_ERROR_INVALID_ARGUMENT );
3949 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
3950 if( generator->ctx.buffer.data == NULL )
3951 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3952 memcpy( generator->ctx.buffer.data, secret, secret_length );
3953 generator->ctx.buffer.size = secret_length;
3954 max_capacity = secret_length;
3955 status = PSA_SUCCESS;
3956 }
3957 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003958#if defined(MBEDTLS_MD_C)
3959 if( PSA_ALG_IS_HKDF( alg ) )
3960 {
3961 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3962 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3963 if( hash_size == 0 )
3964 return( PSA_ERROR_NOT_SUPPORTED );
3965 max_capacity = 255 * hash_size;
3966 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3967 secret, secret_length,
3968 hash_alg,
3969 salt, salt_length,
3970 label, label_length );
3971 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003972 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
3973 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
3974 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003975 {
3976 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3977 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3978
3979 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
3980 if( hash_alg != PSA_ALG_SHA_256 &&
3981 hash_alg != PSA_ALG_SHA_384 )
3982 {
3983 return( PSA_ERROR_NOT_SUPPORTED );
3984 }
3985
3986 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00003987
3988 if( PSA_ALG_IS_TLS12_PRF( alg ) )
3989 {
3990 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
3991 secret, secret_length,
3992 hash_alg, salt, salt_length,
3993 label, label_length );
3994 }
3995 else
3996 {
3997 status = psa_generator_tls12_psk_to_ms_setup(
3998 &generator->ctx.tls12_prf,
3999 secret, secret_length,
4000 hash_alg, salt, salt_length,
4001 label, label_length );
4002 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004003 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004004 else
4005#endif
4006 {
4007 return( PSA_ERROR_NOT_SUPPORTED );
4008 }
4009
4010 if( status != PSA_SUCCESS )
4011 return( status );
4012
4013 if( capacity <= max_capacity )
4014 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004015 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4016 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004017 else
4018 return( PSA_ERROR_INVALID_ARGUMENT );
4019
4020 return( PSA_SUCCESS );
4021}
4022
Gilles Peskineea0fb492018-07-12 17:17:20 +02004023psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004024 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004025 psa_algorithm_t alg,
4026 const uint8_t *salt,
4027 size_t salt_length,
4028 const uint8_t *label,
4029 size_t label_length,
4030 size_t capacity )
4031{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004032 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004033 psa_status_t status;
4034
4035 if( generator->alg != 0 )
4036 return( PSA_ERROR_BAD_STATE );
4037
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004038 /* Make sure that alg is a key derivation algorithm. This prevents
4039 * key selection algorithms, which psa_key_derivation_internal
4040 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004041 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4042 return( PSA_ERROR_INVALID_ARGUMENT );
4043
Gilles Peskinec5487a82018-12-03 18:08:14 +01004044 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004045 if( status != PSA_SUCCESS )
4046 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004047
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004048 if( slot->type != PSA_KEY_TYPE_DERIVE )
4049 return( PSA_ERROR_INVALID_ARGUMENT );
4050
4051 status = psa_key_derivation_internal( generator,
4052 slot->data.raw.data,
4053 slot->data.raw.bytes,
4054 alg,
4055 salt, salt_length,
4056 label, label_length,
4057 capacity );
4058 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004059 psa_generator_abort( generator );
4060 return( status );
4061}
4062
4063
4064
4065/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004066/* Key agreement */
4067/****************************************************************/
4068
Gilles Peskinea05219c2018-11-16 16:02:56 +01004069#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004070static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4071 size_t peer_key_length,
4072 const mbedtls_ecp_keypair *our_key,
4073 uint8_t *shared_secret,
4074 size_t shared_secret_size,
4075 size_t *shared_secret_length )
4076{
4077 mbedtls_pk_context pk;
4078 mbedtls_ecp_keypair *their_key = NULL;
4079 mbedtls_ecdh_context ecdh;
4080 int ret;
4081 mbedtls_ecdh_init( &ecdh );
4082 mbedtls_pk_init( &pk );
4083
4084 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
4085 if( ret != 0 )
4086 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02004087 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004088 {
Gilles Peskine88714d72018-10-25 23:07:25 +02004089 case MBEDTLS_PK_ECKEY:
4090 case MBEDTLS_PK_ECKEY_DH:
4091 break;
4092 default:
4093 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4094 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004095 }
4096 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01004097 if( their_key->grp.id != our_key->grp.id )
4098 {
4099 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4100 goto exit;
4101 }
4102
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004103 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
4104 if( ret != 0 )
4105 goto exit;
4106 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
4107 if( ret != 0 )
4108 goto exit;
4109
4110 ret = mbedtls_ecdh_calc_secret( &ecdh,
4111 shared_secret_length,
4112 shared_secret, shared_secret_size,
4113 mbedtls_ctr_drbg_random,
4114 &global_data.ctr_drbg );
4115
4116exit:
4117 mbedtls_pk_free( &pk );
4118 mbedtls_ecdh_free( &ecdh );
4119 return( mbedtls_to_psa_error( ret ) );
4120}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004121#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004122
Gilles Peskine01d718c2018-09-18 12:01:02 +02004123#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4124
Gilles Peskine346797d2018-11-16 16:05:06 +01004125/* Note that if this function fails, you must call psa_generator_abort()
4126 * to potentially free embedded data structures and wipe confidential data.
4127 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004128static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004129 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004130 const uint8_t *peer_key,
4131 size_t peer_key_length,
4132 psa_algorithm_t alg )
4133{
4134 psa_status_t status;
4135 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4136 size_t shared_secret_length = 0;
4137
4138 /* Step 1: run the secret agreement algorithm to generate the shared
4139 * secret. */
4140 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4141 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004142#if defined(MBEDTLS_ECDH_C)
4143 case PSA_ALG_ECDH_BASE:
4144 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4145 return( PSA_ERROR_INVALID_ARGUMENT );
4146 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4147 private_key->data.ecp,
4148 shared_secret,
4149 sizeof( shared_secret ),
4150 &shared_secret_length );
4151 break;
4152#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004153 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004154 (void) private_key;
4155 (void) peer_key;
4156 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004157 return( PSA_ERROR_NOT_SUPPORTED );
4158 }
4159 if( status != PSA_SUCCESS )
4160 goto exit;
4161
4162 /* Step 2: set up the key derivation to generate key material from
4163 * the shared secret. */
4164 status = psa_key_derivation_internal( generator,
4165 shared_secret, shared_secret_length,
4166 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4167 NULL, 0, NULL, 0,
4168 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4169exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004170 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004171 return( status );
4172}
4173
4174psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004175 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004176 const uint8_t *peer_key,
4177 size_t peer_key_length,
4178 psa_algorithm_t alg )
4179{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004180 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004181 psa_status_t status;
4182 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4183 return( PSA_ERROR_INVALID_ARGUMENT );
4184 status = psa_get_key_from_slot( private_key, &slot,
4185 PSA_KEY_USAGE_DERIVE, alg );
4186 if( status != PSA_SUCCESS )
4187 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004188 status = psa_key_agreement_internal( generator,
4189 slot,
4190 peer_key, peer_key_length,
4191 alg );
4192 if( status != PSA_SUCCESS )
4193 psa_generator_abort( generator );
4194 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004195}
4196
4197
4198
4199/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004200/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004201/****************************************************************/
4202
4203psa_status_t psa_generate_random( uint8_t *output,
4204 size_t output_size )
4205{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004206 int ret;
4207 GUARD_MODULE_INITIALIZED;
4208
4209 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004210 return( mbedtls_to_psa_error( ret ) );
4211}
4212
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004213#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004214
4215/* Support function for error conversion between psa_its error codes to psa crypto */
4216static psa_status_t its_to_psa_error( psa_its_status_t ret )
4217{
4218 switch( ret )
4219 {
4220 case PSA_ITS_SUCCESS:
4221 return( PSA_SUCCESS );
4222
Oren Cohen23a67842019-01-24 14:32:11 +02004223 case PSA_ITS_ERROR_UID_NOT_FOUND:
avolinski13beb102018-11-20 16:51:49 +02004224 return( PSA_ERROR_EMPTY_SLOT );
4225
4226 case PSA_ITS_ERROR_STORAGE_FAILURE:
4227 return( PSA_ERROR_STORAGE_FAILURE );
4228
4229 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4230 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4231
Jaeden Amero58600552018-11-30 12:04:38 +00004232 case PSA_ITS_ERROR_OFFSET_INVALID:
avolinski13beb102018-11-20 16:51:49 +02004233 case PSA_ITS_ERROR_INCORRECT_SIZE:
Oren Cohen23a67842019-01-24 14:32:11 +02004234 case PSA_ITS_ERROR_INVALID_ARGUMENTS:
avolinski13beb102018-11-20 16:51:49 +02004235 return( PSA_ERROR_INVALID_ARGUMENT );
4236
4237 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4238 return( PSA_ERROR_NOT_SUPPORTED );
4239
4240 case PSA_ITS_ERROR_WRITE_ONCE:
4241 return( PSA_ERROR_OCCUPIED_SLOT );
4242
4243 default:
4244 return( PSA_ERROR_UNKNOWN_ERROR );
4245 }
4246}
4247
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004248psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4249 size_t seed_size )
4250{
4251 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004252 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004253 struct psa_its_info_t p_info;
4254 if( global_data.initialized )
4255 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004256
4257 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4258 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4259 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4260 return( PSA_ERROR_INVALID_ARGUMENT );
4261
avolinski0d2c2662018-11-21 17:31:07 +02004262 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004263 status = its_to_psa_error( its_status );
4264
Oren Cohen23a67842019-01-24 14:32:11 +02004265 if( PSA_ITS_ERROR_UID_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004266 {
avolinski0d2c2662018-11-21 17:31:07 +02004267 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004268 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004269 }
avolinski13beb102018-11-20 16:51:49 +02004270 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004271 {
4272 /* You should not be here. Seed needs to be injected only once */
4273 status = PSA_ERROR_NOT_PERMITTED;
4274 }
4275 return( status );
4276}
4277#endif
4278
Gilles Peskinec5487a82018-12-03 18:08:14 +01004279psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004280 psa_key_type_t type,
4281 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004282 const void *extra,
4283 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004284{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004285 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004286 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004287
Gilles Peskine53d991e2018-07-12 01:14:59 +02004288 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004289 return( PSA_ERROR_INVALID_ARGUMENT );
4290
Gilles Peskinec5487a82018-12-03 18:08:14 +01004291 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004292 if( status != PSA_SUCCESS )
4293 return( status );
4294
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004295 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004296 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004297 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004298 if( status != PSA_SUCCESS )
4299 return( status );
4300 status = psa_generate_random( slot->data.raw.data,
4301 slot->data.raw.bytes );
4302 if( status != PSA_SUCCESS )
4303 {
4304 mbedtls_free( slot->data.raw.data );
4305 return( status );
4306 }
4307#if defined(MBEDTLS_DES_C)
4308 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004309 psa_des_set_key_parity( slot->data.raw.data,
4310 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004311#endif /* MBEDTLS_DES_C */
4312 }
4313 else
4314
4315#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4316 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4317 {
4318 mbedtls_rsa_context *rsa;
4319 int ret;
4320 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004321 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4322 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004323 /* Accept only byte-aligned keys, for the same reasons as
4324 * in psa_import_rsa_key(). */
4325 if( bits % 8 != 0 )
4326 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004327 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004328 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004329 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004330 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004331 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004332#if INT_MAX < 0xffffffff
4333 /* Check that the uint32_t value passed by the caller fits
4334 * in the range supported by this implementation. */
4335 if( p->e > INT_MAX )
4336 return( PSA_ERROR_NOT_SUPPORTED );
4337#endif
4338 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004339 }
4340 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4341 if( rsa == NULL )
4342 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4343 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4344 ret = mbedtls_rsa_gen_key( rsa,
4345 mbedtls_ctr_drbg_random,
4346 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004347 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004348 exponent );
4349 if( ret != 0 )
4350 {
4351 mbedtls_rsa_free( rsa );
4352 mbedtls_free( rsa );
4353 return( mbedtls_to_psa_error( ret ) );
4354 }
4355 slot->data.rsa = rsa;
4356 }
4357 else
4358#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4359
4360#if defined(MBEDTLS_ECP_C)
4361 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4362 {
4363 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4364 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4365 const mbedtls_ecp_curve_info *curve_info =
4366 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4367 mbedtls_ecp_keypair *ecp;
4368 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004369 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004370 return( PSA_ERROR_NOT_SUPPORTED );
4371 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4372 return( PSA_ERROR_NOT_SUPPORTED );
4373 if( curve_info->bit_size != bits )
4374 return( PSA_ERROR_INVALID_ARGUMENT );
4375 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4376 if( ecp == NULL )
4377 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4378 mbedtls_ecp_keypair_init( ecp );
4379 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4380 mbedtls_ctr_drbg_random,
4381 &global_data.ctr_drbg );
4382 if( ret != 0 )
4383 {
4384 mbedtls_ecp_keypair_free( ecp );
4385 mbedtls_free( ecp );
4386 return( mbedtls_to_psa_error( ret ) );
4387 }
4388 slot->data.ecp = ecp;
4389 }
4390 else
4391#endif /* MBEDTLS_ECP_C */
4392
4393 return( PSA_ERROR_NOT_SUPPORTED );
4394
4395 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004396
4397#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4398 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4399 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004400 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004401 }
4402#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4403
4404 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004405}
4406
4407
4408/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004409/* Module setup */
4410/****************************************************************/
4411
Gilles Peskine5e769522018-11-20 21:59:56 +01004412psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4413 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4414 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4415{
4416 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4417 return( PSA_ERROR_BAD_STATE );
4418 global_data.entropy_init = entropy_init;
4419 global_data.entropy_free = entropy_free;
4420 return( PSA_SUCCESS );
4421}
4422
Gilles Peskinee59236f2018-01-27 23:32:46 +01004423void mbedtls_psa_crypto_free( void )
4424{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004425 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004426 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4427 {
4428 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004429 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004430 }
4431 /* Wipe all remaining data, including configuration.
4432 * In particular, this sets all state indicator to the value
4433 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004434 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004435}
4436
4437psa_status_t psa_crypto_init( void )
4438{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004439 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004440 const unsigned char drbg_seed[] = "PSA";
4441
Gilles Peskinec6b69072018-11-20 21:42:52 +01004442 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004443 if( global_data.initialized != 0 )
4444 return( PSA_SUCCESS );
4445
Gilles Peskine5e769522018-11-20 21:59:56 +01004446 /* Set default configuration if
4447 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4448 if( global_data.entropy_init == NULL )
4449 global_data.entropy_init = mbedtls_entropy_init;
4450 if( global_data.entropy_free == NULL )
4451 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004452
Gilles Peskinec6b69072018-11-20 21:42:52 +01004453 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004454 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004455 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004456 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004457 status = mbedtls_to_psa_error(
4458 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4459 mbedtls_entropy_func,
4460 &global_data.entropy,
4461 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4462 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004463 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004464 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004465
Gilles Peskine66fb1262018-12-10 16:29:04 +01004466 status = psa_initialize_key_slots( );
4467 if( status != PSA_SUCCESS )
4468 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004469
4470 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004471 global_data.initialized = 1;
4472
Gilles Peskinee59236f2018-01-27 23:32:46 +01004473exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004474 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004475 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004476 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004477}
4478
4479#endif /* MBEDTLS_PSA_CRYPTO_C */