blob: a62fac7b1d8d900ea1f38a706175a2484374255e [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
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include <stdlib.h>
47#include <string.h>
48#if defined(MBEDTLS_PLATFORM_C)
49#include "mbedtls/platform.h"
50#else
51#define mbedtls_calloc calloc
52#define mbedtls_free free
53#endif
54
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020056#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020057#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/blowfish.h"
59#include "mbedtls/camellia.h"
60#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010065#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010066#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/error.h"
68#include "mbedtls/gcm.h"
69#include "mbedtls/md2.h"
70#include "mbedtls/md4.h"
71#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010072#include "mbedtls/md.h"
73#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010074#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/pk_internal.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010077#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/sha1.h"
79#include "mbedtls/sha256.h"
80#include "mbedtls/sha512.h"
81#include "mbedtls/xtea.h"
82
Gilles Peskinee59236f2018-01-27 23:32:46 +010083
84
85/* Implementation that should never be optimized out by the compiler */
86static void mbedtls_zeroize( void *v, size_t n )
87{
88 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
89}
90
Gilles Peskine9ef733f2018-02-07 21:05:37 +010091/* constant-time buffer comparison */
92static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
93{
94 size_t i;
95 unsigned char diff = 0;
96
97 for( i = 0; i < n; i++ )
98 diff |= a[i] ^ b[i];
99
100 return( diff );
101}
102
103
104
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100105/****************************************************************/
106/* Global data, support functions and library management */
107/****************************************************************/
108
109/* Number of key slots (plus one because 0 is not used).
110 * The value is a compile-time constant for now, for simplicity. */
Gilles Peskine828ed142018-06-18 23:25:51 +0200111#define PSA_KEY_SLOT_COUNT 32
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100112
Gilles Peskine2d277862018-06-18 15:41:12 +0200113typedef struct
114{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100115 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +0300116 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200117 psa_key_lifetime_t lifetime;
Gilles Peskine2d277862018-06-18 15:41:12 +0200118 union
119 {
120 struct raw_data
121 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100122 uint8_t *data;
123 size_t bytes;
124 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100125#if defined(MBEDTLS_RSA_C)
126 mbedtls_rsa_context *rsa;
127#endif /* MBEDTLS_RSA_C */
128#if defined(MBEDTLS_ECP_C)
129 mbedtls_ecp_keypair *ecp;
130#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100131 } data;
132} key_slot_t;
133
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200134static int key_type_is_raw_bytes( psa_key_type_t type )
135{
136 psa_key_type_t category = type & PSA_KEY_TYPE_CATEGORY_MASK;
137 return( category == PSA_KEY_TYPE_RAW_DATA ||
138 category == PSA_KEY_TYPE_CATEGORY_SYMMETRIC );
139}
140
Gilles Peskine2d277862018-06-18 15:41:12 +0200141typedef struct
142{
Gilles Peskinee59236f2018-01-27 23:32:46 +0100143 int initialized;
144 mbedtls_entropy_context entropy;
145 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200146 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinee59236f2018-01-27 23:32:46 +0100147} psa_global_data_t;
148
149static psa_global_data_t global_data;
150
151static psa_status_t mbedtls_to_psa_error( int ret )
152{
Gilles Peskinea5905292018-02-07 20:59:33 +0100153 /* If there's both a high-level code and low-level code, dispatch on
154 * the high-level code. */
155 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100156 {
157 case 0:
158 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100159
160 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
161 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
162 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
163 return( PSA_ERROR_NOT_SUPPORTED );
164 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
165 return( PSA_ERROR_HARDWARE_FAILURE );
166
167 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
168 return( PSA_ERROR_HARDWARE_FAILURE );
169
Gilles Peskine9a944802018-06-21 09:35:35 +0200170 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
171 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
172 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
173 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
174 case MBEDTLS_ERR_ASN1_INVALID_DATA:
175 return( PSA_ERROR_INVALID_ARGUMENT );
176 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
177 return( PSA_ERROR_INSUFFICIENT_MEMORY );
178 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
179 return( PSA_ERROR_BUFFER_TOO_SMALL );
180
Gilles Peskinea5905292018-02-07 20:59:33 +0100181 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
182 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
183 return( PSA_ERROR_NOT_SUPPORTED );
184 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
185 return( PSA_ERROR_HARDWARE_FAILURE );
186
187 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
188 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
189 return( PSA_ERROR_NOT_SUPPORTED );
190 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
191 return( PSA_ERROR_HARDWARE_FAILURE );
192
193 case MBEDTLS_ERR_CCM_BAD_INPUT:
194 return( PSA_ERROR_INVALID_ARGUMENT );
195 case MBEDTLS_ERR_CCM_AUTH_FAILED:
196 return( PSA_ERROR_INVALID_SIGNATURE );
197 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
198 return( PSA_ERROR_HARDWARE_FAILURE );
199
200 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
201 return( PSA_ERROR_NOT_SUPPORTED );
202 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
203 return( PSA_ERROR_INVALID_ARGUMENT );
204 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
205 return( PSA_ERROR_INSUFFICIENT_MEMORY );
206 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
207 return( PSA_ERROR_INVALID_PADDING );
208 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
209 return( PSA_ERROR_BAD_STATE );
210 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
211 return( PSA_ERROR_INVALID_SIGNATURE );
212 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
213 return( PSA_ERROR_TAMPERING_DETECTED );
214 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
215 return( PSA_ERROR_HARDWARE_FAILURE );
216
217 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
218 return( PSA_ERROR_HARDWARE_FAILURE );
219
220 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
221 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
222 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
223 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
224 return( PSA_ERROR_NOT_SUPPORTED );
225 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
226 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
227
228 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
229 return( PSA_ERROR_NOT_SUPPORTED );
230 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
231 return( PSA_ERROR_HARDWARE_FAILURE );
232
Gilles Peskinee59236f2018-01-27 23:32:46 +0100233 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
234 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
235 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
236 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100237
238 case MBEDTLS_ERR_GCM_AUTH_FAILED:
239 return( PSA_ERROR_INVALID_SIGNATURE );
240 case MBEDTLS_ERR_GCM_BAD_INPUT:
241 return( PSA_ERROR_NOT_SUPPORTED );
242 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
243 return( PSA_ERROR_HARDWARE_FAILURE );
244
245 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
246 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
247 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
248 return( PSA_ERROR_HARDWARE_FAILURE );
249
250 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
251 return( PSA_ERROR_NOT_SUPPORTED );
252 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
253 return( PSA_ERROR_INVALID_ARGUMENT );
254 case MBEDTLS_ERR_MD_ALLOC_FAILED:
255 return( PSA_ERROR_INSUFFICIENT_MEMORY );
256 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
257 return( PSA_ERROR_STORAGE_FAILURE );
258 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
259 return( PSA_ERROR_HARDWARE_FAILURE );
260
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100261 case MBEDTLS_ERR_PK_ALLOC_FAILED:
262 return( PSA_ERROR_INSUFFICIENT_MEMORY );
263 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
264 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100267 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100268 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
269 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
272 return( PSA_ERROR_NOT_SUPPORTED );
273 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
274 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
275 return( PSA_ERROR_NOT_PERMITTED );
276 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_PK_INVALID_ALG:
279 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
280 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
281 return( PSA_ERROR_NOT_SUPPORTED );
282 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
283 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100284 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
285 return( PSA_ERROR_HARDWARE_FAILURE );
286
287 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
288 return( PSA_ERROR_HARDWARE_FAILURE );
289
290 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_RSA_INVALID_PADDING:
293 return( PSA_ERROR_INVALID_PADDING );
294 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
297 return( PSA_ERROR_INVALID_ARGUMENT );
298 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
299 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
300 return( PSA_ERROR_TAMPERING_DETECTED );
301 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
302 return( PSA_ERROR_INVALID_SIGNATURE );
303 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
304 return( PSA_ERROR_BUFFER_TOO_SMALL );
305 case MBEDTLS_ERR_RSA_RNG_FAILED:
306 return( PSA_ERROR_INSUFFICIENT_MEMORY );
307 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
308 return( PSA_ERROR_NOT_SUPPORTED );
309 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
310 return( PSA_ERROR_HARDWARE_FAILURE );
311
312 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
313 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
314 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
315 return( PSA_ERROR_HARDWARE_FAILURE );
316
317 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
318 return( PSA_ERROR_INVALID_ARGUMENT );
319 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
320 return( PSA_ERROR_HARDWARE_FAILURE );
321
itayzafrir5c753392018-05-08 11:18:38 +0300322 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300323 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300324 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300325 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
326 return( PSA_ERROR_BUFFER_TOO_SMALL );
327 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
328 return( PSA_ERROR_NOT_SUPPORTED );
329 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
330 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
331 return( PSA_ERROR_INVALID_SIGNATURE );
332 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
333 return( PSA_ERROR_INSUFFICIENT_MEMORY );
334 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
335 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300336
Gilles Peskinee59236f2018-01-27 23:32:46 +0100337 default:
338 return( PSA_ERROR_UNKNOWN_ERROR );
339 }
340}
341
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200342/* Retrieve a key slot, occupied or not. */
343static psa_status_t psa_get_key_slot( psa_key_slot_t key,
344 key_slot_t **p_slot )
345{
346 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
347 return( PSA_ERROR_INVALID_ARGUMENT );
348
349 *p_slot = &global_data.key_slots[key];
350 return( PSA_SUCCESS );
351}
352
353/* Retrieve an empty key slot (slot with no key data, but possibly
354 * with some metadata such as a policy). */
355static psa_status_t psa_get_empty_key_slot( psa_key_slot_t key,
356 key_slot_t **p_slot )
357{
358 psa_status_t status;
359 key_slot_t *slot = NULL;
360
361 *p_slot = NULL;
362
363 status = psa_get_key_slot( key, &slot );
364 if( status != PSA_SUCCESS )
365 return( status );
366
367 if( slot->type != PSA_KEY_TYPE_NONE )
368 return( PSA_ERROR_OCCUPIED_SLOT );
369
370 *p_slot = slot;
371 return( status );
372}
373
Jaeden Amerob4fa8c92018-07-11 15:57:44 +0100374/** Retrieve a slot which must contain a key. The key must have allow all the
375 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
376 * operations with this algorithm. */
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200377static psa_status_t psa_get_key_from_slot( psa_key_slot_t key,
378 key_slot_t **p_slot,
379 psa_key_usage_t usage,
380 psa_algorithm_t alg )
381{
382 psa_status_t status;
383 key_slot_t *slot = NULL;
384
385 *p_slot = NULL;
386
387 status = psa_get_key_slot( key, &slot );
388 if( status != PSA_SUCCESS )
389 return( status );
390 if( slot->type == PSA_KEY_TYPE_NONE )
391 return( PSA_ERROR_EMPTY_SLOT );
392
393 /* Enforce that usage policy for the key slot contains all the flags
394 * required by the usage parameter. There is one exception: public
395 * keys can always be exported, so we treat public key objects as
396 * if they had the export flag. */
397 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
398 usage &= ~PSA_KEY_USAGE_EXPORT;
399 if( ( slot->policy.usage & usage ) != usage )
400 return( PSA_ERROR_NOT_PERMITTED );
401 if( alg != 0 && ( alg != slot->policy.alg ) )
402 return( PSA_ERROR_NOT_PERMITTED );
403
404 *p_slot = slot;
405 return( PSA_SUCCESS );
406}
407
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200408
409
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100410/****************************************************************/
411/* Key management */
412/****************************************************************/
413
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200414static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
415{
416 switch( grpid )
417 {
418 case MBEDTLS_ECP_DP_SECP192R1:
419 return( PSA_ECC_CURVE_SECP192R1 );
420 case MBEDTLS_ECP_DP_SECP224R1:
421 return( PSA_ECC_CURVE_SECP224R1 );
422 case MBEDTLS_ECP_DP_SECP256R1:
423 return( PSA_ECC_CURVE_SECP256R1 );
424 case MBEDTLS_ECP_DP_SECP384R1:
425 return( PSA_ECC_CURVE_SECP384R1 );
426 case MBEDTLS_ECP_DP_SECP521R1:
427 return( PSA_ECC_CURVE_SECP521R1 );
428 case MBEDTLS_ECP_DP_BP256R1:
429 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
430 case MBEDTLS_ECP_DP_BP384R1:
431 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
432 case MBEDTLS_ECP_DP_BP512R1:
433 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
434 case MBEDTLS_ECP_DP_CURVE25519:
435 return( PSA_ECC_CURVE_CURVE25519 );
436 case MBEDTLS_ECP_DP_SECP192K1:
437 return( PSA_ECC_CURVE_SECP192K1 );
438 case MBEDTLS_ECP_DP_SECP224K1:
439 return( PSA_ECC_CURVE_SECP224K1 );
440 case MBEDTLS_ECP_DP_SECP256K1:
441 return( PSA_ECC_CURVE_SECP256K1 );
442 case MBEDTLS_ECP_DP_CURVE448:
443 return( PSA_ECC_CURVE_CURVE448 );
444 default:
445 return( 0 );
446 }
447}
448
Gilles Peskine12313cd2018-06-20 00:20:32 +0200449static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
450{
451 switch( curve )
452 {
453 case PSA_ECC_CURVE_SECP192R1:
454 return( MBEDTLS_ECP_DP_SECP192R1 );
455 case PSA_ECC_CURVE_SECP224R1:
456 return( MBEDTLS_ECP_DP_SECP224R1 );
457 case PSA_ECC_CURVE_SECP256R1:
458 return( MBEDTLS_ECP_DP_SECP256R1 );
459 case PSA_ECC_CURVE_SECP384R1:
460 return( MBEDTLS_ECP_DP_SECP384R1 );
461 case PSA_ECC_CURVE_SECP521R1:
462 return( MBEDTLS_ECP_DP_SECP521R1 );
463 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
464 return( MBEDTLS_ECP_DP_BP256R1 );
465 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
466 return( MBEDTLS_ECP_DP_BP384R1 );
467 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
468 return( MBEDTLS_ECP_DP_BP512R1 );
469 case PSA_ECC_CURVE_CURVE25519:
470 return( MBEDTLS_ECP_DP_CURVE25519 );
471 case PSA_ECC_CURVE_SECP192K1:
472 return( MBEDTLS_ECP_DP_SECP192K1 );
473 case PSA_ECC_CURVE_SECP224K1:
474 return( MBEDTLS_ECP_DP_SECP224K1 );
475 case PSA_ECC_CURVE_SECP256K1:
476 return( MBEDTLS_ECP_DP_SECP256K1 );
477 case PSA_ECC_CURVE_CURVE448:
478 return( MBEDTLS_ECP_DP_CURVE448 );
479 default:
480 return( MBEDTLS_ECP_DP_NONE );
481 }
482}
483
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200484static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
485 size_t bits,
486 struct raw_data *raw )
487{
488 /* Check that the bit size is acceptable for the key type */
489 switch( type )
490 {
491 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200492 if( bits == 0 )
493 {
494 raw->bytes = 0;
495 raw->data = NULL;
496 return( PSA_SUCCESS );
497 }
498 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200499#if defined(MBEDTLS_MD_C)
500 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200501#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200502 case PSA_KEY_TYPE_DERIVE:
503 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200504#if defined(MBEDTLS_AES_C)
505 case PSA_KEY_TYPE_AES:
506 if( bits != 128 && bits != 192 && bits != 256 )
507 return( PSA_ERROR_INVALID_ARGUMENT );
508 break;
509#endif
510#if defined(MBEDTLS_CAMELLIA_C)
511 case PSA_KEY_TYPE_CAMELLIA:
512 if( bits != 128 && bits != 192 && bits != 256 )
513 return( PSA_ERROR_INVALID_ARGUMENT );
514 break;
515#endif
516#if defined(MBEDTLS_DES_C)
517 case PSA_KEY_TYPE_DES:
518 if( bits != 64 && bits != 128 && bits != 192 )
519 return( PSA_ERROR_INVALID_ARGUMENT );
520 break;
521#endif
522#if defined(MBEDTLS_ARC4_C)
523 case PSA_KEY_TYPE_ARC4:
524 if( bits < 8 || bits > 2048 )
525 return( PSA_ERROR_INVALID_ARGUMENT );
526 break;
527#endif
528 default:
529 return( PSA_ERROR_NOT_SUPPORTED );
530 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200531 if( bits % 8 != 0 )
532 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200533
534 /* Allocate memory for the key */
535 raw->bytes = PSA_BITS_TO_BYTES( bits );
536 raw->data = mbedtls_calloc( 1, raw->bytes );
537 if( raw->data == NULL )
538 {
539 raw->bytes = 0;
540 return( PSA_ERROR_INSUFFICIENT_MEMORY );
541 }
542 return( PSA_SUCCESS );
543}
544
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200545#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
546static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
547 mbedtls_rsa_context **p_rsa )
548{
549 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
550 return( PSA_ERROR_INVALID_ARGUMENT );
551 else
552 {
553 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
554 size_t bits = mbedtls_rsa_get_bitlen( rsa );
555 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
556 return( PSA_ERROR_NOT_SUPPORTED );
557 *p_rsa = rsa;
558 return( PSA_SUCCESS );
559 }
560}
561#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
562
563#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
564static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
565 mbedtls_pk_context *pk,
566 mbedtls_ecp_keypair **p_ecp )
567{
568 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
569 return( PSA_ERROR_INVALID_ARGUMENT );
570 else
571 {
572 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
573 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
574 if( actual_curve != expected_curve )
575 return( PSA_ERROR_INVALID_ARGUMENT );
576 *p_ecp = ecp;
577 return( PSA_SUCCESS );
578 }
579}
580#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
581
Gilles Peskine2d277862018-06-18 15:41:12 +0200582psa_status_t psa_import_key( psa_key_slot_t key,
583 psa_key_type_t type,
584 const uint8_t *data,
585 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100586{
587 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200588 psa_status_t status = PSA_SUCCESS;
589 status = psa_get_empty_key_slot( key, &slot );
590 if( status != PSA_SUCCESS )
591 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100592
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200593 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100594 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100595 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100596 if( data_length > SIZE_MAX / 8 )
597 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200598 status = prepare_raw_data_slot( type,
599 PSA_BYTES_TO_BITS( data_length ),
600 &slot->data.raw );
601 if( status != PSA_SUCCESS )
602 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200603 if( data_length != 0 )
604 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100605 }
606 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100607#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200608 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100609 {
610 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100611 mbedtls_pk_context pk;
612 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200613
614 /* Parse the data. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100615 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
616 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
617 else
618 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100619 if( ret != 0 )
620 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200621
622 /* We have something that the pkparse module recognizes.
623 * If it has the expected type and passes any type-specific
624 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100625#if defined(MBEDTLS_RSA_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200626 if( PSA_KEY_TYPE_IS_RSA( type ) )
627 status = psa_import_rsa_key( &pk, &slot->data.rsa );
628 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100629#endif /* MBEDTLS_RSA_C */
630#if defined(MBEDTLS_ECP_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200631 if( PSA_KEY_TYPE_IS_ECC( type ) )
632 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( type ),
633 &pk, &slot->data.ecp );
634 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100635#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200636 {
637 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200638 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200639
Gilles Peskinec648d692018-06-28 08:46:13 +0200640 /* Free the content of the pk object only on error. On success,
641 * the content of the object has been stored in the slot. */
642 if( status != PSA_SUCCESS )
643 {
644 mbedtls_pk_free( &pk );
645 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100646 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100647 }
648 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100649#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100650 {
651 return( PSA_ERROR_NOT_SUPPORTED );
652 }
653
654 slot->type = type;
655 return( PSA_SUCCESS );
656}
657
Gilles Peskine2d277862018-06-18 15:41:12 +0200658psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100659{
660 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200661 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100662
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200663 status = psa_get_key_slot( key, &slot );
664 if( status != PSA_SUCCESS )
665 return( status );
666
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100667 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200668 {
669 /* No key material to clean, but do zeroize the slot below to wipe
670 * metadata such as policies. */
671 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200672 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673 {
674 mbedtls_free( slot->data.raw.data );
675 }
676 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100677#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200678 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100679 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100680 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100681 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100682 }
683 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100684#endif /* defined(MBEDTLS_RSA_C) */
685#if defined(MBEDTLS_ECP_C)
686 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
687 {
688 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100689 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100690 }
691 else
692#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100693 {
694 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100695 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100696 return( PSA_ERROR_TAMPERING_DETECTED );
697 }
698
699 mbedtls_zeroize( slot, sizeof( *slot ) );
700 return( PSA_SUCCESS );
701}
702
Gilles Peskineb870b182018-07-06 16:02:09 +0200703/* Return the size of the key in the given slot, in bits. */
704static size_t psa_get_key_bits( const key_slot_t *slot )
705{
706 if( key_type_is_raw_bytes( slot->type ) )
707 return( slot->data.raw.bytes * 8 );
708#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200709 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineb870b182018-07-06 16:02:09 +0200710 return( mbedtls_rsa_get_bitlen( slot->data.rsa ) );
711#endif /* defined(MBEDTLS_RSA_C) */
712#if defined(MBEDTLS_ECP_C)
713 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
714 return( slot->data.ecp->grp.pbits );
715#endif /* defined(MBEDTLS_ECP_C) */
716 /* Shouldn't happen except on an empty slot. */
717 return( 0 );
718}
719
Gilles Peskine2d277862018-06-18 15:41:12 +0200720psa_status_t psa_get_key_information( psa_key_slot_t key,
721 psa_key_type_t *type,
722 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100723{
724 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200725 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100726
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100727 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200728 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100729 if( bits != NULL )
730 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200731 status = psa_get_key_slot( key, &slot );
732 if( status != PSA_SUCCESS )
733 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200734
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100735 if( slot->type == PSA_KEY_TYPE_NONE )
736 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200737 if( type != NULL )
738 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200739 if( bits != NULL )
740 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100741 return( PSA_SUCCESS );
742}
743
Gilles Peskine2d277862018-06-18 15:41:12 +0200744static psa_status_t psa_internal_export_key( psa_key_slot_t key,
745 uint8_t *data,
746 size_t data_size,
747 size_t *data_length,
748 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100749{
750 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200751 psa_status_t status;
752 /* Exporting a public key doesn't require a usage flag. If we're
753 * called by psa_export_public_key(), don't require the EXPORT flag.
754 * If we're called by psa_export_key(), do require the EXPORT flag;
755 * if the key turns out to be public key object, psa_get_key_from_slot()
756 * will ignore this flag. */
757 psa_key_usage_t usage = export_public_key ? 0 : PSA_KEY_USAGE_EXPORT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100758
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100759 /* Set the key to empty now, so that even when there are errors, we always
760 * set data_length to a value between 0 and data_size. On error, setting
761 * the key to empty is a good choice because an empty key representation is
762 * unlikely to be accepted anywhere. */
763 *data_length = 0;
764
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200765 status = psa_get_key_from_slot( key, &slot, usage, 0 );
766 if( status != PSA_SUCCESS )
767 return( status );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200768 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300769 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300770
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200771 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100772 {
773 if( slot->data.raw.bytes > data_size )
774 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200775 if( slot->data.raw.bytes != 0 )
776 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100777 *data_length = slot->data.raw.bytes;
778 return( PSA_SUCCESS );
779 }
780 else
Moran Peker17e36e12018-05-02 12:55:20 +0300781 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100782#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200783 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300784 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100785 {
Moran Pekera998bc62018-04-16 18:16:20 +0300786 mbedtls_pk_context pk;
787 int ret;
788 mbedtls_pk_init( &pk );
Gilles Peskined8008d62018-06-29 19:51:51 +0200789 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300790 {
791 pk.pk_info = &mbedtls_rsa_info;
792 pk.pk_ctx = slot->data.rsa;
793 }
794 else
795 {
796 pk.pk_info = &mbedtls_eckey_info;
797 pk.pk_ctx = slot->data.ecp;
798 }
Moran Pekerd7326592018-05-29 16:56:39 +0300799 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300800 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300801 else
802 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300803 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200804 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200805 /* If data_size is 0 then data may be NULL and then the
806 * call to memset would have undefined behavior. */
807 if( data_size != 0 )
808 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300809 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200810 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200811 /* The mbedtls_pk_xxx functions write to the end of the buffer.
812 * Move the data to the beginning and erase remaining data
813 * at the original location. */
814 if( 2 * (size_t) ret <= data_size )
815 {
816 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200817 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200818 }
819 else if( (size_t) ret < data_size )
820 {
821 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200822 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200823 }
Moran Pekera998bc62018-04-16 18:16:20 +0300824 *data_length = ret;
825 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100826 }
827 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100828#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300829 {
830 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200831 it is valid for a special-purpose implementation to omit
832 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300833 return( PSA_ERROR_NOT_SUPPORTED );
834 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100835 }
836}
837
Gilles Peskine2d277862018-06-18 15:41:12 +0200838psa_status_t psa_export_key( psa_key_slot_t key,
839 uint8_t *data,
840 size_t data_size,
841 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300842{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200843 return( psa_internal_export_key( key, data, data_size,
844 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100845}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100846
Gilles Peskine2d277862018-06-18 15:41:12 +0200847psa_status_t psa_export_public_key( psa_key_slot_t key,
848 uint8_t *data,
849 size_t data_size,
850 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300851{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200852 return( psa_internal_export_key( key, data, data_size,
853 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300854}
855
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200856
857
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100858/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100859/* Message digests */
860/****************************************************************/
861
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100862static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100863{
864 switch( alg )
865 {
866#if defined(MBEDTLS_MD2_C)
867 case PSA_ALG_MD2:
868 return( &mbedtls_md2_info );
869#endif
870#if defined(MBEDTLS_MD4_C)
871 case PSA_ALG_MD4:
872 return( &mbedtls_md4_info );
873#endif
874#if defined(MBEDTLS_MD5_C)
875 case PSA_ALG_MD5:
876 return( &mbedtls_md5_info );
877#endif
878#if defined(MBEDTLS_RIPEMD160_C)
879 case PSA_ALG_RIPEMD160:
880 return( &mbedtls_ripemd160_info );
881#endif
882#if defined(MBEDTLS_SHA1_C)
883 case PSA_ALG_SHA_1:
884 return( &mbedtls_sha1_info );
885#endif
886#if defined(MBEDTLS_SHA256_C)
887 case PSA_ALG_SHA_224:
888 return( &mbedtls_sha224_info );
889 case PSA_ALG_SHA_256:
890 return( &mbedtls_sha256_info );
891#endif
892#if defined(MBEDTLS_SHA512_C)
893 case PSA_ALG_SHA_384:
894 return( &mbedtls_sha384_info );
895 case PSA_ALG_SHA_512:
896 return( &mbedtls_sha512_info );
897#endif
898 default:
899 return( NULL );
900 }
901}
902
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100903psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
904{
905 switch( operation->alg )
906 {
Gilles Peskine81736312018-06-26 15:04:31 +0200907 case 0:
908 /* The object has (apparently) been initialized but it is not
909 * in use. It's ok to call abort on such an object, and there's
910 * nothing to do. */
911 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100912#if defined(MBEDTLS_MD2_C)
913 case PSA_ALG_MD2:
914 mbedtls_md2_free( &operation->ctx.md2 );
915 break;
916#endif
917#if defined(MBEDTLS_MD4_C)
918 case PSA_ALG_MD4:
919 mbedtls_md4_free( &operation->ctx.md4 );
920 break;
921#endif
922#if defined(MBEDTLS_MD5_C)
923 case PSA_ALG_MD5:
924 mbedtls_md5_free( &operation->ctx.md5 );
925 break;
926#endif
927#if defined(MBEDTLS_RIPEMD160_C)
928 case PSA_ALG_RIPEMD160:
929 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
930 break;
931#endif
932#if defined(MBEDTLS_SHA1_C)
933 case PSA_ALG_SHA_1:
934 mbedtls_sha1_free( &operation->ctx.sha1 );
935 break;
936#endif
937#if defined(MBEDTLS_SHA256_C)
938 case PSA_ALG_SHA_224:
939 case PSA_ALG_SHA_256:
940 mbedtls_sha256_free( &operation->ctx.sha256 );
941 break;
942#endif
943#if defined(MBEDTLS_SHA512_C)
944 case PSA_ALG_SHA_384:
945 case PSA_ALG_SHA_512:
946 mbedtls_sha512_free( &operation->ctx.sha512 );
947 break;
948#endif
949 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +0200950 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100951 }
952 operation->alg = 0;
953 return( PSA_SUCCESS );
954}
955
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200956psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100957 psa_algorithm_t alg )
958{
959 int ret;
960 operation->alg = 0;
961 switch( alg )
962 {
963#if defined(MBEDTLS_MD2_C)
964 case PSA_ALG_MD2:
965 mbedtls_md2_init( &operation->ctx.md2 );
966 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
967 break;
968#endif
969#if defined(MBEDTLS_MD4_C)
970 case PSA_ALG_MD4:
971 mbedtls_md4_init( &operation->ctx.md4 );
972 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
973 break;
974#endif
975#if defined(MBEDTLS_MD5_C)
976 case PSA_ALG_MD5:
977 mbedtls_md5_init( &operation->ctx.md5 );
978 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
979 break;
980#endif
981#if defined(MBEDTLS_RIPEMD160_C)
982 case PSA_ALG_RIPEMD160:
983 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
984 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
985 break;
986#endif
987#if defined(MBEDTLS_SHA1_C)
988 case PSA_ALG_SHA_1:
989 mbedtls_sha1_init( &operation->ctx.sha1 );
990 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
991 break;
992#endif
993#if defined(MBEDTLS_SHA256_C)
994 case PSA_ALG_SHA_224:
995 mbedtls_sha256_init( &operation->ctx.sha256 );
996 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
997 break;
998 case PSA_ALG_SHA_256:
999 mbedtls_sha256_init( &operation->ctx.sha256 );
1000 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1001 break;
1002#endif
1003#if defined(MBEDTLS_SHA512_C)
1004 case PSA_ALG_SHA_384:
1005 mbedtls_sha512_init( &operation->ctx.sha512 );
1006 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1007 break;
1008 case PSA_ALG_SHA_512:
1009 mbedtls_sha512_init( &operation->ctx.sha512 );
1010 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1011 break;
1012#endif
1013 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001014 return( PSA_ALG_IS_HASH( alg ) ?
1015 PSA_ERROR_NOT_SUPPORTED :
1016 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001017 }
1018 if( ret == 0 )
1019 operation->alg = alg;
1020 else
1021 psa_hash_abort( operation );
1022 return( mbedtls_to_psa_error( ret ) );
1023}
1024
1025psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1026 const uint8_t *input,
1027 size_t input_length )
1028{
1029 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001030
1031 /* Don't require hash implementations to behave correctly on a
1032 * zero-length input, which may have an invalid pointer. */
1033 if( input_length == 0 )
1034 return( PSA_SUCCESS );
1035
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001036 switch( operation->alg )
1037 {
1038#if defined(MBEDTLS_MD2_C)
1039 case PSA_ALG_MD2:
1040 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1041 input, input_length );
1042 break;
1043#endif
1044#if defined(MBEDTLS_MD4_C)
1045 case PSA_ALG_MD4:
1046 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1047 input, input_length );
1048 break;
1049#endif
1050#if defined(MBEDTLS_MD5_C)
1051 case PSA_ALG_MD5:
1052 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1053 input, input_length );
1054 break;
1055#endif
1056#if defined(MBEDTLS_RIPEMD160_C)
1057 case PSA_ALG_RIPEMD160:
1058 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1059 input, input_length );
1060 break;
1061#endif
1062#if defined(MBEDTLS_SHA1_C)
1063 case PSA_ALG_SHA_1:
1064 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1065 input, input_length );
1066 break;
1067#endif
1068#if defined(MBEDTLS_SHA256_C)
1069 case PSA_ALG_SHA_224:
1070 case PSA_ALG_SHA_256:
1071 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1072 input, input_length );
1073 break;
1074#endif
1075#if defined(MBEDTLS_SHA512_C)
1076 case PSA_ALG_SHA_384:
1077 case PSA_ALG_SHA_512:
1078 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1079 input, input_length );
1080 break;
1081#endif
1082 default:
1083 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1084 break;
1085 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001086
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001087 if( ret != 0 )
1088 psa_hash_abort( operation );
1089 return( mbedtls_to_psa_error( ret ) );
1090}
1091
1092psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1093 uint8_t *hash,
1094 size_t hash_size,
1095 size_t *hash_length )
1096{
1097 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001098 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001099
1100 /* Fill the output buffer with something that isn't a valid hash
1101 * (barring an attack on the hash and deliberately-crafted input),
1102 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001103 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001104 /* If hash_size is 0 then hash may be NULL and then the
1105 * call to memset would have undefined behavior. */
1106 if( hash_size != 0 )
1107 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001108
1109 if( hash_size < actual_hash_length )
1110 return( PSA_ERROR_BUFFER_TOO_SMALL );
1111
1112 switch( operation->alg )
1113 {
1114#if defined(MBEDTLS_MD2_C)
1115 case PSA_ALG_MD2:
1116 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1117 break;
1118#endif
1119#if defined(MBEDTLS_MD4_C)
1120 case PSA_ALG_MD4:
1121 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1122 break;
1123#endif
1124#if defined(MBEDTLS_MD5_C)
1125 case PSA_ALG_MD5:
1126 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1127 break;
1128#endif
1129#if defined(MBEDTLS_RIPEMD160_C)
1130 case PSA_ALG_RIPEMD160:
1131 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1132 break;
1133#endif
1134#if defined(MBEDTLS_SHA1_C)
1135 case PSA_ALG_SHA_1:
1136 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1137 break;
1138#endif
1139#if defined(MBEDTLS_SHA256_C)
1140 case PSA_ALG_SHA_224:
1141 case PSA_ALG_SHA_256:
1142 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1143 break;
1144#endif
1145#if defined(MBEDTLS_SHA512_C)
1146 case PSA_ALG_SHA_384:
1147 case PSA_ALG_SHA_512:
1148 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1149 break;
1150#endif
1151 default:
1152 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1153 break;
1154 }
1155
1156 if( ret == 0 )
1157 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001158 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001159 return( psa_hash_abort( operation ) );
1160 }
1161 else
1162 {
1163 psa_hash_abort( operation );
1164 return( mbedtls_to_psa_error( ret ) );
1165 }
1166}
1167
Gilles Peskine2d277862018-06-18 15:41:12 +02001168psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1169 const uint8_t *hash,
1170 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001171{
1172 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1173 size_t actual_hash_length;
1174 psa_status_t status = psa_hash_finish( operation,
1175 actual_hash, sizeof( actual_hash ),
1176 &actual_hash_length );
1177 if( status != PSA_SUCCESS )
1178 return( status );
1179 if( actual_hash_length != hash_length )
1180 return( PSA_ERROR_INVALID_SIGNATURE );
1181 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1182 return( PSA_ERROR_INVALID_SIGNATURE );
1183 return( PSA_SUCCESS );
1184}
1185
1186
1187
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001188/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001189/* MAC */
1190/****************************************************************/
1191
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001192static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001193 psa_algorithm_t alg,
1194 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001195 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001196 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001197{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001198 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001199 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001200
1201 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1202 {
1203 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001204 {
1205 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1206 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001207
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001208 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001209 {
Gilles Peskine5d1888e2018-07-12 00:32:42 +02001210 case PSA_ALG_STREAM_CIPHER_BASE:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001211 mode = MBEDTLS_MODE_STREAM;
1212 break;
1213 case PSA_ALG_CBC_BASE:
1214 mode = MBEDTLS_MODE_CBC;
1215 break;
1216 case PSA_ALG_CFB_BASE:
1217 mode = MBEDTLS_MODE_CFB;
1218 break;
1219 case PSA_ALG_OFB_BASE:
1220 mode = MBEDTLS_MODE_OFB;
1221 break;
1222 case PSA_ALG_CTR:
1223 mode = MBEDTLS_MODE_CTR;
1224 break;
1225 case PSA_ALG_CCM:
1226 mode = MBEDTLS_MODE_CCM;
1227 break;
1228 case PSA_ALG_GCM:
1229 mode = MBEDTLS_MODE_GCM;
1230 break;
1231 default:
1232 return( NULL );
1233 }
1234 }
1235 else if( alg == PSA_ALG_CMAC )
1236 mode = MBEDTLS_MODE_ECB;
1237 else if( alg == PSA_ALG_GMAC )
1238 mode = MBEDTLS_MODE_GCM;
1239 else
1240 return( NULL );
1241
1242 switch( key_type )
1243 {
1244 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001245 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001246 break;
1247 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001248 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1249 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001250 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001251 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001252 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001253 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001254 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1255 * but two-key Triple-DES is functionally three-key Triple-DES
1256 * with K1=K3, so that's how we present it to mbedtls. */
1257 if( key_bits == 128 )
1258 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001259 break;
1260 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001261 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001262 break;
1263 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001264 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001265 break;
1266 default:
1267 return( NULL );
1268 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001269 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001270 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001271
Jaeden Amero23bbb752018-06-26 14:16:54 +01001272 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1273 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001274}
1275
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001276static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001277{
Gilles Peskine2d277862018-06-18 15:41:12 +02001278 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001279 {
1280 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001281 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001282 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001283 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001284 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001285 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001286 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001287 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001288 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001289 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001290 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001291 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001292 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001293 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001294 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001295 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001296 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001297 return( 128 );
1298 default:
1299 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001300 }
1301}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001302
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001303/* Initialize the MAC operation structure. Once this function has been
1304 * called, psa_mac_abort can run and will do the right thing. */
1305static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1306 psa_algorithm_t alg )
1307{
1308 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1309
1310 operation->alg = alg;
1311 operation->key_set = 0;
1312 operation->iv_set = 0;
1313 operation->iv_required = 0;
1314 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001315 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001316
1317#if defined(MBEDTLS_CMAC_C)
1318 if( alg == PSA_ALG_CMAC )
1319 {
1320 operation->iv_required = 0;
1321 mbedtls_cipher_init( &operation->ctx.cmac );
1322 status = PSA_SUCCESS;
1323 }
1324 else
1325#endif /* MBEDTLS_CMAC_C */
1326#if defined(MBEDTLS_MD_C)
1327 if( PSA_ALG_IS_HMAC( operation->alg ) )
1328 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001329 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1330 operation->ctx.hmac.hash_ctx.alg = 0;
1331 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001332 }
1333 else
1334#endif /* MBEDTLS_MD_C */
1335 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001336 if( ! PSA_ALG_IS_MAC( alg ) )
1337 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001338 }
1339
1340 if( status != PSA_SUCCESS )
1341 memset( operation, 0, sizeof( *operation ) );
1342 return( status );
1343}
1344
Gilles Peskine01126fa2018-07-12 17:04:55 +02001345#if defined(MBEDTLS_MD_C)
1346static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1347{
1348 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1349 return( psa_hash_abort( &hmac->hash_ctx ) );
1350}
1351#endif /* MBEDTLS_MD_C */
1352
Gilles Peskine8c9def32018-02-08 10:02:12 +01001353psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1354{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001355 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001356 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001357 /* The object has (apparently) been initialized but it is not
1358 * in use. It's ok to call abort on such an object, and there's
1359 * nothing to do. */
1360 return( PSA_SUCCESS );
1361 }
1362 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001363#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001364 if( operation->alg == PSA_ALG_CMAC )
1365 {
1366 mbedtls_cipher_free( &operation->ctx.cmac );
1367 }
1368 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001369#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001370#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001371 if( PSA_ALG_IS_HMAC( operation->alg ) )
1372 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001373 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001374 }
1375 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001376#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001377 {
1378 /* Sanity check (shouldn't happen: operation->alg should
1379 * always have been initialized to a valid value). */
1380 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001381 }
Moran Peker41deec42018-04-04 15:43:05 +03001382
Gilles Peskine8c9def32018-02-08 10:02:12 +01001383 operation->alg = 0;
1384 operation->key_set = 0;
1385 operation->iv_set = 0;
1386 operation->iv_required = 0;
1387 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001388 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001389
Gilles Peskine8c9def32018-02-08 10:02:12 +01001390 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001391
1392bad_state:
1393 /* If abort is called on an uninitialized object, we can't trust
1394 * anything. Wipe the object in case it contains confidential data.
1395 * This may result in a memory leak if a pointer gets overwritten,
1396 * but it's too late to do anything about this. */
1397 memset( operation, 0, sizeof( *operation ) );
1398 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001399}
1400
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001401#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001402static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001403 size_t key_bits,
1404 key_slot_t *slot,
1405 const mbedtls_cipher_info_t *cipher_info )
1406{
1407 int ret;
1408
1409 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001410
1411 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1412 if( ret != 0 )
1413 return( ret );
1414
1415 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1416 slot->data.raw.data,
1417 key_bits );
1418 return( ret );
1419}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001420#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001421
Gilles Peskine248051a2018-06-20 16:09:38 +02001422#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001423static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1424 const uint8_t *key,
1425 size_t key_length,
1426 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001427{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001428 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001429 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001430 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001431 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001432 psa_status_t status;
1433
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001434 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1435 * overflow below. This should never trigger if the hash algorithm
1436 * is implemented correctly. */
1437 /* The size checks against the ipad and opad buffers cannot be written
1438 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1439 * because that triggers -Wlogical-op on GCC 7.3. */
1440 if( block_size > sizeof( ipad ) )
1441 return( PSA_ERROR_NOT_SUPPORTED );
1442 if( block_size > sizeof( hmac->opad ) )
1443 return( PSA_ERROR_NOT_SUPPORTED );
1444 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001445 return( PSA_ERROR_NOT_SUPPORTED );
1446
Gilles Peskined223b522018-06-11 18:12:58 +02001447 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001448 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001449 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001450 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001451 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001452 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001453 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001454 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001455 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001456 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001457 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001458 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001459 }
Gilles Peskine96889972018-07-12 17:07:03 +02001460 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1461 * but it is permitted. It is common when HMAC is used in HKDF, for
1462 * example. Don't call `memcpy` in the 0-length because `key` could be
1463 * an invalid pointer which would make the behavior undefined. */
1464 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001465 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001466
Gilles Peskined223b522018-06-11 18:12:58 +02001467 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1468 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001469 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001470 ipad[i] ^= 0x36;
1471 memset( ipad + key_length, 0x36, block_size - key_length );
1472
1473 /* Copy the key material from ipad to opad, flipping the requisite bits,
1474 * and filling the rest of opad with the requisite constant. */
1475 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001476 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1477 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001478
Gilles Peskine01126fa2018-07-12 17:04:55 +02001479 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001480 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001481 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001482
Gilles Peskine01126fa2018-07-12 17:04:55 +02001483 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001484
1485cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001486 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001487
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001488 return( status );
1489}
Gilles Peskine248051a2018-06-20 16:09:38 +02001490#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001491
Gilles Peskine89167cb2018-07-08 20:12:23 +02001492static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1493 psa_key_slot_t key,
1494 psa_algorithm_t alg,
1495 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001496{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001497 psa_status_t status;
1498 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001499 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001500 psa_key_usage_t usage =
1501 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001502
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001503 status = psa_mac_init( operation, alg );
1504 if( status != PSA_SUCCESS )
1505 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001506 if( is_sign )
1507 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001508
Gilles Peskine89167cb2018-07-08 20:12:23 +02001509 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001510 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001511 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001512 key_bits = psa_get_key_bits( slot );
1513
Gilles Peskine8c9def32018-02-08 10:02:12 +01001514#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001515 if( alg == PSA_ALG_CMAC )
1516 {
1517 const mbedtls_cipher_info_t *cipher_info =
1518 mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
1519 int ret;
1520 if( cipher_info == NULL )
1521 {
1522 status = PSA_ERROR_NOT_SUPPORTED;
1523 goto exit;
1524 }
1525 operation->mac_size = cipher_info->block_size;
1526 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1527 status = mbedtls_to_psa_error( ret );
1528 }
1529 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001530#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001531#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001532 if( PSA_ALG_IS_HMAC( alg ) )
1533 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001534 psa_algorithm_t hash_alg = PSA_ALG_HMAC_HASH( alg );
1535 if( hash_alg == 0 )
1536 {
1537 status = PSA_ERROR_NOT_SUPPORTED;
1538 goto exit;
1539 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001540
1541 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1542 /* Sanity check. This shouldn't fail on a valid configuration. */
1543 if( operation->mac_size == 0 ||
1544 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1545 {
1546 status = PSA_ERROR_NOT_SUPPORTED;
1547 goto exit;
1548 }
1549
Gilles Peskine01126fa2018-07-12 17:04:55 +02001550 if( slot->type != PSA_KEY_TYPE_HMAC )
1551 {
1552 status = PSA_ERROR_INVALID_ARGUMENT;
1553 goto exit;
1554 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001555
Gilles Peskine01126fa2018-07-12 17:04:55 +02001556 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1557 slot->data.raw.data,
1558 slot->data.raw.bytes,
1559 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001560 }
1561 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001562#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001563 {
1564 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001565 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001566
Gilles Peskinefbfac682018-07-08 20:51:54 +02001567exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001568 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001569 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001570 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001571 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001572 else
1573 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001574 operation->key_set = 1;
1575 }
1576 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001577}
1578
Gilles Peskine89167cb2018-07-08 20:12:23 +02001579psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1580 psa_key_slot_t key,
1581 psa_algorithm_t alg )
1582{
1583 return( psa_mac_setup( operation, key, alg, 1 ) );
1584}
1585
1586psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1587 psa_key_slot_t key,
1588 psa_algorithm_t alg )
1589{
1590 return( psa_mac_setup( operation, key, alg, 0 ) );
1591}
1592
Gilles Peskine8c9def32018-02-08 10:02:12 +01001593psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1594 const uint8_t *input,
1595 size_t input_length )
1596{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001597 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001598 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001599 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001600 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001601 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001602 operation->has_input = 1;
1603
Gilles Peskine8c9def32018-02-08 10:02:12 +01001604#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001605 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001606 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001607 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1608 input, input_length );
1609 status = mbedtls_to_psa_error( ret );
1610 }
1611 else
1612#endif /* MBEDTLS_CMAC_C */
1613#if defined(MBEDTLS_MD_C)
1614 if( PSA_ALG_IS_HMAC( operation->alg ) )
1615 {
1616 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1617 input_length );
1618 }
1619 else
1620#endif /* MBEDTLS_MD_C */
1621 {
1622 /* This shouldn't happen if `operation` was initialized by
1623 * a setup function. */
1624 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001625 }
1626
Gilles Peskinefbfac682018-07-08 20:51:54 +02001627cleanup:
1628 if( status != PSA_SUCCESS )
1629 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001630 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001631}
1632
Gilles Peskine01126fa2018-07-12 17:04:55 +02001633#if defined(MBEDTLS_MD_C)
1634static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1635 uint8_t *mac,
1636 size_t mac_size )
1637{
1638 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1639 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1640 size_t hash_size = 0;
1641 size_t block_size = psa_get_hash_block_size( hash_alg );
1642 psa_status_t status;
1643
Gilles Peskine01126fa2018-07-12 17:04:55 +02001644 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1645 if( status != PSA_SUCCESS )
1646 return( status );
1647 /* From here on, tmp needs to be wiped. */
1648
1649 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1650 if( status != PSA_SUCCESS )
1651 goto exit;
1652
1653 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1654 if( status != PSA_SUCCESS )
1655 goto exit;
1656
1657 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1658 if( status != PSA_SUCCESS )
1659 goto exit;
1660
1661 status = psa_hash_finish( &hmac->hash_ctx, mac, mac_size, &hash_size );
1662
1663exit:
1664 mbedtls_zeroize( tmp, hash_size );
1665 return( status );
1666}
1667#endif /* MBEDTLS_MD_C */
1668
mohammad16036df908f2018-04-02 08:34:15 -07001669static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001670 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001671 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001672{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001673 if( ! operation->key_set )
1674 return( PSA_ERROR_BAD_STATE );
1675 if( operation->iv_required && ! operation->iv_set )
1676 return( PSA_ERROR_BAD_STATE );
1677
Gilles Peskine8c9def32018-02-08 10:02:12 +01001678 if( mac_size < operation->mac_size )
1679 return( PSA_ERROR_BUFFER_TOO_SMALL );
1680
Gilles Peskine8c9def32018-02-08 10:02:12 +01001681#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001682 if( operation->alg == PSA_ALG_CMAC )
1683 {
1684 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1685 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001686 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001687 else
1688#endif /* MBEDTLS_CMAC_C */
1689#if defined(MBEDTLS_MD_C)
1690 if( PSA_ALG_IS_HMAC( operation->alg ) )
1691 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001692 return( psa_hmac_finish_internal( &operation->ctx.hmac,
1693 mac, mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001694 }
1695 else
1696#endif /* MBEDTLS_MD_C */
1697 {
1698 /* This shouldn't happen if `operation` was initialized by
1699 * a setup function. */
1700 return( PSA_ERROR_BAD_STATE );
1701 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001702}
1703
Gilles Peskineacd4be32018-07-08 19:56:25 +02001704psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1705 uint8_t *mac,
1706 size_t mac_size,
1707 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001708{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001709 psa_status_t status;
1710
1711 /* Fill the output buffer with something that isn't a valid mac
1712 * (barring an attack on the mac and deliberately-crafted input),
1713 * in case the caller doesn't check the return status properly. */
1714 *mac_length = mac_size;
1715 /* If mac_size is 0 then mac may be NULL and then the
1716 * call to memset would have undefined behavior. */
1717 if( mac_size != 0 )
1718 memset( mac, '!', mac_size );
1719
Gilles Peskine89167cb2018-07-08 20:12:23 +02001720 if( ! operation->is_sign )
1721 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001722 status = PSA_ERROR_BAD_STATE;
1723 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001724 }
mohammad16036df908f2018-04-02 08:34:15 -07001725
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001726 status = psa_mac_finish_internal( operation, mac, mac_size );
1727
1728cleanup:
1729 if( status == PSA_SUCCESS )
1730 {
1731 status = psa_mac_abort( operation );
1732 if( status == PSA_SUCCESS )
1733 *mac_length = operation->mac_size;
1734 else
1735 memset( mac, '!', mac_size );
1736 }
1737 else
1738 psa_mac_abort( operation );
1739 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001740}
1741
Gilles Peskineacd4be32018-07-08 19:56:25 +02001742psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1743 const uint8_t *mac,
1744 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001745{
Gilles Peskine828ed142018-06-18 23:25:51 +02001746 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001747 psa_status_t status;
1748
Gilles Peskine89167cb2018-07-08 20:12:23 +02001749 if( operation->is_sign )
1750 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001751 status = PSA_ERROR_BAD_STATE;
1752 goto cleanup;
1753 }
1754 if( operation->mac_size != mac_length )
1755 {
1756 status = PSA_ERROR_INVALID_SIGNATURE;
1757 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001758 }
mohammad16036df908f2018-04-02 08:34:15 -07001759
1760 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001761 actual_mac, sizeof( actual_mac ) );
1762
1763 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1764 status = PSA_ERROR_INVALID_SIGNATURE;
1765
1766cleanup:
1767 if( status == PSA_SUCCESS )
1768 status = psa_mac_abort( operation );
1769 else
1770 psa_mac_abort( operation );
1771
1772 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001773}
1774
1775
Gilles Peskine20035e32018-02-03 22:44:14 +01001776
Gilles Peskine20035e32018-02-03 22:44:14 +01001777/****************************************************************/
1778/* Asymmetric cryptography */
1779/****************************************************************/
1780
Gilles Peskine2b450e32018-06-27 15:42:46 +02001781#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001782/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001783 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001784static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1785 size_t hash_length,
1786 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001787{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001788 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001789 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001790 *md_alg = mbedtls_md_get_type( md_info );
1791
1792 /* The Mbed TLS RSA module uses an unsigned int for hash length
1793 * parameters. Validate that it fits so that we don't risk an
1794 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001795#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001796 if( hash_length > UINT_MAX )
1797 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001798#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001799
1800#if defined(MBEDTLS_PKCS1_V15)
1801 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
1802 * must be correct. */
1803 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
1804 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001805 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001806 if( md_info == NULL )
1807 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001808 if( mbedtls_md_get_size( md_info ) != hash_length )
1809 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001810 }
1811#endif /* MBEDTLS_PKCS1_V15 */
1812
1813#if defined(MBEDTLS_PKCS1_V21)
1814 /* PSS requires a hash internally. */
1815 if( PSA_ALG_IS_RSA_PSS( alg ) )
1816 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001817 if( md_info == NULL )
1818 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001819 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001820#endif /* MBEDTLS_PKCS1_V21 */
1821
Gilles Peskine61b91d42018-06-08 16:09:36 +02001822 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001823}
1824
Gilles Peskine2b450e32018-06-27 15:42:46 +02001825static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1826 psa_algorithm_t alg,
1827 const uint8_t *hash,
1828 size_t hash_length,
1829 uint8_t *signature,
1830 size_t signature_size,
1831 size_t *signature_length )
1832{
1833 psa_status_t status;
1834 int ret;
1835 mbedtls_md_type_t md_alg;
1836
1837 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1838 if( status != PSA_SUCCESS )
1839 return( status );
1840
Gilles Peskine630a18a2018-06-29 17:49:35 +02001841 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001842 return( PSA_ERROR_BUFFER_TOO_SMALL );
1843
1844#if defined(MBEDTLS_PKCS1_V15)
1845 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1846 {
1847 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1848 MBEDTLS_MD_NONE );
1849 ret = mbedtls_rsa_pkcs1_sign( rsa,
1850 mbedtls_ctr_drbg_random,
1851 &global_data.ctr_drbg,
1852 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001853 md_alg,
1854 (unsigned int) hash_length,
1855 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001856 signature );
1857 }
1858 else
1859#endif /* MBEDTLS_PKCS1_V15 */
1860#if defined(MBEDTLS_PKCS1_V21)
1861 if( PSA_ALG_IS_RSA_PSS( alg ) )
1862 {
1863 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1864 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1865 mbedtls_ctr_drbg_random,
1866 &global_data.ctr_drbg,
1867 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001868 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001869 (unsigned int) hash_length,
1870 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001871 signature );
1872 }
1873 else
1874#endif /* MBEDTLS_PKCS1_V21 */
1875 {
1876 return( PSA_ERROR_INVALID_ARGUMENT );
1877 }
1878
1879 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02001880 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02001881 return( mbedtls_to_psa_error( ret ) );
1882}
1883
1884static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1885 psa_algorithm_t alg,
1886 const uint8_t *hash,
1887 size_t hash_length,
1888 const uint8_t *signature,
1889 size_t signature_length )
1890{
1891 psa_status_t status;
1892 int ret;
1893 mbedtls_md_type_t md_alg;
1894
1895 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1896 if( status != PSA_SUCCESS )
1897 return( status );
1898
Gilles Peskine630a18a2018-06-29 17:49:35 +02001899 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001900 return( PSA_ERROR_BUFFER_TOO_SMALL );
1901
1902#if defined(MBEDTLS_PKCS1_V15)
1903 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1904 {
1905 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1906 MBEDTLS_MD_NONE );
1907 ret = mbedtls_rsa_pkcs1_verify( rsa,
1908 mbedtls_ctr_drbg_random,
1909 &global_data.ctr_drbg,
1910 MBEDTLS_RSA_PUBLIC,
1911 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001912 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001913 hash,
1914 signature );
1915 }
1916 else
1917#endif /* MBEDTLS_PKCS1_V15 */
1918#if defined(MBEDTLS_PKCS1_V21)
1919 if( PSA_ALG_IS_RSA_PSS( alg ) )
1920 {
1921 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1922 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1923 mbedtls_ctr_drbg_random,
1924 &global_data.ctr_drbg,
1925 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001926 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001927 (unsigned int) hash_length,
1928 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001929 signature );
1930 }
1931 else
1932#endif /* MBEDTLS_PKCS1_V21 */
1933 {
1934 return( PSA_ERROR_INVALID_ARGUMENT );
1935 }
1936 return( mbedtls_to_psa_error( ret ) );
1937}
1938#endif /* MBEDTLS_RSA_C */
1939
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001940#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001941/* `ecp` cannot be const because `ecp->grp` needs to be non-const
1942 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
1943 * (even though these functions don't modify it). */
1944static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
1945 psa_algorithm_t alg,
1946 const uint8_t *hash,
1947 size_t hash_length,
1948 uint8_t *signature,
1949 size_t signature_size,
1950 size_t *signature_length )
1951{
1952 int ret;
1953 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001954 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001955 mbedtls_mpi_init( &r );
1956 mbedtls_mpi_init( &s );
1957
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001958 if( signature_size < 2 * curve_bytes )
1959 {
1960 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
1961 goto cleanup;
1962 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001963
1964 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
1965 {
1966 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
1967 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1968 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
1969 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
1970 hash, hash_length,
1971 md_alg ) );
1972 }
1973 else
1974 {
1975 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
1976 hash, hash_length,
1977 mbedtls_ctr_drbg_random,
1978 &global_data.ctr_drbg ) );
1979 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001980
1981 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
1982 signature,
1983 curve_bytes ) );
1984 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
1985 signature + curve_bytes,
1986 curve_bytes ) );
1987
1988cleanup:
1989 mbedtls_mpi_free( &r );
1990 mbedtls_mpi_free( &s );
1991 if( ret == 0 )
1992 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001993 return( mbedtls_to_psa_error( ret ) );
1994}
1995
1996static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
1997 const uint8_t *hash,
1998 size_t hash_length,
1999 const uint8_t *signature,
2000 size_t signature_length )
2001{
2002 int ret;
2003 mbedtls_mpi r, s;
2004 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2005 mbedtls_mpi_init( &r );
2006 mbedtls_mpi_init( &s );
2007
2008 if( signature_length != 2 * curve_bytes )
2009 return( PSA_ERROR_INVALID_SIGNATURE );
2010
2011 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2012 signature,
2013 curve_bytes ) );
2014 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2015 signature + curve_bytes,
2016 curve_bytes ) );
2017
2018 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2019 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002020
2021cleanup:
2022 mbedtls_mpi_free( &r );
2023 mbedtls_mpi_free( &s );
2024 return( mbedtls_to_psa_error( ret ) );
2025}
2026#endif /* MBEDTLS_ECDSA_C */
2027
Gilles Peskine61b91d42018-06-08 16:09:36 +02002028psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2029 psa_algorithm_t alg,
2030 const uint8_t *hash,
2031 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002032 uint8_t *signature,
2033 size_t signature_size,
2034 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002035{
2036 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002037 psa_status_t status;
2038
2039 *signature_length = signature_size;
2040
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002041 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2042 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002043 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002044 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002045 {
2046 status = PSA_ERROR_INVALID_ARGUMENT;
2047 goto exit;
2048 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002049
Gilles Peskine20035e32018-02-03 22:44:14 +01002050#if defined(MBEDTLS_RSA_C)
2051 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2052 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002053 status = psa_rsa_sign( slot->data.rsa,
2054 alg,
2055 hash, hash_length,
2056 signature, signature_size,
2057 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002058 }
2059 else
2060#endif /* defined(MBEDTLS_RSA_C) */
2061#if defined(MBEDTLS_ECP_C)
2062 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2063 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002064#if defined(MBEDTLS_ECDSA_C)
2065 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002066 status = psa_ecdsa_sign( slot->data.ecp,
2067 alg,
2068 hash, hash_length,
2069 signature, signature_size,
2070 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002071 else
2072#endif /* defined(MBEDTLS_ECDSA_C) */
2073 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002074 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002075 }
itayzafrir5c753392018-05-08 11:18:38 +03002076 }
2077 else
2078#endif /* defined(MBEDTLS_ECP_C) */
2079 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002080 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002081 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002082
2083exit:
2084 /* Fill the unused part of the output buffer (the whole buffer on error,
2085 * the trailing part on success) with something that isn't a valid mac
2086 * (barring an attack on the mac and deliberately-crafted input),
2087 * in case the caller doesn't check the return status properly. */
2088 if( status == PSA_SUCCESS )
2089 memset( signature + *signature_length, '!',
2090 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002091 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002092 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002093 /* If signature_size is 0 then we have nothing to do. We must not call
2094 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002095 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002096}
2097
2098psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2099 psa_algorithm_t alg,
2100 const uint8_t *hash,
2101 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002102 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002103 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002104{
2105 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002106 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002107
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002108 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2109 if( status != PSA_SUCCESS )
2110 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002111
Gilles Peskine61b91d42018-06-08 16:09:36 +02002112#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002113 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002114 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002115 return( psa_rsa_verify( slot->data.rsa,
2116 alg,
2117 hash, hash_length,
2118 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002119 }
2120 else
2121#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002122#if defined(MBEDTLS_ECP_C)
2123 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2124 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002125#if defined(MBEDTLS_ECDSA_C)
2126 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002127 return( psa_ecdsa_verify( slot->data.ecp,
2128 hash, hash_length,
2129 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002130 else
2131#endif /* defined(MBEDTLS_ECDSA_C) */
2132 {
2133 return( PSA_ERROR_INVALID_ARGUMENT );
2134 }
itayzafrir5c753392018-05-08 11:18:38 +03002135 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002136 else
2137#endif /* defined(MBEDTLS_ECP_C) */
2138 {
2139 return( PSA_ERROR_NOT_SUPPORTED );
2140 }
2141}
2142
Gilles Peskine072ac562018-06-30 00:21:29 +02002143#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2144static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2145 mbedtls_rsa_context *rsa )
2146{
2147 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2148 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2149 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2150 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2151}
2152#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2153
Gilles Peskine61b91d42018-06-08 16:09:36 +02002154psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2155 psa_algorithm_t alg,
2156 const uint8_t *input,
2157 size_t input_length,
2158 const uint8_t *salt,
2159 size_t salt_length,
2160 uint8_t *output,
2161 size_t output_size,
2162 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002163{
2164 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002165 psa_status_t status;
2166
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002167 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002168
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002169 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2170 return( PSA_ERROR_INVALID_ARGUMENT );
2171
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002172 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2173 if( status != PSA_SUCCESS )
2174 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002175 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2176 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002177 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002178
2179#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002180 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002181 {
2182 mbedtls_rsa_context *rsa = slot->data.rsa;
2183 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002184 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002185 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002186#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002187 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002188 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002189 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2190 mbedtls_ctr_drbg_random,
2191 &global_data.ctr_drbg,
2192 MBEDTLS_RSA_PUBLIC,
2193 input_length,
2194 input,
2195 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002196 }
2197 else
2198#endif /* MBEDTLS_PKCS1_V15 */
2199#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002200 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002201 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002202 psa_rsa_oaep_set_padding_mode( alg, rsa );
2203 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2204 mbedtls_ctr_drbg_random,
2205 &global_data.ctr_drbg,
2206 MBEDTLS_RSA_PUBLIC,
2207 salt, salt_length,
2208 input_length,
2209 input,
2210 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002211 }
2212 else
2213#endif /* MBEDTLS_PKCS1_V21 */
2214 {
2215 return( PSA_ERROR_INVALID_ARGUMENT );
2216 }
2217 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002218 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002219 return( mbedtls_to_psa_error( ret ) );
2220 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002221 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002222#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002223 {
2224 return( PSA_ERROR_NOT_SUPPORTED );
2225 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002226}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002227
Gilles Peskine61b91d42018-06-08 16:09:36 +02002228psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2229 psa_algorithm_t alg,
2230 const uint8_t *input,
2231 size_t input_length,
2232 const uint8_t *salt,
2233 size_t salt_length,
2234 uint8_t *output,
2235 size_t output_size,
2236 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002237{
2238 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002239 psa_status_t status;
2240
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002241 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002242
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002243 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2244 return( PSA_ERROR_INVALID_ARGUMENT );
2245
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002246 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2247 if( status != PSA_SUCCESS )
2248 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002249 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2250 return( PSA_ERROR_INVALID_ARGUMENT );
2251
2252#if defined(MBEDTLS_RSA_C)
2253 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2254 {
2255 mbedtls_rsa_context *rsa = slot->data.rsa;
2256 int ret;
2257
Gilles Peskine630a18a2018-06-29 17:49:35 +02002258 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002259 return( PSA_ERROR_INVALID_ARGUMENT );
2260
2261#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002262 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002263 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002264 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2265 mbedtls_ctr_drbg_random,
2266 &global_data.ctr_drbg,
2267 MBEDTLS_RSA_PRIVATE,
2268 output_length,
2269 input,
2270 output,
2271 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002272 }
2273 else
2274#endif /* MBEDTLS_PKCS1_V15 */
2275#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002276 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002277 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002278 psa_rsa_oaep_set_padding_mode( alg, rsa );
2279 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2280 mbedtls_ctr_drbg_random,
2281 &global_data.ctr_drbg,
2282 MBEDTLS_RSA_PRIVATE,
2283 salt, salt_length,
2284 output_length,
2285 input,
2286 output,
2287 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002288 }
2289 else
2290#endif /* MBEDTLS_PKCS1_V21 */
2291 {
2292 return( PSA_ERROR_INVALID_ARGUMENT );
2293 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002294
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002295 return( mbedtls_to_psa_error( ret ) );
2296 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002297 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002298#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002299 {
2300 return( PSA_ERROR_NOT_SUPPORTED );
2301 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002302}
Gilles Peskine20035e32018-02-03 22:44:14 +01002303
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002304
2305
mohammad1603503973b2018-03-12 15:59:30 +02002306/****************************************************************/
2307/* Symmetric cryptography */
2308/****************************************************************/
2309
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002310/* Initialize the cipher operation structure. Once this function has been
2311 * called, psa_cipher_abort can run and will do the right thing. */
2312static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2313 psa_algorithm_t alg )
2314{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002315 if( ! PSA_ALG_IS_CIPHER( alg ) )
2316 {
2317 memset( operation, 0, sizeof( *operation ) );
2318 return( PSA_ERROR_INVALID_ARGUMENT );
2319 }
2320
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002321 operation->alg = alg;
2322 operation->key_set = 0;
2323 operation->iv_set = 0;
2324 operation->iv_required = 1;
2325 operation->iv_size = 0;
2326 operation->block_size = 0;
2327 mbedtls_cipher_init( &operation->ctx.cipher );
2328 return( PSA_SUCCESS );
2329}
2330
Gilles Peskinee553c652018-06-04 16:22:46 +02002331static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2332 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002333 psa_algorithm_t alg,
2334 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002335{
2336 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2337 psa_status_t status;
2338 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002339 size_t key_bits;
2340 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002341 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2342 PSA_KEY_USAGE_ENCRYPT :
2343 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002344
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002345 status = psa_cipher_init( operation, alg );
2346 if( status != PSA_SUCCESS )
2347 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002348
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002349 status = psa_get_key_from_slot( key, &slot, usage, alg);
2350 if( status != PSA_SUCCESS )
2351 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002352 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002353
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002354 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002355 if( cipher_info == NULL )
2356 return( PSA_ERROR_NOT_SUPPORTED );
2357
mohammad1603503973b2018-03-12 15:59:30 +02002358 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002359 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002360 {
2361 psa_cipher_abort( operation );
2362 return( mbedtls_to_psa_error( ret ) );
2363 }
2364
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002365#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002366 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002367 {
2368 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2369 unsigned char keys[24];
2370 memcpy( keys, slot->data.raw.data, 16 );
2371 memcpy( keys + 16, slot->data.raw.data, 8 );
2372 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2373 keys,
2374 192, cipher_operation );
2375 }
2376 else
2377#endif
2378 {
2379 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2380 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002381 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002382 }
Moran Peker41deec42018-04-04 15:43:05 +03002383 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002384 {
2385 psa_cipher_abort( operation );
2386 return( mbedtls_to_psa_error( ret ) );
2387 }
2388
mohammad16038481e742018-03-18 13:57:31 +02002389#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002390 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002391 {
Gilles Peskine53514202018-06-06 15:11:46 +02002392 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2393 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002394
Moran Pekera28258c2018-05-29 16:25:04 +03002395 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002396 {
2397 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2398 mode = MBEDTLS_PADDING_PKCS7;
2399 break;
2400 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2401 mode = MBEDTLS_PADDING_NONE;
2402 break;
2403 default:
Moran Pekerae382792018-05-31 14:06:17 +03002404 psa_cipher_abort( operation );
2405 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002406 }
2407 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002408 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002409 {
2410 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002411 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002412 }
mohammad16038481e742018-03-18 13:57:31 +02002413 }
2414#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2415
mohammad1603503973b2018-03-12 15:59:30 +02002416 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002417 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002418 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) :
Gilles Peskine7e928852018-06-04 16:23:10 +02002419 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002420 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002421 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002422 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002423 }
mohammad1603503973b2018-03-12 15:59:30 +02002424
Moran Peker395db872018-05-31 14:07:14 +03002425 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002426}
2427
Gilles Peskinefe119512018-07-08 21:39:34 +02002428psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2429 psa_key_slot_t key,
2430 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002431{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002432 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002433}
2434
Gilles Peskinefe119512018-07-08 21:39:34 +02002435psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2436 psa_key_slot_t key,
2437 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002438{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002439 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002440}
2441
Gilles Peskinefe119512018-07-08 21:39:34 +02002442psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2443 unsigned char *iv,
2444 size_t iv_size,
2445 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002446{
Moran Peker41deec42018-04-04 15:43:05 +03002447 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002448 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002449 return( PSA_ERROR_BAD_STATE );
2450 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002451 {
Moran Peker41deec42018-04-04 15:43:05 +03002452 ret = PSA_ERROR_BUFFER_TOO_SMALL;
2453 goto exit;
2454 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002455 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2456 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002457 if( ret != 0 )
2458 {
2459 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002460 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002461 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002462
mohammad16038481e742018-03-18 13:57:31 +02002463 *iv_length = operation->iv_size;
Gilles Peskinefe119512018-07-08 21:39:34 +02002464 ret = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002465
Moran Peker395db872018-05-31 14:07:14 +03002466exit:
2467 if( ret != PSA_SUCCESS )
2468 psa_cipher_abort( operation );
2469 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02002470}
2471
Gilles Peskinefe119512018-07-08 21:39:34 +02002472psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2473 const unsigned char *iv,
2474 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002475{
Moran Peker41deec42018-04-04 15:43:05 +03002476 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002477 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002478 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03002479 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002480 {
Moran Pekerae382792018-05-31 14:06:17 +03002481 psa_cipher_abort( operation );
2482 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03002483 }
mohammad1603503973b2018-03-12 15:59:30 +02002484 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002485 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002486 {
2487 psa_cipher_abort( operation );
2488 return( mbedtls_to_psa_error( ret ) );
2489 }
2490
2491 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02002492
Moran Peker395db872018-05-31 14:07:14 +03002493 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002494}
2495
Gilles Peskinee553c652018-06-04 16:22:46 +02002496psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2497 const uint8_t *input,
2498 size_t input_length,
2499 unsigned char *output,
2500 size_t output_size,
2501 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002502{
2503 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002504 size_t expected_output_size;
2505 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2506 {
2507 /* Take the unprocessed partial block left over from previous
2508 * update calls, if any, plus the input to this call. Remove
2509 * the last partial block, if any. You get the data that will be
2510 * output in this call. */
2511 expected_output_size =
2512 ( operation->ctx.cipher.unprocessed_len + input_length )
2513 / operation->block_size * operation->block_size;
2514 }
2515 else
2516 {
2517 expected_output_size = input_length;
2518 }
2519 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03002520 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02002521
mohammad1603503973b2018-03-12 15:59:30 +02002522 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002523 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002524 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002525 {
2526 psa_cipher_abort( operation );
2527 return( mbedtls_to_psa_error( ret ) );
2528 }
2529
Moran Peker395db872018-05-31 14:07:14 +03002530 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002531}
2532
Gilles Peskinee553c652018-06-04 16:22:46 +02002533psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2534 uint8_t *output,
2535 size_t output_size,
2536 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002537{
Janos Follath315b51c2018-07-09 16:04:51 +01002538 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2539 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002540 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002541
mohammad1603503973b2018-03-12 15:59:30 +02002542 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002543 {
Janos Follath315b51c2018-07-09 16:04:51 +01002544 status = PSA_ERROR_BAD_STATE;
2545 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002546 }
2547 if( operation->iv_required && ! operation->iv_set )
2548 {
Janos Follath315b51c2018-07-09 16:04:51 +01002549 status = PSA_ERROR_BAD_STATE;
2550 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002551 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002552 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2553 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002554 {
Gilles Peskine53514202018-06-06 15:11:46 +02002555 psa_algorithm_t padding_mode =
2556 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002557 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002558 {
Janos Follath315b51c2018-07-09 16:04:51 +01002559 status = PSA_ERROR_TAMPERING_DETECTED;
2560 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002561 }
Gilles Peskine53514202018-06-06 15:11:46 +02002562 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002563 {
2564 if( operation->ctx.cipher.unprocessed_len != 0 )
2565 {
Janos Follath315b51c2018-07-09 16:04:51 +01002566 status = PSA_ERROR_INVALID_ARGUMENT;
2567 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002568 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002569 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002570 }
2571
Janos Follath315b51c2018-07-09 16:04:51 +01002572 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2573 temp_output_buffer,
2574 output_length );
2575 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002576 {
Janos Follath315b51c2018-07-09 16:04:51 +01002577 status = mbedtls_to_psa_error( cipher_ret );
2578 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002579 }
Janos Follath315b51c2018-07-09 16:04:51 +01002580
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002581 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002582 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002583 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002584 memcpy( output, temp_output_buffer, *output_length );
2585 else
2586 {
Janos Follath315b51c2018-07-09 16:04:51 +01002587 status = PSA_ERROR_BUFFER_TOO_SMALL;
2588 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002589 }
mohammad1603503973b2018-03-12 15:59:30 +02002590
Janos Follath279ab8e2018-07-09 16:13:21 +01002591 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002592 status = psa_cipher_abort( operation );
2593
2594 return( status );
2595
2596error:
2597
2598 *output_length = 0;
2599
Janos Follath279ab8e2018-07-09 16:13:21 +01002600 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002601 (void) psa_cipher_abort( operation );
2602
2603 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002604}
2605
Gilles Peskinee553c652018-06-04 16:22:46 +02002606psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2607{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002608 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002609 {
2610 /* The object has (apparently) been initialized but it is not
2611 * in use. It's ok to call abort on such an object, and there's
2612 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002613 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002614 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002615
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002616 /* Sanity check (shouldn't happen: operation->alg should
2617 * always have been initialized to a valid value). */
2618 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2619 return( PSA_ERROR_BAD_STATE );
2620
mohammad1603503973b2018-03-12 15:59:30 +02002621 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002622
Moran Peker41deec42018-04-04 15:43:05 +03002623 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002624 operation->key_set = 0;
2625 operation->iv_set = 0;
2626 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002627 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002628 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002629
Moran Peker395db872018-05-31 14:07:14 +03002630 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002631}
2632
Gilles Peskinea0655c32018-04-30 17:06:50 +02002633
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002634
mohammad16038cc1cee2018-03-28 01:21:33 +03002635/****************************************************************/
2636/* Key Policy */
2637/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002638
mohammad160327010052018-07-03 13:16:15 +03002639#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002640void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002641{
Gilles Peskine803ce742018-06-18 16:07:14 +02002642 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002643}
2644
Gilles Peskine2d277862018-06-18 15:41:12 +02002645void psa_key_policy_set_usage( psa_key_policy_t *policy,
2646 psa_key_usage_t usage,
2647 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002648{
mohammad16034eed7572018-03-28 05:14:59 -07002649 policy->usage = usage;
2650 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002651}
2652
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002653psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002654{
mohammad16036df908f2018-04-02 08:34:15 -07002655 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002656}
2657
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002658psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002659{
mohammad16036df908f2018-04-02 08:34:15 -07002660 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002661}
mohammad160327010052018-07-03 13:16:15 +03002662#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002663
Gilles Peskine2d277862018-06-18 15:41:12 +02002664psa_status_t psa_set_key_policy( psa_key_slot_t key,
2665 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002666{
2667 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002668 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002669
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002670 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002671 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002672
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002673 status = psa_get_empty_key_slot( key, &slot );
2674 if( status != PSA_SUCCESS )
2675 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002676
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002677 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2678 PSA_KEY_USAGE_ENCRYPT |
2679 PSA_KEY_USAGE_DECRYPT |
2680 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002681 PSA_KEY_USAGE_VERIFY |
2682 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002683 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002684
mohammad16036df908f2018-04-02 08:34:15 -07002685 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002686
2687 return( PSA_SUCCESS );
2688}
2689
Gilles Peskine2d277862018-06-18 15:41:12 +02002690psa_status_t psa_get_key_policy( psa_key_slot_t key,
2691 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002692{
2693 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002694 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002695
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002696 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002697 return( PSA_ERROR_INVALID_ARGUMENT );
2698
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002699 status = psa_get_key_slot( key, &slot );
2700 if( status != PSA_SUCCESS )
2701 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002702
mohammad16036df908f2018-04-02 08:34:15 -07002703 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002704
2705 return( PSA_SUCCESS );
2706}
Gilles Peskine20035e32018-02-03 22:44:14 +01002707
Gilles Peskinea0655c32018-04-30 17:06:50 +02002708
2709
mohammad1603804cd712018-03-20 22:44:08 +02002710/****************************************************************/
2711/* Key Lifetime */
2712/****************************************************************/
2713
Gilles Peskine2d277862018-06-18 15:41:12 +02002714psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2715 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002716{
2717 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002718 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002719
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002720 status = psa_get_key_slot( key, &slot );
2721 if( status != PSA_SUCCESS )
2722 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002723
mohammad1603804cd712018-03-20 22:44:08 +02002724 *lifetime = slot->lifetime;
2725
2726 return( PSA_SUCCESS );
2727}
2728
Gilles Peskine2d277862018-06-18 15:41:12 +02002729psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002730 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002731{
2732 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002733 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002734
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002735 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2736 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002737 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2738 return( PSA_ERROR_INVALID_ARGUMENT );
2739
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002740 status = psa_get_empty_key_slot( key, &slot );
2741 if( status != PSA_SUCCESS )
2742 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002743
Moran Pekerd7326592018-05-29 16:56:39 +03002744 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002745 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002746
mohammad1603060ad8a2018-03-20 14:28:38 -07002747 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002748
2749 return( PSA_SUCCESS );
2750}
2751
Gilles Peskine20035e32018-02-03 22:44:14 +01002752
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002753
mohammad16035955c982018-04-26 00:53:03 +03002754/****************************************************************/
2755/* AEAD */
2756/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002757
mohammad16035955c982018-04-26 00:53:03 +03002758psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2759 psa_algorithm_t alg,
2760 const uint8_t *nonce,
2761 size_t nonce_length,
2762 const uint8_t *additional_data,
2763 size_t additional_data_length,
2764 const uint8_t *plaintext,
2765 size_t plaintext_length,
2766 uint8_t *ciphertext,
2767 size_t ciphertext_size,
2768 size_t *ciphertext_length )
2769{
2770 int ret;
2771 psa_status_t status;
2772 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002773 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002774 uint8_t *tag;
2775 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002776 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002777 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002778
mohammad1603f08a5502018-06-03 15:05:47 +03002779 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002780
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002781 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2782 if( status != PSA_SUCCESS )
2783 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002784 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002785
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002786 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002787 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002788 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002789 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002790
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002791 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002792 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002793 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002794
mohammad16035955c982018-04-26 00:53:03 +03002795 if( alg == PSA_ALG_GCM )
2796 {
2797 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002798 tag_length = 16;
2799
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002800 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002801 return( PSA_ERROR_INVALID_ARGUMENT );
2802
mohammad160315223a82018-06-03 17:19:55 +03002803 //make sure we have place to hold the tag in the ciphertext buffer
2804 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002805 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002806
2807 //update the tag pointer to point to the end of the ciphertext_length
2808 tag = ciphertext + plaintext_length;
2809
mohammad16035955c982018-04-26 00:53:03 +03002810 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002811 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002812 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002813 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002814 if( ret != 0 )
2815 {
2816 mbedtls_gcm_free( &gcm );
2817 return( mbedtls_to_psa_error( ret ) );
2818 }
2819 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002820 plaintext_length, nonce,
2821 nonce_length, additional_data,
2822 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002823 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002824 mbedtls_gcm_free( &gcm );
2825 }
2826 else if( alg == PSA_ALG_CCM )
2827 {
2828 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002829 tag_length = 16;
2830
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002831 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002832 return( PSA_ERROR_INVALID_ARGUMENT );
2833
mohammad160347ddf3d2018-04-26 01:11:21 +03002834 if( nonce_length < 7 || nonce_length > 13 )
2835 return( PSA_ERROR_INVALID_ARGUMENT );
2836
mohammad160315223a82018-06-03 17:19:55 +03002837 //make sure we have place to hold the tag in the ciphertext buffer
2838 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002839 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002840
2841 //update the tag pointer to point to the end of the ciphertext_length
2842 tag = ciphertext + plaintext_length;
2843
mohammad16035955c982018-04-26 00:53:03 +03002844 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002845 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002846 slot->data.raw.data,
2847 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002848 if( ret != 0 )
2849 {
2850 mbedtls_ccm_free( &ccm );
2851 return( mbedtls_to_psa_error( ret ) );
2852 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002853 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002854 nonce, nonce_length,
2855 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002856 additional_data_length,
2857 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002858 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002859 mbedtls_ccm_free( &ccm );
2860 }
mohammad16035c8845f2018-05-09 05:40:09 -07002861 else
2862 {
mohammad1603554faad2018-06-03 15:07:38 +03002863 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002864 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002865
mohammad160315223a82018-06-03 17:19:55 +03002866 if( ret != 0 )
2867 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002868 /* If ciphertext_size is 0 then ciphertext may be NULL and then the
2869 * call to memset would have undefined behavior. */
2870 if( ciphertext_size != 0 )
2871 memset( ciphertext, 0, ciphertext_size );
mohammad160315223a82018-06-03 17:19:55 +03002872 return( mbedtls_to_psa_error( ret ) );
2873 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002874
mohammad160315223a82018-06-03 17:19:55 +03002875 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002876 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002877}
2878
Gilles Peskineee652a32018-06-01 19:23:52 +02002879/* Locate the tag in a ciphertext buffer containing the encrypted data
2880 * followed by the tag. Return the length of the part preceding the tag in
2881 * *plaintext_length. This is the size of the plaintext in modes where
2882 * the encrypted data has the same size as the plaintext, such as
2883 * CCM and GCM. */
2884static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2885 const uint8_t *ciphertext,
2886 size_t ciphertext_length,
2887 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002888 const uint8_t **p_tag )
2889{
2890 size_t payload_length;
2891 if( tag_length > ciphertext_length )
2892 return( PSA_ERROR_INVALID_ARGUMENT );
2893 payload_length = ciphertext_length - tag_length;
2894 if( payload_length > plaintext_size )
2895 return( PSA_ERROR_BUFFER_TOO_SMALL );
2896 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002897 return( PSA_SUCCESS );
2898}
2899
mohammad16035955c982018-04-26 00:53:03 +03002900psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2901 psa_algorithm_t alg,
2902 const uint8_t *nonce,
2903 size_t nonce_length,
2904 const uint8_t *additional_data,
2905 size_t additional_data_length,
2906 const uint8_t *ciphertext,
2907 size_t ciphertext_length,
2908 uint8_t *plaintext,
2909 size_t plaintext_size,
2910 size_t *plaintext_length )
2911{
2912 int ret;
2913 psa_status_t status;
2914 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002915 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002916 const uint8_t *tag;
2917 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002918 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002919 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002920
Gilles Peskineee652a32018-06-01 19:23:52 +02002921 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002922
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002923 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2924 if( status != PSA_SUCCESS )
2925 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002926 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002927
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002928 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002929 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002930 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002931 return( PSA_ERROR_NOT_SUPPORTED );
2932
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002933 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002934 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002935 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002936
mohammad16035955c982018-04-26 00:53:03 +03002937 if( alg == PSA_ALG_GCM )
2938 {
2939 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002940
Gilles Peskineee652a32018-06-01 19:23:52 +02002941 tag_length = 16;
2942 status = psa_aead_unpadded_locate_tag( tag_length,
2943 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002944 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002945 if( status != PSA_SUCCESS )
2946 return( status );
2947
mohammad16035955c982018-04-26 00:53:03 +03002948 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002949 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002950 slot->data.raw.data,
2951 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002952 if( ret != 0 )
2953 {
2954 mbedtls_gcm_free( &gcm );
2955 return( mbedtls_to_psa_error( ret ) );
2956 }
mohammad16035955c982018-04-26 00:53:03 +03002957
Gilles Peskineee652a32018-06-01 19:23:52 +02002958 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002959 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002960 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002961 additional_data,
2962 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002963 tag, tag_length,
2964 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002965 mbedtls_gcm_free( &gcm );
2966 }
2967 else if( alg == PSA_ALG_CCM )
2968 {
2969 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002970
mohammad160347ddf3d2018-04-26 01:11:21 +03002971 if( nonce_length < 7 || nonce_length > 13 )
2972 return( PSA_ERROR_INVALID_ARGUMENT );
2973
mohammad16039375f842018-06-03 14:28:24 +03002974 tag_length = 16;
2975 status = psa_aead_unpadded_locate_tag( tag_length,
2976 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002977 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002978 if( status != PSA_SUCCESS )
2979 return( status );
2980
mohammad16035955c982018-04-26 00:53:03 +03002981 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002982 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002983 slot->data.raw.data,
2984 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002985 if( ret != 0 )
2986 {
2987 mbedtls_ccm_free( &ccm );
2988 return( mbedtls_to_psa_error( ret ) );
2989 }
mohammad160360a64d02018-06-03 17:20:42 +03002990 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002991 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002992 additional_data,
2993 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002994 ciphertext, plaintext,
2995 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002996 mbedtls_ccm_free( &ccm );
2997 }
mohammad160339574652018-06-01 04:39:53 -07002998 else
2999 {
mohammad1603554faad2018-06-03 15:07:38 +03003000 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003001 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003002
Gilles Peskineee652a32018-06-01 19:23:52 +02003003 if( ret != 0 )
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003004 {
3005 /* If plaintext_size is 0 then plaintext may be NULL and then the
3006 * call to memset has undefined behavior. */
3007 if( plaintext_size != 0 )
3008 memset( plaintext, 0, plaintext_size );
3009 }
mohammad160360a64d02018-06-03 17:20:42 +03003010 else
3011 *plaintext_length = ciphertext_length - tag_length;
3012
Gilles Peskineee652a32018-06-01 19:23:52 +02003013 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03003014}
3015
Gilles Peskinea0655c32018-04-30 17:06:50 +02003016
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003017
Gilles Peskine20035e32018-02-03 22:44:14 +01003018/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003019/* Generators */
3020/****************************************************************/
3021
3022psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3023{
3024 psa_status_t status = PSA_SUCCESS;
3025 if( generator->alg == 0 )
3026 {
3027 /* The object has (apparently) been initialized but it is not
3028 * in use. It's ok to call abort on such an object, and there's
3029 * nothing to do. */
3030 }
3031 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003032#if defined(MBEDTLS_MD_C)
3033 if( PSA_ALG_IS_HKDF( generator->alg ) )
3034 {
3035 mbedtls_free( generator->ctx.hkdf.info );
3036 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3037 }
3038 else
3039#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003040 {
3041 status = PSA_ERROR_BAD_STATE;
3042 }
3043 memset( generator, 0, sizeof( *generator ) );
3044 return( status );
3045}
3046
3047
3048psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3049 size_t *capacity)
3050{
3051 *capacity = generator->capacity;
3052 return( PSA_SUCCESS );
3053}
3054
Gilles Peskinebef7f142018-07-12 17:22:21 +02003055#if defined(MBEDTLS_MD_C)
3056/* Read some bytes from an HKDF-based generator. This performs a chunk
3057 * of the expand phase of the HKDF algorithm. */
3058static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3059 psa_algorithm_t hash_alg,
3060 uint8_t *output,
3061 size_t output_length )
3062{
3063 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3064 psa_status_t status;
3065
3066 while( output_length != 0 )
3067 {
3068 /* Copy what remains of the current block */
3069 uint8_t n = hash_length - hkdf->offset_in_block;
3070 if( n > output_length )
3071 n = (uint8_t) output_length;
3072 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3073 output += n;
3074 output_length -= n;
3075 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003076 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003077 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003078 /* We can't be wanting more output after block 0xff, otherwise
3079 * the capacity check in psa_generator_read() would have
3080 * prevented this call. It could happen only if the generator
3081 * object was corrupted or if this function is called directly
3082 * inside the library. */
3083 if( hkdf->block_number == 0xff )
3084 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003085
3086 /* We need a new block */
3087 ++hkdf->block_number;
3088 hkdf->offset_in_block = 0;
3089 status = psa_hmac_setup_internal( &hkdf->hmac,
3090 hkdf->prk, hash_length,
3091 hash_alg );
3092 if( status != PSA_SUCCESS )
3093 return( status );
3094 if( hkdf->block_number != 1 )
3095 {
3096 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3097 hkdf->output_block,
3098 hash_length );
3099 if( status != PSA_SUCCESS )
3100 return( status );
3101 }
3102 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3103 hkdf->info,
3104 hkdf->info_length );
3105 if( status != PSA_SUCCESS )
3106 return( status );
3107 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3108 &hkdf->block_number, 1 );
3109 if( status != PSA_SUCCESS )
3110 return( status );
3111 status = psa_hmac_finish_internal( &hkdf->hmac,
3112 hkdf->output_block,
3113 sizeof( hkdf->output_block ) );
3114 if( status != PSA_SUCCESS )
3115 return( status );
3116 }
3117
3118 return( PSA_SUCCESS );
3119}
3120#endif /* MBEDTLS_MD_C */
3121
Gilles Peskineeab56e42018-07-12 17:12:33 +02003122psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3123 uint8_t *output,
3124 size_t output_length )
3125{
3126 psa_status_t status;
3127
3128 if( output_length > generator->capacity )
3129 {
3130 generator->capacity = 0;
3131 /* Go through the error path to wipe all confidential data now
3132 * that the generator object is useless. */
3133 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3134 goto exit;
3135 }
3136 if( output_length == 0 &&
3137 generator->capacity == 0 && generator->alg == 0 )
3138 {
3139 /* Edge case: this is a blank or finished generator, and 0
3140 * bytes were requested. The right error in this case could
3141 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3142 * INSUFFICIENT_CAPACITY, which is right for a finished
3143 * generator, for consistency with the case when
3144 * output_length > 0. */
3145 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3146 }
3147 generator->capacity -= output_length;
3148
Gilles Peskinebef7f142018-07-12 17:22:21 +02003149#if defined(MBEDTLS_MD_C)
3150 if( PSA_ALG_IS_HKDF( generator->alg ) )
3151 {
3152 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3153 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3154 output, output_length );
3155 }
3156 else
3157#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003158 {
3159 return( PSA_ERROR_BAD_STATE );
3160 }
3161
3162exit:
3163 if( status != PSA_SUCCESS )
3164 {
3165 psa_generator_abort( generator );
3166 memset( output, '!', output_length );
3167 }
3168 return( status );
3169}
3170
Gilles Peskine08542d82018-07-19 17:05:42 +02003171#if defined(MBEDTLS_DES_C)
3172static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3173{
3174 if( data_size >= 8 )
3175 mbedtls_des_key_set_parity( data );
3176 if( data_size >= 16 )
3177 mbedtls_des_key_set_parity( data + 8 );
3178 if( data_size >= 24 )
3179 mbedtls_des_key_set_parity( data + 16 );
3180}
3181#endif /* MBEDTLS_DES_C */
3182
Gilles Peskineeab56e42018-07-12 17:12:33 +02003183psa_status_t psa_generator_import_key( psa_key_slot_t key,
3184 psa_key_type_t type,
3185 size_t bits,
3186 psa_crypto_generator_t *generator )
3187{
3188 uint8_t *data = NULL;
3189 size_t bytes = PSA_BITS_TO_BYTES( bits );
3190 psa_status_t status;
3191
3192 if( ! key_type_is_raw_bytes( type ) )
3193 return( PSA_ERROR_INVALID_ARGUMENT );
3194 if( bits % 8 != 0 )
3195 return( PSA_ERROR_INVALID_ARGUMENT );
3196 data = mbedtls_calloc( 1, bytes );
3197 if( data == NULL )
3198 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3199
3200 status = psa_generator_read( generator, data, bytes );
3201 if( status != PSA_SUCCESS )
3202 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003203#if defined(MBEDTLS_DES_C)
3204 if( type == PSA_KEY_TYPE_DES )
3205 psa_des_set_key_parity( data, bytes );
3206#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003207 status = psa_import_key( key, type, data, bytes );
3208
3209exit:
3210 mbedtls_free( data );
3211 return( status );
3212}
3213
3214
3215
3216/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003217/* Key derivation */
3218/****************************************************************/
3219
Gilles Peskinebef7f142018-07-12 17:22:21 +02003220/* Set up an HKDF-based generator. This is exactly the extract phase
3221 * of the HKDF algorithm. */
3222static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
3223 key_slot_t *slot,
3224 psa_algorithm_t hash_alg,
3225 const uint8_t *salt,
3226 size_t salt_length,
3227 const uint8_t *label,
3228 size_t label_length )
3229{
3230 psa_status_t status;
3231 status = psa_hmac_setup_internal( &hkdf->hmac,
3232 salt, salt_length,
3233 PSA_ALG_HMAC_HASH( hash_alg ) );
3234 if( status != PSA_SUCCESS )
3235 return( status );
3236 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3237 slot->data.raw.data,
3238 slot->data.raw.bytes );
3239 if( status != PSA_SUCCESS )
3240 return( status );
3241 status = psa_hmac_finish_internal( &hkdf->hmac,
3242 hkdf->prk,
3243 sizeof( hkdf->prk ) );
3244 if( status != PSA_SUCCESS )
3245 return( status );
3246 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3247 hkdf->block_number = 0;
3248 hkdf->info_length = label_length;
3249 if( label_length != 0 )
3250 {
3251 hkdf->info = mbedtls_calloc( 1, label_length );
3252 if( hkdf->info == NULL )
3253 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3254 memcpy( hkdf->info, label, label_length );
3255 }
3256 return( PSA_SUCCESS );
3257}
3258
Gilles Peskineea0fb492018-07-12 17:17:20 +02003259psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
3260 psa_key_type_t key,
3261 psa_algorithm_t alg,
3262 const uint8_t *salt,
3263 size_t salt_length,
3264 const uint8_t *label,
3265 size_t label_length,
3266 size_t capacity )
3267{
3268 key_slot_t *slot;
3269 psa_status_t status;
3270
3271 if( generator->alg != 0 )
3272 return( PSA_ERROR_BAD_STATE );
3273
3274 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
3275 if( status != PSA_SUCCESS )
3276 return( status );
3277 if( slot->type != PSA_KEY_TYPE_DERIVE )
3278 return( PSA_ERROR_INVALID_ARGUMENT );
3279
3280 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
3281 return( PSA_ERROR_INVALID_ARGUMENT );
3282
Gilles Peskinebef7f142018-07-12 17:22:21 +02003283#if defined(MBEDTLS_MD_C)
3284 if( PSA_ALG_IS_HKDF( alg ) )
3285 {
3286 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3287 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3288 if( hash_size == 0 )
3289 return( PSA_ERROR_NOT_SUPPORTED );
3290 if( capacity > 255 * hash_size )
3291 return( PSA_ERROR_INVALID_ARGUMENT );
3292 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3293 slot,
3294 hash_alg,
3295 salt, salt_length,
3296 label, label_length );
3297 }
3298 else
3299#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +02003300 {
3301 return( PSA_ERROR_NOT_SUPPORTED );
3302 }
3303
3304 /* Set generator->alg even on failure so that abort knows what to do. */
3305 generator->alg = alg;
3306 if( status == PSA_SUCCESS )
3307 generator->capacity = capacity;
3308 else
3309 psa_generator_abort( generator );
3310 return( status );
3311}
3312
3313
3314
3315/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003316/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02003317/****************************************************************/
3318
3319psa_status_t psa_generate_random( uint8_t *output,
3320 size_t output_size )
3321{
3322 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3323 output, output_size );
3324 return( mbedtls_to_psa_error( ret ) );
3325}
3326
3327psa_status_t psa_generate_key( psa_key_slot_t key,
3328 psa_key_type_t type,
3329 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003330 const void *extra,
3331 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02003332{
Gilles Peskine12313cd2018-06-20 00:20:32 +02003333 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003334 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003335
Gilles Peskine53d991e2018-07-12 01:14:59 +02003336 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003337 return( PSA_ERROR_INVALID_ARGUMENT );
3338
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003339 status = psa_get_empty_key_slot( key, &slot );
3340 if( status != PSA_SUCCESS )
3341 return( status );
3342
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003343 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003344 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003345 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003346 if( status != PSA_SUCCESS )
3347 return( status );
3348 status = psa_generate_random( slot->data.raw.data,
3349 slot->data.raw.bytes );
3350 if( status != PSA_SUCCESS )
3351 {
3352 mbedtls_free( slot->data.raw.data );
3353 return( status );
3354 }
3355#if defined(MBEDTLS_DES_C)
3356 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02003357 psa_des_set_key_parity( slot->data.raw.data,
3358 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003359#endif /* MBEDTLS_DES_C */
3360 }
3361 else
3362
3363#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3364 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3365 {
3366 mbedtls_rsa_context *rsa;
3367 int ret;
3368 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003369 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3370 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02003371 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003372 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02003373 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003374 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003375 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003376#if INT_MAX < 0xffffffff
3377 /* Check that the uint32_t value passed by the caller fits
3378 * in the range supported by this implementation. */
3379 if( p->e > INT_MAX )
3380 return( PSA_ERROR_NOT_SUPPORTED );
3381#endif
3382 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003383 }
3384 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3385 if( rsa == NULL )
3386 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3387 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3388 ret = mbedtls_rsa_gen_key( rsa,
3389 mbedtls_ctr_drbg_random,
3390 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003391 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003392 exponent );
3393 if( ret != 0 )
3394 {
3395 mbedtls_rsa_free( rsa );
3396 mbedtls_free( rsa );
3397 return( mbedtls_to_psa_error( ret ) );
3398 }
3399 slot->data.rsa = rsa;
3400 }
3401 else
3402#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3403
3404#if defined(MBEDTLS_ECP_C)
3405 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3406 {
3407 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3408 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3409 const mbedtls_ecp_curve_info *curve_info =
3410 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3411 mbedtls_ecp_keypair *ecp;
3412 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003413 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003414 return( PSA_ERROR_NOT_SUPPORTED );
3415 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3416 return( PSA_ERROR_NOT_SUPPORTED );
3417 if( curve_info->bit_size != bits )
3418 return( PSA_ERROR_INVALID_ARGUMENT );
3419 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3420 if( ecp == NULL )
3421 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3422 mbedtls_ecp_keypair_init( ecp );
3423 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3424 mbedtls_ctr_drbg_random,
3425 &global_data.ctr_drbg );
3426 if( ret != 0 )
3427 {
3428 mbedtls_ecp_keypair_free( ecp );
3429 mbedtls_free( ecp );
3430 return( mbedtls_to_psa_error( ret ) );
3431 }
3432 slot->data.ecp = ecp;
3433 }
3434 else
3435#endif /* MBEDTLS_ECP_C */
3436
3437 return( PSA_ERROR_NOT_SUPPORTED );
3438
3439 slot->type = type;
3440 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003441}
3442
3443
3444/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003445/* Module setup */
3446/****************************************************************/
3447
Gilles Peskinee59236f2018-01-27 23:32:46 +01003448void mbedtls_psa_crypto_free( void )
3449{
Jaeden Amero045bd502018-06-26 14:00:08 +01003450 psa_key_slot_t key;
Gilles Peskine828ed142018-06-18 23:25:51 +02003451 for( key = 1; key < PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003452 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003453 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3454 mbedtls_entropy_free( &global_data.entropy );
3455 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3456}
3457
3458psa_status_t psa_crypto_init( void )
3459{
3460 int ret;
3461 const unsigned char drbg_seed[] = "PSA";
3462
3463 if( global_data.initialized != 0 )
3464 return( PSA_SUCCESS );
3465
3466 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3467 mbedtls_entropy_init( &global_data.entropy );
3468 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3469
3470 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3471 mbedtls_entropy_func,
3472 &global_data.entropy,
3473 drbg_seed, sizeof( drbg_seed ) - 1 );
3474 if( ret != 0 )
3475 goto exit;
3476
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003477 global_data.initialized = 1;
3478
Gilles Peskinee59236f2018-01-27 23:32:46 +01003479exit:
3480 if( ret != 0 )
3481 mbedtls_psa_crypto_free( );
3482 return( mbedtls_to_psa_error( ret ) );
3483}
3484
3485#endif /* MBEDTLS_PSA_CRYPTO_C */