blob: 94f6b1776fae085b86e46a7dda76af84e41fad05 [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 Peskine46f1fd72018-06-28 19:31:31 +0200501#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200502 case PSA_KEY_TYPE_DERIVE:
503 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200504#if defined(MBEDTLS_AES_C)
505 case PSA_KEY_TYPE_AES:
506 if( bits != 128 && bits != 192 && bits != 256 )
507 return( PSA_ERROR_INVALID_ARGUMENT );
508 break;
509#endif
510#if defined(MBEDTLS_CAMELLIA_C)
511 case PSA_KEY_TYPE_CAMELLIA:
512 if( bits != 128 && bits != 192 && bits != 256 )
513 return( PSA_ERROR_INVALID_ARGUMENT );
514 break;
515#endif
516#if defined(MBEDTLS_DES_C)
517 case PSA_KEY_TYPE_DES:
518 if( bits != 64 && bits != 128 && bits != 192 )
519 return( PSA_ERROR_INVALID_ARGUMENT );
520 break;
521#endif
522#if defined(MBEDTLS_ARC4_C)
523 case PSA_KEY_TYPE_ARC4:
524 if( bits < 8 || bits > 2048 )
525 return( PSA_ERROR_INVALID_ARGUMENT );
526 break;
527#endif
528 default:
529 return( PSA_ERROR_NOT_SUPPORTED );
530 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200531 if( bits % 8 != 0 )
532 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200533
534 /* Allocate memory for the key */
535 raw->bytes = PSA_BITS_TO_BYTES( bits );
536 raw->data = mbedtls_calloc( 1, raw->bytes );
537 if( raw->data == NULL )
538 {
539 raw->bytes = 0;
540 return( PSA_ERROR_INSUFFICIENT_MEMORY );
541 }
542 return( PSA_SUCCESS );
543}
544
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200545#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
546static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
547 mbedtls_rsa_context **p_rsa )
548{
549 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
550 return( PSA_ERROR_INVALID_ARGUMENT );
551 else
552 {
553 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
554 size_t bits = mbedtls_rsa_get_bitlen( rsa );
555 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
556 return( PSA_ERROR_NOT_SUPPORTED );
557 *p_rsa = rsa;
558 return( PSA_SUCCESS );
559 }
560}
561#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
562
563#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
564static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
565 mbedtls_pk_context *pk,
566 mbedtls_ecp_keypair **p_ecp )
567{
568 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
569 return( PSA_ERROR_INVALID_ARGUMENT );
570 else
571 {
572 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
573 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
574 if( actual_curve != expected_curve )
575 return( PSA_ERROR_INVALID_ARGUMENT );
576 *p_ecp = ecp;
577 return( PSA_SUCCESS );
578 }
579}
580#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
581
Gilles Peskine2d277862018-06-18 15:41:12 +0200582psa_status_t psa_import_key( psa_key_slot_t key,
583 psa_key_type_t type,
584 const uint8_t *data,
585 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100586{
587 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200588 psa_status_t status = PSA_SUCCESS;
589 status = psa_get_empty_key_slot( key, &slot );
590 if( status != PSA_SUCCESS )
591 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100592
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200593 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100594 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100595 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100596 if( data_length > SIZE_MAX / 8 )
597 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200598 status = prepare_raw_data_slot( type,
599 PSA_BYTES_TO_BITS( data_length ),
600 &slot->data.raw );
601 if( status != PSA_SUCCESS )
602 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200603 if( data_length != 0 )
604 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100605 }
606 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100607#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200608 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100609 {
610 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100611 mbedtls_pk_context pk;
612 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200613
614 /* Parse the data. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100615 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
616 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
617 else
618 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100619 if( ret != 0 )
620 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200621
622 /* We have something that the pkparse module recognizes.
623 * If it has the expected type and passes any type-specific
624 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100625#if defined(MBEDTLS_RSA_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200626 if( PSA_KEY_TYPE_IS_RSA( type ) )
627 status = psa_import_rsa_key( &pk, &slot->data.rsa );
628 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100629#endif /* MBEDTLS_RSA_C */
630#if defined(MBEDTLS_ECP_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200631 if( PSA_KEY_TYPE_IS_ECC( type ) )
632 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( type ),
633 &pk, &slot->data.ecp );
634 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100635#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200636 {
637 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200638 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200639
Gilles Peskinec648d692018-06-28 08:46:13 +0200640 /* Free the content of the pk object only on error. On success,
641 * the content of the object has been stored in the slot. */
642 if( status != PSA_SUCCESS )
643 {
644 mbedtls_pk_free( &pk );
645 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100646 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100647 }
648 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100649#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100650 {
651 return( PSA_ERROR_NOT_SUPPORTED );
652 }
653
654 slot->type = type;
655 return( PSA_SUCCESS );
656}
657
Gilles Peskine2d277862018-06-18 15:41:12 +0200658psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100659{
660 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200661 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100662
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200663 status = psa_get_key_slot( key, &slot );
664 if( status != PSA_SUCCESS )
665 return( status );
666
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100667 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200668 {
669 /* No key material to clean, but do zeroize the slot below to wipe
670 * metadata such as policies. */
671 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200672 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673 {
674 mbedtls_free( slot->data.raw.data );
675 }
676 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100677#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200678 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100679 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100680 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100681 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100682 }
683 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100684#endif /* defined(MBEDTLS_RSA_C) */
685#if defined(MBEDTLS_ECP_C)
686 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
687 {
688 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100689 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100690 }
691 else
692#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100693 {
694 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100695 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100696 return( PSA_ERROR_TAMPERING_DETECTED );
697 }
698
699 mbedtls_zeroize( slot, sizeof( *slot ) );
700 return( PSA_SUCCESS );
701}
702
Gilles Peskineb870b182018-07-06 16:02:09 +0200703/* Return the size of the key in the given slot, in bits. */
704static size_t psa_get_key_bits( const key_slot_t *slot )
705{
706 if( key_type_is_raw_bytes( slot->type ) )
707 return( slot->data.raw.bytes * 8 );
708#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200709 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineb870b182018-07-06 16:02:09 +0200710 return( mbedtls_rsa_get_bitlen( slot->data.rsa ) );
711#endif /* defined(MBEDTLS_RSA_C) */
712#if defined(MBEDTLS_ECP_C)
713 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
714 return( slot->data.ecp->grp.pbits );
715#endif /* defined(MBEDTLS_ECP_C) */
716 /* Shouldn't happen except on an empty slot. */
717 return( 0 );
718}
719
Gilles Peskine2d277862018-06-18 15:41:12 +0200720psa_status_t psa_get_key_information( psa_key_slot_t key,
721 psa_key_type_t *type,
722 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100723{
724 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200725 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100726
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100727 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200728 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100729 if( bits != NULL )
730 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200731 status = psa_get_key_slot( key, &slot );
732 if( status != PSA_SUCCESS )
733 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200734
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100735 if( slot->type == PSA_KEY_TYPE_NONE )
736 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200737 if( type != NULL )
738 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200739 if( bits != NULL )
740 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100741 return( PSA_SUCCESS );
742}
743
Gilles Peskine2d277862018-06-18 15:41:12 +0200744static psa_status_t psa_internal_export_key( psa_key_slot_t key,
745 uint8_t *data,
746 size_t data_size,
747 size_t *data_length,
748 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100749{
750 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200751 psa_status_t status;
752 /* Exporting a public key doesn't require a usage flag. If we're
753 * called by psa_export_public_key(), don't require the EXPORT flag.
754 * If we're called by psa_export_key(), do require the EXPORT flag;
755 * if the key turns out to be public key object, psa_get_key_from_slot()
756 * will ignore this flag. */
757 psa_key_usage_t usage = export_public_key ? 0 : PSA_KEY_USAGE_EXPORT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100758
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100759 /* Set the key to empty now, so that even when there are errors, we always
760 * set data_length to a value between 0 and data_size. On error, setting
761 * the key to empty is a good choice because an empty key representation is
762 * unlikely to be accepted anywhere. */
763 *data_length = 0;
764
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200765 status = psa_get_key_from_slot( key, &slot, usage, 0 );
766 if( status != PSA_SUCCESS )
767 return( status );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200768 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300769 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300770
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200771 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100772 {
773 if( slot->data.raw.bytes > data_size )
774 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200775 if( slot->data.raw.bytes != 0 )
776 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100777 *data_length = slot->data.raw.bytes;
778 return( PSA_SUCCESS );
779 }
780 else
Moran Peker17e36e12018-05-02 12:55:20 +0300781 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100782#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200783 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300784 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100785 {
Moran Pekera998bc62018-04-16 18:16:20 +0300786 mbedtls_pk_context pk;
787 int ret;
788 mbedtls_pk_init( &pk );
Gilles Peskined8008d62018-06-29 19:51:51 +0200789 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300790 {
791 pk.pk_info = &mbedtls_rsa_info;
792 pk.pk_ctx = slot->data.rsa;
793 }
794 else
795 {
796 pk.pk_info = &mbedtls_eckey_info;
797 pk.pk_ctx = slot->data.ecp;
798 }
Moran Pekerd7326592018-05-29 16:56:39 +0300799 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300800 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300801 else
802 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300803 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200804 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200805 /* If data_size is 0 then data may be NULL and then the
806 * call to memset would have undefined behavior. */
807 if( data_size != 0 )
808 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300809 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200810 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200811 /* The mbedtls_pk_xxx functions write to the end of the buffer.
812 * Move the data to the beginning and erase remaining data
813 * at the original location. */
814 if( 2 * (size_t) ret <= data_size )
815 {
816 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200817 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200818 }
819 else if( (size_t) ret < data_size )
820 {
821 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200822 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200823 }
Moran Pekera998bc62018-04-16 18:16:20 +0300824 *data_length = ret;
825 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100826 }
827 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100828#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300829 {
830 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200831 it is valid for a special-purpose implementation to omit
832 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300833 return( PSA_ERROR_NOT_SUPPORTED );
834 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100835 }
836}
837
Gilles Peskine2d277862018-06-18 15:41:12 +0200838psa_status_t psa_export_key( psa_key_slot_t key,
839 uint8_t *data,
840 size_t data_size,
841 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300842{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200843 return( psa_internal_export_key( key, data, data_size,
844 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100845}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100846
Gilles Peskine2d277862018-06-18 15:41:12 +0200847psa_status_t psa_export_public_key( psa_key_slot_t key,
848 uint8_t *data,
849 size_t data_size,
850 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300851{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200852 return( psa_internal_export_key( key, data, data_size,
853 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300854}
855
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200856
857
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100858/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100859/* Message digests */
860/****************************************************************/
861
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100862static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100863{
864 switch( alg )
865 {
866#if defined(MBEDTLS_MD2_C)
867 case PSA_ALG_MD2:
868 return( &mbedtls_md2_info );
869#endif
870#if defined(MBEDTLS_MD4_C)
871 case PSA_ALG_MD4:
872 return( &mbedtls_md4_info );
873#endif
874#if defined(MBEDTLS_MD5_C)
875 case PSA_ALG_MD5:
876 return( &mbedtls_md5_info );
877#endif
878#if defined(MBEDTLS_RIPEMD160_C)
879 case PSA_ALG_RIPEMD160:
880 return( &mbedtls_ripemd160_info );
881#endif
882#if defined(MBEDTLS_SHA1_C)
883 case PSA_ALG_SHA_1:
884 return( &mbedtls_sha1_info );
885#endif
886#if defined(MBEDTLS_SHA256_C)
887 case PSA_ALG_SHA_224:
888 return( &mbedtls_sha224_info );
889 case PSA_ALG_SHA_256:
890 return( &mbedtls_sha256_info );
891#endif
892#if defined(MBEDTLS_SHA512_C)
893 case PSA_ALG_SHA_384:
894 return( &mbedtls_sha384_info );
895 case PSA_ALG_SHA_512:
896 return( &mbedtls_sha512_info );
897#endif
898 default:
899 return( NULL );
900 }
901}
902
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100903psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
904{
905 switch( operation->alg )
906 {
Gilles Peskine81736312018-06-26 15:04:31 +0200907 case 0:
908 /* The object has (apparently) been initialized but it is not
909 * in use. It's ok to call abort on such an object, and there's
910 * nothing to do. */
911 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100912#if defined(MBEDTLS_MD2_C)
913 case PSA_ALG_MD2:
914 mbedtls_md2_free( &operation->ctx.md2 );
915 break;
916#endif
917#if defined(MBEDTLS_MD4_C)
918 case PSA_ALG_MD4:
919 mbedtls_md4_free( &operation->ctx.md4 );
920 break;
921#endif
922#if defined(MBEDTLS_MD5_C)
923 case PSA_ALG_MD5:
924 mbedtls_md5_free( &operation->ctx.md5 );
925 break;
926#endif
927#if defined(MBEDTLS_RIPEMD160_C)
928 case PSA_ALG_RIPEMD160:
929 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
930 break;
931#endif
932#if defined(MBEDTLS_SHA1_C)
933 case PSA_ALG_SHA_1:
934 mbedtls_sha1_free( &operation->ctx.sha1 );
935 break;
936#endif
937#if defined(MBEDTLS_SHA256_C)
938 case PSA_ALG_SHA_224:
939 case PSA_ALG_SHA_256:
940 mbedtls_sha256_free( &operation->ctx.sha256 );
941 break;
942#endif
943#if defined(MBEDTLS_SHA512_C)
944 case PSA_ALG_SHA_384:
945 case PSA_ALG_SHA_512:
946 mbedtls_sha512_free( &operation->ctx.sha512 );
947 break;
948#endif
949 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +0200950 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100951 }
952 operation->alg = 0;
953 return( PSA_SUCCESS );
954}
955
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200956psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100957 psa_algorithm_t alg )
958{
959 int ret;
960 operation->alg = 0;
961 switch( alg )
962 {
963#if defined(MBEDTLS_MD2_C)
964 case PSA_ALG_MD2:
965 mbedtls_md2_init( &operation->ctx.md2 );
966 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
967 break;
968#endif
969#if defined(MBEDTLS_MD4_C)
970 case PSA_ALG_MD4:
971 mbedtls_md4_init( &operation->ctx.md4 );
972 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
973 break;
974#endif
975#if defined(MBEDTLS_MD5_C)
976 case PSA_ALG_MD5:
977 mbedtls_md5_init( &operation->ctx.md5 );
978 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
979 break;
980#endif
981#if defined(MBEDTLS_RIPEMD160_C)
982 case PSA_ALG_RIPEMD160:
983 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
984 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
985 break;
986#endif
987#if defined(MBEDTLS_SHA1_C)
988 case PSA_ALG_SHA_1:
989 mbedtls_sha1_init( &operation->ctx.sha1 );
990 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
991 break;
992#endif
993#if defined(MBEDTLS_SHA256_C)
994 case PSA_ALG_SHA_224:
995 mbedtls_sha256_init( &operation->ctx.sha256 );
996 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
997 break;
998 case PSA_ALG_SHA_256:
999 mbedtls_sha256_init( &operation->ctx.sha256 );
1000 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1001 break;
1002#endif
1003#if defined(MBEDTLS_SHA512_C)
1004 case PSA_ALG_SHA_384:
1005 mbedtls_sha512_init( &operation->ctx.sha512 );
1006 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1007 break;
1008 case PSA_ALG_SHA_512:
1009 mbedtls_sha512_init( &operation->ctx.sha512 );
1010 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1011 break;
1012#endif
1013 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001014 return( PSA_ALG_IS_HASH( alg ) ?
1015 PSA_ERROR_NOT_SUPPORTED :
1016 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001017 }
1018 if( ret == 0 )
1019 operation->alg = alg;
1020 else
1021 psa_hash_abort( operation );
1022 return( mbedtls_to_psa_error( ret ) );
1023}
1024
1025psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1026 const uint8_t *input,
1027 size_t input_length )
1028{
1029 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001030
1031 /* Don't require hash implementations to behave correctly on a
1032 * zero-length input, which may have an invalid pointer. */
1033 if( input_length == 0 )
1034 return( PSA_SUCCESS );
1035
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001036 switch( operation->alg )
1037 {
1038#if defined(MBEDTLS_MD2_C)
1039 case PSA_ALG_MD2:
1040 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1041 input, input_length );
1042 break;
1043#endif
1044#if defined(MBEDTLS_MD4_C)
1045 case PSA_ALG_MD4:
1046 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1047 input, input_length );
1048 break;
1049#endif
1050#if defined(MBEDTLS_MD5_C)
1051 case PSA_ALG_MD5:
1052 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1053 input, input_length );
1054 break;
1055#endif
1056#if defined(MBEDTLS_RIPEMD160_C)
1057 case PSA_ALG_RIPEMD160:
1058 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1059 input, input_length );
1060 break;
1061#endif
1062#if defined(MBEDTLS_SHA1_C)
1063 case PSA_ALG_SHA_1:
1064 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1065 input, input_length );
1066 break;
1067#endif
1068#if defined(MBEDTLS_SHA256_C)
1069 case PSA_ALG_SHA_224:
1070 case PSA_ALG_SHA_256:
1071 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1072 input, input_length );
1073 break;
1074#endif
1075#if defined(MBEDTLS_SHA512_C)
1076 case PSA_ALG_SHA_384:
1077 case PSA_ALG_SHA_512:
1078 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1079 input, input_length );
1080 break;
1081#endif
1082 default:
1083 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1084 break;
1085 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001086
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001087 if( ret != 0 )
1088 psa_hash_abort( operation );
1089 return( mbedtls_to_psa_error( ret ) );
1090}
1091
1092psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1093 uint8_t *hash,
1094 size_t hash_size,
1095 size_t *hash_length )
1096{
1097 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001098 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001099
1100 /* Fill the output buffer with something that isn't a valid hash
1101 * (barring an attack on the hash and deliberately-crafted input),
1102 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001103 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001104 /* If hash_size is 0 then hash may be NULL and then the
1105 * call to memset would have undefined behavior. */
1106 if( hash_size != 0 )
1107 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001108
1109 if( hash_size < actual_hash_length )
1110 return( PSA_ERROR_BUFFER_TOO_SMALL );
1111
1112 switch( operation->alg )
1113 {
1114#if defined(MBEDTLS_MD2_C)
1115 case PSA_ALG_MD2:
1116 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1117 break;
1118#endif
1119#if defined(MBEDTLS_MD4_C)
1120 case PSA_ALG_MD4:
1121 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1122 break;
1123#endif
1124#if defined(MBEDTLS_MD5_C)
1125 case PSA_ALG_MD5:
1126 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1127 break;
1128#endif
1129#if defined(MBEDTLS_RIPEMD160_C)
1130 case PSA_ALG_RIPEMD160:
1131 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1132 break;
1133#endif
1134#if defined(MBEDTLS_SHA1_C)
1135 case PSA_ALG_SHA_1:
1136 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1137 break;
1138#endif
1139#if defined(MBEDTLS_SHA256_C)
1140 case PSA_ALG_SHA_224:
1141 case PSA_ALG_SHA_256:
1142 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1143 break;
1144#endif
1145#if defined(MBEDTLS_SHA512_C)
1146 case PSA_ALG_SHA_384:
1147 case PSA_ALG_SHA_512:
1148 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1149 break;
1150#endif
1151 default:
1152 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1153 break;
1154 }
1155
1156 if( ret == 0 )
1157 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001158 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001159 return( psa_hash_abort( operation ) );
1160 }
1161 else
1162 {
1163 psa_hash_abort( operation );
1164 return( mbedtls_to_psa_error( ret ) );
1165 }
1166}
1167
Gilles Peskine2d277862018-06-18 15:41:12 +02001168psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1169 const uint8_t *hash,
1170 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001171{
1172 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1173 size_t actual_hash_length;
1174 psa_status_t status = psa_hash_finish( operation,
1175 actual_hash, sizeof( actual_hash ),
1176 &actual_hash_length );
1177 if( status != PSA_SUCCESS )
1178 return( status );
1179 if( actual_hash_length != hash_length )
1180 return( PSA_ERROR_INVALID_SIGNATURE );
1181 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1182 return( PSA_ERROR_INVALID_SIGNATURE );
1183 return( PSA_SUCCESS );
1184}
1185
1186
1187
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001188/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001189/* MAC */
1190/****************************************************************/
1191
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001192static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001193 psa_algorithm_t alg,
1194 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001195 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001196 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001197{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001198 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001199 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001200
1201 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1202 {
1203 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001204 {
1205 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1206 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001207
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001208 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001209 {
Gilles Peskine5d1888e2018-07-12 00:32:42 +02001210 case PSA_ALG_STREAM_CIPHER_BASE:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001211 mode = MBEDTLS_MODE_STREAM;
1212 break;
1213 case PSA_ALG_CBC_BASE:
1214 mode = MBEDTLS_MODE_CBC;
1215 break;
1216 case PSA_ALG_CFB_BASE:
1217 mode = MBEDTLS_MODE_CFB;
1218 break;
1219 case PSA_ALG_OFB_BASE:
1220 mode = MBEDTLS_MODE_OFB;
1221 break;
1222 case PSA_ALG_CTR:
1223 mode = MBEDTLS_MODE_CTR;
1224 break;
1225 case PSA_ALG_CCM:
1226 mode = MBEDTLS_MODE_CCM;
1227 break;
1228 case PSA_ALG_GCM:
1229 mode = MBEDTLS_MODE_GCM;
1230 break;
1231 default:
1232 return( NULL );
1233 }
1234 }
1235 else if( alg == PSA_ALG_CMAC )
1236 mode = MBEDTLS_MODE_ECB;
1237 else if( alg == PSA_ALG_GMAC )
1238 mode = MBEDTLS_MODE_GCM;
1239 else
1240 return( NULL );
1241
1242 switch( key_type )
1243 {
1244 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001245 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001246 break;
1247 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001248 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1249 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001250 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001251 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001252 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001253 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001254 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1255 * but two-key Triple-DES is functionally three-key Triple-DES
1256 * with K1=K3, so that's how we present it to mbedtls. */
1257 if( key_bits == 128 )
1258 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001259 break;
1260 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001261 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001262 break;
1263 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001264 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001265 break;
1266 default:
1267 return( NULL );
1268 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001269 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001270 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001271
Jaeden Amero23bbb752018-06-26 14:16:54 +01001272 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1273 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001274}
1275
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001276static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001277{
Gilles Peskine2d277862018-06-18 15:41:12 +02001278 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001279 {
1280 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001281 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001282 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001283 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001284 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001285 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001286 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001287 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001288 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001289 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001290 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001291 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001292 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001293 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001294 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001295 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001296 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001297 return( 128 );
1298 default:
1299 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001300 }
1301}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001302
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001303/* Initialize the MAC operation structure. Once this function has been
1304 * called, psa_mac_abort can run and will do the right thing. */
1305static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1306 psa_algorithm_t alg )
1307{
1308 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1309
1310 operation->alg = alg;
1311 operation->key_set = 0;
1312 operation->iv_set = 0;
1313 operation->iv_required = 0;
1314 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001315 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001316
1317#if defined(MBEDTLS_CMAC_C)
1318 if( alg == PSA_ALG_CMAC )
1319 {
1320 operation->iv_required = 0;
1321 mbedtls_cipher_init( &operation->ctx.cmac );
1322 status = PSA_SUCCESS;
1323 }
1324 else
1325#endif /* MBEDTLS_CMAC_C */
1326#if defined(MBEDTLS_MD_C)
1327 if( PSA_ALG_IS_HMAC( operation->alg ) )
1328 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001329 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1330 operation->ctx.hmac.hash_ctx.alg = 0;
1331 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001332 }
1333 else
1334#endif /* MBEDTLS_MD_C */
1335 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001336 if( ! PSA_ALG_IS_MAC( alg ) )
1337 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001338 }
1339
1340 if( status != PSA_SUCCESS )
1341 memset( operation, 0, sizeof( *operation ) );
1342 return( status );
1343}
1344
Gilles Peskine01126fa2018-07-12 17:04:55 +02001345#if defined(MBEDTLS_MD_C)
1346static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1347{
1348 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1349 return( psa_hash_abort( &hmac->hash_ctx ) );
1350}
1351#endif /* MBEDTLS_MD_C */
1352
Gilles Peskine8c9def32018-02-08 10:02:12 +01001353psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1354{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001355 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001356 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001357 /* The object has (apparently) been initialized but it is not
1358 * in use. It's ok to call abort on such an object, and there's
1359 * nothing to do. */
1360 return( PSA_SUCCESS );
1361 }
1362 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001363#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001364 if( operation->alg == PSA_ALG_CMAC )
1365 {
1366 mbedtls_cipher_free( &operation->ctx.cmac );
1367 }
1368 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001369#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001370#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001371 if( PSA_ALG_IS_HMAC( operation->alg ) )
1372 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001373 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001374 }
1375 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001376#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001377 {
1378 /* Sanity check (shouldn't happen: operation->alg should
1379 * always have been initialized to a valid value). */
1380 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001381 }
Moran Peker41deec42018-04-04 15:43:05 +03001382
Gilles Peskine8c9def32018-02-08 10:02:12 +01001383 operation->alg = 0;
1384 operation->key_set = 0;
1385 operation->iv_set = 0;
1386 operation->iv_required = 0;
1387 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001388 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001389
Gilles Peskine8c9def32018-02-08 10:02:12 +01001390 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001391
1392bad_state:
1393 /* If abort is called on an uninitialized object, we can't trust
1394 * anything. Wipe the object in case it contains confidential data.
1395 * This may result in a memory leak if a pointer gets overwritten,
1396 * but it's too late to do anything about this. */
1397 memset( operation, 0, sizeof( *operation ) );
1398 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001399}
1400
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001401#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001402static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001403 size_t key_bits,
1404 key_slot_t *slot,
1405 const mbedtls_cipher_info_t *cipher_info )
1406{
1407 int ret;
1408
1409 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001410
1411 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1412 if( ret != 0 )
1413 return( ret );
1414
1415 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1416 slot->data.raw.data,
1417 key_bits );
1418 return( ret );
1419}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001420#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001421
Gilles Peskine248051a2018-06-20 16:09:38 +02001422#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001423static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1424 const uint8_t *key,
1425 size_t key_length,
1426 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001427{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001428 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001429 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001430 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001431 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001432 psa_status_t status;
1433
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001434 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1435 * overflow below. This should never trigger if the hash algorithm
1436 * is implemented correctly. */
1437 /* The size checks against the ipad and opad buffers cannot be written
1438 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1439 * because that triggers -Wlogical-op on GCC 7.3. */
1440 if( block_size > sizeof( ipad ) )
1441 return( PSA_ERROR_NOT_SUPPORTED );
1442 if( block_size > sizeof( hmac->opad ) )
1443 return( PSA_ERROR_NOT_SUPPORTED );
1444 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001445 return( PSA_ERROR_NOT_SUPPORTED );
1446
Gilles Peskined223b522018-06-11 18:12:58 +02001447 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001448 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001449 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001450 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001451 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001452 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001453 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001454 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001455 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001456 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001457 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001458 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001459 }
Gilles Peskine96889972018-07-12 17:07:03 +02001460 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1461 * but it is permitted. It is common when HMAC is used in HKDF, for
1462 * example. Don't call `memcpy` in the 0-length because `key` could be
1463 * an invalid pointer which would make the behavior undefined. */
1464 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001465 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001466
Gilles Peskined223b522018-06-11 18:12:58 +02001467 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1468 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001469 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001470 ipad[i] ^= 0x36;
1471 memset( ipad + key_length, 0x36, block_size - key_length );
1472
1473 /* Copy the key material from ipad to opad, flipping the requisite bits,
1474 * and filling the rest of opad with the requisite constant. */
1475 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001476 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1477 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001478
Gilles Peskine01126fa2018-07-12 17:04:55 +02001479 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001480 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001481 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001482
Gilles Peskine01126fa2018-07-12 17:04:55 +02001483 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001484
1485cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001486 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001487
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001488 return( status );
1489}
Gilles Peskine248051a2018-06-20 16:09:38 +02001490#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001491
Gilles Peskine89167cb2018-07-08 20:12:23 +02001492static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1493 psa_key_slot_t key,
1494 psa_algorithm_t alg,
1495 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001496{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001497 psa_status_t status;
1498 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001499 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001500 psa_key_usage_t usage =
1501 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001502
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001503 status = psa_mac_init( operation, alg );
1504 if( status != PSA_SUCCESS )
1505 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001506 if( is_sign )
1507 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001508
Gilles Peskine89167cb2018-07-08 20:12:23 +02001509 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001510 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001511 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001512 key_bits = psa_get_key_bits( slot );
1513
Gilles Peskine8c9def32018-02-08 10:02:12 +01001514#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001515 if( alg == PSA_ALG_CMAC )
1516 {
1517 const mbedtls_cipher_info_t *cipher_info =
1518 mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
1519 int ret;
1520 if( cipher_info == NULL )
1521 {
1522 status = PSA_ERROR_NOT_SUPPORTED;
1523 goto exit;
1524 }
1525 operation->mac_size = cipher_info->block_size;
1526 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1527 status = mbedtls_to_psa_error( ret );
1528 }
1529 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001530#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001531#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001532 if( PSA_ALG_IS_HMAC( alg ) )
1533 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001534 psa_algorithm_t hash_alg = PSA_ALG_HMAC_HASH( alg );
1535 if( hash_alg == 0 )
1536 {
1537 status = PSA_ERROR_NOT_SUPPORTED;
1538 goto exit;
1539 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001540
1541 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1542 /* Sanity check. This shouldn't fail on a valid configuration. */
1543 if( operation->mac_size == 0 ||
1544 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1545 {
1546 status = PSA_ERROR_NOT_SUPPORTED;
1547 goto exit;
1548 }
1549
Gilles Peskine01126fa2018-07-12 17:04:55 +02001550 if( slot->type != PSA_KEY_TYPE_HMAC )
1551 {
1552 status = PSA_ERROR_INVALID_ARGUMENT;
1553 goto exit;
1554 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001555
Gilles Peskine01126fa2018-07-12 17:04:55 +02001556 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1557 slot->data.raw.data,
1558 slot->data.raw.bytes,
1559 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001560 }
1561 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001562#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001563 {
1564 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001565 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001566
Gilles Peskinefbfac682018-07-08 20:51:54 +02001567exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001568 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001569 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001570 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001571 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001572 else
1573 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001574 operation->key_set = 1;
1575 }
1576 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001577}
1578
Gilles Peskine89167cb2018-07-08 20:12:23 +02001579psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1580 psa_key_slot_t key,
1581 psa_algorithm_t alg )
1582{
1583 return( psa_mac_setup( operation, key, alg, 1 ) );
1584}
1585
1586psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1587 psa_key_slot_t key,
1588 psa_algorithm_t alg )
1589{
1590 return( psa_mac_setup( operation, key, alg, 0 ) );
1591}
1592
Gilles Peskine8c9def32018-02-08 10:02:12 +01001593psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1594 const uint8_t *input,
1595 size_t input_length )
1596{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001597 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001598 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001599 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001600 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001601 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001602 operation->has_input = 1;
1603
Gilles Peskine8c9def32018-02-08 10:02:12 +01001604#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001605 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001606 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001607 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1608 input, input_length );
1609 status = mbedtls_to_psa_error( ret );
1610 }
1611 else
1612#endif /* MBEDTLS_CMAC_C */
1613#if defined(MBEDTLS_MD_C)
1614 if( PSA_ALG_IS_HMAC( operation->alg ) )
1615 {
1616 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1617 input_length );
1618 }
1619 else
1620#endif /* MBEDTLS_MD_C */
1621 {
1622 /* This shouldn't happen if `operation` was initialized by
1623 * a setup function. */
1624 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001625 }
1626
Gilles Peskinefbfac682018-07-08 20:51:54 +02001627cleanup:
1628 if( status != PSA_SUCCESS )
1629 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001630 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001631}
1632
Gilles Peskine01126fa2018-07-12 17:04:55 +02001633#if defined(MBEDTLS_MD_C)
1634static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1635 uint8_t *mac,
1636 size_t mac_size )
1637{
1638 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1639 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1640 size_t hash_size = 0;
1641 size_t block_size = psa_get_hash_block_size( hash_alg );
1642 psa_status_t status;
1643
Gilles Peskine01126fa2018-07-12 17:04:55 +02001644 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1645 if( status != PSA_SUCCESS )
1646 return( status );
1647 /* From here on, tmp needs to be wiped. */
1648
1649 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1650 if( status != PSA_SUCCESS )
1651 goto exit;
1652
1653 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1654 if( status != PSA_SUCCESS )
1655 goto exit;
1656
1657 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1658 if( status != PSA_SUCCESS )
1659 goto exit;
1660
1661 status = psa_hash_finish( &hmac->hash_ctx, mac, mac_size, &hash_size );
1662
1663exit:
1664 mbedtls_zeroize( tmp, hash_size );
1665 return( status );
1666}
1667#endif /* MBEDTLS_MD_C */
1668
mohammad16036df908f2018-04-02 08:34:15 -07001669static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001670 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001671 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001672{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001673 if( ! operation->key_set )
1674 return( PSA_ERROR_BAD_STATE );
1675 if( operation->iv_required && ! operation->iv_set )
1676 return( PSA_ERROR_BAD_STATE );
1677
Gilles Peskine8c9def32018-02-08 10:02:12 +01001678 if( mac_size < operation->mac_size )
1679 return( PSA_ERROR_BUFFER_TOO_SMALL );
1680
Gilles Peskine8c9def32018-02-08 10:02:12 +01001681#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001682 if( operation->alg == PSA_ALG_CMAC )
1683 {
1684 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1685 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001686 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001687 else
1688#endif /* MBEDTLS_CMAC_C */
1689#if defined(MBEDTLS_MD_C)
1690 if( PSA_ALG_IS_HMAC( operation->alg ) )
1691 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001692 return( psa_hmac_finish_internal( &operation->ctx.hmac,
1693 mac, mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001694 }
1695 else
1696#endif /* MBEDTLS_MD_C */
1697 {
1698 /* This shouldn't happen if `operation` was initialized by
1699 * a setup function. */
1700 return( PSA_ERROR_BAD_STATE );
1701 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001702}
1703
Gilles Peskineacd4be32018-07-08 19:56:25 +02001704psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1705 uint8_t *mac,
1706 size_t mac_size,
1707 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001708{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001709 psa_status_t status;
1710
1711 /* Fill the output buffer with something that isn't a valid mac
1712 * (barring an attack on the mac and deliberately-crafted input),
1713 * in case the caller doesn't check the return status properly. */
1714 *mac_length = mac_size;
1715 /* If mac_size is 0 then mac may be NULL and then the
1716 * call to memset would have undefined behavior. */
1717 if( mac_size != 0 )
1718 memset( mac, '!', mac_size );
1719
Gilles Peskine89167cb2018-07-08 20:12:23 +02001720 if( ! operation->is_sign )
1721 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001722 status = PSA_ERROR_BAD_STATE;
1723 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001724 }
mohammad16036df908f2018-04-02 08:34:15 -07001725
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001726 status = psa_mac_finish_internal( operation, mac, mac_size );
1727
1728cleanup:
1729 if( status == PSA_SUCCESS )
1730 {
1731 status = psa_mac_abort( operation );
1732 if( status == PSA_SUCCESS )
1733 *mac_length = operation->mac_size;
1734 else
1735 memset( mac, '!', mac_size );
1736 }
1737 else
1738 psa_mac_abort( operation );
1739 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001740}
1741
Gilles Peskineacd4be32018-07-08 19:56:25 +02001742psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1743 const uint8_t *mac,
1744 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001745{
Gilles Peskine828ed142018-06-18 23:25:51 +02001746 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001747 psa_status_t status;
1748
Gilles Peskine89167cb2018-07-08 20:12:23 +02001749 if( operation->is_sign )
1750 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001751 status = PSA_ERROR_BAD_STATE;
1752 goto cleanup;
1753 }
1754 if( operation->mac_size != mac_length )
1755 {
1756 status = PSA_ERROR_INVALID_SIGNATURE;
1757 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001758 }
mohammad16036df908f2018-04-02 08:34:15 -07001759
1760 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001761 actual_mac, sizeof( actual_mac ) );
1762
1763 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1764 status = PSA_ERROR_INVALID_SIGNATURE;
1765
1766cleanup:
1767 if( status == PSA_SUCCESS )
1768 status = psa_mac_abort( operation );
1769 else
1770 psa_mac_abort( operation );
1771
1772 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001773}
1774
1775
Gilles Peskine20035e32018-02-03 22:44:14 +01001776
Gilles Peskine20035e32018-02-03 22:44:14 +01001777/****************************************************************/
1778/* Asymmetric cryptography */
1779/****************************************************************/
1780
Gilles Peskine2b450e32018-06-27 15:42:46 +02001781#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001782/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001783 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001784static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1785 size_t hash_length,
1786 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001787{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001788 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001789 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001790 *md_alg = mbedtls_md_get_type( md_info );
1791
1792 /* The Mbed TLS RSA module uses an unsigned int for hash length
1793 * parameters. Validate that it fits so that we don't risk an
1794 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001795#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001796 if( hash_length > UINT_MAX )
1797 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001798#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001799
1800#if defined(MBEDTLS_PKCS1_V15)
1801 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
1802 * must be correct. */
1803 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
1804 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001805 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001806 if( md_info == NULL )
1807 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001808 if( mbedtls_md_get_size( md_info ) != hash_length )
1809 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001810 }
1811#endif /* MBEDTLS_PKCS1_V15 */
1812
1813#if defined(MBEDTLS_PKCS1_V21)
1814 /* PSS requires a hash internally. */
1815 if( PSA_ALG_IS_RSA_PSS( alg ) )
1816 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001817 if( md_info == NULL )
1818 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001819 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001820#endif /* MBEDTLS_PKCS1_V21 */
1821
Gilles Peskine61b91d42018-06-08 16:09:36 +02001822 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001823}
1824
Gilles Peskine2b450e32018-06-27 15:42:46 +02001825static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1826 psa_algorithm_t alg,
1827 const uint8_t *hash,
1828 size_t hash_length,
1829 uint8_t *signature,
1830 size_t signature_size,
1831 size_t *signature_length )
1832{
1833 psa_status_t status;
1834 int ret;
1835 mbedtls_md_type_t md_alg;
1836
1837 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1838 if( status != PSA_SUCCESS )
1839 return( status );
1840
Gilles Peskine630a18a2018-06-29 17:49:35 +02001841 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001842 return( PSA_ERROR_BUFFER_TOO_SMALL );
1843
1844#if defined(MBEDTLS_PKCS1_V15)
1845 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1846 {
1847 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1848 MBEDTLS_MD_NONE );
1849 ret = mbedtls_rsa_pkcs1_sign( rsa,
1850 mbedtls_ctr_drbg_random,
1851 &global_data.ctr_drbg,
1852 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001853 md_alg,
1854 (unsigned int) hash_length,
1855 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001856 signature );
1857 }
1858 else
1859#endif /* MBEDTLS_PKCS1_V15 */
1860#if defined(MBEDTLS_PKCS1_V21)
1861 if( PSA_ALG_IS_RSA_PSS( alg ) )
1862 {
1863 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1864 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1865 mbedtls_ctr_drbg_random,
1866 &global_data.ctr_drbg,
1867 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001868 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001869 (unsigned int) hash_length,
1870 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001871 signature );
1872 }
1873 else
1874#endif /* MBEDTLS_PKCS1_V21 */
1875 {
1876 return( PSA_ERROR_INVALID_ARGUMENT );
1877 }
1878
1879 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02001880 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02001881 return( mbedtls_to_psa_error( ret ) );
1882}
1883
1884static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1885 psa_algorithm_t alg,
1886 const uint8_t *hash,
1887 size_t hash_length,
1888 const uint8_t *signature,
1889 size_t signature_length )
1890{
1891 psa_status_t status;
1892 int ret;
1893 mbedtls_md_type_t md_alg;
1894
1895 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1896 if( status != PSA_SUCCESS )
1897 return( status );
1898
Gilles Peskine630a18a2018-06-29 17:49:35 +02001899 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001900 return( PSA_ERROR_BUFFER_TOO_SMALL );
1901
1902#if defined(MBEDTLS_PKCS1_V15)
1903 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1904 {
1905 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1906 MBEDTLS_MD_NONE );
1907 ret = mbedtls_rsa_pkcs1_verify( rsa,
1908 mbedtls_ctr_drbg_random,
1909 &global_data.ctr_drbg,
1910 MBEDTLS_RSA_PUBLIC,
1911 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001912 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001913 hash,
1914 signature );
1915 }
1916 else
1917#endif /* MBEDTLS_PKCS1_V15 */
1918#if defined(MBEDTLS_PKCS1_V21)
1919 if( PSA_ALG_IS_RSA_PSS( alg ) )
1920 {
1921 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1922 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1923 mbedtls_ctr_drbg_random,
1924 &global_data.ctr_drbg,
1925 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001926 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001927 (unsigned int) hash_length,
1928 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001929 signature );
1930 }
1931 else
1932#endif /* MBEDTLS_PKCS1_V21 */
1933 {
1934 return( PSA_ERROR_INVALID_ARGUMENT );
1935 }
1936 return( mbedtls_to_psa_error( ret ) );
1937}
1938#endif /* MBEDTLS_RSA_C */
1939
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001940#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001941/* `ecp` cannot be const because `ecp->grp` needs to be non-const
1942 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
1943 * (even though these functions don't modify it). */
1944static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
1945 psa_algorithm_t alg,
1946 const uint8_t *hash,
1947 size_t hash_length,
1948 uint8_t *signature,
1949 size_t signature_size,
1950 size_t *signature_length )
1951{
1952 int ret;
1953 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001954 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001955 mbedtls_mpi_init( &r );
1956 mbedtls_mpi_init( &s );
1957
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001958 if( signature_size < 2 * curve_bytes )
1959 {
1960 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
1961 goto cleanup;
1962 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001963
1964 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
1965 {
1966 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
1967 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1968 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
1969 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
1970 hash, hash_length,
1971 md_alg ) );
1972 }
1973 else
1974 {
1975 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
1976 hash, hash_length,
1977 mbedtls_ctr_drbg_random,
1978 &global_data.ctr_drbg ) );
1979 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001980
1981 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
1982 signature,
1983 curve_bytes ) );
1984 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
1985 signature + curve_bytes,
1986 curve_bytes ) );
1987
1988cleanup:
1989 mbedtls_mpi_free( &r );
1990 mbedtls_mpi_free( &s );
1991 if( ret == 0 )
1992 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001993 return( mbedtls_to_psa_error( ret ) );
1994}
1995
1996static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
1997 const uint8_t *hash,
1998 size_t hash_length,
1999 const uint8_t *signature,
2000 size_t signature_length )
2001{
2002 int ret;
2003 mbedtls_mpi r, s;
2004 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2005 mbedtls_mpi_init( &r );
2006 mbedtls_mpi_init( &s );
2007
2008 if( signature_length != 2 * curve_bytes )
2009 return( PSA_ERROR_INVALID_SIGNATURE );
2010
2011 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2012 signature,
2013 curve_bytes ) );
2014 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2015 signature + curve_bytes,
2016 curve_bytes ) );
2017
2018 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2019 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002020
2021cleanup:
2022 mbedtls_mpi_free( &r );
2023 mbedtls_mpi_free( &s );
2024 return( mbedtls_to_psa_error( ret ) );
2025}
2026#endif /* MBEDTLS_ECDSA_C */
2027
Gilles Peskine61b91d42018-06-08 16:09:36 +02002028psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2029 psa_algorithm_t alg,
2030 const uint8_t *hash,
2031 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002032 uint8_t *signature,
2033 size_t signature_size,
2034 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002035{
2036 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002037 psa_status_t status;
2038
2039 *signature_length = signature_size;
2040
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002041 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2042 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002043 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002044 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002045 {
2046 status = PSA_ERROR_INVALID_ARGUMENT;
2047 goto exit;
2048 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002049
Gilles Peskine20035e32018-02-03 22:44:14 +01002050#if defined(MBEDTLS_RSA_C)
2051 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2052 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002053 status = psa_rsa_sign( slot->data.rsa,
2054 alg,
2055 hash, hash_length,
2056 signature, signature_size,
2057 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002058 }
2059 else
2060#endif /* defined(MBEDTLS_RSA_C) */
2061#if defined(MBEDTLS_ECP_C)
2062 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2063 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002064#if defined(MBEDTLS_ECDSA_C)
2065 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002066 status = psa_ecdsa_sign( slot->data.ecp,
2067 alg,
2068 hash, hash_length,
2069 signature, signature_size,
2070 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002071 else
2072#endif /* defined(MBEDTLS_ECDSA_C) */
2073 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002074 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002075 }
itayzafrir5c753392018-05-08 11:18:38 +03002076 }
2077 else
2078#endif /* defined(MBEDTLS_ECP_C) */
2079 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002080 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002081 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002082
2083exit:
2084 /* Fill the unused part of the output buffer (the whole buffer on error,
2085 * the trailing part on success) with something that isn't a valid mac
2086 * (barring an attack on the mac and deliberately-crafted input),
2087 * in case the caller doesn't check the return status properly. */
2088 if( status == PSA_SUCCESS )
2089 memset( signature + *signature_length, '!',
2090 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002091 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002092 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002093 /* If signature_size is 0 then we have nothing to do. We must not call
2094 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002095 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002096}
2097
2098psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2099 psa_algorithm_t alg,
2100 const uint8_t *hash,
2101 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002102 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002103 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002104{
2105 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002106 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002107
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002108 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2109 if( status != PSA_SUCCESS )
2110 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002111
Gilles Peskine61b91d42018-06-08 16:09:36 +02002112#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002113 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002114 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002115 return( psa_rsa_verify( slot->data.rsa,
2116 alg,
2117 hash, hash_length,
2118 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002119 }
2120 else
2121#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002122#if defined(MBEDTLS_ECP_C)
2123 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2124 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002125#if defined(MBEDTLS_ECDSA_C)
2126 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002127 return( psa_ecdsa_verify( slot->data.ecp,
2128 hash, hash_length,
2129 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002130 else
2131#endif /* defined(MBEDTLS_ECDSA_C) */
2132 {
2133 return( PSA_ERROR_INVALID_ARGUMENT );
2134 }
itayzafrir5c753392018-05-08 11:18:38 +03002135 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002136 else
2137#endif /* defined(MBEDTLS_ECP_C) */
2138 {
2139 return( PSA_ERROR_NOT_SUPPORTED );
2140 }
2141}
2142
Gilles Peskine072ac562018-06-30 00:21:29 +02002143#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2144static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2145 mbedtls_rsa_context *rsa )
2146{
2147 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2148 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2149 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2150 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2151}
2152#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2153
Gilles Peskine61b91d42018-06-08 16:09:36 +02002154psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2155 psa_algorithm_t alg,
2156 const uint8_t *input,
2157 size_t input_length,
2158 const uint8_t *salt,
2159 size_t salt_length,
2160 uint8_t *output,
2161 size_t output_size,
2162 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002163{
2164 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002165 psa_status_t status;
2166
Darryl Green5cc689a2018-07-24 15:34:10 +01002167 (void) input;
2168 (void) input_length;
2169 (void) salt;
2170 (void) output;
2171 (void) output_size;
2172
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002173 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002174
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002175 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2176 return( PSA_ERROR_INVALID_ARGUMENT );
2177
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002178 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2179 if( status != PSA_SUCCESS )
2180 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002181 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2182 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002183 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002184
2185#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002186 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002187 {
2188 mbedtls_rsa_context *rsa = slot->data.rsa;
2189 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002190 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002191 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002192#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002193 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002194 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002195 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2196 mbedtls_ctr_drbg_random,
2197 &global_data.ctr_drbg,
2198 MBEDTLS_RSA_PUBLIC,
2199 input_length,
2200 input,
2201 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002202 }
2203 else
2204#endif /* MBEDTLS_PKCS1_V15 */
2205#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002206 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002207 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002208 psa_rsa_oaep_set_padding_mode( alg, rsa );
2209 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2210 mbedtls_ctr_drbg_random,
2211 &global_data.ctr_drbg,
2212 MBEDTLS_RSA_PUBLIC,
2213 salt, salt_length,
2214 input_length,
2215 input,
2216 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002217 }
2218 else
2219#endif /* MBEDTLS_PKCS1_V21 */
2220 {
2221 return( PSA_ERROR_INVALID_ARGUMENT );
2222 }
2223 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002224 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002225 return( mbedtls_to_psa_error( ret ) );
2226 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002227 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002228#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002229 {
2230 return( PSA_ERROR_NOT_SUPPORTED );
2231 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002232}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002233
Gilles Peskine61b91d42018-06-08 16:09:36 +02002234psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2235 psa_algorithm_t alg,
2236 const uint8_t *input,
2237 size_t input_length,
2238 const uint8_t *salt,
2239 size_t salt_length,
2240 uint8_t *output,
2241 size_t output_size,
2242 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002243{
2244 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002245 psa_status_t status;
2246
Darryl Green5cc689a2018-07-24 15:34:10 +01002247 (void) input;
2248 (void) input_length;
2249 (void) salt;
2250 (void) output;
2251 (void) output_size;
2252
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002253 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002254
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002255 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2256 return( PSA_ERROR_INVALID_ARGUMENT );
2257
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002258 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2259 if( status != PSA_SUCCESS )
2260 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002261 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2262 return( PSA_ERROR_INVALID_ARGUMENT );
2263
2264#if defined(MBEDTLS_RSA_C)
2265 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2266 {
2267 mbedtls_rsa_context *rsa = slot->data.rsa;
2268 int ret;
2269
Gilles Peskine630a18a2018-06-29 17:49:35 +02002270 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002271 return( PSA_ERROR_INVALID_ARGUMENT );
2272
2273#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002274 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002275 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002276 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2277 mbedtls_ctr_drbg_random,
2278 &global_data.ctr_drbg,
2279 MBEDTLS_RSA_PRIVATE,
2280 output_length,
2281 input,
2282 output,
2283 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002284 }
2285 else
2286#endif /* MBEDTLS_PKCS1_V15 */
2287#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002288 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002289 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002290 psa_rsa_oaep_set_padding_mode( alg, rsa );
2291 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2292 mbedtls_ctr_drbg_random,
2293 &global_data.ctr_drbg,
2294 MBEDTLS_RSA_PRIVATE,
2295 salt, salt_length,
2296 output_length,
2297 input,
2298 output,
2299 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002300 }
2301 else
2302#endif /* MBEDTLS_PKCS1_V21 */
2303 {
2304 return( PSA_ERROR_INVALID_ARGUMENT );
2305 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002306
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002307 return( mbedtls_to_psa_error( ret ) );
2308 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002309 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002310#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002311 {
2312 return( PSA_ERROR_NOT_SUPPORTED );
2313 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002314}
Gilles Peskine20035e32018-02-03 22:44:14 +01002315
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002316
2317
mohammad1603503973b2018-03-12 15:59:30 +02002318/****************************************************************/
2319/* Symmetric cryptography */
2320/****************************************************************/
2321
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002322/* Initialize the cipher operation structure. Once this function has been
2323 * called, psa_cipher_abort can run and will do the right thing. */
2324static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2325 psa_algorithm_t alg )
2326{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002327 if( ! PSA_ALG_IS_CIPHER( alg ) )
2328 {
2329 memset( operation, 0, sizeof( *operation ) );
2330 return( PSA_ERROR_INVALID_ARGUMENT );
2331 }
2332
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002333 operation->alg = alg;
2334 operation->key_set = 0;
2335 operation->iv_set = 0;
2336 operation->iv_required = 1;
2337 operation->iv_size = 0;
2338 operation->block_size = 0;
2339 mbedtls_cipher_init( &operation->ctx.cipher );
2340 return( PSA_SUCCESS );
2341}
2342
Gilles Peskinee553c652018-06-04 16:22:46 +02002343static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2344 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002345 psa_algorithm_t alg,
2346 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002347{
2348 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2349 psa_status_t status;
2350 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002351 size_t key_bits;
2352 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002353 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2354 PSA_KEY_USAGE_ENCRYPT :
2355 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002356
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002357 status = psa_cipher_init( operation, alg );
2358 if( status != PSA_SUCCESS )
2359 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002360
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002361 status = psa_get_key_from_slot( key, &slot, usage, alg);
2362 if( status != PSA_SUCCESS )
2363 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002364 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002365
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002366 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002367 if( cipher_info == NULL )
2368 return( PSA_ERROR_NOT_SUPPORTED );
2369
mohammad1603503973b2018-03-12 15:59:30 +02002370 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002371 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002372 {
2373 psa_cipher_abort( operation );
2374 return( mbedtls_to_psa_error( ret ) );
2375 }
2376
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002377#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002378 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002379 {
2380 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2381 unsigned char keys[24];
2382 memcpy( keys, slot->data.raw.data, 16 );
2383 memcpy( keys + 16, slot->data.raw.data, 8 );
2384 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2385 keys,
2386 192, cipher_operation );
2387 }
2388 else
2389#endif
2390 {
2391 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2392 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002393 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002394 }
Moran Peker41deec42018-04-04 15:43:05 +03002395 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002396 {
2397 psa_cipher_abort( operation );
2398 return( mbedtls_to_psa_error( ret ) );
2399 }
2400
mohammad16038481e742018-03-18 13:57:31 +02002401#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002402 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002403 {
Gilles Peskine53514202018-06-06 15:11:46 +02002404 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2405 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002406
Moran Pekera28258c2018-05-29 16:25:04 +03002407 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002408 {
2409 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2410 mode = MBEDTLS_PADDING_PKCS7;
2411 break;
2412 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2413 mode = MBEDTLS_PADDING_NONE;
2414 break;
2415 default:
Moran Pekerae382792018-05-31 14:06:17 +03002416 psa_cipher_abort( operation );
2417 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002418 }
2419 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002420 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002421 {
2422 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002423 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002424 }
mohammad16038481e742018-03-18 13:57:31 +02002425 }
2426#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2427
mohammad1603503973b2018-03-12 15:59:30 +02002428 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002429 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002430 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) :
Gilles Peskine7e928852018-06-04 16:23:10 +02002431 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002432 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002433 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002434 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002435 }
mohammad1603503973b2018-03-12 15:59:30 +02002436
Moran Peker395db872018-05-31 14:07:14 +03002437 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002438}
2439
Gilles Peskinefe119512018-07-08 21:39:34 +02002440psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2441 psa_key_slot_t key,
2442 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002443{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002444 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002445}
2446
Gilles Peskinefe119512018-07-08 21:39:34 +02002447psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2448 psa_key_slot_t key,
2449 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002450{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002451 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002452}
2453
Gilles Peskinefe119512018-07-08 21:39:34 +02002454psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2455 unsigned char *iv,
2456 size_t iv_size,
2457 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002458{
Moran Peker41deec42018-04-04 15:43:05 +03002459 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002460 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002461 return( PSA_ERROR_BAD_STATE );
2462 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002463 {
Moran Peker41deec42018-04-04 15:43:05 +03002464 ret = PSA_ERROR_BUFFER_TOO_SMALL;
2465 goto exit;
2466 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002467 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2468 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002469 if( ret != 0 )
2470 {
2471 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002472 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002473 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002474
mohammad16038481e742018-03-18 13:57:31 +02002475 *iv_length = operation->iv_size;
Gilles Peskinefe119512018-07-08 21:39:34 +02002476 ret = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002477
Moran Peker395db872018-05-31 14:07:14 +03002478exit:
2479 if( ret != PSA_SUCCESS )
2480 psa_cipher_abort( operation );
2481 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02002482}
2483
Gilles Peskinefe119512018-07-08 21:39:34 +02002484psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2485 const unsigned char *iv,
2486 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002487{
Moran Peker41deec42018-04-04 15:43:05 +03002488 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002489 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002490 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03002491 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002492 {
Moran Pekerae382792018-05-31 14:06:17 +03002493 psa_cipher_abort( operation );
2494 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03002495 }
mohammad1603503973b2018-03-12 15:59:30 +02002496 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002497 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002498 {
2499 psa_cipher_abort( operation );
2500 return( mbedtls_to_psa_error( ret ) );
2501 }
2502
2503 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02002504
Moran Peker395db872018-05-31 14:07:14 +03002505 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002506}
2507
Gilles Peskinee553c652018-06-04 16:22:46 +02002508psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2509 const uint8_t *input,
2510 size_t input_length,
2511 unsigned char *output,
2512 size_t output_size,
2513 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002514{
2515 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002516 size_t expected_output_size;
2517 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2518 {
2519 /* Take the unprocessed partial block left over from previous
2520 * update calls, if any, plus the input to this call. Remove
2521 * the last partial block, if any. You get the data that will be
2522 * output in this call. */
2523 expected_output_size =
2524 ( operation->ctx.cipher.unprocessed_len + input_length )
2525 / operation->block_size * operation->block_size;
2526 }
2527 else
2528 {
2529 expected_output_size = input_length;
2530 }
2531 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03002532 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02002533
mohammad1603503973b2018-03-12 15:59:30 +02002534 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002535 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002536 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002537 {
2538 psa_cipher_abort( operation );
2539 return( mbedtls_to_psa_error( ret ) );
2540 }
2541
Moran Peker395db872018-05-31 14:07:14 +03002542 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002543}
2544
Gilles Peskinee553c652018-06-04 16:22:46 +02002545psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2546 uint8_t *output,
2547 size_t output_size,
2548 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002549{
Janos Follath315b51c2018-07-09 16:04:51 +01002550 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2551 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002552 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002553
mohammad1603503973b2018-03-12 15:59:30 +02002554 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002555 {
Janos Follath315b51c2018-07-09 16:04:51 +01002556 status = PSA_ERROR_BAD_STATE;
2557 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002558 }
2559 if( operation->iv_required && ! operation->iv_set )
2560 {
Janos Follath315b51c2018-07-09 16:04:51 +01002561 status = PSA_ERROR_BAD_STATE;
2562 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002563 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002564 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2565 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002566 {
Gilles Peskine53514202018-06-06 15:11:46 +02002567 psa_algorithm_t padding_mode =
2568 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002569 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002570 {
Janos Follath315b51c2018-07-09 16:04:51 +01002571 status = PSA_ERROR_TAMPERING_DETECTED;
2572 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002573 }
Gilles Peskine53514202018-06-06 15:11:46 +02002574 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002575 {
2576 if( operation->ctx.cipher.unprocessed_len != 0 )
2577 {
Janos Follath315b51c2018-07-09 16:04:51 +01002578 status = PSA_ERROR_INVALID_ARGUMENT;
2579 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002580 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002581 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002582 }
2583
Janos Follath315b51c2018-07-09 16:04:51 +01002584 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2585 temp_output_buffer,
2586 output_length );
2587 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002588 {
Janos Follath315b51c2018-07-09 16:04:51 +01002589 status = mbedtls_to_psa_error( cipher_ret );
2590 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002591 }
Janos Follath315b51c2018-07-09 16:04:51 +01002592
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002593 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002594 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002595 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002596 memcpy( output, temp_output_buffer, *output_length );
2597 else
2598 {
Janos Follath315b51c2018-07-09 16:04:51 +01002599 status = PSA_ERROR_BUFFER_TOO_SMALL;
2600 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002601 }
mohammad1603503973b2018-03-12 15:59:30 +02002602
Janos Follath279ab8e2018-07-09 16:13:21 +01002603 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002604 status = psa_cipher_abort( operation );
2605
2606 return( status );
2607
2608error:
2609
2610 *output_length = 0;
2611
Janos Follath279ab8e2018-07-09 16:13:21 +01002612 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002613 (void) psa_cipher_abort( operation );
2614
2615 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002616}
2617
Gilles Peskinee553c652018-06-04 16:22:46 +02002618psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2619{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002620 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002621 {
2622 /* The object has (apparently) been initialized but it is not
2623 * in use. It's ok to call abort on such an object, and there's
2624 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002625 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002626 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002627
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002628 /* Sanity check (shouldn't happen: operation->alg should
2629 * always have been initialized to a valid value). */
2630 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2631 return( PSA_ERROR_BAD_STATE );
2632
mohammad1603503973b2018-03-12 15:59:30 +02002633 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002634
Moran Peker41deec42018-04-04 15:43:05 +03002635 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002636 operation->key_set = 0;
2637 operation->iv_set = 0;
2638 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002639 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002640 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002641
Moran Peker395db872018-05-31 14:07:14 +03002642 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002643}
2644
Gilles Peskinea0655c32018-04-30 17:06:50 +02002645
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002646
mohammad16038cc1cee2018-03-28 01:21:33 +03002647/****************************************************************/
2648/* Key Policy */
2649/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002650
mohammad160327010052018-07-03 13:16:15 +03002651#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002652void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002653{
Gilles Peskine803ce742018-06-18 16:07:14 +02002654 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002655}
2656
Gilles Peskine2d277862018-06-18 15:41:12 +02002657void psa_key_policy_set_usage( psa_key_policy_t *policy,
2658 psa_key_usage_t usage,
2659 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002660{
mohammad16034eed7572018-03-28 05:14:59 -07002661 policy->usage = usage;
2662 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002663}
2664
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002665psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002666{
mohammad16036df908f2018-04-02 08:34:15 -07002667 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002668}
2669
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002670psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002671{
mohammad16036df908f2018-04-02 08:34:15 -07002672 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002673}
mohammad160327010052018-07-03 13:16:15 +03002674#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002675
Gilles Peskine2d277862018-06-18 15:41:12 +02002676psa_status_t psa_set_key_policy( psa_key_slot_t key,
2677 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002678{
2679 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002680 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002681
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002682 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002683 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002684
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002685 status = psa_get_empty_key_slot( key, &slot );
2686 if( status != PSA_SUCCESS )
2687 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002688
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002689 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2690 PSA_KEY_USAGE_ENCRYPT |
2691 PSA_KEY_USAGE_DECRYPT |
2692 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002693 PSA_KEY_USAGE_VERIFY |
2694 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002695 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002696
mohammad16036df908f2018-04-02 08:34:15 -07002697 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002698
2699 return( PSA_SUCCESS );
2700}
2701
Gilles Peskine2d277862018-06-18 15:41:12 +02002702psa_status_t psa_get_key_policy( psa_key_slot_t key,
2703 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002704{
2705 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002706 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002707
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002708 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002709 return( PSA_ERROR_INVALID_ARGUMENT );
2710
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002711 status = psa_get_key_slot( key, &slot );
2712 if( status != PSA_SUCCESS )
2713 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002714
mohammad16036df908f2018-04-02 08:34:15 -07002715 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002716
2717 return( PSA_SUCCESS );
2718}
Gilles Peskine20035e32018-02-03 22:44:14 +01002719
Gilles Peskinea0655c32018-04-30 17:06:50 +02002720
2721
mohammad1603804cd712018-03-20 22:44:08 +02002722/****************************************************************/
2723/* Key Lifetime */
2724/****************************************************************/
2725
Gilles Peskine2d277862018-06-18 15:41:12 +02002726psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2727 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002728{
2729 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002730 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002731
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002732 status = psa_get_key_slot( key, &slot );
2733 if( status != PSA_SUCCESS )
2734 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002735
mohammad1603804cd712018-03-20 22:44:08 +02002736 *lifetime = slot->lifetime;
2737
2738 return( PSA_SUCCESS );
2739}
2740
Gilles Peskine2d277862018-06-18 15:41:12 +02002741psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002742 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002743{
2744 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002745 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002746
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002747 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2748 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002749 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2750 return( PSA_ERROR_INVALID_ARGUMENT );
2751
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002752 status = psa_get_empty_key_slot( key, &slot );
2753 if( status != PSA_SUCCESS )
2754 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002755
Moran Pekerd7326592018-05-29 16:56:39 +03002756 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002757 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002758
mohammad1603060ad8a2018-03-20 14:28:38 -07002759 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002760
2761 return( PSA_SUCCESS );
2762}
2763
Gilles Peskine20035e32018-02-03 22:44:14 +01002764
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002765
mohammad16035955c982018-04-26 00:53:03 +03002766/****************************************************************/
2767/* AEAD */
2768/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002769
mohammad16035955c982018-04-26 00:53:03 +03002770psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2771 psa_algorithm_t alg,
2772 const uint8_t *nonce,
2773 size_t nonce_length,
2774 const uint8_t *additional_data,
2775 size_t additional_data_length,
2776 const uint8_t *plaintext,
2777 size_t plaintext_length,
2778 uint8_t *ciphertext,
2779 size_t ciphertext_size,
2780 size_t *ciphertext_length )
2781{
2782 int ret;
2783 psa_status_t status;
2784 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002785 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002786 uint8_t *tag;
2787 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002788 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002789 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002790
mohammad1603f08a5502018-06-03 15:05:47 +03002791 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002792
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002793 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2794 if( status != PSA_SUCCESS )
2795 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002796 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002797
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002798 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002799 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002800 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002801 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002802
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002803 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002804 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002805 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002806
mohammad16035955c982018-04-26 00:53:03 +03002807 if( alg == PSA_ALG_GCM )
2808 {
2809 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002810 tag_length = 16;
2811
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002812 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002813 return( PSA_ERROR_INVALID_ARGUMENT );
2814
mohammad160315223a82018-06-03 17:19:55 +03002815 //make sure we have place to hold the tag in the ciphertext buffer
2816 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002817 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002818
2819 //update the tag pointer to point to the end of the ciphertext_length
2820 tag = ciphertext + plaintext_length;
2821
mohammad16035955c982018-04-26 00:53:03 +03002822 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002823 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002824 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002825 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002826 if( ret != 0 )
2827 {
2828 mbedtls_gcm_free( &gcm );
2829 return( mbedtls_to_psa_error( ret ) );
2830 }
2831 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002832 plaintext_length, nonce,
2833 nonce_length, additional_data,
2834 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002835 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002836 mbedtls_gcm_free( &gcm );
2837 }
2838 else if( alg == PSA_ALG_CCM )
2839 {
2840 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002841 tag_length = 16;
2842
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002843 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002844 return( PSA_ERROR_INVALID_ARGUMENT );
2845
mohammad160347ddf3d2018-04-26 01:11:21 +03002846 if( nonce_length < 7 || nonce_length > 13 )
2847 return( PSA_ERROR_INVALID_ARGUMENT );
2848
mohammad160315223a82018-06-03 17:19:55 +03002849 //make sure we have place to hold the tag in the ciphertext buffer
2850 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002851 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002852
2853 //update the tag pointer to point to the end of the ciphertext_length
2854 tag = ciphertext + plaintext_length;
2855
mohammad16035955c982018-04-26 00:53:03 +03002856 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002857 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002858 slot->data.raw.data,
2859 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002860 if( ret != 0 )
2861 {
2862 mbedtls_ccm_free( &ccm );
2863 return( mbedtls_to_psa_error( ret ) );
2864 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002865 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002866 nonce, nonce_length,
2867 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002868 additional_data_length,
2869 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002870 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002871 mbedtls_ccm_free( &ccm );
2872 }
mohammad16035c8845f2018-05-09 05:40:09 -07002873 else
2874 {
mohammad1603554faad2018-06-03 15:07:38 +03002875 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002876 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002877
mohammad160315223a82018-06-03 17:19:55 +03002878 if( ret != 0 )
2879 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002880 /* If ciphertext_size is 0 then ciphertext may be NULL and then the
2881 * call to memset would have undefined behavior. */
2882 if( ciphertext_size != 0 )
2883 memset( ciphertext, 0, ciphertext_size );
mohammad160315223a82018-06-03 17:19:55 +03002884 return( mbedtls_to_psa_error( ret ) );
2885 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002886
mohammad160315223a82018-06-03 17:19:55 +03002887 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002888 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002889}
2890
Gilles Peskineee652a32018-06-01 19:23:52 +02002891/* Locate the tag in a ciphertext buffer containing the encrypted data
2892 * followed by the tag. Return the length of the part preceding the tag in
2893 * *plaintext_length. This is the size of the plaintext in modes where
2894 * the encrypted data has the same size as the plaintext, such as
2895 * CCM and GCM. */
2896static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2897 const uint8_t *ciphertext,
2898 size_t ciphertext_length,
2899 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002900 const uint8_t **p_tag )
2901{
2902 size_t payload_length;
2903 if( tag_length > ciphertext_length )
2904 return( PSA_ERROR_INVALID_ARGUMENT );
2905 payload_length = ciphertext_length - tag_length;
2906 if( payload_length > plaintext_size )
2907 return( PSA_ERROR_BUFFER_TOO_SMALL );
2908 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002909 return( PSA_SUCCESS );
2910}
2911
mohammad16035955c982018-04-26 00:53:03 +03002912psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2913 psa_algorithm_t alg,
2914 const uint8_t *nonce,
2915 size_t nonce_length,
2916 const uint8_t *additional_data,
2917 size_t additional_data_length,
2918 const uint8_t *ciphertext,
2919 size_t ciphertext_length,
2920 uint8_t *plaintext,
2921 size_t plaintext_size,
2922 size_t *plaintext_length )
2923{
2924 int ret;
2925 psa_status_t status;
2926 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002927 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002928 const uint8_t *tag;
2929 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002930 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002931 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002932
Gilles Peskineee652a32018-06-01 19:23:52 +02002933 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002934
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002935 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2936 if( status != PSA_SUCCESS )
2937 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002938 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002939
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002940 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002941 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002942 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002943 return( PSA_ERROR_NOT_SUPPORTED );
2944
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002945 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002946 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002947 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002948
mohammad16035955c982018-04-26 00:53:03 +03002949 if( alg == PSA_ALG_GCM )
2950 {
2951 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002952
Gilles Peskineee652a32018-06-01 19:23:52 +02002953 tag_length = 16;
2954 status = psa_aead_unpadded_locate_tag( tag_length,
2955 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002956 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002957 if( status != PSA_SUCCESS )
2958 return( status );
2959
mohammad16035955c982018-04-26 00:53:03 +03002960 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002961 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002962 slot->data.raw.data,
2963 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002964 if( ret != 0 )
2965 {
2966 mbedtls_gcm_free( &gcm );
2967 return( mbedtls_to_psa_error( ret ) );
2968 }
mohammad16035955c982018-04-26 00:53:03 +03002969
Gilles Peskineee652a32018-06-01 19:23:52 +02002970 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002971 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002972 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002973 additional_data,
2974 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002975 tag, tag_length,
2976 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002977 mbedtls_gcm_free( &gcm );
2978 }
2979 else if( alg == PSA_ALG_CCM )
2980 {
2981 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002982
mohammad160347ddf3d2018-04-26 01:11:21 +03002983 if( nonce_length < 7 || nonce_length > 13 )
2984 return( PSA_ERROR_INVALID_ARGUMENT );
2985
mohammad16039375f842018-06-03 14:28:24 +03002986 tag_length = 16;
2987 status = psa_aead_unpadded_locate_tag( tag_length,
2988 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002989 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002990 if( status != PSA_SUCCESS )
2991 return( status );
2992
mohammad16035955c982018-04-26 00:53:03 +03002993 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002994 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002995 slot->data.raw.data,
2996 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002997 if( ret != 0 )
2998 {
2999 mbedtls_ccm_free( &ccm );
3000 return( mbedtls_to_psa_error( ret ) );
3001 }
mohammad160360a64d02018-06-03 17:20:42 +03003002 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003003 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003004 additional_data,
3005 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003006 ciphertext, plaintext,
3007 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03003008 mbedtls_ccm_free( &ccm );
3009 }
mohammad160339574652018-06-01 04:39:53 -07003010 else
3011 {
mohammad1603554faad2018-06-03 15:07:38 +03003012 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003013 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003014
Gilles Peskineee652a32018-06-01 19:23:52 +02003015 if( ret != 0 )
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003016 {
3017 /* If plaintext_size is 0 then plaintext may be NULL and then the
3018 * call to memset has undefined behavior. */
3019 if( plaintext_size != 0 )
3020 memset( plaintext, 0, plaintext_size );
3021 }
mohammad160360a64d02018-06-03 17:20:42 +03003022 else
3023 *plaintext_length = ciphertext_length - tag_length;
3024
Gilles Peskineee652a32018-06-01 19:23:52 +02003025 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03003026}
3027
Gilles Peskinea0655c32018-04-30 17:06:50 +02003028
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003029
Gilles Peskine20035e32018-02-03 22:44:14 +01003030/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003031/* Generators */
3032/****************************************************************/
3033
3034psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3035{
3036 psa_status_t status = PSA_SUCCESS;
3037 if( generator->alg == 0 )
3038 {
3039 /* The object has (apparently) been initialized but it is not
3040 * in use. It's ok to call abort on such an object, and there's
3041 * nothing to do. */
3042 }
3043 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003044#if defined(MBEDTLS_MD_C)
3045 if( PSA_ALG_IS_HKDF( generator->alg ) )
3046 {
3047 mbedtls_free( generator->ctx.hkdf.info );
3048 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3049 }
3050 else
3051#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003052 {
3053 status = PSA_ERROR_BAD_STATE;
3054 }
3055 memset( generator, 0, sizeof( *generator ) );
3056 return( status );
3057}
3058
3059
3060psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3061 size_t *capacity)
3062{
3063 *capacity = generator->capacity;
3064 return( PSA_SUCCESS );
3065}
3066
Gilles Peskinebef7f142018-07-12 17:22:21 +02003067#if defined(MBEDTLS_MD_C)
3068/* Read some bytes from an HKDF-based generator. This performs a chunk
3069 * of the expand phase of the HKDF algorithm. */
3070static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3071 psa_algorithm_t hash_alg,
3072 uint8_t *output,
3073 size_t output_length )
3074{
3075 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3076 psa_status_t status;
3077
3078 while( output_length != 0 )
3079 {
3080 /* Copy what remains of the current block */
3081 uint8_t n = hash_length - hkdf->offset_in_block;
3082 if( n > output_length )
3083 n = (uint8_t) output_length;
3084 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3085 output += n;
3086 output_length -= n;
3087 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003088 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003089 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003090 /* We can't be wanting more output after block 0xff, otherwise
3091 * the capacity check in psa_generator_read() would have
3092 * prevented this call. It could happen only if the generator
3093 * object was corrupted or if this function is called directly
3094 * inside the library. */
3095 if( hkdf->block_number == 0xff )
3096 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003097
3098 /* We need a new block */
3099 ++hkdf->block_number;
3100 hkdf->offset_in_block = 0;
3101 status = psa_hmac_setup_internal( &hkdf->hmac,
3102 hkdf->prk, hash_length,
3103 hash_alg );
3104 if( status != PSA_SUCCESS )
3105 return( status );
3106 if( hkdf->block_number != 1 )
3107 {
3108 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3109 hkdf->output_block,
3110 hash_length );
3111 if( status != PSA_SUCCESS )
3112 return( status );
3113 }
3114 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3115 hkdf->info,
3116 hkdf->info_length );
3117 if( status != PSA_SUCCESS )
3118 return( status );
3119 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3120 &hkdf->block_number, 1 );
3121 if( status != PSA_SUCCESS )
3122 return( status );
3123 status = psa_hmac_finish_internal( &hkdf->hmac,
3124 hkdf->output_block,
3125 sizeof( hkdf->output_block ) );
3126 if( status != PSA_SUCCESS )
3127 return( status );
3128 }
3129
3130 return( PSA_SUCCESS );
3131}
3132#endif /* MBEDTLS_MD_C */
3133
Gilles Peskineeab56e42018-07-12 17:12:33 +02003134psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3135 uint8_t *output,
3136 size_t output_length )
3137{
3138 psa_status_t status;
3139
3140 if( output_length > generator->capacity )
3141 {
3142 generator->capacity = 0;
3143 /* Go through the error path to wipe all confidential data now
3144 * that the generator object is useless. */
3145 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3146 goto exit;
3147 }
3148 if( output_length == 0 &&
3149 generator->capacity == 0 && generator->alg == 0 )
3150 {
3151 /* Edge case: this is a blank or finished generator, and 0
3152 * bytes were requested. The right error in this case could
3153 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3154 * INSUFFICIENT_CAPACITY, which is right for a finished
3155 * generator, for consistency with the case when
3156 * output_length > 0. */
3157 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3158 }
3159 generator->capacity -= output_length;
3160
Gilles Peskinebef7f142018-07-12 17:22:21 +02003161#if defined(MBEDTLS_MD_C)
3162 if( PSA_ALG_IS_HKDF( generator->alg ) )
3163 {
3164 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3165 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3166 output, output_length );
3167 }
3168 else
3169#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003170 {
3171 return( PSA_ERROR_BAD_STATE );
3172 }
3173
3174exit:
3175 if( status != PSA_SUCCESS )
3176 {
3177 psa_generator_abort( generator );
3178 memset( output, '!', output_length );
3179 }
3180 return( status );
3181}
3182
Gilles Peskine08542d82018-07-19 17:05:42 +02003183#if defined(MBEDTLS_DES_C)
3184static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3185{
3186 if( data_size >= 8 )
3187 mbedtls_des_key_set_parity( data );
3188 if( data_size >= 16 )
3189 mbedtls_des_key_set_parity( data + 8 );
3190 if( data_size >= 24 )
3191 mbedtls_des_key_set_parity( data + 16 );
3192}
3193#endif /* MBEDTLS_DES_C */
3194
Gilles Peskineeab56e42018-07-12 17:12:33 +02003195psa_status_t psa_generator_import_key( psa_key_slot_t key,
3196 psa_key_type_t type,
3197 size_t bits,
3198 psa_crypto_generator_t *generator )
3199{
3200 uint8_t *data = NULL;
3201 size_t bytes = PSA_BITS_TO_BYTES( bits );
3202 psa_status_t status;
3203
3204 if( ! key_type_is_raw_bytes( type ) )
3205 return( PSA_ERROR_INVALID_ARGUMENT );
3206 if( bits % 8 != 0 )
3207 return( PSA_ERROR_INVALID_ARGUMENT );
3208 data = mbedtls_calloc( 1, bytes );
3209 if( data == NULL )
3210 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3211
3212 status = psa_generator_read( generator, data, bytes );
3213 if( status != PSA_SUCCESS )
3214 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003215#if defined(MBEDTLS_DES_C)
3216 if( type == PSA_KEY_TYPE_DES )
3217 psa_des_set_key_parity( data, bytes );
3218#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003219 status = psa_import_key( key, type, data, bytes );
3220
3221exit:
3222 mbedtls_free( data );
3223 return( status );
3224}
3225
3226
3227
3228/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003229/* Key derivation */
3230/****************************************************************/
3231
Gilles Peskinebef7f142018-07-12 17:22:21 +02003232/* Set up an HKDF-based generator. This is exactly the extract phase
3233 * of the HKDF algorithm. */
3234static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
3235 key_slot_t *slot,
3236 psa_algorithm_t hash_alg,
3237 const uint8_t *salt,
3238 size_t salt_length,
3239 const uint8_t *label,
3240 size_t label_length )
3241{
3242 psa_status_t status;
3243 status = psa_hmac_setup_internal( &hkdf->hmac,
3244 salt, salt_length,
3245 PSA_ALG_HMAC_HASH( hash_alg ) );
3246 if( status != PSA_SUCCESS )
3247 return( status );
3248 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3249 slot->data.raw.data,
3250 slot->data.raw.bytes );
3251 if( status != PSA_SUCCESS )
3252 return( status );
3253 status = psa_hmac_finish_internal( &hkdf->hmac,
3254 hkdf->prk,
3255 sizeof( hkdf->prk ) );
3256 if( status != PSA_SUCCESS )
3257 return( status );
3258 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3259 hkdf->block_number = 0;
3260 hkdf->info_length = label_length;
3261 if( label_length != 0 )
3262 {
3263 hkdf->info = mbedtls_calloc( 1, label_length );
3264 if( hkdf->info == NULL )
3265 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3266 memcpy( hkdf->info, label, label_length );
3267 }
3268 return( PSA_SUCCESS );
3269}
3270
Gilles Peskineea0fb492018-07-12 17:17:20 +02003271psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01003272 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003273 psa_algorithm_t alg,
3274 const uint8_t *salt,
3275 size_t salt_length,
3276 const uint8_t *label,
3277 size_t label_length,
3278 size_t capacity )
3279{
3280 key_slot_t *slot;
3281 psa_status_t status;
3282
3283 if( generator->alg != 0 )
3284 return( PSA_ERROR_BAD_STATE );
3285
3286 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
3287 if( status != PSA_SUCCESS )
3288 return( status );
3289 if( slot->type != PSA_KEY_TYPE_DERIVE )
3290 return( PSA_ERROR_INVALID_ARGUMENT );
3291
3292 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
3293 return( PSA_ERROR_INVALID_ARGUMENT );
3294
Gilles Peskinebef7f142018-07-12 17:22:21 +02003295#if defined(MBEDTLS_MD_C)
3296 if( PSA_ALG_IS_HKDF( alg ) )
3297 {
3298 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3299 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3300 if( hash_size == 0 )
3301 return( PSA_ERROR_NOT_SUPPORTED );
3302 if( capacity > 255 * hash_size )
3303 return( PSA_ERROR_INVALID_ARGUMENT );
3304 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3305 slot,
3306 hash_alg,
3307 salt, salt_length,
3308 label, label_length );
3309 }
3310 else
3311#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +02003312 {
3313 return( PSA_ERROR_NOT_SUPPORTED );
3314 }
3315
3316 /* Set generator->alg even on failure so that abort knows what to do. */
3317 generator->alg = alg;
3318 if( status == PSA_SUCCESS )
3319 generator->capacity = capacity;
3320 else
3321 psa_generator_abort( generator );
3322 return( status );
3323}
3324
3325
3326
3327/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003328/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02003329/****************************************************************/
3330
3331psa_status_t psa_generate_random( uint8_t *output,
3332 size_t output_size )
3333{
3334 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3335 output, output_size );
3336 return( mbedtls_to_psa_error( ret ) );
3337}
3338
3339psa_status_t psa_generate_key( psa_key_slot_t key,
3340 psa_key_type_t type,
3341 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003342 const void *extra,
3343 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02003344{
Gilles Peskine12313cd2018-06-20 00:20:32 +02003345 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003346 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003347
Gilles Peskine53d991e2018-07-12 01:14:59 +02003348 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003349 return( PSA_ERROR_INVALID_ARGUMENT );
3350
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003351 status = psa_get_empty_key_slot( key, &slot );
3352 if( status != PSA_SUCCESS )
3353 return( status );
3354
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003355 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003356 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003357 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003358 if( status != PSA_SUCCESS )
3359 return( status );
3360 status = psa_generate_random( slot->data.raw.data,
3361 slot->data.raw.bytes );
3362 if( status != PSA_SUCCESS )
3363 {
3364 mbedtls_free( slot->data.raw.data );
3365 return( status );
3366 }
3367#if defined(MBEDTLS_DES_C)
3368 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02003369 psa_des_set_key_parity( slot->data.raw.data,
3370 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003371#endif /* MBEDTLS_DES_C */
3372 }
3373 else
3374
3375#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3376 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3377 {
3378 mbedtls_rsa_context *rsa;
3379 int ret;
3380 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003381 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3382 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02003383 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003384 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02003385 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003386 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003387 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003388#if INT_MAX < 0xffffffff
3389 /* Check that the uint32_t value passed by the caller fits
3390 * in the range supported by this implementation. */
3391 if( p->e > INT_MAX )
3392 return( PSA_ERROR_NOT_SUPPORTED );
3393#endif
3394 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003395 }
3396 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3397 if( rsa == NULL )
3398 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3399 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3400 ret = mbedtls_rsa_gen_key( rsa,
3401 mbedtls_ctr_drbg_random,
3402 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003403 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003404 exponent );
3405 if( ret != 0 )
3406 {
3407 mbedtls_rsa_free( rsa );
3408 mbedtls_free( rsa );
3409 return( mbedtls_to_psa_error( ret ) );
3410 }
3411 slot->data.rsa = rsa;
3412 }
3413 else
3414#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3415
3416#if defined(MBEDTLS_ECP_C)
3417 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3418 {
3419 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3420 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3421 const mbedtls_ecp_curve_info *curve_info =
3422 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3423 mbedtls_ecp_keypair *ecp;
3424 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003425 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003426 return( PSA_ERROR_NOT_SUPPORTED );
3427 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3428 return( PSA_ERROR_NOT_SUPPORTED );
3429 if( curve_info->bit_size != bits )
3430 return( PSA_ERROR_INVALID_ARGUMENT );
3431 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3432 if( ecp == NULL )
3433 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3434 mbedtls_ecp_keypair_init( ecp );
3435 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3436 mbedtls_ctr_drbg_random,
3437 &global_data.ctr_drbg );
3438 if( ret != 0 )
3439 {
3440 mbedtls_ecp_keypair_free( ecp );
3441 mbedtls_free( ecp );
3442 return( mbedtls_to_psa_error( ret ) );
3443 }
3444 slot->data.ecp = ecp;
3445 }
3446 else
3447#endif /* MBEDTLS_ECP_C */
3448
3449 return( PSA_ERROR_NOT_SUPPORTED );
3450
3451 slot->type = type;
3452 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003453}
3454
3455
3456/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003457/* Module setup */
3458/****************************************************************/
3459
Gilles Peskinee59236f2018-01-27 23:32:46 +01003460void mbedtls_psa_crypto_free( void )
3461{
Jaeden Amero045bd502018-06-26 14:00:08 +01003462 psa_key_slot_t key;
Gilles Peskine828ed142018-06-18 23:25:51 +02003463 for( key = 1; key < PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003464 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003465 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3466 mbedtls_entropy_free( &global_data.entropy );
3467 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3468}
3469
3470psa_status_t psa_crypto_init( void )
3471{
3472 int ret;
3473 const unsigned char drbg_seed[] = "PSA";
3474
3475 if( global_data.initialized != 0 )
3476 return( PSA_SUCCESS );
3477
3478 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3479 mbedtls_entropy_init( &global_data.entropy );
3480 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3481
3482 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3483 mbedtls_entropy_func,
3484 &global_data.entropy,
3485 drbg_seed, sizeof( drbg_seed ) - 1 );
3486 if( ret != 0 )
3487 goto exit;
3488
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003489 global_data.initialized = 1;
3490
Gilles Peskinee59236f2018-01-27 23:32:46 +01003491exit:
3492 if( ret != 0 )
3493 mbedtls_psa_crypto_free( );
3494 return( mbedtls_to_psa_error( ret ) );
3495}
3496
3497#endif /* MBEDTLS_PSA_CRYPTO_C */