blob: 44862424d8f230893df6724cb166fbb7b8769c1f [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
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001227 if( PSA_ALG_IS_AEAD( alg ) )
1228 alg &= ~PSA_ALG_AEAD_TAG_LENGTH_MASK;
1229
Gilles Peskine8c9def32018-02-08 10:02:12 +01001230 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1231 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001232 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001233 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001234 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001235 mode = MBEDTLS_MODE_STREAM;
1236 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001237 case PSA_ALG_CTR:
1238 mode = MBEDTLS_MODE_CTR;
1239 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001240 case PSA_ALG_CFB:
1241 mode = MBEDTLS_MODE_CFB;
1242 break;
1243 case PSA_ALG_OFB:
1244 mode = MBEDTLS_MODE_OFB;
1245 break;
1246 case PSA_ALG_CBC_NO_PADDING:
1247 mode = MBEDTLS_MODE_CBC;
1248 break;
1249 case PSA_ALG_CBC_PKCS7:
1250 mode = MBEDTLS_MODE_CBC;
1251 break;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001252 case PSA_ALG_CCM & ~PSA_ALG_AEAD_TAG_LENGTH_MASK:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001253 mode = MBEDTLS_MODE_CCM;
1254 break;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001255 case PSA_ALG_GCM & ~PSA_ALG_AEAD_TAG_LENGTH_MASK:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001256 mode = MBEDTLS_MODE_GCM;
1257 break;
1258 default:
1259 return( NULL );
1260 }
1261 }
1262 else if( alg == PSA_ALG_CMAC )
1263 mode = MBEDTLS_MODE_ECB;
1264 else if( alg == PSA_ALG_GMAC )
1265 mode = MBEDTLS_MODE_GCM;
1266 else
1267 return( NULL );
1268
1269 switch( key_type )
1270 {
1271 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001272 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001273 break;
1274 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001275 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1276 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001277 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001278 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001279 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001280 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001281 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1282 * but two-key Triple-DES is functionally three-key Triple-DES
1283 * with K1=K3, so that's how we present it to mbedtls. */
1284 if( key_bits == 128 )
1285 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001286 break;
1287 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001288 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001289 break;
1290 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001291 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001292 break;
1293 default:
1294 return( NULL );
1295 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001296 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001297 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001298
Jaeden Amero23bbb752018-06-26 14:16:54 +01001299 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1300 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001301}
1302
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001303static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001304{
Gilles Peskine2d277862018-06-18 15:41:12 +02001305 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001306 {
1307 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001308 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001309 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001310 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001311 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001312 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001313 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001314 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001315 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001316 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001317 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001318 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001319 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001320 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001321 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001322 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001323 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001324 return( 128 );
1325 default:
1326 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001327 }
1328}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001329
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001330/* Initialize the MAC operation structure. Once this function has been
1331 * called, psa_mac_abort can run and will do the right thing. */
1332static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1333 psa_algorithm_t alg )
1334{
1335 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1336
1337 operation->alg = alg;
1338 operation->key_set = 0;
1339 operation->iv_set = 0;
1340 operation->iv_required = 0;
1341 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001342 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001343
1344#if defined(MBEDTLS_CMAC_C)
1345 if( alg == PSA_ALG_CMAC )
1346 {
1347 operation->iv_required = 0;
1348 mbedtls_cipher_init( &operation->ctx.cmac );
1349 status = PSA_SUCCESS;
1350 }
1351 else
1352#endif /* MBEDTLS_CMAC_C */
1353#if defined(MBEDTLS_MD_C)
1354 if( PSA_ALG_IS_HMAC( operation->alg ) )
1355 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001356 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1357 operation->ctx.hmac.hash_ctx.alg = 0;
1358 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001359 }
1360 else
1361#endif /* MBEDTLS_MD_C */
1362 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001363 if( ! PSA_ALG_IS_MAC( alg ) )
1364 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001365 }
1366
1367 if( status != PSA_SUCCESS )
1368 memset( operation, 0, sizeof( *operation ) );
1369 return( status );
1370}
1371
Gilles Peskine01126fa2018-07-12 17:04:55 +02001372#if defined(MBEDTLS_MD_C)
1373static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1374{
1375 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1376 return( psa_hash_abort( &hmac->hash_ctx ) );
1377}
1378#endif /* MBEDTLS_MD_C */
1379
Gilles Peskine8c9def32018-02-08 10:02:12 +01001380psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1381{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001382 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001383 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001384 /* The object has (apparently) been initialized but it is not
1385 * in use. It's ok to call abort on such an object, and there's
1386 * nothing to do. */
1387 return( PSA_SUCCESS );
1388 }
1389 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001390#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001391 if( operation->alg == PSA_ALG_CMAC )
1392 {
1393 mbedtls_cipher_free( &operation->ctx.cmac );
1394 }
1395 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001396#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001397#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001398 if( PSA_ALG_IS_HMAC( operation->alg ) )
1399 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001400 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001401 }
1402 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001403#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001404 {
1405 /* Sanity check (shouldn't happen: operation->alg should
1406 * always have been initialized to a valid value). */
1407 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001408 }
Moran Peker41deec42018-04-04 15:43:05 +03001409
Gilles Peskine8c9def32018-02-08 10:02:12 +01001410 operation->alg = 0;
1411 operation->key_set = 0;
1412 operation->iv_set = 0;
1413 operation->iv_required = 0;
1414 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001415 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001416
Gilles Peskine8c9def32018-02-08 10:02:12 +01001417 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001418
1419bad_state:
1420 /* If abort is called on an uninitialized object, we can't trust
1421 * anything. Wipe the object in case it contains confidential data.
1422 * This may result in a memory leak if a pointer gets overwritten,
1423 * but it's too late to do anything about this. */
1424 memset( operation, 0, sizeof( *operation ) );
1425 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001426}
1427
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001428#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001429static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001430 size_t key_bits,
1431 key_slot_t *slot,
1432 const mbedtls_cipher_info_t *cipher_info )
1433{
1434 int ret;
1435
1436 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001437
1438 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1439 if( ret != 0 )
1440 return( ret );
1441
1442 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1443 slot->data.raw.data,
1444 key_bits );
1445 return( ret );
1446}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001447#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001448
Gilles Peskine248051a2018-06-20 16:09:38 +02001449#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001450static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1451 const uint8_t *key,
1452 size_t key_length,
1453 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001454{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001455 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001456 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001457 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001458 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001459 psa_status_t status;
1460
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001461 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1462 * overflow below. This should never trigger if the hash algorithm
1463 * is implemented correctly. */
1464 /* The size checks against the ipad and opad buffers cannot be written
1465 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1466 * because that triggers -Wlogical-op on GCC 7.3. */
1467 if( block_size > sizeof( ipad ) )
1468 return( PSA_ERROR_NOT_SUPPORTED );
1469 if( block_size > sizeof( hmac->opad ) )
1470 return( PSA_ERROR_NOT_SUPPORTED );
1471 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001472 return( PSA_ERROR_NOT_SUPPORTED );
1473
Gilles Peskined223b522018-06-11 18:12:58 +02001474 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001475 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001476 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001477 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001478 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001479 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001480 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001481 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001482 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001483 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001484 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001485 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001486 }
Gilles Peskine96889972018-07-12 17:07:03 +02001487 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1488 * but it is permitted. It is common when HMAC is used in HKDF, for
1489 * example. Don't call `memcpy` in the 0-length because `key` could be
1490 * an invalid pointer which would make the behavior undefined. */
1491 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001492 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001493
Gilles Peskined223b522018-06-11 18:12:58 +02001494 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1495 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001496 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001497 ipad[i] ^= 0x36;
1498 memset( ipad + key_length, 0x36, block_size - key_length );
1499
1500 /* Copy the key material from ipad to opad, flipping the requisite bits,
1501 * and filling the rest of opad with the requisite constant. */
1502 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001503 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1504 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001505
Gilles Peskine01126fa2018-07-12 17:04:55 +02001506 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001507 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001508 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001509
Gilles Peskine01126fa2018-07-12 17:04:55 +02001510 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001511
1512cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001513 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001514
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001515 return( status );
1516}
Gilles Peskine248051a2018-06-20 16:09:38 +02001517#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001518
Gilles Peskine89167cb2018-07-08 20:12:23 +02001519static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1520 psa_key_slot_t key,
1521 psa_algorithm_t alg,
1522 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001523{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001524 psa_status_t status;
1525 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001526 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001527 psa_key_usage_t usage =
1528 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001529 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
1530 psa_algorithm_t full_length_alg = alg & ~PSA_ALG_MAC_TRUNCATION_MASK;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001531
Gilles Peskined911eb72018-08-14 15:18:45 +02001532 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001533 if( status != PSA_SUCCESS )
1534 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001535 if( is_sign )
1536 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001537
Gilles Peskine89167cb2018-07-08 20:12:23 +02001538 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001539 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001540 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001541 key_bits = psa_get_key_bits( slot );
1542
Gilles Peskine8c9def32018-02-08 10:02:12 +01001543#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001544 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001545 {
1546 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001547 mbedtls_cipher_info_from_psa( full_length_alg,
1548 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001549 int ret;
1550 if( cipher_info == NULL )
1551 {
1552 status = PSA_ERROR_NOT_SUPPORTED;
1553 goto exit;
1554 }
1555 operation->mac_size = cipher_info->block_size;
1556 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1557 status = mbedtls_to_psa_error( ret );
1558 }
1559 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001560#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001561#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001562 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001563 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001564 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001565 if( hash_alg == 0 )
1566 {
1567 status = PSA_ERROR_NOT_SUPPORTED;
1568 goto exit;
1569 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001570
1571 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1572 /* Sanity check. This shouldn't fail on a valid configuration. */
1573 if( operation->mac_size == 0 ||
1574 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1575 {
1576 status = PSA_ERROR_NOT_SUPPORTED;
1577 goto exit;
1578 }
1579
Gilles Peskine01126fa2018-07-12 17:04:55 +02001580 if( slot->type != PSA_KEY_TYPE_HMAC )
1581 {
1582 status = PSA_ERROR_INVALID_ARGUMENT;
1583 goto exit;
1584 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001585
Gilles Peskine01126fa2018-07-12 17:04:55 +02001586 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1587 slot->data.raw.data,
1588 slot->data.raw.bytes,
1589 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001590 }
1591 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001592#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001593 {
1594 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001595 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001596
Gilles Peskined911eb72018-08-14 15:18:45 +02001597 if( truncated == 0 )
1598 {
1599 /* The "normal" case: untruncated algorithm. Nothing to do. */
1600 }
1601 else if( truncated < 4 )
1602 {
1603 /* Too small to make any sense. Reject. 4 bytes is too small for
1604 * security but ancient protocols with 32-bit MACs do exist. */
1605 status = PSA_ERROR_NOT_SUPPORTED;
1606 }
1607 else if( truncated > operation->mac_size )
1608 {
1609 /* It's impossible to "truncate" to a larger length. */
1610 status = PSA_ERROR_INVALID_ARGUMENT;
1611 }
1612 else
1613 operation->mac_size = truncated;
1614
Gilles Peskinefbfac682018-07-08 20:51:54 +02001615exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001616 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001617 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001618 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001619 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001620 else
1621 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001622 operation->key_set = 1;
1623 }
1624 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001625}
1626
Gilles Peskine89167cb2018-07-08 20:12:23 +02001627psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1628 psa_key_slot_t key,
1629 psa_algorithm_t alg )
1630{
1631 return( psa_mac_setup( operation, key, alg, 1 ) );
1632}
1633
1634psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1635 psa_key_slot_t key,
1636 psa_algorithm_t alg )
1637{
1638 return( psa_mac_setup( operation, key, alg, 0 ) );
1639}
1640
Gilles Peskine8c9def32018-02-08 10:02:12 +01001641psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1642 const uint8_t *input,
1643 size_t input_length )
1644{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001645 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001646 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001647 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001648 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001649 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001650 operation->has_input = 1;
1651
Gilles Peskine8c9def32018-02-08 10:02:12 +01001652#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001653 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001654 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001655 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1656 input, input_length );
1657 status = mbedtls_to_psa_error( ret );
1658 }
1659 else
1660#endif /* MBEDTLS_CMAC_C */
1661#if defined(MBEDTLS_MD_C)
1662 if( PSA_ALG_IS_HMAC( operation->alg ) )
1663 {
1664 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1665 input_length );
1666 }
1667 else
1668#endif /* MBEDTLS_MD_C */
1669 {
1670 /* This shouldn't happen if `operation` was initialized by
1671 * a setup function. */
1672 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001673 }
1674
Gilles Peskinefbfac682018-07-08 20:51:54 +02001675cleanup:
1676 if( status != PSA_SUCCESS )
1677 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001678 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001679}
1680
Gilles Peskine01126fa2018-07-12 17:04:55 +02001681#if defined(MBEDTLS_MD_C)
1682static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1683 uint8_t *mac,
1684 size_t mac_size )
1685{
1686 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1687 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1688 size_t hash_size = 0;
1689 size_t block_size = psa_get_hash_block_size( hash_alg );
1690 psa_status_t status;
1691
Gilles Peskine01126fa2018-07-12 17:04:55 +02001692 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1693 if( status != PSA_SUCCESS )
1694 return( status );
1695 /* From here on, tmp needs to be wiped. */
1696
1697 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1698 if( status != PSA_SUCCESS )
1699 goto exit;
1700
1701 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1702 if( status != PSA_SUCCESS )
1703 goto exit;
1704
1705 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1706 if( status != PSA_SUCCESS )
1707 goto exit;
1708
Gilles Peskined911eb72018-08-14 15:18:45 +02001709 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1710 if( status != PSA_SUCCESS )
1711 goto exit;
1712
1713 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001714
1715exit:
1716 mbedtls_zeroize( tmp, hash_size );
1717 return( status );
1718}
1719#endif /* MBEDTLS_MD_C */
1720
mohammad16036df908f2018-04-02 08:34:15 -07001721static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001722 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001723 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001724{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001725 if( ! operation->key_set )
1726 return( PSA_ERROR_BAD_STATE );
1727 if( operation->iv_required && ! operation->iv_set )
1728 return( PSA_ERROR_BAD_STATE );
1729
Gilles Peskine8c9def32018-02-08 10:02:12 +01001730 if( mac_size < operation->mac_size )
1731 return( PSA_ERROR_BUFFER_TOO_SMALL );
1732
Gilles Peskine8c9def32018-02-08 10:02:12 +01001733#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001734 if( operation->alg == PSA_ALG_CMAC )
1735 {
Gilles Peskined911eb72018-08-14 15:18:45 +02001736 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
1737 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
1738 if( ret == 0 )
1739 memcpy( mac, tmp, mac_size );
1740 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001741 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001742 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001743 else
1744#endif /* MBEDTLS_CMAC_C */
1745#if defined(MBEDTLS_MD_C)
1746 if( PSA_ALG_IS_HMAC( operation->alg ) )
1747 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001748 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02001749 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001750 }
1751 else
1752#endif /* MBEDTLS_MD_C */
1753 {
1754 /* This shouldn't happen if `operation` was initialized by
1755 * a setup function. */
1756 return( PSA_ERROR_BAD_STATE );
1757 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001758}
1759
Gilles Peskineacd4be32018-07-08 19:56:25 +02001760psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1761 uint8_t *mac,
1762 size_t mac_size,
1763 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001764{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001765 psa_status_t status;
1766
1767 /* Fill the output buffer with something that isn't a valid mac
1768 * (barring an attack on the mac and deliberately-crafted input),
1769 * in case the caller doesn't check the return status properly. */
1770 *mac_length = mac_size;
1771 /* If mac_size is 0 then mac may be NULL and then the
1772 * call to memset would have undefined behavior. */
1773 if( mac_size != 0 )
1774 memset( mac, '!', mac_size );
1775
Gilles Peskine89167cb2018-07-08 20:12:23 +02001776 if( ! operation->is_sign )
1777 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001778 status = PSA_ERROR_BAD_STATE;
1779 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001780 }
mohammad16036df908f2018-04-02 08:34:15 -07001781
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001782 status = psa_mac_finish_internal( operation, mac, mac_size );
1783
1784cleanup:
1785 if( status == PSA_SUCCESS )
1786 {
1787 status = psa_mac_abort( operation );
1788 if( status == PSA_SUCCESS )
1789 *mac_length = operation->mac_size;
1790 else
1791 memset( mac, '!', mac_size );
1792 }
1793 else
1794 psa_mac_abort( operation );
1795 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001796}
1797
Gilles Peskineacd4be32018-07-08 19:56:25 +02001798psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1799 const uint8_t *mac,
1800 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001801{
Gilles Peskine828ed142018-06-18 23:25:51 +02001802 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001803 psa_status_t status;
1804
Gilles Peskine89167cb2018-07-08 20:12:23 +02001805 if( operation->is_sign )
1806 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001807 status = PSA_ERROR_BAD_STATE;
1808 goto cleanup;
1809 }
1810 if( operation->mac_size != mac_length )
1811 {
1812 status = PSA_ERROR_INVALID_SIGNATURE;
1813 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001814 }
mohammad16036df908f2018-04-02 08:34:15 -07001815
1816 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001817 actual_mac, sizeof( actual_mac ) );
1818
1819 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1820 status = PSA_ERROR_INVALID_SIGNATURE;
1821
1822cleanup:
1823 if( status == PSA_SUCCESS )
1824 status = psa_mac_abort( operation );
1825 else
1826 psa_mac_abort( operation );
1827
Gilles Peskined911eb72018-08-14 15:18:45 +02001828 mbedtls_zeroize( actual_mac, mac_length );
1829
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001830 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001831}
1832
1833
Gilles Peskine20035e32018-02-03 22:44:14 +01001834
Gilles Peskine20035e32018-02-03 22:44:14 +01001835/****************************************************************/
1836/* Asymmetric cryptography */
1837/****************************************************************/
1838
Gilles Peskine2b450e32018-06-27 15:42:46 +02001839#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001840/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001841 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001842static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1843 size_t hash_length,
1844 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001845{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001846 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001847 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001848 *md_alg = mbedtls_md_get_type( md_info );
1849
1850 /* The Mbed TLS RSA module uses an unsigned int for hash length
1851 * parameters. Validate that it fits so that we don't risk an
1852 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001853#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001854 if( hash_length > UINT_MAX )
1855 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001856#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001857
1858#if defined(MBEDTLS_PKCS1_V15)
1859 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
1860 * must be correct. */
1861 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
1862 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001863 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001864 if( md_info == NULL )
1865 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001866 if( mbedtls_md_get_size( md_info ) != hash_length )
1867 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001868 }
1869#endif /* MBEDTLS_PKCS1_V15 */
1870
1871#if defined(MBEDTLS_PKCS1_V21)
1872 /* PSS requires a hash internally. */
1873 if( PSA_ALG_IS_RSA_PSS( alg ) )
1874 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001875 if( md_info == NULL )
1876 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001877 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001878#endif /* MBEDTLS_PKCS1_V21 */
1879
Gilles Peskine61b91d42018-06-08 16:09:36 +02001880 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001881}
1882
Gilles Peskine2b450e32018-06-27 15:42:46 +02001883static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1884 psa_algorithm_t alg,
1885 const uint8_t *hash,
1886 size_t hash_length,
1887 uint8_t *signature,
1888 size_t signature_size,
1889 size_t *signature_length )
1890{
1891 psa_status_t status;
1892 int ret;
1893 mbedtls_md_type_t md_alg;
1894
1895 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1896 if( status != PSA_SUCCESS )
1897 return( status );
1898
Gilles Peskine630a18a2018-06-29 17:49:35 +02001899 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001900 return( PSA_ERROR_BUFFER_TOO_SMALL );
1901
1902#if defined(MBEDTLS_PKCS1_V15)
1903 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1904 {
1905 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1906 MBEDTLS_MD_NONE );
1907 ret = mbedtls_rsa_pkcs1_sign( rsa,
1908 mbedtls_ctr_drbg_random,
1909 &global_data.ctr_drbg,
1910 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001911 md_alg,
1912 (unsigned int) hash_length,
1913 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001914 signature );
1915 }
1916 else
1917#endif /* MBEDTLS_PKCS1_V15 */
1918#if defined(MBEDTLS_PKCS1_V21)
1919 if( PSA_ALG_IS_RSA_PSS( alg ) )
1920 {
1921 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1922 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1923 mbedtls_ctr_drbg_random,
1924 &global_data.ctr_drbg,
1925 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001926 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001927 (unsigned int) hash_length,
1928 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001929 signature );
1930 }
1931 else
1932#endif /* MBEDTLS_PKCS1_V21 */
1933 {
1934 return( PSA_ERROR_INVALID_ARGUMENT );
1935 }
1936
1937 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02001938 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02001939 return( mbedtls_to_psa_error( ret ) );
1940}
1941
1942static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1943 psa_algorithm_t alg,
1944 const uint8_t *hash,
1945 size_t hash_length,
1946 const uint8_t *signature,
1947 size_t signature_length )
1948{
1949 psa_status_t status;
1950 int ret;
1951 mbedtls_md_type_t md_alg;
1952
1953 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1954 if( status != PSA_SUCCESS )
1955 return( status );
1956
Gilles Peskine630a18a2018-06-29 17:49:35 +02001957 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001958 return( PSA_ERROR_BUFFER_TOO_SMALL );
1959
1960#if defined(MBEDTLS_PKCS1_V15)
1961 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1962 {
1963 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1964 MBEDTLS_MD_NONE );
1965 ret = mbedtls_rsa_pkcs1_verify( rsa,
1966 mbedtls_ctr_drbg_random,
1967 &global_data.ctr_drbg,
1968 MBEDTLS_RSA_PUBLIC,
1969 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001970 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001971 hash,
1972 signature );
1973 }
1974 else
1975#endif /* MBEDTLS_PKCS1_V15 */
1976#if defined(MBEDTLS_PKCS1_V21)
1977 if( PSA_ALG_IS_RSA_PSS( alg ) )
1978 {
1979 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1980 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1981 mbedtls_ctr_drbg_random,
1982 &global_data.ctr_drbg,
1983 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001984 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001985 (unsigned int) hash_length,
1986 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001987 signature );
1988 }
1989 else
1990#endif /* MBEDTLS_PKCS1_V21 */
1991 {
1992 return( PSA_ERROR_INVALID_ARGUMENT );
1993 }
Gilles Peskineef12c632018-09-13 20:37:48 +02001994
1995 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
1996 * the rest of the signature is invalid". This has little use in
1997 * practice and PSA doesn't report this distinction. */
1998 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
1999 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002000 return( mbedtls_to_psa_error( ret ) );
2001}
2002#endif /* MBEDTLS_RSA_C */
2003
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002004#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002005/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2006 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2007 * (even though these functions don't modify it). */
2008static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2009 psa_algorithm_t alg,
2010 const uint8_t *hash,
2011 size_t hash_length,
2012 uint8_t *signature,
2013 size_t signature_size,
2014 size_t *signature_length )
2015{
2016 int ret;
2017 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002018 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002019 mbedtls_mpi_init( &r );
2020 mbedtls_mpi_init( &s );
2021
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002022 if( signature_size < 2 * curve_bytes )
2023 {
2024 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2025 goto cleanup;
2026 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002027
2028 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2029 {
2030 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2031 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2032 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2033 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2034 hash, hash_length,
2035 md_alg ) );
2036 }
2037 else
2038 {
2039 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2040 hash, hash_length,
2041 mbedtls_ctr_drbg_random,
2042 &global_data.ctr_drbg ) );
2043 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002044
2045 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2046 signature,
2047 curve_bytes ) );
2048 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2049 signature + curve_bytes,
2050 curve_bytes ) );
2051
2052cleanup:
2053 mbedtls_mpi_free( &r );
2054 mbedtls_mpi_free( &s );
2055 if( ret == 0 )
2056 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002057 return( mbedtls_to_psa_error( ret ) );
2058}
2059
2060static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2061 const uint8_t *hash,
2062 size_t hash_length,
2063 const uint8_t *signature,
2064 size_t signature_length )
2065{
2066 int ret;
2067 mbedtls_mpi r, s;
2068 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2069 mbedtls_mpi_init( &r );
2070 mbedtls_mpi_init( &s );
2071
2072 if( signature_length != 2 * curve_bytes )
2073 return( PSA_ERROR_INVALID_SIGNATURE );
2074
2075 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2076 signature,
2077 curve_bytes ) );
2078 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2079 signature + curve_bytes,
2080 curve_bytes ) );
2081
2082 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2083 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002084
2085cleanup:
2086 mbedtls_mpi_free( &r );
2087 mbedtls_mpi_free( &s );
2088 return( mbedtls_to_psa_error( ret ) );
2089}
2090#endif /* MBEDTLS_ECDSA_C */
2091
Gilles Peskine61b91d42018-06-08 16:09:36 +02002092psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2093 psa_algorithm_t alg,
2094 const uint8_t *hash,
2095 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002096 uint8_t *signature,
2097 size_t signature_size,
2098 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002099{
2100 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002101 psa_status_t status;
2102
2103 *signature_length = signature_size;
2104
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002105 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2106 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002107 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002108 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002109 {
2110 status = PSA_ERROR_INVALID_ARGUMENT;
2111 goto exit;
2112 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002113
Gilles Peskine20035e32018-02-03 22:44:14 +01002114#if defined(MBEDTLS_RSA_C)
2115 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2116 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002117 status = psa_rsa_sign( slot->data.rsa,
2118 alg,
2119 hash, hash_length,
2120 signature, signature_size,
2121 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002122 }
2123 else
2124#endif /* defined(MBEDTLS_RSA_C) */
2125#if defined(MBEDTLS_ECP_C)
2126 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2127 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002128#if defined(MBEDTLS_ECDSA_C)
2129 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002130 status = psa_ecdsa_sign( slot->data.ecp,
2131 alg,
2132 hash, hash_length,
2133 signature, signature_size,
2134 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002135 else
2136#endif /* defined(MBEDTLS_ECDSA_C) */
2137 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002138 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002139 }
itayzafrir5c753392018-05-08 11:18:38 +03002140 }
2141 else
2142#endif /* defined(MBEDTLS_ECP_C) */
2143 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002144 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002145 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002146
2147exit:
2148 /* Fill the unused part of the output buffer (the whole buffer on error,
2149 * the trailing part on success) with something that isn't a valid mac
2150 * (barring an attack on the mac and deliberately-crafted input),
2151 * in case the caller doesn't check the return status properly. */
2152 if( status == PSA_SUCCESS )
2153 memset( signature + *signature_length, '!',
2154 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002155 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002156 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002157 /* If signature_size is 0 then we have nothing to do. We must not call
2158 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002159 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002160}
2161
2162psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2163 psa_algorithm_t alg,
2164 const uint8_t *hash,
2165 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002166 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002167 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002168{
2169 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002170 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002171
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002172 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2173 if( status != PSA_SUCCESS )
2174 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002175
Gilles Peskine61b91d42018-06-08 16:09:36 +02002176#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002177 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002178 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002179 return( psa_rsa_verify( slot->data.rsa,
2180 alg,
2181 hash, hash_length,
2182 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002183 }
2184 else
2185#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002186#if defined(MBEDTLS_ECP_C)
2187 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2188 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002189#if defined(MBEDTLS_ECDSA_C)
2190 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002191 return( psa_ecdsa_verify( slot->data.ecp,
2192 hash, hash_length,
2193 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002194 else
2195#endif /* defined(MBEDTLS_ECDSA_C) */
2196 {
2197 return( PSA_ERROR_INVALID_ARGUMENT );
2198 }
itayzafrir5c753392018-05-08 11:18:38 +03002199 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002200 else
2201#endif /* defined(MBEDTLS_ECP_C) */
2202 {
2203 return( PSA_ERROR_NOT_SUPPORTED );
2204 }
2205}
2206
Gilles Peskine072ac562018-06-30 00:21:29 +02002207#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2208static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2209 mbedtls_rsa_context *rsa )
2210{
2211 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2212 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2213 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2214 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2215}
2216#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2217
Gilles Peskine61b91d42018-06-08 16:09:36 +02002218psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2219 psa_algorithm_t alg,
2220 const uint8_t *input,
2221 size_t input_length,
2222 const uint8_t *salt,
2223 size_t salt_length,
2224 uint8_t *output,
2225 size_t output_size,
2226 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002227{
2228 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002229 psa_status_t status;
2230
Darryl Green5cc689a2018-07-24 15:34:10 +01002231 (void) input;
2232 (void) input_length;
2233 (void) salt;
2234 (void) output;
2235 (void) output_size;
2236
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002237 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002238
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002239 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2240 return( PSA_ERROR_INVALID_ARGUMENT );
2241
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002242 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2243 if( status != PSA_SUCCESS )
2244 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002245 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2246 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002247 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002248
2249#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002250 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002251 {
2252 mbedtls_rsa_context *rsa = slot->data.rsa;
2253 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002254 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002255 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002256#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002257 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002258 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002259 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2260 mbedtls_ctr_drbg_random,
2261 &global_data.ctr_drbg,
2262 MBEDTLS_RSA_PUBLIC,
2263 input_length,
2264 input,
2265 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002266 }
2267 else
2268#endif /* MBEDTLS_PKCS1_V15 */
2269#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002270 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002271 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002272 psa_rsa_oaep_set_padding_mode( alg, rsa );
2273 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2274 mbedtls_ctr_drbg_random,
2275 &global_data.ctr_drbg,
2276 MBEDTLS_RSA_PUBLIC,
2277 salt, salt_length,
2278 input_length,
2279 input,
2280 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002281 }
2282 else
2283#endif /* MBEDTLS_PKCS1_V21 */
2284 {
2285 return( PSA_ERROR_INVALID_ARGUMENT );
2286 }
2287 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002288 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002289 return( mbedtls_to_psa_error( ret ) );
2290 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002291 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002292#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002293 {
2294 return( PSA_ERROR_NOT_SUPPORTED );
2295 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002296}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002297
Gilles Peskine61b91d42018-06-08 16:09:36 +02002298psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2299 psa_algorithm_t alg,
2300 const uint8_t *input,
2301 size_t input_length,
2302 const uint8_t *salt,
2303 size_t salt_length,
2304 uint8_t *output,
2305 size_t output_size,
2306 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002307{
2308 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002309 psa_status_t status;
2310
Darryl Green5cc689a2018-07-24 15:34:10 +01002311 (void) input;
2312 (void) input_length;
2313 (void) salt;
2314 (void) output;
2315 (void) output_size;
2316
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002317 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002318
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002319 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2320 return( PSA_ERROR_INVALID_ARGUMENT );
2321
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002322 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2323 if( status != PSA_SUCCESS )
2324 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002325 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2326 return( PSA_ERROR_INVALID_ARGUMENT );
2327
2328#if defined(MBEDTLS_RSA_C)
2329 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2330 {
2331 mbedtls_rsa_context *rsa = slot->data.rsa;
2332 int ret;
2333
Gilles Peskine630a18a2018-06-29 17:49:35 +02002334 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002335 return( PSA_ERROR_INVALID_ARGUMENT );
2336
2337#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002338 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002339 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002340 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2341 mbedtls_ctr_drbg_random,
2342 &global_data.ctr_drbg,
2343 MBEDTLS_RSA_PRIVATE,
2344 output_length,
2345 input,
2346 output,
2347 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002348 }
2349 else
2350#endif /* MBEDTLS_PKCS1_V15 */
2351#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002352 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002353 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002354 psa_rsa_oaep_set_padding_mode( alg, rsa );
2355 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2356 mbedtls_ctr_drbg_random,
2357 &global_data.ctr_drbg,
2358 MBEDTLS_RSA_PRIVATE,
2359 salt, salt_length,
2360 output_length,
2361 input,
2362 output,
2363 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002364 }
2365 else
2366#endif /* MBEDTLS_PKCS1_V21 */
2367 {
2368 return( PSA_ERROR_INVALID_ARGUMENT );
2369 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002370
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002371 return( mbedtls_to_psa_error( ret ) );
2372 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002373 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002374#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002375 {
2376 return( PSA_ERROR_NOT_SUPPORTED );
2377 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002378}
Gilles Peskine20035e32018-02-03 22:44:14 +01002379
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002380
2381
mohammad1603503973b2018-03-12 15:59:30 +02002382/****************************************************************/
2383/* Symmetric cryptography */
2384/****************************************************************/
2385
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002386/* Initialize the cipher operation structure. Once this function has been
2387 * called, psa_cipher_abort can run and will do the right thing. */
2388static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2389 psa_algorithm_t alg )
2390{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002391 if( ! PSA_ALG_IS_CIPHER( alg ) )
2392 {
2393 memset( operation, 0, sizeof( *operation ) );
2394 return( PSA_ERROR_INVALID_ARGUMENT );
2395 }
2396
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002397 operation->alg = alg;
2398 operation->key_set = 0;
2399 operation->iv_set = 0;
2400 operation->iv_required = 1;
2401 operation->iv_size = 0;
2402 operation->block_size = 0;
2403 mbedtls_cipher_init( &operation->ctx.cipher );
2404 return( PSA_SUCCESS );
2405}
2406
Gilles Peskinee553c652018-06-04 16:22:46 +02002407static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2408 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002409 psa_algorithm_t alg,
2410 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002411{
2412 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2413 psa_status_t status;
2414 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002415 size_t key_bits;
2416 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002417 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2418 PSA_KEY_USAGE_ENCRYPT :
2419 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002420
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002421 status = psa_cipher_init( operation, alg );
2422 if( status != PSA_SUCCESS )
2423 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002424
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002425 status = psa_get_key_from_slot( key, &slot, usage, alg);
2426 if( status != PSA_SUCCESS )
2427 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002428 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002429
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002430 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002431 if( cipher_info == NULL )
2432 return( PSA_ERROR_NOT_SUPPORTED );
2433
mohammad1603503973b2018-03-12 15:59:30 +02002434 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002435 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002436 {
2437 psa_cipher_abort( operation );
2438 return( mbedtls_to_psa_error( ret ) );
2439 }
2440
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002441#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002442 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002443 {
2444 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2445 unsigned char keys[24];
2446 memcpy( keys, slot->data.raw.data, 16 );
2447 memcpy( keys + 16, slot->data.raw.data, 8 );
2448 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2449 keys,
2450 192, cipher_operation );
2451 }
2452 else
2453#endif
2454 {
2455 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2456 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002457 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002458 }
Moran Peker41deec42018-04-04 15:43:05 +03002459 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002460 {
2461 psa_cipher_abort( operation );
2462 return( mbedtls_to_psa_error( ret ) );
2463 }
2464
mohammad16038481e742018-03-18 13:57:31 +02002465#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002466 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002467 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002468 case PSA_ALG_CBC_NO_PADDING:
2469 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2470 MBEDTLS_PADDING_NONE );
2471 break;
2472 case PSA_ALG_CBC_PKCS7:
2473 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2474 MBEDTLS_PADDING_PKCS7 );
2475 break;
2476 default:
2477 /* The algorithm doesn't involve padding. */
2478 ret = 0;
2479 break;
2480 }
2481 if( ret != 0 )
2482 {
2483 psa_cipher_abort( operation );
2484 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002485 }
2486#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2487
mohammad1603503973b2018-03-12 15:59:30 +02002488 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002489 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2490 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2491 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002492 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002493 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002494 }
mohammad1603503973b2018-03-12 15:59:30 +02002495
Moran Peker395db872018-05-31 14:07:14 +03002496 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002497}
2498
Gilles Peskinefe119512018-07-08 21:39:34 +02002499psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2500 psa_key_slot_t key,
2501 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002502{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002503 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002504}
2505
Gilles Peskinefe119512018-07-08 21:39:34 +02002506psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2507 psa_key_slot_t key,
2508 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002509{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002510 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002511}
2512
Gilles Peskinefe119512018-07-08 21:39:34 +02002513psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2514 unsigned char *iv,
2515 size_t iv_size,
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 Peker41deec42018-04-04 15:43:05 +03002525 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002526 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002527 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002528 goto exit;
2529 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002530 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2531 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002532 if( ret != 0 )
2533 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002534 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002535 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002536 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002537
mohammad16038481e742018-03-18 13:57:31 +02002538 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002539 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002540
Moran Peker395db872018-05-31 14:07:14 +03002541exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002542 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002543 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002544 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002545}
2546
Gilles Peskinefe119512018-07-08 21:39:34 +02002547psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2548 const unsigned char *iv,
2549 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002550{
itayzafrir534bd7c2018-08-02 13:56:32 +03002551 psa_status_t status;
2552 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002553 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002554 {
2555 status = PSA_ERROR_BAD_STATE;
2556 goto exit;
2557 }
Moran Pekera28258c2018-05-29 16:25:04 +03002558 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002559 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002560 status = PSA_ERROR_INVALID_ARGUMENT;
2561 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002562 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002563 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2564 status = mbedtls_to_psa_error( ret );
2565exit:
2566 if( status == PSA_SUCCESS )
2567 operation->iv_set = 1;
2568 else
mohammad1603503973b2018-03-12 15:59:30 +02002569 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002570 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002571}
2572
Gilles Peskinee553c652018-06-04 16:22:46 +02002573psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2574 const uint8_t *input,
2575 size_t input_length,
2576 unsigned char *output,
2577 size_t output_size,
2578 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002579{
itayzafrir534bd7c2018-08-02 13:56:32 +03002580 psa_status_t status;
2581 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002582 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002583 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002584 {
2585 /* Take the unprocessed partial block left over from previous
2586 * update calls, if any, plus the input to this call. Remove
2587 * the last partial block, if any. You get the data that will be
2588 * output in this call. */
2589 expected_output_size =
2590 ( operation->ctx.cipher.unprocessed_len + input_length )
2591 / operation->block_size * operation->block_size;
2592 }
2593 else
2594 {
2595 expected_output_size = input_length;
2596 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002597
Gilles Peskine89d789c2018-06-04 17:17:16 +02002598 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002599 {
2600 status = PSA_ERROR_BUFFER_TOO_SMALL;
2601 goto exit;
2602 }
mohammad160382759612018-03-12 18:16:40 +02002603
mohammad1603503973b2018-03-12 15:59:30 +02002604 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002605 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002606 status = mbedtls_to_psa_error( ret );
2607exit:
2608 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002609 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002610 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002611}
2612
Gilles Peskinee553c652018-06-04 16:22:46 +02002613psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2614 uint8_t *output,
2615 size_t output_size,
2616 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002617{
Janos Follath315b51c2018-07-09 16:04:51 +01002618 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2619 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002620 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002621
mohammad1603503973b2018-03-12 15:59:30 +02002622 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002623 {
Janos Follath315b51c2018-07-09 16:04:51 +01002624 status = PSA_ERROR_BAD_STATE;
2625 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002626 }
2627 if( operation->iv_required && ! operation->iv_set )
2628 {
Janos Follath315b51c2018-07-09 16:04:51 +01002629 status = PSA_ERROR_BAD_STATE;
2630 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002631 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002632
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002633 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002634 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2635 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002636 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002637 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002638 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002639 }
2640
Janos Follath315b51c2018-07-09 16:04:51 +01002641 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2642 temp_output_buffer,
2643 output_length );
2644 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002645 {
Janos Follath315b51c2018-07-09 16:04:51 +01002646 status = mbedtls_to_psa_error( cipher_ret );
2647 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002648 }
Janos Follath315b51c2018-07-09 16:04:51 +01002649
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002650 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002651 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002652 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002653 memcpy( output, temp_output_buffer, *output_length );
2654 else
2655 {
Janos Follath315b51c2018-07-09 16:04:51 +01002656 status = PSA_ERROR_BUFFER_TOO_SMALL;
2657 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002658 }
mohammad1603503973b2018-03-12 15:59:30 +02002659
Janos Follath279ab8e2018-07-09 16:13:21 +01002660 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002661 status = psa_cipher_abort( operation );
2662
2663 return( status );
2664
2665error:
2666
2667 *output_length = 0;
2668
Janos Follath279ab8e2018-07-09 16:13:21 +01002669 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002670 (void) psa_cipher_abort( operation );
2671
2672 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002673}
2674
Gilles Peskinee553c652018-06-04 16:22:46 +02002675psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2676{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002677 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002678 {
2679 /* The object has (apparently) been initialized but it is not
2680 * in use. It's ok to call abort on such an object, and there's
2681 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002682 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002683 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002684
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002685 /* Sanity check (shouldn't happen: operation->alg should
2686 * always have been initialized to a valid value). */
2687 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2688 return( PSA_ERROR_BAD_STATE );
2689
mohammad1603503973b2018-03-12 15:59:30 +02002690 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002691
Moran Peker41deec42018-04-04 15:43:05 +03002692 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002693 operation->key_set = 0;
2694 operation->iv_set = 0;
2695 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002696 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002697 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002698
Moran Peker395db872018-05-31 14:07:14 +03002699 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002700}
2701
Gilles Peskinea0655c32018-04-30 17:06:50 +02002702
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002703
mohammad16038cc1cee2018-03-28 01:21:33 +03002704/****************************************************************/
2705/* Key Policy */
2706/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002707
mohammad160327010052018-07-03 13:16:15 +03002708#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002709void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002710{
Gilles Peskine803ce742018-06-18 16:07:14 +02002711 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002712}
2713
Gilles Peskine2d277862018-06-18 15:41:12 +02002714void psa_key_policy_set_usage( psa_key_policy_t *policy,
2715 psa_key_usage_t usage,
2716 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002717{
mohammad16034eed7572018-03-28 05:14:59 -07002718 policy->usage = usage;
2719 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002720}
2721
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002722psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002723{
mohammad16036df908f2018-04-02 08:34:15 -07002724 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002725}
2726
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002727psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002728{
mohammad16036df908f2018-04-02 08:34:15 -07002729 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002730}
mohammad160327010052018-07-03 13:16:15 +03002731#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002732
Gilles Peskine2d277862018-06-18 15:41:12 +02002733psa_status_t psa_set_key_policy( psa_key_slot_t key,
2734 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002735{
2736 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002737 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002738
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002739 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002740 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002741
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002742 status = psa_get_empty_key_slot( key, &slot );
2743 if( status != PSA_SUCCESS )
2744 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002745
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002746 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2747 PSA_KEY_USAGE_ENCRYPT |
2748 PSA_KEY_USAGE_DECRYPT |
2749 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002750 PSA_KEY_USAGE_VERIFY |
2751 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002752 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002753
mohammad16036df908f2018-04-02 08:34:15 -07002754 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002755
2756 return( PSA_SUCCESS );
2757}
2758
Gilles Peskine2d277862018-06-18 15:41:12 +02002759psa_status_t psa_get_key_policy( psa_key_slot_t key,
2760 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002761{
2762 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002763 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002764
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002765 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002766 return( PSA_ERROR_INVALID_ARGUMENT );
2767
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002768 status = psa_get_key_slot( key, &slot );
2769 if( status != PSA_SUCCESS )
2770 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002771
mohammad16036df908f2018-04-02 08:34:15 -07002772 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002773
2774 return( PSA_SUCCESS );
2775}
Gilles Peskine20035e32018-02-03 22:44:14 +01002776
Gilles Peskinea0655c32018-04-30 17:06:50 +02002777
2778
mohammad1603804cd712018-03-20 22:44:08 +02002779/****************************************************************/
2780/* Key Lifetime */
2781/****************************************************************/
2782
Gilles Peskine2d277862018-06-18 15:41:12 +02002783psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2784 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002785{
2786 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002787 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002788
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002789 status = psa_get_key_slot( key, &slot );
2790 if( status != PSA_SUCCESS )
2791 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002792
mohammad1603804cd712018-03-20 22:44:08 +02002793 *lifetime = slot->lifetime;
2794
2795 return( PSA_SUCCESS );
2796}
2797
Gilles Peskine2d277862018-06-18 15:41:12 +02002798psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002799 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002800{
2801 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002802 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002803
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002804 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2805 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002806 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2807 return( PSA_ERROR_INVALID_ARGUMENT );
2808
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002809 status = psa_get_empty_key_slot( key, &slot );
2810 if( status != PSA_SUCCESS )
2811 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002812
Moran Pekerd7326592018-05-29 16:56:39 +03002813 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002814 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002815
mohammad1603060ad8a2018-03-20 14:28:38 -07002816 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002817
2818 return( PSA_SUCCESS );
2819}
2820
Gilles Peskine20035e32018-02-03 22:44:14 +01002821
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002822
mohammad16035955c982018-04-26 00:53:03 +03002823/****************************************************************/
2824/* AEAD */
2825/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002826
Gilles Peskineedf9a652018-08-17 18:11:56 +02002827typedef struct
2828{
2829 key_slot_t *slot;
2830 const mbedtls_cipher_info_t *cipher_info;
2831 union
2832 {
2833#if defined(MBEDTLS_CCM_C)
2834 mbedtls_ccm_context ccm;
2835#endif /* MBEDTLS_CCM_C */
2836#if defined(MBEDTLS_GCM_C)
2837 mbedtls_gcm_context gcm;
2838#endif /* MBEDTLS_GCM_C */
2839 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002840 psa_algorithm_t core_alg;
2841 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02002842 uint8_t tag_length;
2843} aead_operation_t;
2844
2845static void psa_aead_abort( aead_operation_t *operation,
2846 psa_algorithm_t alg )
2847{
2848 switch( alg )
2849 {
2850#if defined(MBEDTLS_CCM_C)
2851 case PSA_ALG_CCM:
2852 mbedtls_ccm_free( &operation->ctx.ccm );
2853 break;
2854#endif /* MBEDTLS_CCM_C */
2855#if defined(MBEDTLS_CCM_C)
2856 case PSA_ALG_GCM:
2857 mbedtls_gcm_free( &operation->ctx.gcm );
2858 break;
2859#endif /* MBEDTLS_GCM_C */
2860 }
2861}
2862
2863static psa_status_t psa_aead_setup( aead_operation_t *operation,
2864 psa_key_slot_t key,
2865 psa_key_usage_t usage,
2866 psa_algorithm_t alg )
2867{
2868 psa_status_t status;
2869 size_t key_bits;
2870 mbedtls_cipher_id_t cipher_id;
2871
2872 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
2873 if( status != PSA_SUCCESS )
2874 return( status );
2875
2876 key_bits = psa_get_key_bits( operation->slot );
2877
2878 operation->cipher_info =
2879 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
2880 &cipher_id );
2881 if( operation->cipher_info == NULL )
2882 return( PSA_ERROR_NOT_SUPPORTED );
2883
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002884 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02002885 {
2886#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002887 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
2888 operation->core_alg = PSA_ALG_CCM;
2889 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02002890 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
2891 return( PSA_ERROR_INVALID_ARGUMENT );
2892 mbedtls_ccm_init( &operation->ctx.ccm );
2893 status = mbedtls_to_psa_error(
2894 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
2895 operation->slot->data.raw.data,
2896 (unsigned int) key_bits ) );
2897 if( status != 0 )
2898 goto cleanup;
2899 break;
2900#endif /* MBEDTLS_CCM_C */
2901
2902#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002903 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
2904 operation->core_alg = PSA_ALG_GCM;
2905 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02002906 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
2907 return( PSA_ERROR_INVALID_ARGUMENT );
2908 mbedtls_gcm_init( &operation->ctx.gcm );
2909 status = mbedtls_to_psa_error(
2910 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
2911 operation->slot->data.raw.data,
2912 (unsigned int) key_bits ) );
2913 break;
2914#endif /* MBEDTLS_GCM_C */
2915
2916 default:
2917 return( PSA_ERROR_NOT_SUPPORTED );
2918 }
2919
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002920 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
2921 {
2922 status = PSA_ERROR_INVALID_ARGUMENT;
2923 goto cleanup;
2924 }
2925 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
2926 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
2927 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
2928 * In both cases, mbedtls_xxx will validate the tag length below. */
2929
Gilles Peskineedf9a652018-08-17 18:11:56 +02002930 return( PSA_SUCCESS );
2931
2932cleanup:
2933 psa_aead_abort( operation, alg );
2934 return( status );
2935}
2936
mohammad16035955c982018-04-26 00:53:03 +03002937psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2938 psa_algorithm_t alg,
2939 const uint8_t *nonce,
2940 size_t nonce_length,
2941 const uint8_t *additional_data,
2942 size_t additional_data_length,
2943 const uint8_t *plaintext,
2944 size_t plaintext_length,
2945 uint8_t *ciphertext,
2946 size_t ciphertext_size,
2947 size_t *ciphertext_length )
2948{
mohammad16035955c982018-04-26 00:53:03 +03002949 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02002950 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03002951 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02002952
mohammad1603f08a5502018-06-03 15:05:47 +03002953 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002954
Gilles Peskineedf9a652018-08-17 18:11:56 +02002955 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002956 if( status != PSA_SUCCESS )
2957 return( status );
mohammad16035955c982018-04-26 00:53:03 +03002958
Gilles Peskineedf9a652018-08-17 18:11:56 +02002959 /* For all currently supported modes, the tag is at the end of the
2960 * ciphertext. */
2961 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
2962 {
2963 status = PSA_ERROR_BUFFER_TOO_SMALL;
2964 goto exit;
2965 }
2966 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03002967
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002968 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03002969 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02002970 status = mbedtls_to_psa_error(
2971 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
2972 MBEDTLS_GCM_ENCRYPT,
2973 plaintext_length,
2974 nonce, nonce_length,
2975 additional_data, additional_data_length,
2976 plaintext, ciphertext,
2977 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03002978 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002979 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03002980 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02002981 status = mbedtls_to_psa_error(
2982 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
2983 plaintext_length,
2984 nonce, nonce_length,
2985 additional_data,
2986 additional_data_length,
2987 plaintext, ciphertext,
2988 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03002989 }
mohammad16035c8845f2018-05-09 05:40:09 -07002990 else
2991 {
mohammad1603554faad2018-06-03 15:07:38 +03002992 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002993 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002994
Gilles Peskineedf9a652018-08-17 18:11:56 +02002995 if( status != PSA_SUCCESS && ciphertext_size != 0 )
2996 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002997
Gilles Peskineedf9a652018-08-17 18:11:56 +02002998exit:
2999 psa_aead_abort( &operation, alg );
3000 if( status == PSA_SUCCESS )
3001 *ciphertext_length = plaintext_length + operation.tag_length;
3002 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003003}
3004
Gilles Peskineee652a32018-06-01 19:23:52 +02003005/* Locate the tag in a ciphertext buffer containing the encrypted data
3006 * followed by the tag. Return the length of the part preceding the tag in
3007 * *plaintext_length. This is the size of the plaintext in modes where
3008 * the encrypted data has the same size as the plaintext, such as
3009 * CCM and GCM. */
3010static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3011 const uint8_t *ciphertext,
3012 size_t ciphertext_length,
3013 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003014 const uint8_t **p_tag )
3015{
3016 size_t payload_length;
3017 if( tag_length > ciphertext_length )
3018 return( PSA_ERROR_INVALID_ARGUMENT );
3019 payload_length = ciphertext_length - tag_length;
3020 if( payload_length > plaintext_size )
3021 return( PSA_ERROR_BUFFER_TOO_SMALL );
3022 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003023 return( PSA_SUCCESS );
3024}
3025
mohammad16035955c982018-04-26 00:53:03 +03003026psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3027 psa_algorithm_t alg,
3028 const uint8_t *nonce,
3029 size_t nonce_length,
3030 const uint8_t *additional_data,
3031 size_t additional_data_length,
3032 const uint8_t *ciphertext,
3033 size_t ciphertext_length,
3034 uint8_t *plaintext,
3035 size_t plaintext_size,
3036 size_t *plaintext_length )
3037{
mohammad16035955c982018-04-26 00:53:03 +03003038 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003039 aead_operation_t operation;
3040 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003041
Gilles Peskineee652a32018-06-01 19:23:52 +02003042 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003043
Gilles Peskineedf9a652018-08-17 18:11:56 +02003044 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003045 if( status != PSA_SUCCESS )
3046 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003047
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003048 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003049 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003050 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003051 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003052 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003053 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003054 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003055
Gilles Peskineedf9a652018-08-17 18:11:56 +02003056 status = mbedtls_to_psa_error(
3057 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3058 ciphertext_length - operation.tag_length,
3059 nonce, nonce_length,
3060 additional_data,
3061 additional_data_length,
3062 tag, operation.tag_length,
3063 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003064 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003065 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003066 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003067 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003068 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003069 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003070 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003071 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003072
Gilles Peskineedf9a652018-08-17 18:11:56 +02003073 status = mbedtls_to_psa_error(
3074 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3075 ciphertext_length - operation.tag_length,
3076 nonce, nonce_length,
3077 additional_data,
3078 additional_data_length,
3079 ciphertext, plaintext,
3080 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003081 }
mohammad160339574652018-06-01 04:39:53 -07003082 else
3083 {
mohammad1603554faad2018-06-03 15:07:38 +03003084 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003085 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003086
Gilles Peskineedf9a652018-08-17 18:11:56 +02003087 if( status != PSA_SUCCESS && plaintext_size != 0 )
3088 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003089
Gilles Peskineedf9a652018-08-17 18:11:56 +02003090exit:
3091 psa_aead_abort( &operation, alg );
3092 if( status == PSA_SUCCESS )
3093 *plaintext_length = ciphertext_length - operation.tag_length;
3094 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003095}
3096
Gilles Peskinea0655c32018-04-30 17:06:50 +02003097
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003098
Gilles Peskine20035e32018-02-03 22:44:14 +01003099/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003100/* Generators */
3101/****************************************************************/
3102
3103psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3104{
3105 psa_status_t status = PSA_SUCCESS;
3106 if( generator->alg == 0 )
3107 {
3108 /* The object has (apparently) been initialized but it is not
3109 * in use. It's ok to call abort on such an object, and there's
3110 * nothing to do. */
3111 }
3112 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003113#if defined(MBEDTLS_MD_C)
3114 if( PSA_ALG_IS_HKDF( generator->alg ) )
3115 {
3116 mbedtls_free( generator->ctx.hkdf.info );
3117 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3118 }
3119 else
3120#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003121 {
3122 status = PSA_ERROR_BAD_STATE;
3123 }
3124 memset( generator, 0, sizeof( *generator ) );
3125 return( status );
3126}
3127
3128
3129psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3130 size_t *capacity)
3131{
3132 *capacity = generator->capacity;
3133 return( PSA_SUCCESS );
3134}
3135
Gilles Peskinebef7f142018-07-12 17:22:21 +02003136#if defined(MBEDTLS_MD_C)
3137/* Read some bytes from an HKDF-based generator. This performs a chunk
3138 * of the expand phase of the HKDF algorithm. */
3139static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3140 psa_algorithm_t hash_alg,
3141 uint8_t *output,
3142 size_t output_length )
3143{
3144 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3145 psa_status_t status;
3146
3147 while( output_length != 0 )
3148 {
3149 /* Copy what remains of the current block */
3150 uint8_t n = hash_length - hkdf->offset_in_block;
3151 if( n > output_length )
3152 n = (uint8_t) output_length;
3153 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3154 output += n;
3155 output_length -= n;
3156 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003157 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003158 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003159 /* We can't be wanting more output after block 0xff, otherwise
3160 * the capacity check in psa_generator_read() would have
3161 * prevented this call. It could happen only if the generator
3162 * object was corrupted or if this function is called directly
3163 * inside the library. */
3164 if( hkdf->block_number == 0xff )
3165 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003166
3167 /* We need a new block */
3168 ++hkdf->block_number;
3169 hkdf->offset_in_block = 0;
3170 status = psa_hmac_setup_internal( &hkdf->hmac,
3171 hkdf->prk, hash_length,
3172 hash_alg );
3173 if( status != PSA_SUCCESS )
3174 return( status );
3175 if( hkdf->block_number != 1 )
3176 {
3177 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3178 hkdf->output_block,
3179 hash_length );
3180 if( status != PSA_SUCCESS )
3181 return( status );
3182 }
3183 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3184 hkdf->info,
3185 hkdf->info_length );
3186 if( status != PSA_SUCCESS )
3187 return( status );
3188 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3189 &hkdf->block_number, 1 );
3190 if( status != PSA_SUCCESS )
3191 return( status );
3192 status = psa_hmac_finish_internal( &hkdf->hmac,
3193 hkdf->output_block,
3194 sizeof( hkdf->output_block ) );
3195 if( status != PSA_SUCCESS )
3196 return( status );
3197 }
3198
3199 return( PSA_SUCCESS );
3200}
3201#endif /* MBEDTLS_MD_C */
3202
Gilles Peskineeab56e42018-07-12 17:12:33 +02003203psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3204 uint8_t *output,
3205 size_t output_length )
3206{
3207 psa_status_t status;
3208
3209 if( output_length > generator->capacity )
3210 {
3211 generator->capacity = 0;
3212 /* Go through the error path to wipe all confidential data now
3213 * that the generator object is useless. */
3214 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3215 goto exit;
3216 }
3217 if( output_length == 0 &&
3218 generator->capacity == 0 && generator->alg == 0 )
3219 {
3220 /* Edge case: this is a blank or finished generator, and 0
3221 * bytes were requested. The right error in this case could
3222 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3223 * INSUFFICIENT_CAPACITY, which is right for a finished
3224 * generator, for consistency with the case when
3225 * output_length > 0. */
3226 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3227 }
3228 generator->capacity -= output_length;
3229
Gilles Peskinebef7f142018-07-12 17:22:21 +02003230#if defined(MBEDTLS_MD_C)
3231 if( PSA_ALG_IS_HKDF( generator->alg ) )
3232 {
3233 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3234 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3235 output, output_length );
3236 }
3237 else
3238#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003239 {
3240 return( PSA_ERROR_BAD_STATE );
3241 }
3242
3243exit:
3244 if( status != PSA_SUCCESS )
3245 {
3246 psa_generator_abort( generator );
3247 memset( output, '!', output_length );
3248 }
3249 return( status );
3250}
3251
Gilles Peskine08542d82018-07-19 17:05:42 +02003252#if defined(MBEDTLS_DES_C)
3253static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3254{
3255 if( data_size >= 8 )
3256 mbedtls_des_key_set_parity( data );
3257 if( data_size >= 16 )
3258 mbedtls_des_key_set_parity( data + 8 );
3259 if( data_size >= 24 )
3260 mbedtls_des_key_set_parity( data + 16 );
3261}
3262#endif /* MBEDTLS_DES_C */
3263
Gilles Peskineeab56e42018-07-12 17:12:33 +02003264psa_status_t psa_generator_import_key( psa_key_slot_t key,
3265 psa_key_type_t type,
3266 size_t bits,
3267 psa_crypto_generator_t *generator )
3268{
3269 uint8_t *data = NULL;
3270 size_t bytes = PSA_BITS_TO_BYTES( bits );
3271 psa_status_t status;
3272
3273 if( ! key_type_is_raw_bytes( type ) )
3274 return( PSA_ERROR_INVALID_ARGUMENT );
3275 if( bits % 8 != 0 )
3276 return( PSA_ERROR_INVALID_ARGUMENT );
3277 data = mbedtls_calloc( 1, bytes );
3278 if( data == NULL )
3279 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3280
3281 status = psa_generator_read( generator, data, bytes );
3282 if( status != PSA_SUCCESS )
3283 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003284#if defined(MBEDTLS_DES_C)
3285 if( type == PSA_KEY_TYPE_DES )
3286 psa_des_set_key_parity( data, bytes );
3287#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003288 status = psa_import_key( key, type, data, bytes );
3289
3290exit:
3291 mbedtls_free( data );
3292 return( status );
3293}
3294
3295
3296
3297/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003298/* Key derivation */
3299/****************************************************************/
3300
Gilles Peskinebef7f142018-07-12 17:22:21 +02003301/* Set up an HKDF-based generator. This is exactly the extract phase
3302 * of the HKDF algorithm. */
3303static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
3304 key_slot_t *slot,
3305 psa_algorithm_t hash_alg,
3306 const uint8_t *salt,
3307 size_t salt_length,
3308 const uint8_t *label,
3309 size_t label_length )
3310{
3311 psa_status_t status;
3312 status = psa_hmac_setup_internal( &hkdf->hmac,
3313 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003314 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003315 if( status != PSA_SUCCESS )
3316 return( status );
3317 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3318 slot->data.raw.data,
3319 slot->data.raw.bytes );
3320 if( status != PSA_SUCCESS )
3321 return( status );
3322 status = psa_hmac_finish_internal( &hkdf->hmac,
3323 hkdf->prk,
3324 sizeof( hkdf->prk ) );
3325 if( status != PSA_SUCCESS )
3326 return( status );
3327 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3328 hkdf->block_number = 0;
3329 hkdf->info_length = label_length;
3330 if( label_length != 0 )
3331 {
3332 hkdf->info = mbedtls_calloc( 1, label_length );
3333 if( hkdf->info == NULL )
3334 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3335 memcpy( hkdf->info, label, label_length );
3336 }
3337 return( PSA_SUCCESS );
3338}
3339
Gilles Peskineea0fb492018-07-12 17:17:20 +02003340psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01003341 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003342 psa_algorithm_t alg,
3343 const uint8_t *salt,
3344 size_t salt_length,
3345 const uint8_t *label,
3346 size_t label_length,
3347 size_t capacity )
3348{
3349 key_slot_t *slot;
3350 psa_status_t status;
3351
3352 if( generator->alg != 0 )
3353 return( PSA_ERROR_BAD_STATE );
3354
3355 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
3356 if( status != PSA_SUCCESS )
3357 return( status );
3358 if( slot->type != PSA_KEY_TYPE_DERIVE )
3359 return( PSA_ERROR_INVALID_ARGUMENT );
3360
3361 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
3362 return( PSA_ERROR_INVALID_ARGUMENT );
3363
Gilles Peskinebef7f142018-07-12 17:22:21 +02003364#if defined(MBEDTLS_MD_C)
3365 if( PSA_ALG_IS_HKDF( alg ) )
3366 {
3367 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3368 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3369 if( hash_size == 0 )
3370 return( PSA_ERROR_NOT_SUPPORTED );
3371 if( capacity > 255 * hash_size )
3372 return( PSA_ERROR_INVALID_ARGUMENT );
3373 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3374 slot,
3375 hash_alg,
3376 salt, salt_length,
3377 label, label_length );
3378 }
3379 else
3380#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +02003381 {
3382 return( PSA_ERROR_NOT_SUPPORTED );
3383 }
3384
3385 /* Set generator->alg even on failure so that abort knows what to do. */
3386 generator->alg = alg;
3387 if( status == PSA_SUCCESS )
3388 generator->capacity = capacity;
3389 else
3390 psa_generator_abort( generator );
3391 return( status );
3392}
3393
3394
3395
3396/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003397/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02003398/****************************************************************/
3399
3400psa_status_t psa_generate_random( uint8_t *output,
3401 size_t output_size )
3402{
itayzafrir0adf0fc2018-09-06 16:24:41 +03003403 int ret;
3404 GUARD_MODULE_INITIALIZED;
3405
3406 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02003407 return( mbedtls_to_psa_error( ret ) );
3408}
3409
3410psa_status_t psa_generate_key( psa_key_slot_t key,
3411 psa_key_type_t type,
3412 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003413 const void *extra,
3414 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02003415{
Gilles Peskine12313cd2018-06-20 00:20:32 +02003416 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003417 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003418
Gilles Peskine53d991e2018-07-12 01:14:59 +02003419 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003420 return( PSA_ERROR_INVALID_ARGUMENT );
3421
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003422 status = psa_get_empty_key_slot( key, &slot );
3423 if( status != PSA_SUCCESS )
3424 return( status );
3425
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003426 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003427 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003428 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003429 if( status != PSA_SUCCESS )
3430 return( status );
3431 status = psa_generate_random( slot->data.raw.data,
3432 slot->data.raw.bytes );
3433 if( status != PSA_SUCCESS )
3434 {
3435 mbedtls_free( slot->data.raw.data );
3436 return( status );
3437 }
3438#if defined(MBEDTLS_DES_C)
3439 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02003440 psa_des_set_key_parity( slot->data.raw.data,
3441 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003442#endif /* MBEDTLS_DES_C */
3443 }
3444 else
3445
3446#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3447 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3448 {
3449 mbedtls_rsa_context *rsa;
3450 int ret;
3451 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003452 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3453 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02003454 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003455 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02003456 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003457 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003458 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003459#if INT_MAX < 0xffffffff
3460 /* Check that the uint32_t value passed by the caller fits
3461 * in the range supported by this implementation. */
3462 if( p->e > INT_MAX )
3463 return( PSA_ERROR_NOT_SUPPORTED );
3464#endif
3465 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003466 }
3467 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3468 if( rsa == NULL )
3469 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3470 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3471 ret = mbedtls_rsa_gen_key( rsa,
3472 mbedtls_ctr_drbg_random,
3473 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003474 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003475 exponent );
3476 if( ret != 0 )
3477 {
3478 mbedtls_rsa_free( rsa );
3479 mbedtls_free( rsa );
3480 return( mbedtls_to_psa_error( ret ) );
3481 }
3482 slot->data.rsa = rsa;
3483 }
3484 else
3485#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3486
3487#if defined(MBEDTLS_ECP_C)
3488 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3489 {
3490 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3491 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3492 const mbedtls_ecp_curve_info *curve_info =
3493 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3494 mbedtls_ecp_keypair *ecp;
3495 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003496 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003497 return( PSA_ERROR_NOT_SUPPORTED );
3498 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3499 return( PSA_ERROR_NOT_SUPPORTED );
3500 if( curve_info->bit_size != bits )
3501 return( PSA_ERROR_INVALID_ARGUMENT );
3502 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3503 if( ecp == NULL )
3504 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3505 mbedtls_ecp_keypair_init( ecp );
3506 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3507 mbedtls_ctr_drbg_random,
3508 &global_data.ctr_drbg );
3509 if( ret != 0 )
3510 {
3511 mbedtls_ecp_keypair_free( ecp );
3512 mbedtls_free( ecp );
3513 return( mbedtls_to_psa_error( ret ) );
3514 }
3515 slot->data.ecp = ecp;
3516 }
3517 else
3518#endif /* MBEDTLS_ECP_C */
3519
3520 return( PSA_ERROR_NOT_SUPPORTED );
3521
3522 slot->type = type;
3523 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003524}
3525
3526
3527/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003528/* Module setup */
3529/****************************************************************/
3530
Gilles Peskinee59236f2018-01-27 23:32:46 +01003531void mbedtls_psa_crypto_free( void )
3532{
Jaeden Amero045bd502018-06-26 14:00:08 +01003533 psa_key_slot_t key;
Gilles Peskine9a056342018-08-01 15:46:54 +02003534 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003535 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003536 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3537 mbedtls_entropy_free( &global_data.entropy );
3538 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3539}
3540
3541psa_status_t psa_crypto_init( void )
3542{
3543 int ret;
3544 const unsigned char drbg_seed[] = "PSA";
3545
3546 if( global_data.initialized != 0 )
3547 return( PSA_SUCCESS );
3548
3549 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3550 mbedtls_entropy_init( &global_data.entropy );
3551 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3552
3553 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3554 mbedtls_entropy_func,
3555 &global_data.entropy,
3556 drbg_seed, sizeof( drbg_seed ) - 1 );
3557 if( ret != 0 )
3558 goto exit;
3559
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003560 global_data.initialized = 1;
3561
Gilles Peskinee59236f2018-01-27 23:32:46 +01003562exit:
3563 if( ret != 0 )
3564 mbedtls_psa_crypto_free( );
3565 return( mbedtls_to_psa_error( ret ) );
3566}
3567
3568#endif /* MBEDTLS_PSA_CRYPTO_C */