blob: 2a60b6feb96f038d20de5c90720fb7101c854d62 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030029/*
30 * In case MBEDTLS_PSA_CRYPTO_SPM is defined the code is built for SPM (Secure
31 * Partition Manager) integration which separate the code into two parts
32 * NSPE (Non-Secure Process Environment) and SPE (Secure Process Environment).
33 * In this mode an additional header file should be included.
34 */
mohammad160327010052018-07-03 13:16:15 +030035#if defined(MBEDTLS_PSA_CRYPTO_SPM)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030036/*
37 * PSA_CRYPTO_SECURE means that this file is compiled to the SPE side.
38 * some headers will be affected by this flag.
39 */
mohammad160327010052018-07-03 13:16:15 +030040#define PSA_CRYPTO_SECURE 1
41#include "crypto_spe.h"
42#endif
43
Gilles Peskinee59236f2018-01-27 23:32:46 +010044#include "psa/crypto.h"
45
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include <stdlib.h>
47#include <string.h>
48#if defined(MBEDTLS_PLATFORM_C)
49#include "mbedtls/platform.h"
50#else
51#define mbedtls_calloc calloc
52#define mbedtls_free free
53#endif
54
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020056#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020057#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/blowfish.h"
59#include "mbedtls/camellia.h"
60#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010065#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010066#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/error.h"
68#include "mbedtls/gcm.h"
69#include "mbedtls/md2.h"
70#include "mbedtls/md4.h"
71#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010072#include "mbedtls/md.h"
73#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010074#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/pk_internal.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010077#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/sha1.h"
79#include "mbedtls/sha256.h"
80#include "mbedtls/sha512.h"
81#include "mbedtls/xtea.h"
82
Gilles Peskinee59236f2018-01-27 23:32:46 +010083
84
85/* Implementation that should never be optimized out by the compiler */
86static void mbedtls_zeroize( void *v, size_t n )
87{
88 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
89}
90
Gilles Peskine9ef733f2018-02-07 21:05:37 +010091/* constant-time buffer comparison */
92static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
93{
94 size_t i;
95 unsigned char diff = 0;
96
97 for( i = 0; i < n; i++ )
98 diff |= a[i] ^ b[i];
99
100 return( diff );
101}
102
103
104
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100105/****************************************************************/
106/* Global data, support functions and library management */
107/****************************************************************/
108
109/* Number of key slots (plus one because 0 is not used).
110 * The value is a compile-time constant for now, for simplicity. */
Gilles Peskine828ed142018-06-18 23:25:51 +0200111#define PSA_KEY_SLOT_COUNT 32
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100112
Gilles Peskine2d277862018-06-18 15:41:12 +0200113typedef struct
114{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100115 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +0300116 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200117 psa_key_lifetime_t lifetime;
Gilles Peskine2d277862018-06-18 15:41:12 +0200118 union
119 {
120 struct raw_data
121 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100122 uint8_t *data;
123 size_t bytes;
124 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100125#if defined(MBEDTLS_RSA_C)
126 mbedtls_rsa_context *rsa;
127#endif /* MBEDTLS_RSA_C */
128#if defined(MBEDTLS_ECP_C)
129 mbedtls_ecp_keypair *ecp;
130#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100131 } data;
132} key_slot_t;
133
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200134static int key_type_is_raw_bytes( psa_key_type_t type )
135{
136 psa_key_type_t category = type & PSA_KEY_TYPE_CATEGORY_MASK;
137 return( category == PSA_KEY_TYPE_RAW_DATA ||
138 category == PSA_KEY_TYPE_CATEGORY_SYMMETRIC );
139}
140
Gilles Peskine2d277862018-06-18 15:41:12 +0200141typedef struct
142{
Gilles Peskinee59236f2018-01-27 23:32:46 +0100143 int initialized;
144 mbedtls_entropy_context entropy;
145 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200146 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinee59236f2018-01-27 23:32:46 +0100147} psa_global_data_t;
148
149static psa_global_data_t global_data;
150
151static psa_status_t mbedtls_to_psa_error( int ret )
152{
Gilles Peskinea5905292018-02-07 20:59:33 +0100153 /* If there's both a high-level code and low-level code, dispatch on
154 * the high-level code. */
155 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100156 {
157 case 0:
158 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100159
160 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
161 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
162 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
163 return( PSA_ERROR_NOT_SUPPORTED );
164 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
165 return( PSA_ERROR_HARDWARE_FAILURE );
166
167 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
168 return( PSA_ERROR_HARDWARE_FAILURE );
169
Gilles Peskine9a944802018-06-21 09:35:35 +0200170 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
171 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
172 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
173 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
174 case MBEDTLS_ERR_ASN1_INVALID_DATA:
175 return( PSA_ERROR_INVALID_ARGUMENT );
176 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
177 return( PSA_ERROR_INSUFFICIENT_MEMORY );
178 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
179 return( PSA_ERROR_BUFFER_TOO_SMALL );
180
Gilles Peskinea5905292018-02-07 20:59:33 +0100181 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
182 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
183 return( PSA_ERROR_NOT_SUPPORTED );
184 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
185 return( PSA_ERROR_HARDWARE_FAILURE );
186
187 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
188 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
189 return( PSA_ERROR_NOT_SUPPORTED );
190 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
191 return( PSA_ERROR_HARDWARE_FAILURE );
192
193 case MBEDTLS_ERR_CCM_BAD_INPUT:
194 return( PSA_ERROR_INVALID_ARGUMENT );
195 case MBEDTLS_ERR_CCM_AUTH_FAILED:
196 return( PSA_ERROR_INVALID_SIGNATURE );
197 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
198 return( PSA_ERROR_HARDWARE_FAILURE );
199
200 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
201 return( PSA_ERROR_NOT_SUPPORTED );
202 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
203 return( PSA_ERROR_INVALID_ARGUMENT );
204 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
205 return( PSA_ERROR_INSUFFICIENT_MEMORY );
206 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
207 return( PSA_ERROR_INVALID_PADDING );
208 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
209 return( PSA_ERROR_BAD_STATE );
210 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
211 return( PSA_ERROR_INVALID_SIGNATURE );
212 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
213 return( PSA_ERROR_TAMPERING_DETECTED );
214 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
215 return( PSA_ERROR_HARDWARE_FAILURE );
216
217 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
218 return( PSA_ERROR_HARDWARE_FAILURE );
219
220 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
221 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
222 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
223 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
224 return( PSA_ERROR_NOT_SUPPORTED );
225 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
226 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
227
228 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
229 return( PSA_ERROR_NOT_SUPPORTED );
230 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
231 return( PSA_ERROR_HARDWARE_FAILURE );
232
Gilles Peskinee59236f2018-01-27 23:32:46 +0100233 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
234 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
235 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
236 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100237
238 case MBEDTLS_ERR_GCM_AUTH_FAILED:
239 return( PSA_ERROR_INVALID_SIGNATURE );
240 case MBEDTLS_ERR_GCM_BAD_INPUT:
241 return( PSA_ERROR_NOT_SUPPORTED );
242 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
243 return( PSA_ERROR_HARDWARE_FAILURE );
244
245 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
246 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
247 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
248 return( PSA_ERROR_HARDWARE_FAILURE );
249
250 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
251 return( PSA_ERROR_NOT_SUPPORTED );
252 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
253 return( PSA_ERROR_INVALID_ARGUMENT );
254 case MBEDTLS_ERR_MD_ALLOC_FAILED:
255 return( PSA_ERROR_INSUFFICIENT_MEMORY );
256 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
257 return( PSA_ERROR_STORAGE_FAILURE );
258 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
259 return( PSA_ERROR_HARDWARE_FAILURE );
260
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100261 case MBEDTLS_ERR_PK_ALLOC_FAILED:
262 return( PSA_ERROR_INSUFFICIENT_MEMORY );
263 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
264 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100267 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100268 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
269 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
272 return( PSA_ERROR_NOT_SUPPORTED );
273 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
274 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
275 return( PSA_ERROR_NOT_PERMITTED );
276 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_PK_INVALID_ALG:
279 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
280 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
281 return( PSA_ERROR_NOT_SUPPORTED );
282 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
283 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100284 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
285 return( PSA_ERROR_HARDWARE_FAILURE );
286
287 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
288 return( PSA_ERROR_HARDWARE_FAILURE );
289
290 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_RSA_INVALID_PADDING:
293 return( PSA_ERROR_INVALID_PADDING );
294 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
297 return( PSA_ERROR_INVALID_ARGUMENT );
298 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
299 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
300 return( PSA_ERROR_TAMPERING_DETECTED );
301 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
302 return( PSA_ERROR_INVALID_SIGNATURE );
303 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
304 return( PSA_ERROR_BUFFER_TOO_SMALL );
305 case MBEDTLS_ERR_RSA_RNG_FAILED:
306 return( PSA_ERROR_INSUFFICIENT_MEMORY );
307 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
308 return( PSA_ERROR_NOT_SUPPORTED );
309 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
310 return( PSA_ERROR_HARDWARE_FAILURE );
311
312 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
313 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
314 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
315 return( PSA_ERROR_HARDWARE_FAILURE );
316
317 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
318 return( PSA_ERROR_INVALID_ARGUMENT );
319 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
320 return( PSA_ERROR_HARDWARE_FAILURE );
321
itayzafrir5c753392018-05-08 11:18:38 +0300322 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300323 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300324 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300325 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
326 return( PSA_ERROR_BUFFER_TOO_SMALL );
327 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
328 return( PSA_ERROR_NOT_SUPPORTED );
329 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
330 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
331 return( PSA_ERROR_INVALID_SIGNATURE );
332 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
333 return( PSA_ERROR_INSUFFICIENT_MEMORY );
334 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
335 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300336
Gilles Peskinee59236f2018-01-27 23:32:46 +0100337 default:
338 return( PSA_ERROR_UNKNOWN_ERROR );
339 }
340}
341
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200342/* Retrieve a key slot, occupied or not. */
343static psa_status_t psa_get_key_slot( psa_key_slot_t key,
344 key_slot_t **p_slot )
345{
346 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
347 return( PSA_ERROR_INVALID_ARGUMENT );
348
349 *p_slot = &global_data.key_slots[key];
350 return( PSA_SUCCESS );
351}
352
353/* Retrieve an empty key slot (slot with no key data, but possibly
354 * with some metadata such as a policy). */
355static psa_status_t psa_get_empty_key_slot( psa_key_slot_t key,
356 key_slot_t **p_slot )
357{
358 psa_status_t status;
359 key_slot_t *slot = NULL;
360
361 *p_slot = NULL;
362
363 status = psa_get_key_slot( key, &slot );
364 if( status != PSA_SUCCESS )
365 return( status );
366
367 if( slot->type != PSA_KEY_TYPE_NONE )
368 return( PSA_ERROR_OCCUPIED_SLOT );
369
370 *p_slot = slot;
371 return( status );
372}
373
Jaeden Amerob4fa8c92018-07-11 15:57:44 +0100374/** Retrieve a slot which must contain a key. The key must have allow all the
375 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
376 * operations with this algorithm. */
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200377static psa_status_t psa_get_key_from_slot( psa_key_slot_t key,
378 key_slot_t **p_slot,
379 psa_key_usage_t usage,
380 psa_algorithm_t alg )
381{
382 psa_status_t status;
383 key_slot_t *slot = NULL;
384
385 *p_slot = NULL;
386
387 status = psa_get_key_slot( key, &slot );
388 if( status != PSA_SUCCESS )
389 return( status );
390 if( slot->type == PSA_KEY_TYPE_NONE )
391 return( PSA_ERROR_EMPTY_SLOT );
392
393 /* Enforce that usage policy for the key slot contains all the flags
394 * required by the usage parameter. There is one exception: public
395 * keys can always be exported, so we treat public key objects as
396 * if they had the export flag. */
397 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
398 usage &= ~PSA_KEY_USAGE_EXPORT;
399 if( ( slot->policy.usage & usage ) != usage )
400 return( PSA_ERROR_NOT_PERMITTED );
401 if( alg != 0 && ( alg != slot->policy.alg ) )
402 return( PSA_ERROR_NOT_PERMITTED );
403
404 *p_slot = slot;
405 return( PSA_SUCCESS );
406}
407
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200408
409
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100410/****************************************************************/
411/* Key management */
412/****************************************************************/
413
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200414static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
415{
416 switch( grpid )
417 {
418 case MBEDTLS_ECP_DP_SECP192R1:
419 return( PSA_ECC_CURVE_SECP192R1 );
420 case MBEDTLS_ECP_DP_SECP224R1:
421 return( PSA_ECC_CURVE_SECP224R1 );
422 case MBEDTLS_ECP_DP_SECP256R1:
423 return( PSA_ECC_CURVE_SECP256R1 );
424 case MBEDTLS_ECP_DP_SECP384R1:
425 return( PSA_ECC_CURVE_SECP384R1 );
426 case MBEDTLS_ECP_DP_SECP521R1:
427 return( PSA_ECC_CURVE_SECP521R1 );
428 case MBEDTLS_ECP_DP_BP256R1:
429 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
430 case MBEDTLS_ECP_DP_BP384R1:
431 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
432 case MBEDTLS_ECP_DP_BP512R1:
433 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
434 case MBEDTLS_ECP_DP_CURVE25519:
435 return( PSA_ECC_CURVE_CURVE25519 );
436 case MBEDTLS_ECP_DP_SECP192K1:
437 return( PSA_ECC_CURVE_SECP192K1 );
438 case MBEDTLS_ECP_DP_SECP224K1:
439 return( PSA_ECC_CURVE_SECP224K1 );
440 case MBEDTLS_ECP_DP_SECP256K1:
441 return( PSA_ECC_CURVE_SECP256K1 );
442 case MBEDTLS_ECP_DP_CURVE448:
443 return( PSA_ECC_CURVE_CURVE448 );
444 default:
445 return( 0 );
446 }
447}
448
Gilles Peskine12313cd2018-06-20 00:20:32 +0200449static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
450{
451 switch( curve )
452 {
453 case PSA_ECC_CURVE_SECP192R1:
454 return( MBEDTLS_ECP_DP_SECP192R1 );
455 case PSA_ECC_CURVE_SECP224R1:
456 return( MBEDTLS_ECP_DP_SECP224R1 );
457 case PSA_ECC_CURVE_SECP256R1:
458 return( MBEDTLS_ECP_DP_SECP256R1 );
459 case PSA_ECC_CURVE_SECP384R1:
460 return( MBEDTLS_ECP_DP_SECP384R1 );
461 case PSA_ECC_CURVE_SECP521R1:
462 return( MBEDTLS_ECP_DP_SECP521R1 );
463 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
464 return( MBEDTLS_ECP_DP_BP256R1 );
465 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
466 return( MBEDTLS_ECP_DP_BP384R1 );
467 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
468 return( MBEDTLS_ECP_DP_BP512R1 );
469 case PSA_ECC_CURVE_CURVE25519:
470 return( MBEDTLS_ECP_DP_CURVE25519 );
471 case PSA_ECC_CURVE_SECP192K1:
472 return( MBEDTLS_ECP_DP_SECP192K1 );
473 case PSA_ECC_CURVE_SECP224K1:
474 return( MBEDTLS_ECP_DP_SECP224K1 );
475 case PSA_ECC_CURVE_SECP256K1:
476 return( MBEDTLS_ECP_DP_SECP256K1 );
477 case PSA_ECC_CURVE_CURVE448:
478 return( MBEDTLS_ECP_DP_CURVE448 );
479 default:
480 return( MBEDTLS_ECP_DP_NONE );
481 }
482}
483
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200484static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
485 size_t bits,
486 struct raw_data *raw )
487{
488 /* Check that the bit size is acceptable for the key type */
489 switch( type )
490 {
491 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200492 if( bits == 0 )
493 {
494 raw->bytes = 0;
495 raw->data = NULL;
496 return( PSA_SUCCESS );
497 }
498 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200499#if defined(MBEDTLS_MD_C)
500 case PSA_KEY_TYPE_HMAC:
Gilles 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 Peskineaf89fd72018-06-29 19:52:37 +0200544#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
545static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
546 mbedtls_rsa_context **p_rsa )
547{
548 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
549 return( PSA_ERROR_INVALID_ARGUMENT );
550 else
551 {
552 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
553 size_t bits = mbedtls_rsa_get_bitlen( rsa );
554 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
555 return( PSA_ERROR_NOT_SUPPORTED );
556 *p_rsa = rsa;
557 return( PSA_SUCCESS );
558 }
559}
560#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
561
562#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
563static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
564 mbedtls_pk_context *pk,
565 mbedtls_ecp_keypair **p_ecp )
566{
567 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
568 return( PSA_ERROR_INVALID_ARGUMENT );
569 else
570 {
571 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
572 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
573 if( actual_curve != expected_curve )
574 return( PSA_ERROR_INVALID_ARGUMENT );
575 *p_ecp = ecp;
576 return( PSA_SUCCESS );
577 }
578}
579#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
580
Gilles Peskine2d277862018-06-18 15:41:12 +0200581psa_status_t psa_import_key( psa_key_slot_t key,
582 psa_key_type_t type,
583 const uint8_t *data,
584 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100585{
586 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200587 psa_status_t status = PSA_SUCCESS;
588 status = psa_get_empty_key_slot( key, &slot );
589 if( status != PSA_SUCCESS )
590 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100591
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200592 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100593 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100594 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100595 if( data_length > SIZE_MAX / 8 )
596 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200597 status = prepare_raw_data_slot( type,
598 PSA_BYTES_TO_BITS( data_length ),
599 &slot->data.raw );
600 if( status != PSA_SUCCESS )
601 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200602 if( data_length != 0 )
603 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100604 }
605 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100606#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200607 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100608 {
609 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100610 mbedtls_pk_context pk;
611 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200612
613 /* Parse the data. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100614 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
615 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
616 else
617 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100618 if( ret != 0 )
619 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200620
621 /* We have something that the pkparse module recognizes.
622 * If it has the expected type and passes any type-specific
623 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100624#if defined(MBEDTLS_RSA_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200625 if( PSA_KEY_TYPE_IS_RSA( type ) )
626 status = psa_import_rsa_key( &pk, &slot->data.rsa );
627 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100628#endif /* MBEDTLS_RSA_C */
629#if defined(MBEDTLS_ECP_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200630 if( PSA_KEY_TYPE_IS_ECC( type ) )
631 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( type ),
632 &pk, &slot->data.ecp );
633 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100634#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200635 {
636 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200637 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200638
Gilles Peskinec648d692018-06-28 08:46:13 +0200639 /* Free the content of the pk object only on error. On success,
640 * the content of the object has been stored in the slot. */
641 if( status != PSA_SUCCESS )
642 {
643 mbedtls_pk_free( &pk );
644 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100645 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100646 }
647 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100648#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100649 {
650 return( PSA_ERROR_NOT_SUPPORTED );
651 }
652
653 slot->type = type;
654 return( PSA_SUCCESS );
655}
656
Gilles Peskine2d277862018-06-18 15:41:12 +0200657psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100658{
659 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200660 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100661
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200662 status = psa_get_key_slot( key, &slot );
663 if( status != PSA_SUCCESS )
664 return( status );
665
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100666 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200667 {
668 /* No key material to clean, but do zeroize the slot below to wipe
669 * metadata such as policies. */
670 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200671 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100672 {
673 mbedtls_free( slot->data.raw.data );
674 }
675 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100676#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200677 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100679 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100680 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100681 }
682 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100683#endif /* defined(MBEDTLS_RSA_C) */
684#if defined(MBEDTLS_ECP_C)
685 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
686 {
687 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100688 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100689 }
690 else
691#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100692 {
693 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100694 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100695 return( PSA_ERROR_TAMPERING_DETECTED );
696 }
697
698 mbedtls_zeroize( slot, sizeof( *slot ) );
699 return( PSA_SUCCESS );
700}
701
Gilles Peskineb870b182018-07-06 16:02:09 +0200702/* Return the size of the key in the given slot, in bits. */
703static size_t psa_get_key_bits( const key_slot_t *slot )
704{
705 if( key_type_is_raw_bytes( slot->type ) )
706 return( slot->data.raw.bytes * 8 );
707#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200708 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineb870b182018-07-06 16:02:09 +0200709 return( mbedtls_rsa_get_bitlen( slot->data.rsa ) );
710#endif /* defined(MBEDTLS_RSA_C) */
711#if defined(MBEDTLS_ECP_C)
712 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
713 return( slot->data.ecp->grp.pbits );
714#endif /* defined(MBEDTLS_ECP_C) */
715 /* Shouldn't happen except on an empty slot. */
716 return( 0 );
717}
718
Gilles Peskine2d277862018-06-18 15:41:12 +0200719psa_status_t psa_get_key_information( psa_key_slot_t key,
720 psa_key_type_t *type,
721 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100722{
723 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200724 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100725
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100726 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200727 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100728 if( bits != NULL )
729 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200730 status = psa_get_key_slot( key, &slot );
731 if( status != PSA_SUCCESS )
732 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200733
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100734 if( slot->type == PSA_KEY_TYPE_NONE )
735 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200736 if( type != NULL )
737 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200738 if( bits != NULL )
739 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740 return( PSA_SUCCESS );
741}
742
Gilles Peskine2d277862018-06-18 15:41:12 +0200743static psa_status_t psa_internal_export_key( psa_key_slot_t key,
744 uint8_t *data,
745 size_t data_size,
746 size_t *data_length,
747 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100748{
749 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200750 psa_status_t status;
751 /* Exporting a public key doesn't require a usage flag. If we're
752 * called by psa_export_public_key(), don't require the EXPORT flag.
753 * If we're called by psa_export_key(), do require the EXPORT flag;
754 * if the key turns out to be public key object, psa_get_key_from_slot()
755 * will ignore this flag. */
756 psa_key_usage_t usage = export_public_key ? 0 : PSA_KEY_USAGE_EXPORT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100757
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100758 /* Set the key to empty now, so that even when there are errors, we always
759 * set data_length to a value between 0 and data_size. On error, setting
760 * the key to empty is a good choice because an empty key representation is
761 * unlikely to be accepted anywhere. */
762 *data_length = 0;
763
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200764 status = psa_get_key_from_slot( key, &slot, usage, 0 );
765 if( status != PSA_SUCCESS )
766 return( status );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200767 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300768 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300769
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200770 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100771 {
772 if( slot->data.raw.bytes > data_size )
773 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200774 if( slot->data.raw.bytes != 0 )
775 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100776 *data_length = slot->data.raw.bytes;
777 return( PSA_SUCCESS );
778 }
779 else
Moran Peker17e36e12018-05-02 12:55:20 +0300780 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100781#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200782 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300783 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 );
Gilles Peskined8008d62018-06-29 19:51:51 +0200788 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300789 {
790 pk.pk_info = &mbedtls_rsa_info;
791 pk.pk_ctx = slot->data.rsa;
792 }
793 else
794 {
795 pk.pk_info = &mbedtls_eckey_info;
796 pk.pk_ctx = slot->data.ecp;
797 }
Moran Pekerd7326592018-05-29 16:56:39 +0300798 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300799 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300800 else
801 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300802 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200803 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200804 /* If data_size is 0 then data may be NULL and then the
805 * call to memset would have undefined behavior. */
806 if( data_size != 0 )
807 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300808 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200809 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200810 /* The mbedtls_pk_xxx functions write to the end of the buffer.
811 * Move the data to the beginning and erase remaining data
812 * at the original location. */
813 if( 2 * (size_t) ret <= data_size )
814 {
815 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200816 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200817 }
818 else if( (size_t) ret < data_size )
819 {
820 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200821 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200822 }
Moran Pekera998bc62018-04-16 18:16:20 +0300823 *data_length = ret;
824 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100825 }
826 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100827#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300828 {
829 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200830 it is valid for a special-purpose implementation to omit
831 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300832 return( PSA_ERROR_NOT_SUPPORTED );
833 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100834 }
835}
836
Gilles Peskine2d277862018-06-18 15:41:12 +0200837psa_status_t psa_export_key( psa_key_slot_t key,
838 uint8_t *data,
839 size_t data_size,
840 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300841{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200842 return( psa_internal_export_key( key, data, data_size,
843 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100844}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100845
Gilles Peskine2d277862018-06-18 15:41:12 +0200846psa_status_t psa_export_public_key( psa_key_slot_t key,
847 uint8_t *data,
848 size_t data_size,
849 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300850{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200851 return( psa_internal_export_key( key, data, data_size,
852 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300853}
854
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200855
856
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100857/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100858/* Message digests */
859/****************************************************************/
860
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100861static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100862{
863 switch( alg )
864 {
865#if defined(MBEDTLS_MD2_C)
866 case PSA_ALG_MD2:
867 return( &mbedtls_md2_info );
868#endif
869#if defined(MBEDTLS_MD4_C)
870 case PSA_ALG_MD4:
871 return( &mbedtls_md4_info );
872#endif
873#if defined(MBEDTLS_MD5_C)
874 case PSA_ALG_MD5:
875 return( &mbedtls_md5_info );
876#endif
877#if defined(MBEDTLS_RIPEMD160_C)
878 case PSA_ALG_RIPEMD160:
879 return( &mbedtls_ripemd160_info );
880#endif
881#if defined(MBEDTLS_SHA1_C)
882 case PSA_ALG_SHA_1:
883 return( &mbedtls_sha1_info );
884#endif
885#if defined(MBEDTLS_SHA256_C)
886 case PSA_ALG_SHA_224:
887 return( &mbedtls_sha224_info );
888 case PSA_ALG_SHA_256:
889 return( &mbedtls_sha256_info );
890#endif
891#if defined(MBEDTLS_SHA512_C)
892 case PSA_ALG_SHA_384:
893 return( &mbedtls_sha384_info );
894 case PSA_ALG_SHA_512:
895 return( &mbedtls_sha512_info );
896#endif
897 default:
898 return( NULL );
899 }
900}
901
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100902psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
903{
904 switch( operation->alg )
905 {
Gilles Peskine81736312018-06-26 15:04:31 +0200906 case 0:
907 /* The object has (apparently) been initialized but it is not
908 * in use. It's ok to call abort on such an object, and there's
909 * nothing to do. */
910 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100911#if defined(MBEDTLS_MD2_C)
912 case PSA_ALG_MD2:
913 mbedtls_md2_free( &operation->ctx.md2 );
914 break;
915#endif
916#if defined(MBEDTLS_MD4_C)
917 case PSA_ALG_MD4:
918 mbedtls_md4_free( &operation->ctx.md4 );
919 break;
920#endif
921#if defined(MBEDTLS_MD5_C)
922 case PSA_ALG_MD5:
923 mbedtls_md5_free( &operation->ctx.md5 );
924 break;
925#endif
926#if defined(MBEDTLS_RIPEMD160_C)
927 case PSA_ALG_RIPEMD160:
928 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
929 break;
930#endif
931#if defined(MBEDTLS_SHA1_C)
932 case PSA_ALG_SHA_1:
933 mbedtls_sha1_free( &operation->ctx.sha1 );
934 break;
935#endif
936#if defined(MBEDTLS_SHA256_C)
937 case PSA_ALG_SHA_224:
938 case PSA_ALG_SHA_256:
939 mbedtls_sha256_free( &operation->ctx.sha256 );
940 break;
941#endif
942#if defined(MBEDTLS_SHA512_C)
943 case PSA_ALG_SHA_384:
944 case PSA_ALG_SHA_512:
945 mbedtls_sha512_free( &operation->ctx.sha512 );
946 break;
947#endif
948 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +0200949 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100950 }
951 operation->alg = 0;
952 return( PSA_SUCCESS );
953}
954
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200955psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100956 psa_algorithm_t alg )
957{
958 int ret;
959 operation->alg = 0;
960 switch( alg )
961 {
962#if defined(MBEDTLS_MD2_C)
963 case PSA_ALG_MD2:
964 mbedtls_md2_init( &operation->ctx.md2 );
965 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
966 break;
967#endif
968#if defined(MBEDTLS_MD4_C)
969 case PSA_ALG_MD4:
970 mbedtls_md4_init( &operation->ctx.md4 );
971 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
972 break;
973#endif
974#if defined(MBEDTLS_MD5_C)
975 case PSA_ALG_MD5:
976 mbedtls_md5_init( &operation->ctx.md5 );
977 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
978 break;
979#endif
980#if defined(MBEDTLS_RIPEMD160_C)
981 case PSA_ALG_RIPEMD160:
982 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
983 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
984 break;
985#endif
986#if defined(MBEDTLS_SHA1_C)
987 case PSA_ALG_SHA_1:
988 mbedtls_sha1_init( &operation->ctx.sha1 );
989 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
990 break;
991#endif
992#if defined(MBEDTLS_SHA256_C)
993 case PSA_ALG_SHA_224:
994 mbedtls_sha256_init( &operation->ctx.sha256 );
995 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
996 break;
997 case PSA_ALG_SHA_256:
998 mbedtls_sha256_init( &operation->ctx.sha256 );
999 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1000 break;
1001#endif
1002#if defined(MBEDTLS_SHA512_C)
1003 case PSA_ALG_SHA_384:
1004 mbedtls_sha512_init( &operation->ctx.sha512 );
1005 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1006 break;
1007 case PSA_ALG_SHA_512:
1008 mbedtls_sha512_init( &operation->ctx.sha512 );
1009 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1010 break;
1011#endif
1012 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001013 return( PSA_ALG_IS_HASH( alg ) ?
1014 PSA_ERROR_NOT_SUPPORTED :
1015 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001016 }
1017 if( ret == 0 )
1018 operation->alg = alg;
1019 else
1020 psa_hash_abort( operation );
1021 return( mbedtls_to_psa_error( ret ) );
1022}
1023
1024psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1025 const uint8_t *input,
1026 size_t input_length )
1027{
1028 int ret;
1029 switch( operation->alg )
1030 {
1031#if defined(MBEDTLS_MD2_C)
1032 case PSA_ALG_MD2:
1033 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1034 input, input_length );
1035 break;
1036#endif
1037#if defined(MBEDTLS_MD4_C)
1038 case PSA_ALG_MD4:
1039 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1040 input, input_length );
1041 break;
1042#endif
1043#if defined(MBEDTLS_MD5_C)
1044 case PSA_ALG_MD5:
1045 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1046 input, input_length );
1047 break;
1048#endif
1049#if defined(MBEDTLS_RIPEMD160_C)
1050 case PSA_ALG_RIPEMD160:
1051 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1052 input, input_length );
1053 break;
1054#endif
1055#if defined(MBEDTLS_SHA1_C)
1056 case PSA_ALG_SHA_1:
1057 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1058 input, input_length );
1059 break;
1060#endif
1061#if defined(MBEDTLS_SHA256_C)
1062 case PSA_ALG_SHA_224:
1063 case PSA_ALG_SHA_256:
1064 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1065 input, input_length );
1066 break;
1067#endif
1068#if defined(MBEDTLS_SHA512_C)
1069 case PSA_ALG_SHA_384:
1070 case PSA_ALG_SHA_512:
1071 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1072 input, input_length );
1073 break;
1074#endif
1075 default:
1076 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1077 break;
1078 }
1079 if( ret != 0 )
1080 psa_hash_abort( operation );
1081 return( mbedtls_to_psa_error( ret ) );
1082}
1083
1084psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1085 uint8_t *hash,
1086 size_t hash_size,
1087 size_t *hash_length )
1088{
1089 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001090 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001091
1092 /* Fill the output buffer with something that isn't a valid hash
1093 * (barring an attack on the hash and deliberately-crafted input),
1094 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001095 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001096 /* If hash_size is 0 then hash may be NULL and then the
1097 * call to memset would have undefined behavior. */
1098 if( hash_size != 0 )
1099 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001100
1101 if( hash_size < actual_hash_length )
1102 return( PSA_ERROR_BUFFER_TOO_SMALL );
1103
1104 switch( operation->alg )
1105 {
1106#if defined(MBEDTLS_MD2_C)
1107 case PSA_ALG_MD2:
1108 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1109 break;
1110#endif
1111#if defined(MBEDTLS_MD4_C)
1112 case PSA_ALG_MD4:
1113 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1114 break;
1115#endif
1116#if defined(MBEDTLS_MD5_C)
1117 case PSA_ALG_MD5:
1118 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1119 break;
1120#endif
1121#if defined(MBEDTLS_RIPEMD160_C)
1122 case PSA_ALG_RIPEMD160:
1123 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1124 break;
1125#endif
1126#if defined(MBEDTLS_SHA1_C)
1127 case PSA_ALG_SHA_1:
1128 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1129 break;
1130#endif
1131#if defined(MBEDTLS_SHA256_C)
1132 case PSA_ALG_SHA_224:
1133 case PSA_ALG_SHA_256:
1134 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1135 break;
1136#endif
1137#if defined(MBEDTLS_SHA512_C)
1138 case PSA_ALG_SHA_384:
1139 case PSA_ALG_SHA_512:
1140 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1141 break;
1142#endif
1143 default:
1144 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1145 break;
1146 }
1147
1148 if( ret == 0 )
1149 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001150 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001151 return( psa_hash_abort( operation ) );
1152 }
1153 else
1154 {
1155 psa_hash_abort( operation );
1156 return( mbedtls_to_psa_error( ret ) );
1157 }
1158}
1159
Gilles Peskine2d277862018-06-18 15:41:12 +02001160psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1161 const uint8_t *hash,
1162 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001163{
1164 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1165 size_t actual_hash_length;
1166 psa_status_t status = psa_hash_finish( operation,
1167 actual_hash, sizeof( actual_hash ),
1168 &actual_hash_length );
1169 if( status != PSA_SUCCESS )
1170 return( status );
1171 if( actual_hash_length != hash_length )
1172 return( PSA_ERROR_INVALID_SIGNATURE );
1173 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1174 return( PSA_ERROR_INVALID_SIGNATURE );
1175 return( PSA_SUCCESS );
1176}
1177
1178
1179
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001180/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001181/* MAC */
1182/****************************************************************/
1183
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001184static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001185 psa_algorithm_t alg,
1186 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001187 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001188 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001189{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001190 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001191 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001192
1193 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1194 {
1195 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001196 {
1197 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1198 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001199
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001200 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001201 {
Gilles Peskine5d1888e2018-07-12 00:32:42 +02001202 case PSA_ALG_STREAM_CIPHER_BASE:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001203 mode = MBEDTLS_MODE_STREAM;
1204 break;
1205 case PSA_ALG_CBC_BASE:
1206 mode = MBEDTLS_MODE_CBC;
1207 break;
1208 case PSA_ALG_CFB_BASE:
1209 mode = MBEDTLS_MODE_CFB;
1210 break;
1211 case PSA_ALG_OFB_BASE:
1212 mode = MBEDTLS_MODE_OFB;
1213 break;
1214 case PSA_ALG_CTR:
1215 mode = MBEDTLS_MODE_CTR;
1216 break;
1217 case PSA_ALG_CCM:
1218 mode = MBEDTLS_MODE_CCM;
1219 break;
1220 case PSA_ALG_GCM:
1221 mode = MBEDTLS_MODE_GCM;
1222 break;
1223 default:
1224 return( NULL );
1225 }
1226 }
1227 else if( alg == PSA_ALG_CMAC )
1228 mode = MBEDTLS_MODE_ECB;
1229 else if( alg == PSA_ALG_GMAC )
1230 mode = MBEDTLS_MODE_GCM;
1231 else
1232 return( NULL );
1233
1234 switch( key_type )
1235 {
1236 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001237 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001238 break;
1239 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001240 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1241 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001242 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001243 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001244 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001245 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001246 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1247 * but two-key Triple-DES is functionally three-key Triple-DES
1248 * with K1=K3, so that's how we present it to mbedtls. */
1249 if( key_bits == 128 )
1250 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001251 break;
1252 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001253 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001254 break;
1255 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001256 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001257 break;
1258 default:
1259 return( NULL );
1260 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001261 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001262 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001263
Jaeden Amero23bbb752018-06-26 14:16:54 +01001264 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1265 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001266}
1267
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001268static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001269{
Gilles Peskine2d277862018-06-18 15:41:12 +02001270 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001271 {
1272 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001273 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001274 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001275 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001276 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001277 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001278 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001279 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001280 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001281 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001282 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001283 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001284 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001285 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001286 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001287 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001288 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001289 return( 128 );
1290 default:
1291 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001292 }
1293}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001294
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001295/* Initialize the MAC operation structure. Once this function has been
1296 * called, psa_mac_abort can run and will do the right thing. */
1297static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1298 psa_algorithm_t alg )
1299{
1300 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1301
1302 operation->alg = alg;
1303 operation->key_set = 0;
1304 operation->iv_set = 0;
1305 operation->iv_required = 0;
1306 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001307 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001308
1309#if defined(MBEDTLS_CMAC_C)
1310 if( alg == PSA_ALG_CMAC )
1311 {
1312 operation->iv_required = 0;
1313 mbedtls_cipher_init( &operation->ctx.cmac );
1314 status = PSA_SUCCESS;
1315 }
1316 else
1317#endif /* MBEDTLS_CMAC_C */
1318#if defined(MBEDTLS_MD_C)
1319 if( PSA_ALG_IS_HMAC( operation->alg ) )
1320 {
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001321 status = psa_hash_setup( &operation->ctx.hmac.hash_ctx,
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001322 PSA_ALG_HMAC_HASH( alg ) );
1323 }
1324 else
1325#endif /* MBEDTLS_MD_C */
1326 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001327 if( ! PSA_ALG_IS_MAC( alg ) )
1328 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001329 }
1330
1331 if( status != PSA_SUCCESS )
1332 memset( operation, 0, sizeof( *operation ) );
1333 return( status );
1334}
1335
Gilles Peskine8c9def32018-02-08 10:02:12 +01001336psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1337{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001338 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001339 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001340 /* The object has (apparently) been initialized but it is not
1341 * in use. It's ok to call abort on such an object, and there's
1342 * nothing to do. */
1343 return( PSA_SUCCESS );
1344 }
1345 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001346#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001347 if( operation->alg == PSA_ALG_CMAC )
1348 {
1349 mbedtls_cipher_free( &operation->ctx.cmac );
1350 }
1351 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001352#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001353#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001354 if( PSA_ALG_IS_HMAC( operation->alg ) )
1355 {
1356 size_t block_size =
1357 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
1358 if( block_size == 0 )
1359 goto bad_state;
1360 psa_hash_abort( &operation->ctx.hmac.hash_ctx );
1361 mbedtls_zeroize( operation->ctx.hmac.opad, block_size );
1362 }
1363 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001364#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001365 {
1366 /* Sanity check (shouldn't happen: operation->alg should
1367 * always have been initialized to a valid value). */
1368 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001369 }
Moran Peker41deec42018-04-04 15:43:05 +03001370
Gilles Peskine8c9def32018-02-08 10:02:12 +01001371 operation->alg = 0;
1372 operation->key_set = 0;
1373 operation->iv_set = 0;
1374 operation->iv_required = 0;
1375 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001376 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001377
Gilles Peskine8c9def32018-02-08 10:02:12 +01001378 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001379
1380bad_state:
1381 /* If abort is called on an uninitialized object, we can't trust
1382 * anything. Wipe the object in case it contains confidential data.
1383 * This may result in a memory leak if a pointer gets overwritten,
1384 * but it's too late to do anything about this. */
1385 memset( operation, 0, sizeof( *operation ) );
1386 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001387}
1388
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001389#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001390static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001391 size_t key_bits,
1392 key_slot_t *slot,
1393 const mbedtls_cipher_info_t *cipher_info )
1394{
1395 int ret;
1396
1397 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001398
1399 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1400 if( ret != 0 )
1401 return( ret );
1402
1403 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1404 slot->data.raw.data,
1405 key_bits );
1406 return( ret );
1407}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001408#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001409
Gilles Peskine248051a2018-06-20 16:09:38 +02001410#if defined(MBEDTLS_MD_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001411static int psa_hmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001412 psa_key_type_t key_type,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001413 key_slot_t *slot,
1414 psa_algorithm_t alg )
1415{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001416 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001417 unsigned char *opad = operation->ctx.hmac.opad;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001418 size_t i;
Gilles Peskinee1bc6802018-06-11 17:36:05 +02001419 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001420 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001421 unsigned int digest_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001422 PSA_HASH_SIZE( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001423 size_t key_length = slot->data.raw.bytes;
1424 psa_status_t status;
1425
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001426 if( block_size == 0 || digest_size == 0 )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001427 return( PSA_ERROR_NOT_SUPPORTED );
1428
1429 if( key_type != PSA_KEY_TYPE_HMAC )
1430 return( PSA_ERROR_INVALID_ARGUMENT );
1431
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001432 operation->mac_size = digest_size;
1433
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001434 /* The hash was started earlier in psa_mac_init. */
Gilles Peskined223b522018-06-11 18:12:58 +02001435 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001436 {
1437 status = psa_hash_update( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001438 slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001439 if( status != PSA_SUCCESS )
1440 return( status );
1441 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001442 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001443 if( status != PSA_SUCCESS )
1444 return( status );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001445 }
Gilles Peskined223b522018-06-11 18:12:58 +02001446 else
1447 memcpy( ipad, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001448
Gilles Peskined223b522018-06-11 18:12:58 +02001449 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1450 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001451 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001452 ipad[i] ^= 0x36;
1453 memset( ipad + key_length, 0x36, block_size - key_length );
1454
1455 /* Copy the key material from ipad to opad, flipping the requisite bits,
1456 * and filling the rest of opad with the requisite constant. */
1457 for( i = 0; i < key_length; i++ )
1458 opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1459 memset( opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001460
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001461 status = psa_hash_setup( &operation->ctx.hmac.hash_ctx,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001462 PSA_ALG_HMAC_HASH( alg ) );
1463 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001464 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001465
1466 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, ipad,
1467 block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001468
1469cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001470 mbedtls_zeroize( ipad, key_length );
1471 /* opad is in the context. It needs to stay in memory if this function
1472 * succeeds, and it will be wiped by psa_mac_abort() called from
Gilles Peskine89167cb2018-07-08 20:12:23 +02001473 * psa_mac_setup in the error case. */
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001474
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001475 return( status );
1476}
Gilles Peskine248051a2018-06-20 16:09:38 +02001477#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001478
Gilles Peskine89167cb2018-07-08 20:12:23 +02001479static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1480 psa_key_slot_t key,
1481 psa_algorithm_t alg,
1482 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001483{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001484 psa_status_t status;
1485 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001486 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001487 psa_key_usage_t usage =
1488 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001489
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001490 status = psa_mac_init( operation, alg );
1491 if( status != PSA_SUCCESS )
1492 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001493 if( is_sign )
1494 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001495
Gilles Peskine89167cb2018-07-08 20:12:23 +02001496 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001497 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001498 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001499 key_bits = psa_get_key_bits( slot );
1500
Gilles Peskine8c9def32018-02-08 10:02:12 +01001501#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001502 if( alg == PSA_ALG_CMAC )
1503 {
1504 const mbedtls_cipher_info_t *cipher_info =
1505 mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
1506 int ret;
1507 if( cipher_info == NULL )
1508 {
1509 status = PSA_ERROR_NOT_SUPPORTED;
1510 goto exit;
1511 }
1512 operation->mac_size = cipher_info->block_size;
1513 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1514 status = mbedtls_to_psa_error( ret );
1515 }
1516 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001517#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001518#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001519 if( PSA_ALG_IS_HMAC( alg ) )
1520 {
1521 status = psa_hmac_setup( operation, slot->type, slot, alg );
1522 }
1523 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001524#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001525 {
1526 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001527 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001528
Gilles Peskinefbfac682018-07-08 20:51:54 +02001529exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001530 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001531 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001532 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001533 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001534 else
1535 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001536 operation->key_set = 1;
1537 }
1538 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001539}
1540
Gilles Peskine89167cb2018-07-08 20:12:23 +02001541psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1542 psa_key_slot_t key,
1543 psa_algorithm_t alg )
1544{
1545 return( psa_mac_setup( operation, key, alg, 1 ) );
1546}
1547
1548psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1549 psa_key_slot_t key,
1550 psa_algorithm_t alg )
1551{
1552 return( psa_mac_setup( operation, key, alg, 0 ) );
1553}
1554
Gilles Peskine8c9def32018-02-08 10:02:12 +01001555psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1556 const uint8_t *input,
1557 size_t input_length )
1558{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001559 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001560 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001561 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001562 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001563 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001564 operation->has_input = 1;
1565
Gilles Peskine8c9def32018-02-08 10:02:12 +01001566#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001567 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001568 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001569 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1570 input, input_length );
1571 status = mbedtls_to_psa_error( ret );
1572 }
1573 else
1574#endif /* MBEDTLS_CMAC_C */
1575#if defined(MBEDTLS_MD_C)
1576 if( PSA_ALG_IS_HMAC( operation->alg ) )
1577 {
1578 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1579 input_length );
1580 }
1581 else
1582#endif /* MBEDTLS_MD_C */
1583 {
1584 /* This shouldn't happen if `operation` was initialized by
1585 * a setup function. */
1586 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001587 }
1588
Gilles Peskinefbfac682018-07-08 20:51:54 +02001589cleanup:
1590 if( status != PSA_SUCCESS )
1591 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001592 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001593}
1594
mohammad16036df908f2018-04-02 08:34:15 -07001595static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001596 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001597 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001598{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001599 psa_status_t status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001600
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001601 if( ! operation->key_set )
1602 return( PSA_ERROR_BAD_STATE );
1603 if( operation->iv_required && ! operation->iv_set )
1604 return( PSA_ERROR_BAD_STATE );
1605
Gilles Peskine8c9def32018-02-08 10:02:12 +01001606 if( mac_size < operation->mac_size )
1607 return( PSA_ERROR_BUFFER_TOO_SMALL );
1608
Gilles Peskine8c9def32018-02-08 10:02:12 +01001609#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001610 if( operation->alg == PSA_ALG_CMAC )
1611 {
1612 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1613 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001614 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001615 else
1616#endif /* MBEDTLS_CMAC_C */
1617#if defined(MBEDTLS_MD_C)
1618 if( PSA_ALG_IS_HMAC( operation->alg ) )
1619 {
1620 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1621 unsigned char *opad = operation->ctx.hmac.opad;
1622 size_t hash_size = 0;
1623 size_t block_size =
1624 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001625
Gilles Peskinefbfac682018-07-08 20:51:54 +02001626 if( block_size == 0 )
1627 return( PSA_ERROR_NOT_SUPPORTED );
1628
1629 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, tmp,
1630 sizeof( tmp ), &hash_size );
1631 if( status != PSA_SUCCESS )
1632 return( status );
1633 /* From here on, tmp needs to be wiped. */
1634
1635 status = psa_hash_setup( &operation->ctx.hmac.hash_ctx,
1636 PSA_ALG_HMAC_HASH( operation->alg ) );
1637 if( status != PSA_SUCCESS )
1638 goto hmac_cleanup;
1639
1640 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, opad,
1641 block_size );
1642 if( status != PSA_SUCCESS )
1643 goto hmac_cleanup;
1644
1645 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, tmp,
1646 hash_size );
1647 if( status != PSA_SUCCESS )
1648 goto hmac_cleanup;
1649
1650 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, mac,
1651 mac_size, &hash_size );
1652 hmac_cleanup:
1653 mbedtls_zeroize( tmp, hash_size );
1654 return( status );
1655 }
1656 else
1657#endif /* MBEDTLS_MD_C */
1658 {
1659 /* This shouldn't happen if `operation` was initialized by
1660 * a setup function. */
1661 return( PSA_ERROR_BAD_STATE );
1662 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001663}
1664
Gilles Peskineacd4be32018-07-08 19:56:25 +02001665psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1666 uint8_t *mac,
1667 size_t mac_size,
1668 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001669{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001670 psa_status_t status;
1671
1672 /* Fill the output buffer with something that isn't a valid mac
1673 * (barring an attack on the mac and deliberately-crafted input),
1674 * in case the caller doesn't check the return status properly. */
1675 *mac_length = mac_size;
1676 /* If mac_size is 0 then mac may be NULL and then the
1677 * call to memset would have undefined behavior. */
1678 if( mac_size != 0 )
1679 memset( mac, '!', mac_size );
1680
Gilles Peskine89167cb2018-07-08 20:12:23 +02001681 if( ! operation->is_sign )
1682 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001683 status = PSA_ERROR_BAD_STATE;
1684 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001685 }
mohammad16036df908f2018-04-02 08:34:15 -07001686
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001687 status = psa_mac_finish_internal( operation, mac, mac_size );
1688
1689cleanup:
1690 if( status == PSA_SUCCESS )
1691 {
1692 status = psa_mac_abort( operation );
1693 if( status == PSA_SUCCESS )
1694 *mac_length = operation->mac_size;
1695 else
1696 memset( mac, '!', mac_size );
1697 }
1698 else
1699 psa_mac_abort( operation );
1700 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001701}
1702
Gilles Peskineacd4be32018-07-08 19:56:25 +02001703psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1704 const uint8_t *mac,
1705 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001706{
Gilles Peskine828ed142018-06-18 23:25:51 +02001707 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001708 psa_status_t status;
1709
Gilles Peskine89167cb2018-07-08 20:12:23 +02001710 if( operation->is_sign )
1711 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001712 status = PSA_ERROR_BAD_STATE;
1713 goto cleanup;
1714 }
1715 if( operation->mac_size != mac_length )
1716 {
1717 status = PSA_ERROR_INVALID_SIGNATURE;
1718 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001719 }
mohammad16036df908f2018-04-02 08:34:15 -07001720
1721 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001722 actual_mac, sizeof( actual_mac ) );
1723
1724 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1725 status = PSA_ERROR_INVALID_SIGNATURE;
1726
1727cleanup:
1728 if( status == PSA_SUCCESS )
1729 status = psa_mac_abort( operation );
1730 else
1731 psa_mac_abort( operation );
1732
1733 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001734}
1735
1736
Gilles Peskine20035e32018-02-03 22:44:14 +01001737
Gilles Peskine20035e32018-02-03 22:44:14 +01001738/****************************************************************/
1739/* Asymmetric cryptography */
1740/****************************************************************/
1741
Gilles Peskine2b450e32018-06-27 15:42:46 +02001742#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001743/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001744 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001745static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1746 size_t hash_length,
1747 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001748{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001749 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001750 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001751 *md_alg = mbedtls_md_get_type( md_info );
1752
1753 /* The Mbed TLS RSA module uses an unsigned int for hash length
1754 * parameters. Validate that it fits so that we don't risk an
1755 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001756#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001757 if( hash_length > UINT_MAX )
1758 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001759#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001760
1761#if defined(MBEDTLS_PKCS1_V15)
1762 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
1763 * must be correct. */
1764 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
1765 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001766 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001767 if( md_info == NULL )
1768 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001769 if( mbedtls_md_get_size( md_info ) != hash_length )
1770 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001771 }
1772#endif /* MBEDTLS_PKCS1_V15 */
1773
1774#if defined(MBEDTLS_PKCS1_V21)
1775 /* PSS requires a hash internally. */
1776 if( PSA_ALG_IS_RSA_PSS( alg ) )
1777 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001778 if( md_info == NULL )
1779 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001780 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001781#endif /* MBEDTLS_PKCS1_V21 */
1782
Gilles Peskine61b91d42018-06-08 16:09:36 +02001783 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001784}
1785
Gilles Peskine2b450e32018-06-27 15:42:46 +02001786static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1787 psa_algorithm_t alg,
1788 const uint8_t *hash,
1789 size_t hash_length,
1790 uint8_t *signature,
1791 size_t signature_size,
1792 size_t *signature_length )
1793{
1794 psa_status_t status;
1795 int ret;
1796 mbedtls_md_type_t md_alg;
1797
1798 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1799 if( status != PSA_SUCCESS )
1800 return( status );
1801
Gilles Peskine630a18a2018-06-29 17:49:35 +02001802 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001803 return( PSA_ERROR_BUFFER_TOO_SMALL );
1804
1805#if defined(MBEDTLS_PKCS1_V15)
1806 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1807 {
1808 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1809 MBEDTLS_MD_NONE );
1810 ret = mbedtls_rsa_pkcs1_sign( rsa,
1811 mbedtls_ctr_drbg_random,
1812 &global_data.ctr_drbg,
1813 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001814 md_alg,
1815 (unsigned int) hash_length,
1816 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001817 signature );
1818 }
1819 else
1820#endif /* MBEDTLS_PKCS1_V15 */
1821#if defined(MBEDTLS_PKCS1_V21)
1822 if( PSA_ALG_IS_RSA_PSS( alg ) )
1823 {
1824 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1825 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1826 mbedtls_ctr_drbg_random,
1827 &global_data.ctr_drbg,
1828 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001829 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001830 (unsigned int) hash_length,
1831 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001832 signature );
1833 }
1834 else
1835#endif /* MBEDTLS_PKCS1_V21 */
1836 {
1837 return( PSA_ERROR_INVALID_ARGUMENT );
1838 }
1839
1840 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02001841 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02001842 return( mbedtls_to_psa_error( ret ) );
1843}
1844
1845static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1846 psa_algorithm_t alg,
1847 const uint8_t *hash,
1848 size_t hash_length,
1849 const uint8_t *signature,
1850 size_t signature_length )
1851{
1852 psa_status_t status;
1853 int ret;
1854 mbedtls_md_type_t md_alg;
1855
1856 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1857 if( status != PSA_SUCCESS )
1858 return( status );
1859
Gilles Peskine630a18a2018-06-29 17:49:35 +02001860 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001861 return( PSA_ERROR_BUFFER_TOO_SMALL );
1862
1863#if defined(MBEDTLS_PKCS1_V15)
1864 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1865 {
1866 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1867 MBEDTLS_MD_NONE );
1868 ret = mbedtls_rsa_pkcs1_verify( rsa,
1869 mbedtls_ctr_drbg_random,
1870 &global_data.ctr_drbg,
1871 MBEDTLS_RSA_PUBLIC,
1872 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001873 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001874 hash,
1875 signature );
1876 }
1877 else
1878#endif /* MBEDTLS_PKCS1_V15 */
1879#if defined(MBEDTLS_PKCS1_V21)
1880 if( PSA_ALG_IS_RSA_PSS( alg ) )
1881 {
1882 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1883 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1884 mbedtls_ctr_drbg_random,
1885 &global_data.ctr_drbg,
1886 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001887 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001888 (unsigned int) hash_length,
1889 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001890 signature );
1891 }
1892 else
1893#endif /* MBEDTLS_PKCS1_V21 */
1894 {
1895 return( PSA_ERROR_INVALID_ARGUMENT );
1896 }
1897 return( mbedtls_to_psa_error( ret ) );
1898}
1899#endif /* MBEDTLS_RSA_C */
1900
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001901#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001902/* `ecp` cannot be const because `ecp->grp` needs to be non-const
1903 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
1904 * (even though these functions don't modify it). */
1905static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
1906 psa_algorithm_t alg,
1907 const uint8_t *hash,
1908 size_t hash_length,
1909 uint8_t *signature,
1910 size_t signature_size,
1911 size_t *signature_length )
1912{
1913 int ret;
1914 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001915 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001916 mbedtls_mpi_init( &r );
1917 mbedtls_mpi_init( &s );
1918
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001919 if( signature_size < 2 * curve_bytes )
1920 {
1921 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
1922 goto cleanup;
1923 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001924
1925 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
1926 {
1927 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
1928 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1929 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
1930 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
1931 hash, hash_length,
1932 md_alg ) );
1933 }
1934 else
1935 {
1936 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
1937 hash, hash_length,
1938 mbedtls_ctr_drbg_random,
1939 &global_data.ctr_drbg ) );
1940 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001941
1942 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
1943 signature,
1944 curve_bytes ) );
1945 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
1946 signature + curve_bytes,
1947 curve_bytes ) );
1948
1949cleanup:
1950 mbedtls_mpi_free( &r );
1951 mbedtls_mpi_free( &s );
1952 if( ret == 0 )
1953 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001954 return( mbedtls_to_psa_error( ret ) );
1955}
1956
1957static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
1958 const uint8_t *hash,
1959 size_t hash_length,
1960 const uint8_t *signature,
1961 size_t signature_length )
1962{
1963 int ret;
1964 mbedtls_mpi r, s;
1965 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
1966 mbedtls_mpi_init( &r );
1967 mbedtls_mpi_init( &s );
1968
1969 if( signature_length != 2 * curve_bytes )
1970 return( PSA_ERROR_INVALID_SIGNATURE );
1971
1972 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
1973 signature,
1974 curve_bytes ) );
1975 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
1976 signature + curve_bytes,
1977 curve_bytes ) );
1978
1979 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
1980 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001981
1982cleanup:
1983 mbedtls_mpi_free( &r );
1984 mbedtls_mpi_free( &s );
1985 return( mbedtls_to_psa_error( ret ) );
1986}
1987#endif /* MBEDTLS_ECDSA_C */
1988
Gilles Peskine61b91d42018-06-08 16:09:36 +02001989psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
1990 psa_algorithm_t alg,
1991 const uint8_t *hash,
1992 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02001993 uint8_t *signature,
1994 size_t signature_size,
1995 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01001996{
1997 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001998 psa_status_t status;
1999
2000 *signature_length = signature_size;
2001
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002002 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2003 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002004 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002005 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002006 {
2007 status = PSA_ERROR_INVALID_ARGUMENT;
2008 goto exit;
2009 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002010
Gilles Peskine20035e32018-02-03 22:44:14 +01002011#if defined(MBEDTLS_RSA_C)
2012 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2013 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002014 status = psa_rsa_sign( slot->data.rsa,
2015 alg,
2016 hash, hash_length,
2017 signature, signature_size,
2018 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002019 }
2020 else
2021#endif /* defined(MBEDTLS_RSA_C) */
2022#if defined(MBEDTLS_ECP_C)
2023 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2024 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002025#if defined(MBEDTLS_ECDSA_C)
2026 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002027 status = psa_ecdsa_sign( slot->data.ecp,
2028 alg,
2029 hash, hash_length,
2030 signature, signature_size,
2031 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002032 else
2033#endif /* defined(MBEDTLS_ECDSA_C) */
2034 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002035 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002036 }
itayzafrir5c753392018-05-08 11:18:38 +03002037 }
2038 else
2039#endif /* defined(MBEDTLS_ECP_C) */
2040 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002041 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002042 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002043
2044exit:
2045 /* Fill the unused part of the output buffer (the whole buffer on error,
2046 * the trailing part on success) with something that isn't a valid mac
2047 * (barring an attack on the mac and deliberately-crafted input),
2048 * in case the caller doesn't check the return status properly. */
2049 if( status == PSA_SUCCESS )
2050 memset( signature + *signature_length, '!',
2051 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002052 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002053 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002054 /* If signature_size is 0 then we have nothing to do. We must not call
2055 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002056 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002057}
2058
2059psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2060 psa_algorithm_t alg,
2061 const uint8_t *hash,
2062 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002063 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002064 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002065{
2066 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002067 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002068
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002069 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2070 if( status != PSA_SUCCESS )
2071 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002072
Gilles Peskine61b91d42018-06-08 16:09:36 +02002073#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002074 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002075 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002076 return( psa_rsa_verify( slot->data.rsa,
2077 alg,
2078 hash, hash_length,
2079 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002080 }
2081 else
2082#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002083#if defined(MBEDTLS_ECP_C)
2084 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2085 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002086#if defined(MBEDTLS_ECDSA_C)
2087 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002088 return( psa_ecdsa_verify( slot->data.ecp,
2089 hash, hash_length,
2090 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002091 else
2092#endif /* defined(MBEDTLS_ECDSA_C) */
2093 {
2094 return( PSA_ERROR_INVALID_ARGUMENT );
2095 }
itayzafrir5c753392018-05-08 11:18:38 +03002096 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002097 else
2098#endif /* defined(MBEDTLS_ECP_C) */
2099 {
2100 return( PSA_ERROR_NOT_SUPPORTED );
2101 }
2102}
2103
Gilles Peskine61b91d42018-06-08 16:09:36 +02002104psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2105 psa_algorithm_t alg,
2106 const uint8_t *input,
2107 size_t input_length,
2108 const uint8_t *salt,
2109 size_t salt_length,
2110 uint8_t *output,
2111 size_t output_size,
2112 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002113{
2114 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002115 psa_status_t status;
2116
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002117 (void) salt;
2118 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002119 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002120
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002121 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2122 if( status != PSA_SUCCESS )
2123 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002124 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2125 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002126 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002127
2128#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002129 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002130 {
2131 mbedtls_rsa_context *rsa = slot->data.rsa;
2132 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002133 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002134 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002135#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002136 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002137 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002138 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2139 mbedtls_ctr_drbg_random,
2140 &global_data.ctr_drbg,
2141 MBEDTLS_RSA_PUBLIC,
2142 input_length,
2143 input,
2144 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002145 }
2146 else
2147#endif /* MBEDTLS_PKCS1_V15 */
2148#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002149 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002150 {
2151 return( PSA_ERROR_NOT_SUPPORTED );
2152 }
2153 else
2154#endif /* MBEDTLS_PKCS1_V21 */
2155 {
2156 return( PSA_ERROR_INVALID_ARGUMENT );
2157 }
2158 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002159 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002160 return( mbedtls_to_psa_error( ret ) );
2161 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002162 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002163#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002164 {
2165 return( PSA_ERROR_NOT_SUPPORTED );
2166 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002167}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002168
Gilles Peskine61b91d42018-06-08 16:09:36 +02002169psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2170 psa_algorithm_t alg,
2171 const uint8_t *input,
2172 size_t input_length,
2173 const uint8_t *salt,
2174 size_t salt_length,
2175 uint8_t *output,
2176 size_t output_size,
2177 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002178{
2179 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002180 psa_status_t status;
2181
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002182 (void) salt;
2183 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002184 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002185
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002186 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2187 if( status != PSA_SUCCESS )
2188 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002189 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2190 return( PSA_ERROR_INVALID_ARGUMENT );
2191
2192#if defined(MBEDTLS_RSA_C)
2193 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2194 {
2195 mbedtls_rsa_context *rsa = slot->data.rsa;
2196 int ret;
2197
Gilles Peskine630a18a2018-06-29 17:49:35 +02002198 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002199 return( PSA_ERROR_INVALID_ARGUMENT );
2200
2201#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002202 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002203 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002204 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2205 mbedtls_ctr_drbg_random,
2206 &global_data.ctr_drbg,
2207 MBEDTLS_RSA_PRIVATE,
2208 output_length,
2209 input,
2210 output,
2211 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002212 }
2213 else
2214#endif /* MBEDTLS_PKCS1_V15 */
2215#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002216 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002217 {
2218 return( PSA_ERROR_NOT_SUPPORTED );
2219 }
2220 else
2221#endif /* MBEDTLS_PKCS1_V21 */
2222 {
2223 return( PSA_ERROR_INVALID_ARGUMENT );
2224 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002225
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002226 return( mbedtls_to_psa_error( ret ) );
2227 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002228 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002229#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002230 {
2231 return( PSA_ERROR_NOT_SUPPORTED );
2232 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002233}
Gilles Peskine20035e32018-02-03 22:44:14 +01002234
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002235
2236
mohammad1603503973b2018-03-12 15:59:30 +02002237/****************************************************************/
2238/* Symmetric cryptography */
2239/****************************************************************/
2240
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002241/* Initialize the cipher operation structure. Once this function has been
2242 * called, psa_cipher_abort can run and will do the right thing. */
2243static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2244 psa_algorithm_t alg )
2245{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002246 if( ! PSA_ALG_IS_CIPHER( alg ) )
2247 {
2248 memset( operation, 0, sizeof( *operation ) );
2249 return( PSA_ERROR_INVALID_ARGUMENT );
2250 }
2251
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002252 operation->alg = alg;
2253 operation->key_set = 0;
2254 operation->iv_set = 0;
2255 operation->iv_required = 1;
2256 operation->iv_size = 0;
2257 operation->block_size = 0;
2258 mbedtls_cipher_init( &operation->ctx.cipher );
2259 return( PSA_SUCCESS );
2260}
2261
Gilles Peskinee553c652018-06-04 16:22:46 +02002262static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2263 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002264 psa_algorithm_t alg,
2265 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002266{
2267 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2268 psa_status_t status;
2269 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002270 size_t key_bits;
2271 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002272 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2273 PSA_KEY_USAGE_ENCRYPT :
2274 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002275
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002276 status = psa_cipher_init( operation, alg );
2277 if( status != PSA_SUCCESS )
2278 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002279
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 );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002283 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002284
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002285 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002286 if( cipher_info == NULL )
2287 return( PSA_ERROR_NOT_SUPPORTED );
2288
mohammad1603503973b2018-03-12 15:59:30 +02002289 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002290 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002291 {
2292 psa_cipher_abort( operation );
2293 return( mbedtls_to_psa_error( ret ) );
2294 }
2295
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002296#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002297 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002298 {
2299 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2300 unsigned char keys[24];
2301 memcpy( keys, slot->data.raw.data, 16 );
2302 memcpy( keys + 16, slot->data.raw.data, 8 );
2303 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2304 keys,
2305 192, cipher_operation );
2306 }
2307 else
2308#endif
2309 {
2310 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2311 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002312 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002313 }
Moran Peker41deec42018-04-04 15:43:05 +03002314 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002315 {
2316 psa_cipher_abort( operation );
2317 return( mbedtls_to_psa_error( ret ) );
2318 }
2319
mohammad16038481e742018-03-18 13:57:31 +02002320#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002321 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002322 {
Gilles Peskine53514202018-06-06 15:11:46 +02002323 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2324 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002325
Moran Pekera28258c2018-05-29 16:25:04 +03002326 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002327 {
2328 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2329 mode = MBEDTLS_PADDING_PKCS7;
2330 break;
2331 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2332 mode = MBEDTLS_PADDING_NONE;
2333 break;
2334 default:
Moran Pekerae382792018-05-31 14:06:17 +03002335 psa_cipher_abort( operation );
2336 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002337 }
2338 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002339 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002340 {
2341 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002342 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002343 }
mohammad16038481e742018-03-18 13:57:31 +02002344 }
2345#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2346
mohammad1603503973b2018-03-12 15:59:30 +02002347 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002348 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002349 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) :
Gilles Peskine7e928852018-06-04 16:23:10 +02002350 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002351 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002352 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002353 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002354 }
mohammad1603503973b2018-03-12 15:59:30 +02002355
Moran Peker395db872018-05-31 14:07:14 +03002356 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002357}
2358
Gilles Peskinefe119512018-07-08 21:39:34 +02002359psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2360 psa_key_slot_t key,
2361 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002362{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002363 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002364}
2365
Gilles Peskinefe119512018-07-08 21:39:34 +02002366psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2367 psa_key_slot_t key,
2368 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002369{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002370 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002371}
2372
Gilles Peskinefe119512018-07-08 21:39:34 +02002373psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2374 unsigned char *iv,
2375 size_t iv_size,
2376 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002377{
Moran Peker41deec42018-04-04 15:43:05 +03002378 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002379 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002380 return( PSA_ERROR_BAD_STATE );
2381 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002382 {
Moran Peker41deec42018-04-04 15:43:05 +03002383 ret = PSA_ERROR_BUFFER_TOO_SMALL;
2384 goto exit;
2385 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002386 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2387 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002388 if( ret != 0 )
2389 {
2390 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002391 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002392 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002393
mohammad16038481e742018-03-18 13:57:31 +02002394 *iv_length = operation->iv_size;
Gilles Peskinefe119512018-07-08 21:39:34 +02002395 ret = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002396
Moran Peker395db872018-05-31 14:07:14 +03002397exit:
2398 if( ret != PSA_SUCCESS )
2399 psa_cipher_abort( operation );
2400 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02002401}
2402
Gilles Peskinefe119512018-07-08 21:39:34 +02002403psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2404 const unsigned char *iv,
2405 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002406{
Moran Peker41deec42018-04-04 15:43:05 +03002407 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002408 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002409 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03002410 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002411 {
Moran Pekerae382792018-05-31 14:06:17 +03002412 psa_cipher_abort( operation );
2413 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03002414 }
mohammad1603503973b2018-03-12 15:59:30 +02002415 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002416 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002417 {
2418 psa_cipher_abort( operation );
2419 return( mbedtls_to_psa_error( ret ) );
2420 }
2421
2422 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02002423
Moran Peker395db872018-05-31 14:07:14 +03002424 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002425}
2426
Gilles Peskinee553c652018-06-04 16:22:46 +02002427psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2428 const uint8_t *input,
2429 size_t input_length,
2430 unsigned char *output,
2431 size_t output_size,
2432 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002433{
2434 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002435 size_t expected_output_size;
2436 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2437 {
2438 /* Take the unprocessed partial block left over from previous
2439 * update calls, if any, plus the input to this call. Remove
2440 * the last partial block, if any. You get the data that will be
2441 * output in this call. */
2442 expected_output_size =
2443 ( operation->ctx.cipher.unprocessed_len + input_length )
2444 / operation->block_size * operation->block_size;
2445 }
2446 else
2447 {
2448 expected_output_size = input_length;
2449 }
2450 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03002451 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02002452
mohammad1603503973b2018-03-12 15:59:30 +02002453 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002454 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002455 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002456 {
2457 psa_cipher_abort( operation );
2458 return( mbedtls_to_psa_error( ret ) );
2459 }
2460
Moran Peker395db872018-05-31 14:07:14 +03002461 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002462}
2463
Gilles Peskinee553c652018-06-04 16:22:46 +02002464psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2465 uint8_t *output,
2466 size_t output_size,
2467 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002468{
Janos Follath315b51c2018-07-09 16:04:51 +01002469 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2470 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002471 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002472
mohammad1603503973b2018-03-12 15:59:30 +02002473 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002474 {
Janos Follath315b51c2018-07-09 16:04:51 +01002475 status = PSA_ERROR_BAD_STATE;
2476 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002477 }
2478 if( operation->iv_required && ! operation->iv_set )
2479 {
Janos Follath315b51c2018-07-09 16:04:51 +01002480 status = PSA_ERROR_BAD_STATE;
2481 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002482 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002483 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2484 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002485 {
Gilles Peskine53514202018-06-06 15:11:46 +02002486 psa_algorithm_t padding_mode =
2487 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002488 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002489 {
Janos Follath315b51c2018-07-09 16:04:51 +01002490 status = PSA_ERROR_TAMPERING_DETECTED;
2491 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002492 }
Gilles Peskine53514202018-06-06 15:11:46 +02002493 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002494 {
2495 if( operation->ctx.cipher.unprocessed_len != 0 )
2496 {
Janos Follath315b51c2018-07-09 16:04:51 +01002497 status = PSA_ERROR_INVALID_ARGUMENT;
2498 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002499 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002500 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002501 }
2502
Janos Follath315b51c2018-07-09 16:04:51 +01002503 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2504 temp_output_buffer,
2505 output_length );
2506 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002507 {
Janos Follath315b51c2018-07-09 16:04:51 +01002508 status = mbedtls_to_psa_error( cipher_ret );
2509 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002510 }
Janos Follath315b51c2018-07-09 16:04:51 +01002511
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002512 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002513 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002514 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002515 memcpy( output, temp_output_buffer, *output_length );
2516 else
2517 {
Janos Follath315b51c2018-07-09 16:04:51 +01002518 status = PSA_ERROR_BUFFER_TOO_SMALL;
2519 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002520 }
mohammad1603503973b2018-03-12 15:59:30 +02002521
Janos Follath279ab8e2018-07-09 16:13:21 +01002522 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002523 status = psa_cipher_abort( operation );
2524
2525 return( status );
2526
2527error:
2528
2529 *output_length = 0;
2530
Janos Follath279ab8e2018-07-09 16:13:21 +01002531 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002532 (void) psa_cipher_abort( operation );
2533
2534 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002535}
2536
Gilles Peskinee553c652018-06-04 16:22:46 +02002537psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2538{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002539 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002540 {
2541 /* The object has (apparently) been initialized but it is not
2542 * in use. It's ok to call abort on such an object, and there's
2543 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002544 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002545 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002546
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002547 /* Sanity check (shouldn't happen: operation->alg should
2548 * always have been initialized to a valid value). */
2549 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2550 return( PSA_ERROR_BAD_STATE );
2551
mohammad1603503973b2018-03-12 15:59:30 +02002552 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002553
Moran Peker41deec42018-04-04 15:43:05 +03002554 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002555 operation->key_set = 0;
2556 operation->iv_set = 0;
2557 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002558 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002559 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002560
Moran Peker395db872018-05-31 14:07:14 +03002561 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002562}
2563
Gilles Peskinea0655c32018-04-30 17:06:50 +02002564
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002565
mohammad16038cc1cee2018-03-28 01:21:33 +03002566/****************************************************************/
2567/* Key Policy */
2568/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002569
mohammad160327010052018-07-03 13:16:15 +03002570#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002571void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002572{
Gilles Peskine803ce742018-06-18 16:07:14 +02002573 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002574}
2575
Gilles Peskine2d277862018-06-18 15:41:12 +02002576void psa_key_policy_set_usage( psa_key_policy_t *policy,
2577 psa_key_usage_t usage,
2578 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002579{
mohammad16034eed7572018-03-28 05:14:59 -07002580 policy->usage = usage;
2581 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002582}
2583
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002584psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002585{
mohammad16036df908f2018-04-02 08:34:15 -07002586 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002587}
2588
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002589psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002590{
mohammad16036df908f2018-04-02 08:34:15 -07002591 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002592}
mohammad160327010052018-07-03 13:16:15 +03002593#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002594
Gilles Peskine2d277862018-06-18 15:41:12 +02002595psa_status_t psa_set_key_policy( psa_key_slot_t key,
2596 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002597{
2598 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002599 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002600
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002601 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002602 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002603
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002604 status = psa_get_empty_key_slot( key, &slot );
2605 if( status != PSA_SUCCESS )
2606 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002607
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002608 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2609 PSA_KEY_USAGE_ENCRYPT |
2610 PSA_KEY_USAGE_DECRYPT |
2611 PSA_KEY_USAGE_SIGN |
2612 PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002613 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002614
mohammad16036df908f2018-04-02 08:34:15 -07002615 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002616
2617 return( PSA_SUCCESS );
2618}
2619
Gilles Peskine2d277862018-06-18 15:41:12 +02002620psa_status_t psa_get_key_policy( psa_key_slot_t key,
2621 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002622{
2623 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002624 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002625
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002626 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002627 return( PSA_ERROR_INVALID_ARGUMENT );
2628
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002629 status = psa_get_key_slot( key, &slot );
2630 if( status != PSA_SUCCESS )
2631 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002632
mohammad16036df908f2018-04-02 08:34:15 -07002633 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002634
2635 return( PSA_SUCCESS );
2636}
Gilles Peskine20035e32018-02-03 22:44:14 +01002637
Gilles Peskinea0655c32018-04-30 17:06:50 +02002638
2639
mohammad1603804cd712018-03-20 22:44:08 +02002640/****************************************************************/
2641/* Key Lifetime */
2642/****************************************************************/
2643
Gilles Peskine2d277862018-06-18 15:41:12 +02002644psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2645 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002646{
2647 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002648 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002649
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002650 status = psa_get_key_slot( key, &slot );
2651 if( status != PSA_SUCCESS )
2652 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002653
mohammad1603804cd712018-03-20 22:44:08 +02002654 *lifetime = slot->lifetime;
2655
2656 return( PSA_SUCCESS );
2657}
2658
Gilles Peskine2d277862018-06-18 15:41:12 +02002659psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002660 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002661{
2662 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002663 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002664
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002665 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2666 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002667 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2668 return( PSA_ERROR_INVALID_ARGUMENT );
2669
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002670 status = psa_get_empty_key_slot( key, &slot );
2671 if( status != PSA_SUCCESS )
2672 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002673
Moran Pekerd7326592018-05-29 16:56:39 +03002674 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002675 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002676
mohammad1603060ad8a2018-03-20 14:28:38 -07002677 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002678
2679 return( PSA_SUCCESS );
2680}
2681
Gilles Peskine20035e32018-02-03 22:44:14 +01002682
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002683
mohammad16035955c982018-04-26 00:53:03 +03002684/****************************************************************/
2685/* AEAD */
2686/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002687
mohammad16035955c982018-04-26 00:53:03 +03002688psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2689 psa_algorithm_t alg,
2690 const uint8_t *nonce,
2691 size_t nonce_length,
2692 const uint8_t *additional_data,
2693 size_t additional_data_length,
2694 const uint8_t *plaintext,
2695 size_t plaintext_length,
2696 uint8_t *ciphertext,
2697 size_t ciphertext_size,
2698 size_t *ciphertext_length )
2699{
2700 int ret;
2701 psa_status_t status;
2702 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002703 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002704 uint8_t *tag;
2705 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002706 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002707 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002708
mohammad1603f08a5502018-06-03 15:05:47 +03002709 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002710
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002711 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2712 if( status != PSA_SUCCESS )
2713 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002714 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002715
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002716 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002717 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002718 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002719 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002720
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002721 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002722 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002723 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002724
mohammad16035955c982018-04-26 00:53:03 +03002725 if( alg == PSA_ALG_GCM )
2726 {
2727 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002728 tag_length = 16;
2729
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002730 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002731 return( PSA_ERROR_INVALID_ARGUMENT );
2732
mohammad160315223a82018-06-03 17:19:55 +03002733 //make sure we have place to hold the tag in the ciphertext buffer
2734 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002735 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002736
2737 //update the tag pointer to point to the end of the ciphertext_length
2738 tag = ciphertext + plaintext_length;
2739
mohammad16035955c982018-04-26 00:53:03 +03002740 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002741 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002742 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002743 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002744 if( ret != 0 )
2745 {
2746 mbedtls_gcm_free( &gcm );
2747 return( mbedtls_to_psa_error( ret ) );
2748 }
2749 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002750 plaintext_length, nonce,
2751 nonce_length, additional_data,
2752 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002753 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002754 mbedtls_gcm_free( &gcm );
2755 }
2756 else if( alg == PSA_ALG_CCM )
2757 {
2758 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002759 tag_length = 16;
2760
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002761 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002762 return( PSA_ERROR_INVALID_ARGUMENT );
2763
mohammad160347ddf3d2018-04-26 01:11:21 +03002764 if( nonce_length < 7 || nonce_length > 13 )
2765 return( PSA_ERROR_INVALID_ARGUMENT );
2766
mohammad160315223a82018-06-03 17:19:55 +03002767 //make sure we have place to hold the tag in the ciphertext buffer
2768 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002769 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002770
2771 //update the tag pointer to point to the end of the ciphertext_length
2772 tag = ciphertext + plaintext_length;
2773
mohammad16035955c982018-04-26 00:53:03 +03002774 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002775 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002776 slot->data.raw.data,
2777 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002778 if( ret != 0 )
2779 {
2780 mbedtls_ccm_free( &ccm );
2781 return( mbedtls_to_psa_error( ret ) );
2782 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002783 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002784 nonce, nonce_length,
2785 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002786 additional_data_length,
2787 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002788 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002789 mbedtls_ccm_free( &ccm );
2790 }
mohammad16035c8845f2018-05-09 05:40:09 -07002791 else
2792 {
mohammad1603554faad2018-06-03 15:07:38 +03002793 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002794 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002795
mohammad160315223a82018-06-03 17:19:55 +03002796 if( ret != 0 )
2797 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002798 /* If ciphertext_size is 0 then ciphertext may be NULL and then the
2799 * call to memset would have undefined behavior. */
2800 if( ciphertext_size != 0 )
2801 memset( ciphertext, 0, ciphertext_size );
mohammad160315223a82018-06-03 17:19:55 +03002802 return( mbedtls_to_psa_error( ret ) );
2803 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002804
mohammad160315223a82018-06-03 17:19:55 +03002805 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002806 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002807}
2808
Gilles Peskineee652a32018-06-01 19:23:52 +02002809/* Locate the tag in a ciphertext buffer containing the encrypted data
2810 * followed by the tag. Return the length of the part preceding the tag in
2811 * *plaintext_length. This is the size of the plaintext in modes where
2812 * the encrypted data has the same size as the plaintext, such as
2813 * CCM and GCM. */
2814static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2815 const uint8_t *ciphertext,
2816 size_t ciphertext_length,
2817 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002818 const uint8_t **p_tag )
2819{
2820 size_t payload_length;
2821 if( tag_length > ciphertext_length )
2822 return( PSA_ERROR_INVALID_ARGUMENT );
2823 payload_length = ciphertext_length - tag_length;
2824 if( payload_length > plaintext_size )
2825 return( PSA_ERROR_BUFFER_TOO_SMALL );
2826 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002827 return( PSA_SUCCESS );
2828}
2829
mohammad16035955c982018-04-26 00:53:03 +03002830psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2831 psa_algorithm_t alg,
2832 const uint8_t *nonce,
2833 size_t nonce_length,
2834 const uint8_t *additional_data,
2835 size_t additional_data_length,
2836 const uint8_t *ciphertext,
2837 size_t ciphertext_length,
2838 uint8_t *plaintext,
2839 size_t plaintext_size,
2840 size_t *plaintext_length )
2841{
2842 int ret;
2843 psa_status_t status;
2844 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002845 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002846 const uint8_t *tag;
2847 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002848 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002849 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002850
Gilles Peskineee652a32018-06-01 19:23:52 +02002851 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002852
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002853 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2854 if( status != PSA_SUCCESS )
2855 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002856 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002857
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002858 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002859 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002860 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002861 return( PSA_ERROR_NOT_SUPPORTED );
2862
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002863 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002864 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002865 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002866
mohammad16035955c982018-04-26 00:53:03 +03002867 if( alg == PSA_ALG_GCM )
2868 {
2869 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002870
Gilles Peskineee652a32018-06-01 19:23:52 +02002871 tag_length = 16;
2872 status = psa_aead_unpadded_locate_tag( tag_length,
2873 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002874 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002875 if( status != PSA_SUCCESS )
2876 return( status );
2877
mohammad16035955c982018-04-26 00:53:03 +03002878 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002879 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002880 slot->data.raw.data,
2881 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002882 if( ret != 0 )
2883 {
2884 mbedtls_gcm_free( &gcm );
2885 return( mbedtls_to_psa_error( ret ) );
2886 }
mohammad16035955c982018-04-26 00:53:03 +03002887
Gilles Peskineee652a32018-06-01 19:23:52 +02002888 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002889 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002890 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002891 additional_data,
2892 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002893 tag, tag_length,
2894 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002895 mbedtls_gcm_free( &gcm );
2896 }
2897 else if( alg == PSA_ALG_CCM )
2898 {
2899 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002900
mohammad160347ddf3d2018-04-26 01:11:21 +03002901 if( nonce_length < 7 || nonce_length > 13 )
2902 return( PSA_ERROR_INVALID_ARGUMENT );
2903
mohammad16039375f842018-06-03 14:28:24 +03002904 tag_length = 16;
2905 status = psa_aead_unpadded_locate_tag( tag_length,
2906 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002907 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002908 if( status != PSA_SUCCESS )
2909 return( status );
2910
mohammad16035955c982018-04-26 00:53:03 +03002911 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002912 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002913 slot->data.raw.data,
2914 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002915 if( ret != 0 )
2916 {
2917 mbedtls_ccm_free( &ccm );
2918 return( mbedtls_to_psa_error( ret ) );
2919 }
mohammad160360a64d02018-06-03 17:20:42 +03002920 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002921 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002922 additional_data,
2923 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002924 ciphertext, plaintext,
2925 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002926 mbedtls_ccm_free( &ccm );
2927 }
mohammad160339574652018-06-01 04:39:53 -07002928 else
2929 {
mohammad1603554faad2018-06-03 15:07:38 +03002930 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07002931 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002932
Gilles Peskineee652a32018-06-01 19:23:52 +02002933 if( ret != 0 )
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002934 {
2935 /* If plaintext_size is 0 then plaintext may be NULL and then the
2936 * call to memset has undefined behavior. */
2937 if( plaintext_size != 0 )
2938 memset( plaintext, 0, plaintext_size );
2939 }
mohammad160360a64d02018-06-03 17:20:42 +03002940 else
2941 *plaintext_length = ciphertext_length - tag_length;
2942
Gilles Peskineee652a32018-06-01 19:23:52 +02002943 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03002944}
2945
Gilles Peskinea0655c32018-04-30 17:06:50 +02002946
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002947
Gilles Peskine20035e32018-02-03 22:44:14 +01002948/****************************************************************/
Gilles Peskine05d69892018-06-19 22:00:52 +02002949/* Key generation */
2950/****************************************************************/
2951
2952psa_status_t psa_generate_random( uint8_t *output,
2953 size_t output_size )
2954{
2955 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2956 output, output_size );
2957 return( mbedtls_to_psa_error( ret ) );
2958}
2959
2960psa_status_t psa_generate_key( psa_key_slot_t key,
2961 psa_key_type_t type,
2962 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002963 const void *extra,
2964 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02002965{
Gilles Peskine12313cd2018-06-20 00:20:32 +02002966 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002967 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02002968
Gilles Peskine53d991e2018-07-12 01:14:59 +02002969 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002970 return( PSA_ERROR_INVALID_ARGUMENT );
2971
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002972 status = psa_get_empty_key_slot( key, &slot );
2973 if( status != PSA_SUCCESS )
2974 return( status );
2975
Gilles Peskine48c0ea12018-06-21 14:15:31 +02002976 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002977 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002978 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02002979 if( status != PSA_SUCCESS )
2980 return( status );
2981 status = psa_generate_random( slot->data.raw.data,
2982 slot->data.raw.bytes );
2983 if( status != PSA_SUCCESS )
2984 {
2985 mbedtls_free( slot->data.raw.data );
2986 return( status );
2987 }
2988#if defined(MBEDTLS_DES_C)
2989 if( type == PSA_KEY_TYPE_DES )
2990 {
2991 mbedtls_des_key_set_parity( slot->data.raw.data );
2992 if( slot->data.raw.bytes >= 16 )
2993 mbedtls_des_key_set_parity( slot->data.raw.data + 8 );
2994 if( slot->data.raw.bytes == 24 )
2995 mbedtls_des_key_set_parity( slot->data.raw.data + 16 );
2996 }
2997#endif /* MBEDTLS_DES_C */
2998 }
2999 else
3000
3001#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3002 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3003 {
3004 mbedtls_rsa_context *rsa;
3005 int ret;
3006 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003007 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3008 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02003009 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003010 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02003011 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003012 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003013 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003014#if INT_MAX < 0xffffffff
3015 /* Check that the uint32_t value passed by the caller fits
3016 * in the range supported by this implementation. */
3017 if( p->e > INT_MAX )
3018 return( PSA_ERROR_NOT_SUPPORTED );
3019#endif
3020 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003021 }
3022 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3023 if( rsa == NULL )
3024 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3025 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3026 ret = mbedtls_rsa_gen_key( rsa,
3027 mbedtls_ctr_drbg_random,
3028 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003029 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003030 exponent );
3031 if( ret != 0 )
3032 {
3033 mbedtls_rsa_free( rsa );
3034 mbedtls_free( rsa );
3035 return( mbedtls_to_psa_error( ret ) );
3036 }
3037 slot->data.rsa = rsa;
3038 }
3039 else
3040#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3041
3042#if defined(MBEDTLS_ECP_C)
3043 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3044 {
3045 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3046 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3047 const mbedtls_ecp_curve_info *curve_info =
3048 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3049 mbedtls_ecp_keypair *ecp;
3050 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003051 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003052 return( PSA_ERROR_NOT_SUPPORTED );
3053 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3054 return( PSA_ERROR_NOT_SUPPORTED );
3055 if( curve_info->bit_size != bits )
3056 return( PSA_ERROR_INVALID_ARGUMENT );
3057 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3058 if( ecp == NULL )
3059 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3060 mbedtls_ecp_keypair_init( ecp );
3061 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3062 mbedtls_ctr_drbg_random,
3063 &global_data.ctr_drbg );
3064 if( ret != 0 )
3065 {
3066 mbedtls_ecp_keypair_free( ecp );
3067 mbedtls_free( ecp );
3068 return( mbedtls_to_psa_error( ret ) );
3069 }
3070 slot->data.ecp = ecp;
3071 }
3072 else
3073#endif /* MBEDTLS_ECP_C */
3074
3075 return( PSA_ERROR_NOT_SUPPORTED );
3076
3077 slot->type = type;
3078 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003079}
3080
3081
3082/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003083/* Module setup */
3084/****************************************************************/
3085
Gilles Peskinee59236f2018-01-27 23:32:46 +01003086void mbedtls_psa_crypto_free( void )
3087{
Jaeden Amero045bd502018-06-26 14:00:08 +01003088 psa_key_slot_t key;
Gilles Peskine828ed142018-06-18 23:25:51 +02003089 for( key = 1; key < PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003090 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003091 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3092 mbedtls_entropy_free( &global_data.entropy );
3093 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3094}
3095
3096psa_status_t psa_crypto_init( void )
3097{
3098 int ret;
3099 const unsigned char drbg_seed[] = "PSA";
3100
3101 if( global_data.initialized != 0 )
3102 return( PSA_SUCCESS );
3103
3104 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3105 mbedtls_entropy_init( &global_data.entropy );
3106 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3107
3108 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3109 mbedtls_entropy_func,
3110 &global_data.entropy,
3111 drbg_seed, sizeof( drbg_seed ) - 1 );
3112 if( ret != 0 )
3113 goto exit;
3114
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003115 global_data.initialized = 1;
3116
Gilles Peskinee59236f2018-01-27 23:32:46 +01003117exit:
3118 if( ret != 0 )
3119 mbedtls_psa_crypto_free( );
3120 return( mbedtls_to_psa_error( ret ) );
3121}
3122
3123#endif /* MBEDTLS_PSA_CRYPTO_C */