blob: 40c676a5dc8bd3b5774d5d63aa4c144bcbd0af14 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030029/*
itayzafrir14e76782019-01-16 11:16:39 +020030 * When MBEDTLS_PSA_CRYPTO_SPM is defined, the code is being built for SPM
31 * (Secure Partition Manager) integration which separates the code into two
32 * parts: NSPE (Non-Secure Processing Environment) and SPE (Secure Processing
33 * Environment). When building for the SPE, an additional header file should be
34 * included.
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030035 */
mohammad160327010052018-07-03 13:16:15 +030036#if defined(MBEDTLS_PSA_CRYPTO_SPM)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030037/*
itayzafrir14e76782019-01-16 11:16:39 +020038 * PSA_CRYPTO_SECURE means that this file is compiled for the SPE.
39 * Some headers will be affected by this flag.
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030040 */
mohammad160327010052018-07-03 13:16:15 +030041#define PSA_CRYPTO_SECURE 1
42#include "crypto_spe.h"
43#endif
44
Gilles Peskinee59236f2018-01-27 23:32:46 +010045#include "psa/crypto.h"
46
Gilles Peskine039b90c2018-12-07 18:24:41 +010047#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010048#include "psa_crypto_invasive.h"
Gilles Peskine961849f2018-11-30 18:54:54 +010049#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010050/* Include internal declarations that are useful for implementing persistently
51 * stored keys. */
52#include "psa_crypto_storage.h"
53
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010054#include <stdlib.h>
55#include <string.h>
56#if defined(MBEDTLS_PLATFORM_C)
57#include "mbedtls/platform.h"
58#else
59#define mbedtls_calloc calloc
60#define mbedtls_free free
61#endif
62
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020064#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000065#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020066#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/blowfish.h"
68#include "mbedtls/camellia.h"
69#include "mbedtls/cipher.h"
70#include "mbedtls/ccm.h"
71#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010072#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020074#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010076#include "mbedtls/entropy.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020077#include "mbedtls/entropy_poll.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/error.h"
79#include "mbedtls/gcm.h"
80#include "mbedtls/md2.h"
81#include "mbedtls/md4.h"
82#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010083#include "mbedtls/md.h"
84#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010085#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010086#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010087#include "mbedtls/platform_util.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010088#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010089#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010090#include "mbedtls/sha1.h"
91#include "mbedtls/sha256.h"
92#include "mbedtls/sha512.h"
93#include "mbedtls/xtea.h"
94
Netanel Gonen2bcd3122018-11-19 11:53:02 +020095#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
Oren Cohen23a67842019-01-24 14:32:11 +020096#include "psa/internal_trusted_storage.h"
Netanel Gonen2bcd3122018-11-19 11:53:02 +020097#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +010098
Gilles Peskine996deb12018-08-01 15:45:45 +020099#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
100
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100101/* constant-time buffer comparison */
102static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
103{
104 size_t i;
105 unsigned char diff = 0;
106
107 for( i = 0; i < n; i++ )
108 diff |= a[i] ^ b[i];
109
110 return( diff );
111}
112
113
114
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100115/****************************************************************/
116/* Global data, support functions and library management */
117/****************************************************************/
118
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200119static int key_type_is_raw_bytes( psa_key_type_t type )
120{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200121 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200122}
123
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100124/* Values for psa_global_data_t::rng_state */
125#define RNG_NOT_INITIALIZED 0
126#define RNG_INITIALIZED 1
127#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100128
Gilles Peskine2d277862018-06-18 15:41:12 +0200129typedef struct
130{
Gilles Peskine5e769522018-11-20 21:59:56 +0100131 void (* entropy_init )( mbedtls_entropy_context *ctx );
132 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100133 mbedtls_entropy_context entropy;
134 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100135 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100136 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100137} psa_global_data_t;
138
139static psa_global_data_t global_data;
140
itayzafrir0adf0fc2018-09-06 16:24:41 +0300141#define GUARD_MODULE_INITIALIZED \
142 if( global_data.initialized == 0 ) \
143 return( PSA_ERROR_BAD_STATE );
144
Gilles Peskinee59236f2018-01-27 23:32:46 +0100145static psa_status_t mbedtls_to_psa_error( int ret )
146{
Gilles Peskinea5905292018-02-07 20:59:33 +0100147 /* If there's both a high-level code and low-level code, dispatch on
148 * the high-level code. */
149 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100150 {
151 case 0:
152 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100153
154 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
155 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
156 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
157 return( PSA_ERROR_NOT_SUPPORTED );
158 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
159 return( PSA_ERROR_HARDWARE_FAILURE );
160
161 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
162 return( PSA_ERROR_HARDWARE_FAILURE );
163
Gilles Peskine9a944802018-06-21 09:35:35 +0200164 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
165 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
166 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
167 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
168 case MBEDTLS_ERR_ASN1_INVALID_DATA:
169 return( PSA_ERROR_INVALID_ARGUMENT );
170 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
171 return( PSA_ERROR_INSUFFICIENT_MEMORY );
172 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
173 return( PSA_ERROR_BUFFER_TOO_SMALL );
174
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000175 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000181 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Gilles Peskinea5905292018-02-07 20:59:33 +0100182 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
183 return( PSA_ERROR_NOT_SUPPORTED );
184 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
185 return( PSA_ERROR_HARDWARE_FAILURE );
186
187 case MBEDTLS_ERR_CCM_BAD_INPUT:
188 return( PSA_ERROR_INVALID_ARGUMENT );
189 case MBEDTLS_ERR_CCM_AUTH_FAILED:
190 return( PSA_ERROR_INVALID_SIGNATURE );
191 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
192 return( PSA_ERROR_HARDWARE_FAILURE );
193
194 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
195 return( PSA_ERROR_NOT_SUPPORTED );
196 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
197 return( PSA_ERROR_INVALID_ARGUMENT );
198 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
199 return( PSA_ERROR_INSUFFICIENT_MEMORY );
200 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
201 return( PSA_ERROR_INVALID_PADDING );
202 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
203 return( PSA_ERROR_BAD_STATE );
204 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
205 return( PSA_ERROR_INVALID_SIGNATURE );
206 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
207 return( PSA_ERROR_TAMPERING_DETECTED );
208 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
209 return( PSA_ERROR_HARDWARE_FAILURE );
210
211 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
214 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
215 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
216 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
217 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
218 return( PSA_ERROR_NOT_SUPPORTED );
219 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
220 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
221
222 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
223 return( PSA_ERROR_NOT_SUPPORTED );
224 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
225 return( PSA_ERROR_HARDWARE_FAILURE );
226
Gilles Peskinee59236f2018-01-27 23:32:46 +0100227 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
228 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
229 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
230 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100231
232 case MBEDTLS_ERR_GCM_AUTH_FAILED:
233 return( PSA_ERROR_INVALID_SIGNATURE );
234 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200235 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100236 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
237 return( PSA_ERROR_HARDWARE_FAILURE );
238
239 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
240 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
241 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
244 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
245 return( PSA_ERROR_NOT_SUPPORTED );
246 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
247 return( PSA_ERROR_INVALID_ARGUMENT );
248 case MBEDTLS_ERR_MD_ALLOC_FAILED:
249 return( PSA_ERROR_INSUFFICIENT_MEMORY );
250 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
251 return( PSA_ERROR_STORAGE_FAILURE );
252 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
253 return( PSA_ERROR_HARDWARE_FAILURE );
254
Gilles Peskinef76aa772018-10-29 19:24:33 +0100255 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
256 return( PSA_ERROR_STORAGE_FAILURE );
257 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
258 return( PSA_ERROR_INVALID_ARGUMENT );
259 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
262 return( PSA_ERROR_BUFFER_TOO_SMALL );
263 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
264 return( PSA_ERROR_INVALID_ARGUMENT );
265 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
270 return( PSA_ERROR_INSUFFICIENT_MEMORY );
271
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100272 case MBEDTLS_ERR_PK_ALLOC_FAILED:
273 return( PSA_ERROR_INSUFFICIENT_MEMORY );
274 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
275 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
276 return( PSA_ERROR_INVALID_ARGUMENT );
277 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100278 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100279 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
280 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
281 return( PSA_ERROR_INVALID_ARGUMENT );
282 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
283 return( PSA_ERROR_NOT_SUPPORTED );
284 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
285 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
286 return( PSA_ERROR_NOT_PERMITTED );
287 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
288 return( PSA_ERROR_INVALID_ARGUMENT );
289 case MBEDTLS_ERR_PK_INVALID_ALG:
290 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
291 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
292 return( PSA_ERROR_NOT_SUPPORTED );
293 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
294 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100295 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
296 return( PSA_ERROR_HARDWARE_FAILURE );
297
298 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
299 return( PSA_ERROR_HARDWARE_FAILURE );
300
301 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
302 return( PSA_ERROR_INVALID_ARGUMENT );
303 case MBEDTLS_ERR_RSA_INVALID_PADDING:
304 return( PSA_ERROR_INVALID_PADDING );
305 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
306 return( PSA_ERROR_HARDWARE_FAILURE );
307 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
310 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
311 return( PSA_ERROR_TAMPERING_DETECTED );
312 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
313 return( PSA_ERROR_INVALID_SIGNATURE );
314 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
315 return( PSA_ERROR_BUFFER_TOO_SMALL );
316 case MBEDTLS_ERR_RSA_RNG_FAILED:
317 return( PSA_ERROR_INSUFFICIENT_MEMORY );
318 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
319 return( PSA_ERROR_NOT_SUPPORTED );
320 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
321 return( PSA_ERROR_HARDWARE_FAILURE );
322
323 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
324 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
325 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327
328 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
329 return( PSA_ERROR_INVALID_ARGUMENT );
330 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
331 return( PSA_ERROR_HARDWARE_FAILURE );
332
itayzafrir5c753392018-05-08 11:18:38 +0300333 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300334 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300335 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300336 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
337 return( PSA_ERROR_BUFFER_TOO_SMALL );
338 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
339 return( PSA_ERROR_NOT_SUPPORTED );
340 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
341 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
342 return( PSA_ERROR_INVALID_SIGNATURE );
343 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
344 return( PSA_ERROR_INSUFFICIENT_MEMORY );
345 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
346 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300347
Gilles Peskinee59236f2018-01-27 23:32:46 +0100348 default:
349 return( PSA_ERROR_UNKNOWN_ERROR );
350 }
351}
352
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200353
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200354
355
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100356/****************************************************************/
357/* Key management */
358/****************************************************************/
359
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100360#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200361static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
362{
363 switch( grpid )
364 {
365 case MBEDTLS_ECP_DP_SECP192R1:
366 return( PSA_ECC_CURVE_SECP192R1 );
367 case MBEDTLS_ECP_DP_SECP224R1:
368 return( PSA_ECC_CURVE_SECP224R1 );
369 case MBEDTLS_ECP_DP_SECP256R1:
370 return( PSA_ECC_CURVE_SECP256R1 );
371 case MBEDTLS_ECP_DP_SECP384R1:
372 return( PSA_ECC_CURVE_SECP384R1 );
373 case MBEDTLS_ECP_DP_SECP521R1:
374 return( PSA_ECC_CURVE_SECP521R1 );
375 case MBEDTLS_ECP_DP_BP256R1:
376 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
377 case MBEDTLS_ECP_DP_BP384R1:
378 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
379 case MBEDTLS_ECP_DP_BP512R1:
380 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
381 case MBEDTLS_ECP_DP_CURVE25519:
382 return( PSA_ECC_CURVE_CURVE25519 );
383 case MBEDTLS_ECP_DP_SECP192K1:
384 return( PSA_ECC_CURVE_SECP192K1 );
385 case MBEDTLS_ECP_DP_SECP224K1:
386 return( PSA_ECC_CURVE_SECP224K1 );
387 case MBEDTLS_ECP_DP_SECP256K1:
388 return( PSA_ECC_CURVE_SECP256K1 );
389 case MBEDTLS_ECP_DP_CURVE448:
390 return( PSA_ECC_CURVE_CURVE448 );
391 default:
392 return( 0 );
393 }
394}
395
Gilles Peskine12313cd2018-06-20 00:20:32 +0200396static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
397{
398 switch( curve )
399 {
400 case PSA_ECC_CURVE_SECP192R1:
401 return( MBEDTLS_ECP_DP_SECP192R1 );
402 case PSA_ECC_CURVE_SECP224R1:
403 return( MBEDTLS_ECP_DP_SECP224R1 );
404 case PSA_ECC_CURVE_SECP256R1:
405 return( MBEDTLS_ECP_DP_SECP256R1 );
406 case PSA_ECC_CURVE_SECP384R1:
407 return( MBEDTLS_ECP_DP_SECP384R1 );
408 case PSA_ECC_CURVE_SECP521R1:
409 return( MBEDTLS_ECP_DP_SECP521R1 );
410 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
411 return( MBEDTLS_ECP_DP_BP256R1 );
412 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
413 return( MBEDTLS_ECP_DP_BP384R1 );
414 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
415 return( MBEDTLS_ECP_DP_BP512R1 );
416 case PSA_ECC_CURVE_CURVE25519:
417 return( MBEDTLS_ECP_DP_CURVE25519 );
418 case PSA_ECC_CURVE_SECP192K1:
419 return( MBEDTLS_ECP_DP_SECP192K1 );
420 case PSA_ECC_CURVE_SECP224K1:
421 return( MBEDTLS_ECP_DP_SECP224K1 );
422 case PSA_ECC_CURVE_SECP256K1:
423 return( MBEDTLS_ECP_DP_SECP256K1 );
424 case PSA_ECC_CURVE_CURVE448:
425 return( MBEDTLS_ECP_DP_CURVE448 );
426 default:
427 return( MBEDTLS_ECP_DP_NONE );
428 }
429}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100430#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200431
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200432static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
433 size_t bits,
434 struct raw_data *raw )
435{
436 /* Check that the bit size is acceptable for the key type */
437 switch( type )
438 {
439 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200440 if( bits == 0 )
441 {
442 raw->bytes = 0;
443 raw->data = NULL;
444 return( PSA_SUCCESS );
445 }
446 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200447#if defined(MBEDTLS_MD_C)
448 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200449#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200450 case PSA_KEY_TYPE_DERIVE:
451 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200452#if defined(MBEDTLS_AES_C)
453 case PSA_KEY_TYPE_AES:
454 if( bits != 128 && bits != 192 && bits != 256 )
455 return( PSA_ERROR_INVALID_ARGUMENT );
456 break;
457#endif
458#if defined(MBEDTLS_CAMELLIA_C)
459 case PSA_KEY_TYPE_CAMELLIA:
460 if( bits != 128 && bits != 192 && bits != 256 )
461 return( PSA_ERROR_INVALID_ARGUMENT );
462 break;
463#endif
464#if defined(MBEDTLS_DES_C)
465 case PSA_KEY_TYPE_DES:
466 if( bits != 64 && bits != 128 && bits != 192 )
467 return( PSA_ERROR_INVALID_ARGUMENT );
468 break;
469#endif
470#if defined(MBEDTLS_ARC4_C)
471 case PSA_KEY_TYPE_ARC4:
472 if( bits < 8 || bits > 2048 )
473 return( PSA_ERROR_INVALID_ARGUMENT );
474 break;
475#endif
476 default:
477 return( PSA_ERROR_NOT_SUPPORTED );
478 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200479 if( bits % 8 != 0 )
480 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200481
482 /* Allocate memory for the key */
483 raw->bytes = PSA_BITS_TO_BYTES( bits );
484 raw->data = mbedtls_calloc( 1, raw->bytes );
485 if( raw->data == NULL )
486 {
487 raw->bytes = 0;
488 return( PSA_ERROR_INSUFFICIENT_MEMORY );
489 }
490 return( PSA_SUCCESS );
491}
492
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200493#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100494/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
495 * that are not a multiple of 8) well. For example, there is only
496 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
497 * way to return the exact bit size of a key.
498 * To keep things simple, reject non-byte-aligned key sizes. */
499static psa_status_t psa_check_rsa_key_byte_aligned(
500 const mbedtls_rsa_context *rsa )
501{
502 mbedtls_mpi n;
503 psa_status_t status;
504 mbedtls_mpi_init( &n );
505 status = mbedtls_to_psa_error(
506 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
507 if( status == PSA_SUCCESS )
508 {
509 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
510 status = PSA_ERROR_NOT_SUPPORTED;
511 }
512 mbedtls_mpi_free( &n );
513 return( status );
514}
515
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000516static psa_status_t psa_import_rsa_key( psa_key_type_t type,
517 const uint8_t *data,
518 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200519 mbedtls_rsa_context **p_rsa )
520{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000521 psa_status_t status;
522 mbedtls_pk_context pk;
523 mbedtls_rsa_context *rsa;
524 size_t bits;
525
526 mbedtls_pk_init( &pk );
527
528 /* Parse the data. */
529 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
530 status = mbedtls_to_psa_error(
531 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200532 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000533 status = mbedtls_to_psa_error(
534 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
535 if( status != PSA_SUCCESS )
536 goto exit;
537
538 /* We have something that the pkparse module recognizes. If it is a
539 * valid RSA key, store it. */
540 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200541 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000542 status = PSA_ERROR_INVALID_ARGUMENT;
543 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200544 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000545
546 rsa = mbedtls_pk_rsa( pk );
547 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
548 * supports non-byte-aligned key sizes, but not well. For example,
549 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
550 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
551 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
552 {
553 status = PSA_ERROR_NOT_SUPPORTED;
554 goto exit;
555 }
556 status = psa_check_rsa_key_byte_aligned( rsa );
557
558exit:
559 /* Free the content of the pk object only on error. */
560 if( status != PSA_SUCCESS )
561 {
562 mbedtls_pk_free( &pk );
563 return( status );
564 }
565
566 /* On success, store the content of the object in the RSA context. */
567 *p_rsa = rsa;
568
569 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200570}
571#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
572
Jaeden Ameroccdce902019-01-10 11:42:27 +0000573#if defined(MBEDTLS_ECP_C)
574
575/* Import a public key given as the uncompressed representation defined by SEC1
576 * 2.3.3 as the content of an ECPoint. */
577static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
578 const uint8_t *data,
579 size_t data_length,
580 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200581{
Jaeden Ameroccdce902019-01-10 11:42:27 +0000582 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
583 mbedtls_ecp_keypair *ecp = NULL;
584 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
585
586 *p_ecp = NULL;
587 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
588 if( ecp == NULL )
589 return( PSA_ERROR_INSUFFICIENT_MEMORY );
590 mbedtls_ecp_keypair_init( ecp );
591
592 /* Load the group. */
593 status = mbedtls_to_psa_error(
594 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
595 if( status != PSA_SUCCESS )
596 goto exit;
597 /* Load the public value. */
598 status = mbedtls_to_psa_error(
599 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
600 data, data_length ) );
601 if( status != PSA_SUCCESS )
602 goto exit;
603
604 /* Check that the point is on the curve. */
605 status = mbedtls_to_psa_error(
606 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
607 if( status != PSA_SUCCESS )
608 goto exit;
609
610 *p_ecp = ecp;
611 return( PSA_SUCCESS );
612
613exit:
614 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200615 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000616 mbedtls_ecp_keypair_free( ecp );
617 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200618 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000619 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200620}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000621#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200622
Gilles Peskinef76aa772018-10-29 19:24:33 +0100623#if defined(MBEDTLS_ECP_C)
624/* Import a private key given as a byte string which is the private value
625 * in big-endian order. */
626static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
627 const uint8_t *data,
628 size_t data_length,
629 mbedtls_ecp_keypair **p_ecp )
630{
631 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
632 mbedtls_ecp_keypair *ecp = NULL;
633 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
634
635 *p_ecp = NULL;
636 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
637 if( ecp == NULL )
638 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Jaeden Amero83d29392019-01-10 20:17:42 +0000639 mbedtls_ecp_keypair_init( ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100640
641 /* Load the group. */
642 status = mbedtls_to_psa_error(
643 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
644 if( status != PSA_SUCCESS )
645 goto exit;
646 /* Load the secret value. */
647 status = mbedtls_to_psa_error(
648 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
649 if( status != PSA_SUCCESS )
650 goto exit;
651 /* Validate the private key. */
652 status = mbedtls_to_psa_error(
653 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
654 if( status != PSA_SUCCESS )
655 goto exit;
656 /* Calculate the public key from the private key. */
657 status = mbedtls_to_psa_error(
658 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
659 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
660 if( status != PSA_SUCCESS )
661 goto exit;
662
663 *p_ecp = ecp;
664 return( PSA_SUCCESS );
665
666exit:
667 if( ecp != NULL )
668 {
669 mbedtls_ecp_keypair_free( ecp );
670 mbedtls_free( ecp );
671 }
672 return( status );
673}
674#endif /* defined(MBEDTLS_ECP_C) */
675
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100676/** Import key data into a slot. `slot->type` must have been set
677 * previously. This function assumes that the slot does not contain
678 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100679psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
680 const uint8_t *data,
681 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100682{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200683 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100684
Darryl Green940d72c2018-07-13 13:18:51 +0100685 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100686 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100687 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100688 if( data_length > SIZE_MAX / 8 )
689 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100690 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200691 PSA_BYTES_TO_BITS( data_length ),
692 &slot->data.raw );
693 if( status != PSA_SUCCESS )
694 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200695 if( data_length != 0 )
696 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100697 }
698 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100699#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100700 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100701 {
Darryl Green940d72c2018-07-13 13:18:51 +0100702 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100703 data, data_length,
704 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100705 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000706 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100707 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000708 status = psa_import_ec_public_key(
709 PSA_KEY_TYPE_GET_CURVE( slot->type ),
710 data, data_length,
711 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100712 }
713 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100714#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000715#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
716 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100717 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000718 status = psa_import_rsa_key( slot->type,
719 data, data_length,
720 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100721 }
722 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000723#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100724 {
725 return( PSA_ERROR_NOT_SUPPORTED );
726 }
Jaeden Ameroc67200d2019-01-14 13:12:39 +0000727 return( status );
Darryl Green940d72c2018-07-13 13:18:51 +0100728}
729
Darryl Green06fd18d2018-07-16 11:21:11 +0100730/* Retrieve an empty key slot (slot with no key data, but possibly
731 * with some metadata such as a policy). */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100732static psa_status_t psa_get_empty_key_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100733 psa_key_slot_t **p_slot )
Darryl Green06fd18d2018-07-16 11:21:11 +0100734{
735 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100736 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100737
738 *p_slot = NULL;
739
Gilles Peskinec5487a82018-12-03 18:08:14 +0100740 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100741 if( status != PSA_SUCCESS )
742 return( status );
743
744 if( slot->type != PSA_KEY_TYPE_NONE )
745 return( PSA_ERROR_OCCUPIED_SLOT );
746
747 *p_slot = slot;
748 return( status );
749}
750
Gilles Peskinef603c712019-01-19 13:40:11 +0100751/** Calculate the intersection of two algorithm usage policies.
752 *
753 * Return 0 (which allows no operation) on incompatibility.
754 */
755static psa_algorithm_t psa_key_policy_algorithm_intersection(
756 psa_algorithm_t alg1,
757 psa_algorithm_t alg2 )
758{
759 /* Common case: the policy only allows alg. */
760 if( alg1 == alg2 )
761 return( alg1 );
762 /* If the policies are from the same hash-and-sign family, check
763 * if one is a wildcard. If so the other has the specific algorithm. */
764 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
765 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
766 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
767 {
768 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
769 return( alg2 );
770 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
771 return( alg1 );
772 }
773 /* If the policies are incompatible, allow nothing. */
774 return( 0 );
775}
776
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100777/** Test whether a policy permits an algorithm.
778 *
779 * The caller must test usage flags separately.
780 */
781static int psa_key_policy_permits( const psa_key_policy_t *policy,
782 psa_algorithm_t alg )
783{
784 /* Common case: the policy only allows alg. */
785 if( alg == policy->alg )
786 return( 1 );
787 /* If policy->alg is a hash-and-sign with a wildcard for the hash,
788 * and alg is the same hash-and-sign family with any hash,
789 * then alg is compliant with policy->alg. */
790 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) &&
791 PSA_ALG_SIGN_GET_HASH( policy->alg ) == PSA_ALG_ANY_HASH )
792 {
793 return( ( policy->alg & ~PSA_ALG_HASH_MASK ) ==
794 ( alg & ~PSA_ALG_HASH_MASK ) );
795 }
796 /* If it isn't permitted, it's forbidden. */
797 return( 0 );
798}
799
Gilles Peskinef603c712019-01-19 13:40:11 +0100800/** Restrict a key policy based on a constraint.
801 *
802 * \param[in,out] policy The policy to restrict.
803 * \param[in] constraint The policy constraint to apply.
804 *
805 * \retval #PSA_SUCCESS
806 * \c *policy contains the intersection of the original value of
807 * \c *policy and \c *constraint.
808 * \retval #PSA_ERROR_INVALID_ARGUMENT
809 * \c *policy and \c *constraint are incompatible.
810 * \c *policy is unchanged.
811 */
812static psa_status_t psa_restrict_key_policy(
813 psa_key_policy_t *policy,
814 const psa_key_policy_t *constraint )
815{
816 psa_algorithm_t intersection_alg =
817 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
818 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
819 return( PSA_ERROR_INVALID_ARGUMENT );
820 policy->usage &= constraint->usage;
821 policy->alg = intersection_alg;
822 return( PSA_SUCCESS );
823}
824
Darryl Green06fd18d2018-07-16 11:21:11 +0100825/** Retrieve a slot which must contain a key. The key must have allow all the
826 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
827 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100828static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100829 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100830 psa_key_usage_t usage,
831 psa_algorithm_t alg )
832{
833 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100834 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100835
836 *p_slot = NULL;
837
Gilles Peskinec5487a82018-12-03 18:08:14 +0100838 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100839 if( status != PSA_SUCCESS )
840 return( status );
841 if( slot->type == PSA_KEY_TYPE_NONE )
842 return( PSA_ERROR_EMPTY_SLOT );
843
844 /* Enforce that usage policy for the key slot contains all the flags
845 * required by the usage parameter. There is one exception: public
846 * keys can always be exported, so we treat public key objects as
847 * if they had the export flag. */
848 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
849 usage &= ~PSA_KEY_USAGE_EXPORT;
850 if( ( slot->policy.usage & usage ) != usage )
851 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100852
853 /* Enforce that the usage policy permits the requested algortihm. */
854 if( alg != 0 && ! psa_key_policy_permits( &slot->policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100855 return( PSA_ERROR_NOT_PERMITTED );
856
857 *p_slot = slot;
858 return( PSA_SUCCESS );
859}
Darryl Green940d72c2018-07-13 13:18:51 +0100860
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100861/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100862static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000863{
864 if( slot->type == PSA_KEY_TYPE_NONE )
865 {
866 /* No key material to clean. */
867 }
868 else if( key_type_is_raw_bytes( slot->type ) )
869 {
870 mbedtls_free( slot->data.raw.data );
871 }
872 else
873#if defined(MBEDTLS_RSA_C)
874 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
875 {
876 mbedtls_rsa_free( slot->data.rsa );
877 mbedtls_free( slot->data.rsa );
878 }
879 else
880#endif /* defined(MBEDTLS_RSA_C) */
881#if defined(MBEDTLS_ECP_C)
882 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
883 {
884 mbedtls_ecp_keypair_free( slot->data.ecp );
885 mbedtls_free( slot->data.ecp );
886 }
887 else
888#endif /* defined(MBEDTLS_ECP_C) */
889 {
890 /* Shouldn't happen: the key type is not any type that we
891 * put in. */
892 return( PSA_ERROR_TAMPERING_DETECTED );
893 }
894
895 return( PSA_SUCCESS );
896}
897
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100898/** Completely wipe a slot in memory, including its policy.
899 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100900psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100901{
902 psa_status_t status = psa_remove_key_data_from_memory( slot );
903 /* At this point, key material and other type-specific content has
904 * been wiped. Clear remaining metadata. We can call memset and not
905 * zeroize because the metadata is not particularly sensitive. */
906 memset( slot, 0, sizeof( *slot ) );
907 return( status );
908}
909
Gilles Peskinec5487a82018-12-03 18:08:14 +0100910psa_status_t psa_import_key( psa_key_handle_t handle,
Darryl Green940d72c2018-07-13 13:18:51 +0100911 psa_key_type_t type,
912 const uint8_t *data,
913 size_t data_length )
914{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100915 psa_key_slot_t *slot;
Darryl Green940d72c2018-07-13 13:18:51 +0100916 psa_status_t status;
917
Gilles Peskinec5487a82018-12-03 18:08:14 +0100918 status = psa_get_empty_key_slot( handle, &slot );
Darryl Green940d72c2018-07-13 13:18:51 +0100919 if( status != PSA_SUCCESS )
920 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100921
922 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100923
924 status = psa_import_key_into_slot( slot, data, data_length );
925 if( status != PSA_SUCCESS )
926 {
927 slot->type = PSA_KEY_TYPE_NONE;
928 return( status );
929 }
930
Darryl Greend49a4992018-06-18 17:27:26 +0100931#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
932 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
933 {
934 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +0100935 status = psa_save_persistent_key( slot->persistent_storage_id,
936 slot->type, &slot->policy, data,
Darryl Greend49a4992018-06-18 17:27:26 +0100937 data_length );
938 if( status != PSA_SUCCESS )
939 {
940 (void) psa_remove_key_data_from_memory( slot );
941 slot->type = PSA_KEY_TYPE_NONE;
942 }
943 }
944#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
945
946 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100947}
948
Gilles Peskinec5487a82018-12-03 18:08:14 +0100949psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100950{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100951 psa_key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100952 psa_status_t status = PSA_SUCCESS;
953 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100954
Gilles Peskinec5487a82018-12-03 18:08:14 +0100955 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100956 if( status != PSA_SUCCESS )
957 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100958#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
959 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
960 {
Gilles Peskine69f976b2018-11-30 18:46:56 +0100961 storage_status =
962 psa_destroy_persistent_key( slot->persistent_storage_id );
Darryl Greend49a4992018-06-18 17:27:26 +0100963 }
964#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100965 status = psa_wipe_key_slot( slot );
Darryl Greend49a4992018-06-18 17:27:26 +0100966 if( status != PSA_SUCCESS )
967 return( status );
968 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100969}
970
Gilles Peskineb870b182018-07-06 16:02:09 +0200971/* Return the size of the key in the given slot, in bits. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100972static size_t psa_get_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb870b182018-07-06 16:02:09 +0200973{
974 if( key_type_is_raw_bytes( slot->type ) )
975 return( slot->data.raw.bytes * 8 );
976#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200977 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100978 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200979#endif /* defined(MBEDTLS_RSA_C) */
980#if defined(MBEDTLS_ECP_C)
981 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
982 return( slot->data.ecp->grp.pbits );
983#endif /* defined(MBEDTLS_ECP_C) */
984 /* Shouldn't happen except on an empty slot. */
985 return( 0 );
986}
987
Gilles Peskinec5487a82018-12-03 18:08:14 +0100988psa_status_t psa_get_key_information( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +0200989 psa_key_type_t *type,
990 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100991{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100992 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200993 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100994
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100995 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200996 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100997 if( bits != NULL )
998 *bits = 0;
Gilles Peskinec5487a82018-12-03 18:08:14 +0100999 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001000 if( status != PSA_SUCCESS )
1001 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001002
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001003 if( slot->type == PSA_KEY_TYPE_NONE )
1004 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001005 if( type != NULL )
1006 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +02001007 if( bits != NULL )
1008 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001009 return( PSA_SUCCESS );
1010}
1011
Jaeden Ameroccdce902019-01-10 11:42:27 +00001012#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001013static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1014 unsigned char *buf, size_t size )
1015{
1016 int ret;
1017 unsigned char *c;
1018 size_t len = 0;
1019
1020 c = buf + size;
1021
1022 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1023
1024 return( (int) len );
1025}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001026#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001027
Gilles Peskinef603c712019-01-19 13:40:11 +01001028static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1029 uint8_t *data,
1030 size_t data_size,
1031 size_t *data_length,
1032 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001033{
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001034 *data_length = 0;
1035
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001036 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001037 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001038
Gilles Peskine48c0ea12018-06-21 14:15:31 +02001039 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001040 {
1041 if( slot->data.raw.bytes > data_size )
1042 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +01001043 if( data_size != 0 )
1044 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001045 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +01001046 memset( data + slot->data.raw.bytes, 0,
1047 data_size - slot->data.raw.bytes );
1048 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001049 *data_length = slot->data.raw.bytes;
1050 return( PSA_SUCCESS );
1051 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001052#if defined(MBEDTLS_ECP_C)
1053 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
1054 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001055 psa_status_t status;
1056
Gilles Peskine188c71e2018-10-29 19:26:02 +01001057 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1058 if( bytes > data_size )
1059 return( PSA_ERROR_BUFFER_TOO_SMALL );
1060 status = mbedtls_to_psa_error(
1061 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1062 if( status != PSA_SUCCESS )
1063 return( status );
1064 memset( data + bytes, 0, data_size - bytes );
1065 *data_length = bytes;
1066 return( PSA_SUCCESS );
1067 }
1068#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001069 else
Moran Peker17e36e12018-05-02 12:55:20 +03001070 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001071#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001072 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001073 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001074 {
Moran Pekera998bc62018-04-16 18:16:20 +03001075 mbedtls_pk_context pk;
1076 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001077 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001078 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001079#if defined(MBEDTLS_RSA_C)
1080 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001081 pk.pk_info = &mbedtls_rsa_info;
1082 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001083#else
1084 return( PSA_ERROR_NOT_SUPPORTED );
1085#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001086 }
1087 else
1088 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001089#if defined(MBEDTLS_ECP_C)
1090 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001091 pk.pk_info = &mbedtls_eckey_info;
1092 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001093#else
1094 return( PSA_ERROR_NOT_SUPPORTED );
1095#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001096 }
Moran Pekerd7326592018-05-29 16:56:39 +03001097 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001098 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001099 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001100 }
Moran Peker17e36e12018-05-02 12:55:20 +03001101 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001102 {
Moran Peker17e36e12018-05-02 12:55:20 +03001103 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001104 }
Moran Peker60364322018-04-29 11:34:58 +03001105 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001106 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001107 /* If data_size is 0 then data may be NULL and then the
1108 * call to memset would have undefined behavior. */
1109 if( data_size != 0 )
1110 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001111 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001112 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001113 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1114 * Move the data to the beginning and erase remaining data
1115 * at the original location. */
1116 if( 2 * (size_t) ret <= data_size )
1117 {
1118 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001119 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001120 }
1121 else if( (size_t) ret < data_size )
1122 {
1123 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001124 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001125 }
Moran Pekera998bc62018-04-16 18:16:20 +03001126 *data_length = ret;
1127 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001128 }
1129 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001130#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001131 {
1132 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001133 it is valid for a special-purpose implementation to omit
1134 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001135 return( PSA_ERROR_NOT_SUPPORTED );
1136 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001137 }
1138}
1139
Gilles Peskinec5487a82018-12-03 18:08:14 +01001140psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001141 uint8_t *data,
1142 size_t data_size,
1143 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001144{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001145 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001146 psa_status_t status;
1147
1148 /* Set the key to empty now, so that even when there are errors, we always
1149 * set data_length to a value between 0 and data_size. On error, setting
1150 * the key to empty is a good choice because an empty key representation is
1151 * unlikely to be accepted anywhere. */
1152 *data_length = 0;
1153
1154 /* Export requires the EXPORT flag. There is an exception for public keys,
1155 * which don't require any flag, but psa_get_key_from_slot takes
1156 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001157 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001158 if( status != PSA_SUCCESS )
1159 return( status );
1160 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001161 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001162}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001163
Gilles Peskinec5487a82018-12-03 18:08:14 +01001164psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001165 uint8_t *data,
1166 size_t data_size,
1167 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001168{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001169 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001170 psa_status_t status;
1171
1172 /* Set the key to empty now, so that even when there are errors, we always
1173 * set data_length to a value between 0 and data_size. On error, setting
1174 * the key to empty is a good choice because an empty key representation is
1175 * unlikely to be accepted anywhere. */
1176 *data_length = 0;
1177
1178 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001179 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001180 if( status != PSA_SUCCESS )
1181 return( status );
1182 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001183 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001184}
1185
Darryl Green0c6575a2018-11-07 16:05:30 +00001186#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine2f060a82018-12-04 17:12:32 +01001187static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t *slot,
Darryl Green0c6575a2018-11-07 16:05:30 +00001188 size_t bits )
1189{
1190 psa_status_t status;
1191 uint8_t *data;
1192 size_t key_length;
1193 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1194 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001195 if( data == NULL )
1196 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001197 /* Get key data in export format */
1198 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1199 if( status != PSA_SUCCESS )
1200 {
1201 slot->type = PSA_KEY_TYPE_NONE;
1202 goto exit;
1203 }
1204 /* Store in file location */
Gilles Peskine69f976b2018-11-30 18:46:56 +01001205 status = psa_save_persistent_key( slot->persistent_storage_id,
1206 slot->type, &slot->policy,
Darryl Green0c6575a2018-11-07 16:05:30 +00001207 data, key_length );
1208 if( status != PSA_SUCCESS )
1209 {
1210 slot->type = PSA_KEY_TYPE_NONE;
1211 }
1212exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01001213 mbedtls_platform_zeroize( data, key_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00001214 mbedtls_free( data );
1215 return( status );
1216}
1217#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1218
Gilles Peskinef603c712019-01-19 13:40:11 +01001219static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
1220 psa_key_handle_t target )
1221{
1222 psa_status_t status;
1223 uint8_t *buffer = NULL;
1224 size_t buffer_size = 0;
1225 size_t length;
1226
1227 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->type,
1228 psa_get_key_bits( source ) );
1229 buffer = mbedtls_calloc( 1, buffer_size );
Darryl Green8593bca2019-02-11 11:45:58 +00001230 if( buffer == NULL && buffer_size != 0 )
Gilles Peskine122d0022019-01-23 10:55:43 +01001231 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001232 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1233 if( status != PSA_SUCCESS )
1234 goto exit;
1235 status = psa_import_key( target, source->type, buffer, length );
1236
1237exit:
Darryl Green8096caf2019-02-11 14:03:03 +00001238 if( buffer_size != 0 )
1239 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001240 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001241 return( status );
1242}
1243
1244psa_status_t psa_copy_key(psa_key_handle_t source_handle,
1245 psa_key_handle_t target_handle,
1246 const psa_key_policy_t *constraint)
1247{
1248 psa_key_slot_t *source_slot = NULL;
1249 psa_key_slot_t *target_slot = NULL;
1250 psa_key_policy_t new_policy;
1251 psa_status_t status;
1252 status = psa_get_key_from_slot( source_handle, &source_slot, 0, 0 );
1253 if( status != PSA_SUCCESS )
1254 return( status );
1255 status = psa_get_empty_key_slot( target_handle, &target_slot );
1256 if( status != PSA_SUCCESS )
1257 return( status );
1258
1259 new_policy = target_slot->policy;
1260 status = psa_restrict_key_policy( &new_policy, &source_slot->policy );
1261 if( status != PSA_SUCCESS )
1262 return( status );
1263 if( constraint != NULL )
1264 {
1265 status = psa_restrict_key_policy( &new_policy, constraint );
1266 if( status != PSA_SUCCESS )
1267 return( status );
1268 }
1269
1270 status = psa_copy_key_material( source_slot, target_handle );
1271 if( status != PSA_SUCCESS )
1272 return( status );
1273
1274 target_slot->policy = new_policy;
1275 return( PSA_SUCCESS );
1276}
1277
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001278
1279
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001280/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001281/* Message digests */
1282/****************************************************************/
1283
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001284static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001285{
1286 switch( alg )
1287 {
1288#if defined(MBEDTLS_MD2_C)
1289 case PSA_ALG_MD2:
1290 return( &mbedtls_md2_info );
1291#endif
1292#if defined(MBEDTLS_MD4_C)
1293 case PSA_ALG_MD4:
1294 return( &mbedtls_md4_info );
1295#endif
1296#if defined(MBEDTLS_MD5_C)
1297 case PSA_ALG_MD5:
1298 return( &mbedtls_md5_info );
1299#endif
1300#if defined(MBEDTLS_RIPEMD160_C)
1301 case PSA_ALG_RIPEMD160:
1302 return( &mbedtls_ripemd160_info );
1303#endif
1304#if defined(MBEDTLS_SHA1_C)
1305 case PSA_ALG_SHA_1:
1306 return( &mbedtls_sha1_info );
1307#endif
1308#if defined(MBEDTLS_SHA256_C)
1309 case PSA_ALG_SHA_224:
1310 return( &mbedtls_sha224_info );
1311 case PSA_ALG_SHA_256:
1312 return( &mbedtls_sha256_info );
1313#endif
1314#if defined(MBEDTLS_SHA512_C)
1315 case PSA_ALG_SHA_384:
1316 return( &mbedtls_sha384_info );
1317 case PSA_ALG_SHA_512:
1318 return( &mbedtls_sha512_info );
1319#endif
1320 default:
1321 return( NULL );
1322 }
1323}
1324
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001325psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1326{
1327 switch( operation->alg )
1328 {
Gilles Peskine81736312018-06-26 15:04:31 +02001329 case 0:
1330 /* The object has (apparently) been initialized but it is not
1331 * in use. It's ok to call abort on such an object, and there's
1332 * nothing to do. */
1333 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001334#if defined(MBEDTLS_MD2_C)
1335 case PSA_ALG_MD2:
1336 mbedtls_md2_free( &operation->ctx.md2 );
1337 break;
1338#endif
1339#if defined(MBEDTLS_MD4_C)
1340 case PSA_ALG_MD4:
1341 mbedtls_md4_free( &operation->ctx.md4 );
1342 break;
1343#endif
1344#if defined(MBEDTLS_MD5_C)
1345 case PSA_ALG_MD5:
1346 mbedtls_md5_free( &operation->ctx.md5 );
1347 break;
1348#endif
1349#if defined(MBEDTLS_RIPEMD160_C)
1350 case PSA_ALG_RIPEMD160:
1351 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1352 break;
1353#endif
1354#if defined(MBEDTLS_SHA1_C)
1355 case PSA_ALG_SHA_1:
1356 mbedtls_sha1_free( &operation->ctx.sha1 );
1357 break;
1358#endif
1359#if defined(MBEDTLS_SHA256_C)
1360 case PSA_ALG_SHA_224:
1361 case PSA_ALG_SHA_256:
1362 mbedtls_sha256_free( &operation->ctx.sha256 );
1363 break;
1364#endif
1365#if defined(MBEDTLS_SHA512_C)
1366 case PSA_ALG_SHA_384:
1367 case PSA_ALG_SHA_512:
1368 mbedtls_sha512_free( &operation->ctx.sha512 );
1369 break;
1370#endif
1371 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001372 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001373 }
1374 operation->alg = 0;
1375 return( PSA_SUCCESS );
1376}
1377
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001378psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001379 psa_algorithm_t alg )
1380{
1381 int ret;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00001382
1383 /* A context must be freshly initialized before it can be set up. */
1384 if( operation->alg != 0 )
1385 {
1386 return( PSA_ERROR_BAD_STATE );
1387 }
1388
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001389 switch( alg )
1390 {
1391#if defined(MBEDTLS_MD2_C)
1392 case PSA_ALG_MD2:
1393 mbedtls_md2_init( &operation->ctx.md2 );
1394 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1395 break;
1396#endif
1397#if defined(MBEDTLS_MD4_C)
1398 case PSA_ALG_MD4:
1399 mbedtls_md4_init( &operation->ctx.md4 );
1400 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1401 break;
1402#endif
1403#if defined(MBEDTLS_MD5_C)
1404 case PSA_ALG_MD5:
1405 mbedtls_md5_init( &operation->ctx.md5 );
1406 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1407 break;
1408#endif
1409#if defined(MBEDTLS_RIPEMD160_C)
1410 case PSA_ALG_RIPEMD160:
1411 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1412 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1413 break;
1414#endif
1415#if defined(MBEDTLS_SHA1_C)
1416 case PSA_ALG_SHA_1:
1417 mbedtls_sha1_init( &operation->ctx.sha1 );
1418 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1419 break;
1420#endif
1421#if defined(MBEDTLS_SHA256_C)
1422 case PSA_ALG_SHA_224:
1423 mbedtls_sha256_init( &operation->ctx.sha256 );
1424 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1425 break;
1426 case PSA_ALG_SHA_256:
1427 mbedtls_sha256_init( &operation->ctx.sha256 );
1428 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1429 break;
1430#endif
1431#if defined(MBEDTLS_SHA512_C)
1432 case PSA_ALG_SHA_384:
1433 mbedtls_sha512_init( &operation->ctx.sha512 );
1434 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1435 break;
1436 case PSA_ALG_SHA_512:
1437 mbedtls_sha512_init( &operation->ctx.sha512 );
1438 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1439 break;
1440#endif
1441 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001442 return( PSA_ALG_IS_HASH( alg ) ?
1443 PSA_ERROR_NOT_SUPPORTED :
1444 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001445 }
1446 if( ret == 0 )
1447 operation->alg = alg;
1448 else
1449 psa_hash_abort( operation );
1450 return( mbedtls_to_psa_error( ret ) );
1451}
1452
1453psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1454 const uint8_t *input,
1455 size_t input_length )
1456{
1457 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001458
1459 /* Don't require hash implementations to behave correctly on a
1460 * zero-length input, which may have an invalid pointer. */
1461 if( input_length == 0 )
1462 return( PSA_SUCCESS );
1463
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001464 switch( operation->alg )
1465 {
1466#if defined(MBEDTLS_MD2_C)
1467 case PSA_ALG_MD2:
1468 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1469 input, input_length );
1470 break;
1471#endif
1472#if defined(MBEDTLS_MD4_C)
1473 case PSA_ALG_MD4:
1474 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1475 input, input_length );
1476 break;
1477#endif
1478#if defined(MBEDTLS_MD5_C)
1479 case PSA_ALG_MD5:
1480 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1481 input, input_length );
1482 break;
1483#endif
1484#if defined(MBEDTLS_RIPEMD160_C)
1485 case PSA_ALG_RIPEMD160:
1486 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1487 input, input_length );
1488 break;
1489#endif
1490#if defined(MBEDTLS_SHA1_C)
1491 case PSA_ALG_SHA_1:
1492 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1493 input, input_length );
1494 break;
1495#endif
1496#if defined(MBEDTLS_SHA256_C)
1497 case PSA_ALG_SHA_224:
1498 case PSA_ALG_SHA_256:
1499 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1500 input, input_length );
1501 break;
1502#endif
1503#if defined(MBEDTLS_SHA512_C)
1504 case PSA_ALG_SHA_384:
1505 case PSA_ALG_SHA_512:
1506 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1507 input, input_length );
1508 break;
1509#endif
1510 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001511 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001512 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001513
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001514 if( ret != 0 )
1515 psa_hash_abort( operation );
1516 return( mbedtls_to_psa_error( ret ) );
1517}
1518
1519psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1520 uint8_t *hash,
1521 size_t hash_size,
1522 size_t *hash_length )
1523{
itayzafrir40835d42018-08-02 13:14:17 +03001524 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001525 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001526 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001527
1528 /* Fill the output buffer with something that isn't a valid hash
1529 * (barring an attack on the hash and deliberately-crafted input),
1530 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001531 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001532 /* If hash_size is 0 then hash may be NULL and then the
1533 * call to memset would have undefined behavior. */
1534 if( hash_size != 0 )
1535 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001536
1537 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001538 {
1539 status = PSA_ERROR_BUFFER_TOO_SMALL;
1540 goto exit;
1541 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001542
1543 switch( operation->alg )
1544 {
1545#if defined(MBEDTLS_MD2_C)
1546 case PSA_ALG_MD2:
1547 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1548 break;
1549#endif
1550#if defined(MBEDTLS_MD4_C)
1551 case PSA_ALG_MD4:
1552 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1553 break;
1554#endif
1555#if defined(MBEDTLS_MD5_C)
1556 case PSA_ALG_MD5:
1557 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1558 break;
1559#endif
1560#if defined(MBEDTLS_RIPEMD160_C)
1561 case PSA_ALG_RIPEMD160:
1562 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1563 break;
1564#endif
1565#if defined(MBEDTLS_SHA1_C)
1566 case PSA_ALG_SHA_1:
1567 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1568 break;
1569#endif
1570#if defined(MBEDTLS_SHA256_C)
1571 case PSA_ALG_SHA_224:
1572 case PSA_ALG_SHA_256:
1573 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1574 break;
1575#endif
1576#if defined(MBEDTLS_SHA512_C)
1577 case PSA_ALG_SHA_384:
1578 case PSA_ALG_SHA_512:
1579 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1580 break;
1581#endif
1582 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00001583 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001584 }
itayzafrir40835d42018-08-02 13:14:17 +03001585 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001586
itayzafrir40835d42018-08-02 13:14:17 +03001587exit:
1588 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001589 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001590 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001591 return( psa_hash_abort( operation ) );
1592 }
1593 else
1594 {
1595 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001596 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001597 }
1598}
1599
Gilles Peskine2d277862018-06-18 15:41:12 +02001600psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1601 const uint8_t *hash,
1602 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001603{
1604 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1605 size_t actual_hash_length;
1606 psa_status_t status = psa_hash_finish( operation,
1607 actual_hash, sizeof( actual_hash ),
1608 &actual_hash_length );
1609 if( status != PSA_SUCCESS )
1610 return( status );
1611 if( actual_hash_length != hash_length )
1612 return( PSA_ERROR_INVALID_SIGNATURE );
1613 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1614 return( PSA_ERROR_INVALID_SIGNATURE );
1615 return( PSA_SUCCESS );
1616}
1617
Gilles Peskineeb35d782019-01-22 17:56:16 +01001618psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
1619 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01001620{
1621 if( target_operation->alg != 0 )
1622 return( PSA_ERROR_BAD_STATE );
1623
1624 switch( source_operation->alg )
1625 {
1626 case 0:
1627 return( PSA_ERROR_BAD_STATE );
1628#if defined(MBEDTLS_MD2_C)
1629 case PSA_ALG_MD2:
1630 mbedtls_md2_clone( &target_operation->ctx.md2,
1631 &source_operation->ctx.md2 );
1632 break;
1633#endif
1634#if defined(MBEDTLS_MD4_C)
1635 case PSA_ALG_MD4:
1636 mbedtls_md4_clone( &target_operation->ctx.md4,
1637 &source_operation->ctx.md4 );
1638 break;
1639#endif
1640#if defined(MBEDTLS_MD5_C)
1641 case PSA_ALG_MD5:
1642 mbedtls_md5_clone( &target_operation->ctx.md5,
1643 &source_operation->ctx.md5 );
1644 break;
1645#endif
1646#if defined(MBEDTLS_RIPEMD160_C)
1647 case PSA_ALG_RIPEMD160:
1648 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
1649 &source_operation->ctx.ripemd160 );
1650 break;
1651#endif
1652#if defined(MBEDTLS_SHA1_C)
1653 case PSA_ALG_SHA_1:
1654 mbedtls_sha1_clone( &target_operation->ctx.sha1,
1655 &source_operation->ctx.sha1 );
1656 break;
1657#endif
1658#if defined(MBEDTLS_SHA256_C)
1659 case PSA_ALG_SHA_224:
1660 case PSA_ALG_SHA_256:
1661 mbedtls_sha256_clone( &target_operation->ctx.sha256,
1662 &source_operation->ctx.sha256 );
1663 break;
1664#endif
1665#if defined(MBEDTLS_SHA512_C)
1666 case PSA_ALG_SHA_384:
1667 case PSA_ALG_SHA_512:
1668 mbedtls_sha512_clone( &target_operation->ctx.sha512,
1669 &source_operation->ctx.sha512 );
1670 break;
1671#endif
1672 default:
1673 return( PSA_ERROR_NOT_SUPPORTED );
1674 }
1675
1676 target_operation->alg = source_operation->alg;
1677 return( PSA_SUCCESS );
1678}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001679
1680
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001681/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001682/* MAC */
1683/****************************************************************/
1684
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001685static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001686 psa_algorithm_t alg,
1687 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001688 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001689 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001690{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001691 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001692 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001693
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001694 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001695 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001696
Gilles Peskine8c9def32018-02-08 10:02:12 +01001697 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1698 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001699 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001700 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001701 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001702 mode = MBEDTLS_MODE_STREAM;
1703 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001704 case PSA_ALG_CTR:
1705 mode = MBEDTLS_MODE_CTR;
1706 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001707 case PSA_ALG_CFB:
1708 mode = MBEDTLS_MODE_CFB;
1709 break;
1710 case PSA_ALG_OFB:
1711 mode = MBEDTLS_MODE_OFB;
1712 break;
1713 case PSA_ALG_CBC_NO_PADDING:
1714 mode = MBEDTLS_MODE_CBC;
1715 break;
1716 case PSA_ALG_CBC_PKCS7:
1717 mode = MBEDTLS_MODE_CBC;
1718 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001719 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001720 mode = MBEDTLS_MODE_CCM;
1721 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001722 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001723 mode = MBEDTLS_MODE_GCM;
1724 break;
1725 default:
1726 return( NULL );
1727 }
1728 }
1729 else if( alg == PSA_ALG_CMAC )
1730 mode = MBEDTLS_MODE_ECB;
1731 else if( alg == PSA_ALG_GMAC )
1732 mode = MBEDTLS_MODE_GCM;
1733 else
1734 return( NULL );
1735
1736 switch( key_type )
1737 {
1738 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001739 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001740 break;
1741 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001742 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1743 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001744 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001745 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001746 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001747 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001748 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1749 * but two-key Triple-DES is functionally three-key Triple-DES
1750 * with K1=K3, so that's how we present it to mbedtls. */
1751 if( key_bits == 128 )
1752 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001753 break;
1754 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001755 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001756 break;
1757 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001758 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001759 break;
1760 default:
1761 return( NULL );
1762 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001763 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001764 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001765
Jaeden Amero23bbb752018-06-26 14:16:54 +01001766 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1767 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001768}
1769
Gilles Peskinea05219c2018-11-16 16:02:56 +01001770#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001771static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001772{
Gilles Peskine2d277862018-06-18 15:41:12 +02001773 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001774 {
1775 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001776 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001777 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001778 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001779 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001780 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001781 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001782 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001783 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001784 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001785 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001786 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001787 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001788 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001789 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001790 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001791 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001792 return( 128 );
1793 default:
1794 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001795 }
1796}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001797#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001798
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001799/* Initialize the MAC operation structure. Once this function has been
1800 * called, psa_mac_abort can run and will do the right thing. */
1801static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1802 psa_algorithm_t alg )
1803{
1804 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1805
1806 operation->alg = alg;
1807 operation->key_set = 0;
1808 operation->iv_set = 0;
1809 operation->iv_required = 0;
1810 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001811 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001812
1813#if defined(MBEDTLS_CMAC_C)
1814 if( alg == PSA_ALG_CMAC )
1815 {
1816 operation->iv_required = 0;
1817 mbedtls_cipher_init( &operation->ctx.cmac );
1818 status = PSA_SUCCESS;
1819 }
1820 else
1821#endif /* MBEDTLS_CMAC_C */
1822#if defined(MBEDTLS_MD_C)
1823 if( PSA_ALG_IS_HMAC( operation->alg ) )
1824 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001825 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1826 operation->ctx.hmac.hash_ctx.alg = 0;
1827 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001828 }
1829 else
1830#endif /* MBEDTLS_MD_C */
1831 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001832 if( ! PSA_ALG_IS_MAC( alg ) )
1833 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001834 }
1835
1836 if( status != PSA_SUCCESS )
1837 memset( operation, 0, sizeof( *operation ) );
1838 return( status );
1839}
1840
Gilles Peskine01126fa2018-07-12 17:04:55 +02001841#if defined(MBEDTLS_MD_C)
1842static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1843{
Gilles Peskine3f108122018-12-07 18:14:53 +01001844 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001845 return( psa_hash_abort( &hmac->hash_ctx ) );
1846}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001847
1848static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1849{
1850 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1851 memset( hmac, 0, sizeof( *hmac ) );
1852}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001853#endif /* MBEDTLS_MD_C */
1854
Gilles Peskine8c9def32018-02-08 10:02:12 +01001855psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1856{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001857 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001858 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001859 /* The object has (apparently) been initialized but it is not
1860 * in use. It's ok to call abort on such an object, and there's
1861 * nothing to do. */
1862 return( PSA_SUCCESS );
1863 }
1864 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001865#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001866 if( operation->alg == PSA_ALG_CMAC )
1867 {
1868 mbedtls_cipher_free( &operation->ctx.cmac );
1869 }
1870 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001871#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001872#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001873 if( PSA_ALG_IS_HMAC( operation->alg ) )
1874 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001875 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001876 }
1877 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001878#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001879 {
1880 /* Sanity check (shouldn't happen: operation->alg should
1881 * always have been initialized to a valid value). */
1882 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001883 }
Moran Peker41deec42018-04-04 15:43:05 +03001884
Gilles Peskine8c9def32018-02-08 10:02:12 +01001885 operation->alg = 0;
1886 operation->key_set = 0;
1887 operation->iv_set = 0;
1888 operation->iv_required = 0;
1889 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001890 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001891
Gilles Peskine8c9def32018-02-08 10:02:12 +01001892 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001893
1894bad_state:
1895 /* If abort is called on an uninitialized object, we can't trust
1896 * anything. Wipe the object in case it contains confidential data.
1897 * This may result in a memory leak if a pointer gets overwritten,
1898 * but it's too late to do anything about this. */
1899 memset( operation, 0, sizeof( *operation ) );
1900 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001901}
1902
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001903#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001904static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001905 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01001906 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001907 const mbedtls_cipher_info_t *cipher_info )
1908{
1909 int ret;
1910
1911 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001912
1913 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1914 if( ret != 0 )
1915 return( ret );
1916
1917 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1918 slot->data.raw.data,
1919 key_bits );
1920 return( ret );
1921}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001922#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001923
Gilles Peskine248051a2018-06-20 16:09:38 +02001924#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001925static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1926 const uint8_t *key,
1927 size_t key_length,
1928 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001929{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001930 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001931 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001932 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001933 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001934 psa_status_t status;
1935
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001936 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1937 * overflow below. This should never trigger if the hash algorithm
1938 * is implemented correctly. */
1939 /* The size checks against the ipad and opad buffers cannot be written
1940 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1941 * because that triggers -Wlogical-op on GCC 7.3. */
1942 if( block_size > sizeof( ipad ) )
1943 return( PSA_ERROR_NOT_SUPPORTED );
1944 if( block_size > sizeof( hmac->opad ) )
1945 return( PSA_ERROR_NOT_SUPPORTED );
1946 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001947 return( PSA_ERROR_NOT_SUPPORTED );
1948
Gilles Peskined223b522018-06-11 18:12:58 +02001949 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001950 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001951 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001952 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001953 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001954 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001955 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001956 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001957 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001958 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001959 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001960 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001961 }
Gilles Peskine96889972018-07-12 17:07:03 +02001962 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1963 * but it is permitted. It is common when HMAC is used in HKDF, for
1964 * example. Don't call `memcpy` in the 0-length because `key` could be
1965 * an invalid pointer which would make the behavior undefined. */
1966 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001967 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001968
Gilles Peskined223b522018-06-11 18:12:58 +02001969 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1970 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001971 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001972 ipad[i] ^= 0x36;
1973 memset( ipad + key_length, 0x36, block_size - key_length );
1974
1975 /* Copy the key material from ipad to opad, flipping the requisite bits,
1976 * and filling the rest of opad with the requisite constant. */
1977 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001978 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1979 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001980
Gilles Peskine01126fa2018-07-12 17:04:55 +02001981 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001982 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001983 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001984
Gilles Peskine01126fa2018-07-12 17:04:55 +02001985 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001986
1987cleanup:
Gilles Peskine3f108122018-12-07 18:14:53 +01001988 mbedtls_platform_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001989
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001990 return( status );
1991}
Gilles Peskine248051a2018-06-20 16:09:38 +02001992#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001993
Gilles Peskine89167cb2018-07-08 20:12:23 +02001994static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01001995 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02001996 psa_algorithm_t alg,
1997 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001998{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001999 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002000 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002001 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002002 psa_key_usage_t usage =
2003 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02002004 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002005 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002006
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002007 /* A context must be freshly initialized before it can be set up. */
2008 if( operation->alg != 0 )
2009 {
2010 return( PSA_ERROR_BAD_STATE );
2011 }
2012
Gilles Peskined911eb72018-08-14 15:18:45 +02002013 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002014 if( status != PSA_SUCCESS )
2015 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002016 if( is_sign )
2017 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002018
Gilles Peskinec5487a82018-12-03 18:08:14 +01002019 status = psa_get_key_from_slot( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002020 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002021 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002022 key_bits = psa_get_key_bits( slot );
2023
Gilles Peskine8c9def32018-02-08 10:02:12 +01002024#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002025 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002026 {
2027 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002028 mbedtls_cipher_info_from_psa( full_length_alg,
2029 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002030 int ret;
2031 if( cipher_info == NULL )
2032 {
2033 status = PSA_ERROR_NOT_SUPPORTED;
2034 goto exit;
2035 }
2036 operation->mac_size = cipher_info->block_size;
2037 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2038 status = mbedtls_to_psa_error( ret );
2039 }
2040 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002041#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002042#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002043 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002044 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002045 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002046 if( hash_alg == 0 )
2047 {
2048 status = PSA_ERROR_NOT_SUPPORTED;
2049 goto exit;
2050 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002051
2052 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2053 /* Sanity check. This shouldn't fail on a valid configuration. */
2054 if( operation->mac_size == 0 ||
2055 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2056 {
2057 status = PSA_ERROR_NOT_SUPPORTED;
2058 goto exit;
2059 }
2060
Gilles Peskine01126fa2018-07-12 17:04:55 +02002061 if( slot->type != PSA_KEY_TYPE_HMAC )
2062 {
2063 status = PSA_ERROR_INVALID_ARGUMENT;
2064 goto exit;
2065 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002066
Gilles Peskine01126fa2018-07-12 17:04:55 +02002067 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2068 slot->data.raw.data,
2069 slot->data.raw.bytes,
2070 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002071 }
2072 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002073#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002074 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002075 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002076 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002077 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002078
Gilles Peskined911eb72018-08-14 15:18:45 +02002079 if( truncated == 0 )
2080 {
2081 /* The "normal" case: untruncated algorithm. Nothing to do. */
2082 }
2083 else if( truncated < 4 )
2084 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002085 /* A very short MAC is too short for security since it can be
2086 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2087 * so we make this our minimum, even though 32 bits is still
2088 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002089 status = PSA_ERROR_NOT_SUPPORTED;
2090 }
2091 else if( truncated > operation->mac_size )
2092 {
2093 /* It's impossible to "truncate" to a larger length. */
2094 status = PSA_ERROR_INVALID_ARGUMENT;
2095 }
2096 else
2097 operation->mac_size = truncated;
2098
Gilles Peskinefbfac682018-07-08 20:51:54 +02002099exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002100 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002101 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002102 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002103 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002104 else
2105 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002106 operation->key_set = 1;
2107 }
2108 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002109}
2110
Gilles Peskine89167cb2018-07-08 20:12:23 +02002111psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002112 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002113 psa_algorithm_t alg )
2114{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002115 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002116}
2117
2118psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002119 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002120 psa_algorithm_t alg )
2121{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002122 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002123}
2124
Gilles Peskine8c9def32018-02-08 10:02:12 +01002125psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2126 const uint8_t *input,
2127 size_t input_length )
2128{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002129 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002130 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002131 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002132 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002133 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002134 operation->has_input = 1;
2135
Gilles Peskine8c9def32018-02-08 10:02:12 +01002136#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002137 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002138 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002139 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2140 input, input_length );
2141 status = mbedtls_to_psa_error( ret );
2142 }
2143 else
2144#endif /* MBEDTLS_CMAC_C */
2145#if defined(MBEDTLS_MD_C)
2146 if( PSA_ALG_IS_HMAC( operation->alg ) )
2147 {
2148 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2149 input_length );
2150 }
2151 else
2152#endif /* MBEDTLS_MD_C */
2153 {
2154 /* This shouldn't happen if `operation` was initialized by
2155 * a setup function. */
2156 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002157 }
2158
Gilles Peskinefbfac682018-07-08 20:51:54 +02002159cleanup:
2160 if( status != PSA_SUCCESS )
2161 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002162 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002163}
2164
Gilles Peskine01126fa2018-07-12 17:04:55 +02002165#if defined(MBEDTLS_MD_C)
2166static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2167 uint8_t *mac,
2168 size_t mac_size )
2169{
2170 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
2171 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2172 size_t hash_size = 0;
2173 size_t block_size = psa_get_hash_block_size( hash_alg );
2174 psa_status_t status;
2175
Gilles Peskine01126fa2018-07-12 17:04:55 +02002176 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2177 if( status != PSA_SUCCESS )
2178 return( status );
2179 /* From here on, tmp needs to be wiped. */
2180
2181 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2182 if( status != PSA_SUCCESS )
2183 goto exit;
2184
2185 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2186 if( status != PSA_SUCCESS )
2187 goto exit;
2188
2189 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2190 if( status != PSA_SUCCESS )
2191 goto exit;
2192
Gilles Peskined911eb72018-08-14 15:18:45 +02002193 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2194 if( status != PSA_SUCCESS )
2195 goto exit;
2196
2197 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002198
2199exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002200 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002201 return( status );
2202}
2203#endif /* MBEDTLS_MD_C */
2204
mohammad16036df908f2018-04-02 08:34:15 -07002205static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002206 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002207 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002208{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002209 if( ! operation->key_set )
2210 return( PSA_ERROR_BAD_STATE );
2211 if( operation->iv_required && ! operation->iv_set )
2212 return( PSA_ERROR_BAD_STATE );
2213
Gilles Peskine8c9def32018-02-08 10:02:12 +01002214 if( mac_size < operation->mac_size )
2215 return( PSA_ERROR_BUFFER_TOO_SMALL );
2216
Gilles Peskine8c9def32018-02-08 10:02:12 +01002217#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002218 if( operation->alg == PSA_ALG_CMAC )
2219 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002220 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2221 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2222 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002223 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002224 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002225 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002226 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002227 else
2228#endif /* MBEDTLS_CMAC_C */
2229#if defined(MBEDTLS_MD_C)
2230 if( PSA_ALG_IS_HMAC( operation->alg ) )
2231 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002232 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002233 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002234 }
2235 else
2236#endif /* MBEDTLS_MD_C */
2237 {
2238 /* This shouldn't happen if `operation` was initialized by
2239 * a setup function. */
2240 return( PSA_ERROR_BAD_STATE );
2241 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002242}
2243
Gilles Peskineacd4be32018-07-08 19:56:25 +02002244psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2245 uint8_t *mac,
2246 size_t mac_size,
2247 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002248{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002249 psa_status_t status;
2250
Jaeden Amero252ef282019-02-15 14:05:35 +00002251 if( operation->alg == 0 )
2252 {
2253 return( PSA_ERROR_BAD_STATE );
2254 }
2255
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002256 /* Fill the output buffer with something that isn't a valid mac
2257 * (barring an attack on the mac and deliberately-crafted input),
2258 * in case the caller doesn't check the return status properly. */
2259 *mac_length = mac_size;
2260 /* If mac_size is 0 then mac may be NULL and then the
2261 * call to memset would have undefined behavior. */
2262 if( mac_size != 0 )
2263 memset( mac, '!', mac_size );
2264
Gilles Peskine89167cb2018-07-08 20:12:23 +02002265 if( ! operation->is_sign )
2266 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002267 status = PSA_ERROR_BAD_STATE;
2268 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002269 }
mohammad16036df908f2018-04-02 08:34:15 -07002270
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002271 status = psa_mac_finish_internal( operation, mac, mac_size );
2272
2273cleanup:
2274 if( status == PSA_SUCCESS )
2275 {
2276 status = psa_mac_abort( operation );
2277 if( status == PSA_SUCCESS )
2278 *mac_length = operation->mac_size;
2279 else
2280 memset( mac, '!', mac_size );
2281 }
2282 else
2283 psa_mac_abort( operation );
2284 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002285}
2286
Gilles Peskineacd4be32018-07-08 19:56:25 +02002287psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2288 const uint8_t *mac,
2289 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002290{
Gilles Peskine828ed142018-06-18 23:25:51 +02002291 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002292 psa_status_t status;
2293
Jaeden Amero252ef282019-02-15 14:05:35 +00002294 if( operation->alg == 0 )
2295 {
2296 return( PSA_ERROR_BAD_STATE );
2297 }
2298
Gilles Peskine89167cb2018-07-08 20:12:23 +02002299 if( operation->is_sign )
2300 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002301 status = PSA_ERROR_BAD_STATE;
2302 goto cleanup;
2303 }
2304 if( operation->mac_size != mac_length )
2305 {
2306 status = PSA_ERROR_INVALID_SIGNATURE;
2307 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002308 }
mohammad16036df908f2018-04-02 08:34:15 -07002309
2310 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002311 actual_mac, sizeof( actual_mac ) );
2312
2313 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2314 status = PSA_ERROR_INVALID_SIGNATURE;
2315
2316cleanup:
2317 if( status == PSA_SUCCESS )
2318 status = psa_mac_abort( operation );
2319 else
2320 psa_mac_abort( operation );
2321
Gilles Peskine3f108122018-12-07 18:14:53 +01002322 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002323
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002324 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002325}
2326
2327
Gilles Peskine20035e32018-02-03 22:44:14 +01002328
Gilles Peskine20035e32018-02-03 22:44:14 +01002329/****************************************************************/
2330/* Asymmetric cryptography */
2331/****************************************************************/
2332
Gilles Peskine2b450e32018-06-27 15:42:46 +02002333#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002334/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002335 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002336static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2337 size_t hash_length,
2338 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002339{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002340 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002341 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002342 *md_alg = mbedtls_md_get_type( md_info );
2343
2344 /* The Mbed TLS RSA module uses an unsigned int for hash length
2345 * parameters. Validate that it fits so that we don't risk an
2346 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002347#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002348 if( hash_length > UINT_MAX )
2349 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002350#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002351
2352#if defined(MBEDTLS_PKCS1_V15)
2353 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2354 * must be correct. */
2355 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2356 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002357 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002358 if( md_info == NULL )
2359 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002360 if( mbedtls_md_get_size( md_info ) != hash_length )
2361 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002362 }
2363#endif /* MBEDTLS_PKCS1_V15 */
2364
2365#if defined(MBEDTLS_PKCS1_V21)
2366 /* PSS requires a hash internally. */
2367 if( PSA_ALG_IS_RSA_PSS( alg ) )
2368 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002369 if( md_info == NULL )
2370 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002371 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002372#endif /* MBEDTLS_PKCS1_V21 */
2373
Gilles Peskine61b91d42018-06-08 16:09:36 +02002374 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002375}
2376
Gilles Peskine2b450e32018-06-27 15:42:46 +02002377static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2378 psa_algorithm_t alg,
2379 const uint8_t *hash,
2380 size_t hash_length,
2381 uint8_t *signature,
2382 size_t signature_size,
2383 size_t *signature_length )
2384{
2385 psa_status_t status;
2386 int ret;
2387 mbedtls_md_type_t md_alg;
2388
2389 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2390 if( status != PSA_SUCCESS )
2391 return( status );
2392
Gilles Peskine630a18a2018-06-29 17:49:35 +02002393 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002394 return( PSA_ERROR_BUFFER_TOO_SMALL );
2395
2396#if defined(MBEDTLS_PKCS1_V15)
2397 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2398 {
2399 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2400 MBEDTLS_MD_NONE );
2401 ret = mbedtls_rsa_pkcs1_sign( rsa,
2402 mbedtls_ctr_drbg_random,
2403 &global_data.ctr_drbg,
2404 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002405 md_alg,
2406 (unsigned int) hash_length,
2407 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002408 signature );
2409 }
2410 else
2411#endif /* MBEDTLS_PKCS1_V15 */
2412#if defined(MBEDTLS_PKCS1_V21)
2413 if( PSA_ALG_IS_RSA_PSS( alg ) )
2414 {
2415 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2416 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2417 mbedtls_ctr_drbg_random,
2418 &global_data.ctr_drbg,
2419 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002420 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002421 (unsigned int) hash_length,
2422 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002423 signature );
2424 }
2425 else
2426#endif /* MBEDTLS_PKCS1_V21 */
2427 {
2428 return( PSA_ERROR_INVALID_ARGUMENT );
2429 }
2430
2431 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002432 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002433 return( mbedtls_to_psa_error( ret ) );
2434}
2435
2436static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2437 psa_algorithm_t alg,
2438 const uint8_t *hash,
2439 size_t hash_length,
2440 const uint8_t *signature,
2441 size_t signature_length )
2442{
2443 psa_status_t status;
2444 int ret;
2445 mbedtls_md_type_t md_alg;
2446
2447 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2448 if( status != PSA_SUCCESS )
2449 return( status );
2450
Gilles Peskine630a18a2018-06-29 17:49:35 +02002451 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002452 return( PSA_ERROR_BUFFER_TOO_SMALL );
2453
2454#if defined(MBEDTLS_PKCS1_V15)
2455 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2456 {
2457 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2458 MBEDTLS_MD_NONE );
2459 ret = mbedtls_rsa_pkcs1_verify( rsa,
2460 mbedtls_ctr_drbg_random,
2461 &global_data.ctr_drbg,
2462 MBEDTLS_RSA_PUBLIC,
2463 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002464 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002465 hash,
2466 signature );
2467 }
2468 else
2469#endif /* MBEDTLS_PKCS1_V15 */
2470#if defined(MBEDTLS_PKCS1_V21)
2471 if( PSA_ALG_IS_RSA_PSS( alg ) )
2472 {
2473 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2474 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2475 mbedtls_ctr_drbg_random,
2476 &global_data.ctr_drbg,
2477 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002478 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002479 (unsigned int) hash_length,
2480 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002481 signature );
2482 }
2483 else
2484#endif /* MBEDTLS_PKCS1_V21 */
2485 {
2486 return( PSA_ERROR_INVALID_ARGUMENT );
2487 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002488
2489 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2490 * the rest of the signature is invalid". This has little use in
2491 * practice and PSA doesn't report this distinction. */
2492 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2493 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002494 return( mbedtls_to_psa_error( ret ) );
2495}
2496#endif /* MBEDTLS_RSA_C */
2497
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002498#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002499/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2500 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2501 * (even though these functions don't modify it). */
2502static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2503 psa_algorithm_t alg,
2504 const uint8_t *hash,
2505 size_t hash_length,
2506 uint8_t *signature,
2507 size_t signature_size,
2508 size_t *signature_length )
2509{
2510 int ret;
2511 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002512 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002513 mbedtls_mpi_init( &r );
2514 mbedtls_mpi_init( &s );
2515
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002516 if( signature_size < 2 * curve_bytes )
2517 {
2518 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2519 goto cleanup;
2520 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002521
Gilles Peskinea05219c2018-11-16 16:02:56 +01002522#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002523 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2524 {
2525 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2526 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2527 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2528 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2529 hash, hash_length,
2530 md_alg ) );
2531 }
2532 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002533#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002534 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002535 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002536 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2537 hash, hash_length,
2538 mbedtls_ctr_drbg_random,
2539 &global_data.ctr_drbg ) );
2540 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002541
2542 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2543 signature,
2544 curve_bytes ) );
2545 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2546 signature + curve_bytes,
2547 curve_bytes ) );
2548
2549cleanup:
2550 mbedtls_mpi_free( &r );
2551 mbedtls_mpi_free( &s );
2552 if( ret == 0 )
2553 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002554 return( mbedtls_to_psa_error( ret ) );
2555}
2556
2557static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2558 const uint8_t *hash,
2559 size_t hash_length,
2560 const uint8_t *signature,
2561 size_t signature_length )
2562{
2563 int ret;
2564 mbedtls_mpi r, s;
2565 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2566 mbedtls_mpi_init( &r );
2567 mbedtls_mpi_init( &s );
2568
2569 if( signature_length != 2 * curve_bytes )
2570 return( PSA_ERROR_INVALID_SIGNATURE );
2571
2572 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2573 signature,
2574 curve_bytes ) );
2575 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2576 signature + curve_bytes,
2577 curve_bytes ) );
2578
2579 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2580 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002581
2582cleanup:
2583 mbedtls_mpi_free( &r );
2584 mbedtls_mpi_free( &s );
2585 return( mbedtls_to_psa_error( ret ) );
2586}
2587#endif /* MBEDTLS_ECDSA_C */
2588
Gilles Peskinec5487a82018-12-03 18:08:14 +01002589psa_status_t psa_asymmetric_sign( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002590 psa_algorithm_t alg,
2591 const uint8_t *hash,
2592 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002593 uint8_t *signature,
2594 size_t signature_size,
2595 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002596{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002597 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002598 psa_status_t status;
2599
2600 *signature_length = signature_size;
2601
Gilles Peskinec5487a82018-12-03 18:08:14 +01002602 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002603 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002604 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002605 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002606 {
2607 status = PSA_ERROR_INVALID_ARGUMENT;
2608 goto exit;
2609 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002610
Gilles Peskine20035e32018-02-03 22:44:14 +01002611#if defined(MBEDTLS_RSA_C)
2612 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2613 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002614 status = psa_rsa_sign( slot->data.rsa,
2615 alg,
2616 hash, hash_length,
2617 signature, signature_size,
2618 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002619 }
2620 else
2621#endif /* defined(MBEDTLS_RSA_C) */
2622#if defined(MBEDTLS_ECP_C)
2623 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2624 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002625#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002626 if(
2627#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2628 PSA_ALG_IS_ECDSA( alg )
2629#else
2630 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2631#endif
2632 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002633 status = psa_ecdsa_sign( slot->data.ecp,
2634 alg,
2635 hash, hash_length,
2636 signature, signature_size,
2637 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002638 else
2639#endif /* defined(MBEDTLS_ECDSA_C) */
2640 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002641 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002642 }
itayzafrir5c753392018-05-08 11:18:38 +03002643 }
2644 else
2645#endif /* defined(MBEDTLS_ECP_C) */
2646 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002647 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002648 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002649
2650exit:
2651 /* Fill the unused part of the output buffer (the whole buffer on error,
2652 * the trailing part on success) with something that isn't a valid mac
2653 * (barring an attack on the mac and deliberately-crafted input),
2654 * in case the caller doesn't check the return status properly. */
2655 if( status == PSA_SUCCESS )
2656 memset( signature + *signature_length, '!',
2657 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002658 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002659 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002660 /* If signature_size is 0 then we have nothing to do. We must not call
2661 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002662 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002663}
2664
Gilles Peskinec5487a82018-12-03 18:08:14 +01002665psa_status_t psa_asymmetric_verify( psa_key_handle_t handle,
itayzafrir5c753392018-05-08 11:18:38 +03002666 psa_algorithm_t alg,
2667 const uint8_t *hash,
2668 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002669 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002670 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002671{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002672 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002673 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002674
Gilles Peskinec5487a82018-12-03 18:08:14 +01002675 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002676 if( status != PSA_SUCCESS )
2677 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002678
Gilles Peskine61b91d42018-06-08 16:09:36 +02002679#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002680 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002681 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002682 return( psa_rsa_verify( slot->data.rsa,
2683 alg,
2684 hash, hash_length,
2685 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002686 }
2687 else
2688#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002689#if defined(MBEDTLS_ECP_C)
2690 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2691 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002692#if defined(MBEDTLS_ECDSA_C)
2693 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002694 return( psa_ecdsa_verify( slot->data.ecp,
2695 hash, hash_length,
2696 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002697 else
2698#endif /* defined(MBEDTLS_ECDSA_C) */
2699 {
2700 return( PSA_ERROR_INVALID_ARGUMENT );
2701 }
itayzafrir5c753392018-05-08 11:18:38 +03002702 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002703 else
2704#endif /* defined(MBEDTLS_ECP_C) */
2705 {
2706 return( PSA_ERROR_NOT_SUPPORTED );
2707 }
2708}
2709
Gilles Peskine072ac562018-06-30 00:21:29 +02002710#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2711static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2712 mbedtls_rsa_context *rsa )
2713{
2714 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2715 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2716 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2717 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2718}
2719#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2720
Gilles Peskinec5487a82018-12-03 18:08:14 +01002721psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002722 psa_algorithm_t alg,
2723 const uint8_t *input,
2724 size_t input_length,
2725 const uint8_t *salt,
2726 size_t salt_length,
2727 uint8_t *output,
2728 size_t output_size,
2729 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002730{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002731 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002732 psa_status_t status;
2733
Darryl Green5cc689a2018-07-24 15:34:10 +01002734 (void) input;
2735 (void) input_length;
2736 (void) salt;
2737 (void) output;
2738 (void) output_size;
2739
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002740 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002741
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002742 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2743 return( PSA_ERROR_INVALID_ARGUMENT );
2744
Gilles Peskinec5487a82018-12-03 18:08:14 +01002745 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002746 if( status != PSA_SUCCESS )
2747 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002748 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2749 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002750 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002751
2752#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002753 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002754 {
2755 mbedtls_rsa_context *rsa = slot->data.rsa;
2756 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002757 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002758 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002759#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002760 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002761 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002762 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2763 mbedtls_ctr_drbg_random,
2764 &global_data.ctr_drbg,
2765 MBEDTLS_RSA_PUBLIC,
2766 input_length,
2767 input,
2768 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002769 }
2770 else
2771#endif /* MBEDTLS_PKCS1_V15 */
2772#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002773 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002774 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002775 psa_rsa_oaep_set_padding_mode( alg, rsa );
2776 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2777 mbedtls_ctr_drbg_random,
2778 &global_data.ctr_drbg,
2779 MBEDTLS_RSA_PUBLIC,
2780 salt, salt_length,
2781 input_length,
2782 input,
2783 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002784 }
2785 else
2786#endif /* MBEDTLS_PKCS1_V21 */
2787 {
2788 return( PSA_ERROR_INVALID_ARGUMENT );
2789 }
2790 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002791 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002792 return( mbedtls_to_psa_error( ret ) );
2793 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002794 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002795#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002796 {
2797 return( PSA_ERROR_NOT_SUPPORTED );
2798 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002799}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002800
Gilles Peskinec5487a82018-12-03 18:08:14 +01002801psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002802 psa_algorithm_t alg,
2803 const uint8_t *input,
2804 size_t input_length,
2805 const uint8_t *salt,
2806 size_t salt_length,
2807 uint8_t *output,
2808 size_t output_size,
2809 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002810{
Gilles Peskine2f060a82018-12-04 17:12:32 +01002811 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002812 psa_status_t status;
2813
Darryl Green5cc689a2018-07-24 15:34:10 +01002814 (void) input;
2815 (void) input_length;
2816 (void) salt;
2817 (void) output;
2818 (void) output_size;
2819
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002820 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002821
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002822 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2823 return( PSA_ERROR_INVALID_ARGUMENT );
2824
Gilles Peskinec5487a82018-12-03 18:08:14 +01002825 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002826 if( status != PSA_SUCCESS )
2827 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002828 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2829 return( PSA_ERROR_INVALID_ARGUMENT );
2830
2831#if defined(MBEDTLS_RSA_C)
2832 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2833 {
2834 mbedtls_rsa_context *rsa = slot->data.rsa;
2835 int ret;
2836
Gilles Peskine630a18a2018-06-29 17:49:35 +02002837 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002838 return( PSA_ERROR_INVALID_ARGUMENT );
2839
2840#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002841 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002842 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002843 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2844 mbedtls_ctr_drbg_random,
2845 &global_data.ctr_drbg,
2846 MBEDTLS_RSA_PRIVATE,
2847 output_length,
2848 input,
2849 output,
2850 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002851 }
2852 else
2853#endif /* MBEDTLS_PKCS1_V15 */
2854#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002855 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002856 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002857 psa_rsa_oaep_set_padding_mode( alg, rsa );
2858 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2859 mbedtls_ctr_drbg_random,
2860 &global_data.ctr_drbg,
2861 MBEDTLS_RSA_PRIVATE,
2862 salt, salt_length,
2863 output_length,
2864 input,
2865 output,
2866 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002867 }
2868 else
2869#endif /* MBEDTLS_PKCS1_V21 */
2870 {
2871 return( PSA_ERROR_INVALID_ARGUMENT );
2872 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002873
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002874 return( mbedtls_to_psa_error( ret ) );
2875 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002876 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002877#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002878 {
2879 return( PSA_ERROR_NOT_SUPPORTED );
2880 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002881}
Gilles Peskine20035e32018-02-03 22:44:14 +01002882
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002883
2884
mohammad1603503973b2018-03-12 15:59:30 +02002885/****************************************************************/
2886/* Symmetric cryptography */
2887/****************************************************************/
2888
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002889/* Initialize the cipher operation structure. Once this function has been
2890 * called, psa_cipher_abort can run and will do the right thing. */
2891static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2892 psa_algorithm_t alg )
2893{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002894 if( ! PSA_ALG_IS_CIPHER( alg ) )
2895 {
2896 memset( operation, 0, sizeof( *operation ) );
2897 return( PSA_ERROR_INVALID_ARGUMENT );
2898 }
2899
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002900 operation->alg = alg;
2901 operation->key_set = 0;
2902 operation->iv_set = 0;
2903 operation->iv_required = 1;
2904 operation->iv_size = 0;
2905 operation->block_size = 0;
2906 mbedtls_cipher_init( &operation->ctx.cipher );
2907 return( PSA_SUCCESS );
2908}
2909
Gilles Peskinee553c652018-06-04 16:22:46 +02002910static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002911 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02002912 psa_algorithm_t alg,
2913 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002914{
2915 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2916 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002917 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002918 size_t key_bits;
2919 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002920 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2921 PSA_KEY_USAGE_ENCRYPT :
2922 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002923
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002924 /* A context must be freshly initialized before it can be set up. */
2925 if( operation->alg != 0 )
2926 {
2927 return( PSA_ERROR_BAD_STATE );
2928 }
2929
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002930 status = psa_cipher_init( operation, alg );
2931 if( status != PSA_SUCCESS )
2932 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002933
Gilles Peskinec5487a82018-12-03 18:08:14 +01002934 status = psa_get_key_from_slot( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002935 if( status != PSA_SUCCESS )
2936 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002937 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002938
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002939 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002940 if( cipher_info == NULL )
2941 return( PSA_ERROR_NOT_SUPPORTED );
2942
mohammad1603503973b2018-03-12 15:59:30 +02002943 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002944 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002945 {
2946 psa_cipher_abort( operation );
2947 return( mbedtls_to_psa_error( ret ) );
2948 }
2949
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002950#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002951 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002952 {
2953 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2954 unsigned char keys[24];
2955 memcpy( keys, slot->data.raw.data, 16 );
2956 memcpy( keys + 16, slot->data.raw.data, 8 );
2957 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2958 keys,
2959 192, cipher_operation );
2960 }
2961 else
2962#endif
2963 {
2964 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2965 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002966 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002967 }
Moran Peker41deec42018-04-04 15:43:05 +03002968 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002969 {
2970 psa_cipher_abort( operation );
2971 return( mbedtls_to_psa_error( ret ) );
2972 }
2973
mohammad16038481e742018-03-18 13:57:31 +02002974#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002975 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002976 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002977 case PSA_ALG_CBC_NO_PADDING:
2978 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2979 MBEDTLS_PADDING_NONE );
2980 break;
2981 case PSA_ALG_CBC_PKCS7:
2982 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2983 MBEDTLS_PADDING_PKCS7 );
2984 break;
2985 default:
2986 /* The algorithm doesn't involve padding. */
2987 ret = 0;
2988 break;
2989 }
2990 if( ret != 0 )
2991 {
2992 psa_cipher_abort( operation );
2993 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002994 }
2995#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2996
mohammad1603503973b2018-03-12 15:59:30 +02002997 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002998 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2999 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
3000 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003001 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003002 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02003003 }
mohammad1603503973b2018-03-12 15:59:30 +02003004
Moran Peker395db872018-05-31 14:07:14 +03003005 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003006}
3007
Gilles Peskinefe119512018-07-08 21:39:34 +02003008psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003009 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003010 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003011{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003012 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003013}
3014
Gilles Peskinefe119512018-07-08 21:39:34 +02003015psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003016 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003017 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003018{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003019 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003020}
3021
Gilles Peskinefe119512018-07-08 21:39:34 +02003022psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
3023 unsigned char *iv,
3024 size_t iv_size,
3025 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003026{
itayzafrir534bd7c2018-08-02 13:56:32 +03003027 psa_status_t status;
3028 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003029 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003030 {
3031 status = PSA_ERROR_BAD_STATE;
3032 goto exit;
3033 }
Moran Peker41deec42018-04-04 15:43:05 +03003034 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003035 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003036 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003037 goto exit;
3038 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003039 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3040 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003041 if( ret != 0 )
3042 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003043 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003044 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003045 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003046
mohammad16038481e742018-03-18 13:57:31 +02003047 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003048 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003049
Moran Peker395db872018-05-31 14:07:14 +03003050exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003051 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003052 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003053 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003054}
3055
Gilles Peskinefe119512018-07-08 21:39:34 +02003056psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
3057 const unsigned char *iv,
3058 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003059{
itayzafrir534bd7c2018-08-02 13:56:32 +03003060 psa_status_t status;
3061 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003062 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003063 {
3064 status = PSA_ERROR_BAD_STATE;
3065 goto exit;
3066 }
Moran Pekera28258c2018-05-29 16:25:04 +03003067 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003068 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003069 status = PSA_ERROR_INVALID_ARGUMENT;
3070 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003071 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003072 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3073 status = mbedtls_to_psa_error( ret );
3074exit:
3075 if( status == PSA_SUCCESS )
3076 operation->iv_set = 1;
3077 else
mohammad1603503973b2018-03-12 15:59:30 +02003078 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003079 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003080}
3081
Gilles Peskinee553c652018-06-04 16:22:46 +02003082psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3083 const uint8_t *input,
3084 size_t input_length,
3085 unsigned char *output,
3086 size_t output_size,
3087 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003088{
itayzafrir534bd7c2018-08-02 13:56:32 +03003089 psa_status_t status;
3090 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003091 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003092
3093 if( operation->alg == 0 )
3094 {
3095 return( PSA_ERROR_BAD_STATE );
3096 }
3097
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003098 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003099 {
3100 /* Take the unprocessed partial block left over from previous
3101 * update calls, if any, plus the input to this call. Remove
3102 * the last partial block, if any. You get the data that will be
3103 * output in this call. */
3104 expected_output_size =
3105 ( operation->ctx.cipher.unprocessed_len + input_length )
3106 / operation->block_size * operation->block_size;
3107 }
3108 else
3109 {
3110 expected_output_size = input_length;
3111 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003112
Gilles Peskine89d789c2018-06-04 17:17:16 +02003113 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003114 {
3115 status = PSA_ERROR_BUFFER_TOO_SMALL;
3116 goto exit;
3117 }
mohammad160382759612018-03-12 18:16:40 +02003118
mohammad1603503973b2018-03-12 15:59:30 +02003119 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003120 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003121 status = mbedtls_to_psa_error( ret );
3122exit:
3123 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003124 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003125 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003126}
3127
Gilles Peskinee553c652018-06-04 16:22:46 +02003128psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3129 uint8_t *output,
3130 size_t output_size,
3131 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003132{
Janos Follath315b51c2018-07-09 16:04:51 +01003133 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
3134 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003135 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003136
mohammad1603503973b2018-03-12 15:59:30 +02003137 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003138 {
Janos Follath315b51c2018-07-09 16:04:51 +01003139 status = PSA_ERROR_BAD_STATE;
3140 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03003141 }
3142 if( operation->iv_required && ! operation->iv_set )
3143 {
Janos Follath315b51c2018-07-09 16:04:51 +01003144 status = PSA_ERROR_BAD_STATE;
3145 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03003146 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003147
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003148 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003149 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3150 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003151 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003152 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003153 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003154 }
3155
Janos Follath315b51c2018-07-09 16:04:51 +01003156 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3157 temp_output_buffer,
3158 output_length );
3159 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003160 {
Janos Follath315b51c2018-07-09 16:04:51 +01003161 status = mbedtls_to_psa_error( cipher_ret );
3162 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003163 }
Janos Follath315b51c2018-07-09 16:04:51 +01003164
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003165 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003166 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003167 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003168 memcpy( output, temp_output_buffer, *output_length );
3169 else
3170 {
Janos Follath315b51c2018-07-09 16:04:51 +01003171 status = PSA_ERROR_BUFFER_TOO_SMALL;
3172 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003173 }
mohammad1603503973b2018-03-12 15:59:30 +02003174
Gilles Peskine3f108122018-12-07 18:14:53 +01003175 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003176 status = psa_cipher_abort( operation );
3177
3178 return( status );
3179
3180error:
3181
3182 *output_length = 0;
3183
Gilles Peskine3f108122018-12-07 18:14:53 +01003184 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003185 (void) psa_cipher_abort( operation );
3186
3187 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003188}
3189
Gilles Peskinee553c652018-06-04 16:22:46 +02003190psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3191{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003192 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003193 {
3194 /* The object has (apparently) been initialized but it is not
3195 * in use. It's ok to call abort on such an object, and there's
3196 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003197 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003198 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003199
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003200 /* Sanity check (shouldn't happen: operation->alg should
3201 * always have been initialized to a valid value). */
3202 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3203 return( PSA_ERROR_BAD_STATE );
3204
mohammad1603503973b2018-03-12 15:59:30 +02003205 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003206
Moran Peker41deec42018-04-04 15:43:05 +03003207 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003208 operation->key_set = 0;
3209 operation->iv_set = 0;
3210 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003211 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003212 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003213
Moran Peker395db872018-05-31 14:07:14 +03003214 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003215}
3216
Gilles Peskinea0655c32018-04-30 17:06:50 +02003217
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003218
mohammad16038cc1cee2018-03-28 01:21:33 +03003219/****************************************************************/
3220/* Key Policy */
3221/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003222
mohammad160327010052018-07-03 13:16:15 +03003223#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02003224void psa_key_policy_set_usage( psa_key_policy_t *policy,
3225 psa_key_usage_t usage,
3226 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003227{
mohammad16034eed7572018-03-28 05:14:59 -07003228 policy->usage = usage;
3229 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003230}
3231
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003232psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003233{
mohammad16036df908f2018-04-02 08:34:15 -07003234 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003235}
3236
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003237psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003238{
mohammad16036df908f2018-04-02 08:34:15 -07003239 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003240}
mohammad160327010052018-07-03 13:16:15 +03003241#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003242
Gilles Peskinec5487a82018-12-03 18:08:14 +01003243psa_status_t psa_set_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003244 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003245{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003246 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003247 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003248
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003249 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003250 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003251
Gilles Peskinec5487a82018-12-03 18:08:14 +01003252 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003253 if( status != PSA_SUCCESS )
3254 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003255
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003256 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3257 PSA_KEY_USAGE_ENCRYPT |
3258 PSA_KEY_USAGE_DECRYPT |
3259 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003260 PSA_KEY_USAGE_VERIFY |
3261 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003262 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003263
mohammad16036df908f2018-04-02 08:34:15 -07003264 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003265
3266 return( PSA_SUCCESS );
3267}
3268
Gilles Peskinec5487a82018-12-03 18:08:14 +01003269psa_status_t psa_get_key_policy( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003270 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003271{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003272 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003273 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003274
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003275 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003276 return( PSA_ERROR_INVALID_ARGUMENT );
3277
Gilles Peskinec5487a82018-12-03 18:08:14 +01003278 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003279 if( status != PSA_SUCCESS )
3280 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003281
mohammad16036df908f2018-04-02 08:34:15 -07003282 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003283
3284 return( PSA_SUCCESS );
3285}
Gilles Peskine20035e32018-02-03 22:44:14 +01003286
Gilles Peskinea0655c32018-04-30 17:06:50 +02003287
3288
mohammad1603804cd712018-03-20 22:44:08 +02003289/****************************************************************/
3290/* Key Lifetime */
3291/****************************************************************/
3292
Gilles Peskinec5487a82018-12-03 18:08:14 +01003293psa_status_t psa_get_key_lifetime( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02003294 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003295{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003296 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003297 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003298
Gilles Peskinec5487a82018-12-03 18:08:14 +01003299 status = psa_get_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003300 if( status != PSA_SUCCESS )
3301 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003302
mohammad1603804cd712018-03-20 22:44:08 +02003303 *lifetime = slot->lifetime;
3304
3305 return( PSA_SUCCESS );
3306}
3307
Gilles Peskine20035e32018-02-03 22:44:14 +01003308
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003309
mohammad16035955c982018-04-26 00:53:03 +03003310/****************************************************************/
3311/* AEAD */
3312/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003313
Gilles Peskineedf9a652018-08-17 18:11:56 +02003314typedef struct
3315{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003316 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003317 const mbedtls_cipher_info_t *cipher_info;
3318 union
3319 {
3320#if defined(MBEDTLS_CCM_C)
3321 mbedtls_ccm_context ccm;
3322#endif /* MBEDTLS_CCM_C */
3323#if defined(MBEDTLS_GCM_C)
3324 mbedtls_gcm_context gcm;
3325#endif /* MBEDTLS_GCM_C */
3326 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003327 psa_algorithm_t core_alg;
3328 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003329 uint8_t tag_length;
3330} aead_operation_t;
3331
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003332static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003333{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003334 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003335 {
3336#if defined(MBEDTLS_CCM_C)
3337 case PSA_ALG_CCM:
3338 mbedtls_ccm_free( &operation->ctx.ccm );
3339 break;
3340#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003341#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003342 case PSA_ALG_GCM:
3343 mbedtls_gcm_free( &operation->ctx.gcm );
3344 break;
3345#endif /* MBEDTLS_GCM_C */
3346 }
3347}
3348
3349static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003350 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003351 psa_key_usage_t usage,
3352 psa_algorithm_t alg )
3353{
3354 psa_status_t status;
3355 size_t key_bits;
3356 mbedtls_cipher_id_t cipher_id;
3357
Gilles Peskinec5487a82018-12-03 18:08:14 +01003358 status = psa_get_key_from_slot( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003359 if( status != PSA_SUCCESS )
3360 return( status );
3361
3362 key_bits = psa_get_key_bits( operation->slot );
3363
3364 operation->cipher_info =
3365 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3366 &cipher_id );
3367 if( operation->cipher_info == NULL )
3368 return( PSA_ERROR_NOT_SUPPORTED );
3369
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003370 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003371 {
3372#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003373 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3374 operation->core_alg = PSA_ALG_CCM;
3375 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003376 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3377 return( PSA_ERROR_INVALID_ARGUMENT );
3378 mbedtls_ccm_init( &operation->ctx.ccm );
3379 status = mbedtls_to_psa_error(
3380 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3381 operation->slot->data.raw.data,
3382 (unsigned int) key_bits ) );
3383 if( status != 0 )
3384 goto cleanup;
3385 break;
3386#endif /* MBEDTLS_CCM_C */
3387
3388#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003389 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3390 operation->core_alg = PSA_ALG_GCM;
3391 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003392 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3393 return( PSA_ERROR_INVALID_ARGUMENT );
3394 mbedtls_gcm_init( &operation->ctx.gcm );
3395 status = mbedtls_to_psa_error(
3396 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3397 operation->slot->data.raw.data,
3398 (unsigned int) key_bits ) );
3399 break;
3400#endif /* MBEDTLS_GCM_C */
3401
3402 default:
3403 return( PSA_ERROR_NOT_SUPPORTED );
3404 }
3405
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003406 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3407 {
3408 status = PSA_ERROR_INVALID_ARGUMENT;
3409 goto cleanup;
3410 }
3411 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3412 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3413 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3414 * In both cases, mbedtls_xxx will validate the tag length below. */
3415
Gilles Peskineedf9a652018-08-17 18:11:56 +02003416 return( PSA_SUCCESS );
3417
3418cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003419 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003420 return( status );
3421}
3422
Gilles Peskinec5487a82018-12-03 18:08:14 +01003423psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003424 psa_algorithm_t alg,
3425 const uint8_t *nonce,
3426 size_t nonce_length,
3427 const uint8_t *additional_data,
3428 size_t additional_data_length,
3429 const uint8_t *plaintext,
3430 size_t plaintext_length,
3431 uint8_t *ciphertext,
3432 size_t ciphertext_size,
3433 size_t *ciphertext_length )
3434{
mohammad16035955c982018-04-26 00:53:03 +03003435 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003436 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003437 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003438
mohammad1603f08a5502018-06-03 15:05:47 +03003439 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003440
Gilles Peskinec5487a82018-12-03 18:08:14 +01003441 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003442 if( status != PSA_SUCCESS )
3443 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003444
Gilles Peskineedf9a652018-08-17 18:11:56 +02003445 /* For all currently supported modes, the tag is at the end of the
3446 * ciphertext. */
3447 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3448 {
3449 status = PSA_ERROR_BUFFER_TOO_SMALL;
3450 goto exit;
3451 }
3452 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003453
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003454#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003455 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003456 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003457 status = mbedtls_to_psa_error(
3458 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3459 MBEDTLS_GCM_ENCRYPT,
3460 plaintext_length,
3461 nonce, nonce_length,
3462 additional_data, additional_data_length,
3463 plaintext, ciphertext,
3464 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003465 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003466 else
3467#endif /* MBEDTLS_GCM_C */
3468#if defined(MBEDTLS_CCM_C)
3469 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003470 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003471 status = mbedtls_to_psa_error(
3472 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3473 plaintext_length,
3474 nonce, nonce_length,
3475 additional_data,
3476 additional_data_length,
3477 plaintext, ciphertext,
3478 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003479 }
mohammad16035c8845f2018-05-09 05:40:09 -07003480 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003481#endif /* MBEDTLS_CCM_C */
mohammad16035c8845f2018-05-09 05:40:09 -07003482 {
mohammad1603554faad2018-06-03 15:07:38 +03003483 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003484 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003485
Gilles Peskineedf9a652018-08-17 18:11:56 +02003486 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3487 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003488
Gilles Peskineedf9a652018-08-17 18:11:56 +02003489exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003490 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003491 if( status == PSA_SUCCESS )
3492 *ciphertext_length = plaintext_length + operation.tag_length;
3493 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003494}
3495
Gilles Peskineee652a32018-06-01 19:23:52 +02003496/* Locate the tag in a ciphertext buffer containing the encrypted data
3497 * followed by the tag. Return the length of the part preceding the tag in
3498 * *plaintext_length. This is the size of the plaintext in modes where
3499 * the encrypted data has the same size as the plaintext, such as
3500 * CCM and GCM. */
3501static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3502 const uint8_t *ciphertext,
3503 size_t ciphertext_length,
3504 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003505 const uint8_t **p_tag )
3506{
3507 size_t payload_length;
3508 if( tag_length > ciphertext_length )
3509 return( PSA_ERROR_INVALID_ARGUMENT );
3510 payload_length = ciphertext_length - tag_length;
3511 if( payload_length > plaintext_size )
3512 return( PSA_ERROR_BUFFER_TOO_SMALL );
3513 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003514 return( PSA_SUCCESS );
3515}
3516
Gilles Peskinec5487a82018-12-03 18:08:14 +01003517psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03003518 psa_algorithm_t alg,
3519 const uint8_t *nonce,
3520 size_t nonce_length,
3521 const uint8_t *additional_data,
3522 size_t additional_data_length,
3523 const uint8_t *ciphertext,
3524 size_t ciphertext_length,
3525 uint8_t *plaintext,
3526 size_t plaintext_size,
3527 size_t *plaintext_length )
3528{
mohammad16035955c982018-04-26 00:53:03 +03003529 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003530 aead_operation_t operation;
3531 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003532
Gilles Peskineee652a32018-06-01 19:23:52 +02003533 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003534
Gilles Peskinec5487a82018-12-03 18:08:14 +01003535 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003536 if( status != PSA_SUCCESS )
3537 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003538
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003539#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003540 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003541 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003542 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003543 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003544 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003545 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003546 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003547
Gilles Peskineedf9a652018-08-17 18:11:56 +02003548 status = mbedtls_to_psa_error(
3549 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3550 ciphertext_length - operation.tag_length,
3551 nonce, nonce_length,
3552 additional_data,
3553 additional_data_length,
3554 tag, operation.tag_length,
3555 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003556 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003557 else
3558#endif /* MBEDTLS_GCM_C */
3559#if defined(MBEDTLS_CCM_C)
3560 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003561 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003562 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003563 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003564 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003565 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003566 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003567
Gilles Peskineedf9a652018-08-17 18:11:56 +02003568 status = mbedtls_to_psa_error(
3569 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3570 ciphertext_length - operation.tag_length,
3571 nonce, nonce_length,
3572 additional_data,
3573 additional_data_length,
3574 ciphertext, plaintext,
3575 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003576 }
mohammad160339574652018-06-01 04:39:53 -07003577 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003578#endif /* MBEDTLS_CCM_C */
mohammad160339574652018-06-01 04:39:53 -07003579 {
mohammad1603554faad2018-06-03 15:07:38 +03003580 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003581 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003582
Gilles Peskineedf9a652018-08-17 18:11:56 +02003583 if( status != PSA_SUCCESS && plaintext_size != 0 )
3584 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003585
Gilles Peskineedf9a652018-08-17 18:11:56 +02003586exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003587 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003588 if( status == PSA_SUCCESS )
3589 *plaintext_length = ciphertext_length - operation.tag_length;
3590 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003591}
3592
Gilles Peskinea0655c32018-04-30 17:06:50 +02003593
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003594
Gilles Peskine20035e32018-02-03 22:44:14 +01003595/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003596/* Generators */
3597/****************************************************************/
3598
3599psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3600{
3601 psa_status_t status = PSA_SUCCESS;
3602 if( generator->alg == 0 )
3603 {
3604 /* The object has (apparently) been initialized but it is not
3605 * in use. It's ok to call abort on such an object, and there's
3606 * nothing to do. */
3607 }
3608 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003609 if( generator->alg == PSA_ALG_SELECT_RAW )
3610 {
3611 if( generator->ctx.buffer.data != NULL )
3612 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003613 mbedtls_platform_zeroize( generator->ctx.buffer.data,
Gilles Peskine751d9652018-09-18 12:05:44 +02003614 generator->ctx.buffer.size );
3615 mbedtls_free( generator->ctx.buffer.data );
3616 }
3617 }
3618 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003619#if defined(MBEDTLS_MD_C)
3620 if( PSA_ALG_IS_HKDF( generator->alg ) )
3621 {
3622 mbedtls_free( generator->ctx.hkdf.info );
3623 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3624 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003625 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3626 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3627 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003628 {
3629 if( generator->ctx.tls12_prf.key != NULL )
3630 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003631 mbedtls_platform_zeroize( generator->ctx.tls12_prf.key,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003632 generator->ctx.tls12_prf.key_len );
3633 mbedtls_free( generator->ctx.tls12_prf.key );
3634 }
Hanno Becker580fba12018-11-13 20:50:45 +00003635
3636 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3637 {
Gilles Peskine3f108122018-12-07 18:14:53 +01003638 mbedtls_platform_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003639 generator->ctx.tls12_prf.Ai_with_seed_len );
3640 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3641 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003642 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003643 else
3644#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003645 {
3646 status = PSA_ERROR_BAD_STATE;
3647 }
3648 memset( generator, 0, sizeof( *generator ) );
3649 return( status );
3650}
3651
3652
3653psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3654 size_t *capacity)
3655{
3656 *capacity = generator->capacity;
3657 return( PSA_SUCCESS );
3658}
3659
Gilles Peskinebef7f142018-07-12 17:22:21 +02003660#if defined(MBEDTLS_MD_C)
3661/* Read some bytes from an HKDF-based generator. This performs a chunk
3662 * of the expand phase of the HKDF algorithm. */
3663static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3664 psa_algorithm_t hash_alg,
3665 uint8_t *output,
3666 size_t output_length )
3667{
3668 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3669 psa_status_t status;
3670
3671 while( output_length != 0 )
3672 {
3673 /* Copy what remains of the current block */
3674 uint8_t n = hash_length - hkdf->offset_in_block;
3675 if( n > output_length )
3676 n = (uint8_t) output_length;
3677 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3678 output += n;
3679 output_length -= n;
3680 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003681 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003682 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003683 /* We can't be wanting more output after block 0xff, otherwise
3684 * the capacity check in psa_generator_read() would have
3685 * prevented this call. It could happen only if the generator
3686 * object was corrupted or if this function is called directly
3687 * inside the library. */
3688 if( hkdf->block_number == 0xff )
3689 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003690
3691 /* We need a new block */
3692 ++hkdf->block_number;
3693 hkdf->offset_in_block = 0;
3694 status = psa_hmac_setup_internal( &hkdf->hmac,
3695 hkdf->prk, hash_length,
3696 hash_alg );
3697 if( status != PSA_SUCCESS )
3698 return( status );
3699 if( hkdf->block_number != 1 )
3700 {
3701 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3702 hkdf->output_block,
3703 hash_length );
3704 if( status != PSA_SUCCESS )
3705 return( status );
3706 }
3707 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3708 hkdf->info,
3709 hkdf->info_length );
3710 if( status != PSA_SUCCESS )
3711 return( status );
3712 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3713 &hkdf->block_number, 1 );
3714 if( status != PSA_SUCCESS )
3715 return( status );
3716 status = psa_hmac_finish_internal( &hkdf->hmac,
3717 hkdf->output_block,
3718 sizeof( hkdf->output_block ) );
3719 if( status != PSA_SUCCESS )
3720 return( status );
3721 }
3722
3723 return( PSA_SUCCESS );
3724}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003725
3726static psa_status_t psa_generator_tls12_prf_generate_next_block(
3727 psa_tls12_prf_generator_t *tls12_prf,
3728 psa_algorithm_t alg )
3729{
3730 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3731 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3732 psa_hmac_internal_data hmac;
3733 psa_status_t status, cleanup_status;
3734
Hanno Becker3b339e22018-11-13 20:56:14 +00003735 unsigned char *Ai;
3736 size_t Ai_len;
3737
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003738 /* We can't be wanting more output after block 0xff, otherwise
3739 * the capacity check in psa_generator_read() would have
3740 * prevented this call. It could happen only if the generator
3741 * object was corrupted or if this function is called directly
3742 * inside the library. */
3743 if( tls12_prf->block_number == 0xff )
3744 return( PSA_ERROR_BAD_STATE );
3745
3746 /* We need a new block */
3747 ++tls12_prf->block_number;
3748 tls12_prf->offset_in_block = 0;
3749
3750 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3751 *
3752 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3753 *
3754 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3755 * HMAC_hash(secret, A(2) + seed) +
3756 * HMAC_hash(secret, A(3) + seed) + ...
3757 *
3758 * A(0) = seed
3759 * A(i) = HMAC_hash( secret, A(i-1) )
3760 *
3761 * The `psa_tls12_prf_generator` structures saves the block
3762 * `HMAC_hash(secret, A(i) + seed)` from which the output
3763 * is currently extracted as `output_block`, while
3764 * `A(i) + seed` is stored in `Ai_with_seed`.
3765 *
3766 * Generating a new block means recalculating `Ai_with_seed`
3767 * from the A(i)-part of it, and afterwards recalculating
3768 * `output_block`.
3769 *
3770 * A(0) is computed at setup time.
3771 *
3772 */
3773
3774 psa_hmac_init_internal( &hmac );
3775
3776 /* We must distinguish the calculation of A(1) from those
3777 * of A(2) and higher, because A(0)=seed has a different
3778 * length than the other A(i). */
3779 if( tls12_prf->block_number == 1 )
3780 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003781 Ai = tls12_prf->Ai_with_seed + hash_length;
3782 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003783 }
3784 else
3785 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003786 Ai = tls12_prf->Ai_with_seed;
3787 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003788 }
3789
Hanno Becker3b339e22018-11-13 20:56:14 +00003790 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3791 status = psa_hmac_setup_internal( &hmac,
3792 tls12_prf->key,
3793 tls12_prf->key_len,
3794 hash_alg );
3795 if( status != PSA_SUCCESS )
3796 goto cleanup;
3797
3798 status = psa_hash_update( &hmac.hash_ctx,
3799 Ai, Ai_len );
3800 if( status != PSA_SUCCESS )
3801 goto cleanup;
3802
3803 status = psa_hmac_finish_internal( &hmac,
3804 tls12_prf->Ai_with_seed,
3805 hash_length );
3806 if( status != PSA_SUCCESS )
3807 goto cleanup;
3808
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003809 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3810 status = psa_hmac_setup_internal( &hmac,
3811 tls12_prf->key,
3812 tls12_prf->key_len,
3813 hash_alg );
3814 if( status != PSA_SUCCESS )
3815 goto cleanup;
3816
3817 status = psa_hash_update( &hmac.hash_ctx,
3818 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003819 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003820 if( status != PSA_SUCCESS )
3821 goto cleanup;
3822
3823 status = psa_hmac_finish_internal( &hmac,
3824 tls12_prf->output_block,
3825 hash_length );
3826 if( status != PSA_SUCCESS )
3827 goto cleanup;
3828
3829cleanup:
3830
3831 cleanup_status = psa_hmac_abort_internal( &hmac );
3832 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3833 status = cleanup_status;
3834
3835 return( status );
3836}
3837
3838/* Read some bytes from an TLS-1.2-PRF-based generator.
3839 * See Section 5 of RFC 5246. */
3840static psa_status_t psa_generator_tls12_prf_read(
3841 psa_tls12_prf_generator_t *tls12_prf,
3842 psa_algorithm_t alg,
3843 uint8_t *output,
3844 size_t output_length )
3845{
3846 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3847 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3848 psa_status_t status;
3849
3850 while( output_length != 0 )
3851 {
3852 /* Copy what remains of the current block */
3853 uint8_t n = hash_length - tls12_prf->offset_in_block;
3854
3855 /* Check if we have fully processed the current block. */
3856 if( n == 0 )
3857 {
3858 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3859 alg );
3860 if( status != PSA_SUCCESS )
3861 return( status );
3862
3863 continue;
3864 }
3865
3866 if( n > output_length )
3867 n = (uint8_t) output_length;
3868 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3869 n );
3870 output += n;
3871 output_length -= n;
3872 tls12_prf->offset_in_block += n;
3873 }
3874
3875 return( PSA_SUCCESS );
3876}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003877#endif /* MBEDTLS_MD_C */
3878
Gilles Peskineeab56e42018-07-12 17:12:33 +02003879psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3880 uint8_t *output,
3881 size_t output_length )
3882{
3883 psa_status_t status;
3884
3885 if( output_length > generator->capacity )
3886 {
3887 generator->capacity = 0;
3888 /* Go through the error path to wipe all confidential data now
3889 * that the generator object is useless. */
3890 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3891 goto exit;
3892 }
3893 if( output_length == 0 &&
3894 generator->capacity == 0 && generator->alg == 0 )
3895 {
3896 /* Edge case: this is a blank or finished generator, and 0
3897 * bytes were requested. The right error in this case could
3898 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3899 * INSUFFICIENT_CAPACITY, which is right for a finished
3900 * generator, for consistency with the case when
3901 * output_length > 0. */
3902 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3903 }
3904 generator->capacity -= output_length;
3905
Gilles Peskine751d9652018-09-18 12:05:44 +02003906 if( generator->alg == PSA_ALG_SELECT_RAW )
3907 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003908 /* Initially, the capacity of a selection generator is always
3909 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3910 * abbreviated in this comment as `size`. When the remaining
3911 * capacity is `c`, the next bytes to serve start `c` bytes
3912 * from the end of the buffer, i.e. `size - c` from the
3913 * beginning of the buffer. Since `generator->capacity` was just
3914 * decremented above, we need to serve the bytes from
3915 * `size - generator->capacity - output_length` to
3916 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003917 size_t offset =
3918 generator->ctx.buffer.size - generator->capacity - output_length;
3919 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3920 status = PSA_SUCCESS;
3921 }
3922 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003923#if defined(MBEDTLS_MD_C)
3924 if( PSA_ALG_IS_HKDF( generator->alg ) )
3925 {
3926 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3927 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3928 output, output_length );
3929 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003930 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3931 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003932 {
3933 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3934 generator->alg, output,
3935 output_length );
3936 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003937 else
3938#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003939 {
3940 return( PSA_ERROR_BAD_STATE );
3941 }
3942
3943exit:
3944 if( status != PSA_SUCCESS )
3945 {
3946 psa_generator_abort( generator );
3947 memset( output, '!', output_length );
3948 }
3949 return( status );
3950}
3951
Gilles Peskine08542d82018-07-19 17:05:42 +02003952#if defined(MBEDTLS_DES_C)
3953static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3954{
3955 if( data_size >= 8 )
3956 mbedtls_des_key_set_parity( data );
3957 if( data_size >= 16 )
3958 mbedtls_des_key_set_parity( data + 8 );
3959 if( data_size >= 24 )
3960 mbedtls_des_key_set_parity( data + 16 );
3961}
3962#endif /* MBEDTLS_DES_C */
3963
Gilles Peskinec5487a82018-12-03 18:08:14 +01003964psa_status_t psa_generator_import_key( psa_key_handle_t handle,
Gilles Peskineeab56e42018-07-12 17:12:33 +02003965 psa_key_type_t type,
3966 size_t bits,
3967 psa_crypto_generator_t *generator )
3968{
3969 uint8_t *data = NULL;
3970 size_t bytes = PSA_BITS_TO_BYTES( bits );
3971 psa_status_t status;
3972
3973 if( ! key_type_is_raw_bytes( type ) )
3974 return( PSA_ERROR_INVALID_ARGUMENT );
3975 if( bits % 8 != 0 )
3976 return( PSA_ERROR_INVALID_ARGUMENT );
3977 data = mbedtls_calloc( 1, bytes );
3978 if( data == NULL )
3979 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3980
3981 status = psa_generator_read( generator, data, bytes );
3982 if( status != PSA_SUCCESS )
3983 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003984#if defined(MBEDTLS_DES_C)
3985 if( type == PSA_KEY_TYPE_DES )
3986 psa_des_set_key_parity( data, bytes );
3987#endif /* MBEDTLS_DES_C */
Gilles Peskinec5487a82018-12-03 18:08:14 +01003988 status = psa_import_key( handle, type, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02003989
3990exit:
3991 mbedtls_free( data );
3992 return( status );
3993}
3994
3995
3996
3997/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003998/* Key derivation */
3999/****************************************************************/
4000
Gilles Peskinea05219c2018-11-16 16:02:56 +01004001#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02004002/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01004003 * of the HKDF algorithm.
4004 *
4005 * Note that if this function fails, you must call psa_generator_abort()
4006 * to potentially free embedded data structures and wipe confidential data.
4007 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004008static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004009 const uint8_t *secret,
4010 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004011 psa_algorithm_t hash_alg,
4012 const uint8_t *salt,
4013 size_t salt_length,
4014 const uint8_t *label,
4015 size_t label_length )
4016{
4017 psa_status_t status;
4018 status = psa_hmac_setup_internal( &hkdf->hmac,
4019 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02004020 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004021 if( status != PSA_SUCCESS )
4022 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004023 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004024 if( status != PSA_SUCCESS )
4025 return( status );
4026 status = psa_hmac_finish_internal( &hkdf->hmac,
4027 hkdf->prk,
4028 sizeof( hkdf->prk ) );
4029 if( status != PSA_SUCCESS )
4030 return( status );
4031 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4032 hkdf->block_number = 0;
4033 hkdf->info_length = label_length;
4034 if( label_length != 0 )
4035 {
4036 hkdf->info = mbedtls_calloc( 1, label_length );
4037 if( hkdf->info == NULL )
4038 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4039 memcpy( hkdf->info, label, label_length );
4040 }
4041 return( PSA_SUCCESS );
4042}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004043#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004044
Gilles Peskinea05219c2018-11-16 16:02:56 +01004045#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01004046/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
4047 *
4048 * Note that if this function fails, you must call psa_generator_abort()
4049 * to potentially free embedded data structures and wipe confidential data.
4050 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004051static psa_status_t psa_generator_tls12_prf_setup(
4052 psa_tls12_prf_generator_t *tls12_prf,
4053 const unsigned char *key,
4054 size_t key_len,
4055 psa_algorithm_t hash_alg,
4056 const uint8_t *salt,
4057 size_t salt_length,
4058 const uint8_t *label,
4059 size_t label_length )
4060{
4061 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00004062 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
4063 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004064
4065 tls12_prf->key = mbedtls_calloc( 1, key_len );
4066 if( tls12_prf->key == NULL )
4067 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4068 tls12_prf->key_len = key_len;
4069 memcpy( tls12_prf->key, key, key_len );
4070
Hanno Becker580fba12018-11-13 20:50:45 +00004071 overflow = ( salt_length + label_length < salt_length ) ||
4072 ( salt_length + label_length + hash_length < hash_length );
4073 if( overflow )
4074 return( PSA_ERROR_INVALID_ARGUMENT );
4075
4076 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
4077 if( tls12_prf->Ai_with_seed == NULL )
4078 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4079 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
4080
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004081 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
4082 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00004083 if( label_length != 0 )
4084 {
4085 memcpy( tls12_prf->Ai_with_seed + hash_length,
4086 label, label_length );
4087 }
4088
4089 if( salt_length != 0 )
4090 {
4091 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
4092 salt, salt_length );
4093 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004094
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004095 /* The first block gets generated when
4096 * psa_generator_read() is called. */
4097 tls12_prf->block_number = 0;
4098 tls12_prf->offset_in_block = hash_length;
4099
4100 return( PSA_SUCCESS );
4101}
Hanno Becker1aaedc02018-11-16 11:35:34 +00004102
4103/* Set up a TLS-1.2-PSK-to-MS-based generator. */
4104static psa_status_t psa_generator_tls12_psk_to_ms_setup(
4105 psa_tls12_prf_generator_t *tls12_prf,
4106 const unsigned char *psk,
4107 size_t psk_len,
4108 psa_algorithm_t hash_alg,
4109 const uint8_t *salt,
4110 size_t salt_length,
4111 const uint8_t *label,
4112 size_t label_length )
4113{
4114 psa_status_t status;
4115 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
4116
4117 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
4118 return( PSA_ERROR_INVALID_ARGUMENT );
4119
4120 /* Quoting RFC 4279, Section 2:
4121 *
4122 * The premaster secret is formed as follows: if the PSK is N octets
4123 * long, concatenate a uint16 with the value N, N zero octets, a second
4124 * uint16 with the value N, and the PSK itself.
4125 */
4126
4127 pms[0] = ( psk_len >> 8 ) & 0xff;
4128 pms[1] = ( psk_len >> 0 ) & 0xff;
4129 memset( pms + 2, 0, psk_len );
4130 pms[2 + psk_len + 0] = pms[0];
4131 pms[2 + psk_len + 1] = pms[1];
4132 memcpy( pms + 4 + psk_len, psk, psk_len );
4133
4134 status = psa_generator_tls12_prf_setup( tls12_prf,
4135 pms, 4 + 2 * psk_len,
4136 hash_alg,
4137 salt, salt_length,
4138 label, label_length );
4139
Gilles Peskine3f108122018-12-07 18:14:53 +01004140 mbedtls_platform_zeroize( pms, sizeof( pms ) );
Hanno Becker1aaedc02018-11-16 11:35:34 +00004141 return( status );
4142}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004143#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004144
Gilles Peskine346797d2018-11-16 16:05:06 +01004145/* Note that if this function fails, you must call psa_generator_abort()
4146 * to potentially free embedded data structures and wipe confidential data.
4147 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004148static psa_status_t psa_key_derivation_internal(
4149 psa_crypto_generator_t *generator,
4150 const uint8_t *secret, size_t secret_length,
4151 psa_algorithm_t alg,
4152 const uint8_t *salt, size_t salt_length,
4153 const uint8_t *label, size_t label_length,
4154 size_t capacity )
4155{
4156 psa_status_t status;
4157 size_t max_capacity;
4158
4159 /* Set generator->alg even on failure so that abort knows what to do. */
4160 generator->alg = alg;
4161
Gilles Peskine751d9652018-09-18 12:05:44 +02004162 if( alg == PSA_ALG_SELECT_RAW )
4163 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01004164 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02004165 if( salt_length != 0 )
4166 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01004167 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02004168 if( label_length != 0 )
4169 return( PSA_ERROR_INVALID_ARGUMENT );
4170 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
4171 if( generator->ctx.buffer.data == NULL )
4172 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4173 memcpy( generator->ctx.buffer.data, secret, secret_length );
4174 generator->ctx.buffer.size = secret_length;
4175 max_capacity = secret_length;
4176 status = PSA_SUCCESS;
4177 }
4178 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004179#if defined(MBEDTLS_MD_C)
4180 if( PSA_ALG_IS_HKDF( alg ) )
4181 {
4182 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4183 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4184 if( hash_size == 0 )
4185 return( PSA_ERROR_NOT_SUPPORTED );
4186 max_capacity = 255 * hash_size;
4187 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
4188 secret, secret_length,
4189 hash_alg,
4190 salt, salt_length,
4191 label, label_length );
4192 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00004193 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
4194 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
4195 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004196 {
4197 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4198 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4199
4200 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
4201 if( hash_alg != PSA_ALG_SHA_256 &&
4202 hash_alg != PSA_ALG_SHA_384 )
4203 {
4204 return( PSA_ERROR_NOT_SUPPORTED );
4205 }
4206
4207 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004208
4209 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4210 {
4211 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4212 secret, secret_length,
4213 hash_alg, salt, salt_length,
4214 label, label_length );
4215 }
4216 else
4217 {
4218 status = psa_generator_tls12_psk_to_ms_setup(
4219 &generator->ctx.tls12_prf,
4220 secret, secret_length,
4221 hash_alg, salt, salt_length,
4222 label, label_length );
4223 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004224 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004225 else
4226#endif
4227 {
4228 return( PSA_ERROR_NOT_SUPPORTED );
4229 }
4230
4231 if( status != PSA_SUCCESS )
4232 return( status );
4233
4234 if( capacity <= max_capacity )
4235 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004236 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4237 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004238 else
4239 return( PSA_ERROR_INVALID_ARGUMENT );
4240
4241 return( PSA_SUCCESS );
4242}
4243
Gilles Peskineea0fb492018-07-12 17:17:20 +02004244psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004245 psa_key_handle_t handle,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004246 psa_algorithm_t alg,
4247 const uint8_t *salt,
4248 size_t salt_length,
4249 const uint8_t *label,
4250 size_t label_length,
4251 size_t capacity )
4252{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004253 psa_key_slot_t *slot;
Gilles Peskineea0fb492018-07-12 17:17:20 +02004254 psa_status_t status;
4255
4256 if( generator->alg != 0 )
4257 return( PSA_ERROR_BAD_STATE );
4258
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004259 /* Make sure that alg is a key derivation algorithm. This prevents
4260 * key selection algorithms, which psa_key_derivation_internal
4261 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004262 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4263 return( PSA_ERROR_INVALID_ARGUMENT );
4264
Gilles Peskinec5487a82018-12-03 18:08:14 +01004265 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004266 if( status != PSA_SUCCESS )
4267 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004268
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004269 if( slot->type != PSA_KEY_TYPE_DERIVE )
4270 return( PSA_ERROR_INVALID_ARGUMENT );
4271
4272 status = psa_key_derivation_internal( generator,
4273 slot->data.raw.data,
4274 slot->data.raw.bytes,
4275 alg,
4276 salt, salt_length,
4277 label, label_length,
4278 capacity );
4279 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004280 psa_generator_abort( generator );
4281 return( status );
4282}
4283
4284
4285
4286/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004287/* Key agreement */
4288/****************************************************************/
4289
Gilles Peskinea05219c2018-11-16 16:02:56 +01004290#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004291static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4292 size_t peer_key_length,
4293 const mbedtls_ecp_keypair *our_key,
4294 uint8_t *shared_secret,
4295 size_t shared_secret_size,
4296 size_t *shared_secret_length )
4297{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004298 mbedtls_ecp_keypair *their_key = NULL;
4299 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00004300 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004301 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004302
Jaeden Ameroccdce902019-01-10 11:42:27 +00004303 status = psa_import_ec_public_key(
4304 mbedtls_ecc_group_to_psa( our_key->grp.id ),
4305 peer_key, peer_key_length,
4306 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004307 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004308 goto exit;
4309
Jaeden Amero97271b32019-01-10 19:38:51 +00004310 status = mbedtls_to_psa_error(
4311 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
4312 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004313 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00004314 status = mbedtls_to_psa_error(
4315 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
4316 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004317 goto exit;
4318
Jaeden Amero97271b32019-01-10 19:38:51 +00004319 status = mbedtls_to_psa_error(
4320 mbedtls_ecdh_calc_secret( &ecdh,
4321 shared_secret_length,
4322 shared_secret, shared_secret_size,
4323 mbedtls_ctr_drbg_random,
4324 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004325
4326exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004327 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00004328 mbedtls_ecp_keypair_free( their_key );
4329 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00004330 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004331}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004332#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004333
Gilles Peskine01d718c2018-09-18 12:01:02 +02004334#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4335
Gilles Peskine346797d2018-11-16 16:05:06 +01004336/* Note that if this function fails, you must call psa_generator_abort()
4337 * to potentially free embedded data structures and wipe confidential data.
4338 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004339static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
Gilles Peskine2f060a82018-12-04 17:12:32 +01004340 psa_key_slot_t *private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004341 const uint8_t *peer_key,
4342 size_t peer_key_length,
4343 psa_algorithm_t alg )
4344{
4345 psa_status_t status;
4346 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4347 size_t shared_secret_length = 0;
4348
4349 /* Step 1: run the secret agreement algorithm to generate the shared
4350 * secret. */
4351 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4352 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004353#if defined(MBEDTLS_ECDH_C)
4354 case PSA_ALG_ECDH_BASE:
4355 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4356 return( PSA_ERROR_INVALID_ARGUMENT );
4357 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4358 private_key->data.ecp,
4359 shared_secret,
4360 sizeof( shared_secret ),
4361 &shared_secret_length );
4362 break;
4363#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004364 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004365 (void) private_key;
4366 (void) peer_key;
4367 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004368 return( PSA_ERROR_NOT_SUPPORTED );
4369 }
4370 if( status != PSA_SUCCESS )
4371 goto exit;
4372
4373 /* Step 2: set up the key derivation to generate key material from
4374 * the shared secret. */
4375 status = psa_key_derivation_internal( generator,
4376 shared_secret, shared_secret_length,
4377 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4378 NULL, 0, NULL, 0,
4379 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4380exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01004381 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004382 return( status );
4383}
4384
4385psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004386 psa_key_handle_t private_key,
Gilles Peskine01d718c2018-09-18 12:01:02 +02004387 const uint8_t *peer_key,
4388 size_t peer_key_length,
4389 psa_algorithm_t alg )
4390{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004391 psa_key_slot_t *slot;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004392 psa_status_t status;
4393 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4394 return( PSA_ERROR_INVALID_ARGUMENT );
4395 status = psa_get_key_from_slot( private_key, &slot,
4396 PSA_KEY_USAGE_DERIVE, alg );
4397 if( status != PSA_SUCCESS )
4398 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004399 status = psa_key_agreement_internal( generator,
4400 slot,
4401 peer_key, peer_key_length,
4402 alg );
4403 if( status != PSA_SUCCESS )
4404 psa_generator_abort( generator );
4405 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004406}
4407
4408
4409
4410/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004411/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004412/****************************************************************/
4413
4414psa_status_t psa_generate_random( uint8_t *output,
4415 size_t output_size )
4416{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004417 int ret;
4418 GUARD_MODULE_INITIALIZED;
4419
4420 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004421 return( mbedtls_to_psa_error( ret ) );
4422}
4423
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004424#if ( defined(MBEDTLS_ENTROPY_NV_SEED) && defined(MBEDTLS_PSA_HAS_ITS_IO) )
avolinski13beb102018-11-20 16:51:49 +02004425
4426/* Support function for error conversion between psa_its error codes to psa crypto */
4427static psa_status_t its_to_psa_error( psa_its_status_t ret )
4428{
4429 switch( ret )
4430 {
4431 case PSA_ITS_SUCCESS:
4432 return( PSA_SUCCESS );
4433
Oren Cohen23a67842019-01-24 14:32:11 +02004434 case PSA_ITS_ERROR_UID_NOT_FOUND:
avolinski13beb102018-11-20 16:51:49 +02004435 return( PSA_ERROR_EMPTY_SLOT );
4436
4437 case PSA_ITS_ERROR_STORAGE_FAILURE:
4438 return( PSA_ERROR_STORAGE_FAILURE );
4439
4440 case PSA_ITS_ERROR_INSUFFICIENT_SPACE:
4441 return( PSA_ERROR_INSUFFICIENT_STORAGE );
4442
Jaeden Amero58600552018-11-30 12:04:38 +00004443 case PSA_ITS_ERROR_OFFSET_INVALID:
avolinski13beb102018-11-20 16:51:49 +02004444 case PSA_ITS_ERROR_INCORRECT_SIZE:
Oren Cohen23a67842019-01-24 14:32:11 +02004445 case PSA_ITS_ERROR_INVALID_ARGUMENTS:
avolinski13beb102018-11-20 16:51:49 +02004446 return( PSA_ERROR_INVALID_ARGUMENT );
4447
4448 case PSA_ITS_ERROR_FLAGS_NOT_SUPPORTED:
4449 return( PSA_ERROR_NOT_SUPPORTED );
4450
4451 case PSA_ITS_ERROR_WRITE_ONCE:
4452 return( PSA_ERROR_OCCUPIED_SLOT );
4453
4454 default:
4455 return( PSA_ERROR_UNKNOWN_ERROR );
4456 }
4457}
4458
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004459psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
4460 size_t seed_size )
4461{
4462 psa_status_t status;
avolinski13beb102018-11-20 16:51:49 +02004463 psa_its_status_t its_status;
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004464 struct psa_its_info_t p_info;
4465 if( global_data.initialized )
4466 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02004467
4468 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
4469 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
4470 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
4471 return( PSA_ERROR_INVALID_ARGUMENT );
4472
avolinski0d2c2662018-11-21 17:31:07 +02004473 its_status = psa_its_get_info( PSA_CRYPTO_ITS_RANDOM_SEED_UID, &p_info );
avolinski13beb102018-11-20 16:51:49 +02004474 status = its_to_psa_error( its_status );
4475
Oren Cohen23a67842019-01-24 14:32:11 +02004476 if( PSA_ITS_ERROR_UID_NOT_FOUND == its_status ) /* No seed exists */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004477 {
avolinski0d2c2662018-11-21 17:31:07 +02004478 its_status = psa_its_set( PSA_CRYPTO_ITS_RANDOM_SEED_UID, seed_size, seed, 0 );
avolinski13beb102018-11-20 16:51:49 +02004479 status = its_to_psa_error( its_status );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004480 }
avolinski13beb102018-11-20 16:51:49 +02004481 else if( PSA_ITS_SUCCESS == its_status )
Netanel Gonen2bcd3122018-11-19 11:53:02 +02004482 {
4483 /* You should not be here. Seed needs to be injected only once */
4484 status = PSA_ERROR_NOT_PERMITTED;
4485 }
4486 return( status );
4487}
4488#endif
4489
Gilles Peskinec5487a82018-12-03 18:08:14 +01004490psa_status_t psa_generate_key( psa_key_handle_t handle,
Gilles Peskine05d69892018-06-19 22:00:52 +02004491 psa_key_type_t type,
4492 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004493 const void *extra,
4494 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004495{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004496 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004497 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004498
Gilles Peskine53d991e2018-07-12 01:14:59 +02004499 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004500 return( PSA_ERROR_INVALID_ARGUMENT );
4501
Gilles Peskinec5487a82018-12-03 18:08:14 +01004502 status = psa_get_empty_key_slot( handle, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004503 if( status != PSA_SUCCESS )
4504 return( status );
4505
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004506 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004507 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004508 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004509 if( status != PSA_SUCCESS )
4510 return( status );
4511 status = psa_generate_random( slot->data.raw.data,
4512 slot->data.raw.bytes );
4513 if( status != PSA_SUCCESS )
4514 {
4515 mbedtls_free( slot->data.raw.data );
4516 return( status );
4517 }
4518#if defined(MBEDTLS_DES_C)
4519 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004520 psa_des_set_key_parity( slot->data.raw.data,
4521 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004522#endif /* MBEDTLS_DES_C */
4523 }
4524 else
4525
4526#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4527 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4528 {
4529 mbedtls_rsa_context *rsa;
4530 int ret;
4531 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004532 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4533 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004534 /* Accept only byte-aligned keys, for the same reasons as
4535 * in psa_import_rsa_key(). */
4536 if( bits % 8 != 0 )
4537 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004538 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004539 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004540 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004541 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004542 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004543#if INT_MAX < 0xffffffff
4544 /* Check that the uint32_t value passed by the caller fits
4545 * in the range supported by this implementation. */
4546 if( p->e > INT_MAX )
4547 return( PSA_ERROR_NOT_SUPPORTED );
4548#endif
4549 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004550 }
4551 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4552 if( rsa == NULL )
4553 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4554 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4555 ret = mbedtls_rsa_gen_key( rsa,
4556 mbedtls_ctr_drbg_random,
4557 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004558 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004559 exponent );
4560 if( ret != 0 )
4561 {
4562 mbedtls_rsa_free( rsa );
4563 mbedtls_free( rsa );
4564 return( mbedtls_to_psa_error( ret ) );
4565 }
4566 slot->data.rsa = rsa;
4567 }
4568 else
4569#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4570
4571#if defined(MBEDTLS_ECP_C)
4572 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4573 {
4574 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4575 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4576 const mbedtls_ecp_curve_info *curve_info =
4577 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4578 mbedtls_ecp_keypair *ecp;
4579 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004580 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004581 return( PSA_ERROR_NOT_SUPPORTED );
4582 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4583 return( PSA_ERROR_NOT_SUPPORTED );
4584 if( curve_info->bit_size != bits )
4585 return( PSA_ERROR_INVALID_ARGUMENT );
4586 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4587 if( ecp == NULL )
4588 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4589 mbedtls_ecp_keypair_init( ecp );
4590 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4591 mbedtls_ctr_drbg_random,
4592 &global_data.ctr_drbg );
4593 if( ret != 0 )
4594 {
4595 mbedtls_ecp_keypair_free( ecp );
4596 mbedtls_free( ecp );
4597 return( mbedtls_to_psa_error( ret ) );
4598 }
4599 slot->data.ecp = ecp;
4600 }
4601 else
4602#endif /* MBEDTLS_ECP_C */
4603
4604 return( PSA_ERROR_NOT_SUPPORTED );
4605
4606 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004607
4608#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4609 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4610 {
Gilles Peskine69f976b2018-11-30 18:46:56 +01004611 return( psa_save_generated_persistent_key( slot, bits ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00004612 }
4613#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4614
4615 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004616}
4617
4618
4619/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004620/* Module setup */
4621/****************************************************************/
4622
Gilles Peskine5e769522018-11-20 21:59:56 +01004623psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
4624 void (* entropy_init )( mbedtls_entropy_context *ctx ),
4625 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
4626{
4627 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4628 return( PSA_ERROR_BAD_STATE );
4629 global_data.entropy_init = entropy_init;
4630 global_data.entropy_free = entropy_free;
4631 return( PSA_SUCCESS );
4632}
4633
Gilles Peskinee59236f2018-01-27 23:32:46 +01004634void mbedtls_psa_crypto_free( void )
4635{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004636 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004637 if( global_data.rng_state != RNG_NOT_INITIALIZED )
4638 {
4639 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01004640 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004641 }
4642 /* Wipe all remaining data, including configuration.
4643 * In particular, this sets all state indicator to the value
4644 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01004645 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004646}
4647
4648psa_status_t psa_crypto_init( void )
4649{
Gilles Peskine66fb1262018-12-10 16:29:04 +01004650 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004651 const unsigned char drbg_seed[] = "PSA";
4652
Gilles Peskinec6b69072018-11-20 21:42:52 +01004653 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01004654 if( global_data.initialized != 0 )
4655 return( PSA_SUCCESS );
4656
Gilles Peskine5e769522018-11-20 21:59:56 +01004657 /* Set default configuration if
4658 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
4659 if( global_data.entropy_init == NULL )
4660 global_data.entropy_init = mbedtls_entropy_init;
4661 if( global_data.entropy_free == NULL )
4662 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004663
Gilles Peskinec6b69072018-11-20 21:42:52 +01004664 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01004665 global_data.entropy_init( &global_data.entropy );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004666 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01004667 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01004668 status = mbedtls_to_psa_error(
4669 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4670 mbedtls_entropy_func,
4671 &global_data.entropy,
4672 drbg_seed, sizeof( drbg_seed ) - 1 ) );
4673 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004674 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004675 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01004676
Gilles Peskine66fb1262018-12-10 16:29:04 +01004677 status = psa_initialize_key_slots( );
4678 if( status != PSA_SUCCESS )
4679 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01004680
4681 /* All done. */
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004682 global_data.initialized = 1;
4683
Gilles Peskinee59236f2018-01-27 23:32:46 +01004684exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01004685 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01004686 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01004687 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01004688}
4689
4690#endif /* MBEDTLS_PSA_CRYPTO_C */