blob: 82af92086395514e586ac76d9dd08618e9e709df [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030029/*
30 * In case MBEDTLS_PSA_CRYPTO_SPM is defined the code is built for SPM (Secure
31 * Partition Manager) integration which separate the code into two parts
32 * NSPE (Non-Secure Process Environment) and SPE (Secure Process Environment).
33 * In this mode an additional header file should be included.
34 */
mohammad160327010052018-07-03 13:16:15 +030035#if defined(MBEDTLS_PSA_CRYPTO_SPM)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030036/*
37 * PSA_CRYPTO_SECURE means that this file is compiled to the SPE side.
38 * some headers will be affected by this flag.
39 */
mohammad160327010052018-07-03 13:16:15 +030040#define PSA_CRYPTO_SECURE 1
41#include "crypto_spe.h"
42#endif
43
Gilles Peskinee59236f2018-01-27 23:32:46 +010044#include "psa/crypto.h"
45
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include <stdlib.h>
47#include <string.h>
48#if defined(MBEDTLS_PLATFORM_C)
49#include "mbedtls/platform.h"
50#else
51#define mbedtls_calloc calloc
52#define mbedtls_free free
53#endif
54
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020056#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020057#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/blowfish.h"
59#include "mbedtls/camellia.h"
60#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010065#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010066#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/error.h"
68#include "mbedtls/gcm.h"
69#include "mbedtls/md2.h"
70#include "mbedtls/md4.h"
71#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010072#include "mbedtls/md.h"
73#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010074#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/pk_internal.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010077#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/sha1.h"
79#include "mbedtls/sha256.h"
80#include "mbedtls/sha512.h"
81#include "mbedtls/xtea.h"
82
Gilles Peskinee59236f2018-01-27 23:32:46 +010083
84
Gilles Peskine996deb12018-08-01 15:45:45 +020085#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
86
Gilles Peskinee59236f2018-01-27 23:32:46 +010087/* Implementation that should never be optimized out by the compiler */
88static void mbedtls_zeroize( void *v, size_t n )
89{
90 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
91}
92
Gilles Peskine9ef733f2018-02-07 21:05:37 +010093/* constant-time buffer comparison */
94static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
95{
96 size_t i;
97 unsigned char diff = 0;
98
99 for( i = 0; i < n; i++ )
100 diff |= a[i] ^ b[i];
101
102 return( diff );
103}
104
105
106
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100107/****************************************************************/
108/* Global data, support functions and library management */
109/****************************************************************/
110
111/* Number of key slots (plus one because 0 is not used).
112 * The value is a compile-time constant for now, for simplicity. */
Gilles Peskine828ed142018-06-18 23:25:51 +0200113#define PSA_KEY_SLOT_COUNT 32
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100114
Gilles Peskine2d277862018-06-18 15:41:12 +0200115typedef struct
116{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100117 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +0300118 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200119 psa_key_lifetime_t lifetime;
Gilles Peskine2d277862018-06-18 15:41:12 +0200120 union
121 {
122 struct raw_data
123 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100124 uint8_t *data;
125 size_t bytes;
126 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100127#if defined(MBEDTLS_RSA_C)
128 mbedtls_rsa_context *rsa;
129#endif /* MBEDTLS_RSA_C */
130#if defined(MBEDTLS_ECP_C)
131 mbedtls_ecp_keypair *ecp;
132#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100133 } data;
134} key_slot_t;
135
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200136static int key_type_is_raw_bytes( psa_key_type_t type )
137{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200138 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200139}
140
Gilles Peskine2d277862018-06-18 15:41:12 +0200141typedef struct
142{
Gilles Peskinee59236f2018-01-27 23:32:46 +0100143 int initialized;
144 mbedtls_entropy_context entropy;
145 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200146 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinee59236f2018-01-27 23:32:46 +0100147} psa_global_data_t;
148
149static psa_global_data_t global_data;
150
itayzafrir0adf0fc2018-09-06 16:24:41 +0300151#define GUARD_MODULE_INITIALIZED \
152 if( global_data.initialized == 0 ) \
153 return( PSA_ERROR_BAD_STATE );
154
Gilles Peskinee59236f2018-01-27 23:32:46 +0100155static psa_status_t mbedtls_to_psa_error( int ret )
156{
Gilles Peskinea5905292018-02-07 20:59:33 +0100157 /* If there's both a high-level code and low-level code, dispatch on
158 * the high-level code. */
159 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100160 {
161 case 0:
162 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100163
164 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
165 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
166 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
167 return( PSA_ERROR_NOT_SUPPORTED );
168 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
169 return( PSA_ERROR_HARDWARE_FAILURE );
170
171 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
172 return( PSA_ERROR_HARDWARE_FAILURE );
173
Gilles Peskine9a944802018-06-21 09:35:35 +0200174 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
175 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
176 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
177 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
178 case MBEDTLS_ERR_ASN1_INVALID_DATA:
179 return( PSA_ERROR_INVALID_ARGUMENT );
180 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
181 return( PSA_ERROR_INSUFFICIENT_MEMORY );
182 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
183 return( PSA_ERROR_BUFFER_TOO_SMALL );
184
Gilles Peskinea5905292018-02-07 20:59:33 +0100185 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
186 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
187 return( PSA_ERROR_NOT_SUPPORTED );
188 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
189 return( PSA_ERROR_HARDWARE_FAILURE );
190
191 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
192 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
193 return( PSA_ERROR_NOT_SUPPORTED );
194 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
195 return( PSA_ERROR_HARDWARE_FAILURE );
196
197 case MBEDTLS_ERR_CCM_BAD_INPUT:
198 return( PSA_ERROR_INVALID_ARGUMENT );
199 case MBEDTLS_ERR_CCM_AUTH_FAILED:
200 return( PSA_ERROR_INVALID_SIGNATURE );
201 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
202 return( PSA_ERROR_HARDWARE_FAILURE );
203
204 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
205 return( PSA_ERROR_NOT_SUPPORTED );
206 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
207 return( PSA_ERROR_INVALID_ARGUMENT );
208 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
209 return( PSA_ERROR_INSUFFICIENT_MEMORY );
210 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
211 return( PSA_ERROR_INVALID_PADDING );
212 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
213 return( PSA_ERROR_BAD_STATE );
214 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
215 return( PSA_ERROR_INVALID_SIGNATURE );
216 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
217 return( PSA_ERROR_TAMPERING_DETECTED );
218 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
219 return( PSA_ERROR_HARDWARE_FAILURE );
220
221 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
222 return( PSA_ERROR_HARDWARE_FAILURE );
223
224 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
225 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
226 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
227 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
228 return( PSA_ERROR_NOT_SUPPORTED );
229 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
230 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
231
232 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
233 return( PSA_ERROR_NOT_SUPPORTED );
234 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
235 return( PSA_ERROR_HARDWARE_FAILURE );
236
Gilles Peskinee59236f2018-01-27 23:32:46 +0100237 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
238 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
239 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
240 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100241
242 case MBEDTLS_ERR_GCM_AUTH_FAILED:
243 return( PSA_ERROR_INVALID_SIGNATURE );
244 case MBEDTLS_ERR_GCM_BAD_INPUT:
245 return( PSA_ERROR_NOT_SUPPORTED );
246 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
247 return( PSA_ERROR_HARDWARE_FAILURE );
248
249 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
250 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
251 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
254 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
255 return( PSA_ERROR_NOT_SUPPORTED );
256 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MD_ALLOC_FAILED:
259 return( PSA_ERROR_INSUFFICIENT_MEMORY );
260 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
261 return( PSA_ERROR_STORAGE_FAILURE );
262 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
263 return( PSA_ERROR_HARDWARE_FAILURE );
264
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100265 case MBEDTLS_ERR_PK_ALLOC_FAILED:
266 return( PSA_ERROR_INSUFFICIENT_MEMORY );
267 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
268 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100271 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100272 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
273 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
276 return( PSA_ERROR_NOT_SUPPORTED );
277 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
278 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
279 return( PSA_ERROR_NOT_PERMITTED );
280 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
281 return( PSA_ERROR_INVALID_ARGUMENT );
282 case MBEDTLS_ERR_PK_INVALID_ALG:
283 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
284 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
285 return( PSA_ERROR_NOT_SUPPORTED );
286 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
287 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100288 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
289 return( PSA_ERROR_HARDWARE_FAILURE );
290
291 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
292 return( PSA_ERROR_HARDWARE_FAILURE );
293
294 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
295 return( PSA_ERROR_INVALID_ARGUMENT );
296 case MBEDTLS_ERR_RSA_INVALID_PADDING:
297 return( PSA_ERROR_INVALID_PADDING );
298 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
299 return( PSA_ERROR_HARDWARE_FAILURE );
300 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
301 return( PSA_ERROR_INVALID_ARGUMENT );
302 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
303 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
304 return( PSA_ERROR_TAMPERING_DETECTED );
305 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
306 return( PSA_ERROR_INVALID_SIGNATURE );
307 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
308 return( PSA_ERROR_BUFFER_TOO_SMALL );
309 case MBEDTLS_ERR_RSA_RNG_FAILED:
310 return( PSA_ERROR_INSUFFICIENT_MEMORY );
311 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
312 return( PSA_ERROR_NOT_SUPPORTED );
313 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
314 return( PSA_ERROR_HARDWARE_FAILURE );
315
316 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
317 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
318 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
319 return( PSA_ERROR_HARDWARE_FAILURE );
320
321 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
322 return( PSA_ERROR_INVALID_ARGUMENT );
323 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
324 return( PSA_ERROR_HARDWARE_FAILURE );
325
itayzafrir5c753392018-05-08 11:18:38 +0300326 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300327 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300328 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300329 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
330 return( PSA_ERROR_BUFFER_TOO_SMALL );
331 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
332 return( PSA_ERROR_NOT_SUPPORTED );
333 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
334 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
335 return( PSA_ERROR_INVALID_SIGNATURE );
336 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
337 return( PSA_ERROR_INSUFFICIENT_MEMORY );
338 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
339 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300340
Gilles Peskinee59236f2018-01-27 23:32:46 +0100341 default:
342 return( PSA_ERROR_UNKNOWN_ERROR );
343 }
344}
345
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200346/* Retrieve a key slot, occupied or not. */
347static psa_status_t psa_get_key_slot( psa_key_slot_t key,
348 key_slot_t **p_slot )
349{
itayzafrir90d8c7a2018-09-12 11:44:52 +0300350 GUARD_MODULE_INITIALIZED;
351
Gilles Peskine996deb12018-08-01 15:45:45 +0200352 /* 0 is not a valid slot number under any circumstance. This
353 * implementation provides slots number 1 to N where N is the
354 * number of available slots. */
355 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200356 return( PSA_ERROR_INVALID_ARGUMENT );
357
Gilles Peskine996deb12018-08-01 15:45:45 +0200358 *p_slot = &global_data.key_slots[key - 1];
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200359 return( PSA_SUCCESS );
360}
361
362/* Retrieve an empty key slot (slot with no key data, but possibly
363 * with some metadata such as a policy). */
364static psa_status_t psa_get_empty_key_slot( psa_key_slot_t key,
365 key_slot_t **p_slot )
366{
367 psa_status_t status;
368 key_slot_t *slot = NULL;
369
370 *p_slot = NULL;
371
372 status = psa_get_key_slot( key, &slot );
373 if( status != PSA_SUCCESS )
374 return( status );
375
376 if( slot->type != PSA_KEY_TYPE_NONE )
377 return( PSA_ERROR_OCCUPIED_SLOT );
378
379 *p_slot = slot;
380 return( status );
381}
382
Jaeden Amerob4fa8c92018-07-11 15:57:44 +0100383/** Retrieve a slot which must contain a key. The key must have allow all the
384 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
385 * operations with this algorithm. */
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200386static psa_status_t psa_get_key_from_slot( psa_key_slot_t key,
387 key_slot_t **p_slot,
388 psa_key_usage_t usage,
389 psa_algorithm_t alg )
390{
391 psa_status_t status;
392 key_slot_t *slot = NULL;
393
394 *p_slot = NULL;
395
396 status = psa_get_key_slot( key, &slot );
397 if( status != PSA_SUCCESS )
398 return( status );
399 if( slot->type == PSA_KEY_TYPE_NONE )
400 return( PSA_ERROR_EMPTY_SLOT );
401
402 /* Enforce that usage policy for the key slot contains all the flags
403 * required by the usage parameter. There is one exception: public
404 * keys can always be exported, so we treat public key objects as
405 * if they had the export flag. */
406 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
407 usage &= ~PSA_KEY_USAGE_EXPORT;
408 if( ( slot->policy.usage & usage ) != usage )
409 return( PSA_ERROR_NOT_PERMITTED );
410 if( alg != 0 && ( alg != slot->policy.alg ) )
411 return( PSA_ERROR_NOT_PERMITTED );
412
413 *p_slot = slot;
414 return( PSA_SUCCESS );
415}
416
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200417
418
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100419/****************************************************************/
420/* Key management */
421/****************************************************************/
422
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100423#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200424static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
425{
426 switch( grpid )
427 {
428 case MBEDTLS_ECP_DP_SECP192R1:
429 return( PSA_ECC_CURVE_SECP192R1 );
430 case MBEDTLS_ECP_DP_SECP224R1:
431 return( PSA_ECC_CURVE_SECP224R1 );
432 case MBEDTLS_ECP_DP_SECP256R1:
433 return( PSA_ECC_CURVE_SECP256R1 );
434 case MBEDTLS_ECP_DP_SECP384R1:
435 return( PSA_ECC_CURVE_SECP384R1 );
436 case MBEDTLS_ECP_DP_SECP521R1:
437 return( PSA_ECC_CURVE_SECP521R1 );
438 case MBEDTLS_ECP_DP_BP256R1:
439 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
440 case MBEDTLS_ECP_DP_BP384R1:
441 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
442 case MBEDTLS_ECP_DP_BP512R1:
443 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
444 case MBEDTLS_ECP_DP_CURVE25519:
445 return( PSA_ECC_CURVE_CURVE25519 );
446 case MBEDTLS_ECP_DP_SECP192K1:
447 return( PSA_ECC_CURVE_SECP192K1 );
448 case MBEDTLS_ECP_DP_SECP224K1:
449 return( PSA_ECC_CURVE_SECP224K1 );
450 case MBEDTLS_ECP_DP_SECP256K1:
451 return( PSA_ECC_CURVE_SECP256K1 );
452 case MBEDTLS_ECP_DP_CURVE448:
453 return( PSA_ECC_CURVE_CURVE448 );
454 default:
455 return( 0 );
456 }
457}
458
Gilles Peskine12313cd2018-06-20 00:20:32 +0200459static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
460{
461 switch( curve )
462 {
463 case PSA_ECC_CURVE_SECP192R1:
464 return( MBEDTLS_ECP_DP_SECP192R1 );
465 case PSA_ECC_CURVE_SECP224R1:
466 return( MBEDTLS_ECP_DP_SECP224R1 );
467 case PSA_ECC_CURVE_SECP256R1:
468 return( MBEDTLS_ECP_DP_SECP256R1 );
469 case PSA_ECC_CURVE_SECP384R1:
470 return( MBEDTLS_ECP_DP_SECP384R1 );
471 case PSA_ECC_CURVE_SECP521R1:
472 return( MBEDTLS_ECP_DP_SECP521R1 );
473 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
474 return( MBEDTLS_ECP_DP_BP256R1 );
475 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
476 return( MBEDTLS_ECP_DP_BP384R1 );
477 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
478 return( MBEDTLS_ECP_DP_BP512R1 );
479 case PSA_ECC_CURVE_CURVE25519:
480 return( MBEDTLS_ECP_DP_CURVE25519 );
481 case PSA_ECC_CURVE_SECP192K1:
482 return( MBEDTLS_ECP_DP_SECP192K1 );
483 case PSA_ECC_CURVE_SECP224K1:
484 return( MBEDTLS_ECP_DP_SECP224K1 );
485 case PSA_ECC_CURVE_SECP256K1:
486 return( MBEDTLS_ECP_DP_SECP256K1 );
487 case PSA_ECC_CURVE_CURVE448:
488 return( MBEDTLS_ECP_DP_CURVE448 );
489 default:
490 return( MBEDTLS_ECP_DP_NONE );
491 }
492}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100493#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200494
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
496 size_t bits,
497 struct raw_data *raw )
498{
499 /* Check that the bit size is acceptable for the key type */
500 switch( type )
501 {
502 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200503 if( bits == 0 )
504 {
505 raw->bytes = 0;
506 raw->data = NULL;
507 return( PSA_SUCCESS );
508 }
509 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200510#if defined(MBEDTLS_MD_C)
511 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200512#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200513 case PSA_KEY_TYPE_DERIVE:
514 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200515#if defined(MBEDTLS_AES_C)
516 case PSA_KEY_TYPE_AES:
517 if( bits != 128 && bits != 192 && bits != 256 )
518 return( PSA_ERROR_INVALID_ARGUMENT );
519 break;
520#endif
521#if defined(MBEDTLS_CAMELLIA_C)
522 case PSA_KEY_TYPE_CAMELLIA:
523 if( bits != 128 && bits != 192 && bits != 256 )
524 return( PSA_ERROR_INVALID_ARGUMENT );
525 break;
526#endif
527#if defined(MBEDTLS_DES_C)
528 case PSA_KEY_TYPE_DES:
529 if( bits != 64 && bits != 128 && bits != 192 )
530 return( PSA_ERROR_INVALID_ARGUMENT );
531 break;
532#endif
533#if defined(MBEDTLS_ARC4_C)
534 case PSA_KEY_TYPE_ARC4:
535 if( bits < 8 || bits > 2048 )
536 return( PSA_ERROR_INVALID_ARGUMENT );
537 break;
538#endif
539 default:
540 return( PSA_ERROR_NOT_SUPPORTED );
541 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200542 if( bits % 8 != 0 )
543 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200544
545 /* Allocate memory for the key */
546 raw->bytes = PSA_BITS_TO_BYTES( bits );
547 raw->data = mbedtls_calloc( 1, raw->bytes );
548 if( raw->data == NULL )
549 {
550 raw->bytes = 0;
551 return( PSA_ERROR_INSUFFICIENT_MEMORY );
552 }
553 return( PSA_SUCCESS );
554}
555
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200556#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
557static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
558 mbedtls_rsa_context **p_rsa )
559{
560 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
561 return( PSA_ERROR_INVALID_ARGUMENT );
562 else
563 {
564 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
565 size_t bits = mbedtls_rsa_get_bitlen( rsa );
566 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
567 return( PSA_ERROR_NOT_SUPPORTED );
568 *p_rsa = rsa;
569 return( PSA_SUCCESS );
570 }
571}
572#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
573
574#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
575static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
576 mbedtls_pk_context *pk,
577 mbedtls_ecp_keypair **p_ecp )
578{
579 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
580 return( PSA_ERROR_INVALID_ARGUMENT );
581 else
582 {
583 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
584 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
585 if( actual_curve != expected_curve )
586 return( PSA_ERROR_INVALID_ARGUMENT );
587 *p_ecp = ecp;
588 return( PSA_SUCCESS );
589 }
590}
591#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
592
Gilles Peskine2d277862018-06-18 15:41:12 +0200593psa_status_t psa_import_key( psa_key_slot_t key,
594 psa_key_type_t type,
595 const uint8_t *data,
596 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100597{
598 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200599 psa_status_t status = PSA_SUCCESS;
600 status = psa_get_empty_key_slot( key, &slot );
601 if( status != PSA_SUCCESS )
602 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100603
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200604 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100605 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100606 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100607 if( data_length > SIZE_MAX / 8 )
608 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200609 status = prepare_raw_data_slot( type,
610 PSA_BYTES_TO_BITS( data_length ),
611 &slot->data.raw );
612 if( status != PSA_SUCCESS )
613 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200614 if( data_length != 0 )
615 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100616 }
617 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100618#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200619 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100620 {
621 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100622 mbedtls_pk_context pk;
623 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200624
625 /* Parse the data. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100626 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
627 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
628 else
629 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100630 if( ret != 0 )
631 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200632
633 /* We have something that the pkparse module recognizes.
634 * If it has the expected type and passes any type-specific
635 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100636#if defined(MBEDTLS_RSA_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200637 if( PSA_KEY_TYPE_IS_RSA( type ) )
638 status = psa_import_rsa_key( &pk, &slot->data.rsa );
639 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100640#endif /* MBEDTLS_RSA_C */
641#if defined(MBEDTLS_ECP_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200642 if( PSA_KEY_TYPE_IS_ECC( type ) )
643 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( type ),
644 &pk, &slot->data.ecp );
645 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100646#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200647 {
648 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200649 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200650
Gilles Peskinec648d692018-06-28 08:46:13 +0200651 /* Free the content of the pk object only on error. On success,
652 * the content of the object has been stored in the slot. */
653 if( status != PSA_SUCCESS )
654 {
655 mbedtls_pk_free( &pk );
656 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100657 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100658 }
659 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100660#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100661 {
662 return( PSA_ERROR_NOT_SUPPORTED );
663 }
664
665 slot->type = type;
666 return( PSA_SUCCESS );
667}
668
Gilles Peskine2d277862018-06-18 15:41:12 +0200669psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100670{
671 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200672 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200674 status = psa_get_key_slot( key, &slot );
675 if( status != PSA_SUCCESS )
676 return( status );
677
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200679 {
680 /* No key material to clean, but do zeroize the slot below to wipe
681 * metadata such as policies. */
682 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200683 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100684 {
685 mbedtls_free( slot->data.raw.data );
686 }
687 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100688#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200689 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100690 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100691 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100692 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100693 }
694 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100695#endif /* defined(MBEDTLS_RSA_C) */
696#if defined(MBEDTLS_ECP_C)
697 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
698 {
699 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100700 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100701 }
702 else
703#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100704 {
705 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100706 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100707 return( PSA_ERROR_TAMPERING_DETECTED );
708 }
709
710 mbedtls_zeroize( slot, sizeof( *slot ) );
711 return( PSA_SUCCESS );
712}
713
Gilles Peskineb870b182018-07-06 16:02:09 +0200714/* Return the size of the key in the given slot, in bits. */
715static size_t psa_get_key_bits( const key_slot_t *slot )
716{
717 if( key_type_is_raw_bytes( slot->type ) )
718 return( slot->data.raw.bytes * 8 );
719#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200720 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineb870b182018-07-06 16:02:09 +0200721 return( mbedtls_rsa_get_bitlen( slot->data.rsa ) );
722#endif /* defined(MBEDTLS_RSA_C) */
723#if defined(MBEDTLS_ECP_C)
724 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
725 return( slot->data.ecp->grp.pbits );
726#endif /* defined(MBEDTLS_ECP_C) */
727 /* Shouldn't happen except on an empty slot. */
728 return( 0 );
729}
730
Gilles Peskine2d277862018-06-18 15:41:12 +0200731psa_status_t psa_get_key_information( psa_key_slot_t key,
732 psa_key_type_t *type,
733 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100734{
735 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200736 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100737
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200739 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740 if( bits != NULL )
741 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200742 status = psa_get_key_slot( key, &slot );
743 if( status != PSA_SUCCESS )
744 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200745
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746 if( slot->type == PSA_KEY_TYPE_NONE )
747 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200748 if( type != NULL )
749 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200750 if( bits != NULL )
751 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100752 return( PSA_SUCCESS );
753}
754
Gilles Peskine2d277862018-06-18 15:41:12 +0200755static psa_status_t psa_internal_export_key( psa_key_slot_t key,
756 uint8_t *data,
757 size_t data_size,
758 size_t *data_length,
759 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100760{
761 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200762 psa_status_t status;
763 /* Exporting a public key doesn't require a usage flag. If we're
764 * called by psa_export_public_key(), don't require the EXPORT flag.
765 * If we're called by psa_export_key(), do require the EXPORT flag;
766 * if the key turns out to be public key object, psa_get_key_from_slot()
767 * will ignore this flag. */
768 psa_key_usage_t usage = export_public_key ? 0 : PSA_KEY_USAGE_EXPORT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100769
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100770 /* Set the key to empty now, so that even when there are errors, we always
771 * set data_length to a value between 0 and data_size. On error, setting
772 * the key to empty is a good choice because an empty key representation is
773 * unlikely to be accepted anywhere. */
774 *data_length = 0;
775
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200776 status = psa_get_key_from_slot( key, &slot, usage, 0 );
777 if( status != PSA_SUCCESS )
778 return( status );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200779 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300780 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300781
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200782 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100783 {
784 if( slot->data.raw.bytes > data_size )
785 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200786 if( slot->data.raw.bytes != 0 )
787 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100788 *data_length = slot->data.raw.bytes;
789 return( PSA_SUCCESS );
790 }
791 else
Moran Peker17e36e12018-05-02 12:55:20 +0300792 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100793#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200794 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300795 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100796 {
Moran Pekera998bc62018-04-16 18:16:20 +0300797 mbedtls_pk_context pk;
798 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +0200799 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300800 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100801#if defined(MBEDTLS_RSA_C)
802 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300803 pk.pk_info = &mbedtls_rsa_info;
804 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100805#else
806 return( PSA_ERROR_NOT_SUPPORTED );
807#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300808 }
809 else
810 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100811#if defined(MBEDTLS_ECP_C)
812 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300813 pk.pk_info = &mbedtls_eckey_info;
814 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100815#else
816 return( PSA_ERROR_NOT_SUPPORTED );
817#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300818 }
Moran Pekerd7326592018-05-29 16:56:39 +0300819 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300820 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300821 else
822 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300823 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200824 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200825 /* If data_size is 0 then data may be NULL and then the
826 * call to memset would have undefined behavior. */
827 if( data_size != 0 )
828 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300829 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200830 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200831 /* The mbedtls_pk_xxx functions write to the end of the buffer.
832 * Move the data to the beginning and erase remaining data
833 * at the original location. */
834 if( 2 * (size_t) ret <= data_size )
835 {
836 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200837 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200838 }
839 else if( (size_t) ret < data_size )
840 {
841 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200842 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200843 }
Moran Pekera998bc62018-04-16 18:16:20 +0300844 *data_length = ret;
845 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100846 }
847 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100848#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300849 {
850 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200851 it is valid for a special-purpose implementation to omit
852 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300853 return( PSA_ERROR_NOT_SUPPORTED );
854 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100855 }
856}
857
Gilles Peskine2d277862018-06-18 15:41:12 +0200858psa_status_t psa_export_key( psa_key_slot_t key,
859 uint8_t *data,
860 size_t data_size,
861 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300862{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200863 return( psa_internal_export_key( key, data, data_size,
864 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100865}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100866
Gilles Peskine2d277862018-06-18 15:41:12 +0200867psa_status_t psa_export_public_key( psa_key_slot_t key,
868 uint8_t *data,
869 size_t data_size,
870 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300871{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200872 return( psa_internal_export_key( key, data, data_size,
873 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300874}
875
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200876
877
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100878/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100879/* Message digests */
880/****************************************************************/
881
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100882static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100883{
884 switch( alg )
885 {
886#if defined(MBEDTLS_MD2_C)
887 case PSA_ALG_MD2:
888 return( &mbedtls_md2_info );
889#endif
890#if defined(MBEDTLS_MD4_C)
891 case PSA_ALG_MD4:
892 return( &mbedtls_md4_info );
893#endif
894#if defined(MBEDTLS_MD5_C)
895 case PSA_ALG_MD5:
896 return( &mbedtls_md5_info );
897#endif
898#if defined(MBEDTLS_RIPEMD160_C)
899 case PSA_ALG_RIPEMD160:
900 return( &mbedtls_ripemd160_info );
901#endif
902#if defined(MBEDTLS_SHA1_C)
903 case PSA_ALG_SHA_1:
904 return( &mbedtls_sha1_info );
905#endif
906#if defined(MBEDTLS_SHA256_C)
907 case PSA_ALG_SHA_224:
908 return( &mbedtls_sha224_info );
909 case PSA_ALG_SHA_256:
910 return( &mbedtls_sha256_info );
911#endif
912#if defined(MBEDTLS_SHA512_C)
913 case PSA_ALG_SHA_384:
914 return( &mbedtls_sha384_info );
915 case PSA_ALG_SHA_512:
916 return( &mbedtls_sha512_info );
917#endif
918 default:
919 return( NULL );
920 }
921}
922
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100923psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
924{
925 switch( operation->alg )
926 {
Gilles Peskine81736312018-06-26 15:04:31 +0200927 case 0:
928 /* The object has (apparently) been initialized but it is not
929 * in use. It's ok to call abort on such an object, and there's
930 * nothing to do. */
931 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100932#if defined(MBEDTLS_MD2_C)
933 case PSA_ALG_MD2:
934 mbedtls_md2_free( &operation->ctx.md2 );
935 break;
936#endif
937#if defined(MBEDTLS_MD4_C)
938 case PSA_ALG_MD4:
939 mbedtls_md4_free( &operation->ctx.md4 );
940 break;
941#endif
942#if defined(MBEDTLS_MD5_C)
943 case PSA_ALG_MD5:
944 mbedtls_md5_free( &operation->ctx.md5 );
945 break;
946#endif
947#if defined(MBEDTLS_RIPEMD160_C)
948 case PSA_ALG_RIPEMD160:
949 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
950 break;
951#endif
952#if defined(MBEDTLS_SHA1_C)
953 case PSA_ALG_SHA_1:
954 mbedtls_sha1_free( &operation->ctx.sha1 );
955 break;
956#endif
957#if defined(MBEDTLS_SHA256_C)
958 case PSA_ALG_SHA_224:
959 case PSA_ALG_SHA_256:
960 mbedtls_sha256_free( &operation->ctx.sha256 );
961 break;
962#endif
963#if defined(MBEDTLS_SHA512_C)
964 case PSA_ALG_SHA_384:
965 case PSA_ALG_SHA_512:
966 mbedtls_sha512_free( &operation->ctx.sha512 );
967 break;
968#endif
969 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +0200970 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100971 }
972 operation->alg = 0;
973 return( PSA_SUCCESS );
974}
975
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200976psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100977 psa_algorithm_t alg )
978{
979 int ret;
980 operation->alg = 0;
981 switch( alg )
982 {
983#if defined(MBEDTLS_MD2_C)
984 case PSA_ALG_MD2:
985 mbedtls_md2_init( &operation->ctx.md2 );
986 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
987 break;
988#endif
989#if defined(MBEDTLS_MD4_C)
990 case PSA_ALG_MD4:
991 mbedtls_md4_init( &operation->ctx.md4 );
992 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
993 break;
994#endif
995#if defined(MBEDTLS_MD5_C)
996 case PSA_ALG_MD5:
997 mbedtls_md5_init( &operation->ctx.md5 );
998 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
999 break;
1000#endif
1001#if defined(MBEDTLS_RIPEMD160_C)
1002 case PSA_ALG_RIPEMD160:
1003 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1004 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1005 break;
1006#endif
1007#if defined(MBEDTLS_SHA1_C)
1008 case PSA_ALG_SHA_1:
1009 mbedtls_sha1_init( &operation->ctx.sha1 );
1010 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1011 break;
1012#endif
1013#if defined(MBEDTLS_SHA256_C)
1014 case PSA_ALG_SHA_224:
1015 mbedtls_sha256_init( &operation->ctx.sha256 );
1016 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1017 break;
1018 case PSA_ALG_SHA_256:
1019 mbedtls_sha256_init( &operation->ctx.sha256 );
1020 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1021 break;
1022#endif
1023#if defined(MBEDTLS_SHA512_C)
1024 case PSA_ALG_SHA_384:
1025 mbedtls_sha512_init( &operation->ctx.sha512 );
1026 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1027 break;
1028 case PSA_ALG_SHA_512:
1029 mbedtls_sha512_init( &operation->ctx.sha512 );
1030 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1031 break;
1032#endif
1033 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001034 return( PSA_ALG_IS_HASH( alg ) ?
1035 PSA_ERROR_NOT_SUPPORTED :
1036 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001037 }
1038 if( ret == 0 )
1039 operation->alg = alg;
1040 else
1041 psa_hash_abort( operation );
1042 return( mbedtls_to_psa_error( ret ) );
1043}
1044
1045psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1046 const uint8_t *input,
1047 size_t input_length )
1048{
1049 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001050
1051 /* Don't require hash implementations to behave correctly on a
1052 * zero-length input, which may have an invalid pointer. */
1053 if( input_length == 0 )
1054 return( PSA_SUCCESS );
1055
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001056 switch( operation->alg )
1057 {
1058#if defined(MBEDTLS_MD2_C)
1059 case PSA_ALG_MD2:
1060 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1061 input, input_length );
1062 break;
1063#endif
1064#if defined(MBEDTLS_MD4_C)
1065 case PSA_ALG_MD4:
1066 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1067 input, input_length );
1068 break;
1069#endif
1070#if defined(MBEDTLS_MD5_C)
1071 case PSA_ALG_MD5:
1072 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1073 input, input_length );
1074 break;
1075#endif
1076#if defined(MBEDTLS_RIPEMD160_C)
1077 case PSA_ALG_RIPEMD160:
1078 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1079 input, input_length );
1080 break;
1081#endif
1082#if defined(MBEDTLS_SHA1_C)
1083 case PSA_ALG_SHA_1:
1084 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1085 input, input_length );
1086 break;
1087#endif
1088#if defined(MBEDTLS_SHA256_C)
1089 case PSA_ALG_SHA_224:
1090 case PSA_ALG_SHA_256:
1091 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1092 input, input_length );
1093 break;
1094#endif
1095#if defined(MBEDTLS_SHA512_C)
1096 case PSA_ALG_SHA_384:
1097 case PSA_ALG_SHA_512:
1098 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1099 input, input_length );
1100 break;
1101#endif
1102 default:
1103 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1104 break;
1105 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001106
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001107 if( ret != 0 )
1108 psa_hash_abort( operation );
1109 return( mbedtls_to_psa_error( ret ) );
1110}
1111
1112psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1113 uint8_t *hash,
1114 size_t hash_size,
1115 size_t *hash_length )
1116{
itayzafrir40835d42018-08-02 13:14:17 +03001117 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001118 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001119 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001120
1121 /* Fill the output buffer with something that isn't a valid hash
1122 * (barring an attack on the hash and deliberately-crafted input),
1123 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001124 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001125 /* If hash_size is 0 then hash may be NULL and then the
1126 * call to memset would have undefined behavior. */
1127 if( hash_size != 0 )
1128 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001129
1130 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001131 {
1132 status = PSA_ERROR_BUFFER_TOO_SMALL;
1133 goto exit;
1134 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001135
1136 switch( operation->alg )
1137 {
1138#if defined(MBEDTLS_MD2_C)
1139 case PSA_ALG_MD2:
1140 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1141 break;
1142#endif
1143#if defined(MBEDTLS_MD4_C)
1144 case PSA_ALG_MD4:
1145 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1146 break;
1147#endif
1148#if defined(MBEDTLS_MD5_C)
1149 case PSA_ALG_MD5:
1150 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1151 break;
1152#endif
1153#if defined(MBEDTLS_RIPEMD160_C)
1154 case PSA_ALG_RIPEMD160:
1155 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1156 break;
1157#endif
1158#if defined(MBEDTLS_SHA1_C)
1159 case PSA_ALG_SHA_1:
1160 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1161 break;
1162#endif
1163#if defined(MBEDTLS_SHA256_C)
1164 case PSA_ALG_SHA_224:
1165 case PSA_ALG_SHA_256:
1166 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1167 break;
1168#endif
1169#if defined(MBEDTLS_SHA512_C)
1170 case PSA_ALG_SHA_384:
1171 case PSA_ALG_SHA_512:
1172 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1173 break;
1174#endif
1175 default:
1176 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1177 break;
1178 }
itayzafrir40835d42018-08-02 13:14:17 +03001179 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001180
itayzafrir40835d42018-08-02 13:14:17 +03001181exit:
1182 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001183 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001184 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001185 return( psa_hash_abort( operation ) );
1186 }
1187 else
1188 {
1189 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001190 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001191 }
1192}
1193
Gilles Peskine2d277862018-06-18 15:41:12 +02001194psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1195 const uint8_t *hash,
1196 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001197{
1198 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1199 size_t actual_hash_length;
1200 psa_status_t status = psa_hash_finish( operation,
1201 actual_hash, sizeof( actual_hash ),
1202 &actual_hash_length );
1203 if( status != PSA_SUCCESS )
1204 return( status );
1205 if( actual_hash_length != hash_length )
1206 return( PSA_ERROR_INVALID_SIGNATURE );
1207 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1208 return( PSA_ERROR_INVALID_SIGNATURE );
1209 return( PSA_SUCCESS );
1210}
1211
1212
1213
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001214/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001215/* MAC */
1216/****************************************************************/
1217
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001218static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001219 psa_algorithm_t alg,
1220 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001221 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001222 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001223{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001224 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001225 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001226
1227 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1228 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001229 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001230 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001231 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001232 mode = MBEDTLS_MODE_STREAM;
1233 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001234 case PSA_ALG_CTR:
1235 mode = MBEDTLS_MODE_CTR;
1236 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001237 case PSA_ALG_CFB:
1238 mode = MBEDTLS_MODE_CFB;
1239 break;
1240 case PSA_ALG_OFB:
1241 mode = MBEDTLS_MODE_OFB;
1242 break;
1243 case PSA_ALG_CBC_NO_PADDING:
1244 mode = MBEDTLS_MODE_CBC;
1245 break;
1246 case PSA_ALG_CBC_PKCS7:
1247 mode = MBEDTLS_MODE_CBC;
1248 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001249 case PSA_ALG_CCM:
1250 mode = MBEDTLS_MODE_CCM;
1251 break;
1252 case PSA_ALG_GCM:
1253 mode = MBEDTLS_MODE_GCM;
1254 break;
1255 default:
1256 return( NULL );
1257 }
1258 }
1259 else if( alg == PSA_ALG_CMAC )
1260 mode = MBEDTLS_MODE_ECB;
1261 else if( alg == PSA_ALG_GMAC )
1262 mode = MBEDTLS_MODE_GCM;
1263 else
1264 return( NULL );
1265
1266 switch( key_type )
1267 {
1268 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001269 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001270 break;
1271 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001272 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1273 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001274 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001275 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001276 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001277 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001278 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1279 * but two-key Triple-DES is functionally three-key Triple-DES
1280 * with K1=K3, so that's how we present it to mbedtls. */
1281 if( key_bits == 128 )
1282 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001283 break;
1284 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001285 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001286 break;
1287 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001288 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001289 break;
1290 default:
1291 return( NULL );
1292 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001293 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001294 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001295
Jaeden Amero23bbb752018-06-26 14:16:54 +01001296 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1297 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001298}
1299
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001300static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001301{
Gilles Peskine2d277862018-06-18 15:41:12 +02001302 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001303 {
1304 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001305 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001306 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001307 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001308 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001309 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001310 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001311 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001312 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001313 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001314 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001315 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001316 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001317 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001318 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001319 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001320 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001321 return( 128 );
1322 default:
1323 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001324 }
1325}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001326
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001327/* Initialize the MAC operation structure. Once this function has been
1328 * called, psa_mac_abort can run and will do the right thing. */
1329static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1330 psa_algorithm_t alg )
1331{
1332 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1333
1334 operation->alg = alg;
1335 operation->key_set = 0;
1336 operation->iv_set = 0;
1337 operation->iv_required = 0;
1338 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001339 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001340
1341#if defined(MBEDTLS_CMAC_C)
1342 if( alg == PSA_ALG_CMAC )
1343 {
1344 operation->iv_required = 0;
1345 mbedtls_cipher_init( &operation->ctx.cmac );
1346 status = PSA_SUCCESS;
1347 }
1348 else
1349#endif /* MBEDTLS_CMAC_C */
1350#if defined(MBEDTLS_MD_C)
1351 if( PSA_ALG_IS_HMAC( operation->alg ) )
1352 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001353 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1354 operation->ctx.hmac.hash_ctx.alg = 0;
1355 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001356 }
1357 else
1358#endif /* MBEDTLS_MD_C */
1359 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001360 if( ! PSA_ALG_IS_MAC( alg ) )
1361 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001362 }
1363
1364 if( status != PSA_SUCCESS )
1365 memset( operation, 0, sizeof( *operation ) );
1366 return( status );
1367}
1368
Gilles Peskine01126fa2018-07-12 17:04:55 +02001369#if defined(MBEDTLS_MD_C)
1370static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1371{
1372 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1373 return( psa_hash_abort( &hmac->hash_ctx ) );
1374}
1375#endif /* MBEDTLS_MD_C */
1376
Gilles Peskine8c9def32018-02-08 10:02:12 +01001377psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1378{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001379 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001380 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001381 /* The object has (apparently) been initialized but it is not
1382 * in use. It's ok to call abort on such an object, and there's
1383 * nothing to do. */
1384 return( PSA_SUCCESS );
1385 }
1386 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001387#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001388 if( operation->alg == PSA_ALG_CMAC )
1389 {
1390 mbedtls_cipher_free( &operation->ctx.cmac );
1391 }
1392 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001393#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001394#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001395 if( PSA_ALG_IS_HMAC( operation->alg ) )
1396 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001397 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001398 }
1399 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001400#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001401 {
1402 /* Sanity check (shouldn't happen: operation->alg should
1403 * always have been initialized to a valid value). */
1404 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001405 }
Moran Peker41deec42018-04-04 15:43:05 +03001406
Gilles Peskine8c9def32018-02-08 10:02:12 +01001407 operation->alg = 0;
1408 operation->key_set = 0;
1409 operation->iv_set = 0;
1410 operation->iv_required = 0;
1411 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001412 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001413
Gilles Peskine8c9def32018-02-08 10:02:12 +01001414 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001415
1416bad_state:
1417 /* If abort is called on an uninitialized object, we can't trust
1418 * anything. Wipe the object in case it contains confidential data.
1419 * This may result in a memory leak if a pointer gets overwritten,
1420 * but it's too late to do anything about this. */
1421 memset( operation, 0, sizeof( *operation ) );
1422 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001423}
1424
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001425#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001426static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001427 size_t key_bits,
1428 key_slot_t *slot,
1429 const mbedtls_cipher_info_t *cipher_info )
1430{
1431 int ret;
1432
1433 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001434
1435 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1436 if( ret != 0 )
1437 return( ret );
1438
1439 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1440 slot->data.raw.data,
1441 key_bits );
1442 return( ret );
1443}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001444#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001445
Gilles Peskine248051a2018-06-20 16:09:38 +02001446#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001447static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1448 const uint8_t *key,
1449 size_t key_length,
1450 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001451{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001452 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001453 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001454 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001455 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001456 psa_status_t status;
1457
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001458 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1459 * overflow below. This should never trigger if the hash algorithm
1460 * is implemented correctly. */
1461 /* The size checks against the ipad and opad buffers cannot be written
1462 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1463 * because that triggers -Wlogical-op on GCC 7.3. */
1464 if( block_size > sizeof( ipad ) )
1465 return( PSA_ERROR_NOT_SUPPORTED );
1466 if( block_size > sizeof( hmac->opad ) )
1467 return( PSA_ERROR_NOT_SUPPORTED );
1468 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001469 return( PSA_ERROR_NOT_SUPPORTED );
1470
Gilles Peskined223b522018-06-11 18:12:58 +02001471 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001472 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001473 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001474 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001475 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001476 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001477 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001478 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001479 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001480 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001481 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001482 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001483 }
Gilles Peskine96889972018-07-12 17:07:03 +02001484 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1485 * but it is permitted. It is common when HMAC is used in HKDF, for
1486 * example. Don't call `memcpy` in the 0-length because `key` could be
1487 * an invalid pointer which would make the behavior undefined. */
1488 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001489 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001490
Gilles Peskined223b522018-06-11 18:12:58 +02001491 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1492 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001493 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001494 ipad[i] ^= 0x36;
1495 memset( ipad + key_length, 0x36, block_size - key_length );
1496
1497 /* Copy the key material from ipad to opad, flipping the requisite bits,
1498 * and filling the rest of opad with the requisite constant. */
1499 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001500 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1501 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001502
Gilles Peskine01126fa2018-07-12 17:04:55 +02001503 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001504 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001505 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001506
Gilles Peskine01126fa2018-07-12 17:04:55 +02001507 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001508
1509cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001510 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001511
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001512 return( status );
1513}
Gilles Peskine248051a2018-06-20 16:09:38 +02001514#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001515
Gilles Peskine89167cb2018-07-08 20:12:23 +02001516static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1517 psa_key_slot_t key,
1518 psa_algorithm_t alg,
1519 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001520{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001521 psa_status_t status;
1522 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001523 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001524 psa_key_usage_t usage =
1525 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001526 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
1527 psa_algorithm_t full_length_alg = alg & ~PSA_ALG_MAC_TRUNCATION_MASK;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001528
Gilles Peskined911eb72018-08-14 15:18:45 +02001529 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001530 if( status != PSA_SUCCESS )
1531 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001532 if( is_sign )
1533 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001534
Gilles Peskine89167cb2018-07-08 20:12:23 +02001535 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001536 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001537 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001538 key_bits = psa_get_key_bits( slot );
1539
Gilles Peskine8c9def32018-02-08 10:02:12 +01001540#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001541 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001542 {
1543 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001544 mbedtls_cipher_info_from_psa( full_length_alg,
1545 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001546 int ret;
1547 if( cipher_info == NULL )
1548 {
1549 status = PSA_ERROR_NOT_SUPPORTED;
1550 goto exit;
1551 }
1552 operation->mac_size = cipher_info->block_size;
1553 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1554 status = mbedtls_to_psa_error( ret );
1555 }
1556 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001557#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001558#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001559 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001560 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001561 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001562 if( hash_alg == 0 )
1563 {
1564 status = PSA_ERROR_NOT_SUPPORTED;
1565 goto exit;
1566 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001567
1568 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1569 /* Sanity check. This shouldn't fail on a valid configuration. */
1570 if( operation->mac_size == 0 ||
1571 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1572 {
1573 status = PSA_ERROR_NOT_SUPPORTED;
1574 goto exit;
1575 }
1576
Gilles Peskine01126fa2018-07-12 17:04:55 +02001577 if( slot->type != PSA_KEY_TYPE_HMAC )
1578 {
1579 status = PSA_ERROR_INVALID_ARGUMENT;
1580 goto exit;
1581 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001582
Gilles Peskine01126fa2018-07-12 17:04:55 +02001583 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1584 slot->data.raw.data,
1585 slot->data.raw.bytes,
1586 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001587 }
1588 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001589#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001590 {
1591 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001592 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001593
Gilles Peskined911eb72018-08-14 15:18:45 +02001594 if( truncated == 0 )
1595 {
1596 /* The "normal" case: untruncated algorithm. Nothing to do. */
1597 }
1598 else if( truncated < 4 )
1599 {
1600 /* Too small to make any sense. Reject. 4 bytes is too small for
1601 * security but ancient protocols with 32-bit MACs do exist. */
1602 status = PSA_ERROR_NOT_SUPPORTED;
1603 }
1604 else if( truncated > operation->mac_size )
1605 {
1606 /* It's impossible to "truncate" to a larger length. */
1607 status = PSA_ERROR_INVALID_ARGUMENT;
1608 }
1609 else
1610 operation->mac_size = truncated;
1611
Gilles Peskinefbfac682018-07-08 20:51:54 +02001612exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001613 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001614 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001615 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001616 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001617 else
1618 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001619 operation->key_set = 1;
1620 }
1621 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001622}
1623
Gilles Peskine89167cb2018-07-08 20:12:23 +02001624psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1625 psa_key_slot_t key,
1626 psa_algorithm_t alg )
1627{
1628 return( psa_mac_setup( operation, key, alg, 1 ) );
1629}
1630
1631psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1632 psa_key_slot_t key,
1633 psa_algorithm_t alg )
1634{
1635 return( psa_mac_setup( operation, key, alg, 0 ) );
1636}
1637
Gilles Peskine8c9def32018-02-08 10:02:12 +01001638psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1639 const uint8_t *input,
1640 size_t input_length )
1641{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001642 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001643 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001644 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001645 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001646 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001647 operation->has_input = 1;
1648
Gilles Peskine8c9def32018-02-08 10:02:12 +01001649#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001650 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001651 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001652 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1653 input, input_length );
1654 status = mbedtls_to_psa_error( ret );
1655 }
1656 else
1657#endif /* MBEDTLS_CMAC_C */
1658#if defined(MBEDTLS_MD_C)
1659 if( PSA_ALG_IS_HMAC( operation->alg ) )
1660 {
1661 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1662 input_length );
1663 }
1664 else
1665#endif /* MBEDTLS_MD_C */
1666 {
1667 /* This shouldn't happen if `operation` was initialized by
1668 * a setup function. */
1669 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001670 }
1671
Gilles Peskinefbfac682018-07-08 20:51:54 +02001672cleanup:
1673 if( status != PSA_SUCCESS )
1674 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001675 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001676}
1677
Gilles Peskine01126fa2018-07-12 17:04:55 +02001678#if defined(MBEDTLS_MD_C)
1679static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1680 uint8_t *mac,
1681 size_t mac_size )
1682{
1683 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1684 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1685 size_t hash_size = 0;
1686 size_t block_size = psa_get_hash_block_size( hash_alg );
1687 psa_status_t status;
1688
Gilles Peskine01126fa2018-07-12 17:04:55 +02001689 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1690 if( status != PSA_SUCCESS )
1691 return( status );
1692 /* From here on, tmp needs to be wiped. */
1693
1694 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1695 if( status != PSA_SUCCESS )
1696 goto exit;
1697
1698 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1699 if( status != PSA_SUCCESS )
1700 goto exit;
1701
1702 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1703 if( status != PSA_SUCCESS )
1704 goto exit;
1705
Gilles Peskined911eb72018-08-14 15:18:45 +02001706 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1707 if( status != PSA_SUCCESS )
1708 goto exit;
1709
1710 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001711
1712exit:
1713 mbedtls_zeroize( tmp, hash_size );
1714 return( status );
1715}
1716#endif /* MBEDTLS_MD_C */
1717
mohammad16036df908f2018-04-02 08:34:15 -07001718static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001719 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001720 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001721{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001722 if( ! operation->key_set )
1723 return( PSA_ERROR_BAD_STATE );
1724 if( operation->iv_required && ! operation->iv_set )
1725 return( PSA_ERROR_BAD_STATE );
1726
Gilles Peskine8c9def32018-02-08 10:02:12 +01001727 if( mac_size < operation->mac_size )
1728 return( PSA_ERROR_BUFFER_TOO_SMALL );
1729
Gilles Peskine8c9def32018-02-08 10:02:12 +01001730#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001731 if( operation->alg == PSA_ALG_CMAC )
1732 {
Gilles Peskined911eb72018-08-14 15:18:45 +02001733 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
1734 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
1735 if( ret == 0 )
1736 memcpy( mac, tmp, mac_size );
1737 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001738 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001739 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001740 else
1741#endif /* MBEDTLS_CMAC_C */
1742#if defined(MBEDTLS_MD_C)
1743 if( PSA_ALG_IS_HMAC( operation->alg ) )
1744 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001745 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02001746 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001747 }
1748 else
1749#endif /* MBEDTLS_MD_C */
1750 {
1751 /* This shouldn't happen if `operation` was initialized by
1752 * a setup function. */
1753 return( PSA_ERROR_BAD_STATE );
1754 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001755}
1756
Gilles Peskineacd4be32018-07-08 19:56:25 +02001757psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1758 uint8_t *mac,
1759 size_t mac_size,
1760 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001761{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001762 psa_status_t status;
1763
1764 /* Fill the output buffer with something that isn't a valid mac
1765 * (barring an attack on the mac and deliberately-crafted input),
1766 * in case the caller doesn't check the return status properly. */
1767 *mac_length = mac_size;
1768 /* If mac_size is 0 then mac may be NULL and then the
1769 * call to memset would have undefined behavior. */
1770 if( mac_size != 0 )
1771 memset( mac, '!', mac_size );
1772
Gilles Peskine89167cb2018-07-08 20:12:23 +02001773 if( ! operation->is_sign )
1774 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001775 status = PSA_ERROR_BAD_STATE;
1776 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001777 }
mohammad16036df908f2018-04-02 08:34:15 -07001778
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001779 status = psa_mac_finish_internal( operation, mac, mac_size );
1780
1781cleanup:
1782 if( status == PSA_SUCCESS )
1783 {
1784 status = psa_mac_abort( operation );
1785 if( status == PSA_SUCCESS )
1786 *mac_length = operation->mac_size;
1787 else
1788 memset( mac, '!', mac_size );
1789 }
1790 else
1791 psa_mac_abort( operation );
1792 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001793}
1794
Gilles Peskineacd4be32018-07-08 19:56:25 +02001795psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1796 const uint8_t *mac,
1797 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001798{
Gilles Peskine828ed142018-06-18 23:25:51 +02001799 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001800 psa_status_t status;
1801
Gilles Peskine89167cb2018-07-08 20:12:23 +02001802 if( operation->is_sign )
1803 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001804 status = PSA_ERROR_BAD_STATE;
1805 goto cleanup;
1806 }
1807 if( operation->mac_size != mac_length )
1808 {
1809 status = PSA_ERROR_INVALID_SIGNATURE;
1810 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001811 }
mohammad16036df908f2018-04-02 08:34:15 -07001812
1813 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001814 actual_mac, sizeof( actual_mac ) );
1815
1816 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1817 status = PSA_ERROR_INVALID_SIGNATURE;
1818
1819cleanup:
1820 if( status == PSA_SUCCESS )
1821 status = psa_mac_abort( operation );
1822 else
1823 psa_mac_abort( operation );
1824
Gilles Peskined911eb72018-08-14 15:18:45 +02001825 mbedtls_zeroize( actual_mac, mac_length );
1826
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001827 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001828}
1829
1830
Gilles Peskine20035e32018-02-03 22:44:14 +01001831
Gilles Peskine20035e32018-02-03 22:44:14 +01001832/****************************************************************/
1833/* Asymmetric cryptography */
1834/****************************************************************/
1835
Gilles Peskine2b450e32018-06-27 15:42:46 +02001836#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001837/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001838 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001839static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1840 size_t hash_length,
1841 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001842{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001843 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001844 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001845 *md_alg = mbedtls_md_get_type( md_info );
1846
1847 /* The Mbed TLS RSA module uses an unsigned int for hash length
1848 * parameters. Validate that it fits so that we don't risk an
1849 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001850#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001851 if( hash_length > UINT_MAX )
1852 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001853#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001854
1855#if defined(MBEDTLS_PKCS1_V15)
1856 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
1857 * must be correct. */
1858 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
1859 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001860 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001861 if( md_info == NULL )
1862 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001863 if( mbedtls_md_get_size( md_info ) != hash_length )
1864 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001865 }
1866#endif /* MBEDTLS_PKCS1_V15 */
1867
1868#if defined(MBEDTLS_PKCS1_V21)
1869 /* PSS requires a hash internally. */
1870 if( PSA_ALG_IS_RSA_PSS( alg ) )
1871 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001872 if( md_info == NULL )
1873 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001874 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001875#endif /* MBEDTLS_PKCS1_V21 */
1876
Gilles Peskine61b91d42018-06-08 16:09:36 +02001877 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001878}
1879
Gilles Peskine2b450e32018-06-27 15:42:46 +02001880static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1881 psa_algorithm_t alg,
1882 const uint8_t *hash,
1883 size_t hash_length,
1884 uint8_t *signature,
1885 size_t signature_size,
1886 size_t *signature_length )
1887{
1888 psa_status_t status;
1889 int ret;
1890 mbedtls_md_type_t md_alg;
1891
1892 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1893 if( status != PSA_SUCCESS )
1894 return( status );
1895
Gilles Peskine630a18a2018-06-29 17:49:35 +02001896 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001897 return( PSA_ERROR_BUFFER_TOO_SMALL );
1898
1899#if defined(MBEDTLS_PKCS1_V15)
1900 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1901 {
1902 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1903 MBEDTLS_MD_NONE );
1904 ret = mbedtls_rsa_pkcs1_sign( rsa,
1905 mbedtls_ctr_drbg_random,
1906 &global_data.ctr_drbg,
1907 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001908 md_alg,
1909 (unsigned int) hash_length,
1910 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001911 signature );
1912 }
1913 else
1914#endif /* MBEDTLS_PKCS1_V15 */
1915#if defined(MBEDTLS_PKCS1_V21)
1916 if( PSA_ALG_IS_RSA_PSS( alg ) )
1917 {
1918 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1919 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1920 mbedtls_ctr_drbg_random,
1921 &global_data.ctr_drbg,
1922 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001923 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001924 (unsigned int) hash_length,
1925 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001926 signature );
1927 }
1928 else
1929#endif /* MBEDTLS_PKCS1_V21 */
1930 {
1931 return( PSA_ERROR_INVALID_ARGUMENT );
1932 }
1933
1934 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02001935 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02001936 return( mbedtls_to_psa_error( ret ) );
1937}
1938
1939static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1940 psa_algorithm_t alg,
1941 const uint8_t *hash,
1942 size_t hash_length,
1943 const uint8_t *signature,
1944 size_t signature_length )
1945{
1946 psa_status_t status;
1947 int ret;
1948 mbedtls_md_type_t md_alg;
1949
1950 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1951 if( status != PSA_SUCCESS )
1952 return( status );
1953
Gilles Peskine630a18a2018-06-29 17:49:35 +02001954 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001955 return( PSA_ERROR_BUFFER_TOO_SMALL );
1956
1957#if defined(MBEDTLS_PKCS1_V15)
1958 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1959 {
1960 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1961 MBEDTLS_MD_NONE );
1962 ret = mbedtls_rsa_pkcs1_verify( rsa,
1963 mbedtls_ctr_drbg_random,
1964 &global_data.ctr_drbg,
1965 MBEDTLS_RSA_PUBLIC,
1966 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001967 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001968 hash,
1969 signature );
1970 }
1971 else
1972#endif /* MBEDTLS_PKCS1_V15 */
1973#if defined(MBEDTLS_PKCS1_V21)
1974 if( PSA_ALG_IS_RSA_PSS( alg ) )
1975 {
1976 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1977 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1978 mbedtls_ctr_drbg_random,
1979 &global_data.ctr_drbg,
1980 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001981 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001982 (unsigned int) hash_length,
1983 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001984 signature );
1985 }
1986 else
1987#endif /* MBEDTLS_PKCS1_V21 */
1988 {
1989 return( PSA_ERROR_INVALID_ARGUMENT );
1990 }
Gilles Peskineef12c632018-09-13 20:37:48 +02001991
1992 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
1993 * the rest of the signature is invalid". This has little use in
1994 * practice and PSA doesn't report this distinction. */
1995 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
1996 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02001997 return( mbedtls_to_psa_error( ret ) );
1998}
1999#endif /* MBEDTLS_RSA_C */
2000
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002001#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002002/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2003 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2004 * (even though these functions don't modify it). */
2005static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2006 psa_algorithm_t alg,
2007 const uint8_t *hash,
2008 size_t hash_length,
2009 uint8_t *signature,
2010 size_t signature_size,
2011 size_t *signature_length )
2012{
2013 int ret;
2014 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002015 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002016 mbedtls_mpi_init( &r );
2017 mbedtls_mpi_init( &s );
2018
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002019 if( signature_size < 2 * curve_bytes )
2020 {
2021 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2022 goto cleanup;
2023 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002024
2025 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2026 {
2027 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2028 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2029 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2030 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2031 hash, hash_length,
2032 md_alg ) );
2033 }
2034 else
2035 {
2036 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2037 hash, hash_length,
2038 mbedtls_ctr_drbg_random,
2039 &global_data.ctr_drbg ) );
2040 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002041
2042 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2043 signature,
2044 curve_bytes ) );
2045 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2046 signature + curve_bytes,
2047 curve_bytes ) );
2048
2049cleanup:
2050 mbedtls_mpi_free( &r );
2051 mbedtls_mpi_free( &s );
2052 if( ret == 0 )
2053 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002054 return( mbedtls_to_psa_error( ret ) );
2055}
2056
2057static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2058 const uint8_t *hash,
2059 size_t hash_length,
2060 const uint8_t *signature,
2061 size_t signature_length )
2062{
2063 int ret;
2064 mbedtls_mpi r, s;
2065 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2066 mbedtls_mpi_init( &r );
2067 mbedtls_mpi_init( &s );
2068
2069 if( signature_length != 2 * curve_bytes )
2070 return( PSA_ERROR_INVALID_SIGNATURE );
2071
2072 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2073 signature,
2074 curve_bytes ) );
2075 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2076 signature + curve_bytes,
2077 curve_bytes ) );
2078
2079 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2080 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002081
2082cleanup:
2083 mbedtls_mpi_free( &r );
2084 mbedtls_mpi_free( &s );
2085 return( mbedtls_to_psa_error( ret ) );
2086}
2087#endif /* MBEDTLS_ECDSA_C */
2088
Gilles Peskine61b91d42018-06-08 16:09:36 +02002089psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2090 psa_algorithm_t alg,
2091 const uint8_t *hash,
2092 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002093 uint8_t *signature,
2094 size_t signature_size,
2095 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002096{
2097 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002098 psa_status_t status;
2099
2100 *signature_length = signature_size;
2101
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002102 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2103 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002104 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002105 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002106 {
2107 status = PSA_ERROR_INVALID_ARGUMENT;
2108 goto exit;
2109 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002110
Gilles Peskine20035e32018-02-03 22:44:14 +01002111#if defined(MBEDTLS_RSA_C)
2112 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2113 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002114 status = psa_rsa_sign( slot->data.rsa,
2115 alg,
2116 hash, hash_length,
2117 signature, signature_size,
2118 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002119 }
2120 else
2121#endif /* defined(MBEDTLS_RSA_C) */
2122#if defined(MBEDTLS_ECP_C)
2123 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2124 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002125#if defined(MBEDTLS_ECDSA_C)
2126 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002127 status = psa_ecdsa_sign( slot->data.ecp,
2128 alg,
2129 hash, hash_length,
2130 signature, signature_size,
2131 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002132 else
2133#endif /* defined(MBEDTLS_ECDSA_C) */
2134 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002135 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002136 }
itayzafrir5c753392018-05-08 11:18:38 +03002137 }
2138 else
2139#endif /* defined(MBEDTLS_ECP_C) */
2140 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002141 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002142 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002143
2144exit:
2145 /* Fill the unused part of the output buffer (the whole buffer on error,
2146 * the trailing part on success) with something that isn't a valid mac
2147 * (barring an attack on the mac and deliberately-crafted input),
2148 * in case the caller doesn't check the return status properly. */
2149 if( status == PSA_SUCCESS )
2150 memset( signature + *signature_length, '!',
2151 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002152 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002153 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002154 /* If signature_size is 0 then we have nothing to do. We must not call
2155 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002156 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002157}
2158
2159psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2160 psa_algorithm_t alg,
2161 const uint8_t *hash,
2162 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002163 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002164 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002165{
2166 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002167 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002168
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002169 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2170 if( status != PSA_SUCCESS )
2171 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002172
Gilles Peskine61b91d42018-06-08 16:09:36 +02002173#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002174 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002175 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002176 return( psa_rsa_verify( slot->data.rsa,
2177 alg,
2178 hash, hash_length,
2179 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002180 }
2181 else
2182#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002183#if defined(MBEDTLS_ECP_C)
2184 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2185 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002186#if defined(MBEDTLS_ECDSA_C)
2187 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002188 return( psa_ecdsa_verify( slot->data.ecp,
2189 hash, hash_length,
2190 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002191 else
2192#endif /* defined(MBEDTLS_ECDSA_C) */
2193 {
2194 return( PSA_ERROR_INVALID_ARGUMENT );
2195 }
itayzafrir5c753392018-05-08 11:18:38 +03002196 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002197 else
2198#endif /* defined(MBEDTLS_ECP_C) */
2199 {
2200 return( PSA_ERROR_NOT_SUPPORTED );
2201 }
2202}
2203
Gilles Peskine072ac562018-06-30 00:21:29 +02002204#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2205static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2206 mbedtls_rsa_context *rsa )
2207{
2208 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2209 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2210 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2211 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2212}
2213#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2214
Gilles Peskine61b91d42018-06-08 16:09:36 +02002215psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2216 psa_algorithm_t alg,
2217 const uint8_t *input,
2218 size_t input_length,
2219 const uint8_t *salt,
2220 size_t salt_length,
2221 uint8_t *output,
2222 size_t output_size,
2223 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002224{
2225 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002226 psa_status_t status;
2227
Darryl Green5cc689a2018-07-24 15:34:10 +01002228 (void) input;
2229 (void) input_length;
2230 (void) salt;
2231 (void) output;
2232 (void) output_size;
2233
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002234 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002235
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002236 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2237 return( PSA_ERROR_INVALID_ARGUMENT );
2238
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002239 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2240 if( status != PSA_SUCCESS )
2241 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002242 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2243 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002244 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002245
2246#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002247 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002248 {
2249 mbedtls_rsa_context *rsa = slot->data.rsa;
2250 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002251 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002252 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002253#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002254 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002255 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002256 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2257 mbedtls_ctr_drbg_random,
2258 &global_data.ctr_drbg,
2259 MBEDTLS_RSA_PUBLIC,
2260 input_length,
2261 input,
2262 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002263 }
2264 else
2265#endif /* MBEDTLS_PKCS1_V15 */
2266#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002267 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002268 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002269 psa_rsa_oaep_set_padding_mode( alg, rsa );
2270 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2271 mbedtls_ctr_drbg_random,
2272 &global_data.ctr_drbg,
2273 MBEDTLS_RSA_PUBLIC,
2274 salt, salt_length,
2275 input_length,
2276 input,
2277 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002278 }
2279 else
2280#endif /* MBEDTLS_PKCS1_V21 */
2281 {
2282 return( PSA_ERROR_INVALID_ARGUMENT );
2283 }
2284 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002285 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002286 return( mbedtls_to_psa_error( ret ) );
2287 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002288 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002289#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002290 {
2291 return( PSA_ERROR_NOT_SUPPORTED );
2292 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002293}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002294
Gilles Peskine61b91d42018-06-08 16:09:36 +02002295psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2296 psa_algorithm_t alg,
2297 const uint8_t *input,
2298 size_t input_length,
2299 const uint8_t *salt,
2300 size_t salt_length,
2301 uint8_t *output,
2302 size_t output_size,
2303 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002304{
2305 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002306 psa_status_t status;
2307
Darryl Green5cc689a2018-07-24 15:34:10 +01002308 (void) input;
2309 (void) input_length;
2310 (void) salt;
2311 (void) output;
2312 (void) output_size;
2313
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002314 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002315
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002316 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2317 return( PSA_ERROR_INVALID_ARGUMENT );
2318
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002319 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2320 if( status != PSA_SUCCESS )
2321 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002322 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2323 return( PSA_ERROR_INVALID_ARGUMENT );
2324
2325#if defined(MBEDTLS_RSA_C)
2326 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2327 {
2328 mbedtls_rsa_context *rsa = slot->data.rsa;
2329 int ret;
2330
Gilles Peskine630a18a2018-06-29 17:49:35 +02002331 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002332 return( PSA_ERROR_INVALID_ARGUMENT );
2333
2334#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002335 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002336 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002337 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2338 mbedtls_ctr_drbg_random,
2339 &global_data.ctr_drbg,
2340 MBEDTLS_RSA_PRIVATE,
2341 output_length,
2342 input,
2343 output,
2344 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002345 }
2346 else
2347#endif /* MBEDTLS_PKCS1_V15 */
2348#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002349 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002350 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002351 psa_rsa_oaep_set_padding_mode( alg, rsa );
2352 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2353 mbedtls_ctr_drbg_random,
2354 &global_data.ctr_drbg,
2355 MBEDTLS_RSA_PRIVATE,
2356 salt, salt_length,
2357 output_length,
2358 input,
2359 output,
2360 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002361 }
2362 else
2363#endif /* MBEDTLS_PKCS1_V21 */
2364 {
2365 return( PSA_ERROR_INVALID_ARGUMENT );
2366 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002367
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002368 return( mbedtls_to_psa_error( ret ) );
2369 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002370 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002371#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002372 {
2373 return( PSA_ERROR_NOT_SUPPORTED );
2374 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002375}
Gilles Peskine20035e32018-02-03 22:44:14 +01002376
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002377
2378
mohammad1603503973b2018-03-12 15:59:30 +02002379/****************************************************************/
2380/* Symmetric cryptography */
2381/****************************************************************/
2382
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002383/* Initialize the cipher operation structure. Once this function has been
2384 * called, psa_cipher_abort can run and will do the right thing. */
2385static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2386 psa_algorithm_t alg )
2387{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002388 if( ! PSA_ALG_IS_CIPHER( alg ) )
2389 {
2390 memset( operation, 0, sizeof( *operation ) );
2391 return( PSA_ERROR_INVALID_ARGUMENT );
2392 }
2393
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002394 operation->alg = alg;
2395 operation->key_set = 0;
2396 operation->iv_set = 0;
2397 operation->iv_required = 1;
2398 operation->iv_size = 0;
2399 operation->block_size = 0;
2400 mbedtls_cipher_init( &operation->ctx.cipher );
2401 return( PSA_SUCCESS );
2402}
2403
Gilles Peskinee553c652018-06-04 16:22:46 +02002404static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2405 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002406 psa_algorithm_t alg,
2407 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002408{
2409 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2410 psa_status_t status;
2411 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002412 size_t key_bits;
2413 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002414 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2415 PSA_KEY_USAGE_ENCRYPT :
2416 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002417
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002418 status = psa_cipher_init( operation, alg );
2419 if( status != PSA_SUCCESS )
2420 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002421
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002422 status = psa_get_key_from_slot( key, &slot, usage, alg);
2423 if( status != PSA_SUCCESS )
2424 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002425 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002426
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002427 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002428 if( cipher_info == NULL )
2429 return( PSA_ERROR_NOT_SUPPORTED );
2430
mohammad1603503973b2018-03-12 15:59:30 +02002431 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002432 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002433 {
2434 psa_cipher_abort( operation );
2435 return( mbedtls_to_psa_error( ret ) );
2436 }
2437
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002438#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002439 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002440 {
2441 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2442 unsigned char keys[24];
2443 memcpy( keys, slot->data.raw.data, 16 );
2444 memcpy( keys + 16, slot->data.raw.data, 8 );
2445 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2446 keys,
2447 192, cipher_operation );
2448 }
2449 else
2450#endif
2451 {
2452 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2453 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002454 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002455 }
Moran Peker41deec42018-04-04 15:43:05 +03002456 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002457 {
2458 psa_cipher_abort( operation );
2459 return( mbedtls_to_psa_error( ret ) );
2460 }
2461
mohammad16038481e742018-03-18 13:57:31 +02002462#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002463 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002464 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002465 case PSA_ALG_CBC_NO_PADDING:
2466 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2467 MBEDTLS_PADDING_NONE );
2468 break;
2469 case PSA_ALG_CBC_PKCS7:
2470 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2471 MBEDTLS_PADDING_PKCS7 );
2472 break;
2473 default:
2474 /* The algorithm doesn't involve padding. */
2475 ret = 0;
2476 break;
2477 }
2478 if( ret != 0 )
2479 {
2480 psa_cipher_abort( operation );
2481 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002482 }
2483#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2484
mohammad1603503973b2018-03-12 15:59:30 +02002485 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002486 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2487 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2488 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002489 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002490 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002491 }
mohammad1603503973b2018-03-12 15:59:30 +02002492
Moran Peker395db872018-05-31 14:07:14 +03002493 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002494}
2495
Gilles Peskinefe119512018-07-08 21:39:34 +02002496psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2497 psa_key_slot_t key,
2498 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002499{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002500 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002501}
2502
Gilles Peskinefe119512018-07-08 21:39:34 +02002503psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2504 psa_key_slot_t key,
2505 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002506{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002507 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002508}
2509
Gilles Peskinefe119512018-07-08 21:39:34 +02002510psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2511 unsigned char *iv,
2512 size_t iv_size,
2513 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002514{
itayzafrir534bd7c2018-08-02 13:56:32 +03002515 psa_status_t status;
2516 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002517 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002518 {
2519 status = PSA_ERROR_BAD_STATE;
2520 goto exit;
2521 }
Moran Peker41deec42018-04-04 15:43:05 +03002522 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002523 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002524 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002525 goto exit;
2526 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002527 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2528 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002529 if( ret != 0 )
2530 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002531 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002532 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002533 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002534
mohammad16038481e742018-03-18 13:57:31 +02002535 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002536 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002537
Moran Peker395db872018-05-31 14:07:14 +03002538exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002539 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002540 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002541 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002542}
2543
Gilles Peskinefe119512018-07-08 21:39:34 +02002544psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2545 const unsigned char *iv,
2546 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002547{
itayzafrir534bd7c2018-08-02 13:56:32 +03002548 psa_status_t status;
2549 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002550 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002551 {
2552 status = PSA_ERROR_BAD_STATE;
2553 goto exit;
2554 }
Moran Pekera28258c2018-05-29 16:25:04 +03002555 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002556 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002557 status = PSA_ERROR_INVALID_ARGUMENT;
2558 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002559 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002560 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2561 status = mbedtls_to_psa_error( ret );
2562exit:
2563 if( status == PSA_SUCCESS )
2564 operation->iv_set = 1;
2565 else
mohammad1603503973b2018-03-12 15:59:30 +02002566 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002567 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002568}
2569
Gilles Peskinee553c652018-06-04 16:22:46 +02002570psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2571 const uint8_t *input,
2572 size_t input_length,
2573 unsigned char *output,
2574 size_t output_size,
2575 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002576{
itayzafrir534bd7c2018-08-02 13:56:32 +03002577 psa_status_t status;
2578 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002579 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002580 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002581 {
2582 /* Take the unprocessed partial block left over from previous
2583 * update calls, if any, plus the input to this call. Remove
2584 * the last partial block, if any. You get the data that will be
2585 * output in this call. */
2586 expected_output_size =
2587 ( operation->ctx.cipher.unprocessed_len + input_length )
2588 / operation->block_size * operation->block_size;
2589 }
2590 else
2591 {
2592 expected_output_size = input_length;
2593 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002594
Gilles Peskine89d789c2018-06-04 17:17:16 +02002595 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002596 {
2597 status = PSA_ERROR_BUFFER_TOO_SMALL;
2598 goto exit;
2599 }
mohammad160382759612018-03-12 18:16:40 +02002600
mohammad1603503973b2018-03-12 15:59:30 +02002601 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002602 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002603 status = mbedtls_to_psa_error( ret );
2604exit:
2605 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002606 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002607 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002608}
2609
Gilles Peskinee553c652018-06-04 16:22:46 +02002610psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2611 uint8_t *output,
2612 size_t output_size,
2613 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002614{
Janos Follath315b51c2018-07-09 16:04:51 +01002615 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2616 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002617 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002618
mohammad1603503973b2018-03-12 15:59:30 +02002619 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002620 {
Janos Follath315b51c2018-07-09 16:04:51 +01002621 status = PSA_ERROR_BAD_STATE;
2622 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002623 }
2624 if( operation->iv_required && ! operation->iv_set )
2625 {
Janos Follath315b51c2018-07-09 16:04:51 +01002626 status = PSA_ERROR_BAD_STATE;
2627 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002628 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002629
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002630 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002631 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2632 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002633 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002634 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002635 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002636 }
2637
Janos Follath315b51c2018-07-09 16:04:51 +01002638 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2639 temp_output_buffer,
2640 output_length );
2641 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002642 {
Janos Follath315b51c2018-07-09 16:04:51 +01002643 status = mbedtls_to_psa_error( cipher_ret );
2644 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002645 }
Janos Follath315b51c2018-07-09 16:04:51 +01002646
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002647 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002648 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002649 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002650 memcpy( output, temp_output_buffer, *output_length );
2651 else
2652 {
Janos Follath315b51c2018-07-09 16:04:51 +01002653 status = PSA_ERROR_BUFFER_TOO_SMALL;
2654 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002655 }
mohammad1603503973b2018-03-12 15:59:30 +02002656
Janos Follath279ab8e2018-07-09 16:13:21 +01002657 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002658 status = psa_cipher_abort( operation );
2659
2660 return( status );
2661
2662error:
2663
2664 *output_length = 0;
2665
Janos Follath279ab8e2018-07-09 16:13:21 +01002666 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002667 (void) psa_cipher_abort( operation );
2668
2669 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002670}
2671
Gilles Peskinee553c652018-06-04 16:22:46 +02002672psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2673{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002674 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002675 {
2676 /* The object has (apparently) been initialized but it is not
2677 * in use. It's ok to call abort on such an object, and there's
2678 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002679 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002680 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002681
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002682 /* Sanity check (shouldn't happen: operation->alg should
2683 * always have been initialized to a valid value). */
2684 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2685 return( PSA_ERROR_BAD_STATE );
2686
mohammad1603503973b2018-03-12 15:59:30 +02002687 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002688
Moran Peker41deec42018-04-04 15:43:05 +03002689 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002690 operation->key_set = 0;
2691 operation->iv_set = 0;
2692 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002693 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002694 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002695
Moran Peker395db872018-05-31 14:07:14 +03002696 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002697}
2698
Gilles Peskinea0655c32018-04-30 17:06:50 +02002699
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002700
mohammad16038cc1cee2018-03-28 01:21:33 +03002701/****************************************************************/
2702/* Key Policy */
2703/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002704
mohammad160327010052018-07-03 13:16:15 +03002705#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002706void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002707{
Gilles Peskine803ce742018-06-18 16:07:14 +02002708 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002709}
2710
Gilles Peskine2d277862018-06-18 15:41:12 +02002711void psa_key_policy_set_usage( psa_key_policy_t *policy,
2712 psa_key_usage_t usage,
2713 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002714{
mohammad16034eed7572018-03-28 05:14:59 -07002715 policy->usage = usage;
2716 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002717}
2718
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002719psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002720{
mohammad16036df908f2018-04-02 08:34:15 -07002721 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002722}
2723
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002724psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002725{
mohammad16036df908f2018-04-02 08:34:15 -07002726 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002727}
mohammad160327010052018-07-03 13:16:15 +03002728#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002729
Gilles Peskine2d277862018-06-18 15:41:12 +02002730psa_status_t psa_set_key_policy( psa_key_slot_t key,
2731 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002732{
2733 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002734 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002735
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002736 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002737 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002738
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002739 status = psa_get_empty_key_slot( key, &slot );
2740 if( status != PSA_SUCCESS )
2741 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002742
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002743 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2744 PSA_KEY_USAGE_ENCRYPT |
2745 PSA_KEY_USAGE_DECRYPT |
2746 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002747 PSA_KEY_USAGE_VERIFY |
2748 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002749 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002750
mohammad16036df908f2018-04-02 08:34:15 -07002751 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002752
2753 return( PSA_SUCCESS );
2754}
2755
Gilles Peskine2d277862018-06-18 15:41:12 +02002756psa_status_t psa_get_key_policy( psa_key_slot_t key,
2757 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002758{
2759 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002760 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002761
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002762 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002763 return( PSA_ERROR_INVALID_ARGUMENT );
2764
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002765 status = psa_get_key_slot( key, &slot );
2766 if( status != PSA_SUCCESS )
2767 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002768
mohammad16036df908f2018-04-02 08:34:15 -07002769 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002770
2771 return( PSA_SUCCESS );
2772}
Gilles Peskine20035e32018-02-03 22:44:14 +01002773
Gilles Peskinea0655c32018-04-30 17:06:50 +02002774
2775
mohammad1603804cd712018-03-20 22:44:08 +02002776/****************************************************************/
2777/* Key Lifetime */
2778/****************************************************************/
2779
Gilles Peskine2d277862018-06-18 15:41:12 +02002780psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2781 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002782{
2783 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002784 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002785
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002786 status = psa_get_key_slot( key, &slot );
2787 if( status != PSA_SUCCESS )
2788 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002789
mohammad1603804cd712018-03-20 22:44:08 +02002790 *lifetime = slot->lifetime;
2791
2792 return( PSA_SUCCESS );
2793}
2794
Gilles Peskine2d277862018-06-18 15:41:12 +02002795psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002796 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002797{
2798 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002799 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002800
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002801 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2802 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002803 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2804 return( PSA_ERROR_INVALID_ARGUMENT );
2805
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002806 status = psa_get_empty_key_slot( key, &slot );
2807 if( status != PSA_SUCCESS )
2808 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002809
Moran Pekerd7326592018-05-29 16:56:39 +03002810 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002811 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002812
mohammad1603060ad8a2018-03-20 14:28:38 -07002813 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002814
2815 return( PSA_SUCCESS );
2816}
2817
Gilles Peskine20035e32018-02-03 22:44:14 +01002818
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002819
mohammad16035955c982018-04-26 00:53:03 +03002820/****************************************************************/
2821/* AEAD */
2822/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002823
Gilles Peskineedf9a652018-08-17 18:11:56 +02002824typedef struct
2825{
2826 key_slot_t *slot;
2827 const mbedtls_cipher_info_t *cipher_info;
2828 union
2829 {
2830#if defined(MBEDTLS_CCM_C)
2831 mbedtls_ccm_context ccm;
2832#endif /* MBEDTLS_CCM_C */
2833#if defined(MBEDTLS_GCM_C)
2834 mbedtls_gcm_context gcm;
2835#endif /* MBEDTLS_GCM_C */
2836 } ctx;
2837 uint8_t tag_length;
2838} aead_operation_t;
2839
2840static void psa_aead_abort( aead_operation_t *operation,
2841 psa_algorithm_t alg )
2842{
2843 switch( alg )
2844 {
2845#if defined(MBEDTLS_CCM_C)
2846 case PSA_ALG_CCM:
2847 mbedtls_ccm_free( &operation->ctx.ccm );
2848 break;
2849#endif /* MBEDTLS_CCM_C */
2850#if defined(MBEDTLS_CCM_C)
2851 case PSA_ALG_GCM:
2852 mbedtls_gcm_free( &operation->ctx.gcm );
2853 break;
2854#endif /* MBEDTLS_GCM_C */
2855 }
2856}
2857
2858static psa_status_t psa_aead_setup( aead_operation_t *operation,
2859 psa_key_slot_t key,
2860 psa_key_usage_t usage,
2861 psa_algorithm_t alg )
2862{
2863 psa_status_t status;
2864 size_t key_bits;
2865 mbedtls_cipher_id_t cipher_id;
2866
2867 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
2868 if( status != PSA_SUCCESS )
2869 return( status );
2870
2871 key_bits = psa_get_key_bits( operation->slot );
2872
2873 operation->cipher_info =
2874 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
2875 &cipher_id );
2876 if( operation->cipher_info == NULL )
2877 return( PSA_ERROR_NOT_SUPPORTED );
2878
2879 switch( alg )
2880 {
2881#if defined(MBEDTLS_CCM_C)
2882 case PSA_ALG_CCM:
2883 operation->tag_length = 16;
2884 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
2885 return( PSA_ERROR_INVALID_ARGUMENT );
2886 mbedtls_ccm_init( &operation->ctx.ccm );
2887 status = mbedtls_to_psa_error(
2888 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
2889 operation->slot->data.raw.data,
2890 (unsigned int) key_bits ) );
2891 if( status != 0 )
2892 goto cleanup;
2893 break;
2894#endif /* MBEDTLS_CCM_C */
2895
2896#if defined(MBEDTLS_GCM_C)
2897 case PSA_ALG_GCM:
2898 operation->tag_length = 16;
2899 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
2900 return( PSA_ERROR_INVALID_ARGUMENT );
2901 mbedtls_gcm_init( &operation->ctx.gcm );
2902 status = mbedtls_to_psa_error(
2903 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
2904 operation->slot->data.raw.data,
2905 (unsigned int) key_bits ) );
2906 break;
2907#endif /* MBEDTLS_GCM_C */
2908
2909 default:
2910 return( PSA_ERROR_NOT_SUPPORTED );
2911 }
2912
2913 return( PSA_SUCCESS );
2914
2915cleanup:
2916 psa_aead_abort( operation, alg );
2917 return( status );
2918}
2919
mohammad16035955c982018-04-26 00:53:03 +03002920psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2921 psa_algorithm_t alg,
2922 const uint8_t *nonce,
2923 size_t nonce_length,
2924 const uint8_t *additional_data,
2925 size_t additional_data_length,
2926 const uint8_t *plaintext,
2927 size_t plaintext_length,
2928 uint8_t *ciphertext,
2929 size_t ciphertext_size,
2930 size_t *ciphertext_length )
2931{
mohammad16035955c982018-04-26 00:53:03 +03002932 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02002933 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03002934 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02002935
mohammad1603f08a5502018-06-03 15:05:47 +03002936 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002937
Gilles Peskineedf9a652018-08-17 18:11:56 +02002938 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002939 if( status != PSA_SUCCESS )
2940 return( status );
mohammad16035955c982018-04-26 00:53:03 +03002941
Gilles Peskineedf9a652018-08-17 18:11:56 +02002942 /* For all currently supported modes, the tag is at the end of the
2943 * ciphertext. */
2944 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
2945 {
2946 status = PSA_ERROR_BUFFER_TOO_SMALL;
2947 goto exit;
2948 }
2949 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03002950
mohammad16035955c982018-04-26 00:53:03 +03002951 if( alg == PSA_ALG_GCM )
2952 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02002953 status = mbedtls_to_psa_error(
2954 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
2955 MBEDTLS_GCM_ENCRYPT,
2956 plaintext_length,
2957 nonce, nonce_length,
2958 additional_data, additional_data_length,
2959 plaintext, ciphertext,
2960 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03002961 }
2962 else if( alg == PSA_ALG_CCM )
2963 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02002964 status = mbedtls_to_psa_error(
2965 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
2966 plaintext_length,
2967 nonce, nonce_length,
2968 additional_data,
2969 additional_data_length,
2970 plaintext, ciphertext,
2971 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03002972 }
mohammad16035c8845f2018-05-09 05:40:09 -07002973 else
2974 {
mohammad1603554faad2018-06-03 15:07:38 +03002975 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002976 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002977
Gilles Peskineedf9a652018-08-17 18:11:56 +02002978 if( status != PSA_SUCCESS && ciphertext_size != 0 )
2979 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002980
Gilles Peskineedf9a652018-08-17 18:11:56 +02002981exit:
2982 psa_aead_abort( &operation, alg );
2983 if( status == PSA_SUCCESS )
2984 *ciphertext_length = plaintext_length + operation.tag_length;
2985 return( status );
mohammad16035955c982018-04-26 00:53:03 +03002986}
2987
Gilles Peskineee652a32018-06-01 19:23:52 +02002988/* Locate the tag in a ciphertext buffer containing the encrypted data
2989 * followed by the tag. Return the length of the part preceding the tag in
2990 * *plaintext_length. This is the size of the plaintext in modes where
2991 * the encrypted data has the same size as the plaintext, such as
2992 * CCM and GCM. */
2993static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2994 const uint8_t *ciphertext,
2995 size_t ciphertext_length,
2996 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002997 const uint8_t **p_tag )
2998{
2999 size_t payload_length;
3000 if( tag_length > ciphertext_length )
3001 return( PSA_ERROR_INVALID_ARGUMENT );
3002 payload_length = ciphertext_length - tag_length;
3003 if( payload_length > plaintext_size )
3004 return( PSA_ERROR_BUFFER_TOO_SMALL );
3005 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003006 return( PSA_SUCCESS );
3007}
3008
mohammad16035955c982018-04-26 00:53:03 +03003009psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3010 psa_algorithm_t alg,
3011 const uint8_t *nonce,
3012 size_t nonce_length,
3013 const uint8_t *additional_data,
3014 size_t additional_data_length,
3015 const uint8_t *ciphertext,
3016 size_t ciphertext_length,
3017 uint8_t *plaintext,
3018 size_t plaintext_size,
3019 size_t *plaintext_length )
3020{
mohammad16035955c982018-04-26 00:53:03 +03003021 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003022 aead_operation_t operation;
3023 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003024
Gilles Peskineee652a32018-06-01 19:23:52 +02003025 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003026
Gilles Peskineedf9a652018-08-17 18:11:56 +02003027 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003028 if( status != PSA_SUCCESS )
3029 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003030
mohammad16035955c982018-04-26 00:53:03 +03003031 if( alg == PSA_ALG_GCM )
3032 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003033 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003034 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003035 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003036 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003037 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003038
Gilles Peskineedf9a652018-08-17 18:11:56 +02003039 status = mbedtls_to_psa_error(
3040 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3041 ciphertext_length - operation.tag_length,
3042 nonce, nonce_length,
3043 additional_data,
3044 additional_data_length,
3045 tag, operation.tag_length,
3046 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003047 }
3048 else if( alg == PSA_ALG_CCM )
3049 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003050 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003051 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003052 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003053 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003054 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003055
Gilles Peskineedf9a652018-08-17 18:11:56 +02003056 status = mbedtls_to_psa_error(
3057 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3058 ciphertext_length - operation.tag_length,
3059 nonce, nonce_length,
3060 additional_data,
3061 additional_data_length,
3062 ciphertext, plaintext,
3063 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003064 }
mohammad160339574652018-06-01 04:39:53 -07003065 else
3066 {
mohammad1603554faad2018-06-03 15:07:38 +03003067 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003068 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003069
Gilles Peskineedf9a652018-08-17 18:11:56 +02003070 if( status != PSA_SUCCESS && plaintext_size != 0 )
3071 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003072
Gilles Peskineedf9a652018-08-17 18:11:56 +02003073exit:
3074 psa_aead_abort( &operation, alg );
3075 if( status == PSA_SUCCESS )
3076 *plaintext_length = ciphertext_length - operation.tag_length;
3077 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003078}
3079
Gilles Peskinea0655c32018-04-30 17:06:50 +02003080
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003081
Gilles Peskine20035e32018-02-03 22:44:14 +01003082/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003083/* Generators */
3084/****************************************************************/
3085
3086psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3087{
3088 psa_status_t status = PSA_SUCCESS;
3089 if( generator->alg == 0 )
3090 {
3091 /* The object has (apparently) been initialized but it is not
3092 * in use. It's ok to call abort on such an object, and there's
3093 * nothing to do. */
3094 }
3095 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003096#if defined(MBEDTLS_MD_C)
3097 if( PSA_ALG_IS_HKDF( generator->alg ) )
3098 {
3099 mbedtls_free( generator->ctx.hkdf.info );
3100 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3101 }
3102 else
3103#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003104 {
3105 status = PSA_ERROR_BAD_STATE;
3106 }
3107 memset( generator, 0, sizeof( *generator ) );
3108 return( status );
3109}
3110
3111
3112psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3113 size_t *capacity)
3114{
3115 *capacity = generator->capacity;
3116 return( PSA_SUCCESS );
3117}
3118
Gilles Peskinebef7f142018-07-12 17:22:21 +02003119#if defined(MBEDTLS_MD_C)
3120/* Read some bytes from an HKDF-based generator. This performs a chunk
3121 * of the expand phase of the HKDF algorithm. */
3122static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3123 psa_algorithm_t hash_alg,
3124 uint8_t *output,
3125 size_t output_length )
3126{
3127 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3128 psa_status_t status;
3129
3130 while( output_length != 0 )
3131 {
3132 /* Copy what remains of the current block */
3133 uint8_t n = hash_length - hkdf->offset_in_block;
3134 if( n > output_length )
3135 n = (uint8_t) output_length;
3136 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3137 output += n;
3138 output_length -= n;
3139 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003140 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003141 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003142 /* We can't be wanting more output after block 0xff, otherwise
3143 * the capacity check in psa_generator_read() would have
3144 * prevented this call. It could happen only if the generator
3145 * object was corrupted or if this function is called directly
3146 * inside the library. */
3147 if( hkdf->block_number == 0xff )
3148 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003149
3150 /* We need a new block */
3151 ++hkdf->block_number;
3152 hkdf->offset_in_block = 0;
3153 status = psa_hmac_setup_internal( &hkdf->hmac,
3154 hkdf->prk, hash_length,
3155 hash_alg );
3156 if( status != PSA_SUCCESS )
3157 return( status );
3158 if( hkdf->block_number != 1 )
3159 {
3160 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3161 hkdf->output_block,
3162 hash_length );
3163 if( status != PSA_SUCCESS )
3164 return( status );
3165 }
3166 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3167 hkdf->info,
3168 hkdf->info_length );
3169 if( status != PSA_SUCCESS )
3170 return( status );
3171 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3172 &hkdf->block_number, 1 );
3173 if( status != PSA_SUCCESS )
3174 return( status );
3175 status = psa_hmac_finish_internal( &hkdf->hmac,
3176 hkdf->output_block,
3177 sizeof( hkdf->output_block ) );
3178 if( status != PSA_SUCCESS )
3179 return( status );
3180 }
3181
3182 return( PSA_SUCCESS );
3183}
3184#endif /* MBEDTLS_MD_C */
3185
Gilles Peskineeab56e42018-07-12 17:12:33 +02003186psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3187 uint8_t *output,
3188 size_t output_length )
3189{
3190 psa_status_t status;
3191
3192 if( output_length > generator->capacity )
3193 {
3194 generator->capacity = 0;
3195 /* Go through the error path to wipe all confidential data now
3196 * that the generator object is useless. */
3197 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3198 goto exit;
3199 }
3200 if( output_length == 0 &&
3201 generator->capacity == 0 && generator->alg == 0 )
3202 {
3203 /* Edge case: this is a blank or finished generator, and 0
3204 * bytes were requested. The right error in this case could
3205 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3206 * INSUFFICIENT_CAPACITY, which is right for a finished
3207 * generator, for consistency with the case when
3208 * output_length > 0. */
3209 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3210 }
3211 generator->capacity -= output_length;
3212
Gilles Peskinebef7f142018-07-12 17:22:21 +02003213#if defined(MBEDTLS_MD_C)
3214 if( PSA_ALG_IS_HKDF( generator->alg ) )
3215 {
3216 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3217 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3218 output, output_length );
3219 }
3220 else
3221#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003222 {
3223 return( PSA_ERROR_BAD_STATE );
3224 }
3225
3226exit:
3227 if( status != PSA_SUCCESS )
3228 {
3229 psa_generator_abort( generator );
3230 memset( output, '!', output_length );
3231 }
3232 return( status );
3233}
3234
Gilles Peskine08542d82018-07-19 17:05:42 +02003235#if defined(MBEDTLS_DES_C)
3236static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3237{
3238 if( data_size >= 8 )
3239 mbedtls_des_key_set_parity( data );
3240 if( data_size >= 16 )
3241 mbedtls_des_key_set_parity( data + 8 );
3242 if( data_size >= 24 )
3243 mbedtls_des_key_set_parity( data + 16 );
3244}
3245#endif /* MBEDTLS_DES_C */
3246
Gilles Peskineeab56e42018-07-12 17:12:33 +02003247psa_status_t psa_generator_import_key( psa_key_slot_t key,
3248 psa_key_type_t type,
3249 size_t bits,
3250 psa_crypto_generator_t *generator )
3251{
3252 uint8_t *data = NULL;
3253 size_t bytes = PSA_BITS_TO_BYTES( bits );
3254 psa_status_t status;
3255
3256 if( ! key_type_is_raw_bytes( type ) )
3257 return( PSA_ERROR_INVALID_ARGUMENT );
3258 if( bits % 8 != 0 )
3259 return( PSA_ERROR_INVALID_ARGUMENT );
3260 data = mbedtls_calloc( 1, bytes );
3261 if( data == NULL )
3262 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3263
3264 status = psa_generator_read( generator, data, bytes );
3265 if( status != PSA_SUCCESS )
3266 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003267#if defined(MBEDTLS_DES_C)
3268 if( type == PSA_KEY_TYPE_DES )
3269 psa_des_set_key_parity( data, bytes );
3270#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003271 status = psa_import_key( key, type, data, bytes );
3272
3273exit:
3274 mbedtls_free( data );
3275 return( status );
3276}
3277
3278
3279
3280/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003281/* Key derivation */
3282/****************************************************************/
3283
Gilles Peskinebef7f142018-07-12 17:22:21 +02003284/* Set up an HKDF-based generator. This is exactly the extract phase
3285 * of the HKDF algorithm. */
3286static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
3287 key_slot_t *slot,
3288 psa_algorithm_t hash_alg,
3289 const uint8_t *salt,
3290 size_t salt_length,
3291 const uint8_t *label,
3292 size_t label_length )
3293{
3294 psa_status_t status;
3295 status = psa_hmac_setup_internal( &hkdf->hmac,
3296 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003297 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003298 if( status != PSA_SUCCESS )
3299 return( status );
3300 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3301 slot->data.raw.data,
3302 slot->data.raw.bytes );
3303 if( status != PSA_SUCCESS )
3304 return( status );
3305 status = psa_hmac_finish_internal( &hkdf->hmac,
3306 hkdf->prk,
3307 sizeof( hkdf->prk ) );
3308 if( status != PSA_SUCCESS )
3309 return( status );
3310 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3311 hkdf->block_number = 0;
3312 hkdf->info_length = label_length;
3313 if( label_length != 0 )
3314 {
3315 hkdf->info = mbedtls_calloc( 1, label_length );
3316 if( hkdf->info == NULL )
3317 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3318 memcpy( hkdf->info, label, label_length );
3319 }
3320 return( PSA_SUCCESS );
3321}
3322
Gilles Peskineea0fb492018-07-12 17:17:20 +02003323psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01003324 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003325 psa_algorithm_t alg,
3326 const uint8_t *salt,
3327 size_t salt_length,
3328 const uint8_t *label,
3329 size_t label_length,
3330 size_t capacity )
3331{
3332 key_slot_t *slot;
3333 psa_status_t status;
3334
3335 if( generator->alg != 0 )
3336 return( PSA_ERROR_BAD_STATE );
3337
3338 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
3339 if( status != PSA_SUCCESS )
3340 return( status );
3341 if( slot->type != PSA_KEY_TYPE_DERIVE )
3342 return( PSA_ERROR_INVALID_ARGUMENT );
3343
3344 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
3345 return( PSA_ERROR_INVALID_ARGUMENT );
3346
Gilles Peskinebef7f142018-07-12 17:22:21 +02003347#if defined(MBEDTLS_MD_C)
3348 if( PSA_ALG_IS_HKDF( alg ) )
3349 {
3350 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3351 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3352 if( hash_size == 0 )
3353 return( PSA_ERROR_NOT_SUPPORTED );
3354 if( capacity > 255 * hash_size )
3355 return( PSA_ERROR_INVALID_ARGUMENT );
3356 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3357 slot,
3358 hash_alg,
3359 salt, salt_length,
3360 label, label_length );
3361 }
3362 else
3363#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +02003364 {
3365 return( PSA_ERROR_NOT_SUPPORTED );
3366 }
3367
3368 /* Set generator->alg even on failure so that abort knows what to do. */
3369 generator->alg = alg;
3370 if( status == PSA_SUCCESS )
3371 generator->capacity = capacity;
3372 else
3373 psa_generator_abort( generator );
3374 return( status );
3375}
3376
3377
3378
3379/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003380/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02003381/****************************************************************/
3382
3383psa_status_t psa_generate_random( uint8_t *output,
3384 size_t output_size )
3385{
itayzafrir0adf0fc2018-09-06 16:24:41 +03003386 int ret;
3387 GUARD_MODULE_INITIALIZED;
3388
3389 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02003390 return( mbedtls_to_psa_error( ret ) );
3391}
3392
3393psa_status_t psa_generate_key( psa_key_slot_t key,
3394 psa_key_type_t type,
3395 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003396 const void *extra,
3397 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02003398{
Gilles Peskine12313cd2018-06-20 00:20:32 +02003399 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003400 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003401
Gilles Peskine53d991e2018-07-12 01:14:59 +02003402 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003403 return( PSA_ERROR_INVALID_ARGUMENT );
3404
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003405 status = psa_get_empty_key_slot( key, &slot );
3406 if( status != PSA_SUCCESS )
3407 return( status );
3408
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003409 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003410 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003411 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003412 if( status != PSA_SUCCESS )
3413 return( status );
3414 status = psa_generate_random( slot->data.raw.data,
3415 slot->data.raw.bytes );
3416 if( status != PSA_SUCCESS )
3417 {
3418 mbedtls_free( slot->data.raw.data );
3419 return( status );
3420 }
3421#if defined(MBEDTLS_DES_C)
3422 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02003423 psa_des_set_key_parity( slot->data.raw.data,
3424 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003425#endif /* MBEDTLS_DES_C */
3426 }
3427 else
3428
3429#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3430 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3431 {
3432 mbedtls_rsa_context *rsa;
3433 int ret;
3434 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003435 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3436 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02003437 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003438 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02003439 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003440 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003441 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003442#if INT_MAX < 0xffffffff
3443 /* Check that the uint32_t value passed by the caller fits
3444 * in the range supported by this implementation. */
3445 if( p->e > INT_MAX )
3446 return( PSA_ERROR_NOT_SUPPORTED );
3447#endif
3448 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003449 }
3450 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3451 if( rsa == NULL )
3452 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3453 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3454 ret = mbedtls_rsa_gen_key( rsa,
3455 mbedtls_ctr_drbg_random,
3456 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003457 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003458 exponent );
3459 if( ret != 0 )
3460 {
3461 mbedtls_rsa_free( rsa );
3462 mbedtls_free( rsa );
3463 return( mbedtls_to_psa_error( ret ) );
3464 }
3465 slot->data.rsa = rsa;
3466 }
3467 else
3468#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3469
3470#if defined(MBEDTLS_ECP_C)
3471 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3472 {
3473 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3474 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3475 const mbedtls_ecp_curve_info *curve_info =
3476 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3477 mbedtls_ecp_keypair *ecp;
3478 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003479 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003480 return( PSA_ERROR_NOT_SUPPORTED );
3481 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3482 return( PSA_ERROR_NOT_SUPPORTED );
3483 if( curve_info->bit_size != bits )
3484 return( PSA_ERROR_INVALID_ARGUMENT );
3485 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3486 if( ecp == NULL )
3487 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3488 mbedtls_ecp_keypair_init( ecp );
3489 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3490 mbedtls_ctr_drbg_random,
3491 &global_data.ctr_drbg );
3492 if( ret != 0 )
3493 {
3494 mbedtls_ecp_keypair_free( ecp );
3495 mbedtls_free( ecp );
3496 return( mbedtls_to_psa_error( ret ) );
3497 }
3498 slot->data.ecp = ecp;
3499 }
3500 else
3501#endif /* MBEDTLS_ECP_C */
3502
3503 return( PSA_ERROR_NOT_SUPPORTED );
3504
3505 slot->type = type;
3506 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003507}
3508
3509
3510/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003511/* Module setup */
3512/****************************************************************/
3513
Gilles Peskinee59236f2018-01-27 23:32:46 +01003514void mbedtls_psa_crypto_free( void )
3515{
Jaeden Amero045bd502018-06-26 14:00:08 +01003516 psa_key_slot_t key;
Gilles Peskine9a056342018-08-01 15:46:54 +02003517 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003518 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003519 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3520 mbedtls_entropy_free( &global_data.entropy );
3521 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3522}
3523
3524psa_status_t psa_crypto_init( void )
3525{
3526 int ret;
3527 const unsigned char drbg_seed[] = "PSA";
3528
3529 if( global_data.initialized != 0 )
3530 return( PSA_SUCCESS );
3531
3532 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3533 mbedtls_entropy_init( &global_data.entropy );
3534 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3535
3536 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3537 mbedtls_entropy_func,
3538 &global_data.entropy,
3539 drbg_seed, sizeof( drbg_seed ) - 1 );
3540 if( ret != 0 )
3541 goto exit;
3542
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003543 global_data.initialized = 1;
3544
Gilles Peskinee59236f2018-01-27 23:32:46 +01003545exit:
3546 if( ret != 0 )
3547 mbedtls_psa_crypto_free( );
3548 return( mbedtls_to_psa_error( ret ) );
3549}
3550
3551#endif /* MBEDTLS_PSA_CRYPTO_C */