blob: fac1c7564b527a1811d4aba5ca005d2457fb923d [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 Peskine2d277862018-06-18 15:41:12 +0200544psa_status_t psa_import_key( psa_key_slot_t key,
545 psa_key_type_t type,
546 const uint8_t *data,
547 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100548{
549 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200550 psa_status_t status = PSA_SUCCESS;
551 status = psa_get_empty_key_slot( key, &slot );
552 if( status != PSA_SUCCESS )
553 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100554
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200555 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100556 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100557 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100558 if( data_length > SIZE_MAX / 8 )
559 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200560 status = prepare_raw_data_slot( type,
561 PSA_BYTES_TO_BITS( data_length ),
562 &slot->data.raw );
563 if( status != PSA_SUCCESS )
564 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200565 if( data_length != 0 )
566 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100567 }
568 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100569#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200570 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100571 {
572 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100573 mbedtls_pk_context pk;
574 mbedtls_pk_init( &pk );
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100575 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
576 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
577 else
578 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100579 if( ret != 0 )
580 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine969ac722018-01-28 18:16:59 +0100581 switch( mbedtls_pk_get_type( &pk ) )
582 {
583#if defined(MBEDTLS_RSA_C)
584 case MBEDTLS_PK_RSA:
Gilles Peskined8008d62018-06-29 19:51:51 +0200585 if( PSA_KEY_TYPE_IS_RSA( type ) )
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200586 {
587 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
588 size_t bits = mbedtls_rsa_get_bitlen( rsa );
589 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
Gilles Peskine1ae05142018-06-30 17:46:59 +0200590 {
591 status = PSA_ERROR_NOT_SUPPORTED;
592 break;
593 }
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200594 slot->data.rsa = rsa;
595 }
Gilles Peskine969ac722018-01-28 18:16:59 +0100596 else
Gilles Peskinec648d692018-06-28 08:46:13 +0200597 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine969ac722018-01-28 18:16:59 +0100598 break;
599#endif /* MBEDTLS_RSA_C */
600#if defined(MBEDTLS_ECP_C)
601 case MBEDTLS_PK_ECKEY:
602 if( PSA_KEY_TYPE_IS_ECC( type ) )
603 {
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200604 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
605 psa_ecc_curve_t actual_curve =
606 mbedtls_ecc_group_to_psa( ecp->grp.id );
607 psa_ecc_curve_t expected_curve =
608 PSA_KEY_TYPE_GET_CURVE( type );
609 if( actual_curve != expected_curve )
Gilles Peskinec648d692018-06-28 08:46:13 +0200610 {
611 status = PSA_ERROR_INVALID_ARGUMENT;
612 break;
613 }
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200614 slot->data.ecp = ecp;
Gilles Peskine969ac722018-01-28 18:16:59 +0100615 }
616 else
Gilles Peskinec648d692018-06-28 08:46:13 +0200617 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine969ac722018-01-28 18:16:59 +0100618 break;
619#endif /* MBEDTLS_ECP_C */
620 default:
Gilles Peskinec648d692018-06-28 08:46:13 +0200621 status = PSA_ERROR_INVALID_ARGUMENT;
622 break;
623 }
624 /* Free the content of the pk object only on error. On success,
625 * the content of the object has been stored in the slot. */
626 if( status != PSA_SUCCESS )
627 {
628 mbedtls_pk_free( &pk );
629 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100630 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100631 }
632 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100633#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100634 {
635 return( PSA_ERROR_NOT_SUPPORTED );
636 }
637
638 slot->type = type;
639 return( PSA_SUCCESS );
640}
641
Gilles Peskine2d277862018-06-18 15:41:12 +0200642psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100643{
644 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200645 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100646
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200647 status = psa_get_key_slot( key, &slot );
648 if( status != PSA_SUCCESS )
649 return( status );
650
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100651 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200652 {
653 /* No key material to clean, but do zeroize the slot below to wipe
654 * metadata such as policies. */
655 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200656 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100657 {
658 mbedtls_free( slot->data.raw.data );
659 }
660 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100661#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200662 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100663 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100664 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100665 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100666 }
667 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100668#endif /* defined(MBEDTLS_RSA_C) */
669#if defined(MBEDTLS_ECP_C)
670 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
671 {
672 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100673 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100674 }
675 else
676#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100677 {
678 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100679 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100680 return( PSA_ERROR_TAMPERING_DETECTED );
681 }
682
683 mbedtls_zeroize( slot, sizeof( *slot ) );
684 return( PSA_SUCCESS );
685}
686
Gilles Peskineb870b182018-07-06 16:02:09 +0200687/* Return the size of the key in the given slot, in bits. */
688static size_t psa_get_key_bits( const key_slot_t *slot )
689{
690 if( key_type_is_raw_bytes( slot->type ) )
691 return( slot->data.raw.bytes * 8 );
692#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200693 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineb870b182018-07-06 16:02:09 +0200694 return( mbedtls_rsa_get_bitlen( slot->data.rsa ) );
695#endif /* defined(MBEDTLS_RSA_C) */
696#if defined(MBEDTLS_ECP_C)
697 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
698 return( slot->data.ecp->grp.pbits );
699#endif /* defined(MBEDTLS_ECP_C) */
700 /* Shouldn't happen except on an empty slot. */
701 return( 0 );
702}
703
Gilles Peskine2d277862018-06-18 15:41:12 +0200704psa_status_t psa_get_key_information( psa_key_slot_t key,
705 psa_key_type_t *type,
706 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100707{
708 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200709 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100710
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100711 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200712 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100713 if( bits != NULL )
714 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200715 status = psa_get_key_slot( key, &slot );
716 if( status != PSA_SUCCESS )
717 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200718
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100719 if( slot->type == PSA_KEY_TYPE_NONE )
720 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200721 if( type != NULL )
722 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200723 if( bits != NULL )
724 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100725 return( PSA_SUCCESS );
726}
727
Gilles Peskine2d277862018-06-18 15:41:12 +0200728static psa_status_t psa_internal_export_key( psa_key_slot_t key,
729 uint8_t *data,
730 size_t data_size,
731 size_t *data_length,
732 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100733{
734 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200735 psa_status_t status;
736 /* Exporting a public key doesn't require a usage flag. If we're
737 * called by psa_export_public_key(), don't require the EXPORT flag.
738 * If we're called by psa_export_key(), do require the EXPORT flag;
739 * if the key turns out to be public key object, psa_get_key_from_slot()
740 * will ignore this flag. */
741 psa_key_usage_t usage = export_public_key ? 0 : PSA_KEY_USAGE_EXPORT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100742
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100743 /* Set the key to empty now, so that even when there are errors, we always
744 * set data_length to a value between 0 and data_size. On error, setting
745 * the key to empty is a good choice because an empty key representation is
746 * unlikely to be accepted anywhere. */
747 *data_length = 0;
748
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200749 status = psa_get_key_from_slot( key, &slot, usage, 0 );
750 if( status != PSA_SUCCESS )
751 return( status );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200752 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300753 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300754
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200755 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100756 {
757 if( slot->data.raw.bytes > data_size )
758 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200759 if( slot->data.raw.bytes != 0 )
760 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100761 *data_length = slot->data.raw.bytes;
762 return( PSA_SUCCESS );
763 }
764 else
Moran Peker17e36e12018-05-02 12:55:20 +0300765 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100766#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200767 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300768 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100769 {
Moran Pekera998bc62018-04-16 18:16:20 +0300770 mbedtls_pk_context pk;
771 int ret;
772 mbedtls_pk_init( &pk );
Gilles Peskined8008d62018-06-29 19:51:51 +0200773 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300774 {
775 pk.pk_info = &mbedtls_rsa_info;
776 pk.pk_ctx = slot->data.rsa;
777 }
778 else
779 {
780 pk.pk_info = &mbedtls_eckey_info;
781 pk.pk_ctx = slot->data.ecp;
782 }
Moran Pekerd7326592018-05-29 16:56:39 +0300783 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300784 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300785 else
786 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300787 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200788 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200789 /* If data_size is 0 then data may be NULL and then the
790 * call to memset would have undefined behavior. */
791 if( data_size != 0 )
792 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300793 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200794 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200795 /* The mbedtls_pk_xxx functions write to the end of the buffer.
796 * Move the data to the beginning and erase remaining data
797 * at the original location. */
798 if( 2 * (size_t) ret <= data_size )
799 {
800 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200801 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200802 }
803 else if( (size_t) ret < data_size )
804 {
805 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200806 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200807 }
Moran Pekera998bc62018-04-16 18:16:20 +0300808 *data_length = ret;
809 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100810 }
811 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100812#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300813 {
814 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200815 it is valid for a special-purpose implementation to omit
816 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300817 return( PSA_ERROR_NOT_SUPPORTED );
818 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100819 }
820}
821
Gilles Peskine2d277862018-06-18 15:41:12 +0200822psa_status_t psa_export_key( psa_key_slot_t key,
823 uint8_t *data,
824 size_t data_size,
825 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300826{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200827 return( psa_internal_export_key( key, data, data_size,
828 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100829}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100830
Gilles Peskine2d277862018-06-18 15:41:12 +0200831psa_status_t psa_export_public_key( psa_key_slot_t key,
832 uint8_t *data,
833 size_t data_size,
834 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300835{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200836 return( psa_internal_export_key( key, data, data_size,
837 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300838}
839
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200840
841
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100842/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100843/* Message digests */
844/****************************************************************/
845
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100846static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100847{
848 switch( alg )
849 {
850#if defined(MBEDTLS_MD2_C)
851 case PSA_ALG_MD2:
852 return( &mbedtls_md2_info );
853#endif
854#if defined(MBEDTLS_MD4_C)
855 case PSA_ALG_MD4:
856 return( &mbedtls_md4_info );
857#endif
858#if defined(MBEDTLS_MD5_C)
859 case PSA_ALG_MD5:
860 return( &mbedtls_md5_info );
861#endif
862#if defined(MBEDTLS_RIPEMD160_C)
863 case PSA_ALG_RIPEMD160:
864 return( &mbedtls_ripemd160_info );
865#endif
866#if defined(MBEDTLS_SHA1_C)
867 case PSA_ALG_SHA_1:
868 return( &mbedtls_sha1_info );
869#endif
870#if defined(MBEDTLS_SHA256_C)
871 case PSA_ALG_SHA_224:
872 return( &mbedtls_sha224_info );
873 case PSA_ALG_SHA_256:
874 return( &mbedtls_sha256_info );
875#endif
876#if defined(MBEDTLS_SHA512_C)
877 case PSA_ALG_SHA_384:
878 return( &mbedtls_sha384_info );
879 case PSA_ALG_SHA_512:
880 return( &mbedtls_sha512_info );
881#endif
882 default:
883 return( NULL );
884 }
885}
886
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100887psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
888{
889 switch( operation->alg )
890 {
Gilles Peskine81736312018-06-26 15:04:31 +0200891 case 0:
892 /* The object has (apparently) been initialized but it is not
893 * in use. It's ok to call abort on such an object, and there's
894 * nothing to do. */
895 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100896#if defined(MBEDTLS_MD2_C)
897 case PSA_ALG_MD2:
898 mbedtls_md2_free( &operation->ctx.md2 );
899 break;
900#endif
901#if defined(MBEDTLS_MD4_C)
902 case PSA_ALG_MD4:
903 mbedtls_md4_free( &operation->ctx.md4 );
904 break;
905#endif
906#if defined(MBEDTLS_MD5_C)
907 case PSA_ALG_MD5:
908 mbedtls_md5_free( &operation->ctx.md5 );
909 break;
910#endif
911#if defined(MBEDTLS_RIPEMD160_C)
912 case PSA_ALG_RIPEMD160:
913 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
914 break;
915#endif
916#if defined(MBEDTLS_SHA1_C)
917 case PSA_ALG_SHA_1:
918 mbedtls_sha1_free( &operation->ctx.sha1 );
919 break;
920#endif
921#if defined(MBEDTLS_SHA256_C)
922 case PSA_ALG_SHA_224:
923 case PSA_ALG_SHA_256:
924 mbedtls_sha256_free( &operation->ctx.sha256 );
925 break;
926#endif
927#if defined(MBEDTLS_SHA512_C)
928 case PSA_ALG_SHA_384:
929 case PSA_ALG_SHA_512:
930 mbedtls_sha512_free( &operation->ctx.sha512 );
931 break;
932#endif
933 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +0200934 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100935 }
936 operation->alg = 0;
937 return( PSA_SUCCESS );
938}
939
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200940psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100941 psa_algorithm_t alg )
942{
943 int ret;
944 operation->alg = 0;
945 switch( alg )
946 {
947#if defined(MBEDTLS_MD2_C)
948 case PSA_ALG_MD2:
949 mbedtls_md2_init( &operation->ctx.md2 );
950 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
951 break;
952#endif
953#if defined(MBEDTLS_MD4_C)
954 case PSA_ALG_MD4:
955 mbedtls_md4_init( &operation->ctx.md4 );
956 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
957 break;
958#endif
959#if defined(MBEDTLS_MD5_C)
960 case PSA_ALG_MD5:
961 mbedtls_md5_init( &operation->ctx.md5 );
962 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
963 break;
964#endif
965#if defined(MBEDTLS_RIPEMD160_C)
966 case PSA_ALG_RIPEMD160:
967 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
968 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
969 break;
970#endif
971#if defined(MBEDTLS_SHA1_C)
972 case PSA_ALG_SHA_1:
973 mbedtls_sha1_init( &operation->ctx.sha1 );
974 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
975 break;
976#endif
977#if defined(MBEDTLS_SHA256_C)
978 case PSA_ALG_SHA_224:
979 mbedtls_sha256_init( &operation->ctx.sha256 );
980 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
981 break;
982 case PSA_ALG_SHA_256:
983 mbedtls_sha256_init( &operation->ctx.sha256 );
984 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
985 break;
986#endif
987#if defined(MBEDTLS_SHA512_C)
988 case PSA_ALG_SHA_384:
989 mbedtls_sha512_init( &operation->ctx.sha512 );
990 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
991 break;
992 case PSA_ALG_SHA_512:
993 mbedtls_sha512_init( &operation->ctx.sha512 );
994 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
995 break;
996#endif
997 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +0200998 return( PSA_ALG_IS_HASH( alg ) ?
999 PSA_ERROR_NOT_SUPPORTED :
1000 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001001 }
1002 if( ret == 0 )
1003 operation->alg = alg;
1004 else
1005 psa_hash_abort( operation );
1006 return( mbedtls_to_psa_error( ret ) );
1007}
1008
1009psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1010 const uint8_t *input,
1011 size_t input_length )
1012{
1013 int ret;
1014 switch( operation->alg )
1015 {
1016#if defined(MBEDTLS_MD2_C)
1017 case PSA_ALG_MD2:
1018 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1019 input, input_length );
1020 break;
1021#endif
1022#if defined(MBEDTLS_MD4_C)
1023 case PSA_ALG_MD4:
1024 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1025 input, input_length );
1026 break;
1027#endif
1028#if defined(MBEDTLS_MD5_C)
1029 case PSA_ALG_MD5:
1030 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1031 input, input_length );
1032 break;
1033#endif
1034#if defined(MBEDTLS_RIPEMD160_C)
1035 case PSA_ALG_RIPEMD160:
1036 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1037 input, input_length );
1038 break;
1039#endif
1040#if defined(MBEDTLS_SHA1_C)
1041 case PSA_ALG_SHA_1:
1042 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1043 input, input_length );
1044 break;
1045#endif
1046#if defined(MBEDTLS_SHA256_C)
1047 case PSA_ALG_SHA_224:
1048 case PSA_ALG_SHA_256:
1049 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1050 input, input_length );
1051 break;
1052#endif
1053#if defined(MBEDTLS_SHA512_C)
1054 case PSA_ALG_SHA_384:
1055 case PSA_ALG_SHA_512:
1056 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1057 input, input_length );
1058 break;
1059#endif
1060 default:
1061 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1062 break;
1063 }
1064 if( ret != 0 )
1065 psa_hash_abort( operation );
1066 return( mbedtls_to_psa_error( ret ) );
1067}
1068
1069psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1070 uint8_t *hash,
1071 size_t hash_size,
1072 size_t *hash_length )
1073{
1074 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001075 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001076
1077 /* Fill the output buffer with something that isn't a valid hash
1078 * (barring an attack on the hash and deliberately-crafted input),
1079 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001080 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001081 /* If hash_size is 0 then hash may be NULL and then the
1082 * call to memset would have undefined behavior. */
1083 if( hash_size != 0 )
1084 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001085
1086 if( hash_size < actual_hash_length )
1087 return( PSA_ERROR_BUFFER_TOO_SMALL );
1088
1089 switch( operation->alg )
1090 {
1091#if defined(MBEDTLS_MD2_C)
1092 case PSA_ALG_MD2:
1093 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1094 break;
1095#endif
1096#if defined(MBEDTLS_MD4_C)
1097 case PSA_ALG_MD4:
1098 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1099 break;
1100#endif
1101#if defined(MBEDTLS_MD5_C)
1102 case PSA_ALG_MD5:
1103 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1104 break;
1105#endif
1106#if defined(MBEDTLS_RIPEMD160_C)
1107 case PSA_ALG_RIPEMD160:
1108 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1109 break;
1110#endif
1111#if defined(MBEDTLS_SHA1_C)
1112 case PSA_ALG_SHA_1:
1113 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1114 break;
1115#endif
1116#if defined(MBEDTLS_SHA256_C)
1117 case PSA_ALG_SHA_224:
1118 case PSA_ALG_SHA_256:
1119 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1120 break;
1121#endif
1122#if defined(MBEDTLS_SHA512_C)
1123 case PSA_ALG_SHA_384:
1124 case PSA_ALG_SHA_512:
1125 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1126 break;
1127#endif
1128 default:
1129 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1130 break;
1131 }
1132
1133 if( ret == 0 )
1134 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001135 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001136 return( psa_hash_abort( operation ) );
1137 }
1138 else
1139 {
1140 psa_hash_abort( operation );
1141 return( mbedtls_to_psa_error( ret ) );
1142 }
1143}
1144
Gilles Peskine2d277862018-06-18 15:41:12 +02001145psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1146 const uint8_t *hash,
1147 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001148{
1149 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1150 size_t actual_hash_length;
1151 psa_status_t status = psa_hash_finish( operation,
1152 actual_hash, sizeof( actual_hash ),
1153 &actual_hash_length );
1154 if( status != PSA_SUCCESS )
1155 return( status );
1156 if( actual_hash_length != hash_length )
1157 return( PSA_ERROR_INVALID_SIGNATURE );
1158 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1159 return( PSA_ERROR_INVALID_SIGNATURE );
1160 return( PSA_SUCCESS );
1161}
1162
1163
1164
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001165/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001166/* MAC */
1167/****************************************************************/
1168
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001169static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001170 psa_algorithm_t alg,
1171 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001172 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001173 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001174{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001175 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001176 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001177
1178 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1179 {
1180 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001181 {
1182 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1183 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001184
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001185 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001186 {
Gilles Peskine5d1888e2018-07-12 00:32:42 +02001187 case PSA_ALG_STREAM_CIPHER_BASE:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001188 mode = MBEDTLS_MODE_STREAM;
1189 break;
1190 case PSA_ALG_CBC_BASE:
1191 mode = MBEDTLS_MODE_CBC;
1192 break;
1193 case PSA_ALG_CFB_BASE:
1194 mode = MBEDTLS_MODE_CFB;
1195 break;
1196 case PSA_ALG_OFB_BASE:
1197 mode = MBEDTLS_MODE_OFB;
1198 break;
1199 case PSA_ALG_CTR:
1200 mode = MBEDTLS_MODE_CTR;
1201 break;
1202 case PSA_ALG_CCM:
1203 mode = MBEDTLS_MODE_CCM;
1204 break;
1205 case PSA_ALG_GCM:
1206 mode = MBEDTLS_MODE_GCM;
1207 break;
1208 default:
1209 return( NULL );
1210 }
1211 }
1212 else if( alg == PSA_ALG_CMAC )
1213 mode = MBEDTLS_MODE_ECB;
1214 else if( alg == PSA_ALG_GMAC )
1215 mode = MBEDTLS_MODE_GCM;
1216 else
1217 return( NULL );
1218
1219 switch( key_type )
1220 {
1221 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001222 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001223 break;
1224 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001225 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1226 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001227 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001228 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001229 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001230 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001231 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1232 * but two-key Triple-DES is functionally three-key Triple-DES
1233 * with K1=K3, so that's how we present it to mbedtls. */
1234 if( key_bits == 128 )
1235 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001236 break;
1237 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001238 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001239 break;
1240 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001241 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001242 break;
1243 default:
1244 return( NULL );
1245 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001246 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001247 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001248
Jaeden Amero23bbb752018-06-26 14:16:54 +01001249 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1250 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001251}
1252
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001253static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001254{
Gilles Peskine2d277862018-06-18 15:41:12 +02001255 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001256 {
1257 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001258 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001259 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001260 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001261 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001262 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001263 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001264 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001265 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001266 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001267 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001268 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001269 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001270 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001271 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001272 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001273 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001274 return( 128 );
1275 default:
1276 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001277 }
1278}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001279
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001280/* Initialize the MAC operation structure. Once this function has been
1281 * called, psa_mac_abort can run and will do the right thing. */
1282static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1283 psa_algorithm_t alg )
1284{
1285 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1286
1287 operation->alg = alg;
1288 operation->key_set = 0;
1289 operation->iv_set = 0;
1290 operation->iv_required = 0;
1291 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001292 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001293
1294#if defined(MBEDTLS_CMAC_C)
1295 if( alg == PSA_ALG_CMAC )
1296 {
1297 operation->iv_required = 0;
1298 mbedtls_cipher_init( &operation->ctx.cmac );
1299 status = PSA_SUCCESS;
1300 }
1301 else
1302#endif /* MBEDTLS_CMAC_C */
1303#if defined(MBEDTLS_MD_C)
1304 if( PSA_ALG_IS_HMAC( operation->alg ) )
1305 {
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001306 status = psa_hash_setup( &operation->ctx.hmac.hash_ctx,
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001307 PSA_ALG_HMAC_HASH( alg ) );
1308 }
1309 else
1310#endif /* MBEDTLS_MD_C */
1311 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001312 if( ! PSA_ALG_IS_MAC( alg ) )
1313 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001314 }
1315
1316 if( status != PSA_SUCCESS )
1317 memset( operation, 0, sizeof( *operation ) );
1318 return( status );
1319}
1320
Gilles Peskine8c9def32018-02-08 10:02:12 +01001321psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1322{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001323 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001324 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001325 /* The object has (apparently) been initialized but it is not
1326 * in use. It's ok to call abort on such an object, and there's
1327 * nothing to do. */
1328 return( PSA_SUCCESS );
1329 }
1330 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001331#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001332 if( operation->alg == PSA_ALG_CMAC )
1333 {
1334 mbedtls_cipher_free( &operation->ctx.cmac );
1335 }
1336 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001337#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001338#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001339 if( PSA_ALG_IS_HMAC( operation->alg ) )
1340 {
1341 size_t block_size =
1342 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
1343 if( block_size == 0 )
1344 goto bad_state;
1345 psa_hash_abort( &operation->ctx.hmac.hash_ctx );
1346 mbedtls_zeroize( operation->ctx.hmac.opad, block_size );
1347 }
1348 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001349#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001350 {
1351 /* Sanity check (shouldn't happen: operation->alg should
1352 * always have been initialized to a valid value). */
1353 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001354 }
Moran Peker41deec42018-04-04 15:43:05 +03001355
Gilles Peskine8c9def32018-02-08 10:02:12 +01001356 operation->alg = 0;
1357 operation->key_set = 0;
1358 operation->iv_set = 0;
1359 operation->iv_required = 0;
1360 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001361 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001362
Gilles Peskine8c9def32018-02-08 10:02:12 +01001363 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001364
1365bad_state:
1366 /* If abort is called on an uninitialized object, we can't trust
1367 * anything. Wipe the object in case it contains confidential data.
1368 * This may result in a memory leak if a pointer gets overwritten,
1369 * but it's too late to do anything about this. */
1370 memset( operation, 0, sizeof( *operation ) );
1371 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001372}
1373
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001374#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001375static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001376 size_t key_bits,
1377 key_slot_t *slot,
1378 const mbedtls_cipher_info_t *cipher_info )
1379{
1380 int ret;
1381
1382 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001383
1384 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1385 if( ret != 0 )
1386 return( ret );
1387
1388 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1389 slot->data.raw.data,
1390 key_bits );
1391 return( ret );
1392}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001393#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001394
Gilles Peskine248051a2018-06-20 16:09:38 +02001395#if defined(MBEDTLS_MD_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001396static int psa_hmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001397 psa_key_type_t key_type,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001398 key_slot_t *slot,
1399 psa_algorithm_t alg )
1400{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001401 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001402 unsigned char *opad = operation->ctx.hmac.opad;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001403 size_t i;
Gilles Peskinee1bc6802018-06-11 17:36:05 +02001404 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001405 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001406 unsigned int digest_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001407 PSA_HASH_SIZE( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001408 size_t key_length = slot->data.raw.bytes;
1409 psa_status_t status;
1410
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001411 if( block_size == 0 || digest_size == 0 )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001412 return( PSA_ERROR_NOT_SUPPORTED );
1413
1414 if( key_type != PSA_KEY_TYPE_HMAC )
1415 return( PSA_ERROR_INVALID_ARGUMENT );
1416
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001417 operation->mac_size = digest_size;
1418
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001419 /* The hash was started earlier in psa_mac_init. */
Gilles Peskined223b522018-06-11 18:12:58 +02001420 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001421 {
1422 status = psa_hash_update( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001423 slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001424 if( status != PSA_SUCCESS )
1425 return( status );
1426 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001427 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001428 if( status != PSA_SUCCESS )
1429 return( status );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001430 }
Gilles Peskined223b522018-06-11 18:12:58 +02001431 else
1432 memcpy( ipad, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001433
Gilles Peskined223b522018-06-11 18:12:58 +02001434 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1435 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001436 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001437 ipad[i] ^= 0x36;
1438 memset( ipad + key_length, 0x36, block_size - key_length );
1439
1440 /* Copy the key material from ipad to opad, flipping the requisite bits,
1441 * and filling the rest of opad with the requisite constant. */
1442 for( i = 0; i < key_length; i++ )
1443 opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1444 memset( opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001445
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02001446 status = psa_hash_setup( &operation->ctx.hmac.hash_ctx,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001447 PSA_ALG_HMAC_HASH( alg ) );
1448 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001449 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001450
1451 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, ipad,
1452 block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001453
1454cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001455 mbedtls_zeroize( ipad, key_length );
1456 /* opad is in the context. It needs to stay in memory if this function
1457 * succeeds, and it will be wiped by psa_mac_abort() called from
Gilles Peskine89167cb2018-07-08 20:12:23 +02001458 * psa_mac_setup in the error case. */
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001459
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001460 return( status );
1461}
Gilles Peskine248051a2018-06-20 16:09:38 +02001462#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001463
Gilles Peskine89167cb2018-07-08 20:12:23 +02001464static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1465 psa_key_slot_t key,
1466 psa_algorithm_t alg,
1467 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001468{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001469 psa_status_t status;
1470 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001471 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001472 psa_key_usage_t usage =
1473 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001474
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001475 status = psa_mac_init( operation, alg );
1476 if( status != PSA_SUCCESS )
1477 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001478 if( is_sign )
1479 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001480
Gilles Peskine89167cb2018-07-08 20:12:23 +02001481 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001482 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001483 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001484 key_bits = psa_get_key_bits( slot );
1485
Gilles Peskine8c9def32018-02-08 10:02:12 +01001486#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001487 if( alg == PSA_ALG_CMAC )
1488 {
1489 const mbedtls_cipher_info_t *cipher_info =
1490 mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
1491 int ret;
1492 if( cipher_info == NULL )
1493 {
1494 status = PSA_ERROR_NOT_SUPPORTED;
1495 goto exit;
1496 }
1497 operation->mac_size = cipher_info->block_size;
1498 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1499 status = mbedtls_to_psa_error( ret );
1500 }
1501 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001502#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001503#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001504 if( PSA_ALG_IS_HMAC( alg ) )
1505 {
1506 status = psa_hmac_setup( operation, slot->type, slot, alg );
1507 }
1508 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001509#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001510 {
1511 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001512 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001513
Gilles Peskinefbfac682018-07-08 20:51:54 +02001514exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001515 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001516 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001517 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001518 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001519 else
1520 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001521 operation->key_set = 1;
1522 }
1523 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001524}
1525
Gilles Peskine89167cb2018-07-08 20:12:23 +02001526psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1527 psa_key_slot_t key,
1528 psa_algorithm_t alg )
1529{
1530 return( psa_mac_setup( operation, key, alg, 1 ) );
1531}
1532
1533psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1534 psa_key_slot_t key,
1535 psa_algorithm_t alg )
1536{
1537 return( psa_mac_setup( operation, key, alg, 0 ) );
1538}
1539
Gilles Peskine8c9def32018-02-08 10:02:12 +01001540psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1541 const uint8_t *input,
1542 size_t input_length )
1543{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001544 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001545 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001546 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001547 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001548 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001549 operation->has_input = 1;
1550
Gilles Peskine8c9def32018-02-08 10:02:12 +01001551#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001552 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001553 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001554 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1555 input, input_length );
1556 status = mbedtls_to_psa_error( ret );
1557 }
1558 else
1559#endif /* MBEDTLS_CMAC_C */
1560#if defined(MBEDTLS_MD_C)
1561 if( PSA_ALG_IS_HMAC( operation->alg ) )
1562 {
1563 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1564 input_length );
1565 }
1566 else
1567#endif /* MBEDTLS_MD_C */
1568 {
1569 /* This shouldn't happen if `operation` was initialized by
1570 * a setup function. */
1571 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001572 }
1573
Gilles Peskinefbfac682018-07-08 20:51:54 +02001574cleanup:
1575 if( status != PSA_SUCCESS )
1576 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001577 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001578}
1579
mohammad16036df908f2018-04-02 08:34:15 -07001580static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001581 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001582 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001583{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001584 psa_status_t status;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001585
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001586 if( ! operation->key_set )
1587 return( PSA_ERROR_BAD_STATE );
1588 if( operation->iv_required && ! operation->iv_set )
1589 return( PSA_ERROR_BAD_STATE );
1590
Gilles Peskine8c9def32018-02-08 10:02:12 +01001591 if( mac_size < operation->mac_size )
1592 return( PSA_ERROR_BUFFER_TOO_SMALL );
1593
Gilles Peskine8c9def32018-02-08 10:02:12 +01001594#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001595 if( operation->alg == PSA_ALG_CMAC )
1596 {
1597 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1598 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001599 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001600 else
1601#endif /* MBEDTLS_CMAC_C */
1602#if defined(MBEDTLS_MD_C)
1603 if( PSA_ALG_IS_HMAC( operation->alg ) )
1604 {
1605 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1606 unsigned char *opad = operation->ctx.hmac.opad;
1607 size_t hash_size = 0;
1608 size_t block_size =
1609 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001610
Gilles Peskinefbfac682018-07-08 20:51:54 +02001611 if( block_size == 0 )
1612 return( PSA_ERROR_NOT_SUPPORTED );
1613
1614 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, tmp,
1615 sizeof( tmp ), &hash_size );
1616 if( status != PSA_SUCCESS )
1617 return( status );
1618 /* From here on, tmp needs to be wiped. */
1619
1620 status = psa_hash_setup( &operation->ctx.hmac.hash_ctx,
1621 PSA_ALG_HMAC_HASH( operation->alg ) );
1622 if( status != PSA_SUCCESS )
1623 goto hmac_cleanup;
1624
1625 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, opad,
1626 block_size );
1627 if( status != PSA_SUCCESS )
1628 goto hmac_cleanup;
1629
1630 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, tmp,
1631 hash_size );
1632 if( status != PSA_SUCCESS )
1633 goto hmac_cleanup;
1634
1635 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, mac,
1636 mac_size, &hash_size );
1637 hmac_cleanup:
1638 mbedtls_zeroize( tmp, hash_size );
1639 return( status );
1640 }
1641 else
1642#endif /* MBEDTLS_MD_C */
1643 {
1644 /* This shouldn't happen if `operation` was initialized by
1645 * a setup function. */
1646 return( PSA_ERROR_BAD_STATE );
1647 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001648}
1649
Gilles Peskineacd4be32018-07-08 19:56:25 +02001650psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1651 uint8_t *mac,
1652 size_t mac_size,
1653 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001654{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001655 psa_status_t status;
1656
1657 /* Fill the output buffer with something that isn't a valid mac
1658 * (barring an attack on the mac and deliberately-crafted input),
1659 * in case the caller doesn't check the return status properly. */
1660 *mac_length = mac_size;
1661 /* If mac_size is 0 then mac may be NULL and then the
1662 * call to memset would have undefined behavior. */
1663 if( mac_size != 0 )
1664 memset( mac, '!', mac_size );
1665
Gilles Peskine89167cb2018-07-08 20:12:23 +02001666 if( ! operation->is_sign )
1667 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001668 status = PSA_ERROR_BAD_STATE;
1669 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001670 }
mohammad16036df908f2018-04-02 08:34:15 -07001671
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001672 status = psa_mac_finish_internal( operation, mac, mac_size );
1673
1674cleanup:
1675 if( status == PSA_SUCCESS )
1676 {
1677 status = psa_mac_abort( operation );
1678 if( status == PSA_SUCCESS )
1679 *mac_length = operation->mac_size;
1680 else
1681 memset( mac, '!', mac_size );
1682 }
1683 else
1684 psa_mac_abort( operation );
1685 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001686}
1687
Gilles Peskineacd4be32018-07-08 19:56:25 +02001688psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1689 const uint8_t *mac,
1690 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001691{
Gilles Peskine828ed142018-06-18 23:25:51 +02001692 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001693 psa_status_t status;
1694
Gilles Peskine89167cb2018-07-08 20:12:23 +02001695 if( operation->is_sign )
1696 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001697 status = PSA_ERROR_BAD_STATE;
1698 goto cleanup;
1699 }
1700 if( operation->mac_size != mac_length )
1701 {
1702 status = PSA_ERROR_INVALID_SIGNATURE;
1703 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001704 }
mohammad16036df908f2018-04-02 08:34:15 -07001705
1706 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001707 actual_mac, sizeof( actual_mac ) );
1708
1709 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1710 status = PSA_ERROR_INVALID_SIGNATURE;
1711
1712cleanup:
1713 if( status == PSA_SUCCESS )
1714 status = psa_mac_abort( operation );
1715 else
1716 psa_mac_abort( operation );
1717
1718 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001719}
1720
1721
Gilles Peskine20035e32018-02-03 22:44:14 +01001722
Gilles Peskine20035e32018-02-03 22:44:14 +01001723/****************************************************************/
1724/* Asymmetric cryptography */
1725/****************************************************************/
1726
Gilles Peskine2b450e32018-06-27 15:42:46 +02001727#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001728/* Decode the hash algorithm from alg and store the mbedtls encoding in
1729 * md_alg. Verify that the hash length is consistent. */
1730static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1731 size_t hash_length,
1732 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001733{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001734 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001735 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1736 *md_alg = hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1737 if( *md_alg == MBEDTLS_MD_NONE )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001738 {
1739#if SIZE_MAX > UINT_MAX
Gilles Peskine61b91d42018-06-08 16:09:36 +02001740 if( hash_length > UINT_MAX )
1741 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001742#endif
1743 }
1744 else
1745 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001746 if( mbedtls_md_get_size( md_info ) != hash_length )
1747 return( PSA_ERROR_INVALID_ARGUMENT );
1748 if( md_info == NULL )
1749 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001750 }
Gilles Peskine61b91d42018-06-08 16:09:36 +02001751 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001752}
1753
Gilles Peskine2b450e32018-06-27 15:42:46 +02001754static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1755 psa_algorithm_t alg,
1756 const uint8_t *hash,
1757 size_t hash_length,
1758 uint8_t *signature,
1759 size_t signature_size,
1760 size_t *signature_length )
1761{
1762 psa_status_t status;
1763 int ret;
1764 mbedtls_md_type_t md_alg;
1765
1766 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1767 if( status != PSA_SUCCESS )
1768 return( status );
1769
Gilles Peskine630a18a2018-06-29 17:49:35 +02001770 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001771 return( PSA_ERROR_BUFFER_TOO_SMALL );
1772
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001773 /* The Mbed TLS RSA module uses an unsigned int for hash_length. See if
1774 * hash_length will fit and return an error if it doesn't. */
1775#if defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21)
1776#if SIZE_MAX > UINT_MAX
1777 if( hash_length > UINT_MAX )
1778 return( PSA_ERROR_NOT_SUPPORTED );
1779#endif
1780#endif
1781
Gilles Peskine2b450e32018-06-27 15:42:46 +02001782#if defined(MBEDTLS_PKCS1_V15)
1783 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1784 {
1785 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1786 MBEDTLS_MD_NONE );
1787 ret = mbedtls_rsa_pkcs1_sign( rsa,
1788 mbedtls_ctr_drbg_random,
1789 &global_data.ctr_drbg,
1790 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001791 md_alg,
1792 (unsigned int) hash_length,
1793 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001794 signature );
1795 }
1796 else
1797#endif /* MBEDTLS_PKCS1_V15 */
1798#if defined(MBEDTLS_PKCS1_V21)
1799 if( PSA_ALG_IS_RSA_PSS( alg ) )
1800 {
1801 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1802 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1803 mbedtls_ctr_drbg_random,
1804 &global_data.ctr_drbg,
1805 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001806 md_alg,
1807 (unsigned int) hash_length,
1808 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001809 signature );
1810 }
1811 else
1812#endif /* MBEDTLS_PKCS1_V21 */
1813 {
1814 return( PSA_ERROR_INVALID_ARGUMENT );
1815 }
1816
1817 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02001818 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02001819 return( mbedtls_to_psa_error( ret ) );
1820}
1821
1822static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1823 psa_algorithm_t alg,
1824 const uint8_t *hash,
1825 size_t hash_length,
1826 const uint8_t *signature,
1827 size_t signature_length )
1828{
1829 psa_status_t status;
1830 int ret;
1831 mbedtls_md_type_t md_alg;
1832
1833 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1834 if( status != PSA_SUCCESS )
1835 return( status );
1836
Gilles Peskine630a18a2018-06-29 17:49:35 +02001837 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001838 return( PSA_ERROR_BUFFER_TOO_SMALL );
1839
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001840#if defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21)
1841#if SIZE_MAX > UINT_MAX
1842 /* The Mbed TLS RSA module uses an unsigned int for hash_length. See if
1843 * hash_length will fit and return an error if it doesn't. */
1844 if( hash_length > UINT_MAX )
1845 return( PSA_ERROR_NOT_SUPPORTED );
1846#endif
1847#endif
1848
Gilles Peskine2b450e32018-06-27 15:42:46 +02001849#if defined(MBEDTLS_PKCS1_V15)
1850 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1851 {
1852 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1853 MBEDTLS_MD_NONE );
1854 ret = mbedtls_rsa_pkcs1_verify( rsa,
1855 mbedtls_ctr_drbg_random,
1856 &global_data.ctr_drbg,
1857 MBEDTLS_RSA_PUBLIC,
1858 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001859 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001860 hash,
1861 signature );
1862 }
1863 else
1864#endif /* MBEDTLS_PKCS1_V15 */
1865#if defined(MBEDTLS_PKCS1_V21)
1866 if( PSA_ALG_IS_RSA_PSS( alg ) )
1867 {
1868 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1869 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1870 mbedtls_ctr_drbg_random,
1871 &global_data.ctr_drbg,
1872 MBEDTLS_RSA_PUBLIC,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001873 md_alg,
1874 (unsigned int) hash_length,
1875 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001876 signature );
1877 }
1878 else
1879#endif /* MBEDTLS_PKCS1_V21 */
1880 {
1881 return( PSA_ERROR_INVALID_ARGUMENT );
1882 }
1883 return( mbedtls_to_psa_error( ret ) );
1884}
1885#endif /* MBEDTLS_RSA_C */
1886
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001887#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001888/* `ecp` cannot be const because `ecp->grp` needs to be non-const
1889 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
1890 * (even though these functions don't modify it). */
1891static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
1892 psa_algorithm_t alg,
1893 const uint8_t *hash,
1894 size_t hash_length,
1895 uint8_t *signature,
1896 size_t signature_size,
1897 size_t *signature_length )
1898{
1899 int ret;
1900 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001901 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001902 mbedtls_mpi_init( &r );
1903 mbedtls_mpi_init( &s );
1904
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001905 if( signature_size < 2 * curve_bytes )
1906 {
1907 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
1908 goto cleanup;
1909 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001910
1911 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
1912 {
1913 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
1914 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1915 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
1916 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
1917 hash, hash_length,
1918 md_alg ) );
1919 }
1920 else
1921 {
1922 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
1923 hash, hash_length,
1924 mbedtls_ctr_drbg_random,
1925 &global_data.ctr_drbg ) );
1926 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001927
1928 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
1929 signature,
1930 curve_bytes ) );
1931 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
1932 signature + curve_bytes,
1933 curve_bytes ) );
1934
1935cleanup:
1936 mbedtls_mpi_free( &r );
1937 mbedtls_mpi_free( &s );
1938 if( ret == 0 )
1939 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001940 return( mbedtls_to_psa_error( ret ) );
1941}
1942
1943static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
1944 const uint8_t *hash,
1945 size_t hash_length,
1946 const uint8_t *signature,
1947 size_t signature_length )
1948{
1949 int ret;
1950 mbedtls_mpi r, s;
1951 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
1952 mbedtls_mpi_init( &r );
1953 mbedtls_mpi_init( &s );
1954
1955 if( signature_length != 2 * curve_bytes )
1956 return( PSA_ERROR_INVALID_SIGNATURE );
1957
1958 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
1959 signature,
1960 curve_bytes ) );
1961 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
1962 signature + curve_bytes,
1963 curve_bytes ) );
1964
1965 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
1966 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001967
1968cleanup:
1969 mbedtls_mpi_free( &r );
1970 mbedtls_mpi_free( &s );
1971 return( mbedtls_to_psa_error( ret ) );
1972}
1973#endif /* MBEDTLS_ECDSA_C */
1974
Gilles Peskine61b91d42018-06-08 16:09:36 +02001975psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
1976 psa_algorithm_t alg,
1977 const uint8_t *hash,
1978 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02001979 uint8_t *signature,
1980 size_t signature_size,
1981 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01001982{
1983 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001984 psa_status_t status;
1985
1986 *signature_length = signature_size;
1987
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001988 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
1989 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001990 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01001991 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001992 {
1993 status = PSA_ERROR_INVALID_ARGUMENT;
1994 goto exit;
1995 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001996
Gilles Peskine20035e32018-02-03 22:44:14 +01001997#if defined(MBEDTLS_RSA_C)
1998 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1999 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002000 status = psa_rsa_sign( slot->data.rsa,
2001 alg,
2002 hash, hash_length,
2003 signature, signature_size,
2004 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002005 }
2006 else
2007#endif /* defined(MBEDTLS_RSA_C) */
2008#if defined(MBEDTLS_ECP_C)
2009 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2010 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002011#if defined(MBEDTLS_ECDSA_C)
2012 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002013 status = psa_ecdsa_sign( slot->data.ecp,
2014 alg,
2015 hash, hash_length,
2016 signature, signature_size,
2017 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002018 else
2019#endif /* defined(MBEDTLS_ECDSA_C) */
2020 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002021 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002022 }
itayzafrir5c753392018-05-08 11:18:38 +03002023 }
2024 else
2025#endif /* defined(MBEDTLS_ECP_C) */
2026 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002027 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002028 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002029
2030exit:
2031 /* Fill the unused part of the output buffer (the whole buffer on error,
2032 * the trailing part on success) with something that isn't a valid mac
2033 * (barring an attack on the mac and deliberately-crafted input),
2034 * in case the caller doesn't check the return status properly. */
2035 if( status == PSA_SUCCESS )
2036 memset( signature + *signature_length, '!',
2037 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002038 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002039 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002040 /* If signature_size is 0 then we have nothing to do. We must not call
2041 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002042 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002043}
2044
2045psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2046 psa_algorithm_t alg,
2047 const uint8_t *hash,
2048 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002049 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002050 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002051{
2052 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002053 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002054
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002055 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2056 if( status != PSA_SUCCESS )
2057 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002058
Gilles Peskine61b91d42018-06-08 16:09:36 +02002059#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002060 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002061 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002062 return( psa_rsa_verify( slot->data.rsa,
2063 alg,
2064 hash, hash_length,
2065 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002066 }
2067 else
2068#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002069#if defined(MBEDTLS_ECP_C)
2070 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2071 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002072#if defined(MBEDTLS_ECDSA_C)
2073 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002074 return( psa_ecdsa_verify( slot->data.ecp,
2075 hash, hash_length,
2076 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002077 else
2078#endif /* defined(MBEDTLS_ECDSA_C) */
2079 {
2080 return( PSA_ERROR_INVALID_ARGUMENT );
2081 }
itayzafrir5c753392018-05-08 11:18:38 +03002082 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002083 else
2084#endif /* defined(MBEDTLS_ECP_C) */
2085 {
2086 return( PSA_ERROR_NOT_SUPPORTED );
2087 }
2088}
2089
Gilles Peskine61b91d42018-06-08 16:09:36 +02002090psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2091 psa_algorithm_t alg,
2092 const uint8_t *input,
2093 size_t input_length,
2094 const uint8_t *salt,
2095 size_t salt_length,
2096 uint8_t *output,
2097 size_t output_size,
2098 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002099{
2100 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002101 psa_status_t status;
2102
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002103 (void) salt;
2104 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002105 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002106
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002107 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2108 if( status != PSA_SUCCESS )
2109 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002110 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2111 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002112 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002113
2114#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002115 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002116 {
2117 mbedtls_rsa_context *rsa = slot->data.rsa;
2118 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002119 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002120 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002121#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002122 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002123 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002124 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2125 mbedtls_ctr_drbg_random,
2126 &global_data.ctr_drbg,
2127 MBEDTLS_RSA_PUBLIC,
2128 input_length,
2129 input,
2130 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002131 }
2132 else
2133#endif /* MBEDTLS_PKCS1_V15 */
2134#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002135 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002136 {
2137 return( PSA_ERROR_NOT_SUPPORTED );
2138 }
2139 else
2140#endif /* MBEDTLS_PKCS1_V21 */
2141 {
2142 return( PSA_ERROR_INVALID_ARGUMENT );
2143 }
2144 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002145 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002146 return( mbedtls_to_psa_error( ret ) );
2147 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002148 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002149#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002150 {
2151 return( PSA_ERROR_NOT_SUPPORTED );
2152 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002153}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002154
Gilles Peskine61b91d42018-06-08 16:09:36 +02002155psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2156 psa_algorithm_t alg,
2157 const uint8_t *input,
2158 size_t input_length,
2159 const uint8_t *salt,
2160 size_t salt_length,
2161 uint8_t *output,
2162 size_t output_size,
2163 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002164{
2165 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002166 psa_status_t status;
2167
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002168 (void) salt;
2169 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002170 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002171
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002172 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2173 if( status != PSA_SUCCESS )
2174 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002175 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2176 return( PSA_ERROR_INVALID_ARGUMENT );
2177
2178#if defined(MBEDTLS_RSA_C)
2179 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2180 {
2181 mbedtls_rsa_context *rsa = slot->data.rsa;
2182 int ret;
2183
Gilles Peskine630a18a2018-06-29 17:49:35 +02002184 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002185 return( PSA_ERROR_INVALID_ARGUMENT );
2186
2187#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002188 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002189 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002190 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2191 mbedtls_ctr_drbg_random,
2192 &global_data.ctr_drbg,
2193 MBEDTLS_RSA_PRIVATE,
2194 output_length,
2195 input,
2196 output,
2197 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002198 }
2199 else
2200#endif /* MBEDTLS_PKCS1_V15 */
2201#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002202 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002203 {
2204 return( PSA_ERROR_NOT_SUPPORTED );
2205 }
2206 else
2207#endif /* MBEDTLS_PKCS1_V21 */
2208 {
2209 return( PSA_ERROR_INVALID_ARGUMENT );
2210 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002211
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002212 return( mbedtls_to_psa_error( ret ) );
2213 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002214 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002215#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002216 {
2217 return( PSA_ERROR_NOT_SUPPORTED );
2218 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002219}
Gilles Peskine20035e32018-02-03 22:44:14 +01002220
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002221
2222
mohammad1603503973b2018-03-12 15:59:30 +02002223/****************************************************************/
2224/* Symmetric cryptography */
2225/****************************************************************/
2226
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002227/* Initialize the cipher operation structure. Once this function has been
2228 * called, psa_cipher_abort can run and will do the right thing. */
2229static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2230 psa_algorithm_t alg )
2231{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002232 if( ! PSA_ALG_IS_CIPHER( alg ) )
2233 {
2234 memset( operation, 0, sizeof( *operation ) );
2235 return( PSA_ERROR_INVALID_ARGUMENT );
2236 }
2237
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002238 operation->alg = alg;
2239 operation->key_set = 0;
2240 operation->iv_set = 0;
2241 operation->iv_required = 1;
2242 operation->iv_size = 0;
2243 operation->block_size = 0;
2244 mbedtls_cipher_init( &operation->ctx.cipher );
2245 return( PSA_SUCCESS );
2246}
2247
Gilles Peskinee553c652018-06-04 16:22:46 +02002248static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2249 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002250 psa_algorithm_t alg,
2251 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002252{
2253 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2254 psa_status_t status;
2255 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002256 size_t key_bits;
2257 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002258 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2259 PSA_KEY_USAGE_ENCRYPT :
2260 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002261
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002262 status = psa_cipher_init( operation, alg );
2263 if( status != PSA_SUCCESS )
2264 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002265
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002266 status = psa_get_key_from_slot( key, &slot, usage, alg);
2267 if( status != PSA_SUCCESS )
2268 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002269 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002270
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002271 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002272 if( cipher_info == NULL )
2273 return( PSA_ERROR_NOT_SUPPORTED );
2274
mohammad1603503973b2018-03-12 15:59:30 +02002275 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002276 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002277 {
2278 psa_cipher_abort( operation );
2279 return( mbedtls_to_psa_error( ret ) );
2280 }
2281
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002282#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002283 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002284 {
2285 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2286 unsigned char keys[24];
2287 memcpy( keys, slot->data.raw.data, 16 );
2288 memcpy( keys + 16, slot->data.raw.data, 8 );
2289 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2290 keys,
2291 192, cipher_operation );
2292 }
2293 else
2294#endif
2295 {
2296 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2297 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002298 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002299 }
Moran Peker41deec42018-04-04 15:43:05 +03002300 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002301 {
2302 psa_cipher_abort( operation );
2303 return( mbedtls_to_psa_error( ret ) );
2304 }
2305
mohammad16038481e742018-03-18 13:57:31 +02002306#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002307 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002308 {
Gilles Peskine53514202018-06-06 15:11:46 +02002309 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2310 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002311
Moran Pekera28258c2018-05-29 16:25:04 +03002312 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002313 {
2314 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2315 mode = MBEDTLS_PADDING_PKCS7;
2316 break;
2317 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2318 mode = MBEDTLS_PADDING_NONE;
2319 break;
2320 default:
Moran Pekerae382792018-05-31 14:06:17 +03002321 psa_cipher_abort( operation );
2322 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002323 }
2324 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002325 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002326 {
2327 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002328 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002329 }
mohammad16038481e742018-03-18 13:57:31 +02002330 }
2331#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2332
mohammad1603503973b2018-03-12 15:59:30 +02002333 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002334 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002335 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) :
Gilles Peskine7e928852018-06-04 16:23:10 +02002336 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002337 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002338 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002339 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002340 }
mohammad1603503973b2018-03-12 15:59:30 +02002341
Moran Peker395db872018-05-31 14:07:14 +03002342 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002343}
2344
Gilles Peskinefe119512018-07-08 21:39:34 +02002345psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2346 psa_key_slot_t key,
2347 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002348{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002349 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002350}
2351
Gilles Peskinefe119512018-07-08 21:39:34 +02002352psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2353 psa_key_slot_t key,
2354 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002355{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002356 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002357}
2358
Gilles Peskinefe119512018-07-08 21:39:34 +02002359psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2360 unsigned char *iv,
2361 size_t iv_size,
2362 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002363{
Moran Peker41deec42018-04-04 15:43:05 +03002364 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002365 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002366 return( PSA_ERROR_BAD_STATE );
2367 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002368 {
Moran Peker41deec42018-04-04 15:43:05 +03002369 ret = PSA_ERROR_BUFFER_TOO_SMALL;
2370 goto exit;
2371 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002372 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2373 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002374 if( ret != 0 )
2375 {
2376 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002377 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002378 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002379
mohammad16038481e742018-03-18 13:57:31 +02002380 *iv_length = operation->iv_size;
Gilles Peskinefe119512018-07-08 21:39:34 +02002381 ret = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002382
Moran Peker395db872018-05-31 14:07:14 +03002383exit:
2384 if( ret != PSA_SUCCESS )
2385 psa_cipher_abort( operation );
2386 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02002387}
2388
Gilles Peskinefe119512018-07-08 21:39:34 +02002389psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2390 const unsigned char *iv,
2391 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002392{
Moran Peker41deec42018-04-04 15:43:05 +03002393 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002394 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002395 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03002396 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002397 {
Moran Pekerae382792018-05-31 14:06:17 +03002398 psa_cipher_abort( operation );
2399 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03002400 }
mohammad1603503973b2018-03-12 15:59:30 +02002401 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002402 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002403 {
2404 psa_cipher_abort( operation );
2405 return( mbedtls_to_psa_error( ret ) );
2406 }
2407
2408 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02002409
Moran Peker395db872018-05-31 14:07:14 +03002410 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002411}
2412
Gilles Peskinee553c652018-06-04 16:22:46 +02002413psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2414 const uint8_t *input,
2415 size_t input_length,
2416 unsigned char *output,
2417 size_t output_size,
2418 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002419{
2420 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002421 size_t expected_output_size;
2422 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2423 {
2424 /* Take the unprocessed partial block left over from previous
2425 * update calls, if any, plus the input to this call. Remove
2426 * the last partial block, if any. You get the data that will be
2427 * output in this call. */
2428 expected_output_size =
2429 ( operation->ctx.cipher.unprocessed_len + input_length )
2430 / operation->block_size * operation->block_size;
2431 }
2432 else
2433 {
2434 expected_output_size = input_length;
2435 }
2436 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03002437 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02002438
mohammad1603503973b2018-03-12 15:59:30 +02002439 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002440 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002441 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002442 {
2443 psa_cipher_abort( operation );
2444 return( mbedtls_to_psa_error( ret ) );
2445 }
2446
Moran Peker395db872018-05-31 14:07:14 +03002447 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002448}
2449
Gilles Peskinee553c652018-06-04 16:22:46 +02002450psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2451 uint8_t *output,
2452 size_t output_size,
2453 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002454{
Janos Follath315b51c2018-07-09 16:04:51 +01002455 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2456 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002457 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002458
mohammad1603503973b2018-03-12 15:59:30 +02002459 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002460 {
Janos Follath315b51c2018-07-09 16:04:51 +01002461 status = PSA_ERROR_BAD_STATE;
2462 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002463 }
2464 if( operation->iv_required && ! operation->iv_set )
2465 {
Janos Follath315b51c2018-07-09 16:04:51 +01002466 status = PSA_ERROR_BAD_STATE;
2467 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002468 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002469 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2470 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002471 {
Gilles Peskine53514202018-06-06 15:11:46 +02002472 psa_algorithm_t padding_mode =
2473 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002474 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002475 {
Janos Follath315b51c2018-07-09 16:04:51 +01002476 status = PSA_ERROR_TAMPERING_DETECTED;
2477 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002478 }
Gilles Peskine53514202018-06-06 15:11:46 +02002479 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002480 {
2481 if( operation->ctx.cipher.unprocessed_len != 0 )
2482 {
Janos Follath315b51c2018-07-09 16:04:51 +01002483 status = PSA_ERROR_INVALID_ARGUMENT;
2484 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002485 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002486 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002487 }
2488
Janos Follath315b51c2018-07-09 16:04:51 +01002489 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2490 temp_output_buffer,
2491 output_length );
2492 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002493 {
Janos Follath315b51c2018-07-09 16:04:51 +01002494 status = mbedtls_to_psa_error( cipher_ret );
2495 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002496 }
Janos Follath315b51c2018-07-09 16:04:51 +01002497
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002498 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002499 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002500 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002501 memcpy( output, temp_output_buffer, *output_length );
2502 else
2503 {
Janos Follath315b51c2018-07-09 16:04:51 +01002504 status = PSA_ERROR_BUFFER_TOO_SMALL;
2505 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002506 }
mohammad1603503973b2018-03-12 15:59:30 +02002507
Janos Follath279ab8e2018-07-09 16:13:21 +01002508 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002509 status = psa_cipher_abort( operation );
2510
2511 return( status );
2512
2513error:
2514
2515 *output_length = 0;
2516
Janos Follath279ab8e2018-07-09 16:13:21 +01002517 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002518 (void) psa_cipher_abort( operation );
2519
2520 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002521}
2522
Gilles Peskinee553c652018-06-04 16:22:46 +02002523psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2524{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002525 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002526 {
2527 /* The object has (apparently) been initialized but it is not
2528 * in use. It's ok to call abort on such an object, and there's
2529 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002530 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002531 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002532
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002533 /* Sanity check (shouldn't happen: operation->alg should
2534 * always have been initialized to a valid value). */
2535 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2536 return( PSA_ERROR_BAD_STATE );
2537
mohammad1603503973b2018-03-12 15:59:30 +02002538 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002539
Moran Peker41deec42018-04-04 15:43:05 +03002540 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002541 operation->key_set = 0;
2542 operation->iv_set = 0;
2543 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002544 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002545 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002546
Moran Peker395db872018-05-31 14:07:14 +03002547 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002548}
2549
Gilles Peskinea0655c32018-04-30 17:06:50 +02002550
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002551
mohammad16038cc1cee2018-03-28 01:21:33 +03002552/****************************************************************/
2553/* Key Policy */
2554/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002555
mohammad160327010052018-07-03 13:16:15 +03002556#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002557void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002558{
Gilles Peskine803ce742018-06-18 16:07:14 +02002559 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002560}
2561
Gilles Peskine2d277862018-06-18 15:41:12 +02002562void psa_key_policy_set_usage( psa_key_policy_t *policy,
2563 psa_key_usage_t usage,
2564 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002565{
mohammad16034eed7572018-03-28 05:14:59 -07002566 policy->usage = usage;
2567 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002568}
2569
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002570psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002571{
mohammad16036df908f2018-04-02 08:34:15 -07002572 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002573}
2574
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002575psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002576{
mohammad16036df908f2018-04-02 08:34:15 -07002577 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002578}
mohammad160327010052018-07-03 13:16:15 +03002579#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002580
Gilles Peskine2d277862018-06-18 15:41:12 +02002581psa_status_t psa_set_key_policy( psa_key_slot_t key,
2582 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002583{
2584 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002585 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002586
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002587 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002588 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002589
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002590 status = psa_get_empty_key_slot( key, &slot );
2591 if( status != PSA_SUCCESS )
2592 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002593
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002594 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2595 PSA_KEY_USAGE_ENCRYPT |
2596 PSA_KEY_USAGE_DECRYPT |
2597 PSA_KEY_USAGE_SIGN |
2598 PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002599 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002600
mohammad16036df908f2018-04-02 08:34:15 -07002601 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002602
2603 return( PSA_SUCCESS );
2604}
2605
Gilles Peskine2d277862018-06-18 15:41:12 +02002606psa_status_t psa_get_key_policy( psa_key_slot_t key,
2607 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002608{
2609 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002610 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002611
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002612 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002613 return( PSA_ERROR_INVALID_ARGUMENT );
2614
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002615 status = psa_get_key_slot( key, &slot );
2616 if( status != PSA_SUCCESS )
2617 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002618
mohammad16036df908f2018-04-02 08:34:15 -07002619 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002620
2621 return( PSA_SUCCESS );
2622}
Gilles Peskine20035e32018-02-03 22:44:14 +01002623
Gilles Peskinea0655c32018-04-30 17:06:50 +02002624
2625
mohammad1603804cd712018-03-20 22:44:08 +02002626/****************************************************************/
2627/* Key Lifetime */
2628/****************************************************************/
2629
Gilles Peskine2d277862018-06-18 15:41:12 +02002630psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2631 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002632{
2633 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002634 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002635
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002636 status = psa_get_key_slot( key, &slot );
2637 if( status != PSA_SUCCESS )
2638 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002639
mohammad1603804cd712018-03-20 22:44:08 +02002640 *lifetime = slot->lifetime;
2641
2642 return( PSA_SUCCESS );
2643}
2644
Gilles Peskine2d277862018-06-18 15:41:12 +02002645psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002646 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002647{
2648 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002649 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002650
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002651 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2652 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002653 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2654 return( PSA_ERROR_INVALID_ARGUMENT );
2655
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002656 status = psa_get_empty_key_slot( key, &slot );
2657 if( status != PSA_SUCCESS )
2658 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002659
Moran Pekerd7326592018-05-29 16:56:39 +03002660 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002661 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002662
mohammad1603060ad8a2018-03-20 14:28:38 -07002663 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002664
2665 return( PSA_SUCCESS );
2666}
2667
Gilles Peskine20035e32018-02-03 22:44:14 +01002668
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002669
mohammad16035955c982018-04-26 00:53:03 +03002670/****************************************************************/
2671/* AEAD */
2672/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002673
mohammad16035955c982018-04-26 00:53:03 +03002674psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2675 psa_algorithm_t alg,
2676 const uint8_t *nonce,
2677 size_t nonce_length,
2678 const uint8_t *additional_data,
2679 size_t additional_data_length,
2680 const uint8_t *plaintext,
2681 size_t plaintext_length,
2682 uint8_t *ciphertext,
2683 size_t ciphertext_size,
2684 size_t *ciphertext_length )
2685{
2686 int ret;
2687 psa_status_t status;
2688 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002689 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002690 uint8_t *tag;
2691 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002692 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002693 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002694
mohammad1603f08a5502018-06-03 15:05:47 +03002695 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002696
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002697 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2698 if( status != PSA_SUCCESS )
2699 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002700 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002701
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002702 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002703 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002704 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002705 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002706
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002707 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002708 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002709 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002710
mohammad16035955c982018-04-26 00:53:03 +03002711 if( alg == PSA_ALG_GCM )
2712 {
2713 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002714 tag_length = 16;
2715
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002716 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002717 return( PSA_ERROR_INVALID_ARGUMENT );
2718
mohammad160315223a82018-06-03 17:19:55 +03002719 //make sure we have place to hold the tag in the ciphertext buffer
2720 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002721 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002722
2723 //update the tag pointer to point to the end of the ciphertext_length
2724 tag = ciphertext + plaintext_length;
2725
mohammad16035955c982018-04-26 00:53:03 +03002726 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002727 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002728 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002729 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002730 if( ret != 0 )
2731 {
2732 mbedtls_gcm_free( &gcm );
2733 return( mbedtls_to_psa_error( ret ) );
2734 }
2735 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002736 plaintext_length, nonce,
2737 nonce_length, additional_data,
2738 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002739 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002740 mbedtls_gcm_free( &gcm );
2741 }
2742 else if( alg == PSA_ALG_CCM )
2743 {
2744 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002745 tag_length = 16;
2746
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002747 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002748 return( PSA_ERROR_INVALID_ARGUMENT );
2749
mohammad160347ddf3d2018-04-26 01:11:21 +03002750 if( nonce_length < 7 || nonce_length > 13 )
2751 return( PSA_ERROR_INVALID_ARGUMENT );
2752
mohammad160315223a82018-06-03 17:19:55 +03002753 //make sure we have place to hold the tag in the ciphertext buffer
2754 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002755 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002756
2757 //update the tag pointer to point to the end of the ciphertext_length
2758 tag = ciphertext + plaintext_length;
2759
mohammad16035955c982018-04-26 00:53:03 +03002760 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002761 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002762 slot->data.raw.data,
2763 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002764 if( ret != 0 )
2765 {
2766 mbedtls_ccm_free( &ccm );
2767 return( mbedtls_to_psa_error( ret ) );
2768 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002769 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002770 nonce, nonce_length,
2771 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002772 additional_data_length,
2773 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002774 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002775 mbedtls_ccm_free( &ccm );
2776 }
mohammad16035c8845f2018-05-09 05:40:09 -07002777 else
2778 {
mohammad1603554faad2018-06-03 15:07:38 +03002779 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002780 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002781
mohammad160315223a82018-06-03 17:19:55 +03002782 if( ret != 0 )
2783 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002784 /* If ciphertext_size is 0 then ciphertext may be NULL and then the
2785 * call to memset would have undefined behavior. */
2786 if( ciphertext_size != 0 )
2787 memset( ciphertext, 0, ciphertext_size );
mohammad160315223a82018-06-03 17:19:55 +03002788 return( mbedtls_to_psa_error( ret ) );
2789 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002790
mohammad160315223a82018-06-03 17:19:55 +03002791 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002792 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002793}
2794
Gilles Peskineee652a32018-06-01 19:23:52 +02002795/* Locate the tag in a ciphertext buffer containing the encrypted data
2796 * followed by the tag. Return the length of the part preceding the tag in
2797 * *plaintext_length. This is the size of the plaintext in modes where
2798 * the encrypted data has the same size as the plaintext, such as
2799 * CCM and GCM. */
2800static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2801 const uint8_t *ciphertext,
2802 size_t ciphertext_length,
2803 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002804 const uint8_t **p_tag )
2805{
2806 size_t payload_length;
2807 if( tag_length > ciphertext_length )
2808 return( PSA_ERROR_INVALID_ARGUMENT );
2809 payload_length = ciphertext_length - tag_length;
2810 if( payload_length > plaintext_size )
2811 return( PSA_ERROR_BUFFER_TOO_SMALL );
2812 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002813 return( PSA_SUCCESS );
2814}
2815
mohammad16035955c982018-04-26 00:53:03 +03002816psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2817 psa_algorithm_t alg,
2818 const uint8_t *nonce,
2819 size_t nonce_length,
2820 const uint8_t *additional_data,
2821 size_t additional_data_length,
2822 const uint8_t *ciphertext,
2823 size_t ciphertext_length,
2824 uint8_t *plaintext,
2825 size_t plaintext_size,
2826 size_t *plaintext_length )
2827{
2828 int ret;
2829 psa_status_t status;
2830 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002831 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002832 const uint8_t *tag;
2833 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002834 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002835 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002836
Gilles Peskineee652a32018-06-01 19:23:52 +02002837 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002838
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002839 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2840 if( status != PSA_SUCCESS )
2841 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002842 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002843
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002844 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002845 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002846 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002847 return( PSA_ERROR_NOT_SUPPORTED );
2848
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002849 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002850 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002851 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002852
mohammad16035955c982018-04-26 00:53:03 +03002853 if( alg == PSA_ALG_GCM )
2854 {
2855 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002856
Gilles Peskineee652a32018-06-01 19:23:52 +02002857 tag_length = 16;
2858 status = psa_aead_unpadded_locate_tag( tag_length,
2859 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002860 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002861 if( status != PSA_SUCCESS )
2862 return( status );
2863
mohammad16035955c982018-04-26 00:53:03 +03002864 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002865 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002866 slot->data.raw.data,
2867 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002868 if( ret != 0 )
2869 {
2870 mbedtls_gcm_free( &gcm );
2871 return( mbedtls_to_psa_error( ret ) );
2872 }
mohammad16035955c982018-04-26 00:53:03 +03002873
Gilles Peskineee652a32018-06-01 19:23:52 +02002874 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002875 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002876 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002877 additional_data,
2878 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002879 tag, tag_length,
2880 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002881 mbedtls_gcm_free( &gcm );
2882 }
2883 else if( alg == PSA_ALG_CCM )
2884 {
2885 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002886
mohammad160347ddf3d2018-04-26 01:11:21 +03002887 if( nonce_length < 7 || nonce_length > 13 )
2888 return( PSA_ERROR_INVALID_ARGUMENT );
2889
mohammad16039375f842018-06-03 14:28:24 +03002890 tag_length = 16;
2891 status = psa_aead_unpadded_locate_tag( tag_length,
2892 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002893 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002894 if( status != PSA_SUCCESS )
2895 return( status );
2896
mohammad16035955c982018-04-26 00:53:03 +03002897 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002898 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002899 slot->data.raw.data,
2900 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002901 if( ret != 0 )
2902 {
2903 mbedtls_ccm_free( &ccm );
2904 return( mbedtls_to_psa_error( ret ) );
2905 }
mohammad160360a64d02018-06-03 17:20:42 +03002906 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002907 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002908 additional_data,
2909 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002910 ciphertext, plaintext,
2911 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002912 mbedtls_ccm_free( &ccm );
2913 }
mohammad160339574652018-06-01 04:39:53 -07002914 else
2915 {
mohammad1603554faad2018-06-03 15:07:38 +03002916 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07002917 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002918
Gilles Peskineee652a32018-06-01 19:23:52 +02002919 if( ret != 0 )
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002920 {
2921 /* If plaintext_size is 0 then plaintext may be NULL and then the
2922 * call to memset has undefined behavior. */
2923 if( plaintext_size != 0 )
2924 memset( plaintext, 0, plaintext_size );
2925 }
mohammad160360a64d02018-06-03 17:20:42 +03002926 else
2927 *plaintext_length = ciphertext_length - tag_length;
2928
Gilles Peskineee652a32018-06-01 19:23:52 +02002929 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03002930}
2931
Gilles Peskinea0655c32018-04-30 17:06:50 +02002932
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002933
Gilles Peskine20035e32018-02-03 22:44:14 +01002934/****************************************************************/
Gilles Peskine05d69892018-06-19 22:00:52 +02002935/* Key generation */
2936/****************************************************************/
2937
2938psa_status_t psa_generate_random( uint8_t *output,
2939 size_t output_size )
2940{
2941 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2942 output, output_size );
2943 return( mbedtls_to_psa_error( ret ) );
2944}
2945
2946psa_status_t psa_generate_key( psa_key_slot_t key,
2947 psa_key_type_t type,
2948 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002949 const void *extra,
2950 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02002951{
Gilles Peskine12313cd2018-06-20 00:20:32 +02002952 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002953 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02002954
Gilles Peskine53d991e2018-07-12 01:14:59 +02002955 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002956 return( PSA_ERROR_INVALID_ARGUMENT );
2957
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002958 status = psa_get_empty_key_slot( key, &slot );
2959 if( status != PSA_SUCCESS )
2960 return( status );
2961
Gilles Peskine48c0ea12018-06-21 14:15:31 +02002962 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002963 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002964 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02002965 if( status != PSA_SUCCESS )
2966 return( status );
2967 status = psa_generate_random( slot->data.raw.data,
2968 slot->data.raw.bytes );
2969 if( status != PSA_SUCCESS )
2970 {
2971 mbedtls_free( slot->data.raw.data );
2972 return( status );
2973 }
2974#if defined(MBEDTLS_DES_C)
2975 if( type == PSA_KEY_TYPE_DES )
2976 {
2977 mbedtls_des_key_set_parity( slot->data.raw.data );
2978 if( slot->data.raw.bytes >= 16 )
2979 mbedtls_des_key_set_parity( slot->data.raw.data + 8 );
2980 if( slot->data.raw.bytes == 24 )
2981 mbedtls_des_key_set_parity( slot->data.raw.data + 16 );
2982 }
2983#endif /* MBEDTLS_DES_C */
2984 }
2985 else
2986
2987#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
2988 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2989 {
2990 mbedtls_rsa_context *rsa;
2991 int ret;
2992 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02002993 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
2994 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02002995 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002996 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02002997 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02002998 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002999 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003000#if INT_MAX < 0xffffffff
3001 /* Check that the uint32_t value passed by the caller fits
3002 * in the range supported by this implementation. */
3003 if( p->e > INT_MAX )
3004 return( PSA_ERROR_NOT_SUPPORTED );
3005#endif
3006 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003007 }
3008 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3009 if( rsa == NULL )
3010 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3011 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3012 ret = mbedtls_rsa_gen_key( rsa,
3013 mbedtls_ctr_drbg_random,
3014 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003015 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003016 exponent );
3017 if( ret != 0 )
3018 {
3019 mbedtls_rsa_free( rsa );
3020 mbedtls_free( rsa );
3021 return( mbedtls_to_psa_error( ret ) );
3022 }
3023 slot->data.rsa = rsa;
3024 }
3025 else
3026#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3027
3028#if defined(MBEDTLS_ECP_C)
3029 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3030 {
3031 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3032 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3033 const mbedtls_ecp_curve_info *curve_info =
3034 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3035 mbedtls_ecp_keypair *ecp;
3036 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003037 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003038 return( PSA_ERROR_NOT_SUPPORTED );
3039 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3040 return( PSA_ERROR_NOT_SUPPORTED );
3041 if( curve_info->bit_size != bits )
3042 return( PSA_ERROR_INVALID_ARGUMENT );
3043 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3044 if( ecp == NULL )
3045 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3046 mbedtls_ecp_keypair_init( ecp );
3047 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3048 mbedtls_ctr_drbg_random,
3049 &global_data.ctr_drbg );
3050 if( ret != 0 )
3051 {
3052 mbedtls_ecp_keypair_free( ecp );
3053 mbedtls_free( ecp );
3054 return( mbedtls_to_psa_error( ret ) );
3055 }
3056 slot->data.ecp = ecp;
3057 }
3058 else
3059#endif /* MBEDTLS_ECP_C */
3060
3061 return( PSA_ERROR_NOT_SUPPORTED );
3062
3063 slot->type = type;
3064 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003065}
3066
3067
3068/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003069/* Module setup */
3070/****************************************************************/
3071
Gilles Peskinee59236f2018-01-27 23:32:46 +01003072void mbedtls_psa_crypto_free( void )
3073{
Jaeden Amero045bd502018-06-26 14:00:08 +01003074 psa_key_slot_t key;
Gilles Peskine828ed142018-06-18 23:25:51 +02003075 for( key = 1; key < PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003076 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003077 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3078 mbedtls_entropy_free( &global_data.entropy );
3079 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3080}
3081
3082psa_status_t psa_crypto_init( void )
3083{
3084 int ret;
3085 const unsigned char drbg_seed[] = "PSA";
3086
3087 if( global_data.initialized != 0 )
3088 return( PSA_SUCCESS );
3089
3090 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3091 mbedtls_entropy_init( &global_data.entropy );
3092 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3093
3094 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3095 mbedtls_entropy_func,
3096 &global_data.entropy,
3097 drbg_seed, sizeof( drbg_seed ) - 1 );
3098 if( ret != 0 )
3099 goto exit;
3100
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003101 global_data.initialized = 1;
3102
Gilles Peskinee59236f2018-01-27 23:32:46 +01003103exit:
3104 if( ret != 0 )
3105 mbedtls_psa_crypto_free( );
3106 return( mbedtls_to_psa_error( ret ) );
3107}
3108
3109#endif /* MBEDTLS_PSA_CRYPTO_C */