blob: 61eef45ca5c452c5a4d04ce747605fb47e27bb9c [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
374/* Retrieve a slot which must contain a key. The key must have allow all
375 * the usage flags set in \p usage. If \p alg is nonzero, the key must
376 * allow operations with this algorithm. */
377static 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 Peskine0ff4b0f2018-06-19 21:31:50 +0200501 break;
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200502#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200503#if defined(MBEDTLS_AES_C)
504 case PSA_KEY_TYPE_AES:
505 if( bits != 128 && bits != 192 && bits != 256 )
506 return( PSA_ERROR_INVALID_ARGUMENT );
507 break;
508#endif
509#if defined(MBEDTLS_CAMELLIA_C)
510 case PSA_KEY_TYPE_CAMELLIA:
511 if( bits != 128 && bits != 192 && bits != 256 )
512 return( PSA_ERROR_INVALID_ARGUMENT );
513 break;
514#endif
515#if defined(MBEDTLS_DES_C)
516 case PSA_KEY_TYPE_DES:
517 if( bits != 64 && bits != 128 && bits != 192 )
518 return( PSA_ERROR_INVALID_ARGUMENT );
519 break;
520#endif
521#if defined(MBEDTLS_ARC4_C)
522 case PSA_KEY_TYPE_ARC4:
523 if( bits < 8 || bits > 2048 )
524 return( PSA_ERROR_INVALID_ARGUMENT );
525 break;
526#endif
527 default:
528 return( PSA_ERROR_NOT_SUPPORTED );
529 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200530 if( bits % 8 != 0 )
531 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200532
533 /* Allocate memory for the key */
534 raw->bytes = PSA_BITS_TO_BYTES( bits );
535 raw->data = mbedtls_calloc( 1, raw->bytes );
536 if( raw->data == NULL )
537 {
538 raw->bytes = 0;
539 return( PSA_ERROR_INSUFFICIENT_MEMORY );
540 }
541 return( PSA_SUCCESS );
542}
543
Gilles Peskine2d277862018-06-18 15:41:12 +0200544psa_status_t psa_import_key( psa_key_slot_t key,
545 psa_key_type_t type,
546 const uint8_t *data,
547 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100548{
549 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200550 psa_status_t status = PSA_SUCCESS;
551 status = psa_get_empty_key_slot( key, &slot );
552 if( status != PSA_SUCCESS )
553 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100554
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200555 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100556 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100557 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100558 if( data_length > SIZE_MAX / 8 )
559 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200560 status = prepare_raw_data_slot( type,
561 PSA_BYTES_TO_BITS( data_length ),
562 &slot->data.raw );
563 if( status != PSA_SUCCESS )
564 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200565 if( data_length != 0 )
566 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100567 }
568 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100569#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100570 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
571 type == PSA_KEY_TYPE_RSA_KEYPAIR ||
572 PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573 {
574 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100575 mbedtls_pk_context pk;
576 mbedtls_pk_init( &pk );
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100577 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
578 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
579 else
580 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100581 if( ret != 0 )
582 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine969ac722018-01-28 18:16:59 +0100583 switch( mbedtls_pk_get_type( &pk ) )
584 {
585#if defined(MBEDTLS_RSA_C)
586 case MBEDTLS_PK_RSA:
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100587 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
588 type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200589 {
590 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
591 size_t bits = mbedtls_rsa_get_bitlen( rsa );
592 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
Gilles Peskine1ae05142018-06-30 17:46:59 +0200593 {
594 status = PSA_ERROR_NOT_SUPPORTED;
595 break;
596 }
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200597 slot->data.rsa = rsa;
598 }
Gilles Peskine969ac722018-01-28 18:16:59 +0100599 else
Gilles Peskinec648d692018-06-28 08:46:13 +0200600 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine969ac722018-01-28 18:16:59 +0100601 break;
602#endif /* MBEDTLS_RSA_C */
603#if defined(MBEDTLS_ECP_C)
604 case MBEDTLS_PK_ECKEY:
605 if( PSA_KEY_TYPE_IS_ECC( type ) )
606 {
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200607 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
608 psa_ecc_curve_t actual_curve =
609 mbedtls_ecc_group_to_psa( ecp->grp.id );
610 psa_ecc_curve_t expected_curve =
611 PSA_KEY_TYPE_GET_CURVE( type );
612 if( actual_curve != expected_curve )
Gilles Peskinec648d692018-06-28 08:46:13 +0200613 {
614 status = PSA_ERROR_INVALID_ARGUMENT;
615 break;
616 }
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200617 slot->data.ecp = ecp;
Gilles Peskine969ac722018-01-28 18:16:59 +0100618 }
619 else
Gilles Peskinec648d692018-06-28 08:46:13 +0200620 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine969ac722018-01-28 18:16:59 +0100621 break;
622#endif /* MBEDTLS_ECP_C */
623 default:
Gilles Peskinec648d692018-06-28 08:46:13 +0200624 status = PSA_ERROR_INVALID_ARGUMENT;
625 break;
626 }
627 /* Free the content of the pk object only on error. On success,
628 * the content of the object has been stored in the slot. */
629 if( status != PSA_SUCCESS )
630 {
631 mbedtls_pk_free( &pk );
632 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100633 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100634 }
635 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100636#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100637 {
638 return( PSA_ERROR_NOT_SUPPORTED );
639 }
640
641 slot->type = type;
642 return( PSA_SUCCESS );
643}
644
Gilles Peskine2d277862018-06-18 15:41:12 +0200645psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100646{
647 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200648 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100649
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200650 status = psa_get_key_slot( key, &slot );
651 if( status != PSA_SUCCESS )
652 return( status );
653
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100654 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200655 {
656 /* No key material to clean, but do zeroize the slot below to wipe
657 * metadata such as policies. */
658 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200659 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100660 {
661 mbedtls_free( slot->data.raw.data );
662 }
663 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100664#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100665 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
666 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100667 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100668 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100669 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100670 }
671 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100672#endif /* defined(MBEDTLS_RSA_C) */
673#if defined(MBEDTLS_ECP_C)
674 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
675 {
676 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100677 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100678 }
679 else
680#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100681 {
682 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100683 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100684 return( PSA_ERROR_TAMPERING_DETECTED );
685 }
686
687 mbedtls_zeroize( slot, sizeof( *slot ) );
688 return( PSA_SUCCESS );
689}
690
Gilles Peskineb870b182018-07-06 16:02:09 +0200691/* Return the size of the key in the given slot, in bits. */
692static size_t psa_get_key_bits( const key_slot_t *slot )
693{
694 if( key_type_is_raw_bytes( slot->type ) )
695 return( slot->data.raw.bytes * 8 );
696#if defined(MBEDTLS_RSA_C)
697 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
698 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
699 return( mbedtls_rsa_get_bitlen( slot->data.rsa ) );
700#endif /* defined(MBEDTLS_RSA_C) */
701#if defined(MBEDTLS_ECP_C)
702 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
703 return( slot->data.ecp->grp.pbits );
704#endif /* defined(MBEDTLS_ECP_C) */
705 /* Shouldn't happen except on an empty slot. */
706 return( 0 );
707}
708
Gilles Peskine2d277862018-06-18 15:41:12 +0200709psa_status_t psa_get_key_information( psa_key_slot_t key,
710 psa_key_type_t *type,
711 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100712{
713 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200714 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100715
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100716 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200717 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100718 if( bits != NULL )
719 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200720 status = psa_get_key_slot( key, &slot );
721 if( status != PSA_SUCCESS )
722 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200723
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100724 if( slot->type == PSA_KEY_TYPE_NONE )
725 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200726 if( type != NULL )
727 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200728 if( bits != NULL )
729 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100730 return( PSA_SUCCESS );
731}
732
Gilles Peskine2d277862018-06-18 15:41:12 +0200733static psa_status_t psa_internal_export_key( psa_key_slot_t key,
734 uint8_t *data,
735 size_t data_size,
736 size_t *data_length,
737 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738{
739 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200740 psa_status_t status;
741 /* Exporting a public key doesn't require a usage flag. If we're
742 * called by psa_export_public_key(), don't require the EXPORT flag.
743 * If we're called by psa_export_key(), do require the EXPORT flag;
744 * if the key turns out to be public key object, psa_get_key_from_slot()
745 * will ignore this flag. */
746 psa_key_usage_t usage = export_public_key ? 0 : PSA_KEY_USAGE_EXPORT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100747
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100748 /* Set the key to empty now, so that even when there are errors, we always
749 * set data_length to a value between 0 and data_size. On error, setting
750 * the key to empty is a good choice because an empty key representation is
751 * unlikely to be accepted anywhere. */
752 *data_length = 0;
753
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200754 status = psa_get_key_from_slot( key, &slot, usage, 0 );
755 if( status != PSA_SUCCESS )
756 return( status );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200757 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300758 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300759
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200760 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100761 {
762 if( slot->data.raw.bytes > data_size )
763 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200764 if( slot->data.raw.bytes != 0 )
765 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100766 *data_length = slot->data.raw.bytes;
767 return( PSA_SUCCESS );
768 }
769 else
Moran Peker17e36e12018-05-02 12:55:20 +0300770 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100771#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100772 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
Moran Pekera998bc62018-04-16 18:16:20 +0300773 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
774 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100775 {
Moran Pekera998bc62018-04-16 18:16:20 +0300776 mbedtls_pk_context pk;
777 int ret;
778 mbedtls_pk_init( &pk );
779 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
780 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
781 {
782 pk.pk_info = &mbedtls_rsa_info;
783 pk.pk_ctx = slot->data.rsa;
784 }
785 else
786 {
787 pk.pk_info = &mbedtls_eckey_info;
788 pk.pk_ctx = slot->data.ecp;
789 }
Moran Pekerd7326592018-05-29 16:56:39 +0300790 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300791 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300792 else
793 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300794 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200795 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200796 /* If data_size is 0 then data may be NULL and then the
797 * call to memset would have undefined behavior. */
798 if( data_size != 0 )
799 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300800 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200801 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200802 /* The mbedtls_pk_xxx functions write to the end of the buffer.
803 * Move the data to the beginning and erase remaining data
804 * at the original location. */
805 if( 2 * (size_t) ret <= data_size )
806 {
807 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200808 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200809 }
810 else if( (size_t) ret < data_size )
811 {
812 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200813 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200814 }
Moran Pekera998bc62018-04-16 18:16:20 +0300815 *data_length = ret;
816 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100817 }
818 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100819#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300820 {
821 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200822 it is valid for a special-purpose implementation to omit
823 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300824 return( PSA_ERROR_NOT_SUPPORTED );
825 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100826 }
827}
828
Gilles Peskine2d277862018-06-18 15:41:12 +0200829psa_status_t psa_export_key( psa_key_slot_t key,
830 uint8_t *data,
831 size_t data_size,
832 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300833{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200834 return( psa_internal_export_key( key, data, data_size,
835 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100836}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837
Gilles Peskine2d277862018-06-18 15:41:12 +0200838psa_status_t psa_export_public_key( psa_key_slot_t key,
839 uint8_t *data,
840 size_t data_size,
841 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300842{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200843 return( psa_internal_export_key( key, data, data_size,
844 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300845}
846
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200847
848
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100849/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100850/* Message digests */
851/****************************************************************/
852
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100853static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100854{
855 switch( alg )
856 {
857#if defined(MBEDTLS_MD2_C)
858 case PSA_ALG_MD2:
859 return( &mbedtls_md2_info );
860#endif
861#if defined(MBEDTLS_MD4_C)
862 case PSA_ALG_MD4:
863 return( &mbedtls_md4_info );
864#endif
865#if defined(MBEDTLS_MD5_C)
866 case PSA_ALG_MD5:
867 return( &mbedtls_md5_info );
868#endif
869#if defined(MBEDTLS_RIPEMD160_C)
870 case PSA_ALG_RIPEMD160:
871 return( &mbedtls_ripemd160_info );
872#endif
873#if defined(MBEDTLS_SHA1_C)
874 case PSA_ALG_SHA_1:
875 return( &mbedtls_sha1_info );
876#endif
877#if defined(MBEDTLS_SHA256_C)
878 case PSA_ALG_SHA_224:
879 return( &mbedtls_sha224_info );
880 case PSA_ALG_SHA_256:
881 return( &mbedtls_sha256_info );
882#endif
883#if defined(MBEDTLS_SHA512_C)
884 case PSA_ALG_SHA_384:
885 return( &mbedtls_sha384_info );
886 case PSA_ALG_SHA_512:
887 return( &mbedtls_sha512_info );
888#endif
889 default:
890 return( NULL );
891 }
892}
893
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100894psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
895{
896 switch( operation->alg )
897 {
Gilles Peskine81736312018-06-26 15:04:31 +0200898 case 0:
899 /* The object has (apparently) been initialized but it is not
900 * in use. It's ok to call abort on such an object, and there's
901 * nothing to do. */
902 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100903#if defined(MBEDTLS_MD2_C)
904 case PSA_ALG_MD2:
905 mbedtls_md2_free( &operation->ctx.md2 );
906 break;
907#endif
908#if defined(MBEDTLS_MD4_C)
909 case PSA_ALG_MD4:
910 mbedtls_md4_free( &operation->ctx.md4 );
911 break;
912#endif
913#if defined(MBEDTLS_MD5_C)
914 case PSA_ALG_MD5:
915 mbedtls_md5_free( &operation->ctx.md5 );
916 break;
917#endif
918#if defined(MBEDTLS_RIPEMD160_C)
919 case PSA_ALG_RIPEMD160:
920 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
921 break;
922#endif
923#if defined(MBEDTLS_SHA1_C)
924 case PSA_ALG_SHA_1:
925 mbedtls_sha1_free( &operation->ctx.sha1 );
926 break;
927#endif
928#if defined(MBEDTLS_SHA256_C)
929 case PSA_ALG_SHA_224:
930 case PSA_ALG_SHA_256:
931 mbedtls_sha256_free( &operation->ctx.sha256 );
932 break;
933#endif
934#if defined(MBEDTLS_SHA512_C)
935 case PSA_ALG_SHA_384:
936 case PSA_ALG_SHA_512:
937 mbedtls_sha512_free( &operation->ctx.sha512 );
938 break;
939#endif
940 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +0200941 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100942 }
943 operation->alg = 0;
944 return( PSA_SUCCESS );
945}
946
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200947psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100948 psa_algorithm_t alg )
949{
950 int ret;
951 operation->alg = 0;
952 switch( alg )
953 {
954#if defined(MBEDTLS_MD2_C)
955 case PSA_ALG_MD2:
956 mbedtls_md2_init( &operation->ctx.md2 );
957 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
958 break;
959#endif
960#if defined(MBEDTLS_MD4_C)
961 case PSA_ALG_MD4:
962 mbedtls_md4_init( &operation->ctx.md4 );
963 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
964 break;
965#endif
966#if defined(MBEDTLS_MD5_C)
967 case PSA_ALG_MD5:
968 mbedtls_md5_init( &operation->ctx.md5 );
969 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
970 break;
971#endif
972#if defined(MBEDTLS_RIPEMD160_C)
973 case PSA_ALG_RIPEMD160:
974 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
975 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
976 break;
977#endif
978#if defined(MBEDTLS_SHA1_C)
979 case PSA_ALG_SHA_1:
980 mbedtls_sha1_init( &operation->ctx.sha1 );
981 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
982 break;
983#endif
984#if defined(MBEDTLS_SHA256_C)
985 case PSA_ALG_SHA_224:
986 mbedtls_sha256_init( &operation->ctx.sha256 );
987 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
988 break;
989 case PSA_ALG_SHA_256:
990 mbedtls_sha256_init( &operation->ctx.sha256 );
991 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
992 break;
993#endif
994#if defined(MBEDTLS_SHA512_C)
995 case PSA_ALG_SHA_384:
996 mbedtls_sha512_init( &operation->ctx.sha512 );
997 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
998 break;
999 case PSA_ALG_SHA_512:
1000 mbedtls_sha512_init( &operation->ctx.sha512 );
1001 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1002 break;
1003#endif
1004 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001005 return( PSA_ALG_IS_HASH( alg ) ?
1006 PSA_ERROR_NOT_SUPPORTED :
1007 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001008 }
1009 if( ret == 0 )
1010 operation->alg = alg;
1011 else
1012 psa_hash_abort( operation );
1013 return( mbedtls_to_psa_error( ret ) );
1014}
1015
1016psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1017 const uint8_t *input,
1018 size_t input_length )
1019{
1020 int ret;
1021 switch( operation->alg )
1022 {
1023#if defined(MBEDTLS_MD2_C)
1024 case PSA_ALG_MD2:
1025 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1026 input, input_length );
1027 break;
1028#endif
1029#if defined(MBEDTLS_MD4_C)
1030 case PSA_ALG_MD4:
1031 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1032 input, input_length );
1033 break;
1034#endif
1035#if defined(MBEDTLS_MD5_C)
1036 case PSA_ALG_MD5:
1037 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1038 input, input_length );
1039 break;
1040#endif
1041#if defined(MBEDTLS_RIPEMD160_C)
1042 case PSA_ALG_RIPEMD160:
1043 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1044 input, input_length );
1045 break;
1046#endif
1047#if defined(MBEDTLS_SHA1_C)
1048 case PSA_ALG_SHA_1:
1049 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1050 input, input_length );
1051 break;
1052#endif
1053#if defined(MBEDTLS_SHA256_C)
1054 case PSA_ALG_SHA_224:
1055 case PSA_ALG_SHA_256:
1056 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1057 input, input_length );
1058 break;
1059#endif
1060#if defined(MBEDTLS_SHA512_C)
1061 case PSA_ALG_SHA_384:
1062 case PSA_ALG_SHA_512:
1063 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1064 input, input_length );
1065 break;
1066#endif
1067 default:
1068 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1069 break;
1070 }
1071 if( ret != 0 )
1072 psa_hash_abort( operation );
1073 return( mbedtls_to_psa_error( ret ) );
1074}
1075
1076psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1077 uint8_t *hash,
1078 size_t hash_size,
1079 size_t *hash_length )
1080{
1081 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001082 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001083
1084 /* Fill the output buffer with something that isn't a valid hash
1085 * (barring an attack on the hash and deliberately-crafted input),
1086 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001087 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001088 /* If hash_size is 0 then hash may be NULL and then the
1089 * call to memset would have undefined behavior. */
1090 if( hash_size != 0 )
1091 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001092
1093 if( hash_size < actual_hash_length )
1094 return( PSA_ERROR_BUFFER_TOO_SMALL );
1095
1096 switch( operation->alg )
1097 {
1098#if defined(MBEDTLS_MD2_C)
1099 case PSA_ALG_MD2:
1100 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1101 break;
1102#endif
1103#if defined(MBEDTLS_MD4_C)
1104 case PSA_ALG_MD4:
1105 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1106 break;
1107#endif
1108#if defined(MBEDTLS_MD5_C)
1109 case PSA_ALG_MD5:
1110 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1111 break;
1112#endif
1113#if defined(MBEDTLS_RIPEMD160_C)
1114 case PSA_ALG_RIPEMD160:
1115 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1116 break;
1117#endif
1118#if defined(MBEDTLS_SHA1_C)
1119 case PSA_ALG_SHA_1:
1120 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1121 break;
1122#endif
1123#if defined(MBEDTLS_SHA256_C)
1124 case PSA_ALG_SHA_224:
1125 case PSA_ALG_SHA_256:
1126 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1127 break;
1128#endif
1129#if defined(MBEDTLS_SHA512_C)
1130 case PSA_ALG_SHA_384:
1131 case PSA_ALG_SHA_512:
1132 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1133 break;
1134#endif
1135 default:
1136 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1137 break;
1138 }
1139
1140 if( ret == 0 )
1141 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001142 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001143 return( psa_hash_abort( operation ) );
1144 }
1145 else
1146 {
1147 psa_hash_abort( operation );
1148 return( mbedtls_to_psa_error( ret ) );
1149 }
1150}
1151
Gilles Peskine2d277862018-06-18 15:41:12 +02001152psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1153 const uint8_t *hash,
1154 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001155{
1156 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1157 size_t actual_hash_length;
1158 psa_status_t status = psa_hash_finish( operation,
1159 actual_hash, sizeof( actual_hash ),
1160 &actual_hash_length );
1161 if( status != PSA_SUCCESS )
1162 return( status );
1163 if( actual_hash_length != hash_length )
1164 return( PSA_ERROR_INVALID_SIGNATURE );
1165 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1166 return( PSA_ERROR_INVALID_SIGNATURE );
1167 return( PSA_SUCCESS );
1168}
1169
1170
1171
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001172/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001173/* MAC */
1174/****************************************************************/
1175
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001176static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001177 psa_algorithm_t alg,
1178 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001179 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001180 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001181{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001182 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001183 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001184
1185 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1186 {
1187 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001188 {
1189 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1190 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001191
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001192 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001193 {
1194 case PSA_ALG_STREAM_CIPHER:
1195 mode = MBEDTLS_MODE_STREAM;
1196 break;
1197 case PSA_ALG_CBC_BASE:
1198 mode = MBEDTLS_MODE_CBC;
1199 break;
1200 case PSA_ALG_CFB_BASE:
1201 mode = MBEDTLS_MODE_CFB;
1202 break;
1203 case PSA_ALG_OFB_BASE:
1204 mode = MBEDTLS_MODE_OFB;
1205 break;
1206 case PSA_ALG_CTR:
1207 mode = MBEDTLS_MODE_CTR;
1208 break;
1209 case PSA_ALG_CCM:
1210 mode = MBEDTLS_MODE_CCM;
1211 break;
1212 case PSA_ALG_GCM:
1213 mode = MBEDTLS_MODE_GCM;
1214 break;
1215 default:
1216 return( NULL );
1217 }
1218 }
1219 else if( alg == PSA_ALG_CMAC )
1220 mode = MBEDTLS_MODE_ECB;
1221 else if( alg == PSA_ALG_GMAC )
1222 mode = MBEDTLS_MODE_GCM;
1223 else
1224 return( NULL );
1225
1226 switch( key_type )
1227 {
1228 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001229 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001230 break;
1231 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001232 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1233 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001234 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001235 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001236 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001237 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001238 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1239 * but two-key Triple-DES is functionally three-key Triple-DES
1240 * with K1=K3, so that's how we present it to mbedtls. */
1241 if( key_bits == 128 )
1242 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001243 break;
1244 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001245 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001246 break;
1247 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001248 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001249 break;
1250 default:
1251 return( NULL );
1252 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001253 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001254 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001255
Jaeden Amero23bbb752018-06-26 14:16:54 +01001256 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1257 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001258}
1259
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001260static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001261{
Gilles Peskine2d277862018-06-18 15:41:12 +02001262 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001263 {
1264 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001265 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001266 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001267 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001268 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001269 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001270 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001271 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001272 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001273 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001274 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001275 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001276 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001277 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001278 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001279 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001280 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001281 return( 128 );
1282 default:
1283 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001284 }
1285}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001286
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001287/* Initialize the MAC operation structure. Once this function has been
1288 * called, psa_mac_abort can run and will do the right thing. */
1289static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1290 psa_algorithm_t alg )
1291{
1292 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1293
1294 operation->alg = alg;
1295 operation->key_set = 0;
1296 operation->iv_set = 0;
1297 operation->iv_required = 0;
1298 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001299 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001300
1301#if defined(MBEDTLS_CMAC_C)
1302 if( alg == PSA_ALG_CMAC )
1303 {
1304 operation->iv_required = 0;
1305 mbedtls_cipher_init( &operation->ctx.cmac );
1306 status = PSA_SUCCESS;
1307 }
1308 else
1309#endif /* MBEDTLS_CMAC_C */
1310#if defined(MBEDTLS_MD_C)
1311 if( PSA_ALG_IS_HMAC( operation->alg ) )
1312 {
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001313 status = psa_hash_setup( &operation->ctx.hmac.hash_ctx,
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001314 PSA_ALG_HMAC_HASH( alg ) );
1315 }
1316 else
1317#endif /* MBEDTLS_MD_C */
1318 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001319 if( ! PSA_ALG_IS_MAC( alg ) )
1320 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001321 }
1322
1323 if( status != PSA_SUCCESS )
1324 memset( operation, 0, sizeof( *operation ) );
1325 return( status );
1326}
1327
Gilles Peskine8c9def32018-02-08 10:02:12 +01001328psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1329{
1330 switch( operation->alg )
1331 {
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001332 case 0:
Gilles Peskine81736312018-06-26 15:04:31 +02001333 /* The object has (apparently) been initialized but it is not
1334 * in use. It's ok to call abort on such an object, and there's
1335 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001336 return( PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001337#if defined(MBEDTLS_CMAC_C)
1338 case PSA_ALG_CMAC:
1339 mbedtls_cipher_free( &operation->ctx.cmac );
1340 break;
1341#endif /* MBEDTLS_CMAC_C */
1342 default:
1343#if defined(MBEDTLS_MD_C)
1344 if( PSA_ALG_IS_HMAC( operation->alg ) )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001345 {
Jaeden Amero5390f692018-06-26 14:18:50 +01001346 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001347 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001348
Gilles Peskine99bc6492018-06-11 17:13:00 +02001349 if( block_size == 0 )
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001350 return( PSA_ERROR_NOT_SUPPORTED );
1351
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001352 psa_hash_abort( &operation->ctx.hmac.hash_ctx );
Gilles Peskine2d277862018-06-18 15:41:12 +02001353 mbedtls_zeroize( operation->ctx.hmac.opad, block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001354 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001355 else
1356#endif /* MBEDTLS_MD_C */
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001357 {
1358 /* Sanity check (shouldn't happen: operation->alg should
1359 * always have been initialized to a valid value). */
1360 return( PSA_ERROR_BAD_STATE );
1361 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001362 }
Moran Peker41deec42018-04-04 15:43:05 +03001363
Gilles Peskine8c9def32018-02-08 10:02:12 +01001364 operation->alg = 0;
1365 operation->key_set = 0;
1366 operation->iv_set = 0;
1367 operation->iv_required = 0;
1368 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001369 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001370
Gilles Peskine8c9def32018-02-08 10:02:12 +01001371 return( PSA_SUCCESS );
1372}
1373
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001374#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001375static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001376 size_t key_bits,
1377 key_slot_t *slot,
1378 const mbedtls_cipher_info_t *cipher_info )
1379{
1380 int ret;
1381
1382 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001383
1384 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1385 if( ret != 0 )
1386 return( ret );
1387
1388 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1389 slot->data.raw.data,
1390 key_bits );
1391 return( ret );
1392}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001393#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001394
Gilles Peskine248051a2018-06-20 16:09:38 +02001395#if defined(MBEDTLS_MD_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001396static int psa_hmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001397 psa_key_type_t key_type,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001398 key_slot_t *slot,
1399 psa_algorithm_t alg )
1400{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001401 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001402 unsigned char *opad = operation->ctx.hmac.opad;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001403 size_t i;
Gilles Peskinee1bc6802018-06-11 17:36:05 +02001404 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001405 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001406 unsigned int digest_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001407 PSA_HASH_SIZE( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001408 size_t key_length = slot->data.raw.bytes;
1409 psa_status_t status;
1410
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001411 if( block_size == 0 || digest_size == 0 )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001412 return( PSA_ERROR_NOT_SUPPORTED );
1413
1414 if( key_type != PSA_KEY_TYPE_HMAC )
1415 return( PSA_ERROR_INVALID_ARGUMENT );
1416
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001417 operation->mac_size = digest_size;
1418
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001419 /* The hash was started earlier in psa_mac_init. */
Gilles Peskined223b522018-06-11 18:12:58 +02001420 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001421 {
1422 status = psa_hash_update( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001423 slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001424 if( status != PSA_SUCCESS )
1425 return( status );
1426 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001427 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001428 if( status != PSA_SUCCESS )
1429 return( status );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001430 }
Gilles Peskined223b522018-06-11 18:12:58 +02001431 else
1432 memcpy( ipad, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001433
Gilles Peskined223b522018-06-11 18:12:58 +02001434 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1435 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001436 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001437 ipad[i] ^= 0x36;
1438 memset( ipad + key_length, 0x36, block_size - key_length );
1439
1440 /* Copy the key material from ipad to opad, flipping the requisite bits,
1441 * and filling the rest of opad with the requisite constant. */
1442 for( i = 0; i < key_length; i++ )
1443 opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1444 memset( opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001445
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001446 status = psa_hash_setup( &operation->ctx.hmac.hash_ctx,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001447 PSA_ALG_HMAC_HASH( alg ) );
1448 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001449 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001450
1451 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, ipad,
1452 block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001453
1454cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001455 mbedtls_zeroize( ipad, key_length );
1456 /* opad is in the context. It needs to stay in memory if this function
1457 * succeeds, and it will be wiped by psa_mac_abort() called from
Gilles Peskine89167cb2018-07-08 20:12:23 +02001458 * psa_mac_setup in the error case. */
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001459
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001460 return( status );
1461}
Gilles Peskine248051a2018-06-20 16:09:38 +02001462#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001463
Gilles Peskine89167cb2018-07-08 20:12:23 +02001464static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1465 psa_key_slot_t key,
1466 psa_algorithm_t alg,
1467 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001468{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001469 psa_status_t status;
1470 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001471 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001472 psa_key_usage_t usage =
1473 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskine828ed142018-06-18 23:25:51 +02001474 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001475
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001476 status = psa_mac_init( operation, alg );
1477 if( status != PSA_SUCCESS )
1478 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001479 if( is_sign )
1480 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001481
Gilles Peskine89167cb2018-07-08 20:12:23 +02001482 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001483 if( status != PSA_SUCCESS )
1484 return( status );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001485
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001486 key_bits = psa_get_key_bits( slot );
1487
Gilles Peskine8c9def32018-02-08 10:02:12 +01001488 if( ! PSA_ALG_IS_HMAC( alg ) )
1489 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001490 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits,
1491 NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001492 if( cipher_info == NULL )
1493 return( PSA_ERROR_NOT_SUPPORTED );
1494 operation->mac_size = cipher_info->block_size;
1495 }
1496 switch( alg )
1497 {
1498#if defined(MBEDTLS_CMAC_C)
1499 case PSA_ALG_CMAC:
Gilles Peskine89167cb2018-07-08 20:12:23 +02001500 status = mbedtls_to_psa_error( psa_cmac_setup( operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001501 key_bits,
1502 slot,
1503 cipher_info ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001504 break;
1505#endif /* MBEDTLS_CMAC_C */
1506 default:
1507#if defined(MBEDTLS_MD_C)
1508 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskine89167cb2018-07-08 20:12:23 +02001509 status = psa_hmac_setup( operation, slot->type, slot, alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001510 else
1511#endif /* MBEDTLS_MD_C */
1512 return( PSA_ERROR_NOT_SUPPORTED );
1513 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001514
Gilles Peskine8c9def32018-02-08 10:02:12 +01001515 /* If we reach this point, then the algorithm-specific part of the
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001516 * context may contain data that needs to be wiped on error. */
1517 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001518 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001519 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001520 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001521 else
1522 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001523 operation->key_set = 1;
1524 }
1525 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001526}
1527
Gilles Peskine89167cb2018-07-08 20:12:23 +02001528psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1529 psa_key_slot_t key,
1530 psa_algorithm_t alg )
1531{
1532 return( psa_mac_setup( operation, key, alg, 1 ) );
1533}
1534
1535psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1536 psa_key_slot_t key,
1537 psa_algorithm_t alg )
1538{
1539 return( psa_mac_setup( operation, key, alg, 0 ) );
1540}
1541
Gilles Peskine8c9def32018-02-08 10:02:12 +01001542psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1543 const uint8_t *input,
1544 size_t input_length )
1545{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001546 int ret = 0 ;
1547 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001548 if( ! operation->key_set )
1549 return( PSA_ERROR_BAD_STATE );
1550 if( operation->iv_required && ! operation->iv_set )
1551 return( PSA_ERROR_BAD_STATE );
1552 operation->has_input = 1;
1553
1554 switch( operation->alg )
1555 {
1556#if defined(MBEDTLS_CMAC_C)
1557 case PSA_ALG_CMAC:
1558 ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1559 input, input_length );
1560 break;
1561#endif /* MBEDTLS_CMAC_C */
1562 default:
1563#if defined(MBEDTLS_MD_C)
1564 if( PSA_ALG_IS_HMAC( operation->alg ) )
1565 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001566 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
Gilles Peskine2d277862018-06-18 15:41:12 +02001567 input_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001568 }
1569 else
1570#endif /* MBEDTLS_MD_C */
1571 {
1572 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1573 }
1574 break;
1575 }
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001576 if( ret != 0 || status != PSA_SUCCESS )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001577 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001578 psa_mac_abort( operation );
Gilles Peskine2d277862018-06-18 15:41:12 +02001579 if( ret != 0 )
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001580 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001581 }
1582
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001583 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001584}
1585
mohammad16036df908f2018-04-02 08:34:15 -07001586static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001587 uint8_t *mac,
1588 size_t mac_size,
1589 size_t *mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001590{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001591 int ret = 0;
1592 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001593
1594 /* Fill the output buffer with something that isn't a valid mac
1595 * (barring an attack on the mac and deliberately-crafted input),
1596 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001597 *mac_length = mac_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001598 /* If mac_size is 0 then mac may be NULL and then the
1599 * call to memset would have undefined behavior. */
1600 if( mac_size != 0 )
1601 memset( mac, '!', mac_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001602
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001603 if( ! operation->key_set )
1604 return( PSA_ERROR_BAD_STATE );
1605 if( operation->iv_required && ! operation->iv_set )
1606 return( PSA_ERROR_BAD_STATE );
1607
Gilles Peskine8c9def32018-02-08 10:02:12 +01001608 if( mac_size < operation->mac_size )
1609 return( PSA_ERROR_BUFFER_TOO_SMALL );
1610
1611 switch( operation->alg )
1612 {
1613#if defined(MBEDTLS_CMAC_C)
1614 case PSA_ALG_CMAC:
1615 ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1616 break;
1617#endif /* MBEDTLS_CMAC_C */
1618 default:
1619#if defined(MBEDTLS_MD_C)
1620 if( PSA_ALG_IS_HMAC( operation->alg ) )
1621 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001622 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001623 unsigned char *opad = operation->ctx.hmac.opad;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001624 size_t hash_size = 0;
Nir Sonnenschein96272412018-06-17 14:41:10 +03001625 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001626 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001627
Gilles Peskine99bc6492018-06-11 17:13:00 +02001628 if( block_size == 0 )
1629 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001630
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001631 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine99bc6492018-06-11 17:13:00 +02001632 sizeof( tmp ), &hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001633 if( status != PSA_SUCCESS )
1634 goto cleanup;
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001635 /* From here on, tmp needs to be wiped. */
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001636
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001637 status = psa_hash_setup( &operation->ctx.hmac.hash_ctx,
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001638 PSA_ALG_HMAC_HASH( operation->alg ) );
1639 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001640 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001641
1642 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, opad,
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001643 block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001644 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001645 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001646
1647 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine2d277862018-06-18 15:41:12 +02001648 hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001649 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001650 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001651
1652 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, mac,
1653 mac_size, mac_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001654 hmac_cleanup:
1655 mbedtls_zeroize( tmp, hash_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001656 }
1657 else
1658#endif /* MBEDTLS_MD_C */
1659 {
1660 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1661 }
1662 break;
1663 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001664cleanup:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001665
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001666 if( ret == 0 && status == PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001667 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001668 *mac_length = operation->mac_size;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001669 return( psa_mac_abort( operation ) );
1670 }
1671 else
1672 {
1673 psa_mac_abort( operation );
Gilles Peskine99bc6492018-06-11 17:13:00 +02001674 if( ret != 0 )
Gilles Peskine2d277862018-06-18 15:41:12 +02001675 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001676
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001677 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001678 }
1679}
1680
Gilles Peskineacd4be32018-07-08 19:56:25 +02001681psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1682 uint8_t *mac,
1683 size_t mac_size,
1684 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001685{
Gilles Peskine89167cb2018-07-08 20:12:23 +02001686 if( ! operation->is_sign )
1687 {
1688 psa_mac_abort( operation );
1689 return( PSA_ERROR_BAD_STATE );
1690 }
mohammad16036df908f2018-04-02 08:34:15 -07001691
Gilles Peskine99bc6492018-06-11 17:13:00 +02001692 return( psa_mac_finish_internal( operation, mac,
1693 mac_size, mac_length ) );
mohammad16036df908f2018-04-02 08:34:15 -07001694}
1695
Gilles Peskineacd4be32018-07-08 19:56:25 +02001696psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1697 const uint8_t *mac,
1698 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001699{
Gilles Peskine828ed142018-06-18 23:25:51 +02001700 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Gilles Peskine8c9def32018-02-08 10:02:12 +01001701 size_t actual_mac_length;
mohammad16036df908f2018-04-02 08:34:15 -07001702 psa_status_t status;
1703
Gilles Peskine89167cb2018-07-08 20:12:23 +02001704 if( operation->is_sign )
1705 {
1706 psa_mac_abort( operation );
1707 return( PSA_ERROR_BAD_STATE );
1708 }
mohammad16036df908f2018-04-02 08:34:15 -07001709
1710 status = psa_mac_finish_internal( operation,
1711 actual_mac, sizeof( actual_mac ),
1712 &actual_mac_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001713 if( status != PSA_SUCCESS )
1714 return( status );
1715 if( actual_mac_length != mac_length )
1716 return( PSA_ERROR_INVALID_SIGNATURE );
1717 if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
1718 return( PSA_ERROR_INVALID_SIGNATURE );
1719 return( PSA_SUCCESS );
1720}
1721
1722
Gilles Peskine20035e32018-02-03 22:44:14 +01001723
Gilles Peskine20035e32018-02-03 22:44:14 +01001724/****************************************************************/
1725/* Asymmetric cryptography */
1726/****************************************************************/
1727
Gilles Peskine2b450e32018-06-27 15:42:46 +02001728#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001729/* Decode the hash algorithm from alg and store the mbedtls encoding in
1730 * md_alg. Verify that the hash length is consistent. */
1731static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1732 size_t hash_length,
1733 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001734{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001735 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001736 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1737 *md_alg = hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1738 if( *md_alg == MBEDTLS_MD_NONE )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001739 {
1740#if SIZE_MAX > UINT_MAX
Gilles Peskine61b91d42018-06-08 16:09:36 +02001741 if( hash_length > UINT_MAX )
1742 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001743#endif
1744 }
1745 else
1746 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001747 if( mbedtls_md_get_size( md_info ) != hash_length )
1748 return( PSA_ERROR_INVALID_ARGUMENT );
1749 if( md_info == NULL )
1750 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001751 }
Gilles Peskine61b91d42018-06-08 16:09:36 +02001752 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001753}
1754
Gilles Peskine2b450e32018-06-27 15:42:46 +02001755static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1756 psa_algorithm_t alg,
1757 const uint8_t *hash,
1758 size_t hash_length,
1759 uint8_t *signature,
1760 size_t signature_size,
1761 size_t *signature_length )
1762{
1763 psa_status_t status;
1764 int ret;
1765 mbedtls_md_type_t md_alg;
1766
1767 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1768 if( status != PSA_SUCCESS )
1769 return( status );
1770
1771 if( signature_size < rsa->len )
1772 return( PSA_ERROR_BUFFER_TOO_SMALL );
1773
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001774 /* The Mbed TLS RSA module uses an unsigned int for hash_length. See if
1775 * hash_length will fit and return an error if it doesn't. */
1776#if defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21)
1777#if SIZE_MAX > UINT_MAX
1778 if( hash_length > UINT_MAX )
1779 return( PSA_ERROR_NOT_SUPPORTED );
1780#endif
1781#endif
1782
Gilles Peskine2b450e32018-06-27 15:42:46 +02001783#if defined(MBEDTLS_PKCS1_V15)
1784 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1785 {
1786 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1787 MBEDTLS_MD_NONE );
1788 ret = mbedtls_rsa_pkcs1_sign( rsa,
1789 mbedtls_ctr_drbg_random,
1790 &global_data.ctr_drbg,
1791 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001792 md_alg,
1793 (unsigned int) hash_length,
1794 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001795 signature );
1796 }
1797 else
1798#endif /* MBEDTLS_PKCS1_V15 */
1799#if defined(MBEDTLS_PKCS1_V21)
1800 if( PSA_ALG_IS_RSA_PSS( alg ) )
1801 {
1802 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1803 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1804 mbedtls_ctr_drbg_random,
1805 &global_data.ctr_drbg,
1806 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001807 md_alg,
1808 (unsigned int) hash_length,
1809 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001810 signature );
1811 }
1812 else
1813#endif /* MBEDTLS_PKCS1_V21 */
1814 {
1815 return( PSA_ERROR_INVALID_ARGUMENT );
1816 }
1817
1818 if( ret == 0 )
1819 *signature_length = rsa->len;
1820 return( mbedtls_to_psa_error( ret ) );
1821}
1822
1823static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1824 psa_algorithm_t alg,
1825 const uint8_t *hash,
1826 size_t hash_length,
1827 const uint8_t *signature,
1828 size_t signature_length )
1829{
1830 psa_status_t status;
1831 int ret;
1832 mbedtls_md_type_t md_alg;
1833
1834 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1835 if( status != PSA_SUCCESS )
1836 return( status );
1837
1838 if( signature_length < rsa->len )
1839 return( PSA_ERROR_BUFFER_TOO_SMALL );
1840
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001841#if defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21)
1842#if SIZE_MAX > UINT_MAX
1843 /* The Mbed TLS RSA module uses an unsigned int for hash_length. See if
1844 * hash_length will fit and return an error if it doesn't. */
1845 if( hash_length > UINT_MAX )
1846 return( PSA_ERROR_NOT_SUPPORTED );
1847#endif
1848#endif
1849
Gilles Peskine2b450e32018-06-27 15:42:46 +02001850#if defined(MBEDTLS_PKCS1_V15)
1851 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1852 {
1853 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1854 MBEDTLS_MD_NONE );
1855 ret = mbedtls_rsa_pkcs1_verify( rsa,
1856 mbedtls_ctr_drbg_random,
1857 &global_data.ctr_drbg,
1858 MBEDTLS_RSA_PUBLIC,
1859 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001860 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001861 hash,
1862 signature );
1863 }
1864 else
1865#endif /* MBEDTLS_PKCS1_V15 */
1866#if defined(MBEDTLS_PKCS1_V21)
1867 if( PSA_ALG_IS_RSA_PSS( alg ) )
1868 {
1869 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1870 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1871 mbedtls_ctr_drbg_random,
1872 &global_data.ctr_drbg,
1873 MBEDTLS_RSA_PUBLIC,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001874 md_alg,
1875 (unsigned int) hash_length,
1876 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001877 signature );
1878 }
1879 else
1880#endif /* MBEDTLS_PKCS1_V21 */
1881 {
1882 return( PSA_ERROR_INVALID_ARGUMENT );
1883 }
1884 return( mbedtls_to_psa_error( ret ) );
1885}
1886#endif /* MBEDTLS_RSA_C */
1887
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001888#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001889/* `ecp` cannot be const because `ecp->grp` needs to be non-const
1890 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
1891 * (even though these functions don't modify it). */
1892static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
1893 psa_algorithm_t alg,
1894 const uint8_t *hash,
1895 size_t hash_length,
1896 uint8_t *signature,
1897 size_t signature_size,
1898 size_t *signature_length )
1899{
1900 int ret;
1901 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001902 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001903 mbedtls_mpi_init( &r );
1904 mbedtls_mpi_init( &s );
1905
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001906 if( signature_size < 2 * curve_bytes )
1907 {
1908 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
1909 goto cleanup;
1910 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001911
1912 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
1913 {
1914 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
1915 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1916 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
1917 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
1918 hash, hash_length,
1919 md_alg ) );
1920 }
1921 else
1922 {
1923 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
1924 hash, hash_length,
1925 mbedtls_ctr_drbg_random,
1926 &global_data.ctr_drbg ) );
1927 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001928
1929 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
1930 signature,
1931 curve_bytes ) );
1932 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
1933 signature + curve_bytes,
1934 curve_bytes ) );
1935
1936cleanup:
1937 mbedtls_mpi_free( &r );
1938 mbedtls_mpi_free( &s );
1939 if( ret == 0 )
1940 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001941 return( mbedtls_to_psa_error( ret ) );
1942}
1943
1944static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
1945 const uint8_t *hash,
1946 size_t hash_length,
1947 const uint8_t *signature,
1948 size_t signature_length )
1949{
1950 int ret;
1951 mbedtls_mpi r, s;
1952 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
1953 mbedtls_mpi_init( &r );
1954 mbedtls_mpi_init( &s );
1955
1956 if( signature_length != 2 * curve_bytes )
1957 return( PSA_ERROR_INVALID_SIGNATURE );
1958
1959 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
1960 signature,
1961 curve_bytes ) );
1962 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
1963 signature + curve_bytes,
1964 curve_bytes ) );
1965
1966 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
1967 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001968
1969cleanup:
1970 mbedtls_mpi_free( &r );
1971 mbedtls_mpi_free( &s );
1972 return( mbedtls_to_psa_error( ret ) );
1973}
1974#endif /* MBEDTLS_ECDSA_C */
1975
Gilles Peskine61b91d42018-06-08 16:09:36 +02001976psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
1977 psa_algorithm_t alg,
1978 const uint8_t *hash,
1979 size_t hash_length,
1980 const uint8_t *salt,
1981 size_t salt_length,
1982 uint8_t *signature,
1983 size_t signature_size,
1984 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01001985{
1986 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001987 psa_status_t status;
1988
1989 *signature_length = signature_size;
1990
Gilles Peskine93aa0332018-02-03 23:58:03 +01001991 (void) salt;
1992 (void) salt_length;
1993
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001994 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
1995 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001996 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01001997 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001998 {
1999 status = PSA_ERROR_INVALID_ARGUMENT;
2000 goto exit;
2001 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002002
Gilles Peskine20035e32018-02-03 22:44:14 +01002003#if defined(MBEDTLS_RSA_C)
2004 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2005 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002006 status = psa_rsa_sign( slot->data.rsa,
2007 alg,
2008 hash, hash_length,
2009 signature, signature_size,
2010 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002011 }
2012 else
2013#endif /* defined(MBEDTLS_RSA_C) */
2014#if defined(MBEDTLS_ECP_C)
2015 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2016 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002017#if defined(MBEDTLS_ECDSA_C)
2018 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002019 status = psa_ecdsa_sign( slot->data.ecp,
2020 alg,
2021 hash, hash_length,
2022 signature, signature_size,
2023 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002024 else
2025#endif /* defined(MBEDTLS_ECDSA_C) */
2026 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002027 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002028 }
itayzafrir5c753392018-05-08 11:18:38 +03002029 }
2030 else
2031#endif /* defined(MBEDTLS_ECP_C) */
2032 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002033 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002034 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002035
2036exit:
2037 /* Fill the unused part of the output buffer (the whole buffer on error,
2038 * the trailing part on success) with something that isn't a valid mac
2039 * (barring an attack on the mac and deliberately-crafted input),
2040 * in case the caller doesn't check the return status properly. */
2041 if( status == PSA_SUCCESS )
2042 memset( signature + *signature_length, '!',
2043 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002044 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002045 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002046 /* If signature_size is 0 then we have nothing to do. We must not call
2047 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002048 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002049}
2050
2051psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2052 psa_algorithm_t alg,
2053 const uint8_t *hash,
2054 size_t hash_length,
2055 const uint8_t *salt,
2056 size_t salt_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002057 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002058 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002059{
2060 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002061 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002062
itayzafrir5c753392018-05-08 11:18:38 +03002063 (void) salt;
2064 (void) salt_length;
2065
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002066 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2067 if( status != PSA_SUCCESS )
2068 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002069
Gilles Peskine61b91d42018-06-08 16:09:36 +02002070#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002071 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
2072 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002073 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002074 return( psa_rsa_verify( slot->data.rsa,
2075 alg,
2076 hash, hash_length,
2077 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002078 }
2079 else
2080#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002081#if defined(MBEDTLS_ECP_C)
2082 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2083 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002084#if defined(MBEDTLS_ECDSA_C)
2085 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002086 return( psa_ecdsa_verify( slot->data.ecp,
2087 hash, hash_length,
2088 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002089 else
2090#endif /* defined(MBEDTLS_ECDSA_C) */
2091 {
2092 return( PSA_ERROR_INVALID_ARGUMENT );
2093 }
itayzafrir5c753392018-05-08 11:18:38 +03002094 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002095 else
2096#endif /* defined(MBEDTLS_ECP_C) */
2097 {
2098 return( PSA_ERROR_NOT_SUPPORTED );
2099 }
2100}
2101
Gilles Peskine61b91d42018-06-08 16:09:36 +02002102psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2103 psa_algorithm_t alg,
2104 const uint8_t *input,
2105 size_t input_length,
2106 const uint8_t *salt,
2107 size_t salt_length,
2108 uint8_t *output,
2109 size_t output_size,
2110 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002111{
2112 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002113 psa_status_t status;
2114
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002115 (void) salt;
2116 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002117 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002118
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002119 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2120 if( status != PSA_SUCCESS )
2121 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002122 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2123 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002124
2125#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002126 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
2127 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002128 {
2129 mbedtls_rsa_context *rsa = slot->data.rsa;
2130 int ret;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002131 if( output_size < rsa->len )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002132 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002133#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002134 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002135 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002136 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2137 mbedtls_ctr_drbg_random,
2138 &global_data.ctr_drbg,
2139 MBEDTLS_RSA_PUBLIC,
2140 input_length,
2141 input,
2142 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002143 }
2144 else
2145#endif /* MBEDTLS_PKCS1_V15 */
2146#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002147 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002148 {
2149 return( PSA_ERROR_NOT_SUPPORTED );
2150 }
2151 else
2152#endif /* MBEDTLS_PKCS1_V21 */
2153 {
2154 return( PSA_ERROR_INVALID_ARGUMENT );
2155 }
2156 if( ret == 0 )
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002157 *output_length = rsa->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002158 return( mbedtls_to_psa_error( ret ) );
2159 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002160 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002161#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002162 {
2163 return( PSA_ERROR_NOT_SUPPORTED );
2164 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002165}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002166
Gilles Peskine61b91d42018-06-08 16:09:36 +02002167psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2168 psa_algorithm_t alg,
2169 const uint8_t *input,
2170 size_t input_length,
2171 const uint8_t *salt,
2172 size_t salt_length,
2173 uint8_t *output,
2174 size_t output_size,
2175 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002176{
2177 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002178 psa_status_t status;
2179
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002180 (void) salt;
2181 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002182 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002183
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002184 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2185 if( status != PSA_SUCCESS )
2186 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002187 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2188 return( PSA_ERROR_INVALID_ARGUMENT );
2189
2190#if defined(MBEDTLS_RSA_C)
2191 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2192 {
2193 mbedtls_rsa_context *rsa = slot->data.rsa;
2194 int ret;
2195
Gilles Peskinec4def2f2018-06-08 17:53:48 +02002196 if( input_length != rsa->len )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002197 return( PSA_ERROR_INVALID_ARGUMENT );
2198
2199#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002200 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002201 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002202 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2203 mbedtls_ctr_drbg_random,
2204 &global_data.ctr_drbg,
2205 MBEDTLS_RSA_PRIVATE,
2206 output_length,
2207 input,
2208 output,
2209 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002210 }
2211 else
2212#endif /* MBEDTLS_PKCS1_V15 */
2213#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002214 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002215 {
2216 return( PSA_ERROR_NOT_SUPPORTED );
2217 }
2218 else
2219#endif /* MBEDTLS_PKCS1_V21 */
2220 {
2221 return( PSA_ERROR_INVALID_ARGUMENT );
2222 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002223
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002224 return( mbedtls_to_psa_error( ret ) );
2225 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002226 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002227#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002228 {
2229 return( PSA_ERROR_NOT_SUPPORTED );
2230 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002231}
Gilles Peskine20035e32018-02-03 22:44:14 +01002232
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002233
2234
mohammad1603503973b2018-03-12 15:59:30 +02002235/****************************************************************/
2236/* Symmetric cryptography */
2237/****************************************************************/
2238
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002239/* Initialize the cipher operation structure. Once this function has been
2240 * called, psa_cipher_abort can run and will do the right thing. */
2241static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2242 psa_algorithm_t alg )
2243{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002244 if( ! PSA_ALG_IS_CIPHER( alg ) )
2245 {
2246 memset( operation, 0, sizeof( *operation ) );
2247 return( PSA_ERROR_INVALID_ARGUMENT );
2248 }
2249
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002250 operation->alg = alg;
2251 operation->key_set = 0;
2252 operation->iv_set = 0;
2253 operation->iv_required = 1;
2254 operation->iv_size = 0;
2255 operation->block_size = 0;
2256 mbedtls_cipher_init( &operation->ctx.cipher );
2257 return( PSA_SUCCESS );
2258}
2259
Gilles Peskinee553c652018-06-04 16:22:46 +02002260static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2261 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002262 psa_algorithm_t alg,
2263 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002264{
2265 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2266 psa_status_t status;
2267 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002268 size_t key_bits;
2269 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002270 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2271 PSA_KEY_USAGE_ENCRYPT :
2272 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002273
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002274 status = psa_cipher_init( operation, alg );
2275 if( status != PSA_SUCCESS )
2276 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002277
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002278 status = psa_get_key_from_slot( key, &slot, usage, alg);
2279 if( status != PSA_SUCCESS )
2280 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002281 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002282
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002283 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002284 if( cipher_info == NULL )
2285 return( PSA_ERROR_NOT_SUPPORTED );
2286
mohammad1603503973b2018-03-12 15:59:30 +02002287 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002288 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002289 {
2290 psa_cipher_abort( operation );
2291 return( mbedtls_to_psa_error( ret ) );
2292 }
2293
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002294#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002295 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002296 {
2297 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2298 unsigned char keys[24];
2299 memcpy( keys, slot->data.raw.data, 16 );
2300 memcpy( keys + 16, slot->data.raw.data, 8 );
2301 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2302 keys,
2303 192, cipher_operation );
2304 }
2305 else
2306#endif
2307 {
2308 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2309 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002310 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002311 }
Moran Peker41deec42018-04-04 15:43:05 +03002312 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002313 {
2314 psa_cipher_abort( operation );
2315 return( mbedtls_to_psa_error( ret ) );
2316 }
2317
mohammad16038481e742018-03-18 13:57:31 +02002318#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002319 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002320 {
Gilles Peskine53514202018-06-06 15:11:46 +02002321 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2322 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002323
Moran Pekera28258c2018-05-29 16:25:04 +03002324 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002325 {
2326 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2327 mode = MBEDTLS_PADDING_PKCS7;
2328 break;
2329 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2330 mode = MBEDTLS_PADDING_NONE;
2331 break;
2332 default:
Moran Pekerae382792018-05-31 14:06:17 +03002333 psa_cipher_abort( operation );
2334 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002335 }
2336 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002337 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002338 {
2339 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002340 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002341 }
mohammad16038481e742018-03-18 13:57:31 +02002342 }
2343#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2344
mohammad1603503973b2018-03-12 15:59:30 +02002345 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002346 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002347 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) :
Gilles Peskine7e928852018-06-04 16:23:10 +02002348 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002349 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002350 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002351 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002352 }
mohammad1603503973b2018-03-12 15:59:30 +02002353
Moran Peker395db872018-05-31 14:07:14 +03002354 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002355}
2356
Gilles Peskinee553c652018-06-04 16:22:46 +02002357psa_status_t psa_encrypt_setup( psa_cipher_operation_t *operation,
2358 psa_key_slot_t key,
2359 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002360{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002361 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002362}
2363
Gilles Peskinee553c652018-06-04 16:22:46 +02002364psa_status_t psa_decrypt_setup( psa_cipher_operation_t *operation,
2365 psa_key_slot_t key,
2366 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002367{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002368 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002369}
2370
Gilles Peskinee553c652018-06-04 16:22:46 +02002371psa_status_t psa_encrypt_generate_iv( psa_cipher_operation_t *operation,
2372 unsigned char *iv,
2373 size_t iv_size,
2374 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002375{
Moran Peker41deec42018-04-04 15:43:05 +03002376 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002377 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002378 return( PSA_ERROR_BAD_STATE );
2379 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002380 {
Moran Peker41deec42018-04-04 15:43:05 +03002381 ret = PSA_ERROR_BUFFER_TOO_SMALL;
2382 goto exit;
2383 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002384 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2385 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002386 if( ret != 0 )
2387 {
2388 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002389 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002390 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002391
mohammad16038481e742018-03-18 13:57:31 +02002392 *iv_length = operation->iv_size;
mohammad160389e0f462018-04-12 08:48:45 +03002393 ret = psa_encrypt_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002394
Moran Peker395db872018-05-31 14:07:14 +03002395exit:
2396 if( ret != PSA_SUCCESS )
2397 psa_cipher_abort( operation );
2398 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02002399}
2400
Gilles Peskinee553c652018-06-04 16:22:46 +02002401psa_status_t psa_encrypt_set_iv( psa_cipher_operation_t *operation,
2402 const unsigned char *iv,
2403 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002404{
Moran Peker41deec42018-04-04 15:43:05 +03002405 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002406 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002407 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03002408 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002409 {
Moran Pekerae382792018-05-31 14:06:17 +03002410 psa_cipher_abort( operation );
2411 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03002412 }
mohammad1603503973b2018-03-12 15:59:30 +02002413 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002414 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002415 {
2416 psa_cipher_abort( operation );
2417 return( mbedtls_to_psa_error( ret ) );
2418 }
2419
2420 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02002421
Moran Peker395db872018-05-31 14:07:14 +03002422 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002423}
2424
Gilles Peskinee553c652018-06-04 16:22:46 +02002425psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2426 const uint8_t *input,
2427 size_t input_length,
2428 unsigned char *output,
2429 size_t output_size,
2430 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002431{
2432 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002433 size_t expected_output_size;
2434 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2435 {
2436 /* Take the unprocessed partial block left over from previous
2437 * update calls, if any, plus the input to this call. Remove
2438 * the last partial block, if any. You get the data that will be
2439 * output in this call. */
2440 expected_output_size =
2441 ( operation->ctx.cipher.unprocessed_len + input_length )
2442 / operation->block_size * operation->block_size;
2443 }
2444 else
2445 {
2446 expected_output_size = input_length;
2447 }
2448 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03002449 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02002450
mohammad1603503973b2018-03-12 15:59:30 +02002451 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002452 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002453 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002454 {
2455 psa_cipher_abort( operation );
2456 return( mbedtls_to_psa_error( ret ) );
2457 }
2458
Moran Peker395db872018-05-31 14:07:14 +03002459 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002460}
2461
Gilles Peskinee553c652018-06-04 16:22:46 +02002462psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2463 uint8_t *output,
2464 size_t output_size,
2465 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002466{
Janos Follath315b51c2018-07-09 16:04:51 +01002467 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2468 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002469 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002470
mohammad1603503973b2018-03-12 15:59:30 +02002471 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002472 {
Janos Follath315b51c2018-07-09 16:04:51 +01002473 status = PSA_ERROR_BAD_STATE;
2474 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002475 }
2476 if( operation->iv_required && ! operation->iv_set )
2477 {
Janos Follath315b51c2018-07-09 16:04:51 +01002478 status = PSA_ERROR_BAD_STATE;
2479 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002480 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002481 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2482 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002483 {
Gilles Peskine53514202018-06-06 15:11:46 +02002484 psa_algorithm_t padding_mode =
2485 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002486 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002487 {
Janos Follath315b51c2018-07-09 16:04:51 +01002488 status = PSA_ERROR_TAMPERING_DETECTED;
2489 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002490 }
Gilles Peskine53514202018-06-06 15:11:46 +02002491 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002492 {
2493 if( operation->ctx.cipher.unprocessed_len != 0 )
2494 {
Janos Follath315b51c2018-07-09 16:04:51 +01002495 status = PSA_ERROR_INVALID_ARGUMENT;
2496 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002497 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002498 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002499 }
2500
Janos Follath315b51c2018-07-09 16:04:51 +01002501 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2502 temp_output_buffer,
2503 output_length );
2504 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002505 {
Janos Follath315b51c2018-07-09 16:04:51 +01002506 status = mbedtls_to_psa_error( cipher_ret );
2507 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002508 }
Janos Follath315b51c2018-07-09 16:04:51 +01002509
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002510 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002511 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002512 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002513 memcpy( output, temp_output_buffer, *output_length );
2514 else
2515 {
Janos Follath315b51c2018-07-09 16:04:51 +01002516 status = PSA_ERROR_BUFFER_TOO_SMALL;
2517 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002518 }
mohammad1603503973b2018-03-12 15:59:30 +02002519
Janos Follath279ab8e2018-07-09 16:13:21 +01002520 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002521 status = psa_cipher_abort( operation );
2522
2523 return( status );
2524
2525error:
2526
2527 *output_length = 0;
2528
Janos Follath279ab8e2018-07-09 16:13:21 +01002529 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002530 (void) psa_cipher_abort( operation );
2531
2532 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002533}
2534
Gilles Peskinee553c652018-06-04 16:22:46 +02002535psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2536{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002537 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002538 {
2539 /* The object has (apparently) been initialized but it is not
2540 * in use. It's ok to call abort on such an object, and there's
2541 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002542 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002543 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002544
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002545 /* Sanity check (shouldn't happen: operation->alg should
2546 * always have been initialized to a valid value). */
2547 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2548 return( PSA_ERROR_BAD_STATE );
2549
mohammad1603503973b2018-03-12 15:59:30 +02002550 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002551
Moran Peker41deec42018-04-04 15:43:05 +03002552 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002553 operation->key_set = 0;
2554 operation->iv_set = 0;
2555 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002556 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002557 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002558
Moran Peker395db872018-05-31 14:07:14 +03002559 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002560}
2561
Gilles Peskinea0655c32018-04-30 17:06:50 +02002562
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002563
mohammad16038cc1cee2018-03-28 01:21:33 +03002564/****************************************************************/
2565/* Key Policy */
2566/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002567
mohammad160327010052018-07-03 13:16:15 +03002568#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002569void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002570{
Gilles Peskine803ce742018-06-18 16:07:14 +02002571 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002572}
2573
Gilles Peskine2d277862018-06-18 15:41:12 +02002574void psa_key_policy_set_usage( psa_key_policy_t *policy,
2575 psa_key_usage_t usage,
2576 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002577{
mohammad16034eed7572018-03-28 05:14:59 -07002578 policy->usage = usage;
2579 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002580}
2581
Gilles Peskine2d277862018-06-18 15:41:12 +02002582psa_key_usage_t psa_key_policy_get_usage( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002583{
mohammad16036df908f2018-04-02 08:34:15 -07002584 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002585}
2586
Gilles Peskine2d277862018-06-18 15:41:12 +02002587psa_algorithm_t psa_key_policy_get_algorithm( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002588{
mohammad16036df908f2018-04-02 08:34:15 -07002589 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002590}
mohammad160327010052018-07-03 13:16:15 +03002591#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002592
Gilles Peskine2d277862018-06-18 15:41:12 +02002593psa_status_t psa_set_key_policy( psa_key_slot_t key,
2594 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002595{
2596 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002597 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002598
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002599 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002600 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002601
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002602 status = psa_get_empty_key_slot( key, &slot );
2603 if( status != PSA_SUCCESS )
2604 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002605
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002606 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2607 PSA_KEY_USAGE_ENCRYPT |
2608 PSA_KEY_USAGE_DECRYPT |
2609 PSA_KEY_USAGE_SIGN |
2610 PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002611 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002612
mohammad16036df908f2018-04-02 08:34:15 -07002613 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002614
2615 return( PSA_SUCCESS );
2616}
2617
Gilles Peskine2d277862018-06-18 15:41:12 +02002618psa_status_t psa_get_key_policy( psa_key_slot_t key,
2619 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002620{
2621 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002622 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002623
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002624 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002625 return( PSA_ERROR_INVALID_ARGUMENT );
2626
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002627 status = psa_get_key_slot( key, &slot );
2628 if( status != PSA_SUCCESS )
2629 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002630
mohammad16036df908f2018-04-02 08:34:15 -07002631 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002632
2633 return( PSA_SUCCESS );
2634}
Gilles Peskine20035e32018-02-03 22:44:14 +01002635
Gilles Peskinea0655c32018-04-30 17:06:50 +02002636
2637
mohammad1603804cd712018-03-20 22:44:08 +02002638/****************************************************************/
2639/* Key Lifetime */
2640/****************************************************************/
2641
Gilles Peskine2d277862018-06-18 15:41:12 +02002642psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2643 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002644{
2645 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002646 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002647
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002648 status = psa_get_key_slot( key, &slot );
2649 if( status != PSA_SUCCESS )
2650 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002651
mohammad1603804cd712018-03-20 22:44:08 +02002652 *lifetime = slot->lifetime;
2653
2654 return( PSA_SUCCESS );
2655}
2656
Gilles Peskine2d277862018-06-18 15:41:12 +02002657psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002658 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002659{
2660 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002661 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002662
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002663 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2664 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002665 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2666 return( PSA_ERROR_INVALID_ARGUMENT );
2667
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002668 status = psa_get_empty_key_slot( key, &slot );
2669 if( status != PSA_SUCCESS )
2670 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002671
Moran Pekerd7326592018-05-29 16:56:39 +03002672 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002673 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002674
mohammad1603060ad8a2018-03-20 14:28:38 -07002675 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002676
2677 return( PSA_SUCCESS );
2678}
2679
Gilles Peskine20035e32018-02-03 22:44:14 +01002680
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002681
mohammad16035955c982018-04-26 00:53:03 +03002682/****************************************************************/
2683/* AEAD */
2684/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002685
mohammad16035955c982018-04-26 00:53:03 +03002686psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2687 psa_algorithm_t alg,
2688 const uint8_t *nonce,
2689 size_t nonce_length,
2690 const uint8_t *additional_data,
2691 size_t additional_data_length,
2692 const uint8_t *plaintext,
2693 size_t plaintext_length,
2694 uint8_t *ciphertext,
2695 size_t ciphertext_size,
2696 size_t *ciphertext_length )
2697{
2698 int ret;
2699 psa_status_t status;
2700 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002701 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002702 uint8_t *tag;
2703 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002704 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002705 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002706
mohammad1603f08a5502018-06-03 15:05:47 +03002707 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002708
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002709 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2710 if( status != PSA_SUCCESS )
2711 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002712 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002713
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002714 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002715 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002716 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002717 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002718
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002719 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002720 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002721 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002722
mohammad16035955c982018-04-26 00:53:03 +03002723 if( alg == PSA_ALG_GCM )
2724 {
2725 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002726 tag_length = 16;
2727
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002728 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002729 return( PSA_ERROR_INVALID_ARGUMENT );
2730
mohammad160315223a82018-06-03 17:19:55 +03002731 //make sure we have place to hold the tag in the ciphertext buffer
2732 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002733 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002734
2735 //update the tag pointer to point to the end of the ciphertext_length
2736 tag = ciphertext + plaintext_length;
2737
mohammad16035955c982018-04-26 00:53:03 +03002738 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002739 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002740 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002741 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002742 if( ret != 0 )
2743 {
2744 mbedtls_gcm_free( &gcm );
2745 return( mbedtls_to_psa_error( ret ) );
2746 }
2747 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002748 plaintext_length, nonce,
2749 nonce_length, additional_data,
2750 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002751 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002752 mbedtls_gcm_free( &gcm );
2753 }
2754 else if( alg == PSA_ALG_CCM )
2755 {
2756 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002757 tag_length = 16;
2758
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002759 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002760 return( PSA_ERROR_INVALID_ARGUMENT );
2761
mohammad160347ddf3d2018-04-26 01:11:21 +03002762 if( nonce_length < 7 || nonce_length > 13 )
2763 return( PSA_ERROR_INVALID_ARGUMENT );
2764
mohammad160315223a82018-06-03 17:19:55 +03002765 //make sure we have place to hold the tag in the ciphertext buffer
2766 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002767 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002768
2769 //update the tag pointer to point to the end of the ciphertext_length
2770 tag = ciphertext + plaintext_length;
2771
mohammad16035955c982018-04-26 00:53:03 +03002772 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002773 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002774 slot->data.raw.data,
2775 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002776 if( ret != 0 )
2777 {
2778 mbedtls_ccm_free( &ccm );
2779 return( mbedtls_to_psa_error( ret ) );
2780 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002781 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002782 nonce, nonce_length,
2783 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002784 additional_data_length,
2785 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002786 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002787 mbedtls_ccm_free( &ccm );
2788 }
mohammad16035c8845f2018-05-09 05:40:09 -07002789 else
2790 {
mohammad1603554faad2018-06-03 15:07:38 +03002791 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002792 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002793
mohammad160315223a82018-06-03 17:19:55 +03002794 if( ret != 0 )
2795 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002796 /* If ciphertext_size is 0 then ciphertext may be NULL and then the
2797 * call to memset would have undefined behavior. */
2798 if( ciphertext_size != 0 )
2799 memset( ciphertext, 0, ciphertext_size );
mohammad160315223a82018-06-03 17:19:55 +03002800 return( mbedtls_to_psa_error( ret ) );
2801 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002802
mohammad160315223a82018-06-03 17:19:55 +03002803 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002804 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002805}
2806
Gilles Peskineee652a32018-06-01 19:23:52 +02002807/* Locate the tag in a ciphertext buffer containing the encrypted data
2808 * followed by the tag. Return the length of the part preceding the tag in
2809 * *plaintext_length. This is the size of the plaintext in modes where
2810 * the encrypted data has the same size as the plaintext, such as
2811 * CCM and GCM. */
2812static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2813 const uint8_t *ciphertext,
2814 size_t ciphertext_length,
2815 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002816 const uint8_t **p_tag )
2817{
2818 size_t payload_length;
2819 if( tag_length > ciphertext_length )
2820 return( PSA_ERROR_INVALID_ARGUMENT );
2821 payload_length = ciphertext_length - tag_length;
2822 if( payload_length > plaintext_size )
2823 return( PSA_ERROR_BUFFER_TOO_SMALL );
2824 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002825 return( PSA_SUCCESS );
2826}
2827
mohammad16035955c982018-04-26 00:53:03 +03002828psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2829 psa_algorithm_t alg,
2830 const uint8_t *nonce,
2831 size_t nonce_length,
2832 const uint8_t *additional_data,
2833 size_t additional_data_length,
2834 const uint8_t *ciphertext,
2835 size_t ciphertext_length,
2836 uint8_t *plaintext,
2837 size_t plaintext_size,
2838 size_t *plaintext_length )
2839{
2840 int ret;
2841 psa_status_t status;
2842 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002843 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002844 const uint8_t *tag;
2845 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002846 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002847 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002848
Gilles Peskineee652a32018-06-01 19:23:52 +02002849 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002850
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002851 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2852 if( status != PSA_SUCCESS )
2853 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002854 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002855
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002856 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002857 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002858 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002859 return( PSA_ERROR_NOT_SUPPORTED );
2860
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002861 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002862 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002863 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002864
mohammad16035955c982018-04-26 00:53:03 +03002865 if( alg == PSA_ALG_GCM )
2866 {
2867 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002868
Gilles Peskineee652a32018-06-01 19:23:52 +02002869 tag_length = 16;
2870 status = psa_aead_unpadded_locate_tag( tag_length,
2871 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002872 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002873 if( status != PSA_SUCCESS )
2874 return( status );
2875
mohammad16035955c982018-04-26 00:53:03 +03002876 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002877 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002878 slot->data.raw.data,
2879 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002880 if( ret != 0 )
2881 {
2882 mbedtls_gcm_free( &gcm );
2883 return( mbedtls_to_psa_error( ret ) );
2884 }
mohammad16035955c982018-04-26 00:53:03 +03002885
Gilles Peskineee652a32018-06-01 19:23:52 +02002886 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002887 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002888 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002889 additional_data,
2890 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002891 tag, tag_length,
2892 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002893 mbedtls_gcm_free( &gcm );
2894 }
2895 else if( alg == PSA_ALG_CCM )
2896 {
2897 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002898
mohammad160347ddf3d2018-04-26 01:11:21 +03002899 if( nonce_length < 7 || nonce_length > 13 )
2900 return( PSA_ERROR_INVALID_ARGUMENT );
2901
mohammad16039375f842018-06-03 14:28:24 +03002902 tag_length = 16;
2903 status = psa_aead_unpadded_locate_tag( tag_length,
2904 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002905 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002906 if( status != PSA_SUCCESS )
2907 return( status );
2908
mohammad16035955c982018-04-26 00:53:03 +03002909 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002910 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002911 slot->data.raw.data,
2912 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002913 if( ret != 0 )
2914 {
2915 mbedtls_ccm_free( &ccm );
2916 return( mbedtls_to_psa_error( ret ) );
2917 }
mohammad160360a64d02018-06-03 17:20:42 +03002918 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002919 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002920 additional_data,
2921 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002922 ciphertext, plaintext,
2923 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002924 mbedtls_ccm_free( &ccm );
2925 }
mohammad160339574652018-06-01 04:39:53 -07002926 else
2927 {
mohammad1603554faad2018-06-03 15:07:38 +03002928 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07002929 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002930
Gilles Peskineee652a32018-06-01 19:23:52 +02002931 if( ret != 0 )
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002932 {
2933 /* If plaintext_size is 0 then plaintext may be NULL and then the
2934 * call to memset has undefined behavior. */
2935 if( plaintext_size != 0 )
2936 memset( plaintext, 0, plaintext_size );
2937 }
mohammad160360a64d02018-06-03 17:20:42 +03002938 else
2939 *plaintext_length = ciphertext_length - tag_length;
2940
Gilles Peskineee652a32018-06-01 19:23:52 +02002941 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03002942}
2943
Gilles Peskinea0655c32018-04-30 17:06:50 +02002944
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002945
Gilles Peskine20035e32018-02-03 22:44:14 +01002946/****************************************************************/
Gilles Peskine05d69892018-06-19 22:00:52 +02002947/* Key generation */
2948/****************************************************************/
2949
2950psa_status_t psa_generate_random( uint8_t *output,
2951 size_t output_size )
2952{
2953 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2954 output, output_size );
2955 return( mbedtls_to_psa_error( ret ) );
2956}
2957
2958psa_status_t psa_generate_key( psa_key_slot_t key,
2959 psa_key_type_t type,
2960 size_t bits,
2961 const void *parameters,
2962 size_t parameters_size )
2963{
Gilles Peskine12313cd2018-06-20 00:20:32 +02002964 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002965 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02002966
Gilles Peskine12313cd2018-06-20 00:20:32 +02002967 if( parameters == NULL && parameters_size != 0 )
2968 return( PSA_ERROR_INVALID_ARGUMENT );
2969
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002970 status = psa_get_empty_key_slot( key, &slot );
2971 if( status != PSA_SUCCESS )
2972 return( status );
2973
Gilles Peskine48c0ea12018-06-21 14:15:31 +02002974 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002975 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002976 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02002977 if( status != PSA_SUCCESS )
2978 return( status );
2979 status = psa_generate_random( slot->data.raw.data,
2980 slot->data.raw.bytes );
2981 if( status != PSA_SUCCESS )
2982 {
2983 mbedtls_free( slot->data.raw.data );
2984 return( status );
2985 }
2986#if defined(MBEDTLS_DES_C)
2987 if( type == PSA_KEY_TYPE_DES )
2988 {
2989 mbedtls_des_key_set_parity( slot->data.raw.data );
2990 if( slot->data.raw.bytes >= 16 )
2991 mbedtls_des_key_set_parity( slot->data.raw.data + 8 );
2992 if( slot->data.raw.bytes == 24 )
2993 mbedtls_des_key_set_parity( slot->data.raw.data + 16 );
2994 }
2995#endif /* MBEDTLS_DES_C */
2996 }
2997 else
2998
2999#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3000 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3001 {
3002 mbedtls_rsa_context *rsa;
3003 int ret;
3004 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003005 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3006 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003007 if( parameters != NULL )
3008 {
3009 const unsigned *p = parameters;
3010 if( parameters_size != sizeof( *p ) )
3011 return( PSA_ERROR_INVALID_ARGUMENT );
3012 if( *p > INT_MAX )
3013 return( PSA_ERROR_INVALID_ARGUMENT );
3014 exponent = *p;
3015 }
3016 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3017 if( rsa == NULL )
3018 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3019 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3020 ret = mbedtls_rsa_gen_key( rsa,
3021 mbedtls_ctr_drbg_random,
3022 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003023 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003024 exponent );
3025 if( ret != 0 )
3026 {
3027 mbedtls_rsa_free( rsa );
3028 mbedtls_free( rsa );
3029 return( mbedtls_to_psa_error( ret ) );
3030 }
3031 slot->data.rsa = rsa;
3032 }
3033 else
3034#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3035
3036#if defined(MBEDTLS_ECP_C)
3037 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3038 {
3039 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3040 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3041 const mbedtls_ecp_curve_info *curve_info =
3042 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3043 mbedtls_ecp_keypair *ecp;
3044 int ret;
3045 if( parameters != NULL )
3046 return( PSA_ERROR_NOT_SUPPORTED );
3047 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3048 return( PSA_ERROR_NOT_SUPPORTED );
3049 if( curve_info->bit_size != bits )
3050 return( PSA_ERROR_INVALID_ARGUMENT );
3051 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3052 if( ecp == NULL )
3053 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3054 mbedtls_ecp_keypair_init( ecp );
3055 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3056 mbedtls_ctr_drbg_random,
3057 &global_data.ctr_drbg );
3058 if( ret != 0 )
3059 {
3060 mbedtls_ecp_keypair_free( ecp );
3061 mbedtls_free( ecp );
3062 return( mbedtls_to_psa_error( ret ) );
3063 }
3064 slot->data.ecp = ecp;
3065 }
3066 else
3067#endif /* MBEDTLS_ECP_C */
3068
3069 return( PSA_ERROR_NOT_SUPPORTED );
3070
3071 slot->type = type;
3072 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003073}
3074
3075
3076/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003077/* Module setup */
3078/****************************************************************/
3079
Gilles Peskinee59236f2018-01-27 23:32:46 +01003080void mbedtls_psa_crypto_free( void )
3081{
Jaeden Amero045bd502018-06-26 14:00:08 +01003082 psa_key_slot_t key;
Gilles Peskine828ed142018-06-18 23:25:51 +02003083 for( key = 1; key < PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003084 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003085 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3086 mbedtls_entropy_free( &global_data.entropy );
3087 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3088}
3089
3090psa_status_t psa_crypto_init( void )
3091{
3092 int ret;
3093 const unsigned char drbg_seed[] = "PSA";
3094
3095 if( global_data.initialized != 0 )
3096 return( PSA_SUCCESS );
3097
3098 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3099 mbedtls_entropy_init( &global_data.entropy );
3100 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3101
3102 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3103 mbedtls_entropy_func,
3104 &global_data.entropy,
3105 drbg_seed, sizeof( drbg_seed ) - 1 );
3106 if( ret != 0 )
3107 goto exit;
3108
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003109 global_data.initialized = 1;
3110
Gilles Peskinee59236f2018-01-27 23:32:46 +01003111exit:
3112 if( ret != 0 )
3113 mbedtls_psa_crypto_free( );
3114 return( mbedtls_to_psa_error( ret ) );
3115}
3116
3117#endif /* MBEDTLS_PSA_CRYPTO_C */