blob: b1555631ec8d0a29adc8f2ec2f04532bf15c5c71 [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 Peskinec66ea6a2018-02-03 22:43:28 +0100570 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
571 type == PSA_KEY_TYPE_RSA_KEYPAIR ||
572 PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573 {
574 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100575 mbedtls_pk_context pk;
576 mbedtls_pk_init( &pk );
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100577 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
578 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
579 else
580 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100581 if( ret != 0 )
582 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine969ac722018-01-28 18:16:59 +0100583 switch( mbedtls_pk_get_type( &pk ) )
584 {
585#if defined(MBEDTLS_RSA_C)
586 case MBEDTLS_PK_RSA:
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100587 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
588 type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200589 {
590 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
591 size_t bits = mbedtls_rsa_get_bitlen( rsa );
592 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
Gilles Peskine1ae05142018-06-30 17:46:59 +0200593 {
594 status = PSA_ERROR_NOT_SUPPORTED;
595 break;
596 }
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200597 slot->data.rsa = rsa;
598 }
Gilles Peskine969ac722018-01-28 18:16:59 +0100599 else
Gilles Peskinec648d692018-06-28 08:46:13 +0200600 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine969ac722018-01-28 18:16:59 +0100601 break;
602#endif /* MBEDTLS_RSA_C */
603#if defined(MBEDTLS_ECP_C)
604 case MBEDTLS_PK_ECKEY:
605 if( PSA_KEY_TYPE_IS_ECC( type ) )
606 {
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200607 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
608 psa_ecc_curve_t actual_curve =
609 mbedtls_ecc_group_to_psa( ecp->grp.id );
610 psa_ecc_curve_t expected_curve =
611 PSA_KEY_TYPE_GET_CURVE( type );
612 if( actual_curve != expected_curve )
Gilles Peskinec648d692018-06-28 08:46:13 +0200613 {
614 status = PSA_ERROR_INVALID_ARGUMENT;
615 break;
616 }
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200617 slot->data.ecp = ecp;
Gilles Peskine969ac722018-01-28 18:16:59 +0100618 }
619 else
Gilles Peskinec648d692018-06-28 08:46:13 +0200620 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine969ac722018-01-28 18:16:59 +0100621 break;
622#endif /* MBEDTLS_ECP_C */
623 default:
Gilles Peskinec648d692018-06-28 08:46:13 +0200624 status = PSA_ERROR_INVALID_ARGUMENT;
625 break;
626 }
627 /* Free the content of the pk object only on error. On success,
628 * the content of the object has been stored in the slot. */
629 if( status != PSA_SUCCESS )
630 {
631 mbedtls_pk_free( &pk );
632 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100633 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100634 }
635 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100636#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100637 {
638 return( PSA_ERROR_NOT_SUPPORTED );
639 }
640
641 slot->type = type;
642 return( PSA_SUCCESS );
643}
644
Gilles Peskine2d277862018-06-18 15:41:12 +0200645psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100646{
647 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200648 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100649
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200650 status = psa_get_key_slot( key, &slot );
651 if( status != PSA_SUCCESS )
652 return( status );
653
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100654 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200655 {
656 /* No key material to clean, but do zeroize the slot below to wipe
657 * metadata such as policies. */
658 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200659 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100660 {
661 mbedtls_free( slot->data.raw.data );
662 }
663 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100664#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100665 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
666 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100667 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100668 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100669 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100670 }
671 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100672#endif /* defined(MBEDTLS_RSA_C) */
673#if defined(MBEDTLS_ECP_C)
674 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
675 {
676 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100677 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100678 }
679 else
680#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100681 {
682 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100683 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100684 return( PSA_ERROR_TAMPERING_DETECTED );
685 }
686
687 mbedtls_zeroize( slot, sizeof( *slot ) );
688 return( PSA_SUCCESS );
689}
690
Gilles Peskineb870b182018-07-06 16:02:09 +0200691/* Return the size of the key in the given slot, in bits. */
692static size_t psa_get_key_bits( const key_slot_t *slot )
693{
694 if( key_type_is_raw_bytes( slot->type ) )
695 return( slot->data.raw.bytes * 8 );
696#if defined(MBEDTLS_RSA_C)
697 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
698 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
699 return( mbedtls_rsa_get_bitlen( slot->data.rsa ) );
700#endif /* defined(MBEDTLS_RSA_C) */
701#if defined(MBEDTLS_ECP_C)
702 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
703 return( slot->data.ecp->grp.pbits );
704#endif /* defined(MBEDTLS_ECP_C) */
705 /* Shouldn't happen except on an empty slot. */
706 return( 0 );
707}
708
Gilles Peskine2d277862018-06-18 15:41:12 +0200709psa_status_t psa_get_key_information( psa_key_slot_t key,
710 psa_key_type_t *type,
711 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100712{
713 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200714 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100715
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100716 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200717 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100718 if( bits != NULL )
719 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200720 status = psa_get_key_slot( key, &slot );
721 if( status != PSA_SUCCESS )
722 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200723
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100724 if( slot->type == PSA_KEY_TYPE_NONE )
725 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200726 if( type != NULL )
727 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200728 if( bits != NULL )
729 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100730 return( PSA_SUCCESS );
731}
732
Gilles Peskine2d277862018-06-18 15:41:12 +0200733static psa_status_t psa_internal_export_key( psa_key_slot_t key,
734 uint8_t *data,
735 size_t data_size,
736 size_t *data_length,
737 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738{
739 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200740 psa_status_t status;
741 /* Exporting a public key doesn't require a usage flag. If we're
742 * called by psa_export_public_key(), don't require the EXPORT flag.
743 * If we're called by psa_export_key(), do require the EXPORT flag;
744 * if the key turns out to be public key object, psa_get_key_from_slot()
745 * will ignore this flag. */
746 psa_key_usage_t usage = export_public_key ? 0 : PSA_KEY_USAGE_EXPORT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100747
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100748 /* Set the key to empty now, so that even when there are errors, we always
749 * set data_length to a value between 0 and data_size. On error, setting
750 * the key to empty is a good choice because an empty key representation is
751 * unlikely to be accepted anywhere. */
752 *data_length = 0;
753
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200754 status = psa_get_key_from_slot( key, &slot, usage, 0 );
755 if( status != PSA_SUCCESS )
756 return( status );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200757 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300758 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300759
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200760 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100761 {
762 if( slot->data.raw.bytes > data_size )
763 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200764 if( slot->data.raw.bytes != 0 )
765 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100766 *data_length = slot->data.raw.bytes;
767 return( PSA_SUCCESS );
768 }
769 else
Moran Peker17e36e12018-05-02 12:55:20 +0300770 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100771#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100772 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
Moran Pekera998bc62018-04-16 18:16:20 +0300773 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
774 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100775 {
Moran Pekera998bc62018-04-16 18:16:20 +0300776 mbedtls_pk_context pk;
777 int ret;
778 mbedtls_pk_init( &pk );
779 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
780 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
781 {
782 pk.pk_info = &mbedtls_rsa_info;
783 pk.pk_ctx = slot->data.rsa;
784 }
785 else
786 {
787 pk.pk_info = &mbedtls_eckey_info;
788 pk.pk_ctx = slot->data.ecp;
789 }
Moran Pekerd7326592018-05-29 16:56:39 +0300790 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300791 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300792 else
793 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300794 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200795 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200796 /* If data_size is 0 then data may be NULL and then the
797 * call to memset would have undefined behavior. */
798 if( data_size != 0 )
799 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300800 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200801 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200802 /* The mbedtls_pk_xxx functions write to the end of the buffer.
803 * Move the data to the beginning and erase remaining data
804 * at the original location. */
805 if( 2 * (size_t) ret <= data_size )
806 {
807 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200808 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200809 }
810 else if( (size_t) ret < data_size )
811 {
812 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200813 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200814 }
Moran Pekera998bc62018-04-16 18:16:20 +0300815 *data_length = ret;
816 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100817 }
818 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100819#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300820 {
821 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200822 it is valid for a special-purpose implementation to omit
823 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300824 return( PSA_ERROR_NOT_SUPPORTED );
825 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100826 }
827}
828
Gilles Peskine2d277862018-06-18 15:41:12 +0200829psa_status_t psa_export_key( psa_key_slot_t key,
830 uint8_t *data,
831 size_t data_size,
832 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300833{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200834 return( psa_internal_export_key( key, data, data_size,
835 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100836}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837
Gilles Peskine2d277862018-06-18 15:41:12 +0200838psa_status_t psa_export_public_key( psa_key_slot_t key,
839 uint8_t *data,
840 size_t data_size,
841 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300842{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200843 return( psa_internal_export_key( key, data, data_size,
844 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300845}
846
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200847
848
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100849/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100850/* Message digests */
851/****************************************************************/
852
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100853static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100854{
855 switch( alg )
856 {
857#if defined(MBEDTLS_MD2_C)
858 case PSA_ALG_MD2:
859 return( &mbedtls_md2_info );
860#endif
861#if defined(MBEDTLS_MD4_C)
862 case PSA_ALG_MD4:
863 return( &mbedtls_md4_info );
864#endif
865#if defined(MBEDTLS_MD5_C)
866 case PSA_ALG_MD5:
867 return( &mbedtls_md5_info );
868#endif
869#if defined(MBEDTLS_RIPEMD160_C)
870 case PSA_ALG_RIPEMD160:
871 return( &mbedtls_ripemd160_info );
872#endif
873#if defined(MBEDTLS_SHA1_C)
874 case PSA_ALG_SHA_1:
875 return( &mbedtls_sha1_info );
876#endif
877#if defined(MBEDTLS_SHA256_C)
878 case PSA_ALG_SHA_224:
879 return( &mbedtls_sha224_info );
880 case PSA_ALG_SHA_256:
881 return( &mbedtls_sha256_info );
882#endif
883#if defined(MBEDTLS_SHA512_C)
884 case PSA_ALG_SHA_384:
885 return( &mbedtls_sha384_info );
886 case PSA_ALG_SHA_512:
887 return( &mbedtls_sha512_info );
888#endif
889 default:
890 return( NULL );
891 }
892}
893
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100894psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
895{
896 switch( operation->alg )
897 {
Gilles Peskine81736312018-06-26 15:04:31 +0200898 case 0:
899 /* The object has (apparently) been initialized but it is not
900 * in use. It's ok to call abort on such an object, and there's
901 * nothing to do. */
902 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100903#if defined(MBEDTLS_MD2_C)
904 case PSA_ALG_MD2:
905 mbedtls_md2_free( &operation->ctx.md2 );
906 break;
907#endif
908#if defined(MBEDTLS_MD4_C)
909 case PSA_ALG_MD4:
910 mbedtls_md4_free( &operation->ctx.md4 );
911 break;
912#endif
913#if defined(MBEDTLS_MD5_C)
914 case PSA_ALG_MD5:
915 mbedtls_md5_free( &operation->ctx.md5 );
916 break;
917#endif
918#if defined(MBEDTLS_RIPEMD160_C)
919 case PSA_ALG_RIPEMD160:
920 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
921 break;
922#endif
923#if defined(MBEDTLS_SHA1_C)
924 case PSA_ALG_SHA_1:
925 mbedtls_sha1_free( &operation->ctx.sha1 );
926 break;
927#endif
928#if defined(MBEDTLS_SHA256_C)
929 case PSA_ALG_SHA_224:
930 case PSA_ALG_SHA_256:
931 mbedtls_sha256_free( &operation->ctx.sha256 );
932 break;
933#endif
934#if defined(MBEDTLS_SHA512_C)
935 case PSA_ALG_SHA_384:
936 case PSA_ALG_SHA_512:
937 mbedtls_sha512_free( &operation->ctx.sha512 );
938 break;
939#endif
940 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +0200941 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100942 }
943 operation->alg = 0;
944 return( PSA_SUCCESS );
945}
946
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200947psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100948 psa_algorithm_t alg )
949{
950 int ret;
951 operation->alg = 0;
952 switch( alg )
953 {
954#if defined(MBEDTLS_MD2_C)
955 case PSA_ALG_MD2:
956 mbedtls_md2_init( &operation->ctx.md2 );
957 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
958 break;
959#endif
960#if defined(MBEDTLS_MD4_C)
961 case PSA_ALG_MD4:
962 mbedtls_md4_init( &operation->ctx.md4 );
963 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
964 break;
965#endif
966#if defined(MBEDTLS_MD5_C)
967 case PSA_ALG_MD5:
968 mbedtls_md5_init( &operation->ctx.md5 );
969 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
970 break;
971#endif
972#if defined(MBEDTLS_RIPEMD160_C)
973 case PSA_ALG_RIPEMD160:
974 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
975 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
976 break;
977#endif
978#if defined(MBEDTLS_SHA1_C)
979 case PSA_ALG_SHA_1:
980 mbedtls_sha1_init( &operation->ctx.sha1 );
981 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
982 break;
983#endif
984#if defined(MBEDTLS_SHA256_C)
985 case PSA_ALG_SHA_224:
986 mbedtls_sha256_init( &operation->ctx.sha256 );
987 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
988 break;
989 case PSA_ALG_SHA_256:
990 mbedtls_sha256_init( &operation->ctx.sha256 );
991 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
992 break;
993#endif
994#if defined(MBEDTLS_SHA512_C)
995 case PSA_ALG_SHA_384:
996 mbedtls_sha512_init( &operation->ctx.sha512 );
997 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
998 break;
999 case PSA_ALG_SHA_512:
1000 mbedtls_sha512_init( &operation->ctx.sha512 );
1001 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1002 break;
1003#endif
1004 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001005 return( PSA_ALG_IS_HASH( alg ) ?
1006 PSA_ERROR_NOT_SUPPORTED :
1007 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001008 }
1009 if( ret == 0 )
1010 operation->alg = alg;
1011 else
1012 psa_hash_abort( operation );
1013 return( mbedtls_to_psa_error( ret ) );
1014}
1015
1016psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1017 const uint8_t *input,
1018 size_t input_length )
1019{
1020 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001021
1022 /* Don't require hash implementations to behave correctly on a
1023 * zero-length input, which may have an invalid pointer. */
1024 if( input_length == 0 )
1025 return( PSA_SUCCESS );
1026
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001027 switch( operation->alg )
1028 {
1029#if defined(MBEDTLS_MD2_C)
1030 case PSA_ALG_MD2:
1031 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1032 input, input_length );
1033 break;
1034#endif
1035#if defined(MBEDTLS_MD4_C)
1036 case PSA_ALG_MD4:
1037 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1038 input, input_length );
1039 break;
1040#endif
1041#if defined(MBEDTLS_MD5_C)
1042 case PSA_ALG_MD5:
1043 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1044 input, input_length );
1045 break;
1046#endif
1047#if defined(MBEDTLS_RIPEMD160_C)
1048 case PSA_ALG_RIPEMD160:
1049 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1050 input, input_length );
1051 break;
1052#endif
1053#if defined(MBEDTLS_SHA1_C)
1054 case PSA_ALG_SHA_1:
1055 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1056 input, input_length );
1057 break;
1058#endif
1059#if defined(MBEDTLS_SHA256_C)
1060 case PSA_ALG_SHA_224:
1061 case PSA_ALG_SHA_256:
1062 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1063 input, input_length );
1064 break;
1065#endif
1066#if defined(MBEDTLS_SHA512_C)
1067 case PSA_ALG_SHA_384:
1068 case PSA_ALG_SHA_512:
1069 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1070 input, input_length );
1071 break;
1072#endif
1073 default:
1074 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1075 break;
1076 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001077
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001078 if( ret != 0 )
1079 psa_hash_abort( operation );
1080 return( mbedtls_to_psa_error( ret ) );
1081}
1082
1083psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1084 uint8_t *hash,
1085 size_t hash_size,
1086 size_t *hash_length )
1087{
1088 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001089 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001090
1091 /* Fill the output buffer with something that isn't a valid hash
1092 * (barring an attack on the hash and deliberately-crafted input),
1093 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001094 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001095 /* If hash_size is 0 then hash may be NULL and then the
1096 * call to memset would have undefined behavior. */
1097 if( hash_size != 0 )
1098 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001099
1100 if( hash_size < actual_hash_length )
1101 return( PSA_ERROR_BUFFER_TOO_SMALL );
1102
1103 switch( operation->alg )
1104 {
1105#if defined(MBEDTLS_MD2_C)
1106 case PSA_ALG_MD2:
1107 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1108 break;
1109#endif
1110#if defined(MBEDTLS_MD4_C)
1111 case PSA_ALG_MD4:
1112 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1113 break;
1114#endif
1115#if defined(MBEDTLS_MD5_C)
1116 case PSA_ALG_MD5:
1117 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1118 break;
1119#endif
1120#if defined(MBEDTLS_RIPEMD160_C)
1121 case PSA_ALG_RIPEMD160:
1122 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1123 break;
1124#endif
1125#if defined(MBEDTLS_SHA1_C)
1126 case PSA_ALG_SHA_1:
1127 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1128 break;
1129#endif
1130#if defined(MBEDTLS_SHA256_C)
1131 case PSA_ALG_SHA_224:
1132 case PSA_ALG_SHA_256:
1133 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1134 break;
1135#endif
1136#if defined(MBEDTLS_SHA512_C)
1137 case PSA_ALG_SHA_384:
1138 case PSA_ALG_SHA_512:
1139 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1140 break;
1141#endif
1142 default:
1143 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1144 break;
1145 }
1146
1147 if( ret == 0 )
1148 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001149 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001150 return( psa_hash_abort( operation ) );
1151 }
1152 else
1153 {
1154 psa_hash_abort( operation );
1155 return( mbedtls_to_psa_error( ret ) );
1156 }
1157}
1158
Gilles Peskine2d277862018-06-18 15:41:12 +02001159psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1160 const uint8_t *hash,
1161 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001162{
1163 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1164 size_t actual_hash_length;
1165 psa_status_t status = psa_hash_finish( operation,
1166 actual_hash, sizeof( actual_hash ),
1167 &actual_hash_length );
1168 if( status != PSA_SUCCESS )
1169 return( status );
1170 if( actual_hash_length != hash_length )
1171 return( PSA_ERROR_INVALID_SIGNATURE );
1172 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1173 return( PSA_ERROR_INVALID_SIGNATURE );
1174 return( PSA_SUCCESS );
1175}
1176
1177
1178
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001179/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001180/* MAC */
1181/****************************************************************/
1182
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001183static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001184 psa_algorithm_t alg,
1185 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001186 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001187 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001188{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001189 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001190 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001191
1192 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1193 {
1194 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001195 {
1196 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1197 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001198
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001199 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001200 {
Gilles Peskine5d1888e2018-07-12 00:32:42 +02001201 case PSA_ALG_STREAM_CIPHER_BASE:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001202 mode = MBEDTLS_MODE_STREAM;
1203 break;
1204 case PSA_ALG_CBC_BASE:
1205 mode = MBEDTLS_MODE_CBC;
1206 break;
1207 case PSA_ALG_CFB_BASE:
1208 mode = MBEDTLS_MODE_CFB;
1209 break;
1210 case PSA_ALG_OFB_BASE:
1211 mode = MBEDTLS_MODE_OFB;
1212 break;
1213 case PSA_ALG_CTR:
1214 mode = MBEDTLS_MODE_CTR;
1215 break;
1216 case PSA_ALG_CCM:
1217 mode = MBEDTLS_MODE_CCM;
1218 break;
1219 case PSA_ALG_GCM:
1220 mode = MBEDTLS_MODE_GCM;
1221 break;
1222 default:
1223 return( NULL );
1224 }
1225 }
1226 else if( alg == PSA_ALG_CMAC )
1227 mode = MBEDTLS_MODE_ECB;
1228 else if( alg == PSA_ALG_GMAC )
1229 mode = MBEDTLS_MODE_GCM;
1230 else
1231 return( NULL );
1232
1233 switch( key_type )
1234 {
1235 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001236 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001237 break;
1238 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001239 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1240 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001241 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001242 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001243 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001244 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001245 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1246 * but two-key Triple-DES is functionally three-key Triple-DES
1247 * with K1=K3, so that's how we present it to mbedtls. */
1248 if( key_bits == 128 )
1249 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001250 break;
1251 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001252 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001253 break;
1254 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001255 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001256 break;
1257 default:
1258 return( NULL );
1259 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001260 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001261 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001262
Jaeden Amero23bbb752018-06-26 14:16:54 +01001263 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1264 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001265}
1266
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001267static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001268{
Gilles Peskine2d277862018-06-18 15:41:12 +02001269 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001270 {
1271 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001272 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001273 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001274 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001275 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001276 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001277 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001278 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001279 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001280 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001281 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001282 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001283 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001284 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001285 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001286 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001287 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001288 return( 128 );
1289 default:
1290 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001291 }
1292}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001293
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001294/* Initialize the MAC operation structure. Once this function has been
1295 * called, psa_mac_abort can run and will do the right thing. */
1296static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1297 psa_algorithm_t alg )
1298{
1299 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1300
1301 operation->alg = alg;
1302 operation->key_set = 0;
1303 operation->iv_set = 0;
1304 operation->iv_required = 0;
1305 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001306 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001307
1308#if defined(MBEDTLS_CMAC_C)
1309 if( alg == PSA_ALG_CMAC )
1310 {
1311 operation->iv_required = 0;
1312 mbedtls_cipher_init( &operation->ctx.cmac );
1313 status = PSA_SUCCESS;
1314 }
1315 else
1316#endif /* MBEDTLS_CMAC_C */
1317#if defined(MBEDTLS_MD_C)
1318 if( PSA_ALG_IS_HMAC( operation->alg ) )
1319 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001320 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1321 operation->ctx.hmac.hash_ctx.alg = 0;
1322 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001323 }
1324 else
1325#endif /* MBEDTLS_MD_C */
1326 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001327 if( ! PSA_ALG_IS_MAC( alg ) )
1328 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001329 }
1330
1331 if( status != PSA_SUCCESS )
1332 memset( operation, 0, sizeof( *operation ) );
1333 return( status );
1334}
1335
Gilles Peskine01126fa2018-07-12 17:04:55 +02001336#if defined(MBEDTLS_MD_C)
1337static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1338{
1339 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1340 return( psa_hash_abort( &hmac->hash_ctx ) );
1341}
1342#endif /* MBEDTLS_MD_C */
1343
Gilles Peskine8c9def32018-02-08 10:02:12 +01001344psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1345{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001346 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001347 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001348 /* The object has (apparently) been initialized but it is not
1349 * in use. It's ok to call abort on such an object, and there's
1350 * nothing to do. */
1351 return( PSA_SUCCESS );
1352 }
1353 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001354#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001355 if( operation->alg == PSA_ALG_CMAC )
1356 {
1357 mbedtls_cipher_free( &operation->ctx.cmac );
1358 }
1359 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001360#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001361#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001362 if( PSA_ALG_IS_HMAC( operation->alg ) )
1363 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001364 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001365 }
1366 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001367#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001368 {
1369 /* Sanity check (shouldn't happen: operation->alg should
1370 * always have been initialized to a valid value). */
1371 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001372 }
Moran Peker41deec42018-04-04 15:43:05 +03001373
Gilles Peskine8c9def32018-02-08 10:02:12 +01001374 operation->alg = 0;
1375 operation->key_set = 0;
1376 operation->iv_set = 0;
1377 operation->iv_required = 0;
1378 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001379 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001380
Gilles Peskine8c9def32018-02-08 10:02:12 +01001381 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001382
1383bad_state:
1384 /* If abort is called on an uninitialized object, we can't trust
1385 * anything. Wipe the object in case it contains confidential data.
1386 * This may result in a memory leak if a pointer gets overwritten,
1387 * but it's too late to do anything about this. */
1388 memset( operation, 0, sizeof( *operation ) );
1389 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001390}
1391
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001392#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001393static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001394 size_t key_bits,
1395 key_slot_t *slot,
1396 const mbedtls_cipher_info_t *cipher_info )
1397{
1398 int ret;
1399
1400 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001401
1402 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1403 if( ret != 0 )
1404 return( ret );
1405
1406 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1407 slot->data.raw.data,
1408 key_bits );
1409 return( ret );
1410}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001411#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001412
Gilles Peskine248051a2018-06-20 16:09:38 +02001413#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001414static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1415 const uint8_t *key,
1416 size_t key_length,
1417 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001418{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001419 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001420 size_t i;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001421 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001422 psa_status_t status;
1423
Gilles Peskine01126fa2018-07-12 17:04:55 +02001424 if( block_size == 0 )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001425 return( PSA_ERROR_NOT_SUPPORTED );
1426
Gilles Peskineff94abd2018-07-12 17:07:52 +02001427 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1428 if( status != PSA_SUCCESS )
1429 return( status );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001430
Gilles Peskined223b522018-06-11 18:12:58 +02001431 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001432 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001433 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001434 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001435 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001436 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001437 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001438 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001439 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001440 }
Gilles Peskine96889972018-07-12 17:07:03 +02001441 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1442 * but it is permitted. It is common when HMAC is used in HKDF, for
1443 * example. Don't call `memcpy` in the 0-length because `key` could be
1444 * an invalid pointer which would make the behavior undefined. */
1445 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001446 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001447
Gilles Peskined223b522018-06-11 18:12:58 +02001448 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1449 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001450 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001451 ipad[i] ^= 0x36;
1452 memset( ipad + key_length, 0x36, block_size - key_length );
1453
1454 /* Copy the key material from ipad to opad, flipping the requisite bits,
1455 * and filling the rest of opad with the requisite constant. */
1456 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001457 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1458 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001459
Gilles Peskine01126fa2018-07-12 17:04:55 +02001460 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001461 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001462 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001463
Gilles Peskine01126fa2018-07-12 17:04:55 +02001464 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001465
1466cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001467 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001468
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001469 return( status );
1470}
Gilles Peskine248051a2018-06-20 16:09:38 +02001471#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001472
Gilles Peskine89167cb2018-07-08 20:12:23 +02001473static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1474 psa_key_slot_t key,
1475 psa_algorithm_t alg,
1476 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001477{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001478 psa_status_t status;
1479 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001480 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001481 psa_key_usage_t usage =
1482 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001483
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001484 status = psa_mac_init( operation, alg );
1485 if( status != PSA_SUCCESS )
1486 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001487 if( is_sign )
1488 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001489
Gilles Peskine89167cb2018-07-08 20:12:23 +02001490 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001491 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001492 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001493 key_bits = psa_get_key_bits( slot );
1494
Gilles Peskine8c9def32018-02-08 10:02:12 +01001495#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001496 if( alg == PSA_ALG_CMAC )
1497 {
1498 const mbedtls_cipher_info_t *cipher_info =
1499 mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
1500 int ret;
1501 if( cipher_info == NULL )
1502 {
1503 status = PSA_ERROR_NOT_SUPPORTED;
1504 goto exit;
1505 }
1506 operation->mac_size = cipher_info->block_size;
1507 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1508 status = mbedtls_to_psa_error( ret );
1509 }
1510 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001511#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001512#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001513 if( PSA_ALG_IS_HMAC( alg ) )
1514 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001515 psa_algorithm_t hash_alg = PSA_ALG_HMAC_HASH( alg );
1516 if( hash_alg == 0 )
1517 {
1518 status = PSA_ERROR_NOT_SUPPORTED;
1519 goto exit;
1520 }
1521 if( slot->type != PSA_KEY_TYPE_HMAC )
1522 {
1523 status = PSA_ERROR_INVALID_ARGUMENT;
1524 goto exit;
1525 }
1526 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1527 slot->data.raw.data,
1528 slot->data.raw.bytes,
1529 hash_alg );
1530 operation->mac_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001531 }
1532 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001533#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001534 {
1535 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001536 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001537
Gilles Peskinefbfac682018-07-08 20:51:54 +02001538exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001539 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001540 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001541 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001542 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001543 else
1544 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001545 operation->key_set = 1;
1546 }
1547 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001548}
1549
Gilles Peskine89167cb2018-07-08 20:12:23 +02001550psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1551 psa_key_slot_t key,
1552 psa_algorithm_t alg )
1553{
1554 return( psa_mac_setup( operation, key, alg, 1 ) );
1555}
1556
1557psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1558 psa_key_slot_t key,
1559 psa_algorithm_t alg )
1560{
1561 return( psa_mac_setup( operation, key, alg, 0 ) );
1562}
1563
Gilles Peskine8c9def32018-02-08 10:02:12 +01001564psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1565 const uint8_t *input,
1566 size_t input_length )
1567{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001568 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001569 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001570 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001571 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001572 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001573 operation->has_input = 1;
1574
Gilles Peskine8c9def32018-02-08 10:02:12 +01001575#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001576 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001577 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001578 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1579 input, input_length );
1580 status = mbedtls_to_psa_error( ret );
1581 }
1582 else
1583#endif /* MBEDTLS_CMAC_C */
1584#if defined(MBEDTLS_MD_C)
1585 if( PSA_ALG_IS_HMAC( operation->alg ) )
1586 {
1587 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1588 input_length );
1589 }
1590 else
1591#endif /* MBEDTLS_MD_C */
1592 {
1593 /* This shouldn't happen if `operation` was initialized by
1594 * a setup function. */
1595 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001596 }
1597
Gilles Peskinefbfac682018-07-08 20:51:54 +02001598cleanup:
1599 if( status != PSA_SUCCESS )
1600 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001601 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001602}
1603
Gilles Peskine01126fa2018-07-12 17:04:55 +02001604#if defined(MBEDTLS_MD_C)
1605static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1606 uint8_t *mac,
1607 size_t mac_size )
1608{
1609 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1610 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1611 size_t hash_size = 0;
1612 size_t block_size = psa_get_hash_block_size( hash_alg );
1613 psa_status_t status;
1614
1615 if( block_size == 0 )
1616 return( PSA_ERROR_NOT_SUPPORTED );
1617
1618 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1619 if( status != PSA_SUCCESS )
1620 return( status );
1621 /* From here on, tmp needs to be wiped. */
1622
1623 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1624 if( status != PSA_SUCCESS )
1625 goto exit;
1626
1627 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1628 if( status != PSA_SUCCESS )
1629 goto exit;
1630
1631 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1632 if( status != PSA_SUCCESS )
1633 goto exit;
1634
1635 status = psa_hash_finish( &hmac->hash_ctx, mac, mac_size, &hash_size );
1636
1637exit:
1638 mbedtls_zeroize( tmp, hash_size );
1639 return( status );
1640}
1641#endif /* MBEDTLS_MD_C */
1642
mohammad16036df908f2018-04-02 08:34:15 -07001643static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001644 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001645 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001646{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001647 if( ! operation->key_set )
1648 return( PSA_ERROR_BAD_STATE );
1649 if( operation->iv_required && ! operation->iv_set )
1650 return( PSA_ERROR_BAD_STATE );
1651
Gilles Peskine8c9def32018-02-08 10:02:12 +01001652 if( mac_size < operation->mac_size )
1653 return( PSA_ERROR_BUFFER_TOO_SMALL );
1654
Gilles Peskine8c9def32018-02-08 10:02:12 +01001655#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001656 if( operation->alg == PSA_ALG_CMAC )
1657 {
1658 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1659 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001660 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001661 else
1662#endif /* MBEDTLS_CMAC_C */
1663#if defined(MBEDTLS_MD_C)
1664 if( PSA_ALG_IS_HMAC( operation->alg ) )
1665 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001666 return( psa_hmac_finish_internal( &operation->ctx.hmac,
1667 mac, mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001668 }
1669 else
1670#endif /* MBEDTLS_MD_C */
1671 {
1672 /* This shouldn't happen if `operation` was initialized by
1673 * a setup function. */
1674 return( PSA_ERROR_BAD_STATE );
1675 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001676}
1677
Gilles Peskineacd4be32018-07-08 19:56:25 +02001678psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1679 uint8_t *mac,
1680 size_t mac_size,
1681 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001682{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001683 psa_status_t status;
1684
1685 /* Fill the output buffer with something that isn't a valid mac
1686 * (barring an attack on the mac and deliberately-crafted input),
1687 * in case the caller doesn't check the return status properly. */
1688 *mac_length = mac_size;
1689 /* If mac_size is 0 then mac may be NULL and then the
1690 * call to memset would have undefined behavior. */
1691 if( mac_size != 0 )
1692 memset( mac, '!', mac_size );
1693
Gilles Peskine89167cb2018-07-08 20:12:23 +02001694 if( ! operation->is_sign )
1695 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001696 status = PSA_ERROR_BAD_STATE;
1697 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001698 }
mohammad16036df908f2018-04-02 08:34:15 -07001699
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001700 status = psa_mac_finish_internal( operation, mac, mac_size );
1701
1702cleanup:
1703 if( status == PSA_SUCCESS )
1704 {
1705 status = psa_mac_abort( operation );
1706 if( status == PSA_SUCCESS )
1707 *mac_length = operation->mac_size;
1708 else
1709 memset( mac, '!', mac_size );
1710 }
1711 else
1712 psa_mac_abort( operation );
1713 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001714}
1715
Gilles Peskineacd4be32018-07-08 19:56:25 +02001716psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1717 const uint8_t *mac,
1718 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001719{
Gilles Peskine828ed142018-06-18 23:25:51 +02001720 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001721 psa_status_t status;
1722
Gilles Peskine89167cb2018-07-08 20:12:23 +02001723 if( operation->is_sign )
1724 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001725 status = PSA_ERROR_BAD_STATE;
1726 goto cleanup;
1727 }
1728 if( operation->mac_size != mac_length )
1729 {
1730 status = PSA_ERROR_INVALID_SIGNATURE;
1731 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001732 }
mohammad16036df908f2018-04-02 08:34:15 -07001733
1734 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001735 actual_mac, sizeof( actual_mac ) );
1736
1737 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1738 status = PSA_ERROR_INVALID_SIGNATURE;
1739
1740cleanup:
1741 if( status == PSA_SUCCESS )
1742 status = psa_mac_abort( operation );
1743 else
1744 psa_mac_abort( operation );
1745
1746 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001747}
1748
1749
Gilles Peskine20035e32018-02-03 22:44:14 +01001750
Gilles Peskine20035e32018-02-03 22:44:14 +01001751/****************************************************************/
1752/* Asymmetric cryptography */
1753/****************************************************************/
1754
Gilles Peskine2b450e32018-06-27 15:42:46 +02001755#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001756/* Decode the hash algorithm from alg and store the mbedtls encoding in
1757 * md_alg. Verify that the hash length is consistent. */
1758static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1759 size_t hash_length,
1760 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001761{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001762 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001763 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1764 *md_alg = hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1765 if( *md_alg == MBEDTLS_MD_NONE )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001766 {
1767#if SIZE_MAX > UINT_MAX
Gilles Peskine61b91d42018-06-08 16:09:36 +02001768 if( hash_length > UINT_MAX )
1769 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001770#endif
1771 }
1772 else
1773 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001774 if( mbedtls_md_get_size( md_info ) != hash_length )
1775 return( PSA_ERROR_INVALID_ARGUMENT );
1776 if( md_info == NULL )
1777 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001778 }
Gilles Peskine61b91d42018-06-08 16:09:36 +02001779 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001780}
1781
Gilles Peskine2b450e32018-06-27 15:42:46 +02001782static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1783 psa_algorithm_t alg,
1784 const uint8_t *hash,
1785 size_t hash_length,
1786 uint8_t *signature,
1787 size_t signature_size,
1788 size_t *signature_length )
1789{
1790 psa_status_t status;
1791 int ret;
1792 mbedtls_md_type_t md_alg;
1793
1794 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1795 if( status != PSA_SUCCESS )
1796 return( status );
1797
1798 if( signature_size < rsa->len )
1799 return( PSA_ERROR_BUFFER_TOO_SMALL );
1800
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001801 /* The Mbed TLS RSA module uses an unsigned int for hash_length. See if
1802 * hash_length will fit and return an error if it doesn't. */
1803#if defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21)
1804#if SIZE_MAX > UINT_MAX
1805 if( hash_length > UINT_MAX )
1806 return( PSA_ERROR_NOT_SUPPORTED );
1807#endif
1808#endif
1809
Gilles Peskine2b450e32018-06-27 15:42:46 +02001810#if defined(MBEDTLS_PKCS1_V15)
1811 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1812 {
1813 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1814 MBEDTLS_MD_NONE );
1815 ret = mbedtls_rsa_pkcs1_sign( rsa,
1816 mbedtls_ctr_drbg_random,
1817 &global_data.ctr_drbg,
1818 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001819 md_alg,
1820 (unsigned int) hash_length,
1821 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001822 signature );
1823 }
1824 else
1825#endif /* MBEDTLS_PKCS1_V15 */
1826#if defined(MBEDTLS_PKCS1_V21)
1827 if( PSA_ALG_IS_RSA_PSS( alg ) )
1828 {
1829 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1830 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1831 mbedtls_ctr_drbg_random,
1832 &global_data.ctr_drbg,
1833 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001834 md_alg,
1835 (unsigned int) hash_length,
1836 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001837 signature );
1838 }
1839 else
1840#endif /* MBEDTLS_PKCS1_V21 */
1841 {
1842 return( PSA_ERROR_INVALID_ARGUMENT );
1843 }
1844
1845 if( ret == 0 )
1846 *signature_length = rsa->len;
1847 return( mbedtls_to_psa_error( ret ) );
1848}
1849
1850static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1851 psa_algorithm_t alg,
1852 const uint8_t *hash,
1853 size_t hash_length,
1854 const uint8_t *signature,
1855 size_t signature_length )
1856{
1857 psa_status_t status;
1858 int ret;
1859 mbedtls_md_type_t md_alg;
1860
1861 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1862 if( status != PSA_SUCCESS )
1863 return( status );
1864
1865 if( signature_length < rsa->len )
1866 return( PSA_ERROR_BUFFER_TOO_SMALL );
1867
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001868#if defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21)
1869#if SIZE_MAX > UINT_MAX
1870 /* The Mbed TLS RSA module uses an unsigned int for hash_length. See if
1871 * hash_length will fit and return an error if it doesn't. */
1872 if( hash_length > UINT_MAX )
1873 return( PSA_ERROR_NOT_SUPPORTED );
1874#endif
1875#endif
1876
Gilles Peskine2b450e32018-06-27 15:42:46 +02001877#if defined(MBEDTLS_PKCS1_V15)
1878 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1879 {
1880 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1881 MBEDTLS_MD_NONE );
1882 ret = mbedtls_rsa_pkcs1_verify( rsa,
1883 mbedtls_ctr_drbg_random,
1884 &global_data.ctr_drbg,
1885 MBEDTLS_RSA_PUBLIC,
1886 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001887 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001888 hash,
1889 signature );
1890 }
1891 else
1892#endif /* MBEDTLS_PKCS1_V15 */
1893#if defined(MBEDTLS_PKCS1_V21)
1894 if( PSA_ALG_IS_RSA_PSS( alg ) )
1895 {
1896 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1897 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1898 mbedtls_ctr_drbg_random,
1899 &global_data.ctr_drbg,
1900 MBEDTLS_RSA_PUBLIC,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001901 md_alg,
1902 (unsigned int) hash_length,
1903 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001904 signature );
1905 }
1906 else
1907#endif /* MBEDTLS_PKCS1_V21 */
1908 {
1909 return( PSA_ERROR_INVALID_ARGUMENT );
1910 }
1911 return( mbedtls_to_psa_error( ret ) );
1912}
1913#endif /* MBEDTLS_RSA_C */
1914
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001915#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001916/* `ecp` cannot be const because `ecp->grp` needs to be non-const
1917 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
1918 * (even though these functions don't modify it). */
1919static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
1920 psa_algorithm_t alg,
1921 const uint8_t *hash,
1922 size_t hash_length,
1923 uint8_t *signature,
1924 size_t signature_size,
1925 size_t *signature_length )
1926{
1927 int ret;
1928 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001929 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001930 mbedtls_mpi_init( &r );
1931 mbedtls_mpi_init( &s );
1932
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001933 if( signature_size < 2 * curve_bytes )
1934 {
1935 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
1936 goto cleanup;
1937 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001938
1939 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
1940 {
1941 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
1942 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1943 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
1944 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
1945 hash, hash_length,
1946 md_alg ) );
1947 }
1948 else
1949 {
1950 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
1951 hash, hash_length,
1952 mbedtls_ctr_drbg_random,
1953 &global_data.ctr_drbg ) );
1954 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001955
1956 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
1957 signature,
1958 curve_bytes ) );
1959 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
1960 signature + curve_bytes,
1961 curve_bytes ) );
1962
1963cleanup:
1964 mbedtls_mpi_free( &r );
1965 mbedtls_mpi_free( &s );
1966 if( ret == 0 )
1967 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001968 return( mbedtls_to_psa_error( ret ) );
1969}
1970
1971static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
1972 const uint8_t *hash,
1973 size_t hash_length,
1974 const uint8_t *signature,
1975 size_t signature_length )
1976{
1977 int ret;
1978 mbedtls_mpi r, s;
1979 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
1980 mbedtls_mpi_init( &r );
1981 mbedtls_mpi_init( &s );
1982
1983 if( signature_length != 2 * curve_bytes )
1984 return( PSA_ERROR_INVALID_SIGNATURE );
1985
1986 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
1987 signature,
1988 curve_bytes ) );
1989 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
1990 signature + curve_bytes,
1991 curve_bytes ) );
1992
1993 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
1994 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001995
1996cleanup:
1997 mbedtls_mpi_free( &r );
1998 mbedtls_mpi_free( &s );
1999 return( mbedtls_to_psa_error( ret ) );
2000}
2001#endif /* MBEDTLS_ECDSA_C */
2002
Gilles Peskine61b91d42018-06-08 16:09:36 +02002003psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2004 psa_algorithm_t alg,
2005 const uint8_t *hash,
2006 size_t hash_length,
2007 const uint8_t *salt,
2008 size_t salt_length,
2009 uint8_t *signature,
2010 size_t signature_size,
2011 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002012{
2013 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002014 psa_status_t status;
2015
2016 *signature_length = signature_size;
2017
Gilles Peskine93aa0332018-02-03 23:58:03 +01002018 (void) salt;
2019 (void) salt_length;
2020
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002021 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2022 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002023 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002024 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002025 {
2026 status = PSA_ERROR_INVALID_ARGUMENT;
2027 goto exit;
2028 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002029
Gilles Peskine20035e32018-02-03 22:44:14 +01002030#if defined(MBEDTLS_RSA_C)
2031 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2032 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002033 status = psa_rsa_sign( slot->data.rsa,
2034 alg,
2035 hash, hash_length,
2036 signature, signature_size,
2037 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002038 }
2039 else
2040#endif /* defined(MBEDTLS_RSA_C) */
2041#if defined(MBEDTLS_ECP_C)
2042 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2043 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002044#if defined(MBEDTLS_ECDSA_C)
2045 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002046 status = psa_ecdsa_sign( slot->data.ecp,
2047 alg,
2048 hash, hash_length,
2049 signature, signature_size,
2050 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002051 else
2052#endif /* defined(MBEDTLS_ECDSA_C) */
2053 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002054 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002055 }
itayzafrir5c753392018-05-08 11:18:38 +03002056 }
2057 else
2058#endif /* defined(MBEDTLS_ECP_C) */
2059 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002060 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002061 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002062
2063exit:
2064 /* Fill the unused part of the output buffer (the whole buffer on error,
2065 * the trailing part on success) with something that isn't a valid mac
2066 * (barring an attack on the mac and deliberately-crafted input),
2067 * in case the caller doesn't check the return status properly. */
2068 if( status == PSA_SUCCESS )
2069 memset( signature + *signature_length, '!',
2070 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002071 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002072 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002073 /* If signature_size is 0 then we have nothing to do. We must not call
2074 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002075 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002076}
2077
2078psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2079 psa_algorithm_t alg,
2080 const uint8_t *hash,
2081 size_t hash_length,
2082 const uint8_t *salt,
2083 size_t salt_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002084 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002085 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002086{
2087 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002088 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002089
itayzafrir5c753392018-05-08 11:18:38 +03002090 (void) salt;
2091 (void) salt_length;
2092
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002093 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2094 if( status != PSA_SUCCESS )
2095 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002096
Gilles Peskine61b91d42018-06-08 16:09:36 +02002097#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002098 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
2099 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002100 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002101 return( psa_rsa_verify( slot->data.rsa,
2102 alg,
2103 hash, hash_length,
2104 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002105 }
2106 else
2107#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002108#if defined(MBEDTLS_ECP_C)
2109 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2110 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002111#if defined(MBEDTLS_ECDSA_C)
2112 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002113 return( psa_ecdsa_verify( slot->data.ecp,
2114 hash, hash_length,
2115 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002116 else
2117#endif /* defined(MBEDTLS_ECDSA_C) */
2118 {
2119 return( PSA_ERROR_INVALID_ARGUMENT );
2120 }
itayzafrir5c753392018-05-08 11:18:38 +03002121 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002122 else
2123#endif /* defined(MBEDTLS_ECP_C) */
2124 {
2125 return( PSA_ERROR_NOT_SUPPORTED );
2126 }
2127}
2128
Gilles Peskine61b91d42018-06-08 16:09:36 +02002129psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2130 psa_algorithm_t alg,
2131 const uint8_t *input,
2132 size_t input_length,
2133 const uint8_t *salt,
2134 size_t salt_length,
2135 uint8_t *output,
2136 size_t output_size,
2137 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002138{
2139 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002140 psa_status_t status;
2141
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002142 (void) salt;
2143 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002144 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002145
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002146 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2147 if( status != PSA_SUCCESS )
2148 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002149 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2150 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002151
2152#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002153 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
2154 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002155 {
2156 mbedtls_rsa_context *rsa = slot->data.rsa;
2157 int ret;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002158 if( output_size < rsa->len )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002159 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002160#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002161 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002162 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002163 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2164 mbedtls_ctr_drbg_random,
2165 &global_data.ctr_drbg,
2166 MBEDTLS_RSA_PUBLIC,
2167 input_length,
2168 input,
2169 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002170 }
2171 else
2172#endif /* MBEDTLS_PKCS1_V15 */
2173#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002174 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002175 {
2176 return( PSA_ERROR_NOT_SUPPORTED );
2177 }
2178 else
2179#endif /* MBEDTLS_PKCS1_V21 */
2180 {
2181 return( PSA_ERROR_INVALID_ARGUMENT );
2182 }
2183 if( ret == 0 )
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002184 *output_length = rsa->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002185 return( mbedtls_to_psa_error( ret ) );
2186 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002187 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002188#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002189 {
2190 return( PSA_ERROR_NOT_SUPPORTED );
2191 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002192}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002193
Gilles Peskine61b91d42018-06-08 16:09:36 +02002194psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2195 psa_algorithm_t alg,
2196 const uint8_t *input,
2197 size_t input_length,
2198 const uint8_t *salt,
2199 size_t salt_length,
2200 uint8_t *output,
2201 size_t output_size,
2202 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002203{
2204 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002205 psa_status_t status;
2206
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002207 (void) salt;
2208 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002209 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002210
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002211 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2212 if( status != PSA_SUCCESS )
2213 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002214 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2215 return( PSA_ERROR_INVALID_ARGUMENT );
2216
2217#if defined(MBEDTLS_RSA_C)
2218 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2219 {
2220 mbedtls_rsa_context *rsa = slot->data.rsa;
2221 int ret;
2222
Gilles Peskinec4def2f2018-06-08 17:53:48 +02002223 if( input_length != rsa->len )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002224 return( PSA_ERROR_INVALID_ARGUMENT );
2225
2226#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002227 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002228 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002229 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2230 mbedtls_ctr_drbg_random,
2231 &global_data.ctr_drbg,
2232 MBEDTLS_RSA_PRIVATE,
2233 output_length,
2234 input,
2235 output,
2236 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002237 }
2238 else
2239#endif /* MBEDTLS_PKCS1_V15 */
2240#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002241 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002242 {
2243 return( PSA_ERROR_NOT_SUPPORTED );
2244 }
2245 else
2246#endif /* MBEDTLS_PKCS1_V21 */
2247 {
2248 return( PSA_ERROR_INVALID_ARGUMENT );
2249 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002250
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002251 return( mbedtls_to_psa_error( ret ) );
2252 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002253 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002254#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002255 {
2256 return( PSA_ERROR_NOT_SUPPORTED );
2257 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002258}
Gilles Peskine20035e32018-02-03 22:44:14 +01002259
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002260
2261
mohammad1603503973b2018-03-12 15:59:30 +02002262/****************************************************************/
2263/* Symmetric cryptography */
2264/****************************************************************/
2265
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002266/* Initialize the cipher operation structure. Once this function has been
2267 * called, psa_cipher_abort can run and will do the right thing. */
2268static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2269 psa_algorithm_t alg )
2270{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002271 if( ! PSA_ALG_IS_CIPHER( alg ) )
2272 {
2273 memset( operation, 0, sizeof( *operation ) );
2274 return( PSA_ERROR_INVALID_ARGUMENT );
2275 }
2276
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002277 operation->alg = alg;
2278 operation->key_set = 0;
2279 operation->iv_set = 0;
2280 operation->iv_required = 1;
2281 operation->iv_size = 0;
2282 operation->block_size = 0;
2283 mbedtls_cipher_init( &operation->ctx.cipher );
2284 return( PSA_SUCCESS );
2285}
2286
Gilles Peskinee553c652018-06-04 16:22:46 +02002287static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2288 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002289 psa_algorithm_t alg,
2290 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002291{
2292 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2293 psa_status_t status;
2294 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002295 size_t key_bits;
2296 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002297 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2298 PSA_KEY_USAGE_ENCRYPT :
2299 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002300
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002301 status = psa_cipher_init( operation, alg );
2302 if( status != PSA_SUCCESS )
2303 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002304
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002305 status = psa_get_key_from_slot( key, &slot, usage, alg);
2306 if( status != PSA_SUCCESS )
2307 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002308 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002309
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002310 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002311 if( cipher_info == NULL )
2312 return( PSA_ERROR_NOT_SUPPORTED );
2313
mohammad1603503973b2018-03-12 15:59:30 +02002314 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002315 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002316 {
2317 psa_cipher_abort( operation );
2318 return( mbedtls_to_psa_error( ret ) );
2319 }
2320
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002321#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002322 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002323 {
2324 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2325 unsigned char keys[24];
2326 memcpy( keys, slot->data.raw.data, 16 );
2327 memcpy( keys + 16, slot->data.raw.data, 8 );
2328 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2329 keys,
2330 192, cipher_operation );
2331 }
2332 else
2333#endif
2334 {
2335 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2336 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002337 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002338 }
Moran Peker41deec42018-04-04 15:43:05 +03002339 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002340 {
2341 psa_cipher_abort( operation );
2342 return( mbedtls_to_psa_error( ret ) );
2343 }
2344
mohammad16038481e742018-03-18 13:57:31 +02002345#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002346 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002347 {
Gilles Peskine53514202018-06-06 15:11:46 +02002348 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2349 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002350
Moran Pekera28258c2018-05-29 16:25:04 +03002351 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002352 {
2353 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2354 mode = MBEDTLS_PADDING_PKCS7;
2355 break;
2356 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2357 mode = MBEDTLS_PADDING_NONE;
2358 break;
2359 default:
Moran Pekerae382792018-05-31 14:06:17 +03002360 psa_cipher_abort( operation );
2361 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002362 }
2363 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002364 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002365 {
2366 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002367 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002368 }
mohammad16038481e742018-03-18 13:57:31 +02002369 }
2370#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2371
mohammad1603503973b2018-03-12 15:59:30 +02002372 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002373 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002374 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) :
Gilles Peskine7e928852018-06-04 16:23:10 +02002375 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002376 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002377 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002378 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002379 }
mohammad1603503973b2018-03-12 15:59:30 +02002380
Moran Peker395db872018-05-31 14:07:14 +03002381 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002382}
2383
Gilles Peskinefe119512018-07-08 21:39:34 +02002384psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2385 psa_key_slot_t key,
2386 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002387{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002388 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002389}
2390
Gilles Peskinefe119512018-07-08 21:39:34 +02002391psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2392 psa_key_slot_t key,
2393 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002394{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002395 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002396}
2397
Gilles Peskinefe119512018-07-08 21:39:34 +02002398psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2399 unsigned char *iv,
2400 size_t iv_size,
2401 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002402{
Moran Peker41deec42018-04-04 15:43:05 +03002403 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002404 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002405 return( PSA_ERROR_BAD_STATE );
2406 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002407 {
Moran Peker41deec42018-04-04 15:43:05 +03002408 ret = PSA_ERROR_BUFFER_TOO_SMALL;
2409 goto exit;
2410 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002411 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2412 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002413 if( ret != 0 )
2414 {
2415 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002416 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002417 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002418
mohammad16038481e742018-03-18 13:57:31 +02002419 *iv_length = operation->iv_size;
Gilles Peskinefe119512018-07-08 21:39:34 +02002420 ret = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002421
Moran Peker395db872018-05-31 14:07:14 +03002422exit:
2423 if( ret != PSA_SUCCESS )
2424 psa_cipher_abort( operation );
2425 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02002426}
2427
Gilles Peskinefe119512018-07-08 21:39:34 +02002428psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2429 const unsigned char *iv,
2430 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002431{
Moran Peker41deec42018-04-04 15:43:05 +03002432 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002433 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002434 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03002435 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002436 {
Moran Pekerae382792018-05-31 14:06:17 +03002437 psa_cipher_abort( operation );
2438 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03002439 }
mohammad1603503973b2018-03-12 15:59:30 +02002440 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_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
2447 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02002448
Moran Peker395db872018-05-31 14:07:14 +03002449 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002450}
2451
Gilles Peskinee553c652018-06-04 16:22:46 +02002452psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2453 const uint8_t *input,
2454 size_t input_length,
2455 unsigned char *output,
2456 size_t output_size,
2457 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002458{
2459 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002460 size_t expected_output_size;
2461 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2462 {
2463 /* Take the unprocessed partial block left over from previous
2464 * update calls, if any, plus the input to this call. Remove
2465 * the last partial block, if any. You get the data that will be
2466 * output in this call. */
2467 expected_output_size =
2468 ( operation->ctx.cipher.unprocessed_len + input_length )
2469 / operation->block_size * operation->block_size;
2470 }
2471 else
2472 {
2473 expected_output_size = input_length;
2474 }
2475 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03002476 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02002477
mohammad1603503973b2018-03-12 15:59:30 +02002478 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002479 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002480 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002481 {
2482 psa_cipher_abort( operation );
2483 return( mbedtls_to_psa_error( ret ) );
2484 }
2485
Moran Peker395db872018-05-31 14:07:14 +03002486 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002487}
2488
Gilles Peskinee553c652018-06-04 16:22:46 +02002489psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2490 uint8_t *output,
2491 size_t output_size,
2492 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002493{
Janos Follath315b51c2018-07-09 16:04:51 +01002494 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2495 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002496 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002497
mohammad1603503973b2018-03-12 15:59:30 +02002498 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002499 {
Janos Follath315b51c2018-07-09 16:04:51 +01002500 status = PSA_ERROR_BAD_STATE;
2501 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002502 }
2503 if( operation->iv_required && ! operation->iv_set )
2504 {
Janos Follath315b51c2018-07-09 16:04:51 +01002505 status = PSA_ERROR_BAD_STATE;
2506 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002507 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002508 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2509 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002510 {
Gilles Peskine53514202018-06-06 15:11:46 +02002511 psa_algorithm_t padding_mode =
2512 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002513 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002514 {
Janos Follath315b51c2018-07-09 16:04:51 +01002515 status = PSA_ERROR_TAMPERING_DETECTED;
2516 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002517 }
Gilles Peskine53514202018-06-06 15:11:46 +02002518 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002519 {
2520 if( operation->ctx.cipher.unprocessed_len != 0 )
2521 {
Janos Follath315b51c2018-07-09 16:04:51 +01002522 status = PSA_ERROR_INVALID_ARGUMENT;
2523 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002524 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002525 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002526 }
2527
Janos Follath315b51c2018-07-09 16:04:51 +01002528 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2529 temp_output_buffer,
2530 output_length );
2531 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002532 {
Janos Follath315b51c2018-07-09 16:04:51 +01002533 status = mbedtls_to_psa_error( cipher_ret );
2534 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002535 }
Janos Follath315b51c2018-07-09 16:04:51 +01002536
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002537 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002538 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002539 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002540 memcpy( output, temp_output_buffer, *output_length );
2541 else
2542 {
Janos Follath315b51c2018-07-09 16:04:51 +01002543 status = PSA_ERROR_BUFFER_TOO_SMALL;
2544 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002545 }
mohammad1603503973b2018-03-12 15:59:30 +02002546
Janos Follath279ab8e2018-07-09 16:13:21 +01002547 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002548 status = psa_cipher_abort( operation );
2549
2550 return( status );
2551
2552error:
2553
2554 *output_length = 0;
2555
Janos Follath279ab8e2018-07-09 16:13:21 +01002556 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002557 (void) psa_cipher_abort( operation );
2558
2559 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002560}
2561
Gilles Peskinee553c652018-06-04 16:22:46 +02002562psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2563{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002564 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002565 {
2566 /* The object has (apparently) been initialized but it is not
2567 * in use. It's ok to call abort on such an object, and there's
2568 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002569 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002570 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002571
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002572 /* Sanity check (shouldn't happen: operation->alg should
2573 * always have been initialized to a valid value). */
2574 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2575 return( PSA_ERROR_BAD_STATE );
2576
mohammad1603503973b2018-03-12 15:59:30 +02002577 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002578
Moran Peker41deec42018-04-04 15:43:05 +03002579 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002580 operation->key_set = 0;
2581 operation->iv_set = 0;
2582 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002583 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002584 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002585
Moran Peker395db872018-05-31 14:07:14 +03002586 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002587}
2588
Gilles Peskinea0655c32018-04-30 17:06:50 +02002589
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002590
mohammad16038cc1cee2018-03-28 01:21:33 +03002591/****************************************************************/
2592/* Key Policy */
2593/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002594
mohammad160327010052018-07-03 13:16:15 +03002595#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002596void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002597{
Gilles Peskine803ce742018-06-18 16:07:14 +02002598 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002599}
2600
Gilles Peskine2d277862018-06-18 15:41:12 +02002601void psa_key_policy_set_usage( psa_key_policy_t *policy,
2602 psa_key_usage_t usage,
2603 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002604{
mohammad16034eed7572018-03-28 05:14:59 -07002605 policy->usage = usage;
2606 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002607}
2608
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002609psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002610{
mohammad16036df908f2018-04-02 08:34:15 -07002611 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002612}
2613
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002614psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002615{
mohammad16036df908f2018-04-02 08:34:15 -07002616 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002617}
mohammad160327010052018-07-03 13:16:15 +03002618#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002619
Gilles Peskine2d277862018-06-18 15:41:12 +02002620psa_status_t psa_set_key_policy( psa_key_slot_t key,
2621 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002622{
2623 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002624 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002625
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002626 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002627 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002628
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002629 status = psa_get_empty_key_slot( key, &slot );
2630 if( status != PSA_SUCCESS )
2631 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002632
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002633 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2634 PSA_KEY_USAGE_ENCRYPT |
2635 PSA_KEY_USAGE_DECRYPT |
2636 PSA_KEY_USAGE_SIGN |
2637 PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002638 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002639
mohammad16036df908f2018-04-02 08:34:15 -07002640 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002641
2642 return( PSA_SUCCESS );
2643}
2644
Gilles Peskine2d277862018-06-18 15:41:12 +02002645psa_status_t psa_get_key_policy( psa_key_slot_t key,
2646 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002647{
2648 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002649 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002650
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002651 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002652 return( PSA_ERROR_INVALID_ARGUMENT );
2653
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002654 status = psa_get_key_slot( key, &slot );
2655 if( status != PSA_SUCCESS )
2656 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002657
mohammad16036df908f2018-04-02 08:34:15 -07002658 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002659
2660 return( PSA_SUCCESS );
2661}
Gilles Peskine20035e32018-02-03 22:44:14 +01002662
Gilles Peskinea0655c32018-04-30 17:06:50 +02002663
2664
mohammad1603804cd712018-03-20 22:44:08 +02002665/****************************************************************/
2666/* Key Lifetime */
2667/****************************************************************/
2668
Gilles Peskine2d277862018-06-18 15:41:12 +02002669psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2670 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002671{
2672 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002673 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002674
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002675 status = psa_get_key_slot( key, &slot );
2676 if( status != PSA_SUCCESS )
2677 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002678
mohammad1603804cd712018-03-20 22:44:08 +02002679 *lifetime = slot->lifetime;
2680
2681 return( PSA_SUCCESS );
2682}
2683
Gilles Peskine2d277862018-06-18 15:41:12 +02002684psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002685 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002686{
2687 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002688 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002689
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002690 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2691 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002692 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2693 return( PSA_ERROR_INVALID_ARGUMENT );
2694
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002695 status = psa_get_empty_key_slot( key, &slot );
2696 if( status != PSA_SUCCESS )
2697 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002698
Moran Pekerd7326592018-05-29 16:56:39 +03002699 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002700 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002701
mohammad1603060ad8a2018-03-20 14:28:38 -07002702 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002703
2704 return( PSA_SUCCESS );
2705}
2706
Gilles Peskine20035e32018-02-03 22:44:14 +01002707
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002708
mohammad16035955c982018-04-26 00:53:03 +03002709/****************************************************************/
2710/* AEAD */
2711/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002712
mohammad16035955c982018-04-26 00:53:03 +03002713psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2714 psa_algorithm_t alg,
2715 const uint8_t *nonce,
2716 size_t nonce_length,
2717 const uint8_t *additional_data,
2718 size_t additional_data_length,
2719 const uint8_t *plaintext,
2720 size_t plaintext_length,
2721 uint8_t *ciphertext,
2722 size_t ciphertext_size,
2723 size_t *ciphertext_length )
2724{
2725 int ret;
2726 psa_status_t status;
2727 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002728 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002729 uint8_t *tag;
2730 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002731 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002732 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002733
mohammad1603f08a5502018-06-03 15:05:47 +03002734 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002735
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002736 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2737 if( status != PSA_SUCCESS )
2738 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002739 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002740
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002741 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002742 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002743 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002744 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002745
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002746 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002747 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002748 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002749
mohammad16035955c982018-04-26 00:53:03 +03002750 if( alg == PSA_ALG_GCM )
2751 {
2752 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002753 tag_length = 16;
2754
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002755 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002756 return( PSA_ERROR_INVALID_ARGUMENT );
2757
mohammad160315223a82018-06-03 17:19:55 +03002758 //make sure we have place to hold the tag in the ciphertext buffer
2759 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002760 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002761
2762 //update the tag pointer to point to the end of the ciphertext_length
2763 tag = ciphertext + plaintext_length;
2764
mohammad16035955c982018-04-26 00:53:03 +03002765 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002766 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002767 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002768 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002769 if( ret != 0 )
2770 {
2771 mbedtls_gcm_free( &gcm );
2772 return( mbedtls_to_psa_error( ret ) );
2773 }
2774 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002775 plaintext_length, nonce,
2776 nonce_length, additional_data,
2777 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002778 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002779 mbedtls_gcm_free( &gcm );
2780 }
2781 else if( alg == PSA_ALG_CCM )
2782 {
2783 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002784 tag_length = 16;
2785
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002786 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002787 return( PSA_ERROR_INVALID_ARGUMENT );
2788
mohammad160347ddf3d2018-04-26 01:11:21 +03002789 if( nonce_length < 7 || nonce_length > 13 )
2790 return( PSA_ERROR_INVALID_ARGUMENT );
2791
mohammad160315223a82018-06-03 17:19:55 +03002792 //make sure we have place to hold the tag in the ciphertext buffer
2793 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002794 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002795
2796 //update the tag pointer to point to the end of the ciphertext_length
2797 tag = ciphertext + plaintext_length;
2798
mohammad16035955c982018-04-26 00:53:03 +03002799 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002800 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002801 slot->data.raw.data,
2802 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002803 if( ret != 0 )
2804 {
2805 mbedtls_ccm_free( &ccm );
2806 return( mbedtls_to_psa_error( ret ) );
2807 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002808 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002809 nonce, nonce_length,
2810 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002811 additional_data_length,
2812 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002813 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002814 mbedtls_ccm_free( &ccm );
2815 }
mohammad16035c8845f2018-05-09 05:40:09 -07002816 else
2817 {
mohammad1603554faad2018-06-03 15:07:38 +03002818 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002819 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002820
mohammad160315223a82018-06-03 17:19:55 +03002821 if( ret != 0 )
2822 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002823 /* If ciphertext_size is 0 then ciphertext may be NULL and then the
2824 * call to memset would have undefined behavior. */
2825 if( ciphertext_size != 0 )
2826 memset( ciphertext, 0, ciphertext_size );
mohammad160315223a82018-06-03 17:19:55 +03002827 return( mbedtls_to_psa_error( ret ) );
2828 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002829
mohammad160315223a82018-06-03 17:19:55 +03002830 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002831 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002832}
2833
Gilles Peskineee652a32018-06-01 19:23:52 +02002834/* Locate the tag in a ciphertext buffer containing the encrypted data
2835 * followed by the tag. Return the length of the part preceding the tag in
2836 * *plaintext_length. This is the size of the plaintext in modes where
2837 * the encrypted data has the same size as the plaintext, such as
2838 * CCM and GCM. */
2839static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2840 const uint8_t *ciphertext,
2841 size_t ciphertext_length,
2842 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002843 const uint8_t **p_tag )
2844{
2845 size_t payload_length;
2846 if( tag_length > ciphertext_length )
2847 return( PSA_ERROR_INVALID_ARGUMENT );
2848 payload_length = ciphertext_length - tag_length;
2849 if( payload_length > plaintext_size )
2850 return( PSA_ERROR_BUFFER_TOO_SMALL );
2851 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002852 return( PSA_SUCCESS );
2853}
2854
mohammad16035955c982018-04-26 00:53:03 +03002855psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2856 psa_algorithm_t alg,
2857 const uint8_t *nonce,
2858 size_t nonce_length,
2859 const uint8_t *additional_data,
2860 size_t additional_data_length,
2861 const uint8_t *ciphertext,
2862 size_t ciphertext_length,
2863 uint8_t *plaintext,
2864 size_t plaintext_size,
2865 size_t *plaintext_length )
2866{
2867 int ret;
2868 psa_status_t status;
2869 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002870 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002871 const uint8_t *tag;
2872 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002873 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002874 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002875
Gilles Peskineee652a32018-06-01 19:23:52 +02002876 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002877
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002878 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2879 if( status != PSA_SUCCESS )
2880 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002881 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002882
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002883 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002884 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002885 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002886 return( PSA_ERROR_NOT_SUPPORTED );
2887
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002888 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002889 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002890 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002891
mohammad16035955c982018-04-26 00:53:03 +03002892 if( alg == PSA_ALG_GCM )
2893 {
2894 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002895
Gilles Peskineee652a32018-06-01 19:23:52 +02002896 tag_length = 16;
2897 status = psa_aead_unpadded_locate_tag( tag_length,
2898 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002899 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002900 if( status != PSA_SUCCESS )
2901 return( status );
2902
mohammad16035955c982018-04-26 00:53:03 +03002903 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002904 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002905 slot->data.raw.data,
2906 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002907 if( ret != 0 )
2908 {
2909 mbedtls_gcm_free( &gcm );
2910 return( mbedtls_to_psa_error( ret ) );
2911 }
mohammad16035955c982018-04-26 00:53:03 +03002912
Gilles Peskineee652a32018-06-01 19:23:52 +02002913 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002914 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002915 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002916 additional_data,
2917 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002918 tag, tag_length,
2919 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002920 mbedtls_gcm_free( &gcm );
2921 }
2922 else if( alg == PSA_ALG_CCM )
2923 {
2924 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002925
mohammad160347ddf3d2018-04-26 01:11:21 +03002926 if( nonce_length < 7 || nonce_length > 13 )
2927 return( PSA_ERROR_INVALID_ARGUMENT );
2928
mohammad16039375f842018-06-03 14:28:24 +03002929 tag_length = 16;
2930 status = psa_aead_unpadded_locate_tag( tag_length,
2931 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002932 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002933 if( status != PSA_SUCCESS )
2934 return( status );
2935
mohammad16035955c982018-04-26 00:53:03 +03002936 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002937 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002938 slot->data.raw.data,
2939 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002940 if( ret != 0 )
2941 {
2942 mbedtls_ccm_free( &ccm );
2943 return( mbedtls_to_psa_error( ret ) );
2944 }
mohammad160360a64d02018-06-03 17:20:42 +03002945 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002946 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002947 additional_data,
2948 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002949 ciphertext, plaintext,
2950 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002951 mbedtls_ccm_free( &ccm );
2952 }
mohammad160339574652018-06-01 04:39:53 -07002953 else
2954 {
mohammad1603554faad2018-06-03 15:07:38 +03002955 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07002956 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002957
Gilles Peskineee652a32018-06-01 19:23:52 +02002958 if( ret != 0 )
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002959 {
2960 /* If plaintext_size is 0 then plaintext may be NULL and then the
2961 * call to memset has undefined behavior. */
2962 if( plaintext_size != 0 )
2963 memset( plaintext, 0, plaintext_size );
2964 }
mohammad160360a64d02018-06-03 17:20:42 +03002965 else
2966 *plaintext_length = ciphertext_length - tag_length;
2967
Gilles Peskineee652a32018-06-01 19:23:52 +02002968 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03002969}
2970
Gilles Peskinea0655c32018-04-30 17:06:50 +02002971
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002972
Gilles Peskine20035e32018-02-03 22:44:14 +01002973/****************************************************************/
Gilles Peskine05d69892018-06-19 22:00:52 +02002974/* Key generation */
2975/****************************************************************/
2976
2977psa_status_t psa_generate_random( uint8_t *output,
2978 size_t output_size )
2979{
2980 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2981 output, output_size );
2982 return( mbedtls_to_psa_error( ret ) );
2983}
2984
2985psa_status_t psa_generate_key( psa_key_slot_t key,
2986 psa_key_type_t type,
2987 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02002988 const void *extra,
2989 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02002990{
Gilles Peskine12313cd2018-06-20 00:20:32 +02002991 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002992 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02002993
Gilles Peskine53d991e2018-07-12 01:14:59 +02002994 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002995 return( PSA_ERROR_INVALID_ARGUMENT );
2996
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002997 status = psa_get_empty_key_slot( key, &slot );
2998 if( status != PSA_SUCCESS )
2999 return( status );
3000
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003001 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003002 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003003 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003004 if( status != PSA_SUCCESS )
3005 return( status );
3006 status = psa_generate_random( slot->data.raw.data,
3007 slot->data.raw.bytes );
3008 if( status != PSA_SUCCESS )
3009 {
3010 mbedtls_free( slot->data.raw.data );
3011 return( status );
3012 }
3013#if defined(MBEDTLS_DES_C)
3014 if( type == PSA_KEY_TYPE_DES )
3015 {
3016 mbedtls_des_key_set_parity( slot->data.raw.data );
3017 if( slot->data.raw.bytes >= 16 )
3018 mbedtls_des_key_set_parity( slot->data.raw.data + 8 );
3019 if( slot->data.raw.bytes == 24 )
3020 mbedtls_des_key_set_parity( slot->data.raw.data + 16 );
3021 }
3022#endif /* MBEDTLS_DES_C */
3023 }
3024 else
3025
3026#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3027 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3028 {
3029 mbedtls_rsa_context *rsa;
3030 int ret;
3031 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003032 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3033 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02003034 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003035 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02003036 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003037 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003038 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003039#if INT_MAX < 0xffffffff
3040 /* Check that the uint32_t value passed by the caller fits
3041 * in the range supported by this implementation. */
3042 if( p->e > INT_MAX )
3043 return( PSA_ERROR_NOT_SUPPORTED );
3044#endif
3045 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003046 }
3047 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3048 if( rsa == NULL )
3049 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3050 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3051 ret = mbedtls_rsa_gen_key( rsa,
3052 mbedtls_ctr_drbg_random,
3053 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003054 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003055 exponent );
3056 if( ret != 0 )
3057 {
3058 mbedtls_rsa_free( rsa );
3059 mbedtls_free( rsa );
3060 return( mbedtls_to_psa_error( ret ) );
3061 }
3062 slot->data.rsa = rsa;
3063 }
3064 else
3065#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3066
3067#if defined(MBEDTLS_ECP_C)
3068 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3069 {
3070 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3071 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3072 const mbedtls_ecp_curve_info *curve_info =
3073 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3074 mbedtls_ecp_keypair *ecp;
3075 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003076 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003077 return( PSA_ERROR_NOT_SUPPORTED );
3078 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3079 return( PSA_ERROR_NOT_SUPPORTED );
3080 if( curve_info->bit_size != bits )
3081 return( PSA_ERROR_INVALID_ARGUMENT );
3082 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3083 if( ecp == NULL )
3084 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3085 mbedtls_ecp_keypair_init( ecp );
3086 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3087 mbedtls_ctr_drbg_random,
3088 &global_data.ctr_drbg );
3089 if( ret != 0 )
3090 {
3091 mbedtls_ecp_keypair_free( ecp );
3092 mbedtls_free( ecp );
3093 return( mbedtls_to_psa_error( ret ) );
3094 }
3095 slot->data.ecp = ecp;
3096 }
3097 else
3098#endif /* MBEDTLS_ECP_C */
3099
3100 return( PSA_ERROR_NOT_SUPPORTED );
3101
3102 slot->type = type;
3103 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003104}
3105
3106
3107/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003108/* Module setup */
3109/****************************************************************/
3110
Gilles Peskinee59236f2018-01-27 23:32:46 +01003111void mbedtls_psa_crypto_free( void )
3112{
Jaeden Amero045bd502018-06-26 14:00:08 +01003113 psa_key_slot_t key;
Gilles Peskine828ed142018-06-18 23:25:51 +02003114 for( key = 1; key < PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003115 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003116 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3117 mbedtls_entropy_free( &global_data.entropy );
3118 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3119}
3120
3121psa_status_t psa_crypto_init( void )
3122{
3123 int ret;
3124 const unsigned char drbg_seed[] = "PSA";
3125
3126 if( global_data.initialized != 0 )
3127 return( PSA_SUCCESS );
3128
3129 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3130 mbedtls_entropy_init( &global_data.entropy );
3131 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3132
3133 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3134 mbedtls_entropy_func,
3135 &global_data.entropy,
3136 drbg_seed, sizeof( drbg_seed ) - 1 );
3137 if( ret != 0 )
3138 goto exit;
3139
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003140 global_data.initialized = 1;
3141
Gilles Peskinee59236f2018-01-27 23:32:46 +01003142exit:
3143 if( ret != 0 )
3144 mbedtls_psa_crypto_free( );
3145 return( mbedtls_to_psa_error( ret ) );
3146}
3147
3148#endif /* MBEDTLS_PSA_CRYPTO_C */