blob: d9d48708c30068dafd5e13c4b49d52dde5a85c29 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030029/*
itayzafrir14e76782019-01-16 11:16:39 +020030 * When MBEDTLS_PSA_CRYPTO_SPM is defined, the code is being built for SPM
31 * (Secure Partition Manager) integration which separates the code into two
32 * parts: NSPE (Non-Secure Processing Environment) and SPE (Secure Processing
33 * Environment). When building for the SPE, an additional header file should be
34 * included.
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030035 */
mohammad160327010052018-07-03 13:16:15 +030036#if defined(MBEDTLS_PSA_CRYPTO_SPM)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030037/*
itayzafrir14e76782019-01-16 11:16:39 +020038 * PSA_CRYPTO_SECURE means that this file is compiled for the SPE.
39 * Some headers will be affected by this flag.
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030040 */
mohammad160327010052018-07-03 13:16:15 +030041#define PSA_CRYPTO_SECURE 1
42#include "crypto_spe.h"
43#endif
44
Gilles Peskinee59236f2018-01-27 23:32:46 +010045#include "psa/crypto.h"
46
Gilles Peskine039b90c2018-12-07 18:24:41 +010047#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010048#include "psa_crypto_invasive.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010049#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010050/* Include internal declarations that are useful for implementing persistently
51 * stored keys. */
52#include "psa_crypto_storage.h"
53
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010054#include <stdlib.h>
55#include <string.h>
56#if defined(MBEDTLS_PLATFORM_C)
57#include "mbedtls/platform.h"
58#else
59#define mbedtls_calloc calloc
60#define mbedtls_free free
61#endif
62
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020064#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000065#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020066#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/blowfish.h"
68#include "mbedtls/camellia.h"
69#include "mbedtls/cipher.h"
70#include "mbedtls/ccm.h"
71#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010072#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020074#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010076#include "mbedtls/entropy.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020077#include "mbedtls/entropy_poll.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/error.h"
79#include "mbedtls/gcm.h"
80#include "mbedtls/md2.h"
81#include "mbedtls/md4.h"
82#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010083#include "mbedtls/md.h"
84#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010085#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010086#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010087#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010088#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010089#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010090#include "mbedtls/sha1.h"
91#include "mbedtls/sha256.h"
92#include "mbedtls/sha512.h"
93#include "mbedtls/xtea.h"
94
Netanel Gonen2bcd3122018-11-19 11:53:02 +020095#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
Oren Cohen23a67842019-01-24 14:32:11 +020096#include "psa/internal_trusted_storage.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020097#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +010098
Gilles Peskine996deb12018-08-01 15:45:45 +020099#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
100
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100101/* constant-time buffer comparison */
102static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
103{
104 size_t i;
105 unsigned char diff = 0;
106
107 for( i = 0; i < n; i++ )
108 diff |= a[i] ^ b[i];
109
110 return( diff );
111}
112
113
114
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100115/****************************************************************/
116/* Global data, support functions and library management */
117/****************************************************************/
118
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200119static int key_type_is_raw_bytes( psa_key_type_t type )
120{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200121 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200122}
123
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100124/* Values for psa_global_data_t::rng_state */
125#define RNG_NOT_INITIALIZED 0
126#define RNG_INITIALIZED 1
127#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100128
Gilles Peskine2d277862018-06-18 15:41:12 +0200129typedef struct
130{
Gilles Peskine5e769522018-11-20 21:59:56 +0100131 void (* entropy_init )( mbedtls_entropy_context *ctx );
132 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100133 mbedtls_entropy_context entropy;
134 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100135 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100136 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100137} psa_global_data_t;
138
139static psa_global_data_t global_data;
140
itayzafrir0adf0fc2018-09-06 16:24:41 +0300141#define GUARD_MODULE_INITIALIZED \
142 if( global_data.initialized == 0 ) \
143 return( PSA_ERROR_BAD_STATE );
144
Gilles Peskinee59236f2018-01-27 23:32:46 +0100145static psa_status_t mbedtls_to_psa_error( int ret )
146{
Gilles Peskinea5905292018-02-07 20:59:33 +0100147 /* If there's both a high-level code and low-level code, dispatch on
148 * the high-level code. */
149 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100150 {
151 case 0:
152 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100153
154 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
155 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
156 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
157 return( PSA_ERROR_NOT_SUPPORTED );
158 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
159 return( PSA_ERROR_HARDWARE_FAILURE );
160
161 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
162 return( PSA_ERROR_HARDWARE_FAILURE );
163
Gilles Peskine9a944802018-06-21 09:35:35 +0200164 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
165 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
166 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
167 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
168 case MBEDTLS_ERR_ASN1_INVALID_DATA:
169 return( PSA_ERROR_INVALID_ARGUMENT );
170 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
171 return( PSA_ERROR_INSUFFICIENT_MEMORY );
172 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
173 return( PSA_ERROR_BUFFER_TOO_SMALL );
174
Gilles Peskinea5905292018-02-07 20:59:33 +0100175 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
176 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
181 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
182 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
183 return( PSA_ERROR_NOT_SUPPORTED );
184 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
185 return( PSA_ERROR_HARDWARE_FAILURE );
186
187 case MBEDTLS_ERR_CCM_BAD_INPUT:
188 return( PSA_ERROR_INVALID_ARGUMENT );
189 case MBEDTLS_ERR_CCM_AUTH_FAILED:
190 return( PSA_ERROR_INVALID_SIGNATURE );
191 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
192 return( PSA_ERROR_HARDWARE_FAILURE );
193
194 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
195 return( PSA_ERROR_NOT_SUPPORTED );
196 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
197 return( PSA_ERROR_INVALID_ARGUMENT );
198 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
199 return( PSA_ERROR_INSUFFICIENT_MEMORY );
200 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
201 return( PSA_ERROR_INVALID_PADDING );
202 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
203 return( PSA_ERROR_BAD_STATE );
204 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
205 return( PSA_ERROR_INVALID_SIGNATURE );
206 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
207 return( PSA_ERROR_TAMPERING_DETECTED );
208 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
209 return( PSA_ERROR_HARDWARE_FAILURE );
210
211 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
214 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
215 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
216 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
217 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
218 return( PSA_ERROR_NOT_SUPPORTED );
219 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
220 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
221
222 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
223 return( PSA_ERROR_NOT_SUPPORTED );
224 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
225 return( PSA_ERROR_HARDWARE_FAILURE );
226
Gilles Peskinee59236f2018-01-27 23:32:46 +0100227 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
228 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
229 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
230 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100231
232 case MBEDTLS_ERR_GCM_AUTH_FAILED:
233 return( PSA_ERROR_INVALID_SIGNATURE );
234 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200235 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100236 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
237 return( PSA_ERROR_HARDWARE_FAILURE );
238
239 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
240 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
241 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
244 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
245 return( PSA_ERROR_NOT_SUPPORTED );
246 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
247 return( PSA_ERROR_INVALID_ARGUMENT );
248 case MBEDTLS_ERR_MD_ALLOC_FAILED:
249 return( PSA_ERROR_INSUFFICIENT_MEMORY );
250 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
251 return( PSA_ERROR_STORAGE_FAILURE );
252 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
253 return( PSA_ERROR_HARDWARE_FAILURE );
254
Gilles Peskinef76aa772018-10-29 19:24:33 +0100255 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
256 return( PSA_ERROR_STORAGE_FAILURE );
257 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
258 return( PSA_ERROR_INVALID_ARGUMENT );
259 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
262 return( PSA_ERROR_BUFFER_TOO_SMALL );
263 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
264 return( PSA_ERROR_INVALID_ARGUMENT );
265 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
270 return( PSA_ERROR_INSUFFICIENT_MEMORY );
271
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100272 case MBEDTLS_ERR_PK_ALLOC_FAILED:
273 return( PSA_ERROR_INSUFFICIENT_MEMORY );
274 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
275 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
276 return( PSA_ERROR_INVALID_ARGUMENT );
277 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100278 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100279 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
280 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
281 return( PSA_ERROR_INVALID_ARGUMENT );
282 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
283 return( PSA_ERROR_NOT_SUPPORTED );
284 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
285 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
286 return( PSA_ERROR_NOT_PERMITTED );
287 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
288 return( PSA_ERROR_INVALID_ARGUMENT );
289 case MBEDTLS_ERR_PK_INVALID_ALG:
290 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
291 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
292 return( PSA_ERROR_NOT_SUPPORTED );
293 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
294 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100295 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
296 return( PSA_ERROR_HARDWARE_FAILURE );
297
298 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
299 return( PSA_ERROR_HARDWARE_FAILURE );
300
301 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
302 return( PSA_ERROR_INVALID_ARGUMENT );
303 case MBEDTLS_ERR_RSA_INVALID_PADDING:
304 return( PSA_ERROR_INVALID_PADDING );
305 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
306 return( PSA_ERROR_HARDWARE_FAILURE );
307 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
310 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
311 return( PSA_ERROR_TAMPERING_DETECTED );
312 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
313 return( PSA_ERROR_INVALID_SIGNATURE );
314 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
315 return( PSA_ERROR_BUFFER_TOO_SMALL );
316 case MBEDTLS_ERR_RSA_RNG_FAILED:
317 return( PSA_ERROR_INSUFFICIENT_MEMORY );
318 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
319 return( PSA_ERROR_NOT_SUPPORTED );
320 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
321 return( PSA_ERROR_HARDWARE_FAILURE );
322
323 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
324 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
325 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327
328 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
329 return( PSA_ERROR_INVALID_ARGUMENT );
330 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
331 return( PSA_ERROR_HARDWARE_FAILURE );
332
itayzafrir5c753392018-05-08 11:18:38 +0300333 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300334 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300335 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300336 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
337 return( PSA_ERROR_BUFFER_TOO_SMALL );
338 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
339 return( PSA_ERROR_NOT_SUPPORTED );
340 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
341 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
342 return( PSA_ERROR_INVALID_SIGNATURE );
343 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
344 return( PSA_ERROR_INSUFFICIENT_MEMORY );
345 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
346 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300347
Gilles Peskinee59236f2018-01-27 23:32:46 +0100348 default:
349 return( PSA_ERROR_UNKNOWN_ERROR );
350 }
351}
352
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200353
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200354
355
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100356/****************************************************************/
357/* Key management */
358/****************************************************************/
359
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100360#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200361static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
362{
363 switch( grpid )
364 {
365 case MBEDTLS_ECP_DP_SECP192R1:
366 return( PSA_ECC_CURVE_SECP192R1 );
367 case MBEDTLS_ECP_DP_SECP224R1:
368 return( PSA_ECC_CURVE_SECP224R1 );
369 case MBEDTLS_ECP_DP_SECP256R1:
370 return( PSA_ECC_CURVE_SECP256R1 );
371 case MBEDTLS_ECP_DP_SECP384R1:
372 return( PSA_ECC_CURVE_SECP384R1 );
373 case MBEDTLS_ECP_DP_SECP521R1:
374 return( PSA_ECC_CURVE_SECP521R1 );
375 case MBEDTLS_ECP_DP_BP256R1:
376 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
377 case MBEDTLS_ECP_DP_BP384R1:
378 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
379 case MBEDTLS_ECP_DP_BP512R1:
380 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
381 case MBEDTLS_ECP_DP_CURVE25519:
382 return( PSA_ECC_CURVE_CURVE25519 );
383 case MBEDTLS_ECP_DP_SECP192K1:
384 return( PSA_ECC_CURVE_SECP192K1 );
385 case MBEDTLS_ECP_DP_SECP224K1:
386 return( PSA_ECC_CURVE_SECP224K1 );
387 case MBEDTLS_ECP_DP_SECP256K1:
388 return( PSA_ECC_CURVE_SECP256K1 );
389 case MBEDTLS_ECP_DP_CURVE448:
390 return( PSA_ECC_CURVE_CURVE448 );
391 default:
392 return( 0 );
393 }
394}
395
Gilles Peskine12313cd2018-06-20 00:20:32 +0200396static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
397{
398 switch( curve )
399 {
400 case PSA_ECC_CURVE_SECP192R1:
401 return( MBEDTLS_ECP_DP_SECP192R1 );
402 case PSA_ECC_CURVE_SECP224R1:
403 return( MBEDTLS_ECP_DP_SECP224R1 );
404 case PSA_ECC_CURVE_SECP256R1:
405 return( MBEDTLS_ECP_DP_SECP256R1 );
406 case PSA_ECC_CURVE_SECP384R1:
407 return( MBEDTLS_ECP_DP_SECP384R1 );
408 case PSA_ECC_CURVE_SECP521R1:
409 return( MBEDTLS_ECP_DP_SECP521R1 );
410 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
411 return( MBEDTLS_ECP_DP_BP256R1 );
412 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
413 return( MBEDTLS_ECP_DP_BP384R1 );
414 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
415 return( MBEDTLS_ECP_DP_BP512R1 );
416 case PSA_ECC_CURVE_CURVE25519:
417 return( MBEDTLS_ECP_DP_CURVE25519 );
418 case PSA_ECC_CURVE_SECP192K1:
419 return( MBEDTLS_ECP_DP_SECP192K1 );
420 case PSA_ECC_CURVE_SECP224K1:
421 return( MBEDTLS_ECP_DP_SECP224K1 );
422 case PSA_ECC_CURVE_SECP256K1:
423 return( MBEDTLS_ECP_DP_SECP256K1 );
424 case PSA_ECC_CURVE_CURVE448:
425 return( MBEDTLS_ECP_DP_CURVE448 );
426 default:
427 return( MBEDTLS_ECP_DP_NONE );
428 }
429}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100430#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200431
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200432static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
433 size_t bits,
434 struct raw_data *raw )
435{
436 /* Check that the bit size is acceptable for the key type */
437 switch( type )
438 {
439 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200440 if( bits == 0 )
441 {
442 raw->bytes = 0;
443 raw->data = NULL;
444 return( PSA_SUCCESS );
445 }
446 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200447#if defined(MBEDTLS_MD_C)
448 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200449#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200450 case PSA_KEY_TYPE_DERIVE:
451 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200452#if defined(MBEDTLS_AES_C)
453 case PSA_KEY_TYPE_AES:
454 if( bits != 128 && bits != 192 && bits != 256 )
455 return( PSA_ERROR_INVALID_ARGUMENT );
456 break;
457#endif
458#if defined(MBEDTLS_CAMELLIA_C)
459 case PSA_KEY_TYPE_CAMELLIA:
460 if( bits != 128 && bits != 192 && bits != 256 )
461 return( PSA_ERROR_INVALID_ARGUMENT );
462 break;
463#endif
464#if defined(MBEDTLS_DES_C)
465 case PSA_KEY_TYPE_DES:
466 if( bits != 64 && bits != 128 && bits != 192 )
467 return( PSA_ERROR_INVALID_ARGUMENT );
468 break;
469#endif
470#if defined(MBEDTLS_ARC4_C)
471 case PSA_KEY_TYPE_ARC4:
472 if( bits < 8 || bits > 2048 )
473 return( PSA_ERROR_INVALID_ARGUMENT );
474 break;
475#endif
476 default:
477 return( PSA_ERROR_NOT_SUPPORTED );
478 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200479 if( bits % 8 != 0 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200481
482 /* Allocate memory for the key */
483 raw->bytes = PSA_BITS_TO_BYTES( bits );
484 raw->data = mbedtls_calloc( 1, raw->bytes );
485 if( raw->data == NULL )
486 {
487 raw->bytes = 0;
488 return( PSA_ERROR_INSUFFICIENT_MEMORY );
489 }
490 return( PSA_SUCCESS );
491}
492
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200493#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100494/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
495 * that are not a multiple of 8) well. For example, there is only
496 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
497 * way to return the exact bit size of a key.
498 * To keep things simple, reject non-byte-aligned key sizes. */
499static psa_status_t psa_check_rsa_key_byte_aligned(
500 const mbedtls_rsa_context *rsa )
501{
502 mbedtls_mpi n;
503 psa_status_t status;
504 mbedtls_mpi_init( &n );
505 status = mbedtls_to_psa_error(
506 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
507 if( status == PSA_SUCCESS )
508 {
509 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
510 status = PSA_ERROR_NOT_SUPPORTED;
511 }
512 mbedtls_mpi_free( &n );
513 return( status );
514}
515
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000516static psa_status_t psa_import_rsa_key( psa_key_type_t type,
517 const uint8_t *data,
518 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200519 mbedtls_rsa_context **p_rsa )
520{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000521 psa_status_t status;
522 mbedtls_pk_context pk;
523 mbedtls_rsa_context *rsa;
524 size_t bits;
525
526 mbedtls_pk_init( &pk );
527
528 /* Parse the data. */
529 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
530 status = mbedtls_to_psa_error(
531 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200532 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000533 status = mbedtls_to_psa_error(
534 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
535 if( status != PSA_SUCCESS )
536 goto exit;
537
538 /* We have something that the pkparse module recognizes. If it is a
539 * valid RSA key, store it. */
540 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200541 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000542 status = PSA_ERROR_INVALID_ARGUMENT;
543 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200544 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000545
546 rsa = mbedtls_pk_rsa( pk );
547 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
548 * supports non-byte-aligned key sizes, but not well. For example,
549 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
550 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
551 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
552 {
553 status = PSA_ERROR_NOT_SUPPORTED;
554 goto exit;
555 }
556 status = psa_check_rsa_key_byte_aligned( rsa );
557
558exit:
559 /* Free the content of the pk object only on error. */
560 if( status != PSA_SUCCESS )
561 {
562 mbedtls_pk_free( &pk );
563 return( status );
564 }
565
566 /* On success, store the content of the object in the RSA context. */
567 *p_rsa = rsa;
568
569 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200570}
571#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
572
Jaeden Ameroccdce902019-01-10 11:42:27 +0000573#if defined(MBEDTLS_ECP_C)
574
575/* Import a public key given as the uncompressed representation defined by SEC1
576 * 2.3.3 as the content of an ECPoint. */
577static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
578 const uint8_t *data,
579 size_t data_length,
580 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200581{
Jaeden Ameroccdce902019-01-10 11:42:27 +0000582 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
583 mbedtls_ecp_keypair *ecp = NULL;
584 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
585
586 *p_ecp = NULL;
587 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
588 if( ecp == NULL )
589 return( PSA_ERROR_INSUFFICIENT_MEMORY );
590 mbedtls_ecp_keypair_init( ecp );
591
592 /* Load the group. */
593 status = mbedtls_to_psa_error(
594 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
595 if( status != PSA_SUCCESS )
596 goto exit;
597 /* Load the public value. */
598 status = mbedtls_to_psa_error(
599 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
600 data, data_length ) );
601 if( status != PSA_SUCCESS )
602 goto exit;
603
604 /* Check that the point is on the curve. */
605 status = mbedtls_to_psa_error(
606 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
607 if( status != PSA_SUCCESS )
608 goto exit;
609
610 *p_ecp = ecp;
611 return( PSA_SUCCESS );
612
613exit:
614 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200615 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000616 mbedtls_ecp_keypair_free( ecp );
617 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200618 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000619 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200620}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000621#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200622
Gilles Peskinef76aa772018-10-29 19:24:33 +0100623#if defined(MBEDTLS_ECP_C)
624/* Import a private key given as a byte string which is the private value
625 * in big-endian order. */
626static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
627 const uint8_t *data,
628 size_t data_length,
629 mbedtls_ecp_keypair **p_ecp )
630{
631 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
632 mbedtls_ecp_keypair *ecp = NULL;
633 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
634
635 *p_ecp = NULL;
636 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
637 if( ecp == NULL )
638 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000639 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100640
641 /* Load the group. */
642 status = mbedtls_to_psa_error(
643 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
644 if( status != PSA_SUCCESS )
645 goto exit;
646 /* Load the secret value. */
647 status = mbedtls_to_psa_error(
648 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
649 if( status != PSA_SUCCESS )
650 goto exit;
651 /* Validate the private key. */
652 status = mbedtls_to_psa_error(
653 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
654 if( status != PSA_SUCCESS )
655 goto exit;
656 /* Calculate the public key from the private key. */
657 status = mbedtls_to_psa_error(
658 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
659 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
660 if( status != PSA_SUCCESS )
661 goto exit;
662
663 *p_ecp = ecp;
664 return( PSA_SUCCESS );
665
666exit:
667 if( ecp != NULL )
668 {
669 mbedtls_ecp_keypair_free( ecp );
670 mbedtls_free( ecp );
671 }
672 return( status );
673}
674#endif /* defined(MBEDTLS_ECP_C) */
675
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100676/** Import key data into a slot. `slot->type` must have been set
677 * previously. This function assumes that the slot does not contain
678 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100679psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
680 const uint8_t *data,
681 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100682{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200683 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100684
Darryl Green940d72c2018-07-13 13:18:51 +0100685 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100686 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100687 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100688 if( data_length > SIZE_MAX / 8 )
689 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100690 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200691 PSA_BYTES_TO_BITS( data_length ),
692 &slot->data.raw );
693 if( status != PSA_SUCCESS )
694 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200695 if( data_length != 0 )
696 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100697 }
698 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100699#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100700 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100701 {
Darryl Green940d72c2018-07-13 13:18:51 +0100702 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100703 data, data_length,
704 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100705 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000706 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100707 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000708 status = psa_import_ec_public_key(
709 PSA_KEY_TYPE_GET_CURVE( slot->type ),
710 data, data_length,
711 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100712 }
713 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100714#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000715#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
716 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100717 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000718 status = psa_import_rsa_key( slot->type,
719 data, data_length,
720 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100721 }
722 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000723#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100724 {
725 return( PSA_ERROR_NOT_SUPPORTED );
726 }
Jaeden Ameroc67200d2019-01-14 13:12:39 +0000727 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100728}
729
Darryl Green06fd18d2018-07-16 11:21:11 +0100730/* Retrieve an empty key slot (slot with no key data, but possibly
731 * with some metadata such as a policy). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100732static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100733 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100734{
735 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100736 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100737
738 *p_slot = NULL;
739
Gilles Peskinec5487a82018-12-03 18:08:14 +0100740 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100741 if( status != PSA_SUCCESS )
742 return( status );
743
744 if( slot->type != PSA_KEY_TYPE_NONE )
745 return( PSA_ERROR_OCCUPIED_SLOT );
746
747 *p_slot = slot;
748 return( status );
749}
750
Gilles 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 );
1230 if( buffer == NULL )
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:
Gilles Peskine122d0022019-01-23 10:55:43 +01001238 mbedtls_platform_zeroize( buffer, buffer_size );
1239 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001240 return( status );
1241}
1242
1243psa_status_t psa_copy_key(psa_key_handle_t source_handle,
1244 psa_key_handle_t target_handle,
1245 const psa_key_policy_t *constraint)
1246{
1247 psa_key_slot_t *source_slot = NULL;
1248 psa_key_slot_t *target_slot = NULL;
1249 psa_key_policy_t new_policy;
1250 psa_status_t status;
1251 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1252 if( status != PSA_SUCCESS )
1253 return( status );
1254 status = psa_get_empty_key_slot( target_handle, &target_slot );
1255 if( status != PSA_SUCCESS )
1256 return( status );
1257
1258 new_policy = target_slot->policy;
1259 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1260 if( status != PSA_SUCCESS )
1261 return( status );
1262 if( constraint != NULL )
1263 {
1264 status = psa_restrict_key_policy( &new_policy, constraint );
1265 if( status != PSA_SUCCESS )
1266 return( status );
1267 }
1268
1269 status = psa_copy_key_material( source_slot, target_handle );
1270 if( status != PSA_SUCCESS )
1271 return( status );
1272
1273 target_slot->policy = new_policy;
1274 return( PSA_SUCCESS );
1275}
1276
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001277
1278
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001279/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001280/* Message digests */
1281/****************************************************************/
1282
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001283static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001284{
1285 switch( alg )
1286 {
1287#if defined(MBEDTLS_MD2_C)
1288 case PSA_ALG_MD2:
1289 return( &mbedtls_md2_info );
1290#endif
1291#if defined(MBEDTLS_MD4_C)
1292 case PSA_ALG_MD4:
1293 return( &mbedtls_md4_info );
1294#endif
1295#if defined(MBEDTLS_MD5_C)
1296 case PSA_ALG_MD5:
1297 return( &mbedtls_md5_info );
1298#endif
1299#if defined(MBEDTLS_RIPEMD160_C)
1300 case PSA_ALG_RIPEMD160:
1301 return( &mbedtls_ripemd160_info );
1302#endif
1303#if defined(MBEDTLS_SHA1_C)
1304 case PSA_ALG_SHA_1:
1305 return( &mbedtls_sha1_info );
1306#endif
1307#if defined(MBEDTLS_SHA256_C)
1308 case PSA_ALG_SHA_224:
1309 return( &mbedtls_sha224_info );
1310 case PSA_ALG_SHA_256:
1311 return( &mbedtls_sha256_info );
1312#endif
1313#if defined(MBEDTLS_SHA512_C)
1314 case PSA_ALG_SHA_384:
1315 return( &mbedtls_sha384_info );
1316 case PSA_ALG_SHA_512:
1317 return( &mbedtls_sha512_info );
1318#endif
1319 default:
1320 return( NULL );
1321 }
1322}
1323
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001324psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1325{
1326 switch( operation->alg )
1327 {
Gilles Peskine81736312018-06-26 15:04:31 +02001328 case 0:
1329 /* The object has (apparently) been initialized but it is not
1330 * in use. It's ok to call abort on such an object, and there's
1331 * nothing to do. */
1332 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001333#if defined(MBEDTLS_MD2_C)
1334 case PSA_ALG_MD2:
1335 mbedtls_md2_free( &operation->ctx.md2 );
1336 break;
1337#endif
1338#if defined(MBEDTLS_MD4_C)
1339 case PSA_ALG_MD4:
1340 mbedtls_md4_free( &operation->ctx.md4 );
1341 break;
1342#endif
1343#if defined(MBEDTLS_MD5_C)
1344 case PSA_ALG_MD5:
1345 mbedtls_md5_free( &operation->ctx.md5 );
1346 break;
1347#endif
1348#if defined(MBEDTLS_RIPEMD160_C)
1349 case PSA_ALG_RIPEMD160:
1350 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1351 break;
1352#endif
1353#if defined(MBEDTLS_SHA1_C)
1354 case PSA_ALG_SHA_1:
1355 mbedtls_sha1_free( &operation->ctx.sha1 );
1356 break;
1357#endif
1358#if defined(MBEDTLS_SHA256_C)
1359 case PSA_ALG_SHA_224:
1360 case PSA_ALG_SHA_256:
1361 mbedtls_sha256_free( &operation->ctx.sha256 );
1362 break;
1363#endif
1364#if defined(MBEDTLS_SHA512_C)
1365 case PSA_ALG_SHA_384:
1366 case PSA_ALG_SHA_512:
1367 mbedtls_sha512_free( &operation->ctx.sha512 );
1368 break;
1369#endif
1370 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001371 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001372 }
1373 operation->alg = 0;
1374 return( PSA_SUCCESS );
1375}
1376
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001377psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001378 psa_algorithm_t alg )
1379{
1380 int ret;
1381 operation->alg = 0;
1382 switch( alg )
1383 {
1384#if defined(MBEDTLS_MD2_C)
1385 case PSA_ALG_MD2:
1386 mbedtls_md2_init( &operation->ctx.md2 );
1387 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1388 break;
1389#endif
1390#if defined(MBEDTLS_MD4_C)
1391 case PSA_ALG_MD4:
1392 mbedtls_md4_init( &operation->ctx.md4 );
1393 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1394 break;
1395#endif
1396#if defined(MBEDTLS_MD5_C)
1397 case PSA_ALG_MD5:
1398 mbedtls_md5_init( &operation->ctx.md5 );
1399 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1400 break;
1401#endif
1402#if defined(MBEDTLS_RIPEMD160_C)
1403 case PSA_ALG_RIPEMD160:
1404 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1405 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1406 break;
1407#endif
1408#if defined(MBEDTLS_SHA1_C)
1409 case PSA_ALG_SHA_1:
1410 mbedtls_sha1_init( &operation->ctx.sha1 );
1411 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1412 break;
1413#endif
1414#if defined(MBEDTLS_SHA256_C)
1415 case PSA_ALG_SHA_224:
1416 mbedtls_sha256_init( &operation->ctx.sha256 );
1417 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1418 break;
1419 case PSA_ALG_SHA_256:
1420 mbedtls_sha256_init( &operation->ctx.sha256 );
1421 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1422 break;
1423#endif
1424#if defined(MBEDTLS_SHA512_C)
1425 case PSA_ALG_SHA_384:
1426 mbedtls_sha512_init( &operation->ctx.sha512 );
1427 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1428 break;
1429 case PSA_ALG_SHA_512:
1430 mbedtls_sha512_init( &operation->ctx.sha512 );
1431 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1432 break;
1433#endif
1434 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001435 return( PSA_ALG_IS_HASH( alg ) ?
1436 PSA_ERROR_NOT_SUPPORTED :
1437 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001438 }
1439 if( ret == 0 )
1440 operation->alg = alg;
1441 else
1442 psa_hash_abort( operation );
1443 return( mbedtls_to_psa_error( ret ) );
1444}
1445
1446psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1447 const uint8_t *input,
1448 size_t input_length )
1449{
1450 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001451
1452 /* Don't require hash implementations to behave correctly on a
1453 * zero-length input, which may have an invalid pointer. */
1454 if( input_length == 0 )
1455 return( PSA_SUCCESS );
1456
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001457 switch( operation->alg )
1458 {
1459#if defined(MBEDTLS_MD2_C)
1460 case PSA_ALG_MD2:
1461 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1462 input, input_length );
1463 break;
1464#endif
1465#if defined(MBEDTLS_MD4_C)
1466 case PSA_ALG_MD4:
1467 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1468 input, input_length );
1469 break;
1470#endif
1471#if defined(MBEDTLS_MD5_C)
1472 case PSA_ALG_MD5:
1473 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1474 input, input_length );
1475 break;
1476#endif
1477#if defined(MBEDTLS_RIPEMD160_C)
1478 case PSA_ALG_RIPEMD160:
1479 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1480 input, input_length );
1481 break;
1482#endif
1483#if defined(MBEDTLS_SHA1_C)
1484 case PSA_ALG_SHA_1:
1485 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1486 input, input_length );
1487 break;
1488#endif
1489#if defined(MBEDTLS_SHA256_C)
1490 case PSA_ALG_SHA_224:
1491 case PSA_ALG_SHA_256:
1492 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1493 input, input_length );
1494 break;
1495#endif
1496#if defined(MBEDTLS_SHA512_C)
1497 case PSA_ALG_SHA_384:
1498 case PSA_ALG_SHA_512:
1499 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1500 input, input_length );
1501 break;
1502#endif
1503 default:
1504 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1505 break;
1506 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001507
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001508 if( ret != 0 )
1509 psa_hash_abort( operation );
1510 return( mbedtls_to_psa_error( ret ) );
1511}
1512
1513psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1514 uint8_t *hash,
1515 size_t hash_size,
1516 size_t *hash_length )
1517{
itayzafrir40835d42018-08-02 13:14:17 +03001518 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001519 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001520 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001521
1522 /* Fill the output buffer with something that isn't a valid hash
1523 * (barring an attack on the hash and deliberately-crafted input),
1524 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001525 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001526 /* If hash_size is 0 then hash may be NULL and then the
1527 * call to memset would have undefined behavior. */
1528 if( hash_size != 0 )
1529 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001530
1531 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001532 {
1533 status = PSA_ERROR_BUFFER_TOO_SMALL;
1534 goto exit;
1535 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001536
1537 switch( operation->alg )
1538 {
1539#if defined(MBEDTLS_MD2_C)
1540 case PSA_ALG_MD2:
1541 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1542 break;
1543#endif
1544#if defined(MBEDTLS_MD4_C)
1545 case PSA_ALG_MD4:
1546 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1547 break;
1548#endif
1549#if defined(MBEDTLS_MD5_C)
1550 case PSA_ALG_MD5:
1551 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1552 break;
1553#endif
1554#if defined(MBEDTLS_RIPEMD160_C)
1555 case PSA_ALG_RIPEMD160:
1556 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1557 break;
1558#endif
1559#if defined(MBEDTLS_SHA1_C)
1560 case PSA_ALG_SHA_1:
1561 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1562 break;
1563#endif
1564#if defined(MBEDTLS_SHA256_C)
1565 case PSA_ALG_SHA_224:
1566 case PSA_ALG_SHA_256:
1567 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1568 break;
1569#endif
1570#if defined(MBEDTLS_SHA512_C)
1571 case PSA_ALG_SHA_384:
1572 case PSA_ALG_SHA_512:
1573 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1574 break;
1575#endif
1576 default:
1577 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1578 break;
1579 }
itayzafrir40835d42018-08-02 13:14:17 +03001580 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001581
itayzafrir40835d42018-08-02 13:14:17 +03001582exit:
1583 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001584 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001585 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001586 return( psa_hash_abort( operation ) );
1587 }
1588 else
1589 {
1590 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001591 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001592 }
1593}
1594
Gilles Peskine2d277862018-06-18 15:41:12 +02001595psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1596 const uint8_t *hash,
1597 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001598{
1599 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1600 size_t actual_hash_length;
1601 psa_status_t status = psa_hash_finish( operation,
1602 actual_hash, sizeof( actual_hash ),
1603 &actual_hash_length );
1604 if( status != PSA_SUCCESS )
1605 return( status );
1606 if( actual_hash_length != hash_length )
1607 return( PSA_ERROR_INVALID_SIGNATURE );
1608 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1609 return( PSA_ERROR_INVALID_SIGNATURE );
1610 return( PSA_SUCCESS );
1611}
1612
Gilles Peskineeb35d782019-01-22 17:56:16 +01001613psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1614 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001615{
1616 if( target_operation->alg != 0 )
1617 return( PSA_ERROR_BAD_STATE );
1618
1619 switch( source_operation->alg )
1620 {
1621 case 0:
1622 return( PSA_ERROR_BAD_STATE );
1623#if defined(MBEDTLS_MD2_C)
1624 case PSA_ALG_MD2:
1625 mbedtls_md2_clone( &target_operation->ctx.md2,
1626 &source_operation->ctx.md2 );
1627 break;
1628#endif
1629#if defined(MBEDTLS_MD4_C)
1630 case PSA_ALG_MD4:
1631 mbedtls_md4_clone( &target_operation->ctx.md4,
1632 &source_operation->ctx.md4 );
1633 break;
1634#endif
1635#if defined(MBEDTLS_MD5_C)
1636 case PSA_ALG_MD5:
1637 mbedtls_md5_clone( &target_operation->ctx.md5,
1638 &source_operation->ctx.md5 );
1639 break;
1640#endif
1641#if defined(MBEDTLS_RIPEMD160_C)
1642 case PSA_ALG_RIPEMD160:
1643 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1644 &source_operation->ctx.ripemd160 );
1645 break;
1646#endif
1647#if defined(MBEDTLS_SHA1_C)
1648 case PSA_ALG_SHA_1:
1649 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1650 &source_operation->ctx.sha1 );
1651 break;
1652#endif
1653#if defined(MBEDTLS_SHA256_C)
1654 case PSA_ALG_SHA_224:
1655 case PSA_ALG_SHA_256:
1656 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1657 &source_operation->ctx.sha256 );
1658 break;
1659#endif
1660#if defined(MBEDTLS_SHA512_C)
1661 case PSA_ALG_SHA_384:
1662 case PSA_ALG_SHA_512:
1663 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1664 &source_operation->ctx.sha512 );
1665 break;
1666#endif
1667 default:
1668 return( PSA_ERROR_NOT_SUPPORTED );
1669 }
1670
1671 target_operation->alg = source_operation->alg;
1672 return( PSA_SUCCESS );
1673}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001674
1675
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001676/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001677/* MAC */
1678/****************************************************************/
1679
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001680static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001681 psa_algorithm_t alg,
1682 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001683 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001684 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001685{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001686 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001687 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001688
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001689 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001690 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001691
Gilles Peskine8c9def32018-02-08 10:02:12 +01001692 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1693 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001694 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001695 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001696 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001697 mode = MBEDTLS_MODE_STREAM;
1698 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001699 case PSA_ALG_CTR:
1700 mode = MBEDTLS_MODE_CTR;
1701 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001702 case PSA_ALG_CFB:
1703 mode = MBEDTLS_MODE_CFB;
1704 break;
1705 case PSA_ALG_OFB:
1706 mode = MBEDTLS_MODE_OFB;
1707 break;
1708 case PSA_ALG_CBC_NO_PADDING:
1709 mode = MBEDTLS_MODE_CBC;
1710 break;
1711 case PSA_ALG_CBC_PKCS7:
1712 mode = MBEDTLS_MODE_CBC;
1713 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001714 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001715 mode = MBEDTLS_MODE_CCM;
1716 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001717 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001718 mode = MBEDTLS_MODE_GCM;
1719 break;
1720 default:
1721 return( NULL );
1722 }
1723 }
1724 else if( alg == PSA_ALG_CMAC )
1725 mode = MBEDTLS_MODE_ECB;
1726 else if( alg == PSA_ALG_GMAC )
1727 mode = MBEDTLS_MODE_GCM;
1728 else
1729 return( NULL );
1730
1731 switch( key_type )
1732 {
1733 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001734 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001735 break;
1736 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001737 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1738 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001739 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001740 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001741 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001742 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001743 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1744 * but two-key Triple-DES is functionally three-key Triple-DES
1745 * with K1=K3, so that's how we present it to mbedtls. */
1746 if( key_bits == 128 )
1747 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001748 break;
1749 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001750 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001751 break;
1752 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001753 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001754 break;
1755 default:
1756 return( NULL );
1757 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001758 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001759 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001760
Jaeden Amero23bbb752018-06-26 14:16:54 +01001761 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1762 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001763}
1764
Gilles Peskinea05219c2018-11-16 16:02:56 +01001765#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001766static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001767{
Gilles Peskine2d277862018-06-18 15:41:12 +02001768 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001769 {
1770 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001771 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001772 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001773 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001774 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001775 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001776 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001777 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001778 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001779 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001780 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001781 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001782 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001783 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001784 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001785 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001786 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001787 return( 128 );
1788 default:
1789 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001790 }
1791}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001792#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001793
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001794/* Initialize the MAC operation structure. Once this function has been
1795 * called, psa_mac_abort can run and will do the right thing. */
1796static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1797 psa_algorithm_t alg )
1798{
1799 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1800
1801 operation->alg = alg;
1802 operation->key_set = 0;
1803 operation->iv_set = 0;
1804 operation->iv_required = 0;
1805 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001806 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001807
1808#if defined(MBEDTLS_CMAC_C)
1809 if( alg == PSA_ALG_CMAC )
1810 {
1811 operation->iv_required = 0;
1812 mbedtls_cipher_init( &operation->ctx.cmac );
1813 status = PSA_SUCCESS;
1814 }
1815 else
1816#endif /* MBEDTLS_CMAC_C */
1817#if defined(MBEDTLS_MD_C)
1818 if( PSA_ALG_IS_HMAC( operation->alg ) )
1819 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001820 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1821 operation->ctx.hmac.hash_ctx.alg = 0;
1822 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001823 }
1824 else
1825#endif /* MBEDTLS_MD_C */
1826 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001827 if( ! PSA_ALG_IS_MAC( alg ) )
1828 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001829 }
1830
1831 if( status != PSA_SUCCESS )
1832 memset( operation, 0, sizeof( *operation ) );
1833 return( status );
1834}
1835
Gilles Peskine01126fa2018-07-12 17:04:55 +02001836#if defined(MBEDTLS_MD_C)
1837static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1838{
Gilles Peskine3f108122018-12-07 18:14:53 +01001839 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001840 return( psa_hash_abort( &hmac->hash_ctx ) );
1841}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001842
1843static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1844{
1845 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1846 memset( hmac, 0, sizeof( *hmac ) );
1847}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001848#endif /* MBEDTLS_MD_C */
1849
Gilles Peskine8c9def32018-02-08 10:02:12 +01001850psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1851{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001852 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001853 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001854 /* The object has (apparently) been initialized but it is not
1855 * in use. It's ok to call abort on such an object, and there's
1856 * nothing to do. */
1857 return( PSA_SUCCESS );
1858 }
1859 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001860#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001861 if( operation->alg == PSA_ALG_CMAC )
1862 {
1863 mbedtls_cipher_free( &operation->ctx.cmac );
1864 }
1865 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001866#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001867#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001868 if( PSA_ALG_IS_HMAC( operation->alg ) )
1869 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001870 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001871 }
1872 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001873#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001874 {
1875 /* Sanity check (shouldn't happen: operation->alg should
1876 * always have been initialized to a valid value). */
1877 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001878 }
Moran Peker41deec42018-04-04 15:43:05 +03001879
Gilles Peskine8c9def32018-02-08 10:02:12 +01001880 operation->alg = 0;
1881 operation->key_set = 0;
1882 operation->iv_set = 0;
1883 operation->iv_required = 0;
1884 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001885 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001886
Gilles Peskine8c9def32018-02-08 10:02:12 +01001887 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001888
1889bad_state:
1890 /* If abort is called on an uninitialized object, we can't trust
1891 * anything. Wipe the object in case it contains confidential data.
1892 * This may result in a memory leak if a pointer gets overwritten,
1893 * but it's too late to do anything about this. */
1894 memset( operation, 0, sizeof( *operation ) );
1895 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001896}
1897
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001898#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001899static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001900 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001901 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001902 const mbedtls_cipher_info_t *cipher_info )
1903{
1904 int ret;
1905
1906 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001907
1908 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1909 if( ret != 0 )
1910 return( ret );
1911
1912 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1913 slot->data.raw.data,
1914 key_bits );
1915 return( ret );
1916}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001917#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001918
Gilles Peskine248051a2018-06-20 16:09:38 +02001919#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001920static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1921 const uint8_t *key,
1922 size_t key_length,
1923 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001924{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001925 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001926 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001927 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001928 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001929 psa_status_t status;
1930
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001931 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1932 * overflow below. This should never trigger if the hash algorithm
1933 * is implemented correctly. */
1934 /* The size checks against the ipad and opad buffers cannot be written
1935 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1936 * because that triggers -Wlogical-op on GCC 7.3. */
1937 if( block_size > sizeof( ipad ) )
1938 return( PSA_ERROR_NOT_SUPPORTED );
1939 if( block_size > sizeof( hmac->opad ) )
1940 return( PSA_ERROR_NOT_SUPPORTED );
1941 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001942 return( PSA_ERROR_NOT_SUPPORTED );
1943
Gilles Peskined223b522018-06-11 18:12:58 +02001944 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001945 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001946 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001947 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001948 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001949 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001950 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001951 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001952 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001953 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001954 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001955 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001956 }
Gilles Peskine96889972018-07-12 17:07:03 +02001957 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1958 * but it is permitted. It is common when HMAC is used in HKDF, for
1959 * example. Don't call `memcpy` in the 0-length because `key` could be
1960 * an invalid pointer which would make the behavior undefined. */
1961 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001962 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001963
Gilles Peskined223b522018-06-11 18:12:58 +02001964 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1965 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001966 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001967 ipad[i] ^= 0x36;
1968 memset( ipad + key_length, 0x36, block_size - key_length );
1969
1970 /* Copy the key material from ipad to opad, flipping the requisite bits,
1971 * and filling the rest of opad with the requisite constant. */
1972 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001973 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1974 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001975
Gilles Peskine01126fa2018-07-12 17:04:55 +02001976 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001977 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001978 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001979
Gilles Peskine01126fa2018-07-12 17:04:55 +02001980 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001981
1982cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001983 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001984
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001985 return( status );
1986}
Gilles Peskine248051a2018-06-20 16:09:38 +02001987#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001988
Gilles Peskine89167cb2018-07-08 20:12:23 +02001989static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001990 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001991 psa_algorithm_t alg,
1992 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001993{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001994 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001995 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001996 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001997 psa_key_usage_t usage =
1998 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001999 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002000 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002001
Gilles Peskined911eb72018-08-14 15:18:45 +02002002 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002003 if( status != PSA_SUCCESS )
2004 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002005 if( is_sign )
2006 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002007
Gilles Peskinec5487a82018-12-03 18:08:14 +01002008 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002009 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002010 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002011 key_bits = psa_get_key_bits( slot );
2012
Gilles Peskine8c9def32018-02-08 10:02:12 +01002013#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002014 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002015 {
2016 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002017 mbedtls_cipher_info_from_psa( full_length_alg,
2018 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002019 int ret;
2020 if( cipher_info == NULL )
2021 {
2022 status = PSA_ERROR_NOT_SUPPORTED;
2023 goto exit;
2024 }
2025 operation->mac_size = cipher_info->block_size;
2026 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2027 status = mbedtls_to_psa_error( ret );
2028 }
2029 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002030#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002031#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002032 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002033 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002034 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002035 if( hash_alg == 0 )
2036 {
2037 status = PSA_ERROR_NOT_SUPPORTED;
2038 goto exit;
2039 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002040
2041 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2042 /* Sanity check. This shouldn't fail on a valid configuration. */
2043 if( operation->mac_size == 0 ||
2044 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2045 {
2046 status = PSA_ERROR_NOT_SUPPORTED;
2047 goto exit;
2048 }
2049
Gilles Peskine01126fa2018-07-12 17:04:55 +02002050 if( slot->type != PSA_KEY_TYPE_HMAC )
2051 {
2052 status = PSA_ERROR_INVALID_ARGUMENT;
2053 goto exit;
2054 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002055
Gilles Peskine01126fa2018-07-12 17:04:55 +02002056 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2057 slot->data.raw.data,
2058 slot->data.raw.bytes,
2059 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002060 }
2061 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002062#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002063 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002064 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002065 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002066 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002067
Gilles Peskined911eb72018-08-14 15:18:45 +02002068 if( truncated == 0 )
2069 {
2070 /* The "normal" case: untruncated algorithm. Nothing to do. */
2071 }
2072 else if( truncated < 4 )
2073 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002074 /* A very short MAC is too short for security since it can be
2075 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2076 * so we make this our minimum, even though 32 bits is still
2077 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002078 status = PSA_ERROR_NOT_SUPPORTED;
2079 }
2080 else if( truncated > operation->mac_size )
2081 {
2082 /* It's impossible to "truncate" to a larger length. */
2083 status = PSA_ERROR_INVALID_ARGUMENT;
2084 }
2085 else
2086 operation->mac_size = truncated;
2087
Gilles Peskinefbfac682018-07-08 20:51:54 +02002088exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002089 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002090 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002091 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002092 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002093 else
2094 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002095 operation->key_set = 1;
2096 }
2097 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002098}
2099
Gilles Peskine89167cb2018-07-08 20:12:23 +02002100psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002101 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002102 psa_algorithm_t alg )
2103{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002104 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002105}
2106
2107psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002108 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002109 psa_algorithm_t alg )
2110{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002111 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002112}
2113
Gilles Peskine8c9def32018-02-08 10:02:12 +01002114psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2115 const uint8_t *input,
2116 size_t input_length )
2117{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002118 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002119 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002120 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002121 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002122 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002123 operation->has_input = 1;
2124
Gilles Peskine8c9def32018-02-08 10:02:12 +01002125#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002126 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002127 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002128 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2129 input, input_length );
2130 status = mbedtls_to_psa_error( ret );
2131 }
2132 else
2133#endif /* MBEDTLS_CMAC_C */
2134#if defined(MBEDTLS_MD_C)
2135 if( PSA_ALG_IS_HMAC( operation->alg ) )
2136 {
2137 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2138 input_length );
2139 }
2140 else
2141#endif /* MBEDTLS_MD_C */
2142 {
2143 /* This shouldn't happen if `operation` was initialized by
2144 * a setup function. */
2145 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002146 }
2147
Gilles Peskinefbfac682018-07-08 20:51:54 +02002148cleanup:
2149 if( status != PSA_SUCCESS )
2150 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002151 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002152}
2153
Gilles Peskine01126fa2018-07-12 17:04:55 +02002154#if defined(MBEDTLS_MD_C)
2155static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2156 uint8_t *mac,
2157 size_t mac_size )
2158{
2159 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2160 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2161 size_t hash_size = 0;
2162 size_t block_size = psa_get_hash_block_size( hash_alg );
2163 psa_status_t status;
2164
Gilles Peskine01126fa2018-07-12 17:04:55 +02002165 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2166 if( status != PSA_SUCCESS )
2167 return( status );
2168 /* From here on, tmp needs to be wiped. */
2169
2170 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2171 if( status != PSA_SUCCESS )
2172 goto exit;
2173
2174 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2175 if( status != PSA_SUCCESS )
2176 goto exit;
2177
2178 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2179 if( status != PSA_SUCCESS )
2180 goto exit;
2181
Gilles Peskined911eb72018-08-14 15:18:45 +02002182 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2183 if( status != PSA_SUCCESS )
2184 goto exit;
2185
2186 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002187
2188exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002189 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002190 return( status );
2191}
2192#endif /* MBEDTLS_MD_C */
2193
mohammad16036df908f2018-04-02 08:34:15 -07002194static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002195 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002196 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002197{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002198 if( ! operation->key_set )
2199 return( PSA_ERROR_BAD_STATE );
2200 if( operation->iv_required && ! operation->iv_set )
2201 return( PSA_ERROR_BAD_STATE );
2202
Gilles Peskine8c9def32018-02-08 10:02:12 +01002203 if( mac_size < operation->mac_size )
2204 return( PSA_ERROR_BUFFER_TOO_SMALL );
2205
Gilles Peskine8c9def32018-02-08 10:02:12 +01002206#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002207 if( operation->alg == PSA_ALG_CMAC )
2208 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002209 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2210 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2211 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002212 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002213 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002214 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002215 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002216 else
2217#endif /* MBEDTLS_CMAC_C */
2218#if defined(MBEDTLS_MD_C)
2219 if( PSA_ALG_IS_HMAC( operation->alg ) )
2220 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002221 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002222 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002223 }
2224 else
2225#endif /* MBEDTLS_MD_C */
2226 {
2227 /* This shouldn't happen if `operation` was initialized by
2228 * a setup function. */
2229 return( PSA_ERROR_BAD_STATE );
2230 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002231}
2232
Gilles Peskineacd4be32018-07-08 19:56:25 +02002233psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2234 uint8_t *mac,
2235 size_t mac_size,
2236 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002237{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002238 psa_status_t status;
2239
2240 /* Fill the output buffer with something that isn't a valid mac
2241 * (barring an attack on the mac and deliberately-crafted input),
2242 * in case the caller doesn't check the return status properly. */
2243 *mac_length = mac_size;
2244 /* If mac_size is 0 then mac may be NULL and then the
2245 * call to memset would have undefined behavior. */
2246 if( mac_size != 0 )
2247 memset( mac, '!', mac_size );
2248
Gilles Peskine89167cb2018-07-08 20:12:23 +02002249 if( ! operation->is_sign )
2250 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002251 status = PSA_ERROR_BAD_STATE;
2252 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002253 }
mohammad16036df908f2018-04-02 08:34:15 -07002254
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002255 status = psa_mac_finish_internal( operation, mac, mac_size );
2256
2257cleanup:
2258 if( status == PSA_SUCCESS )
2259 {
2260 status = psa_mac_abort( operation );
2261 if( status == PSA_SUCCESS )
2262 *mac_length = operation->mac_size;
2263 else
2264 memset( mac, '!', mac_size );
2265 }
2266 else
2267 psa_mac_abort( operation );
2268 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002269}
2270
Gilles Peskineacd4be32018-07-08 19:56:25 +02002271psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2272 const uint8_t *mac,
2273 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002274{
Gilles Peskine828ed142018-06-18 23:25:51 +02002275 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002276 psa_status_t status;
2277
Gilles Peskine89167cb2018-07-08 20:12:23 +02002278 if( operation->is_sign )
2279 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002280 status = PSA_ERROR_BAD_STATE;
2281 goto cleanup;
2282 }
2283 if( operation->mac_size != mac_length )
2284 {
2285 status = PSA_ERROR_INVALID_SIGNATURE;
2286 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002287 }
mohammad16036df908f2018-04-02 08:34:15 -07002288
2289 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002290 actual_mac, sizeof( actual_mac ) );
2291
2292 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2293 status = PSA_ERROR_INVALID_SIGNATURE;
2294
2295cleanup:
2296 if( status == PSA_SUCCESS )
2297 status = psa_mac_abort( operation );
2298 else
2299 psa_mac_abort( operation );
2300
Gilles Peskine3f108122018-12-07 18:14:53 +01002301 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002302
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002303 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002304}
2305
2306
Gilles Peskine20035e32018-02-03 22:44:14 +01002307
Gilles Peskine20035e32018-02-03 22:44:14 +01002308/****************************************************************/
2309/* Asymmetric cryptography */
2310/****************************************************************/
2311
Gilles Peskine2b450e32018-06-27 15:42:46 +02002312#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002313/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002314 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002315static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2316 size_t hash_length,
2317 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002318{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002319 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002320 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002321 *md_alg = mbedtls_md_get_type( md_info );
2322
2323 /* The Mbed TLS RSA module uses an unsigned int for hash length
2324 * parameters. Validate that it fits so that we don't risk an
2325 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002326#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002327 if( hash_length > UINT_MAX )
2328 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002329#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002330
2331#if defined(MBEDTLS_PKCS1_V15)
2332 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2333 * must be correct. */
2334 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2335 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002336 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002337 if( md_info == NULL )
2338 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002339 if( mbedtls_md_get_size( md_info ) != hash_length )
2340 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002341 }
2342#endif /* MBEDTLS_PKCS1_V15 */
2343
2344#if defined(MBEDTLS_PKCS1_V21)
2345 /* PSS requires a hash internally. */
2346 if( PSA_ALG_IS_RSA_PSS( alg ) )
2347 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002348 if( md_info == NULL )
2349 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002350 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002351#endif /* MBEDTLS_PKCS1_V21 */
2352
Gilles Peskine61b91d42018-06-08 16:09:36 +02002353 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002354}
2355
Gilles Peskine2b450e32018-06-27 15:42:46 +02002356static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2357 psa_algorithm_t alg,
2358 const uint8_t *hash,
2359 size_t hash_length,
2360 uint8_t *signature,
2361 size_t signature_size,
2362 size_t *signature_length )
2363{
2364 psa_status_t status;
2365 int ret;
2366 mbedtls_md_type_t md_alg;
2367
2368 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2369 if( status != PSA_SUCCESS )
2370 return( status );
2371
Gilles Peskine630a18a2018-06-29 17:49:35 +02002372 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002373 return( PSA_ERROR_BUFFER_TOO_SMALL );
2374
2375#if defined(MBEDTLS_PKCS1_V15)
2376 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2377 {
2378 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2379 MBEDTLS_MD_NONE );
2380 ret = mbedtls_rsa_pkcs1_sign( rsa,
2381 mbedtls_ctr_drbg_random,
2382 &global_data.ctr_drbg,
2383 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002384 md_alg,
2385 (unsigned int) hash_length,
2386 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002387 signature );
2388 }
2389 else
2390#endif /* MBEDTLS_PKCS1_V15 */
2391#if defined(MBEDTLS_PKCS1_V21)
2392 if( PSA_ALG_IS_RSA_PSS( alg ) )
2393 {
2394 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2395 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2396 mbedtls_ctr_drbg_random,
2397 &global_data.ctr_drbg,
2398 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002399 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002400 (unsigned int) hash_length,
2401 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002402 signature );
2403 }
2404 else
2405#endif /* MBEDTLS_PKCS1_V21 */
2406 {
2407 return( PSA_ERROR_INVALID_ARGUMENT );
2408 }
2409
2410 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002411 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002412 return( mbedtls_to_psa_error( ret ) );
2413}
2414
2415static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2416 psa_algorithm_t alg,
2417 const uint8_t *hash,
2418 size_t hash_length,
2419 const uint8_t *signature,
2420 size_t signature_length )
2421{
2422 psa_status_t status;
2423 int ret;
2424 mbedtls_md_type_t md_alg;
2425
2426 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2427 if( status != PSA_SUCCESS )
2428 return( status );
2429
Gilles Peskine630a18a2018-06-29 17:49:35 +02002430 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002431 return( PSA_ERROR_BUFFER_TOO_SMALL );
2432
2433#if defined(MBEDTLS_PKCS1_V15)
2434 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2435 {
2436 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2437 MBEDTLS_MD_NONE );
2438 ret = mbedtls_rsa_pkcs1_verify( rsa,
2439 mbedtls_ctr_drbg_random,
2440 &global_data.ctr_drbg,
2441 MBEDTLS_RSA_PUBLIC,
2442 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002443 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002444 hash,
2445 signature );
2446 }
2447 else
2448#endif /* MBEDTLS_PKCS1_V15 */
2449#if defined(MBEDTLS_PKCS1_V21)
2450 if( PSA_ALG_IS_RSA_PSS( alg ) )
2451 {
2452 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2453 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2454 mbedtls_ctr_drbg_random,
2455 &global_data.ctr_drbg,
2456 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002457 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002458 (unsigned int) hash_length,
2459 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002460 signature );
2461 }
2462 else
2463#endif /* MBEDTLS_PKCS1_V21 */
2464 {
2465 return( PSA_ERROR_INVALID_ARGUMENT );
2466 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002467
2468 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2469 * the rest of the signature is invalid". This has little use in
2470 * practice and PSA doesn't report this distinction. */
2471 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2472 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002473 return( mbedtls_to_psa_error( ret ) );
2474}
2475#endif /* MBEDTLS_RSA_C */
2476
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002477#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002478/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2479 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2480 * (even though these functions don't modify it). */
2481static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2482 psa_algorithm_t alg,
2483 const uint8_t *hash,
2484 size_t hash_length,
2485 uint8_t *signature,
2486 size_t signature_size,
2487 size_t *signature_length )
2488{
2489 int ret;
2490 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002491 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002492 mbedtls_mpi_init( &r );
2493 mbedtls_mpi_init( &s );
2494
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002495 if( signature_size < 2 * curve_bytes )
2496 {
2497 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2498 goto cleanup;
2499 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002500
Gilles Peskinea05219c2018-11-16 16:02:56 +01002501#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002502 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2503 {
2504 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2505 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2506 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2507 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2508 hash, hash_length,
2509 md_alg ) );
2510 }
2511 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002512#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002513 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002514 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002515 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2516 hash, hash_length,
2517 mbedtls_ctr_drbg_random,
2518 &global_data.ctr_drbg ) );
2519 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002520
2521 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2522 signature,
2523 curve_bytes ) );
2524 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2525 signature + curve_bytes,
2526 curve_bytes ) );
2527
2528cleanup:
2529 mbedtls_mpi_free( &r );
2530 mbedtls_mpi_free( &s );
2531 if( ret == 0 )
2532 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002533 return( mbedtls_to_psa_error( ret ) );
2534}
2535
2536static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2537 const uint8_t *hash,
2538 size_t hash_length,
2539 const uint8_t *signature,
2540 size_t signature_length )
2541{
2542 int ret;
2543 mbedtls_mpi r, s;
2544 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2545 mbedtls_mpi_init( &r );
2546 mbedtls_mpi_init( &s );
2547
2548 if( signature_length != 2 * curve_bytes )
2549 return( PSA_ERROR_INVALID_SIGNATURE );
2550
2551 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2552 signature,
2553 curve_bytes ) );
2554 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2555 signature + curve_bytes,
2556 curve_bytes ) );
2557
2558 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2559 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002560
2561cleanup:
2562 mbedtls_mpi_free( &r );
2563 mbedtls_mpi_free( &s );
2564 return( mbedtls_to_psa_error( ret ) );
2565}
2566#endif /* MBEDTLS_ECDSA_C */
2567
Gilles Peskinec5487a82018-12-03 18:08:14 +01002568psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002569 psa_algorithm_t alg,
2570 const uint8_t *hash,
2571 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002572 uint8_t *signature,
2573 size_t signature_size,
2574 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002575{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002576 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002577 psa_status_t status;
2578
2579 *signature_length = signature_size;
2580
Gilles Peskinec5487a82018-12-03 18:08:14 +01002581 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002582 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002583 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002584 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002585 {
2586 status = PSA_ERROR_INVALID_ARGUMENT;
2587 goto exit;
2588 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002589
Gilles Peskine20035e32018-02-03 22:44:14 +01002590#if defined(MBEDTLS_RSA_C)
2591 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2592 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002593 status = psa_rsa_sign( slot->data.rsa,
2594 alg,
2595 hash, hash_length,
2596 signature, signature_size,
2597 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002598 }
2599 else
2600#endif /* defined(MBEDTLS_RSA_C) */
2601#if defined(MBEDTLS_ECP_C)
2602 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2603 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002604#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002605 if(
2606#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2607 PSA_ALG_IS_ECDSA( alg )
2608#else
2609 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2610#endif
2611 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002612 status = psa_ecdsa_sign( slot->data.ecp,
2613 alg,
2614 hash, hash_length,
2615 signature, signature_size,
2616 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002617 else
2618#endif /* defined(MBEDTLS_ECDSA_C) */
2619 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002620 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002621 }
itayzafrir5c753392018-05-08 11:18:38 +03002622 }
2623 else
2624#endif /* defined(MBEDTLS_ECP_C) */
2625 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002626 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002627 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002628
2629exit:
2630 /* Fill the unused part of the output buffer (the whole buffer on error,
2631 * the trailing part on success) with something that isn't a valid mac
2632 * (barring an attack on the mac and deliberately-crafted input),
2633 * in case the caller doesn't check the return status properly. */
2634 if( status == PSA_SUCCESS )
2635 memset( signature + *signature_length, '!',
2636 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002637 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002638 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002639 /* If signature_size is 0 then we have nothing to do. We must not call
2640 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002641 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002642}
2643
Gilles Peskinec5487a82018-12-03 18:08:14 +01002644psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002645 psa_algorithm_t alg,
2646 const uint8_t *hash,
2647 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002648 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002649 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002650{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002651 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002652 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002653
Gilles Peskinec5487a82018-12-03 18:08:14 +01002654 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002655 if( status != PSA_SUCCESS )
2656 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002657
Gilles Peskine61b91d42018-06-08 16:09:36 +02002658#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002659 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002660 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002661 return( psa_rsa_verify( slot->data.rsa,
2662 alg,
2663 hash, hash_length,
2664 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002665 }
2666 else
2667#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002668#if defined(MBEDTLS_ECP_C)
2669 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2670 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002671#if defined(MBEDTLS_ECDSA_C)
2672 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002673 return( psa_ecdsa_verify( slot->data.ecp,
2674 hash, hash_length,
2675 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002676 else
2677#endif /* defined(MBEDTLS_ECDSA_C) */
2678 {
2679 return( PSA_ERROR_INVALID_ARGUMENT );
2680 }
itayzafrir5c753392018-05-08 11:18:38 +03002681 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002682 else
2683#endif /* defined(MBEDTLS_ECP_C) */
2684 {
2685 return( PSA_ERROR_NOT_SUPPORTED );
2686 }
2687}
2688
Gilles Peskine072ac562018-06-30 00:21:29 +02002689#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2690static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2691 mbedtls_rsa_context *rsa )
2692{
2693 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2694 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2695 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2696 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2697}
2698#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2699
Gilles Peskinec5487a82018-12-03 18:08:14 +01002700psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002701 psa_algorithm_t alg,
2702 const uint8_t *input,
2703 size_t input_length,
2704 const uint8_t *salt,
2705 size_t salt_length,
2706 uint8_t *output,
2707 size_t output_size,
2708 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002709{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002710 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002711 psa_status_t status;
2712
Darryl Green5cc689a2018-07-24 15:34:10 +01002713 (void) input;
2714 (void) input_length;
2715 (void) salt;
2716 (void) output;
2717 (void) output_size;
2718
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002719 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002720
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002721 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2722 return( PSA_ERROR_INVALID_ARGUMENT );
2723
Gilles Peskinec5487a82018-12-03 18:08:14 +01002724 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002725 if( status != PSA_SUCCESS )
2726 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002727 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2728 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002729 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002730
2731#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002732 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002733 {
2734 mbedtls_rsa_context *rsa = slot->data.rsa;
2735 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002736 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002737 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002738#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002739 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002740 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002741 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2742 mbedtls_ctr_drbg_random,
2743 &global_data.ctr_drbg,
2744 MBEDTLS_RSA_PUBLIC,
2745 input_length,
2746 input,
2747 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002748 }
2749 else
2750#endif /* MBEDTLS_PKCS1_V15 */
2751#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002752 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002753 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002754 psa_rsa_oaep_set_padding_mode( alg, rsa );
2755 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2756 mbedtls_ctr_drbg_random,
2757 &global_data.ctr_drbg,
2758 MBEDTLS_RSA_PUBLIC,
2759 salt, salt_length,
2760 input_length,
2761 input,
2762 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002763 }
2764 else
2765#endif /* MBEDTLS_PKCS1_V21 */
2766 {
2767 return( PSA_ERROR_INVALID_ARGUMENT );
2768 }
2769 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002770 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002771 return( mbedtls_to_psa_error( ret ) );
2772 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002773 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002774#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002775 {
2776 return( PSA_ERROR_NOT_SUPPORTED );
2777 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002778}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002779
Gilles Peskinec5487a82018-12-03 18:08:14 +01002780psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002781 psa_algorithm_t alg,
2782 const uint8_t *input,
2783 size_t input_length,
2784 const uint8_t *salt,
2785 size_t salt_length,
2786 uint8_t *output,
2787 size_t output_size,
2788 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002789{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002790 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002791 psa_status_t status;
2792
Darryl Green5cc689a2018-07-24 15:34:10 +01002793 (void) input;
2794 (void) input_length;
2795 (void) salt;
2796 (void) output;
2797 (void) output_size;
2798
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002799 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002800
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002801 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2802 return( PSA_ERROR_INVALID_ARGUMENT );
2803
Gilles Peskinec5487a82018-12-03 18:08:14 +01002804 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002805 if( status != PSA_SUCCESS )
2806 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002807 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2808 return( PSA_ERROR_INVALID_ARGUMENT );
2809
2810#if defined(MBEDTLS_RSA_C)
2811 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2812 {
2813 mbedtls_rsa_context *rsa = slot->data.rsa;
2814 int ret;
2815
Gilles Peskine630a18a2018-06-29 17:49:35 +02002816 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002817 return( PSA_ERROR_INVALID_ARGUMENT );
2818
2819#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002820 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002821 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002822 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2823 mbedtls_ctr_drbg_random,
2824 &global_data.ctr_drbg,
2825 MBEDTLS_RSA_PRIVATE,
2826 output_length,
2827 input,
2828 output,
2829 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002830 }
2831 else
2832#endif /* MBEDTLS_PKCS1_V15 */
2833#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002834 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002835 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002836 psa_rsa_oaep_set_padding_mode( alg, rsa );
2837 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2838 mbedtls_ctr_drbg_random,
2839 &global_data.ctr_drbg,
2840 MBEDTLS_RSA_PRIVATE,
2841 salt, salt_length,
2842 output_length,
2843 input,
2844 output,
2845 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002846 }
2847 else
2848#endif /* MBEDTLS_PKCS1_V21 */
2849 {
2850 return( PSA_ERROR_INVALID_ARGUMENT );
2851 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002852
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002853 return( mbedtls_to_psa_error( ret ) );
2854 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002855 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002856#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002857 {
2858 return( PSA_ERROR_NOT_SUPPORTED );
2859 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002860}
Gilles Peskine20035e32018-02-03 22:44:14 +01002861
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002862
2863
mohammad1603503973b2018-03-12 15:59:30 +02002864/****************************************************************/
2865/* Symmetric cryptography */
2866/****************************************************************/
2867
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002868/* Initialize the cipher operation structure. Once this function has been
2869 * called, psa_cipher_abort can run and will do the right thing. */
2870static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2871 psa_algorithm_t alg )
2872{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002873 if( ! PSA_ALG_IS_CIPHER( alg ) )
2874 {
2875 memset( operation, 0, sizeof( *operation ) );
2876 return( PSA_ERROR_INVALID_ARGUMENT );
2877 }
2878
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002879 operation->alg = alg;
2880 operation->key_set = 0;
2881 operation->iv_set = 0;
2882 operation->iv_required = 1;
2883 operation->iv_size = 0;
2884 operation->block_size = 0;
2885 mbedtls_cipher_init( &operation->ctx.cipher );
2886 return( PSA_SUCCESS );
2887}
2888
Gilles Peskinee553c652018-06-04 16:22:46 +02002889static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002890 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002891 psa_algorithm_t alg,
2892 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002893{
2894 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2895 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002896 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002897 size_t key_bits;
2898 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002899 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2900 PSA_KEY_USAGE_ENCRYPT :
2901 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002902
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002903 status = psa_cipher_init( operation, alg );
2904 if( status != PSA_SUCCESS )
2905 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002906
Gilles Peskinec5487a82018-12-03 18:08:14 +01002907 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002908 if( status != PSA_SUCCESS )
2909 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002910 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002911
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002912 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002913 if( cipher_info == NULL )
2914 return( PSA_ERROR_NOT_SUPPORTED );
2915
mohammad1603503973b2018-03-12 15:59:30 +02002916 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002917 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002918 {
2919 psa_cipher_abort( operation );
2920 return( mbedtls_to_psa_error( ret ) );
2921 }
2922
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002923#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002924 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002925 {
2926 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2927 unsigned char keys[24];
2928 memcpy( keys, slot->data.raw.data, 16 );
2929 memcpy( keys + 16, slot->data.raw.data, 8 );
2930 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2931 keys,
2932 192, cipher_operation );
2933 }
2934 else
2935#endif
2936 {
2937 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2938 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002939 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002940 }
Moran Peker41deec42018-04-04 15:43:05 +03002941 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002942 {
2943 psa_cipher_abort( operation );
2944 return( mbedtls_to_psa_error( ret ) );
2945 }
2946
mohammad16038481e742018-03-18 13:57:31 +02002947#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002948 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002949 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002950 case PSA_ALG_CBC_NO_PADDING:
2951 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2952 MBEDTLS_PADDING_NONE );
2953 break;
2954 case PSA_ALG_CBC_PKCS7:
2955 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2956 MBEDTLS_PADDING_PKCS7 );
2957 break;
2958 default:
2959 /* The algorithm doesn't involve padding. */
2960 ret = 0;
2961 break;
2962 }
2963 if( ret != 0 )
2964 {
2965 psa_cipher_abort( operation );
2966 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002967 }
2968#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2969
mohammad1603503973b2018-03-12 15:59:30 +02002970 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002971 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2972 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2973 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002974 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002975 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002976 }
mohammad1603503973b2018-03-12 15:59:30 +02002977
Moran Peker395db872018-05-31 14:07:14 +03002978 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002979}
2980
Gilles Peskinefe119512018-07-08 21:39:34 +02002981psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002982 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002983 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002984{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002985 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002986}
2987
Gilles Peskinefe119512018-07-08 21:39:34 +02002988psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002989 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02002990 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002991{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002992 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002993}
2994
Gilles Peskinefe119512018-07-08 21:39:34 +02002995psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2996 unsigned char *iv,
2997 size_t iv_size,
2998 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002999{
itayzafrir534bd7c2018-08-02 13:56:32 +03003000 psa_status_t status;
3001 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003002 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003003 {
3004 status = PSA_ERROR_BAD_STATE;
3005 goto exit;
3006 }
Moran Peker41deec42018-04-04 15:43:05 +03003007 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003008 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003009 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003010 goto exit;
3011 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003012 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3013 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003014 if( ret != 0 )
3015 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003016 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003017 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003018 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003019
mohammad16038481e742018-03-18 13:57:31 +02003020 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003021 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003022
Moran Peker395db872018-05-31 14:07:14 +03003023exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003024 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003025 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003026 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003027}
3028
Gilles Peskinefe119512018-07-08 21:39:34 +02003029psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3030 const unsigned char *iv,
3031 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003032{
itayzafrir534bd7c2018-08-02 13:56:32 +03003033 psa_status_t status;
3034 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003035 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003036 {
3037 status = PSA_ERROR_BAD_STATE;
3038 goto exit;
3039 }
Moran Pekera28258c2018-05-29 16:25:04 +03003040 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003041 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003042 status = PSA_ERROR_INVALID_ARGUMENT;
3043 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003044 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003045 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3046 status = mbedtls_to_psa_error( ret );
3047exit:
3048 if( status == PSA_SUCCESS )
3049 operation->iv_set = 1;
3050 else
mohammad1603503973b2018-03-12 15:59:30 +02003051 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003052 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003053}
3054
Gilles Peskinee553c652018-06-04 16:22:46 +02003055psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3056 const uint8_t *input,
3057 size_t input_length,
3058 unsigned char *output,
3059 size_t output_size,
3060 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003061{
itayzafrir534bd7c2018-08-02 13:56:32 +03003062 psa_status_t status;
3063 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003064 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003065 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003066 {
3067 /* Take the unprocessed partial block left over from previous
3068 * update calls, if any, plus the input to this call. Remove
3069 * the last partial block, if any. You get the data that will be
3070 * output in this call. */
3071 expected_output_size =
3072 ( operation->ctx.cipher.unprocessed_len + input_length )
3073 / operation->block_size * operation->block_size;
3074 }
3075 else
3076 {
3077 expected_output_size = input_length;
3078 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003079
Gilles Peskine89d789c2018-06-04 17:17:16 +02003080 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003081 {
3082 status = PSA_ERROR_BUFFER_TOO_SMALL;
3083 goto exit;
3084 }
mohammad160382759612018-03-12 18:16:40 +02003085
mohammad1603503973b2018-03-12 15:59:30 +02003086 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003087 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003088 status = mbedtls_to_psa_error( ret );
3089exit:
3090 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003091 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003092 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003093}
3094
Gilles Peskinee553c652018-06-04 16:22:46 +02003095psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3096 uint8_t *output,
3097 size_t output_size,
3098 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003099{
Janos Follath315b51c2018-07-09 16:04:51 +01003100 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
3101 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003102 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003103
mohammad1603503973b2018-03-12 15:59:30 +02003104 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003105 {
Janos Follath315b51c2018-07-09 16:04:51 +01003106 status = PSA_ERROR_BAD_STATE;
3107 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03003108 }
3109 if( operation->iv_required && ! operation->iv_set )
3110 {
Janos Follath315b51c2018-07-09 16:04:51 +01003111 status = PSA_ERROR_BAD_STATE;
3112 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03003113 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003114
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003115 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003116 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3117 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003118 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003119 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003120 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003121 }
3122
Janos Follath315b51c2018-07-09 16:04:51 +01003123 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3124 temp_output_buffer,
3125 output_length );
3126 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003127 {
Janos Follath315b51c2018-07-09 16:04:51 +01003128 status = mbedtls_to_psa_error( cipher_ret );
3129 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003130 }
Janos Follath315b51c2018-07-09 16:04:51 +01003131
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003132 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003133 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003134 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003135 memcpy( output, temp_output_buffer, *output_length );
3136 else
3137 {
Janos Follath315b51c2018-07-09 16:04:51 +01003138 status = PSA_ERROR_BUFFER_TOO_SMALL;
3139 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003140 }
mohammad1603503973b2018-03-12 15:59:30 +02003141
Gilles Peskine3f108122018-12-07 18:14:53 +01003142 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003143 status = psa_cipher_abort( operation );
3144
3145 return( status );
3146
3147error:
3148
3149 *output_length = 0;
3150
Gilles Peskine3f108122018-12-07 18:14:53 +01003151 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003152 (void) psa_cipher_abort( operation );
3153
3154 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003155}
3156
Gilles Peskinee553c652018-06-04 16:22:46 +02003157psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3158{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003159 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003160 {
3161 /* The object has (apparently) been initialized but it is not
3162 * in use. It's ok to call abort on such an object, and there's
3163 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003164 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003165 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003166
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003167 /* Sanity check (shouldn't happen: operation->alg should
3168 * always have been initialized to a valid value). */
3169 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3170 return( PSA_ERROR_BAD_STATE );
3171
mohammad1603503973b2018-03-12 15:59:30 +02003172 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003173
Moran Peker41deec42018-04-04 15:43:05 +03003174 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003175 operation->key_set = 0;
3176 operation->iv_set = 0;
3177 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003178 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003179 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003180
Moran Peker395db872018-05-31 14:07:14 +03003181 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003182}
3183
Gilles Peskinea0655c32018-04-30 17:06:50 +02003184
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003185
mohammad16038cc1cee2018-03-28 01:21:33 +03003186/****************************************************************/
3187/* Key Policy */
3188/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003189
mohammad160327010052018-07-03 13:16:15 +03003190#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003191void psa_key_policy_set_usage( psa_key_policy_t *policy,
3192 psa_key_usage_t usage,
3193 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003194{
mohammad16034eed7572018-03-28 05:14:59 -07003195 policy->usage = usage;
3196 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003197}
3198
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003199psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003200{
mohammad16036df908f2018-04-02 08:34:15 -07003201 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003202}
3203
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003204psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003205{
mohammad16036df908f2018-04-02 08:34:15 -07003206 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003207}
mohammad160327010052018-07-03 13:16:15 +03003208#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003209
Gilles Peskinec5487a82018-12-03 18:08:14 +01003210psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003211 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003212{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003213 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003214 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003215
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003216 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003217 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003218
Gilles Peskinec5487a82018-12-03 18:08:14 +01003219 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003220 if( status != PSA_SUCCESS )
3221 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003222
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003223 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3224 PSA_KEY_USAGE_ENCRYPT |
3225 PSA_KEY_USAGE_DECRYPT |
3226 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003227 PSA_KEY_USAGE_VERIFY |
3228 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003229 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003230
mohammad16036df908f2018-04-02 08:34:15 -07003231 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003232
3233 return( PSA_SUCCESS );
3234}
3235
Gilles Peskinec5487a82018-12-03 18:08:14 +01003236psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003237 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003238{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003239 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003240 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003241
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003242 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003243 return( PSA_ERROR_INVALID_ARGUMENT );
3244
Gilles Peskinec5487a82018-12-03 18:08:14 +01003245 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003246 if( status != PSA_SUCCESS )
3247 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003248
mohammad16036df908f2018-04-02 08:34:15 -07003249 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003250
3251 return( PSA_SUCCESS );
3252}
Gilles Peskine20035e32018-02-03 22:44:14 +01003253
Gilles Peskinea0655c32018-04-30 17:06:50 +02003254
3255
mohammad1603804cd712018-03-20 22:44:08 +02003256/****************************************************************/
3257/* Key Lifetime */
3258/****************************************************************/
3259
Gilles Peskinec5487a82018-12-03 18:08:14 +01003260psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003261 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003262{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003263 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003264 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003265
Gilles Peskinec5487a82018-12-03 18:08:14 +01003266 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003267 if( status != PSA_SUCCESS )
3268 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003269
mohammad1603804cd712018-03-20 22:44:08 +02003270 *lifetime = slot->lifetime;
3271
3272 return( PSA_SUCCESS );
3273}
3274
Gilles Peskine20035e32018-02-03 22:44:14 +01003275
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003276
mohammad16035955c982018-04-26 00:53:03 +03003277/****************************************************************/
3278/* AEAD */
3279/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003280
Gilles Peskineedf9a652018-08-17 18:11:56 +02003281typedef struct
3282{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003283 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003284 const mbedtls_cipher_info_t *cipher_info;
3285 union
3286 {
3287#if defined(MBEDTLS_CCM_C)
3288 mbedtls_ccm_context ccm;
3289#endif /* MBEDTLS_CCM_C */
3290#if defined(MBEDTLS_GCM_C)
3291 mbedtls_gcm_context gcm;
3292#endif /* MBEDTLS_GCM_C */
3293 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003294 psa_algorithm_t core_alg;
3295 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003296 uint8_t tag_length;
3297} aead_operation_t;
3298
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003299static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003300{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003301 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003302 {
3303#if defined(MBEDTLS_CCM_C)
3304 case PSA_ALG_CCM:
3305 mbedtls_ccm_free( &operation->ctx.ccm );
3306 break;
3307#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003308#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003309 case PSA_ALG_GCM:
3310 mbedtls_gcm_free( &operation->ctx.gcm );
3311 break;
3312#endif /* MBEDTLS_GCM_C */
3313 }
3314}
3315
3316static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003317 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003318 psa_key_usage_t usage,
3319 psa_algorithm_t alg )
3320{
3321 psa_status_t status;
3322 size_t key_bits;
3323 mbedtls_cipher_id_t cipher_id;
3324
Gilles Peskinec5487a82018-12-03 18:08:14 +01003325 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003326 if( status != PSA_SUCCESS )
3327 return( status );
3328
3329 key_bits = psa_get_key_bits( operation->slot );
3330
3331 operation->cipher_info =
3332 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3333 &cipher_id );
3334 if( operation->cipher_info == NULL )
3335 return( PSA_ERROR_NOT_SUPPORTED );
3336
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003337 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003338 {
3339#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003340 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3341 operation->core_alg = PSA_ALG_CCM;
3342 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003343 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3344 return( PSA_ERROR_INVALID_ARGUMENT );
3345 mbedtls_ccm_init( &operation->ctx.ccm );
3346 status = mbedtls_to_psa_error(
3347 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3348 operation->slot->data.raw.data,
3349 (unsigned int) key_bits ) );
3350 if( status != 0 )
3351 goto cleanup;
3352 break;
3353#endif /* MBEDTLS_CCM_C */
3354
3355#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003356 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3357 operation->core_alg = PSA_ALG_GCM;
3358 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003359 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3360 return( PSA_ERROR_INVALID_ARGUMENT );
3361 mbedtls_gcm_init( &operation->ctx.gcm );
3362 status = mbedtls_to_psa_error(
3363 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3364 operation->slot->data.raw.data,
3365 (unsigned int) key_bits ) );
3366 break;
3367#endif /* MBEDTLS_GCM_C */
3368
3369 default:
3370 return( PSA_ERROR_NOT_SUPPORTED );
3371 }
3372
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003373 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3374 {
3375 status = PSA_ERROR_INVALID_ARGUMENT;
3376 goto cleanup;
3377 }
3378 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3379 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3380 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3381 * In both cases, mbedtls_xxx will validate the tag length below. */
3382
Gilles Peskineedf9a652018-08-17 18:11:56 +02003383 return( PSA_SUCCESS );
3384
3385cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003386 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003387 return( status );
3388}
3389
Gilles Peskinec5487a82018-12-03 18:08:14 +01003390psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003391 psa_algorithm_t alg,
3392 const uint8_t *nonce,
3393 size_t nonce_length,
3394 const uint8_t *additional_data,
3395 size_t additional_data_length,
3396 const uint8_t *plaintext,
3397 size_t plaintext_length,
3398 uint8_t *ciphertext,
3399 size_t ciphertext_size,
3400 size_t *ciphertext_length )
3401{
mohammad16035955c982018-04-26 00:53:03 +03003402 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003403 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003404 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003405
mohammad1603f08a5502018-06-03 15:05:47 +03003406 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003407
Gilles Peskinec5487a82018-12-03 18:08:14 +01003408 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003409 if( status != PSA_SUCCESS )
3410 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003411
Gilles Peskineedf9a652018-08-17 18:11:56 +02003412 /* For all currently supported modes, the tag is at the end of the
3413 * ciphertext. */
3414 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3415 {
3416 status = PSA_ERROR_BUFFER_TOO_SMALL;
3417 goto exit;
3418 }
3419 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003420
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003421#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003422 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003423 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003424 status = mbedtls_to_psa_error(
3425 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3426 MBEDTLS_GCM_ENCRYPT,
3427 plaintext_length,
3428 nonce, nonce_length,
3429 additional_data, additional_data_length,
3430 plaintext, ciphertext,
3431 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003432 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003433 else
3434#endif /* MBEDTLS_GCM_C */
3435#if defined(MBEDTLS_CCM_C)
3436 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003437 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003438 status = mbedtls_to_psa_error(
3439 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3440 plaintext_length,
3441 nonce, nonce_length,
3442 additional_data,
3443 additional_data_length,
3444 plaintext, ciphertext,
3445 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003446 }
mohammad16035c8845f2018-05-09 05:40:09 -07003447 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003448#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003449 {
mohammad1603554faad2018-06-03 15:07:38 +03003450 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003451 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003452
Gilles Peskineedf9a652018-08-17 18:11:56 +02003453 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3454 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003455
Gilles Peskineedf9a652018-08-17 18:11:56 +02003456exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003457 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003458 if( status == PSA_SUCCESS )
3459 *ciphertext_length = plaintext_length + operation.tag_length;
3460 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003461}
3462
Gilles Peskineee652a32018-06-01 19:23:52 +02003463/* Locate the tag in a ciphertext buffer containing the encrypted data
3464 * followed by the tag. Return the length of the part preceding the tag in
3465 * *plaintext_length. This is the size of the plaintext in modes where
3466 * the encrypted data has the same size as the plaintext, such as
3467 * CCM and GCM. */
3468static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3469 const uint8_t *ciphertext,
3470 size_t ciphertext_length,
3471 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003472 const uint8_t **p_tag )
3473{
3474 size_t payload_length;
3475 if( tag_length > ciphertext_length )
3476 return( PSA_ERROR_INVALID_ARGUMENT );
3477 payload_length = ciphertext_length - tag_length;
3478 if( payload_length > plaintext_size )
3479 return( PSA_ERROR_BUFFER_TOO_SMALL );
3480 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003481 return( PSA_SUCCESS );
3482}
3483
Gilles Peskinec5487a82018-12-03 18:08:14 +01003484psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003485 psa_algorithm_t alg,
3486 const uint8_t *nonce,
3487 size_t nonce_length,
3488 const uint8_t *additional_data,
3489 size_t additional_data_length,
3490 const uint8_t *ciphertext,
3491 size_t ciphertext_length,
3492 uint8_t *plaintext,
3493 size_t plaintext_size,
3494 size_t *plaintext_length )
3495{
mohammad16035955c982018-04-26 00:53:03 +03003496 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003497 aead_operation_t operation;
3498 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003499
Gilles Peskineee652a32018-06-01 19:23:52 +02003500 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003501
Gilles Peskinec5487a82018-12-03 18:08:14 +01003502 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003503 if( status != PSA_SUCCESS )
3504 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003505
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003506#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003507 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003508 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003509 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003510 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003511 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003512 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003513 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003514
Gilles Peskineedf9a652018-08-17 18:11:56 +02003515 status = mbedtls_to_psa_error(
3516 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3517 ciphertext_length - operation.tag_length,
3518 nonce, nonce_length,
3519 additional_data,
3520 additional_data_length,
3521 tag, operation.tag_length,
3522 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003523 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003524 else
3525#endif /* MBEDTLS_GCM_C */
3526#if defined(MBEDTLS_CCM_C)
3527 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003528 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003529 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003530 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003531 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003532 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003533 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003534
Gilles Peskineedf9a652018-08-17 18:11:56 +02003535 status = mbedtls_to_psa_error(
3536 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3537 ciphertext_length - operation.tag_length,
3538 nonce, nonce_length,
3539 additional_data,
3540 additional_data_length,
3541 ciphertext, plaintext,
3542 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003543 }
mohammad160339574652018-06-01 04:39:53 -07003544 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003545#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003546 {
mohammad1603554faad2018-06-03 15:07:38 +03003547 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003548 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003549
Gilles Peskineedf9a652018-08-17 18:11:56 +02003550 if( status != PSA_SUCCESS && plaintext_size != 0 )
3551 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003552
Gilles Peskineedf9a652018-08-17 18:11:56 +02003553exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003554 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003555 if( status == PSA_SUCCESS )
3556 *plaintext_length = ciphertext_length - operation.tag_length;
3557 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003558}
3559
Gilles Peskinea0655c32018-04-30 17:06:50 +02003560
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003561
Gilles Peskine20035e32018-02-03 22:44:14 +01003562/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003563/* Generators */
3564/****************************************************************/
3565
3566psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3567{
3568 psa_status_t status = PSA_SUCCESS;
3569 if( generator->alg == 0 )
3570 {
3571 /* The object has (apparently) been initialized but it is not
3572 * in use. It's ok to call abort on such an object, and there's
3573 * nothing to do. */
3574 }
3575 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003576 if( generator->alg == PSA_ALG_SELECT_RAW )
3577 {
3578 if( generator->ctx.buffer.data != NULL )
3579 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003580 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003581 generator->ctx.buffer.size );
3582 mbedtls_free( generator->ctx.buffer.data );
3583 }
3584 }
3585 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003586#if defined(MBEDTLS_MD_C)
3587 if( PSA_ALG_IS_HKDF( generator->alg ) )
3588 {
3589 mbedtls_free( generator->ctx.hkdf.info );
3590 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3591 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003592 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3593 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3594 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003595 {
3596 if( generator->ctx.tls12_prf.key != NULL )
3597 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003598 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003599 generator->ctx.tls12_prf.key_len );
3600 mbedtls_free( generator->ctx.tls12_prf.key );
3601 }
Hanno Becker580fba12018-11-13 20:50:45 +00003602
3603 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3604 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003605 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003606 generator->ctx.tls12_prf.Ai_with_seed_len );
3607 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3608 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003609 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003610 else
3611#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003612 {
3613 status = PSA_ERROR_BAD_STATE;
3614 }
3615 memset( generator, 0, sizeof( *generator ) );
3616 return( status );
3617}
3618
3619
3620psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3621 size_t *capacity)
3622{
3623 *capacity = generator->capacity;
3624 return( PSA_SUCCESS );
3625}
3626
Gilles Peskinebef7f142018-07-12 17:22:21 +02003627#if defined(MBEDTLS_MD_C)
3628/* Read some bytes from an HKDF-based generator. This performs a chunk
3629 * of the expand phase of the HKDF algorithm. */
3630static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3631 psa_algorithm_t hash_alg,
3632 uint8_t *output,
3633 size_t output_length )
3634{
3635 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3636 psa_status_t status;
3637
3638 while( output_length != 0 )
3639 {
3640 /* Copy what remains of the current block */
3641 uint8_t n = hash_length - hkdf->offset_in_block;
3642 if( n > output_length )
3643 n = (uint8_t) output_length;
3644 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3645 output += n;
3646 output_length -= n;
3647 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003648 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003649 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003650 /* We can't be wanting more output after block 0xff, otherwise
3651 * the capacity check in psa_generator_read() would have
3652 * prevented this call. It could happen only if the generator
3653 * object was corrupted or if this function is called directly
3654 * inside the library. */
3655 if( hkdf->block_number == 0xff )
3656 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003657
3658 /* We need a new block */
3659 ++hkdf->block_number;
3660 hkdf->offset_in_block = 0;
3661 status = psa_hmac_setup_internal( &hkdf->hmac,
3662 hkdf->prk, hash_length,
3663 hash_alg );
3664 if( status != PSA_SUCCESS )
3665 return( status );
3666 if( hkdf->block_number != 1 )
3667 {
3668 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3669 hkdf->output_block,
3670 hash_length );
3671 if( status != PSA_SUCCESS )
3672 return( status );
3673 }
3674 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3675 hkdf->info,
3676 hkdf->info_length );
3677 if( status != PSA_SUCCESS )
3678 return( status );
3679 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3680 &hkdf->block_number, 1 );
3681 if( status != PSA_SUCCESS )
3682 return( status );
3683 status = psa_hmac_finish_internal( &hkdf->hmac,
3684 hkdf->output_block,
3685 sizeof( hkdf->output_block ) );
3686 if( status != PSA_SUCCESS )
3687 return( status );
3688 }
3689
3690 return( PSA_SUCCESS );
3691}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003692
3693static psa_status_t psa_generator_tls12_prf_generate_next_block(
3694 psa_tls12_prf_generator_t *tls12_prf,
3695 psa_algorithm_t alg )
3696{
3697 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3698 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3699 psa_hmac_internal_data hmac;
3700 psa_status_t status, cleanup_status;
3701
Hanno Becker3b339e22018-11-13 20:56:14 +00003702 unsigned char *Ai;
3703 size_t Ai_len;
3704
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003705 /* We can't be wanting more output after block 0xff, otherwise
3706 * the capacity check in psa_generator_read() would have
3707 * prevented this call. It could happen only if the generator
3708 * object was corrupted or if this function is called directly
3709 * inside the library. */
3710 if( tls12_prf->block_number == 0xff )
3711 return( PSA_ERROR_BAD_STATE );
3712
3713 /* We need a new block */
3714 ++tls12_prf->block_number;
3715 tls12_prf->offset_in_block = 0;
3716
3717 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3718 *
3719 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3720 *
3721 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3722 * HMAC_hash(secret, A(2) + seed) +
3723 * HMAC_hash(secret, A(3) + seed) + ...
3724 *
3725 * A(0) = seed
3726 * A(i) = HMAC_hash( secret, A(i-1) )
3727 *
3728 * The `psa_tls12_prf_generator` structures saves the block
3729 * `HMAC_hash(secret, A(i) + seed)` from which the output
3730 * is currently extracted as `output_block`, while
3731 * `A(i) + seed` is stored in `Ai_with_seed`.
3732 *
3733 * Generating a new block means recalculating `Ai_with_seed`
3734 * from the A(i)-part of it, and afterwards recalculating
3735 * `output_block`.
3736 *
3737 * A(0) is computed at setup time.
3738 *
3739 */
3740
3741 psa_hmac_init_internal( &hmac );
3742
3743 /* We must distinguish the calculation of A(1) from those
3744 * of A(2) and higher, because A(0)=seed has a different
3745 * length than the other A(i). */
3746 if( tls12_prf->block_number == 1 )
3747 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003748 Ai = tls12_prf->Ai_with_seed + hash_length;
3749 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003750 }
3751 else
3752 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003753 Ai = tls12_prf->Ai_with_seed;
3754 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003755 }
3756
Hanno Becker3b339e22018-11-13 20:56:14 +00003757 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3758 status = psa_hmac_setup_internal( &hmac,
3759 tls12_prf->key,
3760 tls12_prf->key_len,
3761 hash_alg );
3762 if( status != PSA_SUCCESS )
3763 goto cleanup;
3764
3765 status = psa_hash_update( &hmac.hash_ctx,
3766 Ai, Ai_len );
3767 if( status != PSA_SUCCESS )
3768 goto cleanup;
3769
3770 status = psa_hmac_finish_internal( &hmac,
3771 tls12_prf->Ai_with_seed,
3772 hash_length );
3773 if( status != PSA_SUCCESS )
3774 goto cleanup;
3775
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003776 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3777 status = psa_hmac_setup_internal( &hmac,
3778 tls12_prf->key,
3779 tls12_prf->key_len,
3780 hash_alg );
3781 if( status != PSA_SUCCESS )
3782 goto cleanup;
3783
3784 status = psa_hash_update( &hmac.hash_ctx,
3785 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003786 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003787 if( status != PSA_SUCCESS )
3788 goto cleanup;
3789
3790 status = psa_hmac_finish_internal( &hmac,
3791 tls12_prf->output_block,
3792 hash_length );
3793 if( status != PSA_SUCCESS )
3794 goto cleanup;
3795
3796cleanup:
3797
3798 cleanup_status = psa_hmac_abort_internal( &hmac );
3799 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3800 status = cleanup_status;
3801
3802 return( status );
3803}
3804
3805/* Read some bytes from an TLS-1.2-PRF-based generator.
3806 * See Section 5 of RFC 5246. */
3807static psa_status_t psa_generator_tls12_prf_read(
3808 psa_tls12_prf_generator_t *tls12_prf,
3809 psa_algorithm_t alg,
3810 uint8_t *output,
3811 size_t output_length )
3812{
3813 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3814 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3815 psa_status_t status;
3816
3817 while( output_length != 0 )
3818 {
3819 /* Copy what remains of the current block */
3820 uint8_t n = hash_length - tls12_prf->offset_in_block;
3821
3822 /* Check if we have fully processed the current block. */
3823 if( n == 0 )
3824 {
3825 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3826 alg );
3827 if( status != PSA_SUCCESS )
3828 return( status );
3829
3830 continue;
3831 }
3832
3833 if( n > output_length )
3834 n = (uint8_t) output_length;
3835 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3836 n );
3837 output += n;
3838 output_length -= n;
3839 tls12_prf->offset_in_block += n;
3840 }
3841
3842 return( PSA_SUCCESS );
3843}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003844#endif /* MBEDTLS_MD_C */
3845
Gilles Peskineeab56e42018-07-12 17:12:33 +02003846psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3847 uint8_t *output,
3848 size_t output_length )
3849{
3850 psa_status_t status;
3851
3852 if( output_length > generator->capacity )
3853 {
3854 generator->capacity = 0;
3855 /* Go through the error path to wipe all confidential data now
3856 * that the generator object is useless. */
3857 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3858 goto exit;
3859 }
3860 if( output_length == 0 &&
3861 generator->capacity == 0 && generator->alg == 0 )
3862 {
3863 /* Edge case: this is a blank or finished generator, and 0
3864 * bytes were requested. The right error in this case could
3865 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3866 * INSUFFICIENT_CAPACITY, which is right for a finished
3867 * generator, for consistency with the case when
3868 * output_length > 0. */
3869 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3870 }
3871 generator->capacity -= output_length;
3872
Gilles Peskine751d9652018-09-18 12:05:44 +02003873 if( generator->alg == PSA_ALG_SELECT_RAW )
3874 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003875 /* Initially, the capacity of a selection generator is always
3876 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3877 * abbreviated in this comment as `size`. When the remaining
3878 * capacity is `c`, the next bytes to serve start `c` bytes
3879 * from the end of the buffer, i.e. `size - c` from the
3880 * beginning of the buffer. Since `generator->capacity` was just
3881 * decremented above, we need to serve the bytes from
3882 * `size - generator->capacity - output_length` to
3883 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003884 size_t offset =
3885 generator->ctx.buffer.size - generator->capacity - output_length;
3886 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3887 status = PSA_SUCCESS;
3888 }
3889 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003890#if defined(MBEDTLS_MD_C)
3891 if( PSA_ALG_IS_HKDF( generator->alg ) )
3892 {
3893 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3894 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3895 output, output_length );
3896 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003897 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3898 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003899 {
3900 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3901 generator->alg, output,
3902 output_length );
3903 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003904 else
3905#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003906 {
3907 return( PSA_ERROR_BAD_STATE );
3908 }
3909
3910exit:
3911 if( status != PSA_SUCCESS )
3912 {
3913 psa_generator_abort( generator );
3914 memset( output, '!', output_length );
3915 }
3916 return( status );
3917}
3918
Gilles Peskine08542d82018-07-19 17:05:42 +02003919#if defined(MBEDTLS_DES_C)
3920static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3921{
3922 if( data_size >= 8 )
3923 mbedtls_des_key_set_parity( data );
3924 if( data_size >= 16 )
3925 mbedtls_des_key_set_parity( data + 8 );
3926 if( data_size >= 24 )
3927 mbedtls_des_key_set_parity( data + 16 );
3928}
3929#endif /* MBEDTLS_DES_C */
3930
Gilles Peskinec5487a82018-12-03 18:08:14 +01003931psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003932 psa_key_type_t type,
3933 size_t bits,
3934 psa_crypto_generator_t *generator )
3935{
3936 uint8_t *data = NULL;
3937 size_t bytes = PSA_BITS_TO_BYTES( bits );
3938 psa_status_t status;
3939
3940 if( ! key_type_is_raw_bytes( type ) )
3941 return( PSA_ERROR_INVALID_ARGUMENT );
3942 if( bits % 8 != 0 )
3943 return( PSA_ERROR_INVALID_ARGUMENT );
3944 data = mbedtls_calloc( 1, bytes );
3945 if( data == NULL )
3946 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3947
3948 status = psa_generator_read( generator, data, bytes );
3949 if( status != PSA_SUCCESS )
3950 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003951#if defined(MBEDTLS_DES_C)
3952 if( type == PSA_KEY_TYPE_DES )
3953 psa_des_set_key_parity( data, bytes );
3954#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003955 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003956
3957exit:
3958 mbedtls_free( data );
3959 return( status );
3960}
3961
3962
3963
3964/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003965/* Key derivation */
3966/****************************************************************/
3967
Gilles Peskinea05219c2018-11-16 16:02:56 +01003968#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003969/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003970 * of the HKDF algorithm.
3971 *
3972 * Note that if this function fails, you must call psa_generator_abort()
3973 * to potentially free embedded data structures and wipe confidential data.
3974 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003975static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003976 const uint8_t *secret,
3977 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003978 psa_algorithm_t hash_alg,
3979 const uint8_t *salt,
3980 size_t salt_length,
3981 const uint8_t *label,
3982 size_t label_length )
3983{
3984 psa_status_t status;
3985 status = psa_hmac_setup_internal( &hkdf->hmac,
3986 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003987 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003988 if( status != PSA_SUCCESS )
3989 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003990 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003991 if( status != PSA_SUCCESS )
3992 return( status );
3993 status = psa_hmac_finish_internal( &hkdf->hmac,
3994 hkdf->prk,
3995 sizeof( hkdf->prk ) );
3996 if( status != PSA_SUCCESS )
3997 return( status );
3998 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3999 hkdf->block_number = 0;
4000 hkdf->info_length = label_length;
4001 if( label_length != 0 )
4002 {
4003 hkdf->info = mbedtls_calloc( 1, label_length );
4004 if( hkdf->info == NULL )
4005 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4006 memcpy( hkdf->info, label, label_length );
4007 }
4008 return( PSA_SUCCESS );
4009}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004010#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004011
Gilles Peskinea05219c2018-11-16 16:02:56 +01004012#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004013/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4014 *
4015 * Note that if this function fails, you must call psa_generator_abort()
4016 * to potentially free embedded data structures and wipe confidential data.
4017 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004018static psa_status_t psa_generator_tls12_prf_setup(
4019 psa_tls12_prf_generator_t *tls12_prf,
4020 const unsigned char *key,
4021 size_t key_len,
4022 psa_algorithm_t hash_alg,
4023 const uint8_t *salt,
4024 size_t salt_length,
4025 const uint8_t *label,
4026 size_t label_length )
4027{
4028 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004029 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4030 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004031
4032 tls12_prf->key = mbedtls_calloc( 1, key_len );
4033 if( tls12_prf->key == NULL )
4034 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4035 tls12_prf->key_len = key_len;
4036 memcpy( tls12_prf->key, key, key_len );
4037
Hanno Becker580fba12018-11-13 20:50:45 +00004038 overflow = ( salt_length + label_length < salt_length ) ||
4039 ( salt_length + label_length + hash_length < hash_length );
4040 if( overflow )
4041 return( PSA_ERROR_INVALID_ARGUMENT );
4042
4043 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4044 if( tls12_prf->Ai_with_seed == NULL )
4045 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4046 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4047
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004048 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4049 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004050 if( label_length != 0 )
4051 {
4052 memcpy( tls12_prf->Ai_with_seed + hash_length,
4053 label, label_length );
4054 }
4055
4056 if( salt_length != 0 )
4057 {
4058 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4059 salt, salt_length );
4060 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004061
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004062 /* The first block gets generated when
4063 * psa_generator_read() is called. */
4064 tls12_prf->block_number = 0;
4065 tls12_prf->offset_in_block = hash_length;
4066
4067 return( PSA_SUCCESS );
4068}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004069
4070/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4071static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4072 psa_tls12_prf_generator_t *tls12_prf,
4073 const unsigned char *psk,
4074 size_t psk_len,
4075 psa_algorithm_t hash_alg,
4076 const uint8_t *salt,
4077 size_t salt_length,
4078 const uint8_t *label,
4079 size_t label_length )
4080{
4081 psa_status_t status;
4082 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4083
4084 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4085 return( PSA_ERROR_INVALID_ARGUMENT );
4086
4087 /* Quoting RFC 4279, Section 2:
4088 *
4089 * The premaster secret is formed as follows: if the PSK is N octets
4090 * long, concatenate a uint16 with the value N, N zero octets, a second
4091 * uint16 with the value N, and the PSK itself.
4092 */
4093
4094 pms[0] = ( psk_len >> 8 ) & 0xff;
4095 pms[1] = ( psk_len >> 0 ) & 0xff;
4096 memset( pms + 2, 0, psk_len );
4097 pms[2 + psk_len + 0] = pms[0];
4098 pms[2 + psk_len + 1] = pms[1];
4099 memcpy( pms + 4 + psk_len, psk, psk_len );
4100
4101 status = psa_generator_tls12_prf_setup( tls12_prf,
4102 pms, 4 + 2 * psk_len,
4103 hash_alg,
4104 salt, salt_length,
4105 label, label_length );
4106
Gilles Peskine3f108122018-12-07 18:14:53 +01004107 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004108 return( status );
4109}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004110#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004111
Gilles Peskine346797d2018-11-16 16:05:06 +01004112/* Note that if this function fails, you must call psa_generator_abort()
4113 * to potentially free embedded data structures and wipe confidential data.
4114 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004115static psa_status_t psa_key_derivation_internal(
4116 psa_crypto_generator_t *generator,
4117 const uint8_t *secret, size_t secret_length,
4118 psa_algorithm_t alg,
4119 const uint8_t *salt, size_t salt_length,
4120 const uint8_t *label, size_t label_length,
4121 size_t capacity )
4122{
4123 psa_status_t status;
4124 size_t max_capacity;
4125
4126 /* Set generator->alg even on failure so that abort knows what to do. */
4127 generator->alg = alg;
4128
Gilles Peskine751d9652018-09-18 12:05:44 +02004129 if( alg == PSA_ALG_SELECT_RAW )
4130 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004131 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004132 if( salt_length != 0 )
4133 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004134 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004135 if( label_length != 0 )
4136 return( PSA_ERROR_INVALID_ARGUMENT );
4137 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4138 if( generator->ctx.buffer.data == NULL )
4139 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4140 memcpy( generator->ctx.buffer.data, secret, secret_length );
4141 generator->ctx.buffer.size = secret_length;
4142 max_capacity = secret_length;
4143 status = PSA_SUCCESS;
4144 }
4145 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004146#if defined(MBEDTLS_MD_C)
4147 if( PSA_ALG_IS_HKDF( alg ) )
4148 {
4149 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4150 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4151 if( hash_size == 0 )
4152 return( PSA_ERROR_NOT_SUPPORTED );
4153 max_capacity = 255 * hash_size;
4154 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4155 secret, secret_length,
4156 hash_alg,
4157 salt, salt_length,
4158 label, label_length );
4159 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004160 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4161 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4162 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004163 {
4164 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4165 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4166
4167 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4168 if( hash_alg != PSA_ALG_SHA_256 &&
4169 hash_alg != PSA_ALG_SHA_384 )
4170 {
4171 return( PSA_ERROR_NOT_SUPPORTED );
4172 }
4173
4174 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004175
4176 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4177 {
4178 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4179 secret, secret_length,
4180 hash_alg, salt, salt_length,
4181 label, label_length );
4182 }
4183 else
4184 {
4185 status = psa_generator_tls12_psk_to_ms_setup(
4186 &generator->ctx.tls12_prf,
4187 secret, secret_length,
4188 hash_alg, salt, salt_length,
4189 label, label_length );
4190 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004191 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004192 else
4193#endif
4194 {
4195 return( PSA_ERROR_NOT_SUPPORTED );
4196 }
4197
4198 if( status != PSA_SUCCESS )
4199 return( status );
4200
4201 if( capacity <= max_capacity )
4202 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004203 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4204 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004205 else
4206 return( PSA_ERROR_INVALID_ARGUMENT );
4207
4208 return( PSA_SUCCESS );
4209}
4210
Gilles Peskineea0fb492018-07-12 17:17:20 +02004211psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004212 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004213 psa_algorithm_t alg,
4214 const uint8_t *salt,
4215 size_t salt_length,
4216 const uint8_t *label,
4217 size_t label_length,
4218 size_t capacity )
4219{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004220 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004221 psa_status_t status;
4222
4223 if( generator->alg != 0 )
4224 return( PSA_ERROR_BAD_STATE );
4225
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004226 /* Make sure that alg is a key derivation algorithm. This prevents
4227 * key selection algorithms, which psa_key_derivation_internal
4228 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004229 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4230 return( PSA_ERROR_INVALID_ARGUMENT );
4231
Gilles Peskinec5487a82018-12-03 18:08:14 +01004232 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004233 if( status != PSA_SUCCESS )
4234 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004235
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004236 if( slot->type != PSA_KEY_TYPE_DERIVE )
4237 return( PSA_ERROR_INVALID_ARGUMENT );
4238
4239 status = psa_key_derivation_internal( generator,
4240 slot->data.raw.data,
4241 slot->data.raw.bytes,
4242 alg,
4243 salt, salt_length,
4244 label, label_length,
4245 capacity );
4246 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004247 psa_generator_abort( generator );
4248 return( status );
4249}
4250
4251
4252
4253/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004254/* Key agreement */
4255/****************************************************************/
4256
Gilles Peskinea05219c2018-11-16 16:02:56 +01004257#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004258static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4259 size_t peer_key_length,
4260 const mbedtls_ecp_keypair *our_key,
4261 uint8_t *shared_secret,
4262 size_t shared_secret_size,
4263 size_t *shared_secret_length )
4264{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004265 mbedtls_ecp_keypair *their_key = NULL;
4266 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004267 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004268 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004269
Jaeden Ameroccdce902019-01-10 11:42:27 +00004270 status = psa_import_ec_public_key(
4271 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4272 peer_key, peer_key_length,
4273 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004274 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004275 goto exit;
4276
Jaeden Amero97271b32019-01-10 19:38:51 +00004277 status = mbedtls_to_psa_error(
4278 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4279 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004280 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00004281 status = mbedtls_to_psa_error(
4282 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4283 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004284 goto exit;
4285
Jaeden Amero97271b32019-01-10 19:38:51 +00004286 status = mbedtls_to_psa_error(
4287 mbedtls_ecdh_calc_secret( &ecdh,
4288 shared_secret_length,
4289 shared_secret, shared_secret_size,
4290 mbedtls_ctr_drbg_random,
4291 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004292
4293exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004294 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00004295 mbedtls_ecp_keypair_free( their_key );
4296 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004297 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004298}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004299#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004300
Gilles Peskine01d718c2018-09-18 12:01:02 +02004301#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4302
Gilles Peskine346797d2018-11-16 16:05:06 +01004303/* Note that if this function fails, you must call psa_generator_abort()
4304 * to potentially free embedded data structures and wipe confidential data.
4305 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004306static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004307 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004308 const uint8_t *peer_key,
4309 size_t peer_key_length,
4310 psa_algorithm_t alg )
4311{
4312 psa_status_t status;
4313 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4314 size_t shared_secret_length = 0;
4315
4316 /* Step 1: run the secret agreement algorithm to generate the shared
4317 * secret. */
4318 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4319 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004320#if defined(MBEDTLS_ECDH_C)
4321 case PSA_ALG_ECDH_BASE:
4322 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4323 return( PSA_ERROR_INVALID_ARGUMENT );
4324 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4325 private_key->data.ecp,
4326 shared_secret,
4327 sizeof( shared_secret ),
4328 &shared_secret_length );
4329 break;
4330#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004331 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004332 (void) private_key;
4333 (void) peer_key;
4334 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004335 return( PSA_ERROR_NOT_SUPPORTED );
4336 }
4337 if( status != PSA_SUCCESS )
4338 goto exit;
4339
4340 /* Step 2: set up the key derivation to generate key material from
4341 * the shared secret. */
4342 status = psa_key_derivation_internal( generator,
4343 shared_secret, shared_secret_length,
4344 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4345 NULL, 0, NULL, 0,
4346 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4347exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004348 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004349 return( status );
4350}
4351
4352psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004353 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004354 const uint8_t *peer_key,
4355 size_t peer_key_length,
4356 psa_algorithm_t alg )
4357{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004358 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004359 psa_status_t status;
4360 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4361 return( PSA_ERROR_INVALID_ARGUMENT );
4362 status = psa_get_key_from_slot( private_key, &slot,
4363 PSA_KEY_USAGE_DERIVE, alg );
4364 if( status != PSA_SUCCESS )
4365 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004366 status = psa_key_agreement_internal( generator,
4367 slot,
4368 peer_key, peer_key_length,
4369 alg );
4370 if( status != PSA_SUCCESS )
4371 psa_generator_abort( generator );
4372 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004373}
4374
4375
4376
4377/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004378/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004379/****************************************************************/
4380
4381psa_status_t psa_generate_random( uint8_t *output,
4382 size_t output_size )
4383{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004384 int ret;
4385 GUARD_MODULE_INITIALIZED;
4386
4387 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004388 return( mbedtls_to_psa_error( ret ) );
4389}
4390
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004391#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004392
4393/* Support function for error conversion between psa_its error codes to psa crypto */
4394static psa_status_t its_to_psa_error( psa_its_status_t ret )
4395{
4396 switch( ret )
4397 {
4398 case PSA_ITS_SUCCESS:
4399 return( PSA_SUCCESS );
4400
Oren Cohen23a67842019-01-24 14:32:11 +02004401 case PSA_ITS_ERROR_UID_NOT_FOUND:
avolinski13beb102018-11-20 16:51:49 +02004402 return( PSA_ERROR_EMPTY_SLOT );
4403
4404 case PSA_ITS_ERROR_STORAGE_FAILURE:
4405 return( PSA_ERROR_STORAGE_FAILURE );
4406
4407 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4408 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4409
Jaeden Amero58600552018-11-30 12:04:38 +00004410 case PSA_ITS_ERROR_OFFSET_INVALID:
avolinski13beb102018-11-20 16:51:49 +02004411 case PSA_ITS_ERROR_INCORRECT_SIZE:
Oren Cohen23a67842019-01-24 14:32:11 +02004412 case PSA_ITS_ERROR_INVALID_ARGUMENTS:
avolinski13beb102018-11-20 16:51:49 +02004413 return( PSA_ERROR_INVALID_ARGUMENT );
4414
4415 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4416 return( PSA_ERROR_NOT_SUPPORTED );
4417
4418 case PSA_ITS_ERROR_WRITE_ONCE:
4419 return( PSA_ERROR_OCCUPIED_SLOT );
4420
4421 default:
4422 return( PSA_ERROR_UNKNOWN_ERROR );
4423 }
4424}
4425
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004426psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4427 size_t seed_size )
4428{
4429 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004430 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004431 struct psa_its_info_t p_info;
4432 if( global_data.initialized )
4433 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004434
4435 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4436 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4437 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4438 return( PSA_ERROR_INVALID_ARGUMENT );
4439
avolinski0d2c2662018-11-21 17:31:07 +02004440 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004441 status = its_to_psa_error( its_status );
4442
Oren Cohen23a67842019-01-24 14:32:11 +02004443 if( PSA_ITS_ERROR_UID_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004444 {
avolinski0d2c2662018-11-21 17:31:07 +02004445 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004446 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004447 }
avolinski13beb102018-11-20 16:51:49 +02004448 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004449 {
4450 /* You should not be here. Seed needs to be injected only once */
4451 status = PSA_ERROR_NOT_PERMITTED;
4452 }
4453 return( status );
4454}
4455#endif
4456
Gilles Peskinec5487a82018-12-03 18:08:14 +01004457psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004458 psa_key_type_t type,
4459 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004460 const void *extra,
4461 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004462{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004463 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004464 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004465
Gilles Peskine53d991e2018-07-12 01:14:59 +02004466 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004467 return( PSA_ERROR_INVALID_ARGUMENT );
4468
Gilles Peskinec5487a82018-12-03 18:08:14 +01004469 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004470 if( status != PSA_SUCCESS )
4471 return( status );
4472
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004473 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004474 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004475 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004476 if( status != PSA_SUCCESS )
4477 return( status );
4478 status = psa_generate_random( slot->data.raw.data,
4479 slot->data.raw.bytes );
4480 if( status != PSA_SUCCESS )
4481 {
4482 mbedtls_free( slot->data.raw.data );
4483 return( status );
4484 }
4485#if defined(MBEDTLS_DES_C)
4486 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004487 psa_des_set_key_parity( slot->data.raw.data,
4488 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004489#endif /* MBEDTLS_DES_C */
4490 }
4491 else
4492
4493#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4494 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4495 {
4496 mbedtls_rsa_context *rsa;
4497 int ret;
4498 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004499 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4500 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004501 /* Accept only byte-aligned keys, for the same reasons as
4502 * in psa_import_rsa_key(). */
4503 if( bits % 8 != 0 )
4504 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004505 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004506 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004507 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004508 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004509 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004510#if INT_MAX < 0xffffffff
4511 /* Check that the uint32_t value passed by the caller fits
4512 * in the range supported by this implementation. */
4513 if( p->e > INT_MAX )
4514 return( PSA_ERROR_NOT_SUPPORTED );
4515#endif
4516 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004517 }
4518 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4519 if( rsa == NULL )
4520 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4521 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4522 ret = mbedtls_rsa_gen_key( rsa,
4523 mbedtls_ctr_drbg_random,
4524 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004525 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004526 exponent );
4527 if( ret != 0 )
4528 {
4529 mbedtls_rsa_free( rsa );
4530 mbedtls_free( rsa );
4531 return( mbedtls_to_psa_error( ret ) );
4532 }
4533 slot->data.rsa = rsa;
4534 }
4535 else
4536#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4537
4538#if defined(MBEDTLS_ECP_C)
4539 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4540 {
4541 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4542 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4543 const mbedtls_ecp_curve_info *curve_info =
4544 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4545 mbedtls_ecp_keypair *ecp;
4546 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004547 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004548 return( PSA_ERROR_NOT_SUPPORTED );
4549 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4550 return( PSA_ERROR_NOT_SUPPORTED );
4551 if( curve_info->bit_size != bits )
4552 return( PSA_ERROR_INVALID_ARGUMENT );
4553 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4554 if( ecp == NULL )
4555 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4556 mbedtls_ecp_keypair_init( ecp );
4557 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4558 mbedtls_ctr_drbg_random,
4559 &global_data.ctr_drbg );
4560 if( ret != 0 )
4561 {
4562 mbedtls_ecp_keypair_free( ecp );
4563 mbedtls_free( ecp );
4564 return( mbedtls_to_psa_error( ret ) );
4565 }
4566 slot->data.ecp = ecp;
4567 }
4568 else
4569#endif /* MBEDTLS_ECP_C */
4570
4571 return( PSA_ERROR_NOT_SUPPORTED );
4572
4573 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004574
4575#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4576 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4577 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004578 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004579 }
4580#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4581
4582 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004583}
4584
4585
4586/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004587/* Module setup */
4588/****************************************************************/
4589
Gilles Peskine5e769522018-11-20 21:59:56 +01004590psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4591 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4592 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4593{
4594 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4595 return( PSA_ERROR_BAD_STATE );
4596 global_data.entropy_init = entropy_init;
4597 global_data.entropy_free = entropy_free;
4598 return( PSA_SUCCESS );
4599}
4600
Gilles Peskinee59236f2018-01-27 23:32:46 +01004601void mbedtls_psa_crypto_free( void )
4602{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004603 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004604 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4605 {
4606 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004607 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004608 }
4609 /* Wipe all remaining data, including configuration.
4610 * In particular, this sets all state indicator to the value
4611 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004612 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004613}
4614
4615psa_status_t psa_crypto_init( void )
4616{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004617 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004618 const unsigned char drbg_seed[] = "PSA";
4619
Gilles Peskinec6b69072018-11-20 21:42:52 +01004620 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004621 if( global_data.initialized != 0 )
4622 return( PSA_SUCCESS );
4623
Gilles Peskine5e769522018-11-20 21:59:56 +01004624 /* Set default configuration if
4625 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4626 if( global_data.entropy_init == NULL )
4627 global_data.entropy_init = mbedtls_entropy_init;
4628 if( global_data.entropy_free == NULL )
4629 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004630
Gilles Peskinec6b69072018-11-20 21:42:52 +01004631 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004632 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004633 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004634 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004635 status = mbedtls_to_psa_error(
4636 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4637 mbedtls_entropy_func,
4638 &global_data.entropy,
4639 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4640 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004641 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004642 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004643
Gilles Peskine66fb1262018-12-10 16:29:04 +01004644 status = psa_initialize_key_slots( );
4645 if( status != PSA_SUCCESS )
4646 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004647
4648 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004649 global_data.initialized = 1;
4650
Gilles Peskinee59236f2018-01-27 23:32:46 +01004651exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004652 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004653 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004654 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004655}
4656
4657#endif /* MBEDTLS_PSA_CRYPTO_C */