blob: 8aa3145bd85e2627476980ae680d7bc008814640 [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{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200138 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200139}
140
Gilles Peskine2d277862018-06-18 15:41:12 +0200141typedef struct
142{
Gilles Peskinee59236f2018-01-27 23:32:46 +0100143 int initialized;
144 mbedtls_entropy_context entropy;
145 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200146 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinee59236f2018-01-27 23:32:46 +0100147} psa_global_data_t;
148
149static psa_global_data_t global_data;
150
itayzafrir0adf0fc2018-09-06 16:24:41 +0300151#define GUARD_MODULE_INITIALIZED \
152 if( global_data.initialized == 0 ) \
153 return( PSA_ERROR_BAD_STATE );
154
Gilles Peskinee59236f2018-01-27 23:32:46 +0100155static psa_status_t mbedtls_to_psa_error( int ret )
156{
Gilles Peskinea5905292018-02-07 20:59:33 +0100157 /* If there's both a high-level code and low-level code, dispatch on
158 * the high-level code. */
159 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100160 {
161 case 0:
162 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100163
164 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
165 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
166 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
167 return( PSA_ERROR_NOT_SUPPORTED );
168 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
169 return( PSA_ERROR_HARDWARE_FAILURE );
170
171 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
172 return( PSA_ERROR_HARDWARE_FAILURE );
173
Gilles Peskine9a944802018-06-21 09:35:35 +0200174 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
175 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
176 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
177 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
178 case MBEDTLS_ERR_ASN1_INVALID_DATA:
179 return( PSA_ERROR_INVALID_ARGUMENT );
180 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
181 return( PSA_ERROR_INSUFFICIENT_MEMORY );
182 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
183 return( PSA_ERROR_BUFFER_TOO_SMALL );
184
Gilles Peskinea5905292018-02-07 20:59:33 +0100185 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
186 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
187 return( PSA_ERROR_NOT_SUPPORTED );
188 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
189 return( PSA_ERROR_HARDWARE_FAILURE );
190
191 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
192 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
193 return( PSA_ERROR_NOT_SUPPORTED );
194 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
195 return( PSA_ERROR_HARDWARE_FAILURE );
196
197 case MBEDTLS_ERR_CCM_BAD_INPUT:
198 return( PSA_ERROR_INVALID_ARGUMENT );
199 case MBEDTLS_ERR_CCM_AUTH_FAILED:
200 return( PSA_ERROR_INVALID_SIGNATURE );
201 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
202 return( PSA_ERROR_HARDWARE_FAILURE );
203
204 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
205 return( PSA_ERROR_NOT_SUPPORTED );
206 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
207 return( PSA_ERROR_INVALID_ARGUMENT );
208 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
209 return( PSA_ERROR_INSUFFICIENT_MEMORY );
210 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
211 return( PSA_ERROR_INVALID_PADDING );
212 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
213 return( PSA_ERROR_BAD_STATE );
214 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
215 return( PSA_ERROR_INVALID_SIGNATURE );
216 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
217 return( PSA_ERROR_TAMPERING_DETECTED );
218 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
219 return( PSA_ERROR_HARDWARE_FAILURE );
220
221 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
222 return( PSA_ERROR_HARDWARE_FAILURE );
223
224 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
225 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
226 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
227 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
228 return( PSA_ERROR_NOT_SUPPORTED );
229 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
230 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
231
232 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
233 return( PSA_ERROR_NOT_SUPPORTED );
234 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
235 return( PSA_ERROR_HARDWARE_FAILURE );
236
Gilles Peskinee59236f2018-01-27 23:32:46 +0100237 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
238 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
239 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
240 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100241
242 case MBEDTLS_ERR_GCM_AUTH_FAILED:
243 return( PSA_ERROR_INVALID_SIGNATURE );
244 case MBEDTLS_ERR_GCM_BAD_INPUT:
245 return( PSA_ERROR_NOT_SUPPORTED );
246 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
247 return( PSA_ERROR_HARDWARE_FAILURE );
248
249 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
250 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
251 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
254 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
255 return( PSA_ERROR_NOT_SUPPORTED );
256 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MD_ALLOC_FAILED:
259 return( PSA_ERROR_INSUFFICIENT_MEMORY );
260 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
261 return( PSA_ERROR_STORAGE_FAILURE );
262 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
263 return( PSA_ERROR_HARDWARE_FAILURE );
264
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100265 case MBEDTLS_ERR_PK_ALLOC_FAILED:
266 return( PSA_ERROR_INSUFFICIENT_MEMORY );
267 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
268 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100271 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100272 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
273 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
276 return( PSA_ERROR_NOT_SUPPORTED );
277 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
278 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
279 return( PSA_ERROR_NOT_PERMITTED );
280 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
281 return( PSA_ERROR_INVALID_ARGUMENT );
282 case MBEDTLS_ERR_PK_INVALID_ALG:
283 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
284 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
285 return( PSA_ERROR_NOT_SUPPORTED );
286 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
287 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100288 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
289 return( PSA_ERROR_HARDWARE_FAILURE );
290
291 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
292 return( PSA_ERROR_HARDWARE_FAILURE );
293
294 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
295 return( PSA_ERROR_INVALID_ARGUMENT );
296 case MBEDTLS_ERR_RSA_INVALID_PADDING:
297 return( PSA_ERROR_INVALID_PADDING );
298 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
299 return( PSA_ERROR_HARDWARE_FAILURE );
300 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
301 return( PSA_ERROR_INVALID_ARGUMENT );
302 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
303 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
304 return( PSA_ERROR_TAMPERING_DETECTED );
305 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
306 return( PSA_ERROR_INVALID_SIGNATURE );
307 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
308 return( PSA_ERROR_BUFFER_TOO_SMALL );
309 case MBEDTLS_ERR_RSA_RNG_FAILED:
310 return( PSA_ERROR_INSUFFICIENT_MEMORY );
311 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
312 return( PSA_ERROR_NOT_SUPPORTED );
313 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
314 return( PSA_ERROR_HARDWARE_FAILURE );
315
316 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
317 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
318 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
319 return( PSA_ERROR_HARDWARE_FAILURE );
320
321 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
322 return( PSA_ERROR_INVALID_ARGUMENT );
323 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
324 return( PSA_ERROR_HARDWARE_FAILURE );
325
itayzafrir5c753392018-05-08 11:18:38 +0300326 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300327 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300328 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300329 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
330 return( PSA_ERROR_BUFFER_TOO_SMALL );
331 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
332 return( PSA_ERROR_NOT_SUPPORTED );
333 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
334 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
335 return( PSA_ERROR_INVALID_SIGNATURE );
336 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
337 return( PSA_ERROR_INSUFFICIENT_MEMORY );
338 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
339 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300340
Gilles Peskinee59236f2018-01-27 23:32:46 +0100341 default:
342 return( PSA_ERROR_UNKNOWN_ERROR );
343 }
344}
345
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200346/* Retrieve a key slot, occupied or not. */
347static psa_status_t psa_get_key_slot( psa_key_slot_t key,
348 key_slot_t **p_slot )
349{
itayzafrir90d8c7a2018-09-12 11:44:52 +0300350 GUARD_MODULE_INITIALIZED;
351
Gilles Peskine996deb12018-08-01 15:45:45 +0200352 /* 0 is not a valid slot number under any circumstance. This
353 * implementation provides slots number 1 to N where N is the
354 * number of available slots. */
355 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200356 return( PSA_ERROR_INVALID_ARGUMENT );
357
Gilles Peskine996deb12018-08-01 15:45:45 +0200358 *p_slot = &global_data.key_slots[key - 1];
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200359 return( PSA_SUCCESS );
360}
361
362/* Retrieve an empty key slot (slot with no key data, but possibly
363 * with some metadata such as a policy). */
364static psa_status_t psa_get_empty_key_slot( psa_key_slot_t key,
365 key_slot_t **p_slot )
366{
367 psa_status_t status;
368 key_slot_t *slot = NULL;
369
370 *p_slot = NULL;
371
372 status = psa_get_key_slot( key, &slot );
373 if( status != PSA_SUCCESS )
374 return( status );
375
376 if( slot->type != PSA_KEY_TYPE_NONE )
377 return( PSA_ERROR_OCCUPIED_SLOT );
378
379 *p_slot = slot;
380 return( status );
381}
382
Jaeden Amerob4fa8c92018-07-11 15:57:44 +0100383/** Retrieve a slot which must contain a key. The key must have allow all the
384 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
385 * operations with this algorithm. */
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200386static psa_status_t psa_get_key_from_slot( psa_key_slot_t key,
387 key_slot_t **p_slot,
388 psa_key_usage_t usage,
389 psa_algorithm_t alg )
390{
391 psa_status_t status;
392 key_slot_t *slot = NULL;
393
394 *p_slot = NULL;
395
396 status = psa_get_key_slot( key, &slot );
397 if( status != PSA_SUCCESS )
398 return( status );
399 if( slot->type == PSA_KEY_TYPE_NONE )
400 return( PSA_ERROR_EMPTY_SLOT );
401
402 /* Enforce that usage policy for the key slot contains all the flags
403 * required by the usage parameter. There is one exception: public
404 * keys can always be exported, so we treat public key objects as
405 * if they had the export flag. */
406 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
407 usage &= ~PSA_KEY_USAGE_EXPORT;
408 if( ( slot->policy.usage & usage ) != usage )
409 return( PSA_ERROR_NOT_PERMITTED );
410 if( alg != 0 && ( alg != slot->policy.alg ) )
411 return( PSA_ERROR_NOT_PERMITTED );
412
413 *p_slot = slot;
414 return( PSA_SUCCESS );
415}
416
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200417
418
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100419/****************************************************************/
420/* Key management */
421/****************************************************************/
422
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100423#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200424static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
425{
426 switch( grpid )
427 {
428 case MBEDTLS_ECP_DP_SECP192R1:
429 return( PSA_ECC_CURVE_SECP192R1 );
430 case MBEDTLS_ECP_DP_SECP224R1:
431 return( PSA_ECC_CURVE_SECP224R1 );
432 case MBEDTLS_ECP_DP_SECP256R1:
433 return( PSA_ECC_CURVE_SECP256R1 );
434 case MBEDTLS_ECP_DP_SECP384R1:
435 return( PSA_ECC_CURVE_SECP384R1 );
436 case MBEDTLS_ECP_DP_SECP521R1:
437 return( PSA_ECC_CURVE_SECP521R1 );
438 case MBEDTLS_ECP_DP_BP256R1:
439 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
440 case MBEDTLS_ECP_DP_BP384R1:
441 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
442 case MBEDTLS_ECP_DP_BP512R1:
443 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
444 case MBEDTLS_ECP_DP_CURVE25519:
445 return( PSA_ECC_CURVE_CURVE25519 );
446 case MBEDTLS_ECP_DP_SECP192K1:
447 return( PSA_ECC_CURVE_SECP192K1 );
448 case MBEDTLS_ECP_DP_SECP224K1:
449 return( PSA_ECC_CURVE_SECP224K1 );
450 case MBEDTLS_ECP_DP_SECP256K1:
451 return( PSA_ECC_CURVE_SECP256K1 );
452 case MBEDTLS_ECP_DP_CURVE448:
453 return( PSA_ECC_CURVE_CURVE448 );
454 default:
455 return( 0 );
456 }
457}
458
Gilles Peskine12313cd2018-06-20 00:20:32 +0200459static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
460{
461 switch( curve )
462 {
463 case PSA_ECC_CURVE_SECP192R1:
464 return( MBEDTLS_ECP_DP_SECP192R1 );
465 case PSA_ECC_CURVE_SECP224R1:
466 return( MBEDTLS_ECP_DP_SECP224R1 );
467 case PSA_ECC_CURVE_SECP256R1:
468 return( MBEDTLS_ECP_DP_SECP256R1 );
469 case PSA_ECC_CURVE_SECP384R1:
470 return( MBEDTLS_ECP_DP_SECP384R1 );
471 case PSA_ECC_CURVE_SECP521R1:
472 return( MBEDTLS_ECP_DP_SECP521R1 );
473 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
474 return( MBEDTLS_ECP_DP_BP256R1 );
475 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
476 return( MBEDTLS_ECP_DP_BP384R1 );
477 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
478 return( MBEDTLS_ECP_DP_BP512R1 );
479 case PSA_ECC_CURVE_CURVE25519:
480 return( MBEDTLS_ECP_DP_CURVE25519 );
481 case PSA_ECC_CURVE_SECP192K1:
482 return( MBEDTLS_ECP_DP_SECP192K1 );
483 case PSA_ECC_CURVE_SECP224K1:
484 return( MBEDTLS_ECP_DP_SECP224K1 );
485 case PSA_ECC_CURVE_SECP256K1:
486 return( MBEDTLS_ECP_DP_SECP256K1 );
487 case PSA_ECC_CURVE_CURVE448:
488 return( MBEDTLS_ECP_DP_CURVE448 );
489 default:
490 return( MBEDTLS_ECP_DP_NONE );
491 }
492}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100493#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200494
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
496 size_t bits,
497 struct raw_data *raw )
498{
499 /* Check that the bit size is acceptable for the key type */
500 switch( type )
501 {
502 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200503 if( bits == 0 )
504 {
505 raw->bytes = 0;
506 raw->data = NULL;
507 return( PSA_SUCCESS );
508 }
509 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200510#if defined(MBEDTLS_MD_C)
511 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200512#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200513 case PSA_KEY_TYPE_DERIVE:
514 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200515#if defined(MBEDTLS_AES_C)
516 case PSA_KEY_TYPE_AES:
517 if( bits != 128 && bits != 192 && bits != 256 )
518 return( PSA_ERROR_INVALID_ARGUMENT );
519 break;
520#endif
521#if defined(MBEDTLS_CAMELLIA_C)
522 case PSA_KEY_TYPE_CAMELLIA:
523 if( bits != 128 && bits != 192 && bits != 256 )
524 return( PSA_ERROR_INVALID_ARGUMENT );
525 break;
526#endif
527#if defined(MBEDTLS_DES_C)
528 case PSA_KEY_TYPE_DES:
529 if( bits != 64 && bits != 128 && bits != 192 )
530 return( PSA_ERROR_INVALID_ARGUMENT );
531 break;
532#endif
533#if defined(MBEDTLS_ARC4_C)
534 case PSA_KEY_TYPE_ARC4:
535 if( bits < 8 || bits > 2048 )
536 return( PSA_ERROR_INVALID_ARGUMENT );
537 break;
538#endif
539 default:
540 return( PSA_ERROR_NOT_SUPPORTED );
541 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200542 if( bits % 8 != 0 )
543 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200544
545 /* Allocate memory for the key */
546 raw->bytes = PSA_BITS_TO_BYTES( bits );
547 raw->data = mbedtls_calloc( 1, raw->bytes );
548 if( raw->data == NULL )
549 {
550 raw->bytes = 0;
551 return( PSA_ERROR_INSUFFICIENT_MEMORY );
552 }
553 return( PSA_SUCCESS );
554}
555
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200556#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
557static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
558 mbedtls_rsa_context **p_rsa )
559{
560 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
561 return( PSA_ERROR_INVALID_ARGUMENT );
562 else
563 {
564 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
565 size_t bits = mbedtls_rsa_get_bitlen( rsa );
566 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
567 return( PSA_ERROR_NOT_SUPPORTED );
568 *p_rsa = rsa;
569 return( PSA_SUCCESS );
570 }
571}
572#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
573
574#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
575static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
576 mbedtls_pk_context *pk,
577 mbedtls_ecp_keypair **p_ecp )
578{
579 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
580 return( PSA_ERROR_INVALID_ARGUMENT );
581 else
582 {
583 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
584 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
585 if( actual_curve != expected_curve )
586 return( PSA_ERROR_INVALID_ARGUMENT );
587 *p_ecp = ecp;
588 return( PSA_SUCCESS );
589 }
590}
591#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
592
Gilles Peskine2d277862018-06-18 15:41:12 +0200593psa_status_t psa_import_key( psa_key_slot_t key,
594 psa_key_type_t type,
595 const uint8_t *data,
596 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100597{
598 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200599 psa_status_t status = PSA_SUCCESS;
600 status = psa_get_empty_key_slot( key, &slot );
601 if( status != PSA_SUCCESS )
602 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100603
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200604 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100605 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100606 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100607 if( data_length > SIZE_MAX / 8 )
608 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200609 status = prepare_raw_data_slot( type,
610 PSA_BYTES_TO_BITS( data_length ),
611 &slot->data.raw );
612 if( status != PSA_SUCCESS )
613 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200614 if( data_length != 0 )
615 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100616 }
617 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100618#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200619 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100620 {
621 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100622 mbedtls_pk_context pk;
623 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200624
625 /* Parse the data. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100626 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
627 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
628 else
629 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100630 if( ret != 0 )
631 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200632
633 /* We have something that the pkparse module recognizes.
634 * If it has the expected type and passes any type-specific
635 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100636#if defined(MBEDTLS_RSA_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200637 if( PSA_KEY_TYPE_IS_RSA( type ) )
638 status = psa_import_rsa_key( &pk, &slot->data.rsa );
639 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100640#endif /* MBEDTLS_RSA_C */
641#if defined(MBEDTLS_ECP_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200642 if( PSA_KEY_TYPE_IS_ECC( type ) )
643 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( type ),
644 &pk, &slot->data.ecp );
645 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100646#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200647 {
648 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200649 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200650
Gilles Peskinec648d692018-06-28 08:46:13 +0200651 /* Free the content of the pk object only on error. On success,
652 * the content of the object has been stored in the slot. */
653 if( status != PSA_SUCCESS )
654 {
655 mbedtls_pk_free( &pk );
656 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100657 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100658 }
659 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100660#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100661 {
662 return( PSA_ERROR_NOT_SUPPORTED );
663 }
664
665 slot->type = type;
666 return( PSA_SUCCESS );
667}
668
Gilles Peskine2d277862018-06-18 15:41:12 +0200669psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100670{
671 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200672 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200674 status = psa_get_key_slot( key, &slot );
675 if( status != PSA_SUCCESS )
676 return( status );
677
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200679 {
680 /* No key material to clean, but do zeroize the slot below to wipe
681 * metadata such as policies. */
682 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200683 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100684 {
685 mbedtls_free( slot->data.raw.data );
686 }
687 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100688#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200689 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100690 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100691 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100692 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100693 }
694 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100695#endif /* defined(MBEDTLS_RSA_C) */
696#if defined(MBEDTLS_ECP_C)
697 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
698 {
699 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100700 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100701 }
702 else
703#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100704 {
705 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100706 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100707 return( PSA_ERROR_TAMPERING_DETECTED );
708 }
709
710 mbedtls_zeroize( slot, sizeof( *slot ) );
711 return( PSA_SUCCESS );
712}
713
Gilles Peskineb870b182018-07-06 16:02:09 +0200714/* Return the size of the key in the given slot, in bits. */
715static size_t psa_get_key_bits( const key_slot_t *slot )
716{
717 if( key_type_is_raw_bytes( slot->type ) )
718 return( slot->data.raw.bytes * 8 );
719#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200720 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineb870b182018-07-06 16:02:09 +0200721 return( mbedtls_rsa_get_bitlen( slot->data.rsa ) );
722#endif /* defined(MBEDTLS_RSA_C) */
723#if defined(MBEDTLS_ECP_C)
724 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
725 return( slot->data.ecp->grp.pbits );
726#endif /* defined(MBEDTLS_ECP_C) */
727 /* Shouldn't happen except on an empty slot. */
728 return( 0 );
729}
730
Gilles Peskine2d277862018-06-18 15:41:12 +0200731psa_status_t psa_get_key_information( psa_key_slot_t key,
732 psa_key_type_t *type,
733 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100734{
735 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200736 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100737
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200739 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740 if( bits != NULL )
741 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200742 status = psa_get_key_slot( key, &slot );
743 if( status != PSA_SUCCESS )
744 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200745
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746 if( slot->type == PSA_KEY_TYPE_NONE )
747 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200748 if( type != NULL )
749 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200750 if( bits != NULL )
751 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100752 return( PSA_SUCCESS );
753}
754
Gilles Peskine2d277862018-06-18 15:41:12 +0200755static psa_status_t psa_internal_export_key( psa_key_slot_t key,
756 uint8_t *data,
757 size_t data_size,
758 size_t *data_length,
759 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100760{
761 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200762 psa_status_t status;
763 /* Exporting a public key doesn't require a usage flag. If we're
764 * called by psa_export_public_key(), don't require the EXPORT flag.
765 * If we're called by psa_export_key(), do require the EXPORT flag;
766 * if the key turns out to be public key object, psa_get_key_from_slot()
767 * will ignore this flag. */
768 psa_key_usage_t usage = export_public_key ? 0 : PSA_KEY_USAGE_EXPORT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100769
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100770 /* Set the key to empty now, so that even when there are errors, we always
771 * set data_length to a value between 0 and data_size. On error, setting
772 * the key to empty is a good choice because an empty key representation is
773 * unlikely to be accepted anywhere. */
774 *data_length = 0;
775
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200776 status = psa_get_key_from_slot( key, &slot, usage, 0 );
777 if( status != PSA_SUCCESS )
778 return( status );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200779 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300780 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300781
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200782 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100783 {
784 if( slot->data.raw.bytes > data_size )
785 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200786 if( slot->data.raw.bytes != 0 )
787 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100788 *data_length = slot->data.raw.bytes;
789 return( PSA_SUCCESS );
790 }
791 else
Moran Peker17e36e12018-05-02 12:55:20 +0300792 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100793#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200794 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300795 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100796 {
Moran Pekera998bc62018-04-16 18:16:20 +0300797 mbedtls_pk_context pk;
798 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +0200799 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300800 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100801#if defined(MBEDTLS_RSA_C)
802 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300803 pk.pk_info = &mbedtls_rsa_info;
804 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100805#else
806 return( PSA_ERROR_NOT_SUPPORTED );
807#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300808 }
809 else
810 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100811#if defined(MBEDTLS_ECP_C)
812 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300813 pk.pk_info = &mbedtls_eckey_info;
814 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100815#else
816 return( PSA_ERROR_NOT_SUPPORTED );
817#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300818 }
Moran Pekerd7326592018-05-29 16:56:39 +0300819 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300820 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300821 else
822 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300823 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200824 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200825 /* If data_size is 0 then data may be NULL and then the
826 * call to memset would have undefined behavior. */
827 if( data_size != 0 )
828 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300829 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200830 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200831 /* The mbedtls_pk_xxx functions write to the end of the buffer.
832 * Move the data to the beginning and erase remaining data
833 * at the original location. */
834 if( 2 * (size_t) ret <= data_size )
835 {
836 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200837 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200838 }
839 else if( (size_t) ret < data_size )
840 {
841 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200842 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200843 }
Moran Pekera998bc62018-04-16 18:16:20 +0300844 *data_length = ret;
845 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100846 }
847 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100848#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300849 {
850 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200851 it is valid for a special-purpose implementation to omit
852 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300853 return( PSA_ERROR_NOT_SUPPORTED );
854 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100855 }
856}
857
Gilles Peskine2d277862018-06-18 15:41:12 +0200858psa_status_t psa_export_key( psa_key_slot_t key,
859 uint8_t *data,
860 size_t data_size,
861 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300862{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200863 return( psa_internal_export_key( key, data, data_size,
864 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100865}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100866
Gilles Peskine2d277862018-06-18 15:41:12 +0200867psa_status_t psa_export_public_key( psa_key_slot_t key,
868 uint8_t *data,
869 size_t data_size,
870 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300871{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200872 return( psa_internal_export_key( key, data, data_size,
873 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300874}
875
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200876
877
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100878/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100879/* Message digests */
880/****************************************************************/
881
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100882static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100883{
884 switch( alg )
885 {
886#if defined(MBEDTLS_MD2_C)
887 case PSA_ALG_MD2:
888 return( &mbedtls_md2_info );
889#endif
890#if defined(MBEDTLS_MD4_C)
891 case PSA_ALG_MD4:
892 return( &mbedtls_md4_info );
893#endif
894#if defined(MBEDTLS_MD5_C)
895 case PSA_ALG_MD5:
896 return( &mbedtls_md5_info );
897#endif
898#if defined(MBEDTLS_RIPEMD160_C)
899 case PSA_ALG_RIPEMD160:
900 return( &mbedtls_ripemd160_info );
901#endif
902#if defined(MBEDTLS_SHA1_C)
903 case PSA_ALG_SHA_1:
904 return( &mbedtls_sha1_info );
905#endif
906#if defined(MBEDTLS_SHA256_C)
907 case PSA_ALG_SHA_224:
908 return( &mbedtls_sha224_info );
909 case PSA_ALG_SHA_256:
910 return( &mbedtls_sha256_info );
911#endif
912#if defined(MBEDTLS_SHA512_C)
913 case PSA_ALG_SHA_384:
914 return( &mbedtls_sha384_info );
915 case PSA_ALG_SHA_512:
916 return( &mbedtls_sha512_info );
917#endif
918 default:
919 return( NULL );
920 }
921}
922
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100923psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
924{
925 switch( operation->alg )
926 {
Gilles Peskine81736312018-06-26 15:04:31 +0200927 case 0:
928 /* The object has (apparently) been initialized but it is not
929 * in use. It's ok to call abort on such an object, and there's
930 * nothing to do. */
931 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100932#if defined(MBEDTLS_MD2_C)
933 case PSA_ALG_MD2:
934 mbedtls_md2_free( &operation->ctx.md2 );
935 break;
936#endif
937#if defined(MBEDTLS_MD4_C)
938 case PSA_ALG_MD4:
939 mbedtls_md4_free( &operation->ctx.md4 );
940 break;
941#endif
942#if defined(MBEDTLS_MD5_C)
943 case PSA_ALG_MD5:
944 mbedtls_md5_free( &operation->ctx.md5 );
945 break;
946#endif
947#if defined(MBEDTLS_RIPEMD160_C)
948 case PSA_ALG_RIPEMD160:
949 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
950 break;
951#endif
952#if defined(MBEDTLS_SHA1_C)
953 case PSA_ALG_SHA_1:
954 mbedtls_sha1_free( &operation->ctx.sha1 );
955 break;
956#endif
957#if defined(MBEDTLS_SHA256_C)
958 case PSA_ALG_SHA_224:
959 case PSA_ALG_SHA_256:
960 mbedtls_sha256_free( &operation->ctx.sha256 );
961 break;
962#endif
963#if defined(MBEDTLS_SHA512_C)
964 case PSA_ALG_SHA_384:
965 case PSA_ALG_SHA_512:
966 mbedtls_sha512_free( &operation->ctx.sha512 );
967 break;
968#endif
969 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +0200970 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100971 }
972 operation->alg = 0;
973 return( PSA_SUCCESS );
974}
975
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200976psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100977 psa_algorithm_t alg )
978{
979 int ret;
980 operation->alg = 0;
981 switch( alg )
982 {
983#if defined(MBEDTLS_MD2_C)
984 case PSA_ALG_MD2:
985 mbedtls_md2_init( &operation->ctx.md2 );
986 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
987 break;
988#endif
989#if defined(MBEDTLS_MD4_C)
990 case PSA_ALG_MD4:
991 mbedtls_md4_init( &operation->ctx.md4 );
992 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
993 break;
994#endif
995#if defined(MBEDTLS_MD5_C)
996 case PSA_ALG_MD5:
997 mbedtls_md5_init( &operation->ctx.md5 );
998 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
999 break;
1000#endif
1001#if defined(MBEDTLS_RIPEMD160_C)
1002 case PSA_ALG_RIPEMD160:
1003 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1004 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1005 break;
1006#endif
1007#if defined(MBEDTLS_SHA1_C)
1008 case PSA_ALG_SHA_1:
1009 mbedtls_sha1_init( &operation->ctx.sha1 );
1010 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1011 break;
1012#endif
1013#if defined(MBEDTLS_SHA256_C)
1014 case PSA_ALG_SHA_224:
1015 mbedtls_sha256_init( &operation->ctx.sha256 );
1016 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1017 break;
1018 case PSA_ALG_SHA_256:
1019 mbedtls_sha256_init( &operation->ctx.sha256 );
1020 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1021 break;
1022#endif
1023#if defined(MBEDTLS_SHA512_C)
1024 case PSA_ALG_SHA_384:
1025 mbedtls_sha512_init( &operation->ctx.sha512 );
1026 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1027 break;
1028 case PSA_ALG_SHA_512:
1029 mbedtls_sha512_init( &operation->ctx.sha512 );
1030 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1031 break;
1032#endif
1033 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001034 return( PSA_ALG_IS_HASH( alg ) ?
1035 PSA_ERROR_NOT_SUPPORTED :
1036 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001037 }
1038 if( ret == 0 )
1039 operation->alg = alg;
1040 else
1041 psa_hash_abort( operation );
1042 return( mbedtls_to_psa_error( ret ) );
1043}
1044
1045psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1046 const uint8_t *input,
1047 size_t input_length )
1048{
1049 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001050
1051 /* Don't require hash implementations to behave correctly on a
1052 * zero-length input, which may have an invalid pointer. */
1053 if( input_length == 0 )
1054 return( PSA_SUCCESS );
1055
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001056 switch( operation->alg )
1057 {
1058#if defined(MBEDTLS_MD2_C)
1059 case PSA_ALG_MD2:
1060 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1061 input, input_length );
1062 break;
1063#endif
1064#if defined(MBEDTLS_MD4_C)
1065 case PSA_ALG_MD4:
1066 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1067 input, input_length );
1068 break;
1069#endif
1070#if defined(MBEDTLS_MD5_C)
1071 case PSA_ALG_MD5:
1072 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1073 input, input_length );
1074 break;
1075#endif
1076#if defined(MBEDTLS_RIPEMD160_C)
1077 case PSA_ALG_RIPEMD160:
1078 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1079 input, input_length );
1080 break;
1081#endif
1082#if defined(MBEDTLS_SHA1_C)
1083 case PSA_ALG_SHA_1:
1084 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1085 input, input_length );
1086 break;
1087#endif
1088#if defined(MBEDTLS_SHA256_C)
1089 case PSA_ALG_SHA_224:
1090 case PSA_ALG_SHA_256:
1091 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1092 input, input_length );
1093 break;
1094#endif
1095#if defined(MBEDTLS_SHA512_C)
1096 case PSA_ALG_SHA_384:
1097 case PSA_ALG_SHA_512:
1098 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1099 input, input_length );
1100 break;
1101#endif
1102 default:
1103 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1104 break;
1105 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001106
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001107 if( ret != 0 )
1108 psa_hash_abort( operation );
1109 return( mbedtls_to_psa_error( ret ) );
1110}
1111
1112psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1113 uint8_t *hash,
1114 size_t hash_size,
1115 size_t *hash_length )
1116{
itayzafrir40835d42018-08-02 13:14:17 +03001117 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001118 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001119 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001120
1121 /* Fill the output buffer with something that isn't a valid hash
1122 * (barring an attack on the hash and deliberately-crafted input),
1123 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001124 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001125 /* If hash_size is 0 then hash may be NULL and then the
1126 * call to memset would have undefined behavior. */
1127 if( hash_size != 0 )
1128 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001129
1130 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001131 {
1132 status = PSA_ERROR_BUFFER_TOO_SMALL;
1133 goto exit;
1134 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001135
1136 switch( operation->alg )
1137 {
1138#if defined(MBEDTLS_MD2_C)
1139 case PSA_ALG_MD2:
1140 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1141 break;
1142#endif
1143#if defined(MBEDTLS_MD4_C)
1144 case PSA_ALG_MD4:
1145 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1146 break;
1147#endif
1148#if defined(MBEDTLS_MD5_C)
1149 case PSA_ALG_MD5:
1150 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1151 break;
1152#endif
1153#if defined(MBEDTLS_RIPEMD160_C)
1154 case PSA_ALG_RIPEMD160:
1155 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1156 break;
1157#endif
1158#if defined(MBEDTLS_SHA1_C)
1159 case PSA_ALG_SHA_1:
1160 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1161 break;
1162#endif
1163#if defined(MBEDTLS_SHA256_C)
1164 case PSA_ALG_SHA_224:
1165 case PSA_ALG_SHA_256:
1166 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1167 break;
1168#endif
1169#if defined(MBEDTLS_SHA512_C)
1170 case PSA_ALG_SHA_384:
1171 case PSA_ALG_SHA_512:
1172 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1173 break;
1174#endif
1175 default:
1176 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1177 break;
1178 }
itayzafrir40835d42018-08-02 13:14:17 +03001179 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001180
itayzafrir40835d42018-08-02 13:14:17 +03001181exit:
1182 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001183 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001184 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001185 return( psa_hash_abort( operation ) );
1186 }
1187 else
1188 {
1189 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001190 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001191 }
1192}
1193
Gilles Peskine2d277862018-06-18 15:41:12 +02001194psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1195 const uint8_t *hash,
1196 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001197{
1198 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1199 size_t actual_hash_length;
1200 psa_status_t status = psa_hash_finish( operation,
1201 actual_hash, sizeof( actual_hash ),
1202 &actual_hash_length );
1203 if( status != PSA_SUCCESS )
1204 return( status );
1205 if( actual_hash_length != hash_length )
1206 return( PSA_ERROR_INVALID_SIGNATURE );
1207 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1208 return( PSA_ERROR_INVALID_SIGNATURE );
1209 return( PSA_SUCCESS );
1210}
1211
1212
1213
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001214/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001215/* MAC */
1216/****************************************************************/
1217
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001218static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001219 psa_algorithm_t alg,
1220 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001221 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001222 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001223{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001224 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001225 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001226
1227 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1228 {
1229 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001230 {
1231 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1232 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001233
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001234 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001235 {
Gilles Peskine5d1888e2018-07-12 00:32:42 +02001236 case PSA_ALG_STREAM_CIPHER_BASE:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001237 mode = MBEDTLS_MODE_STREAM;
1238 break;
1239 case PSA_ALG_CBC_BASE:
1240 mode = MBEDTLS_MODE_CBC;
1241 break;
1242 case PSA_ALG_CFB_BASE:
1243 mode = MBEDTLS_MODE_CFB;
1244 break;
1245 case PSA_ALG_OFB_BASE:
1246 mode = MBEDTLS_MODE_OFB;
1247 break;
1248 case PSA_ALG_CTR:
1249 mode = MBEDTLS_MODE_CTR;
1250 break;
1251 case PSA_ALG_CCM:
1252 mode = MBEDTLS_MODE_CCM;
1253 break;
1254 case PSA_ALG_GCM:
1255 mode = MBEDTLS_MODE_GCM;
1256 break;
1257 default:
1258 return( NULL );
1259 }
1260 }
1261 else if( alg == PSA_ALG_CMAC )
1262 mode = MBEDTLS_MODE_ECB;
1263 else if( alg == PSA_ALG_GMAC )
1264 mode = MBEDTLS_MODE_GCM;
1265 else
1266 return( NULL );
1267
1268 switch( key_type )
1269 {
1270 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001271 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001272 break;
1273 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001274 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1275 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001276 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001277 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001278 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001279 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001280 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1281 * but two-key Triple-DES is functionally three-key Triple-DES
1282 * with K1=K3, so that's how we present it to mbedtls. */
1283 if( key_bits == 128 )
1284 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001285 break;
1286 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001287 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001288 break;
1289 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001290 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001291 break;
1292 default:
1293 return( NULL );
1294 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001295 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001296 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001297
Jaeden Amero23bbb752018-06-26 14:16:54 +01001298 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1299 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001300}
1301
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001302static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001303{
Gilles Peskine2d277862018-06-18 15:41:12 +02001304 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001305 {
1306 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001307 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001308 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001309 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001310 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001311 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001312 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001313 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001314 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001315 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001316 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001317 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001318 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001319 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001320 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001321 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001322 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001323 return( 128 );
1324 default:
1325 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001326 }
1327}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001328
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001329/* Initialize the MAC operation structure. Once this function has been
1330 * called, psa_mac_abort can run and will do the right thing. */
1331static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1332 psa_algorithm_t alg )
1333{
1334 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1335
1336 operation->alg = alg;
1337 operation->key_set = 0;
1338 operation->iv_set = 0;
1339 operation->iv_required = 0;
1340 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001341 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001342
1343#if defined(MBEDTLS_CMAC_C)
1344 if( alg == PSA_ALG_CMAC )
1345 {
1346 operation->iv_required = 0;
1347 mbedtls_cipher_init( &operation->ctx.cmac );
1348 status = PSA_SUCCESS;
1349 }
1350 else
1351#endif /* MBEDTLS_CMAC_C */
1352#if defined(MBEDTLS_MD_C)
1353 if( PSA_ALG_IS_HMAC( operation->alg ) )
1354 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001355 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1356 operation->ctx.hmac.hash_ctx.alg = 0;
1357 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001358 }
1359 else
1360#endif /* MBEDTLS_MD_C */
1361 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001362 if( ! PSA_ALG_IS_MAC( alg ) )
1363 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001364 }
1365
1366 if( status != PSA_SUCCESS )
1367 memset( operation, 0, sizeof( *operation ) );
1368 return( status );
1369}
1370
Gilles Peskine01126fa2018-07-12 17:04:55 +02001371#if defined(MBEDTLS_MD_C)
1372static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1373{
1374 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1375 return( psa_hash_abort( &hmac->hash_ctx ) );
1376}
1377#endif /* MBEDTLS_MD_C */
1378
Gilles Peskine8c9def32018-02-08 10:02:12 +01001379psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1380{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001381 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001382 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001383 /* The object has (apparently) been initialized but it is not
1384 * in use. It's ok to call abort on such an object, and there's
1385 * nothing to do. */
1386 return( PSA_SUCCESS );
1387 }
1388 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001389#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001390 if( operation->alg == PSA_ALG_CMAC )
1391 {
1392 mbedtls_cipher_free( &operation->ctx.cmac );
1393 }
1394 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001395#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001396#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001397 if( PSA_ALG_IS_HMAC( operation->alg ) )
1398 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001399 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001400 }
1401 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001402#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001403 {
1404 /* Sanity check (shouldn't happen: operation->alg should
1405 * always have been initialized to a valid value). */
1406 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001407 }
Moran Peker41deec42018-04-04 15:43:05 +03001408
Gilles Peskine8c9def32018-02-08 10:02:12 +01001409 operation->alg = 0;
1410 operation->key_set = 0;
1411 operation->iv_set = 0;
1412 operation->iv_required = 0;
1413 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001414 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001415
Gilles Peskine8c9def32018-02-08 10:02:12 +01001416 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001417
1418bad_state:
1419 /* If abort is called on an uninitialized object, we can't trust
1420 * anything. Wipe the object in case it contains confidential data.
1421 * This may result in a memory leak if a pointer gets overwritten,
1422 * but it's too late to do anything about this. */
1423 memset( operation, 0, sizeof( *operation ) );
1424 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001425}
1426
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001427#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001428static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001429 size_t key_bits,
1430 key_slot_t *slot,
1431 const mbedtls_cipher_info_t *cipher_info )
1432{
1433 int ret;
1434
1435 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001436
1437 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1438 if( ret != 0 )
1439 return( ret );
1440
1441 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1442 slot->data.raw.data,
1443 key_bits );
1444 return( ret );
1445}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001446#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001447
Gilles Peskine248051a2018-06-20 16:09:38 +02001448#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001449static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1450 const uint8_t *key,
1451 size_t key_length,
1452 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001453{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001454 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001455 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001456 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001457 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001458 psa_status_t status;
1459
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001460 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1461 * overflow below. This should never trigger if the hash algorithm
1462 * is implemented correctly. */
1463 /* The size checks against the ipad and opad buffers cannot be written
1464 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1465 * because that triggers -Wlogical-op on GCC 7.3. */
1466 if( block_size > sizeof( ipad ) )
1467 return( PSA_ERROR_NOT_SUPPORTED );
1468 if( block_size > sizeof( hmac->opad ) )
1469 return( PSA_ERROR_NOT_SUPPORTED );
1470 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001471 return( PSA_ERROR_NOT_SUPPORTED );
1472
Gilles Peskined223b522018-06-11 18:12:58 +02001473 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001474 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001475 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001476 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001477 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001478 status = psa_hash_update( &hmac->hash_ctx, key, 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 Peskine01126fa2018-07-12 17:04:55 +02001481 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001482 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001483 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001484 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001485 }
Gilles Peskine96889972018-07-12 17:07:03 +02001486 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1487 * but it is permitted. It is common when HMAC is used in HKDF, for
1488 * example. Don't call `memcpy` in the 0-length because `key` could be
1489 * an invalid pointer which would make the behavior undefined. */
1490 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001491 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001492
Gilles Peskined223b522018-06-11 18:12:58 +02001493 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1494 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001495 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001496 ipad[i] ^= 0x36;
1497 memset( ipad + key_length, 0x36, block_size - key_length );
1498
1499 /* Copy the key material from ipad to opad, flipping the requisite bits,
1500 * and filling the rest of opad with the requisite constant. */
1501 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001502 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1503 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001504
Gilles Peskine01126fa2018-07-12 17:04:55 +02001505 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001506 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001507 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001508
Gilles Peskine01126fa2018-07-12 17:04:55 +02001509 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001510
1511cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001512 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001513
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001514 return( status );
1515}
Gilles Peskine248051a2018-06-20 16:09:38 +02001516#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001517
Gilles Peskine89167cb2018-07-08 20:12:23 +02001518static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1519 psa_key_slot_t key,
1520 psa_algorithm_t alg,
1521 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001522{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001523 psa_status_t status;
1524 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001525 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001526 psa_key_usage_t usage =
1527 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001528
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001529 status = psa_mac_init( operation, alg );
1530 if( status != PSA_SUCCESS )
1531 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001532 if( is_sign )
1533 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001534
Gilles Peskine89167cb2018-07-08 20:12:23 +02001535 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001536 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001537 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001538 key_bits = psa_get_key_bits( slot );
1539
Gilles Peskine8c9def32018-02-08 10:02:12 +01001540#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001541 if( alg == PSA_ALG_CMAC )
1542 {
1543 const mbedtls_cipher_info_t *cipher_info =
1544 mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
1545 int ret;
1546 if( cipher_info == NULL )
1547 {
1548 status = PSA_ERROR_NOT_SUPPORTED;
1549 goto exit;
1550 }
1551 operation->mac_size = cipher_info->block_size;
1552 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1553 status = mbedtls_to_psa_error( ret );
1554 }
1555 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001556#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001557#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001558 if( PSA_ALG_IS_HMAC( alg ) )
1559 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001560 psa_algorithm_t hash_alg = PSA_ALG_HMAC_HASH( alg );
1561 if( hash_alg == 0 )
1562 {
1563 status = PSA_ERROR_NOT_SUPPORTED;
1564 goto exit;
1565 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001566
1567 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1568 /* Sanity check. This shouldn't fail on a valid configuration. */
1569 if( operation->mac_size == 0 ||
1570 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1571 {
1572 status = PSA_ERROR_NOT_SUPPORTED;
1573 goto exit;
1574 }
1575
Gilles Peskine01126fa2018-07-12 17:04:55 +02001576 if( slot->type != PSA_KEY_TYPE_HMAC )
1577 {
1578 status = PSA_ERROR_INVALID_ARGUMENT;
1579 goto exit;
1580 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001581
Gilles Peskine01126fa2018-07-12 17:04:55 +02001582 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1583 slot->data.raw.data,
1584 slot->data.raw.bytes,
1585 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001586 }
1587 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001588#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001589 {
1590 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001591 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001592
Gilles Peskinefbfac682018-07-08 20:51:54 +02001593exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001594 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001595 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001596 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001597 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001598 else
1599 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001600 operation->key_set = 1;
1601 }
1602 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001603}
1604
Gilles Peskine89167cb2018-07-08 20:12:23 +02001605psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1606 psa_key_slot_t key,
1607 psa_algorithm_t alg )
1608{
1609 return( psa_mac_setup( operation, key, alg, 1 ) );
1610}
1611
1612psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1613 psa_key_slot_t key,
1614 psa_algorithm_t alg )
1615{
1616 return( psa_mac_setup( operation, key, alg, 0 ) );
1617}
1618
Gilles Peskine8c9def32018-02-08 10:02:12 +01001619psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1620 const uint8_t *input,
1621 size_t input_length )
1622{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001623 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001624 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001625 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001626 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001627 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001628 operation->has_input = 1;
1629
Gilles Peskine8c9def32018-02-08 10:02:12 +01001630#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001631 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001632 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001633 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1634 input, input_length );
1635 status = mbedtls_to_psa_error( ret );
1636 }
1637 else
1638#endif /* MBEDTLS_CMAC_C */
1639#if defined(MBEDTLS_MD_C)
1640 if( PSA_ALG_IS_HMAC( operation->alg ) )
1641 {
1642 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1643 input_length );
1644 }
1645 else
1646#endif /* MBEDTLS_MD_C */
1647 {
1648 /* This shouldn't happen if `operation` was initialized by
1649 * a setup function. */
1650 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001651 }
1652
Gilles Peskinefbfac682018-07-08 20:51:54 +02001653cleanup:
1654 if( status != PSA_SUCCESS )
1655 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001656 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001657}
1658
Gilles Peskine01126fa2018-07-12 17:04:55 +02001659#if defined(MBEDTLS_MD_C)
1660static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1661 uint8_t *mac,
1662 size_t mac_size )
1663{
1664 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1665 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1666 size_t hash_size = 0;
1667 size_t block_size = psa_get_hash_block_size( hash_alg );
1668 psa_status_t status;
1669
Gilles Peskine01126fa2018-07-12 17:04:55 +02001670 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1671 if( status != PSA_SUCCESS )
1672 return( status );
1673 /* From here on, tmp needs to be wiped. */
1674
1675 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1676 if( status != PSA_SUCCESS )
1677 goto exit;
1678
1679 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1680 if( status != PSA_SUCCESS )
1681 goto exit;
1682
1683 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1684 if( status != PSA_SUCCESS )
1685 goto exit;
1686
1687 status = psa_hash_finish( &hmac->hash_ctx, mac, mac_size, &hash_size );
1688
1689exit:
1690 mbedtls_zeroize( tmp, hash_size );
1691 return( status );
1692}
1693#endif /* MBEDTLS_MD_C */
1694
mohammad16036df908f2018-04-02 08:34:15 -07001695static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001696 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001697 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001698{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001699 if( ! operation->key_set )
1700 return( PSA_ERROR_BAD_STATE );
1701 if( operation->iv_required && ! operation->iv_set )
1702 return( PSA_ERROR_BAD_STATE );
1703
Gilles Peskine8c9def32018-02-08 10:02:12 +01001704 if( mac_size < operation->mac_size )
1705 return( PSA_ERROR_BUFFER_TOO_SMALL );
1706
Gilles Peskine8c9def32018-02-08 10:02:12 +01001707#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001708 if( operation->alg == PSA_ALG_CMAC )
1709 {
1710 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1711 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001712 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001713 else
1714#endif /* MBEDTLS_CMAC_C */
1715#if defined(MBEDTLS_MD_C)
1716 if( PSA_ALG_IS_HMAC( operation->alg ) )
1717 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001718 return( psa_hmac_finish_internal( &operation->ctx.hmac,
1719 mac, mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001720 }
1721 else
1722#endif /* MBEDTLS_MD_C */
1723 {
1724 /* This shouldn't happen if `operation` was initialized by
1725 * a setup function. */
1726 return( PSA_ERROR_BAD_STATE );
1727 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001728}
1729
Gilles Peskineacd4be32018-07-08 19:56:25 +02001730psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1731 uint8_t *mac,
1732 size_t mac_size,
1733 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001734{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001735 psa_status_t status;
1736
1737 /* Fill the output buffer with something that isn't a valid mac
1738 * (barring an attack on the mac and deliberately-crafted input),
1739 * in case the caller doesn't check the return status properly. */
1740 *mac_length = mac_size;
1741 /* If mac_size is 0 then mac may be NULL and then the
1742 * call to memset would have undefined behavior. */
1743 if( mac_size != 0 )
1744 memset( mac, '!', mac_size );
1745
Gilles Peskine89167cb2018-07-08 20:12:23 +02001746 if( ! operation->is_sign )
1747 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001748 status = PSA_ERROR_BAD_STATE;
1749 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001750 }
mohammad16036df908f2018-04-02 08:34:15 -07001751
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001752 status = psa_mac_finish_internal( operation, mac, mac_size );
1753
1754cleanup:
1755 if( status == PSA_SUCCESS )
1756 {
1757 status = psa_mac_abort( operation );
1758 if( status == PSA_SUCCESS )
1759 *mac_length = operation->mac_size;
1760 else
1761 memset( mac, '!', mac_size );
1762 }
1763 else
1764 psa_mac_abort( operation );
1765 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001766}
1767
Gilles Peskineacd4be32018-07-08 19:56:25 +02001768psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1769 const uint8_t *mac,
1770 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001771{
Gilles Peskine828ed142018-06-18 23:25:51 +02001772 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001773 psa_status_t status;
1774
Gilles Peskine89167cb2018-07-08 20:12:23 +02001775 if( operation->is_sign )
1776 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001777 status = PSA_ERROR_BAD_STATE;
1778 goto cleanup;
1779 }
1780 if( operation->mac_size != mac_length )
1781 {
1782 status = PSA_ERROR_INVALID_SIGNATURE;
1783 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001784 }
mohammad16036df908f2018-04-02 08:34:15 -07001785
1786 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001787 actual_mac, sizeof( actual_mac ) );
1788
1789 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1790 status = PSA_ERROR_INVALID_SIGNATURE;
1791
1792cleanup:
1793 if( status == PSA_SUCCESS )
1794 status = psa_mac_abort( operation );
1795 else
1796 psa_mac_abort( operation );
1797
1798 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001799}
1800
1801
Gilles Peskine20035e32018-02-03 22:44:14 +01001802
Gilles Peskine20035e32018-02-03 22:44:14 +01001803/****************************************************************/
1804/* Asymmetric cryptography */
1805/****************************************************************/
1806
Gilles Peskine2b450e32018-06-27 15:42:46 +02001807#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001808/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001809 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001810static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1811 size_t hash_length,
1812 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001813{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001814 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001815 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001816 *md_alg = mbedtls_md_get_type( md_info );
1817
1818 /* The Mbed TLS RSA module uses an unsigned int for hash length
1819 * parameters. Validate that it fits so that we don't risk an
1820 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001821#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001822 if( hash_length > UINT_MAX )
1823 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001824#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001825
1826#if defined(MBEDTLS_PKCS1_V15)
1827 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
1828 * must be correct. */
1829 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
1830 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001831 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001832 if( md_info == NULL )
1833 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001834 if( mbedtls_md_get_size( md_info ) != hash_length )
1835 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001836 }
1837#endif /* MBEDTLS_PKCS1_V15 */
1838
1839#if defined(MBEDTLS_PKCS1_V21)
1840 /* PSS requires a hash internally. */
1841 if( PSA_ALG_IS_RSA_PSS( alg ) )
1842 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001843 if( md_info == NULL )
1844 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001845 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001846#endif /* MBEDTLS_PKCS1_V21 */
1847
Gilles Peskine61b91d42018-06-08 16:09:36 +02001848 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001849}
1850
Gilles Peskine2b450e32018-06-27 15:42:46 +02001851static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1852 psa_algorithm_t alg,
1853 const uint8_t *hash,
1854 size_t hash_length,
1855 uint8_t *signature,
1856 size_t signature_size,
1857 size_t *signature_length )
1858{
1859 psa_status_t status;
1860 int ret;
1861 mbedtls_md_type_t md_alg;
1862
1863 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1864 if( status != PSA_SUCCESS )
1865 return( status );
1866
Gilles Peskine630a18a2018-06-29 17:49:35 +02001867 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001868 return( PSA_ERROR_BUFFER_TOO_SMALL );
1869
1870#if defined(MBEDTLS_PKCS1_V15)
1871 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1872 {
1873 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1874 MBEDTLS_MD_NONE );
1875 ret = mbedtls_rsa_pkcs1_sign( rsa,
1876 mbedtls_ctr_drbg_random,
1877 &global_data.ctr_drbg,
1878 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001879 md_alg,
1880 (unsigned int) hash_length,
1881 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001882 signature );
1883 }
1884 else
1885#endif /* MBEDTLS_PKCS1_V15 */
1886#if defined(MBEDTLS_PKCS1_V21)
1887 if( PSA_ALG_IS_RSA_PSS( alg ) )
1888 {
1889 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1890 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1891 mbedtls_ctr_drbg_random,
1892 &global_data.ctr_drbg,
1893 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001894 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001895 (unsigned int) hash_length,
1896 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001897 signature );
1898 }
1899 else
1900#endif /* MBEDTLS_PKCS1_V21 */
1901 {
1902 return( PSA_ERROR_INVALID_ARGUMENT );
1903 }
1904
1905 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02001906 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02001907 return( mbedtls_to_psa_error( ret ) );
1908}
1909
1910static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1911 psa_algorithm_t alg,
1912 const uint8_t *hash,
1913 size_t hash_length,
1914 const uint8_t *signature,
1915 size_t signature_length )
1916{
1917 psa_status_t status;
1918 int ret;
1919 mbedtls_md_type_t md_alg;
1920
1921 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1922 if( status != PSA_SUCCESS )
1923 return( status );
1924
Gilles Peskine630a18a2018-06-29 17:49:35 +02001925 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001926 return( PSA_ERROR_BUFFER_TOO_SMALL );
1927
1928#if defined(MBEDTLS_PKCS1_V15)
1929 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1930 {
1931 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1932 MBEDTLS_MD_NONE );
1933 ret = mbedtls_rsa_pkcs1_verify( rsa,
1934 mbedtls_ctr_drbg_random,
1935 &global_data.ctr_drbg,
1936 MBEDTLS_RSA_PUBLIC,
1937 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001938 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001939 hash,
1940 signature );
1941 }
1942 else
1943#endif /* MBEDTLS_PKCS1_V15 */
1944#if defined(MBEDTLS_PKCS1_V21)
1945 if( PSA_ALG_IS_RSA_PSS( alg ) )
1946 {
1947 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1948 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1949 mbedtls_ctr_drbg_random,
1950 &global_data.ctr_drbg,
1951 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001952 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001953 (unsigned int) hash_length,
1954 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001955 signature );
1956 }
1957 else
1958#endif /* MBEDTLS_PKCS1_V21 */
1959 {
1960 return( PSA_ERROR_INVALID_ARGUMENT );
1961 }
1962 return( mbedtls_to_psa_error( ret ) );
1963}
1964#endif /* MBEDTLS_RSA_C */
1965
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001966#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001967/* `ecp` cannot be const because `ecp->grp` needs to be non-const
1968 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
1969 * (even though these functions don't modify it). */
1970static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
1971 psa_algorithm_t alg,
1972 const uint8_t *hash,
1973 size_t hash_length,
1974 uint8_t *signature,
1975 size_t signature_size,
1976 size_t *signature_length )
1977{
1978 int ret;
1979 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001980 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001981 mbedtls_mpi_init( &r );
1982 mbedtls_mpi_init( &s );
1983
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001984 if( signature_size < 2 * curve_bytes )
1985 {
1986 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
1987 goto cleanup;
1988 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001989
1990 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
1991 {
1992 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
1993 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1994 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
1995 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
1996 hash, hash_length,
1997 md_alg ) );
1998 }
1999 else
2000 {
2001 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2002 hash, hash_length,
2003 mbedtls_ctr_drbg_random,
2004 &global_data.ctr_drbg ) );
2005 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002006
2007 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2008 signature,
2009 curve_bytes ) );
2010 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2011 signature + curve_bytes,
2012 curve_bytes ) );
2013
2014cleanup:
2015 mbedtls_mpi_free( &r );
2016 mbedtls_mpi_free( &s );
2017 if( ret == 0 )
2018 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002019 return( mbedtls_to_psa_error( ret ) );
2020}
2021
2022static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2023 const uint8_t *hash,
2024 size_t hash_length,
2025 const uint8_t *signature,
2026 size_t signature_length )
2027{
2028 int ret;
2029 mbedtls_mpi r, s;
2030 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2031 mbedtls_mpi_init( &r );
2032 mbedtls_mpi_init( &s );
2033
2034 if( signature_length != 2 * curve_bytes )
2035 return( PSA_ERROR_INVALID_SIGNATURE );
2036
2037 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2038 signature,
2039 curve_bytes ) );
2040 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2041 signature + curve_bytes,
2042 curve_bytes ) );
2043
2044 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2045 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002046
2047cleanup:
2048 mbedtls_mpi_free( &r );
2049 mbedtls_mpi_free( &s );
2050 return( mbedtls_to_psa_error( ret ) );
2051}
2052#endif /* MBEDTLS_ECDSA_C */
2053
Gilles Peskine61b91d42018-06-08 16:09:36 +02002054psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2055 psa_algorithm_t alg,
2056 const uint8_t *hash,
2057 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002058 uint8_t *signature,
2059 size_t signature_size,
2060 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002061{
2062 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002063 psa_status_t status;
2064
2065 *signature_length = signature_size;
2066
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002067 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2068 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002069 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002070 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002071 {
2072 status = PSA_ERROR_INVALID_ARGUMENT;
2073 goto exit;
2074 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002075
Gilles Peskine20035e32018-02-03 22:44:14 +01002076#if defined(MBEDTLS_RSA_C)
2077 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2078 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002079 status = psa_rsa_sign( slot->data.rsa,
2080 alg,
2081 hash, hash_length,
2082 signature, signature_size,
2083 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002084 }
2085 else
2086#endif /* defined(MBEDTLS_RSA_C) */
2087#if defined(MBEDTLS_ECP_C)
2088 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2089 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002090#if defined(MBEDTLS_ECDSA_C)
2091 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002092 status = psa_ecdsa_sign( slot->data.ecp,
2093 alg,
2094 hash, hash_length,
2095 signature, signature_size,
2096 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002097 else
2098#endif /* defined(MBEDTLS_ECDSA_C) */
2099 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002100 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002101 }
itayzafrir5c753392018-05-08 11:18:38 +03002102 }
2103 else
2104#endif /* defined(MBEDTLS_ECP_C) */
2105 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002106 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002107 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002108
2109exit:
2110 /* Fill the unused part of the output buffer (the whole buffer on error,
2111 * the trailing part on success) with something that isn't a valid mac
2112 * (barring an attack on the mac and deliberately-crafted input),
2113 * in case the caller doesn't check the return status properly. */
2114 if( status == PSA_SUCCESS )
2115 memset( signature + *signature_length, '!',
2116 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002117 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002118 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002119 /* If signature_size is 0 then we have nothing to do. We must not call
2120 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002121 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002122}
2123
2124psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2125 psa_algorithm_t alg,
2126 const uint8_t *hash,
2127 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002128 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002129 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002130{
2131 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002132 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002133
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002134 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2135 if( status != PSA_SUCCESS )
2136 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002137
Gilles Peskine61b91d42018-06-08 16:09:36 +02002138#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002139 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002140 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002141 return( psa_rsa_verify( slot->data.rsa,
2142 alg,
2143 hash, hash_length,
2144 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002145 }
2146 else
2147#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002148#if defined(MBEDTLS_ECP_C)
2149 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2150 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002151#if defined(MBEDTLS_ECDSA_C)
2152 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002153 return( psa_ecdsa_verify( slot->data.ecp,
2154 hash, hash_length,
2155 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002156 else
2157#endif /* defined(MBEDTLS_ECDSA_C) */
2158 {
2159 return( PSA_ERROR_INVALID_ARGUMENT );
2160 }
itayzafrir5c753392018-05-08 11:18:38 +03002161 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002162 else
2163#endif /* defined(MBEDTLS_ECP_C) */
2164 {
2165 return( PSA_ERROR_NOT_SUPPORTED );
2166 }
2167}
2168
Gilles Peskine072ac562018-06-30 00:21:29 +02002169#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2170static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2171 mbedtls_rsa_context *rsa )
2172{
2173 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2174 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2175 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2176 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2177}
2178#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2179
Gilles Peskine61b91d42018-06-08 16:09:36 +02002180psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2181 psa_algorithm_t alg,
2182 const uint8_t *input,
2183 size_t input_length,
2184 const uint8_t *salt,
2185 size_t salt_length,
2186 uint8_t *output,
2187 size_t output_size,
2188 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002189{
2190 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002191 psa_status_t status;
2192
Darryl Green5cc689a2018-07-24 15:34:10 +01002193 (void) input;
2194 (void) input_length;
2195 (void) salt;
2196 (void) output;
2197 (void) output_size;
2198
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002199 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002200
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002201 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2202 return( PSA_ERROR_INVALID_ARGUMENT );
2203
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002204 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2205 if( status != PSA_SUCCESS )
2206 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002207 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2208 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002209 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002210
2211#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002212 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002213 {
2214 mbedtls_rsa_context *rsa = slot->data.rsa;
2215 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002216 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002217 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002218#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002219 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002220 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002221 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2222 mbedtls_ctr_drbg_random,
2223 &global_data.ctr_drbg,
2224 MBEDTLS_RSA_PUBLIC,
2225 input_length,
2226 input,
2227 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002228 }
2229 else
2230#endif /* MBEDTLS_PKCS1_V15 */
2231#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002232 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002233 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002234 psa_rsa_oaep_set_padding_mode( alg, rsa );
2235 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2236 mbedtls_ctr_drbg_random,
2237 &global_data.ctr_drbg,
2238 MBEDTLS_RSA_PUBLIC,
2239 salt, salt_length,
2240 input_length,
2241 input,
2242 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002243 }
2244 else
2245#endif /* MBEDTLS_PKCS1_V21 */
2246 {
2247 return( PSA_ERROR_INVALID_ARGUMENT );
2248 }
2249 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002250 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002251 return( mbedtls_to_psa_error( ret ) );
2252 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002253 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002254#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002255 {
2256 return( PSA_ERROR_NOT_SUPPORTED );
2257 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002258}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002259
Gilles Peskine61b91d42018-06-08 16:09:36 +02002260psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2261 psa_algorithm_t alg,
2262 const uint8_t *input,
2263 size_t input_length,
2264 const uint8_t *salt,
2265 size_t salt_length,
2266 uint8_t *output,
2267 size_t output_size,
2268 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002269{
2270 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002271 psa_status_t status;
2272
Darryl Green5cc689a2018-07-24 15:34:10 +01002273 (void) input;
2274 (void) input_length;
2275 (void) salt;
2276 (void) output;
2277 (void) output_size;
2278
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002279 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002280
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002281 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2282 return( PSA_ERROR_INVALID_ARGUMENT );
2283
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002284 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2285 if( status != PSA_SUCCESS )
2286 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002287 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2288 return( PSA_ERROR_INVALID_ARGUMENT );
2289
2290#if defined(MBEDTLS_RSA_C)
2291 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2292 {
2293 mbedtls_rsa_context *rsa = slot->data.rsa;
2294 int ret;
2295
Gilles Peskine630a18a2018-06-29 17:49:35 +02002296 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002297 return( PSA_ERROR_INVALID_ARGUMENT );
2298
2299#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002300 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002301 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002302 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2303 mbedtls_ctr_drbg_random,
2304 &global_data.ctr_drbg,
2305 MBEDTLS_RSA_PRIVATE,
2306 output_length,
2307 input,
2308 output,
2309 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002310 }
2311 else
2312#endif /* MBEDTLS_PKCS1_V15 */
2313#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002314 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002315 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002316 psa_rsa_oaep_set_padding_mode( alg, rsa );
2317 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2318 mbedtls_ctr_drbg_random,
2319 &global_data.ctr_drbg,
2320 MBEDTLS_RSA_PRIVATE,
2321 salt, salt_length,
2322 output_length,
2323 input,
2324 output,
2325 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002326 }
2327 else
2328#endif /* MBEDTLS_PKCS1_V21 */
2329 {
2330 return( PSA_ERROR_INVALID_ARGUMENT );
2331 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002332
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002333 return( mbedtls_to_psa_error( ret ) );
2334 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002335 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002336#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002337 {
2338 return( PSA_ERROR_NOT_SUPPORTED );
2339 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002340}
Gilles Peskine20035e32018-02-03 22:44:14 +01002341
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002342
2343
mohammad1603503973b2018-03-12 15:59:30 +02002344/****************************************************************/
2345/* Symmetric cryptography */
2346/****************************************************************/
2347
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002348/* Initialize the cipher operation structure. Once this function has been
2349 * called, psa_cipher_abort can run and will do the right thing. */
2350static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2351 psa_algorithm_t alg )
2352{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002353 if( ! PSA_ALG_IS_CIPHER( alg ) )
2354 {
2355 memset( operation, 0, sizeof( *operation ) );
2356 return( PSA_ERROR_INVALID_ARGUMENT );
2357 }
2358
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002359 operation->alg = alg;
2360 operation->key_set = 0;
2361 operation->iv_set = 0;
2362 operation->iv_required = 1;
2363 operation->iv_size = 0;
2364 operation->block_size = 0;
2365 mbedtls_cipher_init( &operation->ctx.cipher );
2366 return( PSA_SUCCESS );
2367}
2368
Gilles Peskinee553c652018-06-04 16:22:46 +02002369static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2370 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002371 psa_algorithm_t alg,
2372 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002373{
2374 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2375 psa_status_t status;
2376 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002377 size_t key_bits;
2378 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002379 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2380 PSA_KEY_USAGE_ENCRYPT :
2381 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002382
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002383 status = psa_cipher_init( operation, alg );
2384 if( status != PSA_SUCCESS )
2385 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002386
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002387 status = psa_get_key_from_slot( key, &slot, usage, alg);
2388 if( status != PSA_SUCCESS )
2389 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002390 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002391
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002392 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002393 if( cipher_info == NULL )
2394 return( PSA_ERROR_NOT_SUPPORTED );
2395
mohammad1603503973b2018-03-12 15:59:30 +02002396 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002397 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002398 {
2399 psa_cipher_abort( operation );
2400 return( mbedtls_to_psa_error( ret ) );
2401 }
2402
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002403#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002404 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002405 {
2406 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2407 unsigned char keys[24];
2408 memcpy( keys, slot->data.raw.data, 16 );
2409 memcpy( keys + 16, slot->data.raw.data, 8 );
2410 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2411 keys,
2412 192, cipher_operation );
2413 }
2414 else
2415#endif
2416 {
2417 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2418 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002419 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002420 }
Moran Peker41deec42018-04-04 15:43:05 +03002421 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002422 {
2423 psa_cipher_abort( operation );
2424 return( mbedtls_to_psa_error( ret ) );
2425 }
2426
mohammad16038481e742018-03-18 13:57:31 +02002427#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002428 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002429 {
Gilles Peskine53514202018-06-06 15:11:46 +02002430 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2431 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002432
Moran Pekera28258c2018-05-29 16:25:04 +03002433 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002434 {
2435 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2436 mode = MBEDTLS_PADDING_PKCS7;
2437 break;
2438 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2439 mode = MBEDTLS_PADDING_NONE;
2440 break;
2441 default:
Moran Pekerae382792018-05-31 14:06:17 +03002442 psa_cipher_abort( operation );
2443 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002444 }
2445 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002446 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002447 {
2448 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002449 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002450 }
mohammad16038481e742018-03-18 13:57:31 +02002451 }
2452#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2453
mohammad1603503973b2018-03-12 15:59:30 +02002454 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002455 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002456 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) :
Gilles Peskine7e928852018-06-04 16:23:10 +02002457 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002458 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002459 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002460 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002461 }
mohammad1603503973b2018-03-12 15:59:30 +02002462
Moran Peker395db872018-05-31 14:07:14 +03002463 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002464}
2465
Gilles Peskinefe119512018-07-08 21:39:34 +02002466psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2467 psa_key_slot_t key,
2468 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002469{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002470 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002471}
2472
Gilles Peskinefe119512018-07-08 21:39:34 +02002473psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2474 psa_key_slot_t key,
2475 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002476{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002477 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002478}
2479
Gilles Peskinefe119512018-07-08 21:39:34 +02002480psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2481 unsigned char *iv,
2482 size_t iv_size,
2483 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002484{
itayzafrir534bd7c2018-08-02 13:56:32 +03002485 psa_status_t status;
2486 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002487 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002488 {
2489 status = PSA_ERROR_BAD_STATE;
2490 goto exit;
2491 }
Moran Peker41deec42018-04-04 15:43:05 +03002492 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002493 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002494 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002495 goto exit;
2496 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002497 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2498 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002499 if( ret != 0 )
2500 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002501 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002502 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002503 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002504
mohammad16038481e742018-03-18 13:57:31 +02002505 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002506 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002507
Moran Peker395db872018-05-31 14:07:14 +03002508exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002509 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002510 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002511 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002512}
2513
Gilles Peskinefe119512018-07-08 21:39:34 +02002514psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2515 const unsigned char *iv,
2516 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002517{
itayzafrir534bd7c2018-08-02 13:56:32 +03002518 psa_status_t status;
2519 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002520 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002521 {
2522 status = PSA_ERROR_BAD_STATE;
2523 goto exit;
2524 }
Moran Pekera28258c2018-05-29 16:25:04 +03002525 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002526 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002527 status = PSA_ERROR_INVALID_ARGUMENT;
2528 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002529 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002530 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2531 status = mbedtls_to_psa_error( ret );
2532exit:
2533 if( status == PSA_SUCCESS )
2534 operation->iv_set = 1;
2535 else
mohammad1603503973b2018-03-12 15:59:30 +02002536 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002537 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002538}
2539
Gilles Peskinee553c652018-06-04 16:22:46 +02002540psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2541 const uint8_t *input,
2542 size_t input_length,
2543 unsigned char *output,
2544 size_t output_size,
2545 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002546{
itayzafrir534bd7c2018-08-02 13:56:32 +03002547 psa_status_t status;
2548 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002549 size_t expected_output_size;
2550 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2551 {
2552 /* Take the unprocessed partial block left over from previous
2553 * update calls, if any, plus the input to this call. Remove
2554 * the last partial block, if any. You get the data that will be
2555 * output in this call. */
2556 expected_output_size =
2557 ( operation->ctx.cipher.unprocessed_len + input_length )
2558 / operation->block_size * operation->block_size;
2559 }
2560 else
2561 {
2562 expected_output_size = input_length;
2563 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002564
Gilles Peskine89d789c2018-06-04 17:17:16 +02002565 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002566 {
2567 status = PSA_ERROR_BUFFER_TOO_SMALL;
2568 goto exit;
2569 }
mohammad160382759612018-03-12 18:16:40 +02002570
mohammad1603503973b2018-03-12 15:59:30 +02002571 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002572 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002573 status = mbedtls_to_psa_error( ret );
2574exit:
2575 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002576 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002577 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002578}
2579
Gilles Peskinee553c652018-06-04 16:22:46 +02002580psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2581 uint8_t *output,
2582 size_t output_size,
2583 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002584{
Janos Follath315b51c2018-07-09 16:04:51 +01002585 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2586 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002587 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002588
mohammad1603503973b2018-03-12 15:59:30 +02002589 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002590 {
Janos Follath315b51c2018-07-09 16:04:51 +01002591 status = PSA_ERROR_BAD_STATE;
2592 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002593 }
2594 if( operation->iv_required && ! operation->iv_set )
2595 {
Janos Follath315b51c2018-07-09 16:04:51 +01002596 status = PSA_ERROR_BAD_STATE;
2597 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002598 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002599 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2600 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002601 {
Gilles Peskine53514202018-06-06 15:11:46 +02002602 psa_algorithm_t padding_mode =
2603 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002604 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002605 {
Janos Follath315b51c2018-07-09 16:04:51 +01002606 status = PSA_ERROR_TAMPERING_DETECTED;
2607 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002608 }
Gilles Peskine53514202018-06-06 15:11:46 +02002609 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002610 {
2611 if( operation->ctx.cipher.unprocessed_len != 0 )
2612 {
Janos Follath315b51c2018-07-09 16:04:51 +01002613 status = PSA_ERROR_INVALID_ARGUMENT;
2614 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002615 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002616 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002617 }
2618
Janos Follath315b51c2018-07-09 16:04:51 +01002619 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2620 temp_output_buffer,
2621 output_length );
2622 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002623 {
Janos Follath315b51c2018-07-09 16:04:51 +01002624 status = mbedtls_to_psa_error( cipher_ret );
2625 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002626 }
Janos Follath315b51c2018-07-09 16:04:51 +01002627
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002628 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002629 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002630 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002631 memcpy( output, temp_output_buffer, *output_length );
2632 else
2633 {
Janos Follath315b51c2018-07-09 16:04:51 +01002634 status = PSA_ERROR_BUFFER_TOO_SMALL;
2635 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002636 }
mohammad1603503973b2018-03-12 15:59:30 +02002637
Janos Follath279ab8e2018-07-09 16:13:21 +01002638 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002639 status = psa_cipher_abort( operation );
2640
2641 return( status );
2642
2643error:
2644
2645 *output_length = 0;
2646
Janos Follath279ab8e2018-07-09 16:13:21 +01002647 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002648 (void) psa_cipher_abort( operation );
2649
2650 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002651}
2652
Gilles Peskinee553c652018-06-04 16:22:46 +02002653psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2654{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002655 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002656 {
2657 /* The object has (apparently) been initialized but it is not
2658 * in use. It's ok to call abort on such an object, and there's
2659 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002660 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002661 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002662
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002663 /* Sanity check (shouldn't happen: operation->alg should
2664 * always have been initialized to a valid value). */
2665 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2666 return( PSA_ERROR_BAD_STATE );
2667
mohammad1603503973b2018-03-12 15:59:30 +02002668 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002669
Moran Peker41deec42018-04-04 15:43:05 +03002670 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002671 operation->key_set = 0;
2672 operation->iv_set = 0;
2673 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002674 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002675 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002676
Moran Peker395db872018-05-31 14:07:14 +03002677 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002678}
2679
Gilles Peskinea0655c32018-04-30 17:06:50 +02002680
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002681
mohammad16038cc1cee2018-03-28 01:21:33 +03002682/****************************************************************/
2683/* Key Policy */
2684/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002685
mohammad160327010052018-07-03 13:16:15 +03002686#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002687void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002688{
Gilles Peskine803ce742018-06-18 16:07:14 +02002689 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002690}
2691
Gilles Peskine2d277862018-06-18 15:41:12 +02002692void psa_key_policy_set_usage( psa_key_policy_t *policy,
2693 psa_key_usage_t usage,
2694 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002695{
mohammad16034eed7572018-03-28 05:14:59 -07002696 policy->usage = usage;
2697 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002698}
2699
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002700psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002701{
mohammad16036df908f2018-04-02 08:34:15 -07002702 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002703}
2704
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002705psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002706{
mohammad16036df908f2018-04-02 08:34:15 -07002707 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002708}
mohammad160327010052018-07-03 13:16:15 +03002709#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002710
Gilles Peskine2d277862018-06-18 15:41:12 +02002711psa_status_t psa_set_key_policy( psa_key_slot_t key,
2712 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002713{
2714 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002715 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002716
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002717 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002718 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002719
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002720 status = psa_get_empty_key_slot( key, &slot );
2721 if( status != PSA_SUCCESS )
2722 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002723
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002724 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2725 PSA_KEY_USAGE_ENCRYPT |
2726 PSA_KEY_USAGE_DECRYPT |
2727 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002728 PSA_KEY_USAGE_VERIFY |
2729 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002730 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002731
mohammad16036df908f2018-04-02 08:34:15 -07002732 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002733
2734 return( PSA_SUCCESS );
2735}
2736
Gilles Peskine2d277862018-06-18 15:41:12 +02002737psa_status_t psa_get_key_policy( psa_key_slot_t key,
2738 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002739{
2740 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002741 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002742
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002743 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002744 return( PSA_ERROR_INVALID_ARGUMENT );
2745
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002746 status = psa_get_key_slot( key, &slot );
2747 if( status != PSA_SUCCESS )
2748 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002749
mohammad16036df908f2018-04-02 08:34:15 -07002750 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002751
2752 return( PSA_SUCCESS );
2753}
Gilles Peskine20035e32018-02-03 22:44:14 +01002754
Gilles Peskinea0655c32018-04-30 17:06:50 +02002755
2756
mohammad1603804cd712018-03-20 22:44:08 +02002757/****************************************************************/
2758/* Key Lifetime */
2759/****************************************************************/
2760
Gilles Peskine2d277862018-06-18 15:41:12 +02002761psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2762 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002763{
2764 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002765 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002766
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002767 status = psa_get_key_slot( key, &slot );
2768 if( status != PSA_SUCCESS )
2769 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002770
mohammad1603804cd712018-03-20 22:44:08 +02002771 *lifetime = slot->lifetime;
2772
2773 return( PSA_SUCCESS );
2774}
2775
Gilles Peskine2d277862018-06-18 15:41:12 +02002776psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002777 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002778{
2779 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002780 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002781
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002782 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2783 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002784 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2785 return( PSA_ERROR_INVALID_ARGUMENT );
2786
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002787 status = psa_get_empty_key_slot( key, &slot );
2788 if( status != PSA_SUCCESS )
2789 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002790
Moran Pekerd7326592018-05-29 16:56:39 +03002791 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002792 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002793
mohammad1603060ad8a2018-03-20 14:28:38 -07002794 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002795
2796 return( PSA_SUCCESS );
2797}
2798
Gilles Peskine20035e32018-02-03 22:44:14 +01002799
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002800
mohammad16035955c982018-04-26 00:53:03 +03002801/****************************************************************/
2802/* AEAD */
2803/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002804
mohammad16035955c982018-04-26 00:53:03 +03002805psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2806 psa_algorithm_t alg,
2807 const uint8_t *nonce,
2808 size_t nonce_length,
2809 const uint8_t *additional_data,
2810 size_t additional_data_length,
2811 const uint8_t *plaintext,
2812 size_t plaintext_length,
2813 uint8_t *ciphertext,
2814 size_t ciphertext_size,
2815 size_t *ciphertext_length )
2816{
2817 int ret;
2818 psa_status_t status;
2819 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002820 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002821 uint8_t *tag;
2822 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002823 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002824 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002825
mohammad1603f08a5502018-06-03 15:05:47 +03002826 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002827
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002828 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2829 if( status != PSA_SUCCESS )
2830 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002831 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002832
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002833 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002834 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002835 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002836 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002837
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002838 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002839 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002840 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002841
mohammad16035955c982018-04-26 00:53:03 +03002842 if( alg == PSA_ALG_GCM )
2843 {
2844 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002845 tag_length = 16;
2846
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002847 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002848 return( PSA_ERROR_INVALID_ARGUMENT );
2849
mohammad160315223a82018-06-03 17:19:55 +03002850 //make sure we have place to hold the tag in the ciphertext buffer
2851 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002852 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002853
2854 //update the tag pointer to point to the end of the ciphertext_length
2855 tag = ciphertext + plaintext_length;
2856
mohammad16035955c982018-04-26 00:53:03 +03002857 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002858 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002859 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002860 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002861 if( ret != 0 )
2862 {
2863 mbedtls_gcm_free( &gcm );
2864 return( mbedtls_to_psa_error( ret ) );
2865 }
2866 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002867 plaintext_length, nonce,
2868 nonce_length, additional_data,
2869 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002870 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002871 mbedtls_gcm_free( &gcm );
2872 }
2873 else if( alg == PSA_ALG_CCM )
2874 {
2875 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002876 tag_length = 16;
2877
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002878 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002879 return( PSA_ERROR_INVALID_ARGUMENT );
2880
mohammad160347ddf3d2018-04-26 01:11:21 +03002881 if( nonce_length < 7 || nonce_length > 13 )
2882 return( PSA_ERROR_INVALID_ARGUMENT );
2883
mohammad160315223a82018-06-03 17:19:55 +03002884 //make sure we have place to hold the tag in the ciphertext buffer
2885 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002886 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002887
2888 //update the tag pointer to point to the end of the ciphertext_length
2889 tag = ciphertext + plaintext_length;
2890
mohammad16035955c982018-04-26 00:53:03 +03002891 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002892 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002893 slot->data.raw.data,
2894 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002895 if( ret != 0 )
2896 {
2897 mbedtls_ccm_free( &ccm );
2898 return( mbedtls_to_psa_error( ret ) );
2899 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002900 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002901 nonce, nonce_length,
2902 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002903 additional_data_length,
2904 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002905 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002906 mbedtls_ccm_free( &ccm );
2907 }
mohammad16035c8845f2018-05-09 05:40:09 -07002908 else
2909 {
mohammad1603554faad2018-06-03 15:07:38 +03002910 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002911 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002912
mohammad160315223a82018-06-03 17:19:55 +03002913 if( ret != 0 )
2914 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002915 /* If ciphertext_size is 0 then ciphertext may be NULL and then the
2916 * call to memset would have undefined behavior. */
2917 if( ciphertext_size != 0 )
2918 memset( ciphertext, 0, ciphertext_size );
mohammad160315223a82018-06-03 17:19:55 +03002919 return( mbedtls_to_psa_error( ret ) );
2920 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002921
mohammad160315223a82018-06-03 17:19:55 +03002922 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002923 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002924}
2925
Gilles Peskineee652a32018-06-01 19:23:52 +02002926/* Locate the tag in a ciphertext buffer containing the encrypted data
2927 * followed by the tag. Return the length of the part preceding the tag in
2928 * *plaintext_length. This is the size of the plaintext in modes where
2929 * the encrypted data has the same size as the plaintext, such as
2930 * CCM and GCM. */
2931static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2932 const uint8_t *ciphertext,
2933 size_t ciphertext_length,
2934 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002935 const uint8_t **p_tag )
2936{
2937 size_t payload_length;
2938 if( tag_length > ciphertext_length )
2939 return( PSA_ERROR_INVALID_ARGUMENT );
2940 payload_length = ciphertext_length - tag_length;
2941 if( payload_length > plaintext_size )
2942 return( PSA_ERROR_BUFFER_TOO_SMALL );
2943 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002944 return( PSA_SUCCESS );
2945}
2946
mohammad16035955c982018-04-26 00:53:03 +03002947psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2948 psa_algorithm_t alg,
2949 const uint8_t *nonce,
2950 size_t nonce_length,
2951 const uint8_t *additional_data,
2952 size_t additional_data_length,
2953 const uint8_t *ciphertext,
2954 size_t ciphertext_length,
2955 uint8_t *plaintext,
2956 size_t plaintext_size,
2957 size_t *plaintext_length )
2958{
2959 int ret;
2960 psa_status_t status;
2961 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002962 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002963 const uint8_t *tag;
2964 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002965 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002966 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002967
Gilles Peskineee652a32018-06-01 19:23:52 +02002968 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002969
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002970 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2971 if( status != PSA_SUCCESS )
2972 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002973 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002974
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002975 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002976 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002977 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002978 return( PSA_ERROR_NOT_SUPPORTED );
2979
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002980 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002981 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002982 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002983
mohammad16035955c982018-04-26 00:53:03 +03002984 if( alg == PSA_ALG_GCM )
2985 {
2986 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002987
Gilles Peskineee652a32018-06-01 19:23:52 +02002988 tag_length = 16;
2989 status = psa_aead_unpadded_locate_tag( tag_length,
2990 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002991 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002992 if( status != PSA_SUCCESS )
2993 return( status );
2994
mohammad16035955c982018-04-26 00:53:03 +03002995 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002996 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002997 slot->data.raw.data,
2998 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002999 if( ret != 0 )
3000 {
3001 mbedtls_gcm_free( &gcm );
3002 return( mbedtls_to_psa_error( ret ) );
3003 }
mohammad16035955c982018-04-26 00:53:03 +03003004
Gilles Peskineee652a32018-06-01 19:23:52 +02003005 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03003006 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003007 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03003008 additional_data,
3009 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003010 tag, tag_length,
3011 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03003012 mbedtls_gcm_free( &gcm );
3013 }
3014 else if( alg == PSA_ALG_CCM )
3015 {
3016 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02003017
mohammad160347ddf3d2018-04-26 01:11:21 +03003018 if( nonce_length < 7 || nonce_length > 13 )
3019 return( PSA_ERROR_INVALID_ARGUMENT );
3020
mohammad16039375f842018-06-03 14:28:24 +03003021 tag_length = 16;
3022 status = psa_aead_unpadded_locate_tag( tag_length,
3023 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003024 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003025 if( status != PSA_SUCCESS )
3026 return( status );
3027
mohammad16035955c982018-04-26 00:53:03 +03003028 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02003029 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003030 slot->data.raw.data,
3031 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03003032 if( ret != 0 )
3033 {
3034 mbedtls_ccm_free( &ccm );
3035 return( mbedtls_to_psa_error( ret ) );
3036 }
mohammad160360a64d02018-06-03 17:20:42 +03003037 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003038 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003039 additional_data,
3040 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003041 ciphertext, plaintext,
3042 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03003043 mbedtls_ccm_free( &ccm );
3044 }
mohammad160339574652018-06-01 04:39:53 -07003045 else
3046 {
mohammad1603554faad2018-06-03 15:07:38 +03003047 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003048 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003049
Gilles Peskineee652a32018-06-01 19:23:52 +02003050 if( ret != 0 )
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003051 {
3052 /* If plaintext_size is 0 then plaintext may be NULL and then the
3053 * call to memset has undefined behavior. */
3054 if( plaintext_size != 0 )
3055 memset( plaintext, 0, plaintext_size );
3056 }
mohammad160360a64d02018-06-03 17:20:42 +03003057 else
3058 *plaintext_length = ciphertext_length - tag_length;
3059
Gilles Peskineee652a32018-06-01 19:23:52 +02003060 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03003061}
3062
Gilles Peskinea0655c32018-04-30 17:06:50 +02003063
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003064
Gilles Peskine20035e32018-02-03 22:44:14 +01003065/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003066/* Generators */
3067/****************************************************************/
3068
3069psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3070{
3071 psa_status_t status = PSA_SUCCESS;
3072 if( generator->alg == 0 )
3073 {
3074 /* The object has (apparently) been initialized but it is not
3075 * in use. It's ok to call abort on such an object, and there's
3076 * nothing to do. */
3077 }
3078 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003079#if defined(MBEDTLS_MD_C)
3080 if( PSA_ALG_IS_HKDF( generator->alg ) )
3081 {
3082 mbedtls_free( generator->ctx.hkdf.info );
3083 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3084 }
3085 else
3086#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003087 {
3088 status = PSA_ERROR_BAD_STATE;
3089 }
3090 memset( generator, 0, sizeof( *generator ) );
3091 return( status );
3092}
3093
3094
3095psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3096 size_t *capacity)
3097{
3098 *capacity = generator->capacity;
3099 return( PSA_SUCCESS );
3100}
3101
Gilles Peskinebef7f142018-07-12 17:22:21 +02003102#if defined(MBEDTLS_MD_C)
3103/* Read some bytes from an HKDF-based generator. This performs a chunk
3104 * of the expand phase of the HKDF algorithm. */
3105static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3106 psa_algorithm_t hash_alg,
3107 uint8_t *output,
3108 size_t output_length )
3109{
3110 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3111 psa_status_t status;
3112
3113 while( output_length != 0 )
3114 {
3115 /* Copy what remains of the current block */
3116 uint8_t n = hash_length - hkdf->offset_in_block;
3117 if( n > output_length )
3118 n = (uint8_t) output_length;
3119 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3120 output += n;
3121 output_length -= n;
3122 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003123 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003124 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003125 /* We can't be wanting more output after block 0xff, otherwise
3126 * the capacity check in psa_generator_read() would have
3127 * prevented this call. It could happen only if the generator
3128 * object was corrupted or if this function is called directly
3129 * inside the library. */
3130 if( hkdf->block_number == 0xff )
3131 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003132
3133 /* We need a new block */
3134 ++hkdf->block_number;
3135 hkdf->offset_in_block = 0;
3136 status = psa_hmac_setup_internal( &hkdf->hmac,
3137 hkdf->prk, hash_length,
3138 hash_alg );
3139 if( status != PSA_SUCCESS )
3140 return( status );
3141 if( hkdf->block_number != 1 )
3142 {
3143 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3144 hkdf->output_block,
3145 hash_length );
3146 if( status != PSA_SUCCESS )
3147 return( status );
3148 }
3149 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3150 hkdf->info,
3151 hkdf->info_length );
3152 if( status != PSA_SUCCESS )
3153 return( status );
3154 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3155 &hkdf->block_number, 1 );
3156 if( status != PSA_SUCCESS )
3157 return( status );
3158 status = psa_hmac_finish_internal( &hkdf->hmac,
3159 hkdf->output_block,
3160 sizeof( hkdf->output_block ) );
3161 if( status != PSA_SUCCESS )
3162 return( status );
3163 }
3164
3165 return( PSA_SUCCESS );
3166}
3167#endif /* MBEDTLS_MD_C */
3168
Gilles Peskineeab56e42018-07-12 17:12:33 +02003169psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3170 uint8_t *output,
3171 size_t output_length )
3172{
3173 psa_status_t status;
3174
3175 if( output_length > generator->capacity )
3176 {
3177 generator->capacity = 0;
3178 /* Go through the error path to wipe all confidential data now
3179 * that the generator object is useless. */
3180 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3181 goto exit;
3182 }
3183 if( output_length == 0 &&
3184 generator->capacity == 0 && generator->alg == 0 )
3185 {
3186 /* Edge case: this is a blank or finished generator, and 0
3187 * bytes were requested. The right error in this case could
3188 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3189 * INSUFFICIENT_CAPACITY, which is right for a finished
3190 * generator, for consistency with the case when
3191 * output_length > 0. */
3192 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3193 }
3194 generator->capacity -= output_length;
3195
Gilles Peskinebef7f142018-07-12 17:22:21 +02003196#if defined(MBEDTLS_MD_C)
3197 if( PSA_ALG_IS_HKDF( generator->alg ) )
3198 {
3199 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3200 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3201 output, output_length );
3202 }
3203 else
3204#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003205 {
3206 return( PSA_ERROR_BAD_STATE );
3207 }
3208
3209exit:
3210 if( status != PSA_SUCCESS )
3211 {
3212 psa_generator_abort( generator );
3213 memset( output, '!', output_length );
3214 }
3215 return( status );
3216}
3217
Gilles Peskine08542d82018-07-19 17:05:42 +02003218#if defined(MBEDTLS_DES_C)
3219static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3220{
3221 if( data_size >= 8 )
3222 mbedtls_des_key_set_parity( data );
3223 if( data_size >= 16 )
3224 mbedtls_des_key_set_parity( data + 8 );
3225 if( data_size >= 24 )
3226 mbedtls_des_key_set_parity( data + 16 );
3227}
3228#endif /* MBEDTLS_DES_C */
3229
Gilles Peskineeab56e42018-07-12 17:12:33 +02003230psa_status_t psa_generator_import_key( psa_key_slot_t key,
3231 psa_key_type_t type,
3232 size_t bits,
3233 psa_crypto_generator_t *generator )
3234{
3235 uint8_t *data = NULL;
3236 size_t bytes = PSA_BITS_TO_BYTES( bits );
3237 psa_status_t status;
3238
3239 if( ! key_type_is_raw_bytes( type ) )
3240 return( PSA_ERROR_INVALID_ARGUMENT );
3241 if( bits % 8 != 0 )
3242 return( PSA_ERROR_INVALID_ARGUMENT );
3243 data = mbedtls_calloc( 1, bytes );
3244 if( data == NULL )
3245 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3246
3247 status = psa_generator_read( generator, data, bytes );
3248 if( status != PSA_SUCCESS )
3249 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003250#if defined(MBEDTLS_DES_C)
3251 if( type == PSA_KEY_TYPE_DES )
3252 psa_des_set_key_parity( data, bytes );
3253#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003254 status = psa_import_key( key, type, data, bytes );
3255
3256exit:
3257 mbedtls_free( data );
3258 return( status );
3259}
3260
3261
3262
3263/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003264/* Key derivation */
3265/****************************************************************/
3266
Gilles Peskinebef7f142018-07-12 17:22:21 +02003267/* Set up an HKDF-based generator. This is exactly the extract phase
3268 * of the HKDF algorithm. */
3269static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
3270 key_slot_t *slot,
3271 psa_algorithm_t hash_alg,
3272 const uint8_t *salt,
3273 size_t salt_length,
3274 const uint8_t *label,
3275 size_t label_length )
3276{
3277 psa_status_t status;
3278 status = psa_hmac_setup_internal( &hkdf->hmac,
3279 salt, salt_length,
3280 PSA_ALG_HMAC_HASH( hash_alg ) );
3281 if( status != PSA_SUCCESS )
3282 return( status );
3283 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3284 slot->data.raw.data,
3285 slot->data.raw.bytes );
3286 if( status != PSA_SUCCESS )
3287 return( status );
3288 status = psa_hmac_finish_internal( &hkdf->hmac,
3289 hkdf->prk,
3290 sizeof( hkdf->prk ) );
3291 if( status != PSA_SUCCESS )
3292 return( status );
3293 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3294 hkdf->block_number = 0;
3295 hkdf->info_length = label_length;
3296 if( label_length != 0 )
3297 {
3298 hkdf->info = mbedtls_calloc( 1, label_length );
3299 if( hkdf->info == NULL )
3300 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3301 memcpy( hkdf->info, label, label_length );
3302 }
3303 return( PSA_SUCCESS );
3304}
3305
Gilles Peskineea0fb492018-07-12 17:17:20 +02003306psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01003307 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003308 psa_algorithm_t alg,
3309 const uint8_t *salt,
3310 size_t salt_length,
3311 const uint8_t *label,
3312 size_t label_length,
3313 size_t capacity )
3314{
3315 key_slot_t *slot;
3316 psa_status_t status;
3317
3318 if( generator->alg != 0 )
3319 return( PSA_ERROR_BAD_STATE );
3320
3321 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
3322 if( status != PSA_SUCCESS )
3323 return( status );
3324 if( slot->type != PSA_KEY_TYPE_DERIVE )
3325 return( PSA_ERROR_INVALID_ARGUMENT );
3326
3327 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
3328 return( PSA_ERROR_INVALID_ARGUMENT );
3329
Gilles Peskinebef7f142018-07-12 17:22:21 +02003330#if defined(MBEDTLS_MD_C)
3331 if( PSA_ALG_IS_HKDF( alg ) )
3332 {
3333 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3334 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3335 if( hash_size == 0 )
3336 return( PSA_ERROR_NOT_SUPPORTED );
3337 if( capacity > 255 * hash_size )
3338 return( PSA_ERROR_INVALID_ARGUMENT );
3339 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3340 slot,
3341 hash_alg,
3342 salt, salt_length,
3343 label, label_length );
3344 }
3345 else
3346#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +02003347 {
3348 return( PSA_ERROR_NOT_SUPPORTED );
3349 }
3350
3351 /* Set generator->alg even on failure so that abort knows what to do. */
3352 generator->alg = alg;
3353 if( status == PSA_SUCCESS )
3354 generator->capacity = capacity;
3355 else
3356 psa_generator_abort( generator );
3357 return( status );
3358}
3359
3360
3361
3362/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003363/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02003364/****************************************************************/
3365
3366psa_status_t psa_generate_random( uint8_t *output,
3367 size_t output_size )
3368{
itayzafrir0adf0fc2018-09-06 16:24:41 +03003369 int ret;
3370 GUARD_MODULE_INITIALIZED;
3371
3372 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02003373 return( mbedtls_to_psa_error( ret ) );
3374}
3375
3376psa_status_t psa_generate_key( psa_key_slot_t key,
3377 psa_key_type_t type,
3378 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003379 const void *extra,
3380 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02003381{
Gilles Peskine12313cd2018-06-20 00:20:32 +02003382 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003383 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003384
Gilles Peskine53d991e2018-07-12 01:14:59 +02003385 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003386 return( PSA_ERROR_INVALID_ARGUMENT );
3387
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003388 status = psa_get_empty_key_slot( key, &slot );
3389 if( status != PSA_SUCCESS )
3390 return( status );
3391
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003392 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003393 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003394 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003395 if( status != PSA_SUCCESS )
3396 return( status );
3397 status = psa_generate_random( slot->data.raw.data,
3398 slot->data.raw.bytes );
3399 if( status != PSA_SUCCESS )
3400 {
3401 mbedtls_free( slot->data.raw.data );
3402 return( status );
3403 }
3404#if defined(MBEDTLS_DES_C)
3405 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02003406 psa_des_set_key_parity( slot->data.raw.data,
3407 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003408#endif /* MBEDTLS_DES_C */
3409 }
3410 else
3411
3412#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3413 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3414 {
3415 mbedtls_rsa_context *rsa;
3416 int ret;
3417 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003418 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3419 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02003420 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003421 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02003422 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003423 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003424 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003425#if INT_MAX < 0xffffffff
3426 /* Check that the uint32_t value passed by the caller fits
3427 * in the range supported by this implementation. */
3428 if( p->e > INT_MAX )
3429 return( PSA_ERROR_NOT_SUPPORTED );
3430#endif
3431 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003432 }
3433 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3434 if( rsa == NULL )
3435 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3436 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3437 ret = mbedtls_rsa_gen_key( rsa,
3438 mbedtls_ctr_drbg_random,
3439 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003440 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003441 exponent );
3442 if( ret != 0 )
3443 {
3444 mbedtls_rsa_free( rsa );
3445 mbedtls_free( rsa );
3446 return( mbedtls_to_psa_error( ret ) );
3447 }
3448 slot->data.rsa = rsa;
3449 }
3450 else
3451#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3452
3453#if defined(MBEDTLS_ECP_C)
3454 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3455 {
3456 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3457 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3458 const mbedtls_ecp_curve_info *curve_info =
3459 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3460 mbedtls_ecp_keypair *ecp;
3461 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003462 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003463 return( PSA_ERROR_NOT_SUPPORTED );
3464 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3465 return( PSA_ERROR_NOT_SUPPORTED );
3466 if( curve_info->bit_size != bits )
3467 return( PSA_ERROR_INVALID_ARGUMENT );
3468 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3469 if( ecp == NULL )
3470 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3471 mbedtls_ecp_keypair_init( ecp );
3472 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3473 mbedtls_ctr_drbg_random,
3474 &global_data.ctr_drbg );
3475 if( ret != 0 )
3476 {
3477 mbedtls_ecp_keypair_free( ecp );
3478 mbedtls_free( ecp );
3479 return( mbedtls_to_psa_error( ret ) );
3480 }
3481 slot->data.ecp = ecp;
3482 }
3483 else
3484#endif /* MBEDTLS_ECP_C */
3485
3486 return( PSA_ERROR_NOT_SUPPORTED );
3487
3488 slot->type = type;
3489 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003490}
3491
3492
3493/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003494/* Module setup */
3495/****************************************************************/
3496
Gilles Peskinee59236f2018-01-27 23:32:46 +01003497void mbedtls_psa_crypto_free( void )
3498{
Jaeden Amero045bd502018-06-26 14:00:08 +01003499 psa_key_slot_t key;
Gilles Peskine9a056342018-08-01 15:46:54 +02003500 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003501 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003502 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3503 mbedtls_entropy_free( &global_data.entropy );
3504 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3505}
3506
3507psa_status_t psa_crypto_init( void )
3508{
3509 int ret;
3510 const unsigned char drbg_seed[] = "PSA";
3511
3512 if( global_data.initialized != 0 )
3513 return( PSA_SUCCESS );
3514
3515 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3516 mbedtls_entropy_init( &global_data.entropy );
3517 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3518
3519 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3520 mbedtls_entropy_func,
3521 &global_data.entropy,
3522 drbg_seed, sizeof( drbg_seed ) - 1 );
3523 if( ret != 0 )
3524 goto exit;
3525
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003526 global_data.initialized = 1;
3527
Gilles Peskinee59236f2018-01-27 23:32:46 +01003528exit:
3529 if( ret != 0 )
3530 mbedtls_psa_crypto_free( );
3531 return( mbedtls_to_psa_error( ret ) );
3532}
3533
3534#endif /* MBEDTLS_PSA_CRYPTO_C */