blob: 6b089831e26fc8b6223d8ae598daee020d308b58 [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/*
30 * In case MBEDTLS_PSA_CRYPTO_SPM is defined the code is built for SPM (Secure
31 * Partition Manager) integration which separate the code into two parts
32 * NSPE (Non-Secure Process Environment) and SPE (Secure Process Environment).
33 * In this mode an additional header file should be included.
34 */
mohammad160327010052018-07-03 13:16:15 +030035#if defined(MBEDTLS_PSA_CRYPTO_SPM)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030036/*
37 * PSA_CRYPTO_SECURE means that this file is compiled to the SPE side.
38 * some headers will be affected by this flag.
39 */
mohammad160327010052018-07-03 13:16:15 +030040#define PSA_CRYPTO_SECURE 1
41#include "crypto_spe.h"
42#endif
43
Gilles Peskinee59236f2018-01-27 23:32:46 +010044#include "psa/crypto.h"
45
Darryl Greend49a4992018-06-18 17:27:26 +010046/* Include internal declarations that are useful for implementing persistently
47 * stored keys. */
48#include "psa_crypto_storage.h"
49
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010050#include <stdlib.h>
51#include <string.h>
52#if defined(MBEDTLS_PLATFORM_C)
53#include "mbedtls/platform.h"
54#else
55#define mbedtls_calloc calloc
56#define mbedtls_free free
57#endif
58
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020060#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020061#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/blowfish.h"
63#include "mbedtls/camellia.h"
64#include "mbedtls/cipher.h"
65#include "mbedtls/ccm.h"
66#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020069#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010070#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010071#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010072#include "mbedtls/error.h"
73#include "mbedtls/gcm.h"
74#include "mbedtls/md2.h"
75#include "mbedtls/md4.h"
76#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010077#include "mbedtls/md.h"
78#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010079#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/pk_internal.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010082#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010083#include "mbedtls/sha1.h"
84#include "mbedtls/sha256.h"
85#include "mbedtls/sha512.h"
86#include "mbedtls/xtea.h"
87
Gilles Peskinee59236f2018-01-27 23:32:46 +010088
89
Gilles Peskine996deb12018-08-01 15:45:45 +020090#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
91
Gilles Peskinee59236f2018-01-27 23:32:46 +010092/* Implementation that should never be optimized out by the compiler */
93static void mbedtls_zeroize( void *v, size_t n )
94{
95 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
96}
97
Gilles Peskine9ef733f2018-02-07 21:05:37 +010098/* constant-time buffer comparison */
99static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
100{
101 size_t i;
102 unsigned char diff = 0;
103
104 for( i = 0; i < n; i++ )
105 diff |= a[i] ^ b[i];
106
107 return( diff );
108}
109
110
111
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100112/****************************************************************/
113/* Global data, support functions and library management */
114/****************************************************************/
115
116/* Number of key slots (plus one because 0 is not used).
117 * The value is a compile-time constant for now, for simplicity. */
Gilles Peskine828ed142018-06-18 23:25:51 +0200118#define PSA_KEY_SLOT_COUNT 32
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100119
Gilles Peskine2d277862018-06-18 15:41:12 +0200120typedef struct
121{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100122 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +0300123 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200124 psa_key_lifetime_t lifetime;
Gilles Peskine2d277862018-06-18 15:41:12 +0200125 union
126 {
127 struct raw_data
128 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100129 uint8_t *data;
130 size_t bytes;
131 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100132#if defined(MBEDTLS_RSA_C)
133 mbedtls_rsa_context *rsa;
134#endif /* MBEDTLS_RSA_C */
135#if defined(MBEDTLS_ECP_C)
136 mbedtls_ecp_keypair *ecp;
137#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100138 } data;
139} key_slot_t;
140
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200141static int key_type_is_raw_bytes( psa_key_type_t type )
142{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200143 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200144}
145
Gilles Peskine2d277862018-06-18 15:41:12 +0200146typedef struct
147{
Gilles Peskinee59236f2018-01-27 23:32:46 +0100148 int initialized;
149 mbedtls_entropy_context entropy;
150 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200151 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinee59236f2018-01-27 23:32:46 +0100152} psa_global_data_t;
153
154static psa_global_data_t global_data;
155
itayzafrir0adf0fc2018-09-06 16:24:41 +0300156#define GUARD_MODULE_INITIALIZED \
157 if( global_data.initialized == 0 ) \
158 return( PSA_ERROR_BAD_STATE );
159
Gilles Peskinee59236f2018-01-27 23:32:46 +0100160static psa_status_t mbedtls_to_psa_error( int ret )
161{
Gilles Peskinea5905292018-02-07 20:59:33 +0100162 /* If there's both a high-level code and low-level code, dispatch on
163 * the high-level code. */
164 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100165 {
166 case 0:
167 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100168
169 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
170 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
171 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
172 return( PSA_ERROR_NOT_SUPPORTED );
173 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
174 return( PSA_ERROR_HARDWARE_FAILURE );
175
176 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
177 return( PSA_ERROR_HARDWARE_FAILURE );
178
Gilles Peskine9a944802018-06-21 09:35:35 +0200179 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
180 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
181 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
182 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
183 case MBEDTLS_ERR_ASN1_INVALID_DATA:
184 return( PSA_ERROR_INVALID_ARGUMENT );
185 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
186 return( PSA_ERROR_INSUFFICIENT_MEMORY );
187 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
188 return( PSA_ERROR_BUFFER_TOO_SMALL );
189
Gilles Peskinea5905292018-02-07 20:59:33 +0100190 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
191 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
192 return( PSA_ERROR_NOT_SUPPORTED );
193 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
194 return( PSA_ERROR_HARDWARE_FAILURE );
195
196 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
197 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
198 return( PSA_ERROR_NOT_SUPPORTED );
199 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
200 return( PSA_ERROR_HARDWARE_FAILURE );
201
202 case MBEDTLS_ERR_CCM_BAD_INPUT:
203 return( PSA_ERROR_INVALID_ARGUMENT );
204 case MBEDTLS_ERR_CCM_AUTH_FAILED:
205 return( PSA_ERROR_INVALID_SIGNATURE );
206 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
207 return( PSA_ERROR_HARDWARE_FAILURE );
208
209 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
210 return( PSA_ERROR_NOT_SUPPORTED );
211 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
212 return( PSA_ERROR_INVALID_ARGUMENT );
213 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
214 return( PSA_ERROR_INSUFFICIENT_MEMORY );
215 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
216 return( PSA_ERROR_INVALID_PADDING );
217 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
218 return( PSA_ERROR_BAD_STATE );
219 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
220 return( PSA_ERROR_INVALID_SIGNATURE );
221 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
222 return( PSA_ERROR_TAMPERING_DETECTED );
223 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
224 return( PSA_ERROR_HARDWARE_FAILURE );
225
226 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
227 return( PSA_ERROR_HARDWARE_FAILURE );
228
229 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
230 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
231 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
232 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
233 return( PSA_ERROR_NOT_SUPPORTED );
234 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
235 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
236
237 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
238 return( PSA_ERROR_NOT_SUPPORTED );
239 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
240 return( PSA_ERROR_HARDWARE_FAILURE );
241
Gilles Peskinee59236f2018-01-27 23:32:46 +0100242 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
243 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
244 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
245 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100246
247 case MBEDTLS_ERR_GCM_AUTH_FAILED:
248 return( PSA_ERROR_INVALID_SIGNATURE );
249 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200250 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100251 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
254 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
255 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
256 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
257 return( PSA_ERROR_HARDWARE_FAILURE );
258
259 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
260 return( PSA_ERROR_NOT_SUPPORTED );
261 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MD_ALLOC_FAILED:
264 return( PSA_ERROR_INSUFFICIENT_MEMORY );
265 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
266 return( PSA_ERROR_STORAGE_FAILURE );
267 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
268 return( PSA_ERROR_HARDWARE_FAILURE );
269
Gilles Peskinef76aa772018-10-29 19:24:33 +0100270 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
271 return( PSA_ERROR_STORAGE_FAILURE );
272 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
273 return( PSA_ERROR_INVALID_ARGUMENT );
274 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
275 return( PSA_ERROR_INVALID_ARGUMENT );
276 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
277 return( PSA_ERROR_BUFFER_TOO_SMALL );
278 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
279 return( PSA_ERROR_INVALID_ARGUMENT );
280 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
281 return( PSA_ERROR_INVALID_ARGUMENT );
282 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
283 return( PSA_ERROR_INVALID_ARGUMENT );
284 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
285 return( PSA_ERROR_INSUFFICIENT_MEMORY );
286
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100287 case MBEDTLS_ERR_PK_ALLOC_FAILED:
288 return( PSA_ERROR_INSUFFICIENT_MEMORY );
289 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
290 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100293 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100294 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
295 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
296 return( PSA_ERROR_INVALID_ARGUMENT );
297 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
298 return( PSA_ERROR_NOT_SUPPORTED );
299 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
300 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
301 return( PSA_ERROR_NOT_PERMITTED );
302 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
303 return( PSA_ERROR_INVALID_ARGUMENT );
304 case MBEDTLS_ERR_PK_INVALID_ALG:
305 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
306 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
307 return( PSA_ERROR_NOT_SUPPORTED );
308 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
309 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100310 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
311 return( PSA_ERROR_HARDWARE_FAILURE );
312
313 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
314 return( PSA_ERROR_HARDWARE_FAILURE );
315
316 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
317 return( PSA_ERROR_INVALID_ARGUMENT );
318 case MBEDTLS_ERR_RSA_INVALID_PADDING:
319 return( PSA_ERROR_INVALID_PADDING );
320 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
321 return( PSA_ERROR_HARDWARE_FAILURE );
322 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
323 return( PSA_ERROR_INVALID_ARGUMENT );
324 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
325 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
326 return( PSA_ERROR_TAMPERING_DETECTED );
327 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
328 return( PSA_ERROR_INVALID_SIGNATURE );
329 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
330 return( PSA_ERROR_BUFFER_TOO_SMALL );
331 case MBEDTLS_ERR_RSA_RNG_FAILED:
332 return( PSA_ERROR_INSUFFICIENT_MEMORY );
333 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
334 return( PSA_ERROR_NOT_SUPPORTED );
335 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
336 return( PSA_ERROR_HARDWARE_FAILURE );
337
338 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
339 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
340 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
341 return( PSA_ERROR_HARDWARE_FAILURE );
342
343 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
344 return( PSA_ERROR_INVALID_ARGUMENT );
345 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
346 return( PSA_ERROR_HARDWARE_FAILURE );
347
itayzafrir5c753392018-05-08 11:18:38 +0300348 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300349 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300350 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300351 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
352 return( PSA_ERROR_BUFFER_TOO_SMALL );
353 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
354 return( PSA_ERROR_NOT_SUPPORTED );
355 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
356 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
357 return( PSA_ERROR_INVALID_SIGNATURE );
358 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
359 return( PSA_ERROR_INSUFFICIENT_MEMORY );
360 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
361 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300362
Gilles Peskinee59236f2018-01-27 23:32:46 +0100363 default:
364 return( PSA_ERROR_UNKNOWN_ERROR );
365 }
366}
367
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200368
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200369
370
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100371/****************************************************************/
372/* Key management */
373/****************************************************************/
374
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100375#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200376static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
377{
378 switch( grpid )
379 {
380 case MBEDTLS_ECP_DP_SECP192R1:
381 return( PSA_ECC_CURVE_SECP192R1 );
382 case MBEDTLS_ECP_DP_SECP224R1:
383 return( PSA_ECC_CURVE_SECP224R1 );
384 case MBEDTLS_ECP_DP_SECP256R1:
385 return( PSA_ECC_CURVE_SECP256R1 );
386 case MBEDTLS_ECP_DP_SECP384R1:
387 return( PSA_ECC_CURVE_SECP384R1 );
388 case MBEDTLS_ECP_DP_SECP521R1:
389 return( PSA_ECC_CURVE_SECP521R1 );
390 case MBEDTLS_ECP_DP_BP256R1:
391 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
392 case MBEDTLS_ECP_DP_BP384R1:
393 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
394 case MBEDTLS_ECP_DP_BP512R1:
395 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
396 case MBEDTLS_ECP_DP_CURVE25519:
397 return( PSA_ECC_CURVE_CURVE25519 );
398 case MBEDTLS_ECP_DP_SECP192K1:
399 return( PSA_ECC_CURVE_SECP192K1 );
400 case MBEDTLS_ECP_DP_SECP224K1:
401 return( PSA_ECC_CURVE_SECP224K1 );
402 case MBEDTLS_ECP_DP_SECP256K1:
403 return( PSA_ECC_CURVE_SECP256K1 );
404 case MBEDTLS_ECP_DP_CURVE448:
405 return( PSA_ECC_CURVE_CURVE448 );
406 default:
407 return( 0 );
408 }
409}
410
Gilles Peskine12313cd2018-06-20 00:20:32 +0200411static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
412{
413 switch( curve )
414 {
415 case PSA_ECC_CURVE_SECP192R1:
416 return( MBEDTLS_ECP_DP_SECP192R1 );
417 case PSA_ECC_CURVE_SECP224R1:
418 return( MBEDTLS_ECP_DP_SECP224R1 );
419 case PSA_ECC_CURVE_SECP256R1:
420 return( MBEDTLS_ECP_DP_SECP256R1 );
421 case PSA_ECC_CURVE_SECP384R1:
422 return( MBEDTLS_ECP_DP_SECP384R1 );
423 case PSA_ECC_CURVE_SECP521R1:
424 return( MBEDTLS_ECP_DP_SECP521R1 );
425 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
426 return( MBEDTLS_ECP_DP_BP256R1 );
427 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
428 return( MBEDTLS_ECP_DP_BP384R1 );
429 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
430 return( MBEDTLS_ECP_DP_BP512R1 );
431 case PSA_ECC_CURVE_CURVE25519:
432 return( MBEDTLS_ECP_DP_CURVE25519 );
433 case PSA_ECC_CURVE_SECP192K1:
434 return( MBEDTLS_ECP_DP_SECP192K1 );
435 case PSA_ECC_CURVE_SECP224K1:
436 return( MBEDTLS_ECP_DP_SECP224K1 );
437 case PSA_ECC_CURVE_SECP256K1:
438 return( MBEDTLS_ECP_DP_SECP256K1 );
439 case PSA_ECC_CURVE_CURVE448:
440 return( MBEDTLS_ECP_DP_CURVE448 );
441 default:
442 return( MBEDTLS_ECP_DP_NONE );
443 }
444}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100445#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200446
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200447static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
448 size_t bits,
449 struct raw_data *raw )
450{
451 /* Check that the bit size is acceptable for the key type */
452 switch( type )
453 {
454 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200455 if( bits == 0 )
456 {
457 raw->bytes = 0;
458 raw->data = NULL;
459 return( PSA_SUCCESS );
460 }
461 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200462#if defined(MBEDTLS_MD_C)
463 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200464#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200465 case PSA_KEY_TYPE_DERIVE:
466 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200467#if defined(MBEDTLS_AES_C)
468 case PSA_KEY_TYPE_AES:
469 if( bits != 128 && bits != 192 && bits != 256 )
470 return( PSA_ERROR_INVALID_ARGUMENT );
471 break;
472#endif
473#if defined(MBEDTLS_CAMELLIA_C)
474 case PSA_KEY_TYPE_CAMELLIA:
475 if( bits != 128 && bits != 192 && bits != 256 )
476 return( PSA_ERROR_INVALID_ARGUMENT );
477 break;
478#endif
479#if defined(MBEDTLS_DES_C)
480 case PSA_KEY_TYPE_DES:
481 if( bits != 64 && bits != 128 && bits != 192 )
482 return( PSA_ERROR_INVALID_ARGUMENT );
483 break;
484#endif
485#if defined(MBEDTLS_ARC4_C)
486 case PSA_KEY_TYPE_ARC4:
487 if( bits < 8 || bits > 2048 )
488 return( PSA_ERROR_INVALID_ARGUMENT );
489 break;
490#endif
491 default:
492 return( PSA_ERROR_NOT_SUPPORTED );
493 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200494 if( bits % 8 != 0 )
495 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200496
497 /* Allocate memory for the key */
498 raw->bytes = PSA_BITS_TO_BYTES( bits );
499 raw->data = mbedtls_calloc( 1, raw->bytes );
500 if( raw->data == NULL )
501 {
502 raw->bytes = 0;
503 return( PSA_ERROR_INSUFFICIENT_MEMORY );
504 }
505 return( PSA_SUCCESS );
506}
507
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200508#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100509/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
510 * that are not a multiple of 8) well. For example, there is only
511 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
512 * way to return the exact bit size of a key.
513 * To keep things simple, reject non-byte-aligned key sizes. */
514static psa_status_t psa_check_rsa_key_byte_aligned(
515 const mbedtls_rsa_context *rsa )
516{
517 mbedtls_mpi n;
518 psa_status_t status;
519 mbedtls_mpi_init( &n );
520 status = mbedtls_to_psa_error(
521 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
522 if( status == PSA_SUCCESS )
523 {
524 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
525 status = PSA_ERROR_NOT_SUPPORTED;
526 }
527 mbedtls_mpi_free( &n );
528 return( status );
529}
530
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200531static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
532 mbedtls_rsa_context **p_rsa )
533{
534 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
535 return( PSA_ERROR_INVALID_ARGUMENT );
536 else
537 {
538 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
Gilles Peskineaac64a22018-11-12 18:37:42 +0100539 /* The size of an RSA key doesn't have to be a multiple of 8.
540 * Mbed TLS supports non-byte-aligned key sizes, but not well.
541 * For example, mbedtls_rsa_get_len() returns the key size in
542 * bytes, not in bits. */
543 size_t bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100544 psa_status_t status;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200545 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
546 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +0100547 status = psa_check_rsa_key_byte_aligned( rsa );
548 if( status != PSA_SUCCESS )
549 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200550 *p_rsa = rsa;
551 return( PSA_SUCCESS );
552 }
553}
554#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
555
556#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinef76aa772018-10-29 19:24:33 +0100557/* Import an elliptic curve parsed by the mbedtls pk module. */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200558static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
559 mbedtls_pk_context *pk,
560 mbedtls_ecp_keypair **p_ecp )
561{
562 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
563 return( PSA_ERROR_INVALID_ARGUMENT );
564 else
565 {
566 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
567 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
568 if( actual_curve != expected_curve )
569 return( PSA_ERROR_INVALID_ARGUMENT );
570 *p_ecp = ecp;
571 return( PSA_SUCCESS );
572 }
573}
574#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
575
Gilles Peskinef76aa772018-10-29 19:24:33 +0100576#if defined(MBEDTLS_ECP_C)
577/* Import a private key given as a byte string which is the private value
578 * in big-endian order. */
579static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
580 const uint8_t *data,
581 size_t data_length,
582 mbedtls_ecp_keypair **p_ecp )
583{
584 psa_status_t status = PSA_ERROR_TAMPERING_DETECTED;
585 mbedtls_ecp_keypair *ecp = NULL;
586 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
587
588 *p_ecp = NULL;
589 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
590 if( ecp == NULL )
591 return( PSA_ERROR_INSUFFICIENT_MEMORY );
592
593 /* Load the group. */
594 status = mbedtls_to_psa_error(
595 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
596 if( status != PSA_SUCCESS )
597 goto exit;
598 /* Load the secret value. */
599 status = mbedtls_to_psa_error(
600 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
601 if( status != PSA_SUCCESS )
602 goto exit;
603 /* Validate the private key. */
604 status = mbedtls_to_psa_error(
605 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
606 if( status != PSA_SUCCESS )
607 goto exit;
608 /* Calculate the public key from the private key. */
609 status = mbedtls_to_psa_error(
610 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
611 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
612 if( status != PSA_SUCCESS )
613 goto exit;
614
615 *p_ecp = ecp;
616 return( PSA_SUCCESS );
617
618exit:
619 if( ecp != NULL )
620 {
621 mbedtls_ecp_keypair_free( ecp );
622 mbedtls_free( ecp );
623 }
624 return( status );
625}
626#endif /* defined(MBEDTLS_ECP_C) */
627
Darryl Green940d72c2018-07-13 13:18:51 +0100628static psa_status_t psa_import_key_into_slot( key_slot_t *slot,
629 const uint8_t *data,
630 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100631{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200632 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100633
Darryl Green940d72c2018-07-13 13:18:51 +0100634 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100635 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100636 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100637 if( data_length > SIZE_MAX / 8 )
638 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green940d72c2018-07-13 13:18:51 +0100639 status = prepare_raw_data_slot( slot->type,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200640 PSA_BYTES_TO_BITS( data_length ),
641 &slot->data.raw );
642 if( status != PSA_SUCCESS )
643 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200644 if( data_length != 0 )
645 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100646 }
647 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100648#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100649 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100650 {
Darryl Green940d72c2018-07-13 13:18:51 +0100651 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100652 data, data_length,
653 &slot->data.ecp );
654 if( status != PSA_SUCCESS )
655 return( status );
656 }
657 else
658#endif /* MBEDTLS_ECP_C */
Gilles Peskine969ac722018-01-28 18:16:59 +0100659#if defined(MBEDTLS_PK_PARSE_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100660 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
661 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100662 {
663 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100664 mbedtls_pk_context pk;
665 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200666
667 /* Parse the data. */
Darryl Green940d72c2018-07-13 13:18:51 +0100668 if( PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100669 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
670 else
671 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100672 if( ret != 0 )
673 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200674
675 /* We have something that the pkparse module recognizes.
676 * If it has the expected type and passes any type-specific
677 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100678#if defined(MBEDTLS_RSA_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100679 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200680 status = psa_import_rsa_key( &pk, &slot->data.rsa );
681 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100682#endif /* MBEDTLS_RSA_C */
683#if defined(MBEDTLS_ECP_C)
Darryl Green940d72c2018-07-13 13:18:51 +0100684 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
685 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( slot->type ),
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200686 &pk, &slot->data.ecp );
687 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100688#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200689 {
690 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200691 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200692
Gilles Peskinec648d692018-06-28 08:46:13 +0200693 /* Free the content of the pk object only on error. On success,
694 * the content of the object has been stored in the slot. */
695 if( status != PSA_SUCCESS )
696 {
697 mbedtls_pk_free( &pk );
698 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100699 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100700 }
701 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100702#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100703 {
704 return( PSA_ERROR_NOT_SUPPORTED );
705 }
Darryl Green940d72c2018-07-13 13:18:51 +0100706 return( PSA_SUCCESS );
707}
708
Darryl Greend49a4992018-06-18 17:27:26 +0100709#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
710static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t key,
711 key_slot_t *p_slot )
712{
713 psa_status_t status = PSA_SUCCESS;
714 uint8_t *key_data = NULL;
715 size_t key_data_length = 0;
716
717 status = psa_load_persistent_key( key, &( p_slot )->type,
718 &( p_slot )->policy, &key_data,
719 &key_data_length );
720 if( status != PSA_SUCCESS )
721 goto exit;
722 status = psa_import_key_into_slot( p_slot,
723 key_data, key_data_length );
724exit:
725 psa_free_persistent_key_data( key_data, key_data_length );
726 return( status );
727}
728#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
729
Darryl Green06fd18d2018-07-16 11:21:11 +0100730/* Retrieve a key slot, occupied or not. */
731static psa_status_t psa_get_key_slot( psa_key_slot_t key,
732 key_slot_t **p_slot )
733{
734 GUARD_MODULE_INITIALIZED;
735
736 /* 0 is not a valid slot number under any circumstance. This
737 * implementation provides slots number 1 to N where N is the
738 * number of available slots. */
739 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
740 return( PSA_ERROR_INVALID_ARGUMENT );
741
742 *p_slot = &global_data.key_slots[key - 1];
Darryl Greend49a4992018-06-18 17:27:26 +0100743
744#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
745 if( ( *p_slot )->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
746 {
747 /* There are two circumstances this can occur: the key material has
748 * not yet been created, or the key exists in storage but has not yet
749 * been loaded into memory. */
750 if( ( *p_slot )->type == PSA_KEY_TYPE_NONE )
751 {
752 psa_status_t status = PSA_SUCCESS;
753 status = psa_load_persistent_key_into_slot( key, *p_slot );
754 if( status != PSA_ERROR_EMPTY_SLOT )
755 return( status );
756 }
757 }
758#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
759
Darryl Green06fd18d2018-07-16 11:21:11 +0100760 return( PSA_SUCCESS );
761}
762
763/* Retrieve an empty key slot (slot with no key data, but possibly
764 * with some metadata such as a policy). */
765static psa_status_t psa_get_empty_key_slot( psa_key_slot_t key,
766 key_slot_t **p_slot )
767{
768 psa_status_t status;
769 key_slot_t *slot = NULL;
770
771 *p_slot = NULL;
772
773 status = psa_get_key_slot( key, &slot );
774 if( status != PSA_SUCCESS )
775 return( status );
776
777 if( slot->type != PSA_KEY_TYPE_NONE )
778 return( PSA_ERROR_OCCUPIED_SLOT );
779
780 *p_slot = slot;
781 return( status );
782}
783
784/** Retrieve a slot which must contain a key. The key must have allow all the
785 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
786 * operations with this algorithm. */
787static psa_status_t psa_get_key_from_slot( psa_key_slot_t key,
788 key_slot_t **p_slot,
789 psa_key_usage_t usage,
790 psa_algorithm_t alg )
791{
792 psa_status_t status;
793 key_slot_t *slot = NULL;
794
795 *p_slot = NULL;
796
797 status = psa_get_key_slot( key, &slot );
798 if( status != PSA_SUCCESS )
799 return( status );
800 if( slot->type == PSA_KEY_TYPE_NONE )
801 return( PSA_ERROR_EMPTY_SLOT );
802
803 /* Enforce that usage policy for the key slot contains all the flags
804 * required by the usage parameter. There is one exception: public
805 * keys can always be exported, so we treat public key objects as
806 * if they had the export flag. */
807 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
808 usage &= ~PSA_KEY_USAGE_EXPORT;
809 if( ( slot->policy.usage & usage ) != usage )
810 return( PSA_ERROR_NOT_PERMITTED );
811 if( alg != 0 && ( alg != slot->policy.alg ) )
812 return( PSA_ERROR_NOT_PERMITTED );
813
814 *p_slot = slot;
815 return( PSA_SUCCESS );
816}
Darryl Green940d72c2018-07-13 13:18:51 +0100817
Darryl Green40225ba2018-11-15 14:48:15 +0000818static psa_status_t psa_remove_key_data_from_memory( key_slot_t *slot )
819{
820 if( slot->type == PSA_KEY_TYPE_NONE )
821 {
822 /* No key material to clean. */
823 }
824 else if( key_type_is_raw_bytes( slot->type ) )
825 {
826 mbedtls_free( slot->data.raw.data );
827 }
828 else
829#if defined(MBEDTLS_RSA_C)
830 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
831 {
832 mbedtls_rsa_free( slot->data.rsa );
833 mbedtls_free( slot->data.rsa );
834 }
835 else
836#endif /* defined(MBEDTLS_RSA_C) */
837#if defined(MBEDTLS_ECP_C)
838 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
839 {
840 mbedtls_ecp_keypair_free( slot->data.ecp );
841 mbedtls_free( slot->data.ecp );
842 }
843 else
844#endif /* defined(MBEDTLS_ECP_C) */
845 {
846 /* Shouldn't happen: the key type is not any type that we
847 * put in. */
848 return( PSA_ERROR_TAMPERING_DETECTED );
849 }
850
851 return( PSA_SUCCESS );
852}
853
Darryl Green940d72c2018-07-13 13:18:51 +0100854psa_status_t psa_import_key( psa_key_slot_t key,
855 psa_key_type_t type,
856 const uint8_t *data,
857 size_t data_length )
858{
859 key_slot_t *slot;
860 psa_status_t status;
861
862 status = psa_get_empty_key_slot( key, &slot );
863 if( status != PSA_SUCCESS )
864 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100865
866 slot->type = type;
Darryl Green940d72c2018-07-13 13:18:51 +0100867
868 status = psa_import_key_into_slot( slot, data, data_length );
869 if( status != PSA_SUCCESS )
870 {
871 slot->type = PSA_KEY_TYPE_NONE;
872 return( status );
873 }
874
Darryl Greend49a4992018-06-18 17:27:26 +0100875#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
876 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
877 {
878 /* Store in file location */
879 status = psa_save_persistent_key( key, slot->type, &slot->policy, data,
880 data_length );
881 if( status != PSA_SUCCESS )
882 {
883 (void) psa_remove_key_data_from_memory( slot );
884 slot->type = PSA_KEY_TYPE_NONE;
885 }
886 }
887#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
888
889 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100890}
891
Gilles Peskine2d277862018-06-18 15:41:12 +0200892psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100893{
894 key_slot_t *slot;
Darryl Greend49a4992018-06-18 17:27:26 +0100895 psa_status_t status = PSA_SUCCESS;
896 psa_status_t storage_status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100897
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200898 status = psa_get_key_slot( key, &slot );
899 if( status != PSA_SUCCESS )
900 return( status );
Darryl Greend49a4992018-06-18 17:27:26 +0100901#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
902 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
903 {
904 storage_status = psa_destroy_persistent_key( key );
905 }
906#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
907 status = psa_remove_key_data_from_memory( slot );
908 /* Zeroize the slot to wipe metadata such as policies. */
909 mbedtls_zeroize( slot, sizeof( *slot ) );
910 if( status != PSA_SUCCESS )
911 return( status );
912 return( storage_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100913}
914
Gilles Peskineb870b182018-07-06 16:02:09 +0200915/* Return the size of the key in the given slot, in bits. */
916static size_t psa_get_key_bits( const key_slot_t *slot )
917{
918 if( key_type_is_raw_bytes( slot->type ) )
919 return( slot->data.raw.bytes * 8 );
920#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200921 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineaac64a22018-11-12 18:37:42 +0100922 return( PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) ) );
Gilles Peskineb870b182018-07-06 16:02:09 +0200923#endif /* defined(MBEDTLS_RSA_C) */
924#if defined(MBEDTLS_ECP_C)
925 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
926 return( slot->data.ecp->grp.pbits );
927#endif /* defined(MBEDTLS_ECP_C) */
928 /* Shouldn't happen except on an empty slot. */
929 return( 0 );
930}
931
Gilles Peskine2d277862018-06-18 15:41:12 +0200932psa_status_t psa_get_key_information( psa_key_slot_t key,
933 psa_key_type_t *type,
934 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100935{
936 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200937 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100938
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100939 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200940 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100941 if( bits != NULL )
942 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200943 status = psa_get_key_slot( key, &slot );
944 if( status != PSA_SUCCESS )
945 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200946
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100947 if( slot->type == PSA_KEY_TYPE_NONE )
948 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200949 if( type != NULL )
950 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200951 if( bits != NULL )
952 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100953 return( PSA_SUCCESS );
954}
955
Gilles Peskine2d277862018-06-18 15:41:12 +0200956static psa_status_t psa_internal_export_key( psa_key_slot_t key,
957 uint8_t *data,
958 size_t data_size,
959 size_t *data_length,
960 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100961{
962 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200963 psa_status_t status;
964 /* Exporting a public key doesn't require a usage flag. If we're
965 * called by psa_export_public_key(), don't require the EXPORT flag.
966 * If we're called by psa_export_key(), do require the EXPORT flag;
967 * if the key turns out to be public key object, psa_get_key_from_slot()
968 * will ignore this flag. */
969 psa_key_usage_t usage = export_public_key ? 0 : PSA_KEY_USAGE_EXPORT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100970
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100971 /* Set the key to empty now, so that even when there are errors, we always
972 * set data_length to a value between 0 and data_size. On error, setting
973 * the key to empty is a good choice because an empty key representation is
974 * unlikely to be accepted anywhere. */
975 *data_length = 0;
976
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200977 status = psa_get_key_from_slot( key, &slot, usage, 0 );
978 if( status != PSA_SUCCESS )
979 return( status );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200980 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300981 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300982
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200983 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100984 {
985 if( slot->data.raw.bytes > data_size )
986 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +0100987 if( data_size != 0 )
988 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200989 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +0100990 memset( data + slot->data.raw.bytes, 0,
991 data_size - slot->data.raw.bytes );
992 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100993 *data_length = slot->data.raw.bytes;
994 return( PSA_SUCCESS );
995 }
Gilles Peskine188c71e2018-10-29 19:26:02 +0100996#if defined(MBEDTLS_ECP_C)
997 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
998 {
999 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
1000 if( bytes > data_size )
1001 return( PSA_ERROR_BUFFER_TOO_SMALL );
1002 status = mbedtls_to_psa_error(
1003 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1004 if( status != PSA_SUCCESS )
1005 return( status );
1006 memset( data + bytes, 0, data_size - bytes );
1007 *data_length = bytes;
1008 return( PSA_SUCCESS );
1009 }
1010#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001011 else
Moran Peker17e36e12018-05-02 12:55:20 +03001012 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001013#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001014 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001015 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001016 {
Moran Pekera998bc62018-04-16 18:16:20 +03001017 mbedtls_pk_context pk;
1018 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001019 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001020 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001021#if defined(MBEDTLS_RSA_C)
1022 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001023 pk.pk_info = &mbedtls_rsa_info;
1024 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001025#else
1026 return( PSA_ERROR_NOT_SUPPORTED );
1027#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001028 }
1029 else
1030 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001031#if defined(MBEDTLS_ECP_C)
1032 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001033 pk.pk_info = &mbedtls_eckey_info;
1034 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001035#else
1036 return( PSA_ERROR_NOT_SUPPORTED );
1037#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001038 }
Moran Pekerd7326592018-05-29 16:56:39 +03001039 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001040 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +03001041 else
1042 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +03001043 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001044 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001045 /* If data_size is 0 then data may be NULL and then the
1046 * call to memset would have undefined behavior. */
1047 if( data_size != 0 )
1048 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001049 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001050 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001051 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1052 * Move the data to the beginning and erase remaining data
1053 * at the original location. */
1054 if( 2 * (size_t) ret <= data_size )
1055 {
1056 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001057 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001058 }
1059 else if( (size_t) ret < data_size )
1060 {
1061 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001062 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001063 }
Moran Pekera998bc62018-04-16 18:16:20 +03001064 *data_length = ret;
1065 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001066 }
1067 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001068#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001069 {
1070 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001071 it is valid for a special-purpose implementation to omit
1072 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001073 return( PSA_ERROR_NOT_SUPPORTED );
1074 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001075 }
1076}
1077
Gilles Peskine2d277862018-06-18 15:41:12 +02001078psa_status_t psa_export_key( psa_key_slot_t key,
1079 uint8_t *data,
1080 size_t data_size,
1081 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001082{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001083 return( psa_internal_export_key( key, data, data_size,
1084 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001085}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001086
Gilles Peskine2d277862018-06-18 15:41:12 +02001087psa_status_t psa_export_public_key( psa_key_slot_t key,
1088 uint8_t *data,
1089 size_t data_size,
1090 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001091{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001092 return( psa_internal_export_key( key, data, data_size,
1093 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001094}
1095
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001096
1097
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001098/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001099/* Message digests */
1100/****************************************************************/
1101
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001102static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001103{
1104 switch( alg )
1105 {
1106#if defined(MBEDTLS_MD2_C)
1107 case PSA_ALG_MD2:
1108 return( &mbedtls_md2_info );
1109#endif
1110#if defined(MBEDTLS_MD4_C)
1111 case PSA_ALG_MD4:
1112 return( &mbedtls_md4_info );
1113#endif
1114#if defined(MBEDTLS_MD5_C)
1115 case PSA_ALG_MD5:
1116 return( &mbedtls_md5_info );
1117#endif
1118#if defined(MBEDTLS_RIPEMD160_C)
1119 case PSA_ALG_RIPEMD160:
1120 return( &mbedtls_ripemd160_info );
1121#endif
1122#if defined(MBEDTLS_SHA1_C)
1123 case PSA_ALG_SHA_1:
1124 return( &mbedtls_sha1_info );
1125#endif
1126#if defined(MBEDTLS_SHA256_C)
1127 case PSA_ALG_SHA_224:
1128 return( &mbedtls_sha224_info );
1129 case PSA_ALG_SHA_256:
1130 return( &mbedtls_sha256_info );
1131#endif
1132#if defined(MBEDTLS_SHA512_C)
1133 case PSA_ALG_SHA_384:
1134 return( &mbedtls_sha384_info );
1135 case PSA_ALG_SHA_512:
1136 return( &mbedtls_sha512_info );
1137#endif
1138 default:
1139 return( NULL );
1140 }
1141}
1142
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001143psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1144{
1145 switch( operation->alg )
1146 {
Gilles Peskine81736312018-06-26 15:04:31 +02001147 case 0:
1148 /* The object has (apparently) been initialized but it is not
1149 * in use. It's ok to call abort on such an object, and there's
1150 * nothing to do. */
1151 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001152#if defined(MBEDTLS_MD2_C)
1153 case PSA_ALG_MD2:
1154 mbedtls_md2_free( &operation->ctx.md2 );
1155 break;
1156#endif
1157#if defined(MBEDTLS_MD4_C)
1158 case PSA_ALG_MD4:
1159 mbedtls_md4_free( &operation->ctx.md4 );
1160 break;
1161#endif
1162#if defined(MBEDTLS_MD5_C)
1163 case PSA_ALG_MD5:
1164 mbedtls_md5_free( &operation->ctx.md5 );
1165 break;
1166#endif
1167#if defined(MBEDTLS_RIPEMD160_C)
1168 case PSA_ALG_RIPEMD160:
1169 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1170 break;
1171#endif
1172#if defined(MBEDTLS_SHA1_C)
1173 case PSA_ALG_SHA_1:
1174 mbedtls_sha1_free( &operation->ctx.sha1 );
1175 break;
1176#endif
1177#if defined(MBEDTLS_SHA256_C)
1178 case PSA_ALG_SHA_224:
1179 case PSA_ALG_SHA_256:
1180 mbedtls_sha256_free( &operation->ctx.sha256 );
1181 break;
1182#endif
1183#if defined(MBEDTLS_SHA512_C)
1184 case PSA_ALG_SHA_384:
1185 case PSA_ALG_SHA_512:
1186 mbedtls_sha512_free( &operation->ctx.sha512 );
1187 break;
1188#endif
1189 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001190 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001191 }
1192 operation->alg = 0;
1193 return( PSA_SUCCESS );
1194}
1195
Gilles Peskineda8191d2018-07-08 19:46:38 +02001196psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001197 psa_algorithm_t alg )
1198{
1199 int ret;
1200 operation->alg = 0;
1201 switch( alg )
1202 {
1203#if defined(MBEDTLS_MD2_C)
1204 case PSA_ALG_MD2:
1205 mbedtls_md2_init( &operation->ctx.md2 );
1206 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1207 break;
1208#endif
1209#if defined(MBEDTLS_MD4_C)
1210 case PSA_ALG_MD4:
1211 mbedtls_md4_init( &operation->ctx.md4 );
1212 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1213 break;
1214#endif
1215#if defined(MBEDTLS_MD5_C)
1216 case PSA_ALG_MD5:
1217 mbedtls_md5_init( &operation->ctx.md5 );
1218 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1219 break;
1220#endif
1221#if defined(MBEDTLS_RIPEMD160_C)
1222 case PSA_ALG_RIPEMD160:
1223 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1224 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1225 break;
1226#endif
1227#if defined(MBEDTLS_SHA1_C)
1228 case PSA_ALG_SHA_1:
1229 mbedtls_sha1_init( &operation->ctx.sha1 );
1230 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1231 break;
1232#endif
1233#if defined(MBEDTLS_SHA256_C)
1234 case PSA_ALG_SHA_224:
1235 mbedtls_sha256_init( &operation->ctx.sha256 );
1236 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1237 break;
1238 case PSA_ALG_SHA_256:
1239 mbedtls_sha256_init( &operation->ctx.sha256 );
1240 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1241 break;
1242#endif
1243#if defined(MBEDTLS_SHA512_C)
1244 case PSA_ALG_SHA_384:
1245 mbedtls_sha512_init( &operation->ctx.sha512 );
1246 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1247 break;
1248 case PSA_ALG_SHA_512:
1249 mbedtls_sha512_init( &operation->ctx.sha512 );
1250 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1251 break;
1252#endif
1253 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001254 return( PSA_ALG_IS_HASH( alg ) ?
1255 PSA_ERROR_NOT_SUPPORTED :
1256 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001257 }
1258 if( ret == 0 )
1259 operation->alg = alg;
1260 else
1261 psa_hash_abort( operation );
1262 return( mbedtls_to_psa_error( ret ) );
1263}
1264
1265psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1266 const uint8_t *input,
1267 size_t input_length )
1268{
1269 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001270
1271 /* Don't require hash implementations to behave correctly on a
1272 * zero-length input, which may have an invalid pointer. */
1273 if( input_length == 0 )
1274 return( PSA_SUCCESS );
1275
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001276 switch( operation->alg )
1277 {
1278#if defined(MBEDTLS_MD2_C)
1279 case PSA_ALG_MD2:
1280 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1281 input, input_length );
1282 break;
1283#endif
1284#if defined(MBEDTLS_MD4_C)
1285 case PSA_ALG_MD4:
1286 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1287 input, input_length );
1288 break;
1289#endif
1290#if defined(MBEDTLS_MD5_C)
1291 case PSA_ALG_MD5:
1292 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1293 input, input_length );
1294 break;
1295#endif
1296#if defined(MBEDTLS_RIPEMD160_C)
1297 case PSA_ALG_RIPEMD160:
1298 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1299 input, input_length );
1300 break;
1301#endif
1302#if defined(MBEDTLS_SHA1_C)
1303 case PSA_ALG_SHA_1:
1304 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1305 input, input_length );
1306 break;
1307#endif
1308#if defined(MBEDTLS_SHA256_C)
1309 case PSA_ALG_SHA_224:
1310 case PSA_ALG_SHA_256:
1311 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1312 input, input_length );
1313 break;
1314#endif
1315#if defined(MBEDTLS_SHA512_C)
1316 case PSA_ALG_SHA_384:
1317 case PSA_ALG_SHA_512:
1318 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1319 input, input_length );
1320 break;
1321#endif
1322 default:
1323 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1324 break;
1325 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001326
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001327 if( ret != 0 )
1328 psa_hash_abort( operation );
1329 return( mbedtls_to_psa_error( ret ) );
1330}
1331
1332psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1333 uint8_t *hash,
1334 size_t hash_size,
1335 size_t *hash_length )
1336{
itayzafrir40835d42018-08-02 13:14:17 +03001337 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001338 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001339 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001340
1341 /* Fill the output buffer with something that isn't a valid hash
1342 * (barring an attack on the hash and deliberately-crafted input),
1343 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001344 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001345 /* If hash_size is 0 then hash may be NULL and then the
1346 * call to memset would have undefined behavior. */
1347 if( hash_size != 0 )
1348 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001349
1350 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001351 {
1352 status = PSA_ERROR_BUFFER_TOO_SMALL;
1353 goto exit;
1354 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001355
1356 switch( operation->alg )
1357 {
1358#if defined(MBEDTLS_MD2_C)
1359 case PSA_ALG_MD2:
1360 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1361 break;
1362#endif
1363#if defined(MBEDTLS_MD4_C)
1364 case PSA_ALG_MD4:
1365 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1366 break;
1367#endif
1368#if defined(MBEDTLS_MD5_C)
1369 case PSA_ALG_MD5:
1370 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1371 break;
1372#endif
1373#if defined(MBEDTLS_RIPEMD160_C)
1374 case PSA_ALG_RIPEMD160:
1375 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1376 break;
1377#endif
1378#if defined(MBEDTLS_SHA1_C)
1379 case PSA_ALG_SHA_1:
1380 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1381 break;
1382#endif
1383#if defined(MBEDTLS_SHA256_C)
1384 case PSA_ALG_SHA_224:
1385 case PSA_ALG_SHA_256:
1386 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1387 break;
1388#endif
1389#if defined(MBEDTLS_SHA512_C)
1390 case PSA_ALG_SHA_384:
1391 case PSA_ALG_SHA_512:
1392 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1393 break;
1394#endif
1395 default:
1396 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1397 break;
1398 }
itayzafrir40835d42018-08-02 13:14:17 +03001399 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001400
itayzafrir40835d42018-08-02 13:14:17 +03001401exit:
1402 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001403 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001404 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001405 return( psa_hash_abort( operation ) );
1406 }
1407 else
1408 {
1409 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001410 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001411 }
1412}
1413
Gilles Peskine2d277862018-06-18 15:41:12 +02001414psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1415 const uint8_t *hash,
1416 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001417{
1418 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1419 size_t actual_hash_length;
1420 psa_status_t status = psa_hash_finish( operation,
1421 actual_hash, sizeof( actual_hash ),
1422 &actual_hash_length );
1423 if( status != PSA_SUCCESS )
1424 return( status );
1425 if( actual_hash_length != hash_length )
1426 return( PSA_ERROR_INVALID_SIGNATURE );
1427 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1428 return( PSA_ERROR_INVALID_SIGNATURE );
1429 return( PSA_SUCCESS );
1430}
1431
1432
1433
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001434/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001435/* MAC */
1436/****************************************************************/
1437
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001438static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001439 psa_algorithm_t alg,
1440 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001441 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001442 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001443{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001444 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001445 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001446
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001447 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001448 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001449
Gilles Peskine8c9def32018-02-08 10:02:12 +01001450 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1451 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001452 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001453 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001454 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001455 mode = MBEDTLS_MODE_STREAM;
1456 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001457 case PSA_ALG_CTR:
1458 mode = MBEDTLS_MODE_CTR;
1459 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001460 case PSA_ALG_CFB:
1461 mode = MBEDTLS_MODE_CFB;
1462 break;
1463 case PSA_ALG_OFB:
1464 mode = MBEDTLS_MODE_OFB;
1465 break;
1466 case PSA_ALG_CBC_NO_PADDING:
1467 mode = MBEDTLS_MODE_CBC;
1468 break;
1469 case PSA_ALG_CBC_PKCS7:
1470 mode = MBEDTLS_MODE_CBC;
1471 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001472 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001473 mode = MBEDTLS_MODE_CCM;
1474 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001475 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001476 mode = MBEDTLS_MODE_GCM;
1477 break;
1478 default:
1479 return( NULL );
1480 }
1481 }
1482 else if( alg == PSA_ALG_CMAC )
1483 mode = MBEDTLS_MODE_ECB;
1484 else if( alg == PSA_ALG_GMAC )
1485 mode = MBEDTLS_MODE_GCM;
1486 else
1487 return( NULL );
1488
1489 switch( key_type )
1490 {
1491 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001492 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001493 break;
1494 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001495 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1496 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001497 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001498 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001499 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001500 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001501 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1502 * but two-key Triple-DES is functionally three-key Triple-DES
1503 * with K1=K3, so that's how we present it to mbedtls. */
1504 if( key_bits == 128 )
1505 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001506 break;
1507 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001508 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001509 break;
1510 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001511 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001512 break;
1513 default:
1514 return( NULL );
1515 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001516 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001517 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001518
Jaeden Amero23bbb752018-06-26 14:16:54 +01001519 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1520 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001521}
1522
Gilles Peskinea05219c2018-11-16 16:02:56 +01001523#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001524static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001525{
Gilles Peskine2d277862018-06-18 15:41:12 +02001526 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001527 {
1528 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001529 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001530 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001531 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001532 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001533 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001534 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001535 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001536 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001537 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001538 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001539 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001540 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001541 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001542 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001543 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001544 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001545 return( 128 );
1546 default:
1547 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001548 }
1549}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001550#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001551
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001552/* Initialize the MAC operation structure. Once this function has been
1553 * called, psa_mac_abort can run and will do the right thing. */
1554static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1555 psa_algorithm_t alg )
1556{
1557 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1558
1559 operation->alg = alg;
1560 operation->key_set = 0;
1561 operation->iv_set = 0;
1562 operation->iv_required = 0;
1563 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001564 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001565
1566#if defined(MBEDTLS_CMAC_C)
1567 if( alg == PSA_ALG_CMAC )
1568 {
1569 operation->iv_required = 0;
1570 mbedtls_cipher_init( &operation->ctx.cmac );
1571 status = PSA_SUCCESS;
1572 }
1573 else
1574#endif /* MBEDTLS_CMAC_C */
1575#if defined(MBEDTLS_MD_C)
1576 if( PSA_ALG_IS_HMAC( operation->alg ) )
1577 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001578 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1579 operation->ctx.hmac.hash_ctx.alg = 0;
1580 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001581 }
1582 else
1583#endif /* MBEDTLS_MD_C */
1584 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001585 if( ! PSA_ALG_IS_MAC( alg ) )
1586 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001587 }
1588
1589 if( status != PSA_SUCCESS )
1590 memset( operation, 0, sizeof( *operation ) );
1591 return( status );
1592}
1593
Gilles Peskine01126fa2018-07-12 17:04:55 +02001594#if defined(MBEDTLS_MD_C)
1595static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1596{
1597 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1598 return( psa_hash_abort( &hmac->hash_ctx ) );
1599}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001600
1601static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1602{
1603 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1604 memset( hmac, 0, sizeof( *hmac ) );
1605}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001606#endif /* MBEDTLS_MD_C */
1607
Gilles Peskine8c9def32018-02-08 10:02:12 +01001608psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1609{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001610 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001611 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001612 /* The object has (apparently) been initialized but it is not
1613 * in use. It's ok to call abort on such an object, and there's
1614 * nothing to do. */
1615 return( PSA_SUCCESS );
1616 }
1617 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001618#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001619 if( operation->alg == PSA_ALG_CMAC )
1620 {
1621 mbedtls_cipher_free( &operation->ctx.cmac );
1622 }
1623 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001624#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001625#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001626 if( PSA_ALG_IS_HMAC( operation->alg ) )
1627 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001628 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001629 }
1630 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001631#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001632 {
1633 /* Sanity check (shouldn't happen: operation->alg should
1634 * always have been initialized to a valid value). */
1635 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001636 }
Moran Peker41deec42018-04-04 15:43:05 +03001637
Gilles Peskine8c9def32018-02-08 10:02:12 +01001638 operation->alg = 0;
1639 operation->key_set = 0;
1640 operation->iv_set = 0;
1641 operation->iv_required = 0;
1642 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001643 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001644
Gilles Peskine8c9def32018-02-08 10:02:12 +01001645 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001646
1647bad_state:
1648 /* If abort is called on an uninitialized object, we can't trust
1649 * anything. Wipe the object in case it contains confidential data.
1650 * This may result in a memory leak if a pointer gets overwritten,
1651 * but it's too late to do anything about this. */
1652 memset( operation, 0, sizeof( *operation ) );
1653 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001654}
1655
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001656#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001657static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001658 size_t key_bits,
1659 key_slot_t *slot,
1660 const mbedtls_cipher_info_t *cipher_info )
1661{
1662 int ret;
1663
1664 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001665
1666 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1667 if( ret != 0 )
1668 return( ret );
1669
1670 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1671 slot->data.raw.data,
1672 key_bits );
1673 return( ret );
1674}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001675#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001676
Gilles Peskine248051a2018-06-20 16:09:38 +02001677#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001678static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1679 const uint8_t *key,
1680 size_t key_length,
1681 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001682{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001683 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001684 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001685 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001686 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001687 psa_status_t status;
1688
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001689 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1690 * overflow below. This should never trigger if the hash algorithm
1691 * is implemented correctly. */
1692 /* The size checks against the ipad and opad buffers cannot be written
1693 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1694 * because that triggers -Wlogical-op on GCC 7.3. */
1695 if( block_size > sizeof( ipad ) )
1696 return( PSA_ERROR_NOT_SUPPORTED );
1697 if( block_size > sizeof( hmac->opad ) )
1698 return( PSA_ERROR_NOT_SUPPORTED );
1699 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001700 return( PSA_ERROR_NOT_SUPPORTED );
1701
Gilles Peskined223b522018-06-11 18:12:58 +02001702 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001703 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001704 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001705 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001706 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001707 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001708 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001709 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001710 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001711 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001712 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001713 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001714 }
Gilles Peskine96889972018-07-12 17:07:03 +02001715 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1716 * but it is permitted. It is common when HMAC is used in HKDF, for
1717 * example. Don't call `memcpy` in the 0-length because `key` could be
1718 * an invalid pointer which would make the behavior undefined. */
1719 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001720 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001721
Gilles Peskined223b522018-06-11 18:12:58 +02001722 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1723 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001724 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001725 ipad[i] ^= 0x36;
1726 memset( ipad + key_length, 0x36, block_size - key_length );
1727
1728 /* Copy the key material from ipad to opad, flipping the requisite bits,
1729 * and filling the rest of opad with the requisite constant. */
1730 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001731 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1732 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001733
Gilles Peskine01126fa2018-07-12 17:04:55 +02001734 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001735 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001736 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001737
Gilles Peskine01126fa2018-07-12 17:04:55 +02001738 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001739
1740cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001741 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001742
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001743 return( status );
1744}
Gilles Peskine248051a2018-06-20 16:09:38 +02001745#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001746
Gilles Peskine89167cb2018-07-08 20:12:23 +02001747static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1748 psa_key_slot_t key,
1749 psa_algorithm_t alg,
1750 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001751{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001752 psa_status_t status;
1753 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001754 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001755 psa_key_usage_t usage =
1756 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001757 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001758 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001759
Gilles Peskined911eb72018-08-14 15:18:45 +02001760 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001761 if( status != PSA_SUCCESS )
1762 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001763 if( is_sign )
1764 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001765
Gilles Peskine89167cb2018-07-08 20:12:23 +02001766 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001767 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001768 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001769 key_bits = psa_get_key_bits( slot );
1770
Gilles Peskine8c9def32018-02-08 10:02:12 +01001771#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001772 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001773 {
1774 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001775 mbedtls_cipher_info_from_psa( full_length_alg,
1776 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001777 int ret;
1778 if( cipher_info == NULL )
1779 {
1780 status = PSA_ERROR_NOT_SUPPORTED;
1781 goto exit;
1782 }
1783 operation->mac_size = cipher_info->block_size;
1784 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1785 status = mbedtls_to_psa_error( ret );
1786 }
1787 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001788#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001789#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001790 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001791 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001792 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001793 if( hash_alg == 0 )
1794 {
1795 status = PSA_ERROR_NOT_SUPPORTED;
1796 goto exit;
1797 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001798
1799 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1800 /* Sanity check. This shouldn't fail on a valid configuration. */
1801 if( operation->mac_size == 0 ||
1802 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1803 {
1804 status = PSA_ERROR_NOT_SUPPORTED;
1805 goto exit;
1806 }
1807
Gilles Peskine01126fa2018-07-12 17:04:55 +02001808 if( slot->type != PSA_KEY_TYPE_HMAC )
1809 {
1810 status = PSA_ERROR_INVALID_ARGUMENT;
1811 goto exit;
1812 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001813
Gilles Peskine01126fa2018-07-12 17:04:55 +02001814 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1815 slot->data.raw.data,
1816 slot->data.raw.bytes,
1817 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001818 }
1819 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001820#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001821 {
1822 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001823 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001824
Gilles Peskined911eb72018-08-14 15:18:45 +02001825 if( truncated == 0 )
1826 {
1827 /* The "normal" case: untruncated algorithm. Nothing to do. */
1828 }
1829 else if( truncated < 4 )
1830 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001831 /* A very short MAC is too short for security since it can be
1832 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1833 * so we make this our minimum, even though 32 bits is still
1834 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001835 status = PSA_ERROR_NOT_SUPPORTED;
1836 }
1837 else if( truncated > operation->mac_size )
1838 {
1839 /* It's impossible to "truncate" to a larger length. */
1840 status = PSA_ERROR_INVALID_ARGUMENT;
1841 }
1842 else
1843 operation->mac_size = truncated;
1844
Gilles Peskinefbfac682018-07-08 20:51:54 +02001845exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001846 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001847 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001848 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001849 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001850 else
1851 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001852 operation->key_set = 1;
1853 }
1854 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001855}
1856
Gilles Peskine89167cb2018-07-08 20:12:23 +02001857psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1858 psa_key_slot_t key,
1859 psa_algorithm_t alg )
1860{
1861 return( psa_mac_setup( operation, key, alg, 1 ) );
1862}
1863
1864psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1865 psa_key_slot_t key,
1866 psa_algorithm_t alg )
1867{
1868 return( psa_mac_setup( operation, key, alg, 0 ) );
1869}
1870
Gilles Peskine8c9def32018-02-08 10:02:12 +01001871psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1872 const uint8_t *input,
1873 size_t input_length )
1874{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001875 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001876 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001877 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001878 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001879 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001880 operation->has_input = 1;
1881
Gilles Peskine8c9def32018-02-08 10:02:12 +01001882#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001883 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001884 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001885 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1886 input, input_length );
1887 status = mbedtls_to_psa_error( ret );
1888 }
1889 else
1890#endif /* MBEDTLS_CMAC_C */
1891#if defined(MBEDTLS_MD_C)
1892 if( PSA_ALG_IS_HMAC( operation->alg ) )
1893 {
1894 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1895 input_length );
1896 }
1897 else
1898#endif /* MBEDTLS_MD_C */
1899 {
1900 /* This shouldn't happen if `operation` was initialized by
1901 * a setup function. */
1902 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001903 }
1904
Gilles Peskinefbfac682018-07-08 20:51:54 +02001905cleanup:
1906 if( status != PSA_SUCCESS )
1907 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001908 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001909}
1910
Gilles Peskine01126fa2018-07-12 17:04:55 +02001911#if defined(MBEDTLS_MD_C)
1912static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1913 uint8_t *mac,
1914 size_t mac_size )
1915{
1916 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1917 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1918 size_t hash_size = 0;
1919 size_t block_size = psa_get_hash_block_size( hash_alg );
1920 psa_status_t status;
1921
Gilles Peskine01126fa2018-07-12 17:04:55 +02001922 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1923 if( status != PSA_SUCCESS )
1924 return( status );
1925 /* From here on, tmp needs to be wiped. */
1926
1927 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1928 if( status != PSA_SUCCESS )
1929 goto exit;
1930
1931 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1932 if( status != PSA_SUCCESS )
1933 goto exit;
1934
1935 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1936 if( status != PSA_SUCCESS )
1937 goto exit;
1938
Gilles Peskined911eb72018-08-14 15:18:45 +02001939 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1940 if( status != PSA_SUCCESS )
1941 goto exit;
1942
1943 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001944
1945exit:
1946 mbedtls_zeroize( tmp, hash_size );
1947 return( status );
1948}
1949#endif /* MBEDTLS_MD_C */
1950
mohammad16036df908f2018-04-02 08:34:15 -07001951static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001952 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001953 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001954{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001955 if( ! operation->key_set )
1956 return( PSA_ERROR_BAD_STATE );
1957 if( operation->iv_required && ! operation->iv_set )
1958 return( PSA_ERROR_BAD_STATE );
1959
Gilles Peskine8c9def32018-02-08 10:02:12 +01001960 if( mac_size < operation->mac_size )
1961 return( PSA_ERROR_BUFFER_TOO_SMALL );
1962
Gilles Peskine8c9def32018-02-08 10:02:12 +01001963#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001964 if( operation->alg == PSA_ALG_CMAC )
1965 {
Gilles Peskined911eb72018-08-14 15:18:45 +02001966 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
1967 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
1968 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02001969 memcpy( mac, tmp, operation->mac_size );
Gilles Peskined911eb72018-08-14 15:18:45 +02001970 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001971 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001972 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001973 else
1974#endif /* MBEDTLS_CMAC_C */
1975#if defined(MBEDTLS_MD_C)
1976 if( PSA_ALG_IS_HMAC( operation->alg ) )
1977 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001978 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02001979 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001980 }
1981 else
1982#endif /* MBEDTLS_MD_C */
1983 {
1984 /* This shouldn't happen if `operation` was initialized by
1985 * a setup function. */
1986 return( PSA_ERROR_BAD_STATE );
1987 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001988}
1989
Gilles Peskineacd4be32018-07-08 19:56:25 +02001990psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1991 uint8_t *mac,
1992 size_t mac_size,
1993 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001994{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001995 psa_status_t status;
1996
1997 /* Fill the output buffer with something that isn't a valid mac
1998 * (barring an attack on the mac and deliberately-crafted input),
1999 * in case the caller doesn't check the return status properly. */
2000 *mac_length = mac_size;
2001 /* If mac_size is 0 then mac may be NULL and then the
2002 * call to memset would have undefined behavior. */
2003 if( mac_size != 0 )
2004 memset( mac, '!', mac_size );
2005
Gilles Peskine89167cb2018-07-08 20:12:23 +02002006 if( ! operation->is_sign )
2007 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002008 status = PSA_ERROR_BAD_STATE;
2009 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002010 }
mohammad16036df908f2018-04-02 08:34:15 -07002011
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002012 status = psa_mac_finish_internal( operation, mac, mac_size );
2013
2014cleanup:
2015 if( status == PSA_SUCCESS )
2016 {
2017 status = psa_mac_abort( operation );
2018 if( status == PSA_SUCCESS )
2019 *mac_length = operation->mac_size;
2020 else
2021 memset( mac, '!', mac_size );
2022 }
2023 else
2024 psa_mac_abort( operation );
2025 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002026}
2027
Gilles Peskineacd4be32018-07-08 19:56:25 +02002028psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2029 const uint8_t *mac,
2030 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002031{
Gilles Peskine828ed142018-06-18 23:25:51 +02002032 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002033 psa_status_t status;
2034
Gilles Peskine89167cb2018-07-08 20:12:23 +02002035 if( operation->is_sign )
2036 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002037 status = PSA_ERROR_BAD_STATE;
2038 goto cleanup;
2039 }
2040 if( operation->mac_size != mac_length )
2041 {
2042 status = PSA_ERROR_INVALID_SIGNATURE;
2043 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002044 }
mohammad16036df908f2018-04-02 08:34:15 -07002045
2046 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002047 actual_mac, sizeof( actual_mac ) );
2048
2049 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2050 status = PSA_ERROR_INVALID_SIGNATURE;
2051
2052cleanup:
2053 if( status == PSA_SUCCESS )
2054 status = psa_mac_abort( operation );
2055 else
2056 psa_mac_abort( operation );
2057
Gilles Peskine99b7d6b2018-08-21 14:56:19 +02002058 mbedtls_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002059
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002060 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002061}
2062
2063
Gilles Peskine20035e32018-02-03 22:44:14 +01002064
Gilles Peskine20035e32018-02-03 22:44:14 +01002065/****************************************************************/
2066/* Asymmetric cryptography */
2067/****************************************************************/
2068
Gilles Peskine2b450e32018-06-27 15:42:46 +02002069#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002070/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002071 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002072static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2073 size_t hash_length,
2074 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002075{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002076 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002077 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002078 *md_alg = mbedtls_md_get_type( md_info );
2079
2080 /* The Mbed TLS RSA module uses an unsigned int for hash length
2081 * parameters. Validate that it fits so that we don't risk an
2082 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002083#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002084 if( hash_length > UINT_MAX )
2085 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002086#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002087
2088#if defined(MBEDTLS_PKCS1_V15)
2089 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2090 * must be correct. */
2091 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2092 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002093 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002094 if( md_info == NULL )
2095 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002096 if( mbedtls_md_get_size( md_info ) != hash_length )
2097 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002098 }
2099#endif /* MBEDTLS_PKCS1_V15 */
2100
2101#if defined(MBEDTLS_PKCS1_V21)
2102 /* PSS requires a hash internally. */
2103 if( PSA_ALG_IS_RSA_PSS( alg ) )
2104 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002105 if( md_info == NULL )
2106 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002107 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002108#endif /* MBEDTLS_PKCS1_V21 */
2109
Gilles Peskine61b91d42018-06-08 16:09:36 +02002110 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002111}
2112
Gilles Peskine2b450e32018-06-27 15:42:46 +02002113static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2114 psa_algorithm_t alg,
2115 const uint8_t *hash,
2116 size_t hash_length,
2117 uint8_t *signature,
2118 size_t signature_size,
2119 size_t *signature_length )
2120{
2121 psa_status_t status;
2122 int ret;
2123 mbedtls_md_type_t md_alg;
2124
2125 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2126 if( status != PSA_SUCCESS )
2127 return( status );
2128
Gilles Peskine630a18a2018-06-29 17:49:35 +02002129 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002130 return( PSA_ERROR_BUFFER_TOO_SMALL );
2131
2132#if defined(MBEDTLS_PKCS1_V15)
2133 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2134 {
2135 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2136 MBEDTLS_MD_NONE );
2137 ret = mbedtls_rsa_pkcs1_sign( rsa,
2138 mbedtls_ctr_drbg_random,
2139 &global_data.ctr_drbg,
2140 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002141 md_alg,
2142 (unsigned int) hash_length,
2143 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002144 signature );
2145 }
2146 else
2147#endif /* MBEDTLS_PKCS1_V15 */
2148#if defined(MBEDTLS_PKCS1_V21)
2149 if( PSA_ALG_IS_RSA_PSS( alg ) )
2150 {
2151 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2152 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2153 mbedtls_ctr_drbg_random,
2154 &global_data.ctr_drbg,
2155 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002156 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002157 (unsigned int) hash_length,
2158 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002159 signature );
2160 }
2161 else
2162#endif /* MBEDTLS_PKCS1_V21 */
2163 {
2164 return( PSA_ERROR_INVALID_ARGUMENT );
2165 }
2166
2167 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002168 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002169 return( mbedtls_to_psa_error( ret ) );
2170}
2171
2172static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2173 psa_algorithm_t alg,
2174 const uint8_t *hash,
2175 size_t hash_length,
2176 const uint8_t *signature,
2177 size_t signature_length )
2178{
2179 psa_status_t status;
2180 int ret;
2181 mbedtls_md_type_t md_alg;
2182
2183 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2184 if( status != PSA_SUCCESS )
2185 return( status );
2186
Gilles Peskine630a18a2018-06-29 17:49:35 +02002187 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002188 return( PSA_ERROR_BUFFER_TOO_SMALL );
2189
2190#if defined(MBEDTLS_PKCS1_V15)
2191 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2192 {
2193 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2194 MBEDTLS_MD_NONE );
2195 ret = mbedtls_rsa_pkcs1_verify( rsa,
2196 mbedtls_ctr_drbg_random,
2197 &global_data.ctr_drbg,
2198 MBEDTLS_RSA_PUBLIC,
2199 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002200 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002201 hash,
2202 signature );
2203 }
2204 else
2205#endif /* MBEDTLS_PKCS1_V15 */
2206#if defined(MBEDTLS_PKCS1_V21)
2207 if( PSA_ALG_IS_RSA_PSS( alg ) )
2208 {
2209 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2210 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2211 mbedtls_ctr_drbg_random,
2212 &global_data.ctr_drbg,
2213 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002214 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002215 (unsigned int) hash_length,
2216 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002217 signature );
2218 }
2219 else
2220#endif /* MBEDTLS_PKCS1_V21 */
2221 {
2222 return( PSA_ERROR_INVALID_ARGUMENT );
2223 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002224
2225 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2226 * the rest of the signature is invalid". This has little use in
2227 * practice and PSA doesn't report this distinction. */
2228 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2229 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002230 return( mbedtls_to_psa_error( ret ) );
2231}
2232#endif /* MBEDTLS_RSA_C */
2233
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002234#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002235/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2236 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2237 * (even though these functions don't modify it). */
2238static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2239 psa_algorithm_t alg,
2240 const uint8_t *hash,
2241 size_t hash_length,
2242 uint8_t *signature,
2243 size_t signature_size,
2244 size_t *signature_length )
2245{
2246 int ret;
2247 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002248 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002249 mbedtls_mpi_init( &r );
2250 mbedtls_mpi_init( &s );
2251
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002252 if( signature_size < 2 * curve_bytes )
2253 {
2254 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2255 goto cleanup;
2256 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002257
Gilles Peskinea05219c2018-11-16 16:02:56 +01002258#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002259 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2260 {
2261 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2262 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2263 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2264 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2265 hash, hash_length,
2266 md_alg ) );
2267 }
2268 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002269#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002270 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002271 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002272 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2273 hash, hash_length,
2274 mbedtls_ctr_drbg_random,
2275 &global_data.ctr_drbg ) );
2276 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002277
2278 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2279 signature,
2280 curve_bytes ) );
2281 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2282 signature + curve_bytes,
2283 curve_bytes ) );
2284
2285cleanup:
2286 mbedtls_mpi_free( &r );
2287 mbedtls_mpi_free( &s );
2288 if( ret == 0 )
2289 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002290 return( mbedtls_to_psa_error( ret ) );
2291}
2292
2293static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2294 const uint8_t *hash,
2295 size_t hash_length,
2296 const uint8_t *signature,
2297 size_t signature_length )
2298{
2299 int ret;
2300 mbedtls_mpi r, s;
2301 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2302 mbedtls_mpi_init( &r );
2303 mbedtls_mpi_init( &s );
2304
2305 if( signature_length != 2 * curve_bytes )
2306 return( PSA_ERROR_INVALID_SIGNATURE );
2307
2308 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2309 signature,
2310 curve_bytes ) );
2311 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2312 signature + curve_bytes,
2313 curve_bytes ) );
2314
2315 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2316 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002317
2318cleanup:
2319 mbedtls_mpi_free( &r );
2320 mbedtls_mpi_free( &s );
2321 return( mbedtls_to_psa_error( ret ) );
2322}
2323#endif /* MBEDTLS_ECDSA_C */
2324
Gilles Peskine61b91d42018-06-08 16:09:36 +02002325psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2326 psa_algorithm_t alg,
2327 const uint8_t *hash,
2328 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002329 uint8_t *signature,
2330 size_t signature_size,
2331 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002332{
2333 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002334 psa_status_t status;
2335
2336 *signature_length = signature_size;
2337
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002338 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2339 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002340 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002341 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002342 {
2343 status = PSA_ERROR_INVALID_ARGUMENT;
2344 goto exit;
2345 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002346
Gilles Peskine20035e32018-02-03 22:44:14 +01002347#if defined(MBEDTLS_RSA_C)
2348 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2349 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002350 status = psa_rsa_sign( slot->data.rsa,
2351 alg,
2352 hash, hash_length,
2353 signature, signature_size,
2354 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002355 }
2356 else
2357#endif /* defined(MBEDTLS_RSA_C) */
2358#if defined(MBEDTLS_ECP_C)
2359 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2360 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002361#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002362 if(
2363#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2364 PSA_ALG_IS_ECDSA( alg )
2365#else
2366 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2367#endif
2368 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002369 status = psa_ecdsa_sign( slot->data.ecp,
2370 alg,
2371 hash, hash_length,
2372 signature, signature_size,
2373 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002374 else
2375#endif /* defined(MBEDTLS_ECDSA_C) */
2376 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002377 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002378 }
itayzafrir5c753392018-05-08 11:18:38 +03002379 }
2380 else
2381#endif /* defined(MBEDTLS_ECP_C) */
2382 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002383 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002384 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002385
2386exit:
2387 /* Fill the unused part of the output buffer (the whole buffer on error,
2388 * the trailing part on success) with something that isn't a valid mac
2389 * (barring an attack on the mac and deliberately-crafted input),
2390 * in case the caller doesn't check the return status properly. */
2391 if( status == PSA_SUCCESS )
2392 memset( signature + *signature_length, '!',
2393 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002394 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002395 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002396 /* If signature_size is 0 then we have nothing to do. We must not call
2397 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002398 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002399}
2400
2401psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2402 psa_algorithm_t alg,
2403 const uint8_t *hash,
2404 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002405 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002406 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002407{
2408 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002409 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002410
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002411 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2412 if( status != PSA_SUCCESS )
2413 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002414
Gilles Peskine61b91d42018-06-08 16:09:36 +02002415#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002416 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002417 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002418 return( psa_rsa_verify( slot->data.rsa,
2419 alg,
2420 hash, hash_length,
2421 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002422 }
2423 else
2424#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002425#if defined(MBEDTLS_ECP_C)
2426 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2427 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002428#if defined(MBEDTLS_ECDSA_C)
2429 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002430 return( psa_ecdsa_verify( slot->data.ecp,
2431 hash, hash_length,
2432 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002433 else
2434#endif /* defined(MBEDTLS_ECDSA_C) */
2435 {
2436 return( PSA_ERROR_INVALID_ARGUMENT );
2437 }
itayzafrir5c753392018-05-08 11:18:38 +03002438 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002439 else
2440#endif /* defined(MBEDTLS_ECP_C) */
2441 {
2442 return( PSA_ERROR_NOT_SUPPORTED );
2443 }
2444}
2445
Gilles Peskine072ac562018-06-30 00:21:29 +02002446#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2447static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2448 mbedtls_rsa_context *rsa )
2449{
2450 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2451 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2452 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2453 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2454}
2455#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2456
Gilles Peskine61b91d42018-06-08 16:09:36 +02002457psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2458 psa_algorithm_t alg,
2459 const uint8_t *input,
2460 size_t input_length,
2461 const uint8_t *salt,
2462 size_t salt_length,
2463 uint8_t *output,
2464 size_t output_size,
2465 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002466{
2467 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002468 psa_status_t status;
2469
Darryl Green5cc689a2018-07-24 15:34:10 +01002470 (void) input;
2471 (void) input_length;
2472 (void) salt;
2473 (void) output;
2474 (void) output_size;
2475
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002476 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002477
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002478 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2479 return( PSA_ERROR_INVALID_ARGUMENT );
2480
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002481 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2482 if( status != PSA_SUCCESS )
2483 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002484 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2485 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002486 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002487
2488#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002489 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002490 {
2491 mbedtls_rsa_context *rsa = slot->data.rsa;
2492 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002493 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002494 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002495#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002496 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002497 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002498 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2499 mbedtls_ctr_drbg_random,
2500 &global_data.ctr_drbg,
2501 MBEDTLS_RSA_PUBLIC,
2502 input_length,
2503 input,
2504 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002505 }
2506 else
2507#endif /* MBEDTLS_PKCS1_V15 */
2508#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002509 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002510 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002511 psa_rsa_oaep_set_padding_mode( alg, rsa );
2512 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2513 mbedtls_ctr_drbg_random,
2514 &global_data.ctr_drbg,
2515 MBEDTLS_RSA_PUBLIC,
2516 salt, salt_length,
2517 input_length,
2518 input,
2519 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002520 }
2521 else
2522#endif /* MBEDTLS_PKCS1_V21 */
2523 {
2524 return( PSA_ERROR_INVALID_ARGUMENT );
2525 }
2526 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002527 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002528 return( mbedtls_to_psa_error( ret ) );
2529 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002530 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002531#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002532 {
2533 return( PSA_ERROR_NOT_SUPPORTED );
2534 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002535}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002536
Gilles Peskine61b91d42018-06-08 16:09:36 +02002537psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2538 psa_algorithm_t alg,
2539 const uint8_t *input,
2540 size_t input_length,
2541 const uint8_t *salt,
2542 size_t salt_length,
2543 uint8_t *output,
2544 size_t output_size,
2545 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002546{
2547 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002548 psa_status_t status;
2549
Darryl Green5cc689a2018-07-24 15:34:10 +01002550 (void) input;
2551 (void) input_length;
2552 (void) salt;
2553 (void) output;
2554 (void) output_size;
2555
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002556 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002557
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002558 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2559 return( PSA_ERROR_INVALID_ARGUMENT );
2560
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002561 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2562 if( status != PSA_SUCCESS )
2563 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002564 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2565 return( PSA_ERROR_INVALID_ARGUMENT );
2566
2567#if defined(MBEDTLS_RSA_C)
2568 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2569 {
2570 mbedtls_rsa_context *rsa = slot->data.rsa;
2571 int ret;
2572
Gilles Peskine630a18a2018-06-29 17:49:35 +02002573 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002574 return( PSA_ERROR_INVALID_ARGUMENT );
2575
2576#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002577 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002578 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002579 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2580 mbedtls_ctr_drbg_random,
2581 &global_data.ctr_drbg,
2582 MBEDTLS_RSA_PRIVATE,
2583 output_length,
2584 input,
2585 output,
2586 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002587 }
2588 else
2589#endif /* MBEDTLS_PKCS1_V15 */
2590#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002591 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002592 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002593 psa_rsa_oaep_set_padding_mode( alg, rsa );
2594 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2595 mbedtls_ctr_drbg_random,
2596 &global_data.ctr_drbg,
2597 MBEDTLS_RSA_PRIVATE,
2598 salt, salt_length,
2599 output_length,
2600 input,
2601 output,
2602 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002603 }
2604 else
2605#endif /* MBEDTLS_PKCS1_V21 */
2606 {
2607 return( PSA_ERROR_INVALID_ARGUMENT );
2608 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002609
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002610 return( mbedtls_to_psa_error( ret ) );
2611 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002612 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002613#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002614 {
2615 return( PSA_ERROR_NOT_SUPPORTED );
2616 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002617}
Gilles Peskine20035e32018-02-03 22:44:14 +01002618
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002619
2620
mohammad1603503973b2018-03-12 15:59:30 +02002621/****************************************************************/
2622/* Symmetric cryptography */
2623/****************************************************************/
2624
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002625/* Initialize the cipher operation structure. Once this function has been
2626 * called, psa_cipher_abort can run and will do the right thing. */
2627static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2628 psa_algorithm_t alg )
2629{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002630 if( ! PSA_ALG_IS_CIPHER( alg ) )
2631 {
2632 memset( operation, 0, sizeof( *operation ) );
2633 return( PSA_ERROR_INVALID_ARGUMENT );
2634 }
2635
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002636 operation->alg = alg;
2637 operation->key_set = 0;
2638 operation->iv_set = 0;
2639 operation->iv_required = 1;
2640 operation->iv_size = 0;
2641 operation->block_size = 0;
2642 mbedtls_cipher_init( &operation->ctx.cipher );
2643 return( PSA_SUCCESS );
2644}
2645
Gilles Peskinee553c652018-06-04 16:22:46 +02002646static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2647 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002648 psa_algorithm_t alg,
2649 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002650{
2651 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2652 psa_status_t status;
2653 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002654 size_t key_bits;
2655 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002656 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2657 PSA_KEY_USAGE_ENCRYPT :
2658 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002659
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002660 status = psa_cipher_init( operation, alg );
2661 if( status != PSA_SUCCESS )
2662 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002663
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002664 status = psa_get_key_from_slot( key, &slot, usage, alg);
2665 if( status != PSA_SUCCESS )
2666 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002667 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002668
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002669 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002670 if( cipher_info == NULL )
2671 return( PSA_ERROR_NOT_SUPPORTED );
2672
mohammad1603503973b2018-03-12 15:59:30 +02002673 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002674 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002675 {
2676 psa_cipher_abort( operation );
2677 return( mbedtls_to_psa_error( ret ) );
2678 }
2679
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002680#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002681 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002682 {
2683 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2684 unsigned char keys[24];
2685 memcpy( keys, slot->data.raw.data, 16 );
2686 memcpy( keys + 16, slot->data.raw.data, 8 );
2687 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2688 keys,
2689 192, cipher_operation );
2690 }
2691 else
2692#endif
2693 {
2694 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2695 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002696 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002697 }
Moran Peker41deec42018-04-04 15:43:05 +03002698 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002699 {
2700 psa_cipher_abort( operation );
2701 return( mbedtls_to_psa_error( ret ) );
2702 }
2703
mohammad16038481e742018-03-18 13:57:31 +02002704#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002705 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002706 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002707 case PSA_ALG_CBC_NO_PADDING:
2708 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2709 MBEDTLS_PADDING_NONE );
2710 break;
2711 case PSA_ALG_CBC_PKCS7:
2712 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2713 MBEDTLS_PADDING_PKCS7 );
2714 break;
2715 default:
2716 /* The algorithm doesn't involve padding. */
2717 ret = 0;
2718 break;
2719 }
2720 if( ret != 0 )
2721 {
2722 psa_cipher_abort( operation );
2723 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002724 }
2725#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2726
mohammad1603503973b2018-03-12 15:59:30 +02002727 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002728 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2729 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2730 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002731 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002732 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002733 }
mohammad1603503973b2018-03-12 15:59:30 +02002734
Moran Peker395db872018-05-31 14:07:14 +03002735 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002736}
2737
Gilles Peskinefe119512018-07-08 21:39:34 +02002738psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2739 psa_key_slot_t key,
2740 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002741{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002742 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002743}
2744
Gilles Peskinefe119512018-07-08 21:39:34 +02002745psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2746 psa_key_slot_t key,
2747 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002748{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002749 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002750}
2751
Gilles Peskinefe119512018-07-08 21:39:34 +02002752psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2753 unsigned char *iv,
2754 size_t iv_size,
2755 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002756{
itayzafrir534bd7c2018-08-02 13:56:32 +03002757 psa_status_t status;
2758 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002759 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002760 {
2761 status = PSA_ERROR_BAD_STATE;
2762 goto exit;
2763 }
Moran Peker41deec42018-04-04 15:43:05 +03002764 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002765 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002766 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002767 goto exit;
2768 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002769 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2770 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002771 if( ret != 0 )
2772 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002773 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002774 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002775 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002776
mohammad16038481e742018-03-18 13:57:31 +02002777 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002778 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002779
Moran Peker395db872018-05-31 14:07:14 +03002780exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002781 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002782 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002783 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002784}
2785
Gilles Peskinefe119512018-07-08 21:39:34 +02002786psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2787 const unsigned char *iv,
2788 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002789{
itayzafrir534bd7c2018-08-02 13:56:32 +03002790 psa_status_t status;
2791 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002792 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002793 {
2794 status = PSA_ERROR_BAD_STATE;
2795 goto exit;
2796 }
Moran Pekera28258c2018-05-29 16:25:04 +03002797 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002798 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002799 status = PSA_ERROR_INVALID_ARGUMENT;
2800 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002801 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002802 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2803 status = mbedtls_to_psa_error( ret );
2804exit:
2805 if( status == PSA_SUCCESS )
2806 operation->iv_set = 1;
2807 else
mohammad1603503973b2018-03-12 15:59:30 +02002808 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002809 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002810}
2811
Gilles Peskinee553c652018-06-04 16:22:46 +02002812psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2813 const uint8_t *input,
2814 size_t input_length,
2815 unsigned char *output,
2816 size_t output_size,
2817 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002818{
itayzafrir534bd7c2018-08-02 13:56:32 +03002819 psa_status_t status;
2820 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002821 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002822 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002823 {
2824 /* Take the unprocessed partial block left over from previous
2825 * update calls, if any, plus the input to this call. Remove
2826 * the last partial block, if any. You get the data that will be
2827 * output in this call. */
2828 expected_output_size =
2829 ( operation->ctx.cipher.unprocessed_len + input_length )
2830 / operation->block_size * operation->block_size;
2831 }
2832 else
2833 {
2834 expected_output_size = input_length;
2835 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002836
Gilles Peskine89d789c2018-06-04 17:17:16 +02002837 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002838 {
2839 status = PSA_ERROR_BUFFER_TOO_SMALL;
2840 goto exit;
2841 }
mohammad160382759612018-03-12 18:16:40 +02002842
mohammad1603503973b2018-03-12 15:59:30 +02002843 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002844 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002845 status = mbedtls_to_psa_error( ret );
2846exit:
2847 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002848 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002849 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002850}
2851
Gilles Peskinee553c652018-06-04 16:22:46 +02002852psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2853 uint8_t *output,
2854 size_t output_size,
2855 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002856{
Janos Follath315b51c2018-07-09 16:04:51 +01002857 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2858 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002859 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002860
mohammad1603503973b2018-03-12 15:59:30 +02002861 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002862 {
Janos Follath315b51c2018-07-09 16:04:51 +01002863 status = PSA_ERROR_BAD_STATE;
2864 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002865 }
2866 if( operation->iv_required && ! operation->iv_set )
2867 {
Janos Follath315b51c2018-07-09 16:04:51 +01002868 status = PSA_ERROR_BAD_STATE;
2869 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002870 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002871
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002872 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002873 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2874 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002875 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002876 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002877 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002878 }
2879
Janos Follath315b51c2018-07-09 16:04:51 +01002880 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2881 temp_output_buffer,
2882 output_length );
2883 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002884 {
Janos Follath315b51c2018-07-09 16:04:51 +01002885 status = mbedtls_to_psa_error( cipher_ret );
2886 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002887 }
Janos Follath315b51c2018-07-09 16:04:51 +01002888
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002889 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002890 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002891 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002892 memcpy( output, temp_output_buffer, *output_length );
2893 else
2894 {
Janos Follath315b51c2018-07-09 16:04:51 +01002895 status = PSA_ERROR_BUFFER_TOO_SMALL;
2896 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002897 }
mohammad1603503973b2018-03-12 15:59:30 +02002898
Janos Follath279ab8e2018-07-09 16:13:21 +01002899 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002900 status = psa_cipher_abort( operation );
2901
2902 return( status );
2903
2904error:
2905
2906 *output_length = 0;
2907
Janos Follath279ab8e2018-07-09 16:13:21 +01002908 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002909 (void) psa_cipher_abort( operation );
2910
2911 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002912}
2913
Gilles Peskinee553c652018-06-04 16:22:46 +02002914psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2915{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002916 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002917 {
2918 /* The object has (apparently) been initialized but it is not
2919 * in use. It's ok to call abort on such an object, and there's
2920 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002921 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002922 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002923
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002924 /* Sanity check (shouldn't happen: operation->alg should
2925 * always have been initialized to a valid value). */
2926 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2927 return( PSA_ERROR_BAD_STATE );
2928
mohammad1603503973b2018-03-12 15:59:30 +02002929 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002930
Moran Peker41deec42018-04-04 15:43:05 +03002931 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002932 operation->key_set = 0;
2933 operation->iv_set = 0;
2934 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002935 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002936 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002937
Moran Peker395db872018-05-31 14:07:14 +03002938 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002939}
2940
Gilles Peskinea0655c32018-04-30 17:06:50 +02002941
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002942
mohammad16038cc1cee2018-03-28 01:21:33 +03002943/****************************************************************/
2944/* Key Policy */
2945/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002946
mohammad160327010052018-07-03 13:16:15 +03002947#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002948void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002949{
Gilles Peskine803ce742018-06-18 16:07:14 +02002950 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002951}
2952
Gilles Peskine2d277862018-06-18 15:41:12 +02002953void psa_key_policy_set_usage( psa_key_policy_t *policy,
2954 psa_key_usage_t usage,
2955 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002956{
mohammad16034eed7572018-03-28 05:14:59 -07002957 policy->usage = usage;
2958 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002959}
2960
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002961psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002962{
mohammad16036df908f2018-04-02 08:34:15 -07002963 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002964}
2965
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002966psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002967{
mohammad16036df908f2018-04-02 08:34:15 -07002968 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002969}
mohammad160327010052018-07-03 13:16:15 +03002970#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002971
Gilles Peskine2d277862018-06-18 15:41:12 +02002972psa_status_t psa_set_key_policy( psa_key_slot_t key,
2973 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002974{
2975 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002976 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002977
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002978 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002979 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002980
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002981 status = psa_get_empty_key_slot( key, &slot );
2982 if( status != PSA_SUCCESS )
2983 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002984
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002985 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2986 PSA_KEY_USAGE_ENCRYPT |
2987 PSA_KEY_USAGE_DECRYPT |
2988 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002989 PSA_KEY_USAGE_VERIFY |
2990 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002991 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002992
mohammad16036df908f2018-04-02 08:34:15 -07002993 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002994
2995 return( PSA_SUCCESS );
2996}
2997
Gilles Peskine2d277862018-06-18 15:41:12 +02002998psa_status_t psa_get_key_policy( psa_key_slot_t key,
2999 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003000{
3001 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003002 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003003
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003004 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003005 return( PSA_ERROR_INVALID_ARGUMENT );
3006
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003007 status = psa_get_key_slot( key, &slot );
3008 if( status != PSA_SUCCESS )
3009 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003010
mohammad16036df908f2018-04-02 08:34:15 -07003011 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003012
3013 return( PSA_SUCCESS );
3014}
Gilles Peskine20035e32018-02-03 22:44:14 +01003015
Gilles Peskinea0655c32018-04-30 17:06:50 +02003016
3017
mohammad1603804cd712018-03-20 22:44:08 +02003018/****************************************************************/
3019/* Key Lifetime */
3020/****************************************************************/
3021
Gilles Peskine2d277862018-06-18 15:41:12 +02003022psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
3023 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003024{
3025 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003026 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003027
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003028 status = psa_get_key_slot( key, &slot );
3029 if( status != PSA_SUCCESS )
3030 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003031
mohammad1603804cd712018-03-20 22:44:08 +02003032 *lifetime = slot->lifetime;
3033
3034 return( PSA_SUCCESS );
3035}
3036
Gilles Peskine2d277862018-06-18 15:41:12 +02003037psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01003038 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003039{
3040 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003041 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003042
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003043 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
3044 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
Darryl Greend49a4992018-06-18 17:27:26 +01003045 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003046 return( PSA_ERROR_INVALID_ARGUMENT );
3047
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003048 status = psa_get_empty_key_slot( key, &slot );
3049 if( status != PSA_SUCCESS )
3050 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02003051
Darryl Greend49a4992018-06-18 17:27:26 +01003052 if( lifetime == PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003053 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003054
Darryl Greend49a4992018-06-18 17:27:26 +01003055#if !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
3056 if( lifetime == PSA_KEY_LIFETIME_PERSISTENT )
3057 return( PSA_ERROR_NOT_SUPPORTED );
3058#endif
3059
mohammad1603060ad8a2018-03-20 14:28:38 -07003060 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02003061
3062 return( PSA_SUCCESS );
3063}
3064
Gilles Peskine20035e32018-02-03 22:44:14 +01003065
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003066
mohammad16035955c982018-04-26 00:53:03 +03003067/****************************************************************/
3068/* AEAD */
3069/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003070
Gilles Peskineedf9a652018-08-17 18:11:56 +02003071typedef struct
3072{
3073 key_slot_t *slot;
3074 const mbedtls_cipher_info_t *cipher_info;
3075 union
3076 {
3077#if defined(MBEDTLS_CCM_C)
3078 mbedtls_ccm_context ccm;
3079#endif /* MBEDTLS_CCM_C */
3080#if defined(MBEDTLS_GCM_C)
3081 mbedtls_gcm_context gcm;
3082#endif /* MBEDTLS_GCM_C */
3083 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003084 psa_algorithm_t core_alg;
3085 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003086 uint8_t tag_length;
3087} aead_operation_t;
3088
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003089static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003090{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003091 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003092 {
3093#if defined(MBEDTLS_CCM_C)
3094 case PSA_ALG_CCM:
3095 mbedtls_ccm_free( &operation->ctx.ccm );
3096 break;
3097#endif /* MBEDTLS_CCM_C */
3098#if defined(MBEDTLS_CCM_C)
3099 case PSA_ALG_GCM:
3100 mbedtls_gcm_free( &operation->ctx.gcm );
3101 break;
3102#endif /* MBEDTLS_GCM_C */
3103 }
3104}
3105
3106static psa_status_t psa_aead_setup( aead_operation_t *operation,
3107 psa_key_slot_t key,
3108 psa_key_usage_t usage,
3109 psa_algorithm_t alg )
3110{
3111 psa_status_t status;
3112 size_t key_bits;
3113 mbedtls_cipher_id_t cipher_id;
3114
3115 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
3116 if( status != PSA_SUCCESS )
3117 return( status );
3118
3119 key_bits = psa_get_key_bits( operation->slot );
3120
3121 operation->cipher_info =
3122 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3123 &cipher_id );
3124 if( operation->cipher_info == NULL )
3125 return( PSA_ERROR_NOT_SUPPORTED );
3126
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003127 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003128 {
3129#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003130 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3131 operation->core_alg = PSA_ALG_CCM;
3132 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003133 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3134 return( PSA_ERROR_INVALID_ARGUMENT );
3135 mbedtls_ccm_init( &operation->ctx.ccm );
3136 status = mbedtls_to_psa_error(
3137 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3138 operation->slot->data.raw.data,
3139 (unsigned int) key_bits ) );
3140 if( status != 0 )
3141 goto cleanup;
3142 break;
3143#endif /* MBEDTLS_CCM_C */
3144
3145#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003146 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3147 operation->core_alg = PSA_ALG_GCM;
3148 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003149 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3150 return( PSA_ERROR_INVALID_ARGUMENT );
3151 mbedtls_gcm_init( &operation->ctx.gcm );
3152 status = mbedtls_to_psa_error(
3153 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3154 operation->slot->data.raw.data,
3155 (unsigned int) key_bits ) );
3156 break;
3157#endif /* MBEDTLS_GCM_C */
3158
3159 default:
3160 return( PSA_ERROR_NOT_SUPPORTED );
3161 }
3162
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003163 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3164 {
3165 status = PSA_ERROR_INVALID_ARGUMENT;
3166 goto cleanup;
3167 }
3168 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3169 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3170 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3171 * In both cases, mbedtls_xxx will validate the tag length below. */
3172
Gilles Peskineedf9a652018-08-17 18:11:56 +02003173 return( PSA_SUCCESS );
3174
3175cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003176 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003177 return( status );
3178}
3179
mohammad16035955c982018-04-26 00:53:03 +03003180psa_status_t psa_aead_encrypt( psa_key_slot_t key,
3181 psa_algorithm_t alg,
3182 const uint8_t *nonce,
3183 size_t nonce_length,
3184 const uint8_t *additional_data,
3185 size_t additional_data_length,
3186 const uint8_t *plaintext,
3187 size_t plaintext_length,
3188 uint8_t *ciphertext,
3189 size_t ciphertext_size,
3190 size_t *ciphertext_length )
3191{
mohammad16035955c982018-04-26 00:53:03 +03003192 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003193 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003194 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003195
mohammad1603f08a5502018-06-03 15:05:47 +03003196 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003197
Gilles Peskineedf9a652018-08-17 18:11:56 +02003198 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003199 if( status != PSA_SUCCESS )
3200 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003201
Gilles Peskineedf9a652018-08-17 18:11:56 +02003202 /* For all currently supported modes, the tag is at the end of the
3203 * ciphertext. */
3204 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3205 {
3206 status = PSA_ERROR_BUFFER_TOO_SMALL;
3207 goto exit;
3208 }
3209 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003210
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003211 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003212 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003213 status = mbedtls_to_psa_error(
3214 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3215 MBEDTLS_GCM_ENCRYPT,
3216 plaintext_length,
3217 nonce, nonce_length,
3218 additional_data, additional_data_length,
3219 plaintext, ciphertext,
3220 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003221 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003222 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003223 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003224 status = mbedtls_to_psa_error(
3225 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3226 plaintext_length,
3227 nonce, nonce_length,
3228 additional_data,
3229 additional_data_length,
3230 plaintext, ciphertext,
3231 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003232 }
mohammad16035c8845f2018-05-09 05:40:09 -07003233 else
3234 {
mohammad1603554faad2018-06-03 15:07:38 +03003235 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003236 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003237
Gilles Peskineedf9a652018-08-17 18:11:56 +02003238 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3239 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003240
Gilles Peskineedf9a652018-08-17 18:11:56 +02003241exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003242 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003243 if( status == PSA_SUCCESS )
3244 *ciphertext_length = plaintext_length + operation.tag_length;
3245 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003246}
3247
Gilles Peskineee652a32018-06-01 19:23:52 +02003248/* Locate the tag in a ciphertext buffer containing the encrypted data
3249 * followed by the tag. Return the length of the part preceding the tag in
3250 * *plaintext_length. This is the size of the plaintext in modes where
3251 * the encrypted data has the same size as the plaintext, such as
3252 * CCM and GCM. */
3253static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3254 const uint8_t *ciphertext,
3255 size_t ciphertext_length,
3256 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003257 const uint8_t **p_tag )
3258{
3259 size_t payload_length;
3260 if( tag_length > ciphertext_length )
3261 return( PSA_ERROR_INVALID_ARGUMENT );
3262 payload_length = ciphertext_length - tag_length;
3263 if( payload_length > plaintext_size )
3264 return( PSA_ERROR_BUFFER_TOO_SMALL );
3265 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003266 return( PSA_SUCCESS );
3267}
3268
mohammad16035955c982018-04-26 00:53:03 +03003269psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3270 psa_algorithm_t alg,
3271 const uint8_t *nonce,
3272 size_t nonce_length,
3273 const uint8_t *additional_data,
3274 size_t additional_data_length,
3275 const uint8_t *ciphertext,
3276 size_t ciphertext_length,
3277 uint8_t *plaintext,
3278 size_t plaintext_size,
3279 size_t *plaintext_length )
3280{
mohammad16035955c982018-04-26 00:53:03 +03003281 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003282 aead_operation_t operation;
3283 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003284
Gilles Peskineee652a32018-06-01 19:23:52 +02003285 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003286
Gilles Peskineedf9a652018-08-17 18:11:56 +02003287 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003288 if( status != PSA_SUCCESS )
3289 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003290
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003291 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003292 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003293 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003294 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003295 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003296 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003297 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003298
Gilles Peskineedf9a652018-08-17 18:11:56 +02003299 status = mbedtls_to_psa_error(
3300 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3301 ciphertext_length - operation.tag_length,
3302 nonce, nonce_length,
3303 additional_data,
3304 additional_data_length,
3305 tag, operation.tag_length,
3306 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003307 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003308 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003309 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003310 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003311 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003312 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003313 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003314 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003315
Gilles Peskineedf9a652018-08-17 18:11:56 +02003316 status = mbedtls_to_psa_error(
3317 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3318 ciphertext_length - operation.tag_length,
3319 nonce, nonce_length,
3320 additional_data,
3321 additional_data_length,
3322 ciphertext, plaintext,
3323 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003324 }
mohammad160339574652018-06-01 04:39:53 -07003325 else
3326 {
mohammad1603554faad2018-06-03 15:07:38 +03003327 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003328 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003329
Gilles Peskineedf9a652018-08-17 18:11:56 +02003330 if( status != PSA_SUCCESS && plaintext_size != 0 )
3331 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003332
Gilles Peskineedf9a652018-08-17 18:11:56 +02003333exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003334 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003335 if( status == PSA_SUCCESS )
3336 *plaintext_length = ciphertext_length - operation.tag_length;
3337 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003338}
3339
Gilles Peskinea0655c32018-04-30 17:06:50 +02003340
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003341
Gilles Peskine20035e32018-02-03 22:44:14 +01003342/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003343/* Generators */
3344/****************************************************************/
3345
3346psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3347{
3348 psa_status_t status = PSA_SUCCESS;
3349 if( generator->alg == 0 )
3350 {
3351 /* The object has (apparently) been initialized but it is not
3352 * in use. It's ok to call abort on such an object, and there's
3353 * nothing to do. */
3354 }
3355 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003356 if( generator->alg == PSA_ALG_SELECT_RAW )
3357 {
3358 if( generator->ctx.buffer.data != NULL )
3359 {
3360 mbedtls_zeroize( generator->ctx.buffer.data,
3361 generator->ctx.buffer.size );
3362 mbedtls_free( generator->ctx.buffer.data );
3363 }
3364 }
3365 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003366#if defined(MBEDTLS_MD_C)
3367 if( PSA_ALG_IS_HKDF( generator->alg ) )
3368 {
3369 mbedtls_free( generator->ctx.hkdf.info );
3370 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3371 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003372 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3373 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3374 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003375 {
3376 if( generator->ctx.tls12_prf.key != NULL )
3377 {
3378 mbedtls_zeroize( generator->ctx.tls12_prf.key,
3379 generator->ctx.tls12_prf.key_len );
3380 mbedtls_free( generator->ctx.tls12_prf.key );
3381 }
Hanno Becker580fba12018-11-13 20:50:45 +00003382
3383 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3384 {
3385 mbedtls_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
3386 generator->ctx.tls12_prf.Ai_with_seed_len );
3387 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3388 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003389 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003390 else
3391#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003392 {
3393 status = PSA_ERROR_BAD_STATE;
3394 }
3395 memset( generator, 0, sizeof( *generator ) );
3396 return( status );
3397}
3398
3399
3400psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3401 size_t *capacity)
3402{
3403 *capacity = generator->capacity;
3404 return( PSA_SUCCESS );
3405}
3406
Gilles Peskinebef7f142018-07-12 17:22:21 +02003407#if defined(MBEDTLS_MD_C)
3408/* Read some bytes from an HKDF-based generator. This performs a chunk
3409 * of the expand phase of the HKDF algorithm. */
3410static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3411 psa_algorithm_t hash_alg,
3412 uint8_t *output,
3413 size_t output_length )
3414{
3415 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3416 psa_status_t status;
3417
3418 while( output_length != 0 )
3419 {
3420 /* Copy what remains of the current block */
3421 uint8_t n = hash_length - hkdf->offset_in_block;
3422 if( n > output_length )
3423 n = (uint8_t) output_length;
3424 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3425 output += n;
3426 output_length -= n;
3427 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003428 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003429 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003430 /* We can't be wanting more output after block 0xff, otherwise
3431 * the capacity check in psa_generator_read() would have
3432 * prevented this call. It could happen only if the generator
3433 * object was corrupted or if this function is called directly
3434 * inside the library. */
3435 if( hkdf->block_number == 0xff )
3436 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003437
3438 /* We need a new block */
3439 ++hkdf->block_number;
3440 hkdf->offset_in_block = 0;
3441 status = psa_hmac_setup_internal( &hkdf->hmac,
3442 hkdf->prk, hash_length,
3443 hash_alg );
3444 if( status != PSA_SUCCESS )
3445 return( status );
3446 if( hkdf->block_number != 1 )
3447 {
3448 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3449 hkdf->output_block,
3450 hash_length );
3451 if( status != PSA_SUCCESS )
3452 return( status );
3453 }
3454 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3455 hkdf->info,
3456 hkdf->info_length );
3457 if( status != PSA_SUCCESS )
3458 return( status );
3459 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3460 &hkdf->block_number, 1 );
3461 if( status != PSA_SUCCESS )
3462 return( status );
3463 status = psa_hmac_finish_internal( &hkdf->hmac,
3464 hkdf->output_block,
3465 sizeof( hkdf->output_block ) );
3466 if( status != PSA_SUCCESS )
3467 return( status );
3468 }
3469
3470 return( PSA_SUCCESS );
3471}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003472
3473static psa_status_t psa_generator_tls12_prf_generate_next_block(
3474 psa_tls12_prf_generator_t *tls12_prf,
3475 psa_algorithm_t alg )
3476{
3477 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3478 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3479 psa_hmac_internal_data hmac;
3480 psa_status_t status, cleanup_status;
3481
Hanno Becker3b339e22018-11-13 20:56:14 +00003482 unsigned char *Ai;
3483 size_t Ai_len;
3484
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003485 /* We can't be wanting more output after block 0xff, otherwise
3486 * the capacity check in psa_generator_read() would have
3487 * prevented this call. It could happen only if the generator
3488 * object was corrupted or if this function is called directly
3489 * inside the library. */
3490 if( tls12_prf->block_number == 0xff )
3491 return( PSA_ERROR_BAD_STATE );
3492
3493 /* We need a new block */
3494 ++tls12_prf->block_number;
3495 tls12_prf->offset_in_block = 0;
3496
3497 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3498 *
3499 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3500 *
3501 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3502 * HMAC_hash(secret, A(2) + seed) +
3503 * HMAC_hash(secret, A(3) + seed) + ...
3504 *
3505 * A(0) = seed
3506 * A(i) = HMAC_hash( secret, A(i-1) )
3507 *
3508 * The `psa_tls12_prf_generator` structures saves the block
3509 * `HMAC_hash(secret, A(i) + seed)` from which the output
3510 * is currently extracted as `output_block`, while
3511 * `A(i) + seed` is stored in `Ai_with_seed`.
3512 *
3513 * Generating a new block means recalculating `Ai_with_seed`
3514 * from the A(i)-part of it, and afterwards recalculating
3515 * `output_block`.
3516 *
3517 * A(0) is computed at setup time.
3518 *
3519 */
3520
3521 psa_hmac_init_internal( &hmac );
3522
3523 /* We must distinguish the calculation of A(1) from those
3524 * of A(2) and higher, because A(0)=seed has a different
3525 * length than the other A(i). */
3526 if( tls12_prf->block_number == 1 )
3527 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003528 Ai = tls12_prf->Ai_with_seed + hash_length;
3529 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003530 }
3531 else
3532 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003533 Ai = tls12_prf->Ai_with_seed;
3534 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003535 }
3536
Hanno Becker3b339e22018-11-13 20:56:14 +00003537 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3538 status = psa_hmac_setup_internal( &hmac,
3539 tls12_prf->key,
3540 tls12_prf->key_len,
3541 hash_alg );
3542 if( status != PSA_SUCCESS )
3543 goto cleanup;
3544
3545 status = psa_hash_update( &hmac.hash_ctx,
3546 Ai, Ai_len );
3547 if( status != PSA_SUCCESS )
3548 goto cleanup;
3549
3550 status = psa_hmac_finish_internal( &hmac,
3551 tls12_prf->Ai_with_seed,
3552 hash_length );
3553 if( status != PSA_SUCCESS )
3554 goto cleanup;
3555
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003556 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3557 status = psa_hmac_setup_internal( &hmac,
3558 tls12_prf->key,
3559 tls12_prf->key_len,
3560 hash_alg );
3561 if( status != PSA_SUCCESS )
3562 goto cleanup;
3563
3564 status = psa_hash_update( &hmac.hash_ctx,
3565 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003566 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003567 if( status != PSA_SUCCESS )
3568 goto cleanup;
3569
3570 status = psa_hmac_finish_internal( &hmac,
3571 tls12_prf->output_block,
3572 hash_length );
3573 if( status != PSA_SUCCESS )
3574 goto cleanup;
3575
3576cleanup:
3577
3578 cleanup_status = psa_hmac_abort_internal( &hmac );
3579 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3580 status = cleanup_status;
3581
3582 return( status );
3583}
3584
3585/* Read some bytes from an TLS-1.2-PRF-based generator.
3586 * See Section 5 of RFC 5246. */
3587static psa_status_t psa_generator_tls12_prf_read(
3588 psa_tls12_prf_generator_t *tls12_prf,
3589 psa_algorithm_t alg,
3590 uint8_t *output,
3591 size_t output_length )
3592{
3593 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3594 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3595 psa_status_t status;
3596
3597 while( output_length != 0 )
3598 {
3599 /* Copy what remains of the current block */
3600 uint8_t n = hash_length - tls12_prf->offset_in_block;
3601
3602 /* Check if we have fully processed the current block. */
3603 if( n == 0 )
3604 {
3605 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3606 alg );
3607 if( status != PSA_SUCCESS )
3608 return( status );
3609
3610 continue;
3611 }
3612
3613 if( n > output_length )
3614 n = (uint8_t) output_length;
3615 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3616 n );
3617 output += n;
3618 output_length -= n;
3619 tls12_prf->offset_in_block += n;
3620 }
3621
3622 return( PSA_SUCCESS );
3623}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003624#endif /* MBEDTLS_MD_C */
3625
Gilles Peskineeab56e42018-07-12 17:12:33 +02003626psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3627 uint8_t *output,
3628 size_t output_length )
3629{
3630 psa_status_t status;
3631
3632 if( output_length > generator->capacity )
3633 {
3634 generator->capacity = 0;
3635 /* Go through the error path to wipe all confidential data now
3636 * that the generator object is useless. */
3637 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3638 goto exit;
3639 }
3640 if( output_length == 0 &&
3641 generator->capacity == 0 && generator->alg == 0 )
3642 {
3643 /* Edge case: this is a blank or finished generator, and 0
3644 * bytes were requested. The right error in this case could
3645 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3646 * INSUFFICIENT_CAPACITY, which is right for a finished
3647 * generator, for consistency with the case when
3648 * output_length > 0. */
3649 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3650 }
3651 generator->capacity -= output_length;
3652
Gilles Peskine751d9652018-09-18 12:05:44 +02003653 if( generator->alg == PSA_ALG_SELECT_RAW )
3654 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003655 /* Initially, the capacity of a selection generator is always
3656 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3657 * abbreviated in this comment as `size`. When the remaining
3658 * capacity is `c`, the next bytes to serve start `c` bytes
3659 * from the end of the buffer, i.e. `size - c` from the
3660 * beginning of the buffer. Since `generator->capacity` was just
3661 * decremented above, we need to serve the bytes from
3662 * `size - generator->capacity - output_length` to
3663 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003664 size_t offset =
3665 generator->ctx.buffer.size - generator->capacity - output_length;
3666 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3667 status = PSA_SUCCESS;
3668 }
3669 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003670#if defined(MBEDTLS_MD_C)
3671 if( PSA_ALG_IS_HKDF( generator->alg ) )
3672 {
3673 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3674 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3675 output, output_length );
3676 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003677 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3678 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003679 {
3680 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3681 generator->alg, output,
3682 output_length );
3683 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003684 else
3685#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003686 {
3687 return( PSA_ERROR_BAD_STATE );
3688 }
3689
3690exit:
3691 if( status != PSA_SUCCESS )
3692 {
3693 psa_generator_abort( generator );
3694 memset( output, '!', output_length );
3695 }
3696 return( status );
3697}
3698
Gilles Peskine08542d82018-07-19 17:05:42 +02003699#if defined(MBEDTLS_DES_C)
3700static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3701{
3702 if( data_size >= 8 )
3703 mbedtls_des_key_set_parity( data );
3704 if( data_size >= 16 )
3705 mbedtls_des_key_set_parity( data + 8 );
3706 if( data_size >= 24 )
3707 mbedtls_des_key_set_parity( data + 16 );
3708}
3709#endif /* MBEDTLS_DES_C */
3710
Gilles Peskineeab56e42018-07-12 17:12:33 +02003711psa_status_t psa_generator_import_key( psa_key_slot_t key,
3712 psa_key_type_t type,
3713 size_t bits,
3714 psa_crypto_generator_t *generator )
3715{
3716 uint8_t *data = NULL;
3717 size_t bytes = PSA_BITS_TO_BYTES( bits );
3718 psa_status_t status;
3719
3720 if( ! key_type_is_raw_bytes( type ) )
3721 return( PSA_ERROR_INVALID_ARGUMENT );
3722 if( bits % 8 != 0 )
3723 return( PSA_ERROR_INVALID_ARGUMENT );
3724 data = mbedtls_calloc( 1, bytes );
3725 if( data == NULL )
3726 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3727
3728 status = psa_generator_read( generator, data, bytes );
3729 if( status != PSA_SUCCESS )
3730 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003731#if defined(MBEDTLS_DES_C)
3732 if( type == PSA_KEY_TYPE_DES )
3733 psa_des_set_key_parity( data, bytes );
3734#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003735 status = psa_import_key( key, type, data, bytes );
3736
3737exit:
3738 mbedtls_free( data );
3739 return( status );
3740}
3741
3742
3743
3744/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003745/* Key derivation */
3746/****************************************************************/
3747
Gilles Peskinea05219c2018-11-16 16:02:56 +01003748#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003749/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003750 * of the HKDF algorithm.
3751 *
3752 * Note that if this function fails, you must call psa_generator_abort()
3753 * to potentially free embedded data structures and wipe confidential data.
3754 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003755static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003756 const uint8_t *secret,
3757 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003758 psa_algorithm_t hash_alg,
3759 const uint8_t *salt,
3760 size_t salt_length,
3761 const uint8_t *label,
3762 size_t label_length )
3763{
3764 psa_status_t status;
3765 status = psa_hmac_setup_internal( &hkdf->hmac,
3766 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003767 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003768 if( status != PSA_SUCCESS )
3769 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003770 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003771 if( status != PSA_SUCCESS )
3772 return( status );
3773 status = psa_hmac_finish_internal( &hkdf->hmac,
3774 hkdf->prk,
3775 sizeof( hkdf->prk ) );
3776 if( status != PSA_SUCCESS )
3777 return( status );
3778 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3779 hkdf->block_number = 0;
3780 hkdf->info_length = label_length;
3781 if( label_length != 0 )
3782 {
3783 hkdf->info = mbedtls_calloc( 1, label_length );
3784 if( hkdf->info == NULL )
3785 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3786 memcpy( hkdf->info, label, label_length );
3787 }
3788 return( PSA_SUCCESS );
3789}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003790#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003791
Gilles Peskinea05219c2018-11-16 16:02:56 +01003792#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003793/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3794 *
3795 * Note that if this function fails, you must call psa_generator_abort()
3796 * to potentially free embedded data structures and wipe confidential data.
3797 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003798static psa_status_t psa_generator_tls12_prf_setup(
3799 psa_tls12_prf_generator_t *tls12_prf,
3800 const unsigned char *key,
3801 size_t key_len,
3802 psa_algorithm_t hash_alg,
3803 const uint8_t *salt,
3804 size_t salt_length,
3805 const uint8_t *label,
3806 size_t label_length )
3807{
3808 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003809 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3810 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003811
3812 tls12_prf->key = mbedtls_calloc( 1, key_len );
3813 if( tls12_prf->key == NULL )
3814 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3815 tls12_prf->key_len = key_len;
3816 memcpy( tls12_prf->key, key, key_len );
3817
Hanno Becker580fba12018-11-13 20:50:45 +00003818 overflow = ( salt_length + label_length < salt_length ) ||
3819 ( salt_length + label_length + hash_length < hash_length );
3820 if( overflow )
3821 return( PSA_ERROR_INVALID_ARGUMENT );
3822
3823 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3824 if( tls12_prf->Ai_with_seed == NULL )
3825 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3826 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3827
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003828 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3829 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003830 if( label_length != 0 )
3831 {
3832 memcpy( tls12_prf->Ai_with_seed + hash_length,
3833 label, label_length );
3834 }
3835
3836 if( salt_length != 0 )
3837 {
3838 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3839 salt, salt_length );
3840 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003841
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003842 /* The first block gets generated when
3843 * psa_generator_read() is called. */
3844 tls12_prf->block_number = 0;
3845 tls12_prf->offset_in_block = hash_length;
3846
3847 return( PSA_SUCCESS );
3848}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003849
3850/* Set up a TLS-1.2-PSK-to-MS-based generator. */
3851static psa_status_t psa_generator_tls12_psk_to_ms_setup(
3852 psa_tls12_prf_generator_t *tls12_prf,
3853 const unsigned char *psk,
3854 size_t psk_len,
3855 psa_algorithm_t hash_alg,
3856 const uint8_t *salt,
3857 size_t salt_length,
3858 const uint8_t *label,
3859 size_t label_length )
3860{
3861 psa_status_t status;
3862 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
3863
3864 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
3865 return( PSA_ERROR_INVALID_ARGUMENT );
3866
3867 /* Quoting RFC 4279, Section 2:
3868 *
3869 * The premaster secret is formed as follows: if the PSK is N octets
3870 * long, concatenate a uint16 with the value N, N zero octets, a second
3871 * uint16 with the value N, and the PSK itself.
3872 */
3873
3874 pms[0] = ( psk_len >> 8 ) & 0xff;
3875 pms[1] = ( psk_len >> 0 ) & 0xff;
3876 memset( pms + 2, 0, psk_len );
3877 pms[2 + psk_len + 0] = pms[0];
3878 pms[2 + psk_len + 1] = pms[1];
3879 memcpy( pms + 4 + psk_len, psk, psk_len );
3880
3881 status = psa_generator_tls12_prf_setup( tls12_prf,
3882 pms, 4 + 2 * psk_len,
3883 hash_alg,
3884 salt, salt_length,
3885 label, label_length );
3886
3887 mbedtls_zeroize( pms, sizeof( pms ) );
3888 return( status );
3889}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003890#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003891
Gilles Peskine346797d2018-11-16 16:05:06 +01003892/* Note that if this function fails, you must call psa_generator_abort()
3893 * to potentially free embedded data structures and wipe confidential data.
3894 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003895static psa_status_t psa_key_derivation_internal(
3896 psa_crypto_generator_t *generator,
3897 const uint8_t *secret, size_t secret_length,
3898 psa_algorithm_t alg,
3899 const uint8_t *salt, size_t salt_length,
3900 const uint8_t *label, size_t label_length,
3901 size_t capacity )
3902{
3903 psa_status_t status;
3904 size_t max_capacity;
3905
3906 /* Set generator->alg even on failure so that abort knows what to do. */
3907 generator->alg = alg;
3908
Gilles Peskine751d9652018-09-18 12:05:44 +02003909 if( alg == PSA_ALG_SELECT_RAW )
3910 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003911 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02003912 if( salt_length != 0 )
3913 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01003914 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02003915 if( label_length != 0 )
3916 return( PSA_ERROR_INVALID_ARGUMENT );
3917 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
3918 if( generator->ctx.buffer.data == NULL )
3919 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3920 memcpy( generator->ctx.buffer.data, secret, secret_length );
3921 generator->ctx.buffer.size = secret_length;
3922 max_capacity = secret_length;
3923 status = PSA_SUCCESS;
3924 }
3925 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003926#if defined(MBEDTLS_MD_C)
3927 if( PSA_ALG_IS_HKDF( alg ) )
3928 {
3929 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3930 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3931 if( hash_size == 0 )
3932 return( PSA_ERROR_NOT_SUPPORTED );
3933 max_capacity = 255 * hash_size;
3934 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3935 secret, secret_length,
3936 hash_alg,
3937 salt, salt_length,
3938 label, label_length );
3939 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003940 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
3941 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
3942 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003943 {
3944 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3945 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3946
3947 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
3948 if( hash_alg != PSA_ALG_SHA_256 &&
3949 hash_alg != PSA_ALG_SHA_384 )
3950 {
3951 return( PSA_ERROR_NOT_SUPPORTED );
3952 }
3953
3954 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00003955
3956 if( PSA_ALG_IS_TLS12_PRF( alg ) )
3957 {
3958 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
3959 secret, secret_length,
3960 hash_alg, salt, salt_length,
3961 label, label_length );
3962 }
3963 else
3964 {
3965 status = psa_generator_tls12_psk_to_ms_setup(
3966 &generator->ctx.tls12_prf,
3967 secret, secret_length,
3968 hash_alg, salt, salt_length,
3969 label, label_length );
3970 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003971 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003972 else
3973#endif
3974 {
3975 return( PSA_ERROR_NOT_SUPPORTED );
3976 }
3977
3978 if( status != PSA_SUCCESS )
3979 return( status );
3980
3981 if( capacity <= max_capacity )
3982 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02003983 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
3984 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003985 else
3986 return( PSA_ERROR_INVALID_ARGUMENT );
3987
3988 return( PSA_SUCCESS );
3989}
3990
Gilles Peskineea0fb492018-07-12 17:17:20 +02003991psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01003992 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003993 psa_algorithm_t alg,
3994 const uint8_t *salt,
3995 size_t salt_length,
3996 const uint8_t *label,
3997 size_t label_length,
3998 size_t capacity )
3999{
4000 key_slot_t *slot;
4001 psa_status_t status;
4002
4003 if( generator->alg != 0 )
4004 return( PSA_ERROR_BAD_STATE );
4005
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004006 /* Make sure that alg is a key derivation algorithm. This prevents
4007 * key selection algorithms, which psa_key_derivation_internal
4008 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004009 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4010 return( PSA_ERROR_INVALID_ARGUMENT );
4011
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004012 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
4013 if( status != PSA_SUCCESS )
4014 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004015
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004016 if( slot->type != PSA_KEY_TYPE_DERIVE )
4017 return( PSA_ERROR_INVALID_ARGUMENT );
4018
4019 status = psa_key_derivation_internal( generator,
4020 slot->data.raw.data,
4021 slot->data.raw.bytes,
4022 alg,
4023 salt, salt_length,
4024 label, label_length,
4025 capacity );
4026 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004027 psa_generator_abort( generator );
4028 return( status );
4029}
4030
4031
4032
4033/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004034/* Key agreement */
4035/****************************************************************/
4036
Gilles Peskinea05219c2018-11-16 16:02:56 +01004037#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004038static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4039 size_t peer_key_length,
4040 const mbedtls_ecp_keypair *our_key,
4041 uint8_t *shared_secret,
4042 size_t shared_secret_size,
4043 size_t *shared_secret_length )
4044{
4045 mbedtls_pk_context pk;
4046 mbedtls_ecp_keypair *their_key = NULL;
4047 mbedtls_ecdh_context ecdh;
4048 int ret;
4049 mbedtls_ecdh_init( &ecdh );
4050 mbedtls_pk_init( &pk );
4051
4052 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
4053 if( ret != 0 )
4054 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02004055 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004056 {
Gilles Peskine88714d72018-10-25 23:07:25 +02004057 case MBEDTLS_PK_ECKEY:
4058 case MBEDTLS_PK_ECKEY_DH:
4059 break;
4060 default:
4061 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4062 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004063 }
4064 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01004065 if( their_key->grp.id != our_key->grp.id )
4066 {
4067 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4068 goto exit;
4069 }
4070
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004071 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
4072 if( ret != 0 )
4073 goto exit;
4074 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
4075 if( ret != 0 )
4076 goto exit;
4077
4078 ret = mbedtls_ecdh_calc_secret( &ecdh,
4079 shared_secret_length,
4080 shared_secret, shared_secret_size,
4081 mbedtls_ctr_drbg_random,
4082 &global_data.ctr_drbg );
4083
4084exit:
4085 mbedtls_pk_free( &pk );
4086 mbedtls_ecdh_free( &ecdh );
4087 return( mbedtls_to_psa_error( ret ) );
4088}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004089#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004090
Gilles Peskine01d718c2018-09-18 12:01:02 +02004091#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4092
Gilles Peskine346797d2018-11-16 16:05:06 +01004093/* Note that if this function fails, you must call psa_generator_abort()
4094 * to potentially free embedded data structures and wipe confidential data.
4095 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004096static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
4097 key_slot_t *private_key,
4098 const uint8_t *peer_key,
4099 size_t peer_key_length,
4100 psa_algorithm_t alg )
4101{
4102 psa_status_t status;
4103 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4104 size_t shared_secret_length = 0;
4105
4106 /* Step 1: run the secret agreement algorithm to generate the shared
4107 * secret. */
4108 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4109 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004110#if defined(MBEDTLS_ECDH_C)
4111 case PSA_ALG_ECDH_BASE:
4112 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4113 return( PSA_ERROR_INVALID_ARGUMENT );
4114 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4115 private_key->data.ecp,
4116 shared_secret,
4117 sizeof( shared_secret ),
4118 &shared_secret_length );
4119 break;
4120#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004121 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004122 (void) private_key;
4123 (void) peer_key;
4124 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004125 return( PSA_ERROR_NOT_SUPPORTED );
4126 }
4127 if( status != PSA_SUCCESS )
4128 goto exit;
4129
4130 /* Step 2: set up the key derivation to generate key material from
4131 * the shared secret. */
4132 status = psa_key_derivation_internal( generator,
4133 shared_secret, shared_secret_length,
4134 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4135 NULL, 0, NULL, 0,
4136 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4137exit:
4138 mbedtls_zeroize( shared_secret, shared_secret_length );
4139 return( status );
4140}
4141
4142psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
4143 psa_key_slot_t private_key,
4144 const uint8_t *peer_key,
4145 size_t peer_key_length,
4146 psa_algorithm_t alg )
4147{
4148 key_slot_t *slot;
4149 psa_status_t status;
4150 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4151 return( PSA_ERROR_INVALID_ARGUMENT );
4152 status = psa_get_key_from_slot( private_key, &slot,
4153 PSA_KEY_USAGE_DERIVE, alg );
4154 if( status != PSA_SUCCESS )
4155 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004156 status = psa_key_agreement_internal( generator,
4157 slot,
4158 peer_key, peer_key_length,
4159 alg );
4160 if( status != PSA_SUCCESS )
4161 psa_generator_abort( generator );
4162 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004163}
4164
4165
4166
4167/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004168/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004169/****************************************************************/
4170
4171psa_status_t psa_generate_random( uint8_t *output,
4172 size_t output_size )
4173{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004174 int ret;
4175 GUARD_MODULE_INITIALIZED;
4176
4177 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004178 return( mbedtls_to_psa_error( ret ) );
4179}
4180
4181psa_status_t psa_generate_key( psa_key_slot_t key,
4182 psa_key_type_t type,
4183 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004184 const void *extra,
4185 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004186{
Gilles Peskine12313cd2018-06-20 00:20:32 +02004187 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004188 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004189
Gilles Peskine53d991e2018-07-12 01:14:59 +02004190 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004191 return( PSA_ERROR_INVALID_ARGUMENT );
4192
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004193 status = psa_get_empty_key_slot( key, &slot );
4194 if( status != PSA_SUCCESS )
4195 return( status );
4196
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004197 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004198 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004199 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004200 if( status != PSA_SUCCESS )
4201 return( status );
4202 status = psa_generate_random( slot->data.raw.data,
4203 slot->data.raw.bytes );
4204 if( status != PSA_SUCCESS )
4205 {
4206 mbedtls_free( slot->data.raw.data );
4207 return( status );
4208 }
4209#if defined(MBEDTLS_DES_C)
4210 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004211 psa_des_set_key_parity( slot->data.raw.data,
4212 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004213#endif /* MBEDTLS_DES_C */
4214 }
4215 else
4216
4217#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4218 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4219 {
4220 mbedtls_rsa_context *rsa;
4221 int ret;
4222 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004223 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4224 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004225 /* Accept only byte-aligned keys, for the same reasons as
4226 * in psa_import_rsa_key(). */
4227 if( bits % 8 != 0 )
4228 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004229 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004230 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004231 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004232 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004233 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004234#if INT_MAX < 0xffffffff
4235 /* Check that the uint32_t value passed by the caller fits
4236 * in the range supported by this implementation. */
4237 if( p->e > INT_MAX )
4238 return( PSA_ERROR_NOT_SUPPORTED );
4239#endif
4240 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004241 }
4242 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4243 if( rsa == NULL )
4244 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4245 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4246 ret = mbedtls_rsa_gen_key( rsa,
4247 mbedtls_ctr_drbg_random,
4248 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004249 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004250 exponent );
4251 if( ret != 0 )
4252 {
4253 mbedtls_rsa_free( rsa );
4254 mbedtls_free( rsa );
4255 return( mbedtls_to_psa_error( ret ) );
4256 }
4257 slot->data.rsa = rsa;
4258 }
4259 else
4260#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4261
4262#if defined(MBEDTLS_ECP_C)
4263 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4264 {
4265 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4266 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4267 const mbedtls_ecp_curve_info *curve_info =
4268 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4269 mbedtls_ecp_keypair *ecp;
4270 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004271 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004272 return( PSA_ERROR_NOT_SUPPORTED );
4273 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4274 return( PSA_ERROR_NOT_SUPPORTED );
4275 if( curve_info->bit_size != bits )
4276 return( PSA_ERROR_INVALID_ARGUMENT );
4277 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4278 if( ecp == NULL )
4279 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4280 mbedtls_ecp_keypair_init( ecp );
4281 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4282 mbedtls_ctr_drbg_random,
4283 &global_data.ctr_drbg );
4284 if( ret != 0 )
4285 {
4286 mbedtls_ecp_keypair_free( ecp );
4287 mbedtls_free( ecp );
4288 return( mbedtls_to_psa_error( ret ) );
4289 }
4290 slot->data.ecp = ecp;
4291 }
4292 else
4293#endif /* MBEDTLS_ECP_C */
4294
4295 return( PSA_ERROR_NOT_SUPPORTED );
4296
4297 slot->type = type;
4298 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02004299}
4300
4301
4302/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004303/* Module setup */
4304/****************************************************************/
4305
Gilles Peskinee59236f2018-01-27 23:32:46 +01004306void mbedtls_psa_crypto_free( void )
4307{
Jaeden Amero045bd502018-06-26 14:00:08 +01004308 psa_key_slot_t key;
Darryl Green40225ba2018-11-15 14:48:15 +00004309 key_slot_t *slot;
4310 psa_status_t status;
4311
Gilles Peskine9a056342018-08-01 15:46:54 +02004312 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Darryl Green40225ba2018-11-15 14:48:15 +00004313 {
4314 status = psa_get_key_slot( key, &slot );
4315 if( status != PSA_SUCCESS )
4316 continue;
4317 psa_remove_key_data_from_memory( slot );
4318 /* Zeroize the slot to wipe metadata such as policies. */
4319 mbedtls_zeroize( slot, sizeof( *slot ) );
4320 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01004321 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
4322 mbedtls_entropy_free( &global_data.entropy );
4323 mbedtls_zeroize( &global_data, sizeof( global_data ) );
4324}
4325
4326psa_status_t psa_crypto_init( void )
4327{
4328 int ret;
4329 const unsigned char drbg_seed[] = "PSA";
4330
4331 if( global_data.initialized != 0 )
4332 return( PSA_SUCCESS );
4333
4334 mbedtls_zeroize( &global_data, sizeof( global_data ) );
4335 mbedtls_entropy_init( &global_data.entropy );
4336 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
4337
4338 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4339 mbedtls_entropy_func,
4340 &global_data.entropy,
4341 drbg_seed, sizeof( drbg_seed ) - 1 );
4342 if( ret != 0 )
4343 goto exit;
4344
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004345 global_data.initialized = 1;
4346
Gilles Peskinee59236f2018-01-27 23:32:46 +01004347exit:
4348 if( ret != 0 )
4349 mbedtls_psa_crypto_free( );
4350 return( mbedtls_to_psa_error( ret ) );
4351}
4352
4353#endif /* MBEDTLS_PSA_CRYPTO_C */