blob: f156d0c271c969e6f87ee075a7409c5c244ffded [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 Peskine2d277862018-06-18 15:41:12 +0200691psa_status_t psa_get_key_information( psa_key_slot_t key,
692 psa_key_type_t *type,
693 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100694{
695 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200696 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100697
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100698 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200699 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100700 if( bits != NULL )
701 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200702 status = psa_get_key_slot( key, &slot );
703 if( status != PSA_SUCCESS )
704 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100705 if( slot->type == PSA_KEY_TYPE_NONE )
706 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200707 if( type != NULL )
708 *type = slot->type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100709
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200710 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100711 {
712 if( bits != NULL )
713 *bits = slot->data.raw.bytes * 8;
714 }
715 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100716#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100717 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
718 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100719 {
720 if( bits != NULL )
Gilles Peskine969ac722018-01-28 18:16:59 +0100721 *bits = mbedtls_rsa_get_bitlen( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100722 }
723 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100724#endif /* defined(MBEDTLS_RSA_C) */
725#if defined(MBEDTLS_ECP_C)
726 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
727 {
728 if( bits != NULL )
729 *bits = slot->data.ecp->grp.pbits;
730 }
731 else
732#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100733 {
734 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100735 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100736 return( PSA_ERROR_TAMPERING_DETECTED );
737 }
738
739 return( PSA_SUCCESS );
740}
741
Gilles Peskine2d277862018-06-18 15:41:12 +0200742static psa_status_t psa_internal_export_key( psa_key_slot_t key,
743 uint8_t *data,
744 size_t data_size,
745 size_t *data_length,
746 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100747{
748 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200749 psa_status_t status;
750 /* Exporting a public key doesn't require a usage flag. If we're
751 * called by psa_export_public_key(), don't require the EXPORT flag.
752 * If we're called by psa_export_key(), do require the EXPORT flag;
753 * if the key turns out to be public key object, psa_get_key_from_slot()
754 * will ignore this flag. */
755 psa_key_usage_t usage = export_public_key ? 0 : PSA_KEY_USAGE_EXPORT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100756
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100757 /* Set the key to empty now, so that even when there are errors, we always
758 * set data_length to a value between 0 and data_size. On error, setting
759 * the key to empty is a good choice because an empty key representation is
760 * unlikely to be accepted anywhere. */
761 *data_length = 0;
762
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200763 status = psa_get_key_from_slot( key, &slot, usage, 0 );
764 if( status != PSA_SUCCESS )
765 return( status );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200766 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300767 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300768
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200769 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100770 {
771 if( slot->data.raw.bytes > data_size )
772 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200773 if( slot->data.raw.bytes != 0 )
774 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100775 *data_length = slot->data.raw.bytes;
776 return( PSA_SUCCESS );
777 }
778 else
Moran Peker17e36e12018-05-02 12:55:20 +0300779 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100780#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100781 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
Moran Pekera998bc62018-04-16 18:16:20 +0300782 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
783 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100784 {
Moran Pekera998bc62018-04-16 18:16:20 +0300785 mbedtls_pk_context pk;
786 int ret;
787 mbedtls_pk_init( &pk );
788 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
789 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
790 {
791 pk.pk_info = &mbedtls_rsa_info;
792 pk.pk_ctx = slot->data.rsa;
793 }
794 else
795 {
796 pk.pk_info = &mbedtls_eckey_info;
797 pk.pk_ctx = slot->data.ecp;
798 }
Moran Pekerd7326592018-05-29 16:56:39 +0300799 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300800 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300801 else
802 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300803 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200804 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200805 /* If data_size is 0 then data may be NULL and then the
806 * call to memset would have undefined behavior. */
807 if( data_size != 0 )
808 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300809 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200810 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200811 /* The mbedtls_pk_xxx functions write to the end of the buffer.
812 * Move the data to the beginning and erase remaining data
813 * at the original location. */
814 if( 2 * (size_t) ret <= data_size )
815 {
816 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200817 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200818 }
819 else if( (size_t) ret < data_size )
820 {
821 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200822 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200823 }
Moran Pekera998bc62018-04-16 18:16:20 +0300824 *data_length = ret;
825 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100826 }
827 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100828#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300829 {
830 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200831 it is valid for a special-purpose implementation to omit
832 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300833 return( PSA_ERROR_NOT_SUPPORTED );
834 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100835 }
836}
837
Gilles Peskine2d277862018-06-18 15:41:12 +0200838psa_status_t psa_export_key( psa_key_slot_t key,
839 uint8_t *data,
840 size_t data_size,
841 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300842{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200843 return( psa_internal_export_key( key, data, data_size,
844 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100845}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100846
Gilles Peskine2d277862018-06-18 15:41:12 +0200847psa_status_t psa_export_public_key( psa_key_slot_t key,
848 uint8_t *data,
849 size_t data_size,
850 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300851{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200852 return( psa_internal_export_key( key, data, data_size,
853 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300854}
855
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200856
857
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100858/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100859/* Message digests */
860/****************************************************************/
861
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100862static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100863{
864 switch( alg )
865 {
866#if defined(MBEDTLS_MD2_C)
867 case PSA_ALG_MD2:
868 return( &mbedtls_md2_info );
869#endif
870#if defined(MBEDTLS_MD4_C)
871 case PSA_ALG_MD4:
872 return( &mbedtls_md4_info );
873#endif
874#if defined(MBEDTLS_MD5_C)
875 case PSA_ALG_MD5:
876 return( &mbedtls_md5_info );
877#endif
878#if defined(MBEDTLS_RIPEMD160_C)
879 case PSA_ALG_RIPEMD160:
880 return( &mbedtls_ripemd160_info );
881#endif
882#if defined(MBEDTLS_SHA1_C)
883 case PSA_ALG_SHA_1:
884 return( &mbedtls_sha1_info );
885#endif
886#if defined(MBEDTLS_SHA256_C)
887 case PSA_ALG_SHA_224:
888 return( &mbedtls_sha224_info );
889 case PSA_ALG_SHA_256:
890 return( &mbedtls_sha256_info );
891#endif
892#if defined(MBEDTLS_SHA512_C)
893 case PSA_ALG_SHA_384:
894 return( &mbedtls_sha384_info );
895 case PSA_ALG_SHA_512:
896 return( &mbedtls_sha512_info );
897#endif
898 default:
899 return( NULL );
900 }
901}
902
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100903psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
904{
905 switch( operation->alg )
906 {
Gilles Peskine81736312018-06-26 15:04:31 +0200907 case 0:
908 /* The object has (apparently) been initialized but it is not
909 * in use. It's ok to call abort on such an object, and there's
910 * nothing to do. */
911 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100912#if defined(MBEDTLS_MD2_C)
913 case PSA_ALG_MD2:
914 mbedtls_md2_free( &operation->ctx.md2 );
915 break;
916#endif
917#if defined(MBEDTLS_MD4_C)
918 case PSA_ALG_MD4:
919 mbedtls_md4_free( &operation->ctx.md4 );
920 break;
921#endif
922#if defined(MBEDTLS_MD5_C)
923 case PSA_ALG_MD5:
924 mbedtls_md5_free( &operation->ctx.md5 );
925 break;
926#endif
927#if defined(MBEDTLS_RIPEMD160_C)
928 case PSA_ALG_RIPEMD160:
929 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
930 break;
931#endif
932#if defined(MBEDTLS_SHA1_C)
933 case PSA_ALG_SHA_1:
934 mbedtls_sha1_free( &operation->ctx.sha1 );
935 break;
936#endif
937#if defined(MBEDTLS_SHA256_C)
938 case PSA_ALG_SHA_224:
939 case PSA_ALG_SHA_256:
940 mbedtls_sha256_free( &operation->ctx.sha256 );
941 break;
942#endif
943#if defined(MBEDTLS_SHA512_C)
944 case PSA_ALG_SHA_384:
945 case PSA_ALG_SHA_512:
946 mbedtls_sha512_free( &operation->ctx.sha512 );
947 break;
948#endif
949 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +0200950 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100951 }
952 operation->alg = 0;
953 return( PSA_SUCCESS );
954}
955
956psa_status_t psa_hash_start( psa_hash_operation_t *operation,
957 psa_algorithm_t alg )
958{
959 int ret;
960 operation->alg = 0;
961 switch( alg )
962 {
963#if defined(MBEDTLS_MD2_C)
964 case PSA_ALG_MD2:
965 mbedtls_md2_init( &operation->ctx.md2 );
966 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
967 break;
968#endif
969#if defined(MBEDTLS_MD4_C)
970 case PSA_ALG_MD4:
971 mbedtls_md4_init( &operation->ctx.md4 );
972 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
973 break;
974#endif
975#if defined(MBEDTLS_MD5_C)
976 case PSA_ALG_MD5:
977 mbedtls_md5_init( &operation->ctx.md5 );
978 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
979 break;
980#endif
981#if defined(MBEDTLS_RIPEMD160_C)
982 case PSA_ALG_RIPEMD160:
983 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
984 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
985 break;
986#endif
987#if defined(MBEDTLS_SHA1_C)
988 case PSA_ALG_SHA_1:
989 mbedtls_sha1_init( &operation->ctx.sha1 );
990 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
991 break;
992#endif
993#if defined(MBEDTLS_SHA256_C)
994 case PSA_ALG_SHA_224:
995 mbedtls_sha256_init( &operation->ctx.sha256 );
996 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
997 break;
998 case PSA_ALG_SHA_256:
999 mbedtls_sha256_init( &operation->ctx.sha256 );
1000 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1001 break;
1002#endif
1003#if defined(MBEDTLS_SHA512_C)
1004 case PSA_ALG_SHA_384:
1005 mbedtls_sha512_init( &operation->ctx.sha512 );
1006 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1007 break;
1008 case PSA_ALG_SHA_512:
1009 mbedtls_sha512_init( &operation->ctx.sha512 );
1010 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1011 break;
1012#endif
1013 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001014 return( PSA_ALG_IS_HASH( alg ) ?
1015 PSA_ERROR_NOT_SUPPORTED :
1016 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001017 }
1018 if( ret == 0 )
1019 operation->alg = alg;
1020 else
1021 psa_hash_abort( operation );
1022 return( mbedtls_to_psa_error( ret ) );
1023}
1024
1025psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1026 const uint8_t *input,
1027 size_t input_length )
1028{
1029 int ret;
1030 switch( operation->alg )
1031 {
1032#if defined(MBEDTLS_MD2_C)
1033 case PSA_ALG_MD2:
1034 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1035 input, input_length );
1036 break;
1037#endif
1038#if defined(MBEDTLS_MD4_C)
1039 case PSA_ALG_MD4:
1040 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1041 input, input_length );
1042 break;
1043#endif
1044#if defined(MBEDTLS_MD5_C)
1045 case PSA_ALG_MD5:
1046 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1047 input, input_length );
1048 break;
1049#endif
1050#if defined(MBEDTLS_RIPEMD160_C)
1051 case PSA_ALG_RIPEMD160:
1052 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1053 input, input_length );
1054 break;
1055#endif
1056#if defined(MBEDTLS_SHA1_C)
1057 case PSA_ALG_SHA_1:
1058 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1059 input, input_length );
1060 break;
1061#endif
1062#if defined(MBEDTLS_SHA256_C)
1063 case PSA_ALG_SHA_224:
1064 case PSA_ALG_SHA_256:
1065 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1066 input, input_length );
1067 break;
1068#endif
1069#if defined(MBEDTLS_SHA512_C)
1070 case PSA_ALG_SHA_384:
1071 case PSA_ALG_SHA_512:
1072 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1073 input, input_length );
1074 break;
1075#endif
1076 default:
1077 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1078 break;
1079 }
1080 if( ret != 0 )
1081 psa_hash_abort( operation );
1082 return( mbedtls_to_psa_error( ret ) );
1083}
1084
1085psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1086 uint8_t *hash,
1087 size_t hash_size,
1088 size_t *hash_length )
1089{
1090 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001091 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001092
1093 /* Fill the output buffer with something that isn't a valid hash
1094 * (barring an attack on the hash and deliberately-crafted input),
1095 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001096 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001097 /* If hash_size is 0 then hash may be NULL and then the
1098 * call to memset would have undefined behavior. */
1099 if( hash_size != 0 )
1100 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001101
1102 if( hash_size < actual_hash_length )
1103 return( PSA_ERROR_BUFFER_TOO_SMALL );
1104
1105 switch( operation->alg )
1106 {
1107#if defined(MBEDTLS_MD2_C)
1108 case PSA_ALG_MD2:
1109 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1110 break;
1111#endif
1112#if defined(MBEDTLS_MD4_C)
1113 case PSA_ALG_MD4:
1114 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1115 break;
1116#endif
1117#if defined(MBEDTLS_MD5_C)
1118 case PSA_ALG_MD5:
1119 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1120 break;
1121#endif
1122#if defined(MBEDTLS_RIPEMD160_C)
1123 case PSA_ALG_RIPEMD160:
1124 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1125 break;
1126#endif
1127#if defined(MBEDTLS_SHA1_C)
1128 case PSA_ALG_SHA_1:
1129 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1130 break;
1131#endif
1132#if defined(MBEDTLS_SHA256_C)
1133 case PSA_ALG_SHA_224:
1134 case PSA_ALG_SHA_256:
1135 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1136 break;
1137#endif
1138#if defined(MBEDTLS_SHA512_C)
1139 case PSA_ALG_SHA_384:
1140 case PSA_ALG_SHA_512:
1141 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1142 break;
1143#endif
1144 default:
1145 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1146 break;
1147 }
1148
1149 if( ret == 0 )
1150 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001151 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001152 return( psa_hash_abort( operation ) );
1153 }
1154 else
1155 {
1156 psa_hash_abort( operation );
1157 return( mbedtls_to_psa_error( ret ) );
1158 }
1159}
1160
Gilles Peskine2d277862018-06-18 15:41:12 +02001161psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1162 const uint8_t *hash,
1163 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001164{
1165 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1166 size_t actual_hash_length;
1167 psa_status_t status = psa_hash_finish( operation,
1168 actual_hash, sizeof( actual_hash ),
1169 &actual_hash_length );
1170 if( status != PSA_SUCCESS )
1171 return( status );
1172 if( actual_hash_length != hash_length )
1173 return( PSA_ERROR_INVALID_SIGNATURE );
1174 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1175 return( PSA_ERROR_INVALID_SIGNATURE );
1176 return( PSA_SUCCESS );
1177}
1178
1179
1180
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001181/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001182/* MAC */
1183/****************************************************************/
1184
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001185static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001186 psa_algorithm_t alg,
1187 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001188 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001189 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001190{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001191 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001192 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001193
1194 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1195 {
1196 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001197 {
1198 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1199 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001200
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001201 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001202 {
1203 case PSA_ALG_STREAM_CIPHER:
1204 mode = MBEDTLS_MODE_STREAM;
1205 break;
1206 case PSA_ALG_CBC_BASE:
1207 mode = MBEDTLS_MODE_CBC;
1208 break;
1209 case PSA_ALG_CFB_BASE:
1210 mode = MBEDTLS_MODE_CFB;
1211 break;
1212 case PSA_ALG_OFB_BASE:
1213 mode = MBEDTLS_MODE_OFB;
1214 break;
1215 case PSA_ALG_CTR:
1216 mode = MBEDTLS_MODE_CTR;
1217 break;
1218 case PSA_ALG_CCM:
1219 mode = MBEDTLS_MODE_CCM;
1220 break;
1221 case PSA_ALG_GCM:
1222 mode = MBEDTLS_MODE_GCM;
1223 break;
1224 default:
1225 return( NULL );
1226 }
1227 }
1228 else if( alg == PSA_ALG_CMAC )
1229 mode = MBEDTLS_MODE_ECB;
1230 else if( alg == PSA_ALG_GMAC )
1231 mode = MBEDTLS_MODE_GCM;
1232 else
1233 return( NULL );
1234
1235 switch( key_type )
1236 {
1237 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001238 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001239 break;
1240 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001241 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1242 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001243 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001244 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001245 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001246 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001247 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1248 * but two-key Triple-DES is functionally three-key Triple-DES
1249 * with K1=K3, so that's how we present it to mbedtls. */
1250 if( key_bits == 128 )
1251 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001252 break;
1253 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001254 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001255 break;
1256 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001257 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001258 break;
1259 default:
1260 return( NULL );
1261 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001262 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001263 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001264
Jaeden Amero23bbb752018-06-26 14:16:54 +01001265 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1266 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001267}
1268
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001269static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001270{
Gilles Peskine2d277862018-06-18 15:41:12 +02001271 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001272 {
1273 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001274 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001275 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001276 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001277 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001278 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001279 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001280 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001281 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001282 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001283 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001284 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001285 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001286 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001287 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001288 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001289 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001290 return( 128 );
1291 default:
1292 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001293 }
1294}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001295
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001296/* Initialize the MAC operation structure. Once this function has been
1297 * called, psa_mac_abort can run and will do the right thing. */
1298static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1299 psa_algorithm_t alg )
1300{
1301 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1302
1303 operation->alg = alg;
1304 operation->key_set = 0;
1305 operation->iv_set = 0;
1306 operation->iv_required = 0;
1307 operation->has_input = 0;
1308 operation->key_usage_sign = 0;
1309 operation->key_usage_verify = 0;
1310
1311#if defined(MBEDTLS_CMAC_C)
1312 if( alg == PSA_ALG_CMAC )
1313 {
1314 operation->iv_required = 0;
1315 mbedtls_cipher_init( &operation->ctx.cmac );
1316 status = PSA_SUCCESS;
1317 }
1318 else
1319#endif /* MBEDTLS_CMAC_C */
1320#if defined(MBEDTLS_MD_C)
1321 if( PSA_ALG_IS_HMAC( operation->alg ) )
1322 {
1323 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1324 PSA_ALG_HMAC_HASH( alg ) );
1325 }
1326 else
1327#endif /* MBEDTLS_MD_C */
1328 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001329 if( ! PSA_ALG_IS_MAC( alg ) )
1330 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001331 }
1332
1333 if( status != PSA_SUCCESS )
1334 memset( operation, 0, sizeof( *operation ) );
1335 return( status );
1336}
1337
Gilles Peskine8c9def32018-02-08 10:02:12 +01001338psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1339{
1340 switch( operation->alg )
1341 {
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001342 case 0:
Gilles Peskine81736312018-06-26 15:04:31 +02001343 /* The object has (apparently) been initialized but it is not
1344 * in use. It's ok to call abort on such an object, and there's
1345 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001346 return( PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001347#if defined(MBEDTLS_CMAC_C)
1348 case PSA_ALG_CMAC:
1349 mbedtls_cipher_free( &operation->ctx.cmac );
1350 break;
1351#endif /* MBEDTLS_CMAC_C */
1352 default:
1353#if defined(MBEDTLS_MD_C)
1354 if( PSA_ALG_IS_HMAC( operation->alg ) )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001355 {
Jaeden Amero5390f692018-06-26 14:18:50 +01001356 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001357 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001358
Gilles Peskine99bc6492018-06-11 17:13:00 +02001359 if( block_size == 0 )
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001360 return( PSA_ERROR_NOT_SUPPORTED );
1361
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001362 psa_hash_abort( &operation->ctx.hmac.hash_ctx );
Gilles Peskine2d277862018-06-18 15:41:12 +02001363 mbedtls_zeroize( operation->ctx.hmac.opad, block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001364 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001365 else
1366#endif /* MBEDTLS_MD_C */
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001367 {
1368 /* Sanity check (shouldn't happen: operation->alg should
1369 * always have been initialized to a valid value). */
1370 return( PSA_ERROR_BAD_STATE );
1371 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001372 }
Moran Peker41deec42018-04-04 15:43:05 +03001373
Gilles Peskine8c9def32018-02-08 10:02:12 +01001374 operation->alg = 0;
1375 operation->key_set = 0;
1376 operation->iv_set = 0;
1377 operation->iv_required = 0;
1378 operation->has_input = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001379 operation->key_usage_sign = 0;
1380 operation->key_usage_verify = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001381
Gilles Peskine8c9def32018-02-08 10:02:12 +01001382 return( PSA_SUCCESS );
1383}
1384
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001385#if defined(MBEDTLS_CMAC_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001386static int psa_cmac_start( psa_mac_operation_t *operation,
1387 size_t key_bits,
1388 key_slot_t *slot,
1389 const mbedtls_cipher_info_t *cipher_info )
1390{
1391 int ret;
1392
1393 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001394
1395 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1396 if( ret != 0 )
1397 return( ret );
1398
1399 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1400 slot->data.raw.data,
1401 key_bits );
1402 return( ret );
1403}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001404#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001405
Gilles Peskine248051a2018-06-20 16:09:38 +02001406#if defined(MBEDTLS_MD_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001407static int psa_hmac_start( psa_mac_operation_t *operation,
1408 psa_key_type_t key_type,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001409 key_slot_t *slot,
1410 psa_algorithm_t alg )
1411{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001412 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001413 unsigned char *opad = operation->ctx.hmac.opad;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001414 size_t i;
Gilles Peskinee1bc6802018-06-11 17:36:05 +02001415 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001416 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001417 unsigned int digest_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001418 PSA_HASH_SIZE( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001419 size_t key_length = slot->data.raw.bytes;
1420 psa_status_t status;
1421
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001422 if( block_size == 0 || digest_size == 0 )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001423 return( PSA_ERROR_NOT_SUPPORTED );
1424
1425 if( key_type != PSA_KEY_TYPE_HMAC )
1426 return( PSA_ERROR_INVALID_ARGUMENT );
1427
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001428 operation->mac_size = digest_size;
1429
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001430 /* The hash was started earlier in psa_mac_init. */
Gilles Peskined223b522018-06-11 18:12:58 +02001431 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001432 {
1433 status = psa_hash_update( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001434 slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001435 if( status != PSA_SUCCESS )
1436 return( status );
1437 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001438 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001439 if( status != PSA_SUCCESS )
1440 return( status );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001441 }
Gilles Peskined223b522018-06-11 18:12:58 +02001442 else
1443 memcpy( ipad, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001444
Gilles Peskined223b522018-06-11 18:12:58 +02001445 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1446 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001447 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001448 ipad[i] ^= 0x36;
1449 memset( ipad + key_length, 0x36, block_size - key_length );
1450
1451 /* Copy the key material from ipad to opad, flipping the requisite bits,
1452 * and filling the rest of opad with the requisite constant. */
1453 for( i = 0; i < key_length; i++ )
1454 opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1455 memset( opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001456
1457 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1458 PSA_ALG_HMAC_HASH( alg ) );
1459 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001460 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001461
1462 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, ipad,
1463 block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001464
1465cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001466 mbedtls_zeroize( ipad, key_length );
1467 /* opad is in the context. It needs to stay in memory if this function
1468 * succeeds, and it will be wiped by psa_mac_abort() called from
1469 * psa_mac_start in the error case. */
1470
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001471 return( status );
1472}
Gilles Peskine248051a2018-06-20 16:09:38 +02001473#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001474
Gilles Peskine8c9def32018-02-08 10:02:12 +01001475psa_status_t psa_mac_start( psa_mac_operation_t *operation,
1476 psa_key_slot_t key,
1477 psa_algorithm_t alg )
1478{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001479 psa_status_t status;
1480 key_slot_t *slot;
1481 psa_key_type_t key_type;
1482 size_t key_bits;
Gilles Peskine828ed142018-06-18 23:25:51 +02001483 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001484
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001485 status = psa_mac_init( operation, alg );
1486 if( status != PSA_SUCCESS )
1487 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001488
1489 status = psa_get_key_information( key, &key_type, &key_bits );
1490 if( status != PSA_SUCCESS )
1491 return( status );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001492
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001493 status = psa_get_key_from_slot( key, &slot, 0, alg );
1494 if( status != PSA_SUCCESS )
1495 return( status );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001496
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001497 /* Since this function is called identically for a sign or verify
1498 * operation, we don't know yet whether the operation is permitted.
1499 * Store the part of the key policy that we can't check in the
1500 * operation structure. psa_mac_finish() or psa_mac_verify() will
1501 * check that remaining part. */
Moran Pekerd7326592018-05-29 16:56:39 +03001502 if( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001503 operation->key_usage_sign = 1;
Moran Pekerd7326592018-05-29 16:56:39 +03001504 if( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001505 operation->key_usage_verify = 1;
1506
Gilles Peskine8c9def32018-02-08 10:02:12 +01001507 if( ! PSA_ALG_IS_HMAC( alg ) )
1508 {
mohammad1603f4f0d612018-06-03 15:04:51 +03001509 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001510 if( cipher_info == NULL )
1511 return( PSA_ERROR_NOT_SUPPORTED );
1512 operation->mac_size = cipher_info->block_size;
1513 }
1514 switch( alg )
1515 {
1516#if defined(MBEDTLS_CMAC_C)
1517 case PSA_ALG_CMAC:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001518 status = mbedtls_to_psa_error( psa_cmac_start( operation,
1519 key_bits,
1520 slot,
1521 cipher_info ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001522 break;
1523#endif /* MBEDTLS_CMAC_C */
1524 default:
1525#if defined(MBEDTLS_MD_C)
1526 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskined223b522018-06-11 18:12:58 +02001527 status = psa_hmac_start( operation, key_type, slot, alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001528 else
1529#endif /* MBEDTLS_MD_C */
1530 return( PSA_ERROR_NOT_SUPPORTED );
1531 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001532
Gilles Peskine8c9def32018-02-08 10:02:12 +01001533 /* If we reach this point, then the algorithm-specific part of the
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001534 * context may contain data that needs to be wiped on error. */
1535 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001536 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001537 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001538 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001539 else
1540 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001541 operation->key_set = 1;
1542 }
1543 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001544}
1545
1546psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1547 const uint8_t *input,
1548 size_t input_length )
1549{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001550 int ret = 0 ;
1551 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001552 if( ! operation->key_set )
1553 return( PSA_ERROR_BAD_STATE );
1554 if( operation->iv_required && ! operation->iv_set )
1555 return( PSA_ERROR_BAD_STATE );
1556 operation->has_input = 1;
1557
1558 switch( operation->alg )
1559 {
1560#if defined(MBEDTLS_CMAC_C)
1561 case PSA_ALG_CMAC:
1562 ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1563 input, input_length );
1564 break;
1565#endif /* MBEDTLS_CMAC_C */
1566 default:
1567#if defined(MBEDTLS_MD_C)
1568 if( PSA_ALG_IS_HMAC( operation->alg ) )
1569 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001570 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
Gilles Peskine2d277862018-06-18 15:41:12 +02001571 input_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001572 }
1573 else
1574#endif /* MBEDTLS_MD_C */
1575 {
1576 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1577 }
1578 break;
1579 }
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001580 if( ret != 0 || status != PSA_SUCCESS )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001581 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001582 psa_mac_abort( operation );
Gilles Peskine2d277862018-06-18 15:41:12 +02001583 if( ret != 0 )
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001584 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001585 }
1586
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001587 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001588}
1589
mohammad16036df908f2018-04-02 08:34:15 -07001590static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001591 uint8_t *mac,
1592 size_t mac_size,
1593 size_t *mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001594{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001595 int ret = 0;
1596 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001597
1598 /* Fill the output buffer with something that isn't a valid mac
1599 * (barring an attack on the mac and deliberately-crafted input),
1600 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001601 *mac_length = mac_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001602 /* If mac_size is 0 then mac may be NULL and then the
1603 * call to memset would have undefined behavior. */
1604 if( mac_size != 0 )
1605 memset( mac, '!', mac_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001606
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001607 if( ! operation->key_set )
1608 return( PSA_ERROR_BAD_STATE );
1609 if( operation->iv_required && ! operation->iv_set )
1610 return( PSA_ERROR_BAD_STATE );
1611
Gilles Peskine8c9def32018-02-08 10:02:12 +01001612 if( mac_size < operation->mac_size )
1613 return( PSA_ERROR_BUFFER_TOO_SMALL );
1614
1615 switch( operation->alg )
1616 {
1617#if defined(MBEDTLS_CMAC_C)
1618 case PSA_ALG_CMAC:
1619 ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1620 break;
1621#endif /* MBEDTLS_CMAC_C */
1622 default:
1623#if defined(MBEDTLS_MD_C)
1624 if( PSA_ALG_IS_HMAC( operation->alg ) )
1625 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001626 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001627 unsigned char *opad = operation->ctx.hmac.opad;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001628 size_t hash_size = 0;
Nir Sonnenschein96272412018-06-17 14:41:10 +03001629 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001630 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001631
Gilles Peskine99bc6492018-06-11 17:13:00 +02001632 if( block_size == 0 )
1633 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001634
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001635 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine99bc6492018-06-11 17:13:00 +02001636 sizeof( tmp ), &hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001637 if( status != PSA_SUCCESS )
1638 goto cleanup;
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001639 /* From here on, tmp needs to be wiped. */
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001640
1641 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1642 PSA_ALG_HMAC_HASH( operation->alg ) );
1643 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001644 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001645
1646 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, opad,
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001647 block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001648 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001649 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001650
1651 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine2d277862018-06-18 15:41:12 +02001652 hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001653 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001654 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001655
1656 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, mac,
1657 mac_size, mac_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001658 hmac_cleanup:
1659 mbedtls_zeroize( tmp, hash_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001660 }
1661 else
1662#endif /* MBEDTLS_MD_C */
1663 {
1664 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1665 }
1666 break;
1667 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001668cleanup:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001669
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001670 if( ret == 0 && status == PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001671 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001672 *mac_length = operation->mac_size;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001673 return( psa_mac_abort( operation ) );
1674 }
1675 else
1676 {
1677 psa_mac_abort( operation );
Gilles Peskine99bc6492018-06-11 17:13:00 +02001678 if( ret != 0 )
Gilles Peskine2d277862018-06-18 15:41:12 +02001679 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001680
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001681 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001682 }
1683}
1684
mohammad16036df908f2018-04-02 08:34:15 -07001685psa_status_t psa_mac_finish( psa_mac_operation_t *operation,
1686 uint8_t *mac,
1687 size_t mac_size,
1688 size_t *mac_length )
1689{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001690 if( ! operation->key_usage_sign )
mohammad16036df908f2018-04-02 08:34:15 -07001691 return( PSA_ERROR_NOT_PERMITTED );
1692
Gilles Peskine99bc6492018-06-11 17:13:00 +02001693 return( psa_mac_finish_internal( operation, mac,
1694 mac_size, mac_length ) );
mohammad16036df908f2018-04-02 08:34:15 -07001695}
1696
Gilles Peskine8c9def32018-02-08 10:02:12 +01001697psa_status_t psa_mac_verify( psa_mac_operation_t *operation,
1698 const uint8_t *mac,
1699 size_t mac_length )
1700{
Gilles Peskine828ed142018-06-18 23:25:51 +02001701 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Gilles Peskine8c9def32018-02-08 10:02:12 +01001702 size_t actual_mac_length;
mohammad16036df908f2018-04-02 08:34:15 -07001703 psa_status_t status;
1704
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001705 if( ! operation->key_usage_verify )
mohammad16036df908f2018-04-02 08:34:15 -07001706 return( PSA_ERROR_NOT_PERMITTED );
1707
1708 status = psa_mac_finish_internal( operation,
1709 actual_mac, sizeof( actual_mac ),
1710 &actual_mac_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001711 if( status != PSA_SUCCESS )
1712 return( status );
1713 if( actual_mac_length != mac_length )
1714 return( PSA_ERROR_INVALID_SIGNATURE );
1715 if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
1716 return( PSA_ERROR_INVALID_SIGNATURE );
1717 return( PSA_SUCCESS );
1718}
1719
1720
Gilles Peskine20035e32018-02-03 22:44:14 +01001721
Gilles Peskine20035e32018-02-03 22:44:14 +01001722/****************************************************************/
1723/* Asymmetric cryptography */
1724/****************************************************************/
1725
Gilles Peskine2b450e32018-06-27 15:42:46 +02001726#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001727/* Decode the hash algorithm from alg and store the mbedtls encoding in
1728 * md_alg. Verify that the hash length is consistent. */
1729static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1730 size_t hash_length,
1731 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001732{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001733 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001734 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1735 *md_alg = hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1736 if( *md_alg == MBEDTLS_MD_NONE )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001737 {
1738#if SIZE_MAX > UINT_MAX
Gilles Peskine61b91d42018-06-08 16:09:36 +02001739 if( hash_length > UINT_MAX )
1740 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001741#endif
1742 }
1743 else
1744 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001745 if( mbedtls_md_get_size( md_info ) != hash_length )
1746 return( PSA_ERROR_INVALID_ARGUMENT );
1747 if( md_info == NULL )
1748 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001749 }
Gilles Peskine61b91d42018-06-08 16:09:36 +02001750 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001751}
1752
Gilles Peskine2b450e32018-06-27 15:42:46 +02001753static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1754 psa_algorithm_t alg,
1755 const uint8_t *hash,
1756 size_t hash_length,
1757 uint8_t *signature,
1758 size_t signature_size,
1759 size_t *signature_length )
1760{
1761 psa_status_t status;
1762 int ret;
1763 mbedtls_md_type_t md_alg;
1764
1765 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1766 if( status != PSA_SUCCESS )
1767 return( status );
1768
1769 if( signature_size < rsa->len )
1770 return( PSA_ERROR_BUFFER_TOO_SMALL );
1771
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001772 /* The Mbed TLS RSA module uses an unsigned int for hash_length. See if
1773 * hash_length will fit and return an error if it doesn't. */
1774#if defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21)
1775#if SIZE_MAX > UINT_MAX
1776 if( hash_length > UINT_MAX )
1777 return( PSA_ERROR_NOT_SUPPORTED );
1778#endif
1779#endif
1780
Gilles Peskine2b450e32018-06-27 15:42:46 +02001781#if defined(MBEDTLS_PKCS1_V15)
1782 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1783 {
1784 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1785 MBEDTLS_MD_NONE );
1786 ret = mbedtls_rsa_pkcs1_sign( rsa,
1787 mbedtls_ctr_drbg_random,
1788 &global_data.ctr_drbg,
1789 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001790 md_alg,
1791 (unsigned int) hash_length,
1792 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001793 signature );
1794 }
1795 else
1796#endif /* MBEDTLS_PKCS1_V15 */
1797#if defined(MBEDTLS_PKCS1_V21)
1798 if( PSA_ALG_IS_RSA_PSS( alg ) )
1799 {
1800 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1801 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1802 mbedtls_ctr_drbg_random,
1803 &global_data.ctr_drbg,
1804 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001805 md_alg,
1806 (unsigned int) hash_length,
1807 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001808 signature );
1809 }
1810 else
1811#endif /* MBEDTLS_PKCS1_V21 */
1812 {
1813 return( PSA_ERROR_INVALID_ARGUMENT );
1814 }
1815
1816 if( ret == 0 )
1817 *signature_length = rsa->len;
1818 return( mbedtls_to_psa_error( ret ) );
1819}
1820
1821static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1822 psa_algorithm_t alg,
1823 const uint8_t *hash,
1824 size_t hash_length,
1825 const uint8_t *signature,
1826 size_t signature_length )
1827{
1828 psa_status_t status;
1829 int ret;
1830 mbedtls_md_type_t md_alg;
1831
1832 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1833 if( status != PSA_SUCCESS )
1834 return( status );
1835
1836 if( signature_length < rsa->len )
1837 return( PSA_ERROR_BUFFER_TOO_SMALL );
1838
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001839#if defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21)
1840#if SIZE_MAX > UINT_MAX
1841 /* The Mbed TLS RSA module uses an unsigned int for hash_length. See if
1842 * hash_length will fit and return an error if it doesn't. */
1843 if( hash_length > UINT_MAX )
1844 return( PSA_ERROR_NOT_SUPPORTED );
1845#endif
1846#endif
1847
Gilles Peskine2b450e32018-06-27 15:42:46 +02001848#if defined(MBEDTLS_PKCS1_V15)
1849 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1850 {
1851 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1852 MBEDTLS_MD_NONE );
1853 ret = mbedtls_rsa_pkcs1_verify( rsa,
1854 mbedtls_ctr_drbg_random,
1855 &global_data.ctr_drbg,
1856 MBEDTLS_RSA_PUBLIC,
1857 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001858 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001859 hash,
1860 signature );
1861 }
1862 else
1863#endif /* MBEDTLS_PKCS1_V15 */
1864#if defined(MBEDTLS_PKCS1_V21)
1865 if( PSA_ALG_IS_RSA_PSS( alg ) )
1866 {
1867 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1868 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1869 mbedtls_ctr_drbg_random,
1870 &global_data.ctr_drbg,
1871 MBEDTLS_RSA_PUBLIC,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001872 md_alg,
1873 (unsigned int) hash_length,
1874 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001875 signature );
1876 }
1877 else
1878#endif /* MBEDTLS_PKCS1_V21 */
1879 {
1880 return( PSA_ERROR_INVALID_ARGUMENT );
1881 }
1882 return( mbedtls_to_psa_error( ret ) );
1883}
1884#endif /* MBEDTLS_RSA_C */
1885
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001886#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001887/* `ecp` cannot be const because `ecp->grp` needs to be non-const
1888 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
1889 * (even though these functions don't modify it). */
1890static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
1891 psa_algorithm_t alg,
1892 const uint8_t *hash,
1893 size_t hash_length,
1894 uint8_t *signature,
1895 size_t signature_size,
1896 size_t *signature_length )
1897{
1898 int ret;
1899 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001900 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001901 mbedtls_mpi_init( &r );
1902 mbedtls_mpi_init( &s );
1903
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001904 if( signature_size < 2 * curve_bytes )
1905 {
1906 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
1907 goto cleanup;
1908 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001909
1910 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
1911 {
1912 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
1913 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1914 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
1915 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
1916 hash, hash_length,
1917 md_alg ) );
1918 }
1919 else
1920 {
1921 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
1922 hash, hash_length,
1923 mbedtls_ctr_drbg_random,
1924 &global_data.ctr_drbg ) );
1925 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001926
1927 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
1928 signature,
1929 curve_bytes ) );
1930 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
1931 signature + curve_bytes,
1932 curve_bytes ) );
1933
1934cleanup:
1935 mbedtls_mpi_free( &r );
1936 mbedtls_mpi_free( &s );
1937 if( ret == 0 )
1938 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001939 return( mbedtls_to_psa_error( ret ) );
1940}
1941
1942static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
1943 const uint8_t *hash,
1944 size_t hash_length,
1945 const uint8_t *signature,
1946 size_t signature_length )
1947{
1948 int ret;
1949 mbedtls_mpi r, s;
1950 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
1951 mbedtls_mpi_init( &r );
1952 mbedtls_mpi_init( &s );
1953
1954 if( signature_length != 2 * curve_bytes )
1955 return( PSA_ERROR_INVALID_SIGNATURE );
1956
1957 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
1958 signature,
1959 curve_bytes ) );
1960 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
1961 signature + curve_bytes,
1962 curve_bytes ) );
1963
1964 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
1965 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001966
1967cleanup:
1968 mbedtls_mpi_free( &r );
1969 mbedtls_mpi_free( &s );
1970 return( mbedtls_to_psa_error( ret ) );
1971}
1972#endif /* MBEDTLS_ECDSA_C */
1973
Gilles Peskine61b91d42018-06-08 16:09:36 +02001974psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
1975 psa_algorithm_t alg,
1976 const uint8_t *hash,
1977 size_t hash_length,
1978 const uint8_t *salt,
1979 size_t salt_length,
1980 uint8_t *signature,
1981 size_t signature_size,
1982 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01001983{
1984 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001985 psa_status_t status;
1986
1987 *signature_length = signature_size;
1988
Gilles Peskine93aa0332018-02-03 23:58:03 +01001989 (void) salt;
1990 (void) salt_length;
1991
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001992 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
1993 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001994 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01001995 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001996 {
1997 status = PSA_ERROR_INVALID_ARGUMENT;
1998 goto exit;
1999 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002000
Gilles Peskine20035e32018-02-03 22:44:14 +01002001#if defined(MBEDTLS_RSA_C)
2002 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2003 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002004 status = psa_rsa_sign( slot->data.rsa,
2005 alg,
2006 hash, hash_length,
2007 signature, signature_size,
2008 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002009 }
2010 else
2011#endif /* defined(MBEDTLS_RSA_C) */
2012#if defined(MBEDTLS_ECP_C)
2013 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2014 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002015#if defined(MBEDTLS_ECDSA_C)
2016 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002017 status = psa_ecdsa_sign( slot->data.ecp,
2018 alg,
2019 hash, hash_length,
2020 signature, signature_size,
2021 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002022 else
2023#endif /* defined(MBEDTLS_ECDSA_C) */
2024 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002025 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002026 }
itayzafrir5c753392018-05-08 11:18:38 +03002027 }
2028 else
2029#endif /* defined(MBEDTLS_ECP_C) */
2030 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002031 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002032 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002033
2034exit:
2035 /* Fill the unused part of the output buffer (the whole buffer on error,
2036 * the trailing part on success) with something that isn't a valid mac
2037 * (barring an attack on the mac and deliberately-crafted input),
2038 * in case the caller doesn't check the return status properly. */
2039 if( status == PSA_SUCCESS )
2040 memset( signature + *signature_length, '!',
2041 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002042 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002043 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002044 /* If signature_size is 0 then we have nothing to do. We must not call
2045 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002046 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002047}
2048
2049psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2050 psa_algorithm_t alg,
2051 const uint8_t *hash,
2052 size_t hash_length,
2053 const uint8_t *salt,
2054 size_t salt_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002055 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002056 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002057{
2058 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002059 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002060
itayzafrir5c753392018-05-08 11:18:38 +03002061 (void) salt;
2062 (void) salt_length;
2063
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002064 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2065 if( status != PSA_SUCCESS )
2066 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002067
Gilles Peskine61b91d42018-06-08 16:09:36 +02002068#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002069 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
2070 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002071 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002072 return( psa_rsa_verify( slot->data.rsa,
2073 alg,
2074 hash, hash_length,
2075 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002076 }
2077 else
2078#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002079#if defined(MBEDTLS_ECP_C)
2080 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2081 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002082#if defined(MBEDTLS_ECDSA_C)
2083 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002084 return( psa_ecdsa_verify( slot->data.ecp,
2085 hash, hash_length,
2086 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002087 else
2088#endif /* defined(MBEDTLS_ECDSA_C) */
2089 {
2090 return( PSA_ERROR_INVALID_ARGUMENT );
2091 }
itayzafrir5c753392018-05-08 11:18:38 +03002092 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002093 else
2094#endif /* defined(MBEDTLS_ECP_C) */
2095 {
2096 return( PSA_ERROR_NOT_SUPPORTED );
2097 }
2098}
2099
Gilles Peskine61b91d42018-06-08 16:09:36 +02002100psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2101 psa_algorithm_t alg,
2102 const uint8_t *input,
2103 size_t input_length,
2104 const uint8_t *salt,
2105 size_t salt_length,
2106 uint8_t *output,
2107 size_t output_size,
2108 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002109{
2110 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002111 psa_status_t status;
2112
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002113 (void) salt;
2114 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002115 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002116
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002117 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2118 if( status != PSA_SUCCESS )
2119 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002120 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2121 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002122
2123#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002124 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
2125 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002126 {
2127 mbedtls_rsa_context *rsa = slot->data.rsa;
2128 int ret;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002129 if( output_size < rsa->len )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002130 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002131#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002132 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002133 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002134 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2135 mbedtls_ctr_drbg_random,
2136 &global_data.ctr_drbg,
2137 MBEDTLS_RSA_PUBLIC,
2138 input_length,
2139 input,
2140 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002141 }
2142 else
2143#endif /* MBEDTLS_PKCS1_V15 */
2144#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002145 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002146 {
2147 return( PSA_ERROR_NOT_SUPPORTED );
2148 }
2149 else
2150#endif /* MBEDTLS_PKCS1_V21 */
2151 {
2152 return( PSA_ERROR_INVALID_ARGUMENT );
2153 }
2154 if( ret == 0 )
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002155 *output_length = rsa->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002156 return( mbedtls_to_psa_error( ret ) );
2157 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002158 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002159#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002160 {
2161 return( PSA_ERROR_NOT_SUPPORTED );
2162 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002163}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002164
Gilles Peskine61b91d42018-06-08 16:09:36 +02002165psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2166 psa_algorithm_t alg,
2167 const uint8_t *input,
2168 size_t input_length,
2169 const uint8_t *salt,
2170 size_t salt_length,
2171 uint8_t *output,
2172 size_t output_size,
2173 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002174{
2175 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002176 psa_status_t status;
2177
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002178 (void) salt;
2179 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002180 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002181
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002182 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2183 if( status != PSA_SUCCESS )
2184 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002185 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2186 return( PSA_ERROR_INVALID_ARGUMENT );
2187
2188#if defined(MBEDTLS_RSA_C)
2189 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2190 {
2191 mbedtls_rsa_context *rsa = slot->data.rsa;
2192 int ret;
2193
Gilles Peskinec4def2f2018-06-08 17:53:48 +02002194 if( input_length != rsa->len )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002195 return( PSA_ERROR_INVALID_ARGUMENT );
2196
2197#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002198 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002199 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002200 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2201 mbedtls_ctr_drbg_random,
2202 &global_data.ctr_drbg,
2203 MBEDTLS_RSA_PRIVATE,
2204 output_length,
2205 input,
2206 output,
2207 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002208 }
2209 else
2210#endif /* MBEDTLS_PKCS1_V15 */
2211#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002212 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002213 {
2214 return( PSA_ERROR_NOT_SUPPORTED );
2215 }
2216 else
2217#endif /* MBEDTLS_PKCS1_V21 */
2218 {
2219 return( PSA_ERROR_INVALID_ARGUMENT );
2220 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002221
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002222 return( mbedtls_to_psa_error( ret ) );
2223 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002224 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002225#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002226 {
2227 return( PSA_ERROR_NOT_SUPPORTED );
2228 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002229}
Gilles Peskine20035e32018-02-03 22:44:14 +01002230
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002231
2232
mohammad1603503973b2018-03-12 15:59:30 +02002233/****************************************************************/
2234/* Symmetric cryptography */
2235/****************************************************************/
2236
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002237/* Initialize the cipher operation structure. Once this function has been
2238 * called, psa_cipher_abort can run and will do the right thing. */
2239static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2240 psa_algorithm_t alg )
2241{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002242 if( ! PSA_ALG_IS_CIPHER( alg ) )
2243 {
2244 memset( operation, 0, sizeof( *operation ) );
2245 return( PSA_ERROR_INVALID_ARGUMENT );
2246 }
2247
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002248 operation->alg = alg;
2249 operation->key_set = 0;
2250 operation->iv_set = 0;
2251 operation->iv_required = 1;
2252 operation->iv_size = 0;
2253 operation->block_size = 0;
2254 mbedtls_cipher_init( &operation->ctx.cipher );
2255 return( PSA_SUCCESS );
2256}
2257
Gilles Peskinee553c652018-06-04 16:22:46 +02002258static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2259 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002260 psa_algorithm_t alg,
2261 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002262{
2263 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2264 psa_status_t status;
2265 key_slot_t *slot;
2266 psa_key_type_t key_type;
2267 size_t key_bits;
2268 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002269 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2270 PSA_KEY_USAGE_ENCRYPT :
2271 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002272
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002273 status = psa_cipher_init( operation, alg );
2274 if( status != PSA_SUCCESS )
2275 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002276
2277 status = psa_get_key_information( key, &key_type, &key_bits );
2278 if( status != PSA_SUCCESS )
2279 return( status );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002280 status = psa_get_key_from_slot( key, &slot, usage, alg);
2281 if( status != PSA_SUCCESS )
2282 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002283
Gilles Peskinebb1072f2018-06-08 18:46:05 +02002284 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002285 if( cipher_info == NULL )
2286 return( PSA_ERROR_NOT_SUPPORTED );
2287
mohammad1603503973b2018-03-12 15:59:30 +02002288 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002289 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002290 {
2291 psa_cipher_abort( operation );
2292 return( mbedtls_to_psa_error( ret ) );
2293 }
2294
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002295#if defined(MBEDTLS_DES_C)
2296 if( key_type == PSA_KEY_TYPE_DES && key_bits == 128 )
2297 {
2298 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2299 unsigned char keys[24];
2300 memcpy( keys, slot->data.raw.data, 16 );
2301 memcpy( keys + 16, slot->data.raw.data, 8 );
2302 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2303 keys,
2304 192, cipher_operation );
2305 }
2306 else
2307#endif
2308 {
2309 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2310 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002311 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002312 }
Moran Peker41deec42018-04-04 15:43:05 +03002313 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002314 {
2315 psa_cipher_abort( operation );
2316 return( mbedtls_to_psa_error( ret ) );
2317 }
2318
mohammad16038481e742018-03-18 13:57:31 +02002319#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002320 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002321 {
Gilles Peskine53514202018-06-06 15:11:46 +02002322 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2323 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002324
Moran Pekera28258c2018-05-29 16:25:04 +03002325 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002326 {
2327 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2328 mode = MBEDTLS_PADDING_PKCS7;
2329 break;
2330 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2331 mode = MBEDTLS_PADDING_NONE;
2332 break;
2333 default:
Moran Pekerae382792018-05-31 14:06:17 +03002334 psa_cipher_abort( operation );
2335 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002336 }
2337 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002338 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002339 {
2340 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002341 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002342 }
mohammad16038481e742018-03-18 13:57:31 +02002343 }
2344#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2345
mohammad1603503973b2018-03-12 15:59:30 +02002346 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002347 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
2348 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) :
2349 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002350 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002351 {
mohammad160389e0f462018-04-12 08:48:45 +03002352 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
mohammad16038481e742018-03-18 13:57:31 +02002353 }
mohammad1603503973b2018-03-12 15:59:30 +02002354
Moran Peker395db872018-05-31 14:07:14 +03002355 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002356}
2357
Gilles Peskinee553c652018-06-04 16:22:46 +02002358psa_status_t psa_encrypt_setup( psa_cipher_operation_t *operation,
2359 psa_key_slot_t key,
2360 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002361{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002362 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002363}
2364
Gilles Peskinee553c652018-06-04 16:22:46 +02002365psa_status_t psa_decrypt_setup( psa_cipher_operation_t *operation,
2366 psa_key_slot_t key,
2367 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002368{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002369 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002370}
2371
Gilles Peskinee553c652018-06-04 16:22:46 +02002372psa_status_t psa_encrypt_generate_iv( psa_cipher_operation_t *operation,
2373 unsigned char *iv,
2374 size_t iv_size,
2375 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002376{
Moran Peker41deec42018-04-04 15:43:05 +03002377 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002378 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002379 return( PSA_ERROR_BAD_STATE );
2380 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002381 {
Moran Peker41deec42018-04-04 15:43:05 +03002382 ret = PSA_ERROR_BUFFER_TOO_SMALL;
2383 goto exit;
2384 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002385 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2386 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002387 if( ret != 0 )
2388 {
2389 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002390 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002391 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002392
mohammad16038481e742018-03-18 13:57:31 +02002393 *iv_length = operation->iv_size;
mohammad160389e0f462018-04-12 08:48:45 +03002394 ret = psa_encrypt_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002395
Moran Peker395db872018-05-31 14:07:14 +03002396exit:
2397 if( ret != PSA_SUCCESS )
2398 psa_cipher_abort( operation );
2399 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02002400}
2401
Gilles Peskinee553c652018-06-04 16:22:46 +02002402psa_status_t psa_encrypt_set_iv( psa_cipher_operation_t *operation,
2403 const unsigned char *iv,
2404 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002405{
Moran Peker41deec42018-04-04 15:43:05 +03002406 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002407 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002408 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03002409 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002410 {
Moran Pekerae382792018-05-31 14:06:17 +03002411 psa_cipher_abort( operation );
2412 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03002413 }
mohammad1603503973b2018-03-12 15:59:30 +02002414 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002415 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002416 {
2417 psa_cipher_abort( operation );
2418 return( mbedtls_to_psa_error( ret ) );
2419 }
2420
2421 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02002422
Moran Peker395db872018-05-31 14:07:14 +03002423 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002424}
2425
Gilles Peskinee553c652018-06-04 16:22:46 +02002426psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2427 const uint8_t *input,
2428 size_t input_length,
2429 unsigned char *output,
2430 size_t output_size,
2431 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002432{
2433 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002434 size_t expected_output_size;
2435 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2436 {
2437 /* Take the unprocessed partial block left over from previous
2438 * update calls, if any, plus the input to this call. Remove
2439 * the last partial block, if any. You get the data that will be
2440 * output in this call. */
2441 expected_output_size =
2442 ( operation->ctx.cipher.unprocessed_len + input_length )
2443 / operation->block_size * operation->block_size;
2444 }
2445 else
2446 {
2447 expected_output_size = input_length;
2448 }
2449 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03002450 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02002451
mohammad1603503973b2018-03-12 15:59:30 +02002452 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002453 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002454 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002455 {
2456 psa_cipher_abort( operation );
2457 return( mbedtls_to_psa_error( ret ) );
2458 }
2459
Moran Peker395db872018-05-31 14:07:14 +03002460 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002461}
2462
Gilles Peskinee553c652018-06-04 16:22:46 +02002463psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2464 uint8_t *output,
2465 size_t output_size,
2466 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002467{
2468 int 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 {
Moran Peker7cb22b82018-06-05 11:40:02 +03002473 psa_cipher_abort( operation );
2474 return( PSA_ERROR_BAD_STATE );
2475 }
2476 if( operation->iv_required && ! operation->iv_set )
2477 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002478 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002479 return( PSA_ERROR_BAD_STATE );
2480 }
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 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002488 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002489 return( PSA_ERROR_TAMPERING_DETECTED );
2490 }
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 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002495 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002496 return( PSA_ERROR_INVALID_ARGUMENT );
2497 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002498 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002499 }
2500
2501 ret = mbedtls_cipher_finish( &operation->ctx.cipher, temp_output_buffer,
Gilles Peskinee553c652018-06-04 16:22:46 +02002502 output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002503 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002504 {
2505 psa_cipher_abort( operation );
2506 return( mbedtls_to_psa_error( ret ) );
2507 }
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002508 if( *output_length == 0 )
2509 /* Nothing to copy. Note that output may be NULL in this case. */ ;
2510 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002511 memcpy( output, temp_output_buffer, *output_length );
2512 else
2513 {
2514 psa_cipher_abort( operation );
2515 return( PSA_ERROR_BUFFER_TOO_SMALL );
2516 }
mohammad1603503973b2018-03-12 15:59:30 +02002517
Moran Peker4c80d832018-04-22 20:15:31 +03002518 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002519}
2520
Gilles Peskinee553c652018-06-04 16:22:46 +02002521psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2522{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002523 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002524 {
2525 /* The object has (apparently) been initialized but it is not
2526 * in use. It's ok to call abort on such an object, and there's
2527 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002528 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002529 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002530
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002531 /* Sanity check (shouldn't happen: operation->alg should
2532 * always have been initialized to a valid value). */
2533 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2534 return( PSA_ERROR_BAD_STATE );
2535
mohammad1603503973b2018-03-12 15:59:30 +02002536 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002537
Moran Peker41deec42018-04-04 15:43:05 +03002538 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002539 operation->key_set = 0;
2540 operation->iv_set = 0;
2541 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002542 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002543 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002544
Moran Peker395db872018-05-31 14:07:14 +03002545 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002546}
2547
Gilles Peskinea0655c32018-04-30 17:06:50 +02002548
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002549
mohammad16038cc1cee2018-03-28 01:21:33 +03002550/****************************************************************/
2551/* Key Policy */
2552/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002553
mohammad160327010052018-07-03 13:16:15 +03002554#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002555void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002556{
Gilles Peskine803ce742018-06-18 16:07:14 +02002557 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002558}
2559
Gilles Peskine2d277862018-06-18 15:41:12 +02002560void psa_key_policy_set_usage( psa_key_policy_t *policy,
2561 psa_key_usage_t usage,
2562 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002563{
mohammad16034eed7572018-03-28 05:14:59 -07002564 policy->usage = usage;
2565 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002566}
2567
Gilles Peskine2d277862018-06-18 15:41:12 +02002568psa_key_usage_t psa_key_policy_get_usage( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002569{
mohammad16036df908f2018-04-02 08:34:15 -07002570 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002571}
2572
Gilles Peskine2d277862018-06-18 15:41:12 +02002573psa_algorithm_t psa_key_policy_get_algorithm( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002574{
mohammad16036df908f2018-04-02 08:34:15 -07002575 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002576}
mohammad160327010052018-07-03 13:16:15 +03002577#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002578
Gilles Peskine2d277862018-06-18 15:41:12 +02002579psa_status_t psa_set_key_policy( psa_key_slot_t key,
2580 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002581{
2582 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002583 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002584
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002585 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002586 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002587
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002588 status = psa_get_empty_key_slot( key, &slot );
2589 if( status != PSA_SUCCESS )
2590 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002591
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002592 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2593 PSA_KEY_USAGE_ENCRYPT |
2594 PSA_KEY_USAGE_DECRYPT |
2595 PSA_KEY_USAGE_SIGN |
2596 PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002597 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002598
mohammad16036df908f2018-04-02 08:34:15 -07002599 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002600
2601 return( PSA_SUCCESS );
2602}
2603
Gilles Peskine2d277862018-06-18 15:41:12 +02002604psa_status_t psa_get_key_policy( psa_key_slot_t key,
2605 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002606{
2607 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002608 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002609
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002610 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002611 return( PSA_ERROR_INVALID_ARGUMENT );
2612
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002613 status = psa_get_key_slot( key, &slot );
2614 if( status != PSA_SUCCESS )
2615 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002616
mohammad16036df908f2018-04-02 08:34:15 -07002617 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002618
2619 return( PSA_SUCCESS );
2620}
Gilles Peskine20035e32018-02-03 22:44:14 +01002621
Gilles Peskinea0655c32018-04-30 17:06:50 +02002622
2623
mohammad1603804cd712018-03-20 22:44:08 +02002624/****************************************************************/
2625/* Key Lifetime */
2626/****************************************************************/
2627
Gilles Peskine2d277862018-06-18 15:41:12 +02002628psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2629 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002630{
2631 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002632 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002633
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002634 status = psa_get_key_slot( key, &slot );
2635 if( status != PSA_SUCCESS )
2636 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002637
mohammad1603804cd712018-03-20 22:44:08 +02002638 *lifetime = slot->lifetime;
2639
2640 return( PSA_SUCCESS );
2641}
2642
Gilles Peskine2d277862018-06-18 15:41:12 +02002643psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002644 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002645{
2646 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002647 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002648
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002649 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2650 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002651 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2652 return( PSA_ERROR_INVALID_ARGUMENT );
2653
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002654 status = psa_get_empty_key_slot( key, &slot );
2655 if( status != PSA_SUCCESS )
2656 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002657
Moran Pekerd7326592018-05-29 16:56:39 +03002658 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002659 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002660
mohammad1603060ad8a2018-03-20 14:28:38 -07002661 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002662
2663 return( PSA_SUCCESS );
2664}
2665
Gilles Peskine20035e32018-02-03 22:44:14 +01002666
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002667
mohammad16035955c982018-04-26 00:53:03 +03002668/****************************************************************/
2669/* AEAD */
2670/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002671
mohammad16035955c982018-04-26 00:53:03 +03002672psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2673 psa_algorithm_t alg,
2674 const uint8_t *nonce,
2675 size_t nonce_length,
2676 const uint8_t *additional_data,
2677 size_t additional_data_length,
2678 const uint8_t *plaintext,
2679 size_t plaintext_length,
2680 uint8_t *ciphertext,
2681 size_t ciphertext_size,
2682 size_t *ciphertext_length )
2683{
2684 int ret;
2685 psa_status_t status;
2686 key_slot_t *slot;
2687 psa_key_type_t key_type;
2688 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002689 uint8_t *tag;
2690 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002691 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002692 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002693
mohammad1603f08a5502018-06-03 15:05:47 +03002694 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002695
mohammad16035955c982018-04-26 00:53:03 +03002696 status = psa_get_key_information( key, &key_type, &key_bits );
2697 if( status != PSA_SUCCESS )
2698 return( status );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002699 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2700 if( status != PSA_SUCCESS )
2701 return( status );
mohammad16035955c982018-04-26 00:53:03 +03002702
mohammad16035ed06212018-06-06 13:09:34 +03002703 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2704 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002705 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002706 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002707
Gilles Peskine2d277862018-06-18 15:41:12 +02002708 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2709 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002710 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002711
mohammad16035955c982018-04-26 00:53:03 +03002712 if( alg == PSA_ALG_GCM )
2713 {
2714 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002715 tag_length = 16;
2716
mohammad160396910d82018-06-04 14:33:00 +03002717 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2718 return( PSA_ERROR_INVALID_ARGUMENT );
2719
mohammad160315223a82018-06-03 17:19:55 +03002720 //make sure we have place to hold the tag in the ciphertext buffer
2721 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002722 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002723
2724 //update the tag pointer to point to the end of the ciphertext_length
2725 tag = ciphertext + plaintext_length;
2726
mohammad16035955c982018-04-26 00:53:03 +03002727 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002728 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002729 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002730 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002731 if( ret != 0 )
2732 {
2733 mbedtls_gcm_free( &gcm );
2734 return( mbedtls_to_psa_error( ret ) );
2735 }
2736 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002737 plaintext_length, nonce,
2738 nonce_length, additional_data,
2739 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002740 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002741 mbedtls_gcm_free( &gcm );
2742 }
2743 else if( alg == PSA_ALG_CCM )
2744 {
2745 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002746 tag_length = 16;
2747
mohammad160396910d82018-06-04 14:33:00 +03002748 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2749 return( PSA_ERROR_INVALID_ARGUMENT );
2750
mohammad160347ddf3d2018-04-26 01:11:21 +03002751 if( nonce_length < 7 || nonce_length > 13 )
2752 return( PSA_ERROR_INVALID_ARGUMENT );
2753
mohammad160315223a82018-06-03 17:19:55 +03002754 //make sure we have place to hold the tag in the ciphertext buffer
2755 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002756 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002757
2758 //update the tag pointer to point to the end of the ciphertext_length
2759 tag = ciphertext + plaintext_length;
2760
mohammad16035955c982018-04-26 00:53:03 +03002761 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002762 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002763 slot->data.raw.data,
2764 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002765 if( ret != 0 )
2766 {
2767 mbedtls_ccm_free( &ccm );
2768 return( mbedtls_to_psa_error( ret ) );
2769 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002770 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002771 nonce, nonce_length,
2772 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002773 additional_data_length,
2774 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002775 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002776 mbedtls_ccm_free( &ccm );
2777 }
mohammad16035c8845f2018-05-09 05:40:09 -07002778 else
2779 {
mohammad1603554faad2018-06-03 15:07:38 +03002780 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002781 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002782
mohammad160315223a82018-06-03 17:19:55 +03002783 if( ret != 0 )
2784 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002785 /* If ciphertext_size is 0 then ciphertext may be NULL and then the
2786 * call to memset would have undefined behavior. */
2787 if( ciphertext_size != 0 )
2788 memset( ciphertext, 0, ciphertext_size );
mohammad160315223a82018-06-03 17:19:55 +03002789 return( mbedtls_to_psa_error( ret ) );
2790 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002791
mohammad160315223a82018-06-03 17:19:55 +03002792 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002793 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002794}
2795
Gilles Peskineee652a32018-06-01 19:23:52 +02002796/* Locate the tag in a ciphertext buffer containing the encrypted data
2797 * followed by the tag. Return the length of the part preceding the tag in
2798 * *plaintext_length. This is the size of the plaintext in modes where
2799 * the encrypted data has the same size as the plaintext, such as
2800 * CCM and GCM. */
2801static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2802 const uint8_t *ciphertext,
2803 size_t ciphertext_length,
2804 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002805 const uint8_t **p_tag )
2806{
2807 size_t payload_length;
2808 if( tag_length > ciphertext_length )
2809 return( PSA_ERROR_INVALID_ARGUMENT );
2810 payload_length = ciphertext_length - tag_length;
2811 if( payload_length > plaintext_size )
2812 return( PSA_ERROR_BUFFER_TOO_SMALL );
2813 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002814 return( PSA_SUCCESS );
2815}
2816
mohammad16035955c982018-04-26 00:53:03 +03002817psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2818 psa_algorithm_t alg,
2819 const uint8_t *nonce,
2820 size_t nonce_length,
2821 const uint8_t *additional_data,
2822 size_t additional_data_length,
2823 const uint8_t *ciphertext,
2824 size_t ciphertext_length,
2825 uint8_t *plaintext,
2826 size_t plaintext_size,
2827 size_t *plaintext_length )
2828{
2829 int ret;
2830 psa_status_t status;
2831 key_slot_t *slot;
2832 psa_key_type_t key_type;
2833 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002834 const uint8_t *tag;
2835 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002836 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002837 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002838
Gilles Peskineee652a32018-06-01 19:23:52 +02002839 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002840
mohammad16035955c982018-04-26 00:53:03 +03002841 status = psa_get_key_information( key, &key_type, &key_bits );
2842 if( status != PSA_SUCCESS )
2843 return( status );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002844 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2845 if( status != PSA_SUCCESS )
2846 return( status );
mohammad16035955c982018-04-26 00:53:03 +03002847
mohammad16035ed06212018-06-06 13:09:34 +03002848 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2849 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002850 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002851 return( PSA_ERROR_NOT_SUPPORTED );
2852
Gilles Peskine2d277862018-06-18 15:41:12 +02002853 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2854 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002855 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002856
mohammad16035955c982018-04-26 00:53:03 +03002857 if( alg == PSA_ALG_GCM )
2858 {
2859 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002860
Gilles Peskineee652a32018-06-01 19:23:52 +02002861 tag_length = 16;
2862 status = psa_aead_unpadded_locate_tag( tag_length,
2863 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002864 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002865 if( status != PSA_SUCCESS )
2866 return( status );
2867
mohammad16035955c982018-04-26 00:53:03 +03002868 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002869 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002870 slot->data.raw.data,
2871 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002872 if( ret != 0 )
2873 {
2874 mbedtls_gcm_free( &gcm );
2875 return( mbedtls_to_psa_error( ret ) );
2876 }
mohammad16035955c982018-04-26 00:53:03 +03002877
Gilles Peskineee652a32018-06-01 19:23:52 +02002878 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002879 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002880 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002881 additional_data,
2882 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002883 tag, tag_length,
2884 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002885 mbedtls_gcm_free( &gcm );
2886 }
2887 else if( alg == PSA_ALG_CCM )
2888 {
2889 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002890
mohammad160347ddf3d2018-04-26 01:11:21 +03002891 if( nonce_length < 7 || nonce_length > 13 )
2892 return( PSA_ERROR_INVALID_ARGUMENT );
2893
mohammad16039375f842018-06-03 14:28:24 +03002894 tag_length = 16;
2895 status = psa_aead_unpadded_locate_tag( tag_length,
2896 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002897 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002898 if( status != PSA_SUCCESS )
2899 return( status );
2900
mohammad16035955c982018-04-26 00:53:03 +03002901 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002902 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002903 slot->data.raw.data,
2904 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002905 if( ret != 0 )
2906 {
2907 mbedtls_ccm_free( &ccm );
2908 return( mbedtls_to_psa_error( ret ) );
2909 }
mohammad160360a64d02018-06-03 17:20:42 +03002910 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002911 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002912 additional_data,
2913 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002914 ciphertext, plaintext,
2915 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002916 mbedtls_ccm_free( &ccm );
2917 }
mohammad160339574652018-06-01 04:39:53 -07002918 else
2919 {
mohammad1603554faad2018-06-03 15:07:38 +03002920 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07002921 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002922
Gilles Peskineee652a32018-06-01 19:23:52 +02002923 if( ret != 0 )
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002924 {
2925 /* If plaintext_size is 0 then plaintext may be NULL and then the
2926 * call to memset has undefined behavior. */
2927 if( plaintext_size != 0 )
2928 memset( plaintext, 0, plaintext_size );
2929 }
mohammad160360a64d02018-06-03 17:20:42 +03002930 else
2931 *plaintext_length = ciphertext_length - tag_length;
2932
Gilles Peskineee652a32018-06-01 19:23:52 +02002933 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03002934}
2935
Gilles Peskinea0655c32018-04-30 17:06:50 +02002936
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002937
Gilles Peskine20035e32018-02-03 22:44:14 +01002938/****************************************************************/
Gilles Peskine05d69892018-06-19 22:00:52 +02002939/* Key generation */
2940/****************************************************************/
2941
2942psa_status_t psa_generate_random( uint8_t *output,
2943 size_t output_size )
2944{
2945 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2946 output, output_size );
2947 return( mbedtls_to_psa_error( ret ) );
2948}
2949
2950psa_status_t psa_generate_key( psa_key_slot_t key,
2951 psa_key_type_t type,
2952 size_t bits,
2953 const void *parameters,
2954 size_t parameters_size )
2955{
Gilles Peskine12313cd2018-06-20 00:20:32 +02002956 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002957 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02002958
Gilles Peskine12313cd2018-06-20 00:20:32 +02002959 if( parameters == NULL && parameters_size != 0 )
2960 return( PSA_ERROR_INVALID_ARGUMENT );
2961
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002962 status = psa_get_empty_key_slot( key, &slot );
2963 if( status != PSA_SUCCESS )
2964 return( status );
2965
Gilles Peskine48c0ea12018-06-21 14:15:31 +02002966 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002967 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002968 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02002969 if( status != PSA_SUCCESS )
2970 return( status );
2971 status = psa_generate_random( slot->data.raw.data,
2972 slot->data.raw.bytes );
2973 if( status != PSA_SUCCESS )
2974 {
2975 mbedtls_free( slot->data.raw.data );
2976 return( status );
2977 }
2978#if defined(MBEDTLS_DES_C)
2979 if( type == PSA_KEY_TYPE_DES )
2980 {
2981 mbedtls_des_key_set_parity( slot->data.raw.data );
2982 if( slot->data.raw.bytes >= 16 )
2983 mbedtls_des_key_set_parity( slot->data.raw.data + 8 );
2984 if( slot->data.raw.bytes == 24 )
2985 mbedtls_des_key_set_parity( slot->data.raw.data + 16 );
2986 }
2987#endif /* MBEDTLS_DES_C */
2988 }
2989 else
2990
2991#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
2992 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2993 {
2994 mbedtls_rsa_context *rsa;
2995 int ret;
2996 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02002997 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
2998 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine12313cd2018-06-20 00:20:32 +02002999 if( parameters != NULL )
3000 {
3001 const unsigned *p = parameters;
3002 if( parameters_size != sizeof( *p ) )
3003 return( PSA_ERROR_INVALID_ARGUMENT );
3004 if( *p > INT_MAX )
3005 return( PSA_ERROR_INVALID_ARGUMENT );
3006 exponent = *p;
3007 }
3008 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3009 if( rsa == NULL )
3010 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3011 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3012 ret = mbedtls_rsa_gen_key( rsa,
3013 mbedtls_ctr_drbg_random,
3014 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003015 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003016 exponent );
3017 if( ret != 0 )
3018 {
3019 mbedtls_rsa_free( rsa );
3020 mbedtls_free( rsa );
3021 return( mbedtls_to_psa_error( ret ) );
3022 }
3023 slot->data.rsa = rsa;
3024 }
3025 else
3026#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3027
3028#if defined(MBEDTLS_ECP_C)
3029 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3030 {
3031 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3032 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3033 const mbedtls_ecp_curve_info *curve_info =
3034 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3035 mbedtls_ecp_keypair *ecp;
3036 int ret;
3037 if( parameters != NULL )
3038 return( PSA_ERROR_NOT_SUPPORTED );
3039 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3040 return( PSA_ERROR_NOT_SUPPORTED );
3041 if( curve_info->bit_size != bits )
3042 return( PSA_ERROR_INVALID_ARGUMENT );
3043 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3044 if( ecp == NULL )
3045 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3046 mbedtls_ecp_keypair_init( ecp );
3047 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3048 mbedtls_ctr_drbg_random,
3049 &global_data.ctr_drbg );
3050 if( ret != 0 )
3051 {
3052 mbedtls_ecp_keypair_free( ecp );
3053 mbedtls_free( ecp );
3054 return( mbedtls_to_psa_error( ret ) );
3055 }
3056 slot->data.ecp = ecp;
3057 }
3058 else
3059#endif /* MBEDTLS_ECP_C */
3060
3061 return( PSA_ERROR_NOT_SUPPORTED );
3062
3063 slot->type = type;
3064 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003065}
3066
3067
3068/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003069/* Module setup */
3070/****************************************************************/
3071
Gilles Peskinee59236f2018-01-27 23:32:46 +01003072void mbedtls_psa_crypto_free( void )
3073{
Jaeden Amero045bd502018-06-26 14:00:08 +01003074 psa_key_slot_t key;
Gilles Peskine828ed142018-06-18 23:25:51 +02003075 for( key = 1; key < PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003076 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003077 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3078 mbedtls_entropy_free( &global_data.entropy );
3079 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3080}
3081
3082psa_status_t psa_crypto_init( void )
3083{
3084 int ret;
3085 const unsigned char drbg_seed[] = "PSA";
3086
3087 if( global_data.initialized != 0 )
3088 return( PSA_SUCCESS );
3089
3090 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3091 mbedtls_entropy_init( &global_data.entropy );
3092 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3093
3094 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3095 mbedtls_entropy_func,
3096 &global_data.entropy,
3097 drbg_seed, sizeof( drbg_seed ) - 1 );
3098 if( ret != 0 )
3099 goto exit;
3100
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003101 global_data.initialized = 1;
3102
Gilles Peskinee59236f2018-01-27 23:32:46 +01003103exit:
3104 if( ret != 0 )
3105 mbedtls_psa_crypto_free( );
3106 return( mbedtls_to_psa_error( ret ) );
3107}
3108
3109#endif /* MBEDTLS_PSA_CRYPTO_C */