blob: 316acbe64e0b01dfbf1296ee75beb4e74b1becd8 [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{
itayzafrir40835d42018-08-02 13:14:17 +03001113 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001114 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001115 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001116
1117 /* Fill the output buffer with something that isn't a valid hash
1118 * (barring an attack on the hash and deliberately-crafted input),
1119 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001120 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001121 /* If hash_size is 0 then hash may be NULL and then the
1122 * call to memset would have undefined behavior. */
1123 if( hash_size != 0 )
1124 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001125
1126 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001127 {
1128 status = PSA_ERROR_BUFFER_TOO_SMALL;
1129 goto exit;
1130 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001131
1132 switch( operation->alg )
1133 {
1134#if defined(MBEDTLS_MD2_C)
1135 case PSA_ALG_MD2:
1136 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1137 break;
1138#endif
1139#if defined(MBEDTLS_MD4_C)
1140 case PSA_ALG_MD4:
1141 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1142 break;
1143#endif
1144#if defined(MBEDTLS_MD5_C)
1145 case PSA_ALG_MD5:
1146 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1147 break;
1148#endif
1149#if defined(MBEDTLS_RIPEMD160_C)
1150 case PSA_ALG_RIPEMD160:
1151 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1152 break;
1153#endif
1154#if defined(MBEDTLS_SHA1_C)
1155 case PSA_ALG_SHA_1:
1156 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1157 break;
1158#endif
1159#if defined(MBEDTLS_SHA256_C)
1160 case PSA_ALG_SHA_224:
1161 case PSA_ALG_SHA_256:
1162 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1163 break;
1164#endif
1165#if defined(MBEDTLS_SHA512_C)
1166 case PSA_ALG_SHA_384:
1167 case PSA_ALG_SHA_512:
1168 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1169 break;
1170#endif
1171 default:
1172 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1173 break;
1174 }
itayzafrir40835d42018-08-02 13:14:17 +03001175 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001176
itayzafrir40835d42018-08-02 13:14:17 +03001177exit:
1178 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001179 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001180 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001181 return( psa_hash_abort( operation ) );
1182 }
1183 else
1184 {
1185 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001186 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001187 }
1188}
1189
Gilles Peskine2d277862018-06-18 15:41:12 +02001190psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1191 const uint8_t *hash,
1192 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001193{
1194 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1195 size_t actual_hash_length;
1196 psa_status_t status = psa_hash_finish( operation,
1197 actual_hash, sizeof( actual_hash ),
1198 &actual_hash_length );
1199 if( status != PSA_SUCCESS )
1200 return( status );
1201 if( actual_hash_length != hash_length )
1202 return( PSA_ERROR_INVALID_SIGNATURE );
1203 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1204 return( PSA_ERROR_INVALID_SIGNATURE );
1205 return( PSA_SUCCESS );
1206}
1207
1208
1209
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001210/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001211/* MAC */
1212/****************************************************************/
1213
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001214static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001215 psa_algorithm_t alg,
1216 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001217 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001218 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001219{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001220 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001221 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001222
1223 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1224 {
1225 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001226 {
1227 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1228 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001229
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001230 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001231 {
Gilles Peskine5d1888e2018-07-12 00:32:42 +02001232 case PSA_ALG_STREAM_CIPHER_BASE:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001233 mode = MBEDTLS_MODE_STREAM;
1234 break;
1235 case PSA_ALG_CBC_BASE:
1236 mode = MBEDTLS_MODE_CBC;
1237 break;
1238 case PSA_ALG_CFB_BASE:
1239 mode = MBEDTLS_MODE_CFB;
1240 break;
1241 case PSA_ALG_OFB_BASE:
1242 mode = MBEDTLS_MODE_OFB;
1243 break;
1244 case PSA_ALG_CTR:
1245 mode = MBEDTLS_MODE_CTR;
1246 break;
1247 case PSA_ALG_CCM:
1248 mode = MBEDTLS_MODE_CCM;
1249 break;
1250 case PSA_ALG_GCM:
1251 mode = MBEDTLS_MODE_GCM;
1252 break;
1253 default:
1254 return( NULL );
1255 }
1256 }
1257 else if( alg == PSA_ALG_CMAC )
1258 mode = MBEDTLS_MODE_ECB;
1259 else if( alg == PSA_ALG_GMAC )
1260 mode = MBEDTLS_MODE_GCM;
1261 else
1262 return( NULL );
1263
1264 switch( key_type )
1265 {
1266 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001267 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001268 break;
1269 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001270 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1271 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001272 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001273 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001274 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001275 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001276 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1277 * but two-key Triple-DES is functionally three-key Triple-DES
1278 * with K1=K3, so that's how we present it to mbedtls. */
1279 if( key_bits == 128 )
1280 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001281 break;
1282 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001283 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001284 break;
1285 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001286 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001287 break;
1288 default:
1289 return( NULL );
1290 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001291 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001292 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001293
Jaeden Amero23bbb752018-06-26 14:16:54 +01001294 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1295 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001296}
1297
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001298static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001299{
Gilles Peskine2d277862018-06-18 15:41:12 +02001300 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001301 {
1302 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001303 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001304 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001305 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001306 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001307 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001308 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001309 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001310 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001311 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001312 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001313 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001314 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001315 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001316 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001317 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001318 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001319 return( 128 );
1320 default:
1321 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001322 }
1323}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001324
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001325/* Initialize the MAC operation structure. Once this function has been
1326 * called, psa_mac_abort can run and will do the right thing. */
1327static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1328 psa_algorithm_t alg )
1329{
1330 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1331
1332 operation->alg = alg;
1333 operation->key_set = 0;
1334 operation->iv_set = 0;
1335 operation->iv_required = 0;
1336 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001337 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001338
1339#if defined(MBEDTLS_CMAC_C)
1340 if( alg == PSA_ALG_CMAC )
1341 {
1342 operation->iv_required = 0;
1343 mbedtls_cipher_init( &operation->ctx.cmac );
1344 status = PSA_SUCCESS;
1345 }
1346 else
1347#endif /* MBEDTLS_CMAC_C */
1348#if defined(MBEDTLS_MD_C)
1349 if( PSA_ALG_IS_HMAC( operation->alg ) )
1350 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001351 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1352 operation->ctx.hmac.hash_ctx.alg = 0;
1353 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001354 }
1355 else
1356#endif /* MBEDTLS_MD_C */
1357 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001358 if( ! PSA_ALG_IS_MAC( alg ) )
1359 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001360 }
1361
1362 if( status != PSA_SUCCESS )
1363 memset( operation, 0, sizeof( *operation ) );
1364 return( status );
1365}
1366
Gilles Peskine01126fa2018-07-12 17:04:55 +02001367#if defined(MBEDTLS_MD_C)
1368static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1369{
1370 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1371 return( psa_hash_abort( &hmac->hash_ctx ) );
1372}
1373#endif /* MBEDTLS_MD_C */
1374
Gilles Peskine8c9def32018-02-08 10:02:12 +01001375psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1376{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001377 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001378 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001379 /* The object has (apparently) been initialized but it is not
1380 * in use. It's ok to call abort on such an object, and there's
1381 * nothing to do. */
1382 return( PSA_SUCCESS );
1383 }
1384 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001385#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001386 if( operation->alg == PSA_ALG_CMAC )
1387 {
1388 mbedtls_cipher_free( &operation->ctx.cmac );
1389 }
1390 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001391#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001392#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001393 if( PSA_ALG_IS_HMAC( operation->alg ) )
1394 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001395 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001396 }
1397 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001398#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001399 {
1400 /* Sanity check (shouldn't happen: operation->alg should
1401 * always have been initialized to a valid value). */
1402 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001403 }
Moran Peker41deec42018-04-04 15:43:05 +03001404
Gilles Peskine8c9def32018-02-08 10:02:12 +01001405 operation->alg = 0;
1406 operation->key_set = 0;
1407 operation->iv_set = 0;
1408 operation->iv_required = 0;
1409 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001410 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001411
Gilles Peskine8c9def32018-02-08 10:02:12 +01001412 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001413
1414bad_state:
1415 /* If abort is called on an uninitialized object, we can't trust
1416 * anything. Wipe the object in case it contains confidential data.
1417 * This may result in a memory leak if a pointer gets overwritten,
1418 * but it's too late to do anything about this. */
1419 memset( operation, 0, sizeof( *operation ) );
1420 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001421}
1422
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001423#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001424static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001425 size_t key_bits,
1426 key_slot_t *slot,
1427 const mbedtls_cipher_info_t *cipher_info )
1428{
1429 int ret;
1430
1431 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001432
1433 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1434 if( ret != 0 )
1435 return( ret );
1436
1437 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1438 slot->data.raw.data,
1439 key_bits );
1440 return( ret );
1441}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001442#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001443
Gilles Peskine248051a2018-06-20 16:09:38 +02001444#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001445static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1446 const uint8_t *key,
1447 size_t key_length,
1448 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001449{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001450 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001451 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001452 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001453 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001454 psa_status_t status;
1455
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001456 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1457 * overflow below. This should never trigger if the hash algorithm
1458 * is implemented correctly. */
1459 /* The size checks against the ipad and opad buffers cannot be written
1460 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1461 * because that triggers -Wlogical-op on GCC 7.3. */
1462 if( block_size > sizeof( ipad ) )
1463 return( PSA_ERROR_NOT_SUPPORTED );
1464 if( block_size > sizeof( hmac->opad ) )
1465 return( PSA_ERROR_NOT_SUPPORTED );
1466 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001467 return( PSA_ERROR_NOT_SUPPORTED );
1468
Gilles Peskined223b522018-06-11 18:12:58 +02001469 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001470 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001471 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001472 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001473 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001474 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001475 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001476 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001477 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001478 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001479 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001480 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001481 }
Gilles Peskine96889972018-07-12 17:07:03 +02001482 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1483 * but it is permitted. It is common when HMAC is used in HKDF, for
1484 * example. Don't call `memcpy` in the 0-length because `key` could be
1485 * an invalid pointer which would make the behavior undefined. */
1486 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001487 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001488
Gilles Peskined223b522018-06-11 18:12:58 +02001489 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1490 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001491 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001492 ipad[i] ^= 0x36;
1493 memset( ipad + key_length, 0x36, block_size - key_length );
1494
1495 /* Copy the key material from ipad to opad, flipping the requisite bits,
1496 * and filling the rest of opad with the requisite constant. */
1497 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001498 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1499 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001500
Gilles Peskine01126fa2018-07-12 17:04:55 +02001501 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001502 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001503 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001504
Gilles Peskine01126fa2018-07-12 17:04:55 +02001505 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001506
1507cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001508 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001509
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001510 return( status );
1511}
Gilles Peskine248051a2018-06-20 16:09:38 +02001512#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001513
Gilles Peskine89167cb2018-07-08 20:12:23 +02001514static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1515 psa_key_slot_t key,
1516 psa_algorithm_t alg,
1517 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001518{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001519 psa_status_t status;
1520 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001521 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001522 psa_key_usage_t usage =
1523 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001524
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001525 status = psa_mac_init( operation, alg );
1526 if( status != PSA_SUCCESS )
1527 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001528 if( is_sign )
1529 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001530
Gilles Peskine89167cb2018-07-08 20:12:23 +02001531 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001532 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001533 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001534 key_bits = psa_get_key_bits( slot );
1535
Gilles Peskine8c9def32018-02-08 10:02:12 +01001536#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001537 if( alg == PSA_ALG_CMAC )
1538 {
1539 const mbedtls_cipher_info_t *cipher_info =
1540 mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
1541 int ret;
1542 if( cipher_info == NULL )
1543 {
1544 status = PSA_ERROR_NOT_SUPPORTED;
1545 goto exit;
1546 }
1547 operation->mac_size = cipher_info->block_size;
1548 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1549 status = mbedtls_to_psa_error( ret );
1550 }
1551 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001552#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001553#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001554 if( PSA_ALG_IS_HMAC( alg ) )
1555 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001556 psa_algorithm_t hash_alg = PSA_ALG_HMAC_HASH( alg );
1557 if( hash_alg == 0 )
1558 {
1559 status = PSA_ERROR_NOT_SUPPORTED;
1560 goto exit;
1561 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001562
1563 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1564 /* Sanity check. This shouldn't fail on a valid configuration. */
1565 if( operation->mac_size == 0 ||
1566 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1567 {
1568 status = PSA_ERROR_NOT_SUPPORTED;
1569 goto exit;
1570 }
1571
Gilles Peskine01126fa2018-07-12 17:04:55 +02001572 if( slot->type != PSA_KEY_TYPE_HMAC )
1573 {
1574 status = PSA_ERROR_INVALID_ARGUMENT;
1575 goto exit;
1576 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001577
Gilles Peskine01126fa2018-07-12 17:04:55 +02001578 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1579 slot->data.raw.data,
1580 slot->data.raw.bytes,
1581 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001582 }
1583 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001584#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001585 {
1586 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001587 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001588
Gilles Peskinefbfac682018-07-08 20:51:54 +02001589exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001590 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001591 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001592 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001593 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001594 else
1595 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001596 operation->key_set = 1;
1597 }
1598 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001599}
1600
Gilles Peskine89167cb2018-07-08 20:12:23 +02001601psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1602 psa_key_slot_t key,
1603 psa_algorithm_t alg )
1604{
1605 return( psa_mac_setup( operation, key, alg, 1 ) );
1606}
1607
1608psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1609 psa_key_slot_t key,
1610 psa_algorithm_t alg )
1611{
1612 return( psa_mac_setup( operation, key, alg, 0 ) );
1613}
1614
Gilles Peskine8c9def32018-02-08 10:02:12 +01001615psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1616 const uint8_t *input,
1617 size_t input_length )
1618{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001619 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001620 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001621 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001622 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001623 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001624 operation->has_input = 1;
1625
Gilles Peskine8c9def32018-02-08 10:02:12 +01001626#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001627 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001628 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001629 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1630 input, input_length );
1631 status = mbedtls_to_psa_error( ret );
1632 }
1633 else
1634#endif /* MBEDTLS_CMAC_C */
1635#if defined(MBEDTLS_MD_C)
1636 if( PSA_ALG_IS_HMAC( operation->alg ) )
1637 {
1638 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1639 input_length );
1640 }
1641 else
1642#endif /* MBEDTLS_MD_C */
1643 {
1644 /* This shouldn't happen if `operation` was initialized by
1645 * a setup function. */
1646 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001647 }
1648
Gilles Peskinefbfac682018-07-08 20:51:54 +02001649cleanup:
1650 if( status != PSA_SUCCESS )
1651 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001652 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001653}
1654
Gilles Peskine01126fa2018-07-12 17:04:55 +02001655#if defined(MBEDTLS_MD_C)
1656static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1657 uint8_t *mac,
1658 size_t mac_size )
1659{
1660 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1661 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1662 size_t hash_size = 0;
1663 size_t block_size = psa_get_hash_block_size( hash_alg );
1664 psa_status_t status;
1665
Gilles Peskine01126fa2018-07-12 17:04:55 +02001666 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1667 if( status != PSA_SUCCESS )
1668 return( status );
1669 /* From here on, tmp needs to be wiped. */
1670
1671 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1672 if( status != PSA_SUCCESS )
1673 goto exit;
1674
1675 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1676 if( status != PSA_SUCCESS )
1677 goto exit;
1678
1679 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1680 if( status != PSA_SUCCESS )
1681 goto exit;
1682
1683 status = psa_hash_finish( &hmac->hash_ctx, mac, mac_size, &hash_size );
1684
1685exit:
1686 mbedtls_zeroize( tmp, hash_size );
1687 return( status );
1688}
1689#endif /* MBEDTLS_MD_C */
1690
mohammad16036df908f2018-04-02 08:34:15 -07001691static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001692 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001693 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001694{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001695 if( ! operation->key_set )
1696 return( PSA_ERROR_BAD_STATE );
1697 if( operation->iv_required && ! operation->iv_set )
1698 return( PSA_ERROR_BAD_STATE );
1699
Gilles Peskine8c9def32018-02-08 10:02:12 +01001700 if( mac_size < operation->mac_size )
1701 return( PSA_ERROR_BUFFER_TOO_SMALL );
1702
Gilles Peskine8c9def32018-02-08 10:02:12 +01001703#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001704 if( operation->alg == PSA_ALG_CMAC )
1705 {
1706 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1707 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001708 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001709 else
1710#endif /* MBEDTLS_CMAC_C */
1711#if defined(MBEDTLS_MD_C)
1712 if( PSA_ALG_IS_HMAC( operation->alg ) )
1713 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001714 return( psa_hmac_finish_internal( &operation->ctx.hmac,
1715 mac, mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001716 }
1717 else
1718#endif /* MBEDTLS_MD_C */
1719 {
1720 /* This shouldn't happen if `operation` was initialized by
1721 * a setup function. */
1722 return( PSA_ERROR_BAD_STATE );
1723 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001724}
1725
Gilles Peskineacd4be32018-07-08 19:56:25 +02001726psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1727 uint8_t *mac,
1728 size_t mac_size,
1729 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001730{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001731 psa_status_t status;
1732
1733 /* Fill the output buffer with something that isn't a valid mac
1734 * (barring an attack on the mac and deliberately-crafted input),
1735 * in case the caller doesn't check the return status properly. */
1736 *mac_length = mac_size;
1737 /* If mac_size is 0 then mac may be NULL and then the
1738 * call to memset would have undefined behavior. */
1739 if( mac_size != 0 )
1740 memset( mac, '!', mac_size );
1741
Gilles Peskine89167cb2018-07-08 20:12:23 +02001742 if( ! operation->is_sign )
1743 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001744 status = PSA_ERROR_BAD_STATE;
1745 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001746 }
mohammad16036df908f2018-04-02 08:34:15 -07001747
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001748 status = psa_mac_finish_internal( operation, mac, mac_size );
1749
1750cleanup:
1751 if( status == PSA_SUCCESS )
1752 {
1753 status = psa_mac_abort( operation );
1754 if( status == PSA_SUCCESS )
1755 *mac_length = operation->mac_size;
1756 else
1757 memset( mac, '!', mac_size );
1758 }
1759 else
1760 psa_mac_abort( operation );
1761 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001762}
1763
Gilles Peskineacd4be32018-07-08 19:56:25 +02001764psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1765 const uint8_t *mac,
1766 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001767{
Gilles Peskine828ed142018-06-18 23:25:51 +02001768 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001769 psa_status_t status;
1770
Gilles Peskine89167cb2018-07-08 20:12:23 +02001771 if( operation->is_sign )
1772 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001773 status = PSA_ERROR_BAD_STATE;
1774 goto cleanup;
1775 }
1776 if( operation->mac_size != mac_length )
1777 {
1778 status = PSA_ERROR_INVALID_SIGNATURE;
1779 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001780 }
mohammad16036df908f2018-04-02 08:34:15 -07001781
1782 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001783 actual_mac, sizeof( actual_mac ) );
1784
1785 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1786 status = PSA_ERROR_INVALID_SIGNATURE;
1787
1788cleanup:
1789 if( status == PSA_SUCCESS )
1790 status = psa_mac_abort( operation );
1791 else
1792 psa_mac_abort( operation );
1793
1794 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001795}
1796
1797
Gilles Peskine20035e32018-02-03 22:44:14 +01001798
Gilles Peskine20035e32018-02-03 22:44:14 +01001799/****************************************************************/
1800/* Asymmetric cryptography */
1801/****************************************************************/
1802
Gilles Peskine2b450e32018-06-27 15:42:46 +02001803#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001804/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001805 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001806static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1807 size_t hash_length,
1808 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001809{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001810 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001811 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001812 *md_alg = mbedtls_md_get_type( md_info );
1813
1814 /* The Mbed TLS RSA module uses an unsigned int for hash length
1815 * parameters. Validate that it fits so that we don't risk an
1816 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001817#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001818 if( hash_length > UINT_MAX )
1819 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001820#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001821
1822#if defined(MBEDTLS_PKCS1_V15)
1823 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
1824 * must be correct. */
1825 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
1826 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001827 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001828 if( md_info == NULL )
1829 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001830 if( mbedtls_md_get_size( md_info ) != hash_length )
1831 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001832 }
1833#endif /* MBEDTLS_PKCS1_V15 */
1834
1835#if defined(MBEDTLS_PKCS1_V21)
1836 /* PSS requires a hash internally. */
1837 if( PSA_ALG_IS_RSA_PSS( alg ) )
1838 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001839 if( md_info == NULL )
1840 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001841 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001842#endif /* MBEDTLS_PKCS1_V21 */
1843
Gilles Peskine61b91d42018-06-08 16:09:36 +02001844 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001845}
1846
Gilles Peskine2b450e32018-06-27 15:42:46 +02001847static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1848 psa_algorithm_t alg,
1849 const uint8_t *hash,
1850 size_t hash_length,
1851 uint8_t *signature,
1852 size_t signature_size,
1853 size_t *signature_length )
1854{
1855 psa_status_t status;
1856 int ret;
1857 mbedtls_md_type_t md_alg;
1858
1859 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1860 if( status != PSA_SUCCESS )
1861 return( status );
1862
Gilles Peskine630a18a2018-06-29 17:49:35 +02001863 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001864 return( PSA_ERROR_BUFFER_TOO_SMALL );
1865
1866#if defined(MBEDTLS_PKCS1_V15)
1867 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1868 {
1869 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1870 MBEDTLS_MD_NONE );
1871 ret = mbedtls_rsa_pkcs1_sign( rsa,
1872 mbedtls_ctr_drbg_random,
1873 &global_data.ctr_drbg,
1874 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001875 md_alg,
1876 (unsigned int) hash_length,
1877 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001878 signature );
1879 }
1880 else
1881#endif /* MBEDTLS_PKCS1_V15 */
1882#if defined(MBEDTLS_PKCS1_V21)
1883 if( PSA_ALG_IS_RSA_PSS( alg ) )
1884 {
1885 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1886 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1887 mbedtls_ctr_drbg_random,
1888 &global_data.ctr_drbg,
1889 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001890 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001891 (unsigned int) hash_length,
1892 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001893 signature );
1894 }
1895 else
1896#endif /* MBEDTLS_PKCS1_V21 */
1897 {
1898 return( PSA_ERROR_INVALID_ARGUMENT );
1899 }
1900
1901 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02001902 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02001903 return( mbedtls_to_psa_error( ret ) );
1904}
1905
1906static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1907 psa_algorithm_t alg,
1908 const uint8_t *hash,
1909 size_t hash_length,
1910 const uint8_t *signature,
1911 size_t signature_length )
1912{
1913 psa_status_t status;
1914 int ret;
1915 mbedtls_md_type_t md_alg;
1916
1917 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1918 if( status != PSA_SUCCESS )
1919 return( status );
1920
Gilles Peskine630a18a2018-06-29 17:49:35 +02001921 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001922 return( PSA_ERROR_BUFFER_TOO_SMALL );
1923
1924#if defined(MBEDTLS_PKCS1_V15)
1925 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1926 {
1927 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1928 MBEDTLS_MD_NONE );
1929 ret = mbedtls_rsa_pkcs1_verify( rsa,
1930 mbedtls_ctr_drbg_random,
1931 &global_data.ctr_drbg,
1932 MBEDTLS_RSA_PUBLIC,
1933 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001934 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001935 hash,
1936 signature );
1937 }
1938 else
1939#endif /* MBEDTLS_PKCS1_V15 */
1940#if defined(MBEDTLS_PKCS1_V21)
1941 if( PSA_ALG_IS_RSA_PSS( alg ) )
1942 {
1943 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1944 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1945 mbedtls_ctr_drbg_random,
1946 &global_data.ctr_drbg,
1947 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001948 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001949 (unsigned int) hash_length,
1950 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001951 signature );
1952 }
1953 else
1954#endif /* MBEDTLS_PKCS1_V21 */
1955 {
1956 return( PSA_ERROR_INVALID_ARGUMENT );
1957 }
1958 return( mbedtls_to_psa_error( ret ) );
1959}
1960#endif /* MBEDTLS_RSA_C */
1961
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001962#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001963/* `ecp` cannot be const because `ecp->grp` needs to be non-const
1964 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
1965 * (even though these functions don't modify it). */
1966static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
1967 psa_algorithm_t alg,
1968 const uint8_t *hash,
1969 size_t hash_length,
1970 uint8_t *signature,
1971 size_t signature_size,
1972 size_t *signature_length )
1973{
1974 int ret;
1975 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001976 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001977 mbedtls_mpi_init( &r );
1978 mbedtls_mpi_init( &s );
1979
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001980 if( signature_size < 2 * curve_bytes )
1981 {
1982 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
1983 goto cleanup;
1984 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001985
1986 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
1987 {
1988 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
1989 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1990 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
1991 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
1992 hash, hash_length,
1993 md_alg ) );
1994 }
1995 else
1996 {
1997 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
1998 hash, hash_length,
1999 mbedtls_ctr_drbg_random,
2000 &global_data.ctr_drbg ) );
2001 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002002
2003 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2004 signature,
2005 curve_bytes ) );
2006 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2007 signature + curve_bytes,
2008 curve_bytes ) );
2009
2010cleanup:
2011 mbedtls_mpi_free( &r );
2012 mbedtls_mpi_free( &s );
2013 if( ret == 0 )
2014 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002015 return( mbedtls_to_psa_error( ret ) );
2016}
2017
2018static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2019 const uint8_t *hash,
2020 size_t hash_length,
2021 const uint8_t *signature,
2022 size_t signature_length )
2023{
2024 int ret;
2025 mbedtls_mpi r, s;
2026 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2027 mbedtls_mpi_init( &r );
2028 mbedtls_mpi_init( &s );
2029
2030 if( signature_length != 2 * curve_bytes )
2031 return( PSA_ERROR_INVALID_SIGNATURE );
2032
2033 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2034 signature,
2035 curve_bytes ) );
2036 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2037 signature + curve_bytes,
2038 curve_bytes ) );
2039
2040 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2041 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002042
2043cleanup:
2044 mbedtls_mpi_free( &r );
2045 mbedtls_mpi_free( &s );
2046 return( mbedtls_to_psa_error( ret ) );
2047}
2048#endif /* MBEDTLS_ECDSA_C */
2049
Gilles Peskine61b91d42018-06-08 16:09:36 +02002050psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2051 psa_algorithm_t alg,
2052 const uint8_t *hash,
2053 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002054 uint8_t *signature,
2055 size_t signature_size,
2056 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002057{
2058 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002059 psa_status_t status;
2060
2061 *signature_length = signature_size;
2062
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002063 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2064 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002065 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002066 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002067 {
2068 status = PSA_ERROR_INVALID_ARGUMENT;
2069 goto exit;
2070 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002071
Gilles Peskine20035e32018-02-03 22:44:14 +01002072#if defined(MBEDTLS_RSA_C)
2073 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2074 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002075 status = psa_rsa_sign( slot->data.rsa,
2076 alg,
2077 hash, hash_length,
2078 signature, signature_size,
2079 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002080 }
2081 else
2082#endif /* defined(MBEDTLS_RSA_C) */
2083#if defined(MBEDTLS_ECP_C)
2084 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2085 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002086#if defined(MBEDTLS_ECDSA_C)
2087 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002088 status = psa_ecdsa_sign( slot->data.ecp,
2089 alg,
2090 hash, hash_length,
2091 signature, signature_size,
2092 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002093 else
2094#endif /* defined(MBEDTLS_ECDSA_C) */
2095 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002096 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002097 }
itayzafrir5c753392018-05-08 11:18:38 +03002098 }
2099 else
2100#endif /* defined(MBEDTLS_ECP_C) */
2101 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002102 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002103 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002104
2105exit:
2106 /* Fill the unused part of the output buffer (the whole buffer on error,
2107 * the trailing part on success) with something that isn't a valid mac
2108 * (barring an attack on the mac and deliberately-crafted input),
2109 * in case the caller doesn't check the return status properly. */
2110 if( status == PSA_SUCCESS )
2111 memset( signature + *signature_length, '!',
2112 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002113 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002114 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002115 /* If signature_size is 0 then we have nothing to do. We must not call
2116 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002117 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002118}
2119
2120psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2121 psa_algorithm_t alg,
2122 const uint8_t *hash,
2123 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002124 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002125 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002126{
2127 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002128 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002129
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002130 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2131 if( status != PSA_SUCCESS )
2132 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002133
Gilles Peskine61b91d42018-06-08 16:09:36 +02002134#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002135 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002136 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002137 return( psa_rsa_verify( slot->data.rsa,
2138 alg,
2139 hash, hash_length,
2140 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002141 }
2142 else
2143#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002144#if defined(MBEDTLS_ECP_C)
2145 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2146 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002147#if defined(MBEDTLS_ECDSA_C)
2148 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002149 return( psa_ecdsa_verify( slot->data.ecp,
2150 hash, hash_length,
2151 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002152 else
2153#endif /* defined(MBEDTLS_ECDSA_C) */
2154 {
2155 return( PSA_ERROR_INVALID_ARGUMENT );
2156 }
itayzafrir5c753392018-05-08 11:18:38 +03002157 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002158 else
2159#endif /* defined(MBEDTLS_ECP_C) */
2160 {
2161 return( PSA_ERROR_NOT_SUPPORTED );
2162 }
2163}
2164
Gilles Peskine072ac562018-06-30 00:21:29 +02002165#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2166static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2167 mbedtls_rsa_context *rsa )
2168{
2169 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2170 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2171 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2172 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2173}
2174#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2175
Gilles Peskine61b91d42018-06-08 16:09:36 +02002176psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2177 psa_algorithm_t alg,
2178 const uint8_t *input,
2179 size_t input_length,
2180 const uint8_t *salt,
2181 size_t salt_length,
2182 uint8_t *output,
2183 size_t output_size,
2184 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002185{
2186 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002187 psa_status_t status;
2188
Darryl Green5cc689a2018-07-24 15:34:10 +01002189 (void) input;
2190 (void) input_length;
2191 (void) salt;
2192 (void) output;
2193 (void) output_size;
2194
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002195 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002196
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002197 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2198 return( PSA_ERROR_INVALID_ARGUMENT );
2199
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002200 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2201 if( status != PSA_SUCCESS )
2202 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002203 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2204 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002205 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002206
2207#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002208 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002209 {
2210 mbedtls_rsa_context *rsa = slot->data.rsa;
2211 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002212 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002213 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002214#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002215 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002216 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002217 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2218 mbedtls_ctr_drbg_random,
2219 &global_data.ctr_drbg,
2220 MBEDTLS_RSA_PUBLIC,
2221 input_length,
2222 input,
2223 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002224 }
2225 else
2226#endif /* MBEDTLS_PKCS1_V15 */
2227#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002228 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002229 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002230 psa_rsa_oaep_set_padding_mode( alg, rsa );
2231 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2232 mbedtls_ctr_drbg_random,
2233 &global_data.ctr_drbg,
2234 MBEDTLS_RSA_PUBLIC,
2235 salt, salt_length,
2236 input_length,
2237 input,
2238 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002239 }
2240 else
2241#endif /* MBEDTLS_PKCS1_V21 */
2242 {
2243 return( PSA_ERROR_INVALID_ARGUMENT );
2244 }
2245 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002246 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002247 return( mbedtls_to_psa_error( ret ) );
2248 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002249 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002250#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002251 {
2252 return( PSA_ERROR_NOT_SUPPORTED );
2253 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002254}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002255
Gilles Peskine61b91d42018-06-08 16:09:36 +02002256psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2257 psa_algorithm_t alg,
2258 const uint8_t *input,
2259 size_t input_length,
2260 const uint8_t *salt,
2261 size_t salt_length,
2262 uint8_t *output,
2263 size_t output_size,
2264 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002265{
2266 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002267 psa_status_t status;
2268
Darryl Green5cc689a2018-07-24 15:34:10 +01002269 (void) input;
2270 (void) input_length;
2271 (void) salt;
2272 (void) output;
2273 (void) output_size;
2274
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002275 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002276
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002277 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2278 return( PSA_ERROR_INVALID_ARGUMENT );
2279
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002280 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2281 if( status != PSA_SUCCESS )
2282 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002283 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2284 return( PSA_ERROR_INVALID_ARGUMENT );
2285
2286#if defined(MBEDTLS_RSA_C)
2287 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2288 {
2289 mbedtls_rsa_context *rsa = slot->data.rsa;
2290 int ret;
2291
Gilles Peskine630a18a2018-06-29 17:49:35 +02002292 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002293 return( PSA_ERROR_INVALID_ARGUMENT );
2294
2295#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002296 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002297 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002298 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2299 mbedtls_ctr_drbg_random,
2300 &global_data.ctr_drbg,
2301 MBEDTLS_RSA_PRIVATE,
2302 output_length,
2303 input,
2304 output,
2305 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002306 }
2307 else
2308#endif /* MBEDTLS_PKCS1_V15 */
2309#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002310 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002311 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002312 psa_rsa_oaep_set_padding_mode( alg, rsa );
2313 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2314 mbedtls_ctr_drbg_random,
2315 &global_data.ctr_drbg,
2316 MBEDTLS_RSA_PRIVATE,
2317 salt, salt_length,
2318 output_length,
2319 input,
2320 output,
2321 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002322 }
2323 else
2324#endif /* MBEDTLS_PKCS1_V21 */
2325 {
2326 return( PSA_ERROR_INVALID_ARGUMENT );
2327 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002328
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002329 return( mbedtls_to_psa_error( ret ) );
2330 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002331 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002332#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002333 {
2334 return( PSA_ERROR_NOT_SUPPORTED );
2335 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002336}
Gilles Peskine20035e32018-02-03 22:44:14 +01002337
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002338
2339
mohammad1603503973b2018-03-12 15:59:30 +02002340/****************************************************************/
2341/* Symmetric cryptography */
2342/****************************************************************/
2343
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002344/* Initialize the cipher operation structure. Once this function has been
2345 * called, psa_cipher_abort can run and will do the right thing. */
2346static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2347 psa_algorithm_t alg )
2348{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002349 if( ! PSA_ALG_IS_CIPHER( alg ) )
2350 {
2351 memset( operation, 0, sizeof( *operation ) );
2352 return( PSA_ERROR_INVALID_ARGUMENT );
2353 }
2354
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002355 operation->alg = alg;
2356 operation->key_set = 0;
2357 operation->iv_set = 0;
2358 operation->iv_required = 1;
2359 operation->iv_size = 0;
2360 operation->block_size = 0;
2361 mbedtls_cipher_init( &operation->ctx.cipher );
2362 return( PSA_SUCCESS );
2363}
2364
Gilles Peskinee553c652018-06-04 16:22:46 +02002365static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2366 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002367 psa_algorithm_t alg,
2368 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002369{
2370 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2371 psa_status_t status;
2372 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002373 size_t key_bits;
2374 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002375 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2376 PSA_KEY_USAGE_ENCRYPT :
2377 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002378
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002379 status = psa_cipher_init( operation, alg );
2380 if( status != PSA_SUCCESS )
2381 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002382
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002383 status = psa_get_key_from_slot( key, &slot, usage, alg);
2384 if( status != PSA_SUCCESS )
2385 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002386 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002387
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002388 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002389 if( cipher_info == NULL )
2390 return( PSA_ERROR_NOT_SUPPORTED );
2391
mohammad1603503973b2018-03-12 15:59:30 +02002392 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002393 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002394 {
2395 psa_cipher_abort( operation );
2396 return( mbedtls_to_psa_error( ret ) );
2397 }
2398
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002399#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002400 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002401 {
2402 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2403 unsigned char keys[24];
2404 memcpy( keys, slot->data.raw.data, 16 );
2405 memcpy( keys + 16, slot->data.raw.data, 8 );
2406 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2407 keys,
2408 192, cipher_operation );
2409 }
2410 else
2411#endif
2412 {
2413 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2414 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002415 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002416 }
Moran Peker41deec42018-04-04 15:43:05 +03002417 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002418 {
2419 psa_cipher_abort( operation );
2420 return( mbedtls_to_psa_error( ret ) );
2421 }
2422
mohammad16038481e742018-03-18 13:57:31 +02002423#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002424 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002425 {
Gilles Peskine53514202018-06-06 15:11:46 +02002426 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2427 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002428
Moran Pekera28258c2018-05-29 16:25:04 +03002429 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002430 {
2431 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2432 mode = MBEDTLS_PADDING_PKCS7;
2433 break;
2434 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2435 mode = MBEDTLS_PADDING_NONE;
2436 break;
2437 default:
Moran Pekerae382792018-05-31 14:06:17 +03002438 psa_cipher_abort( operation );
2439 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002440 }
2441 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002442 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002443 {
2444 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002445 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002446 }
mohammad16038481e742018-03-18 13:57:31 +02002447 }
2448#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2449
mohammad1603503973b2018-03-12 15:59:30 +02002450 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002451 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002452 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) :
Gilles Peskine7e928852018-06-04 16:23:10 +02002453 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002454 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002455 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002456 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002457 }
mohammad1603503973b2018-03-12 15:59:30 +02002458
Moran Peker395db872018-05-31 14:07:14 +03002459 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002460}
2461
Gilles Peskinefe119512018-07-08 21:39:34 +02002462psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2463 psa_key_slot_t key,
2464 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002465{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002466 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002467}
2468
Gilles Peskinefe119512018-07-08 21:39:34 +02002469psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2470 psa_key_slot_t key,
2471 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002472{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002473 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002474}
2475
Gilles Peskinefe119512018-07-08 21:39:34 +02002476psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2477 unsigned char *iv,
2478 size_t iv_size,
2479 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002480{
itayzafrir534bd7c2018-08-02 13:56:32 +03002481 psa_status_t status;
2482 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002483 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002484 {
2485 status = PSA_ERROR_BAD_STATE;
2486 goto exit;
2487 }
Moran Peker41deec42018-04-04 15:43:05 +03002488 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002489 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002490 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002491 goto exit;
2492 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002493 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2494 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002495 if( ret != 0 )
2496 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002497 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002498 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002499 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002500
mohammad16038481e742018-03-18 13:57:31 +02002501 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002502 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002503
Moran Peker395db872018-05-31 14:07:14 +03002504exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002505 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002506 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002507 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002508}
2509
Gilles Peskinefe119512018-07-08 21:39:34 +02002510psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2511 const unsigned char *iv,
2512 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002513{
itayzafrir534bd7c2018-08-02 13:56:32 +03002514 psa_status_t status;
2515 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002516 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002517 {
2518 status = PSA_ERROR_BAD_STATE;
2519 goto exit;
2520 }
Moran Pekera28258c2018-05-29 16:25:04 +03002521 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002522 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002523 status = PSA_ERROR_INVALID_ARGUMENT;
2524 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002525 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002526 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2527 status = mbedtls_to_psa_error( ret );
2528exit:
2529 if( status == PSA_SUCCESS )
2530 operation->iv_set = 1;
2531 else
mohammad1603503973b2018-03-12 15:59:30 +02002532 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002533 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002534}
2535
Gilles Peskinee553c652018-06-04 16:22:46 +02002536psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2537 const uint8_t *input,
2538 size_t input_length,
2539 unsigned char *output,
2540 size_t output_size,
2541 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002542{
itayzafrir534bd7c2018-08-02 13:56:32 +03002543 psa_status_t status;
2544 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002545 size_t expected_output_size;
2546 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2547 {
2548 /* Take the unprocessed partial block left over from previous
2549 * update calls, if any, plus the input to this call. Remove
2550 * the last partial block, if any. You get the data that will be
2551 * output in this call. */
2552 expected_output_size =
2553 ( operation->ctx.cipher.unprocessed_len + input_length )
2554 / operation->block_size * operation->block_size;
2555 }
2556 else
2557 {
2558 expected_output_size = input_length;
2559 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002560
Gilles Peskine89d789c2018-06-04 17:17:16 +02002561 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002562 {
2563 status = PSA_ERROR_BUFFER_TOO_SMALL;
2564 goto exit;
2565 }
mohammad160382759612018-03-12 18:16:40 +02002566
mohammad1603503973b2018-03-12 15:59:30 +02002567 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002568 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002569 status = mbedtls_to_psa_error( ret );
2570exit:
2571 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002572 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002573 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002574}
2575
Gilles Peskinee553c652018-06-04 16:22:46 +02002576psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2577 uint8_t *output,
2578 size_t output_size,
2579 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002580{
Janos Follath315b51c2018-07-09 16:04:51 +01002581 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2582 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002583 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002584
mohammad1603503973b2018-03-12 15:59:30 +02002585 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002586 {
Janos Follath315b51c2018-07-09 16:04:51 +01002587 status = PSA_ERROR_BAD_STATE;
2588 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002589 }
2590 if( operation->iv_required && ! operation->iv_set )
2591 {
Janos Follath315b51c2018-07-09 16:04:51 +01002592 status = PSA_ERROR_BAD_STATE;
2593 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002594 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002595 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2596 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002597 {
Gilles Peskine53514202018-06-06 15:11:46 +02002598 psa_algorithm_t padding_mode =
2599 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002600 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002601 {
Janos Follath315b51c2018-07-09 16:04:51 +01002602 status = PSA_ERROR_TAMPERING_DETECTED;
2603 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002604 }
Gilles Peskine53514202018-06-06 15:11:46 +02002605 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002606 {
2607 if( operation->ctx.cipher.unprocessed_len != 0 )
2608 {
Janos Follath315b51c2018-07-09 16:04:51 +01002609 status = PSA_ERROR_INVALID_ARGUMENT;
2610 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002611 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002612 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002613 }
2614
Janos Follath315b51c2018-07-09 16:04:51 +01002615 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2616 temp_output_buffer,
2617 output_length );
2618 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002619 {
Janos Follath315b51c2018-07-09 16:04:51 +01002620 status = mbedtls_to_psa_error( cipher_ret );
2621 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002622 }
Janos Follath315b51c2018-07-09 16:04:51 +01002623
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002624 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002625 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002626 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002627 memcpy( output, temp_output_buffer, *output_length );
2628 else
2629 {
Janos Follath315b51c2018-07-09 16:04:51 +01002630 status = PSA_ERROR_BUFFER_TOO_SMALL;
2631 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002632 }
mohammad1603503973b2018-03-12 15:59:30 +02002633
Janos Follath279ab8e2018-07-09 16:13:21 +01002634 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002635 status = psa_cipher_abort( operation );
2636
2637 return( status );
2638
2639error:
2640
2641 *output_length = 0;
2642
Janos Follath279ab8e2018-07-09 16:13:21 +01002643 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002644 (void) psa_cipher_abort( operation );
2645
2646 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002647}
2648
Gilles Peskinee553c652018-06-04 16:22:46 +02002649psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2650{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002651 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002652 {
2653 /* The object has (apparently) been initialized but it is not
2654 * in use. It's ok to call abort on such an object, and there's
2655 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002656 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002657 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002658
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002659 /* Sanity check (shouldn't happen: operation->alg should
2660 * always have been initialized to a valid value). */
2661 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2662 return( PSA_ERROR_BAD_STATE );
2663
mohammad1603503973b2018-03-12 15:59:30 +02002664 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002665
Moran Peker41deec42018-04-04 15:43:05 +03002666 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002667 operation->key_set = 0;
2668 operation->iv_set = 0;
2669 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002670 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002671 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002672
Moran Peker395db872018-05-31 14:07:14 +03002673 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002674}
2675
Gilles Peskinea0655c32018-04-30 17:06:50 +02002676
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002677
mohammad16038cc1cee2018-03-28 01:21:33 +03002678/****************************************************************/
2679/* Key Policy */
2680/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002681
mohammad160327010052018-07-03 13:16:15 +03002682#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002683void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002684{
Gilles Peskine803ce742018-06-18 16:07:14 +02002685 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002686}
2687
Gilles Peskine2d277862018-06-18 15:41:12 +02002688void psa_key_policy_set_usage( psa_key_policy_t *policy,
2689 psa_key_usage_t usage,
2690 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002691{
mohammad16034eed7572018-03-28 05:14:59 -07002692 policy->usage = usage;
2693 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002694}
2695
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002696psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002697{
mohammad16036df908f2018-04-02 08:34:15 -07002698 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002699}
2700
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002701psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002702{
mohammad16036df908f2018-04-02 08:34:15 -07002703 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002704}
mohammad160327010052018-07-03 13:16:15 +03002705#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002706
Gilles Peskine2d277862018-06-18 15:41:12 +02002707psa_status_t psa_set_key_policy( psa_key_slot_t key,
2708 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002709{
2710 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002711 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002712
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002713 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002714 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002715
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002716 status = psa_get_empty_key_slot( key, &slot );
2717 if( status != PSA_SUCCESS )
2718 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002719
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002720 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2721 PSA_KEY_USAGE_ENCRYPT |
2722 PSA_KEY_USAGE_DECRYPT |
2723 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002724 PSA_KEY_USAGE_VERIFY |
2725 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002726 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002727
mohammad16036df908f2018-04-02 08:34:15 -07002728 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002729
2730 return( PSA_SUCCESS );
2731}
2732
Gilles Peskine2d277862018-06-18 15:41:12 +02002733psa_status_t psa_get_key_policy( psa_key_slot_t key,
2734 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002735{
2736 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002737 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002738
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002739 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002740 return( PSA_ERROR_INVALID_ARGUMENT );
2741
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002742 status = psa_get_key_slot( key, &slot );
2743 if( status != PSA_SUCCESS )
2744 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002745
mohammad16036df908f2018-04-02 08:34:15 -07002746 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002747
2748 return( PSA_SUCCESS );
2749}
Gilles Peskine20035e32018-02-03 22:44:14 +01002750
Gilles Peskinea0655c32018-04-30 17:06:50 +02002751
2752
mohammad1603804cd712018-03-20 22:44:08 +02002753/****************************************************************/
2754/* Key Lifetime */
2755/****************************************************************/
2756
Gilles Peskine2d277862018-06-18 15:41:12 +02002757psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2758 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 Peskineb0b255c2018-07-06 17:01:38 +02002763 status = psa_get_key_slot( key, &slot );
2764 if( status != PSA_SUCCESS )
2765 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002766
mohammad1603804cd712018-03-20 22:44:08 +02002767 *lifetime = slot->lifetime;
2768
2769 return( PSA_SUCCESS );
2770}
2771
Gilles Peskine2d277862018-06-18 15:41:12 +02002772psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002773 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002774{
2775 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002776 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002777
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002778 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2779 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002780 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2781 return( PSA_ERROR_INVALID_ARGUMENT );
2782
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002783 status = psa_get_empty_key_slot( key, &slot );
2784 if( status != PSA_SUCCESS )
2785 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002786
Moran Pekerd7326592018-05-29 16:56:39 +03002787 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002788 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002789
mohammad1603060ad8a2018-03-20 14:28:38 -07002790 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002791
2792 return( PSA_SUCCESS );
2793}
2794
Gilles Peskine20035e32018-02-03 22:44:14 +01002795
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002796
mohammad16035955c982018-04-26 00:53:03 +03002797/****************************************************************/
2798/* AEAD */
2799/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002800
mohammad16035955c982018-04-26 00:53:03 +03002801psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2802 psa_algorithm_t alg,
2803 const uint8_t *nonce,
2804 size_t nonce_length,
2805 const uint8_t *additional_data,
2806 size_t additional_data_length,
2807 const uint8_t *plaintext,
2808 size_t plaintext_length,
2809 uint8_t *ciphertext,
2810 size_t ciphertext_size,
2811 size_t *ciphertext_length )
2812{
2813 int ret;
2814 psa_status_t status;
2815 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002816 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002817 uint8_t *tag;
2818 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002819 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002820 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002821
mohammad1603f08a5502018-06-03 15:05:47 +03002822 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002823
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002824 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2825 if( status != PSA_SUCCESS )
2826 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002827 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002828
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002829 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002830 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002831 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002832 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002833
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002834 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002835 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002836 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002837
mohammad16035955c982018-04-26 00:53:03 +03002838 if( alg == PSA_ALG_GCM )
2839 {
2840 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002841 tag_length = 16;
2842
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002843 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002844 return( PSA_ERROR_INVALID_ARGUMENT );
2845
mohammad160315223a82018-06-03 17:19:55 +03002846 //make sure we have place to hold the tag in the ciphertext buffer
2847 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002848 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002849
2850 //update the tag pointer to point to the end of the ciphertext_length
2851 tag = ciphertext + plaintext_length;
2852
mohammad16035955c982018-04-26 00:53:03 +03002853 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002854 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002855 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002856 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002857 if( ret != 0 )
2858 {
2859 mbedtls_gcm_free( &gcm );
2860 return( mbedtls_to_psa_error( ret ) );
2861 }
2862 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002863 plaintext_length, nonce,
2864 nonce_length, additional_data,
2865 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002866 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002867 mbedtls_gcm_free( &gcm );
2868 }
2869 else if( alg == PSA_ALG_CCM )
2870 {
2871 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002872 tag_length = 16;
2873
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002874 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002875 return( PSA_ERROR_INVALID_ARGUMENT );
2876
mohammad160347ddf3d2018-04-26 01:11:21 +03002877 if( nonce_length < 7 || nonce_length > 13 )
2878 return( PSA_ERROR_INVALID_ARGUMENT );
2879
mohammad160315223a82018-06-03 17:19:55 +03002880 //make sure we have place to hold the tag in the ciphertext buffer
2881 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002882 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002883
2884 //update the tag pointer to point to the end of the ciphertext_length
2885 tag = ciphertext + plaintext_length;
2886
mohammad16035955c982018-04-26 00:53:03 +03002887 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002888 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002889 slot->data.raw.data,
2890 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002891 if( ret != 0 )
2892 {
2893 mbedtls_ccm_free( &ccm );
2894 return( mbedtls_to_psa_error( ret ) );
2895 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002896 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002897 nonce, nonce_length,
2898 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002899 additional_data_length,
2900 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002901 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002902 mbedtls_ccm_free( &ccm );
2903 }
mohammad16035c8845f2018-05-09 05:40:09 -07002904 else
2905 {
mohammad1603554faad2018-06-03 15:07:38 +03002906 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002907 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002908
mohammad160315223a82018-06-03 17:19:55 +03002909 if( ret != 0 )
2910 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002911 /* If ciphertext_size is 0 then ciphertext may be NULL and then the
2912 * call to memset would have undefined behavior. */
2913 if( ciphertext_size != 0 )
2914 memset( ciphertext, 0, ciphertext_size );
mohammad160315223a82018-06-03 17:19:55 +03002915 return( mbedtls_to_psa_error( ret ) );
2916 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002917
mohammad160315223a82018-06-03 17:19:55 +03002918 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002919 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002920}
2921
Gilles Peskineee652a32018-06-01 19:23:52 +02002922/* Locate the tag in a ciphertext buffer containing the encrypted data
2923 * followed by the tag. Return the length of the part preceding the tag in
2924 * *plaintext_length. This is the size of the plaintext in modes where
2925 * the encrypted data has the same size as the plaintext, such as
2926 * CCM and GCM. */
2927static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2928 const uint8_t *ciphertext,
2929 size_t ciphertext_length,
2930 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002931 const uint8_t **p_tag )
2932{
2933 size_t payload_length;
2934 if( tag_length > ciphertext_length )
2935 return( PSA_ERROR_INVALID_ARGUMENT );
2936 payload_length = ciphertext_length - tag_length;
2937 if( payload_length > plaintext_size )
2938 return( PSA_ERROR_BUFFER_TOO_SMALL );
2939 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002940 return( PSA_SUCCESS );
2941}
2942
mohammad16035955c982018-04-26 00:53:03 +03002943psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2944 psa_algorithm_t alg,
2945 const uint8_t *nonce,
2946 size_t nonce_length,
2947 const uint8_t *additional_data,
2948 size_t additional_data_length,
2949 const uint8_t *ciphertext,
2950 size_t ciphertext_length,
2951 uint8_t *plaintext,
2952 size_t plaintext_size,
2953 size_t *plaintext_length )
2954{
2955 int ret;
2956 psa_status_t status;
2957 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002958 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002959 const uint8_t *tag;
2960 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002961 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002962 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002963
Gilles Peskineee652a32018-06-01 19:23:52 +02002964 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002965
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002966 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2967 if( status != PSA_SUCCESS )
2968 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002969 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002970
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002971 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002972 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002973 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002974 return( PSA_ERROR_NOT_SUPPORTED );
2975
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002976 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002977 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002978 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002979
mohammad16035955c982018-04-26 00:53:03 +03002980 if( alg == PSA_ALG_GCM )
2981 {
2982 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002983
Gilles Peskineee652a32018-06-01 19:23:52 +02002984 tag_length = 16;
2985 status = psa_aead_unpadded_locate_tag( tag_length,
2986 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002987 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002988 if( status != PSA_SUCCESS )
2989 return( status );
2990
mohammad16035955c982018-04-26 00:53:03 +03002991 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002992 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002993 slot->data.raw.data,
2994 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002995 if( ret != 0 )
2996 {
2997 mbedtls_gcm_free( &gcm );
2998 return( mbedtls_to_psa_error( ret ) );
2999 }
mohammad16035955c982018-04-26 00:53:03 +03003000
Gilles Peskineee652a32018-06-01 19:23:52 +02003001 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03003002 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003003 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03003004 additional_data,
3005 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003006 tag, tag_length,
3007 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03003008 mbedtls_gcm_free( &gcm );
3009 }
3010 else if( alg == PSA_ALG_CCM )
3011 {
3012 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02003013
mohammad160347ddf3d2018-04-26 01:11:21 +03003014 if( nonce_length < 7 || nonce_length > 13 )
3015 return( PSA_ERROR_INVALID_ARGUMENT );
3016
mohammad16039375f842018-06-03 14:28:24 +03003017 tag_length = 16;
3018 status = psa_aead_unpadded_locate_tag( tag_length,
3019 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003020 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003021 if( status != PSA_SUCCESS )
3022 return( status );
3023
mohammad16035955c982018-04-26 00:53:03 +03003024 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02003025 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003026 slot->data.raw.data,
3027 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03003028 if( ret != 0 )
3029 {
3030 mbedtls_ccm_free( &ccm );
3031 return( mbedtls_to_psa_error( ret ) );
3032 }
mohammad160360a64d02018-06-03 17:20:42 +03003033 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003034 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003035 additional_data,
3036 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003037 ciphertext, plaintext,
3038 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03003039 mbedtls_ccm_free( &ccm );
3040 }
mohammad160339574652018-06-01 04:39:53 -07003041 else
3042 {
mohammad1603554faad2018-06-03 15:07:38 +03003043 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003044 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003045
Gilles Peskineee652a32018-06-01 19:23:52 +02003046 if( ret != 0 )
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003047 {
3048 /* If plaintext_size is 0 then plaintext may be NULL and then the
3049 * call to memset has undefined behavior. */
3050 if( plaintext_size != 0 )
3051 memset( plaintext, 0, plaintext_size );
3052 }
mohammad160360a64d02018-06-03 17:20:42 +03003053 else
3054 *plaintext_length = ciphertext_length - tag_length;
3055
Gilles Peskineee652a32018-06-01 19:23:52 +02003056 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03003057}
3058
Gilles Peskinea0655c32018-04-30 17:06:50 +02003059
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003060
Gilles Peskine20035e32018-02-03 22:44:14 +01003061/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003062/* Generators */
3063/****************************************************************/
3064
3065psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3066{
3067 psa_status_t status = PSA_SUCCESS;
3068 if( generator->alg == 0 )
3069 {
3070 /* The object has (apparently) been initialized but it is not
3071 * in use. It's ok to call abort on such an object, and there's
3072 * nothing to do. */
3073 }
3074 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003075#if defined(MBEDTLS_MD_C)
3076 if( PSA_ALG_IS_HKDF( generator->alg ) )
3077 {
3078 mbedtls_free( generator->ctx.hkdf.info );
3079 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3080 }
3081 else
3082#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003083 {
3084 status = PSA_ERROR_BAD_STATE;
3085 }
3086 memset( generator, 0, sizeof( *generator ) );
3087 return( status );
3088}
3089
3090
3091psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3092 size_t *capacity)
3093{
3094 *capacity = generator->capacity;
3095 return( PSA_SUCCESS );
3096}
3097
Gilles Peskinebef7f142018-07-12 17:22:21 +02003098#if defined(MBEDTLS_MD_C)
3099/* Read some bytes from an HKDF-based generator. This performs a chunk
3100 * of the expand phase of the HKDF algorithm. */
3101static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3102 psa_algorithm_t hash_alg,
3103 uint8_t *output,
3104 size_t output_length )
3105{
3106 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3107 psa_status_t status;
3108
3109 while( output_length != 0 )
3110 {
3111 /* Copy what remains of the current block */
3112 uint8_t n = hash_length - hkdf->offset_in_block;
3113 if( n > output_length )
3114 n = (uint8_t) output_length;
3115 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3116 output += n;
3117 output_length -= n;
3118 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003119 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003120 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003121 /* We can't be wanting more output after block 0xff, otherwise
3122 * the capacity check in psa_generator_read() would have
3123 * prevented this call. It could happen only if the generator
3124 * object was corrupted or if this function is called directly
3125 * inside the library. */
3126 if( hkdf->block_number == 0xff )
3127 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003128
3129 /* We need a new block */
3130 ++hkdf->block_number;
3131 hkdf->offset_in_block = 0;
3132 status = psa_hmac_setup_internal( &hkdf->hmac,
3133 hkdf->prk, hash_length,
3134 hash_alg );
3135 if( status != PSA_SUCCESS )
3136 return( status );
3137 if( hkdf->block_number != 1 )
3138 {
3139 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3140 hkdf->output_block,
3141 hash_length );
3142 if( status != PSA_SUCCESS )
3143 return( status );
3144 }
3145 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3146 hkdf->info,
3147 hkdf->info_length );
3148 if( status != PSA_SUCCESS )
3149 return( status );
3150 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3151 &hkdf->block_number, 1 );
3152 if( status != PSA_SUCCESS )
3153 return( status );
3154 status = psa_hmac_finish_internal( &hkdf->hmac,
3155 hkdf->output_block,
3156 sizeof( hkdf->output_block ) );
3157 if( status != PSA_SUCCESS )
3158 return( status );
3159 }
3160
3161 return( PSA_SUCCESS );
3162}
3163#endif /* MBEDTLS_MD_C */
3164
Gilles Peskineeab56e42018-07-12 17:12:33 +02003165psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3166 uint8_t *output,
3167 size_t output_length )
3168{
3169 psa_status_t status;
3170
3171 if( output_length > generator->capacity )
3172 {
3173 generator->capacity = 0;
3174 /* Go through the error path to wipe all confidential data now
3175 * that the generator object is useless. */
3176 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3177 goto exit;
3178 }
3179 if( output_length == 0 &&
3180 generator->capacity == 0 && generator->alg == 0 )
3181 {
3182 /* Edge case: this is a blank or finished generator, and 0
3183 * bytes were requested. The right error in this case could
3184 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3185 * INSUFFICIENT_CAPACITY, which is right for a finished
3186 * generator, for consistency with the case when
3187 * output_length > 0. */
3188 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3189 }
3190 generator->capacity -= output_length;
3191
Gilles Peskinebef7f142018-07-12 17:22:21 +02003192#if defined(MBEDTLS_MD_C)
3193 if( PSA_ALG_IS_HKDF( generator->alg ) )
3194 {
3195 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3196 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3197 output, output_length );
3198 }
3199 else
3200#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003201 {
3202 return( PSA_ERROR_BAD_STATE );
3203 }
3204
3205exit:
3206 if( status != PSA_SUCCESS )
3207 {
3208 psa_generator_abort( generator );
3209 memset( output, '!', output_length );
3210 }
3211 return( status );
3212}
3213
Gilles Peskine08542d82018-07-19 17:05:42 +02003214#if defined(MBEDTLS_DES_C)
3215static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3216{
3217 if( data_size >= 8 )
3218 mbedtls_des_key_set_parity( data );
3219 if( data_size >= 16 )
3220 mbedtls_des_key_set_parity( data + 8 );
3221 if( data_size >= 24 )
3222 mbedtls_des_key_set_parity( data + 16 );
3223}
3224#endif /* MBEDTLS_DES_C */
3225
Gilles Peskineeab56e42018-07-12 17:12:33 +02003226psa_status_t psa_generator_import_key( psa_key_slot_t key,
3227 psa_key_type_t type,
3228 size_t bits,
3229 psa_crypto_generator_t *generator )
3230{
3231 uint8_t *data = NULL;
3232 size_t bytes = PSA_BITS_TO_BYTES( bits );
3233 psa_status_t status;
3234
3235 if( ! key_type_is_raw_bytes( type ) )
3236 return( PSA_ERROR_INVALID_ARGUMENT );
3237 if( bits % 8 != 0 )
3238 return( PSA_ERROR_INVALID_ARGUMENT );
3239 data = mbedtls_calloc( 1, bytes );
3240 if( data == NULL )
3241 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3242
3243 status = psa_generator_read( generator, data, bytes );
3244 if( status != PSA_SUCCESS )
3245 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003246#if defined(MBEDTLS_DES_C)
3247 if( type == PSA_KEY_TYPE_DES )
3248 psa_des_set_key_parity( data, bytes );
3249#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003250 status = psa_import_key( key, type, data, bytes );
3251
3252exit:
3253 mbedtls_free( data );
3254 return( status );
3255}
3256
3257
3258
3259/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003260/* Key derivation */
3261/****************************************************************/
3262
Gilles Peskinebef7f142018-07-12 17:22:21 +02003263/* Set up an HKDF-based generator. This is exactly the extract phase
3264 * of the HKDF algorithm. */
3265static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
3266 key_slot_t *slot,
3267 psa_algorithm_t hash_alg,
3268 const uint8_t *salt,
3269 size_t salt_length,
3270 const uint8_t *label,
3271 size_t label_length )
3272{
3273 psa_status_t status;
3274 status = psa_hmac_setup_internal( &hkdf->hmac,
3275 salt, salt_length,
3276 PSA_ALG_HMAC_HASH( hash_alg ) );
3277 if( status != PSA_SUCCESS )
3278 return( status );
3279 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3280 slot->data.raw.data,
3281 slot->data.raw.bytes );
3282 if( status != PSA_SUCCESS )
3283 return( status );
3284 status = psa_hmac_finish_internal( &hkdf->hmac,
3285 hkdf->prk,
3286 sizeof( hkdf->prk ) );
3287 if( status != PSA_SUCCESS )
3288 return( status );
3289 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3290 hkdf->block_number = 0;
3291 hkdf->info_length = label_length;
3292 if( label_length != 0 )
3293 {
3294 hkdf->info = mbedtls_calloc( 1, label_length );
3295 if( hkdf->info == NULL )
3296 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3297 memcpy( hkdf->info, label, label_length );
3298 }
3299 return( PSA_SUCCESS );
3300}
3301
Gilles Peskineea0fb492018-07-12 17:17:20 +02003302psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01003303 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003304 psa_algorithm_t alg,
3305 const uint8_t *salt,
3306 size_t salt_length,
3307 const uint8_t *label,
3308 size_t label_length,
3309 size_t capacity )
3310{
3311 key_slot_t *slot;
3312 psa_status_t status;
3313
3314 if( generator->alg != 0 )
3315 return( PSA_ERROR_BAD_STATE );
3316
3317 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
3318 if( status != PSA_SUCCESS )
3319 return( status );
3320 if( slot->type != PSA_KEY_TYPE_DERIVE )
3321 return( PSA_ERROR_INVALID_ARGUMENT );
3322
3323 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
3324 return( PSA_ERROR_INVALID_ARGUMENT );
3325
Gilles Peskinebef7f142018-07-12 17:22:21 +02003326#if defined(MBEDTLS_MD_C)
3327 if( PSA_ALG_IS_HKDF( alg ) )
3328 {
3329 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3330 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3331 if( hash_size == 0 )
3332 return( PSA_ERROR_NOT_SUPPORTED );
3333 if( capacity > 255 * hash_size )
3334 return( PSA_ERROR_INVALID_ARGUMENT );
3335 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3336 slot,
3337 hash_alg,
3338 salt, salt_length,
3339 label, label_length );
3340 }
3341 else
3342#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +02003343 {
3344 return( PSA_ERROR_NOT_SUPPORTED );
3345 }
3346
3347 /* Set generator->alg even on failure so that abort knows what to do. */
3348 generator->alg = alg;
3349 if( status == PSA_SUCCESS )
3350 generator->capacity = capacity;
3351 else
3352 psa_generator_abort( generator );
3353 return( status );
3354}
3355
3356
3357
3358/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003359/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02003360/****************************************************************/
3361
3362psa_status_t psa_generate_random( uint8_t *output,
3363 size_t output_size )
3364{
3365 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3366 output, output_size );
3367 return( mbedtls_to_psa_error( ret ) );
3368}
3369
3370psa_status_t psa_generate_key( psa_key_slot_t key,
3371 psa_key_type_t type,
3372 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003373 const void *extra,
3374 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02003375{
Gilles Peskine12313cd2018-06-20 00:20:32 +02003376 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003377 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003378
Gilles Peskine53d991e2018-07-12 01:14:59 +02003379 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003380 return( PSA_ERROR_INVALID_ARGUMENT );
3381
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003382 status = psa_get_empty_key_slot( key, &slot );
3383 if( status != PSA_SUCCESS )
3384 return( status );
3385
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003386 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003387 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003388 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003389 if( status != PSA_SUCCESS )
3390 return( status );
3391 status = psa_generate_random( slot->data.raw.data,
3392 slot->data.raw.bytes );
3393 if( status != PSA_SUCCESS )
3394 {
3395 mbedtls_free( slot->data.raw.data );
3396 return( status );
3397 }
3398#if defined(MBEDTLS_DES_C)
3399 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02003400 psa_des_set_key_parity( slot->data.raw.data,
3401 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003402#endif /* MBEDTLS_DES_C */
3403 }
3404 else
3405
3406#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3407 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3408 {
3409 mbedtls_rsa_context *rsa;
3410 int ret;
3411 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003412 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3413 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02003414 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003415 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02003416 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003417 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003418 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003419#if INT_MAX < 0xffffffff
3420 /* Check that the uint32_t value passed by the caller fits
3421 * in the range supported by this implementation. */
3422 if( p->e > INT_MAX )
3423 return( PSA_ERROR_NOT_SUPPORTED );
3424#endif
3425 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003426 }
3427 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3428 if( rsa == NULL )
3429 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3430 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3431 ret = mbedtls_rsa_gen_key( rsa,
3432 mbedtls_ctr_drbg_random,
3433 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003434 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003435 exponent );
3436 if( ret != 0 )
3437 {
3438 mbedtls_rsa_free( rsa );
3439 mbedtls_free( rsa );
3440 return( mbedtls_to_psa_error( ret ) );
3441 }
3442 slot->data.rsa = rsa;
3443 }
3444 else
3445#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3446
3447#if defined(MBEDTLS_ECP_C)
3448 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3449 {
3450 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3451 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3452 const mbedtls_ecp_curve_info *curve_info =
3453 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3454 mbedtls_ecp_keypair *ecp;
3455 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003456 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003457 return( PSA_ERROR_NOT_SUPPORTED );
3458 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3459 return( PSA_ERROR_NOT_SUPPORTED );
3460 if( curve_info->bit_size != bits )
3461 return( PSA_ERROR_INVALID_ARGUMENT );
3462 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3463 if( ecp == NULL )
3464 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3465 mbedtls_ecp_keypair_init( ecp );
3466 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3467 mbedtls_ctr_drbg_random,
3468 &global_data.ctr_drbg );
3469 if( ret != 0 )
3470 {
3471 mbedtls_ecp_keypair_free( ecp );
3472 mbedtls_free( ecp );
3473 return( mbedtls_to_psa_error( ret ) );
3474 }
3475 slot->data.ecp = ecp;
3476 }
3477 else
3478#endif /* MBEDTLS_ECP_C */
3479
3480 return( PSA_ERROR_NOT_SUPPORTED );
3481
3482 slot->type = type;
3483 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003484}
3485
3486
3487/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003488/* Module setup */
3489/****************************************************************/
3490
Gilles Peskinee59236f2018-01-27 23:32:46 +01003491void mbedtls_psa_crypto_free( void )
3492{
Jaeden Amero045bd502018-06-26 14:00:08 +01003493 psa_key_slot_t key;
Gilles Peskine9a056342018-08-01 15:46:54 +02003494 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003495 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003496 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3497 mbedtls_entropy_free( &global_data.entropy );
3498 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3499}
3500
3501psa_status_t psa_crypto_init( void )
3502{
3503 int ret;
3504 const unsigned char drbg_seed[] = "PSA";
3505
3506 if( global_data.initialized != 0 )
3507 return( PSA_SUCCESS );
3508
3509 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3510 mbedtls_entropy_init( &global_data.entropy );
3511 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3512
3513 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3514 mbedtls_entropy_func,
3515 &global_data.entropy,
3516 drbg_seed, sizeof( drbg_seed ) - 1 );
3517 if( ret != 0 )
3518 goto exit;
3519
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003520 global_data.initialized = 1;
3521
Gilles Peskinee59236f2018-01-27 23:32:46 +01003522exit:
3523 if( ret != 0 )
3524 mbedtls_psa_crypto_free( );
3525 return( mbedtls_to_psa_error( ret ) );
3526}
3527
3528#endif /* MBEDTLS_PSA_CRYPTO_C */