blob: 7fb1adb295bed81b2a15dcd2a173c2d5b724d7a2 [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"
Jaeden Amero25384a22019-01-10 10:23:21 +000065#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020066#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/blowfish.h"
68#include "mbedtls/camellia.h"
69#include "mbedtls/cipher.h"
70#include "mbedtls/ccm.h"
71#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010072#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020074#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010076#include "mbedtls/entropy.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020077#include "mbedtls/entropy_poll.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/error.h"
79#include "mbedtls/gcm.h"
80#include "mbedtls/md2.h"
81#include "mbedtls/md4.h"
82#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010083#include "mbedtls/md.h"
84#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010085#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010086#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010087#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010088#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010089#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010090#include "mbedtls/sha1.h"
91#include "mbedtls/sha256.h"
92#include "mbedtls/sha512.h"
93#include "mbedtls/xtea.h"
94
Netanel Gonen2bcd3122018-11-19 11:53:02 +020095#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
Oren Cohen23a67842019-01-24 14:32:11 +020096#include "psa/internal_trusted_storage.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020097#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +010098
Gilles Peskine996deb12018-08-01 15:45:45 +020099#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
100
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100101/* constant-time buffer comparison */
102static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
103{
104 size_t i;
105 unsigned char diff = 0;
106
107 for( i = 0; i < n; i++ )
108 diff |= a[i] ^ b[i];
109
110 return( diff );
111}
112
113
114
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100115/****************************************************************/
116/* Global data, support functions and library management */
117/****************************************************************/
118
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200119static int key_type_is_raw_bytes( psa_key_type_t type )
120{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200121 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200122}
123
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100124/* Values for psa_global_data_t::rng_state */
125#define RNG_NOT_INITIALIZED 0
126#define RNG_INITIALIZED 1
127#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100128
Gilles Peskine2d277862018-06-18 15:41:12 +0200129typedef struct
130{
Gilles Peskine5e769522018-11-20 21:59:56 +0100131 void (* entropy_init )( mbedtls_entropy_context *ctx );
132 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100133 mbedtls_entropy_context entropy;
134 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100135 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100136 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100137} psa_global_data_t;
138
139static psa_global_data_t global_data;
140
itayzafrir0adf0fc2018-09-06 16:24:41 +0300141#define GUARD_MODULE_INITIALIZED \
142 if( global_data.initialized == 0 ) \
143 return( PSA_ERROR_BAD_STATE );
144
Gilles Peskinee59236f2018-01-27 23:32:46 +0100145static psa_status_t mbedtls_to_psa_error( int ret )
146{
Gilles Peskinea5905292018-02-07 20:59:33 +0100147 /* If there's both a high-level code and low-level code, dispatch on
148 * the high-level code. */
149 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100150 {
151 case 0:
152 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100153
154 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
155 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
156 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
157 return( PSA_ERROR_NOT_SUPPORTED );
158 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
159 return( PSA_ERROR_HARDWARE_FAILURE );
160
161 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
162 return( PSA_ERROR_HARDWARE_FAILURE );
163
Gilles Peskine9a944802018-06-21 09:35:35 +0200164 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
165 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
166 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
167 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
168 case MBEDTLS_ERR_ASN1_INVALID_DATA:
169 return( PSA_ERROR_INVALID_ARGUMENT );
170 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
171 return( PSA_ERROR_INSUFFICIENT_MEMORY );
172 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
173 return( PSA_ERROR_BUFFER_TOO_SMALL );
174
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
176 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
181 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
182 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
183 return( PSA_ERROR_NOT_SUPPORTED );
184 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
185 return( PSA_ERROR_HARDWARE_FAILURE );
186
187 case MBEDTLS_ERR_CCM_BAD_INPUT:
188 return( PSA_ERROR_INVALID_ARGUMENT );
189 case MBEDTLS_ERR_CCM_AUTH_FAILED:
190 return( PSA_ERROR_INVALID_SIGNATURE );
191 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
192 return( PSA_ERROR_HARDWARE_FAILURE );
193
194 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
195 return( PSA_ERROR_NOT_SUPPORTED );
196 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
197 return( PSA_ERROR_INVALID_ARGUMENT );
198 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
199 return( PSA_ERROR_INSUFFICIENT_MEMORY );
200 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
201 return( PSA_ERROR_INVALID_PADDING );
202 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
203 return( PSA_ERROR_BAD_STATE );
204 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
205 return( PSA_ERROR_INVALID_SIGNATURE );
206 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
207 return( PSA_ERROR_TAMPERING_DETECTED );
208 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
209 return( PSA_ERROR_HARDWARE_FAILURE );
210
211 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
214 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
215 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
216 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
217 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
218 return( PSA_ERROR_NOT_SUPPORTED );
219 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
220 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
221
222 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
223 return( PSA_ERROR_NOT_SUPPORTED );
224 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
225 return( PSA_ERROR_HARDWARE_FAILURE );
226
Gilles Peskinee59236f2018-01-27 23:32:46 +0100227 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
228 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
229 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
230 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100231
232 case MBEDTLS_ERR_GCM_AUTH_FAILED:
233 return( PSA_ERROR_INVALID_SIGNATURE );
234 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200235 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100236 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
237 return( PSA_ERROR_HARDWARE_FAILURE );
238
239 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
240 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
241 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
244 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
245 return( PSA_ERROR_NOT_SUPPORTED );
246 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
247 return( PSA_ERROR_INVALID_ARGUMENT );
248 case MBEDTLS_ERR_MD_ALLOC_FAILED:
249 return( PSA_ERROR_INSUFFICIENT_MEMORY );
250 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
251 return( PSA_ERROR_STORAGE_FAILURE );
252 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
253 return( PSA_ERROR_HARDWARE_FAILURE );
254
Gilles Peskinef76aa772018-10-29 19:24:33 +0100255 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
256 return( PSA_ERROR_STORAGE_FAILURE );
257 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
258 return( PSA_ERROR_INVALID_ARGUMENT );
259 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
262 return( PSA_ERROR_BUFFER_TOO_SMALL );
263 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
264 return( PSA_ERROR_INVALID_ARGUMENT );
265 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
270 return( PSA_ERROR_INSUFFICIENT_MEMORY );
271
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100272 case MBEDTLS_ERR_PK_ALLOC_FAILED:
273 return( PSA_ERROR_INSUFFICIENT_MEMORY );
274 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
275 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
276 return( PSA_ERROR_INVALID_ARGUMENT );
277 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100278 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100279 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
280 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
281 return( PSA_ERROR_INVALID_ARGUMENT );
282 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
283 return( PSA_ERROR_NOT_SUPPORTED );
284 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
285 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
286 return( PSA_ERROR_NOT_PERMITTED );
287 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
288 return( PSA_ERROR_INVALID_ARGUMENT );
289 case MBEDTLS_ERR_PK_INVALID_ALG:
290 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
291 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
292 return( PSA_ERROR_NOT_SUPPORTED );
293 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
294 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100295 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
296 return( PSA_ERROR_HARDWARE_FAILURE );
297
298 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
299 return( PSA_ERROR_HARDWARE_FAILURE );
300
301 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
302 return( PSA_ERROR_INVALID_ARGUMENT );
303 case MBEDTLS_ERR_RSA_INVALID_PADDING:
304 return( PSA_ERROR_INVALID_PADDING );
305 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
306 return( PSA_ERROR_HARDWARE_FAILURE );
307 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
310 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
311 return( PSA_ERROR_TAMPERING_DETECTED );
312 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
313 return( PSA_ERROR_INVALID_SIGNATURE );
314 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
315 return( PSA_ERROR_BUFFER_TOO_SMALL );
316 case MBEDTLS_ERR_RSA_RNG_FAILED:
317 return( PSA_ERROR_INSUFFICIENT_MEMORY );
318 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
319 return( PSA_ERROR_NOT_SUPPORTED );
320 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
321 return( PSA_ERROR_HARDWARE_FAILURE );
322
323 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
324 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
325 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327
328 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
329 return( PSA_ERROR_INVALID_ARGUMENT );
330 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
331 return( PSA_ERROR_HARDWARE_FAILURE );
332
itayzafrir5c753392018-05-08 11:18:38 +0300333 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300334 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300335 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300336 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
337 return( PSA_ERROR_BUFFER_TOO_SMALL );
338 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
339 return( PSA_ERROR_NOT_SUPPORTED );
340 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
341 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
342 return( PSA_ERROR_INVALID_SIGNATURE );
343 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
344 return( PSA_ERROR_INSUFFICIENT_MEMORY );
345 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
346 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300347
Gilles Peskinee59236f2018-01-27 23:32:46 +0100348 default:
349 return( PSA_ERROR_UNKNOWN_ERROR );
350 }
351}
352
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200353
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200354
355
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100356/****************************************************************/
357/* Key management */
358/****************************************************************/
359
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100360#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200361static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
362{
363 switch( grpid )
364 {
365 case MBEDTLS_ECP_DP_SECP192R1:
366 return( PSA_ECC_CURVE_SECP192R1 );
367 case MBEDTLS_ECP_DP_SECP224R1:
368 return( PSA_ECC_CURVE_SECP224R1 );
369 case MBEDTLS_ECP_DP_SECP256R1:
370 return( PSA_ECC_CURVE_SECP256R1 );
371 case MBEDTLS_ECP_DP_SECP384R1:
372 return( PSA_ECC_CURVE_SECP384R1 );
373 case MBEDTLS_ECP_DP_SECP521R1:
374 return( PSA_ECC_CURVE_SECP521R1 );
375 case MBEDTLS_ECP_DP_BP256R1:
376 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
377 case MBEDTLS_ECP_DP_BP384R1:
378 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
379 case MBEDTLS_ECP_DP_BP512R1:
380 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
381 case MBEDTLS_ECP_DP_CURVE25519:
382 return( PSA_ECC_CURVE_CURVE25519 );
383 case MBEDTLS_ECP_DP_SECP192K1:
384 return( PSA_ECC_CURVE_SECP192K1 );
385 case MBEDTLS_ECP_DP_SECP224K1:
386 return( PSA_ECC_CURVE_SECP224K1 );
387 case MBEDTLS_ECP_DP_SECP256K1:
388 return( PSA_ECC_CURVE_SECP256K1 );
389 case MBEDTLS_ECP_DP_CURVE448:
390 return( PSA_ECC_CURVE_CURVE448 );
391 default:
392 return( 0 );
393 }
394}
395
Gilles Peskine12313cd2018-06-20 00:20:32 +0200396static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
397{
398 switch( curve )
399 {
400 case PSA_ECC_CURVE_SECP192R1:
401 return( MBEDTLS_ECP_DP_SECP192R1 );
402 case PSA_ECC_CURVE_SECP224R1:
403 return( MBEDTLS_ECP_DP_SECP224R1 );
404 case PSA_ECC_CURVE_SECP256R1:
405 return( MBEDTLS_ECP_DP_SECP256R1 );
406 case PSA_ECC_CURVE_SECP384R1:
407 return( MBEDTLS_ECP_DP_SECP384R1 );
408 case PSA_ECC_CURVE_SECP521R1:
409 return( MBEDTLS_ECP_DP_SECP521R1 );
410 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
411 return( MBEDTLS_ECP_DP_BP256R1 );
412 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
413 return( MBEDTLS_ECP_DP_BP384R1 );
414 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
415 return( MBEDTLS_ECP_DP_BP512R1 );
416 case PSA_ECC_CURVE_CURVE25519:
417 return( MBEDTLS_ECP_DP_CURVE25519 );
418 case PSA_ECC_CURVE_SECP192K1:
419 return( MBEDTLS_ECP_DP_SECP192K1 );
420 case PSA_ECC_CURVE_SECP224K1:
421 return( MBEDTLS_ECP_DP_SECP224K1 );
422 case PSA_ECC_CURVE_SECP256K1:
423 return( MBEDTLS_ECP_DP_SECP256K1 );
424 case PSA_ECC_CURVE_CURVE448:
425 return( MBEDTLS_ECP_DP_CURVE448 );
426 default:
427 return( MBEDTLS_ECP_DP_NONE );
428 }
429}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100430#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200431
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200432static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
433 size_t bits,
434 struct raw_data *raw )
435{
436 /* Check that the bit size is acceptable for the key type */
437 switch( type )
438 {
439 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200440 if( bits == 0 )
441 {
442 raw->bytes = 0;
443 raw->data = NULL;
444 return( PSA_SUCCESS );
445 }
446 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200447#if defined(MBEDTLS_MD_C)
448 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200449#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200450 case PSA_KEY_TYPE_DERIVE:
451 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200452#if defined(MBEDTLS_AES_C)
453 case PSA_KEY_TYPE_AES:
454 if( bits != 128 && bits != 192 && bits != 256 )
455 return( PSA_ERROR_INVALID_ARGUMENT );
456 break;
457#endif
458#if defined(MBEDTLS_CAMELLIA_C)
459 case PSA_KEY_TYPE_CAMELLIA:
460 if( bits != 128 && bits != 192 && bits != 256 )
461 return( PSA_ERROR_INVALID_ARGUMENT );
462 break;
463#endif
464#if defined(MBEDTLS_DES_C)
465 case PSA_KEY_TYPE_DES:
466 if( bits != 64 && bits != 128 && bits != 192 )
467 return( PSA_ERROR_INVALID_ARGUMENT );
468 break;
469#endif
470#if defined(MBEDTLS_ARC4_C)
471 case PSA_KEY_TYPE_ARC4:
472 if( bits < 8 || bits > 2048 )
473 return( PSA_ERROR_INVALID_ARGUMENT );
474 break;
475#endif
476 default:
477 return( PSA_ERROR_NOT_SUPPORTED );
478 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200479 if( bits % 8 != 0 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200481
482 /* Allocate memory for the key */
483 raw->bytes = PSA_BITS_TO_BYTES( bits );
484 raw->data = mbedtls_calloc( 1, raw->bytes );
485 if( raw->data == NULL )
486 {
487 raw->bytes = 0;
488 return( PSA_ERROR_INSUFFICIENT_MEMORY );
489 }
490 return( PSA_SUCCESS );
491}
492
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200493#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100494/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
495 * that are not a multiple of 8) well. For example, there is only
496 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
497 * way to return the exact bit size of a key.
498 * To keep things simple, reject non-byte-aligned key sizes. */
499static psa_status_t psa_check_rsa_key_byte_aligned(
500 const mbedtls_rsa_context *rsa )
501{
502 mbedtls_mpi n;
503 psa_status_t status;
504 mbedtls_mpi_init( &n );
505 status = mbedtls_to_psa_error(
506 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
507 if( status == PSA_SUCCESS )
508 {
509 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
510 status = PSA_ERROR_NOT_SUPPORTED;
511 }
512 mbedtls_mpi_free( &n );
513 return( status );
514}
515
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000516static psa_status_t psa_import_rsa_key( psa_key_type_t type,
517 const uint8_t *data,
518 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200519 mbedtls_rsa_context **p_rsa )
520{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000521 psa_status_t status;
522 mbedtls_pk_context pk;
523 mbedtls_rsa_context *rsa;
524 size_t bits;
525
526 mbedtls_pk_init( &pk );
527
528 /* Parse the data. */
529 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
530 status = mbedtls_to_psa_error(
531 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200532 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000533 status = mbedtls_to_psa_error(
534 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
535 if( status != PSA_SUCCESS )
536 goto exit;
537
538 /* We have something that the pkparse module recognizes. If it is a
539 * valid RSA key, store it. */
540 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200541 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000542 status = PSA_ERROR_INVALID_ARGUMENT;
543 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200544 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000545
546 rsa = mbedtls_pk_rsa( pk );
547 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
548 * supports non-byte-aligned key sizes, but not well. For example,
549 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
550 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
551 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
552 {
553 status = PSA_ERROR_NOT_SUPPORTED;
554 goto exit;
555 }
556 status = psa_check_rsa_key_byte_aligned( rsa );
557
558exit:
559 /* Free the content of the pk object only on error. */
560 if( status != PSA_SUCCESS )
561 {
562 mbedtls_pk_free( &pk );
563 return( status );
564 }
565
566 /* On success, store the content of the object in the RSA context. */
567 *p_rsa = rsa;
568
569 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200570}
571#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
572
Jaeden Ameroccdce902019-01-10 11:42:27 +0000573#if defined(MBEDTLS_ECP_C)
574
575/* Import a public key given as the uncompressed representation defined by SEC1
576 * 2.3.3 as the content of an ECPoint. */
577static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
578 const uint8_t *data,
579 size_t data_length,
580 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200581{
Jaeden Ameroccdce902019-01-10 11:42:27 +0000582 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
583 mbedtls_ecp_keypair *ecp = NULL;
584 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
585
586 *p_ecp = NULL;
587 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
588 if( ecp == NULL )
589 return( PSA_ERROR_INSUFFICIENT_MEMORY );
590 mbedtls_ecp_keypair_init( ecp );
591
592 /* Load the group. */
593 status = mbedtls_to_psa_error(
594 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
595 if( status != PSA_SUCCESS )
596 goto exit;
597 /* Load the public value. */
598 status = mbedtls_to_psa_error(
599 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
600 data, data_length ) );
601 if( status != PSA_SUCCESS )
602 goto exit;
603
604 /* Check that the point is on the curve. */
605 status = mbedtls_to_psa_error(
606 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
607 if( status != PSA_SUCCESS )
608 goto exit;
609
610 *p_ecp = ecp;
611 return( PSA_SUCCESS );
612
613exit:
614 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200615 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000616 mbedtls_ecp_keypair_free( ecp );
617 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200618 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000619 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200620}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000621#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200622
Gilles Peskinef76aa772018-10-29 19:24:33 +0100623#if defined(MBEDTLS_ECP_C)
624/* Import a private key given as a byte string which is the private value
625 * in big-endian order. */
626static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
627 const uint8_t *data,
628 size_t data_length,
629 mbedtls_ecp_keypair **p_ecp )
630{
631 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
632 mbedtls_ecp_keypair *ecp = NULL;
633 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
634
635 *p_ecp = NULL;
636 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
637 if( ecp == NULL )
638 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000639 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100640
641 /* Load the group. */
642 status = mbedtls_to_psa_error(
643 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
644 if( status != PSA_SUCCESS )
645 goto exit;
646 /* Load the secret value. */
647 status = mbedtls_to_psa_error(
648 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
649 if( status != PSA_SUCCESS )
650 goto exit;
651 /* Validate the private key. */
652 status = mbedtls_to_psa_error(
653 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
654 if( status != PSA_SUCCESS )
655 goto exit;
656 /* Calculate the public key from the private key. */
657 status = mbedtls_to_psa_error(
658 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
659 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
660 if( status != PSA_SUCCESS )
661 goto exit;
662
663 *p_ecp = ecp;
664 return( PSA_SUCCESS );
665
666exit:
667 if( ecp != NULL )
668 {
669 mbedtls_ecp_keypair_free( ecp );
670 mbedtls_free( ecp );
671 }
672 return( status );
673}
674#endif /* defined(MBEDTLS_ECP_C) */
675
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100676/** Import key data into a slot. `slot->type` must have been set
677 * previously. This function assumes that the slot does not contain
678 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100679psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
680 const uint8_t *data,
681 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100682{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200683 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100684
Darryl Green940d72c2018-07-13 13:18:51 +0100685 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100686 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100687 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100688 if( data_length > SIZE_MAX / 8 )
689 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100690 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200691 PSA_BYTES_TO_BITS( data_length ),
692 &slot->data.raw );
693 if( status != PSA_SUCCESS )
694 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200695 if( data_length != 0 )
696 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100697 }
698 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100699#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100700 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100701 {
Darryl Green940d72c2018-07-13 13:18:51 +0100702 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100703 data, data_length,
704 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100705 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000706 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100707 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000708 status = psa_import_ec_public_key(
709 PSA_KEY_TYPE_GET_CURVE( slot->type ),
710 data, data_length,
711 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100712 }
713 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100714#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000715#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
716 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100717 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000718 status = psa_import_rsa_key( slot->type,
719 data, data_length,
720 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100721 }
722 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000723#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100724 {
725 return( PSA_ERROR_NOT_SUPPORTED );
726 }
Jaeden Ameroc67200d2019-01-14 13:12:39 +0000727 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100728}
729
Darryl Green06fd18d2018-07-16 11:21:11 +0100730/* Retrieve an empty key slot (slot with no key data, but possibly
731 * with some metadata such as a policy). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100732static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100733 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100734{
735 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100736 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100737
738 *p_slot = NULL;
739
Gilles Peskinec5487a82018-12-03 18:08:14 +0100740 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100741 if( status != PSA_SUCCESS )
742 return( status );
743
744 if( slot->type != PSA_KEY_TYPE_NONE )
745 return( PSA_ERROR_OCCUPIED_SLOT );
746
747 *p_slot = slot;
748 return( status );
749}
750
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100751/** Test whether a policy permits an algorithm.
752 *
753 * The caller must test usage flags separately.
754 */
755static int psa_key_policy_permits( const psa_key_policy_t *policy,
756 psa_algorithm_t alg )
757{
758 /* Common case: the policy only allows alg. */
759 if( alg == policy->alg )
760 return( 1 );
761 /* If policy->alg is a hash-and-sign with a wildcard for the hash,
762 * and alg is the same hash-and-sign family with any hash,
763 * then alg is compliant with policy->alg. */
764 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) &&
765 PSA_ALG_SIGN_GET_HASH( policy->alg ) == PSA_ALG_ANY_HASH )
766 {
767 return( ( policy->alg & ~PSA_ALG_HASH_MASK ) ==
768 ( alg & ~PSA_ALG_HASH_MASK ) );
769 }
770 /* If it isn't permitted, it's forbidden. */
771 return( 0 );
772}
773
Darryl Green06fd18d2018-07-16 11:21:11 +0100774/** Retrieve a slot which must contain a key. The key must have allow all the
775 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
776 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100777static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100778 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100779 psa_key_usage_t usage,
780 psa_algorithm_t alg )
781{
782 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100783 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100784
785 *p_slot = NULL;
786
Gilles Peskinec5487a82018-12-03 18:08:14 +0100787 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100788 if( status != PSA_SUCCESS )
789 return( status );
790 if( slot->type == PSA_KEY_TYPE_NONE )
791 return( PSA_ERROR_EMPTY_SLOT );
792
793 /* Enforce that usage policy for the key slot contains all the flags
794 * required by the usage parameter. There is one exception: public
795 * keys can always be exported, so we treat public key objects as
796 * if they had the export flag. */
797 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
798 usage &= ~PSA_KEY_USAGE_EXPORT;
799 if( ( slot->policy.usage & usage ) != usage )
800 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100801
802 /* Enforce that the usage policy permits the requested algortihm. */
803 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100804 return( PSA_ERROR_NOT_PERMITTED );
805
806 *p_slot = slot;
807 return( PSA_SUCCESS );
808}
Darryl Green940d72c2018-07-13 13:18:51 +0100809
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100810/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100811static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000812{
813 if( slot->type == PSA_KEY_TYPE_NONE )
814 {
815 /* No key material to clean. */
816 }
817 else if( key_type_is_raw_bytes( slot->type ) )
818 {
819 mbedtls_free( slot->data.raw.data );
820 }
821 else
822#if defined(MBEDTLS_RSA_C)
823 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
824 {
825 mbedtls_rsa_free( slot->data.rsa );
826 mbedtls_free( slot->data.rsa );
827 }
828 else
829#endif /* defined(MBEDTLS_RSA_C) */
830#if defined(MBEDTLS_ECP_C)
831 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
832 {
833 mbedtls_ecp_keypair_free( slot->data.ecp );
834 mbedtls_free( slot->data.ecp );
835 }
836 else
837#endif /* defined(MBEDTLS_ECP_C) */
838 {
839 /* Shouldn't happen: the key type is not any type that we
840 * put in. */
841 return( PSA_ERROR_TAMPERING_DETECTED );
842 }
843
844 return( PSA_SUCCESS );
845}
846
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100847/** Completely wipe a slot in memory, including its policy.
848 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100849psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100850{
851 psa_status_t status = psa_remove_key_data_from_memory( slot );
852 /* At this point, key material and other type-specific content has
853 * been wiped. Clear remaining metadata. We can call memset and not
854 * zeroize because the metadata is not particularly sensitive. */
855 memset( slot, 0, sizeof( *slot ) );
856 return( status );
857}
858
Gilles Peskinec5487a82018-12-03 18:08:14 +0100859psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100860 psa_key_type_t type,
861 const uint8_t *data,
862 size_t data_length )
863{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100864 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100865 psa_status_t status;
866
Gilles Peskinec5487a82018-12-03 18:08:14 +0100867 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100868 if( status != PSA_SUCCESS )
869 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100870
871 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100872
873 status = psa_import_key_into_slot( slot, data, data_length );
874 if( status != PSA_SUCCESS )
875 {
876 slot->type = PSA_KEY_TYPE_NONE;
877 return( status );
878 }
879
Darryl Greend49a4992018-06-18 17:27:26 +0100880#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
881 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
882 {
883 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100884 status = psa_save_persistent_key( slot->persistent_storage_id,
885 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100886 data_length );
887 if( status != PSA_SUCCESS )
888 {
889 (void) psa_remove_key_data_from_memory( slot );
890 slot->type = PSA_KEY_TYPE_NONE;
891 }
892 }
893#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
894
895 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100896}
897
Gilles Peskinec5487a82018-12-03 18:08:14 +0100898psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100899{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100900 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100901 psa_status_t status = PSA_SUCCESS;
902 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100903
Gilles Peskinec5487a82018-12-03 18:08:14 +0100904 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100905 if( status != PSA_SUCCESS )
906 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100907#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
908 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
909 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100910 storage_status =
911 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100912 }
913#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100914 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100915 if( status != PSA_SUCCESS )
916 return( status );
917 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100918}
919
Gilles Peskineb870b182018-07-06 16:02:09 +0200920/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100921static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200922{
923 if( key_type_is_raw_bytes( slot->type ) )
924 return( slot->data.raw.bytes * 8 );
925#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200926 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100927 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200928#endif /* defined(MBEDTLS_RSA_C) */
929#if defined(MBEDTLS_ECP_C)
930 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
931 return( slot->data.ecp->grp.pbits );
932#endif /* defined(MBEDTLS_ECP_C) */
933 /* Shouldn't happen except on an empty slot. */
934 return( 0 );
935}
936
Gilles Peskinec5487a82018-12-03 18:08:14 +0100937psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200938 psa_key_type_t *type,
939 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100940{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100941 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200942 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100943
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100944 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200945 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100946 if( bits != NULL )
947 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100948 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200949 if( status != PSA_SUCCESS )
950 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200951
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100952 if( slot->type == PSA_KEY_TYPE_NONE )
953 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200954 if( type != NULL )
955 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200956 if( bits != NULL )
957 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100958 return( PSA_SUCCESS );
959}
960
Jaeden Ameroccdce902019-01-10 11:42:27 +0000961#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +0000962static int pk_write_pubkey_simple( mbedtls_pk_context *key,
963 unsigned char *buf, size_t size )
964{
965 int ret;
966 unsigned char *c;
967 size_t len = 0;
968
969 c = buf + size;
970
971 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
972
973 return( (int) len );
974}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000975#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +0000976
Gilles Peskine2f060a82018-12-04 17:12:32 +0100977static psa_status_t psa_internal_export_key( psa_key_slot_t *slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200978 uint8_t *data,
979 size_t data_size,
980 size_t *data_length,
981 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100982{
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100983 *data_length = 0;
984
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200985 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300986 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300987
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200988 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100989 {
990 if( slot->data.raw.bytes > data_size )
991 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +0100992 if( data_size != 0 )
993 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200994 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +0100995 memset( data + slot->data.raw.bytes, 0,
996 data_size - slot->data.raw.bytes );
997 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100998 *data_length = slot->data.raw.bytes;
999 return( PSA_SUCCESS );
1000 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001001#if defined(MBEDTLS_ECP_C)
1002 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1003 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001004 psa_status_t status;
1005
Gilles Peskine188c71e2018-10-29 19:26:02 +01001006 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1007 if( bytes > data_size )
1008 return( PSA_ERROR_BUFFER_TOO_SMALL );
1009 status = mbedtls_to_psa_error(
1010 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1011 if( status != PSA_SUCCESS )
1012 return( status );
1013 memset( data + bytes, 0, data_size - bytes );
1014 *data_length = bytes;
1015 return( PSA_SUCCESS );
1016 }
1017#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001018 else
Moran Peker17e36e12018-05-02 12:55:20 +03001019 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001020#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001021 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001022 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001023 {
Moran Pekera998bc62018-04-16 18:16:20 +03001024 mbedtls_pk_context pk;
1025 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001026 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001027 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001028#if defined(MBEDTLS_RSA_C)
1029 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001030 pk.pk_info = &mbedtls_rsa_info;
1031 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001032#else
1033 return( PSA_ERROR_NOT_SUPPORTED );
1034#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001035 }
1036 else
1037 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001038#if defined(MBEDTLS_ECP_C)
1039 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001040 pk.pk_info = &mbedtls_eckey_info;
1041 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001042#else
1043 return( PSA_ERROR_NOT_SUPPORTED );
1044#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001045 }
Moran Pekerd7326592018-05-29 16:56:39 +03001046 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001047 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001048 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001049 }
Moran Peker17e36e12018-05-02 12:55:20 +03001050 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001051 {
Moran Peker17e36e12018-05-02 12:55:20 +03001052 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001053 }
Moran Peker60364322018-04-29 11:34:58 +03001054 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001055 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001056 /* If data_size is 0 then data may be NULL and then the
1057 * call to memset would have undefined behavior. */
1058 if( data_size != 0 )
1059 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001060 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001061 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001062 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1063 * Move the data to the beginning and erase remaining data
1064 * at the original location. */
1065 if( 2 * (size_t) ret <= data_size )
1066 {
1067 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001068 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001069 }
1070 else if( (size_t) ret < data_size )
1071 {
1072 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001073 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001074 }
Moran Pekera998bc62018-04-16 18:16:20 +03001075 *data_length = ret;
1076 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001077 }
1078 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001079#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001080 {
1081 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001082 it is valid for a special-purpose implementation to omit
1083 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001084 return( PSA_ERROR_NOT_SUPPORTED );
1085 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001086 }
1087}
1088
Gilles Peskinec5487a82018-12-03 18:08:14 +01001089psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001090 uint8_t *data,
1091 size_t data_size,
1092 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001093{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001094 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001095 psa_status_t status;
1096
1097 /* Set the key to empty now, so that even when there are errors, we always
1098 * set data_length to a value between 0 and data_size. On error, setting
1099 * the key to empty is a good choice because an empty key representation is
1100 * unlikely to be accepted anywhere. */
1101 *data_length = 0;
1102
1103 /* Export requires the EXPORT flag. There is an exception for public keys,
1104 * which don't require any flag, but psa_get_key_from_slot takes
1105 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001106 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001107 if( status != PSA_SUCCESS )
1108 return( status );
1109 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001110 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001111}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001112
Gilles Peskinec5487a82018-12-03 18:08:14 +01001113psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001114 uint8_t *data,
1115 size_t data_size,
1116 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001117{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001118 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001119 psa_status_t status;
1120
1121 /* Set the key to empty now, so that even when there are errors, we always
1122 * set data_length to a value between 0 and data_size. On error, setting
1123 * the key to empty is a good choice because an empty key representation is
1124 * unlikely to be accepted anywhere. */
1125 *data_length = 0;
1126
1127 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001128 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001129 if( status != PSA_SUCCESS )
1130 return( status );
1131 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001132 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001133}
1134
Darryl Green0c6575a2018-11-07 16:05:30 +00001135#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001136static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001137 size_t bits )
1138{
1139 psa_status_t status;
1140 uint8_t *data;
1141 size_t key_length;
1142 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1143 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001144 if( data == NULL )
1145 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001146 /* Get key data in export format */
1147 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1148 if( status != PSA_SUCCESS )
1149 {
1150 slot->type = PSA_KEY_TYPE_NONE;
1151 goto exit;
1152 }
1153 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001154 status = psa_save_persistent_key( slot->persistent_storage_id,
1155 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001156 data, key_length );
1157 if( status != PSA_SUCCESS )
1158 {
1159 slot->type = PSA_KEY_TYPE_NONE;
1160 }
1161exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001162 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001163 mbedtls_free( data );
1164 return( status );
1165}
1166#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1167
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001168
1169
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001170/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001171/* Message digests */
1172/****************************************************************/
1173
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001174static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001175{
1176 switch( alg )
1177 {
1178#if defined(MBEDTLS_MD2_C)
1179 case PSA_ALG_MD2:
1180 return( &mbedtls_md2_info );
1181#endif
1182#if defined(MBEDTLS_MD4_C)
1183 case PSA_ALG_MD4:
1184 return( &mbedtls_md4_info );
1185#endif
1186#if defined(MBEDTLS_MD5_C)
1187 case PSA_ALG_MD5:
1188 return( &mbedtls_md5_info );
1189#endif
1190#if defined(MBEDTLS_RIPEMD160_C)
1191 case PSA_ALG_RIPEMD160:
1192 return( &mbedtls_ripemd160_info );
1193#endif
1194#if defined(MBEDTLS_SHA1_C)
1195 case PSA_ALG_SHA_1:
1196 return( &mbedtls_sha1_info );
1197#endif
1198#if defined(MBEDTLS_SHA256_C)
1199 case PSA_ALG_SHA_224:
1200 return( &mbedtls_sha224_info );
1201 case PSA_ALG_SHA_256:
1202 return( &mbedtls_sha256_info );
1203#endif
1204#if defined(MBEDTLS_SHA512_C)
1205 case PSA_ALG_SHA_384:
1206 return( &mbedtls_sha384_info );
1207 case PSA_ALG_SHA_512:
1208 return( &mbedtls_sha512_info );
1209#endif
1210 default:
1211 return( NULL );
1212 }
1213}
1214
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001215psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1216{
1217 switch( operation->alg )
1218 {
Gilles Peskine81736312018-06-26 15:04:31 +02001219 case 0:
1220 /* The object has (apparently) been initialized but it is not
1221 * in use. It's ok to call abort on such an object, and there's
1222 * nothing to do. */
1223 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001224#if defined(MBEDTLS_MD2_C)
1225 case PSA_ALG_MD2:
1226 mbedtls_md2_free( &operation->ctx.md2 );
1227 break;
1228#endif
1229#if defined(MBEDTLS_MD4_C)
1230 case PSA_ALG_MD4:
1231 mbedtls_md4_free( &operation->ctx.md4 );
1232 break;
1233#endif
1234#if defined(MBEDTLS_MD5_C)
1235 case PSA_ALG_MD5:
1236 mbedtls_md5_free( &operation->ctx.md5 );
1237 break;
1238#endif
1239#if defined(MBEDTLS_RIPEMD160_C)
1240 case PSA_ALG_RIPEMD160:
1241 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1242 break;
1243#endif
1244#if defined(MBEDTLS_SHA1_C)
1245 case PSA_ALG_SHA_1:
1246 mbedtls_sha1_free( &operation->ctx.sha1 );
1247 break;
1248#endif
1249#if defined(MBEDTLS_SHA256_C)
1250 case PSA_ALG_SHA_224:
1251 case PSA_ALG_SHA_256:
1252 mbedtls_sha256_free( &operation->ctx.sha256 );
1253 break;
1254#endif
1255#if defined(MBEDTLS_SHA512_C)
1256 case PSA_ALG_SHA_384:
1257 case PSA_ALG_SHA_512:
1258 mbedtls_sha512_free( &operation->ctx.sha512 );
1259 break;
1260#endif
1261 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001262 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001263 }
1264 operation->alg = 0;
1265 return( PSA_SUCCESS );
1266}
1267
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001268psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001269 psa_algorithm_t alg )
1270{
1271 int ret;
1272 operation->alg = 0;
1273 switch( alg )
1274 {
1275#if defined(MBEDTLS_MD2_C)
1276 case PSA_ALG_MD2:
1277 mbedtls_md2_init( &operation->ctx.md2 );
1278 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1279 break;
1280#endif
1281#if defined(MBEDTLS_MD4_C)
1282 case PSA_ALG_MD4:
1283 mbedtls_md4_init( &operation->ctx.md4 );
1284 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1285 break;
1286#endif
1287#if defined(MBEDTLS_MD5_C)
1288 case PSA_ALG_MD5:
1289 mbedtls_md5_init( &operation->ctx.md5 );
1290 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1291 break;
1292#endif
1293#if defined(MBEDTLS_RIPEMD160_C)
1294 case PSA_ALG_RIPEMD160:
1295 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1296 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1297 break;
1298#endif
1299#if defined(MBEDTLS_SHA1_C)
1300 case PSA_ALG_SHA_1:
1301 mbedtls_sha1_init( &operation->ctx.sha1 );
1302 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1303 break;
1304#endif
1305#if defined(MBEDTLS_SHA256_C)
1306 case PSA_ALG_SHA_224:
1307 mbedtls_sha256_init( &operation->ctx.sha256 );
1308 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1309 break;
1310 case PSA_ALG_SHA_256:
1311 mbedtls_sha256_init( &operation->ctx.sha256 );
1312 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1313 break;
1314#endif
1315#if defined(MBEDTLS_SHA512_C)
1316 case PSA_ALG_SHA_384:
1317 mbedtls_sha512_init( &operation->ctx.sha512 );
1318 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1319 break;
1320 case PSA_ALG_SHA_512:
1321 mbedtls_sha512_init( &operation->ctx.sha512 );
1322 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1323 break;
1324#endif
1325 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001326 return( PSA_ALG_IS_HASH( alg ) ?
1327 PSA_ERROR_NOT_SUPPORTED :
1328 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001329 }
1330 if( ret == 0 )
1331 operation->alg = alg;
1332 else
1333 psa_hash_abort( operation );
1334 return( mbedtls_to_psa_error( ret ) );
1335}
1336
1337psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1338 const uint8_t *input,
1339 size_t input_length )
1340{
1341 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001342
1343 /* Don't require hash implementations to behave correctly on a
1344 * zero-length input, which may have an invalid pointer. */
1345 if( input_length == 0 )
1346 return( PSA_SUCCESS );
1347
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001348 switch( operation->alg )
1349 {
1350#if defined(MBEDTLS_MD2_C)
1351 case PSA_ALG_MD2:
1352 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1353 input, input_length );
1354 break;
1355#endif
1356#if defined(MBEDTLS_MD4_C)
1357 case PSA_ALG_MD4:
1358 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1359 input, input_length );
1360 break;
1361#endif
1362#if defined(MBEDTLS_MD5_C)
1363 case PSA_ALG_MD5:
1364 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1365 input, input_length );
1366 break;
1367#endif
1368#if defined(MBEDTLS_RIPEMD160_C)
1369 case PSA_ALG_RIPEMD160:
1370 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1371 input, input_length );
1372 break;
1373#endif
1374#if defined(MBEDTLS_SHA1_C)
1375 case PSA_ALG_SHA_1:
1376 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1377 input, input_length );
1378 break;
1379#endif
1380#if defined(MBEDTLS_SHA256_C)
1381 case PSA_ALG_SHA_224:
1382 case PSA_ALG_SHA_256:
1383 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1384 input, input_length );
1385 break;
1386#endif
1387#if defined(MBEDTLS_SHA512_C)
1388 case PSA_ALG_SHA_384:
1389 case PSA_ALG_SHA_512:
1390 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1391 input, input_length );
1392 break;
1393#endif
1394 default:
1395 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1396 break;
1397 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001398
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001399 if( ret != 0 )
1400 psa_hash_abort( operation );
1401 return( mbedtls_to_psa_error( ret ) );
1402}
1403
1404psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1405 uint8_t *hash,
1406 size_t hash_size,
1407 size_t *hash_length )
1408{
itayzafrir40835d42018-08-02 13:14:17 +03001409 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001410 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001411 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001412
1413 /* Fill the output buffer with something that isn't a valid hash
1414 * (barring an attack on the hash and deliberately-crafted input),
1415 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001416 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001417 /* If hash_size is 0 then hash may be NULL and then the
1418 * call to memset would have undefined behavior. */
1419 if( hash_size != 0 )
1420 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001421
1422 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001423 {
1424 status = PSA_ERROR_BUFFER_TOO_SMALL;
1425 goto exit;
1426 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001427
1428 switch( operation->alg )
1429 {
1430#if defined(MBEDTLS_MD2_C)
1431 case PSA_ALG_MD2:
1432 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1433 break;
1434#endif
1435#if defined(MBEDTLS_MD4_C)
1436 case PSA_ALG_MD4:
1437 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1438 break;
1439#endif
1440#if defined(MBEDTLS_MD5_C)
1441 case PSA_ALG_MD5:
1442 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1443 break;
1444#endif
1445#if defined(MBEDTLS_RIPEMD160_C)
1446 case PSA_ALG_RIPEMD160:
1447 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1448 break;
1449#endif
1450#if defined(MBEDTLS_SHA1_C)
1451 case PSA_ALG_SHA_1:
1452 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1453 break;
1454#endif
1455#if defined(MBEDTLS_SHA256_C)
1456 case PSA_ALG_SHA_224:
1457 case PSA_ALG_SHA_256:
1458 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1459 break;
1460#endif
1461#if defined(MBEDTLS_SHA512_C)
1462 case PSA_ALG_SHA_384:
1463 case PSA_ALG_SHA_512:
1464 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1465 break;
1466#endif
1467 default:
1468 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1469 break;
1470 }
itayzafrir40835d42018-08-02 13:14:17 +03001471 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001472
itayzafrir40835d42018-08-02 13:14:17 +03001473exit:
1474 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001475 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001476 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001477 return( psa_hash_abort( operation ) );
1478 }
1479 else
1480 {
1481 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001482 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001483 }
1484}
1485
Gilles Peskine2d277862018-06-18 15:41:12 +02001486psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1487 const uint8_t *hash,
1488 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001489{
1490 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1491 size_t actual_hash_length;
1492 psa_status_t status = psa_hash_finish( operation,
1493 actual_hash, sizeof( actual_hash ),
1494 &actual_hash_length );
1495 if( status != PSA_SUCCESS )
1496 return( status );
1497 if( actual_hash_length != hash_length )
1498 return( PSA_ERROR_INVALID_SIGNATURE );
1499 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1500 return( PSA_ERROR_INVALID_SIGNATURE );
1501 return( PSA_SUCCESS );
1502}
1503
Gilles Peskineeb35d782019-01-22 17:56:16 +01001504psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1505 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001506{
1507 if( target_operation->alg != 0 )
1508 return( PSA_ERROR_BAD_STATE );
1509
1510 switch( source_operation->alg )
1511 {
1512 case 0:
1513 return( PSA_ERROR_BAD_STATE );
1514#if defined(MBEDTLS_MD2_C)
1515 case PSA_ALG_MD2:
1516 mbedtls_md2_clone( &target_operation->ctx.md2,
1517 &source_operation->ctx.md2 );
1518 break;
1519#endif
1520#if defined(MBEDTLS_MD4_C)
1521 case PSA_ALG_MD4:
1522 mbedtls_md4_clone( &target_operation->ctx.md4,
1523 &source_operation->ctx.md4 );
1524 break;
1525#endif
1526#if defined(MBEDTLS_MD5_C)
1527 case PSA_ALG_MD5:
1528 mbedtls_md5_clone( &target_operation->ctx.md5,
1529 &source_operation->ctx.md5 );
1530 break;
1531#endif
1532#if defined(MBEDTLS_RIPEMD160_C)
1533 case PSA_ALG_RIPEMD160:
1534 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1535 &source_operation->ctx.ripemd160 );
1536 break;
1537#endif
1538#if defined(MBEDTLS_SHA1_C)
1539 case PSA_ALG_SHA_1:
1540 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1541 &source_operation->ctx.sha1 );
1542 break;
1543#endif
1544#if defined(MBEDTLS_SHA256_C)
1545 case PSA_ALG_SHA_224:
1546 case PSA_ALG_SHA_256:
1547 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1548 &source_operation->ctx.sha256 );
1549 break;
1550#endif
1551#if defined(MBEDTLS_SHA512_C)
1552 case PSA_ALG_SHA_384:
1553 case PSA_ALG_SHA_512:
1554 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1555 &source_operation->ctx.sha512 );
1556 break;
1557#endif
1558 default:
1559 return( PSA_ERROR_NOT_SUPPORTED );
1560 }
1561
1562 target_operation->alg = source_operation->alg;
1563 return( PSA_SUCCESS );
1564}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001565
1566
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001567/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001568/* MAC */
1569/****************************************************************/
1570
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001571static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001572 psa_algorithm_t alg,
1573 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001574 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001575 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001576{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001577 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001578 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001579
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001580 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001581 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001582
Gilles Peskine8c9def32018-02-08 10:02:12 +01001583 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1584 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001585 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001586 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001587 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001588 mode = MBEDTLS_MODE_STREAM;
1589 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001590 case PSA_ALG_CTR:
1591 mode = MBEDTLS_MODE_CTR;
1592 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001593 case PSA_ALG_CFB:
1594 mode = MBEDTLS_MODE_CFB;
1595 break;
1596 case PSA_ALG_OFB:
1597 mode = MBEDTLS_MODE_OFB;
1598 break;
1599 case PSA_ALG_CBC_NO_PADDING:
1600 mode = MBEDTLS_MODE_CBC;
1601 break;
1602 case PSA_ALG_CBC_PKCS7:
1603 mode = MBEDTLS_MODE_CBC;
1604 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001605 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001606 mode = MBEDTLS_MODE_CCM;
1607 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001608 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001609 mode = MBEDTLS_MODE_GCM;
1610 break;
1611 default:
1612 return( NULL );
1613 }
1614 }
1615 else if( alg == PSA_ALG_CMAC )
1616 mode = MBEDTLS_MODE_ECB;
1617 else if( alg == PSA_ALG_GMAC )
1618 mode = MBEDTLS_MODE_GCM;
1619 else
1620 return( NULL );
1621
1622 switch( key_type )
1623 {
1624 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001625 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001626 break;
1627 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001628 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1629 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001630 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001631 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001632 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001633 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001634 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1635 * but two-key Triple-DES is functionally three-key Triple-DES
1636 * with K1=K3, so that's how we present it to mbedtls. */
1637 if( key_bits == 128 )
1638 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001639 break;
1640 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001641 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001642 break;
1643 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001644 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001645 break;
1646 default:
1647 return( NULL );
1648 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001649 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001650 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001651
Jaeden Amero23bbb752018-06-26 14:16:54 +01001652 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1653 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001654}
1655
Gilles Peskinea05219c2018-11-16 16:02:56 +01001656#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001657static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001658{
Gilles Peskine2d277862018-06-18 15:41:12 +02001659 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001660 {
1661 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001662 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001663 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001664 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001665 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001666 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001667 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001668 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001669 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001670 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001671 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001672 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001673 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001674 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001675 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001676 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001677 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001678 return( 128 );
1679 default:
1680 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001681 }
1682}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001683#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001684
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001685/* Initialize the MAC operation structure. Once this function has been
1686 * called, psa_mac_abort can run and will do the right thing. */
1687static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1688 psa_algorithm_t alg )
1689{
1690 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1691
1692 operation->alg = alg;
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;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001698
1699#if defined(MBEDTLS_CMAC_C)
1700 if( alg == PSA_ALG_CMAC )
1701 {
1702 operation->iv_required = 0;
1703 mbedtls_cipher_init( &operation->ctx.cmac );
1704 status = PSA_SUCCESS;
1705 }
1706 else
1707#endif /* MBEDTLS_CMAC_C */
1708#if defined(MBEDTLS_MD_C)
1709 if( PSA_ALG_IS_HMAC( operation->alg ) )
1710 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001711 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1712 operation->ctx.hmac.hash_ctx.alg = 0;
1713 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001714 }
1715 else
1716#endif /* MBEDTLS_MD_C */
1717 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001718 if( ! PSA_ALG_IS_MAC( alg ) )
1719 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001720 }
1721
1722 if( status != PSA_SUCCESS )
1723 memset( operation, 0, sizeof( *operation ) );
1724 return( status );
1725}
1726
Gilles Peskine01126fa2018-07-12 17:04:55 +02001727#if defined(MBEDTLS_MD_C)
1728static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1729{
Gilles Peskine3f108122018-12-07 18:14:53 +01001730 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001731 return( psa_hash_abort( &hmac->hash_ctx ) );
1732}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001733
1734static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1735{
1736 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1737 memset( hmac, 0, sizeof( *hmac ) );
1738}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001739#endif /* MBEDTLS_MD_C */
1740
Gilles Peskine8c9def32018-02-08 10:02:12 +01001741psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1742{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001743 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001744 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001745 /* The object has (apparently) been initialized but it is not
1746 * in use. It's ok to call abort on such an object, and there's
1747 * nothing to do. */
1748 return( PSA_SUCCESS );
1749 }
1750 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001751#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001752 if( operation->alg == PSA_ALG_CMAC )
1753 {
1754 mbedtls_cipher_free( &operation->ctx.cmac );
1755 }
1756 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001757#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001758#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001759 if( PSA_ALG_IS_HMAC( operation->alg ) )
1760 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001761 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001762 }
1763 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001764#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001765 {
1766 /* Sanity check (shouldn't happen: operation->alg should
1767 * always have been initialized to a valid value). */
1768 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001769 }
Moran Peker41deec42018-04-04 15:43:05 +03001770
Gilles Peskine8c9def32018-02-08 10:02:12 +01001771 operation->alg = 0;
1772 operation->key_set = 0;
1773 operation->iv_set = 0;
1774 operation->iv_required = 0;
1775 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001776 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001777
Gilles Peskine8c9def32018-02-08 10:02:12 +01001778 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001779
1780bad_state:
1781 /* If abort is called on an uninitialized object, we can't trust
1782 * anything. Wipe the object in case it contains confidential data.
1783 * This may result in a memory leak if a pointer gets overwritten,
1784 * but it's too late to do anything about this. */
1785 memset( operation, 0, sizeof( *operation ) );
1786 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001787}
1788
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001789#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001790static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001791 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001792 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001793 const mbedtls_cipher_info_t *cipher_info )
1794{
1795 int ret;
1796
1797 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001798
1799 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1800 if( ret != 0 )
1801 return( ret );
1802
1803 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1804 slot->data.raw.data,
1805 key_bits );
1806 return( ret );
1807}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001808#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001809
Gilles Peskine248051a2018-06-20 16:09:38 +02001810#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001811static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1812 const uint8_t *key,
1813 size_t key_length,
1814 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001815{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001816 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001817 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001818 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001819 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001820 psa_status_t status;
1821
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001822 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1823 * overflow below. This should never trigger if the hash algorithm
1824 * is implemented correctly. */
1825 /* The size checks against the ipad and opad buffers cannot be written
1826 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1827 * because that triggers -Wlogical-op on GCC 7.3. */
1828 if( block_size > sizeof( ipad ) )
1829 return( PSA_ERROR_NOT_SUPPORTED );
1830 if( block_size > sizeof( hmac->opad ) )
1831 return( PSA_ERROR_NOT_SUPPORTED );
1832 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001833 return( PSA_ERROR_NOT_SUPPORTED );
1834
Gilles Peskined223b522018-06-11 18:12:58 +02001835 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001836 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001837 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001838 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001839 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001840 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001841 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001842 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001843 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001844 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001845 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001846 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001847 }
Gilles Peskine96889972018-07-12 17:07:03 +02001848 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1849 * but it is permitted. It is common when HMAC is used in HKDF, for
1850 * example. Don't call `memcpy` in the 0-length because `key` could be
1851 * an invalid pointer which would make the behavior undefined. */
1852 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001853 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001854
Gilles Peskined223b522018-06-11 18:12:58 +02001855 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1856 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001857 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001858 ipad[i] ^= 0x36;
1859 memset( ipad + key_length, 0x36, block_size - key_length );
1860
1861 /* Copy the key material from ipad to opad, flipping the requisite bits,
1862 * and filling the rest of opad with the requisite constant. */
1863 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001864 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1865 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001866
Gilles Peskine01126fa2018-07-12 17:04:55 +02001867 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001868 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001869 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001870
Gilles Peskine01126fa2018-07-12 17:04:55 +02001871 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001872
1873cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001874 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001875
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001876 return( status );
1877}
Gilles Peskine248051a2018-06-20 16:09:38 +02001878#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001879
Gilles Peskine89167cb2018-07-08 20:12:23 +02001880static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001881 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001882 psa_algorithm_t alg,
1883 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001884{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001885 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001886 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001887 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001888 psa_key_usage_t usage =
1889 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001890 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001891 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001892
Gilles Peskined911eb72018-08-14 15:18:45 +02001893 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001894 if( status != PSA_SUCCESS )
1895 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001896 if( is_sign )
1897 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001898
Gilles Peskinec5487a82018-12-03 18:08:14 +01001899 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001900 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001901 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001902 key_bits = psa_get_key_bits( slot );
1903
Gilles Peskine8c9def32018-02-08 10:02:12 +01001904#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001905 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001906 {
1907 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001908 mbedtls_cipher_info_from_psa( full_length_alg,
1909 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001910 int ret;
1911 if( cipher_info == NULL )
1912 {
1913 status = PSA_ERROR_NOT_SUPPORTED;
1914 goto exit;
1915 }
1916 operation->mac_size = cipher_info->block_size;
1917 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1918 status = mbedtls_to_psa_error( ret );
1919 }
1920 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001921#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001922#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001923 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001924 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001925 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001926 if( hash_alg == 0 )
1927 {
1928 status = PSA_ERROR_NOT_SUPPORTED;
1929 goto exit;
1930 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001931
1932 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1933 /* Sanity check. This shouldn't fail on a valid configuration. */
1934 if( operation->mac_size == 0 ||
1935 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1936 {
1937 status = PSA_ERROR_NOT_SUPPORTED;
1938 goto exit;
1939 }
1940
Gilles Peskine01126fa2018-07-12 17:04:55 +02001941 if( slot->type != PSA_KEY_TYPE_HMAC )
1942 {
1943 status = PSA_ERROR_INVALID_ARGUMENT;
1944 goto exit;
1945 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001946
Gilles Peskine01126fa2018-07-12 17:04:55 +02001947 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1948 slot->data.raw.data,
1949 slot->data.raw.bytes,
1950 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001951 }
1952 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001953#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001954 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00001955 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02001956 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001957 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001958
Gilles Peskined911eb72018-08-14 15:18:45 +02001959 if( truncated == 0 )
1960 {
1961 /* The "normal" case: untruncated algorithm. Nothing to do. */
1962 }
1963 else if( truncated < 4 )
1964 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001965 /* A very short MAC is too short for security since it can be
1966 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1967 * so we make this our minimum, even though 32 bits is still
1968 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001969 status = PSA_ERROR_NOT_SUPPORTED;
1970 }
1971 else if( truncated > operation->mac_size )
1972 {
1973 /* It's impossible to "truncate" to a larger length. */
1974 status = PSA_ERROR_INVALID_ARGUMENT;
1975 }
1976 else
1977 operation->mac_size = truncated;
1978
Gilles Peskinefbfac682018-07-08 20:51:54 +02001979exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001980 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001981 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001982 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001983 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001984 else
1985 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001986 operation->key_set = 1;
1987 }
1988 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001989}
1990
Gilles Peskine89167cb2018-07-08 20:12:23 +02001991psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001992 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001993 psa_algorithm_t alg )
1994{
Gilles Peskinec5487a82018-12-03 18:08:14 +01001995 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001996}
1997
1998psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001999 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002000 psa_algorithm_t alg )
2001{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002002 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002003}
2004
Gilles Peskine8c9def32018-02-08 10:02:12 +01002005psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2006 const uint8_t *input,
2007 size_t input_length )
2008{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002009 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002010 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002011 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002012 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002013 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002014 operation->has_input = 1;
2015
Gilles Peskine8c9def32018-02-08 10:02:12 +01002016#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002017 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002018 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002019 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2020 input, input_length );
2021 status = mbedtls_to_psa_error( ret );
2022 }
2023 else
2024#endif /* MBEDTLS_CMAC_C */
2025#if defined(MBEDTLS_MD_C)
2026 if( PSA_ALG_IS_HMAC( operation->alg ) )
2027 {
2028 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2029 input_length );
2030 }
2031 else
2032#endif /* MBEDTLS_MD_C */
2033 {
2034 /* This shouldn't happen if `operation` was initialized by
2035 * a setup function. */
2036 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002037 }
2038
Gilles Peskinefbfac682018-07-08 20:51:54 +02002039cleanup:
2040 if( status != PSA_SUCCESS )
2041 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002042 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002043}
2044
Gilles Peskine01126fa2018-07-12 17:04:55 +02002045#if defined(MBEDTLS_MD_C)
2046static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2047 uint8_t *mac,
2048 size_t mac_size )
2049{
2050 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2051 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2052 size_t hash_size = 0;
2053 size_t block_size = psa_get_hash_block_size( hash_alg );
2054 psa_status_t status;
2055
Gilles Peskine01126fa2018-07-12 17:04:55 +02002056 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2057 if( status != PSA_SUCCESS )
2058 return( status );
2059 /* From here on, tmp needs to be wiped. */
2060
2061 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2062 if( status != PSA_SUCCESS )
2063 goto exit;
2064
2065 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2066 if( status != PSA_SUCCESS )
2067 goto exit;
2068
2069 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2070 if( status != PSA_SUCCESS )
2071 goto exit;
2072
Gilles Peskined911eb72018-08-14 15:18:45 +02002073 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2074 if( status != PSA_SUCCESS )
2075 goto exit;
2076
2077 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002078
2079exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002080 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002081 return( status );
2082}
2083#endif /* MBEDTLS_MD_C */
2084
mohammad16036df908f2018-04-02 08:34:15 -07002085static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002086 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002087 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002088{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002089 if( ! operation->key_set )
2090 return( PSA_ERROR_BAD_STATE );
2091 if( operation->iv_required && ! operation->iv_set )
2092 return( PSA_ERROR_BAD_STATE );
2093
Gilles Peskine8c9def32018-02-08 10:02:12 +01002094 if( mac_size < operation->mac_size )
2095 return( PSA_ERROR_BUFFER_TOO_SMALL );
2096
Gilles Peskine8c9def32018-02-08 10:02:12 +01002097#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002098 if( operation->alg == PSA_ALG_CMAC )
2099 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002100 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2101 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2102 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002103 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002104 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002105 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002106 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002107 else
2108#endif /* MBEDTLS_CMAC_C */
2109#if defined(MBEDTLS_MD_C)
2110 if( PSA_ALG_IS_HMAC( operation->alg ) )
2111 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002112 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002113 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002114 }
2115 else
2116#endif /* MBEDTLS_MD_C */
2117 {
2118 /* This shouldn't happen if `operation` was initialized by
2119 * a setup function. */
2120 return( PSA_ERROR_BAD_STATE );
2121 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002122}
2123
Gilles Peskineacd4be32018-07-08 19:56:25 +02002124psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2125 uint8_t *mac,
2126 size_t mac_size,
2127 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002128{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002129 psa_status_t status;
2130
2131 /* Fill the output buffer with something that isn't a valid mac
2132 * (barring an attack on the mac and deliberately-crafted input),
2133 * in case the caller doesn't check the return status properly. */
2134 *mac_length = mac_size;
2135 /* If mac_size is 0 then mac may be NULL and then the
2136 * call to memset would have undefined behavior. */
2137 if( mac_size != 0 )
2138 memset( mac, '!', mac_size );
2139
Gilles Peskine89167cb2018-07-08 20:12:23 +02002140 if( ! operation->is_sign )
2141 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002142 status = PSA_ERROR_BAD_STATE;
2143 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002144 }
mohammad16036df908f2018-04-02 08:34:15 -07002145
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002146 status = psa_mac_finish_internal( operation, mac, mac_size );
2147
2148cleanup:
2149 if( status == PSA_SUCCESS )
2150 {
2151 status = psa_mac_abort( operation );
2152 if( status == PSA_SUCCESS )
2153 *mac_length = operation->mac_size;
2154 else
2155 memset( mac, '!', mac_size );
2156 }
2157 else
2158 psa_mac_abort( operation );
2159 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002160}
2161
Gilles Peskineacd4be32018-07-08 19:56:25 +02002162psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2163 const uint8_t *mac,
2164 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002165{
Gilles Peskine828ed142018-06-18 23:25:51 +02002166 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002167 psa_status_t status;
2168
Gilles Peskine89167cb2018-07-08 20:12:23 +02002169 if( operation->is_sign )
2170 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002171 status = PSA_ERROR_BAD_STATE;
2172 goto cleanup;
2173 }
2174 if( operation->mac_size != mac_length )
2175 {
2176 status = PSA_ERROR_INVALID_SIGNATURE;
2177 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002178 }
mohammad16036df908f2018-04-02 08:34:15 -07002179
2180 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002181 actual_mac, sizeof( actual_mac ) );
2182
2183 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2184 status = PSA_ERROR_INVALID_SIGNATURE;
2185
2186cleanup:
2187 if( status == PSA_SUCCESS )
2188 status = psa_mac_abort( operation );
2189 else
2190 psa_mac_abort( operation );
2191
Gilles Peskine3f108122018-12-07 18:14:53 +01002192 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002193
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002194 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002195}
2196
2197
Gilles Peskine20035e32018-02-03 22:44:14 +01002198
Gilles Peskine20035e32018-02-03 22:44:14 +01002199/****************************************************************/
2200/* Asymmetric cryptography */
2201/****************************************************************/
2202
Gilles Peskine2b450e32018-06-27 15:42:46 +02002203#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002204/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002205 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002206static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2207 size_t hash_length,
2208 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002209{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002210 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002211 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002212 *md_alg = mbedtls_md_get_type( md_info );
2213
2214 /* The Mbed TLS RSA module uses an unsigned int for hash length
2215 * parameters. Validate that it fits so that we don't risk an
2216 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002217#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002218 if( hash_length > UINT_MAX )
2219 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002220#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002221
2222#if defined(MBEDTLS_PKCS1_V15)
2223 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2224 * must be correct. */
2225 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2226 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002227 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002228 if( md_info == NULL )
2229 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002230 if( mbedtls_md_get_size( md_info ) != hash_length )
2231 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002232 }
2233#endif /* MBEDTLS_PKCS1_V15 */
2234
2235#if defined(MBEDTLS_PKCS1_V21)
2236 /* PSS requires a hash internally. */
2237 if( PSA_ALG_IS_RSA_PSS( alg ) )
2238 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002239 if( md_info == NULL )
2240 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002241 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002242#endif /* MBEDTLS_PKCS1_V21 */
2243
Gilles Peskine61b91d42018-06-08 16:09:36 +02002244 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002245}
2246
Gilles Peskine2b450e32018-06-27 15:42:46 +02002247static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2248 psa_algorithm_t alg,
2249 const uint8_t *hash,
2250 size_t hash_length,
2251 uint8_t *signature,
2252 size_t signature_size,
2253 size_t *signature_length )
2254{
2255 psa_status_t status;
2256 int ret;
2257 mbedtls_md_type_t md_alg;
2258
2259 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2260 if( status != PSA_SUCCESS )
2261 return( status );
2262
Gilles Peskine630a18a2018-06-29 17:49:35 +02002263 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002264 return( PSA_ERROR_BUFFER_TOO_SMALL );
2265
2266#if defined(MBEDTLS_PKCS1_V15)
2267 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2268 {
2269 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2270 MBEDTLS_MD_NONE );
2271 ret = mbedtls_rsa_pkcs1_sign( rsa,
2272 mbedtls_ctr_drbg_random,
2273 &global_data.ctr_drbg,
2274 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002275 md_alg,
2276 (unsigned int) hash_length,
2277 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002278 signature );
2279 }
2280 else
2281#endif /* MBEDTLS_PKCS1_V15 */
2282#if defined(MBEDTLS_PKCS1_V21)
2283 if( PSA_ALG_IS_RSA_PSS( alg ) )
2284 {
2285 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2286 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2287 mbedtls_ctr_drbg_random,
2288 &global_data.ctr_drbg,
2289 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002290 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002291 (unsigned int) hash_length,
2292 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002293 signature );
2294 }
2295 else
2296#endif /* MBEDTLS_PKCS1_V21 */
2297 {
2298 return( PSA_ERROR_INVALID_ARGUMENT );
2299 }
2300
2301 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002302 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002303 return( mbedtls_to_psa_error( ret ) );
2304}
2305
2306static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2307 psa_algorithm_t alg,
2308 const uint8_t *hash,
2309 size_t hash_length,
2310 const uint8_t *signature,
2311 size_t signature_length )
2312{
2313 psa_status_t status;
2314 int ret;
2315 mbedtls_md_type_t md_alg;
2316
2317 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2318 if( status != PSA_SUCCESS )
2319 return( status );
2320
Gilles Peskine630a18a2018-06-29 17:49:35 +02002321 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002322 return( PSA_ERROR_BUFFER_TOO_SMALL );
2323
2324#if defined(MBEDTLS_PKCS1_V15)
2325 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2326 {
2327 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2328 MBEDTLS_MD_NONE );
2329 ret = mbedtls_rsa_pkcs1_verify( rsa,
2330 mbedtls_ctr_drbg_random,
2331 &global_data.ctr_drbg,
2332 MBEDTLS_RSA_PUBLIC,
2333 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002334 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002335 hash,
2336 signature );
2337 }
2338 else
2339#endif /* MBEDTLS_PKCS1_V15 */
2340#if defined(MBEDTLS_PKCS1_V21)
2341 if( PSA_ALG_IS_RSA_PSS( alg ) )
2342 {
2343 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2344 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2345 mbedtls_ctr_drbg_random,
2346 &global_data.ctr_drbg,
2347 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002348 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002349 (unsigned int) hash_length,
2350 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002351 signature );
2352 }
2353 else
2354#endif /* MBEDTLS_PKCS1_V21 */
2355 {
2356 return( PSA_ERROR_INVALID_ARGUMENT );
2357 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002358
2359 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2360 * the rest of the signature is invalid". This has little use in
2361 * practice and PSA doesn't report this distinction. */
2362 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2363 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002364 return( mbedtls_to_psa_error( ret ) );
2365}
2366#endif /* MBEDTLS_RSA_C */
2367
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002368#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002369/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2370 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2371 * (even though these functions don't modify it). */
2372static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2373 psa_algorithm_t alg,
2374 const uint8_t *hash,
2375 size_t hash_length,
2376 uint8_t *signature,
2377 size_t signature_size,
2378 size_t *signature_length )
2379{
2380 int ret;
2381 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002382 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002383 mbedtls_mpi_init( &r );
2384 mbedtls_mpi_init( &s );
2385
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002386 if( signature_size < 2 * curve_bytes )
2387 {
2388 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2389 goto cleanup;
2390 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002391
Gilles Peskinea05219c2018-11-16 16:02:56 +01002392#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002393 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2394 {
2395 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2396 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2397 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2398 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2399 hash, hash_length,
2400 md_alg ) );
2401 }
2402 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002403#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002404 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002405 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002406 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2407 hash, hash_length,
2408 mbedtls_ctr_drbg_random,
2409 &global_data.ctr_drbg ) );
2410 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002411
2412 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2413 signature,
2414 curve_bytes ) );
2415 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2416 signature + curve_bytes,
2417 curve_bytes ) );
2418
2419cleanup:
2420 mbedtls_mpi_free( &r );
2421 mbedtls_mpi_free( &s );
2422 if( ret == 0 )
2423 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002424 return( mbedtls_to_psa_error( ret ) );
2425}
2426
2427static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2428 const uint8_t *hash,
2429 size_t hash_length,
2430 const uint8_t *signature,
2431 size_t signature_length )
2432{
2433 int ret;
2434 mbedtls_mpi r, s;
2435 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2436 mbedtls_mpi_init( &r );
2437 mbedtls_mpi_init( &s );
2438
2439 if( signature_length != 2 * curve_bytes )
2440 return( PSA_ERROR_INVALID_SIGNATURE );
2441
2442 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2443 signature,
2444 curve_bytes ) );
2445 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2446 signature + curve_bytes,
2447 curve_bytes ) );
2448
2449 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2450 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002451
2452cleanup:
2453 mbedtls_mpi_free( &r );
2454 mbedtls_mpi_free( &s );
2455 return( mbedtls_to_psa_error( ret ) );
2456}
2457#endif /* MBEDTLS_ECDSA_C */
2458
Gilles Peskinec5487a82018-12-03 18:08:14 +01002459psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002460 psa_algorithm_t alg,
2461 const uint8_t *hash,
2462 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002463 uint8_t *signature,
2464 size_t signature_size,
2465 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002466{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002467 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002468 psa_status_t status;
2469
2470 *signature_length = signature_size;
2471
Gilles Peskinec5487a82018-12-03 18:08:14 +01002472 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002473 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002474 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002475 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002476 {
2477 status = PSA_ERROR_INVALID_ARGUMENT;
2478 goto exit;
2479 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002480
Gilles Peskine20035e32018-02-03 22:44:14 +01002481#if defined(MBEDTLS_RSA_C)
2482 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2483 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002484 status = psa_rsa_sign( slot->data.rsa,
2485 alg,
2486 hash, hash_length,
2487 signature, signature_size,
2488 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002489 }
2490 else
2491#endif /* defined(MBEDTLS_RSA_C) */
2492#if defined(MBEDTLS_ECP_C)
2493 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2494 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002495#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002496 if(
2497#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2498 PSA_ALG_IS_ECDSA( alg )
2499#else
2500 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2501#endif
2502 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002503 status = psa_ecdsa_sign( slot->data.ecp,
2504 alg,
2505 hash, hash_length,
2506 signature, signature_size,
2507 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002508 else
2509#endif /* defined(MBEDTLS_ECDSA_C) */
2510 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002511 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002512 }
itayzafrir5c753392018-05-08 11:18:38 +03002513 }
2514 else
2515#endif /* defined(MBEDTLS_ECP_C) */
2516 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002517 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002518 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002519
2520exit:
2521 /* Fill the unused part of the output buffer (the whole buffer on error,
2522 * the trailing part on success) with something that isn't a valid mac
2523 * (barring an attack on the mac and deliberately-crafted input),
2524 * in case the caller doesn't check the return status properly. */
2525 if( status == PSA_SUCCESS )
2526 memset( signature + *signature_length, '!',
2527 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002528 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002529 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002530 /* If signature_size is 0 then we have nothing to do. We must not call
2531 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002532 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002533}
2534
Gilles Peskinec5487a82018-12-03 18:08:14 +01002535psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002536 psa_algorithm_t alg,
2537 const uint8_t *hash,
2538 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002539 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002540 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002541{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002542 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002543 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002544
Gilles Peskinec5487a82018-12-03 18:08:14 +01002545 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002546 if( status != PSA_SUCCESS )
2547 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002548
Gilles Peskine61b91d42018-06-08 16:09:36 +02002549#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002550 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002551 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002552 return( psa_rsa_verify( slot->data.rsa,
2553 alg,
2554 hash, hash_length,
2555 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002556 }
2557 else
2558#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002559#if defined(MBEDTLS_ECP_C)
2560 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2561 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002562#if defined(MBEDTLS_ECDSA_C)
2563 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002564 return( psa_ecdsa_verify( slot->data.ecp,
2565 hash, hash_length,
2566 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002567 else
2568#endif /* defined(MBEDTLS_ECDSA_C) */
2569 {
2570 return( PSA_ERROR_INVALID_ARGUMENT );
2571 }
itayzafrir5c753392018-05-08 11:18:38 +03002572 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002573 else
2574#endif /* defined(MBEDTLS_ECP_C) */
2575 {
2576 return( PSA_ERROR_NOT_SUPPORTED );
2577 }
2578}
2579
Gilles Peskine072ac562018-06-30 00:21:29 +02002580#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2581static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2582 mbedtls_rsa_context *rsa )
2583{
2584 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2585 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2586 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2587 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2588}
2589#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2590
Gilles Peskinec5487a82018-12-03 18:08:14 +01002591psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002592 psa_algorithm_t alg,
2593 const uint8_t *input,
2594 size_t input_length,
2595 const uint8_t *salt,
2596 size_t salt_length,
2597 uint8_t *output,
2598 size_t output_size,
2599 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002600{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002601 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002602 psa_status_t status;
2603
Darryl Green5cc689a2018-07-24 15:34:10 +01002604 (void) input;
2605 (void) input_length;
2606 (void) salt;
2607 (void) output;
2608 (void) output_size;
2609
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002610 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002611
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002612 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2613 return( PSA_ERROR_INVALID_ARGUMENT );
2614
Gilles Peskinec5487a82018-12-03 18:08:14 +01002615 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002616 if( status != PSA_SUCCESS )
2617 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002618 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2619 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002620 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002621
2622#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002623 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002624 {
2625 mbedtls_rsa_context *rsa = slot->data.rsa;
2626 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002627 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002628 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002629#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002630 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002631 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002632 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2633 mbedtls_ctr_drbg_random,
2634 &global_data.ctr_drbg,
2635 MBEDTLS_RSA_PUBLIC,
2636 input_length,
2637 input,
2638 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002639 }
2640 else
2641#endif /* MBEDTLS_PKCS1_V15 */
2642#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002643 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002644 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002645 psa_rsa_oaep_set_padding_mode( alg, rsa );
2646 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2647 mbedtls_ctr_drbg_random,
2648 &global_data.ctr_drbg,
2649 MBEDTLS_RSA_PUBLIC,
2650 salt, salt_length,
2651 input_length,
2652 input,
2653 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002654 }
2655 else
2656#endif /* MBEDTLS_PKCS1_V21 */
2657 {
2658 return( PSA_ERROR_INVALID_ARGUMENT );
2659 }
2660 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002661 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002662 return( mbedtls_to_psa_error( ret ) );
2663 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002664 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002665#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002666 {
2667 return( PSA_ERROR_NOT_SUPPORTED );
2668 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002669}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002670
Gilles Peskinec5487a82018-12-03 18:08:14 +01002671psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002672 psa_algorithm_t alg,
2673 const uint8_t *input,
2674 size_t input_length,
2675 const uint8_t *salt,
2676 size_t salt_length,
2677 uint8_t *output,
2678 size_t output_size,
2679 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002680{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002681 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002682 psa_status_t status;
2683
Darryl Green5cc689a2018-07-24 15:34:10 +01002684 (void) input;
2685 (void) input_length;
2686 (void) salt;
2687 (void) output;
2688 (void) output_size;
2689
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002690 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002691
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002692 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2693 return( PSA_ERROR_INVALID_ARGUMENT );
2694
Gilles Peskinec5487a82018-12-03 18:08:14 +01002695 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002696 if( status != PSA_SUCCESS )
2697 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002698 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2699 return( PSA_ERROR_INVALID_ARGUMENT );
2700
2701#if defined(MBEDTLS_RSA_C)
2702 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2703 {
2704 mbedtls_rsa_context *rsa = slot->data.rsa;
2705 int ret;
2706
Gilles Peskine630a18a2018-06-29 17:49:35 +02002707 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002708 return( PSA_ERROR_INVALID_ARGUMENT );
2709
2710#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002711 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002712 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002713 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2714 mbedtls_ctr_drbg_random,
2715 &global_data.ctr_drbg,
2716 MBEDTLS_RSA_PRIVATE,
2717 output_length,
2718 input,
2719 output,
2720 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002721 }
2722 else
2723#endif /* MBEDTLS_PKCS1_V15 */
2724#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002725 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002726 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002727 psa_rsa_oaep_set_padding_mode( alg, rsa );
2728 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2729 mbedtls_ctr_drbg_random,
2730 &global_data.ctr_drbg,
2731 MBEDTLS_RSA_PRIVATE,
2732 salt, salt_length,
2733 output_length,
2734 input,
2735 output,
2736 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002737 }
2738 else
2739#endif /* MBEDTLS_PKCS1_V21 */
2740 {
2741 return( PSA_ERROR_INVALID_ARGUMENT );
2742 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002743
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002744 return( mbedtls_to_psa_error( ret ) );
2745 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002746 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002747#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002748 {
2749 return( PSA_ERROR_NOT_SUPPORTED );
2750 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002751}
Gilles Peskine20035e32018-02-03 22:44:14 +01002752
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002753
2754
mohammad1603503973b2018-03-12 15:59:30 +02002755/****************************************************************/
2756/* Symmetric cryptography */
2757/****************************************************************/
2758
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002759/* Initialize the cipher operation structure. Once this function has been
2760 * called, psa_cipher_abort can run and will do the right thing. */
2761static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2762 psa_algorithm_t alg )
2763{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002764 if( ! PSA_ALG_IS_CIPHER( alg ) )
2765 {
2766 memset( operation, 0, sizeof( *operation ) );
2767 return( PSA_ERROR_INVALID_ARGUMENT );
2768 }
2769
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002770 operation->alg = alg;
2771 operation->key_set = 0;
2772 operation->iv_set = 0;
2773 operation->iv_required = 1;
2774 operation->iv_size = 0;
2775 operation->block_size = 0;
2776 mbedtls_cipher_init( &operation->ctx.cipher );
2777 return( PSA_SUCCESS );
2778}
2779
Gilles Peskinee553c652018-06-04 16:22:46 +02002780static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002781 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002782 psa_algorithm_t alg,
2783 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002784{
2785 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2786 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002787 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002788 size_t key_bits;
2789 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002790 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2791 PSA_KEY_USAGE_ENCRYPT :
2792 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002793
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002794 status = psa_cipher_init( operation, alg );
2795 if( status != PSA_SUCCESS )
2796 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002797
Gilles Peskinec5487a82018-12-03 18:08:14 +01002798 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002799 if( status != PSA_SUCCESS )
2800 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002801 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002802
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002803 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002804 if( cipher_info == NULL )
2805 return( PSA_ERROR_NOT_SUPPORTED );
2806
mohammad1603503973b2018-03-12 15:59:30 +02002807 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002808 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002809 {
2810 psa_cipher_abort( operation );
2811 return( mbedtls_to_psa_error( ret ) );
2812 }
2813
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002814#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002815 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002816 {
2817 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2818 unsigned char keys[24];
2819 memcpy( keys, slot->data.raw.data, 16 );
2820 memcpy( keys + 16, slot->data.raw.data, 8 );
2821 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2822 keys,
2823 192, cipher_operation );
2824 }
2825 else
2826#endif
2827 {
2828 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2829 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002830 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002831 }
Moran Peker41deec42018-04-04 15:43:05 +03002832 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002833 {
2834 psa_cipher_abort( operation );
2835 return( mbedtls_to_psa_error( ret ) );
2836 }
2837
mohammad16038481e742018-03-18 13:57:31 +02002838#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002839 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002840 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002841 case PSA_ALG_CBC_NO_PADDING:
2842 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2843 MBEDTLS_PADDING_NONE );
2844 break;
2845 case PSA_ALG_CBC_PKCS7:
2846 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2847 MBEDTLS_PADDING_PKCS7 );
2848 break;
2849 default:
2850 /* The algorithm doesn't involve padding. */
2851 ret = 0;
2852 break;
2853 }
2854 if( ret != 0 )
2855 {
2856 psa_cipher_abort( operation );
2857 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002858 }
2859#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2860
mohammad1603503973b2018-03-12 15:59:30 +02002861 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002862 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2863 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2864 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002865 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002866 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002867 }
mohammad1603503973b2018-03-12 15:59:30 +02002868
Moran Peker395db872018-05-31 14:07:14 +03002869 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002870}
2871
Gilles Peskinefe119512018-07-08 21:39:34 +02002872psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002873 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002874 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002875{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002876 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002877}
2878
Gilles Peskinefe119512018-07-08 21:39:34 +02002879psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002880 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002881 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002882{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002883 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002884}
2885
Gilles Peskinefe119512018-07-08 21:39:34 +02002886psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2887 unsigned char *iv,
2888 size_t iv_size,
2889 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002890{
itayzafrir534bd7c2018-08-02 13:56:32 +03002891 psa_status_t status;
2892 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002893 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002894 {
2895 status = PSA_ERROR_BAD_STATE;
2896 goto exit;
2897 }
Moran Peker41deec42018-04-04 15:43:05 +03002898 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002899 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002900 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002901 goto exit;
2902 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002903 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2904 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002905 if( ret != 0 )
2906 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002907 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002908 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002909 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002910
mohammad16038481e742018-03-18 13:57:31 +02002911 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002912 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002913
Moran Peker395db872018-05-31 14:07:14 +03002914exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002915 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002916 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002917 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002918}
2919
Gilles Peskinefe119512018-07-08 21:39:34 +02002920psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2921 const unsigned char *iv,
2922 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002923{
itayzafrir534bd7c2018-08-02 13:56:32 +03002924 psa_status_t status;
2925 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002926 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002927 {
2928 status = PSA_ERROR_BAD_STATE;
2929 goto exit;
2930 }
Moran Pekera28258c2018-05-29 16:25:04 +03002931 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002932 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002933 status = PSA_ERROR_INVALID_ARGUMENT;
2934 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002935 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002936 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2937 status = mbedtls_to_psa_error( ret );
2938exit:
2939 if( status == PSA_SUCCESS )
2940 operation->iv_set = 1;
2941 else
mohammad1603503973b2018-03-12 15:59:30 +02002942 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002943 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002944}
2945
Gilles Peskinee553c652018-06-04 16:22:46 +02002946psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2947 const uint8_t *input,
2948 size_t input_length,
2949 unsigned char *output,
2950 size_t output_size,
2951 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002952{
itayzafrir534bd7c2018-08-02 13:56:32 +03002953 psa_status_t status;
2954 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002955 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002956 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002957 {
2958 /* Take the unprocessed partial block left over from previous
2959 * update calls, if any, plus the input to this call. Remove
2960 * the last partial block, if any. You get the data that will be
2961 * output in this call. */
2962 expected_output_size =
2963 ( operation->ctx.cipher.unprocessed_len + input_length )
2964 / operation->block_size * operation->block_size;
2965 }
2966 else
2967 {
2968 expected_output_size = input_length;
2969 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002970
Gilles Peskine89d789c2018-06-04 17:17:16 +02002971 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002972 {
2973 status = PSA_ERROR_BUFFER_TOO_SMALL;
2974 goto exit;
2975 }
mohammad160382759612018-03-12 18:16:40 +02002976
mohammad1603503973b2018-03-12 15:59:30 +02002977 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002978 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002979 status = mbedtls_to_psa_error( ret );
2980exit:
2981 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002982 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002983 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002984}
2985
Gilles Peskinee553c652018-06-04 16:22:46 +02002986psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2987 uint8_t *output,
2988 size_t output_size,
2989 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002990{
Janos Follath315b51c2018-07-09 16:04:51 +01002991 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2992 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002993 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002994
mohammad1603503973b2018-03-12 15:59:30 +02002995 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002996 {
Janos Follath315b51c2018-07-09 16:04:51 +01002997 status = PSA_ERROR_BAD_STATE;
2998 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002999 }
3000 if( operation->iv_required && ! operation->iv_set )
3001 {
Janos Follath315b51c2018-07-09 16:04:51 +01003002 status = PSA_ERROR_BAD_STATE;
3003 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03003004 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003005
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003006 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003007 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3008 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003009 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003010 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003011 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003012 }
3013
Janos Follath315b51c2018-07-09 16:04:51 +01003014 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3015 temp_output_buffer,
3016 output_length );
3017 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003018 {
Janos Follath315b51c2018-07-09 16:04:51 +01003019 status = mbedtls_to_psa_error( cipher_ret );
3020 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003021 }
Janos Follath315b51c2018-07-09 16:04:51 +01003022
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003023 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003024 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003025 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003026 memcpy( output, temp_output_buffer, *output_length );
3027 else
3028 {
Janos Follath315b51c2018-07-09 16:04:51 +01003029 status = PSA_ERROR_BUFFER_TOO_SMALL;
3030 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003031 }
mohammad1603503973b2018-03-12 15:59:30 +02003032
Gilles Peskine3f108122018-12-07 18:14:53 +01003033 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003034 status = psa_cipher_abort( operation );
3035
3036 return( status );
3037
3038error:
3039
3040 *output_length = 0;
3041
Gilles Peskine3f108122018-12-07 18:14:53 +01003042 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003043 (void) psa_cipher_abort( operation );
3044
3045 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003046}
3047
Gilles Peskinee553c652018-06-04 16:22:46 +02003048psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3049{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003050 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003051 {
3052 /* The object has (apparently) been initialized but it is not
3053 * in use. It's ok to call abort on such an object, and there's
3054 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003055 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003056 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003057
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003058 /* Sanity check (shouldn't happen: operation->alg should
3059 * always have been initialized to a valid value). */
3060 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3061 return( PSA_ERROR_BAD_STATE );
3062
mohammad1603503973b2018-03-12 15:59:30 +02003063 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003064
Moran Peker41deec42018-04-04 15:43:05 +03003065 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003066 operation->key_set = 0;
3067 operation->iv_set = 0;
3068 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003069 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003070 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003071
Moran Peker395db872018-05-31 14:07:14 +03003072 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003073}
3074
Gilles Peskinea0655c32018-04-30 17:06:50 +02003075
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003076
mohammad16038cc1cee2018-03-28 01:21:33 +03003077/****************************************************************/
3078/* Key Policy */
3079/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003080
mohammad160327010052018-07-03 13:16:15 +03003081#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003082void psa_key_policy_set_usage( psa_key_policy_t *policy,
3083 psa_key_usage_t usage,
3084 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003085{
mohammad16034eed7572018-03-28 05:14:59 -07003086 policy->usage = usage;
3087 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003088}
3089
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003090psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003091{
mohammad16036df908f2018-04-02 08:34:15 -07003092 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003093}
3094
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003095psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003096{
mohammad16036df908f2018-04-02 08:34:15 -07003097 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003098}
mohammad160327010052018-07-03 13:16:15 +03003099#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003100
Gilles Peskinec5487a82018-12-03 18:08:14 +01003101psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003102 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003103{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003104 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003105 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003106
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003107 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003108 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003109
Gilles Peskinec5487a82018-12-03 18:08:14 +01003110 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003111 if( status != PSA_SUCCESS )
3112 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003113
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003114 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3115 PSA_KEY_USAGE_ENCRYPT |
3116 PSA_KEY_USAGE_DECRYPT |
3117 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003118 PSA_KEY_USAGE_VERIFY |
3119 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003120 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003121
mohammad16036df908f2018-04-02 08:34:15 -07003122 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003123
3124 return( PSA_SUCCESS );
3125}
3126
Gilles Peskinec5487a82018-12-03 18:08:14 +01003127psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003128 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003129{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003130 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003131 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003132
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003133 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003134 return( PSA_ERROR_INVALID_ARGUMENT );
3135
Gilles Peskinec5487a82018-12-03 18:08:14 +01003136 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003137 if( status != PSA_SUCCESS )
3138 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003139
mohammad16036df908f2018-04-02 08:34:15 -07003140 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003141
3142 return( PSA_SUCCESS );
3143}
Gilles Peskine20035e32018-02-03 22:44:14 +01003144
Gilles Peskinea0655c32018-04-30 17:06:50 +02003145
3146
mohammad1603804cd712018-03-20 22:44:08 +02003147/****************************************************************/
3148/* Key Lifetime */
3149/****************************************************************/
3150
Gilles Peskinec5487a82018-12-03 18:08:14 +01003151psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003152 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003153{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003154 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003155 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003156
Gilles Peskinec5487a82018-12-03 18:08:14 +01003157 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003158 if( status != PSA_SUCCESS )
3159 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003160
mohammad1603804cd712018-03-20 22:44:08 +02003161 *lifetime = slot->lifetime;
3162
3163 return( PSA_SUCCESS );
3164}
3165
Gilles Peskine20035e32018-02-03 22:44:14 +01003166
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003167
mohammad16035955c982018-04-26 00:53:03 +03003168/****************************************************************/
3169/* AEAD */
3170/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003171
Gilles Peskineedf9a652018-08-17 18:11:56 +02003172typedef struct
3173{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003174 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003175 const mbedtls_cipher_info_t *cipher_info;
3176 union
3177 {
3178#if defined(MBEDTLS_CCM_C)
3179 mbedtls_ccm_context ccm;
3180#endif /* MBEDTLS_CCM_C */
3181#if defined(MBEDTLS_GCM_C)
3182 mbedtls_gcm_context gcm;
3183#endif /* MBEDTLS_GCM_C */
3184 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003185 psa_algorithm_t core_alg;
3186 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003187 uint8_t tag_length;
3188} aead_operation_t;
3189
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003190static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003191{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003192 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003193 {
3194#if defined(MBEDTLS_CCM_C)
3195 case PSA_ALG_CCM:
3196 mbedtls_ccm_free( &operation->ctx.ccm );
3197 break;
3198#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003199#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003200 case PSA_ALG_GCM:
3201 mbedtls_gcm_free( &operation->ctx.gcm );
3202 break;
3203#endif /* MBEDTLS_GCM_C */
3204 }
3205}
3206
3207static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003208 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003209 psa_key_usage_t usage,
3210 psa_algorithm_t alg )
3211{
3212 psa_status_t status;
3213 size_t key_bits;
3214 mbedtls_cipher_id_t cipher_id;
3215
Gilles Peskinec5487a82018-12-03 18:08:14 +01003216 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003217 if( status != PSA_SUCCESS )
3218 return( status );
3219
3220 key_bits = psa_get_key_bits( operation->slot );
3221
3222 operation->cipher_info =
3223 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3224 &cipher_id );
3225 if( operation->cipher_info == NULL )
3226 return( PSA_ERROR_NOT_SUPPORTED );
3227
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003228 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003229 {
3230#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003231 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3232 operation->core_alg = PSA_ALG_CCM;
3233 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003234 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3235 return( PSA_ERROR_INVALID_ARGUMENT );
3236 mbedtls_ccm_init( &operation->ctx.ccm );
3237 status = mbedtls_to_psa_error(
3238 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3239 operation->slot->data.raw.data,
3240 (unsigned int) key_bits ) );
3241 if( status != 0 )
3242 goto cleanup;
3243 break;
3244#endif /* MBEDTLS_CCM_C */
3245
3246#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003247 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3248 operation->core_alg = PSA_ALG_GCM;
3249 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003250 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3251 return( PSA_ERROR_INVALID_ARGUMENT );
3252 mbedtls_gcm_init( &operation->ctx.gcm );
3253 status = mbedtls_to_psa_error(
3254 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3255 operation->slot->data.raw.data,
3256 (unsigned int) key_bits ) );
3257 break;
3258#endif /* MBEDTLS_GCM_C */
3259
3260 default:
3261 return( PSA_ERROR_NOT_SUPPORTED );
3262 }
3263
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003264 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3265 {
3266 status = PSA_ERROR_INVALID_ARGUMENT;
3267 goto cleanup;
3268 }
3269 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3270 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3271 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3272 * In both cases, mbedtls_xxx will validate the tag length below. */
3273
Gilles Peskineedf9a652018-08-17 18:11:56 +02003274 return( PSA_SUCCESS );
3275
3276cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003277 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003278 return( status );
3279}
3280
Gilles Peskinec5487a82018-12-03 18:08:14 +01003281psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003282 psa_algorithm_t alg,
3283 const uint8_t *nonce,
3284 size_t nonce_length,
3285 const uint8_t *additional_data,
3286 size_t additional_data_length,
3287 const uint8_t *plaintext,
3288 size_t plaintext_length,
3289 uint8_t *ciphertext,
3290 size_t ciphertext_size,
3291 size_t *ciphertext_length )
3292{
mohammad16035955c982018-04-26 00:53:03 +03003293 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003294 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003295 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003296
mohammad1603f08a5502018-06-03 15:05:47 +03003297 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003298
Gilles Peskinec5487a82018-12-03 18:08:14 +01003299 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003300 if( status != PSA_SUCCESS )
3301 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003302
Gilles Peskineedf9a652018-08-17 18:11:56 +02003303 /* For all currently supported modes, the tag is at the end of the
3304 * ciphertext. */
3305 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3306 {
3307 status = PSA_ERROR_BUFFER_TOO_SMALL;
3308 goto exit;
3309 }
3310 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003311
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003312#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003313 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003314 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003315 status = mbedtls_to_psa_error(
3316 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3317 MBEDTLS_GCM_ENCRYPT,
3318 plaintext_length,
3319 nonce, nonce_length,
3320 additional_data, additional_data_length,
3321 plaintext, ciphertext,
3322 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003323 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003324 else
3325#endif /* MBEDTLS_GCM_C */
3326#if defined(MBEDTLS_CCM_C)
3327 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003328 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003329 status = mbedtls_to_psa_error(
3330 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3331 plaintext_length,
3332 nonce, nonce_length,
3333 additional_data,
3334 additional_data_length,
3335 plaintext, ciphertext,
3336 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003337 }
mohammad16035c8845f2018-05-09 05:40:09 -07003338 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003339#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003340 {
mohammad1603554faad2018-06-03 15:07:38 +03003341 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003342 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003343
Gilles Peskineedf9a652018-08-17 18:11:56 +02003344 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3345 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003346
Gilles Peskineedf9a652018-08-17 18:11:56 +02003347exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003348 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003349 if( status == PSA_SUCCESS )
3350 *ciphertext_length = plaintext_length + operation.tag_length;
3351 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003352}
3353
Gilles Peskineee652a32018-06-01 19:23:52 +02003354/* Locate the tag in a ciphertext buffer containing the encrypted data
3355 * followed by the tag. Return the length of the part preceding the tag in
3356 * *plaintext_length. This is the size of the plaintext in modes where
3357 * the encrypted data has the same size as the plaintext, such as
3358 * CCM and GCM. */
3359static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3360 const uint8_t *ciphertext,
3361 size_t ciphertext_length,
3362 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003363 const uint8_t **p_tag )
3364{
3365 size_t payload_length;
3366 if( tag_length > ciphertext_length )
3367 return( PSA_ERROR_INVALID_ARGUMENT );
3368 payload_length = ciphertext_length - tag_length;
3369 if( payload_length > plaintext_size )
3370 return( PSA_ERROR_BUFFER_TOO_SMALL );
3371 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003372 return( PSA_SUCCESS );
3373}
3374
Gilles Peskinec5487a82018-12-03 18:08:14 +01003375psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003376 psa_algorithm_t alg,
3377 const uint8_t *nonce,
3378 size_t nonce_length,
3379 const uint8_t *additional_data,
3380 size_t additional_data_length,
3381 const uint8_t *ciphertext,
3382 size_t ciphertext_length,
3383 uint8_t *plaintext,
3384 size_t plaintext_size,
3385 size_t *plaintext_length )
3386{
mohammad16035955c982018-04-26 00:53:03 +03003387 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003388 aead_operation_t operation;
3389 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003390
Gilles Peskineee652a32018-06-01 19:23:52 +02003391 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003392
Gilles Peskinec5487a82018-12-03 18:08:14 +01003393 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003394 if( status != PSA_SUCCESS )
3395 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003396
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003397#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003398 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003399 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003400 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003401 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003402 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003403 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003404 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003405
Gilles Peskineedf9a652018-08-17 18:11:56 +02003406 status = mbedtls_to_psa_error(
3407 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3408 ciphertext_length - operation.tag_length,
3409 nonce, nonce_length,
3410 additional_data,
3411 additional_data_length,
3412 tag, operation.tag_length,
3413 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003414 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003415 else
3416#endif /* MBEDTLS_GCM_C */
3417#if defined(MBEDTLS_CCM_C)
3418 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003419 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003420 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003421 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003422 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003423 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003424 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003425
Gilles Peskineedf9a652018-08-17 18:11:56 +02003426 status = mbedtls_to_psa_error(
3427 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3428 ciphertext_length - operation.tag_length,
3429 nonce, nonce_length,
3430 additional_data,
3431 additional_data_length,
3432 ciphertext, plaintext,
3433 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003434 }
mohammad160339574652018-06-01 04:39:53 -07003435 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003436#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003437 {
mohammad1603554faad2018-06-03 15:07:38 +03003438 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003439 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003440
Gilles Peskineedf9a652018-08-17 18:11:56 +02003441 if( status != PSA_SUCCESS && plaintext_size != 0 )
3442 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003443
Gilles Peskineedf9a652018-08-17 18:11:56 +02003444exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003445 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003446 if( status == PSA_SUCCESS )
3447 *plaintext_length = ciphertext_length - operation.tag_length;
3448 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003449}
3450
Gilles Peskinea0655c32018-04-30 17:06:50 +02003451
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003452
Gilles Peskine20035e32018-02-03 22:44:14 +01003453/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003454/* Generators */
3455/****************************************************************/
3456
3457psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3458{
3459 psa_status_t status = PSA_SUCCESS;
3460 if( generator->alg == 0 )
3461 {
3462 /* The object has (apparently) been initialized but it is not
3463 * in use. It's ok to call abort on such an object, and there's
3464 * nothing to do. */
3465 }
3466 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003467 if( generator->alg == PSA_ALG_SELECT_RAW )
3468 {
3469 if( generator->ctx.buffer.data != NULL )
3470 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003471 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003472 generator->ctx.buffer.size );
3473 mbedtls_free( generator->ctx.buffer.data );
3474 }
3475 }
3476 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003477#if defined(MBEDTLS_MD_C)
3478 if( PSA_ALG_IS_HKDF( generator->alg ) )
3479 {
3480 mbedtls_free( generator->ctx.hkdf.info );
3481 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3482 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003483 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3484 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3485 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003486 {
3487 if( generator->ctx.tls12_prf.key != NULL )
3488 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003489 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003490 generator->ctx.tls12_prf.key_len );
3491 mbedtls_free( generator->ctx.tls12_prf.key );
3492 }
Hanno Becker580fba12018-11-13 20:50:45 +00003493
3494 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3495 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003496 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003497 generator->ctx.tls12_prf.Ai_with_seed_len );
3498 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3499 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003500 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003501 else
3502#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003503 {
3504 status = PSA_ERROR_BAD_STATE;
3505 }
3506 memset( generator, 0, sizeof( *generator ) );
3507 return( status );
3508}
3509
3510
3511psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3512 size_t *capacity)
3513{
3514 *capacity = generator->capacity;
3515 return( PSA_SUCCESS );
3516}
3517
Gilles Peskinebef7f142018-07-12 17:22:21 +02003518#if defined(MBEDTLS_MD_C)
3519/* Read some bytes from an HKDF-based generator. This performs a chunk
3520 * of the expand phase of the HKDF algorithm. */
3521static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3522 psa_algorithm_t hash_alg,
3523 uint8_t *output,
3524 size_t output_length )
3525{
3526 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3527 psa_status_t status;
3528
3529 while( output_length != 0 )
3530 {
3531 /* Copy what remains of the current block */
3532 uint8_t n = hash_length - hkdf->offset_in_block;
3533 if( n > output_length )
3534 n = (uint8_t) output_length;
3535 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3536 output += n;
3537 output_length -= n;
3538 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003539 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003540 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003541 /* We can't be wanting more output after block 0xff, otherwise
3542 * the capacity check in psa_generator_read() would have
3543 * prevented this call. It could happen only if the generator
3544 * object was corrupted or if this function is called directly
3545 * inside the library. */
3546 if( hkdf->block_number == 0xff )
3547 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003548
3549 /* We need a new block */
3550 ++hkdf->block_number;
3551 hkdf->offset_in_block = 0;
3552 status = psa_hmac_setup_internal( &hkdf->hmac,
3553 hkdf->prk, hash_length,
3554 hash_alg );
3555 if( status != PSA_SUCCESS )
3556 return( status );
3557 if( hkdf->block_number != 1 )
3558 {
3559 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3560 hkdf->output_block,
3561 hash_length );
3562 if( status != PSA_SUCCESS )
3563 return( status );
3564 }
3565 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3566 hkdf->info,
3567 hkdf->info_length );
3568 if( status != PSA_SUCCESS )
3569 return( status );
3570 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3571 &hkdf->block_number, 1 );
3572 if( status != PSA_SUCCESS )
3573 return( status );
3574 status = psa_hmac_finish_internal( &hkdf->hmac,
3575 hkdf->output_block,
3576 sizeof( hkdf->output_block ) );
3577 if( status != PSA_SUCCESS )
3578 return( status );
3579 }
3580
3581 return( PSA_SUCCESS );
3582}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003583
3584static psa_status_t psa_generator_tls12_prf_generate_next_block(
3585 psa_tls12_prf_generator_t *tls12_prf,
3586 psa_algorithm_t alg )
3587{
3588 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3589 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3590 psa_hmac_internal_data hmac;
3591 psa_status_t status, cleanup_status;
3592
Hanno Becker3b339e22018-11-13 20:56:14 +00003593 unsigned char *Ai;
3594 size_t Ai_len;
3595
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003596 /* We can't be wanting more output after block 0xff, otherwise
3597 * the capacity check in psa_generator_read() would have
3598 * prevented this call. It could happen only if the generator
3599 * object was corrupted or if this function is called directly
3600 * inside the library. */
3601 if( tls12_prf->block_number == 0xff )
3602 return( PSA_ERROR_BAD_STATE );
3603
3604 /* We need a new block */
3605 ++tls12_prf->block_number;
3606 tls12_prf->offset_in_block = 0;
3607
3608 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3609 *
3610 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3611 *
3612 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3613 * HMAC_hash(secret, A(2) + seed) +
3614 * HMAC_hash(secret, A(3) + seed) + ...
3615 *
3616 * A(0) = seed
3617 * A(i) = HMAC_hash( secret, A(i-1) )
3618 *
3619 * The `psa_tls12_prf_generator` structures saves the block
3620 * `HMAC_hash(secret, A(i) + seed)` from which the output
3621 * is currently extracted as `output_block`, while
3622 * `A(i) + seed` is stored in `Ai_with_seed`.
3623 *
3624 * Generating a new block means recalculating `Ai_with_seed`
3625 * from the A(i)-part of it, and afterwards recalculating
3626 * `output_block`.
3627 *
3628 * A(0) is computed at setup time.
3629 *
3630 */
3631
3632 psa_hmac_init_internal( &hmac );
3633
3634 /* We must distinguish the calculation of A(1) from those
3635 * of A(2) and higher, because A(0)=seed has a different
3636 * length than the other A(i). */
3637 if( tls12_prf->block_number == 1 )
3638 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003639 Ai = tls12_prf->Ai_with_seed + hash_length;
3640 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003641 }
3642 else
3643 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003644 Ai = tls12_prf->Ai_with_seed;
3645 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003646 }
3647
Hanno Becker3b339e22018-11-13 20:56:14 +00003648 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3649 status = psa_hmac_setup_internal( &hmac,
3650 tls12_prf->key,
3651 tls12_prf->key_len,
3652 hash_alg );
3653 if( status != PSA_SUCCESS )
3654 goto cleanup;
3655
3656 status = psa_hash_update( &hmac.hash_ctx,
3657 Ai, Ai_len );
3658 if( status != PSA_SUCCESS )
3659 goto cleanup;
3660
3661 status = psa_hmac_finish_internal( &hmac,
3662 tls12_prf->Ai_with_seed,
3663 hash_length );
3664 if( status != PSA_SUCCESS )
3665 goto cleanup;
3666
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003667 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3668 status = psa_hmac_setup_internal( &hmac,
3669 tls12_prf->key,
3670 tls12_prf->key_len,
3671 hash_alg );
3672 if( status != PSA_SUCCESS )
3673 goto cleanup;
3674
3675 status = psa_hash_update( &hmac.hash_ctx,
3676 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003677 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003678 if( status != PSA_SUCCESS )
3679 goto cleanup;
3680
3681 status = psa_hmac_finish_internal( &hmac,
3682 tls12_prf->output_block,
3683 hash_length );
3684 if( status != PSA_SUCCESS )
3685 goto cleanup;
3686
3687cleanup:
3688
3689 cleanup_status = psa_hmac_abort_internal( &hmac );
3690 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3691 status = cleanup_status;
3692
3693 return( status );
3694}
3695
3696/* Read some bytes from an TLS-1.2-PRF-based generator.
3697 * See Section 5 of RFC 5246. */
3698static psa_status_t psa_generator_tls12_prf_read(
3699 psa_tls12_prf_generator_t *tls12_prf,
3700 psa_algorithm_t alg,
3701 uint8_t *output,
3702 size_t output_length )
3703{
3704 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3705 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3706 psa_status_t status;
3707
3708 while( output_length != 0 )
3709 {
3710 /* Copy what remains of the current block */
3711 uint8_t n = hash_length - tls12_prf->offset_in_block;
3712
3713 /* Check if we have fully processed the current block. */
3714 if( n == 0 )
3715 {
3716 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3717 alg );
3718 if( status != PSA_SUCCESS )
3719 return( status );
3720
3721 continue;
3722 }
3723
3724 if( n > output_length )
3725 n = (uint8_t) output_length;
3726 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3727 n );
3728 output += n;
3729 output_length -= n;
3730 tls12_prf->offset_in_block += n;
3731 }
3732
3733 return( PSA_SUCCESS );
3734}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003735#endif /* MBEDTLS_MD_C */
3736
Gilles Peskineeab56e42018-07-12 17:12:33 +02003737psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3738 uint8_t *output,
3739 size_t output_length )
3740{
3741 psa_status_t status;
3742
3743 if( output_length > generator->capacity )
3744 {
3745 generator->capacity = 0;
3746 /* Go through the error path to wipe all confidential data now
3747 * that the generator object is useless. */
3748 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3749 goto exit;
3750 }
3751 if( output_length == 0 &&
3752 generator->capacity == 0 && generator->alg == 0 )
3753 {
3754 /* Edge case: this is a blank or finished generator, and 0
3755 * bytes were requested. The right error in this case could
3756 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3757 * INSUFFICIENT_CAPACITY, which is right for a finished
3758 * generator, for consistency with the case when
3759 * output_length > 0. */
3760 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3761 }
3762 generator->capacity -= output_length;
3763
Gilles Peskine751d9652018-09-18 12:05:44 +02003764 if( generator->alg == PSA_ALG_SELECT_RAW )
3765 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003766 /* Initially, the capacity of a selection generator is always
3767 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3768 * abbreviated in this comment as `size`. When the remaining
3769 * capacity is `c`, the next bytes to serve start `c` bytes
3770 * from the end of the buffer, i.e. `size - c` from the
3771 * beginning of the buffer. Since `generator->capacity` was just
3772 * decremented above, we need to serve the bytes from
3773 * `size - generator->capacity - output_length` to
3774 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003775 size_t offset =
3776 generator->ctx.buffer.size - generator->capacity - output_length;
3777 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3778 status = PSA_SUCCESS;
3779 }
3780 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003781#if defined(MBEDTLS_MD_C)
3782 if( PSA_ALG_IS_HKDF( generator->alg ) )
3783 {
3784 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3785 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3786 output, output_length );
3787 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003788 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3789 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003790 {
3791 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3792 generator->alg, output,
3793 output_length );
3794 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003795 else
3796#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003797 {
3798 return( PSA_ERROR_BAD_STATE );
3799 }
3800
3801exit:
3802 if( status != PSA_SUCCESS )
3803 {
3804 psa_generator_abort( generator );
3805 memset( output, '!', output_length );
3806 }
3807 return( status );
3808}
3809
Gilles Peskine08542d82018-07-19 17:05:42 +02003810#if defined(MBEDTLS_DES_C)
3811static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3812{
3813 if( data_size >= 8 )
3814 mbedtls_des_key_set_parity( data );
3815 if( data_size >= 16 )
3816 mbedtls_des_key_set_parity( data + 8 );
3817 if( data_size >= 24 )
3818 mbedtls_des_key_set_parity( data + 16 );
3819}
3820#endif /* MBEDTLS_DES_C */
3821
Gilles Peskinec5487a82018-12-03 18:08:14 +01003822psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003823 psa_key_type_t type,
3824 size_t bits,
3825 psa_crypto_generator_t *generator )
3826{
3827 uint8_t *data = NULL;
3828 size_t bytes = PSA_BITS_TO_BYTES( bits );
3829 psa_status_t status;
3830
3831 if( ! key_type_is_raw_bytes( type ) )
3832 return( PSA_ERROR_INVALID_ARGUMENT );
3833 if( bits % 8 != 0 )
3834 return( PSA_ERROR_INVALID_ARGUMENT );
3835 data = mbedtls_calloc( 1, bytes );
3836 if( data == NULL )
3837 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3838
3839 status = psa_generator_read( generator, data, bytes );
3840 if( status != PSA_SUCCESS )
3841 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003842#if defined(MBEDTLS_DES_C)
3843 if( type == PSA_KEY_TYPE_DES )
3844 psa_des_set_key_parity( data, bytes );
3845#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003846 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003847
3848exit:
3849 mbedtls_free( data );
3850 return( status );
3851}
3852
3853
3854
3855/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003856/* Key derivation */
3857/****************************************************************/
3858
Gilles Peskinea05219c2018-11-16 16:02:56 +01003859#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003860/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003861 * of the HKDF algorithm.
3862 *
3863 * Note that if this function fails, you must call psa_generator_abort()
3864 * to potentially free embedded data structures and wipe confidential data.
3865 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003866static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003867 const uint8_t *secret,
3868 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003869 psa_algorithm_t hash_alg,
3870 const uint8_t *salt,
3871 size_t salt_length,
3872 const uint8_t *label,
3873 size_t label_length )
3874{
3875 psa_status_t status;
3876 status = psa_hmac_setup_internal( &hkdf->hmac,
3877 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003878 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003879 if( status != PSA_SUCCESS )
3880 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003881 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003882 if( status != PSA_SUCCESS )
3883 return( status );
3884 status = psa_hmac_finish_internal( &hkdf->hmac,
3885 hkdf->prk,
3886 sizeof( hkdf->prk ) );
3887 if( status != PSA_SUCCESS )
3888 return( status );
3889 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3890 hkdf->block_number = 0;
3891 hkdf->info_length = label_length;
3892 if( label_length != 0 )
3893 {
3894 hkdf->info = mbedtls_calloc( 1, label_length );
3895 if( hkdf->info == NULL )
3896 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3897 memcpy( hkdf->info, label, label_length );
3898 }
3899 return( PSA_SUCCESS );
3900}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003901#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003902
Gilles Peskinea05219c2018-11-16 16:02:56 +01003903#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003904/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3905 *
3906 * Note that if this function fails, you must call psa_generator_abort()
3907 * to potentially free embedded data structures and wipe confidential data.
3908 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003909static psa_status_t psa_generator_tls12_prf_setup(
3910 psa_tls12_prf_generator_t *tls12_prf,
3911 const unsigned char *key,
3912 size_t key_len,
3913 psa_algorithm_t hash_alg,
3914 const uint8_t *salt,
3915 size_t salt_length,
3916 const uint8_t *label,
3917 size_t label_length )
3918{
3919 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003920 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3921 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003922
3923 tls12_prf->key = mbedtls_calloc( 1, key_len );
3924 if( tls12_prf->key == NULL )
3925 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3926 tls12_prf->key_len = key_len;
3927 memcpy( tls12_prf->key, key, key_len );
3928
Hanno Becker580fba12018-11-13 20:50:45 +00003929 overflow = ( salt_length + label_length < salt_length ) ||
3930 ( salt_length + label_length + hash_length < hash_length );
3931 if( overflow )
3932 return( PSA_ERROR_INVALID_ARGUMENT );
3933
3934 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3935 if( tls12_prf->Ai_with_seed == NULL )
3936 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3937 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3938
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003939 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3940 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003941 if( label_length != 0 )
3942 {
3943 memcpy( tls12_prf->Ai_with_seed + hash_length,
3944 label, label_length );
3945 }
3946
3947 if( salt_length != 0 )
3948 {
3949 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3950 salt, salt_length );
3951 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003952
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003953 /* The first block gets generated when
3954 * psa_generator_read() is called. */
3955 tls12_prf->block_number = 0;
3956 tls12_prf->offset_in_block = hash_length;
3957
3958 return( PSA_SUCCESS );
3959}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003960
3961/* Set up a TLS-1.2-PSK-to-MS-based generator. */
3962static psa_status_t psa_generator_tls12_psk_to_ms_setup(
3963 psa_tls12_prf_generator_t *tls12_prf,
3964 const unsigned char *psk,
3965 size_t psk_len,
3966 psa_algorithm_t hash_alg,
3967 const uint8_t *salt,
3968 size_t salt_length,
3969 const uint8_t *label,
3970 size_t label_length )
3971{
3972 psa_status_t status;
3973 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
3974
3975 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
3976 return( PSA_ERROR_INVALID_ARGUMENT );
3977
3978 /* Quoting RFC 4279, Section 2:
3979 *
3980 * The premaster secret is formed as follows: if the PSK is N octets
3981 * long, concatenate a uint16 with the value N, N zero octets, a second
3982 * uint16 with the value N, and the PSK itself.
3983 */
3984
3985 pms[0] = ( psk_len >> 8 ) & 0xff;
3986 pms[1] = ( psk_len >> 0 ) & 0xff;
3987 memset( pms + 2, 0, psk_len );
3988 pms[2 + psk_len + 0] = pms[0];
3989 pms[2 + psk_len + 1] = pms[1];
3990 memcpy( pms + 4 + psk_len, psk, psk_len );
3991
3992 status = psa_generator_tls12_prf_setup( tls12_prf,
3993 pms, 4 + 2 * psk_len,
3994 hash_alg,
3995 salt, salt_length,
3996 label, label_length );
3997
Gilles Peskine3f108122018-12-07 18:14:53 +01003998 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00003999 return( status );
4000}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004001#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004002
Gilles Peskine346797d2018-11-16 16:05:06 +01004003/* Note that if this function fails, you must call psa_generator_abort()
4004 * to potentially free embedded data structures and wipe confidential data.
4005 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004006static psa_status_t psa_key_derivation_internal(
4007 psa_crypto_generator_t *generator,
4008 const uint8_t *secret, size_t secret_length,
4009 psa_algorithm_t alg,
4010 const uint8_t *salt, size_t salt_length,
4011 const uint8_t *label, size_t label_length,
4012 size_t capacity )
4013{
4014 psa_status_t status;
4015 size_t max_capacity;
4016
4017 /* Set generator->alg even on failure so that abort knows what to do. */
4018 generator->alg = alg;
4019
Gilles Peskine751d9652018-09-18 12:05:44 +02004020 if( alg == PSA_ALG_SELECT_RAW )
4021 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004022 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004023 if( salt_length != 0 )
4024 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004025 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004026 if( label_length != 0 )
4027 return( PSA_ERROR_INVALID_ARGUMENT );
4028 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4029 if( generator->ctx.buffer.data == NULL )
4030 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4031 memcpy( generator->ctx.buffer.data, secret, secret_length );
4032 generator->ctx.buffer.size = secret_length;
4033 max_capacity = secret_length;
4034 status = PSA_SUCCESS;
4035 }
4036 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004037#if defined(MBEDTLS_MD_C)
4038 if( PSA_ALG_IS_HKDF( alg ) )
4039 {
4040 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4041 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4042 if( hash_size == 0 )
4043 return( PSA_ERROR_NOT_SUPPORTED );
4044 max_capacity = 255 * hash_size;
4045 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4046 secret, secret_length,
4047 hash_alg,
4048 salt, salt_length,
4049 label, label_length );
4050 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004051 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4052 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4053 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004054 {
4055 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4056 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4057
4058 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4059 if( hash_alg != PSA_ALG_SHA_256 &&
4060 hash_alg != PSA_ALG_SHA_384 )
4061 {
4062 return( PSA_ERROR_NOT_SUPPORTED );
4063 }
4064
4065 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004066
4067 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4068 {
4069 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4070 secret, secret_length,
4071 hash_alg, salt, salt_length,
4072 label, label_length );
4073 }
4074 else
4075 {
4076 status = psa_generator_tls12_psk_to_ms_setup(
4077 &generator->ctx.tls12_prf,
4078 secret, secret_length,
4079 hash_alg, salt, salt_length,
4080 label, label_length );
4081 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004082 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004083 else
4084#endif
4085 {
4086 return( PSA_ERROR_NOT_SUPPORTED );
4087 }
4088
4089 if( status != PSA_SUCCESS )
4090 return( status );
4091
4092 if( capacity <= max_capacity )
4093 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004094 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4095 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004096 else
4097 return( PSA_ERROR_INVALID_ARGUMENT );
4098
4099 return( PSA_SUCCESS );
4100}
4101
Gilles Peskineea0fb492018-07-12 17:17:20 +02004102psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004103 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004104 psa_algorithm_t alg,
4105 const uint8_t *salt,
4106 size_t salt_length,
4107 const uint8_t *label,
4108 size_t label_length,
4109 size_t capacity )
4110{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004111 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004112 psa_status_t status;
4113
4114 if( generator->alg != 0 )
4115 return( PSA_ERROR_BAD_STATE );
4116
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004117 /* Make sure that alg is a key derivation algorithm. This prevents
4118 * key selection algorithms, which psa_key_derivation_internal
4119 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004120 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4121 return( PSA_ERROR_INVALID_ARGUMENT );
4122
Gilles Peskinec5487a82018-12-03 18:08:14 +01004123 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004124 if( status != PSA_SUCCESS )
4125 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004126
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004127 if( slot->type != PSA_KEY_TYPE_DERIVE )
4128 return( PSA_ERROR_INVALID_ARGUMENT );
4129
4130 status = psa_key_derivation_internal( generator,
4131 slot->data.raw.data,
4132 slot->data.raw.bytes,
4133 alg,
4134 salt, salt_length,
4135 label, label_length,
4136 capacity );
4137 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004138 psa_generator_abort( generator );
4139 return( status );
4140}
4141
4142
4143
4144/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004145/* Key agreement */
4146/****************************************************************/
4147
Gilles Peskinea05219c2018-11-16 16:02:56 +01004148#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004149static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4150 size_t peer_key_length,
4151 const mbedtls_ecp_keypair *our_key,
4152 uint8_t *shared_secret,
4153 size_t shared_secret_size,
4154 size_t *shared_secret_length )
4155{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004156 mbedtls_ecp_keypair *their_key = NULL;
4157 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004158 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004159 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004160
Jaeden Ameroccdce902019-01-10 11:42:27 +00004161 status = psa_import_ec_public_key(
4162 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4163 peer_key, peer_key_length,
4164 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004165 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004166 goto exit;
4167
Jaeden Amero97271b32019-01-10 19:38:51 +00004168 status = mbedtls_to_psa_error(
4169 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4170 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004171 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00004172 status = mbedtls_to_psa_error(
4173 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4174 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004175 goto exit;
4176
Jaeden Amero97271b32019-01-10 19:38:51 +00004177 status = mbedtls_to_psa_error(
4178 mbedtls_ecdh_calc_secret( &ecdh,
4179 shared_secret_length,
4180 shared_secret, shared_secret_size,
4181 mbedtls_ctr_drbg_random,
4182 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004183
4184exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004185 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00004186 mbedtls_ecp_keypair_free( their_key );
4187 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004188 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004189}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004190#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004191
Gilles Peskine01d718c2018-09-18 12:01:02 +02004192#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4193
Gilles Peskine346797d2018-11-16 16:05:06 +01004194/* Note that if this function fails, you must call psa_generator_abort()
4195 * to potentially free embedded data structures and wipe confidential data.
4196 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004197static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004198 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004199 const uint8_t *peer_key,
4200 size_t peer_key_length,
4201 psa_algorithm_t alg )
4202{
4203 psa_status_t status;
4204 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4205 size_t shared_secret_length = 0;
4206
4207 /* Step 1: run the secret agreement algorithm to generate the shared
4208 * secret. */
4209 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4210 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004211#if defined(MBEDTLS_ECDH_C)
4212 case PSA_ALG_ECDH_BASE:
4213 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4214 return( PSA_ERROR_INVALID_ARGUMENT );
4215 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4216 private_key->data.ecp,
4217 shared_secret,
4218 sizeof( shared_secret ),
4219 &shared_secret_length );
4220 break;
4221#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004222 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004223 (void) private_key;
4224 (void) peer_key;
4225 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004226 return( PSA_ERROR_NOT_SUPPORTED );
4227 }
4228 if( status != PSA_SUCCESS )
4229 goto exit;
4230
4231 /* Step 2: set up the key derivation to generate key material from
4232 * the shared secret. */
4233 status = psa_key_derivation_internal( generator,
4234 shared_secret, shared_secret_length,
4235 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4236 NULL, 0, NULL, 0,
4237 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4238exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004239 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004240 return( status );
4241}
4242
4243psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004244 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004245 const uint8_t *peer_key,
4246 size_t peer_key_length,
4247 psa_algorithm_t alg )
4248{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004249 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004250 psa_status_t status;
4251 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4252 return( PSA_ERROR_INVALID_ARGUMENT );
4253 status = psa_get_key_from_slot( private_key, &slot,
4254 PSA_KEY_USAGE_DERIVE, alg );
4255 if( status != PSA_SUCCESS )
4256 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004257 status = psa_key_agreement_internal( generator,
4258 slot,
4259 peer_key, peer_key_length,
4260 alg );
4261 if( status != PSA_SUCCESS )
4262 psa_generator_abort( generator );
4263 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004264}
4265
4266
4267
4268/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004269/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004270/****************************************************************/
4271
4272psa_status_t psa_generate_random( uint8_t *output,
4273 size_t output_size )
4274{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004275 int ret;
4276 GUARD_MODULE_INITIALIZED;
4277
4278 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004279 return( mbedtls_to_psa_error( ret ) );
4280}
4281
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004282#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004283
4284/* Support function for error conversion between psa_its error codes to psa crypto */
4285static psa_status_t its_to_psa_error( psa_its_status_t ret )
4286{
4287 switch( ret )
4288 {
4289 case PSA_ITS_SUCCESS:
4290 return( PSA_SUCCESS );
4291
Oren Cohen23a67842019-01-24 14:32:11 +02004292 case PSA_ITS_ERROR_UID_NOT_FOUND:
avolinski13beb102018-11-20 16:51:49 +02004293 return( PSA_ERROR_EMPTY_SLOT );
4294
4295 case PSA_ITS_ERROR_STORAGE_FAILURE:
4296 return( PSA_ERROR_STORAGE_FAILURE );
4297
4298 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4299 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4300
Jaeden Amero58600552018-11-30 12:04:38 +00004301 case PSA_ITS_ERROR_OFFSET_INVALID:
avolinski13beb102018-11-20 16:51:49 +02004302 case PSA_ITS_ERROR_INCORRECT_SIZE:
Oren Cohen23a67842019-01-24 14:32:11 +02004303 case PSA_ITS_ERROR_INVALID_ARGUMENTS:
avolinski13beb102018-11-20 16:51:49 +02004304 return( PSA_ERROR_INVALID_ARGUMENT );
4305
4306 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4307 return( PSA_ERROR_NOT_SUPPORTED );
4308
4309 case PSA_ITS_ERROR_WRITE_ONCE:
4310 return( PSA_ERROR_OCCUPIED_SLOT );
4311
4312 default:
4313 return( PSA_ERROR_UNKNOWN_ERROR );
4314 }
4315}
4316
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004317psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4318 size_t seed_size )
4319{
4320 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004321 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004322 struct psa_its_info_t p_info;
4323 if( global_data.initialized )
4324 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004325
4326 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4327 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4328 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4329 return( PSA_ERROR_INVALID_ARGUMENT );
4330
avolinski0d2c2662018-11-21 17:31:07 +02004331 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004332 status = its_to_psa_error( its_status );
4333
Oren Cohen23a67842019-01-24 14:32:11 +02004334 if( PSA_ITS_ERROR_UID_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004335 {
avolinski0d2c2662018-11-21 17:31:07 +02004336 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004337 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004338 }
avolinski13beb102018-11-20 16:51:49 +02004339 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004340 {
4341 /* You should not be here. Seed needs to be injected only once */
4342 status = PSA_ERROR_NOT_PERMITTED;
4343 }
4344 return( status );
4345}
4346#endif
4347
Gilles Peskinec5487a82018-12-03 18:08:14 +01004348psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004349 psa_key_type_t type,
4350 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004351 const void *extra,
4352 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004353{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004354 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004355 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004356
Gilles Peskine53d991e2018-07-12 01:14:59 +02004357 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004358 return( PSA_ERROR_INVALID_ARGUMENT );
4359
Gilles Peskinec5487a82018-12-03 18:08:14 +01004360 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004361 if( status != PSA_SUCCESS )
4362 return( status );
4363
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004364 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004365 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004366 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004367 if( status != PSA_SUCCESS )
4368 return( status );
4369 status = psa_generate_random( slot->data.raw.data,
4370 slot->data.raw.bytes );
4371 if( status != PSA_SUCCESS )
4372 {
4373 mbedtls_free( slot->data.raw.data );
4374 return( status );
4375 }
4376#if defined(MBEDTLS_DES_C)
4377 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004378 psa_des_set_key_parity( slot->data.raw.data,
4379 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004380#endif /* MBEDTLS_DES_C */
4381 }
4382 else
4383
4384#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4385 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4386 {
4387 mbedtls_rsa_context *rsa;
4388 int ret;
4389 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004390 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4391 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004392 /* Accept only byte-aligned keys, for the same reasons as
4393 * in psa_import_rsa_key(). */
4394 if( bits % 8 != 0 )
4395 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004396 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004397 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004398 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004399 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004400 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004401#if INT_MAX < 0xffffffff
4402 /* Check that the uint32_t value passed by the caller fits
4403 * in the range supported by this implementation. */
4404 if( p->e > INT_MAX )
4405 return( PSA_ERROR_NOT_SUPPORTED );
4406#endif
4407 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004408 }
4409 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4410 if( rsa == NULL )
4411 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4412 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4413 ret = mbedtls_rsa_gen_key( rsa,
4414 mbedtls_ctr_drbg_random,
4415 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004416 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004417 exponent );
4418 if( ret != 0 )
4419 {
4420 mbedtls_rsa_free( rsa );
4421 mbedtls_free( rsa );
4422 return( mbedtls_to_psa_error( ret ) );
4423 }
4424 slot->data.rsa = rsa;
4425 }
4426 else
4427#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4428
4429#if defined(MBEDTLS_ECP_C)
4430 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4431 {
4432 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4433 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4434 const mbedtls_ecp_curve_info *curve_info =
4435 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4436 mbedtls_ecp_keypair *ecp;
4437 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004438 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004439 return( PSA_ERROR_NOT_SUPPORTED );
4440 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4441 return( PSA_ERROR_NOT_SUPPORTED );
4442 if( curve_info->bit_size != bits )
4443 return( PSA_ERROR_INVALID_ARGUMENT );
4444 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4445 if( ecp == NULL )
4446 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4447 mbedtls_ecp_keypair_init( ecp );
4448 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4449 mbedtls_ctr_drbg_random,
4450 &global_data.ctr_drbg );
4451 if( ret != 0 )
4452 {
4453 mbedtls_ecp_keypair_free( ecp );
4454 mbedtls_free( ecp );
4455 return( mbedtls_to_psa_error( ret ) );
4456 }
4457 slot->data.ecp = ecp;
4458 }
4459 else
4460#endif /* MBEDTLS_ECP_C */
4461
4462 return( PSA_ERROR_NOT_SUPPORTED );
4463
4464 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004465
4466#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4467 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4468 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004469 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004470 }
4471#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4472
4473 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004474}
4475
4476
4477/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004478/* Module setup */
4479/****************************************************************/
4480
Gilles Peskine5e769522018-11-20 21:59:56 +01004481psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4482 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4483 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4484{
4485 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4486 return( PSA_ERROR_BAD_STATE );
4487 global_data.entropy_init = entropy_init;
4488 global_data.entropy_free = entropy_free;
4489 return( PSA_SUCCESS );
4490}
4491
Gilles Peskinee59236f2018-01-27 23:32:46 +01004492void mbedtls_psa_crypto_free( void )
4493{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004494 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004495 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4496 {
4497 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004498 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004499 }
4500 /* Wipe all remaining data, including configuration.
4501 * In particular, this sets all state indicator to the value
4502 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004503 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004504}
4505
4506psa_status_t psa_crypto_init( void )
4507{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004508 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004509 const unsigned char drbg_seed[] = "PSA";
4510
Gilles Peskinec6b69072018-11-20 21:42:52 +01004511 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004512 if( global_data.initialized != 0 )
4513 return( PSA_SUCCESS );
4514
Gilles Peskine5e769522018-11-20 21:59:56 +01004515 /* Set default configuration if
4516 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4517 if( global_data.entropy_init == NULL )
4518 global_data.entropy_init = mbedtls_entropy_init;
4519 if( global_data.entropy_free == NULL )
4520 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004521
Gilles Peskinec6b69072018-11-20 21:42:52 +01004522 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004523 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004524 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004525 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004526 status = mbedtls_to_psa_error(
4527 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4528 mbedtls_entropy_func,
4529 &global_data.entropy,
4530 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4531 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004532 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004533 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004534
Gilles Peskine66fb1262018-12-10 16:29:04 +01004535 status = psa_initialize_key_slots( );
4536 if( status != PSA_SUCCESS )
4537 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004538
4539 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004540 global_data.initialized = 1;
4541
Gilles Peskinee59236f2018-01-27 23:32:46 +01004542exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004543 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004544 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004545 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004546}
4547
4548#endif /* MBEDTLS_PSA_CRYPTO_C */