blob: c7d5a67807aa600e909992d3166c6969e807025d [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
Gilles Peskine996deb12018-08-01 15:45:45 +020085#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
86
Gilles Peskinee59236f2018-01-27 23:32:46 +010087/* Implementation that should never be optimized out by the compiler */
88static void mbedtls_zeroize( void *v, size_t n )
89{
90 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
91}
92
Gilles Peskine9ef733f2018-02-07 21:05:37 +010093/* constant-time buffer comparison */
94static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
95{
96 size_t i;
97 unsigned char diff = 0;
98
99 for( i = 0; i < n; i++ )
100 diff |= a[i] ^ b[i];
101
102 return( diff );
103}
104
105
106
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100107/****************************************************************/
108/* Global data, support functions and library management */
109/****************************************************************/
110
111/* Number of key slots (plus one because 0 is not used).
112 * The value is a compile-time constant for now, for simplicity. */
Gilles Peskine828ed142018-06-18 23:25:51 +0200113#define PSA_KEY_SLOT_COUNT 32
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100114
Gilles Peskine2d277862018-06-18 15:41:12 +0200115typedef struct
116{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100117 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +0300118 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200119 psa_key_lifetime_t lifetime;
Gilles Peskine2d277862018-06-18 15:41:12 +0200120 union
121 {
122 struct raw_data
123 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100124 uint8_t *data;
125 size_t bytes;
126 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100127#if defined(MBEDTLS_RSA_C)
128 mbedtls_rsa_context *rsa;
129#endif /* MBEDTLS_RSA_C */
130#if defined(MBEDTLS_ECP_C)
131 mbedtls_ecp_keypair *ecp;
132#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100133 } data;
134} key_slot_t;
135
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200136static int key_type_is_raw_bytes( psa_key_type_t type )
137{
138 psa_key_type_t category = type & PSA_KEY_TYPE_CATEGORY_MASK;
139 return( category == PSA_KEY_TYPE_RAW_DATA ||
140 category == PSA_KEY_TYPE_CATEGORY_SYMMETRIC );
141}
142
Gilles Peskine2d277862018-06-18 15:41:12 +0200143typedef struct
144{
Gilles Peskinee59236f2018-01-27 23:32:46 +0100145 int initialized;
146 mbedtls_entropy_context entropy;
147 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200148 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinee59236f2018-01-27 23:32:46 +0100149} psa_global_data_t;
150
151static psa_global_data_t global_data;
152
153static psa_status_t mbedtls_to_psa_error( int ret )
154{
Gilles Peskinea5905292018-02-07 20:59:33 +0100155 /* If there's both a high-level code and low-level code, dispatch on
156 * the high-level code. */
157 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100158 {
159 case 0:
160 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100161
162 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
163 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
164 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
165 return( PSA_ERROR_NOT_SUPPORTED );
166 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
167 return( PSA_ERROR_HARDWARE_FAILURE );
168
169 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
170 return( PSA_ERROR_HARDWARE_FAILURE );
171
Gilles Peskine9a944802018-06-21 09:35:35 +0200172 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
173 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
174 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
175 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
176 case MBEDTLS_ERR_ASN1_INVALID_DATA:
177 return( PSA_ERROR_INVALID_ARGUMENT );
178 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
179 return( PSA_ERROR_INSUFFICIENT_MEMORY );
180 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
181 return( PSA_ERROR_BUFFER_TOO_SMALL );
182
Gilles Peskinea5905292018-02-07 20:59:33 +0100183 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
184 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
185 return( PSA_ERROR_NOT_SUPPORTED );
186 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
187 return( PSA_ERROR_HARDWARE_FAILURE );
188
189 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
190 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
191 return( PSA_ERROR_NOT_SUPPORTED );
192 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
193 return( PSA_ERROR_HARDWARE_FAILURE );
194
195 case MBEDTLS_ERR_CCM_BAD_INPUT:
196 return( PSA_ERROR_INVALID_ARGUMENT );
197 case MBEDTLS_ERR_CCM_AUTH_FAILED:
198 return( PSA_ERROR_INVALID_SIGNATURE );
199 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
200 return( PSA_ERROR_HARDWARE_FAILURE );
201
202 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
203 return( PSA_ERROR_NOT_SUPPORTED );
204 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
205 return( PSA_ERROR_INVALID_ARGUMENT );
206 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
207 return( PSA_ERROR_INSUFFICIENT_MEMORY );
208 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
209 return( PSA_ERROR_INVALID_PADDING );
210 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
211 return( PSA_ERROR_BAD_STATE );
212 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
213 return( PSA_ERROR_INVALID_SIGNATURE );
214 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
215 return( PSA_ERROR_TAMPERING_DETECTED );
216 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
217 return( PSA_ERROR_HARDWARE_FAILURE );
218
219 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
220 return( PSA_ERROR_HARDWARE_FAILURE );
221
222 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
223 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
224 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
225 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
226 return( PSA_ERROR_NOT_SUPPORTED );
227 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
228 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
229
230 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
231 return( PSA_ERROR_NOT_SUPPORTED );
232 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
233 return( PSA_ERROR_HARDWARE_FAILURE );
234
Gilles Peskinee59236f2018-01-27 23:32:46 +0100235 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
236 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
237 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
238 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100239
240 case MBEDTLS_ERR_GCM_AUTH_FAILED:
241 return( PSA_ERROR_INVALID_SIGNATURE );
242 case MBEDTLS_ERR_GCM_BAD_INPUT:
243 return( PSA_ERROR_NOT_SUPPORTED );
244 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
245 return( PSA_ERROR_HARDWARE_FAILURE );
246
247 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
248 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
249 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
250 return( PSA_ERROR_HARDWARE_FAILURE );
251
252 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
253 return( PSA_ERROR_NOT_SUPPORTED );
254 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
255 return( PSA_ERROR_INVALID_ARGUMENT );
256 case MBEDTLS_ERR_MD_ALLOC_FAILED:
257 return( PSA_ERROR_INSUFFICIENT_MEMORY );
258 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
259 return( PSA_ERROR_STORAGE_FAILURE );
260 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
261 return( PSA_ERROR_HARDWARE_FAILURE );
262
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100263 case MBEDTLS_ERR_PK_ALLOC_FAILED:
264 return( PSA_ERROR_INSUFFICIENT_MEMORY );
265 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
266 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100269 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100270 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
271 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
272 return( PSA_ERROR_INVALID_ARGUMENT );
273 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
274 return( PSA_ERROR_NOT_SUPPORTED );
275 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
276 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
277 return( PSA_ERROR_NOT_PERMITTED );
278 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
279 return( PSA_ERROR_INVALID_ARGUMENT );
280 case MBEDTLS_ERR_PK_INVALID_ALG:
281 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
282 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
283 return( PSA_ERROR_NOT_SUPPORTED );
284 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
285 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100286 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
287 return( PSA_ERROR_HARDWARE_FAILURE );
288
289 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
290 return( PSA_ERROR_HARDWARE_FAILURE );
291
292 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
293 return( PSA_ERROR_INVALID_ARGUMENT );
294 case MBEDTLS_ERR_RSA_INVALID_PADDING:
295 return( PSA_ERROR_INVALID_PADDING );
296 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
297 return( PSA_ERROR_HARDWARE_FAILURE );
298 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
299 return( PSA_ERROR_INVALID_ARGUMENT );
300 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
301 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
302 return( PSA_ERROR_TAMPERING_DETECTED );
303 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
304 return( PSA_ERROR_INVALID_SIGNATURE );
305 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
306 return( PSA_ERROR_BUFFER_TOO_SMALL );
307 case MBEDTLS_ERR_RSA_RNG_FAILED:
308 return( PSA_ERROR_INSUFFICIENT_MEMORY );
309 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
310 return( PSA_ERROR_NOT_SUPPORTED );
311 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
312 return( PSA_ERROR_HARDWARE_FAILURE );
313
314 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
315 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
316 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
317 return( PSA_ERROR_HARDWARE_FAILURE );
318
319 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
320 return( PSA_ERROR_INVALID_ARGUMENT );
321 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
322 return( PSA_ERROR_HARDWARE_FAILURE );
323
itayzafrir5c753392018-05-08 11:18:38 +0300324 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300325 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300326 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300327 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
328 return( PSA_ERROR_BUFFER_TOO_SMALL );
329 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
330 return( PSA_ERROR_NOT_SUPPORTED );
331 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
332 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
333 return( PSA_ERROR_INVALID_SIGNATURE );
334 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
335 return( PSA_ERROR_INSUFFICIENT_MEMORY );
336 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
337 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300338
Gilles Peskinee59236f2018-01-27 23:32:46 +0100339 default:
340 return( PSA_ERROR_UNKNOWN_ERROR );
341 }
342}
343
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200344/* Retrieve a key slot, occupied or not. */
345static psa_status_t psa_get_key_slot( psa_key_slot_t key,
346 key_slot_t **p_slot )
347{
Gilles Peskine996deb12018-08-01 15:45:45 +0200348 /* 0 is not a valid slot number under any circumstance. This
349 * implementation provides slots number 1 to N where N is the
350 * number of available slots. */
351 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200352 return( PSA_ERROR_INVALID_ARGUMENT );
353
Gilles Peskine996deb12018-08-01 15:45:45 +0200354 *p_slot = &global_data.key_slots[key - 1];
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200355 return( PSA_SUCCESS );
356}
357
358/* Retrieve an empty key slot (slot with no key data, but possibly
359 * with some metadata such as a policy). */
360static psa_status_t psa_get_empty_key_slot( psa_key_slot_t key,
361 key_slot_t **p_slot )
362{
363 psa_status_t status;
364 key_slot_t *slot = NULL;
365
366 *p_slot = NULL;
367
368 status = psa_get_key_slot( key, &slot );
369 if( status != PSA_SUCCESS )
370 return( status );
371
372 if( slot->type != PSA_KEY_TYPE_NONE )
373 return( PSA_ERROR_OCCUPIED_SLOT );
374
375 *p_slot = slot;
376 return( status );
377}
378
Jaeden Amerob4fa8c92018-07-11 15:57:44 +0100379/** Retrieve a slot which must contain a key. The key must have allow all the
380 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
381 * operations with this algorithm. */
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200382static psa_status_t psa_get_key_from_slot( psa_key_slot_t key,
383 key_slot_t **p_slot,
384 psa_key_usage_t usage,
385 psa_algorithm_t alg )
386{
387 psa_status_t status;
388 key_slot_t *slot = NULL;
389
390 *p_slot = NULL;
391
392 status = psa_get_key_slot( key, &slot );
393 if( status != PSA_SUCCESS )
394 return( status );
395 if( slot->type == PSA_KEY_TYPE_NONE )
396 return( PSA_ERROR_EMPTY_SLOT );
397
398 /* Enforce that usage policy for the key slot contains all the flags
399 * required by the usage parameter. There is one exception: public
400 * keys can always be exported, so we treat public key objects as
401 * if they had the export flag. */
402 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
403 usage &= ~PSA_KEY_USAGE_EXPORT;
404 if( ( slot->policy.usage & usage ) != usage )
405 return( PSA_ERROR_NOT_PERMITTED );
406 if( alg != 0 && ( alg != slot->policy.alg ) )
407 return( PSA_ERROR_NOT_PERMITTED );
408
409 *p_slot = slot;
410 return( PSA_SUCCESS );
411}
412
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200413
414
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100415/****************************************************************/
416/* Key management */
417/****************************************************************/
418
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100419#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200420static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
421{
422 switch( grpid )
423 {
424 case MBEDTLS_ECP_DP_SECP192R1:
425 return( PSA_ECC_CURVE_SECP192R1 );
426 case MBEDTLS_ECP_DP_SECP224R1:
427 return( PSA_ECC_CURVE_SECP224R1 );
428 case MBEDTLS_ECP_DP_SECP256R1:
429 return( PSA_ECC_CURVE_SECP256R1 );
430 case MBEDTLS_ECP_DP_SECP384R1:
431 return( PSA_ECC_CURVE_SECP384R1 );
432 case MBEDTLS_ECP_DP_SECP521R1:
433 return( PSA_ECC_CURVE_SECP521R1 );
434 case MBEDTLS_ECP_DP_BP256R1:
435 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
436 case MBEDTLS_ECP_DP_BP384R1:
437 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
438 case MBEDTLS_ECP_DP_BP512R1:
439 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
440 case MBEDTLS_ECP_DP_CURVE25519:
441 return( PSA_ECC_CURVE_CURVE25519 );
442 case MBEDTLS_ECP_DP_SECP192K1:
443 return( PSA_ECC_CURVE_SECP192K1 );
444 case MBEDTLS_ECP_DP_SECP224K1:
445 return( PSA_ECC_CURVE_SECP224K1 );
446 case MBEDTLS_ECP_DP_SECP256K1:
447 return( PSA_ECC_CURVE_SECP256K1 );
448 case MBEDTLS_ECP_DP_CURVE448:
449 return( PSA_ECC_CURVE_CURVE448 );
450 default:
451 return( 0 );
452 }
453}
454
Gilles Peskine12313cd2018-06-20 00:20:32 +0200455static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
456{
457 switch( curve )
458 {
459 case PSA_ECC_CURVE_SECP192R1:
460 return( MBEDTLS_ECP_DP_SECP192R1 );
461 case PSA_ECC_CURVE_SECP224R1:
462 return( MBEDTLS_ECP_DP_SECP224R1 );
463 case PSA_ECC_CURVE_SECP256R1:
464 return( MBEDTLS_ECP_DP_SECP256R1 );
465 case PSA_ECC_CURVE_SECP384R1:
466 return( MBEDTLS_ECP_DP_SECP384R1 );
467 case PSA_ECC_CURVE_SECP521R1:
468 return( MBEDTLS_ECP_DP_SECP521R1 );
469 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
470 return( MBEDTLS_ECP_DP_BP256R1 );
471 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
472 return( MBEDTLS_ECP_DP_BP384R1 );
473 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
474 return( MBEDTLS_ECP_DP_BP512R1 );
475 case PSA_ECC_CURVE_CURVE25519:
476 return( MBEDTLS_ECP_DP_CURVE25519 );
477 case PSA_ECC_CURVE_SECP192K1:
478 return( MBEDTLS_ECP_DP_SECP192K1 );
479 case PSA_ECC_CURVE_SECP224K1:
480 return( MBEDTLS_ECP_DP_SECP224K1 );
481 case PSA_ECC_CURVE_SECP256K1:
482 return( MBEDTLS_ECP_DP_SECP256K1 );
483 case PSA_ECC_CURVE_CURVE448:
484 return( MBEDTLS_ECP_DP_CURVE448 );
485 default:
486 return( MBEDTLS_ECP_DP_NONE );
487 }
488}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100489#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200490
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200491static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
492 size_t bits,
493 struct raw_data *raw )
494{
495 /* Check that the bit size is acceptable for the key type */
496 switch( type )
497 {
498 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200499 if( bits == 0 )
500 {
501 raw->bytes = 0;
502 raw->data = NULL;
503 return( PSA_SUCCESS );
504 }
505 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200506#if defined(MBEDTLS_MD_C)
507 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200508#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200509 case PSA_KEY_TYPE_DERIVE:
510 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200511#if defined(MBEDTLS_AES_C)
512 case PSA_KEY_TYPE_AES:
513 if( bits != 128 && bits != 192 && bits != 256 )
514 return( PSA_ERROR_INVALID_ARGUMENT );
515 break;
516#endif
517#if defined(MBEDTLS_CAMELLIA_C)
518 case PSA_KEY_TYPE_CAMELLIA:
519 if( bits != 128 && bits != 192 && bits != 256 )
520 return( PSA_ERROR_INVALID_ARGUMENT );
521 break;
522#endif
523#if defined(MBEDTLS_DES_C)
524 case PSA_KEY_TYPE_DES:
525 if( bits != 64 && bits != 128 && bits != 192 )
526 return( PSA_ERROR_INVALID_ARGUMENT );
527 break;
528#endif
529#if defined(MBEDTLS_ARC4_C)
530 case PSA_KEY_TYPE_ARC4:
531 if( bits < 8 || bits > 2048 )
532 return( PSA_ERROR_INVALID_ARGUMENT );
533 break;
534#endif
535 default:
536 return( PSA_ERROR_NOT_SUPPORTED );
537 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200538 if( bits % 8 != 0 )
539 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200540
541 /* Allocate memory for the key */
542 raw->bytes = PSA_BITS_TO_BYTES( bits );
543 raw->data = mbedtls_calloc( 1, raw->bytes );
544 if( raw->data == NULL )
545 {
546 raw->bytes = 0;
547 return( PSA_ERROR_INSUFFICIENT_MEMORY );
548 }
549 return( PSA_SUCCESS );
550}
551
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200552#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
553static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
554 mbedtls_rsa_context **p_rsa )
555{
556 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
557 return( PSA_ERROR_INVALID_ARGUMENT );
558 else
559 {
560 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
561 size_t bits = mbedtls_rsa_get_bitlen( rsa );
562 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
563 return( PSA_ERROR_NOT_SUPPORTED );
564 *p_rsa = rsa;
565 return( PSA_SUCCESS );
566 }
567}
568#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
569
570#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
571static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
572 mbedtls_pk_context *pk,
573 mbedtls_ecp_keypair **p_ecp )
574{
575 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
576 return( PSA_ERROR_INVALID_ARGUMENT );
577 else
578 {
579 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
580 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
581 if( actual_curve != expected_curve )
582 return( PSA_ERROR_INVALID_ARGUMENT );
583 *p_ecp = ecp;
584 return( PSA_SUCCESS );
585 }
586}
587#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
588
Gilles Peskine2d277862018-06-18 15:41:12 +0200589psa_status_t psa_import_key( psa_key_slot_t key,
590 psa_key_type_t type,
591 const uint8_t *data,
592 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100593{
594 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200595 psa_status_t status = PSA_SUCCESS;
596 status = psa_get_empty_key_slot( key, &slot );
597 if( status != PSA_SUCCESS )
598 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100599
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200600 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100601 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100602 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100603 if( data_length > SIZE_MAX / 8 )
604 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200605 status = prepare_raw_data_slot( type,
606 PSA_BYTES_TO_BITS( data_length ),
607 &slot->data.raw );
608 if( status != PSA_SUCCESS )
609 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200610 if( data_length != 0 )
611 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100612 }
613 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100614#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200615 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100616 {
617 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100618 mbedtls_pk_context pk;
619 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200620
621 /* Parse the data. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100622 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
623 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
624 else
625 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100626 if( ret != 0 )
627 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200628
629 /* We have something that the pkparse module recognizes.
630 * If it has the expected type and passes any type-specific
631 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100632#if defined(MBEDTLS_RSA_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200633 if( PSA_KEY_TYPE_IS_RSA( type ) )
634 status = psa_import_rsa_key( &pk, &slot->data.rsa );
635 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100636#endif /* MBEDTLS_RSA_C */
637#if defined(MBEDTLS_ECP_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200638 if( PSA_KEY_TYPE_IS_ECC( type ) )
639 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( type ),
640 &pk, &slot->data.ecp );
641 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100642#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200643 {
644 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200645 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200646
Gilles Peskinec648d692018-06-28 08:46:13 +0200647 /* Free the content of the pk object only on error. On success,
648 * the content of the object has been stored in the slot. */
649 if( status != PSA_SUCCESS )
650 {
651 mbedtls_pk_free( &pk );
652 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100653 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100654 }
655 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100656#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100657 {
658 return( PSA_ERROR_NOT_SUPPORTED );
659 }
660
661 slot->type = type;
662 return( PSA_SUCCESS );
663}
664
Gilles Peskine2d277862018-06-18 15:41:12 +0200665psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100666{
667 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200668 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100669
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200670 status = psa_get_key_slot( key, &slot );
671 if( status != PSA_SUCCESS )
672 return( status );
673
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100674 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200675 {
676 /* No key material to clean, but do zeroize the slot below to wipe
677 * metadata such as policies. */
678 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200679 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100680 {
681 mbedtls_free( slot->data.raw.data );
682 }
683 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100684#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200685 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100686 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100687 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100688 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100689 }
690 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100691#endif /* defined(MBEDTLS_RSA_C) */
692#if defined(MBEDTLS_ECP_C)
693 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
694 {
695 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100696 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100697 }
698 else
699#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100700 {
701 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100702 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100703 return( PSA_ERROR_TAMPERING_DETECTED );
704 }
705
706 mbedtls_zeroize( slot, sizeof( *slot ) );
707 return( PSA_SUCCESS );
708}
709
Gilles Peskineb870b182018-07-06 16:02:09 +0200710/* Return the size of the key in the given slot, in bits. */
711static size_t psa_get_key_bits( const key_slot_t *slot )
712{
713 if( key_type_is_raw_bytes( slot->type ) )
714 return( slot->data.raw.bytes * 8 );
715#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200716 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineb870b182018-07-06 16:02:09 +0200717 return( mbedtls_rsa_get_bitlen( slot->data.rsa ) );
718#endif /* defined(MBEDTLS_RSA_C) */
719#if defined(MBEDTLS_ECP_C)
720 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
721 return( slot->data.ecp->grp.pbits );
722#endif /* defined(MBEDTLS_ECP_C) */
723 /* Shouldn't happen except on an empty slot. */
724 return( 0 );
725}
726
Gilles Peskine2d277862018-06-18 15:41:12 +0200727psa_status_t psa_get_key_information( psa_key_slot_t key,
728 psa_key_type_t *type,
729 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100730{
731 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200732 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100733
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100734 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200735 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100736 if( bits != NULL )
737 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200738 status = psa_get_key_slot( key, &slot );
739 if( status != PSA_SUCCESS )
740 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200741
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100742 if( slot->type == PSA_KEY_TYPE_NONE )
743 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200744 if( type != NULL )
745 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200746 if( bits != NULL )
747 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100748 return( PSA_SUCCESS );
749}
750
Gilles Peskine2d277862018-06-18 15:41:12 +0200751static psa_status_t psa_internal_export_key( psa_key_slot_t key,
752 uint8_t *data,
753 size_t data_size,
754 size_t *data_length,
755 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100756{
757 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200758 psa_status_t status;
759 /* Exporting a public key doesn't require a usage flag. If we're
760 * called by psa_export_public_key(), don't require the EXPORT flag.
761 * If we're called by psa_export_key(), do require the EXPORT flag;
762 * if the key turns out to be public key object, psa_get_key_from_slot()
763 * will ignore this flag. */
764 psa_key_usage_t usage = export_public_key ? 0 : PSA_KEY_USAGE_EXPORT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100765
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100766 /* Set the key to empty now, so that even when there are errors, we always
767 * set data_length to a value between 0 and data_size. On error, setting
768 * the key to empty is a good choice because an empty key representation is
769 * unlikely to be accepted anywhere. */
770 *data_length = 0;
771
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200772 status = psa_get_key_from_slot( key, &slot, usage, 0 );
773 if( status != PSA_SUCCESS )
774 return( status );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200775 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300776 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300777
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200778 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100779 {
780 if( slot->data.raw.bytes > data_size )
781 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200782 if( slot->data.raw.bytes != 0 )
783 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100784 *data_length = slot->data.raw.bytes;
785 return( PSA_SUCCESS );
786 }
787 else
Moran Peker17e36e12018-05-02 12:55:20 +0300788 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100789#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200790 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300791 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100792 {
Moran Pekera998bc62018-04-16 18:16:20 +0300793 mbedtls_pk_context pk;
794 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +0200795 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300796 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100797#if defined(MBEDTLS_RSA_C)
798 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300799 pk.pk_info = &mbedtls_rsa_info;
800 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100801#else
802 return( PSA_ERROR_NOT_SUPPORTED );
803#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300804 }
805 else
806 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100807#if defined(MBEDTLS_ECP_C)
808 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300809 pk.pk_info = &mbedtls_eckey_info;
810 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100811#else
812 return( PSA_ERROR_NOT_SUPPORTED );
813#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300814 }
Moran Pekerd7326592018-05-29 16:56:39 +0300815 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300816 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300817 else
818 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300819 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200820 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200821 /* If data_size is 0 then data may be NULL and then the
822 * call to memset would have undefined behavior. */
823 if( data_size != 0 )
824 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300825 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200826 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200827 /* The mbedtls_pk_xxx functions write to the end of the buffer.
828 * Move the data to the beginning and erase remaining data
829 * at the original location. */
830 if( 2 * (size_t) ret <= data_size )
831 {
832 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200833 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200834 }
835 else if( (size_t) ret < data_size )
836 {
837 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200838 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200839 }
Moran Pekera998bc62018-04-16 18:16:20 +0300840 *data_length = ret;
841 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100842 }
843 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100844#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300845 {
846 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200847 it is valid for a special-purpose implementation to omit
848 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300849 return( PSA_ERROR_NOT_SUPPORTED );
850 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100851 }
852}
853
Gilles Peskine2d277862018-06-18 15:41:12 +0200854psa_status_t psa_export_key( psa_key_slot_t key,
855 uint8_t *data,
856 size_t data_size,
857 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300858{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200859 return( psa_internal_export_key( key, data, data_size,
860 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100861}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100862
Gilles Peskine2d277862018-06-18 15:41:12 +0200863psa_status_t psa_export_public_key( psa_key_slot_t key,
864 uint8_t *data,
865 size_t data_size,
866 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300867{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200868 return( psa_internal_export_key( key, data, data_size,
869 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300870}
871
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200872
873
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100874/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100875/* Message digests */
876/****************************************************************/
877
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100878static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100879{
880 switch( alg )
881 {
882#if defined(MBEDTLS_MD2_C)
883 case PSA_ALG_MD2:
884 return( &mbedtls_md2_info );
885#endif
886#if defined(MBEDTLS_MD4_C)
887 case PSA_ALG_MD4:
888 return( &mbedtls_md4_info );
889#endif
890#if defined(MBEDTLS_MD5_C)
891 case PSA_ALG_MD5:
892 return( &mbedtls_md5_info );
893#endif
894#if defined(MBEDTLS_RIPEMD160_C)
895 case PSA_ALG_RIPEMD160:
896 return( &mbedtls_ripemd160_info );
897#endif
898#if defined(MBEDTLS_SHA1_C)
899 case PSA_ALG_SHA_1:
900 return( &mbedtls_sha1_info );
901#endif
902#if defined(MBEDTLS_SHA256_C)
903 case PSA_ALG_SHA_224:
904 return( &mbedtls_sha224_info );
905 case PSA_ALG_SHA_256:
906 return( &mbedtls_sha256_info );
907#endif
908#if defined(MBEDTLS_SHA512_C)
909 case PSA_ALG_SHA_384:
910 return( &mbedtls_sha384_info );
911 case PSA_ALG_SHA_512:
912 return( &mbedtls_sha512_info );
913#endif
914 default:
915 return( NULL );
916 }
917}
918
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100919psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
920{
921 switch( operation->alg )
922 {
Gilles Peskine81736312018-06-26 15:04:31 +0200923 case 0:
924 /* The object has (apparently) been initialized but it is not
925 * in use. It's ok to call abort on such an object, and there's
926 * nothing to do. */
927 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100928#if defined(MBEDTLS_MD2_C)
929 case PSA_ALG_MD2:
930 mbedtls_md2_free( &operation->ctx.md2 );
931 break;
932#endif
933#if defined(MBEDTLS_MD4_C)
934 case PSA_ALG_MD4:
935 mbedtls_md4_free( &operation->ctx.md4 );
936 break;
937#endif
938#if defined(MBEDTLS_MD5_C)
939 case PSA_ALG_MD5:
940 mbedtls_md5_free( &operation->ctx.md5 );
941 break;
942#endif
943#if defined(MBEDTLS_RIPEMD160_C)
944 case PSA_ALG_RIPEMD160:
945 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
946 break;
947#endif
948#if defined(MBEDTLS_SHA1_C)
949 case PSA_ALG_SHA_1:
950 mbedtls_sha1_free( &operation->ctx.sha1 );
951 break;
952#endif
953#if defined(MBEDTLS_SHA256_C)
954 case PSA_ALG_SHA_224:
955 case PSA_ALG_SHA_256:
956 mbedtls_sha256_free( &operation->ctx.sha256 );
957 break;
958#endif
959#if defined(MBEDTLS_SHA512_C)
960 case PSA_ALG_SHA_384:
961 case PSA_ALG_SHA_512:
962 mbedtls_sha512_free( &operation->ctx.sha512 );
963 break;
964#endif
965 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +0200966 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100967 }
968 operation->alg = 0;
969 return( PSA_SUCCESS );
970}
971
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200972psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100973 psa_algorithm_t alg )
974{
975 int ret;
976 operation->alg = 0;
977 switch( alg )
978 {
979#if defined(MBEDTLS_MD2_C)
980 case PSA_ALG_MD2:
981 mbedtls_md2_init( &operation->ctx.md2 );
982 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
983 break;
984#endif
985#if defined(MBEDTLS_MD4_C)
986 case PSA_ALG_MD4:
987 mbedtls_md4_init( &operation->ctx.md4 );
988 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
989 break;
990#endif
991#if defined(MBEDTLS_MD5_C)
992 case PSA_ALG_MD5:
993 mbedtls_md5_init( &operation->ctx.md5 );
994 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
995 break;
996#endif
997#if defined(MBEDTLS_RIPEMD160_C)
998 case PSA_ALG_RIPEMD160:
999 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1000 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1001 break;
1002#endif
1003#if defined(MBEDTLS_SHA1_C)
1004 case PSA_ALG_SHA_1:
1005 mbedtls_sha1_init( &operation->ctx.sha1 );
1006 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1007 break;
1008#endif
1009#if defined(MBEDTLS_SHA256_C)
1010 case PSA_ALG_SHA_224:
1011 mbedtls_sha256_init( &operation->ctx.sha256 );
1012 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1013 break;
1014 case PSA_ALG_SHA_256:
1015 mbedtls_sha256_init( &operation->ctx.sha256 );
1016 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1017 break;
1018#endif
1019#if defined(MBEDTLS_SHA512_C)
1020 case PSA_ALG_SHA_384:
1021 mbedtls_sha512_init( &operation->ctx.sha512 );
1022 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1023 break;
1024 case PSA_ALG_SHA_512:
1025 mbedtls_sha512_init( &operation->ctx.sha512 );
1026 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1027 break;
1028#endif
1029 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001030 return( PSA_ALG_IS_HASH( alg ) ?
1031 PSA_ERROR_NOT_SUPPORTED :
1032 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001033 }
1034 if( ret == 0 )
1035 operation->alg = alg;
1036 else
1037 psa_hash_abort( operation );
1038 return( mbedtls_to_psa_error( ret ) );
1039}
1040
1041psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1042 const uint8_t *input,
1043 size_t input_length )
1044{
1045 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001046
1047 /* Don't require hash implementations to behave correctly on a
1048 * zero-length input, which may have an invalid pointer. */
1049 if( input_length == 0 )
1050 return( PSA_SUCCESS );
1051
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001052 switch( operation->alg )
1053 {
1054#if defined(MBEDTLS_MD2_C)
1055 case PSA_ALG_MD2:
1056 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1057 input, input_length );
1058 break;
1059#endif
1060#if defined(MBEDTLS_MD4_C)
1061 case PSA_ALG_MD4:
1062 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1063 input, input_length );
1064 break;
1065#endif
1066#if defined(MBEDTLS_MD5_C)
1067 case PSA_ALG_MD5:
1068 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1069 input, input_length );
1070 break;
1071#endif
1072#if defined(MBEDTLS_RIPEMD160_C)
1073 case PSA_ALG_RIPEMD160:
1074 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1075 input, input_length );
1076 break;
1077#endif
1078#if defined(MBEDTLS_SHA1_C)
1079 case PSA_ALG_SHA_1:
1080 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1081 input, input_length );
1082 break;
1083#endif
1084#if defined(MBEDTLS_SHA256_C)
1085 case PSA_ALG_SHA_224:
1086 case PSA_ALG_SHA_256:
1087 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1088 input, input_length );
1089 break;
1090#endif
1091#if defined(MBEDTLS_SHA512_C)
1092 case PSA_ALG_SHA_384:
1093 case PSA_ALG_SHA_512:
1094 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1095 input, input_length );
1096 break;
1097#endif
1098 default:
1099 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1100 break;
1101 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001102
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001103 if( ret != 0 )
1104 psa_hash_abort( operation );
1105 return( mbedtls_to_psa_error( ret ) );
1106}
1107
1108psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1109 uint8_t *hash,
1110 size_t hash_size,
1111 size_t *hash_length )
1112{
1113 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001114 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001115
1116 /* Fill the output buffer with something that isn't a valid hash
1117 * (barring an attack on the hash and deliberately-crafted input),
1118 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001119 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001120 /* If hash_size is 0 then hash may be NULL and then the
1121 * call to memset would have undefined behavior. */
1122 if( hash_size != 0 )
1123 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001124
1125 if( hash_size < actual_hash_length )
1126 return( PSA_ERROR_BUFFER_TOO_SMALL );
1127
1128 switch( operation->alg )
1129 {
1130#if defined(MBEDTLS_MD2_C)
1131 case PSA_ALG_MD2:
1132 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1133 break;
1134#endif
1135#if defined(MBEDTLS_MD4_C)
1136 case PSA_ALG_MD4:
1137 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1138 break;
1139#endif
1140#if defined(MBEDTLS_MD5_C)
1141 case PSA_ALG_MD5:
1142 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1143 break;
1144#endif
1145#if defined(MBEDTLS_RIPEMD160_C)
1146 case PSA_ALG_RIPEMD160:
1147 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1148 break;
1149#endif
1150#if defined(MBEDTLS_SHA1_C)
1151 case PSA_ALG_SHA_1:
1152 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1153 break;
1154#endif
1155#if defined(MBEDTLS_SHA256_C)
1156 case PSA_ALG_SHA_224:
1157 case PSA_ALG_SHA_256:
1158 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1159 break;
1160#endif
1161#if defined(MBEDTLS_SHA512_C)
1162 case PSA_ALG_SHA_384:
1163 case PSA_ALG_SHA_512:
1164 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1165 break;
1166#endif
1167 default:
1168 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1169 break;
1170 }
1171
1172 if( ret == 0 )
1173 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001174 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001175 return( psa_hash_abort( operation ) );
1176 }
1177 else
1178 {
1179 psa_hash_abort( operation );
1180 return( mbedtls_to_psa_error( ret ) );
1181 }
1182}
1183
Gilles Peskine2d277862018-06-18 15:41:12 +02001184psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1185 const uint8_t *hash,
1186 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001187{
1188 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1189 size_t actual_hash_length;
1190 psa_status_t status = psa_hash_finish( operation,
1191 actual_hash, sizeof( actual_hash ),
1192 &actual_hash_length );
1193 if( status != PSA_SUCCESS )
1194 return( status );
1195 if( actual_hash_length != hash_length )
1196 return( PSA_ERROR_INVALID_SIGNATURE );
1197 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1198 return( PSA_ERROR_INVALID_SIGNATURE );
1199 return( PSA_SUCCESS );
1200}
1201
1202
1203
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001204/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001205/* MAC */
1206/****************************************************************/
1207
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001208static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001209 psa_algorithm_t alg,
1210 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001211 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001212 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001213{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001214 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001215 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001216
1217 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1218 {
1219 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001220 {
1221 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1222 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001223
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001224 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001225 {
Gilles Peskine5d1888e2018-07-12 00:32:42 +02001226 case PSA_ALG_STREAM_CIPHER_BASE:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001227 mode = MBEDTLS_MODE_STREAM;
1228 break;
1229 case PSA_ALG_CBC_BASE:
1230 mode = MBEDTLS_MODE_CBC;
1231 break;
1232 case PSA_ALG_CFB_BASE:
1233 mode = MBEDTLS_MODE_CFB;
1234 break;
1235 case PSA_ALG_OFB_BASE:
1236 mode = MBEDTLS_MODE_OFB;
1237 break;
1238 case PSA_ALG_CTR:
1239 mode = MBEDTLS_MODE_CTR;
1240 break;
1241 case PSA_ALG_CCM:
1242 mode = MBEDTLS_MODE_CCM;
1243 break;
1244 case PSA_ALG_GCM:
1245 mode = MBEDTLS_MODE_GCM;
1246 break;
1247 default:
1248 return( NULL );
1249 }
1250 }
1251 else if( alg == PSA_ALG_CMAC )
1252 mode = MBEDTLS_MODE_ECB;
1253 else if( alg == PSA_ALG_GMAC )
1254 mode = MBEDTLS_MODE_GCM;
1255 else
1256 return( NULL );
1257
1258 switch( key_type )
1259 {
1260 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001261 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001262 break;
1263 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001264 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1265 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001266 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001267 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001268 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001269 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001270 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1271 * but two-key Triple-DES is functionally three-key Triple-DES
1272 * with K1=K3, so that's how we present it to mbedtls. */
1273 if( key_bits == 128 )
1274 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001275 break;
1276 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001277 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001278 break;
1279 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001280 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001281 break;
1282 default:
1283 return( NULL );
1284 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001285 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001286 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001287
Jaeden Amero23bbb752018-06-26 14:16:54 +01001288 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1289 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001290}
1291
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001292static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001293{
Gilles Peskine2d277862018-06-18 15:41:12 +02001294 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001295 {
1296 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001297 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001298 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001299 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001300 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001301 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001302 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001303 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001304 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001305 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001306 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001307 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001308 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001309 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001310 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001311 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001312 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001313 return( 128 );
1314 default:
1315 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001316 }
1317}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001318
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001319/* Initialize the MAC operation structure. Once this function has been
1320 * called, psa_mac_abort can run and will do the right thing. */
1321static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1322 psa_algorithm_t alg )
1323{
1324 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1325
1326 operation->alg = alg;
1327 operation->key_set = 0;
1328 operation->iv_set = 0;
1329 operation->iv_required = 0;
1330 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001331 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001332
1333#if defined(MBEDTLS_CMAC_C)
1334 if( alg == PSA_ALG_CMAC )
1335 {
1336 operation->iv_required = 0;
1337 mbedtls_cipher_init( &operation->ctx.cmac );
1338 status = PSA_SUCCESS;
1339 }
1340 else
1341#endif /* MBEDTLS_CMAC_C */
1342#if defined(MBEDTLS_MD_C)
1343 if( PSA_ALG_IS_HMAC( operation->alg ) )
1344 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001345 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1346 operation->ctx.hmac.hash_ctx.alg = 0;
1347 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001348 }
1349 else
1350#endif /* MBEDTLS_MD_C */
1351 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001352 if( ! PSA_ALG_IS_MAC( alg ) )
1353 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001354 }
1355
1356 if( status != PSA_SUCCESS )
1357 memset( operation, 0, sizeof( *operation ) );
1358 return( status );
1359}
1360
Gilles Peskine01126fa2018-07-12 17:04:55 +02001361#if defined(MBEDTLS_MD_C)
1362static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1363{
1364 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1365 return( psa_hash_abort( &hmac->hash_ctx ) );
1366}
1367#endif /* MBEDTLS_MD_C */
1368
Gilles Peskine8c9def32018-02-08 10:02:12 +01001369psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1370{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001371 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001372 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001373 /* The object has (apparently) been initialized but it is not
1374 * in use. It's ok to call abort on such an object, and there's
1375 * nothing to do. */
1376 return( PSA_SUCCESS );
1377 }
1378 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001379#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001380 if( operation->alg == PSA_ALG_CMAC )
1381 {
1382 mbedtls_cipher_free( &operation->ctx.cmac );
1383 }
1384 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001385#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001386#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001387 if( PSA_ALG_IS_HMAC( operation->alg ) )
1388 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001389 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001390 }
1391 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001392#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001393 {
1394 /* Sanity check (shouldn't happen: operation->alg should
1395 * always have been initialized to a valid value). */
1396 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001397 }
Moran Peker41deec42018-04-04 15:43:05 +03001398
Gilles Peskine8c9def32018-02-08 10:02:12 +01001399 operation->alg = 0;
1400 operation->key_set = 0;
1401 operation->iv_set = 0;
1402 operation->iv_required = 0;
1403 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001404 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001405
Gilles Peskine8c9def32018-02-08 10:02:12 +01001406 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001407
1408bad_state:
1409 /* If abort is called on an uninitialized object, we can't trust
1410 * anything. Wipe the object in case it contains confidential data.
1411 * This may result in a memory leak if a pointer gets overwritten,
1412 * but it's too late to do anything about this. */
1413 memset( operation, 0, sizeof( *operation ) );
1414 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001415}
1416
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001417#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001418static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001419 size_t key_bits,
1420 key_slot_t *slot,
1421 const mbedtls_cipher_info_t *cipher_info )
1422{
1423 int ret;
1424
1425 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001426
1427 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1428 if( ret != 0 )
1429 return( ret );
1430
1431 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1432 slot->data.raw.data,
1433 key_bits );
1434 return( ret );
1435}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001436#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001437
Gilles Peskine248051a2018-06-20 16:09:38 +02001438#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001439static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1440 const uint8_t *key,
1441 size_t key_length,
1442 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001443{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001444 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001445 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001446 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001447 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001448 psa_status_t status;
1449
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001450 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1451 * overflow below. This should never trigger if the hash algorithm
1452 * is implemented correctly. */
1453 /* The size checks against the ipad and opad buffers cannot be written
1454 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1455 * because that triggers -Wlogical-op on GCC 7.3. */
1456 if( block_size > sizeof( ipad ) )
1457 return( PSA_ERROR_NOT_SUPPORTED );
1458 if( block_size > sizeof( hmac->opad ) )
1459 return( PSA_ERROR_NOT_SUPPORTED );
1460 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001461 return( PSA_ERROR_NOT_SUPPORTED );
1462
Gilles Peskined223b522018-06-11 18:12:58 +02001463 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001464 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001465 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001466 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001467 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001468 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001469 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001470 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001471 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001472 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001473 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001474 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001475 }
Gilles Peskine96889972018-07-12 17:07:03 +02001476 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1477 * but it is permitted. It is common when HMAC is used in HKDF, for
1478 * example. Don't call `memcpy` in the 0-length because `key` could be
1479 * an invalid pointer which would make the behavior undefined. */
1480 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001481 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001482
Gilles Peskined223b522018-06-11 18:12:58 +02001483 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1484 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001485 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001486 ipad[i] ^= 0x36;
1487 memset( ipad + key_length, 0x36, block_size - key_length );
1488
1489 /* Copy the key material from ipad to opad, flipping the requisite bits,
1490 * and filling the rest of opad with the requisite constant. */
1491 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001492 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1493 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001494
Gilles Peskine01126fa2018-07-12 17:04:55 +02001495 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001496 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001497 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001498
Gilles Peskine01126fa2018-07-12 17:04:55 +02001499 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001500
1501cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001502 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001503
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001504 return( status );
1505}
Gilles Peskine248051a2018-06-20 16:09:38 +02001506#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001507
Gilles Peskine89167cb2018-07-08 20:12:23 +02001508static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1509 psa_key_slot_t key,
1510 psa_algorithm_t alg,
1511 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001512{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001513 psa_status_t status;
1514 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001515 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001516 psa_key_usage_t usage =
1517 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001518
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001519 status = psa_mac_init( operation, alg );
1520 if( status != PSA_SUCCESS )
1521 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001522 if( is_sign )
1523 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001524
Gilles Peskine89167cb2018-07-08 20:12:23 +02001525 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001526 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001527 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001528 key_bits = psa_get_key_bits( slot );
1529
Gilles Peskine8c9def32018-02-08 10:02:12 +01001530#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001531 if( alg == PSA_ALG_CMAC )
1532 {
1533 const mbedtls_cipher_info_t *cipher_info =
1534 mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
1535 int ret;
1536 if( cipher_info == NULL )
1537 {
1538 status = PSA_ERROR_NOT_SUPPORTED;
1539 goto exit;
1540 }
1541 operation->mac_size = cipher_info->block_size;
1542 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1543 status = mbedtls_to_psa_error( ret );
1544 }
1545 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001546#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001547#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001548 if( PSA_ALG_IS_HMAC( alg ) )
1549 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001550 psa_algorithm_t hash_alg = PSA_ALG_HMAC_HASH( alg );
1551 if( hash_alg == 0 )
1552 {
1553 status = PSA_ERROR_NOT_SUPPORTED;
1554 goto exit;
1555 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001556
1557 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1558 /* Sanity check. This shouldn't fail on a valid configuration. */
1559 if( operation->mac_size == 0 ||
1560 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1561 {
1562 status = PSA_ERROR_NOT_SUPPORTED;
1563 goto exit;
1564 }
1565
Gilles Peskine01126fa2018-07-12 17:04:55 +02001566 if( slot->type != PSA_KEY_TYPE_HMAC )
1567 {
1568 status = PSA_ERROR_INVALID_ARGUMENT;
1569 goto exit;
1570 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001571
Gilles Peskine01126fa2018-07-12 17:04:55 +02001572 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1573 slot->data.raw.data,
1574 slot->data.raw.bytes,
1575 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001576 }
1577 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001578#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001579 {
1580 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001581 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001582
Gilles Peskinefbfac682018-07-08 20:51:54 +02001583exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001584 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001585 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001586 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001587 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001588 else
1589 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001590 operation->key_set = 1;
1591 }
1592 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001593}
1594
Gilles Peskine89167cb2018-07-08 20:12:23 +02001595psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1596 psa_key_slot_t key,
1597 psa_algorithm_t alg )
1598{
1599 return( psa_mac_setup( operation, key, alg, 1 ) );
1600}
1601
1602psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1603 psa_key_slot_t key,
1604 psa_algorithm_t alg )
1605{
1606 return( psa_mac_setup( operation, key, alg, 0 ) );
1607}
1608
Gilles Peskine8c9def32018-02-08 10:02:12 +01001609psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1610 const uint8_t *input,
1611 size_t input_length )
1612{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001613 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001614 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001615 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001616 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001617 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001618 operation->has_input = 1;
1619
Gilles Peskine8c9def32018-02-08 10:02:12 +01001620#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001621 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001622 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001623 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1624 input, input_length );
1625 status = mbedtls_to_psa_error( ret );
1626 }
1627 else
1628#endif /* MBEDTLS_CMAC_C */
1629#if defined(MBEDTLS_MD_C)
1630 if( PSA_ALG_IS_HMAC( operation->alg ) )
1631 {
1632 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1633 input_length );
1634 }
1635 else
1636#endif /* MBEDTLS_MD_C */
1637 {
1638 /* This shouldn't happen if `operation` was initialized by
1639 * a setup function. */
1640 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001641 }
1642
Gilles Peskinefbfac682018-07-08 20:51:54 +02001643cleanup:
1644 if( status != PSA_SUCCESS )
1645 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001646 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001647}
1648
Gilles Peskine01126fa2018-07-12 17:04:55 +02001649#if defined(MBEDTLS_MD_C)
1650static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1651 uint8_t *mac,
1652 size_t mac_size )
1653{
1654 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1655 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1656 size_t hash_size = 0;
1657 size_t block_size = psa_get_hash_block_size( hash_alg );
1658 psa_status_t status;
1659
Gilles Peskine01126fa2018-07-12 17:04:55 +02001660 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1661 if( status != PSA_SUCCESS )
1662 return( status );
1663 /* From here on, tmp needs to be wiped. */
1664
1665 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1666 if( status != PSA_SUCCESS )
1667 goto exit;
1668
1669 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1670 if( status != PSA_SUCCESS )
1671 goto exit;
1672
1673 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1674 if( status != PSA_SUCCESS )
1675 goto exit;
1676
1677 status = psa_hash_finish( &hmac->hash_ctx, mac, mac_size, &hash_size );
1678
1679exit:
1680 mbedtls_zeroize( tmp, hash_size );
1681 return( status );
1682}
1683#endif /* MBEDTLS_MD_C */
1684
mohammad16036df908f2018-04-02 08:34:15 -07001685static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001686 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001687 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001688{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001689 if( ! operation->key_set )
1690 return( PSA_ERROR_BAD_STATE );
1691 if( operation->iv_required && ! operation->iv_set )
1692 return( PSA_ERROR_BAD_STATE );
1693
Gilles Peskine8c9def32018-02-08 10:02:12 +01001694 if( mac_size < operation->mac_size )
1695 return( PSA_ERROR_BUFFER_TOO_SMALL );
1696
Gilles Peskine8c9def32018-02-08 10:02:12 +01001697#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001698 if( operation->alg == PSA_ALG_CMAC )
1699 {
1700 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1701 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001702 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001703 else
1704#endif /* MBEDTLS_CMAC_C */
1705#if defined(MBEDTLS_MD_C)
1706 if( PSA_ALG_IS_HMAC( operation->alg ) )
1707 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001708 return( psa_hmac_finish_internal( &operation->ctx.hmac,
1709 mac, mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001710 }
1711 else
1712#endif /* MBEDTLS_MD_C */
1713 {
1714 /* This shouldn't happen if `operation` was initialized by
1715 * a setup function. */
1716 return( PSA_ERROR_BAD_STATE );
1717 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001718}
1719
Gilles Peskineacd4be32018-07-08 19:56:25 +02001720psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1721 uint8_t *mac,
1722 size_t mac_size,
1723 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001724{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001725 psa_status_t status;
1726
1727 /* Fill the output buffer with something that isn't a valid mac
1728 * (barring an attack on the mac and deliberately-crafted input),
1729 * in case the caller doesn't check the return status properly. */
1730 *mac_length = mac_size;
1731 /* If mac_size is 0 then mac may be NULL and then the
1732 * call to memset would have undefined behavior. */
1733 if( mac_size != 0 )
1734 memset( mac, '!', mac_size );
1735
Gilles Peskine89167cb2018-07-08 20:12:23 +02001736 if( ! operation->is_sign )
1737 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001738 status = PSA_ERROR_BAD_STATE;
1739 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001740 }
mohammad16036df908f2018-04-02 08:34:15 -07001741
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001742 status = psa_mac_finish_internal( operation, mac, mac_size );
1743
1744cleanup:
1745 if( status == PSA_SUCCESS )
1746 {
1747 status = psa_mac_abort( operation );
1748 if( status == PSA_SUCCESS )
1749 *mac_length = operation->mac_size;
1750 else
1751 memset( mac, '!', mac_size );
1752 }
1753 else
1754 psa_mac_abort( operation );
1755 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001756}
1757
Gilles Peskineacd4be32018-07-08 19:56:25 +02001758psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1759 const uint8_t *mac,
1760 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001761{
Gilles Peskine828ed142018-06-18 23:25:51 +02001762 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001763 psa_status_t status;
1764
Gilles Peskine89167cb2018-07-08 20:12:23 +02001765 if( operation->is_sign )
1766 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001767 status = PSA_ERROR_BAD_STATE;
1768 goto cleanup;
1769 }
1770 if( operation->mac_size != mac_length )
1771 {
1772 status = PSA_ERROR_INVALID_SIGNATURE;
1773 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001774 }
mohammad16036df908f2018-04-02 08:34:15 -07001775
1776 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001777 actual_mac, sizeof( actual_mac ) );
1778
1779 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1780 status = PSA_ERROR_INVALID_SIGNATURE;
1781
1782cleanup:
1783 if( status == PSA_SUCCESS )
1784 status = psa_mac_abort( operation );
1785 else
1786 psa_mac_abort( operation );
1787
1788 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001789}
1790
1791
Gilles Peskine20035e32018-02-03 22:44:14 +01001792
Gilles Peskine20035e32018-02-03 22:44:14 +01001793/****************************************************************/
1794/* Asymmetric cryptography */
1795/****************************************************************/
1796
Gilles Peskine2b450e32018-06-27 15:42:46 +02001797#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001798/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001799 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001800static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1801 size_t hash_length,
1802 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001803{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001804 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001805 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001806 *md_alg = mbedtls_md_get_type( md_info );
1807
1808 /* The Mbed TLS RSA module uses an unsigned int for hash length
1809 * parameters. Validate that it fits so that we don't risk an
1810 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001811#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001812 if( hash_length > UINT_MAX )
1813 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001814#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001815
1816#if defined(MBEDTLS_PKCS1_V15)
1817 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
1818 * must be correct. */
1819 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
1820 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001821 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001822 if( md_info == NULL )
1823 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001824 if( mbedtls_md_get_size( md_info ) != hash_length )
1825 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001826 }
1827#endif /* MBEDTLS_PKCS1_V15 */
1828
1829#if defined(MBEDTLS_PKCS1_V21)
1830 /* PSS requires a hash internally. */
1831 if( PSA_ALG_IS_RSA_PSS( alg ) )
1832 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001833 if( md_info == NULL )
1834 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001835 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001836#endif /* MBEDTLS_PKCS1_V21 */
1837
Gilles Peskine61b91d42018-06-08 16:09:36 +02001838 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001839}
1840
Gilles Peskine2b450e32018-06-27 15:42:46 +02001841static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1842 psa_algorithm_t alg,
1843 const uint8_t *hash,
1844 size_t hash_length,
1845 uint8_t *signature,
1846 size_t signature_size,
1847 size_t *signature_length )
1848{
1849 psa_status_t status;
1850 int ret;
1851 mbedtls_md_type_t md_alg;
1852
1853 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1854 if( status != PSA_SUCCESS )
1855 return( status );
1856
Gilles Peskine630a18a2018-06-29 17:49:35 +02001857 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001858 return( PSA_ERROR_BUFFER_TOO_SMALL );
1859
1860#if defined(MBEDTLS_PKCS1_V15)
1861 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1862 {
1863 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1864 MBEDTLS_MD_NONE );
1865 ret = mbedtls_rsa_pkcs1_sign( rsa,
1866 mbedtls_ctr_drbg_random,
1867 &global_data.ctr_drbg,
1868 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001869 md_alg,
1870 (unsigned int) hash_length,
1871 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001872 signature );
1873 }
1874 else
1875#endif /* MBEDTLS_PKCS1_V15 */
1876#if defined(MBEDTLS_PKCS1_V21)
1877 if( PSA_ALG_IS_RSA_PSS( alg ) )
1878 {
1879 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1880 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1881 mbedtls_ctr_drbg_random,
1882 &global_data.ctr_drbg,
1883 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001884 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001885 (unsigned int) hash_length,
1886 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001887 signature );
1888 }
1889 else
1890#endif /* MBEDTLS_PKCS1_V21 */
1891 {
1892 return( PSA_ERROR_INVALID_ARGUMENT );
1893 }
1894
1895 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02001896 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02001897 return( mbedtls_to_psa_error( ret ) );
1898}
1899
1900static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1901 psa_algorithm_t alg,
1902 const uint8_t *hash,
1903 size_t hash_length,
1904 const uint8_t *signature,
1905 size_t signature_length )
1906{
1907 psa_status_t status;
1908 int ret;
1909 mbedtls_md_type_t md_alg;
1910
1911 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1912 if( status != PSA_SUCCESS )
1913 return( status );
1914
Gilles Peskine630a18a2018-06-29 17:49:35 +02001915 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001916 return( PSA_ERROR_BUFFER_TOO_SMALL );
1917
1918#if defined(MBEDTLS_PKCS1_V15)
1919 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1920 {
1921 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1922 MBEDTLS_MD_NONE );
1923 ret = mbedtls_rsa_pkcs1_verify( rsa,
1924 mbedtls_ctr_drbg_random,
1925 &global_data.ctr_drbg,
1926 MBEDTLS_RSA_PUBLIC,
1927 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001928 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001929 hash,
1930 signature );
1931 }
1932 else
1933#endif /* MBEDTLS_PKCS1_V15 */
1934#if defined(MBEDTLS_PKCS1_V21)
1935 if( PSA_ALG_IS_RSA_PSS( alg ) )
1936 {
1937 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1938 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1939 mbedtls_ctr_drbg_random,
1940 &global_data.ctr_drbg,
1941 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001942 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001943 (unsigned int) hash_length,
1944 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001945 signature );
1946 }
1947 else
1948#endif /* MBEDTLS_PKCS1_V21 */
1949 {
1950 return( PSA_ERROR_INVALID_ARGUMENT );
1951 }
1952 return( mbedtls_to_psa_error( ret ) );
1953}
1954#endif /* MBEDTLS_RSA_C */
1955
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001956#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001957/* `ecp` cannot be const because `ecp->grp` needs to be non-const
1958 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
1959 * (even though these functions don't modify it). */
1960static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
1961 psa_algorithm_t alg,
1962 const uint8_t *hash,
1963 size_t hash_length,
1964 uint8_t *signature,
1965 size_t signature_size,
1966 size_t *signature_length )
1967{
1968 int ret;
1969 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001970 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001971 mbedtls_mpi_init( &r );
1972 mbedtls_mpi_init( &s );
1973
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001974 if( signature_size < 2 * curve_bytes )
1975 {
1976 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
1977 goto cleanup;
1978 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001979
1980 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
1981 {
1982 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
1983 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1984 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
1985 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
1986 hash, hash_length,
1987 md_alg ) );
1988 }
1989 else
1990 {
1991 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
1992 hash, hash_length,
1993 mbedtls_ctr_drbg_random,
1994 &global_data.ctr_drbg ) );
1995 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001996
1997 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
1998 signature,
1999 curve_bytes ) );
2000 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2001 signature + curve_bytes,
2002 curve_bytes ) );
2003
2004cleanup:
2005 mbedtls_mpi_free( &r );
2006 mbedtls_mpi_free( &s );
2007 if( ret == 0 )
2008 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002009 return( mbedtls_to_psa_error( ret ) );
2010}
2011
2012static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2013 const uint8_t *hash,
2014 size_t hash_length,
2015 const uint8_t *signature,
2016 size_t signature_length )
2017{
2018 int ret;
2019 mbedtls_mpi r, s;
2020 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2021 mbedtls_mpi_init( &r );
2022 mbedtls_mpi_init( &s );
2023
2024 if( signature_length != 2 * curve_bytes )
2025 return( PSA_ERROR_INVALID_SIGNATURE );
2026
2027 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2028 signature,
2029 curve_bytes ) );
2030 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2031 signature + curve_bytes,
2032 curve_bytes ) );
2033
2034 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2035 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002036
2037cleanup:
2038 mbedtls_mpi_free( &r );
2039 mbedtls_mpi_free( &s );
2040 return( mbedtls_to_psa_error( ret ) );
2041}
2042#endif /* MBEDTLS_ECDSA_C */
2043
Gilles Peskine61b91d42018-06-08 16:09:36 +02002044psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2045 psa_algorithm_t alg,
2046 const uint8_t *hash,
2047 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002048 uint8_t *signature,
2049 size_t signature_size,
2050 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002051{
2052 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002053 psa_status_t status;
2054
2055 *signature_length = signature_size;
2056
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002057 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2058 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002059 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002060 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002061 {
2062 status = PSA_ERROR_INVALID_ARGUMENT;
2063 goto exit;
2064 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002065
Gilles Peskine20035e32018-02-03 22:44:14 +01002066#if defined(MBEDTLS_RSA_C)
2067 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2068 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002069 status = psa_rsa_sign( slot->data.rsa,
2070 alg,
2071 hash, hash_length,
2072 signature, signature_size,
2073 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002074 }
2075 else
2076#endif /* defined(MBEDTLS_RSA_C) */
2077#if defined(MBEDTLS_ECP_C)
2078 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2079 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002080#if defined(MBEDTLS_ECDSA_C)
2081 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002082 status = psa_ecdsa_sign( slot->data.ecp,
2083 alg,
2084 hash, hash_length,
2085 signature, signature_size,
2086 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002087 else
2088#endif /* defined(MBEDTLS_ECDSA_C) */
2089 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002090 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002091 }
itayzafrir5c753392018-05-08 11:18:38 +03002092 }
2093 else
2094#endif /* defined(MBEDTLS_ECP_C) */
2095 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002096 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002097 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002098
2099exit:
2100 /* Fill the unused part of the output buffer (the whole buffer on error,
2101 * the trailing part on success) with something that isn't a valid mac
2102 * (barring an attack on the mac and deliberately-crafted input),
2103 * in case the caller doesn't check the return status properly. */
2104 if( status == PSA_SUCCESS )
2105 memset( signature + *signature_length, '!',
2106 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002107 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002108 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002109 /* If signature_size is 0 then we have nothing to do. We must not call
2110 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002111 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002112}
2113
2114psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2115 psa_algorithm_t alg,
2116 const uint8_t *hash,
2117 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002118 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002119 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002120{
2121 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002122 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002123
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002124 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2125 if( status != PSA_SUCCESS )
2126 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002127
Gilles Peskine61b91d42018-06-08 16:09:36 +02002128#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002129 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002130 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002131 return( psa_rsa_verify( slot->data.rsa,
2132 alg,
2133 hash, hash_length,
2134 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002135 }
2136 else
2137#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002138#if defined(MBEDTLS_ECP_C)
2139 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2140 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002141#if defined(MBEDTLS_ECDSA_C)
2142 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002143 return( psa_ecdsa_verify( slot->data.ecp,
2144 hash, hash_length,
2145 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002146 else
2147#endif /* defined(MBEDTLS_ECDSA_C) */
2148 {
2149 return( PSA_ERROR_INVALID_ARGUMENT );
2150 }
itayzafrir5c753392018-05-08 11:18:38 +03002151 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002152 else
2153#endif /* defined(MBEDTLS_ECP_C) */
2154 {
2155 return( PSA_ERROR_NOT_SUPPORTED );
2156 }
2157}
2158
Gilles Peskine072ac562018-06-30 00:21:29 +02002159#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2160static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2161 mbedtls_rsa_context *rsa )
2162{
2163 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2164 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2165 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2166 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2167}
2168#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2169
Gilles Peskine61b91d42018-06-08 16:09:36 +02002170psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2171 psa_algorithm_t alg,
2172 const uint8_t *input,
2173 size_t input_length,
2174 const uint8_t *salt,
2175 size_t salt_length,
2176 uint8_t *output,
2177 size_t output_size,
2178 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002179{
2180 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002181 psa_status_t status;
2182
Darryl Green5cc689a2018-07-24 15:34:10 +01002183 (void) input;
2184 (void) input_length;
2185 (void) salt;
2186 (void) output;
2187 (void) output_size;
2188
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002189 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002190
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002191 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2192 return( PSA_ERROR_INVALID_ARGUMENT );
2193
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002194 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2195 if( status != PSA_SUCCESS )
2196 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002197 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2198 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002199 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002200
2201#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002202 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002203 {
2204 mbedtls_rsa_context *rsa = slot->data.rsa;
2205 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002206 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002207 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002208#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002209 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002210 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002211 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2212 mbedtls_ctr_drbg_random,
2213 &global_data.ctr_drbg,
2214 MBEDTLS_RSA_PUBLIC,
2215 input_length,
2216 input,
2217 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002218 }
2219 else
2220#endif /* MBEDTLS_PKCS1_V15 */
2221#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002222 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002223 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002224 psa_rsa_oaep_set_padding_mode( alg, rsa );
2225 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2226 mbedtls_ctr_drbg_random,
2227 &global_data.ctr_drbg,
2228 MBEDTLS_RSA_PUBLIC,
2229 salt, salt_length,
2230 input_length,
2231 input,
2232 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002233 }
2234 else
2235#endif /* MBEDTLS_PKCS1_V21 */
2236 {
2237 return( PSA_ERROR_INVALID_ARGUMENT );
2238 }
2239 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002240 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002241 return( mbedtls_to_psa_error( ret ) );
2242 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002243 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002244#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002245 {
2246 return( PSA_ERROR_NOT_SUPPORTED );
2247 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002248}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002249
Gilles Peskine61b91d42018-06-08 16:09:36 +02002250psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2251 psa_algorithm_t alg,
2252 const uint8_t *input,
2253 size_t input_length,
2254 const uint8_t *salt,
2255 size_t salt_length,
2256 uint8_t *output,
2257 size_t output_size,
2258 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002259{
2260 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002261 psa_status_t status;
2262
Darryl Green5cc689a2018-07-24 15:34:10 +01002263 (void) input;
2264 (void) input_length;
2265 (void) salt;
2266 (void) output;
2267 (void) output_size;
2268
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002269 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002270
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002271 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2272 return( PSA_ERROR_INVALID_ARGUMENT );
2273
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002274 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2275 if( status != PSA_SUCCESS )
2276 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002277 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2278 return( PSA_ERROR_INVALID_ARGUMENT );
2279
2280#if defined(MBEDTLS_RSA_C)
2281 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2282 {
2283 mbedtls_rsa_context *rsa = slot->data.rsa;
2284 int ret;
2285
Gilles Peskine630a18a2018-06-29 17:49:35 +02002286 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002287 return( PSA_ERROR_INVALID_ARGUMENT );
2288
2289#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002290 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002291 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002292 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2293 mbedtls_ctr_drbg_random,
2294 &global_data.ctr_drbg,
2295 MBEDTLS_RSA_PRIVATE,
2296 output_length,
2297 input,
2298 output,
2299 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002300 }
2301 else
2302#endif /* MBEDTLS_PKCS1_V15 */
2303#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002304 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002305 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002306 psa_rsa_oaep_set_padding_mode( alg, rsa );
2307 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2308 mbedtls_ctr_drbg_random,
2309 &global_data.ctr_drbg,
2310 MBEDTLS_RSA_PRIVATE,
2311 salt, salt_length,
2312 output_length,
2313 input,
2314 output,
2315 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002316 }
2317 else
2318#endif /* MBEDTLS_PKCS1_V21 */
2319 {
2320 return( PSA_ERROR_INVALID_ARGUMENT );
2321 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002322
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002323 return( mbedtls_to_psa_error( ret ) );
2324 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002325 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002326#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002327 {
2328 return( PSA_ERROR_NOT_SUPPORTED );
2329 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002330}
Gilles Peskine20035e32018-02-03 22:44:14 +01002331
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002332
2333
mohammad1603503973b2018-03-12 15:59:30 +02002334/****************************************************************/
2335/* Symmetric cryptography */
2336/****************************************************************/
2337
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002338/* Initialize the cipher operation structure. Once this function has been
2339 * called, psa_cipher_abort can run and will do the right thing. */
2340static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2341 psa_algorithm_t alg )
2342{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002343 if( ! PSA_ALG_IS_CIPHER( alg ) )
2344 {
2345 memset( operation, 0, sizeof( *operation ) );
2346 return( PSA_ERROR_INVALID_ARGUMENT );
2347 }
2348
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002349 operation->alg = alg;
2350 operation->key_set = 0;
2351 operation->iv_set = 0;
2352 operation->iv_required = 1;
2353 operation->iv_size = 0;
2354 operation->block_size = 0;
2355 mbedtls_cipher_init( &operation->ctx.cipher );
2356 return( PSA_SUCCESS );
2357}
2358
Gilles Peskinee553c652018-06-04 16:22:46 +02002359static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2360 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002361 psa_algorithm_t alg,
2362 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002363{
2364 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2365 psa_status_t status;
2366 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002367 size_t key_bits;
2368 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002369 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2370 PSA_KEY_USAGE_ENCRYPT :
2371 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002372
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002373 status = psa_cipher_init( operation, alg );
2374 if( status != PSA_SUCCESS )
2375 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002376
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002377 status = psa_get_key_from_slot( key, &slot, usage, alg);
2378 if( status != PSA_SUCCESS )
2379 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002380 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002381
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002382 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002383 if( cipher_info == NULL )
2384 return( PSA_ERROR_NOT_SUPPORTED );
2385
mohammad1603503973b2018-03-12 15:59:30 +02002386 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002387 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002388 {
2389 psa_cipher_abort( operation );
2390 return( mbedtls_to_psa_error( ret ) );
2391 }
2392
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002393#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002394 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002395 {
2396 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2397 unsigned char keys[24];
2398 memcpy( keys, slot->data.raw.data, 16 );
2399 memcpy( keys + 16, slot->data.raw.data, 8 );
2400 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2401 keys,
2402 192, cipher_operation );
2403 }
2404 else
2405#endif
2406 {
2407 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2408 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002409 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002410 }
Moran Peker41deec42018-04-04 15:43:05 +03002411 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002412 {
2413 psa_cipher_abort( operation );
2414 return( mbedtls_to_psa_error( ret ) );
2415 }
2416
mohammad16038481e742018-03-18 13:57:31 +02002417#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002418 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002419 {
Gilles Peskine53514202018-06-06 15:11:46 +02002420 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2421 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002422
Moran Pekera28258c2018-05-29 16:25:04 +03002423 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002424 {
2425 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2426 mode = MBEDTLS_PADDING_PKCS7;
2427 break;
2428 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2429 mode = MBEDTLS_PADDING_NONE;
2430 break;
2431 default:
Moran Pekerae382792018-05-31 14:06:17 +03002432 psa_cipher_abort( operation );
2433 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002434 }
2435 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002436 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002437 {
2438 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002439 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002440 }
mohammad16038481e742018-03-18 13:57:31 +02002441 }
2442#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2443
mohammad1603503973b2018-03-12 15:59:30 +02002444 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002445 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002446 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) :
Gilles Peskine7e928852018-06-04 16:23:10 +02002447 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002448 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002449 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002450 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002451 }
mohammad1603503973b2018-03-12 15:59:30 +02002452
Moran Peker395db872018-05-31 14:07:14 +03002453 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002454}
2455
Gilles Peskinefe119512018-07-08 21:39:34 +02002456psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2457 psa_key_slot_t key,
2458 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002459{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002460 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002461}
2462
Gilles Peskinefe119512018-07-08 21:39:34 +02002463psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2464 psa_key_slot_t key,
2465 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002466{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002467 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002468}
2469
Gilles Peskinefe119512018-07-08 21:39:34 +02002470psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2471 unsigned char *iv,
2472 size_t iv_size,
2473 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002474{
Moran Peker41deec42018-04-04 15:43:05 +03002475 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002476 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002477 return( PSA_ERROR_BAD_STATE );
2478 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002479 {
Moran Peker41deec42018-04-04 15:43:05 +03002480 ret = PSA_ERROR_BUFFER_TOO_SMALL;
2481 goto exit;
2482 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002483 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2484 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002485 if( ret != 0 )
2486 {
2487 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002488 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002489 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002490
mohammad16038481e742018-03-18 13:57:31 +02002491 *iv_length = operation->iv_size;
Gilles Peskinefe119512018-07-08 21:39:34 +02002492 ret = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002493
Moran Peker395db872018-05-31 14:07:14 +03002494exit:
2495 if( ret != PSA_SUCCESS )
2496 psa_cipher_abort( operation );
2497 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02002498}
2499
Gilles Peskinefe119512018-07-08 21:39:34 +02002500psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2501 const unsigned char *iv,
2502 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002503{
Moran Peker41deec42018-04-04 15:43:05 +03002504 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002505 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002506 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03002507 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002508 {
Moran Pekerae382792018-05-31 14:06:17 +03002509 psa_cipher_abort( operation );
2510 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03002511 }
mohammad1603503973b2018-03-12 15:59:30 +02002512 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002513 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002514 {
2515 psa_cipher_abort( operation );
2516 return( mbedtls_to_psa_error( ret ) );
2517 }
2518
2519 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02002520
Moran Peker395db872018-05-31 14:07:14 +03002521 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002522}
2523
Gilles Peskinee553c652018-06-04 16:22:46 +02002524psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2525 const uint8_t *input,
2526 size_t input_length,
2527 unsigned char *output,
2528 size_t output_size,
2529 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002530{
2531 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002532 size_t expected_output_size;
2533 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2534 {
2535 /* Take the unprocessed partial block left over from previous
2536 * update calls, if any, plus the input to this call. Remove
2537 * the last partial block, if any. You get the data that will be
2538 * output in this call. */
2539 expected_output_size =
2540 ( operation->ctx.cipher.unprocessed_len + input_length )
2541 / operation->block_size * operation->block_size;
2542 }
2543 else
2544 {
2545 expected_output_size = input_length;
2546 }
2547 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03002548 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02002549
mohammad1603503973b2018-03-12 15:59:30 +02002550 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002551 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002552 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002553 {
2554 psa_cipher_abort( operation );
2555 return( mbedtls_to_psa_error( ret ) );
2556 }
2557
Moran Peker395db872018-05-31 14:07:14 +03002558 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002559}
2560
Gilles Peskinee553c652018-06-04 16:22:46 +02002561psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2562 uint8_t *output,
2563 size_t output_size,
2564 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002565{
Janos Follath315b51c2018-07-09 16:04:51 +01002566 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2567 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002568 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002569
mohammad1603503973b2018-03-12 15:59:30 +02002570 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002571 {
Janos Follath315b51c2018-07-09 16:04:51 +01002572 status = PSA_ERROR_BAD_STATE;
2573 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002574 }
2575 if( operation->iv_required && ! operation->iv_set )
2576 {
Janos Follath315b51c2018-07-09 16:04:51 +01002577 status = PSA_ERROR_BAD_STATE;
2578 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002579 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002580 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2581 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002582 {
Gilles Peskine53514202018-06-06 15:11:46 +02002583 psa_algorithm_t padding_mode =
2584 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002585 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002586 {
Janos Follath315b51c2018-07-09 16:04:51 +01002587 status = PSA_ERROR_TAMPERING_DETECTED;
2588 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002589 }
Gilles Peskine53514202018-06-06 15:11:46 +02002590 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002591 {
2592 if( operation->ctx.cipher.unprocessed_len != 0 )
2593 {
Janos Follath315b51c2018-07-09 16:04:51 +01002594 status = PSA_ERROR_INVALID_ARGUMENT;
2595 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002596 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002597 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002598 }
2599
Janos Follath315b51c2018-07-09 16:04:51 +01002600 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2601 temp_output_buffer,
2602 output_length );
2603 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002604 {
Janos Follath315b51c2018-07-09 16:04:51 +01002605 status = mbedtls_to_psa_error( cipher_ret );
2606 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002607 }
Janos Follath315b51c2018-07-09 16:04:51 +01002608
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002609 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002610 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002611 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002612 memcpy( output, temp_output_buffer, *output_length );
2613 else
2614 {
Janos Follath315b51c2018-07-09 16:04:51 +01002615 status = PSA_ERROR_BUFFER_TOO_SMALL;
2616 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002617 }
mohammad1603503973b2018-03-12 15:59:30 +02002618
Janos Follath279ab8e2018-07-09 16:13:21 +01002619 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002620 status = psa_cipher_abort( operation );
2621
2622 return( status );
2623
2624error:
2625
2626 *output_length = 0;
2627
Janos Follath279ab8e2018-07-09 16:13:21 +01002628 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002629 (void) psa_cipher_abort( operation );
2630
2631 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002632}
2633
Gilles Peskinee553c652018-06-04 16:22:46 +02002634psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2635{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002636 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002637 {
2638 /* The object has (apparently) been initialized but it is not
2639 * in use. It's ok to call abort on such an object, and there's
2640 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002641 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002642 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002643
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002644 /* Sanity check (shouldn't happen: operation->alg should
2645 * always have been initialized to a valid value). */
2646 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2647 return( PSA_ERROR_BAD_STATE );
2648
mohammad1603503973b2018-03-12 15:59:30 +02002649 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002650
Moran Peker41deec42018-04-04 15:43:05 +03002651 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002652 operation->key_set = 0;
2653 operation->iv_set = 0;
2654 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002655 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002656 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002657
Moran Peker395db872018-05-31 14:07:14 +03002658 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002659}
2660
Gilles Peskinea0655c32018-04-30 17:06:50 +02002661
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002662
mohammad16038cc1cee2018-03-28 01:21:33 +03002663/****************************************************************/
2664/* Key Policy */
2665/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002666
mohammad160327010052018-07-03 13:16:15 +03002667#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002668void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002669{
Gilles Peskine803ce742018-06-18 16:07:14 +02002670 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002671}
2672
Gilles Peskine2d277862018-06-18 15:41:12 +02002673void psa_key_policy_set_usage( psa_key_policy_t *policy,
2674 psa_key_usage_t usage,
2675 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002676{
mohammad16034eed7572018-03-28 05:14:59 -07002677 policy->usage = usage;
2678 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002679}
2680
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002681psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002682{
mohammad16036df908f2018-04-02 08:34:15 -07002683 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002684}
2685
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002686psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002687{
mohammad16036df908f2018-04-02 08:34:15 -07002688 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002689}
mohammad160327010052018-07-03 13:16:15 +03002690#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002691
Gilles Peskine2d277862018-06-18 15:41:12 +02002692psa_status_t psa_set_key_policy( psa_key_slot_t key,
2693 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002694{
2695 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002696 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002697
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002698 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002699 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002700
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002701 status = psa_get_empty_key_slot( key, &slot );
2702 if( status != PSA_SUCCESS )
2703 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002704
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002705 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2706 PSA_KEY_USAGE_ENCRYPT |
2707 PSA_KEY_USAGE_DECRYPT |
2708 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002709 PSA_KEY_USAGE_VERIFY |
2710 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002711 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002712
mohammad16036df908f2018-04-02 08:34:15 -07002713 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002714
2715 return( PSA_SUCCESS );
2716}
2717
Gilles Peskine2d277862018-06-18 15:41:12 +02002718psa_status_t psa_get_key_policy( psa_key_slot_t key,
2719 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002720{
2721 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002722 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002723
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002724 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002725 return( PSA_ERROR_INVALID_ARGUMENT );
2726
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002727 status = psa_get_key_slot( key, &slot );
2728 if( status != PSA_SUCCESS )
2729 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002730
mohammad16036df908f2018-04-02 08:34:15 -07002731 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002732
2733 return( PSA_SUCCESS );
2734}
Gilles Peskine20035e32018-02-03 22:44:14 +01002735
Gilles Peskinea0655c32018-04-30 17:06:50 +02002736
2737
mohammad1603804cd712018-03-20 22:44:08 +02002738/****************************************************************/
2739/* Key Lifetime */
2740/****************************************************************/
2741
Gilles Peskine2d277862018-06-18 15:41:12 +02002742psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2743 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002744{
2745 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002746 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002747
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002748 status = psa_get_key_slot( key, &slot );
2749 if( status != PSA_SUCCESS )
2750 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002751
mohammad1603804cd712018-03-20 22:44:08 +02002752 *lifetime = slot->lifetime;
2753
2754 return( PSA_SUCCESS );
2755}
2756
Gilles Peskine2d277862018-06-18 15:41:12 +02002757psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002758 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002759{
2760 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002761 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002762
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002763 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2764 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002765 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2766 return( PSA_ERROR_INVALID_ARGUMENT );
2767
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002768 status = psa_get_empty_key_slot( key, &slot );
2769 if( status != PSA_SUCCESS )
2770 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002771
Moran Pekerd7326592018-05-29 16:56:39 +03002772 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002773 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002774
mohammad1603060ad8a2018-03-20 14:28:38 -07002775 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002776
2777 return( PSA_SUCCESS );
2778}
2779
Gilles Peskine20035e32018-02-03 22:44:14 +01002780
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002781
mohammad16035955c982018-04-26 00:53:03 +03002782/****************************************************************/
2783/* AEAD */
2784/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002785
mohammad16035955c982018-04-26 00:53:03 +03002786psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2787 psa_algorithm_t alg,
2788 const uint8_t *nonce,
2789 size_t nonce_length,
2790 const uint8_t *additional_data,
2791 size_t additional_data_length,
2792 const uint8_t *plaintext,
2793 size_t plaintext_length,
2794 uint8_t *ciphertext,
2795 size_t ciphertext_size,
2796 size_t *ciphertext_length )
2797{
2798 int ret;
2799 psa_status_t status;
2800 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002801 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002802 uint8_t *tag;
2803 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002804 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002805 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002806
mohammad1603f08a5502018-06-03 15:05:47 +03002807 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002808
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002809 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2810 if( status != PSA_SUCCESS )
2811 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002812 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002813
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002814 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002815 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002816 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002817 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002818
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002819 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002820 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002821 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002822
mohammad16035955c982018-04-26 00:53:03 +03002823 if( alg == PSA_ALG_GCM )
2824 {
2825 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002826 tag_length = 16;
2827
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002828 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002829 return( PSA_ERROR_INVALID_ARGUMENT );
2830
mohammad160315223a82018-06-03 17:19:55 +03002831 //make sure we have place to hold the tag in the ciphertext buffer
2832 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002833 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002834
2835 //update the tag pointer to point to the end of the ciphertext_length
2836 tag = ciphertext + plaintext_length;
2837
mohammad16035955c982018-04-26 00:53:03 +03002838 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002839 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002840 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002841 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002842 if( ret != 0 )
2843 {
2844 mbedtls_gcm_free( &gcm );
2845 return( mbedtls_to_psa_error( ret ) );
2846 }
2847 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002848 plaintext_length, nonce,
2849 nonce_length, additional_data,
2850 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002851 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002852 mbedtls_gcm_free( &gcm );
2853 }
2854 else if( alg == PSA_ALG_CCM )
2855 {
2856 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002857 tag_length = 16;
2858
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002859 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002860 return( PSA_ERROR_INVALID_ARGUMENT );
2861
mohammad160347ddf3d2018-04-26 01:11:21 +03002862 if( nonce_length < 7 || nonce_length > 13 )
2863 return( PSA_ERROR_INVALID_ARGUMENT );
2864
mohammad160315223a82018-06-03 17:19:55 +03002865 //make sure we have place to hold the tag in the ciphertext buffer
2866 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002867 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002868
2869 //update the tag pointer to point to the end of the ciphertext_length
2870 tag = ciphertext + plaintext_length;
2871
mohammad16035955c982018-04-26 00:53:03 +03002872 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002873 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002874 slot->data.raw.data,
2875 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002876 if( ret != 0 )
2877 {
2878 mbedtls_ccm_free( &ccm );
2879 return( mbedtls_to_psa_error( ret ) );
2880 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002881 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002882 nonce, nonce_length,
2883 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002884 additional_data_length,
2885 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002886 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002887 mbedtls_ccm_free( &ccm );
2888 }
mohammad16035c8845f2018-05-09 05:40:09 -07002889 else
2890 {
mohammad1603554faad2018-06-03 15:07:38 +03002891 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002892 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002893
mohammad160315223a82018-06-03 17:19:55 +03002894 if( ret != 0 )
2895 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002896 /* If ciphertext_size is 0 then ciphertext may be NULL and then the
2897 * call to memset would have undefined behavior. */
2898 if( ciphertext_size != 0 )
2899 memset( ciphertext, 0, ciphertext_size );
mohammad160315223a82018-06-03 17:19:55 +03002900 return( mbedtls_to_psa_error( ret ) );
2901 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002902
mohammad160315223a82018-06-03 17:19:55 +03002903 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002904 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002905}
2906
Gilles Peskineee652a32018-06-01 19:23:52 +02002907/* Locate the tag in a ciphertext buffer containing the encrypted data
2908 * followed by the tag. Return the length of the part preceding the tag in
2909 * *plaintext_length. This is the size of the plaintext in modes where
2910 * the encrypted data has the same size as the plaintext, such as
2911 * CCM and GCM. */
2912static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2913 const uint8_t *ciphertext,
2914 size_t ciphertext_length,
2915 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002916 const uint8_t **p_tag )
2917{
2918 size_t payload_length;
2919 if( tag_length > ciphertext_length )
2920 return( PSA_ERROR_INVALID_ARGUMENT );
2921 payload_length = ciphertext_length - tag_length;
2922 if( payload_length > plaintext_size )
2923 return( PSA_ERROR_BUFFER_TOO_SMALL );
2924 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002925 return( PSA_SUCCESS );
2926}
2927
mohammad16035955c982018-04-26 00:53:03 +03002928psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2929 psa_algorithm_t alg,
2930 const uint8_t *nonce,
2931 size_t nonce_length,
2932 const uint8_t *additional_data,
2933 size_t additional_data_length,
2934 const uint8_t *ciphertext,
2935 size_t ciphertext_length,
2936 uint8_t *plaintext,
2937 size_t plaintext_size,
2938 size_t *plaintext_length )
2939{
2940 int ret;
2941 psa_status_t status;
2942 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002943 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002944 const uint8_t *tag;
2945 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002946 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002947 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002948
Gilles Peskineee652a32018-06-01 19:23:52 +02002949 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002950
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002951 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2952 if( status != PSA_SUCCESS )
2953 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002954 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002955
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002956 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002957 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002958 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002959 return( PSA_ERROR_NOT_SUPPORTED );
2960
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002961 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002962 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002963 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002964
mohammad16035955c982018-04-26 00:53:03 +03002965 if( alg == PSA_ALG_GCM )
2966 {
2967 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002968
Gilles Peskineee652a32018-06-01 19:23:52 +02002969 tag_length = 16;
2970 status = psa_aead_unpadded_locate_tag( tag_length,
2971 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002972 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002973 if( status != PSA_SUCCESS )
2974 return( status );
2975
mohammad16035955c982018-04-26 00:53:03 +03002976 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002977 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002978 slot->data.raw.data,
2979 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002980 if( ret != 0 )
2981 {
2982 mbedtls_gcm_free( &gcm );
2983 return( mbedtls_to_psa_error( ret ) );
2984 }
mohammad16035955c982018-04-26 00:53:03 +03002985
Gilles Peskineee652a32018-06-01 19:23:52 +02002986 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002987 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002988 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002989 additional_data,
2990 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002991 tag, tag_length,
2992 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002993 mbedtls_gcm_free( &gcm );
2994 }
2995 else if( alg == PSA_ALG_CCM )
2996 {
2997 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002998
mohammad160347ddf3d2018-04-26 01:11:21 +03002999 if( nonce_length < 7 || nonce_length > 13 )
3000 return( PSA_ERROR_INVALID_ARGUMENT );
3001
mohammad16039375f842018-06-03 14:28:24 +03003002 tag_length = 16;
3003 status = psa_aead_unpadded_locate_tag( tag_length,
3004 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003005 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003006 if( status != PSA_SUCCESS )
3007 return( status );
3008
mohammad16035955c982018-04-26 00:53:03 +03003009 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02003010 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003011 slot->data.raw.data,
3012 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03003013 if( ret != 0 )
3014 {
3015 mbedtls_ccm_free( &ccm );
3016 return( mbedtls_to_psa_error( ret ) );
3017 }
mohammad160360a64d02018-06-03 17:20:42 +03003018 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003019 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003020 additional_data,
3021 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003022 ciphertext, plaintext,
3023 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03003024 mbedtls_ccm_free( &ccm );
3025 }
mohammad160339574652018-06-01 04:39:53 -07003026 else
3027 {
mohammad1603554faad2018-06-03 15:07:38 +03003028 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003029 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003030
Gilles Peskineee652a32018-06-01 19:23:52 +02003031 if( ret != 0 )
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003032 {
3033 /* If plaintext_size is 0 then plaintext may be NULL and then the
3034 * call to memset has undefined behavior. */
3035 if( plaintext_size != 0 )
3036 memset( plaintext, 0, plaintext_size );
3037 }
mohammad160360a64d02018-06-03 17:20:42 +03003038 else
3039 *plaintext_length = ciphertext_length - tag_length;
3040
Gilles Peskineee652a32018-06-01 19:23:52 +02003041 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03003042}
3043
Gilles Peskinea0655c32018-04-30 17:06:50 +02003044
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003045
Gilles Peskine20035e32018-02-03 22:44:14 +01003046/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003047/* Generators */
3048/****************************************************************/
3049
3050psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3051{
3052 psa_status_t status = PSA_SUCCESS;
3053 if( generator->alg == 0 )
3054 {
3055 /* The object has (apparently) been initialized but it is not
3056 * in use. It's ok to call abort on such an object, and there's
3057 * nothing to do. */
3058 }
3059 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003060#if defined(MBEDTLS_MD_C)
3061 if( PSA_ALG_IS_HKDF( generator->alg ) )
3062 {
3063 mbedtls_free( generator->ctx.hkdf.info );
3064 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3065 }
3066 else
3067#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003068 {
3069 status = PSA_ERROR_BAD_STATE;
3070 }
3071 memset( generator, 0, sizeof( *generator ) );
3072 return( status );
3073}
3074
3075
3076psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3077 size_t *capacity)
3078{
3079 *capacity = generator->capacity;
3080 return( PSA_SUCCESS );
3081}
3082
Gilles Peskinebef7f142018-07-12 17:22:21 +02003083#if defined(MBEDTLS_MD_C)
3084/* Read some bytes from an HKDF-based generator. This performs a chunk
3085 * of the expand phase of the HKDF algorithm. */
3086static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3087 psa_algorithm_t hash_alg,
3088 uint8_t *output,
3089 size_t output_length )
3090{
3091 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3092 psa_status_t status;
3093
3094 while( output_length != 0 )
3095 {
3096 /* Copy what remains of the current block */
3097 uint8_t n = hash_length - hkdf->offset_in_block;
3098 if( n > output_length )
3099 n = (uint8_t) output_length;
3100 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3101 output += n;
3102 output_length -= n;
3103 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003104 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003105 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003106 /* We can't be wanting more output after block 0xff, otherwise
3107 * the capacity check in psa_generator_read() would have
3108 * prevented this call. It could happen only if the generator
3109 * object was corrupted or if this function is called directly
3110 * inside the library. */
3111 if( hkdf->block_number == 0xff )
3112 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003113
3114 /* We need a new block */
3115 ++hkdf->block_number;
3116 hkdf->offset_in_block = 0;
3117 status = psa_hmac_setup_internal( &hkdf->hmac,
3118 hkdf->prk, hash_length,
3119 hash_alg );
3120 if( status != PSA_SUCCESS )
3121 return( status );
3122 if( hkdf->block_number != 1 )
3123 {
3124 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3125 hkdf->output_block,
3126 hash_length );
3127 if( status != PSA_SUCCESS )
3128 return( status );
3129 }
3130 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3131 hkdf->info,
3132 hkdf->info_length );
3133 if( status != PSA_SUCCESS )
3134 return( status );
3135 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3136 &hkdf->block_number, 1 );
3137 if( status != PSA_SUCCESS )
3138 return( status );
3139 status = psa_hmac_finish_internal( &hkdf->hmac,
3140 hkdf->output_block,
3141 sizeof( hkdf->output_block ) );
3142 if( status != PSA_SUCCESS )
3143 return( status );
3144 }
3145
3146 return( PSA_SUCCESS );
3147}
3148#endif /* MBEDTLS_MD_C */
3149
Gilles Peskineeab56e42018-07-12 17:12:33 +02003150psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3151 uint8_t *output,
3152 size_t output_length )
3153{
3154 psa_status_t status;
3155
3156 if( output_length > generator->capacity )
3157 {
3158 generator->capacity = 0;
3159 /* Go through the error path to wipe all confidential data now
3160 * that the generator object is useless. */
3161 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3162 goto exit;
3163 }
3164 if( output_length == 0 &&
3165 generator->capacity == 0 && generator->alg == 0 )
3166 {
3167 /* Edge case: this is a blank or finished generator, and 0
3168 * bytes were requested. The right error in this case could
3169 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3170 * INSUFFICIENT_CAPACITY, which is right for a finished
3171 * generator, for consistency with the case when
3172 * output_length > 0. */
3173 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3174 }
3175 generator->capacity -= output_length;
3176
Gilles Peskinebef7f142018-07-12 17:22:21 +02003177#if defined(MBEDTLS_MD_C)
3178 if( PSA_ALG_IS_HKDF( generator->alg ) )
3179 {
3180 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3181 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3182 output, output_length );
3183 }
3184 else
3185#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003186 {
3187 return( PSA_ERROR_BAD_STATE );
3188 }
3189
3190exit:
3191 if( status != PSA_SUCCESS )
3192 {
3193 psa_generator_abort( generator );
3194 memset( output, '!', output_length );
3195 }
3196 return( status );
3197}
3198
Gilles Peskine08542d82018-07-19 17:05:42 +02003199#if defined(MBEDTLS_DES_C)
3200static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3201{
3202 if( data_size >= 8 )
3203 mbedtls_des_key_set_parity( data );
3204 if( data_size >= 16 )
3205 mbedtls_des_key_set_parity( data + 8 );
3206 if( data_size >= 24 )
3207 mbedtls_des_key_set_parity( data + 16 );
3208}
3209#endif /* MBEDTLS_DES_C */
3210
Gilles Peskineeab56e42018-07-12 17:12:33 +02003211psa_status_t psa_generator_import_key( psa_key_slot_t key,
3212 psa_key_type_t type,
3213 size_t bits,
3214 psa_crypto_generator_t *generator )
3215{
3216 uint8_t *data = NULL;
3217 size_t bytes = PSA_BITS_TO_BYTES( bits );
3218 psa_status_t status;
3219
3220 if( ! key_type_is_raw_bytes( type ) )
3221 return( PSA_ERROR_INVALID_ARGUMENT );
3222 if( bits % 8 != 0 )
3223 return( PSA_ERROR_INVALID_ARGUMENT );
3224 data = mbedtls_calloc( 1, bytes );
3225 if( data == NULL )
3226 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3227
3228 status = psa_generator_read( generator, data, bytes );
3229 if( status != PSA_SUCCESS )
3230 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003231#if defined(MBEDTLS_DES_C)
3232 if( type == PSA_KEY_TYPE_DES )
3233 psa_des_set_key_parity( data, bytes );
3234#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003235 status = psa_import_key( key, type, data, bytes );
3236
3237exit:
3238 mbedtls_free( data );
3239 return( status );
3240}
3241
3242
3243
3244/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003245/* Key derivation */
3246/****************************************************************/
3247
Gilles Peskinebef7f142018-07-12 17:22:21 +02003248/* Set up an HKDF-based generator. This is exactly the extract phase
3249 * of the HKDF algorithm. */
3250static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
3251 key_slot_t *slot,
3252 psa_algorithm_t hash_alg,
3253 const uint8_t *salt,
3254 size_t salt_length,
3255 const uint8_t *label,
3256 size_t label_length )
3257{
3258 psa_status_t status;
3259 status = psa_hmac_setup_internal( &hkdf->hmac,
3260 salt, salt_length,
3261 PSA_ALG_HMAC_HASH( hash_alg ) );
3262 if( status != PSA_SUCCESS )
3263 return( status );
3264 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3265 slot->data.raw.data,
3266 slot->data.raw.bytes );
3267 if( status != PSA_SUCCESS )
3268 return( status );
3269 status = psa_hmac_finish_internal( &hkdf->hmac,
3270 hkdf->prk,
3271 sizeof( hkdf->prk ) );
3272 if( status != PSA_SUCCESS )
3273 return( status );
3274 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3275 hkdf->block_number = 0;
3276 hkdf->info_length = label_length;
3277 if( label_length != 0 )
3278 {
3279 hkdf->info = mbedtls_calloc( 1, label_length );
3280 if( hkdf->info == NULL )
3281 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3282 memcpy( hkdf->info, label, label_length );
3283 }
3284 return( PSA_SUCCESS );
3285}
3286
Gilles Peskineea0fb492018-07-12 17:17:20 +02003287psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01003288 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003289 psa_algorithm_t alg,
3290 const uint8_t *salt,
3291 size_t salt_length,
3292 const uint8_t *label,
3293 size_t label_length,
3294 size_t capacity )
3295{
3296 key_slot_t *slot;
3297 psa_status_t status;
3298
3299 if( generator->alg != 0 )
3300 return( PSA_ERROR_BAD_STATE );
3301
3302 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
3303 if( status != PSA_SUCCESS )
3304 return( status );
3305 if( slot->type != PSA_KEY_TYPE_DERIVE )
3306 return( PSA_ERROR_INVALID_ARGUMENT );
3307
3308 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
3309 return( PSA_ERROR_INVALID_ARGUMENT );
3310
Gilles Peskinebef7f142018-07-12 17:22:21 +02003311#if defined(MBEDTLS_MD_C)
3312 if( PSA_ALG_IS_HKDF( alg ) )
3313 {
3314 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3315 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3316 if( hash_size == 0 )
3317 return( PSA_ERROR_NOT_SUPPORTED );
3318 if( capacity > 255 * hash_size )
3319 return( PSA_ERROR_INVALID_ARGUMENT );
3320 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3321 slot,
3322 hash_alg,
3323 salt, salt_length,
3324 label, label_length );
3325 }
3326 else
3327#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +02003328 {
3329 return( PSA_ERROR_NOT_SUPPORTED );
3330 }
3331
3332 /* Set generator->alg even on failure so that abort knows what to do. */
3333 generator->alg = alg;
3334 if( status == PSA_SUCCESS )
3335 generator->capacity = capacity;
3336 else
3337 psa_generator_abort( generator );
3338 return( status );
3339}
3340
3341
3342
3343/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003344/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02003345/****************************************************************/
3346
3347psa_status_t psa_generate_random( uint8_t *output,
3348 size_t output_size )
3349{
3350 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3351 output, output_size );
3352 return( mbedtls_to_psa_error( ret ) );
3353}
3354
3355psa_status_t psa_generate_key( psa_key_slot_t key,
3356 psa_key_type_t type,
3357 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003358 const void *extra,
3359 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02003360{
Gilles Peskine12313cd2018-06-20 00:20:32 +02003361 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003362 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003363
Gilles Peskine53d991e2018-07-12 01:14:59 +02003364 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003365 return( PSA_ERROR_INVALID_ARGUMENT );
3366
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003367 status = psa_get_empty_key_slot( key, &slot );
3368 if( status != PSA_SUCCESS )
3369 return( status );
3370
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003371 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003372 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003373 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003374 if( status != PSA_SUCCESS )
3375 return( status );
3376 status = psa_generate_random( slot->data.raw.data,
3377 slot->data.raw.bytes );
3378 if( status != PSA_SUCCESS )
3379 {
3380 mbedtls_free( slot->data.raw.data );
3381 return( status );
3382 }
3383#if defined(MBEDTLS_DES_C)
3384 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02003385 psa_des_set_key_parity( slot->data.raw.data,
3386 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003387#endif /* MBEDTLS_DES_C */
3388 }
3389 else
3390
3391#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3392 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3393 {
3394 mbedtls_rsa_context *rsa;
3395 int ret;
3396 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003397 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3398 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02003399 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003400 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02003401 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003402 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003403 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003404#if INT_MAX < 0xffffffff
3405 /* Check that the uint32_t value passed by the caller fits
3406 * in the range supported by this implementation. */
3407 if( p->e > INT_MAX )
3408 return( PSA_ERROR_NOT_SUPPORTED );
3409#endif
3410 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003411 }
3412 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3413 if( rsa == NULL )
3414 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3415 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3416 ret = mbedtls_rsa_gen_key( rsa,
3417 mbedtls_ctr_drbg_random,
3418 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003419 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003420 exponent );
3421 if( ret != 0 )
3422 {
3423 mbedtls_rsa_free( rsa );
3424 mbedtls_free( rsa );
3425 return( mbedtls_to_psa_error( ret ) );
3426 }
3427 slot->data.rsa = rsa;
3428 }
3429 else
3430#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3431
3432#if defined(MBEDTLS_ECP_C)
3433 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3434 {
3435 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3436 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3437 const mbedtls_ecp_curve_info *curve_info =
3438 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3439 mbedtls_ecp_keypair *ecp;
3440 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003441 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003442 return( PSA_ERROR_NOT_SUPPORTED );
3443 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3444 return( PSA_ERROR_NOT_SUPPORTED );
3445 if( curve_info->bit_size != bits )
3446 return( PSA_ERROR_INVALID_ARGUMENT );
3447 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3448 if( ecp == NULL )
3449 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3450 mbedtls_ecp_keypair_init( ecp );
3451 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3452 mbedtls_ctr_drbg_random,
3453 &global_data.ctr_drbg );
3454 if( ret != 0 )
3455 {
3456 mbedtls_ecp_keypair_free( ecp );
3457 mbedtls_free( ecp );
3458 return( mbedtls_to_psa_error( ret ) );
3459 }
3460 slot->data.ecp = ecp;
3461 }
3462 else
3463#endif /* MBEDTLS_ECP_C */
3464
3465 return( PSA_ERROR_NOT_SUPPORTED );
3466
3467 slot->type = type;
3468 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003469}
3470
3471
3472/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003473/* Module setup */
3474/****************************************************************/
3475
Gilles Peskinee59236f2018-01-27 23:32:46 +01003476void mbedtls_psa_crypto_free( void )
3477{
Jaeden Amero045bd502018-06-26 14:00:08 +01003478 psa_key_slot_t key;
Gilles Peskine9a056342018-08-01 15:46:54 +02003479 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003480 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003481 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3482 mbedtls_entropy_free( &global_data.entropy );
3483 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3484}
3485
3486psa_status_t psa_crypto_init( void )
3487{
3488 int ret;
3489 const unsigned char drbg_seed[] = "PSA";
3490
3491 if( global_data.initialized != 0 )
3492 return( PSA_SUCCESS );
3493
3494 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3495 mbedtls_entropy_init( &global_data.entropy );
3496 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3497
3498 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3499 mbedtls_entropy_func,
3500 &global_data.entropy,
3501 drbg_seed, sizeof( drbg_seed ) - 1 );
3502 if( ret != 0 )
3503 goto exit;
3504
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003505 global_data.initialized = 1;
3506
Gilles Peskinee59236f2018-01-27 23:32:46 +01003507exit:
3508 if( ret != 0 )
3509 mbedtls_psa_crypto_free( );
3510 return( mbedtls_to_psa_error( ret ) );
3511}
3512
3513#endif /* MBEDTLS_PSA_CRYPTO_C */