blob: 75c7ea8a57d7603fd9cdfd4eef7f9b2875090c20 [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
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100414#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200415static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
416{
417 switch( grpid )
418 {
419 case MBEDTLS_ECP_DP_SECP192R1:
420 return( PSA_ECC_CURVE_SECP192R1 );
421 case MBEDTLS_ECP_DP_SECP224R1:
422 return( PSA_ECC_CURVE_SECP224R1 );
423 case MBEDTLS_ECP_DP_SECP256R1:
424 return( PSA_ECC_CURVE_SECP256R1 );
425 case MBEDTLS_ECP_DP_SECP384R1:
426 return( PSA_ECC_CURVE_SECP384R1 );
427 case MBEDTLS_ECP_DP_SECP521R1:
428 return( PSA_ECC_CURVE_SECP521R1 );
429 case MBEDTLS_ECP_DP_BP256R1:
430 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
431 case MBEDTLS_ECP_DP_BP384R1:
432 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
433 case MBEDTLS_ECP_DP_BP512R1:
434 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
435 case MBEDTLS_ECP_DP_CURVE25519:
436 return( PSA_ECC_CURVE_CURVE25519 );
437 case MBEDTLS_ECP_DP_SECP192K1:
438 return( PSA_ECC_CURVE_SECP192K1 );
439 case MBEDTLS_ECP_DP_SECP224K1:
440 return( PSA_ECC_CURVE_SECP224K1 );
441 case MBEDTLS_ECP_DP_SECP256K1:
442 return( PSA_ECC_CURVE_SECP256K1 );
443 case MBEDTLS_ECP_DP_CURVE448:
444 return( PSA_ECC_CURVE_CURVE448 );
445 default:
446 return( 0 );
447 }
448}
449
Gilles Peskine12313cd2018-06-20 00:20:32 +0200450static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
451{
452 switch( curve )
453 {
454 case PSA_ECC_CURVE_SECP192R1:
455 return( MBEDTLS_ECP_DP_SECP192R1 );
456 case PSA_ECC_CURVE_SECP224R1:
457 return( MBEDTLS_ECP_DP_SECP224R1 );
458 case PSA_ECC_CURVE_SECP256R1:
459 return( MBEDTLS_ECP_DP_SECP256R1 );
460 case PSA_ECC_CURVE_SECP384R1:
461 return( MBEDTLS_ECP_DP_SECP384R1 );
462 case PSA_ECC_CURVE_SECP521R1:
463 return( MBEDTLS_ECP_DP_SECP521R1 );
464 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
465 return( MBEDTLS_ECP_DP_BP256R1 );
466 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
467 return( MBEDTLS_ECP_DP_BP384R1 );
468 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
469 return( MBEDTLS_ECP_DP_BP512R1 );
470 case PSA_ECC_CURVE_CURVE25519:
471 return( MBEDTLS_ECP_DP_CURVE25519 );
472 case PSA_ECC_CURVE_SECP192K1:
473 return( MBEDTLS_ECP_DP_SECP192K1 );
474 case PSA_ECC_CURVE_SECP224K1:
475 return( MBEDTLS_ECP_DP_SECP224K1 );
476 case PSA_ECC_CURVE_SECP256K1:
477 return( MBEDTLS_ECP_DP_SECP256K1 );
478 case PSA_ECC_CURVE_CURVE448:
479 return( MBEDTLS_ECP_DP_CURVE448 );
480 default:
481 return( MBEDTLS_ECP_DP_NONE );
482 }
483}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100484#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200485
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200486static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
487 size_t bits,
488 struct raw_data *raw )
489{
490 /* Check that the bit size is acceptable for the key type */
491 switch( type )
492 {
493 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200494 if( bits == 0 )
495 {
496 raw->bytes = 0;
497 raw->data = NULL;
498 return( PSA_SUCCESS );
499 }
500 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200501#if defined(MBEDTLS_MD_C)
502 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200503#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200504 case PSA_KEY_TYPE_DERIVE:
505 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200506#if defined(MBEDTLS_AES_C)
507 case PSA_KEY_TYPE_AES:
508 if( bits != 128 && bits != 192 && bits != 256 )
509 return( PSA_ERROR_INVALID_ARGUMENT );
510 break;
511#endif
512#if defined(MBEDTLS_CAMELLIA_C)
513 case PSA_KEY_TYPE_CAMELLIA:
514 if( bits != 128 && bits != 192 && bits != 256 )
515 return( PSA_ERROR_INVALID_ARGUMENT );
516 break;
517#endif
518#if defined(MBEDTLS_DES_C)
519 case PSA_KEY_TYPE_DES:
520 if( bits != 64 && bits != 128 && bits != 192 )
521 return( PSA_ERROR_INVALID_ARGUMENT );
522 break;
523#endif
524#if defined(MBEDTLS_ARC4_C)
525 case PSA_KEY_TYPE_ARC4:
526 if( bits < 8 || bits > 2048 )
527 return( PSA_ERROR_INVALID_ARGUMENT );
528 break;
529#endif
530 default:
531 return( PSA_ERROR_NOT_SUPPORTED );
532 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200533 if( bits % 8 != 0 )
534 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200535
536 /* Allocate memory for the key */
537 raw->bytes = PSA_BITS_TO_BYTES( bits );
538 raw->data = mbedtls_calloc( 1, raw->bytes );
539 if( raw->data == NULL )
540 {
541 raw->bytes = 0;
542 return( PSA_ERROR_INSUFFICIENT_MEMORY );
543 }
544 return( PSA_SUCCESS );
545}
546
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200547#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
548static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
549 mbedtls_rsa_context **p_rsa )
550{
551 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
552 return( PSA_ERROR_INVALID_ARGUMENT );
553 else
554 {
555 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
556 size_t bits = mbedtls_rsa_get_bitlen( rsa );
557 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
558 return( PSA_ERROR_NOT_SUPPORTED );
559 *p_rsa = rsa;
560 return( PSA_SUCCESS );
561 }
562}
563#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
564
565#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
566static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
567 mbedtls_pk_context *pk,
568 mbedtls_ecp_keypair **p_ecp )
569{
570 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
571 return( PSA_ERROR_INVALID_ARGUMENT );
572 else
573 {
574 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
575 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
576 if( actual_curve != expected_curve )
577 return( PSA_ERROR_INVALID_ARGUMENT );
578 *p_ecp = ecp;
579 return( PSA_SUCCESS );
580 }
581}
582#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
583
Gilles Peskine2d277862018-06-18 15:41:12 +0200584psa_status_t psa_import_key( psa_key_slot_t key,
585 psa_key_type_t type,
586 const uint8_t *data,
587 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100588{
589 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200590 psa_status_t status = PSA_SUCCESS;
591 status = psa_get_empty_key_slot( key, &slot );
592 if( status != PSA_SUCCESS )
593 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100594
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200595 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100596 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100597 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100598 if( data_length > SIZE_MAX / 8 )
599 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200600 status = prepare_raw_data_slot( type,
601 PSA_BYTES_TO_BITS( data_length ),
602 &slot->data.raw );
603 if( status != PSA_SUCCESS )
604 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200605 if( data_length != 0 )
606 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100607 }
608 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100609#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200610 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100611 {
612 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100613 mbedtls_pk_context pk;
614 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200615
616 /* Parse the data. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100617 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
618 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
619 else
620 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100621 if( ret != 0 )
622 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200623
624 /* We have something that the pkparse module recognizes.
625 * If it has the expected type and passes any type-specific
626 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100627#if defined(MBEDTLS_RSA_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200628 if( PSA_KEY_TYPE_IS_RSA( type ) )
629 status = psa_import_rsa_key( &pk, &slot->data.rsa );
630 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100631#endif /* MBEDTLS_RSA_C */
632#if defined(MBEDTLS_ECP_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200633 if( PSA_KEY_TYPE_IS_ECC( type ) )
634 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( type ),
635 &pk, &slot->data.ecp );
636 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100637#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200638 {
639 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200640 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200641
Gilles Peskinec648d692018-06-28 08:46:13 +0200642 /* Free the content of the pk object only on error. On success,
643 * the content of the object has been stored in the slot. */
644 if( status != PSA_SUCCESS )
645 {
646 mbedtls_pk_free( &pk );
647 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100648 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100649 }
650 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100651#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100652 {
653 return( PSA_ERROR_NOT_SUPPORTED );
654 }
655
656 slot->type = type;
657 return( PSA_SUCCESS );
658}
659
Gilles Peskine2d277862018-06-18 15:41:12 +0200660psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100661{
662 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200663 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100664
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200665 status = psa_get_key_slot( key, &slot );
666 if( status != PSA_SUCCESS )
667 return( status );
668
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100669 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200670 {
671 /* No key material to clean, but do zeroize the slot below to wipe
672 * metadata such as policies. */
673 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200674 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100675 {
676 mbedtls_free( slot->data.raw.data );
677 }
678 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100679#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200680 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100681 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100682 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100683 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100684 }
685 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100686#endif /* defined(MBEDTLS_RSA_C) */
687#if defined(MBEDTLS_ECP_C)
688 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
689 {
690 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100691 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100692 }
693 else
694#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100695 {
696 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100697 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100698 return( PSA_ERROR_TAMPERING_DETECTED );
699 }
700
701 mbedtls_zeroize( slot, sizeof( *slot ) );
702 return( PSA_SUCCESS );
703}
704
Gilles Peskineb870b182018-07-06 16:02:09 +0200705/* Return the size of the key in the given slot, in bits. */
706static size_t psa_get_key_bits( const key_slot_t *slot )
707{
708 if( key_type_is_raw_bytes( slot->type ) )
709 return( slot->data.raw.bytes * 8 );
710#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200711 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineb870b182018-07-06 16:02:09 +0200712 return( mbedtls_rsa_get_bitlen( slot->data.rsa ) );
713#endif /* defined(MBEDTLS_RSA_C) */
714#if defined(MBEDTLS_ECP_C)
715 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
716 return( slot->data.ecp->grp.pbits );
717#endif /* defined(MBEDTLS_ECP_C) */
718 /* Shouldn't happen except on an empty slot. */
719 return( 0 );
720}
721
Gilles Peskine2d277862018-06-18 15:41:12 +0200722psa_status_t psa_get_key_information( psa_key_slot_t key,
723 psa_key_type_t *type,
724 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100725{
726 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200727 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100728
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100729 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200730 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100731 if( bits != NULL )
732 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200733 status = psa_get_key_slot( key, &slot );
734 if( status != PSA_SUCCESS )
735 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200736
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100737 if( slot->type == PSA_KEY_TYPE_NONE )
738 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200739 if( type != NULL )
740 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200741 if( bits != NULL )
742 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100743 return( PSA_SUCCESS );
744}
745
Gilles Peskine2d277862018-06-18 15:41:12 +0200746static psa_status_t psa_internal_export_key( psa_key_slot_t key,
747 uint8_t *data,
748 size_t data_size,
749 size_t *data_length,
750 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100751{
752 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200753 psa_status_t status;
754 /* Exporting a public key doesn't require a usage flag. If we're
755 * called by psa_export_public_key(), don't require the EXPORT flag.
756 * If we're called by psa_export_key(), do require the EXPORT flag;
757 * if the key turns out to be public key object, psa_get_key_from_slot()
758 * will ignore this flag. */
759 psa_key_usage_t usage = export_public_key ? 0 : PSA_KEY_USAGE_EXPORT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100760
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100761 /* Set the key to empty now, so that even when there are errors, we always
762 * set data_length to a value between 0 and data_size. On error, setting
763 * the key to empty is a good choice because an empty key representation is
764 * unlikely to be accepted anywhere. */
765 *data_length = 0;
766
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200767 status = psa_get_key_from_slot( key, &slot, usage, 0 );
768 if( status != PSA_SUCCESS )
769 return( status );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200770 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300771 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300772
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200773 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100774 {
775 if( slot->data.raw.bytes > data_size )
776 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200777 if( slot->data.raw.bytes != 0 )
778 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100779 *data_length = slot->data.raw.bytes;
780 return( PSA_SUCCESS );
781 }
782 else
Moran Peker17e36e12018-05-02 12:55:20 +0300783 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100784#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200785 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300786 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100787 {
Moran Pekera998bc62018-04-16 18:16:20 +0300788 mbedtls_pk_context pk;
789 int ret;
790 mbedtls_pk_init( &pk );
Gilles Peskined8008d62018-06-29 19:51:51 +0200791 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300792 {
793 pk.pk_info = &mbedtls_rsa_info;
794 pk.pk_ctx = slot->data.rsa;
795 }
796 else
797 {
798 pk.pk_info = &mbedtls_eckey_info;
799 pk.pk_ctx = slot->data.ecp;
800 }
Moran Pekerd7326592018-05-29 16:56:39 +0300801 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300802 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300803 else
804 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300805 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200806 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200807 /* If data_size is 0 then data may be NULL and then the
808 * call to memset would have undefined behavior. */
809 if( data_size != 0 )
810 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300811 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200812 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200813 /* The mbedtls_pk_xxx functions write to the end of the buffer.
814 * Move the data to the beginning and erase remaining data
815 * at the original location. */
816 if( 2 * (size_t) ret <= data_size )
817 {
818 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200819 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200820 }
821 else if( (size_t) ret < data_size )
822 {
823 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200824 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200825 }
Moran Pekera998bc62018-04-16 18:16:20 +0300826 *data_length = ret;
827 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100828 }
829 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100830#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300831 {
832 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200833 it is valid for a special-purpose implementation to omit
834 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300835 return( PSA_ERROR_NOT_SUPPORTED );
836 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100837 }
838}
839
Gilles Peskine2d277862018-06-18 15:41:12 +0200840psa_status_t psa_export_key( psa_key_slot_t key,
841 uint8_t *data,
842 size_t data_size,
843 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300844{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200845 return( psa_internal_export_key( key, data, data_size,
846 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100847}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100848
Gilles Peskine2d277862018-06-18 15:41:12 +0200849psa_status_t psa_export_public_key( psa_key_slot_t key,
850 uint8_t *data,
851 size_t data_size,
852 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300853{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200854 return( psa_internal_export_key( key, data, data_size,
855 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300856}
857
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200858
859
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100860/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100861/* Message digests */
862/****************************************************************/
863
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100864static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100865{
866 switch( alg )
867 {
868#if defined(MBEDTLS_MD2_C)
869 case PSA_ALG_MD2:
870 return( &mbedtls_md2_info );
871#endif
872#if defined(MBEDTLS_MD4_C)
873 case PSA_ALG_MD4:
874 return( &mbedtls_md4_info );
875#endif
876#if defined(MBEDTLS_MD5_C)
877 case PSA_ALG_MD5:
878 return( &mbedtls_md5_info );
879#endif
880#if defined(MBEDTLS_RIPEMD160_C)
881 case PSA_ALG_RIPEMD160:
882 return( &mbedtls_ripemd160_info );
883#endif
884#if defined(MBEDTLS_SHA1_C)
885 case PSA_ALG_SHA_1:
886 return( &mbedtls_sha1_info );
887#endif
888#if defined(MBEDTLS_SHA256_C)
889 case PSA_ALG_SHA_224:
890 return( &mbedtls_sha224_info );
891 case PSA_ALG_SHA_256:
892 return( &mbedtls_sha256_info );
893#endif
894#if defined(MBEDTLS_SHA512_C)
895 case PSA_ALG_SHA_384:
896 return( &mbedtls_sha384_info );
897 case PSA_ALG_SHA_512:
898 return( &mbedtls_sha512_info );
899#endif
900 default:
901 return( NULL );
902 }
903}
904
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100905psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
906{
907 switch( operation->alg )
908 {
Gilles Peskine81736312018-06-26 15:04:31 +0200909 case 0:
910 /* The object has (apparently) been initialized but it is not
911 * in use. It's ok to call abort on such an object, and there's
912 * nothing to do. */
913 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100914#if defined(MBEDTLS_MD2_C)
915 case PSA_ALG_MD2:
916 mbedtls_md2_free( &operation->ctx.md2 );
917 break;
918#endif
919#if defined(MBEDTLS_MD4_C)
920 case PSA_ALG_MD4:
921 mbedtls_md4_free( &operation->ctx.md4 );
922 break;
923#endif
924#if defined(MBEDTLS_MD5_C)
925 case PSA_ALG_MD5:
926 mbedtls_md5_free( &operation->ctx.md5 );
927 break;
928#endif
929#if defined(MBEDTLS_RIPEMD160_C)
930 case PSA_ALG_RIPEMD160:
931 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
932 break;
933#endif
934#if defined(MBEDTLS_SHA1_C)
935 case PSA_ALG_SHA_1:
936 mbedtls_sha1_free( &operation->ctx.sha1 );
937 break;
938#endif
939#if defined(MBEDTLS_SHA256_C)
940 case PSA_ALG_SHA_224:
941 case PSA_ALG_SHA_256:
942 mbedtls_sha256_free( &operation->ctx.sha256 );
943 break;
944#endif
945#if defined(MBEDTLS_SHA512_C)
946 case PSA_ALG_SHA_384:
947 case PSA_ALG_SHA_512:
948 mbedtls_sha512_free( &operation->ctx.sha512 );
949 break;
950#endif
951 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +0200952 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100953 }
954 operation->alg = 0;
955 return( PSA_SUCCESS );
956}
957
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200958psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100959 psa_algorithm_t alg )
960{
961 int ret;
962 operation->alg = 0;
963 switch( alg )
964 {
965#if defined(MBEDTLS_MD2_C)
966 case PSA_ALG_MD2:
967 mbedtls_md2_init( &operation->ctx.md2 );
968 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
969 break;
970#endif
971#if defined(MBEDTLS_MD4_C)
972 case PSA_ALG_MD4:
973 mbedtls_md4_init( &operation->ctx.md4 );
974 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
975 break;
976#endif
977#if defined(MBEDTLS_MD5_C)
978 case PSA_ALG_MD5:
979 mbedtls_md5_init( &operation->ctx.md5 );
980 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
981 break;
982#endif
983#if defined(MBEDTLS_RIPEMD160_C)
984 case PSA_ALG_RIPEMD160:
985 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
986 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
987 break;
988#endif
989#if defined(MBEDTLS_SHA1_C)
990 case PSA_ALG_SHA_1:
991 mbedtls_sha1_init( &operation->ctx.sha1 );
992 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
993 break;
994#endif
995#if defined(MBEDTLS_SHA256_C)
996 case PSA_ALG_SHA_224:
997 mbedtls_sha256_init( &operation->ctx.sha256 );
998 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
999 break;
1000 case PSA_ALG_SHA_256:
1001 mbedtls_sha256_init( &operation->ctx.sha256 );
1002 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1003 break;
1004#endif
1005#if defined(MBEDTLS_SHA512_C)
1006 case PSA_ALG_SHA_384:
1007 mbedtls_sha512_init( &operation->ctx.sha512 );
1008 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1009 break;
1010 case PSA_ALG_SHA_512:
1011 mbedtls_sha512_init( &operation->ctx.sha512 );
1012 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1013 break;
1014#endif
1015 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001016 return( PSA_ALG_IS_HASH( alg ) ?
1017 PSA_ERROR_NOT_SUPPORTED :
1018 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001019 }
1020 if( ret == 0 )
1021 operation->alg = alg;
1022 else
1023 psa_hash_abort( operation );
1024 return( mbedtls_to_psa_error( ret ) );
1025}
1026
1027psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1028 const uint8_t *input,
1029 size_t input_length )
1030{
1031 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001032
1033 /* Don't require hash implementations to behave correctly on a
1034 * zero-length input, which may have an invalid pointer. */
1035 if( input_length == 0 )
1036 return( PSA_SUCCESS );
1037
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001038 switch( operation->alg )
1039 {
1040#if defined(MBEDTLS_MD2_C)
1041 case PSA_ALG_MD2:
1042 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1043 input, input_length );
1044 break;
1045#endif
1046#if defined(MBEDTLS_MD4_C)
1047 case PSA_ALG_MD4:
1048 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1049 input, input_length );
1050 break;
1051#endif
1052#if defined(MBEDTLS_MD5_C)
1053 case PSA_ALG_MD5:
1054 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1055 input, input_length );
1056 break;
1057#endif
1058#if defined(MBEDTLS_RIPEMD160_C)
1059 case PSA_ALG_RIPEMD160:
1060 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1061 input, input_length );
1062 break;
1063#endif
1064#if defined(MBEDTLS_SHA1_C)
1065 case PSA_ALG_SHA_1:
1066 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1067 input, input_length );
1068 break;
1069#endif
1070#if defined(MBEDTLS_SHA256_C)
1071 case PSA_ALG_SHA_224:
1072 case PSA_ALG_SHA_256:
1073 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1074 input, input_length );
1075 break;
1076#endif
1077#if defined(MBEDTLS_SHA512_C)
1078 case PSA_ALG_SHA_384:
1079 case PSA_ALG_SHA_512:
1080 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1081 input, input_length );
1082 break;
1083#endif
1084 default:
1085 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1086 break;
1087 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001088
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001089 if( ret != 0 )
1090 psa_hash_abort( operation );
1091 return( mbedtls_to_psa_error( ret ) );
1092}
1093
1094psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1095 uint8_t *hash,
1096 size_t hash_size,
1097 size_t *hash_length )
1098{
1099 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001100 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001101
1102 /* Fill the output buffer with something that isn't a valid hash
1103 * (barring an attack on the hash and deliberately-crafted input),
1104 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001105 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001106 /* If hash_size is 0 then hash may be NULL and then the
1107 * call to memset would have undefined behavior. */
1108 if( hash_size != 0 )
1109 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001110
1111 if( hash_size < actual_hash_length )
1112 return( PSA_ERROR_BUFFER_TOO_SMALL );
1113
1114 switch( operation->alg )
1115 {
1116#if defined(MBEDTLS_MD2_C)
1117 case PSA_ALG_MD2:
1118 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1119 break;
1120#endif
1121#if defined(MBEDTLS_MD4_C)
1122 case PSA_ALG_MD4:
1123 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1124 break;
1125#endif
1126#if defined(MBEDTLS_MD5_C)
1127 case PSA_ALG_MD5:
1128 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1129 break;
1130#endif
1131#if defined(MBEDTLS_RIPEMD160_C)
1132 case PSA_ALG_RIPEMD160:
1133 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1134 break;
1135#endif
1136#if defined(MBEDTLS_SHA1_C)
1137 case PSA_ALG_SHA_1:
1138 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1139 break;
1140#endif
1141#if defined(MBEDTLS_SHA256_C)
1142 case PSA_ALG_SHA_224:
1143 case PSA_ALG_SHA_256:
1144 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1145 break;
1146#endif
1147#if defined(MBEDTLS_SHA512_C)
1148 case PSA_ALG_SHA_384:
1149 case PSA_ALG_SHA_512:
1150 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1151 break;
1152#endif
1153 default:
1154 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1155 break;
1156 }
1157
1158 if( ret == 0 )
1159 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001160 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001161 return( psa_hash_abort( operation ) );
1162 }
1163 else
1164 {
1165 psa_hash_abort( operation );
1166 return( mbedtls_to_psa_error( ret ) );
1167 }
1168}
1169
Gilles Peskine2d277862018-06-18 15:41:12 +02001170psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1171 const uint8_t *hash,
1172 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001173{
1174 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1175 size_t actual_hash_length;
1176 psa_status_t status = psa_hash_finish( operation,
1177 actual_hash, sizeof( actual_hash ),
1178 &actual_hash_length );
1179 if( status != PSA_SUCCESS )
1180 return( status );
1181 if( actual_hash_length != hash_length )
1182 return( PSA_ERROR_INVALID_SIGNATURE );
1183 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1184 return( PSA_ERROR_INVALID_SIGNATURE );
1185 return( PSA_SUCCESS );
1186}
1187
1188
1189
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001190/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001191/* MAC */
1192/****************************************************************/
1193
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001194static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001195 psa_algorithm_t alg,
1196 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001197 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001198 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001199{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001200 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001201 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001202
1203 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1204 {
1205 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001206 {
1207 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1208 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001209
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001210 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001211 {
Gilles Peskine5d1888e2018-07-12 00:32:42 +02001212 case PSA_ALG_STREAM_CIPHER_BASE:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001213 mode = MBEDTLS_MODE_STREAM;
1214 break;
1215 case PSA_ALG_CBC_BASE:
1216 mode = MBEDTLS_MODE_CBC;
1217 break;
1218 case PSA_ALG_CFB_BASE:
1219 mode = MBEDTLS_MODE_CFB;
1220 break;
1221 case PSA_ALG_OFB_BASE:
1222 mode = MBEDTLS_MODE_OFB;
1223 break;
1224 case PSA_ALG_CTR:
1225 mode = MBEDTLS_MODE_CTR;
1226 break;
1227 case PSA_ALG_CCM:
1228 mode = MBEDTLS_MODE_CCM;
1229 break;
1230 case PSA_ALG_GCM:
1231 mode = MBEDTLS_MODE_GCM;
1232 break;
1233 default:
1234 return( NULL );
1235 }
1236 }
1237 else if( alg == PSA_ALG_CMAC )
1238 mode = MBEDTLS_MODE_ECB;
1239 else if( alg == PSA_ALG_GMAC )
1240 mode = MBEDTLS_MODE_GCM;
1241 else
1242 return( NULL );
1243
1244 switch( key_type )
1245 {
1246 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001247 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001248 break;
1249 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001250 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1251 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001252 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001253 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001254 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001255 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001256 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1257 * but two-key Triple-DES is functionally three-key Triple-DES
1258 * with K1=K3, so that's how we present it to mbedtls. */
1259 if( key_bits == 128 )
1260 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001261 break;
1262 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001263 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001264 break;
1265 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001266 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001267 break;
1268 default:
1269 return( NULL );
1270 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001271 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001272 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001273
Jaeden Amero23bbb752018-06-26 14:16:54 +01001274 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1275 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001276}
1277
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001278static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001279{
Gilles Peskine2d277862018-06-18 15:41:12 +02001280 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001281 {
1282 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001283 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001284 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001285 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001286 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001287 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001288 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001289 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001290 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001291 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001292 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001293 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001294 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001295 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001296 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001297 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001298 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001299 return( 128 );
1300 default:
1301 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001302 }
1303}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001304
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001305/* Initialize the MAC operation structure. Once this function has been
1306 * called, psa_mac_abort can run and will do the right thing. */
1307static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1308 psa_algorithm_t alg )
1309{
1310 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1311
1312 operation->alg = alg;
1313 operation->key_set = 0;
1314 operation->iv_set = 0;
1315 operation->iv_required = 0;
1316 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001317 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001318
1319#if defined(MBEDTLS_CMAC_C)
1320 if( alg == PSA_ALG_CMAC )
1321 {
1322 operation->iv_required = 0;
1323 mbedtls_cipher_init( &operation->ctx.cmac );
1324 status = PSA_SUCCESS;
1325 }
1326 else
1327#endif /* MBEDTLS_CMAC_C */
1328#if defined(MBEDTLS_MD_C)
1329 if( PSA_ALG_IS_HMAC( operation->alg ) )
1330 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001331 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1332 operation->ctx.hmac.hash_ctx.alg = 0;
1333 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001334 }
1335 else
1336#endif /* MBEDTLS_MD_C */
1337 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001338 if( ! PSA_ALG_IS_MAC( alg ) )
1339 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001340 }
1341
1342 if( status != PSA_SUCCESS )
1343 memset( operation, 0, sizeof( *operation ) );
1344 return( status );
1345}
1346
Gilles Peskine01126fa2018-07-12 17:04:55 +02001347#if defined(MBEDTLS_MD_C)
1348static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1349{
1350 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1351 return( psa_hash_abort( &hmac->hash_ctx ) );
1352}
1353#endif /* MBEDTLS_MD_C */
1354
Gilles Peskine8c9def32018-02-08 10:02:12 +01001355psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1356{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001357 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001358 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001359 /* The object has (apparently) been initialized but it is not
1360 * in use. It's ok to call abort on such an object, and there's
1361 * nothing to do. */
1362 return( PSA_SUCCESS );
1363 }
1364 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001365#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001366 if( operation->alg == PSA_ALG_CMAC )
1367 {
1368 mbedtls_cipher_free( &operation->ctx.cmac );
1369 }
1370 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001371#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001372#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001373 if( PSA_ALG_IS_HMAC( operation->alg ) )
1374 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001375 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001376 }
1377 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001378#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001379 {
1380 /* Sanity check (shouldn't happen: operation->alg should
1381 * always have been initialized to a valid value). */
1382 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001383 }
Moran Peker41deec42018-04-04 15:43:05 +03001384
Gilles Peskine8c9def32018-02-08 10:02:12 +01001385 operation->alg = 0;
1386 operation->key_set = 0;
1387 operation->iv_set = 0;
1388 operation->iv_required = 0;
1389 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001390 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001391
Gilles Peskine8c9def32018-02-08 10:02:12 +01001392 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001393
1394bad_state:
1395 /* If abort is called on an uninitialized object, we can't trust
1396 * anything. Wipe the object in case it contains confidential data.
1397 * This may result in a memory leak if a pointer gets overwritten,
1398 * but it's too late to do anything about this. */
1399 memset( operation, 0, sizeof( *operation ) );
1400 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001401}
1402
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001403#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001404static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001405 size_t key_bits,
1406 key_slot_t *slot,
1407 const mbedtls_cipher_info_t *cipher_info )
1408{
1409 int ret;
1410
1411 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001412
1413 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1414 if( ret != 0 )
1415 return( ret );
1416
1417 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1418 slot->data.raw.data,
1419 key_bits );
1420 return( ret );
1421}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001422#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001423
Gilles Peskine248051a2018-06-20 16:09:38 +02001424#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001425static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1426 const uint8_t *key,
1427 size_t key_length,
1428 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001429{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001430 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001431 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001432 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001433 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001434 psa_status_t status;
1435
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001436 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1437 * overflow below. This should never trigger if the hash algorithm
1438 * is implemented correctly. */
1439 /* The size checks against the ipad and opad buffers cannot be written
1440 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1441 * because that triggers -Wlogical-op on GCC 7.3. */
1442 if( block_size > sizeof( ipad ) )
1443 return( PSA_ERROR_NOT_SUPPORTED );
1444 if( block_size > sizeof( hmac->opad ) )
1445 return( PSA_ERROR_NOT_SUPPORTED );
1446 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001447 return( PSA_ERROR_NOT_SUPPORTED );
1448
Gilles Peskined223b522018-06-11 18:12:58 +02001449 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001450 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001451 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001452 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001453 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001454 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001455 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001456 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001457 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001458 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001459 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001460 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001461 }
Gilles Peskine96889972018-07-12 17:07:03 +02001462 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1463 * but it is permitted. It is common when HMAC is used in HKDF, for
1464 * example. Don't call `memcpy` in the 0-length because `key` could be
1465 * an invalid pointer which would make the behavior undefined. */
1466 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001467 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001468
Gilles Peskined223b522018-06-11 18:12:58 +02001469 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1470 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001471 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001472 ipad[i] ^= 0x36;
1473 memset( ipad + key_length, 0x36, block_size - key_length );
1474
1475 /* Copy the key material from ipad to opad, flipping the requisite bits,
1476 * and filling the rest of opad with the requisite constant. */
1477 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001478 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1479 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001480
Gilles Peskine01126fa2018-07-12 17:04:55 +02001481 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001482 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001483 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001484
Gilles Peskine01126fa2018-07-12 17:04:55 +02001485 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001486
1487cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001488 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001489
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001490 return( status );
1491}
Gilles Peskine248051a2018-06-20 16:09:38 +02001492#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001493
Gilles Peskine89167cb2018-07-08 20:12:23 +02001494static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1495 psa_key_slot_t key,
1496 psa_algorithm_t alg,
1497 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001498{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001499 psa_status_t status;
1500 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001501 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001502 psa_key_usage_t usage =
1503 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001504
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001505 status = psa_mac_init( operation, alg );
1506 if( status != PSA_SUCCESS )
1507 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001508 if( is_sign )
1509 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001510
Gilles Peskine89167cb2018-07-08 20:12:23 +02001511 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001512 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001513 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001514 key_bits = psa_get_key_bits( slot );
1515
Gilles Peskine8c9def32018-02-08 10:02:12 +01001516#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001517 if( alg == PSA_ALG_CMAC )
1518 {
1519 const mbedtls_cipher_info_t *cipher_info =
1520 mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
1521 int ret;
1522 if( cipher_info == NULL )
1523 {
1524 status = PSA_ERROR_NOT_SUPPORTED;
1525 goto exit;
1526 }
1527 operation->mac_size = cipher_info->block_size;
1528 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1529 status = mbedtls_to_psa_error( ret );
1530 }
1531 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001532#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001533#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001534 if( PSA_ALG_IS_HMAC( alg ) )
1535 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001536 psa_algorithm_t hash_alg = PSA_ALG_HMAC_HASH( alg );
1537 if( hash_alg == 0 )
1538 {
1539 status = PSA_ERROR_NOT_SUPPORTED;
1540 goto exit;
1541 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001542
1543 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1544 /* Sanity check. This shouldn't fail on a valid configuration. */
1545 if( operation->mac_size == 0 ||
1546 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1547 {
1548 status = PSA_ERROR_NOT_SUPPORTED;
1549 goto exit;
1550 }
1551
Gilles Peskine01126fa2018-07-12 17:04:55 +02001552 if( slot->type != PSA_KEY_TYPE_HMAC )
1553 {
1554 status = PSA_ERROR_INVALID_ARGUMENT;
1555 goto exit;
1556 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001557
Gilles Peskine01126fa2018-07-12 17:04:55 +02001558 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1559 slot->data.raw.data,
1560 slot->data.raw.bytes,
1561 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001562 }
1563 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001564#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001565 {
1566 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001567 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001568
Gilles Peskinefbfac682018-07-08 20:51:54 +02001569exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001570 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001571 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001572 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001573 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001574 else
1575 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001576 operation->key_set = 1;
1577 }
1578 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001579}
1580
Gilles Peskine89167cb2018-07-08 20:12:23 +02001581psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1582 psa_key_slot_t key,
1583 psa_algorithm_t alg )
1584{
1585 return( psa_mac_setup( operation, key, alg, 1 ) );
1586}
1587
1588psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1589 psa_key_slot_t key,
1590 psa_algorithm_t alg )
1591{
1592 return( psa_mac_setup( operation, key, alg, 0 ) );
1593}
1594
Gilles Peskine8c9def32018-02-08 10:02:12 +01001595psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1596 const uint8_t *input,
1597 size_t input_length )
1598{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001599 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001600 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001601 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001602 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001603 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001604 operation->has_input = 1;
1605
Gilles Peskine8c9def32018-02-08 10:02:12 +01001606#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001607 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001608 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001609 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1610 input, input_length );
1611 status = mbedtls_to_psa_error( ret );
1612 }
1613 else
1614#endif /* MBEDTLS_CMAC_C */
1615#if defined(MBEDTLS_MD_C)
1616 if( PSA_ALG_IS_HMAC( operation->alg ) )
1617 {
1618 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1619 input_length );
1620 }
1621 else
1622#endif /* MBEDTLS_MD_C */
1623 {
1624 /* This shouldn't happen if `operation` was initialized by
1625 * a setup function. */
1626 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001627 }
1628
Gilles Peskinefbfac682018-07-08 20:51:54 +02001629cleanup:
1630 if( status != PSA_SUCCESS )
1631 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001632 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001633}
1634
Gilles Peskine01126fa2018-07-12 17:04:55 +02001635#if defined(MBEDTLS_MD_C)
1636static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1637 uint8_t *mac,
1638 size_t mac_size )
1639{
1640 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1641 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1642 size_t hash_size = 0;
1643 size_t block_size = psa_get_hash_block_size( hash_alg );
1644 psa_status_t status;
1645
Gilles Peskine01126fa2018-07-12 17:04:55 +02001646 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1647 if( status != PSA_SUCCESS )
1648 return( status );
1649 /* From here on, tmp needs to be wiped. */
1650
1651 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1652 if( status != PSA_SUCCESS )
1653 goto exit;
1654
1655 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1656 if( status != PSA_SUCCESS )
1657 goto exit;
1658
1659 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1660 if( status != PSA_SUCCESS )
1661 goto exit;
1662
1663 status = psa_hash_finish( &hmac->hash_ctx, mac, mac_size, &hash_size );
1664
1665exit:
1666 mbedtls_zeroize( tmp, hash_size );
1667 return( status );
1668}
1669#endif /* MBEDTLS_MD_C */
1670
mohammad16036df908f2018-04-02 08:34:15 -07001671static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001672 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001673 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001674{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001675 if( ! operation->key_set )
1676 return( PSA_ERROR_BAD_STATE );
1677 if( operation->iv_required && ! operation->iv_set )
1678 return( PSA_ERROR_BAD_STATE );
1679
Gilles Peskine8c9def32018-02-08 10:02:12 +01001680 if( mac_size < operation->mac_size )
1681 return( PSA_ERROR_BUFFER_TOO_SMALL );
1682
Gilles Peskine8c9def32018-02-08 10:02:12 +01001683#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001684 if( operation->alg == PSA_ALG_CMAC )
1685 {
1686 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1687 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001688 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001689 else
1690#endif /* MBEDTLS_CMAC_C */
1691#if defined(MBEDTLS_MD_C)
1692 if( PSA_ALG_IS_HMAC( operation->alg ) )
1693 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001694 return( psa_hmac_finish_internal( &operation->ctx.hmac,
1695 mac, mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001696 }
1697 else
1698#endif /* MBEDTLS_MD_C */
1699 {
1700 /* This shouldn't happen if `operation` was initialized by
1701 * a setup function. */
1702 return( PSA_ERROR_BAD_STATE );
1703 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001704}
1705
Gilles Peskineacd4be32018-07-08 19:56:25 +02001706psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1707 uint8_t *mac,
1708 size_t mac_size,
1709 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001710{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001711 psa_status_t status;
1712
1713 /* Fill the output buffer with something that isn't a valid mac
1714 * (barring an attack on the mac and deliberately-crafted input),
1715 * in case the caller doesn't check the return status properly. */
1716 *mac_length = mac_size;
1717 /* If mac_size is 0 then mac may be NULL and then the
1718 * call to memset would have undefined behavior. */
1719 if( mac_size != 0 )
1720 memset( mac, '!', mac_size );
1721
Gilles Peskine89167cb2018-07-08 20:12:23 +02001722 if( ! operation->is_sign )
1723 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001724 status = PSA_ERROR_BAD_STATE;
1725 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001726 }
mohammad16036df908f2018-04-02 08:34:15 -07001727
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001728 status = psa_mac_finish_internal( operation, mac, mac_size );
1729
1730cleanup:
1731 if( status == PSA_SUCCESS )
1732 {
1733 status = psa_mac_abort( operation );
1734 if( status == PSA_SUCCESS )
1735 *mac_length = operation->mac_size;
1736 else
1737 memset( mac, '!', mac_size );
1738 }
1739 else
1740 psa_mac_abort( operation );
1741 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001742}
1743
Gilles Peskineacd4be32018-07-08 19:56:25 +02001744psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1745 const uint8_t *mac,
1746 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001747{
Gilles Peskine828ed142018-06-18 23:25:51 +02001748 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001749 psa_status_t status;
1750
Gilles Peskine89167cb2018-07-08 20:12:23 +02001751 if( operation->is_sign )
1752 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001753 status = PSA_ERROR_BAD_STATE;
1754 goto cleanup;
1755 }
1756 if( operation->mac_size != mac_length )
1757 {
1758 status = PSA_ERROR_INVALID_SIGNATURE;
1759 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001760 }
mohammad16036df908f2018-04-02 08:34:15 -07001761
1762 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001763 actual_mac, sizeof( actual_mac ) );
1764
1765 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1766 status = PSA_ERROR_INVALID_SIGNATURE;
1767
1768cleanup:
1769 if( status == PSA_SUCCESS )
1770 status = psa_mac_abort( operation );
1771 else
1772 psa_mac_abort( operation );
1773
1774 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001775}
1776
1777
Gilles Peskine20035e32018-02-03 22:44:14 +01001778
Gilles Peskine20035e32018-02-03 22:44:14 +01001779/****************************************************************/
1780/* Asymmetric cryptography */
1781/****************************************************************/
1782
Gilles Peskine2b450e32018-06-27 15:42:46 +02001783#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001784/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001785 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001786static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1787 size_t hash_length,
1788 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001789{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001790 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001791 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001792 *md_alg = mbedtls_md_get_type( md_info );
1793
1794 /* The Mbed TLS RSA module uses an unsigned int for hash length
1795 * parameters. Validate that it fits so that we don't risk an
1796 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001797#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001798 if( hash_length > UINT_MAX )
1799 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001800#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001801
1802#if defined(MBEDTLS_PKCS1_V15)
1803 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
1804 * must be correct. */
1805 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
1806 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001807 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001808 if( md_info == NULL )
1809 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001810 if( mbedtls_md_get_size( md_info ) != hash_length )
1811 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001812 }
1813#endif /* MBEDTLS_PKCS1_V15 */
1814
1815#if defined(MBEDTLS_PKCS1_V21)
1816 /* PSS requires a hash internally. */
1817 if( PSA_ALG_IS_RSA_PSS( alg ) )
1818 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001819 if( md_info == NULL )
1820 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001821 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001822#endif /* MBEDTLS_PKCS1_V21 */
1823
Gilles Peskine61b91d42018-06-08 16:09:36 +02001824 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001825}
1826
Gilles Peskine2b450e32018-06-27 15:42:46 +02001827static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1828 psa_algorithm_t alg,
1829 const uint8_t *hash,
1830 size_t hash_length,
1831 uint8_t *signature,
1832 size_t signature_size,
1833 size_t *signature_length )
1834{
1835 psa_status_t status;
1836 int ret;
1837 mbedtls_md_type_t md_alg;
1838
1839 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1840 if( status != PSA_SUCCESS )
1841 return( status );
1842
Gilles Peskine630a18a2018-06-29 17:49:35 +02001843 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001844 return( PSA_ERROR_BUFFER_TOO_SMALL );
1845
1846#if defined(MBEDTLS_PKCS1_V15)
1847 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1848 {
1849 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1850 MBEDTLS_MD_NONE );
1851 ret = mbedtls_rsa_pkcs1_sign( rsa,
1852 mbedtls_ctr_drbg_random,
1853 &global_data.ctr_drbg,
1854 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001855 md_alg,
1856 (unsigned int) hash_length,
1857 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001858 signature );
1859 }
1860 else
1861#endif /* MBEDTLS_PKCS1_V15 */
1862#if defined(MBEDTLS_PKCS1_V21)
1863 if( PSA_ALG_IS_RSA_PSS( alg ) )
1864 {
1865 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1866 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1867 mbedtls_ctr_drbg_random,
1868 &global_data.ctr_drbg,
1869 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001870 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001871 (unsigned int) hash_length,
1872 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001873 signature );
1874 }
1875 else
1876#endif /* MBEDTLS_PKCS1_V21 */
1877 {
1878 return( PSA_ERROR_INVALID_ARGUMENT );
1879 }
1880
1881 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02001882 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02001883 return( mbedtls_to_psa_error( ret ) );
1884}
1885
1886static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1887 psa_algorithm_t alg,
1888 const uint8_t *hash,
1889 size_t hash_length,
1890 const uint8_t *signature,
1891 size_t signature_length )
1892{
1893 psa_status_t status;
1894 int ret;
1895 mbedtls_md_type_t md_alg;
1896
1897 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1898 if( status != PSA_SUCCESS )
1899 return( status );
1900
Gilles Peskine630a18a2018-06-29 17:49:35 +02001901 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001902 return( PSA_ERROR_BUFFER_TOO_SMALL );
1903
1904#if defined(MBEDTLS_PKCS1_V15)
1905 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1906 {
1907 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1908 MBEDTLS_MD_NONE );
1909 ret = mbedtls_rsa_pkcs1_verify( rsa,
1910 mbedtls_ctr_drbg_random,
1911 &global_data.ctr_drbg,
1912 MBEDTLS_RSA_PUBLIC,
1913 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001914 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001915 hash,
1916 signature );
1917 }
1918 else
1919#endif /* MBEDTLS_PKCS1_V15 */
1920#if defined(MBEDTLS_PKCS1_V21)
1921 if( PSA_ALG_IS_RSA_PSS( alg ) )
1922 {
1923 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1924 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1925 mbedtls_ctr_drbg_random,
1926 &global_data.ctr_drbg,
1927 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001928 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001929 (unsigned int) hash_length,
1930 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001931 signature );
1932 }
1933 else
1934#endif /* MBEDTLS_PKCS1_V21 */
1935 {
1936 return( PSA_ERROR_INVALID_ARGUMENT );
1937 }
1938 return( mbedtls_to_psa_error( ret ) );
1939}
1940#endif /* MBEDTLS_RSA_C */
1941
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001942#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001943/* `ecp` cannot be const because `ecp->grp` needs to be non-const
1944 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
1945 * (even though these functions don't modify it). */
1946static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
1947 psa_algorithm_t alg,
1948 const uint8_t *hash,
1949 size_t hash_length,
1950 uint8_t *signature,
1951 size_t signature_size,
1952 size_t *signature_length )
1953{
1954 int ret;
1955 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001956 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001957 mbedtls_mpi_init( &r );
1958 mbedtls_mpi_init( &s );
1959
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001960 if( signature_size < 2 * curve_bytes )
1961 {
1962 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
1963 goto cleanup;
1964 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001965
1966 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
1967 {
1968 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
1969 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1970 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
1971 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
1972 hash, hash_length,
1973 md_alg ) );
1974 }
1975 else
1976 {
1977 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
1978 hash, hash_length,
1979 mbedtls_ctr_drbg_random,
1980 &global_data.ctr_drbg ) );
1981 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001982
1983 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
1984 signature,
1985 curve_bytes ) );
1986 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
1987 signature + curve_bytes,
1988 curve_bytes ) );
1989
1990cleanup:
1991 mbedtls_mpi_free( &r );
1992 mbedtls_mpi_free( &s );
1993 if( ret == 0 )
1994 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001995 return( mbedtls_to_psa_error( ret ) );
1996}
1997
1998static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
1999 const uint8_t *hash,
2000 size_t hash_length,
2001 const uint8_t *signature,
2002 size_t signature_length )
2003{
2004 int ret;
2005 mbedtls_mpi r, s;
2006 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2007 mbedtls_mpi_init( &r );
2008 mbedtls_mpi_init( &s );
2009
2010 if( signature_length != 2 * curve_bytes )
2011 return( PSA_ERROR_INVALID_SIGNATURE );
2012
2013 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2014 signature,
2015 curve_bytes ) );
2016 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2017 signature + curve_bytes,
2018 curve_bytes ) );
2019
2020 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2021 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002022
2023cleanup:
2024 mbedtls_mpi_free( &r );
2025 mbedtls_mpi_free( &s );
2026 return( mbedtls_to_psa_error( ret ) );
2027}
2028#endif /* MBEDTLS_ECDSA_C */
2029
Gilles Peskine61b91d42018-06-08 16:09:36 +02002030psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2031 psa_algorithm_t alg,
2032 const uint8_t *hash,
2033 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002034 uint8_t *signature,
2035 size_t signature_size,
2036 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002037{
2038 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002039 psa_status_t status;
2040
2041 *signature_length = signature_size;
2042
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002043 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2044 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002045 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002046 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002047 {
2048 status = PSA_ERROR_INVALID_ARGUMENT;
2049 goto exit;
2050 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002051
Gilles Peskine20035e32018-02-03 22:44:14 +01002052#if defined(MBEDTLS_RSA_C)
2053 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2054 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002055 status = psa_rsa_sign( slot->data.rsa,
2056 alg,
2057 hash, hash_length,
2058 signature, signature_size,
2059 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002060 }
2061 else
2062#endif /* defined(MBEDTLS_RSA_C) */
2063#if defined(MBEDTLS_ECP_C)
2064 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2065 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002066#if defined(MBEDTLS_ECDSA_C)
2067 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002068 status = psa_ecdsa_sign( slot->data.ecp,
2069 alg,
2070 hash, hash_length,
2071 signature, signature_size,
2072 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002073 else
2074#endif /* defined(MBEDTLS_ECDSA_C) */
2075 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002076 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002077 }
itayzafrir5c753392018-05-08 11:18:38 +03002078 }
2079 else
2080#endif /* defined(MBEDTLS_ECP_C) */
2081 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002082 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002083 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002084
2085exit:
2086 /* Fill the unused part of the output buffer (the whole buffer on error,
2087 * the trailing part on success) with something that isn't a valid mac
2088 * (barring an attack on the mac and deliberately-crafted input),
2089 * in case the caller doesn't check the return status properly. */
2090 if( status == PSA_SUCCESS )
2091 memset( signature + *signature_length, '!',
2092 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002093 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002094 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002095 /* If signature_size is 0 then we have nothing to do. We must not call
2096 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002097 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002098}
2099
2100psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2101 psa_algorithm_t alg,
2102 const uint8_t *hash,
2103 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002104 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002105 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002106{
2107 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002108 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002109
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002110 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2111 if( status != PSA_SUCCESS )
2112 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002113
Gilles Peskine61b91d42018-06-08 16:09:36 +02002114#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002115 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002116 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002117 return( psa_rsa_verify( slot->data.rsa,
2118 alg,
2119 hash, hash_length,
2120 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002121 }
2122 else
2123#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002124#if defined(MBEDTLS_ECP_C)
2125 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2126 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002127#if defined(MBEDTLS_ECDSA_C)
2128 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002129 return( psa_ecdsa_verify( slot->data.ecp,
2130 hash, hash_length,
2131 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002132 else
2133#endif /* defined(MBEDTLS_ECDSA_C) */
2134 {
2135 return( PSA_ERROR_INVALID_ARGUMENT );
2136 }
itayzafrir5c753392018-05-08 11:18:38 +03002137 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002138 else
2139#endif /* defined(MBEDTLS_ECP_C) */
2140 {
2141 return( PSA_ERROR_NOT_SUPPORTED );
2142 }
2143}
2144
Gilles Peskine072ac562018-06-30 00:21:29 +02002145#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2146static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2147 mbedtls_rsa_context *rsa )
2148{
2149 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2150 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2151 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2152 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2153}
2154#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2155
Gilles Peskine61b91d42018-06-08 16:09:36 +02002156psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2157 psa_algorithm_t alg,
2158 const uint8_t *input,
2159 size_t input_length,
2160 const uint8_t *salt,
2161 size_t salt_length,
2162 uint8_t *output,
2163 size_t output_size,
2164 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002165{
2166 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002167 psa_status_t status;
2168
Darryl Green5cc689a2018-07-24 15:34:10 +01002169 (void) input;
2170 (void) input_length;
2171 (void) salt;
2172 (void) output;
2173 (void) output_size;
2174
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002175 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002176
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002177 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2178 return( PSA_ERROR_INVALID_ARGUMENT );
2179
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002180 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2181 if( status != PSA_SUCCESS )
2182 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002183 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2184 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002185 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002186
2187#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002188 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002189 {
2190 mbedtls_rsa_context *rsa = slot->data.rsa;
2191 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002192 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002193 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002194#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002195 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002196 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002197 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2198 mbedtls_ctr_drbg_random,
2199 &global_data.ctr_drbg,
2200 MBEDTLS_RSA_PUBLIC,
2201 input_length,
2202 input,
2203 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002204 }
2205 else
2206#endif /* MBEDTLS_PKCS1_V15 */
2207#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002208 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002209 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002210 psa_rsa_oaep_set_padding_mode( alg, rsa );
2211 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2212 mbedtls_ctr_drbg_random,
2213 &global_data.ctr_drbg,
2214 MBEDTLS_RSA_PUBLIC,
2215 salt, salt_length,
2216 input_length,
2217 input,
2218 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002219 }
2220 else
2221#endif /* MBEDTLS_PKCS1_V21 */
2222 {
2223 return( PSA_ERROR_INVALID_ARGUMENT );
2224 }
2225 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002226 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002227 return( mbedtls_to_psa_error( ret ) );
2228 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002229 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002230#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002231 {
2232 return( PSA_ERROR_NOT_SUPPORTED );
2233 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002234}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002235
Gilles Peskine61b91d42018-06-08 16:09:36 +02002236psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2237 psa_algorithm_t alg,
2238 const uint8_t *input,
2239 size_t input_length,
2240 const uint8_t *salt,
2241 size_t salt_length,
2242 uint8_t *output,
2243 size_t output_size,
2244 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002245{
2246 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002247 psa_status_t status;
2248
Darryl Green5cc689a2018-07-24 15:34:10 +01002249 (void) input;
2250 (void) input_length;
2251 (void) salt;
2252 (void) output;
2253 (void) output_size;
2254
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002255 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002256
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002257 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2258 return( PSA_ERROR_INVALID_ARGUMENT );
2259
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002260 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2261 if( status != PSA_SUCCESS )
2262 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002263 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2264 return( PSA_ERROR_INVALID_ARGUMENT );
2265
2266#if defined(MBEDTLS_RSA_C)
2267 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2268 {
2269 mbedtls_rsa_context *rsa = slot->data.rsa;
2270 int ret;
2271
Gilles Peskine630a18a2018-06-29 17:49:35 +02002272 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002273 return( PSA_ERROR_INVALID_ARGUMENT );
2274
2275#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002276 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002277 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002278 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2279 mbedtls_ctr_drbg_random,
2280 &global_data.ctr_drbg,
2281 MBEDTLS_RSA_PRIVATE,
2282 output_length,
2283 input,
2284 output,
2285 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002286 }
2287 else
2288#endif /* MBEDTLS_PKCS1_V15 */
2289#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002290 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002291 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002292 psa_rsa_oaep_set_padding_mode( alg, rsa );
2293 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2294 mbedtls_ctr_drbg_random,
2295 &global_data.ctr_drbg,
2296 MBEDTLS_RSA_PRIVATE,
2297 salt, salt_length,
2298 output_length,
2299 input,
2300 output,
2301 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002302 }
2303 else
2304#endif /* MBEDTLS_PKCS1_V21 */
2305 {
2306 return( PSA_ERROR_INVALID_ARGUMENT );
2307 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002308
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002309 return( mbedtls_to_psa_error( ret ) );
2310 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002311 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002312#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002313 {
2314 return( PSA_ERROR_NOT_SUPPORTED );
2315 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002316}
Gilles Peskine20035e32018-02-03 22:44:14 +01002317
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002318
2319
mohammad1603503973b2018-03-12 15:59:30 +02002320/****************************************************************/
2321/* Symmetric cryptography */
2322/****************************************************************/
2323
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002324/* Initialize the cipher operation structure. Once this function has been
2325 * called, psa_cipher_abort can run and will do the right thing. */
2326static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2327 psa_algorithm_t alg )
2328{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002329 if( ! PSA_ALG_IS_CIPHER( alg ) )
2330 {
2331 memset( operation, 0, sizeof( *operation ) );
2332 return( PSA_ERROR_INVALID_ARGUMENT );
2333 }
2334
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002335 operation->alg = alg;
2336 operation->key_set = 0;
2337 operation->iv_set = 0;
2338 operation->iv_required = 1;
2339 operation->iv_size = 0;
2340 operation->block_size = 0;
2341 mbedtls_cipher_init( &operation->ctx.cipher );
2342 return( PSA_SUCCESS );
2343}
2344
Gilles Peskinee553c652018-06-04 16:22:46 +02002345static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2346 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002347 psa_algorithm_t alg,
2348 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002349{
2350 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2351 psa_status_t status;
2352 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002353 size_t key_bits;
2354 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002355 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2356 PSA_KEY_USAGE_ENCRYPT :
2357 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002358
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002359 status = psa_cipher_init( operation, alg );
2360 if( status != PSA_SUCCESS )
2361 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002362
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002363 status = psa_get_key_from_slot( key, &slot, usage, alg);
2364 if( status != PSA_SUCCESS )
2365 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002366 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002367
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002368 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002369 if( cipher_info == NULL )
2370 return( PSA_ERROR_NOT_SUPPORTED );
2371
mohammad1603503973b2018-03-12 15:59:30 +02002372 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002373 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002374 {
2375 psa_cipher_abort( operation );
2376 return( mbedtls_to_psa_error( ret ) );
2377 }
2378
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002379#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002380 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002381 {
2382 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2383 unsigned char keys[24];
2384 memcpy( keys, slot->data.raw.data, 16 );
2385 memcpy( keys + 16, slot->data.raw.data, 8 );
2386 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2387 keys,
2388 192, cipher_operation );
2389 }
2390 else
2391#endif
2392 {
2393 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2394 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002395 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002396 }
Moran Peker41deec42018-04-04 15:43:05 +03002397 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002398 {
2399 psa_cipher_abort( operation );
2400 return( mbedtls_to_psa_error( ret ) );
2401 }
2402
mohammad16038481e742018-03-18 13:57:31 +02002403#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002404 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002405 {
Gilles Peskine53514202018-06-06 15:11:46 +02002406 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2407 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002408
Moran Pekera28258c2018-05-29 16:25:04 +03002409 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002410 {
2411 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2412 mode = MBEDTLS_PADDING_PKCS7;
2413 break;
2414 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2415 mode = MBEDTLS_PADDING_NONE;
2416 break;
2417 default:
Moran Pekerae382792018-05-31 14:06:17 +03002418 psa_cipher_abort( operation );
2419 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002420 }
2421 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002422 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002423 {
2424 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002425 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002426 }
mohammad16038481e742018-03-18 13:57:31 +02002427 }
2428#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2429
mohammad1603503973b2018-03-12 15:59:30 +02002430 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002431 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002432 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) :
Gilles Peskine7e928852018-06-04 16:23:10 +02002433 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002434 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002435 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002436 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002437 }
mohammad1603503973b2018-03-12 15:59:30 +02002438
Moran Peker395db872018-05-31 14:07:14 +03002439 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002440}
2441
Gilles Peskinefe119512018-07-08 21:39:34 +02002442psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2443 psa_key_slot_t key,
2444 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002445{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002446 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002447}
2448
Gilles Peskinefe119512018-07-08 21:39:34 +02002449psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2450 psa_key_slot_t key,
2451 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002452{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002453 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002454}
2455
Gilles Peskinefe119512018-07-08 21:39:34 +02002456psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2457 unsigned char *iv,
2458 size_t iv_size,
2459 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002460{
Moran Peker41deec42018-04-04 15:43:05 +03002461 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002462 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002463 return( PSA_ERROR_BAD_STATE );
2464 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002465 {
Moran Peker41deec42018-04-04 15:43:05 +03002466 ret = PSA_ERROR_BUFFER_TOO_SMALL;
2467 goto exit;
2468 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002469 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2470 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002471 if( ret != 0 )
2472 {
2473 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002474 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002475 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002476
mohammad16038481e742018-03-18 13:57:31 +02002477 *iv_length = operation->iv_size;
Gilles Peskinefe119512018-07-08 21:39:34 +02002478 ret = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002479
Moran Peker395db872018-05-31 14:07:14 +03002480exit:
2481 if( ret != PSA_SUCCESS )
2482 psa_cipher_abort( operation );
2483 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02002484}
2485
Gilles Peskinefe119512018-07-08 21:39:34 +02002486psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2487 const unsigned char *iv,
2488 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002489{
Moran Peker41deec42018-04-04 15:43:05 +03002490 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002491 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002492 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03002493 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002494 {
Moran Pekerae382792018-05-31 14:06:17 +03002495 psa_cipher_abort( operation );
2496 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03002497 }
mohammad1603503973b2018-03-12 15:59:30 +02002498 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002499 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002500 {
2501 psa_cipher_abort( operation );
2502 return( mbedtls_to_psa_error( ret ) );
2503 }
2504
2505 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02002506
Moran Peker395db872018-05-31 14:07:14 +03002507 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002508}
2509
Gilles Peskinee553c652018-06-04 16:22:46 +02002510psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2511 const uint8_t *input,
2512 size_t input_length,
2513 unsigned char *output,
2514 size_t output_size,
2515 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002516{
2517 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002518 size_t expected_output_size;
2519 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2520 {
2521 /* Take the unprocessed partial block left over from previous
2522 * update calls, if any, plus the input to this call. Remove
2523 * the last partial block, if any. You get the data that will be
2524 * output in this call. */
2525 expected_output_size =
2526 ( operation->ctx.cipher.unprocessed_len + input_length )
2527 / operation->block_size * operation->block_size;
2528 }
2529 else
2530 {
2531 expected_output_size = input_length;
2532 }
2533 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03002534 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02002535
mohammad1603503973b2018-03-12 15:59:30 +02002536 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002537 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002538 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002539 {
2540 psa_cipher_abort( operation );
2541 return( mbedtls_to_psa_error( ret ) );
2542 }
2543
Moran Peker395db872018-05-31 14:07:14 +03002544 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002545}
2546
Gilles Peskinee553c652018-06-04 16:22:46 +02002547psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2548 uint8_t *output,
2549 size_t output_size,
2550 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002551{
Janos Follath315b51c2018-07-09 16:04:51 +01002552 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2553 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002554 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002555
mohammad1603503973b2018-03-12 15:59:30 +02002556 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002557 {
Janos Follath315b51c2018-07-09 16:04:51 +01002558 status = PSA_ERROR_BAD_STATE;
2559 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002560 }
2561 if( operation->iv_required && ! operation->iv_set )
2562 {
Janos Follath315b51c2018-07-09 16:04:51 +01002563 status = PSA_ERROR_BAD_STATE;
2564 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002565 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002566 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2567 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002568 {
Gilles Peskine53514202018-06-06 15:11:46 +02002569 psa_algorithm_t padding_mode =
2570 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002571 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002572 {
Janos Follath315b51c2018-07-09 16:04:51 +01002573 status = PSA_ERROR_TAMPERING_DETECTED;
2574 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002575 }
Gilles Peskine53514202018-06-06 15:11:46 +02002576 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002577 {
2578 if( operation->ctx.cipher.unprocessed_len != 0 )
2579 {
Janos Follath315b51c2018-07-09 16:04:51 +01002580 status = PSA_ERROR_INVALID_ARGUMENT;
2581 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002582 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002583 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002584 }
2585
Janos Follath315b51c2018-07-09 16:04:51 +01002586 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2587 temp_output_buffer,
2588 output_length );
2589 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002590 {
Janos Follath315b51c2018-07-09 16:04:51 +01002591 status = mbedtls_to_psa_error( cipher_ret );
2592 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002593 }
Janos Follath315b51c2018-07-09 16:04:51 +01002594
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002595 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002596 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002597 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002598 memcpy( output, temp_output_buffer, *output_length );
2599 else
2600 {
Janos Follath315b51c2018-07-09 16:04:51 +01002601 status = PSA_ERROR_BUFFER_TOO_SMALL;
2602 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002603 }
mohammad1603503973b2018-03-12 15:59:30 +02002604
Janos Follath279ab8e2018-07-09 16:13:21 +01002605 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002606 status = psa_cipher_abort( operation );
2607
2608 return( status );
2609
2610error:
2611
2612 *output_length = 0;
2613
Janos Follath279ab8e2018-07-09 16:13:21 +01002614 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002615 (void) psa_cipher_abort( operation );
2616
2617 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002618}
2619
Gilles Peskinee553c652018-06-04 16:22:46 +02002620psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2621{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002622 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002623 {
2624 /* The object has (apparently) been initialized but it is not
2625 * in use. It's ok to call abort on such an object, and there's
2626 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002627 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002628 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002629
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002630 /* Sanity check (shouldn't happen: operation->alg should
2631 * always have been initialized to a valid value). */
2632 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2633 return( PSA_ERROR_BAD_STATE );
2634
mohammad1603503973b2018-03-12 15:59:30 +02002635 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002636
Moran Peker41deec42018-04-04 15:43:05 +03002637 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002638 operation->key_set = 0;
2639 operation->iv_set = 0;
2640 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002641 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002642 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002643
Moran Peker395db872018-05-31 14:07:14 +03002644 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002645}
2646
Gilles Peskinea0655c32018-04-30 17:06:50 +02002647
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002648
mohammad16038cc1cee2018-03-28 01:21:33 +03002649/****************************************************************/
2650/* Key Policy */
2651/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002652
mohammad160327010052018-07-03 13:16:15 +03002653#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002654void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002655{
Gilles Peskine803ce742018-06-18 16:07:14 +02002656 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002657}
2658
Gilles Peskine2d277862018-06-18 15:41:12 +02002659void psa_key_policy_set_usage( psa_key_policy_t *policy,
2660 psa_key_usage_t usage,
2661 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002662{
mohammad16034eed7572018-03-28 05:14:59 -07002663 policy->usage = usage;
2664 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002665}
2666
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002667psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002668{
mohammad16036df908f2018-04-02 08:34:15 -07002669 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002670}
2671
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002672psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002673{
mohammad16036df908f2018-04-02 08:34:15 -07002674 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002675}
mohammad160327010052018-07-03 13:16:15 +03002676#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002677
Gilles Peskine2d277862018-06-18 15:41:12 +02002678psa_status_t psa_set_key_policy( psa_key_slot_t key,
2679 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002680{
2681 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002682 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002683
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002684 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002685 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002686
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002687 status = psa_get_empty_key_slot( key, &slot );
2688 if( status != PSA_SUCCESS )
2689 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002690
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002691 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2692 PSA_KEY_USAGE_ENCRYPT |
2693 PSA_KEY_USAGE_DECRYPT |
2694 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002695 PSA_KEY_USAGE_VERIFY |
2696 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002697 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002698
mohammad16036df908f2018-04-02 08:34:15 -07002699 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002700
2701 return( PSA_SUCCESS );
2702}
2703
Gilles Peskine2d277862018-06-18 15:41:12 +02002704psa_status_t psa_get_key_policy( psa_key_slot_t key,
2705 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002706{
2707 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002708 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002709
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002710 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002711 return( PSA_ERROR_INVALID_ARGUMENT );
2712
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002713 status = psa_get_key_slot( key, &slot );
2714 if( status != PSA_SUCCESS )
2715 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002716
mohammad16036df908f2018-04-02 08:34:15 -07002717 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002718
2719 return( PSA_SUCCESS );
2720}
Gilles Peskine20035e32018-02-03 22:44:14 +01002721
Gilles Peskinea0655c32018-04-30 17:06:50 +02002722
2723
mohammad1603804cd712018-03-20 22:44:08 +02002724/****************************************************************/
2725/* Key Lifetime */
2726/****************************************************************/
2727
Gilles Peskine2d277862018-06-18 15:41:12 +02002728psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2729 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002730{
2731 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002732 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002733
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002734 status = psa_get_key_slot( key, &slot );
2735 if( status != PSA_SUCCESS )
2736 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002737
mohammad1603804cd712018-03-20 22:44:08 +02002738 *lifetime = slot->lifetime;
2739
2740 return( PSA_SUCCESS );
2741}
2742
Gilles Peskine2d277862018-06-18 15:41:12 +02002743psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002744 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002745{
2746 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002747 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002748
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002749 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2750 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002751 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2752 return( PSA_ERROR_INVALID_ARGUMENT );
2753
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002754 status = psa_get_empty_key_slot( key, &slot );
2755 if( status != PSA_SUCCESS )
2756 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002757
Moran Pekerd7326592018-05-29 16:56:39 +03002758 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002759 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002760
mohammad1603060ad8a2018-03-20 14:28:38 -07002761 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002762
2763 return( PSA_SUCCESS );
2764}
2765
Gilles Peskine20035e32018-02-03 22:44:14 +01002766
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002767
mohammad16035955c982018-04-26 00:53:03 +03002768/****************************************************************/
2769/* AEAD */
2770/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002771
mohammad16035955c982018-04-26 00:53:03 +03002772psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2773 psa_algorithm_t alg,
2774 const uint8_t *nonce,
2775 size_t nonce_length,
2776 const uint8_t *additional_data,
2777 size_t additional_data_length,
2778 const uint8_t *plaintext,
2779 size_t plaintext_length,
2780 uint8_t *ciphertext,
2781 size_t ciphertext_size,
2782 size_t *ciphertext_length )
2783{
2784 int ret;
2785 psa_status_t status;
2786 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002787 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002788 uint8_t *tag;
2789 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002790 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002791 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002792
mohammad1603f08a5502018-06-03 15:05:47 +03002793 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002794
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002795 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2796 if( status != PSA_SUCCESS )
2797 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002798 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002799
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002800 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002801 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002802 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002803 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002804
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002805 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002806 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002807 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002808
mohammad16035955c982018-04-26 00:53:03 +03002809 if( alg == PSA_ALG_GCM )
2810 {
2811 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002812 tag_length = 16;
2813
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002814 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002815 return( PSA_ERROR_INVALID_ARGUMENT );
2816
mohammad160315223a82018-06-03 17:19:55 +03002817 //make sure we have place to hold the tag in the ciphertext buffer
2818 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002819 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002820
2821 //update the tag pointer to point to the end of the ciphertext_length
2822 tag = ciphertext + plaintext_length;
2823
mohammad16035955c982018-04-26 00:53:03 +03002824 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002825 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002826 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002827 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002828 if( ret != 0 )
2829 {
2830 mbedtls_gcm_free( &gcm );
2831 return( mbedtls_to_psa_error( ret ) );
2832 }
2833 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002834 plaintext_length, nonce,
2835 nonce_length, additional_data,
2836 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002837 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002838 mbedtls_gcm_free( &gcm );
2839 }
2840 else if( alg == PSA_ALG_CCM )
2841 {
2842 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002843 tag_length = 16;
2844
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002845 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002846 return( PSA_ERROR_INVALID_ARGUMENT );
2847
mohammad160347ddf3d2018-04-26 01:11:21 +03002848 if( nonce_length < 7 || nonce_length > 13 )
2849 return( PSA_ERROR_INVALID_ARGUMENT );
2850
mohammad160315223a82018-06-03 17:19:55 +03002851 //make sure we have place to hold the tag in the ciphertext buffer
2852 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002853 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002854
2855 //update the tag pointer to point to the end of the ciphertext_length
2856 tag = ciphertext + plaintext_length;
2857
mohammad16035955c982018-04-26 00:53:03 +03002858 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002859 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002860 slot->data.raw.data,
2861 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002862 if( ret != 0 )
2863 {
2864 mbedtls_ccm_free( &ccm );
2865 return( mbedtls_to_psa_error( ret ) );
2866 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002867 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002868 nonce, nonce_length,
2869 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002870 additional_data_length,
2871 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002872 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002873 mbedtls_ccm_free( &ccm );
2874 }
mohammad16035c8845f2018-05-09 05:40:09 -07002875 else
2876 {
mohammad1603554faad2018-06-03 15:07:38 +03002877 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002878 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002879
mohammad160315223a82018-06-03 17:19:55 +03002880 if( ret != 0 )
2881 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002882 /* If ciphertext_size is 0 then ciphertext may be NULL and then the
2883 * call to memset would have undefined behavior. */
2884 if( ciphertext_size != 0 )
2885 memset( ciphertext, 0, ciphertext_size );
mohammad160315223a82018-06-03 17:19:55 +03002886 return( mbedtls_to_psa_error( ret ) );
2887 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002888
mohammad160315223a82018-06-03 17:19:55 +03002889 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002890 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002891}
2892
Gilles Peskineee652a32018-06-01 19:23:52 +02002893/* Locate the tag in a ciphertext buffer containing the encrypted data
2894 * followed by the tag. Return the length of the part preceding the tag in
2895 * *plaintext_length. This is the size of the plaintext in modes where
2896 * the encrypted data has the same size as the plaintext, such as
2897 * CCM and GCM. */
2898static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2899 const uint8_t *ciphertext,
2900 size_t ciphertext_length,
2901 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002902 const uint8_t **p_tag )
2903{
2904 size_t payload_length;
2905 if( tag_length > ciphertext_length )
2906 return( PSA_ERROR_INVALID_ARGUMENT );
2907 payload_length = ciphertext_length - tag_length;
2908 if( payload_length > plaintext_size )
2909 return( PSA_ERROR_BUFFER_TOO_SMALL );
2910 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002911 return( PSA_SUCCESS );
2912}
2913
mohammad16035955c982018-04-26 00:53:03 +03002914psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2915 psa_algorithm_t alg,
2916 const uint8_t *nonce,
2917 size_t nonce_length,
2918 const uint8_t *additional_data,
2919 size_t additional_data_length,
2920 const uint8_t *ciphertext,
2921 size_t ciphertext_length,
2922 uint8_t *plaintext,
2923 size_t plaintext_size,
2924 size_t *plaintext_length )
2925{
2926 int ret;
2927 psa_status_t status;
2928 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002929 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002930 const uint8_t *tag;
2931 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002932 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002933 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002934
Gilles Peskineee652a32018-06-01 19:23:52 +02002935 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002936
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002937 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2938 if( status != PSA_SUCCESS )
2939 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002940 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002941
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002942 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002943 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002944 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002945 return( PSA_ERROR_NOT_SUPPORTED );
2946
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002947 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002948 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002949 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002950
mohammad16035955c982018-04-26 00:53:03 +03002951 if( alg == PSA_ALG_GCM )
2952 {
2953 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002954
Gilles Peskineee652a32018-06-01 19:23:52 +02002955 tag_length = 16;
2956 status = psa_aead_unpadded_locate_tag( tag_length,
2957 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002958 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002959 if( status != PSA_SUCCESS )
2960 return( status );
2961
mohammad16035955c982018-04-26 00:53:03 +03002962 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002963 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002964 slot->data.raw.data,
2965 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002966 if( ret != 0 )
2967 {
2968 mbedtls_gcm_free( &gcm );
2969 return( mbedtls_to_psa_error( ret ) );
2970 }
mohammad16035955c982018-04-26 00:53:03 +03002971
Gilles Peskineee652a32018-06-01 19:23:52 +02002972 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002973 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002974 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002975 additional_data,
2976 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002977 tag, tag_length,
2978 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002979 mbedtls_gcm_free( &gcm );
2980 }
2981 else if( alg == PSA_ALG_CCM )
2982 {
2983 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002984
mohammad160347ddf3d2018-04-26 01:11:21 +03002985 if( nonce_length < 7 || nonce_length > 13 )
2986 return( PSA_ERROR_INVALID_ARGUMENT );
2987
mohammad16039375f842018-06-03 14:28:24 +03002988 tag_length = 16;
2989 status = psa_aead_unpadded_locate_tag( tag_length,
2990 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002991 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002992 if( status != PSA_SUCCESS )
2993 return( status );
2994
mohammad16035955c982018-04-26 00:53:03 +03002995 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002996 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002997 slot->data.raw.data,
2998 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002999 if( ret != 0 )
3000 {
3001 mbedtls_ccm_free( &ccm );
3002 return( mbedtls_to_psa_error( ret ) );
3003 }
mohammad160360a64d02018-06-03 17:20:42 +03003004 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003005 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003006 additional_data,
3007 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003008 ciphertext, plaintext,
3009 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03003010 mbedtls_ccm_free( &ccm );
3011 }
mohammad160339574652018-06-01 04:39:53 -07003012 else
3013 {
mohammad1603554faad2018-06-03 15:07:38 +03003014 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003015 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003016
Gilles Peskineee652a32018-06-01 19:23:52 +02003017 if( ret != 0 )
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003018 {
3019 /* If plaintext_size is 0 then plaintext may be NULL and then the
3020 * call to memset has undefined behavior. */
3021 if( plaintext_size != 0 )
3022 memset( plaintext, 0, plaintext_size );
3023 }
mohammad160360a64d02018-06-03 17:20:42 +03003024 else
3025 *plaintext_length = ciphertext_length - tag_length;
3026
Gilles Peskineee652a32018-06-01 19:23:52 +02003027 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03003028}
3029
Gilles Peskinea0655c32018-04-30 17:06:50 +02003030
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003031
Gilles Peskine20035e32018-02-03 22:44:14 +01003032/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003033/* Generators */
3034/****************************************************************/
3035
3036psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3037{
3038 psa_status_t status = PSA_SUCCESS;
3039 if( generator->alg == 0 )
3040 {
3041 /* The object has (apparently) been initialized but it is not
3042 * in use. It's ok to call abort on such an object, and there's
3043 * nothing to do. */
3044 }
3045 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003046#if defined(MBEDTLS_MD_C)
3047 if( PSA_ALG_IS_HKDF( generator->alg ) )
3048 {
3049 mbedtls_free( generator->ctx.hkdf.info );
3050 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3051 }
3052 else
3053#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003054 {
3055 status = PSA_ERROR_BAD_STATE;
3056 }
3057 memset( generator, 0, sizeof( *generator ) );
3058 return( status );
3059}
3060
3061
3062psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3063 size_t *capacity)
3064{
3065 *capacity = generator->capacity;
3066 return( PSA_SUCCESS );
3067}
3068
Gilles Peskinebef7f142018-07-12 17:22:21 +02003069#if defined(MBEDTLS_MD_C)
3070/* Read some bytes from an HKDF-based generator. This performs a chunk
3071 * of the expand phase of the HKDF algorithm. */
3072static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3073 psa_algorithm_t hash_alg,
3074 uint8_t *output,
3075 size_t output_length )
3076{
3077 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3078 psa_status_t status;
3079
3080 while( output_length != 0 )
3081 {
3082 /* Copy what remains of the current block */
3083 uint8_t n = hash_length - hkdf->offset_in_block;
3084 if( n > output_length )
3085 n = (uint8_t) output_length;
3086 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3087 output += n;
3088 output_length -= n;
3089 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003090 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003091 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003092 /* We can't be wanting more output after block 0xff, otherwise
3093 * the capacity check in psa_generator_read() would have
3094 * prevented this call. It could happen only if the generator
3095 * object was corrupted or if this function is called directly
3096 * inside the library. */
3097 if( hkdf->block_number == 0xff )
3098 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003099
3100 /* We need a new block */
3101 ++hkdf->block_number;
3102 hkdf->offset_in_block = 0;
3103 status = psa_hmac_setup_internal( &hkdf->hmac,
3104 hkdf->prk, hash_length,
3105 hash_alg );
3106 if( status != PSA_SUCCESS )
3107 return( status );
3108 if( hkdf->block_number != 1 )
3109 {
3110 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3111 hkdf->output_block,
3112 hash_length );
3113 if( status != PSA_SUCCESS )
3114 return( status );
3115 }
3116 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3117 hkdf->info,
3118 hkdf->info_length );
3119 if( status != PSA_SUCCESS )
3120 return( status );
3121 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3122 &hkdf->block_number, 1 );
3123 if( status != PSA_SUCCESS )
3124 return( status );
3125 status = psa_hmac_finish_internal( &hkdf->hmac,
3126 hkdf->output_block,
3127 sizeof( hkdf->output_block ) );
3128 if( status != PSA_SUCCESS )
3129 return( status );
3130 }
3131
3132 return( PSA_SUCCESS );
3133}
3134#endif /* MBEDTLS_MD_C */
3135
Gilles Peskineeab56e42018-07-12 17:12:33 +02003136psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3137 uint8_t *output,
3138 size_t output_length )
3139{
3140 psa_status_t status;
3141
3142 if( output_length > generator->capacity )
3143 {
3144 generator->capacity = 0;
3145 /* Go through the error path to wipe all confidential data now
3146 * that the generator object is useless. */
3147 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3148 goto exit;
3149 }
3150 if( output_length == 0 &&
3151 generator->capacity == 0 && generator->alg == 0 )
3152 {
3153 /* Edge case: this is a blank or finished generator, and 0
3154 * bytes were requested. The right error in this case could
3155 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3156 * INSUFFICIENT_CAPACITY, which is right for a finished
3157 * generator, for consistency with the case when
3158 * output_length > 0. */
3159 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3160 }
3161 generator->capacity -= output_length;
3162
Gilles Peskinebef7f142018-07-12 17:22:21 +02003163#if defined(MBEDTLS_MD_C)
3164 if( PSA_ALG_IS_HKDF( generator->alg ) )
3165 {
3166 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3167 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3168 output, output_length );
3169 }
3170 else
3171#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003172 {
3173 return( PSA_ERROR_BAD_STATE );
3174 }
3175
3176exit:
3177 if( status != PSA_SUCCESS )
3178 {
3179 psa_generator_abort( generator );
3180 memset( output, '!', output_length );
3181 }
3182 return( status );
3183}
3184
Gilles Peskine08542d82018-07-19 17:05:42 +02003185#if defined(MBEDTLS_DES_C)
3186static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3187{
3188 if( data_size >= 8 )
3189 mbedtls_des_key_set_parity( data );
3190 if( data_size >= 16 )
3191 mbedtls_des_key_set_parity( data + 8 );
3192 if( data_size >= 24 )
3193 mbedtls_des_key_set_parity( data + 16 );
3194}
3195#endif /* MBEDTLS_DES_C */
3196
Gilles Peskineeab56e42018-07-12 17:12:33 +02003197psa_status_t psa_generator_import_key( psa_key_slot_t key,
3198 psa_key_type_t type,
3199 size_t bits,
3200 psa_crypto_generator_t *generator )
3201{
3202 uint8_t *data = NULL;
3203 size_t bytes = PSA_BITS_TO_BYTES( bits );
3204 psa_status_t status;
3205
3206 if( ! key_type_is_raw_bytes( type ) )
3207 return( PSA_ERROR_INVALID_ARGUMENT );
3208 if( bits % 8 != 0 )
3209 return( PSA_ERROR_INVALID_ARGUMENT );
3210 data = mbedtls_calloc( 1, bytes );
3211 if( data == NULL )
3212 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3213
3214 status = psa_generator_read( generator, data, bytes );
3215 if( status != PSA_SUCCESS )
3216 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003217#if defined(MBEDTLS_DES_C)
3218 if( type == PSA_KEY_TYPE_DES )
3219 psa_des_set_key_parity( data, bytes );
3220#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003221 status = psa_import_key( key, type, data, bytes );
3222
3223exit:
3224 mbedtls_free( data );
3225 return( status );
3226}
3227
3228
3229
3230/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003231/* Key derivation */
3232/****************************************************************/
3233
Gilles Peskinebef7f142018-07-12 17:22:21 +02003234/* Set up an HKDF-based generator. This is exactly the extract phase
3235 * of the HKDF algorithm. */
3236static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
3237 key_slot_t *slot,
3238 psa_algorithm_t hash_alg,
3239 const uint8_t *salt,
3240 size_t salt_length,
3241 const uint8_t *label,
3242 size_t label_length )
3243{
3244 psa_status_t status;
3245 status = psa_hmac_setup_internal( &hkdf->hmac,
3246 salt, salt_length,
3247 PSA_ALG_HMAC_HASH( hash_alg ) );
3248 if( status != PSA_SUCCESS )
3249 return( status );
3250 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3251 slot->data.raw.data,
3252 slot->data.raw.bytes );
3253 if( status != PSA_SUCCESS )
3254 return( status );
3255 status = psa_hmac_finish_internal( &hkdf->hmac,
3256 hkdf->prk,
3257 sizeof( hkdf->prk ) );
3258 if( status != PSA_SUCCESS )
3259 return( status );
3260 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3261 hkdf->block_number = 0;
3262 hkdf->info_length = label_length;
3263 if( label_length != 0 )
3264 {
3265 hkdf->info = mbedtls_calloc( 1, label_length );
3266 if( hkdf->info == NULL )
3267 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3268 memcpy( hkdf->info, label, label_length );
3269 }
3270 return( PSA_SUCCESS );
3271}
3272
Gilles Peskineea0fb492018-07-12 17:17:20 +02003273psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01003274 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003275 psa_algorithm_t alg,
3276 const uint8_t *salt,
3277 size_t salt_length,
3278 const uint8_t *label,
3279 size_t label_length,
3280 size_t capacity )
3281{
3282 key_slot_t *slot;
3283 psa_status_t status;
3284
3285 if( generator->alg != 0 )
3286 return( PSA_ERROR_BAD_STATE );
3287
3288 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
3289 if( status != PSA_SUCCESS )
3290 return( status );
3291 if( slot->type != PSA_KEY_TYPE_DERIVE )
3292 return( PSA_ERROR_INVALID_ARGUMENT );
3293
3294 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
3295 return( PSA_ERROR_INVALID_ARGUMENT );
3296
Gilles Peskinebef7f142018-07-12 17:22:21 +02003297#if defined(MBEDTLS_MD_C)
3298 if( PSA_ALG_IS_HKDF( alg ) )
3299 {
3300 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3301 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3302 if( hash_size == 0 )
3303 return( PSA_ERROR_NOT_SUPPORTED );
3304 if( capacity > 255 * hash_size )
3305 return( PSA_ERROR_INVALID_ARGUMENT );
3306 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3307 slot,
3308 hash_alg,
3309 salt, salt_length,
3310 label, label_length );
3311 }
3312 else
3313#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +02003314 {
3315 return( PSA_ERROR_NOT_SUPPORTED );
3316 }
3317
3318 /* Set generator->alg even on failure so that abort knows what to do. */
3319 generator->alg = alg;
3320 if( status == PSA_SUCCESS )
3321 generator->capacity = capacity;
3322 else
3323 psa_generator_abort( generator );
3324 return( status );
3325}
3326
3327
3328
3329/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003330/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02003331/****************************************************************/
3332
3333psa_status_t psa_generate_random( uint8_t *output,
3334 size_t output_size )
3335{
3336 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3337 output, output_size );
3338 return( mbedtls_to_psa_error( ret ) );
3339}
3340
3341psa_status_t psa_generate_key( psa_key_slot_t key,
3342 psa_key_type_t type,
3343 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003344 const void *extra,
3345 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02003346{
Gilles Peskine12313cd2018-06-20 00:20:32 +02003347 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003348 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003349
Gilles Peskine53d991e2018-07-12 01:14:59 +02003350 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003351 return( PSA_ERROR_INVALID_ARGUMENT );
3352
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003353 status = psa_get_empty_key_slot( key, &slot );
3354 if( status != PSA_SUCCESS )
3355 return( status );
3356
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003357 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003358 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003359 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003360 if( status != PSA_SUCCESS )
3361 return( status );
3362 status = psa_generate_random( slot->data.raw.data,
3363 slot->data.raw.bytes );
3364 if( status != PSA_SUCCESS )
3365 {
3366 mbedtls_free( slot->data.raw.data );
3367 return( status );
3368 }
3369#if defined(MBEDTLS_DES_C)
3370 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02003371 psa_des_set_key_parity( slot->data.raw.data,
3372 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003373#endif /* MBEDTLS_DES_C */
3374 }
3375 else
3376
3377#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3378 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3379 {
3380 mbedtls_rsa_context *rsa;
3381 int ret;
3382 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003383 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3384 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02003385 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003386 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02003387 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003388 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003389 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003390#if INT_MAX < 0xffffffff
3391 /* Check that the uint32_t value passed by the caller fits
3392 * in the range supported by this implementation. */
3393 if( p->e > INT_MAX )
3394 return( PSA_ERROR_NOT_SUPPORTED );
3395#endif
3396 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003397 }
3398 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3399 if( rsa == NULL )
3400 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3401 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3402 ret = mbedtls_rsa_gen_key( rsa,
3403 mbedtls_ctr_drbg_random,
3404 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003405 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003406 exponent );
3407 if( ret != 0 )
3408 {
3409 mbedtls_rsa_free( rsa );
3410 mbedtls_free( rsa );
3411 return( mbedtls_to_psa_error( ret ) );
3412 }
3413 slot->data.rsa = rsa;
3414 }
3415 else
3416#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3417
3418#if defined(MBEDTLS_ECP_C)
3419 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3420 {
3421 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3422 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3423 const mbedtls_ecp_curve_info *curve_info =
3424 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3425 mbedtls_ecp_keypair *ecp;
3426 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003427 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003428 return( PSA_ERROR_NOT_SUPPORTED );
3429 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3430 return( PSA_ERROR_NOT_SUPPORTED );
3431 if( curve_info->bit_size != bits )
3432 return( PSA_ERROR_INVALID_ARGUMENT );
3433 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3434 if( ecp == NULL )
3435 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3436 mbedtls_ecp_keypair_init( ecp );
3437 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3438 mbedtls_ctr_drbg_random,
3439 &global_data.ctr_drbg );
3440 if( ret != 0 )
3441 {
3442 mbedtls_ecp_keypair_free( ecp );
3443 mbedtls_free( ecp );
3444 return( mbedtls_to_psa_error( ret ) );
3445 }
3446 slot->data.ecp = ecp;
3447 }
3448 else
3449#endif /* MBEDTLS_ECP_C */
3450
3451 return( PSA_ERROR_NOT_SUPPORTED );
3452
3453 slot->type = type;
3454 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003455}
3456
3457
3458/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003459/* Module setup */
3460/****************************************************************/
3461
Gilles Peskinee59236f2018-01-27 23:32:46 +01003462void mbedtls_psa_crypto_free( void )
3463{
Jaeden Amero045bd502018-06-26 14:00:08 +01003464 psa_key_slot_t key;
Gilles Peskine828ed142018-06-18 23:25:51 +02003465 for( key = 1; key < PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003466 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003467 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3468 mbedtls_entropy_free( &global_data.entropy );
3469 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3470}
3471
3472psa_status_t psa_crypto_init( void )
3473{
3474 int ret;
3475 const unsigned char drbg_seed[] = "PSA";
3476
3477 if( global_data.initialized != 0 )
3478 return( PSA_SUCCESS );
3479
3480 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3481 mbedtls_entropy_init( &global_data.entropy );
3482 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3483
3484 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3485 mbedtls_entropy_func,
3486 &global_data.entropy,
3487 drbg_seed, sizeof( drbg_seed ) - 1 );
3488 if( ret != 0 )
3489 goto exit;
3490
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003491 global_data.initialized = 1;
3492
Gilles Peskinee59236f2018-01-27 23:32:46 +01003493exit:
3494 if( ret != 0 )
3495 mbedtls_psa_crypto_free( );
3496 return( mbedtls_to_psa_error( ret ) );
3497}
3498
3499#endif /* MBEDTLS_PSA_CRYPTO_C */