blob: 9bfe8d28c7628317f3926a70e10622b7fd822625 [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
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000175 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 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
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000181 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100182 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 Peskinef603c712019-01-19 13:40:11 +0100751/** Calculate the intersection of two algorithm usage policies.
752 *
753 * Return 0 (which allows no operation) on incompatibility.
754 */
755static psa_algorithm_t psa_key_policy_algorithm_intersection(
756 psa_algorithm_t alg1,
757 psa_algorithm_t alg2 )
758{
759 /* Common case: the policy only allows alg. */
760 if( alg1 == alg2 )
761 return( alg1 );
762 /* If the policies are from the same hash-and-sign family, check
763 * if one is a wildcard. If so the other has the specific algorithm. */
764 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
765 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
766 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
767 {
768 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
769 return( alg2 );
770 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
771 return( alg1 );
772 }
773 /* If the policies are incompatible, allow nothing. */
774 return( 0 );
775}
776
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100777/** Test whether a policy permits an algorithm.
778 *
779 * The caller must test usage flags separately.
780 */
781static int psa_key_policy_permits( const psa_key_policy_t *policy,
782 psa_algorithm_t alg )
783{
784 /* Common case: the policy only allows alg. */
785 if( alg == policy->alg )
786 return( 1 );
787 /* If policy->alg is a hash-and-sign with a wildcard for the hash,
788 * and alg is the same hash-and-sign family with any hash,
789 * then alg is compliant with policy->alg. */
790 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) &&
791 PSA_ALG_SIGN_GET_HASH( policy->alg ) == PSA_ALG_ANY_HASH )
792 {
793 return( ( policy->alg & ~PSA_ALG_HASH_MASK ) ==
794 ( alg & ~PSA_ALG_HASH_MASK ) );
795 }
796 /* If it isn't permitted, it's forbidden. */
797 return( 0 );
798}
799
Gilles Peskinef603c712019-01-19 13:40:11 +0100800/** Restrict a key policy based on a constraint.
801 *
802 * \param[in,out] policy The policy to restrict.
803 * \param[in] constraint The policy constraint to apply.
804 *
805 * \retval #PSA_SUCCESS
806 * \c *policy contains the intersection of the original value of
807 * \c *policy and \c *constraint.
808 * \retval #PSA_ERROR_INVALID_ARGUMENT
809 * \c *policy and \c *constraint are incompatible.
810 * \c *policy is unchanged.
811 */
812static psa_status_t psa_restrict_key_policy(
813 psa_key_policy_t *policy,
814 const psa_key_policy_t *constraint )
815{
816 psa_algorithm_t intersection_alg =
817 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
818 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
819 return( PSA_ERROR_INVALID_ARGUMENT );
820 policy->usage &= constraint->usage;
821 policy->alg = intersection_alg;
822 return( PSA_SUCCESS );
823}
824
Darryl Green06fd18d2018-07-16 11:21:11 +0100825/** Retrieve a slot which must contain a key. The key must have allow all the
826 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
827 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100828static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100829 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100830 psa_key_usage_t usage,
831 psa_algorithm_t alg )
832{
833 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100834 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100835
836 *p_slot = NULL;
837
Gilles Peskinec5487a82018-12-03 18:08:14 +0100838 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100839 if( status != PSA_SUCCESS )
840 return( status );
841 if( slot->type == PSA_KEY_TYPE_NONE )
842 return( PSA_ERROR_EMPTY_SLOT );
843
844 /* Enforce that usage policy for the key slot contains all the flags
845 * required by the usage parameter. There is one exception: public
846 * keys can always be exported, so we treat public key objects as
847 * if they had the export flag. */
848 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
849 usage &= ~PSA_KEY_USAGE_EXPORT;
850 if( ( slot->policy.usage & usage ) != usage )
851 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100852
853 /* Enforce that the usage policy permits the requested algortihm. */
854 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100855 return( PSA_ERROR_NOT_PERMITTED );
856
857 *p_slot = slot;
858 return( PSA_SUCCESS );
859}
Darryl Green940d72c2018-07-13 13:18:51 +0100860
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100861/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100862static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000863{
864 if( slot->type == PSA_KEY_TYPE_NONE )
865 {
866 /* No key material to clean. */
867 }
868 else if( key_type_is_raw_bytes( slot->type ) )
869 {
870 mbedtls_free( slot->data.raw.data );
871 }
872 else
873#if defined(MBEDTLS_RSA_C)
874 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
875 {
876 mbedtls_rsa_free( slot->data.rsa );
877 mbedtls_free( slot->data.rsa );
878 }
879 else
880#endif /* defined(MBEDTLS_RSA_C) */
881#if defined(MBEDTLS_ECP_C)
882 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
883 {
884 mbedtls_ecp_keypair_free( slot->data.ecp );
885 mbedtls_free( slot->data.ecp );
886 }
887 else
888#endif /* defined(MBEDTLS_ECP_C) */
889 {
890 /* Shouldn't happen: the key type is not any type that we
891 * put in. */
892 return( PSA_ERROR_TAMPERING_DETECTED );
893 }
894
895 return( PSA_SUCCESS );
896}
897
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100898/** Completely wipe a slot in memory, including its policy.
899 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100900psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100901{
902 psa_status_t status = psa_remove_key_data_from_memory( slot );
903 /* At this point, key material and other type-specific content has
904 * been wiped. Clear remaining metadata. We can call memset and not
905 * zeroize because the metadata is not particularly sensitive. */
906 memset( slot, 0, sizeof( *slot ) );
907 return( status );
908}
909
Gilles Peskinec5487a82018-12-03 18:08:14 +0100910psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100911 psa_key_type_t type,
912 const uint8_t *data,
913 size_t data_length )
914{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100915 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100916 psa_status_t status;
917
Gilles Peskinec5487a82018-12-03 18:08:14 +0100918 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100919 if( status != PSA_SUCCESS )
920 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100921
922 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100923
924 status = psa_import_key_into_slot( slot, data, data_length );
925 if( status != PSA_SUCCESS )
926 {
927 slot->type = PSA_KEY_TYPE_NONE;
928 return( status );
929 }
930
Darryl Greend49a4992018-06-18 17:27:26 +0100931#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
932 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
933 {
934 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100935 status = psa_save_persistent_key( slot->persistent_storage_id,
936 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100937 data_length );
938 if( status != PSA_SUCCESS )
939 {
940 (void) psa_remove_key_data_from_memory( slot );
941 slot->type = PSA_KEY_TYPE_NONE;
942 }
943 }
944#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
945
946 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100947}
948
Gilles Peskinec5487a82018-12-03 18:08:14 +0100949psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100950{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100951 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100952 psa_status_t status = PSA_SUCCESS;
953 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100954
Gilles Peskinec5487a82018-12-03 18:08:14 +0100955 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100956 if( status != PSA_SUCCESS )
957 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100958#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
959 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
960 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100961 storage_status =
962 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100963 }
964#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100965 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100966 if( status != PSA_SUCCESS )
967 return( status );
968 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100969}
970
Gilles Peskineb870b182018-07-06 16:02:09 +0200971/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100972static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200973{
974 if( key_type_is_raw_bytes( slot->type ) )
975 return( slot->data.raw.bytes * 8 );
976#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200977 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100978 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200979#endif /* defined(MBEDTLS_RSA_C) */
980#if defined(MBEDTLS_ECP_C)
981 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
982 return( slot->data.ecp->grp.pbits );
983#endif /* defined(MBEDTLS_ECP_C) */
984 /* Shouldn't happen except on an empty slot. */
985 return( 0 );
986}
987
Gilles Peskinec5487a82018-12-03 18:08:14 +0100988psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200989 psa_key_type_t *type,
990 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100991{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100992 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200993 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100994
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200996 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100997 if( bits != NULL )
998 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100999 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001000 if( status != PSA_SUCCESS )
1001 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001002
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001003 if( slot->type == PSA_KEY_TYPE_NONE )
1004 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001005 if( type != NULL )
1006 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001007 if( bits != NULL )
1008 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001009 return( PSA_SUCCESS );
1010}
1011
Jaeden Ameroccdce902019-01-10 11:42:27 +00001012#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001013static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1014 unsigned char *buf, size_t size )
1015{
1016 int ret;
1017 unsigned char *c;
1018 size_t len = 0;
1019
1020 c = buf + size;
1021
1022 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1023
1024 return( (int) len );
1025}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001026#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001027
Gilles Peskinef603c712019-01-19 13:40:11 +01001028static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1029 uint8_t *data,
1030 size_t data_size,
1031 size_t *data_length,
1032 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001033{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001034 *data_length = 0;
1035
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001036 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001037 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001038
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001039 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001040 {
1041 if( slot->data.raw.bytes > data_size )
1042 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001043 if( data_size != 0 )
1044 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001045 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001046 memset( data + slot->data.raw.bytes, 0,
1047 data_size - slot->data.raw.bytes );
1048 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001049 *data_length = slot->data.raw.bytes;
1050 return( PSA_SUCCESS );
1051 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001052#if defined(MBEDTLS_ECP_C)
1053 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1054 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001055 psa_status_t status;
1056
Gilles Peskine188c71e2018-10-29 19:26:02 +01001057 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1058 if( bytes > data_size )
1059 return( PSA_ERROR_BUFFER_TOO_SMALL );
1060 status = mbedtls_to_psa_error(
1061 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1062 if( status != PSA_SUCCESS )
1063 return( status );
1064 memset( data + bytes, 0, data_size - bytes );
1065 *data_length = bytes;
1066 return( PSA_SUCCESS );
1067 }
1068#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001069 else
Moran Peker17e36e12018-05-02 12:55:20 +03001070 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001071#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001072 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001073 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001074 {
Moran Pekera998bc62018-04-16 18:16:20 +03001075 mbedtls_pk_context pk;
1076 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001077 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001078 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001079#if defined(MBEDTLS_RSA_C)
1080 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001081 pk.pk_info = &mbedtls_rsa_info;
1082 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001083#else
1084 return( PSA_ERROR_NOT_SUPPORTED );
1085#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001086 }
1087 else
1088 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001089#if defined(MBEDTLS_ECP_C)
1090 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001091 pk.pk_info = &mbedtls_eckey_info;
1092 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001093#else
1094 return( PSA_ERROR_NOT_SUPPORTED );
1095#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001096 }
Moran Pekerd7326592018-05-29 16:56:39 +03001097 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001098 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001099 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001100 }
Moran Peker17e36e12018-05-02 12:55:20 +03001101 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001102 {
Moran Peker17e36e12018-05-02 12:55:20 +03001103 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001104 }
Moran Peker60364322018-04-29 11:34:58 +03001105 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001106 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001107 /* If data_size is 0 then data may be NULL and then the
1108 * call to memset would have undefined behavior. */
1109 if( data_size != 0 )
1110 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001111 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001112 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001113 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1114 * Move the data to the beginning and erase remaining data
1115 * at the original location. */
1116 if( 2 * (size_t) ret <= data_size )
1117 {
1118 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001119 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001120 }
1121 else if( (size_t) ret < data_size )
1122 {
1123 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001124 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001125 }
Moran Pekera998bc62018-04-16 18:16:20 +03001126 *data_length = ret;
1127 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001128 }
1129 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001130#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001131 {
1132 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001133 it is valid for a special-purpose implementation to omit
1134 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001135 return( PSA_ERROR_NOT_SUPPORTED );
1136 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001137 }
1138}
1139
Gilles Peskinec5487a82018-12-03 18:08:14 +01001140psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001141 uint8_t *data,
1142 size_t data_size,
1143 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001144{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001145 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001146 psa_status_t status;
1147
1148 /* Set the key to empty now, so that even when there are errors, we always
1149 * set data_length to a value between 0 and data_size. On error, setting
1150 * the key to empty is a good choice because an empty key representation is
1151 * unlikely to be accepted anywhere. */
1152 *data_length = 0;
1153
1154 /* Export requires the EXPORT flag. There is an exception for public keys,
1155 * which don't require any flag, but psa_get_key_from_slot takes
1156 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001157 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001158 if( status != PSA_SUCCESS )
1159 return( status );
1160 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001161 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001162}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001163
Gilles Peskinec5487a82018-12-03 18:08:14 +01001164psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001165 uint8_t *data,
1166 size_t data_size,
1167 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001168{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001169 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001170 psa_status_t status;
1171
1172 /* Set the key to empty now, so that even when there are errors, we always
1173 * set data_length to a value between 0 and data_size. On error, setting
1174 * the key to empty is a good choice because an empty key representation is
1175 * unlikely to be accepted anywhere. */
1176 *data_length = 0;
1177
1178 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001179 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001180 if( status != PSA_SUCCESS )
1181 return( status );
1182 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001183 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001184}
1185
Darryl Green0c6575a2018-11-07 16:05:30 +00001186#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001187static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001188 size_t bits )
1189{
1190 psa_status_t status;
1191 uint8_t *data;
1192 size_t key_length;
1193 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1194 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001195 if( data == NULL )
1196 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001197 /* Get key data in export format */
1198 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1199 if( status != PSA_SUCCESS )
1200 {
1201 slot->type = PSA_KEY_TYPE_NONE;
1202 goto exit;
1203 }
1204 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001205 status = psa_save_persistent_key( slot->persistent_storage_id,
1206 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001207 data, key_length );
1208 if( status != PSA_SUCCESS )
1209 {
1210 slot->type = PSA_KEY_TYPE_NONE;
1211 }
1212exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001213 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001214 mbedtls_free( data );
1215 return( status );
1216}
1217#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1218
Gilles Peskinef603c712019-01-19 13:40:11 +01001219static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
1220 psa_key_handle_t target )
1221{
1222 psa_status_t status;
1223 uint8_t *buffer = NULL;
1224 size_t buffer_size = 0;
1225 size_t length;
1226
1227 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
1228 psa_get_key_bits( source ) );
1229 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001230 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001231 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001232 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1233 if( status != PSA_SUCCESS )
1234 goto exit;
1235 status = psa_import_key( target, source->type, buffer, length );
1236
1237exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001238 if( buffer_size != 0 )
1239 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001240 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001241 return( status );
1242}
1243
1244psa_status_t psa_copy_key(psa_key_handle_t source_handle,
1245 psa_key_handle_t target_handle,
1246 const psa_key_policy_t *constraint)
1247{
1248 psa_key_slot_t *source_slot = NULL;
1249 psa_key_slot_t *target_slot = NULL;
1250 psa_key_policy_t new_policy;
1251 psa_status_t status;
1252 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1253 if( status != PSA_SUCCESS )
1254 return( status );
1255 status = psa_get_empty_key_slot( target_handle, &target_slot );
1256 if( status != PSA_SUCCESS )
1257 return( status );
1258
1259 new_policy = target_slot->policy;
1260 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1261 if( status != PSA_SUCCESS )
1262 return( status );
1263 if( constraint != NULL )
1264 {
1265 status = psa_restrict_key_policy( &new_policy, constraint );
1266 if( status != PSA_SUCCESS )
1267 return( status );
1268 }
1269
1270 status = psa_copy_key_material( source_slot, target_handle );
1271 if( status != PSA_SUCCESS )
1272 return( status );
1273
1274 target_slot->policy = new_policy;
1275 return( PSA_SUCCESS );
1276}
1277
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001278
1279
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001280/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001281/* Message digests */
1282/****************************************************************/
1283
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001284static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001285{
1286 switch( alg )
1287 {
1288#if defined(MBEDTLS_MD2_C)
1289 case PSA_ALG_MD2:
1290 return( &mbedtls_md2_info );
1291#endif
1292#if defined(MBEDTLS_MD4_C)
1293 case PSA_ALG_MD4:
1294 return( &mbedtls_md4_info );
1295#endif
1296#if defined(MBEDTLS_MD5_C)
1297 case PSA_ALG_MD5:
1298 return( &mbedtls_md5_info );
1299#endif
1300#if defined(MBEDTLS_RIPEMD160_C)
1301 case PSA_ALG_RIPEMD160:
1302 return( &mbedtls_ripemd160_info );
1303#endif
1304#if defined(MBEDTLS_SHA1_C)
1305 case PSA_ALG_SHA_1:
1306 return( &mbedtls_sha1_info );
1307#endif
1308#if defined(MBEDTLS_SHA256_C)
1309 case PSA_ALG_SHA_224:
1310 return( &mbedtls_sha224_info );
1311 case PSA_ALG_SHA_256:
1312 return( &mbedtls_sha256_info );
1313#endif
1314#if defined(MBEDTLS_SHA512_C)
1315 case PSA_ALG_SHA_384:
1316 return( &mbedtls_sha384_info );
1317 case PSA_ALG_SHA_512:
1318 return( &mbedtls_sha512_info );
1319#endif
1320 default:
1321 return( NULL );
1322 }
1323}
1324
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001325psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1326{
1327 switch( operation->alg )
1328 {
Gilles Peskine81736312018-06-26 15:04:31 +02001329 case 0:
1330 /* The object has (apparently) been initialized but it is not
1331 * in use. It's ok to call abort on such an object, and there's
1332 * nothing to do. */
1333 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001334#if defined(MBEDTLS_MD2_C)
1335 case PSA_ALG_MD2:
1336 mbedtls_md2_free( &operation->ctx.md2 );
1337 break;
1338#endif
1339#if defined(MBEDTLS_MD4_C)
1340 case PSA_ALG_MD4:
1341 mbedtls_md4_free( &operation->ctx.md4 );
1342 break;
1343#endif
1344#if defined(MBEDTLS_MD5_C)
1345 case PSA_ALG_MD5:
1346 mbedtls_md5_free( &operation->ctx.md5 );
1347 break;
1348#endif
1349#if defined(MBEDTLS_RIPEMD160_C)
1350 case PSA_ALG_RIPEMD160:
1351 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1352 break;
1353#endif
1354#if defined(MBEDTLS_SHA1_C)
1355 case PSA_ALG_SHA_1:
1356 mbedtls_sha1_free( &operation->ctx.sha1 );
1357 break;
1358#endif
1359#if defined(MBEDTLS_SHA256_C)
1360 case PSA_ALG_SHA_224:
1361 case PSA_ALG_SHA_256:
1362 mbedtls_sha256_free( &operation->ctx.sha256 );
1363 break;
1364#endif
1365#if defined(MBEDTLS_SHA512_C)
1366 case PSA_ALG_SHA_384:
1367 case PSA_ALG_SHA_512:
1368 mbedtls_sha512_free( &operation->ctx.sha512 );
1369 break;
1370#endif
1371 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001372 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001373 }
1374 operation->alg = 0;
1375 return( PSA_SUCCESS );
1376}
1377
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001378psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001379 psa_algorithm_t alg )
1380{
1381 int ret;
1382 operation->alg = 0;
1383 switch( alg )
1384 {
1385#if defined(MBEDTLS_MD2_C)
1386 case PSA_ALG_MD2:
1387 mbedtls_md2_init( &operation->ctx.md2 );
1388 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1389 break;
1390#endif
1391#if defined(MBEDTLS_MD4_C)
1392 case PSA_ALG_MD4:
1393 mbedtls_md4_init( &operation->ctx.md4 );
1394 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1395 break;
1396#endif
1397#if defined(MBEDTLS_MD5_C)
1398 case PSA_ALG_MD5:
1399 mbedtls_md5_init( &operation->ctx.md5 );
1400 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1401 break;
1402#endif
1403#if defined(MBEDTLS_RIPEMD160_C)
1404 case PSA_ALG_RIPEMD160:
1405 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1406 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1407 break;
1408#endif
1409#if defined(MBEDTLS_SHA1_C)
1410 case PSA_ALG_SHA_1:
1411 mbedtls_sha1_init( &operation->ctx.sha1 );
1412 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1413 break;
1414#endif
1415#if defined(MBEDTLS_SHA256_C)
1416 case PSA_ALG_SHA_224:
1417 mbedtls_sha256_init( &operation->ctx.sha256 );
1418 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1419 break;
1420 case PSA_ALG_SHA_256:
1421 mbedtls_sha256_init( &operation->ctx.sha256 );
1422 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1423 break;
1424#endif
1425#if defined(MBEDTLS_SHA512_C)
1426 case PSA_ALG_SHA_384:
1427 mbedtls_sha512_init( &operation->ctx.sha512 );
1428 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1429 break;
1430 case PSA_ALG_SHA_512:
1431 mbedtls_sha512_init( &operation->ctx.sha512 );
1432 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1433 break;
1434#endif
1435 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001436 return( PSA_ALG_IS_HASH( alg ) ?
1437 PSA_ERROR_NOT_SUPPORTED :
1438 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001439 }
1440 if( ret == 0 )
1441 operation->alg = alg;
1442 else
1443 psa_hash_abort( operation );
1444 return( mbedtls_to_psa_error( ret ) );
1445}
1446
1447psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1448 const uint8_t *input,
1449 size_t input_length )
1450{
1451 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001452
1453 /* Don't require hash implementations to behave correctly on a
1454 * zero-length input, which may have an invalid pointer. */
1455 if( input_length == 0 )
1456 return( PSA_SUCCESS );
1457
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001458 switch( operation->alg )
1459 {
1460#if defined(MBEDTLS_MD2_C)
1461 case PSA_ALG_MD2:
1462 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1463 input, input_length );
1464 break;
1465#endif
1466#if defined(MBEDTLS_MD4_C)
1467 case PSA_ALG_MD4:
1468 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1469 input, input_length );
1470 break;
1471#endif
1472#if defined(MBEDTLS_MD5_C)
1473 case PSA_ALG_MD5:
1474 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1475 input, input_length );
1476 break;
1477#endif
1478#if defined(MBEDTLS_RIPEMD160_C)
1479 case PSA_ALG_RIPEMD160:
1480 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1481 input, input_length );
1482 break;
1483#endif
1484#if defined(MBEDTLS_SHA1_C)
1485 case PSA_ALG_SHA_1:
1486 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1487 input, input_length );
1488 break;
1489#endif
1490#if defined(MBEDTLS_SHA256_C)
1491 case PSA_ALG_SHA_224:
1492 case PSA_ALG_SHA_256:
1493 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1494 input, input_length );
1495 break;
1496#endif
1497#if defined(MBEDTLS_SHA512_C)
1498 case PSA_ALG_SHA_384:
1499 case PSA_ALG_SHA_512:
1500 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1501 input, input_length );
1502 break;
1503#endif
1504 default:
1505 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1506 break;
1507 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001508
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001509 if( ret != 0 )
1510 psa_hash_abort( operation );
1511 return( mbedtls_to_psa_error( ret ) );
1512}
1513
1514psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1515 uint8_t *hash,
1516 size_t hash_size,
1517 size_t *hash_length )
1518{
itayzafrir40835d42018-08-02 13:14:17 +03001519 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001520 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001521 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001522
1523 /* Fill the output buffer with something that isn't a valid hash
1524 * (barring an attack on the hash and deliberately-crafted input),
1525 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001526 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001527 /* If hash_size is 0 then hash may be NULL and then the
1528 * call to memset would have undefined behavior. */
1529 if( hash_size != 0 )
1530 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001531
1532 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001533 {
1534 status = PSA_ERROR_BUFFER_TOO_SMALL;
1535 goto exit;
1536 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001537
1538 switch( operation->alg )
1539 {
1540#if defined(MBEDTLS_MD2_C)
1541 case PSA_ALG_MD2:
1542 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1543 break;
1544#endif
1545#if defined(MBEDTLS_MD4_C)
1546 case PSA_ALG_MD4:
1547 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1548 break;
1549#endif
1550#if defined(MBEDTLS_MD5_C)
1551 case PSA_ALG_MD5:
1552 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1553 break;
1554#endif
1555#if defined(MBEDTLS_RIPEMD160_C)
1556 case PSA_ALG_RIPEMD160:
1557 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1558 break;
1559#endif
1560#if defined(MBEDTLS_SHA1_C)
1561 case PSA_ALG_SHA_1:
1562 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1563 break;
1564#endif
1565#if defined(MBEDTLS_SHA256_C)
1566 case PSA_ALG_SHA_224:
1567 case PSA_ALG_SHA_256:
1568 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1569 break;
1570#endif
1571#if defined(MBEDTLS_SHA512_C)
1572 case PSA_ALG_SHA_384:
1573 case PSA_ALG_SHA_512:
1574 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1575 break;
1576#endif
1577 default:
1578 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1579 break;
1580 }
itayzafrir40835d42018-08-02 13:14:17 +03001581 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001582
itayzafrir40835d42018-08-02 13:14:17 +03001583exit:
1584 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001585 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001586 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001587 return( psa_hash_abort( operation ) );
1588 }
1589 else
1590 {
1591 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001592 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001593 }
1594}
1595
Gilles Peskine2d277862018-06-18 15:41:12 +02001596psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1597 const uint8_t *hash,
1598 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001599{
1600 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1601 size_t actual_hash_length;
1602 psa_status_t status = psa_hash_finish( operation,
1603 actual_hash, sizeof( actual_hash ),
1604 &actual_hash_length );
1605 if( status != PSA_SUCCESS )
1606 return( status );
1607 if( actual_hash_length != hash_length )
1608 return( PSA_ERROR_INVALID_SIGNATURE );
1609 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1610 return( PSA_ERROR_INVALID_SIGNATURE );
1611 return( PSA_SUCCESS );
1612}
1613
Gilles Peskineeb35d782019-01-22 17:56:16 +01001614psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1615 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001616{
1617 if( target_operation->alg != 0 )
1618 return( PSA_ERROR_BAD_STATE );
1619
1620 switch( source_operation->alg )
1621 {
1622 case 0:
1623 return( PSA_ERROR_BAD_STATE );
1624#if defined(MBEDTLS_MD2_C)
1625 case PSA_ALG_MD2:
1626 mbedtls_md2_clone( &target_operation->ctx.md2,
1627 &source_operation->ctx.md2 );
1628 break;
1629#endif
1630#if defined(MBEDTLS_MD4_C)
1631 case PSA_ALG_MD4:
1632 mbedtls_md4_clone( &target_operation->ctx.md4,
1633 &source_operation->ctx.md4 );
1634 break;
1635#endif
1636#if defined(MBEDTLS_MD5_C)
1637 case PSA_ALG_MD5:
1638 mbedtls_md5_clone( &target_operation->ctx.md5,
1639 &source_operation->ctx.md5 );
1640 break;
1641#endif
1642#if defined(MBEDTLS_RIPEMD160_C)
1643 case PSA_ALG_RIPEMD160:
1644 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1645 &source_operation->ctx.ripemd160 );
1646 break;
1647#endif
1648#if defined(MBEDTLS_SHA1_C)
1649 case PSA_ALG_SHA_1:
1650 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1651 &source_operation->ctx.sha1 );
1652 break;
1653#endif
1654#if defined(MBEDTLS_SHA256_C)
1655 case PSA_ALG_SHA_224:
1656 case PSA_ALG_SHA_256:
1657 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1658 &source_operation->ctx.sha256 );
1659 break;
1660#endif
1661#if defined(MBEDTLS_SHA512_C)
1662 case PSA_ALG_SHA_384:
1663 case PSA_ALG_SHA_512:
1664 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1665 &source_operation->ctx.sha512 );
1666 break;
1667#endif
1668 default:
1669 return( PSA_ERROR_NOT_SUPPORTED );
1670 }
1671
1672 target_operation->alg = source_operation->alg;
1673 return( PSA_SUCCESS );
1674}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001675
1676
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001677/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001678/* MAC */
1679/****************************************************************/
1680
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001681static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001682 psa_algorithm_t alg,
1683 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001684 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001685 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001686{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001687 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001688 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001689
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001690 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001691 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001692
Gilles Peskine8c9def32018-02-08 10:02:12 +01001693 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1694 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001695 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001696 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001697 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001698 mode = MBEDTLS_MODE_STREAM;
1699 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001700 case PSA_ALG_CTR:
1701 mode = MBEDTLS_MODE_CTR;
1702 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001703 case PSA_ALG_CFB:
1704 mode = MBEDTLS_MODE_CFB;
1705 break;
1706 case PSA_ALG_OFB:
1707 mode = MBEDTLS_MODE_OFB;
1708 break;
1709 case PSA_ALG_CBC_NO_PADDING:
1710 mode = MBEDTLS_MODE_CBC;
1711 break;
1712 case PSA_ALG_CBC_PKCS7:
1713 mode = MBEDTLS_MODE_CBC;
1714 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001715 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001716 mode = MBEDTLS_MODE_CCM;
1717 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001718 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001719 mode = MBEDTLS_MODE_GCM;
1720 break;
1721 default:
1722 return( NULL );
1723 }
1724 }
1725 else if( alg == PSA_ALG_CMAC )
1726 mode = MBEDTLS_MODE_ECB;
1727 else if( alg == PSA_ALG_GMAC )
1728 mode = MBEDTLS_MODE_GCM;
1729 else
1730 return( NULL );
1731
1732 switch( key_type )
1733 {
1734 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001735 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001736 break;
1737 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001738 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1739 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001740 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001741 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001742 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001743 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001744 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1745 * but two-key Triple-DES is functionally three-key Triple-DES
1746 * with K1=K3, so that's how we present it to mbedtls. */
1747 if( key_bits == 128 )
1748 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001749 break;
1750 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001751 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001752 break;
1753 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001754 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001755 break;
1756 default:
1757 return( NULL );
1758 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001759 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001760 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001761
Jaeden Amero23bbb752018-06-26 14:16:54 +01001762 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1763 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001764}
1765
Gilles Peskinea05219c2018-11-16 16:02:56 +01001766#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001767static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001768{
Gilles Peskine2d277862018-06-18 15:41:12 +02001769 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001770 {
1771 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001772 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001773 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001774 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001775 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001776 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001777 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001778 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001779 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001780 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001781 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001782 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001783 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001784 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001785 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001786 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001787 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001788 return( 128 );
1789 default:
1790 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001791 }
1792}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001793#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001794
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001795/* Initialize the MAC operation structure. Once this function has been
1796 * called, psa_mac_abort can run and will do the right thing. */
1797static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1798 psa_algorithm_t alg )
1799{
1800 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1801
1802 operation->alg = alg;
1803 operation->key_set = 0;
1804 operation->iv_set = 0;
1805 operation->iv_required = 0;
1806 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001807 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001808
1809#if defined(MBEDTLS_CMAC_C)
1810 if( alg == PSA_ALG_CMAC )
1811 {
1812 operation->iv_required = 0;
1813 mbedtls_cipher_init( &operation->ctx.cmac );
1814 status = PSA_SUCCESS;
1815 }
1816 else
1817#endif /* MBEDTLS_CMAC_C */
1818#if defined(MBEDTLS_MD_C)
1819 if( PSA_ALG_IS_HMAC( operation->alg ) )
1820 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001821 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1822 operation->ctx.hmac.hash_ctx.alg = 0;
1823 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001824 }
1825 else
1826#endif /* MBEDTLS_MD_C */
1827 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001828 if( ! PSA_ALG_IS_MAC( alg ) )
1829 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001830 }
1831
1832 if( status != PSA_SUCCESS )
1833 memset( operation, 0, sizeof( *operation ) );
1834 return( status );
1835}
1836
Gilles Peskine01126fa2018-07-12 17:04:55 +02001837#if defined(MBEDTLS_MD_C)
1838static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1839{
Gilles Peskine3f108122018-12-07 18:14:53 +01001840 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001841 return( psa_hash_abort( &hmac->hash_ctx ) );
1842}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001843
1844static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1845{
1846 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1847 memset( hmac, 0, sizeof( *hmac ) );
1848}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001849#endif /* MBEDTLS_MD_C */
1850
Gilles Peskine8c9def32018-02-08 10:02:12 +01001851psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1852{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001853 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001854 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001855 /* The object has (apparently) been initialized but it is not
1856 * in use. It's ok to call abort on such an object, and there's
1857 * nothing to do. */
1858 return( PSA_SUCCESS );
1859 }
1860 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001861#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001862 if( operation->alg == PSA_ALG_CMAC )
1863 {
1864 mbedtls_cipher_free( &operation->ctx.cmac );
1865 }
1866 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001867#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001868#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001869 if( PSA_ALG_IS_HMAC( operation->alg ) )
1870 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001871 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001872 }
1873 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001874#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001875 {
1876 /* Sanity check (shouldn't happen: operation->alg should
1877 * always have been initialized to a valid value). */
1878 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001879 }
Moran Peker41deec42018-04-04 15:43:05 +03001880
Gilles Peskine8c9def32018-02-08 10:02:12 +01001881 operation->alg = 0;
1882 operation->key_set = 0;
1883 operation->iv_set = 0;
1884 operation->iv_required = 0;
1885 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001886 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001887
Gilles Peskine8c9def32018-02-08 10:02:12 +01001888 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001889
1890bad_state:
1891 /* If abort is called on an uninitialized object, we can't trust
1892 * anything. Wipe the object in case it contains confidential data.
1893 * This may result in a memory leak if a pointer gets overwritten,
1894 * but it's too late to do anything about this. */
1895 memset( operation, 0, sizeof( *operation ) );
1896 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001897}
1898
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001899#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001900static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001901 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001902 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001903 const mbedtls_cipher_info_t *cipher_info )
1904{
1905 int ret;
1906
1907 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001908
1909 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1910 if( ret != 0 )
1911 return( ret );
1912
1913 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1914 slot->data.raw.data,
1915 key_bits );
1916 return( ret );
1917}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001918#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001919
Gilles Peskine248051a2018-06-20 16:09:38 +02001920#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001921static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1922 const uint8_t *key,
1923 size_t key_length,
1924 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001925{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001926 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001927 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001928 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001929 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001930 psa_status_t status;
1931
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001932 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1933 * overflow below. This should never trigger if the hash algorithm
1934 * is implemented correctly. */
1935 /* The size checks against the ipad and opad buffers cannot be written
1936 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1937 * because that triggers -Wlogical-op on GCC 7.3. */
1938 if( block_size > sizeof( ipad ) )
1939 return( PSA_ERROR_NOT_SUPPORTED );
1940 if( block_size > sizeof( hmac->opad ) )
1941 return( PSA_ERROR_NOT_SUPPORTED );
1942 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001943 return( PSA_ERROR_NOT_SUPPORTED );
1944
Gilles Peskined223b522018-06-11 18:12:58 +02001945 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001946 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001947 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001948 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001949 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001950 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001951 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001952 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001953 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001954 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001955 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001956 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001957 }
Gilles Peskine96889972018-07-12 17:07:03 +02001958 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1959 * but it is permitted. It is common when HMAC is used in HKDF, for
1960 * example. Don't call `memcpy` in the 0-length because `key` could be
1961 * an invalid pointer which would make the behavior undefined. */
1962 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001963 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001964
Gilles Peskined223b522018-06-11 18:12:58 +02001965 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1966 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001967 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001968 ipad[i] ^= 0x36;
1969 memset( ipad + key_length, 0x36, block_size - key_length );
1970
1971 /* Copy the key material from ipad to opad, flipping the requisite bits,
1972 * and filling the rest of opad with the requisite constant. */
1973 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001974 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1975 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001976
Gilles Peskine01126fa2018-07-12 17:04:55 +02001977 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001978 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001979 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001980
Gilles Peskine01126fa2018-07-12 17:04:55 +02001981 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001982
1983cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001984 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001985
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001986 return( status );
1987}
Gilles Peskine248051a2018-06-20 16:09:38 +02001988#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001989
Gilles Peskine89167cb2018-07-08 20:12:23 +02001990static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001991 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001992 psa_algorithm_t alg,
1993 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001994{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001995 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001996 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001997 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001998 psa_key_usage_t usage =
1999 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002000 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002001 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002002
Gilles Peskined911eb72018-08-14 15:18:45 +02002003 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002004 if( status != PSA_SUCCESS )
2005 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002006 if( is_sign )
2007 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002008
Gilles Peskinec5487a82018-12-03 18:08:14 +01002009 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002010 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002011 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002012 key_bits = psa_get_key_bits( slot );
2013
Gilles Peskine8c9def32018-02-08 10:02:12 +01002014#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002015 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002016 {
2017 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002018 mbedtls_cipher_info_from_psa( full_length_alg,
2019 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002020 int ret;
2021 if( cipher_info == NULL )
2022 {
2023 status = PSA_ERROR_NOT_SUPPORTED;
2024 goto exit;
2025 }
2026 operation->mac_size = cipher_info->block_size;
2027 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2028 status = mbedtls_to_psa_error( ret );
2029 }
2030 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002031#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002032#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002033 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002034 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002035 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002036 if( hash_alg == 0 )
2037 {
2038 status = PSA_ERROR_NOT_SUPPORTED;
2039 goto exit;
2040 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002041
2042 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2043 /* Sanity check. This shouldn't fail on a valid configuration. */
2044 if( operation->mac_size == 0 ||
2045 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2046 {
2047 status = PSA_ERROR_NOT_SUPPORTED;
2048 goto exit;
2049 }
2050
Gilles Peskine01126fa2018-07-12 17:04:55 +02002051 if( slot->type != PSA_KEY_TYPE_HMAC )
2052 {
2053 status = PSA_ERROR_INVALID_ARGUMENT;
2054 goto exit;
2055 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002056
Gilles Peskine01126fa2018-07-12 17:04:55 +02002057 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2058 slot->data.raw.data,
2059 slot->data.raw.bytes,
2060 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002061 }
2062 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002063#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002064 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002065 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002066 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002067 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002068
Gilles Peskined911eb72018-08-14 15:18:45 +02002069 if( truncated == 0 )
2070 {
2071 /* The "normal" case: untruncated algorithm. Nothing to do. */
2072 }
2073 else if( truncated < 4 )
2074 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002075 /* A very short MAC is too short for security since it can be
2076 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2077 * so we make this our minimum, even though 32 bits is still
2078 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002079 status = PSA_ERROR_NOT_SUPPORTED;
2080 }
2081 else if( truncated > operation->mac_size )
2082 {
2083 /* It's impossible to "truncate" to a larger length. */
2084 status = PSA_ERROR_INVALID_ARGUMENT;
2085 }
2086 else
2087 operation->mac_size = truncated;
2088
Gilles Peskinefbfac682018-07-08 20:51:54 +02002089exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002090 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002091 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002092 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002093 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002094 else
2095 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002096 operation->key_set = 1;
2097 }
2098 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002099}
2100
Gilles Peskine89167cb2018-07-08 20:12:23 +02002101psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002102 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002103 psa_algorithm_t alg )
2104{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002105 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002106}
2107
2108psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002109 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002110 psa_algorithm_t alg )
2111{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002112 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002113}
2114
Gilles Peskine8c9def32018-02-08 10:02:12 +01002115psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2116 const uint8_t *input,
2117 size_t input_length )
2118{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002119 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002120 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002121 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002122 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002123 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002124 operation->has_input = 1;
2125
Gilles Peskine8c9def32018-02-08 10:02:12 +01002126#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002127 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002128 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002129 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2130 input, input_length );
2131 status = mbedtls_to_psa_error( ret );
2132 }
2133 else
2134#endif /* MBEDTLS_CMAC_C */
2135#if defined(MBEDTLS_MD_C)
2136 if( PSA_ALG_IS_HMAC( operation->alg ) )
2137 {
2138 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2139 input_length );
2140 }
2141 else
2142#endif /* MBEDTLS_MD_C */
2143 {
2144 /* This shouldn't happen if `operation` was initialized by
2145 * a setup function. */
2146 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002147 }
2148
Gilles Peskinefbfac682018-07-08 20:51:54 +02002149cleanup:
2150 if( status != PSA_SUCCESS )
2151 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002152 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002153}
2154
Gilles Peskine01126fa2018-07-12 17:04:55 +02002155#if defined(MBEDTLS_MD_C)
2156static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2157 uint8_t *mac,
2158 size_t mac_size )
2159{
2160 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2161 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2162 size_t hash_size = 0;
2163 size_t block_size = psa_get_hash_block_size( hash_alg );
2164 psa_status_t status;
2165
Gilles Peskine01126fa2018-07-12 17:04:55 +02002166 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2167 if( status != PSA_SUCCESS )
2168 return( status );
2169 /* From here on, tmp needs to be wiped. */
2170
2171 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2172 if( status != PSA_SUCCESS )
2173 goto exit;
2174
2175 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2176 if( status != PSA_SUCCESS )
2177 goto exit;
2178
2179 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2180 if( status != PSA_SUCCESS )
2181 goto exit;
2182
Gilles Peskined911eb72018-08-14 15:18:45 +02002183 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2184 if( status != PSA_SUCCESS )
2185 goto exit;
2186
2187 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002188
2189exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002190 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002191 return( status );
2192}
2193#endif /* MBEDTLS_MD_C */
2194
mohammad16036df908f2018-04-02 08:34:15 -07002195static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002196 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002197 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002198{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002199 if( ! operation->key_set )
2200 return( PSA_ERROR_BAD_STATE );
2201 if( operation->iv_required && ! operation->iv_set )
2202 return( PSA_ERROR_BAD_STATE );
2203
Gilles Peskine8c9def32018-02-08 10:02:12 +01002204 if( mac_size < operation->mac_size )
2205 return( PSA_ERROR_BUFFER_TOO_SMALL );
2206
Gilles Peskine8c9def32018-02-08 10:02:12 +01002207#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002208 if( operation->alg == PSA_ALG_CMAC )
2209 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002210 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2211 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2212 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002213 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002214 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002215 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002216 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002217 else
2218#endif /* MBEDTLS_CMAC_C */
2219#if defined(MBEDTLS_MD_C)
2220 if( PSA_ALG_IS_HMAC( operation->alg ) )
2221 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002222 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002223 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002224 }
2225 else
2226#endif /* MBEDTLS_MD_C */
2227 {
2228 /* This shouldn't happen if `operation` was initialized by
2229 * a setup function. */
2230 return( PSA_ERROR_BAD_STATE );
2231 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002232}
2233
Gilles Peskineacd4be32018-07-08 19:56:25 +02002234psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2235 uint8_t *mac,
2236 size_t mac_size,
2237 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002238{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002239 psa_status_t status;
2240
Jaeden Amero252ef282019-02-15 14:05:35 +00002241 if( operation->alg == 0 )
2242 {
2243 return( PSA_ERROR_BAD_STATE );
2244 }
2245
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002246 /* Fill the output buffer with something that isn't a valid mac
2247 * (barring an attack on the mac and deliberately-crafted input),
2248 * in case the caller doesn't check the return status properly. */
2249 *mac_length = mac_size;
2250 /* If mac_size is 0 then mac may be NULL and then the
2251 * call to memset would have undefined behavior. */
2252 if( mac_size != 0 )
2253 memset( mac, '!', mac_size );
2254
Gilles Peskine89167cb2018-07-08 20:12:23 +02002255 if( ! operation->is_sign )
2256 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002257 status = PSA_ERROR_BAD_STATE;
2258 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002259 }
mohammad16036df908f2018-04-02 08:34:15 -07002260
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002261 status = psa_mac_finish_internal( operation, mac, mac_size );
2262
2263cleanup:
2264 if( status == PSA_SUCCESS )
2265 {
2266 status = psa_mac_abort( operation );
2267 if( status == PSA_SUCCESS )
2268 *mac_length = operation->mac_size;
2269 else
2270 memset( mac, '!', mac_size );
2271 }
2272 else
2273 psa_mac_abort( operation );
2274 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002275}
2276
Gilles Peskineacd4be32018-07-08 19:56:25 +02002277psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2278 const uint8_t *mac,
2279 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002280{
Gilles Peskine828ed142018-06-18 23:25:51 +02002281 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002282 psa_status_t status;
2283
Jaeden Amero252ef282019-02-15 14:05:35 +00002284 if( operation->alg == 0 )
2285 {
2286 return( PSA_ERROR_BAD_STATE );
2287 }
2288
Gilles Peskine89167cb2018-07-08 20:12:23 +02002289 if( operation->is_sign )
2290 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002291 status = PSA_ERROR_BAD_STATE;
2292 goto cleanup;
2293 }
2294 if( operation->mac_size != mac_length )
2295 {
2296 status = PSA_ERROR_INVALID_SIGNATURE;
2297 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002298 }
mohammad16036df908f2018-04-02 08:34:15 -07002299
2300 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002301 actual_mac, sizeof( actual_mac ) );
2302
2303 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2304 status = PSA_ERROR_INVALID_SIGNATURE;
2305
2306cleanup:
2307 if( status == PSA_SUCCESS )
2308 status = psa_mac_abort( operation );
2309 else
2310 psa_mac_abort( operation );
2311
Gilles Peskine3f108122018-12-07 18:14:53 +01002312 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002313
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002314 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002315}
2316
2317
Gilles Peskine20035e32018-02-03 22:44:14 +01002318
Gilles Peskine20035e32018-02-03 22:44:14 +01002319/****************************************************************/
2320/* Asymmetric cryptography */
2321/****************************************************************/
2322
Gilles Peskine2b450e32018-06-27 15:42:46 +02002323#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002324/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002325 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002326static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2327 size_t hash_length,
2328 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002329{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002330 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002331 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002332 *md_alg = mbedtls_md_get_type( md_info );
2333
2334 /* The Mbed TLS RSA module uses an unsigned int for hash length
2335 * parameters. Validate that it fits so that we don't risk an
2336 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002337#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002338 if( hash_length > UINT_MAX )
2339 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002340#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002341
2342#if defined(MBEDTLS_PKCS1_V15)
2343 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2344 * must be correct. */
2345 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2346 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002347 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002348 if( md_info == NULL )
2349 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002350 if( mbedtls_md_get_size( md_info ) != hash_length )
2351 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002352 }
2353#endif /* MBEDTLS_PKCS1_V15 */
2354
2355#if defined(MBEDTLS_PKCS1_V21)
2356 /* PSS requires a hash internally. */
2357 if( PSA_ALG_IS_RSA_PSS( alg ) )
2358 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002359 if( md_info == NULL )
2360 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002361 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002362#endif /* MBEDTLS_PKCS1_V21 */
2363
Gilles Peskine61b91d42018-06-08 16:09:36 +02002364 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002365}
2366
Gilles Peskine2b450e32018-06-27 15:42:46 +02002367static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2368 psa_algorithm_t alg,
2369 const uint8_t *hash,
2370 size_t hash_length,
2371 uint8_t *signature,
2372 size_t signature_size,
2373 size_t *signature_length )
2374{
2375 psa_status_t status;
2376 int ret;
2377 mbedtls_md_type_t md_alg;
2378
2379 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2380 if( status != PSA_SUCCESS )
2381 return( status );
2382
Gilles Peskine630a18a2018-06-29 17:49:35 +02002383 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002384 return( PSA_ERROR_BUFFER_TOO_SMALL );
2385
2386#if defined(MBEDTLS_PKCS1_V15)
2387 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2388 {
2389 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2390 MBEDTLS_MD_NONE );
2391 ret = mbedtls_rsa_pkcs1_sign( rsa,
2392 mbedtls_ctr_drbg_random,
2393 &global_data.ctr_drbg,
2394 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002395 md_alg,
2396 (unsigned int) hash_length,
2397 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002398 signature );
2399 }
2400 else
2401#endif /* MBEDTLS_PKCS1_V15 */
2402#if defined(MBEDTLS_PKCS1_V21)
2403 if( PSA_ALG_IS_RSA_PSS( alg ) )
2404 {
2405 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2406 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2407 mbedtls_ctr_drbg_random,
2408 &global_data.ctr_drbg,
2409 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002410 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002411 (unsigned int) hash_length,
2412 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002413 signature );
2414 }
2415 else
2416#endif /* MBEDTLS_PKCS1_V21 */
2417 {
2418 return( PSA_ERROR_INVALID_ARGUMENT );
2419 }
2420
2421 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002422 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002423 return( mbedtls_to_psa_error( ret ) );
2424}
2425
2426static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2427 psa_algorithm_t alg,
2428 const uint8_t *hash,
2429 size_t hash_length,
2430 const uint8_t *signature,
2431 size_t signature_length )
2432{
2433 psa_status_t status;
2434 int ret;
2435 mbedtls_md_type_t md_alg;
2436
2437 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2438 if( status != PSA_SUCCESS )
2439 return( status );
2440
Gilles Peskine630a18a2018-06-29 17:49:35 +02002441 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002442 return( PSA_ERROR_BUFFER_TOO_SMALL );
2443
2444#if defined(MBEDTLS_PKCS1_V15)
2445 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2446 {
2447 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2448 MBEDTLS_MD_NONE );
2449 ret = mbedtls_rsa_pkcs1_verify( rsa,
2450 mbedtls_ctr_drbg_random,
2451 &global_data.ctr_drbg,
2452 MBEDTLS_RSA_PUBLIC,
2453 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002454 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002455 hash,
2456 signature );
2457 }
2458 else
2459#endif /* MBEDTLS_PKCS1_V15 */
2460#if defined(MBEDTLS_PKCS1_V21)
2461 if( PSA_ALG_IS_RSA_PSS( alg ) )
2462 {
2463 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2464 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2465 mbedtls_ctr_drbg_random,
2466 &global_data.ctr_drbg,
2467 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002468 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002469 (unsigned int) hash_length,
2470 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002471 signature );
2472 }
2473 else
2474#endif /* MBEDTLS_PKCS1_V21 */
2475 {
2476 return( PSA_ERROR_INVALID_ARGUMENT );
2477 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002478
2479 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2480 * the rest of the signature is invalid". This has little use in
2481 * practice and PSA doesn't report this distinction. */
2482 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2483 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002484 return( mbedtls_to_psa_error( ret ) );
2485}
2486#endif /* MBEDTLS_RSA_C */
2487
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002488#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002489/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2490 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2491 * (even though these functions don't modify it). */
2492static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2493 psa_algorithm_t alg,
2494 const uint8_t *hash,
2495 size_t hash_length,
2496 uint8_t *signature,
2497 size_t signature_size,
2498 size_t *signature_length )
2499{
2500 int ret;
2501 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002502 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002503 mbedtls_mpi_init( &r );
2504 mbedtls_mpi_init( &s );
2505
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002506 if( signature_size < 2 * curve_bytes )
2507 {
2508 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2509 goto cleanup;
2510 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002511
Gilles Peskinea05219c2018-11-16 16:02:56 +01002512#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002513 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2514 {
2515 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2516 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2517 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2518 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2519 hash, hash_length,
2520 md_alg ) );
2521 }
2522 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002523#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002524 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002525 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002526 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2527 hash, hash_length,
2528 mbedtls_ctr_drbg_random,
2529 &global_data.ctr_drbg ) );
2530 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002531
2532 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2533 signature,
2534 curve_bytes ) );
2535 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2536 signature + curve_bytes,
2537 curve_bytes ) );
2538
2539cleanup:
2540 mbedtls_mpi_free( &r );
2541 mbedtls_mpi_free( &s );
2542 if( ret == 0 )
2543 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002544 return( mbedtls_to_psa_error( ret ) );
2545}
2546
2547static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2548 const uint8_t *hash,
2549 size_t hash_length,
2550 const uint8_t *signature,
2551 size_t signature_length )
2552{
2553 int ret;
2554 mbedtls_mpi r, s;
2555 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2556 mbedtls_mpi_init( &r );
2557 mbedtls_mpi_init( &s );
2558
2559 if( signature_length != 2 * curve_bytes )
2560 return( PSA_ERROR_INVALID_SIGNATURE );
2561
2562 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2563 signature,
2564 curve_bytes ) );
2565 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2566 signature + curve_bytes,
2567 curve_bytes ) );
2568
2569 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2570 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002571
2572cleanup:
2573 mbedtls_mpi_free( &r );
2574 mbedtls_mpi_free( &s );
2575 return( mbedtls_to_psa_error( ret ) );
2576}
2577#endif /* MBEDTLS_ECDSA_C */
2578
Gilles Peskinec5487a82018-12-03 18:08:14 +01002579psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002580 psa_algorithm_t alg,
2581 const uint8_t *hash,
2582 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002583 uint8_t *signature,
2584 size_t signature_size,
2585 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002586{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002587 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002588 psa_status_t status;
2589
2590 *signature_length = signature_size;
2591
Gilles Peskinec5487a82018-12-03 18:08:14 +01002592 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002593 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002594 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002595 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002596 {
2597 status = PSA_ERROR_INVALID_ARGUMENT;
2598 goto exit;
2599 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002600
Gilles Peskine20035e32018-02-03 22:44:14 +01002601#if defined(MBEDTLS_RSA_C)
2602 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2603 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002604 status = psa_rsa_sign( slot->data.rsa,
2605 alg,
2606 hash, hash_length,
2607 signature, signature_size,
2608 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002609 }
2610 else
2611#endif /* defined(MBEDTLS_RSA_C) */
2612#if defined(MBEDTLS_ECP_C)
2613 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2614 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002615#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002616 if(
2617#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2618 PSA_ALG_IS_ECDSA( alg )
2619#else
2620 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2621#endif
2622 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002623 status = psa_ecdsa_sign( slot->data.ecp,
2624 alg,
2625 hash, hash_length,
2626 signature, signature_size,
2627 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002628 else
2629#endif /* defined(MBEDTLS_ECDSA_C) */
2630 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002631 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002632 }
itayzafrir5c753392018-05-08 11:18:38 +03002633 }
2634 else
2635#endif /* defined(MBEDTLS_ECP_C) */
2636 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002637 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002638 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002639
2640exit:
2641 /* Fill the unused part of the output buffer (the whole buffer on error,
2642 * the trailing part on success) with something that isn't a valid mac
2643 * (barring an attack on the mac and deliberately-crafted input),
2644 * in case the caller doesn't check the return status properly. */
2645 if( status == PSA_SUCCESS )
2646 memset( signature + *signature_length, '!',
2647 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002648 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002649 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002650 /* If signature_size is 0 then we have nothing to do. We must not call
2651 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002652 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002653}
2654
Gilles Peskinec5487a82018-12-03 18:08:14 +01002655psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002656 psa_algorithm_t alg,
2657 const uint8_t *hash,
2658 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002659 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002660 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002661{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002662 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002663 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002664
Gilles Peskinec5487a82018-12-03 18:08:14 +01002665 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002666 if( status != PSA_SUCCESS )
2667 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002668
Gilles Peskine61b91d42018-06-08 16:09:36 +02002669#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002670 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002671 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002672 return( psa_rsa_verify( slot->data.rsa,
2673 alg,
2674 hash, hash_length,
2675 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002676 }
2677 else
2678#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002679#if defined(MBEDTLS_ECP_C)
2680 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2681 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002682#if defined(MBEDTLS_ECDSA_C)
2683 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002684 return( psa_ecdsa_verify( slot->data.ecp,
2685 hash, hash_length,
2686 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002687 else
2688#endif /* defined(MBEDTLS_ECDSA_C) */
2689 {
2690 return( PSA_ERROR_INVALID_ARGUMENT );
2691 }
itayzafrir5c753392018-05-08 11:18:38 +03002692 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002693 else
2694#endif /* defined(MBEDTLS_ECP_C) */
2695 {
2696 return( PSA_ERROR_NOT_SUPPORTED );
2697 }
2698}
2699
Gilles Peskine072ac562018-06-30 00:21:29 +02002700#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2701static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2702 mbedtls_rsa_context *rsa )
2703{
2704 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2705 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2706 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2707 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2708}
2709#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2710
Gilles Peskinec5487a82018-12-03 18:08:14 +01002711psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002712 psa_algorithm_t alg,
2713 const uint8_t *input,
2714 size_t input_length,
2715 const uint8_t *salt,
2716 size_t salt_length,
2717 uint8_t *output,
2718 size_t output_size,
2719 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002720{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002721 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002722 psa_status_t status;
2723
Darryl Green5cc689a2018-07-24 15:34:10 +01002724 (void) input;
2725 (void) input_length;
2726 (void) salt;
2727 (void) output;
2728 (void) output_size;
2729
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002730 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002731
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002732 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2733 return( PSA_ERROR_INVALID_ARGUMENT );
2734
Gilles Peskinec5487a82018-12-03 18:08:14 +01002735 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002736 if( status != PSA_SUCCESS )
2737 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002738 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2739 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002740 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002741
2742#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002743 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002744 {
2745 mbedtls_rsa_context *rsa = slot->data.rsa;
2746 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002747 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002748 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002749#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002750 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002751 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002752 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2753 mbedtls_ctr_drbg_random,
2754 &global_data.ctr_drbg,
2755 MBEDTLS_RSA_PUBLIC,
2756 input_length,
2757 input,
2758 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002759 }
2760 else
2761#endif /* MBEDTLS_PKCS1_V15 */
2762#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002763 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002764 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002765 psa_rsa_oaep_set_padding_mode( alg, rsa );
2766 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2767 mbedtls_ctr_drbg_random,
2768 &global_data.ctr_drbg,
2769 MBEDTLS_RSA_PUBLIC,
2770 salt, salt_length,
2771 input_length,
2772 input,
2773 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002774 }
2775 else
2776#endif /* MBEDTLS_PKCS1_V21 */
2777 {
2778 return( PSA_ERROR_INVALID_ARGUMENT );
2779 }
2780 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002781 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002782 return( mbedtls_to_psa_error( ret ) );
2783 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002784 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002785#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002786 {
2787 return( PSA_ERROR_NOT_SUPPORTED );
2788 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002789}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002790
Gilles Peskinec5487a82018-12-03 18:08:14 +01002791psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002792 psa_algorithm_t alg,
2793 const uint8_t *input,
2794 size_t input_length,
2795 const uint8_t *salt,
2796 size_t salt_length,
2797 uint8_t *output,
2798 size_t output_size,
2799 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002800{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002801 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002802 psa_status_t status;
2803
Darryl Green5cc689a2018-07-24 15:34:10 +01002804 (void) input;
2805 (void) input_length;
2806 (void) salt;
2807 (void) output;
2808 (void) output_size;
2809
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002810 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002811
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002812 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2813 return( PSA_ERROR_INVALID_ARGUMENT );
2814
Gilles Peskinec5487a82018-12-03 18:08:14 +01002815 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002816 if( status != PSA_SUCCESS )
2817 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002818 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2819 return( PSA_ERROR_INVALID_ARGUMENT );
2820
2821#if defined(MBEDTLS_RSA_C)
2822 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2823 {
2824 mbedtls_rsa_context *rsa = slot->data.rsa;
2825 int ret;
2826
Gilles Peskine630a18a2018-06-29 17:49:35 +02002827 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002828 return( PSA_ERROR_INVALID_ARGUMENT );
2829
2830#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002831 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002832 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002833 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2834 mbedtls_ctr_drbg_random,
2835 &global_data.ctr_drbg,
2836 MBEDTLS_RSA_PRIVATE,
2837 output_length,
2838 input,
2839 output,
2840 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002841 }
2842 else
2843#endif /* MBEDTLS_PKCS1_V15 */
2844#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002845 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002846 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002847 psa_rsa_oaep_set_padding_mode( alg, rsa );
2848 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2849 mbedtls_ctr_drbg_random,
2850 &global_data.ctr_drbg,
2851 MBEDTLS_RSA_PRIVATE,
2852 salt, salt_length,
2853 output_length,
2854 input,
2855 output,
2856 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002857 }
2858 else
2859#endif /* MBEDTLS_PKCS1_V21 */
2860 {
2861 return( PSA_ERROR_INVALID_ARGUMENT );
2862 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002863
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002864 return( mbedtls_to_psa_error( ret ) );
2865 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002866 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002867#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002868 {
2869 return( PSA_ERROR_NOT_SUPPORTED );
2870 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002871}
Gilles Peskine20035e32018-02-03 22:44:14 +01002872
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002873
2874
mohammad1603503973b2018-03-12 15:59:30 +02002875/****************************************************************/
2876/* Symmetric cryptography */
2877/****************************************************************/
2878
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002879/* Initialize the cipher operation structure. Once this function has been
2880 * called, psa_cipher_abort can run and will do the right thing. */
2881static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2882 psa_algorithm_t alg )
2883{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002884 if( ! PSA_ALG_IS_CIPHER( alg ) )
2885 {
2886 memset( operation, 0, sizeof( *operation ) );
2887 return( PSA_ERROR_INVALID_ARGUMENT );
2888 }
2889
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002890 operation->alg = alg;
2891 operation->key_set = 0;
2892 operation->iv_set = 0;
2893 operation->iv_required = 1;
2894 operation->iv_size = 0;
2895 operation->block_size = 0;
2896 mbedtls_cipher_init( &operation->ctx.cipher );
2897 return( PSA_SUCCESS );
2898}
2899
Gilles Peskinee553c652018-06-04 16:22:46 +02002900static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002901 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002902 psa_algorithm_t alg,
2903 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002904{
2905 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2906 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002907 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002908 size_t key_bits;
2909 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002910 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2911 PSA_KEY_USAGE_ENCRYPT :
2912 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002913
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002914 status = psa_cipher_init( operation, alg );
2915 if( status != PSA_SUCCESS )
2916 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002917
Gilles Peskinec5487a82018-12-03 18:08:14 +01002918 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002919 if( status != PSA_SUCCESS )
2920 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002921 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002922
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002923 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002924 if( cipher_info == NULL )
2925 return( PSA_ERROR_NOT_SUPPORTED );
2926
mohammad1603503973b2018-03-12 15:59:30 +02002927 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002928 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002929 {
2930 psa_cipher_abort( operation );
2931 return( mbedtls_to_psa_error( ret ) );
2932 }
2933
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002934#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002935 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002936 {
2937 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2938 unsigned char keys[24];
2939 memcpy( keys, slot->data.raw.data, 16 );
2940 memcpy( keys + 16, slot->data.raw.data, 8 );
2941 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2942 keys,
2943 192, cipher_operation );
2944 }
2945 else
2946#endif
2947 {
2948 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2949 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002950 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002951 }
Moran Peker41deec42018-04-04 15:43:05 +03002952 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002953 {
2954 psa_cipher_abort( operation );
2955 return( mbedtls_to_psa_error( ret ) );
2956 }
2957
mohammad16038481e742018-03-18 13:57:31 +02002958#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002959 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002960 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002961 case PSA_ALG_CBC_NO_PADDING:
2962 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2963 MBEDTLS_PADDING_NONE );
2964 break;
2965 case PSA_ALG_CBC_PKCS7:
2966 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2967 MBEDTLS_PADDING_PKCS7 );
2968 break;
2969 default:
2970 /* The algorithm doesn't involve padding. */
2971 ret = 0;
2972 break;
2973 }
2974 if( ret != 0 )
2975 {
2976 psa_cipher_abort( operation );
2977 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002978 }
2979#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2980
mohammad1603503973b2018-03-12 15:59:30 +02002981 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002982 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2983 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2984 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002985 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002986 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002987 }
mohammad1603503973b2018-03-12 15:59:30 +02002988
Moran Peker395db872018-05-31 14:07:14 +03002989 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002990}
2991
Gilles Peskinefe119512018-07-08 21:39:34 +02002992psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002993 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002994 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002995{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002996 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002997}
2998
Gilles Peskinefe119512018-07-08 21:39:34 +02002999psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003000 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003001 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003002{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003003 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003004}
3005
Gilles Peskinefe119512018-07-08 21:39:34 +02003006psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3007 unsigned char *iv,
3008 size_t iv_size,
3009 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003010{
itayzafrir534bd7c2018-08-02 13:56:32 +03003011 psa_status_t status;
3012 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003013 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003014 {
3015 status = PSA_ERROR_BAD_STATE;
3016 goto exit;
3017 }
Moran Peker41deec42018-04-04 15:43:05 +03003018 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003019 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003020 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003021 goto exit;
3022 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003023 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3024 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003025 if( ret != 0 )
3026 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003027 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003028 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003029 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003030
mohammad16038481e742018-03-18 13:57:31 +02003031 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003032 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003033
Moran Peker395db872018-05-31 14:07:14 +03003034exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003035 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003036 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003037 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003038}
3039
Gilles Peskinefe119512018-07-08 21:39:34 +02003040psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3041 const unsigned char *iv,
3042 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003043{
itayzafrir534bd7c2018-08-02 13:56:32 +03003044 psa_status_t status;
3045 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003046 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003047 {
3048 status = PSA_ERROR_BAD_STATE;
3049 goto exit;
3050 }
Moran Pekera28258c2018-05-29 16:25:04 +03003051 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003052 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003053 status = PSA_ERROR_INVALID_ARGUMENT;
3054 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003055 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003056 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3057 status = mbedtls_to_psa_error( ret );
3058exit:
3059 if( status == PSA_SUCCESS )
3060 operation->iv_set = 1;
3061 else
mohammad1603503973b2018-03-12 15:59:30 +02003062 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003063 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003064}
3065
Gilles Peskinee553c652018-06-04 16:22:46 +02003066psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3067 const uint8_t *input,
3068 size_t input_length,
3069 unsigned char *output,
3070 size_t output_size,
3071 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003072{
itayzafrir534bd7c2018-08-02 13:56:32 +03003073 psa_status_t status;
3074 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003075 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003076 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003077 {
3078 /* Take the unprocessed partial block left over from previous
3079 * update calls, if any, plus the input to this call. Remove
3080 * the last partial block, if any. You get the data that will be
3081 * output in this call. */
3082 expected_output_size =
3083 ( operation->ctx.cipher.unprocessed_len + input_length )
3084 / operation->block_size * operation->block_size;
3085 }
3086 else
3087 {
3088 expected_output_size = input_length;
3089 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003090
Gilles Peskine89d789c2018-06-04 17:17:16 +02003091 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003092 {
3093 status = PSA_ERROR_BUFFER_TOO_SMALL;
3094 goto exit;
3095 }
mohammad160382759612018-03-12 18:16:40 +02003096
mohammad1603503973b2018-03-12 15:59:30 +02003097 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003098 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003099 status = mbedtls_to_psa_error( ret );
3100exit:
3101 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003102 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003103 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003104}
3105
Gilles Peskinee553c652018-06-04 16:22:46 +02003106psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3107 uint8_t *output,
3108 size_t output_size,
3109 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003110{
Janos Follath315b51c2018-07-09 16:04:51 +01003111 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
3112 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003113 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003114
mohammad1603503973b2018-03-12 15:59:30 +02003115 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003116 {
Janos Follath315b51c2018-07-09 16:04:51 +01003117 status = PSA_ERROR_BAD_STATE;
3118 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03003119 }
3120 if( operation->iv_required && ! operation->iv_set )
3121 {
Janos Follath315b51c2018-07-09 16:04:51 +01003122 status = PSA_ERROR_BAD_STATE;
3123 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03003124 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003125
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003126 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003127 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3128 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003129 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003130 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003131 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003132 }
3133
Janos Follath315b51c2018-07-09 16:04:51 +01003134 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3135 temp_output_buffer,
3136 output_length );
3137 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003138 {
Janos Follath315b51c2018-07-09 16:04:51 +01003139 status = mbedtls_to_psa_error( cipher_ret );
3140 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003141 }
Janos Follath315b51c2018-07-09 16:04:51 +01003142
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003143 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003144 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003145 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003146 memcpy( output, temp_output_buffer, *output_length );
3147 else
3148 {
Janos Follath315b51c2018-07-09 16:04:51 +01003149 status = PSA_ERROR_BUFFER_TOO_SMALL;
3150 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003151 }
mohammad1603503973b2018-03-12 15:59:30 +02003152
Gilles Peskine3f108122018-12-07 18:14:53 +01003153 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003154 status = psa_cipher_abort( operation );
3155
3156 return( status );
3157
3158error:
3159
3160 *output_length = 0;
3161
Gilles Peskine3f108122018-12-07 18:14:53 +01003162 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003163 (void) psa_cipher_abort( operation );
3164
3165 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003166}
3167
Gilles Peskinee553c652018-06-04 16:22:46 +02003168psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3169{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003170 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003171 {
3172 /* The object has (apparently) been initialized but it is not
3173 * in use. It's ok to call abort on such an object, and there's
3174 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003175 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003176 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003177
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003178 /* Sanity check (shouldn't happen: operation->alg should
3179 * always have been initialized to a valid value). */
3180 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3181 return( PSA_ERROR_BAD_STATE );
3182
mohammad1603503973b2018-03-12 15:59:30 +02003183 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003184
Moran Peker41deec42018-04-04 15:43:05 +03003185 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003186 operation->key_set = 0;
3187 operation->iv_set = 0;
3188 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003189 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003190 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003191
Moran Peker395db872018-05-31 14:07:14 +03003192 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003193}
3194
Gilles Peskinea0655c32018-04-30 17:06:50 +02003195
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003196
mohammad16038cc1cee2018-03-28 01:21:33 +03003197/****************************************************************/
3198/* Key Policy */
3199/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003200
mohammad160327010052018-07-03 13:16:15 +03003201#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003202void psa_key_policy_set_usage( psa_key_policy_t *policy,
3203 psa_key_usage_t usage,
3204 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003205{
mohammad16034eed7572018-03-28 05:14:59 -07003206 policy->usage = usage;
3207 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003208}
3209
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003210psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003211{
mohammad16036df908f2018-04-02 08:34:15 -07003212 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003213}
3214
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003215psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003216{
mohammad16036df908f2018-04-02 08:34:15 -07003217 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003218}
mohammad160327010052018-07-03 13:16:15 +03003219#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003220
Gilles Peskinec5487a82018-12-03 18:08:14 +01003221psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003222 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003223{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003224 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003225 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003226
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003227 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003228 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003229
Gilles Peskinec5487a82018-12-03 18:08:14 +01003230 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003231 if( status != PSA_SUCCESS )
3232 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003233
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003234 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3235 PSA_KEY_USAGE_ENCRYPT |
3236 PSA_KEY_USAGE_DECRYPT |
3237 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003238 PSA_KEY_USAGE_VERIFY |
3239 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003240 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003241
mohammad16036df908f2018-04-02 08:34:15 -07003242 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003243
3244 return( PSA_SUCCESS );
3245}
3246
Gilles Peskinec5487a82018-12-03 18:08:14 +01003247psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003248 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003249{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003250 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003251 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003252
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003253 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003254 return( PSA_ERROR_INVALID_ARGUMENT );
3255
Gilles Peskinec5487a82018-12-03 18:08:14 +01003256 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003257 if( status != PSA_SUCCESS )
3258 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003259
mohammad16036df908f2018-04-02 08:34:15 -07003260 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003261
3262 return( PSA_SUCCESS );
3263}
Gilles Peskine20035e32018-02-03 22:44:14 +01003264
Gilles Peskinea0655c32018-04-30 17:06:50 +02003265
3266
mohammad1603804cd712018-03-20 22:44:08 +02003267/****************************************************************/
3268/* Key Lifetime */
3269/****************************************************************/
3270
Gilles Peskinec5487a82018-12-03 18:08:14 +01003271psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003272 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003273{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003274 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003275 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003276
Gilles Peskinec5487a82018-12-03 18:08:14 +01003277 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003278 if( status != PSA_SUCCESS )
3279 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003280
mohammad1603804cd712018-03-20 22:44:08 +02003281 *lifetime = slot->lifetime;
3282
3283 return( PSA_SUCCESS );
3284}
3285
Gilles Peskine20035e32018-02-03 22:44:14 +01003286
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003287
mohammad16035955c982018-04-26 00:53:03 +03003288/****************************************************************/
3289/* AEAD */
3290/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003291
Gilles Peskineedf9a652018-08-17 18:11:56 +02003292typedef struct
3293{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003294 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003295 const mbedtls_cipher_info_t *cipher_info;
3296 union
3297 {
3298#if defined(MBEDTLS_CCM_C)
3299 mbedtls_ccm_context ccm;
3300#endif /* MBEDTLS_CCM_C */
3301#if defined(MBEDTLS_GCM_C)
3302 mbedtls_gcm_context gcm;
3303#endif /* MBEDTLS_GCM_C */
3304 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003305 psa_algorithm_t core_alg;
3306 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003307 uint8_t tag_length;
3308} aead_operation_t;
3309
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003310static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003311{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003312 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003313 {
3314#if defined(MBEDTLS_CCM_C)
3315 case PSA_ALG_CCM:
3316 mbedtls_ccm_free( &operation->ctx.ccm );
3317 break;
3318#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003319#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003320 case PSA_ALG_GCM:
3321 mbedtls_gcm_free( &operation->ctx.gcm );
3322 break;
3323#endif /* MBEDTLS_GCM_C */
3324 }
3325}
3326
3327static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003328 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003329 psa_key_usage_t usage,
3330 psa_algorithm_t alg )
3331{
3332 psa_status_t status;
3333 size_t key_bits;
3334 mbedtls_cipher_id_t cipher_id;
3335
Gilles Peskinec5487a82018-12-03 18:08:14 +01003336 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003337 if( status != PSA_SUCCESS )
3338 return( status );
3339
3340 key_bits = psa_get_key_bits( operation->slot );
3341
3342 operation->cipher_info =
3343 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3344 &cipher_id );
3345 if( operation->cipher_info == NULL )
3346 return( PSA_ERROR_NOT_SUPPORTED );
3347
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003348 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003349 {
3350#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003351 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3352 operation->core_alg = PSA_ALG_CCM;
3353 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003354 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3355 return( PSA_ERROR_INVALID_ARGUMENT );
3356 mbedtls_ccm_init( &operation->ctx.ccm );
3357 status = mbedtls_to_psa_error(
3358 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3359 operation->slot->data.raw.data,
3360 (unsigned int) key_bits ) );
3361 if( status != 0 )
3362 goto cleanup;
3363 break;
3364#endif /* MBEDTLS_CCM_C */
3365
3366#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003367 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3368 operation->core_alg = PSA_ALG_GCM;
3369 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003370 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3371 return( PSA_ERROR_INVALID_ARGUMENT );
3372 mbedtls_gcm_init( &operation->ctx.gcm );
3373 status = mbedtls_to_psa_error(
3374 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3375 operation->slot->data.raw.data,
3376 (unsigned int) key_bits ) );
3377 break;
3378#endif /* MBEDTLS_GCM_C */
3379
3380 default:
3381 return( PSA_ERROR_NOT_SUPPORTED );
3382 }
3383
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003384 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3385 {
3386 status = PSA_ERROR_INVALID_ARGUMENT;
3387 goto cleanup;
3388 }
3389 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3390 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3391 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3392 * In both cases, mbedtls_xxx will validate the tag length below. */
3393
Gilles Peskineedf9a652018-08-17 18:11:56 +02003394 return( PSA_SUCCESS );
3395
3396cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003397 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003398 return( status );
3399}
3400
Gilles Peskinec5487a82018-12-03 18:08:14 +01003401psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003402 psa_algorithm_t alg,
3403 const uint8_t *nonce,
3404 size_t nonce_length,
3405 const uint8_t *additional_data,
3406 size_t additional_data_length,
3407 const uint8_t *plaintext,
3408 size_t plaintext_length,
3409 uint8_t *ciphertext,
3410 size_t ciphertext_size,
3411 size_t *ciphertext_length )
3412{
mohammad16035955c982018-04-26 00:53:03 +03003413 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003414 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003415 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003416
mohammad1603f08a5502018-06-03 15:05:47 +03003417 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003418
Gilles Peskinec5487a82018-12-03 18:08:14 +01003419 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003420 if( status != PSA_SUCCESS )
3421 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003422
Gilles Peskineedf9a652018-08-17 18:11:56 +02003423 /* For all currently supported modes, the tag is at the end of the
3424 * ciphertext. */
3425 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3426 {
3427 status = PSA_ERROR_BUFFER_TOO_SMALL;
3428 goto exit;
3429 }
3430 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003431
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003432#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003433 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003434 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003435 status = mbedtls_to_psa_error(
3436 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3437 MBEDTLS_GCM_ENCRYPT,
3438 plaintext_length,
3439 nonce, nonce_length,
3440 additional_data, additional_data_length,
3441 plaintext, ciphertext,
3442 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003443 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003444 else
3445#endif /* MBEDTLS_GCM_C */
3446#if defined(MBEDTLS_CCM_C)
3447 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003448 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003449 status = mbedtls_to_psa_error(
3450 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3451 plaintext_length,
3452 nonce, nonce_length,
3453 additional_data,
3454 additional_data_length,
3455 plaintext, ciphertext,
3456 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003457 }
mohammad16035c8845f2018-05-09 05:40:09 -07003458 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003459#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003460 {
mohammad1603554faad2018-06-03 15:07:38 +03003461 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003462 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003463
Gilles Peskineedf9a652018-08-17 18:11:56 +02003464 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3465 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003466
Gilles Peskineedf9a652018-08-17 18:11:56 +02003467exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003468 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003469 if( status == PSA_SUCCESS )
3470 *ciphertext_length = plaintext_length + operation.tag_length;
3471 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003472}
3473
Gilles Peskineee652a32018-06-01 19:23:52 +02003474/* Locate the tag in a ciphertext buffer containing the encrypted data
3475 * followed by the tag. Return the length of the part preceding the tag in
3476 * *plaintext_length. This is the size of the plaintext in modes where
3477 * the encrypted data has the same size as the plaintext, such as
3478 * CCM and GCM. */
3479static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3480 const uint8_t *ciphertext,
3481 size_t ciphertext_length,
3482 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003483 const uint8_t **p_tag )
3484{
3485 size_t payload_length;
3486 if( tag_length > ciphertext_length )
3487 return( PSA_ERROR_INVALID_ARGUMENT );
3488 payload_length = ciphertext_length - tag_length;
3489 if( payload_length > plaintext_size )
3490 return( PSA_ERROR_BUFFER_TOO_SMALL );
3491 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003492 return( PSA_SUCCESS );
3493}
3494
Gilles Peskinec5487a82018-12-03 18:08:14 +01003495psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003496 psa_algorithm_t alg,
3497 const uint8_t *nonce,
3498 size_t nonce_length,
3499 const uint8_t *additional_data,
3500 size_t additional_data_length,
3501 const uint8_t *ciphertext,
3502 size_t ciphertext_length,
3503 uint8_t *plaintext,
3504 size_t plaintext_size,
3505 size_t *plaintext_length )
3506{
mohammad16035955c982018-04-26 00:53:03 +03003507 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003508 aead_operation_t operation;
3509 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003510
Gilles Peskineee652a32018-06-01 19:23:52 +02003511 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003512
Gilles Peskinec5487a82018-12-03 18:08:14 +01003513 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003514 if( status != PSA_SUCCESS )
3515 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003516
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003517#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003518 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003519 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003520 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003521 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003522 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003523 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003524 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003525
Gilles Peskineedf9a652018-08-17 18:11:56 +02003526 status = mbedtls_to_psa_error(
3527 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3528 ciphertext_length - operation.tag_length,
3529 nonce, nonce_length,
3530 additional_data,
3531 additional_data_length,
3532 tag, operation.tag_length,
3533 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003534 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003535 else
3536#endif /* MBEDTLS_GCM_C */
3537#if defined(MBEDTLS_CCM_C)
3538 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003539 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003540 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003541 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003542 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003543 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003544 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003545
Gilles Peskineedf9a652018-08-17 18:11:56 +02003546 status = mbedtls_to_psa_error(
3547 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3548 ciphertext_length - operation.tag_length,
3549 nonce, nonce_length,
3550 additional_data,
3551 additional_data_length,
3552 ciphertext, plaintext,
3553 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003554 }
mohammad160339574652018-06-01 04:39:53 -07003555 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003556#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003557 {
mohammad1603554faad2018-06-03 15:07:38 +03003558 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003559 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003560
Gilles Peskineedf9a652018-08-17 18:11:56 +02003561 if( status != PSA_SUCCESS && plaintext_size != 0 )
3562 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003563
Gilles Peskineedf9a652018-08-17 18:11:56 +02003564exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003565 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003566 if( status == PSA_SUCCESS )
3567 *plaintext_length = ciphertext_length - operation.tag_length;
3568 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003569}
3570
Gilles Peskinea0655c32018-04-30 17:06:50 +02003571
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003572
Gilles Peskine20035e32018-02-03 22:44:14 +01003573/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003574/* Generators */
3575/****************************************************************/
3576
3577psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3578{
3579 psa_status_t status = PSA_SUCCESS;
3580 if( generator->alg == 0 )
3581 {
3582 /* The object has (apparently) been initialized but it is not
3583 * in use. It's ok to call abort on such an object, and there's
3584 * nothing to do. */
3585 }
3586 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003587 if( generator->alg == PSA_ALG_SELECT_RAW )
3588 {
3589 if( generator->ctx.buffer.data != NULL )
3590 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003591 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003592 generator->ctx.buffer.size );
3593 mbedtls_free( generator->ctx.buffer.data );
3594 }
3595 }
3596 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003597#if defined(MBEDTLS_MD_C)
3598 if( PSA_ALG_IS_HKDF( generator->alg ) )
3599 {
3600 mbedtls_free( generator->ctx.hkdf.info );
3601 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3602 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003603 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3604 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3605 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003606 {
3607 if( generator->ctx.tls12_prf.key != NULL )
3608 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003609 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003610 generator->ctx.tls12_prf.key_len );
3611 mbedtls_free( generator->ctx.tls12_prf.key );
3612 }
Hanno Becker580fba12018-11-13 20:50:45 +00003613
3614 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3615 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003616 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003617 generator->ctx.tls12_prf.Ai_with_seed_len );
3618 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3619 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003620 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003621 else
3622#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003623 {
3624 status = PSA_ERROR_BAD_STATE;
3625 }
3626 memset( generator, 0, sizeof( *generator ) );
3627 return( status );
3628}
3629
3630
3631psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3632 size_t *capacity)
3633{
3634 *capacity = generator->capacity;
3635 return( PSA_SUCCESS );
3636}
3637
Gilles Peskinebef7f142018-07-12 17:22:21 +02003638#if defined(MBEDTLS_MD_C)
3639/* Read some bytes from an HKDF-based generator. This performs a chunk
3640 * of the expand phase of the HKDF algorithm. */
3641static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3642 psa_algorithm_t hash_alg,
3643 uint8_t *output,
3644 size_t output_length )
3645{
3646 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3647 psa_status_t status;
3648
3649 while( output_length != 0 )
3650 {
3651 /* Copy what remains of the current block */
3652 uint8_t n = hash_length - hkdf->offset_in_block;
3653 if( n > output_length )
3654 n = (uint8_t) output_length;
3655 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3656 output += n;
3657 output_length -= n;
3658 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003659 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003660 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003661 /* We can't be wanting more output after block 0xff, otherwise
3662 * the capacity check in psa_generator_read() would have
3663 * prevented this call. It could happen only if the generator
3664 * object was corrupted or if this function is called directly
3665 * inside the library. */
3666 if( hkdf->block_number == 0xff )
3667 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003668
3669 /* We need a new block */
3670 ++hkdf->block_number;
3671 hkdf->offset_in_block = 0;
3672 status = psa_hmac_setup_internal( &hkdf->hmac,
3673 hkdf->prk, hash_length,
3674 hash_alg );
3675 if( status != PSA_SUCCESS )
3676 return( status );
3677 if( hkdf->block_number != 1 )
3678 {
3679 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3680 hkdf->output_block,
3681 hash_length );
3682 if( status != PSA_SUCCESS )
3683 return( status );
3684 }
3685 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3686 hkdf->info,
3687 hkdf->info_length );
3688 if( status != PSA_SUCCESS )
3689 return( status );
3690 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3691 &hkdf->block_number, 1 );
3692 if( status != PSA_SUCCESS )
3693 return( status );
3694 status = psa_hmac_finish_internal( &hkdf->hmac,
3695 hkdf->output_block,
3696 sizeof( hkdf->output_block ) );
3697 if( status != PSA_SUCCESS )
3698 return( status );
3699 }
3700
3701 return( PSA_SUCCESS );
3702}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003703
3704static psa_status_t psa_generator_tls12_prf_generate_next_block(
3705 psa_tls12_prf_generator_t *tls12_prf,
3706 psa_algorithm_t alg )
3707{
3708 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3709 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3710 psa_hmac_internal_data hmac;
3711 psa_status_t status, cleanup_status;
3712
Hanno Becker3b339e22018-11-13 20:56:14 +00003713 unsigned char *Ai;
3714 size_t Ai_len;
3715
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003716 /* We can't be wanting more output after block 0xff, otherwise
3717 * the capacity check in psa_generator_read() would have
3718 * prevented this call. It could happen only if the generator
3719 * object was corrupted or if this function is called directly
3720 * inside the library. */
3721 if( tls12_prf->block_number == 0xff )
3722 return( PSA_ERROR_BAD_STATE );
3723
3724 /* We need a new block */
3725 ++tls12_prf->block_number;
3726 tls12_prf->offset_in_block = 0;
3727
3728 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3729 *
3730 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3731 *
3732 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3733 * HMAC_hash(secret, A(2) + seed) +
3734 * HMAC_hash(secret, A(3) + seed) + ...
3735 *
3736 * A(0) = seed
3737 * A(i) = HMAC_hash( secret, A(i-1) )
3738 *
3739 * The `psa_tls12_prf_generator` structures saves the block
3740 * `HMAC_hash(secret, A(i) + seed)` from which the output
3741 * is currently extracted as `output_block`, while
3742 * `A(i) + seed` is stored in `Ai_with_seed`.
3743 *
3744 * Generating a new block means recalculating `Ai_with_seed`
3745 * from the A(i)-part of it, and afterwards recalculating
3746 * `output_block`.
3747 *
3748 * A(0) is computed at setup time.
3749 *
3750 */
3751
3752 psa_hmac_init_internal( &hmac );
3753
3754 /* We must distinguish the calculation of A(1) from those
3755 * of A(2) and higher, because A(0)=seed has a different
3756 * length than the other A(i). */
3757 if( tls12_prf->block_number == 1 )
3758 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003759 Ai = tls12_prf->Ai_with_seed + hash_length;
3760 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003761 }
3762 else
3763 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003764 Ai = tls12_prf->Ai_with_seed;
3765 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003766 }
3767
Hanno Becker3b339e22018-11-13 20:56:14 +00003768 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3769 status = psa_hmac_setup_internal( &hmac,
3770 tls12_prf->key,
3771 tls12_prf->key_len,
3772 hash_alg );
3773 if( status != PSA_SUCCESS )
3774 goto cleanup;
3775
3776 status = psa_hash_update( &hmac.hash_ctx,
3777 Ai, Ai_len );
3778 if( status != PSA_SUCCESS )
3779 goto cleanup;
3780
3781 status = psa_hmac_finish_internal( &hmac,
3782 tls12_prf->Ai_with_seed,
3783 hash_length );
3784 if( status != PSA_SUCCESS )
3785 goto cleanup;
3786
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003787 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3788 status = psa_hmac_setup_internal( &hmac,
3789 tls12_prf->key,
3790 tls12_prf->key_len,
3791 hash_alg );
3792 if( status != PSA_SUCCESS )
3793 goto cleanup;
3794
3795 status = psa_hash_update( &hmac.hash_ctx,
3796 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003797 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003798 if( status != PSA_SUCCESS )
3799 goto cleanup;
3800
3801 status = psa_hmac_finish_internal( &hmac,
3802 tls12_prf->output_block,
3803 hash_length );
3804 if( status != PSA_SUCCESS )
3805 goto cleanup;
3806
3807cleanup:
3808
3809 cleanup_status = psa_hmac_abort_internal( &hmac );
3810 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3811 status = cleanup_status;
3812
3813 return( status );
3814}
3815
3816/* Read some bytes from an TLS-1.2-PRF-based generator.
3817 * See Section 5 of RFC 5246. */
3818static psa_status_t psa_generator_tls12_prf_read(
3819 psa_tls12_prf_generator_t *tls12_prf,
3820 psa_algorithm_t alg,
3821 uint8_t *output,
3822 size_t output_length )
3823{
3824 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3825 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3826 psa_status_t status;
3827
3828 while( output_length != 0 )
3829 {
3830 /* Copy what remains of the current block */
3831 uint8_t n = hash_length - tls12_prf->offset_in_block;
3832
3833 /* Check if we have fully processed the current block. */
3834 if( n == 0 )
3835 {
3836 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3837 alg );
3838 if( status != PSA_SUCCESS )
3839 return( status );
3840
3841 continue;
3842 }
3843
3844 if( n > output_length )
3845 n = (uint8_t) output_length;
3846 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3847 n );
3848 output += n;
3849 output_length -= n;
3850 tls12_prf->offset_in_block += n;
3851 }
3852
3853 return( PSA_SUCCESS );
3854}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003855#endif /* MBEDTLS_MD_C */
3856
Gilles Peskineeab56e42018-07-12 17:12:33 +02003857psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3858 uint8_t *output,
3859 size_t output_length )
3860{
3861 psa_status_t status;
3862
3863 if( output_length > generator->capacity )
3864 {
3865 generator->capacity = 0;
3866 /* Go through the error path to wipe all confidential data now
3867 * that the generator object is useless. */
3868 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3869 goto exit;
3870 }
3871 if( output_length == 0 &&
3872 generator->capacity == 0 && generator->alg == 0 )
3873 {
3874 /* Edge case: this is a blank or finished generator, and 0
3875 * bytes were requested. The right error in this case could
3876 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3877 * INSUFFICIENT_CAPACITY, which is right for a finished
3878 * generator, for consistency with the case when
3879 * output_length > 0. */
3880 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3881 }
3882 generator->capacity -= output_length;
3883
Gilles Peskine751d9652018-09-18 12:05:44 +02003884 if( generator->alg == PSA_ALG_SELECT_RAW )
3885 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003886 /* Initially, the capacity of a selection generator is always
3887 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3888 * abbreviated in this comment as `size`. When the remaining
3889 * capacity is `c`, the next bytes to serve start `c` bytes
3890 * from the end of the buffer, i.e. `size - c` from the
3891 * beginning of the buffer. Since `generator->capacity` was just
3892 * decremented above, we need to serve the bytes from
3893 * `size - generator->capacity - output_length` to
3894 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003895 size_t offset =
3896 generator->ctx.buffer.size - generator->capacity - output_length;
3897 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3898 status = PSA_SUCCESS;
3899 }
3900 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003901#if defined(MBEDTLS_MD_C)
3902 if( PSA_ALG_IS_HKDF( generator->alg ) )
3903 {
3904 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3905 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3906 output, output_length );
3907 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003908 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3909 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003910 {
3911 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3912 generator->alg, output,
3913 output_length );
3914 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003915 else
3916#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003917 {
3918 return( PSA_ERROR_BAD_STATE );
3919 }
3920
3921exit:
3922 if( status != PSA_SUCCESS )
3923 {
3924 psa_generator_abort( generator );
3925 memset( output, '!', output_length );
3926 }
3927 return( status );
3928}
3929
Gilles Peskine08542d82018-07-19 17:05:42 +02003930#if defined(MBEDTLS_DES_C)
3931static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3932{
3933 if( data_size >= 8 )
3934 mbedtls_des_key_set_parity( data );
3935 if( data_size >= 16 )
3936 mbedtls_des_key_set_parity( data + 8 );
3937 if( data_size >= 24 )
3938 mbedtls_des_key_set_parity( data + 16 );
3939}
3940#endif /* MBEDTLS_DES_C */
3941
Gilles Peskinec5487a82018-12-03 18:08:14 +01003942psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003943 psa_key_type_t type,
3944 size_t bits,
3945 psa_crypto_generator_t *generator )
3946{
3947 uint8_t *data = NULL;
3948 size_t bytes = PSA_BITS_TO_BYTES( bits );
3949 psa_status_t status;
3950
3951 if( ! key_type_is_raw_bytes( type ) )
3952 return( PSA_ERROR_INVALID_ARGUMENT );
3953 if( bits % 8 != 0 )
3954 return( PSA_ERROR_INVALID_ARGUMENT );
3955 data = mbedtls_calloc( 1, bytes );
3956 if( data == NULL )
3957 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3958
3959 status = psa_generator_read( generator, data, bytes );
3960 if( status != PSA_SUCCESS )
3961 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003962#if defined(MBEDTLS_DES_C)
3963 if( type == PSA_KEY_TYPE_DES )
3964 psa_des_set_key_parity( data, bytes );
3965#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003966 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003967
3968exit:
3969 mbedtls_free( data );
3970 return( status );
3971}
3972
3973
3974
3975/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003976/* Key derivation */
3977/****************************************************************/
3978
Gilles Peskinea05219c2018-11-16 16:02:56 +01003979#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003980/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003981 * of the HKDF algorithm.
3982 *
3983 * Note that if this function fails, you must call psa_generator_abort()
3984 * to potentially free embedded data structures and wipe confidential data.
3985 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003986static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003987 const uint8_t *secret,
3988 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003989 psa_algorithm_t hash_alg,
3990 const uint8_t *salt,
3991 size_t salt_length,
3992 const uint8_t *label,
3993 size_t label_length )
3994{
3995 psa_status_t status;
3996 status = psa_hmac_setup_internal( &hkdf->hmac,
3997 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003998 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003999 if( status != PSA_SUCCESS )
4000 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004001 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004002 if( status != PSA_SUCCESS )
4003 return( status );
4004 status = psa_hmac_finish_internal( &hkdf->hmac,
4005 hkdf->prk,
4006 sizeof( hkdf->prk ) );
4007 if( status != PSA_SUCCESS )
4008 return( status );
4009 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4010 hkdf->block_number = 0;
4011 hkdf->info_length = label_length;
4012 if( label_length != 0 )
4013 {
4014 hkdf->info = mbedtls_calloc( 1, label_length );
4015 if( hkdf->info == NULL )
4016 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4017 memcpy( hkdf->info, label, label_length );
4018 }
4019 return( PSA_SUCCESS );
4020}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004021#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004022
Gilles Peskinea05219c2018-11-16 16:02:56 +01004023#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004024/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4025 *
4026 * Note that if this function fails, you must call psa_generator_abort()
4027 * to potentially free embedded data structures and wipe confidential data.
4028 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004029static psa_status_t psa_generator_tls12_prf_setup(
4030 psa_tls12_prf_generator_t *tls12_prf,
4031 const unsigned char *key,
4032 size_t key_len,
4033 psa_algorithm_t hash_alg,
4034 const uint8_t *salt,
4035 size_t salt_length,
4036 const uint8_t *label,
4037 size_t label_length )
4038{
4039 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004040 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4041 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004042
4043 tls12_prf->key = mbedtls_calloc( 1, key_len );
4044 if( tls12_prf->key == NULL )
4045 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4046 tls12_prf->key_len = key_len;
4047 memcpy( tls12_prf->key, key, key_len );
4048
Hanno Becker580fba12018-11-13 20:50:45 +00004049 overflow = ( salt_length + label_length < salt_length ) ||
4050 ( salt_length + label_length + hash_length < hash_length );
4051 if( overflow )
4052 return( PSA_ERROR_INVALID_ARGUMENT );
4053
4054 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4055 if( tls12_prf->Ai_with_seed == NULL )
4056 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4057 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4058
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004059 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4060 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004061 if( label_length != 0 )
4062 {
4063 memcpy( tls12_prf->Ai_with_seed + hash_length,
4064 label, label_length );
4065 }
4066
4067 if( salt_length != 0 )
4068 {
4069 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4070 salt, salt_length );
4071 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004072
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004073 /* The first block gets generated when
4074 * psa_generator_read() is called. */
4075 tls12_prf->block_number = 0;
4076 tls12_prf->offset_in_block = hash_length;
4077
4078 return( PSA_SUCCESS );
4079}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004080
4081/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4082static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4083 psa_tls12_prf_generator_t *tls12_prf,
4084 const unsigned char *psk,
4085 size_t psk_len,
4086 psa_algorithm_t hash_alg,
4087 const uint8_t *salt,
4088 size_t salt_length,
4089 const uint8_t *label,
4090 size_t label_length )
4091{
4092 psa_status_t status;
4093 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4094
4095 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4096 return( PSA_ERROR_INVALID_ARGUMENT );
4097
4098 /* Quoting RFC 4279, Section 2:
4099 *
4100 * The premaster secret is formed as follows: if the PSK is N octets
4101 * long, concatenate a uint16 with the value N, N zero octets, a second
4102 * uint16 with the value N, and the PSK itself.
4103 */
4104
4105 pms[0] = ( psk_len >> 8 ) & 0xff;
4106 pms[1] = ( psk_len >> 0 ) & 0xff;
4107 memset( pms + 2, 0, psk_len );
4108 pms[2 + psk_len + 0] = pms[0];
4109 pms[2 + psk_len + 1] = pms[1];
4110 memcpy( pms + 4 + psk_len, psk, psk_len );
4111
4112 status = psa_generator_tls12_prf_setup( tls12_prf,
4113 pms, 4 + 2 * psk_len,
4114 hash_alg,
4115 salt, salt_length,
4116 label, label_length );
4117
Gilles Peskine3f108122018-12-07 18:14:53 +01004118 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004119 return( status );
4120}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004121#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004122
Gilles Peskine346797d2018-11-16 16:05:06 +01004123/* Note that if this function fails, you must call psa_generator_abort()
4124 * to potentially free embedded data structures and wipe confidential data.
4125 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004126static psa_status_t psa_key_derivation_internal(
4127 psa_crypto_generator_t *generator,
4128 const uint8_t *secret, size_t secret_length,
4129 psa_algorithm_t alg,
4130 const uint8_t *salt, size_t salt_length,
4131 const uint8_t *label, size_t label_length,
4132 size_t capacity )
4133{
4134 psa_status_t status;
4135 size_t max_capacity;
4136
4137 /* Set generator->alg even on failure so that abort knows what to do. */
4138 generator->alg = alg;
4139
Gilles Peskine751d9652018-09-18 12:05:44 +02004140 if( alg == PSA_ALG_SELECT_RAW )
4141 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004142 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004143 if( salt_length != 0 )
4144 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004145 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004146 if( label_length != 0 )
4147 return( PSA_ERROR_INVALID_ARGUMENT );
4148 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4149 if( generator->ctx.buffer.data == NULL )
4150 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4151 memcpy( generator->ctx.buffer.data, secret, secret_length );
4152 generator->ctx.buffer.size = secret_length;
4153 max_capacity = secret_length;
4154 status = PSA_SUCCESS;
4155 }
4156 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004157#if defined(MBEDTLS_MD_C)
4158 if( PSA_ALG_IS_HKDF( alg ) )
4159 {
4160 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4161 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4162 if( hash_size == 0 )
4163 return( PSA_ERROR_NOT_SUPPORTED );
4164 max_capacity = 255 * hash_size;
4165 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4166 secret, secret_length,
4167 hash_alg,
4168 salt, salt_length,
4169 label, label_length );
4170 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004171 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4172 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4173 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004174 {
4175 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4176 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4177
4178 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4179 if( hash_alg != PSA_ALG_SHA_256 &&
4180 hash_alg != PSA_ALG_SHA_384 )
4181 {
4182 return( PSA_ERROR_NOT_SUPPORTED );
4183 }
4184
4185 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004186
4187 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4188 {
4189 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4190 secret, secret_length,
4191 hash_alg, salt, salt_length,
4192 label, label_length );
4193 }
4194 else
4195 {
4196 status = psa_generator_tls12_psk_to_ms_setup(
4197 &generator->ctx.tls12_prf,
4198 secret, secret_length,
4199 hash_alg, salt, salt_length,
4200 label, label_length );
4201 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004202 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004203 else
4204#endif
4205 {
4206 return( PSA_ERROR_NOT_SUPPORTED );
4207 }
4208
4209 if( status != PSA_SUCCESS )
4210 return( status );
4211
4212 if( capacity <= max_capacity )
4213 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004214 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4215 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004216 else
4217 return( PSA_ERROR_INVALID_ARGUMENT );
4218
4219 return( PSA_SUCCESS );
4220}
4221
Gilles Peskineea0fb492018-07-12 17:17:20 +02004222psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004223 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004224 psa_algorithm_t alg,
4225 const uint8_t *salt,
4226 size_t salt_length,
4227 const uint8_t *label,
4228 size_t label_length,
4229 size_t capacity )
4230{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004231 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004232 psa_status_t status;
4233
4234 if( generator->alg != 0 )
4235 return( PSA_ERROR_BAD_STATE );
4236
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004237 /* Make sure that alg is a key derivation algorithm. This prevents
4238 * key selection algorithms, which psa_key_derivation_internal
4239 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004240 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4241 return( PSA_ERROR_INVALID_ARGUMENT );
4242
Gilles Peskinec5487a82018-12-03 18:08:14 +01004243 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004244 if( status != PSA_SUCCESS )
4245 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004246
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004247 if( slot->type != PSA_KEY_TYPE_DERIVE )
4248 return( PSA_ERROR_INVALID_ARGUMENT );
4249
4250 status = psa_key_derivation_internal( generator,
4251 slot->data.raw.data,
4252 slot->data.raw.bytes,
4253 alg,
4254 salt, salt_length,
4255 label, label_length,
4256 capacity );
4257 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004258 psa_generator_abort( generator );
4259 return( status );
4260}
4261
4262
4263
4264/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004265/* Key agreement */
4266/****************************************************************/
4267
Gilles Peskinea05219c2018-11-16 16:02:56 +01004268#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004269static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4270 size_t peer_key_length,
4271 const mbedtls_ecp_keypair *our_key,
4272 uint8_t *shared_secret,
4273 size_t shared_secret_size,
4274 size_t *shared_secret_length )
4275{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004276 mbedtls_ecp_keypair *their_key = NULL;
4277 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004278 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004279 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004280
Jaeden Ameroccdce902019-01-10 11:42:27 +00004281 status = psa_import_ec_public_key(
4282 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4283 peer_key, peer_key_length,
4284 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004285 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004286 goto exit;
4287
Jaeden Amero97271b32019-01-10 19:38:51 +00004288 status = mbedtls_to_psa_error(
4289 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4290 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004291 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00004292 status = mbedtls_to_psa_error(
4293 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4294 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004295 goto exit;
4296
Jaeden Amero97271b32019-01-10 19:38:51 +00004297 status = mbedtls_to_psa_error(
4298 mbedtls_ecdh_calc_secret( &ecdh,
4299 shared_secret_length,
4300 shared_secret, shared_secret_size,
4301 mbedtls_ctr_drbg_random,
4302 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004303
4304exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004305 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00004306 mbedtls_ecp_keypair_free( their_key );
4307 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004308 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004309}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004310#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004311
Gilles Peskine01d718c2018-09-18 12:01:02 +02004312#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4313
Gilles Peskine346797d2018-11-16 16:05:06 +01004314/* Note that if this function fails, you must call psa_generator_abort()
4315 * to potentially free embedded data structures and wipe confidential data.
4316 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004317static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004318 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004319 const uint8_t *peer_key,
4320 size_t peer_key_length,
4321 psa_algorithm_t alg )
4322{
4323 psa_status_t status;
4324 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4325 size_t shared_secret_length = 0;
4326
4327 /* Step 1: run the secret agreement algorithm to generate the shared
4328 * secret. */
4329 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4330 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004331#if defined(MBEDTLS_ECDH_C)
4332 case PSA_ALG_ECDH_BASE:
4333 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4334 return( PSA_ERROR_INVALID_ARGUMENT );
4335 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4336 private_key->data.ecp,
4337 shared_secret,
4338 sizeof( shared_secret ),
4339 &shared_secret_length );
4340 break;
4341#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004342 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004343 (void) private_key;
4344 (void) peer_key;
4345 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004346 return( PSA_ERROR_NOT_SUPPORTED );
4347 }
4348 if( status != PSA_SUCCESS )
4349 goto exit;
4350
4351 /* Step 2: set up the key derivation to generate key material from
4352 * the shared secret. */
4353 status = psa_key_derivation_internal( generator,
4354 shared_secret, shared_secret_length,
4355 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4356 NULL, 0, NULL, 0,
4357 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4358exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004359 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004360 return( status );
4361}
4362
4363psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004364 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004365 const uint8_t *peer_key,
4366 size_t peer_key_length,
4367 psa_algorithm_t alg )
4368{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004369 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004370 psa_status_t status;
4371 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4372 return( PSA_ERROR_INVALID_ARGUMENT );
4373 status = psa_get_key_from_slot( private_key, &slot,
4374 PSA_KEY_USAGE_DERIVE, alg );
4375 if( status != PSA_SUCCESS )
4376 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004377 status = psa_key_agreement_internal( generator,
4378 slot,
4379 peer_key, peer_key_length,
4380 alg );
4381 if( status != PSA_SUCCESS )
4382 psa_generator_abort( generator );
4383 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004384}
4385
4386
4387
4388/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004389/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004390/****************************************************************/
4391
4392psa_status_t psa_generate_random( uint8_t *output,
4393 size_t output_size )
4394{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004395 int ret;
4396 GUARD_MODULE_INITIALIZED;
4397
4398 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004399 return( mbedtls_to_psa_error( ret ) );
4400}
4401
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004402#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004403
4404/* Support function for error conversion between psa_its error codes to psa crypto */
4405static psa_status_t its_to_psa_error( psa_its_status_t ret )
4406{
4407 switch( ret )
4408 {
4409 case PSA_ITS_SUCCESS:
4410 return( PSA_SUCCESS );
4411
Oren Cohen23a67842019-01-24 14:32:11 +02004412 case PSA_ITS_ERROR_UID_NOT_FOUND:
avolinski13beb102018-11-20 16:51:49 +02004413 return( PSA_ERROR_EMPTY_SLOT );
4414
4415 case PSA_ITS_ERROR_STORAGE_FAILURE:
4416 return( PSA_ERROR_STORAGE_FAILURE );
4417
4418 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4419 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4420
Jaeden Amero58600552018-11-30 12:04:38 +00004421 case PSA_ITS_ERROR_OFFSET_INVALID:
avolinski13beb102018-11-20 16:51:49 +02004422 case PSA_ITS_ERROR_INCORRECT_SIZE:
Oren Cohen23a67842019-01-24 14:32:11 +02004423 case PSA_ITS_ERROR_INVALID_ARGUMENTS:
avolinski13beb102018-11-20 16:51:49 +02004424 return( PSA_ERROR_INVALID_ARGUMENT );
4425
4426 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4427 return( PSA_ERROR_NOT_SUPPORTED );
4428
4429 case PSA_ITS_ERROR_WRITE_ONCE:
4430 return( PSA_ERROR_OCCUPIED_SLOT );
4431
4432 default:
4433 return( PSA_ERROR_UNKNOWN_ERROR );
4434 }
4435}
4436
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004437psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4438 size_t seed_size )
4439{
4440 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004441 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004442 struct psa_its_info_t p_info;
4443 if( global_data.initialized )
4444 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004445
4446 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4447 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4448 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4449 return( PSA_ERROR_INVALID_ARGUMENT );
4450
avolinski0d2c2662018-11-21 17:31:07 +02004451 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004452 status = its_to_psa_error( its_status );
4453
Oren Cohen23a67842019-01-24 14:32:11 +02004454 if( PSA_ITS_ERROR_UID_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004455 {
avolinski0d2c2662018-11-21 17:31:07 +02004456 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004457 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004458 }
avolinski13beb102018-11-20 16:51:49 +02004459 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004460 {
4461 /* You should not be here. Seed needs to be injected only once */
4462 status = PSA_ERROR_NOT_PERMITTED;
4463 }
4464 return( status );
4465}
4466#endif
4467
Gilles Peskinec5487a82018-12-03 18:08:14 +01004468psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004469 psa_key_type_t type,
4470 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004471 const void *extra,
4472 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004473{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004474 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004475 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004476
Gilles Peskine53d991e2018-07-12 01:14:59 +02004477 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004478 return( PSA_ERROR_INVALID_ARGUMENT );
4479
Gilles Peskinec5487a82018-12-03 18:08:14 +01004480 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004481 if( status != PSA_SUCCESS )
4482 return( status );
4483
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004484 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004485 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004486 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004487 if( status != PSA_SUCCESS )
4488 return( status );
4489 status = psa_generate_random( slot->data.raw.data,
4490 slot->data.raw.bytes );
4491 if( status != PSA_SUCCESS )
4492 {
4493 mbedtls_free( slot->data.raw.data );
4494 return( status );
4495 }
4496#if defined(MBEDTLS_DES_C)
4497 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004498 psa_des_set_key_parity( slot->data.raw.data,
4499 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004500#endif /* MBEDTLS_DES_C */
4501 }
4502 else
4503
4504#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4505 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4506 {
4507 mbedtls_rsa_context *rsa;
4508 int ret;
4509 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004510 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4511 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004512 /* Accept only byte-aligned keys, for the same reasons as
4513 * in psa_import_rsa_key(). */
4514 if( bits % 8 != 0 )
4515 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004516 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004517 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004518 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004519 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004520 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004521#if INT_MAX < 0xffffffff
4522 /* Check that the uint32_t value passed by the caller fits
4523 * in the range supported by this implementation. */
4524 if( p->e > INT_MAX )
4525 return( PSA_ERROR_NOT_SUPPORTED );
4526#endif
4527 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004528 }
4529 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4530 if( rsa == NULL )
4531 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4532 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4533 ret = mbedtls_rsa_gen_key( rsa,
4534 mbedtls_ctr_drbg_random,
4535 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004536 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004537 exponent );
4538 if( ret != 0 )
4539 {
4540 mbedtls_rsa_free( rsa );
4541 mbedtls_free( rsa );
4542 return( mbedtls_to_psa_error( ret ) );
4543 }
4544 slot->data.rsa = rsa;
4545 }
4546 else
4547#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4548
4549#if defined(MBEDTLS_ECP_C)
4550 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4551 {
4552 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4553 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4554 const mbedtls_ecp_curve_info *curve_info =
4555 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4556 mbedtls_ecp_keypair *ecp;
4557 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004558 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004559 return( PSA_ERROR_NOT_SUPPORTED );
4560 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4561 return( PSA_ERROR_NOT_SUPPORTED );
4562 if( curve_info->bit_size != bits )
4563 return( PSA_ERROR_INVALID_ARGUMENT );
4564 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4565 if( ecp == NULL )
4566 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4567 mbedtls_ecp_keypair_init( ecp );
4568 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4569 mbedtls_ctr_drbg_random,
4570 &global_data.ctr_drbg );
4571 if( ret != 0 )
4572 {
4573 mbedtls_ecp_keypair_free( ecp );
4574 mbedtls_free( ecp );
4575 return( mbedtls_to_psa_error( ret ) );
4576 }
4577 slot->data.ecp = ecp;
4578 }
4579 else
4580#endif /* MBEDTLS_ECP_C */
4581
4582 return( PSA_ERROR_NOT_SUPPORTED );
4583
4584 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004585
4586#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4587 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4588 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004589 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004590 }
4591#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4592
4593 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004594}
4595
4596
4597/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004598/* Module setup */
4599/****************************************************************/
4600
Gilles Peskine5e769522018-11-20 21:59:56 +01004601psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4602 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4603 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4604{
4605 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4606 return( PSA_ERROR_BAD_STATE );
4607 global_data.entropy_init = entropy_init;
4608 global_data.entropy_free = entropy_free;
4609 return( PSA_SUCCESS );
4610}
4611
Gilles Peskinee59236f2018-01-27 23:32:46 +01004612void mbedtls_psa_crypto_free( void )
4613{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004614 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004615 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4616 {
4617 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004618 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004619 }
4620 /* Wipe all remaining data, including configuration.
4621 * In particular, this sets all state indicator to the value
4622 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004623 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004624}
4625
4626psa_status_t psa_crypto_init( void )
4627{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004628 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004629 const unsigned char drbg_seed[] = "PSA";
4630
Gilles Peskinec6b69072018-11-20 21:42:52 +01004631 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004632 if( global_data.initialized != 0 )
4633 return( PSA_SUCCESS );
4634
Gilles Peskine5e769522018-11-20 21:59:56 +01004635 /* Set default configuration if
4636 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4637 if( global_data.entropy_init == NULL )
4638 global_data.entropy_init = mbedtls_entropy_init;
4639 if( global_data.entropy_free == NULL )
4640 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004641
Gilles Peskinec6b69072018-11-20 21:42:52 +01004642 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004643 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004644 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004645 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004646 status = mbedtls_to_psa_error(
4647 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4648 mbedtls_entropy_func,
4649 &global_data.entropy,
4650 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4651 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004652 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004653 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004654
Gilles Peskine66fb1262018-12-10 16:29:04 +01004655 status = psa_initialize_key_slots( );
4656 if( status != PSA_SUCCESS )
4657 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004658
4659 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004660 global_data.initialized = 1;
4661
Gilles Peskinee59236f2018-01-27 23:32:46 +01004662exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004663 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004664 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004665 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004666}
4667
4668#endif /* MBEDTLS_PSA_CRYPTO_C */