blob: 05b8a8e8b21aca0907cbf0ef165dd3ad3a00bc21 [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
Darryl Greendd8fb772018-11-07 16:00:44 +0000956static psa_status_t psa_internal_export_key( key_slot_t *slot,
Gilles Peskine2d277862018-06-18 15:41:12 +0200957 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{
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100962 *data_length = 0;
963
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200964 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300965 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300966
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200967 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100968 {
969 if( slot->data.raw.bytes > data_size )
970 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine52b90182018-10-29 19:26:27 +0100971 if( data_size != 0 )
972 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200973 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine52b90182018-10-29 19:26:27 +0100974 memset( data + slot->data.raw.bytes, 0,
975 data_size - slot->data.raw.bytes );
976 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100977 *data_length = slot->data.raw.bytes;
978 return( PSA_SUCCESS );
979 }
Gilles Peskine188c71e2018-10-29 19:26:02 +0100980#if defined(MBEDTLS_ECP_C)
981 if( PSA_KEY_TYPE_IS_ECC_KEYPAIR( slot->type ) && !export_public_key )
982 {
Darryl Greendd8fb772018-11-07 16:00:44 +0000983 psa_status_t status;
984
Gilles Peskine188c71e2018-10-29 19:26:02 +0100985 size_t bytes = PSA_BITS_TO_BYTES( psa_get_key_bits( slot ) );
986 if( bytes > data_size )
987 return( PSA_ERROR_BUFFER_TOO_SMALL );
988 status = mbedtls_to_psa_error(
989 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
990 if( status != PSA_SUCCESS )
991 return( status );
992 memset( data + bytes, 0, data_size - bytes );
993 *data_length = bytes;
994 return( PSA_SUCCESS );
995 }
996#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100997 else
Moran Peker17e36e12018-05-02 12:55:20 +0300998 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100999#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02001000 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +03001001 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001002 {
Moran Pekera998bc62018-04-16 18:16:20 +03001003 mbedtls_pk_context pk;
1004 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +02001005 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001006 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001007#if defined(MBEDTLS_RSA_C)
1008 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001009 pk.pk_info = &mbedtls_rsa_info;
1010 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001011#else
1012 return( PSA_ERROR_NOT_SUPPORTED );
1013#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001014 }
1015 else
1016 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001017#if defined(MBEDTLS_ECP_C)
1018 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001019 pk.pk_info = &mbedtls_eckey_info;
1020 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001021#else
1022 return( PSA_ERROR_NOT_SUPPORTED );
1023#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001024 }
Moran Pekerd7326592018-05-29 16:56:39 +03001025 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001026 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +03001027 else
1028 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +03001029 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001030 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001031 /* If data_size is 0 then data may be NULL and then the
1032 * call to memset would have undefined behavior. */
1033 if( data_size != 0 )
1034 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001035 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001036 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001037 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1038 * Move the data to the beginning and erase remaining data
1039 * at the original location. */
1040 if( 2 * (size_t) ret <= data_size )
1041 {
1042 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001043 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001044 }
1045 else if( (size_t) ret < data_size )
1046 {
1047 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001048 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001049 }
Moran Pekera998bc62018-04-16 18:16:20 +03001050 *data_length = ret;
1051 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001052 }
1053 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001054#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001055 {
1056 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001057 it is valid for a special-purpose implementation to omit
1058 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001059 return( PSA_ERROR_NOT_SUPPORTED );
1060 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001061 }
1062}
1063
Gilles Peskine2d277862018-06-18 15:41:12 +02001064psa_status_t psa_export_key( psa_key_slot_t key,
1065 uint8_t *data,
1066 size_t data_size,
1067 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001068{
Darryl Greendd8fb772018-11-07 16:00:44 +00001069 key_slot_t *slot;
1070 psa_status_t status;
1071
1072 /* Set the key to empty now, so that even when there are errors, we always
1073 * set data_length to a value between 0 and data_size. On error, setting
1074 * the key to empty is a good choice because an empty key representation is
1075 * unlikely to be accepted anywhere. */
1076 *data_length = 0;
1077
1078 /* Export requires the EXPORT flag. There is an exception for public keys,
1079 * which don't require any flag, but psa_get_key_from_slot takes
1080 * care of this. */
1081 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_EXPORT, 0 );
1082 if( status != PSA_SUCCESS )
1083 return( status );
1084 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001085 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001086}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001087
Gilles Peskine2d277862018-06-18 15:41:12 +02001088psa_status_t psa_export_public_key( psa_key_slot_t key,
1089 uint8_t *data,
1090 size_t data_size,
1091 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001092{
Darryl Greendd8fb772018-11-07 16:00:44 +00001093 key_slot_t *slot;
1094 psa_status_t status;
1095
1096 /* Set the key to empty now, so that even when there are errors, we always
1097 * set data_length to a value between 0 and data_size. On error, setting
1098 * the key to empty is a good choice because an empty key representation is
1099 * unlikely to be accepted anywhere. */
1100 *data_length = 0;
1101
1102 /* Exporting a public key doesn't require a usage flag. */
1103 status = psa_get_key_from_slot( key, &slot, 0, 0 );
1104 if( status != PSA_SUCCESS )
1105 return( status );
1106 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001107 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001108}
1109
Darryl Green0c6575a2018-11-07 16:05:30 +00001110#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1111static psa_status_t psa_save_generated_persistent_key( psa_key_slot_t key,
1112 key_slot_t *slot,
1113 size_t bits )
1114{
1115 psa_status_t status;
1116 uint8_t *data;
1117 size_t key_length;
1118 size_t data_size = PSA_KEY_EXPORT_MAX_SIZE( slot->type, bits );
1119 data = mbedtls_calloc( 1, data_size );
itayzafrir910c76b2018-11-21 16:03:21 +02001120 if( data == NULL )
1121 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Darryl Green0c6575a2018-11-07 16:05:30 +00001122 /* Get key data in export format */
1123 status = psa_internal_export_key( slot, data, data_size, &key_length, 0 );
1124 if( status != PSA_SUCCESS )
1125 {
1126 slot->type = PSA_KEY_TYPE_NONE;
1127 goto exit;
1128 }
1129 /* Store in file location */
1130 status = psa_save_persistent_key( key, slot->type, &slot->policy,
1131 data, key_length );
1132 if( status != PSA_SUCCESS )
1133 {
1134 slot->type = PSA_KEY_TYPE_NONE;
1135 }
1136exit:
1137 mbedtls_zeroize( data, key_length );
1138 mbedtls_free( data );
1139 return( status );
1140}
1141#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1142
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001143
1144
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001145/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01001146/* Message digests */
1147/****************************************************************/
1148
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001149static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01001150{
1151 switch( alg )
1152 {
1153#if defined(MBEDTLS_MD2_C)
1154 case PSA_ALG_MD2:
1155 return( &mbedtls_md2_info );
1156#endif
1157#if defined(MBEDTLS_MD4_C)
1158 case PSA_ALG_MD4:
1159 return( &mbedtls_md4_info );
1160#endif
1161#if defined(MBEDTLS_MD5_C)
1162 case PSA_ALG_MD5:
1163 return( &mbedtls_md5_info );
1164#endif
1165#if defined(MBEDTLS_RIPEMD160_C)
1166 case PSA_ALG_RIPEMD160:
1167 return( &mbedtls_ripemd160_info );
1168#endif
1169#if defined(MBEDTLS_SHA1_C)
1170 case PSA_ALG_SHA_1:
1171 return( &mbedtls_sha1_info );
1172#endif
1173#if defined(MBEDTLS_SHA256_C)
1174 case PSA_ALG_SHA_224:
1175 return( &mbedtls_sha224_info );
1176 case PSA_ALG_SHA_256:
1177 return( &mbedtls_sha256_info );
1178#endif
1179#if defined(MBEDTLS_SHA512_C)
1180 case PSA_ALG_SHA_384:
1181 return( &mbedtls_sha384_info );
1182 case PSA_ALG_SHA_512:
1183 return( &mbedtls_sha512_info );
1184#endif
1185 default:
1186 return( NULL );
1187 }
1188}
1189
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001190psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
1191{
1192 switch( operation->alg )
1193 {
Gilles Peskine81736312018-06-26 15:04:31 +02001194 case 0:
1195 /* The object has (apparently) been initialized but it is not
1196 * in use. It's ok to call abort on such an object, and there's
1197 * nothing to do. */
1198 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001199#if defined(MBEDTLS_MD2_C)
1200 case PSA_ALG_MD2:
1201 mbedtls_md2_free( &operation->ctx.md2 );
1202 break;
1203#endif
1204#if defined(MBEDTLS_MD4_C)
1205 case PSA_ALG_MD4:
1206 mbedtls_md4_free( &operation->ctx.md4 );
1207 break;
1208#endif
1209#if defined(MBEDTLS_MD5_C)
1210 case PSA_ALG_MD5:
1211 mbedtls_md5_free( &operation->ctx.md5 );
1212 break;
1213#endif
1214#if defined(MBEDTLS_RIPEMD160_C)
1215 case PSA_ALG_RIPEMD160:
1216 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
1217 break;
1218#endif
1219#if defined(MBEDTLS_SHA1_C)
1220 case PSA_ALG_SHA_1:
1221 mbedtls_sha1_free( &operation->ctx.sha1 );
1222 break;
1223#endif
1224#if defined(MBEDTLS_SHA256_C)
1225 case PSA_ALG_SHA_224:
1226 case PSA_ALG_SHA_256:
1227 mbedtls_sha256_free( &operation->ctx.sha256 );
1228 break;
1229#endif
1230#if defined(MBEDTLS_SHA512_C)
1231 case PSA_ALG_SHA_384:
1232 case PSA_ALG_SHA_512:
1233 mbedtls_sha512_free( &operation->ctx.sha512 );
1234 break;
1235#endif
1236 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001237 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001238 }
1239 operation->alg = 0;
1240 return( PSA_SUCCESS );
1241}
1242
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001243psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001244 psa_algorithm_t alg )
1245{
1246 int ret;
1247 operation->alg = 0;
1248 switch( alg )
1249 {
1250#if defined(MBEDTLS_MD2_C)
1251 case PSA_ALG_MD2:
1252 mbedtls_md2_init( &operation->ctx.md2 );
1253 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
1254 break;
1255#endif
1256#if defined(MBEDTLS_MD4_C)
1257 case PSA_ALG_MD4:
1258 mbedtls_md4_init( &operation->ctx.md4 );
1259 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
1260 break;
1261#endif
1262#if defined(MBEDTLS_MD5_C)
1263 case PSA_ALG_MD5:
1264 mbedtls_md5_init( &operation->ctx.md5 );
1265 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
1266 break;
1267#endif
1268#if defined(MBEDTLS_RIPEMD160_C)
1269 case PSA_ALG_RIPEMD160:
1270 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1271 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1272 break;
1273#endif
1274#if defined(MBEDTLS_SHA1_C)
1275 case PSA_ALG_SHA_1:
1276 mbedtls_sha1_init( &operation->ctx.sha1 );
1277 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1278 break;
1279#endif
1280#if defined(MBEDTLS_SHA256_C)
1281 case PSA_ALG_SHA_224:
1282 mbedtls_sha256_init( &operation->ctx.sha256 );
1283 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1284 break;
1285 case PSA_ALG_SHA_256:
1286 mbedtls_sha256_init( &operation->ctx.sha256 );
1287 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1288 break;
1289#endif
1290#if defined(MBEDTLS_SHA512_C)
1291 case PSA_ALG_SHA_384:
1292 mbedtls_sha512_init( &operation->ctx.sha512 );
1293 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1294 break;
1295 case PSA_ALG_SHA_512:
1296 mbedtls_sha512_init( &operation->ctx.sha512 );
1297 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1298 break;
1299#endif
1300 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001301 return( PSA_ALG_IS_HASH( alg ) ?
1302 PSA_ERROR_NOT_SUPPORTED :
1303 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001304 }
1305 if( ret == 0 )
1306 operation->alg = alg;
1307 else
1308 psa_hash_abort( operation );
1309 return( mbedtls_to_psa_error( ret ) );
1310}
1311
1312psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1313 const uint8_t *input,
1314 size_t input_length )
1315{
1316 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001317
1318 /* Don't require hash implementations to behave correctly on a
1319 * zero-length input, which may have an invalid pointer. */
1320 if( input_length == 0 )
1321 return( PSA_SUCCESS );
1322
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001323 switch( operation->alg )
1324 {
1325#if defined(MBEDTLS_MD2_C)
1326 case PSA_ALG_MD2:
1327 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1328 input, input_length );
1329 break;
1330#endif
1331#if defined(MBEDTLS_MD4_C)
1332 case PSA_ALG_MD4:
1333 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1334 input, input_length );
1335 break;
1336#endif
1337#if defined(MBEDTLS_MD5_C)
1338 case PSA_ALG_MD5:
1339 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1340 input, input_length );
1341 break;
1342#endif
1343#if defined(MBEDTLS_RIPEMD160_C)
1344 case PSA_ALG_RIPEMD160:
1345 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1346 input, input_length );
1347 break;
1348#endif
1349#if defined(MBEDTLS_SHA1_C)
1350 case PSA_ALG_SHA_1:
1351 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1352 input, input_length );
1353 break;
1354#endif
1355#if defined(MBEDTLS_SHA256_C)
1356 case PSA_ALG_SHA_224:
1357 case PSA_ALG_SHA_256:
1358 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1359 input, input_length );
1360 break;
1361#endif
1362#if defined(MBEDTLS_SHA512_C)
1363 case PSA_ALG_SHA_384:
1364 case PSA_ALG_SHA_512:
1365 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1366 input, input_length );
1367 break;
1368#endif
1369 default:
1370 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1371 break;
1372 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001373
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001374 if( ret != 0 )
1375 psa_hash_abort( operation );
1376 return( mbedtls_to_psa_error( ret ) );
1377}
1378
1379psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1380 uint8_t *hash,
1381 size_t hash_size,
1382 size_t *hash_length )
1383{
itayzafrir40835d42018-08-02 13:14:17 +03001384 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001385 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001386 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001387
1388 /* Fill the output buffer with something that isn't a valid hash
1389 * (barring an attack on the hash and deliberately-crafted input),
1390 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001391 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001392 /* If hash_size is 0 then hash may be NULL and then the
1393 * call to memset would have undefined behavior. */
1394 if( hash_size != 0 )
1395 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001396
1397 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001398 {
1399 status = PSA_ERROR_BUFFER_TOO_SMALL;
1400 goto exit;
1401 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001402
1403 switch( operation->alg )
1404 {
1405#if defined(MBEDTLS_MD2_C)
1406 case PSA_ALG_MD2:
1407 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1408 break;
1409#endif
1410#if defined(MBEDTLS_MD4_C)
1411 case PSA_ALG_MD4:
1412 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1413 break;
1414#endif
1415#if defined(MBEDTLS_MD5_C)
1416 case PSA_ALG_MD5:
1417 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1418 break;
1419#endif
1420#if defined(MBEDTLS_RIPEMD160_C)
1421 case PSA_ALG_RIPEMD160:
1422 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1423 break;
1424#endif
1425#if defined(MBEDTLS_SHA1_C)
1426 case PSA_ALG_SHA_1:
1427 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1428 break;
1429#endif
1430#if defined(MBEDTLS_SHA256_C)
1431 case PSA_ALG_SHA_224:
1432 case PSA_ALG_SHA_256:
1433 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1434 break;
1435#endif
1436#if defined(MBEDTLS_SHA512_C)
1437 case PSA_ALG_SHA_384:
1438 case PSA_ALG_SHA_512:
1439 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1440 break;
1441#endif
1442 default:
1443 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1444 break;
1445 }
itayzafrir40835d42018-08-02 13:14:17 +03001446 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001447
itayzafrir40835d42018-08-02 13:14:17 +03001448exit:
1449 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001450 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001451 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001452 return( psa_hash_abort( operation ) );
1453 }
1454 else
1455 {
1456 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001457 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001458 }
1459}
1460
Gilles Peskine2d277862018-06-18 15:41:12 +02001461psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1462 const uint8_t *hash,
1463 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001464{
1465 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1466 size_t actual_hash_length;
1467 psa_status_t status = psa_hash_finish( operation,
1468 actual_hash, sizeof( actual_hash ),
1469 &actual_hash_length );
1470 if( status != PSA_SUCCESS )
1471 return( status );
1472 if( actual_hash_length != hash_length )
1473 return( PSA_ERROR_INVALID_SIGNATURE );
1474 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1475 return( PSA_ERROR_INVALID_SIGNATURE );
1476 return( PSA_SUCCESS );
1477}
1478
1479
1480
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001481/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001482/* MAC */
1483/****************************************************************/
1484
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001485static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001486 psa_algorithm_t alg,
1487 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001488 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001489 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001490{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001491 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001492 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001493
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001494 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001495 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001496
Gilles Peskine8c9def32018-02-08 10:02:12 +01001497 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1498 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001499 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001500 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001501 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001502 mode = MBEDTLS_MODE_STREAM;
1503 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001504 case PSA_ALG_CTR:
1505 mode = MBEDTLS_MODE_CTR;
1506 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001507 case PSA_ALG_CFB:
1508 mode = MBEDTLS_MODE_CFB;
1509 break;
1510 case PSA_ALG_OFB:
1511 mode = MBEDTLS_MODE_OFB;
1512 break;
1513 case PSA_ALG_CBC_NO_PADDING:
1514 mode = MBEDTLS_MODE_CBC;
1515 break;
1516 case PSA_ALG_CBC_PKCS7:
1517 mode = MBEDTLS_MODE_CBC;
1518 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001519 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001520 mode = MBEDTLS_MODE_CCM;
1521 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001522 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001523 mode = MBEDTLS_MODE_GCM;
1524 break;
1525 default:
1526 return( NULL );
1527 }
1528 }
1529 else if( alg == PSA_ALG_CMAC )
1530 mode = MBEDTLS_MODE_ECB;
1531 else if( alg == PSA_ALG_GMAC )
1532 mode = MBEDTLS_MODE_GCM;
1533 else
1534 return( NULL );
1535
1536 switch( key_type )
1537 {
1538 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001539 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001540 break;
1541 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001542 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1543 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001544 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001545 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001546 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001547 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001548 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1549 * but two-key Triple-DES is functionally three-key Triple-DES
1550 * with K1=K3, so that's how we present it to mbedtls. */
1551 if( key_bits == 128 )
1552 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001553 break;
1554 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001555 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001556 break;
1557 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001558 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001559 break;
1560 default:
1561 return( NULL );
1562 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001563 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001564 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001565
Jaeden Amero23bbb752018-06-26 14:16:54 +01001566 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1567 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001568}
1569
Gilles Peskinea05219c2018-11-16 16:02:56 +01001570#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001571static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001572{
Gilles Peskine2d277862018-06-18 15:41:12 +02001573 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001574 {
1575 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001576 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001577 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001578 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001579 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001580 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001581 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001582 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001583 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001584 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001585 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001586 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001587 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001588 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001589 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001590 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001591 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001592 return( 128 );
1593 default:
1594 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001595 }
1596}
Gilles Peskinea05219c2018-11-16 16:02:56 +01001597#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001598
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001599/* Initialize the MAC operation structure. Once this function has been
1600 * called, psa_mac_abort can run and will do the right thing. */
1601static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1602 psa_algorithm_t alg )
1603{
1604 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1605
1606 operation->alg = alg;
1607 operation->key_set = 0;
1608 operation->iv_set = 0;
1609 operation->iv_required = 0;
1610 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001611 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001612
1613#if defined(MBEDTLS_CMAC_C)
1614 if( alg == PSA_ALG_CMAC )
1615 {
1616 operation->iv_required = 0;
1617 mbedtls_cipher_init( &operation->ctx.cmac );
1618 status = PSA_SUCCESS;
1619 }
1620 else
1621#endif /* MBEDTLS_CMAC_C */
1622#if defined(MBEDTLS_MD_C)
1623 if( PSA_ALG_IS_HMAC( operation->alg ) )
1624 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001625 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1626 operation->ctx.hmac.hash_ctx.alg = 0;
1627 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001628 }
1629 else
1630#endif /* MBEDTLS_MD_C */
1631 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001632 if( ! PSA_ALG_IS_MAC( alg ) )
1633 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001634 }
1635
1636 if( status != PSA_SUCCESS )
1637 memset( operation, 0, sizeof( *operation ) );
1638 return( status );
1639}
1640
Gilles Peskine01126fa2018-07-12 17:04:55 +02001641#if defined(MBEDTLS_MD_C)
1642static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1643{
1644 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1645 return( psa_hash_abort( &hmac->hash_ctx ) );
1646}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01001647
1648static void psa_hmac_init_internal( psa_hmac_internal_data *hmac )
1649{
1650 /* Instances of psa_hash_operation_s can be initialized by zeroization. */
1651 memset( hmac, 0, sizeof( *hmac ) );
1652}
Gilles Peskine01126fa2018-07-12 17:04:55 +02001653#endif /* MBEDTLS_MD_C */
1654
Gilles Peskine8c9def32018-02-08 10:02:12 +01001655psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1656{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001657 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001658 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001659 /* The object has (apparently) been initialized but it is not
1660 * in use. It's ok to call abort on such an object, and there's
1661 * nothing to do. */
1662 return( PSA_SUCCESS );
1663 }
1664 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001665#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001666 if( operation->alg == PSA_ALG_CMAC )
1667 {
1668 mbedtls_cipher_free( &operation->ctx.cmac );
1669 }
1670 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001671#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001672#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001673 if( PSA_ALG_IS_HMAC( operation->alg ) )
1674 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001675 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001676 }
1677 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001678#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001679 {
1680 /* Sanity check (shouldn't happen: operation->alg should
1681 * always have been initialized to a valid value). */
1682 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001683 }
Moran Peker41deec42018-04-04 15:43:05 +03001684
Gilles Peskine8c9def32018-02-08 10:02:12 +01001685 operation->alg = 0;
1686 operation->key_set = 0;
1687 operation->iv_set = 0;
1688 operation->iv_required = 0;
1689 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001690 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001691
Gilles Peskine8c9def32018-02-08 10:02:12 +01001692 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001693
1694bad_state:
1695 /* If abort is called on an uninitialized object, we can't trust
1696 * anything. Wipe the object in case it contains confidential data.
1697 * This may result in a memory leak if a pointer gets overwritten,
1698 * but it's too late to do anything about this. */
1699 memset( operation, 0, sizeof( *operation ) );
1700 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001701}
1702
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001703#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001704static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001705 size_t key_bits,
1706 key_slot_t *slot,
1707 const mbedtls_cipher_info_t *cipher_info )
1708{
1709 int ret;
1710
1711 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001712
1713 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1714 if( ret != 0 )
1715 return( ret );
1716
1717 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1718 slot->data.raw.data,
1719 key_bits );
1720 return( ret );
1721}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001722#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001723
Gilles Peskine248051a2018-06-20 16:09:38 +02001724#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001725static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1726 const uint8_t *key,
1727 size_t key_length,
1728 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001729{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001730 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001731 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001732 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001733 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001734 psa_status_t status;
1735
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001736 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1737 * overflow below. This should never trigger if the hash algorithm
1738 * is implemented correctly. */
1739 /* The size checks against the ipad and opad buffers cannot be written
1740 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1741 * because that triggers -Wlogical-op on GCC 7.3. */
1742 if( block_size > sizeof( ipad ) )
1743 return( PSA_ERROR_NOT_SUPPORTED );
1744 if( block_size > sizeof( hmac->opad ) )
1745 return( PSA_ERROR_NOT_SUPPORTED );
1746 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001747 return( PSA_ERROR_NOT_SUPPORTED );
1748
Gilles Peskined223b522018-06-11 18:12:58 +02001749 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001750 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001751 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001752 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001753 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001754 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001755 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001756 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001757 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001758 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001759 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001760 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001761 }
Gilles Peskine96889972018-07-12 17:07:03 +02001762 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1763 * but it is permitted. It is common when HMAC is used in HKDF, for
1764 * example. Don't call `memcpy` in the 0-length because `key` could be
1765 * an invalid pointer which would make the behavior undefined. */
1766 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001767 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001768
Gilles Peskined223b522018-06-11 18:12:58 +02001769 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1770 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001771 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001772 ipad[i] ^= 0x36;
1773 memset( ipad + key_length, 0x36, block_size - key_length );
1774
1775 /* Copy the key material from ipad to opad, flipping the requisite bits,
1776 * and filling the rest of opad with the requisite constant. */
1777 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001778 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1779 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001780
Gilles Peskine01126fa2018-07-12 17:04:55 +02001781 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001782 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001783 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001784
Gilles Peskine01126fa2018-07-12 17:04:55 +02001785 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001786
1787cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001788 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001789
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001790 return( status );
1791}
Gilles Peskine248051a2018-06-20 16:09:38 +02001792#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001793
Gilles Peskine89167cb2018-07-08 20:12:23 +02001794static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1795 psa_key_slot_t key,
1796 psa_algorithm_t alg,
1797 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001798{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001799 psa_status_t status;
1800 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001801 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001802 psa_key_usage_t usage =
1803 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001804 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02001805 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001806
Gilles Peskined911eb72018-08-14 15:18:45 +02001807 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001808 if( status != PSA_SUCCESS )
1809 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001810 if( is_sign )
1811 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001812
Gilles Peskine89167cb2018-07-08 20:12:23 +02001813 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001814 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001815 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001816 key_bits = psa_get_key_bits( slot );
1817
Gilles Peskine8c9def32018-02-08 10:02:12 +01001818#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001819 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001820 {
1821 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001822 mbedtls_cipher_info_from_psa( full_length_alg,
1823 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001824 int ret;
1825 if( cipher_info == NULL )
1826 {
1827 status = PSA_ERROR_NOT_SUPPORTED;
1828 goto exit;
1829 }
1830 operation->mac_size = cipher_info->block_size;
1831 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1832 status = mbedtls_to_psa_error( ret );
1833 }
1834 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001835#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001836#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001837 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001838 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001839 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001840 if( hash_alg == 0 )
1841 {
1842 status = PSA_ERROR_NOT_SUPPORTED;
1843 goto exit;
1844 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001845
1846 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1847 /* Sanity check. This shouldn't fail on a valid configuration. */
1848 if( operation->mac_size == 0 ||
1849 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1850 {
1851 status = PSA_ERROR_NOT_SUPPORTED;
1852 goto exit;
1853 }
1854
Gilles Peskine01126fa2018-07-12 17:04:55 +02001855 if( slot->type != PSA_KEY_TYPE_HMAC )
1856 {
1857 status = PSA_ERROR_INVALID_ARGUMENT;
1858 goto exit;
1859 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001860
Gilles Peskine01126fa2018-07-12 17:04:55 +02001861 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1862 slot->data.raw.data,
1863 slot->data.raw.bytes,
1864 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001865 }
1866 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001867#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001868 {
1869 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001870 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001871
Gilles Peskined911eb72018-08-14 15:18:45 +02001872 if( truncated == 0 )
1873 {
1874 /* The "normal" case: untruncated algorithm. Nothing to do. */
1875 }
1876 else if( truncated < 4 )
1877 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001878 /* A very short MAC is too short for security since it can be
1879 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1880 * so we make this our minimum, even though 32 bits is still
1881 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001882 status = PSA_ERROR_NOT_SUPPORTED;
1883 }
1884 else if( truncated > operation->mac_size )
1885 {
1886 /* It's impossible to "truncate" to a larger length. */
1887 status = PSA_ERROR_INVALID_ARGUMENT;
1888 }
1889 else
1890 operation->mac_size = truncated;
1891
Gilles Peskinefbfac682018-07-08 20:51:54 +02001892exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001893 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001894 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001895 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001896 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001897 else
1898 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001899 operation->key_set = 1;
1900 }
1901 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001902}
1903
Gilles Peskine89167cb2018-07-08 20:12:23 +02001904psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1905 psa_key_slot_t key,
1906 psa_algorithm_t alg )
1907{
1908 return( psa_mac_setup( operation, key, alg, 1 ) );
1909}
1910
1911psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1912 psa_key_slot_t key,
1913 psa_algorithm_t alg )
1914{
1915 return( psa_mac_setup( operation, key, alg, 0 ) );
1916}
1917
Gilles Peskine8c9def32018-02-08 10:02:12 +01001918psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1919 const uint8_t *input,
1920 size_t input_length )
1921{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001922 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001923 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001924 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001925 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001926 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001927 operation->has_input = 1;
1928
Gilles Peskine8c9def32018-02-08 10:02:12 +01001929#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001930 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001931 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001932 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1933 input, input_length );
1934 status = mbedtls_to_psa_error( ret );
1935 }
1936 else
1937#endif /* MBEDTLS_CMAC_C */
1938#if defined(MBEDTLS_MD_C)
1939 if( PSA_ALG_IS_HMAC( operation->alg ) )
1940 {
1941 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1942 input_length );
1943 }
1944 else
1945#endif /* MBEDTLS_MD_C */
1946 {
1947 /* This shouldn't happen if `operation` was initialized by
1948 * a setup function. */
1949 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001950 }
1951
Gilles Peskinefbfac682018-07-08 20:51:54 +02001952cleanup:
1953 if( status != PSA_SUCCESS )
1954 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001955 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001956}
1957
Gilles Peskine01126fa2018-07-12 17:04:55 +02001958#if defined(MBEDTLS_MD_C)
1959static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1960 uint8_t *mac,
1961 size_t mac_size )
1962{
1963 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1964 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1965 size_t hash_size = 0;
1966 size_t block_size = psa_get_hash_block_size( hash_alg );
1967 psa_status_t status;
1968
Gilles Peskine01126fa2018-07-12 17:04:55 +02001969 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1970 if( status != PSA_SUCCESS )
1971 return( status );
1972 /* From here on, tmp needs to be wiped. */
1973
1974 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1975 if( status != PSA_SUCCESS )
1976 goto exit;
1977
1978 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1979 if( status != PSA_SUCCESS )
1980 goto exit;
1981
1982 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1983 if( status != PSA_SUCCESS )
1984 goto exit;
1985
Gilles Peskined911eb72018-08-14 15:18:45 +02001986 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1987 if( status != PSA_SUCCESS )
1988 goto exit;
1989
1990 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001991
1992exit:
1993 mbedtls_zeroize( tmp, hash_size );
1994 return( status );
1995}
1996#endif /* MBEDTLS_MD_C */
1997
mohammad16036df908f2018-04-02 08:34:15 -07001998static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001999 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002000 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002001{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002002 if( ! operation->key_set )
2003 return( PSA_ERROR_BAD_STATE );
2004 if( operation->iv_required && ! operation->iv_set )
2005 return( PSA_ERROR_BAD_STATE );
2006
Gilles Peskine8c9def32018-02-08 10:02:12 +01002007 if( mac_size < operation->mac_size )
2008 return( PSA_ERROR_BUFFER_TOO_SMALL );
2009
Gilles Peskine8c9def32018-02-08 10:02:12 +01002010#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002011 if( operation->alg == PSA_ALG_CMAC )
2012 {
Gilles Peskined911eb72018-08-14 15:18:45 +02002013 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
2014 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2015 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002016 memcpy( mac, tmp, operation->mac_size );
Gilles Peskined911eb72018-08-14 15:18:45 +02002017 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002018 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002019 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002020 else
2021#endif /* MBEDTLS_CMAC_C */
2022#if defined(MBEDTLS_MD_C)
2023 if( PSA_ALG_IS_HMAC( operation->alg ) )
2024 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002025 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002026 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002027 }
2028 else
2029#endif /* MBEDTLS_MD_C */
2030 {
2031 /* This shouldn't happen if `operation` was initialized by
2032 * a setup function. */
2033 return( PSA_ERROR_BAD_STATE );
2034 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002035}
2036
Gilles Peskineacd4be32018-07-08 19:56:25 +02002037psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2038 uint8_t *mac,
2039 size_t mac_size,
2040 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002041{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002042 psa_status_t status;
2043
2044 /* Fill the output buffer with something that isn't a valid mac
2045 * (barring an attack on the mac and deliberately-crafted input),
2046 * in case the caller doesn't check the return status properly. */
2047 *mac_length = mac_size;
2048 /* If mac_size is 0 then mac may be NULL and then the
2049 * call to memset would have undefined behavior. */
2050 if( mac_size != 0 )
2051 memset( mac, '!', mac_size );
2052
Gilles Peskine89167cb2018-07-08 20:12:23 +02002053 if( ! operation->is_sign )
2054 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002055 status = PSA_ERROR_BAD_STATE;
2056 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002057 }
mohammad16036df908f2018-04-02 08:34:15 -07002058
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002059 status = psa_mac_finish_internal( operation, mac, mac_size );
2060
2061cleanup:
2062 if( status == PSA_SUCCESS )
2063 {
2064 status = psa_mac_abort( operation );
2065 if( status == PSA_SUCCESS )
2066 *mac_length = operation->mac_size;
2067 else
2068 memset( mac, '!', mac_size );
2069 }
2070 else
2071 psa_mac_abort( operation );
2072 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002073}
2074
Gilles Peskineacd4be32018-07-08 19:56:25 +02002075psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2076 const uint8_t *mac,
2077 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002078{
Gilles Peskine828ed142018-06-18 23:25:51 +02002079 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002080 psa_status_t status;
2081
Gilles Peskine89167cb2018-07-08 20:12:23 +02002082 if( operation->is_sign )
2083 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002084 status = PSA_ERROR_BAD_STATE;
2085 goto cleanup;
2086 }
2087 if( operation->mac_size != mac_length )
2088 {
2089 status = PSA_ERROR_INVALID_SIGNATURE;
2090 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002091 }
mohammad16036df908f2018-04-02 08:34:15 -07002092
2093 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002094 actual_mac, sizeof( actual_mac ) );
2095
2096 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2097 status = PSA_ERROR_INVALID_SIGNATURE;
2098
2099cleanup:
2100 if( status == PSA_SUCCESS )
2101 status = psa_mac_abort( operation );
2102 else
2103 psa_mac_abort( operation );
2104
Gilles Peskine99b7d6b2018-08-21 14:56:19 +02002105 mbedtls_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002106
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002107 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002108}
2109
2110
Gilles Peskine20035e32018-02-03 22:44:14 +01002111
Gilles Peskine20035e32018-02-03 22:44:14 +01002112/****************************************************************/
2113/* Asymmetric cryptography */
2114/****************************************************************/
2115
Gilles Peskine2b450e32018-06-27 15:42:46 +02002116#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002117/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002118 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02002119static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
2120 size_t hash_length,
2121 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002122{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02002123 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002124 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002125 *md_alg = mbedtls_md_get_type( md_info );
2126
2127 /* The Mbed TLS RSA module uses an unsigned int for hash length
2128 * parameters. Validate that it fits so that we don't risk an
2129 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002130#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002131 if( hash_length > UINT_MAX )
2132 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002133#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002134
2135#if defined(MBEDTLS_PKCS1_V15)
2136 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
2137 * must be correct. */
2138 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
2139 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002140 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002141 if( md_info == NULL )
2142 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002143 if( mbedtls_md_get_size( md_info ) != hash_length )
2144 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002145 }
2146#endif /* MBEDTLS_PKCS1_V15 */
2147
2148#if defined(MBEDTLS_PKCS1_V21)
2149 /* PSS requires a hash internally. */
2150 if( PSA_ALG_IS_RSA_PSS( alg ) )
2151 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002152 if( md_info == NULL )
2153 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002154 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002155#endif /* MBEDTLS_PKCS1_V21 */
2156
Gilles Peskine61b91d42018-06-08 16:09:36 +02002157 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03002158}
2159
Gilles Peskine2b450e32018-06-27 15:42:46 +02002160static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
2161 psa_algorithm_t alg,
2162 const uint8_t *hash,
2163 size_t hash_length,
2164 uint8_t *signature,
2165 size_t signature_size,
2166 size_t *signature_length )
2167{
2168 psa_status_t status;
2169 int ret;
2170 mbedtls_md_type_t md_alg;
2171
2172 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2173 if( status != PSA_SUCCESS )
2174 return( status );
2175
Gilles Peskine630a18a2018-06-29 17:49:35 +02002176 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002177 return( PSA_ERROR_BUFFER_TOO_SMALL );
2178
2179#if defined(MBEDTLS_PKCS1_V15)
2180 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2181 {
2182 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2183 MBEDTLS_MD_NONE );
2184 ret = mbedtls_rsa_pkcs1_sign( rsa,
2185 mbedtls_ctr_drbg_random,
2186 &global_data.ctr_drbg,
2187 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002188 md_alg,
2189 (unsigned int) hash_length,
2190 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002191 signature );
2192 }
2193 else
2194#endif /* MBEDTLS_PKCS1_V15 */
2195#if defined(MBEDTLS_PKCS1_V21)
2196 if( PSA_ALG_IS_RSA_PSS( alg ) )
2197 {
2198 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2199 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
2200 mbedtls_ctr_drbg_random,
2201 &global_data.ctr_drbg,
2202 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002203 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002204 (unsigned int) hash_length,
2205 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002206 signature );
2207 }
2208 else
2209#endif /* MBEDTLS_PKCS1_V21 */
2210 {
2211 return( PSA_ERROR_INVALID_ARGUMENT );
2212 }
2213
2214 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002215 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002216 return( mbedtls_to_psa_error( ret ) );
2217}
2218
2219static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
2220 psa_algorithm_t alg,
2221 const uint8_t *hash,
2222 size_t hash_length,
2223 const uint8_t *signature,
2224 size_t signature_length )
2225{
2226 psa_status_t status;
2227 int ret;
2228 mbedtls_md_type_t md_alg;
2229
2230 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
2231 if( status != PSA_SUCCESS )
2232 return( status );
2233
Gilles Peskine630a18a2018-06-29 17:49:35 +02002234 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02002235 return( PSA_ERROR_BUFFER_TOO_SMALL );
2236
2237#if defined(MBEDTLS_PKCS1_V15)
2238 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
2239 {
2240 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
2241 MBEDTLS_MD_NONE );
2242 ret = mbedtls_rsa_pkcs1_verify( rsa,
2243 mbedtls_ctr_drbg_random,
2244 &global_data.ctr_drbg,
2245 MBEDTLS_RSA_PUBLIC,
2246 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002247 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002248 hash,
2249 signature );
2250 }
2251 else
2252#endif /* MBEDTLS_PKCS1_V15 */
2253#if defined(MBEDTLS_PKCS1_V21)
2254 if( PSA_ALG_IS_RSA_PSS( alg ) )
2255 {
2256 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2257 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
2258 mbedtls_ctr_drbg_random,
2259 &global_data.ctr_drbg,
2260 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02002261 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01002262 (unsigned int) hash_length,
2263 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02002264 signature );
2265 }
2266 else
2267#endif /* MBEDTLS_PKCS1_V21 */
2268 {
2269 return( PSA_ERROR_INVALID_ARGUMENT );
2270 }
Gilles Peskineef12c632018-09-13 20:37:48 +02002271
2272 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
2273 * the rest of the signature is invalid". This has little use in
2274 * practice and PSA doesn't report this distinction. */
2275 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2276 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002277 return( mbedtls_to_psa_error( ret ) );
2278}
2279#endif /* MBEDTLS_RSA_C */
2280
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002281#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002282/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2283 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2284 * (even though these functions don't modify it). */
2285static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2286 psa_algorithm_t alg,
2287 const uint8_t *hash,
2288 size_t hash_length,
2289 uint8_t *signature,
2290 size_t signature_size,
2291 size_t *signature_length )
2292{
2293 int ret;
2294 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002295 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002296 mbedtls_mpi_init( &r );
2297 mbedtls_mpi_init( &s );
2298
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002299 if( signature_size < 2 * curve_bytes )
2300 {
2301 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2302 goto cleanup;
2303 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002304
Gilles Peskinea05219c2018-11-16 16:02:56 +01002305#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002306 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2307 {
2308 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2309 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2310 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2311 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2312 hash, hash_length,
2313 md_alg ) );
2314 }
2315 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01002316#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002317 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01002318 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002319 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2320 hash, hash_length,
2321 mbedtls_ctr_drbg_random,
2322 &global_data.ctr_drbg ) );
2323 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002324
2325 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2326 signature,
2327 curve_bytes ) );
2328 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2329 signature + curve_bytes,
2330 curve_bytes ) );
2331
2332cleanup:
2333 mbedtls_mpi_free( &r );
2334 mbedtls_mpi_free( &s );
2335 if( ret == 0 )
2336 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002337 return( mbedtls_to_psa_error( ret ) );
2338}
2339
2340static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2341 const uint8_t *hash,
2342 size_t hash_length,
2343 const uint8_t *signature,
2344 size_t signature_length )
2345{
2346 int ret;
2347 mbedtls_mpi r, s;
2348 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2349 mbedtls_mpi_init( &r );
2350 mbedtls_mpi_init( &s );
2351
2352 if( signature_length != 2 * curve_bytes )
2353 return( PSA_ERROR_INVALID_SIGNATURE );
2354
2355 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2356 signature,
2357 curve_bytes ) );
2358 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2359 signature + curve_bytes,
2360 curve_bytes ) );
2361
2362 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2363 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002364
2365cleanup:
2366 mbedtls_mpi_free( &r );
2367 mbedtls_mpi_free( &s );
2368 return( mbedtls_to_psa_error( ret ) );
2369}
2370#endif /* MBEDTLS_ECDSA_C */
2371
Gilles Peskine61b91d42018-06-08 16:09:36 +02002372psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2373 psa_algorithm_t alg,
2374 const uint8_t *hash,
2375 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002376 uint8_t *signature,
2377 size_t signature_size,
2378 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002379{
2380 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002381 psa_status_t status;
2382
2383 *signature_length = signature_size;
2384
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002385 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2386 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002387 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002388 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002389 {
2390 status = PSA_ERROR_INVALID_ARGUMENT;
2391 goto exit;
2392 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002393
Gilles Peskine20035e32018-02-03 22:44:14 +01002394#if defined(MBEDTLS_RSA_C)
2395 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2396 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002397 status = psa_rsa_sign( slot->data.rsa,
2398 alg,
2399 hash, hash_length,
2400 signature, signature_size,
2401 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002402 }
2403 else
2404#endif /* defined(MBEDTLS_RSA_C) */
2405#if defined(MBEDTLS_ECP_C)
2406 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2407 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002408#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01002409 if(
2410#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
2411 PSA_ALG_IS_ECDSA( alg )
2412#else
2413 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
2414#endif
2415 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002416 status = psa_ecdsa_sign( slot->data.ecp,
2417 alg,
2418 hash, hash_length,
2419 signature, signature_size,
2420 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002421 else
2422#endif /* defined(MBEDTLS_ECDSA_C) */
2423 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002424 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002425 }
itayzafrir5c753392018-05-08 11:18:38 +03002426 }
2427 else
2428#endif /* defined(MBEDTLS_ECP_C) */
2429 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002430 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002431 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002432
2433exit:
2434 /* Fill the unused part of the output buffer (the whole buffer on error,
2435 * the trailing part on success) with something that isn't a valid mac
2436 * (barring an attack on the mac and deliberately-crafted input),
2437 * in case the caller doesn't check the return status properly. */
2438 if( status == PSA_SUCCESS )
2439 memset( signature + *signature_length, '!',
2440 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002441 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002442 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002443 /* If signature_size is 0 then we have nothing to do. We must not call
2444 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002445 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002446}
2447
2448psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2449 psa_algorithm_t alg,
2450 const uint8_t *hash,
2451 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002452 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002453 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002454{
2455 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002456 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002457
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002458 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2459 if( status != PSA_SUCCESS )
2460 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002461
Gilles Peskine61b91d42018-06-08 16:09:36 +02002462#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002463 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002464 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002465 return( psa_rsa_verify( slot->data.rsa,
2466 alg,
2467 hash, hash_length,
2468 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002469 }
2470 else
2471#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002472#if defined(MBEDTLS_ECP_C)
2473 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2474 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002475#if defined(MBEDTLS_ECDSA_C)
2476 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002477 return( psa_ecdsa_verify( slot->data.ecp,
2478 hash, hash_length,
2479 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002480 else
2481#endif /* defined(MBEDTLS_ECDSA_C) */
2482 {
2483 return( PSA_ERROR_INVALID_ARGUMENT );
2484 }
itayzafrir5c753392018-05-08 11:18:38 +03002485 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002486 else
2487#endif /* defined(MBEDTLS_ECP_C) */
2488 {
2489 return( PSA_ERROR_NOT_SUPPORTED );
2490 }
2491}
2492
Gilles Peskine072ac562018-06-30 00:21:29 +02002493#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2494static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2495 mbedtls_rsa_context *rsa )
2496{
2497 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2498 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2499 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2500 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2501}
2502#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2503
Gilles Peskine61b91d42018-06-08 16:09:36 +02002504psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2505 psa_algorithm_t alg,
2506 const uint8_t *input,
2507 size_t input_length,
2508 const uint8_t *salt,
2509 size_t salt_length,
2510 uint8_t *output,
2511 size_t output_size,
2512 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002513{
2514 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002515 psa_status_t status;
2516
Darryl Green5cc689a2018-07-24 15:34:10 +01002517 (void) input;
2518 (void) input_length;
2519 (void) salt;
2520 (void) output;
2521 (void) output_size;
2522
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002523 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002524
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002525 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2526 return( PSA_ERROR_INVALID_ARGUMENT );
2527
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002528 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2529 if( status != PSA_SUCCESS )
2530 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002531 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2532 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002533 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002534
2535#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002536 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002537 {
2538 mbedtls_rsa_context *rsa = slot->data.rsa;
2539 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002540 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002541 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002542#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002543 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002544 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002545 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2546 mbedtls_ctr_drbg_random,
2547 &global_data.ctr_drbg,
2548 MBEDTLS_RSA_PUBLIC,
2549 input_length,
2550 input,
2551 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002552 }
2553 else
2554#endif /* MBEDTLS_PKCS1_V15 */
2555#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002556 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002557 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002558 psa_rsa_oaep_set_padding_mode( alg, rsa );
2559 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2560 mbedtls_ctr_drbg_random,
2561 &global_data.ctr_drbg,
2562 MBEDTLS_RSA_PUBLIC,
2563 salt, salt_length,
2564 input_length,
2565 input,
2566 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002567 }
2568 else
2569#endif /* MBEDTLS_PKCS1_V21 */
2570 {
2571 return( PSA_ERROR_INVALID_ARGUMENT );
2572 }
2573 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002574 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002575 return( mbedtls_to_psa_error( ret ) );
2576 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002577 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002578#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002579 {
2580 return( PSA_ERROR_NOT_SUPPORTED );
2581 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002582}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002583
Gilles Peskine61b91d42018-06-08 16:09:36 +02002584psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2585 psa_algorithm_t alg,
2586 const uint8_t *input,
2587 size_t input_length,
2588 const uint8_t *salt,
2589 size_t salt_length,
2590 uint8_t *output,
2591 size_t output_size,
2592 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002593{
2594 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002595 psa_status_t status;
2596
Darryl Green5cc689a2018-07-24 15:34:10 +01002597 (void) input;
2598 (void) input_length;
2599 (void) salt;
2600 (void) output;
2601 (void) output_size;
2602
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002603 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002604
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002605 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2606 return( PSA_ERROR_INVALID_ARGUMENT );
2607
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002608 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2609 if( status != PSA_SUCCESS )
2610 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002611 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2612 return( PSA_ERROR_INVALID_ARGUMENT );
2613
2614#if defined(MBEDTLS_RSA_C)
2615 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2616 {
2617 mbedtls_rsa_context *rsa = slot->data.rsa;
2618 int ret;
2619
Gilles Peskine630a18a2018-06-29 17:49:35 +02002620 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002621 return( PSA_ERROR_INVALID_ARGUMENT );
2622
2623#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002624 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002625 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002626 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2627 mbedtls_ctr_drbg_random,
2628 &global_data.ctr_drbg,
2629 MBEDTLS_RSA_PRIVATE,
2630 output_length,
2631 input,
2632 output,
2633 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002634 }
2635 else
2636#endif /* MBEDTLS_PKCS1_V15 */
2637#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002638 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002639 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002640 psa_rsa_oaep_set_padding_mode( alg, rsa );
2641 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2642 mbedtls_ctr_drbg_random,
2643 &global_data.ctr_drbg,
2644 MBEDTLS_RSA_PRIVATE,
2645 salt, salt_length,
2646 output_length,
2647 input,
2648 output,
2649 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002650 }
2651 else
2652#endif /* MBEDTLS_PKCS1_V21 */
2653 {
2654 return( PSA_ERROR_INVALID_ARGUMENT );
2655 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002656
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002657 return( mbedtls_to_psa_error( ret ) );
2658 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002659 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002660#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002661 {
2662 return( PSA_ERROR_NOT_SUPPORTED );
2663 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002664}
Gilles Peskine20035e32018-02-03 22:44:14 +01002665
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002666
2667
mohammad1603503973b2018-03-12 15:59:30 +02002668/****************************************************************/
2669/* Symmetric cryptography */
2670/****************************************************************/
2671
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002672/* Initialize the cipher operation structure. Once this function has been
2673 * called, psa_cipher_abort can run and will do the right thing. */
2674static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2675 psa_algorithm_t alg )
2676{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002677 if( ! PSA_ALG_IS_CIPHER( alg ) )
2678 {
2679 memset( operation, 0, sizeof( *operation ) );
2680 return( PSA_ERROR_INVALID_ARGUMENT );
2681 }
2682
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002683 operation->alg = alg;
2684 operation->key_set = 0;
2685 operation->iv_set = 0;
2686 operation->iv_required = 1;
2687 operation->iv_size = 0;
2688 operation->block_size = 0;
2689 mbedtls_cipher_init( &operation->ctx.cipher );
2690 return( PSA_SUCCESS );
2691}
2692
Gilles Peskinee553c652018-06-04 16:22:46 +02002693static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2694 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002695 psa_algorithm_t alg,
2696 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002697{
2698 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2699 psa_status_t status;
2700 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002701 size_t key_bits;
2702 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002703 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2704 PSA_KEY_USAGE_ENCRYPT :
2705 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002706
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002707 status = psa_cipher_init( operation, alg );
2708 if( status != PSA_SUCCESS )
2709 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002710
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002711 status = psa_get_key_from_slot( key, &slot, usage, alg);
2712 if( status != PSA_SUCCESS )
2713 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002714 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002715
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002716 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002717 if( cipher_info == NULL )
2718 return( PSA_ERROR_NOT_SUPPORTED );
2719
mohammad1603503973b2018-03-12 15:59:30 +02002720 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002721 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002722 {
2723 psa_cipher_abort( operation );
2724 return( mbedtls_to_psa_error( ret ) );
2725 }
2726
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002727#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002728 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002729 {
2730 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2731 unsigned char keys[24];
2732 memcpy( keys, slot->data.raw.data, 16 );
2733 memcpy( keys + 16, slot->data.raw.data, 8 );
2734 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2735 keys,
2736 192, cipher_operation );
2737 }
2738 else
2739#endif
2740 {
2741 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2742 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002743 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002744 }
Moran Peker41deec42018-04-04 15:43:05 +03002745 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002746 {
2747 psa_cipher_abort( operation );
2748 return( mbedtls_to_psa_error( ret ) );
2749 }
2750
mohammad16038481e742018-03-18 13:57:31 +02002751#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002752 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002753 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002754 case PSA_ALG_CBC_NO_PADDING:
2755 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2756 MBEDTLS_PADDING_NONE );
2757 break;
2758 case PSA_ALG_CBC_PKCS7:
2759 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2760 MBEDTLS_PADDING_PKCS7 );
2761 break;
2762 default:
2763 /* The algorithm doesn't involve padding. */
2764 ret = 0;
2765 break;
2766 }
2767 if( ret != 0 )
2768 {
2769 psa_cipher_abort( operation );
2770 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002771 }
2772#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2773
mohammad1603503973b2018-03-12 15:59:30 +02002774 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002775 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2776 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2777 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002778 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002779 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002780 }
mohammad1603503973b2018-03-12 15:59:30 +02002781
Moran Peker395db872018-05-31 14:07:14 +03002782 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002783}
2784
Gilles Peskinefe119512018-07-08 21:39:34 +02002785psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2786 psa_key_slot_t key,
2787 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002788{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002789 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002790}
2791
Gilles Peskinefe119512018-07-08 21:39:34 +02002792psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2793 psa_key_slot_t key,
2794 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002795{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002796 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002797}
2798
Gilles Peskinefe119512018-07-08 21:39:34 +02002799psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2800 unsigned char *iv,
2801 size_t iv_size,
2802 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002803{
itayzafrir534bd7c2018-08-02 13:56:32 +03002804 psa_status_t status;
2805 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002806 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002807 {
2808 status = PSA_ERROR_BAD_STATE;
2809 goto exit;
2810 }
Moran Peker41deec42018-04-04 15:43:05 +03002811 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002812 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002813 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002814 goto exit;
2815 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002816 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2817 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002818 if( ret != 0 )
2819 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002820 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002821 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002822 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002823
mohammad16038481e742018-03-18 13:57:31 +02002824 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002825 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002826
Moran Peker395db872018-05-31 14:07:14 +03002827exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002828 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002829 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002830 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002831}
2832
Gilles Peskinefe119512018-07-08 21:39:34 +02002833psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2834 const unsigned char *iv,
2835 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002836{
itayzafrir534bd7c2018-08-02 13:56:32 +03002837 psa_status_t status;
2838 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002839 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002840 {
2841 status = PSA_ERROR_BAD_STATE;
2842 goto exit;
2843 }
Moran Pekera28258c2018-05-29 16:25:04 +03002844 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002845 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002846 status = PSA_ERROR_INVALID_ARGUMENT;
2847 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002848 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002849 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2850 status = mbedtls_to_psa_error( ret );
2851exit:
2852 if( status == PSA_SUCCESS )
2853 operation->iv_set = 1;
2854 else
mohammad1603503973b2018-03-12 15:59:30 +02002855 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002856 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002857}
2858
Gilles Peskinee553c652018-06-04 16:22:46 +02002859psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2860 const uint8_t *input,
2861 size_t input_length,
2862 unsigned char *output,
2863 size_t output_size,
2864 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002865{
itayzafrir534bd7c2018-08-02 13:56:32 +03002866 psa_status_t status;
2867 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002868 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002869 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002870 {
2871 /* Take the unprocessed partial block left over from previous
2872 * update calls, if any, plus the input to this call. Remove
2873 * the last partial block, if any. You get the data that will be
2874 * output in this call. */
2875 expected_output_size =
2876 ( operation->ctx.cipher.unprocessed_len + input_length )
2877 / operation->block_size * operation->block_size;
2878 }
2879 else
2880 {
2881 expected_output_size = input_length;
2882 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002883
Gilles Peskine89d789c2018-06-04 17:17:16 +02002884 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002885 {
2886 status = PSA_ERROR_BUFFER_TOO_SMALL;
2887 goto exit;
2888 }
mohammad160382759612018-03-12 18:16:40 +02002889
mohammad1603503973b2018-03-12 15:59:30 +02002890 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002891 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002892 status = mbedtls_to_psa_error( ret );
2893exit:
2894 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002895 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002896 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002897}
2898
Gilles Peskinee553c652018-06-04 16:22:46 +02002899psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2900 uint8_t *output,
2901 size_t output_size,
2902 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002903{
Janos Follath315b51c2018-07-09 16:04:51 +01002904 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2905 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002906 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002907
mohammad1603503973b2018-03-12 15:59:30 +02002908 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002909 {
Janos Follath315b51c2018-07-09 16:04:51 +01002910 status = PSA_ERROR_BAD_STATE;
2911 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002912 }
2913 if( operation->iv_required && ! operation->iv_set )
2914 {
Janos Follath315b51c2018-07-09 16:04:51 +01002915 status = PSA_ERROR_BAD_STATE;
2916 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002917 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002918
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002919 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002920 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2921 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002922 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002923 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002924 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002925 }
2926
Janos Follath315b51c2018-07-09 16:04:51 +01002927 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2928 temp_output_buffer,
2929 output_length );
2930 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002931 {
Janos Follath315b51c2018-07-09 16:04:51 +01002932 status = mbedtls_to_psa_error( cipher_ret );
2933 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002934 }
Janos Follath315b51c2018-07-09 16:04:51 +01002935
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002936 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002937 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002938 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002939 memcpy( output, temp_output_buffer, *output_length );
2940 else
2941 {
Janos Follath315b51c2018-07-09 16:04:51 +01002942 status = PSA_ERROR_BUFFER_TOO_SMALL;
2943 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002944 }
mohammad1603503973b2018-03-12 15:59:30 +02002945
Janos Follath279ab8e2018-07-09 16:13:21 +01002946 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002947 status = psa_cipher_abort( operation );
2948
2949 return( status );
2950
2951error:
2952
2953 *output_length = 0;
2954
Janos Follath279ab8e2018-07-09 16:13:21 +01002955 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002956 (void) psa_cipher_abort( operation );
2957
2958 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002959}
2960
Gilles Peskinee553c652018-06-04 16:22:46 +02002961psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2962{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002963 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002964 {
2965 /* The object has (apparently) been initialized but it is not
2966 * in use. It's ok to call abort on such an object, and there's
2967 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002968 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002969 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002970
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002971 /* Sanity check (shouldn't happen: operation->alg should
2972 * always have been initialized to a valid value). */
2973 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2974 return( PSA_ERROR_BAD_STATE );
2975
mohammad1603503973b2018-03-12 15:59:30 +02002976 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002977
Moran Peker41deec42018-04-04 15:43:05 +03002978 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002979 operation->key_set = 0;
2980 operation->iv_set = 0;
2981 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002982 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002983 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002984
Moran Peker395db872018-05-31 14:07:14 +03002985 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002986}
2987
Gilles Peskinea0655c32018-04-30 17:06:50 +02002988
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002989
mohammad16038cc1cee2018-03-28 01:21:33 +03002990/****************************************************************/
2991/* Key Policy */
2992/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002993
mohammad160327010052018-07-03 13:16:15 +03002994#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002995void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002996{
Gilles Peskine803ce742018-06-18 16:07:14 +02002997 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002998}
2999
Gilles Peskine2d277862018-06-18 15:41:12 +02003000void psa_key_policy_set_usage( psa_key_policy_t *policy,
3001 psa_key_usage_t usage,
3002 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03003003{
mohammad16034eed7572018-03-28 05:14:59 -07003004 policy->usage = usage;
3005 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03003006}
3007
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003008psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003009{
mohammad16036df908f2018-04-02 08:34:15 -07003010 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03003011}
3012
Gilles Peskineaa7bc472018-07-12 00:54:56 +02003013psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003014{
mohammad16036df908f2018-04-02 08:34:15 -07003015 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03003016}
mohammad160327010052018-07-03 13:16:15 +03003017#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03003018
Gilles Peskine2d277862018-06-18 15:41:12 +02003019psa_status_t psa_set_key_policy( psa_key_slot_t key,
3020 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003021{
3022 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003023 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003024
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003025 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003026 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003027
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003028 status = psa_get_empty_key_slot( key, &slot );
3029 if( status != PSA_SUCCESS )
3030 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03003031
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003032 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
3033 PSA_KEY_USAGE_ENCRYPT |
3034 PSA_KEY_USAGE_DECRYPT |
3035 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02003036 PSA_KEY_USAGE_VERIFY |
3037 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07003038 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03003039
mohammad16036df908f2018-04-02 08:34:15 -07003040 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003041
3042 return( PSA_SUCCESS );
3043}
3044
Gilles Peskine2d277862018-06-18 15:41:12 +02003045psa_status_t psa_get_key_policy( psa_key_slot_t key,
3046 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03003047{
3048 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003049 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03003050
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003051 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03003052 return( PSA_ERROR_INVALID_ARGUMENT );
3053
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003054 status = psa_get_key_slot( key, &slot );
3055 if( status != PSA_SUCCESS )
3056 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003057
mohammad16036df908f2018-04-02 08:34:15 -07003058 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03003059
3060 return( PSA_SUCCESS );
3061}
Gilles Peskine20035e32018-02-03 22:44:14 +01003062
Gilles Peskinea0655c32018-04-30 17:06:50 +02003063
3064
mohammad1603804cd712018-03-20 22:44:08 +02003065/****************************************************************/
3066/* Key Lifetime */
3067/****************************************************************/
3068
Gilles Peskine2d277862018-06-18 15:41:12 +02003069psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
3070 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003071{
3072 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003073 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003074
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003075 status = psa_get_key_slot( key, &slot );
3076 if( status != PSA_SUCCESS )
3077 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003078
mohammad1603804cd712018-03-20 22:44:08 +02003079 *lifetime = slot->lifetime;
3080
3081 return( PSA_SUCCESS );
3082}
3083
Gilles Peskine2d277862018-06-18 15:41:12 +02003084psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01003085 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02003086{
3087 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003088 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02003089
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003090 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
3091 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
Darryl Greend49a4992018-06-18 17:27:26 +01003092 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003093 return( PSA_ERROR_INVALID_ARGUMENT );
3094
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003095 status = psa_get_empty_key_slot( key, &slot );
3096 if( status != PSA_SUCCESS )
3097 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02003098
Darryl Greend49a4992018-06-18 17:27:26 +01003099 if( lifetime == PSA_KEY_LIFETIME_WRITE_ONCE )
mohammad1603ba178512018-03-21 04:35:20 -07003100 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003101
Darryl Greend49a4992018-06-18 17:27:26 +01003102#if !defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
3103 if( lifetime == PSA_KEY_LIFETIME_PERSISTENT )
3104 return( PSA_ERROR_NOT_SUPPORTED );
3105#endif
3106
mohammad1603060ad8a2018-03-20 14:28:38 -07003107 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02003108
3109 return( PSA_SUCCESS );
3110}
3111
Gilles Peskine20035e32018-02-03 22:44:14 +01003112
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003113
mohammad16035955c982018-04-26 00:53:03 +03003114/****************************************************************/
3115/* AEAD */
3116/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003117
Gilles Peskineedf9a652018-08-17 18:11:56 +02003118typedef struct
3119{
3120 key_slot_t *slot;
3121 const mbedtls_cipher_info_t *cipher_info;
3122 union
3123 {
3124#if defined(MBEDTLS_CCM_C)
3125 mbedtls_ccm_context ccm;
3126#endif /* MBEDTLS_CCM_C */
3127#if defined(MBEDTLS_GCM_C)
3128 mbedtls_gcm_context gcm;
3129#endif /* MBEDTLS_GCM_C */
3130 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003131 psa_algorithm_t core_alg;
3132 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003133 uint8_t tag_length;
3134} aead_operation_t;
3135
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003136static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003137{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003138 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003139 {
3140#if defined(MBEDTLS_CCM_C)
3141 case PSA_ALG_CCM:
3142 mbedtls_ccm_free( &operation->ctx.ccm );
3143 break;
3144#endif /* MBEDTLS_CCM_C */
3145#if defined(MBEDTLS_CCM_C)
3146 case PSA_ALG_GCM:
3147 mbedtls_gcm_free( &operation->ctx.gcm );
3148 break;
3149#endif /* MBEDTLS_GCM_C */
3150 }
3151}
3152
3153static psa_status_t psa_aead_setup( aead_operation_t *operation,
3154 psa_key_slot_t key,
3155 psa_key_usage_t usage,
3156 psa_algorithm_t alg )
3157{
3158 psa_status_t status;
3159 size_t key_bits;
3160 mbedtls_cipher_id_t cipher_id;
3161
3162 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
3163 if( status != PSA_SUCCESS )
3164 return( status );
3165
3166 key_bits = psa_get_key_bits( operation->slot );
3167
3168 operation->cipher_info =
3169 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
3170 &cipher_id );
3171 if( operation->cipher_info == NULL )
3172 return( PSA_ERROR_NOT_SUPPORTED );
3173
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003174 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003175 {
3176#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003177 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
3178 operation->core_alg = PSA_ALG_CCM;
3179 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003180 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3181 return( PSA_ERROR_INVALID_ARGUMENT );
3182 mbedtls_ccm_init( &operation->ctx.ccm );
3183 status = mbedtls_to_psa_error(
3184 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
3185 operation->slot->data.raw.data,
3186 (unsigned int) key_bits ) );
3187 if( status != 0 )
3188 goto cleanup;
3189 break;
3190#endif /* MBEDTLS_CCM_C */
3191
3192#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003193 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
3194 operation->core_alg = PSA_ALG_GCM;
3195 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003196 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
3197 return( PSA_ERROR_INVALID_ARGUMENT );
3198 mbedtls_gcm_init( &operation->ctx.gcm );
3199 status = mbedtls_to_psa_error(
3200 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
3201 operation->slot->data.raw.data,
3202 (unsigned int) key_bits ) );
3203 break;
3204#endif /* MBEDTLS_GCM_C */
3205
3206 default:
3207 return( PSA_ERROR_NOT_SUPPORTED );
3208 }
3209
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003210 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
3211 {
3212 status = PSA_ERROR_INVALID_ARGUMENT;
3213 goto cleanup;
3214 }
3215 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
3216 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3217 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3218 * In both cases, mbedtls_xxx will validate the tag length below. */
3219
Gilles Peskineedf9a652018-08-17 18:11:56 +02003220 return( PSA_SUCCESS );
3221
3222cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003223 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003224 return( status );
3225}
3226
mohammad16035955c982018-04-26 00:53:03 +03003227psa_status_t psa_aead_encrypt( psa_key_slot_t key,
3228 psa_algorithm_t alg,
3229 const uint8_t *nonce,
3230 size_t nonce_length,
3231 const uint8_t *additional_data,
3232 size_t additional_data_length,
3233 const uint8_t *plaintext,
3234 size_t plaintext_length,
3235 uint8_t *ciphertext,
3236 size_t ciphertext_size,
3237 size_t *ciphertext_length )
3238{
mohammad16035955c982018-04-26 00:53:03 +03003239 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003240 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03003241 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02003242
mohammad1603f08a5502018-06-03 15:05:47 +03003243 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07003244
Gilles Peskineedf9a652018-08-17 18:11:56 +02003245 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003246 if( status != PSA_SUCCESS )
3247 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003248
Gilles Peskineedf9a652018-08-17 18:11:56 +02003249 /* For all currently supported modes, the tag is at the end of the
3250 * ciphertext. */
3251 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
3252 {
3253 status = PSA_ERROR_BUFFER_TOO_SMALL;
3254 goto exit;
3255 }
3256 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03003257
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003258 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003259 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003260 status = mbedtls_to_psa_error(
3261 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
3262 MBEDTLS_GCM_ENCRYPT,
3263 plaintext_length,
3264 nonce, nonce_length,
3265 additional_data, additional_data_length,
3266 plaintext, ciphertext,
3267 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03003268 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003269 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003270 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003271 status = mbedtls_to_psa_error(
3272 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
3273 plaintext_length,
3274 nonce, nonce_length,
3275 additional_data,
3276 additional_data_length,
3277 plaintext, ciphertext,
3278 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003279 }
mohammad16035c8845f2018-05-09 05:40:09 -07003280 else
3281 {
mohammad1603554faad2018-06-03 15:07:38 +03003282 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07003283 }
Gilles Peskine2d277862018-06-18 15:41:12 +02003284
Gilles Peskineedf9a652018-08-17 18:11:56 +02003285 if( status != PSA_SUCCESS && ciphertext_size != 0 )
3286 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02003287
Gilles Peskineedf9a652018-08-17 18:11:56 +02003288exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003289 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003290 if( status == PSA_SUCCESS )
3291 *ciphertext_length = plaintext_length + operation.tag_length;
3292 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003293}
3294
Gilles Peskineee652a32018-06-01 19:23:52 +02003295/* Locate the tag in a ciphertext buffer containing the encrypted data
3296 * followed by the tag. Return the length of the part preceding the tag in
3297 * *plaintext_length. This is the size of the plaintext in modes where
3298 * the encrypted data has the same size as the plaintext, such as
3299 * CCM and GCM. */
3300static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3301 const uint8_t *ciphertext,
3302 size_t ciphertext_length,
3303 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003304 const uint8_t **p_tag )
3305{
3306 size_t payload_length;
3307 if( tag_length > ciphertext_length )
3308 return( PSA_ERROR_INVALID_ARGUMENT );
3309 payload_length = ciphertext_length - tag_length;
3310 if( payload_length > plaintext_size )
3311 return( PSA_ERROR_BUFFER_TOO_SMALL );
3312 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003313 return( PSA_SUCCESS );
3314}
3315
mohammad16035955c982018-04-26 00:53:03 +03003316psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3317 psa_algorithm_t alg,
3318 const uint8_t *nonce,
3319 size_t nonce_length,
3320 const uint8_t *additional_data,
3321 size_t additional_data_length,
3322 const uint8_t *ciphertext,
3323 size_t ciphertext_length,
3324 uint8_t *plaintext,
3325 size_t plaintext_size,
3326 size_t *plaintext_length )
3327{
mohammad16035955c982018-04-26 00:53:03 +03003328 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003329 aead_operation_t operation;
3330 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003331
Gilles Peskineee652a32018-06-01 19:23:52 +02003332 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003333
Gilles Peskineedf9a652018-08-17 18:11:56 +02003334 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003335 if( status != PSA_SUCCESS )
3336 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003337
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003338 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003339 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003340 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003341 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003342 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003343 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003344 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003345
Gilles Peskineedf9a652018-08-17 18:11:56 +02003346 status = mbedtls_to_psa_error(
3347 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3348 ciphertext_length - operation.tag_length,
3349 nonce, nonce_length,
3350 additional_data,
3351 additional_data_length,
3352 tag, operation.tag_length,
3353 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003354 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003355 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003356 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003357 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003358 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003359 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003360 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003361 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003362
Gilles Peskineedf9a652018-08-17 18:11:56 +02003363 status = mbedtls_to_psa_error(
3364 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3365 ciphertext_length - operation.tag_length,
3366 nonce, nonce_length,
3367 additional_data,
3368 additional_data_length,
3369 ciphertext, plaintext,
3370 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003371 }
mohammad160339574652018-06-01 04:39:53 -07003372 else
3373 {
mohammad1603554faad2018-06-03 15:07:38 +03003374 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003375 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003376
Gilles Peskineedf9a652018-08-17 18:11:56 +02003377 if( status != PSA_SUCCESS && plaintext_size != 0 )
3378 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003379
Gilles Peskineedf9a652018-08-17 18:11:56 +02003380exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003381 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003382 if( status == PSA_SUCCESS )
3383 *plaintext_length = ciphertext_length - operation.tag_length;
3384 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003385}
3386
Gilles Peskinea0655c32018-04-30 17:06:50 +02003387
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003388
Gilles Peskine20035e32018-02-03 22:44:14 +01003389/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003390/* Generators */
3391/****************************************************************/
3392
3393psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3394{
3395 psa_status_t status = PSA_SUCCESS;
3396 if( generator->alg == 0 )
3397 {
3398 /* The object has (apparently) been initialized but it is not
3399 * in use. It's ok to call abort on such an object, and there's
3400 * nothing to do. */
3401 }
3402 else
Gilles Peskine751d9652018-09-18 12:05:44 +02003403 if( generator->alg == PSA_ALG_SELECT_RAW )
3404 {
3405 if( generator->ctx.buffer.data != NULL )
3406 {
3407 mbedtls_zeroize( generator->ctx.buffer.data,
3408 generator->ctx.buffer.size );
3409 mbedtls_free( generator->ctx.buffer.data );
3410 }
3411 }
3412 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003413#if defined(MBEDTLS_MD_C)
3414 if( PSA_ALG_IS_HKDF( generator->alg ) )
3415 {
3416 mbedtls_free( generator->ctx.hkdf.info );
3417 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3418 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003419 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3420 /* TLS-1.2 PSK-to-MS KDF uses the same generator as TLS-1.2 PRF */
3421 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003422 {
3423 if( generator->ctx.tls12_prf.key != NULL )
3424 {
3425 mbedtls_zeroize( generator->ctx.tls12_prf.key,
3426 generator->ctx.tls12_prf.key_len );
3427 mbedtls_free( generator->ctx.tls12_prf.key );
3428 }
Hanno Becker580fba12018-11-13 20:50:45 +00003429
3430 if( generator->ctx.tls12_prf.Ai_with_seed != NULL )
3431 {
3432 mbedtls_zeroize( generator->ctx.tls12_prf.Ai_with_seed,
3433 generator->ctx.tls12_prf.Ai_with_seed_len );
3434 mbedtls_free( generator->ctx.tls12_prf.Ai_with_seed );
3435 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003436 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003437 else
3438#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003439 {
3440 status = PSA_ERROR_BAD_STATE;
3441 }
3442 memset( generator, 0, sizeof( *generator ) );
3443 return( status );
3444}
3445
3446
3447psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3448 size_t *capacity)
3449{
3450 *capacity = generator->capacity;
3451 return( PSA_SUCCESS );
3452}
3453
Gilles Peskinebef7f142018-07-12 17:22:21 +02003454#if defined(MBEDTLS_MD_C)
3455/* Read some bytes from an HKDF-based generator. This performs a chunk
3456 * of the expand phase of the HKDF algorithm. */
3457static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3458 psa_algorithm_t hash_alg,
3459 uint8_t *output,
3460 size_t output_length )
3461{
3462 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3463 psa_status_t status;
3464
3465 while( output_length != 0 )
3466 {
3467 /* Copy what remains of the current block */
3468 uint8_t n = hash_length - hkdf->offset_in_block;
3469 if( n > output_length )
3470 n = (uint8_t) output_length;
3471 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3472 output += n;
3473 output_length -= n;
3474 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003475 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003476 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003477 /* We can't be wanting more output after block 0xff, otherwise
3478 * the capacity check in psa_generator_read() would have
3479 * prevented this call. It could happen only if the generator
3480 * object was corrupted or if this function is called directly
3481 * inside the library. */
3482 if( hkdf->block_number == 0xff )
3483 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003484
3485 /* We need a new block */
3486 ++hkdf->block_number;
3487 hkdf->offset_in_block = 0;
3488 status = psa_hmac_setup_internal( &hkdf->hmac,
3489 hkdf->prk, hash_length,
3490 hash_alg );
3491 if( status != PSA_SUCCESS )
3492 return( status );
3493 if( hkdf->block_number != 1 )
3494 {
3495 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3496 hkdf->output_block,
3497 hash_length );
3498 if( status != PSA_SUCCESS )
3499 return( status );
3500 }
3501 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3502 hkdf->info,
3503 hkdf->info_length );
3504 if( status != PSA_SUCCESS )
3505 return( status );
3506 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3507 &hkdf->block_number, 1 );
3508 if( status != PSA_SUCCESS )
3509 return( status );
3510 status = psa_hmac_finish_internal( &hkdf->hmac,
3511 hkdf->output_block,
3512 sizeof( hkdf->output_block ) );
3513 if( status != PSA_SUCCESS )
3514 return( status );
3515 }
3516
3517 return( PSA_SUCCESS );
3518}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003519
3520static psa_status_t psa_generator_tls12_prf_generate_next_block(
3521 psa_tls12_prf_generator_t *tls12_prf,
3522 psa_algorithm_t alg )
3523{
3524 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3525 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3526 psa_hmac_internal_data hmac;
3527 psa_status_t status, cleanup_status;
3528
Hanno Becker3b339e22018-11-13 20:56:14 +00003529 unsigned char *Ai;
3530 size_t Ai_len;
3531
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003532 /* We can't be wanting more output after block 0xff, otherwise
3533 * the capacity check in psa_generator_read() would have
3534 * prevented this call. It could happen only if the generator
3535 * object was corrupted or if this function is called directly
3536 * inside the library. */
3537 if( tls12_prf->block_number == 0xff )
3538 return( PSA_ERROR_BAD_STATE );
3539
3540 /* We need a new block */
3541 ++tls12_prf->block_number;
3542 tls12_prf->offset_in_block = 0;
3543
3544 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
3545 *
3546 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
3547 *
3548 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
3549 * HMAC_hash(secret, A(2) + seed) +
3550 * HMAC_hash(secret, A(3) + seed) + ...
3551 *
3552 * A(0) = seed
3553 * A(i) = HMAC_hash( secret, A(i-1) )
3554 *
3555 * The `psa_tls12_prf_generator` structures saves the block
3556 * `HMAC_hash(secret, A(i) + seed)` from which the output
3557 * is currently extracted as `output_block`, while
3558 * `A(i) + seed` is stored in `Ai_with_seed`.
3559 *
3560 * Generating a new block means recalculating `Ai_with_seed`
3561 * from the A(i)-part of it, and afterwards recalculating
3562 * `output_block`.
3563 *
3564 * A(0) is computed at setup time.
3565 *
3566 */
3567
3568 psa_hmac_init_internal( &hmac );
3569
3570 /* We must distinguish the calculation of A(1) from those
3571 * of A(2) and higher, because A(0)=seed has a different
3572 * length than the other A(i). */
3573 if( tls12_prf->block_number == 1 )
3574 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003575 Ai = tls12_prf->Ai_with_seed + hash_length;
3576 Ai_len = tls12_prf->Ai_with_seed_len - hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003577 }
3578 else
3579 {
Hanno Becker3b339e22018-11-13 20:56:14 +00003580 Ai = tls12_prf->Ai_with_seed;
3581 Ai_len = hash_length;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003582 }
3583
Hanno Becker3b339e22018-11-13 20:56:14 +00003584 /* Compute A(i+1) = HMAC_hash(secret, A(i)) */
3585 status = psa_hmac_setup_internal( &hmac,
3586 tls12_prf->key,
3587 tls12_prf->key_len,
3588 hash_alg );
3589 if( status != PSA_SUCCESS )
3590 goto cleanup;
3591
3592 status = psa_hash_update( &hmac.hash_ctx,
3593 Ai, Ai_len );
3594 if( status != PSA_SUCCESS )
3595 goto cleanup;
3596
3597 status = psa_hmac_finish_internal( &hmac,
3598 tls12_prf->Ai_with_seed,
3599 hash_length );
3600 if( status != PSA_SUCCESS )
3601 goto cleanup;
3602
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003603 /* Compute the next block `HMAC_hash(secret, A(i+1) + seed)`. */
3604 status = psa_hmac_setup_internal( &hmac,
3605 tls12_prf->key,
3606 tls12_prf->key_len,
3607 hash_alg );
3608 if( status != PSA_SUCCESS )
3609 goto cleanup;
3610
3611 status = psa_hash_update( &hmac.hash_ctx,
3612 tls12_prf->Ai_with_seed,
Hanno Becker580fba12018-11-13 20:50:45 +00003613 tls12_prf->Ai_with_seed_len );
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003614 if( status != PSA_SUCCESS )
3615 goto cleanup;
3616
3617 status = psa_hmac_finish_internal( &hmac,
3618 tls12_prf->output_block,
3619 hash_length );
3620 if( status != PSA_SUCCESS )
3621 goto cleanup;
3622
3623cleanup:
3624
3625 cleanup_status = psa_hmac_abort_internal( &hmac );
3626 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
3627 status = cleanup_status;
3628
3629 return( status );
3630}
3631
3632/* Read some bytes from an TLS-1.2-PRF-based generator.
3633 * See Section 5 of RFC 5246. */
3634static psa_status_t psa_generator_tls12_prf_read(
3635 psa_tls12_prf_generator_t *tls12_prf,
3636 psa_algorithm_t alg,
3637 uint8_t *output,
3638 size_t output_length )
3639{
3640 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3641 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3642 psa_status_t status;
3643
3644 while( output_length != 0 )
3645 {
3646 /* Copy what remains of the current block */
3647 uint8_t n = hash_length - tls12_prf->offset_in_block;
3648
3649 /* Check if we have fully processed the current block. */
3650 if( n == 0 )
3651 {
3652 status = psa_generator_tls12_prf_generate_next_block( tls12_prf,
3653 alg );
3654 if( status != PSA_SUCCESS )
3655 return( status );
3656
3657 continue;
3658 }
3659
3660 if( n > output_length )
3661 n = (uint8_t) output_length;
3662 memcpy( output, tls12_prf->output_block + tls12_prf->offset_in_block,
3663 n );
3664 output += n;
3665 output_length -= n;
3666 tls12_prf->offset_in_block += n;
3667 }
3668
3669 return( PSA_SUCCESS );
3670}
Gilles Peskinebef7f142018-07-12 17:22:21 +02003671#endif /* MBEDTLS_MD_C */
3672
Gilles Peskineeab56e42018-07-12 17:12:33 +02003673psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3674 uint8_t *output,
3675 size_t output_length )
3676{
3677 psa_status_t status;
3678
3679 if( output_length > generator->capacity )
3680 {
3681 generator->capacity = 0;
3682 /* Go through the error path to wipe all confidential data now
3683 * that the generator object is useless. */
3684 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3685 goto exit;
3686 }
3687 if( output_length == 0 &&
3688 generator->capacity == 0 && generator->alg == 0 )
3689 {
3690 /* Edge case: this is a blank or finished generator, and 0
3691 * bytes were requested. The right error in this case could
3692 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3693 * INSUFFICIENT_CAPACITY, which is right for a finished
3694 * generator, for consistency with the case when
3695 * output_length > 0. */
3696 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3697 }
3698 generator->capacity -= output_length;
3699
Gilles Peskine751d9652018-09-18 12:05:44 +02003700 if( generator->alg == PSA_ALG_SELECT_RAW )
3701 {
Gilles Peskine211a4362018-10-25 22:22:31 +02003702 /* Initially, the capacity of a selection generator is always
3703 * the size of the buffer, i.e. `generator->ctx.buffer.size`,
3704 * abbreviated in this comment as `size`. When the remaining
3705 * capacity is `c`, the next bytes to serve start `c` bytes
3706 * from the end of the buffer, i.e. `size - c` from the
3707 * beginning of the buffer. Since `generator->capacity` was just
3708 * decremented above, we need to serve the bytes from
3709 * `size - generator->capacity - output_length` to
3710 * `size - generator->capacity`. */
Gilles Peskine751d9652018-09-18 12:05:44 +02003711 size_t offset =
3712 generator->ctx.buffer.size - generator->capacity - output_length;
3713 memcpy( output, generator->ctx.buffer.data + offset, output_length );
3714 status = PSA_SUCCESS;
3715 }
3716 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003717#if defined(MBEDTLS_MD_C)
3718 if( PSA_ALG_IS_HKDF( generator->alg ) )
3719 {
3720 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3721 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3722 output, output_length );
3723 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003724 else if( PSA_ALG_IS_TLS12_PRF( generator->alg ) ||
3725 PSA_ALG_IS_TLS12_PSK_TO_MS( generator->alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003726 {
3727 status = psa_generator_tls12_prf_read( &generator->ctx.tls12_prf,
3728 generator->alg, output,
3729 output_length );
3730 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02003731 else
3732#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003733 {
3734 return( PSA_ERROR_BAD_STATE );
3735 }
3736
3737exit:
3738 if( status != PSA_SUCCESS )
3739 {
3740 psa_generator_abort( generator );
3741 memset( output, '!', output_length );
3742 }
3743 return( status );
3744}
3745
Gilles Peskine08542d82018-07-19 17:05:42 +02003746#if defined(MBEDTLS_DES_C)
3747static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3748{
3749 if( data_size >= 8 )
3750 mbedtls_des_key_set_parity( data );
3751 if( data_size >= 16 )
3752 mbedtls_des_key_set_parity( data + 8 );
3753 if( data_size >= 24 )
3754 mbedtls_des_key_set_parity( data + 16 );
3755}
3756#endif /* MBEDTLS_DES_C */
3757
Gilles Peskineeab56e42018-07-12 17:12:33 +02003758psa_status_t psa_generator_import_key( psa_key_slot_t key,
3759 psa_key_type_t type,
3760 size_t bits,
3761 psa_crypto_generator_t *generator )
3762{
3763 uint8_t *data = NULL;
3764 size_t bytes = PSA_BITS_TO_BYTES( bits );
3765 psa_status_t status;
3766
3767 if( ! key_type_is_raw_bytes( type ) )
3768 return( PSA_ERROR_INVALID_ARGUMENT );
3769 if( bits % 8 != 0 )
3770 return( PSA_ERROR_INVALID_ARGUMENT );
3771 data = mbedtls_calloc( 1, bytes );
3772 if( data == NULL )
3773 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3774
3775 status = psa_generator_read( generator, data, bytes );
3776 if( status != PSA_SUCCESS )
3777 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003778#if defined(MBEDTLS_DES_C)
3779 if( type == PSA_KEY_TYPE_DES )
3780 psa_des_set_key_parity( data, bytes );
3781#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003782 status = psa_import_key( key, type, data, bytes );
3783
3784exit:
3785 mbedtls_free( data );
3786 return( status );
3787}
3788
3789
3790
3791/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003792/* Key derivation */
3793/****************************************************************/
3794
Gilles Peskinea05219c2018-11-16 16:02:56 +01003795#if defined(MBEDTLS_MD_C)
Gilles Peskinebef7f142018-07-12 17:22:21 +02003796/* Set up an HKDF-based generator. This is exactly the extract phase
Gilles Peskine346797d2018-11-16 16:05:06 +01003797 * of the HKDF algorithm.
3798 *
3799 * Note that if this function fails, you must call psa_generator_abort()
3800 * to potentially free embedded data structures and wipe confidential data.
3801 */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003802static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003803 const uint8_t *secret,
3804 size_t secret_length,
Gilles Peskinebef7f142018-07-12 17:22:21 +02003805 psa_algorithm_t hash_alg,
3806 const uint8_t *salt,
3807 size_t salt_length,
3808 const uint8_t *label,
3809 size_t label_length )
3810{
3811 psa_status_t status;
3812 status = psa_hmac_setup_internal( &hkdf->hmac,
3813 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003814 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003815 if( status != PSA_SUCCESS )
3816 return( status );
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003817 status = psa_hash_update( &hkdf->hmac.hash_ctx, secret, secret_length );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003818 if( status != PSA_SUCCESS )
3819 return( status );
3820 status = psa_hmac_finish_internal( &hkdf->hmac,
3821 hkdf->prk,
3822 sizeof( hkdf->prk ) );
3823 if( status != PSA_SUCCESS )
3824 return( status );
3825 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3826 hkdf->block_number = 0;
3827 hkdf->info_length = label_length;
3828 if( label_length != 0 )
3829 {
3830 hkdf->info = mbedtls_calloc( 1, label_length );
3831 if( hkdf->info == NULL )
3832 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3833 memcpy( hkdf->info, label, label_length );
3834 }
3835 return( PSA_SUCCESS );
3836}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003837#endif /* MBEDTLS_MD_C */
Gilles Peskinebef7f142018-07-12 17:22:21 +02003838
Gilles Peskinea05219c2018-11-16 16:02:56 +01003839#if defined(MBEDTLS_MD_C)
Gilles Peskine346797d2018-11-16 16:05:06 +01003840/* Set up a TLS-1.2-prf-based generator (see RFC 5246, Section 5).
3841 *
3842 * Note that if this function fails, you must call psa_generator_abort()
3843 * to potentially free embedded data structures and wipe confidential data.
3844 */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003845static psa_status_t psa_generator_tls12_prf_setup(
3846 psa_tls12_prf_generator_t *tls12_prf,
3847 const unsigned char *key,
3848 size_t key_len,
3849 psa_algorithm_t hash_alg,
3850 const uint8_t *salt,
3851 size_t salt_length,
3852 const uint8_t *label,
3853 size_t label_length )
3854{
3855 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Hanno Becker580fba12018-11-13 20:50:45 +00003856 size_t Ai_with_seed_len = hash_length + salt_length + label_length;
3857 int overflow;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003858
3859 tls12_prf->key = mbedtls_calloc( 1, key_len );
3860 if( tls12_prf->key == NULL )
3861 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3862 tls12_prf->key_len = key_len;
3863 memcpy( tls12_prf->key, key, key_len );
3864
Hanno Becker580fba12018-11-13 20:50:45 +00003865 overflow = ( salt_length + label_length < salt_length ) ||
3866 ( salt_length + label_length + hash_length < hash_length );
3867 if( overflow )
3868 return( PSA_ERROR_INVALID_ARGUMENT );
3869
3870 tls12_prf->Ai_with_seed = mbedtls_calloc( 1, Ai_with_seed_len );
3871 if( tls12_prf->Ai_with_seed == NULL )
3872 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3873 tls12_prf->Ai_with_seed_len = Ai_with_seed_len;
3874
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003875 /* Write `label + seed' at the end of the `A(i) + seed` buffer,
3876 * leaving the initial `hash_length` bytes unspecified for now. */
Hanno Becker353e4532018-11-15 09:53:57 +00003877 if( label_length != 0 )
3878 {
3879 memcpy( tls12_prf->Ai_with_seed + hash_length,
3880 label, label_length );
3881 }
3882
3883 if( salt_length != 0 )
3884 {
3885 memcpy( tls12_prf->Ai_with_seed + hash_length + label_length,
3886 salt, salt_length );
3887 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003888
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003889 /* The first block gets generated when
3890 * psa_generator_read() is called. */
3891 tls12_prf->block_number = 0;
3892 tls12_prf->offset_in_block = hash_length;
3893
3894 return( PSA_SUCCESS );
3895}
Hanno Becker1aaedc02018-11-16 11:35:34 +00003896
3897/* Set up a TLS-1.2-PSK-to-MS-based generator. */
3898static psa_status_t psa_generator_tls12_psk_to_ms_setup(
3899 psa_tls12_prf_generator_t *tls12_prf,
3900 const unsigned char *psk,
3901 size_t psk_len,
3902 psa_algorithm_t hash_alg,
3903 const uint8_t *salt,
3904 size_t salt_length,
3905 const uint8_t *label,
3906 size_t label_length )
3907{
3908 psa_status_t status;
3909 unsigned char pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
3910
3911 if( psk_len > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
3912 return( PSA_ERROR_INVALID_ARGUMENT );
3913
3914 /* Quoting RFC 4279, Section 2:
3915 *
3916 * The premaster secret is formed as follows: if the PSK is N octets
3917 * long, concatenate a uint16 with the value N, N zero octets, a second
3918 * uint16 with the value N, and the PSK itself.
3919 */
3920
3921 pms[0] = ( psk_len >> 8 ) & 0xff;
3922 pms[1] = ( psk_len >> 0 ) & 0xff;
3923 memset( pms + 2, 0, psk_len );
3924 pms[2 + psk_len + 0] = pms[0];
3925 pms[2 + psk_len + 1] = pms[1];
3926 memcpy( pms + 4 + psk_len, psk, psk_len );
3927
3928 status = psa_generator_tls12_prf_setup( tls12_prf,
3929 pms, 4 + 2 * psk_len,
3930 hash_alg,
3931 salt, salt_length,
3932 label, label_length );
3933
3934 mbedtls_zeroize( pms, sizeof( pms ) );
3935 return( status );
3936}
Gilles Peskinea05219c2018-11-16 16:02:56 +01003937#endif /* MBEDTLS_MD_C */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003938
Gilles Peskine346797d2018-11-16 16:05:06 +01003939/* Note that if this function fails, you must call psa_generator_abort()
3940 * to potentially free embedded data structures and wipe confidential data.
3941 */
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003942static psa_status_t psa_key_derivation_internal(
3943 psa_crypto_generator_t *generator,
3944 const uint8_t *secret, size_t secret_length,
3945 psa_algorithm_t alg,
3946 const uint8_t *salt, size_t salt_length,
3947 const uint8_t *label, size_t label_length,
3948 size_t capacity )
3949{
3950 psa_status_t status;
3951 size_t max_capacity;
3952
3953 /* Set generator->alg even on failure so that abort knows what to do. */
3954 generator->alg = alg;
3955
Gilles Peskine751d9652018-09-18 12:05:44 +02003956 if( alg == PSA_ALG_SELECT_RAW )
3957 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003958 (void) salt;
Gilles Peskine751d9652018-09-18 12:05:44 +02003959 if( salt_length != 0 )
3960 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea05219c2018-11-16 16:02:56 +01003961 (void) label;
Gilles Peskine751d9652018-09-18 12:05:44 +02003962 if( label_length != 0 )
3963 return( PSA_ERROR_INVALID_ARGUMENT );
3964 generator->ctx.buffer.data = mbedtls_calloc( 1, secret_length );
3965 if( generator->ctx.buffer.data == NULL )
3966 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3967 memcpy( generator->ctx.buffer.data, secret, secret_length );
3968 generator->ctx.buffer.size = secret_length;
3969 max_capacity = secret_length;
3970 status = PSA_SUCCESS;
3971 }
3972 else
Gilles Peskinecce18ae2018-09-18 12:03:52 +02003973#if defined(MBEDTLS_MD_C)
3974 if( PSA_ALG_IS_HKDF( alg ) )
3975 {
3976 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3977 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3978 if( hash_size == 0 )
3979 return( PSA_ERROR_NOT_SUPPORTED );
3980 max_capacity = 255 * hash_size;
3981 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3982 secret, secret_length,
3983 hash_alg,
3984 salt, salt_length,
3985 label, label_length );
3986 }
Hanno Becker1aaedc02018-11-16 11:35:34 +00003987 /* TLS-1.2 PRF and TLS-1.2 PSK-to-MS are very similar, so share code. */
3988 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
3989 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01003990 {
3991 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
3992 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3993
3994 /* TLS-1.2 PRF supports only SHA-256 and SHA-384. */
3995 if( hash_alg != PSA_ALG_SHA_256 &&
3996 hash_alg != PSA_ALG_SHA_384 )
3997 {
3998 return( PSA_ERROR_NOT_SUPPORTED );
3999 }
4000
4001 max_capacity = 255 * hash_size;
Hanno Becker1aaedc02018-11-16 11:35:34 +00004002
4003 if( PSA_ALG_IS_TLS12_PRF( alg ) )
4004 {
4005 status = psa_generator_tls12_prf_setup( &generator->ctx.tls12_prf,
4006 secret, secret_length,
4007 hash_alg, salt, salt_length,
4008 label, label_length );
4009 }
4010 else
4011 {
4012 status = psa_generator_tls12_psk_to_ms_setup(
4013 &generator->ctx.tls12_prf,
4014 secret, secret_length,
4015 hash_alg, salt, salt_length,
4016 label, label_length );
4017 }
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004018 }
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004019 else
4020#endif
4021 {
4022 return( PSA_ERROR_NOT_SUPPORTED );
4023 }
4024
4025 if( status != PSA_SUCCESS )
4026 return( status );
4027
4028 if( capacity <= max_capacity )
4029 generator->capacity = capacity;
Gilles Peskine8feb3a82018-09-18 12:06:11 +02004030 else if( capacity == PSA_GENERATOR_UNBRIDLED_CAPACITY )
4031 generator->capacity = max_capacity;
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004032 else
4033 return( PSA_ERROR_INVALID_ARGUMENT );
4034
4035 return( PSA_SUCCESS );
4036}
4037
Gilles Peskineea0fb492018-07-12 17:17:20 +02004038psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01004039 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02004040 psa_algorithm_t alg,
4041 const uint8_t *salt,
4042 size_t salt_length,
4043 const uint8_t *label,
4044 size_t label_length,
4045 size_t capacity )
4046{
4047 key_slot_t *slot;
4048 psa_status_t status;
4049
4050 if( generator->alg != 0 )
4051 return( PSA_ERROR_BAD_STATE );
4052
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004053 /* Make sure that alg is a key derivation algorithm. This prevents
4054 * key selection algorithms, which psa_key_derivation_internal
4055 * accepts for the sake of key agreement. */
Gilles Peskineea0fb492018-07-12 17:17:20 +02004056 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
4057 return( PSA_ERROR_INVALID_ARGUMENT );
4058
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004059 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
4060 if( status != PSA_SUCCESS )
4061 return( status );
Gilles Peskineea0fb492018-07-12 17:17:20 +02004062
Gilles Peskinecce18ae2018-09-18 12:03:52 +02004063 if( slot->type != PSA_KEY_TYPE_DERIVE )
4064 return( PSA_ERROR_INVALID_ARGUMENT );
4065
4066 status = psa_key_derivation_internal( generator,
4067 slot->data.raw.data,
4068 slot->data.raw.bytes,
4069 alg,
4070 salt, salt_length,
4071 label, label_length,
4072 capacity );
4073 if( status != PSA_SUCCESS )
Gilles Peskineea0fb492018-07-12 17:17:20 +02004074 psa_generator_abort( generator );
4075 return( status );
4076}
4077
4078
4079
4080/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02004081/* Key agreement */
4082/****************************************************************/
4083
Gilles Peskinea05219c2018-11-16 16:02:56 +01004084#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004085static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
4086 size_t peer_key_length,
4087 const mbedtls_ecp_keypair *our_key,
4088 uint8_t *shared_secret,
4089 size_t shared_secret_size,
4090 size_t *shared_secret_length )
4091{
4092 mbedtls_pk_context pk;
4093 mbedtls_ecp_keypair *their_key = NULL;
4094 mbedtls_ecdh_context ecdh;
4095 int ret;
4096 mbedtls_ecdh_init( &ecdh );
4097 mbedtls_pk_init( &pk );
4098
4099 ret = mbedtls_pk_parse_public_key( &pk, peer_key, peer_key_length );
4100 if( ret != 0 )
4101 goto exit;
Gilles Peskine88714d72018-10-25 23:07:25 +02004102 switch( mbedtls_pk_get_type( &pk ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004103 {
Gilles Peskine88714d72018-10-25 23:07:25 +02004104 case MBEDTLS_PK_ECKEY:
4105 case MBEDTLS_PK_ECKEY_DH:
4106 break;
4107 default:
4108 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4109 goto exit;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004110 }
4111 their_key = mbedtls_pk_ec( pk );
Gilles Peskineb4086612018-11-14 20:51:23 +01004112 if( their_key->grp.id != our_key->grp.id )
4113 {
4114 ret = MBEDTLS_ERR_ECP_INVALID_KEY;
4115 goto exit;
4116 }
4117
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004118 ret = mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS );
4119 if( ret != 0 )
4120 goto exit;
4121 ret = mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS );
4122 if( ret != 0 )
4123 goto exit;
4124
4125 ret = mbedtls_ecdh_calc_secret( &ecdh,
4126 shared_secret_length,
4127 shared_secret, shared_secret_size,
4128 mbedtls_ctr_drbg_random,
4129 &global_data.ctr_drbg );
4130
4131exit:
4132 mbedtls_pk_free( &pk );
4133 mbedtls_ecdh_free( &ecdh );
4134 return( mbedtls_to_psa_error( ret ) );
4135}
Gilles Peskinea05219c2018-11-16 16:02:56 +01004136#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004137
Gilles Peskine01d718c2018-09-18 12:01:02 +02004138#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
4139
Gilles Peskine346797d2018-11-16 16:05:06 +01004140/* Note that if this function fails, you must call psa_generator_abort()
4141 * to potentially free embedded data structures and wipe confidential data.
4142 */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004143static psa_status_t psa_key_agreement_internal( psa_crypto_generator_t *generator,
4144 key_slot_t *private_key,
4145 const uint8_t *peer_key,
4146 size_t peer_key_length,
4147 psa_algorithm_t alg )
4148{
4149 psa_status_t status;
4150 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
4151 size_t shared_secret_length = 0;
4152
4153 /* Step 1: run the secret agreement algorithm to generate the shared
4154 * secret. */
4155 switch( PSA_ALG_KEY_AGREEMENT_GET_BASE( alg ) )
4156 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02004157#if defined(MBEDTLS_ECDH_C)
4158 case PSA_ALG_ECDH_BASE:
4159 if( ! PSA_KEY_TYPE_IS_ECC_KEYPAIR( private_key->type ) )
4160 return( PSA_ERROR_INVALID_ARGUMENT );
4161 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
4162 private_key->data.ecp,
4163 shared_secret,
4164 sizeof( shared_secret ),
4165 &shared_secret_length );
4166 break;
4167#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02004168 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01004169 (void) private_key;
4170 (void) peer_key;
4171 (void) peer_key_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02004172 return( PSA_ERROR_NOT_SUPPORTED );
4173 }
4174 if( status != PSA_SUCCESS )
4175 goto exit;
4176
4177 /* Step 2: set up the key derivation to generate key material from
4178 * the shared secret. */
4179 status = psa_key_derivation_internal( generator,
4180 shared_secret, shared_secret_length,
4181 PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ),
4182 NULL, 0, NULL, 0,
4183 PSA_GENERATOR_UNBRIDLED_CAPACITY );
4184exit:
4185 mbedtls_zeroize( shared_secret, shared_secret_length );
4186 return( status );
4187}
4188
4189psa_status_t psa_key_agreement( psa_crypto_generator_t *generator,
4190 psa_key_slot_t private_key,
4191 const uint8_t *peer_key,
4192 size_t peer_key_length,
4193 psa_algorithm_t alg )
4194{
4195 key_slot_t *slot;
4196 psa_status_t status;
4197 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
4198 return( PSA_ERROR_INVALID_ARGUMENT );
4199 status = psa_get_key_from_slot( private_key, &slot,
4200 PSA_KEY_USAGE_DERIVE, alg );
4201 if( status != PSA_SUCCESS )
4202 return( status );
Gilles Peskine346797d2018-11-16 16:05:06 +01004203 status = psa_key_agreement_internal( generator,
4204 slot,
4205 peer_key, peer_key_length,
4206 alg );
4207 if( status != PSA_SUCCESS )
4208 psa_generator_abort( generator );
4209 return( status );
Gilles Peskine01d718c2018-09-18 12:01:02 +02004210}
4211
4212
4213
4214/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004215/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02004216/****************************************************************/
4217
4218psa_status_t psa_generate_random( uint8_t *output,
4219 size_t output_size )
4220{
itayzafrir0adf0fc2018-09-06 16:24:41 +03004221 int ret;
4222 GUARD_MODULE_INITIALIZED;
4223
4224 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02004225 return( mbedtls_to_psa_error( ret ) );
4226}
4227
4228psa_status_t psa_generate_key( psa_key_slot_t key,
4229 psa_key_type_t type,
4230 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02004231 const void *extra,
4232 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02004233{
Gilles Peskine12313cd2018-06-20 00:20:32 +02004234 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004235 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004236
Gilles Peskine53d991e2018-07-12 01:14:59 +02004237 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004238 return( PSA_ERROR_INVALID_ARGUMENT );
4239
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004240 status = psa_get_empty_key_slot( key, &slot );
4241 if( status != PSA_SUCCESS )
4242 return( status );
4243
Gilles Peskine48c0ea12018-06-21 14:15:31 +02004244 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004245 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004246 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004247 if( status != PSA_SUCCESS )
4248 return( status );
4249 status = psa_generate_random( slot->data.raw.data,
4250 slot->data.raw.bytes );
4251 if( status != PSA_SUCCESS )
4252 {
4253 mbedtls_free( slot->data.raw.data );
4254 return( status );
4255 }
4256#if defined(MBEDTLS_DES_C)
4257 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004258 psa_des_set_key_parity( slot->data.raw.data,
4259 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02004260#endif /* MBEDTLS_DES_C */
4261 }
4262 else
4263
4264#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
4265 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
4266 {
4267 mbedtls_rsa_context *rsa;
4268 int ret;
4269 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02004270 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
4271 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine86a440b2018-11-12 18:39:40 +01004272 /* Accept only byte-aligned keys, for the same reasons as
4273 * in psa_import_rsa_key(). */
4274 if( bits % 8 != 0 )
4275 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02004276 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004277 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02004278 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004279 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004280 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02004281#if INT_MAX < 0xffffffff
4282 /* Check that the uint32_t value passed by the caller fits
4283 * in the range supported by this implementation. */
4284 if( p->e > INT_MAX )
4285 return( PSA_ERROR_NOT_SUPPORTED );
4286#endif
4287 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02004288 }
4289 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
4290 if( rsa == NULL )
4291 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4292 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
4293 ret = mbedtls_rsa_gen_key( rsa,
4294 mbedtls_ctr_drbg_random,
4295 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004296 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02004297 exponent );
4298 if( ret != 0 )
4299 {
4300 mbedtls_rsa_free( rsa );
4301 mbedtls_free( rsa );
4302 return( mbedtls_to_psa_error( ret ) );
4303 }
4304 slot->data.rsa = rsa;
4305 }
4306 else
4307#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
4308
4309#if defined(MBEDTLS_ECP_C)
4310 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
4311 {
4312 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
4313 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
4314 const mbedtls_ecp_curve_info *curve_info =
4315 mbedtls_ecp_curve_info_from_grp_id( grp_id );
4316 mbedtls_ecp_keypair *ecp;
4317 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02004318 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02004319 return( PSA_ERROR_NOT_SUPPORTED );
4320 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
4321 return( PSA_ERROR_NOT_SUPPORTED );
4322 if( curve_info->bit_size != bits )
4323 return( PSA_ERROR_INVALID_ARGUMENT );
4324 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
4325 if( ecp == NULL )
4326 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4327 mbedtls_ecp_keypair_init( ecp );
4328 ret = mbedtls_ecp_gen_key( grp_id, ecp,
4329 mbedtls_ctr_drbg_random,
4330 &global_data.ctr_drbg );
4331 if( ret != 0 )
4332 {
4333 mbedtls_ecp_keypair_free( ecp );
4334 mbedtls_free( ecp );
4335 return( mbedtls_to_psa_error( ret ) );
4336 }
4337 slot->data.ecp = ecp;
4338 }
4339 else
4340#endif /* MBEDTLS_ECP_C */
4341
4342 return( PSA_ERROR_NOT_SUPPORTED );
4343
4344 slot->type = type;
Darryl Green0c6575a2018-11-07 16:05:30 +00004345
4346#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
4347 if( slot->lifetime == PSA_KEY_LIFETIME_PERSISTENT )
4348 {
4349 return( psa_save_generated_persistent_key( key, slot, bits ) );
4350 }
4351#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
4352
4353 return( status );
Gilles Peskine05d69892018-06-19 22:00:52 +02004354}
4355
4356
4357/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01004358/* Module setup */
4359/****************************************************************/
4360
Gilles Peskinee59236f2018-01-27 23:32:46 +01004361void mbedtls_psa_crypto_free( void )
4362{
Jaeden Amero045bd502018-06-26 14:00:08 +01004363 psa_key_slot_t key;
Darryl Green40225ba2018-11-15 14:48:15 +00004364 key_slot_t *slot;
4365 psa_status_t status;
4366
Gilles Peskine9a056342018-08-01 15:46:54 +02004367 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Darryl Green40225ba2018-11-15 14:48:15 +00004368 {
4369 status = psa_get_key_slot( key, &slot );
4370 if( status != PSA_SUCCESS )
4371 continue;
4372 psa_remove_key_data_from_memory( slot );
4373 /* Zeroize the slot to wipe metadata such as policies. */
4374 mbedtls_zeroize( slot, sizeof( *slot ) );
4375 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01004376 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
4377 mbedtls_entropy_free( &global_data.entropy );
4378 mbedtls_zeroize( &global_data, sizeof( global_data ) );
4379}
4380
4381psa_status_t psa_crypto_init( void )
4382{
4383 int ret;
4384 const unsigned char drbg_seed[] = "PSA";
4385
4386 if( global_data.initialized != 0 )
4387 return( PSA_SUCCESS );
4388
4389 mbedtls_zeroize( &global_data, sizeof( global_data ) );
4390 mbedtls_entropy_init( &global_data.entropy );
4391 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
4392
4393 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
4394 mbedtls_entropy_func,
4395 &global_data.entropy,
4396 drbg_seed, sizeof( drbg_seed ) - 1 );
4397 if( ret != 0 )
4398 goto exit;
4399
Gilles Peskinee4ebc122018-03-07 14:16:44 +01004400 global_data.initialized = 1;
4401
Gilles Peskinee59236f2018-01-27 23:32:46 +01004402exit:
4403 if( ret != 0 )
4404 mbedtls_psa_crypto_free( );
4405 return( mbedtls_to_psa_error( ret ) );
4406}
4407
4408#endif /* MBEDTLS_PSA_CRYPTO_C */