blob: 0100441aca20eb8ec4095cc504408bf726c447e2 [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 Peskineda8191d2018-07-08 19:46:38 +0200976psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100977 psa_algorithm_t alg )
978{
979 int ret;
980 operation->alg = 0;
981 switch( alg )
982 {
983#if defined(MBEDTLS_MD2_C)
984 case PSA_ALG_MD2:
985 mbedtls_md2_init( &operation->ctx.md2 );
986 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
987 break;
988#endif
989#if defined(MBEDTLS_MD4_C)
990 case PSA_ALG_MD4:
991 mbedtls_md4_init( &operation->ctx.md4 );
992 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
993 break;
994#endif
995#if defined(MBEDTLS_MD5_C)
996 case PSA_ALG_MD5:
997 mbedtls_md5_init( &operation->ctx.md5 );
998 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
999 break;
1000#endif
1001#if defined(MBEDTLS_RIPEMD160_C)
1002 case PSA_ALG_RIPEMD160:
1003 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1004 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1005 break;
1006#endif
1007#if defined(MBEDTLS_SHA1_C)
1008 case PSA_ALG_SHA_1:
1009 mbedtls_sha1_init( &operation->ctx.sha1 );
1010 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1011 break;
1012#endif
1013#if defined(MBEDTLS_SHA256_C)
1014 case PSA_ALG_SHA_224:
1015 mbedtls_sha256_init( &operation->ctx.sha256 );
1016 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1017 break;
1018 case PSA_ALG_SHA_256:
1019 mbedtls_sha256_init( &operation->ctx.sha256 );
1020 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1021 break;
1022#endif
1023#if defined(MBEDTLS_SHA512_C)
1024 case PSA_ALG_SHA_384:
1025 mbedtls_sha512_init( &operation->ctx.sha512 );
1026 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1027 break;
1028 case PSA_ALG_SHA_512:
1029 mbedtls_sha512_init( &operation->ctx.sha512 );
1030 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1031 break;
1032#endif
1033 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001034 return( PSA_ALG_IS_HASH( alg ) ?
1035 PSA_ERROR_NOT_SUPPORTED :
1036 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001037 }
1038 if( ret == 0 )
1039 operation->alg = alg;
1040 else
1041 psa_hash_abort( operation );
1042 return( mbedtls_to_psa_error( ret ) );
1043}
1044
1045psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1046 const uint8_t *input,
1047 size_t input_length )
1048{
1049 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001050
1051 /* Don't require hash implementations to behave correctly on a
1052 * zero-length input, which may have an invalid pointer. */
1053 if( input_length == 0 )
1054 return( PSA_SUCCESS );
1055
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001056 switch( operation->alg )
1057 {
1058#if defined(MBEDTLS_MD2_C)
1059 case PSA_ALG_MD2:
1060 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1061 input, input_length );
1062 break;
1063#endif
1064#if defined(MBEDTLS_MD4_C)
1065 case PSA_ALG_MD4:
1066 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1067 input, input_length );
1068 break;
1069#endif
1070#if defined(MBEDTLS_MD5_C)
1071 case PSA_ALG_MD5:
1072 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1073 input, input_length );
1074 break;
1075#endif
1076#if defined(MBEDTLS_RIPEMD160_C)
1077 case PSA_ALG_RIPEMD160:
1078 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1079 input, input_length );
1080 break;
1081#endif
1082#if defined(MBEDTLS_SHA1_C)
1083 case PSA_ALG_SHA_1:
1084 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1085 input, input_length );
1086 break;
1087#endif
1088#if defined(MBEDTLS_SHA256_C)
1089 case PSA_ALG_SHA_224:
1090 case PSA_ALG_SHA_256:
1091 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1092 input, input_length );
1093 break;
1094#endif
1095#if defined(MBEDTLS_SHA512_C)
1096 case PSA_ALG_SHA_384:
1097 case PSA_ALG_SHA_512:
1098 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1099 input, input_length );
1100 break;
1101#endif
1102 default:
1103 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1104 break;
1105 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001106
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001107 if( ret != 0 )
1108 psa_hash_abort( operation );
1109 return( mbedtls_to_psa_error( ret ) );
1110}
1111
1112psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1113 uint8_t *hash,
1114 size_t hash_size,
1115 size_t *hash_length )
1116{
itayzafrir40835d42018-08-02 13:14:17 +03001117 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001118 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001119 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001120
1121 /* Fill the output buffer with something that isn't a valid hash
1122 * (barring an attack on the hash and deliberately-crafted input),
1123 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001124 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001125 /* If hash_size is 0 then hash may be NULL and then the
1126 * call to memset would have undefined behavior. */
1127 if( hash_size != 0 )
1128 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001129
1130 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001131 {
1132 status = PSA_ERROR_BUFFER_TOO_SMALL;
1133 goto exit;
1134 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001135
1136 switch( operation->alg )
1137 {
1138#if defined(MBEDTLS_MD2_C)
1139 case PSA_ALG_MD2:
1140 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1141 break;
1142#endif
1143#if defined(MBEDTLS_MD4_C)
1144 case PSA_ALG_MD4:
1145 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1146 break;
1147#endif
1148#if defined(MBEDTLS_MD5_C)
1149 case PSA_ALG_MD5:
1150 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1151 break;
1152#endif
1153#if defined(MBEDTLS_RIPEMD160_C)
1154 case PSA_ALG_RIPEMD160:
1155 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1156 break;
1157#endif
1158#if defined(MBEDTLS_SHA1_C)
1159 case PSA_ALG_SHA_1:
1160 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1161 break;
1162#endif
1163#if defined(MBEDTLS_SHA256_C)
1164 case PSA_ALG_SHA_224:
1165 case PSA_ALG_SHA_256:
1166 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1167 break;
1168#endif
1169#if defined(MBEDTLS_SHA512_C)
1170 case PSA_ALG_SHA_384:
1171 case PSA_ALG_SHA_512:
1172 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1173 break;
1174#endif
1175 default:
1176 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1177 break;
1178 }
itayzafrir40835d42018-08-02 13:14:17 +03001179 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001180
itayzafrir40835d42018-08-02 13:14:17 +03001181exit:
1182 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001183 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001184 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001185 return( psa_hash_abort( operation ) );
1186 }
1187 else
1188 {
1189 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001190 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001191 }
1192}
1193
Gilles Peskine2d277862018-06-18 15:41:12 +02001194psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1195 const uint8_t *hash,
1196 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001197{
1198 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1199 size_t actual_hash_length;
1200 psa_status_t status = psa_hash_finish( operation,
1201 actual_hash, sizeof( actual_hash ),
1202 &actual_hash_length );
1203 if( status != PSA_SUCCESS )
1204 return( status );
1205 if( actual_hash_length != hash_length )
1206 return( PSA_ERROR_INVALID_SIGNATURE );
1207 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1208 return( PSA_ERROR_INVALID_SIGNATURE );
1209 return( PSA_SUCCESS );
1210}
1211
1212
1213
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001214/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001215/* MAC */
1216/****************************************************************/
1217
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001218static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001219 psa_algorithm_t alg,
1220 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001221 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001222 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001223{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001224 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001225 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001226
1227 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1228 {
1229 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001230 {
1231 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1232 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001233
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001234 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001235 {
Gilles Peskine5d1888e2018-07-12 00:32:42 +02001236 case PSA_ALG_STREAM_CIPHER_BASE:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001237 mode = MBEDTLS_MODE_STREAM;
1238 break;
1239 case PSA_ALG_CBC_BASE:
1240 mode = MBEDTLS_MODE_CBC;
1241 break;
1242 case PSA_ALG_CFB_BASE:
1243 mode = MBEDTLS_MODE_CFB;
1244 break;
1245 case PSA_ALG_OFB_BASE:
1246 mode = MBEDTLS_MODE_OFB;
1247 break;
1248 case PSA_ALG_CTR:
1249 mode = MBEDTLS_MODE_CTR;
1250 break;
1251 case PSA_ALG_CCM:
1252 mode = MBEDTLS_MODE_CCM;
1253 break;
1254 case PSA_ALG_GCM:
1255 mode = MBEDTLS_MODE_GCM;
1256 break;
1257 default:
1258 return( NULL );
1259 }
1260 }
1261 else if( alg == PSA_ALG_CMAC )
1262 mode = MBEDTLS_MODE_ECB;
1263 else if( alg == PSA_ALG_GMAC )
1264 mode = MBEDTLS_MODE_GCM;
1265 else
1266 return( NULL );
1267
1268 switch( key_type )
1269 {
1270 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001271 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001272 break;
1273 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001274 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1275 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001276 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001277 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001278 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001279 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001280 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1281 * but two-key Triple-DES is functionally three-key Triple-DES
1282 * with K1=K3, so that's how we present it to mbedtls. */
1283 if( key_bits == 128 )
1284 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001285 break;
1286 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001287 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001288 break;
1289 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001290 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001291 break;
1292 default:
1293 return( NULL );
1294 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001295 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001296 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001297
Jaeden Amero23bbb752018-06-26 14:16:54 +01001298 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1299 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001300}
1301
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001302static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001303{
Gilles Peskine2d277862018-06-18 15:41:12 +02001304 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001305 {
1306 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001307 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001308 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001309 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001310 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001311 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001312 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001313 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001314 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001315 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001316 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001317 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001318 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001319 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001320 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001321 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001322 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001323 return( 128 );
1324 default:
1325 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001326 }
1327}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001328
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001329/* Initialize the MAC operation structure. Once this function has been
1330 * called, psa_mac_abort can run and will do the right thing. */
1331static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1332 psa_algorithm_t alg )
1333{
1334 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1335
1336 operation->alg = alg;
1337 operation->key_set = 0;
1338 operation->iv_set = 0;
1339 operation->iv_required = 0;
1340 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001341 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001342
1343#if defined(MBEDTLS_CMAC_C)
1344 if( alg == PSA_ALG_CMAC )
1345 {
1346 operation->iv_required = 0;
1347 mbedtls_cipher_init( &operation->ctx.cmac );
1348 status = PSA_SUCCESS;
1349 }
1350 else
1351#endif /* MBEDTLS_CMAC_C */
1352#if defined(MBEDTLS_MD_C)
1353 if( PSA_ALG_IS_HMAC( operation->alg ) )
1354 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001355 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1356 operation->ctx.hmac.hash_ctx.alg = 0;
1357 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001358 }
1359 else
1360#endif /* MBEDTLS_MD_C */
1361 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001362 if( ! PSA_ALG_IS_MAC( alg ) )
1363 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001364 }
1365
1366 if( status != PSA_SUCCESS )
1367 memset( operation, 0, sizeof( *operation ) );
1368 return( status );
1369}
1370
Gilles Peskine01126fa2018-07-12 17:04:55 +02001371#if defined(MBEDTLS_MD_C)
1372static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1373{
1374 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1375 return( psa_hash_abort( &hmac->hash_ctx ) );
1376}
1377#endif /* MBEDTLS_MD_C */
1378
Gilles Peskine8c9def32018-02-08 10:02:12 +01001379psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1380{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001381 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001382 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001383 /* The object has (apparently) been initialized but it is not
1384 * in use. It's ok to call abort on such an object, and there's
1385 * nothing to do. */
1386 return( PSA_SUCCESS );
1387 }
1388 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001389#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001390 if( operation->alg == PSA_ALG_CMAC )
1391 {
1392 mbedtls_cipher_free( &operation->ctx.cmac );
1393 }
1394 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001395#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001396#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001397 if( PSA_ALG_IS_HMAC( operation->alg ) )
1398 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001399 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001400 }
1401 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001402#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001403 {
1404 /* Sanity check (shouldn't happen: operation->alg should
1405 * always have been initialized to a valid value). */
1406 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001407 }
Moran Peker41deec42018-04-04 15:43:05 +03001408
Gilles Peskine8c9def32018-02-08 10:02:12 +01001409 operation->alg = 0;
1410 operation->key_set = 0;
1411 operation->iv_set = 0;
1412 operation->iv_required = 0;
1413 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001414 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001415
Gilles Peskine8c9def32018-02-08 10:02:12 +01001416 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001417
1418bad_state:
1419 /* If abort is called on an uninitialized object, we can't trust
1420 * anything. Wipe the object in case it contains confidential data.
1421 * This may result in a memory leak if a pointer gets overwritten,
1422 * but it's too late to do anything about this. */
1423 memset( operation, 0, sizeof( *operation ) );
1424 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001425}
1426
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001427#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001428static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001429 size_t key_bits,
1430 key_slot_t *slot,
1431 const mbedtls_cipher_info_t *cipher_info )
1432{
1433 int ret;
1434
1435 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001436
1437 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1438 if( ret != 0 )
1439 return( ret );
1440
1441 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1442 slot->data.raw.data,
1443 key_bits );
1444 return( ret );
1445}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001446#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001447
Gilles Peskine248051a2018-06-20 16:09:38 +02001448#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001449static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1450 const uint8_t *key,
1451 size_t key_length,
1452 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001453{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001454 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001455 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001456 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001457 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001458 psa_status_t status;
1459
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001460 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1461 * overflow below. This should never trigger if the hash algorithm
1462 * is implemented correctly. */
1463 /* The size checks against the ipad and opad buffers cannot be written
1464 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1465 * because that triggers -Wlogical-op on GCC 7.3. */
1466 if( block_size > sizeof( ipad ) )
1467 return( PSA_ERROR_NOT_SUPPORTED );
1468 if( block_size > sizeof( hmac->opad ) )
1469 return( PSA_ERROR_NOT_SUPPORTED );
1470 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001471 return( PSA_ERROR_NOT_SUPPORTED );
1472
Gilles Peskined223b522018-06-11 18:12:58 +02001473 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001474 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001475 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001476 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001477 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001478 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001479 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001480 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001481 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001482 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001483 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001484 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001485 }
Gilles Peskine96889972018-07-12 17:07:03 +02001486 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1487 * but it is permitted. It is common when HMAC is used in HKDF, for
1488 * example. Don't call `memcpy` in the 0-length because `key` could be
1489 * an invalid pointer which would make the behavior undefined. */
1490 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001491 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001492
Gilles Peskined223b522018-06-11 18:12:58 +02001493 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1494 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001495 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001496 ipad[i] ^= 0x36;
1497 memset( ipad + key_length, 0x36, block_size - key_length );
1498
1499 /* Copy the key material from ipad to opad, flipping the requisite bits,
1500 * and filling the rest of opad with the requisite constant. */
1501 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001502 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1503 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001504
Gilles Peskine01126fa2018-07-12 17:04:55 +02001505 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001506 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001507 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001508
Gilles Peskine01126fa2018-07-12 17:04:55 +02001509 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001510
1511cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001512 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001513
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001514 return( status );
1515}
Gilles Peskine248051a2018-06-20 16:09:38 +02001516#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001517
Gilles Peskine89167cb2018-07-08 20:12:23 +02001518static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1519 psa_key_slot_t key,
1520 psa_algorithm_t alg,
1521 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001522{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001523 psa_status_t status;
1524 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001525 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001526 psa_key_usage_t usage =
1527 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001528
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001529 status = psa_mac_init( operation, alg );
1530 if( status != PSA_SUCCESS )
1531 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001532 if( is_sign )
1533 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001534
Gilles Peskine89167cb2018-07-08 20:12:23 +02001535 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001536 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001537 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001538 key_bits = psa_get_key_bits( slot );
1539
Gilles Peskine8c9def32018-02-08 10:02:12 +01001540#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001541 if( alg == PSA_ALG_CMAC )
1542 {
1543 const mbedtls_cipher_info_t *cipher_info =
1544 mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
1545 int ret;
1546 if( cipher_info == NULL )
1547 {
1548 status = PSA_ERROR_NOT_SUPPORTED;
1549 goto exit;
1550 }
1551 operation->mac_size = cipher_info->block_size;
1552 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1553 status = mbedtls_to_psa_error( ret );
1554 }
1555 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001556#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001557#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001558 if( PSA_ALG_IS_HMAC( alg ) )
1559 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001560 psa_algorithm_t hash_alg = PSA_ALG_HMAC_HASH( alg );
1561 if( hash_alg == 0 )
1562 {
1563 status = PSA_ERROR_NOT_SUPPORTED;
1564 goto exit;
1565 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001566
1567 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1568 /* Sanity check. This shouldn't fail on a valid configuration. */
1569 if( operation->mac_size == 0 ||
1570 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1571 {
1572 status = PSA_ERROR_NOT_SUPPORTED;
1573 goto exit;
1574 }
1575
Gilles Peskine01126fa2018-07-12 17:04:55 +02001576 if( slot->type != PSA_KEY_TYPE_HMAC )
1577 {
1578 status = PSA_ERROR_INVALID_ARGUMENT;
1579 goto exit;
1580 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001581
Gilles Peskine01126fa2018-07-12 17:04:55 +02001582 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1583 slot->data.raw.data,
1584 slot->data.raw.bytes,
1585 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001586 }
1587 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001588#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001589 {
1590 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001591 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001592
Gilles Peskinefbfac682018-07-08 20:51:54 +02001593exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001594 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001595 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001596 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001597 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001598 else
1599 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001600 operation->key_set = 1;
1601 }
1602 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001603}
1604
Gilles Peskine89167cb2018-07-08 20:12:23 +02001605psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1606 psa_key_slot_t key,
1607 psa_algorithm_t alg )
1608{
1609 return( psa_mac_setup( operation, key, alg, 1 ) );
1610}
1611
1612psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1613 psa_key_slot_t key,
1614 psa_algorithm_t alg )
1615{
1616 return( psa_mac_setup( operation, key, alg, 0 ) );
1617}
1618
Gilles Peskine8c9def32018-02-08 10:02:12 +01001619psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1620 const uint8_t *input,
1621 size_t input_length )
1622{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001623 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001624 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001625 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001626 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001627 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001628 operation->has_input = 1;
1629
Gilles Peskine8c9def32018-02-08 10:02:12 +01001630#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001631 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001632 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001633 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1634 input, input_length );
1635 status = mbedtls_to_psa_error( ret );
1636 }
1637 else
1638#endif /* MBEDTLS_CMAC_C */
1639#if defined(MBEDTLS_MD_C)
1640 if( PSA_ALG_IS_HMAC( operation->alg ) )
1641 {
1642 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1643 input_length );
1644 }
1645 else
1646#endif /* MBEDTLS_MD_C */
1647 {
1648 /* This shouldn't happen if `operation` was initialized by
1649 * a setup function. */
1650 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001651 }
1652
Gilles Peskinefbfac682018-07-08 20:51:54 +02001653cleanup:
1654 if( status != PSA_SUCCESS )
1655 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001656 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001657}
1658
Gilles Peskine01126fa2018-07-12 17:04:55 +02001659#if defined(MBEDTLS_MD_C)
1660static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1661 uint8_t *mac,
1662 size_t mac_size )
1663{
1664 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1665 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1666 size_t hash_size = 0;
1667 size_t block_size = psa_get_hash_block_size( hash_alg );
1668 psa_status_t status;
1669
Gilles Peskine01126fa2018-07-12 17:04:55 +02001670 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1671 if( status != PSA_SUCCESS )
1672 return( status );
1673 /* From here on, tmp needs to be wiped. */
1674
1675 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1676 if( status != PSA_SUCCESS )
1677 goto exit;
1678
1679 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1680 if( status != PSA_SUCCESS )
1681 goto exit;
1682
1683 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1684 if( status != PSA_SUCCESS )
1685 goto exit;
1686
1687 status = psa_hash_finish( &hmac->hash_ctx, mac, mac_size, &hash_size );
1688
1689exit:
1690 mbedtls_zeroize( tmp, hash_size );
1691 return( status );
1692}
1693#endif /* MBEDTLS_MD_C */
1694
mohammad16036df908f2018-04-02 08:34:15 -07001695static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001696 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001697 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001698{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001699 if( ! operation->key_set )
1700 return( PSA_ERROR_BAD_STATE );
1701 if( operation->iv_required && ! operation->iv_set )
1702 return( PSA_ERROR_BAD_STATE );
1703
Gilles Peskine8c9def32018-02-08 10:02:12 +01001704 if( mac_size < operation->mac_size )
1705 return( PSA_ERROR_BUFFER_TOO_SMALL );
1706
Gilles Peskine8c9def32018-02-08 10:02:12 +01001707#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001708 if( operation->alg == PSA_ALG_CMAC )
1709 {
1710 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1711 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001712 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001713 else
1714#endif /* MBEDTLS_CMAC_C */
1715#if defined(MBEDTLS_MD_C)
1716 if( PSA_ALG_IS_HMAC( operation->alg ) )
1717 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001718 return( psa_hmac_finish_internal( &operation->ctx.hmac,
1719 mac, mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001720 }
1721 else
1722#endif /* MBEDTLS_MD_C */
1723 {
1724 /* This shouldn't happen if `operation` was initialized by
1725 * a setup function. */
1726 return( PSA_ERROR_BAD_STATE );
1727 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001728}
1729
Gilles Peskineacd4be32018-07-08 19:56:25 +02001730psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1731 uint8_t *mac,
1732 size_t mac_size,
1733 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001734{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001735 psa_status_t status;
1736
1737 /* Fill the output buffer with something that isn't a valid mac
1738 * (barring an attack on the mac and deliberately-crafted input),
1739 * in case the caller doesn't check the return status properly. */
1740 *mac_length = mac_size;
1741 /* If mac_size is 0 then mac may be NULL and then the
1742 * call to memset would have undefined behavior. */
1743 if( mac_size != 0 )
1744 memset( mac, '!', mac_size );
1745
Gilles Peskine89167cb2018-07-08 20:12:23 +02001746 if( ! operation->is_sign )
1747 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001748 status = PSA_ERROR_BAD_STATE;
1749 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001750 }
mohammad16036df908f2018-04-02 08:34:15 -07001751
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001752 status = psa_mac_finish_internal( operation, mac, mac_size );
1753
1754cleanup:
1755 if( status == PSA_SUCCESS )
1756 {
1757 status = psa_mac_abort( operation );
1758 if( status == PSA_SUCCESS )
1759 *mac_length = operation->mac_size;
1760 else
1761 memset( mac, '!', mac_size );
1762 }
1763 else
1764 psa_mac_abort( operation );
1765 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001766}
1767
Gilles Peskineacd4be32018-07-08 19:56:25 +02001768psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1769 const uint8_t *mac,
1770 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001771{
Gilles Peskine828ed142018-06-18 23:25:51 +02001772 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001773 psa_status_t status;
1774
Gilles Peskine89167cb2018-07-08 20:12:23 +02001775 if( operation->is_sign )
1776 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001777 status = PSA_ERROR_BAD_STATE;
1778 goto cleanup;
1779 }
1780 if( operation->mac_size != mac_length )
1781 {
1782 status = PSA_ERROR_INVALID_SIGNATURE;
1783 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001784 }
mohammad16036df908f2018-04-02 08:34:15 -07001785
1786 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001787 actual_mac, sizeof( actual_mac ) );
1788
1789 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1790 status = PSA_ERROR_INVALID_SIGNATURE;
1791
1792cleanup:
1793 if( status == PSA_SUCCESS )
1794 status = psa_mac_abort( operation );
1795 else
1796 psa_mac_abort( operation );
1797
1798 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001799}
1800
1801
Gilles Peskine20035e32018-02-03 22:44:14 +01001802
Gilles Peskine20035e32018-02-03 22:44:14 +01001803/****************************************************************/
1804/* Asymmetric cryptography */
1805/****************************************************************/
1806
Gilles Peskine2b450e32018-06-27 15:42:46 +02001807#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001808/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001809 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001810static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1811 size_t hash_length,
1812 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001813{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001814 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001815 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001816 *md_alg = mbedtls_md_get_type( md_info );
1817
1818 /* The Mbed TLS RSA module uses an unsigned int for hash length
1819 * parameters. Validate that it fits so that we don't risk an
1820 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001821#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001822 if( hash_length > UINT_MAX )
1823 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001824#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001825
1826#if defined(MBEDTLS_PKCS1_V15)
1827 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
1828 * must be correct. */
1829 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
1830 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001831 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001832 if( md_info == NULL )
1833 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001834 if( mbedtls_md_get_size( md_info ) != hash_length )
1835 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001836 }
1837#endif /* MBEDTLS_PKCS1_V15 */
1838
1839#if defined(MBEDTLS_PKCS1_V21)
1840 /* PSS requires a hash internally. */
1841 if( PSA_ALG_IS_RSA_PSS( alg ) )
1842 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001843 if( md_info == NULL )
1844 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001845 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001846#endif /* MBEDTLS_PKCS1_V21 */
1847
Gilles Peskine61b91d42018-06-08 16:09:36 +02001848 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001849}
1850
Gilles Peskine2b450e32018-06-27 15:42:46 +02001851static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1852 psa_algorithm_t alg,
1853 const uint8_t *hash,
1854 size_t hash_length,
1855 uint8_t *signature,
1856 size_t signature_size,
1857 size_t *signature_length )
1858{
1859 psa_status_t status;
1860 int ret;
1861 mbedtls_md_type_t md_alg;
1862
1863 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1864 if( status != PSA_SUCCESS )
1865 return( status );
1866
Gilles Peskine630a18a2018-06-29 17:49:35 +02001867 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001868 return( PSA_ERROR_BUFFER_TOO_SMALL );
1869
1870#if defined(MBEDTLS_PKCS1_V15)
1871 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1872 {
1873 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1874 MBEDTLS_MD_NONE );
1875 ret = mbedtls_rsa_pkcs1_sign( rsa,
1876 mbedtls_ctr_drbg_random,
1877 &global_data.ctr_drbg,
1878 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001879 md_alg,
1880 (unsigned int) hash_length,
1881 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001882 signature );
1883 }
1884 else
1885#endif /* MBEDTLS_PKCS1_V15 */
1886#if defined(MBEDTLS_PKCS1_V21)
1887 if( PSA_ALG_IS_RSA_PSS( alg ) )
1888 {
1889 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1890 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1891 mbedtls_ctr_drbg_random,
1892 &global_data.ctr_drbg,
1893 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001894 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001895 (unsigned int) hash_length,
1896 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001897 signature );
1898 }
1899 else
1900#endif /* MBEDTLS_PKCS1_V21 */
1901 {
1902 return( PSA_ERROR_INVALID_ARGUMENT );
1903 }
1904
1905 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02001906 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02001907 return( mbedtls_to_psa_error( ret ) );
1908}
1909
1910static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1911 psa_algorithm_t alg,
1912 const uint8_t *hash,
1913 size_t hash_length,
1914 const uint8_t *signature,
1915 size_t signature_length )
1916{
1917 psa_status_t status;
1918 int ret;
1919 mbedtls_md_type_t md_alg;
1920
1921 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1922 if( status != PSA_SUCCESS )
1923 return( status );
1924
Gilles Peskine630a18a2018-06-29 17:49:35 +02001925 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001926 return( PSA_ERROR_BUFFER_TOO_SMALL );
1927
1928#if defined(MBEDTLS_PKCS1_V15)
1929 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1930 {
1931 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1932 MBEDTLS_MD_NONE );
1933 ret = mbedtls_rsa_pkcs1_verify( rsa,
1934 mbedtls_ctr_drbg_random,
1935 &global_data.ctr_drbg,
1936 MBEDTLS_RSA_PUBLIC,
1937 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001938 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001939 hash,
1940 signature );
1941 }
1942 else
1943#endif /* MBEDTLS_PKCS1_V15 */
1944#if defined(MBEDTLS_PKCS1_V21)
1945 if( PSA_ALG_IS_RSA_PSS( alg ) )
1946 {
1947 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1948 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1949 mbedtls_ctr_drbg_random,
1950 &global_data.ctr_drbg,
1951 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001952 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001953 (unsigned int) hash_length,
1954 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001955 signature );
1956 }
1957 else
1958#endif /* MBEDTLS_PKCS1_V21 */
1959 {
1960 return( PSA_ERROR_INVALID_ARGUMENT );
1961 }
Gilles Peskineef12c632018-09-13 20:37:48 +02001962
1963 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
1964 * the rest of the signature is invalid". This has little use in
1965 * practice and PSA doesn't report this distinction. */
1966 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
1967 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02001968 return( mbedtls_to_psa_error( ret ) );
1969}
1970#endif /* MBEDTLS_RSA_C */
1971
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001972#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001973/* `ecp` cannot be const because `ecp->grp` needs to be non-const
1974 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
1975 * (even though these functions don't modify it). */
1976static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
1977 psa_algorithm_t alg,
1978 const uint8_t *hash,
1979 size_t hash_length,
1980 uint8_t *signature,
1981 size_t signature_size,
1982 size_t *signature_length )
1983{
1984 int ret;
1985 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001986 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001987 mbedtls_mpi_init( &r );
1988 mbedtls_mpi_init( &s );
1989
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001990 if( signature_size < 2 * curve_bytes )
1991 {
1992 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
1993 goto cleanup;
1994 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001995
1996 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
1997 {
1998 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
1999 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2000 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2001 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2002 hash, hash_length,
2003 md_alg ) );
2004 }
2005 else
2006 {
2007 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2008 hash, hash_length,
2009 mbedtls_ctr_drbg_random,
2010 &global_data.ctr_drbg ) );
2011 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002012
2013 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2014 signature,
2015 curve_bytes ) );
2016 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2017 signature + curve_bytes,
2018 curve_bytes ) );
2019
2020cleanup:
2021 mbedtls_mpi_free( &r );
2022 mbedtls_mpi_free( &s );
2023 if( ret == 0 )
2024 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002025 return( mbedtls_to_psa_error( ret ) );
2026}
2027
2028static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2029 const uint8_t *hash,
2030 size_t hash_length,
2031 const uint8_t *signature,
2032 size_t signature_length )
2033{
2034 int ret;
2035 mbedtls_mpi r, s;
2036 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2037 mbedtls_mpi_init( &r );
2038 mbedtls_mpi_init( &s );
2039
2040 if( signature_length != 2 * curve_bytes )
2041 return( PSA_ERROR_INVALID_SIGNATURE );
2042
2043 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2044 signature,
2045 curve_bytes ) );
2046 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2047 signature + curve_bytes,
2048 curve_bytes ) );
2049
2050 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2051 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002052
2053cleanup:
2054 mbedtls_mpi_free( &r );
2055 mbedtls_mpi_free( &s );
2056 return( mbedtls_to_psa_error( ret ) );
2057}
2058#endif /* MBEDTLS_ECDSA_C */
2059
Gilles Peskine61b91d42018-06-08 16:09:36 +02002060psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2061 psa_algorithm_t alg,
2062 const uint8_t *hash,
2063 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002064 uint8_t *signature,
2065 size_t signature_size,
2066 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002067{
2068 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002069 psa_status_t status;
2070
2071 *signature_length = signature_size;
2072
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002073 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2074 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002075 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002076 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002077 {
2078 status = PSA_ERROR_INVALID_ARGUMENT;
2079 goto exit;
2080 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002081
Gilles Peskine20035e32018-02-03 22:44:14 +01002082#if defined(MBEDTLS_RSA_C)
2083 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2084 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002085 status = psa_rsa_sign( slot->data.rsa,
2086 alg,
2087 hash, hash_length,
2088 signature, signature_size,
2089 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002090 }
2091 else
2092#endif /* defined(MBEDTLS_RSA_C) */
2093#if defined(MBEDTLS_ECP_C)
2094 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2095 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002096#if defined(MBEDTLS_ECDSA_C)
2097 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002098 status = psa_ecdsa_sign( slot->data.ecp,
2099 alg,
2100 hash, hash_length,
2101 signature, signature_size,
2102 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002103 else
2104#endif /* defined(MBEDTLS_ECDSA_C) */
2105 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002106 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002107 }
itayzafrir5c753392018-05-08 11:18:38 +03002108 }
2109 else
2110#endif /* defined(MBEDTLS_ECP_C) */
2111 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002112 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002113 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002114
2115exit:
2116 /* Fill the unused part of the output buffer (the whole buffer on error,
2117 * the trailing part on success) with something that isn't a valid mac
2118 * (barring an attack on the mac and deliberately-crafted input),
2119 * in case the caller doesn't check the return status properly. */
2120 if( status == PSA_SUCCESS )
2121 memset( signature + *signature_length, '!',
2122 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002123 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002124 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002125 /* If signature_size is 0 then we have nothing to do. We must not call
2126 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002127 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002128}
2129
2130psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2131 psa_algorithm_t alg,
2132 const uint8_t *hash,
2133 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002134 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002135 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002136{
2137 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002138 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002139
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002140 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2141 if( status != PSA_SUCCESS )
2142 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002143
Gilles Peskine61b91d42018-06-08 16:09:36 +02002144#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002145 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002146 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002147 return( psa_rsa_verify( slot->data.rsa,
2148 alg,
2149 hash, hash_length,
2150 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002151 }
2152 else
2153#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002154#if defined(MBEDTLS_ECP_C)
2155 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2156 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002157#if defined(MBEDTLS_ECDSA_C)
2158 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002159 return( psa_ecdsa_verify( slot->data.ecp,
2160 hash, hash_length,
2161 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002162 else
2163#endif /* defined(MBEDTLS_ECDSA_C) */
2164 {
2165 return( PSA_ERROR_INVALID_ARGUMENT );
2166 }
itayzafrir5c753392018-05-08 11:18:38 +03002167 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002168 else
2169#endif /* defined(MBEDTLS_ECP_C) */
2170 {
2171 return( PSA_ERROR_NOT_SUPPORTED );
2172 }
2173}
2174
Gilles Peskine072ac562018-06-30 00:21:29 +02002175#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2176static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2177 mbedtls_rsa_context *rsa )
2178{
2179 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2180 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2181 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2182 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2183}
2184#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2185
Gilles Peskine61b91d42018-06-08 16:09:36 +02002186psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2187 psa_algorithm_t alg,
2188 const uint8_t *input,
2189 size_t input_length,
2190 const uint8_t *salt,
2191 size_t salt_length,
2192 uint8_t *output,
2193 size_t output_size,
2194 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002195{
2196 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002197 psa_status_t status;
2198
Darryl Green5cc689a2018-07-24 15:34:10 +01002199 (void) input;
2200 (void) input_length;
2201 (void) salt;
2202 (void) output;
2203 (void) output_size;
2204
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002205 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002206
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002207 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2208 return( PSA_ERROR_INVALID_ARGUMENT );
2209
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002210 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2211 if( status != PSA_SUCCESS )
2212 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002213 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2214 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002215 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002216
2217#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002218 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002219 {
2220 mbedtls_rsa_context *rsa = slot->data.rsa;
2221 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002222 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002223 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002224#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002225 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002226 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002227 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2228 mbedtls_ctr_drbg_random,
2229 &global_data.ctr_drbg,
2230 MBEDTLS_RSA_PUBLIC,
2231 input_length,
2232 input,
2233 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002234 }
2235 else
2236#endif /* MBEDTLS_PKCS1_V15 */
2237#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002238 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002239 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002240 psa_rsa_oaep_set_padding_mode( alg, rsa );
2241 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2242 mbedtls_ctr_drbg_random,
2243 &global_data.ctr_drbg,
2244 MBEDTLS_RSA_PUBLIC,
2245 salt, salt_length,
2246 input_length,
2247 input,
2248 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002249 }
2250 else
2251#endif /* MBEDTLS_PKCS1_V21 */
2252 {
2253 return( PSA_ERROR_INVALID_ARGUMENT );
2254 }
2255 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002256 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002257 return( mbedtls_to_psa_error( ret ) );
2258 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002259 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002260#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002261 {
2262 return( PSA_ERROR_NOT_SUPPORTED );
2263 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002264}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002265
Gilles Peskine61b91d42018-06-08 16:09:36 +02002266psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2267 psa_algorithm_t alg,
2268 const uint8_t *input,
2269 size_t input_length,
2270 const uint8_t *salt,
2271 size_t salt_length,
2272 uint8_t *output,
2273 size_t output_size,
2274 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002275{
2276 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002277 psa_status_t status;
2278
Darryl Green5cc689a2018-07-24 15:34:10 +01002279 (void) input;
2280 (void) input_length;
2281 (void) salt;
2282 (void) output;
2283 (void) output_size;
2284
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002285 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002286
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002287 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2288 return( PSA_ERROR_INVALID_ARGUMENT );
2289
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002290 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2291 if( status != PSA_SUCCESS )
2292 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002293 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2294 return( PSA_ERROR_INVALID_ARGUMENT );
2295
2296#if defined(MBEDTLS_RSA_C)
2297 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2298 {
2299 mbedtls_rsa_context *rsa = slot->data.rsa;
2300 int ret;
2301
Gilles Peskine630a18a2018-06-29 17:49:35 +02002302 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002303 return( PSA_ERROR_INVALID_ARGUMENT );
2304
2305#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002306 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002307 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002308 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2309 mbedtls_ctr_drbg_random,
2310 &global_data.ctr_drbg,
2311 MBEDTLS_RSA_PRIVATE,
2312 output_length,
2313 input,
2314 output,
2315 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002316 }
2317 else
2318#endif /* MBEDTLS_PKCS1_V15 */
2319#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002320 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002321 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002322 psa_rsa_oaep_set_padding_mode( alg, rsa );
2323 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2324 mbedtls_ctr_drbg_random,
2325 &global_data.ctr_drbg,
2326 MBEDTLS_RSA_PRIVATE,
2327 salt, salt_length,
2328 output_length,
2329 input,
2330 output,
2331 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002332 }
2333 else
2334#endif /* MBEDTLS_PKCS1_V21 */
2335 {
2336 return( PSA_ERROR_INVALID_ARGUMENT );
2337 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002338
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002339 return( mbedtls_to_psa_error( ret ) );
2340 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002341 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002342#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002343 {
2344 return( PSA_ERROR_NOT_SUPPORTED );
2345 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002346}
Gilles Peskine20035e32018-02-03 22:44:14 +01002347
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002348
2349
mohammad1603503973b2018-03-12 15:59:30 +02002350/****************************************************************/
2351/* Symmetric cryptography */
2352/****************************************************************/
2353
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002354/* Initialize the cipher operation structure. Once this function has been
2355 * called, psa_cipher_abort can run and will do the right thing. */
2356static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2357 psa_algorithm_t alg )
2358{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002359 if( ! PSA_ALG_IS_CIPHER( alg ) )
2360 {
2361 memset( operation, 0, sizeof( *operation ) );
2362 return( PSA_ERROR_INVALID_ARGUMENT );
2363 }
2364
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002365 operation->alg = alg;
2366 operation->key_set = 0;
2367 operation->iv_set = 0;
2368 operation->iv_required = 1;
2369 operation->iv_size = 0;
2370 operation->block_size = 0;
2371 mbedtls_cipher_init( &operation->ctx.cipher );
2372 return( PSA_SUCCESS );
2373}
2374
Gilles Peskinee553c652018-06-04 16:22:46 +02002375static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2376 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002377 psa_algorithm_t alg,
2378 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002379{
2380 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2381 psa_status_t status;
2382 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002383 size_t key_bits;
2384 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002385 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2386 PSA_KEY_USAGE_ENCRYPT :
2387 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002388
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002389 status = psa_cipher_init( operation, alg );
2390 if( status != PSA_SUCCESS )
2391 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002392
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002393 status = psa_get_key_from_slot( key, &slot, usage, alg);
2394 if( status != PSA_SUCCESS )
2395 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002396 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002397
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002398 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002399 if( cipher_info == NULL )
2400 return( PSA_ERROR_NOT_SUPPORTED );
2401
mohammad1603503973b2018-03-12 15:59:30 +02002402 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002403 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002404 {
2405 psa_cipher_abort( operation );
2406 return( mbedtls_to_psa_error( ret ) );
2407 }
2408
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002409#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002410 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002411 {
2412 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2413 unsigned char keys[24];
2414 memcpy( keys, slot->data.raw.data, 16 );
2415 memcpy( keys + 16, slot->data.raw.data, 8 );
2416 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2417 keys,
2418 192, cipher_operation );
2419 }
2420 else
2421#endif
2422 {
2423 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2424 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002425 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002426 }
Moran Peker41deec42018-04-04 15:43:05 +03002427 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002428 {
2429 psa_cipher_abort( operation );
2430 return( mbedtls_to_psa_error( ret ) );
2431 }
2432
mohammad16038481e742018-03-18 13:57:31 +02002433#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002434 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002435 {
Gilles Peskine53514202018-06-06 15:11:46 +02002436 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2437 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002438
Moran Pekera28258c2018-05-29 16:25:04 +03002439 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002440 {
2441 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2442 mode = MBEDTLS_PADDING_PKCS7;
2443 break;
2444 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2445 mode = MBEDTLS_PADDING_NONE;
2446 break;
2447 default:
Moran Pekerae382792018-05-31 14:06:17 +03002448 psa_cipher_abort( operation );
2449 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002450 }
2451 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002452 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002453 {
2454 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002455 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002456 }
mohammad16038481e742018-03-18 13:57:31 +02002457 }
2458#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2459
mohammad1603503973b2018-03-12 15:59:30 +02002460 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002461 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002462 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) :
Gilles Peskine7e928852018-06-04 16:23:10 +02002463 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002464 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002465 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002466 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002467 }
mohammad1603503973b2018-03-12 15:59:30 +02002468
Moran Peker395db872018-05-31 14:07:14 +03002469 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002470}
2471
Gilles Peskinefe119512018-07-08 21:39:34 +02002472psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2473 psa_key_slot_t key,
2474 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002475{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002476 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002477}
2478
Gilles Peskinefe119512018-07-08 21:39:34 +02002479psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2480 psa_key_slot_t key,
2481 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002482{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002483 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002484}
2485
Gilles Peskinefe119512018-07-08 21:39:34 +02002486psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2487 unsigned char *iv,
2488 size_t iv_size,
2489 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002490{
itayzafrir534bd7c2018-08-02 13:56:32 +03002491 psa_status_t status;
2492 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002493 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002494 {
2495 status = PSA_ERROR_BAD_STATE;
2496 goto exit;
2497 }
Moran Peker41deec42018-04-04 15:43:05 +03002498 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002499 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002500 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002501 goto exit;
2502 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002503 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2504 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002505 if( ret != 0 )
2506 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002507 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002508 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002509 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002510
mohammad16038481e742018-03-18 13:57:31 +02002511 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002512 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002513
Moran Peker395db872018-05-31 14:07:14 +03002514exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002515 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002516 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002517 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002518}
2519
Gilles Peskinefe119512018-07-08 21:39:34 +02002520psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2521 const unsigned char *iv,
2522 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002523{
itayzafrir534bd7c2018-08-02 13:56:32 +03002524 psa_status_t status;
2525 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002526 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002527 {
2528 status = PSA_ERROR_BAD_STATE;
2529 goto exit;
2530 }
Moran Pekera28258c2018-05-29 16:25:04 +03002531 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002532 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002533 status = PSA_ERROR_INVALID_ARGUMENT;
2534 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002535 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002536 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2537 status = mbedtls_to_psa_error( ret );
2538exit:
2539 if( status == PSA_SUCCESS )
2540 operation->iv_set = 1;
2541 else
mohammad1603503973b2018-03-12 15:59:30 +02002542 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002543 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002544}
2545
Gilles Peskinee553c652018-06-04 16:22:46 +02002546psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2547 const uint8_t *input,
2548 size_t input_length,
2549 unsigned char *output,
2550 size_t output_size,
2551 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002552{
itayzafrir534bd7c2018-08-02 13:56:32 +03002553 psa_status_t status;
2554 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002555 size_t expected_output_size;
2556 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2557 {
2558 /* Take the unprocessed partial block left over from previous
2559 * update calls, if any, plus the input to this call. Remove
2560 * the last partial block, if any. You get the data that will be
2561 * output in this call. */
2562 expected_output_size =
2563 ( operation->ctx.cipher.unprocessed_len + input_length )
2564 / operation->block_size * operation->block_size;
2565 }
2566 else
2567 {
2568 expected_output_size = input_length;
2569 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002570
Gilles Peskine89d789c2018-06-04 17:17:16 +02002571 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002572 {
2573 status = PSA_ERROR_BUFFER_TOO_SMALL;
2574 goto exit;
2575 }
mohammad160382759612018-03-12 18:16:40 +02002576
mohammad1603503973b2018-03-12 15:59:30 +02002577 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002578 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002579 status = mbedtls_to_psa_error( ret );
2580exit:
2581 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002582 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002583 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002584}
2585
Gilles Peskinee553c652018-06-04 16:22:46 +02002586psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2587 uint8_t *output,
2588 size_t output_size,
2589 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002590{
Janos Follath315b51c2018-07-09 16:04:51 +01002591 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2592 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002593 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002594
mohammad1603503973b2018-03-12 15:59:30 +02002595 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002596 {
Janos Follath315b51c2018-07-09 16:04:51 +01002597 status = PSA_ERROR_BAD_STATE;
2598 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002599 }
2600 if( operation->iv_required && ! operation->iv_set )
2601 {
Janos Follath315b51c2018-07-09 16:04:51 +01002602 status = PSA_ERROR_BAD_STATE;
2603 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002604 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002605 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2606 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002607 {
Gilles Peskine53514202018-06-06 15:11:46 +02002608 psa_algorithm_t padding_mode =
2609 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002610 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002611 {
Janos Follath315b51c2018-07-09 16:04:51 +01002612 status = PSA_ERROR_TAMPERING_DETECTED;
2613 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002614 }
Gilles Peskine53514202018-06-06 15:11:46 +02002615 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002616 {
2617 if( operation->ctx.cipher.unprocessed_len != 0 )
2618 {
Janos Follath315b51c2018-07-09 16:04:51 +01002619 status = PSA_ERROR_INVALID_ARGUMENT;
2620 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002621 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002622 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002623 }
2624
Janos Follath315b51c2018-07-09 16:04:51 +01002625 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2626 temp_output_buffer,
2627 output_length );
2628 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002629 {
Janos Follath315b51c2018-07-09 16:04:51 +01002630 status = mbedtls_to_psa_error( cipher_ret );
2631 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002632 }
Janos Follath315b51c2018-07-09 16:04:51 +01002633
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002634 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002635 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002636 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002637 memcpy( output, temp_output_buffer, *output_length );
2638 else
2639 {
Janos Follath315b51c2018-07-09 16:04:51 +01002640 status = PSA_ERROR_BUFFER_TOO_SMALL;
2641 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002642 }
mohammad1603503973b2018-03-12 15:59:30 +02002643
Janos Follath279ab8e2018-07-09 16:13:21 +01002644 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002645 status = psa_cipher_abort( operation );
2646
2647 return( status );
2648
2649error:
2650
2651 *output_length = 0;
2652
Janos Follath279ab8e2018-07-09 16:13:21 +01002653 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002654 (void) psa_cipher_abort( operation );
2655
2656 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002657}
2658
Gilles Peskinee553c652018-06-04 16:22:46 +02002659psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2660{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002661 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002662 {
2663 /* The object has (apparently) been initialized but it is not
2664 * in use. It's ok to call abort on such an object, and there's
2665 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002666 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002667 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002668
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002669 /* Sanity check (shouldn't happen: operation->alg should
2670 * always have been initialized to a valid value). */
2671 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2672 return( PSA_ERROR_BAD_STATE );
2673
mohammad1603503973b2018-03-12 15:59:30 +02002674 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002675
Moran Peker41deec42018-04-04 15:43:05 +03002676 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002677 operation->key_set = 0;
2678 operation->iv_set = 0;
2679 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002680 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002681 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002682
Moran Peker395db872018-05-31 14:07:14 +03002683 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002684}
2685
Gilles Peskinea0655c32018-04-30 17:06:50 +02002686
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002687
mohammad16038cc1cee2018-03-28 01:21:33 +03002688/****************************************************************/
2689/* Key Policy */
2690/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002691
mohammad160327010052018-07-03 13:16:15 +03002692#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002693void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002694{
Gilles Peskine803ce742018-06-18 16:07:14 +02002695 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002696}
2697
Gilles Peskine2d277862018-06-18 15:41:12 +02002698void psa_key_policy_set_usage( psa_key_policy_t *policy,
2699 psa_key_usage_t usage,
2700 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002701{
mohammad16034eed7572018-03-28 05:14:59 -07002702 policy->usage = usage;
2703 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002704}
2705
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002706psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002707{
mohammad16036df908f2018-04-02 08:34:15 -07002708 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002709}
2710
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002711psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002712{
mohammad16036df908f2018-04-02 08:34:15 -07002713 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002714}
mohammad160327010052018-07-03 13:16:15 +03002715#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002716
Gilles Peskine2d277862018-06-18 15:41:12 +02002717psa_status_t psa_set_key_policy( psa_key_slot_t key,
2718 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002719{
2720 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002721 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002722
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002723 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002724 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002725
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002726 status = psa_get_empty_key_slot( key, &slot );
2727 if( status != PSA_SUCCESS )
2728 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002729
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002730 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2731 PSA_KEY_USAGE_ENCRYPT |
2732 PSA_KEY_USAGE_DECRYPT |
2733 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002734 PSA_KEY_USAGE_VERIFY |
2735 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002736 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002737
mohammad16036df908f2018-04-02 08:34:15 -07002738 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002739
2740 return( PSA_SUCCESS );
2741}
2742
Gilles Peskine2d277862018-06-18 15:41:12 +02002743psa_status_t psa_get_key_policy( psa_key_slot_t key,
2744 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002745{
2746 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002747 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002748
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002749 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002750 return( PSA_ERROR_INVALID_ARGUMENT );
2751
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002752 status = psa_get_key_slot( key, &slot );
2753 if( status != PSA_SUCCESS )
2754 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002755
mohammad16036df908f2018-04-02 08:34:15 -07002756 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002757
2758 return( PSA_SUCCESS );
2759}
Gilles Peskine20035e32018-02-03 22:44:14 +01002760
Gilles Peskinea0655c32018-04-30 17:06:50 +02002761
2762
mohammad1603804cd712018-03-20 22:44:08 +02002763/****************************************************************/
2764/* Key Lifetime */
2765/****************************************************************/
2766
Gilles Peskine2d277862018-06-18 15:41:12 +02002767psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2768 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002769{
2770 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002771 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002772
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002773 status = psa_get_key_slot( key, &slot );
2774 if( status != PSA_SUCCESS )
2775 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002776
mohammad1603804cd712018-03-20 22:44:08 +02002777 *lifetime = slot->lifetime;
2778
2779 return( PSA_SUCCESS );
2780}
2781
Gilles Peskine2d277862018-06-18 15:41:12 +02002782psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002783 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002784{
2785 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002786 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002787
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002788 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2789 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002790 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2791 return( PSA_ERROR_INVALID_ARGUMENT );
2792
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002793 status = psa_get_empty_key_slot( key, &slot );
2794 if( status != PSA_SUCCESS )
2795 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002796
Moran Pekerd7326592018-05-29 16:56:39 +03002797 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002798 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002799
mohammad1603060ad8a2018-03-20 14:28:38 -07002800 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002801
2802 return( PSA_SUCCESS );
2803}
2804
Gilles Peskine20035e32018-02-03 22:44:14 +01002805
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002806
mohammad16035955c982018-04-26 00:53:03 +03002807/****************************************************************/
2808/* AEAD */
2809/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002810
mohammad16035955c982018-04-26 00:53:03 +03002811psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2812 psa_algorithm_t alg,
2813 const uint8_t *nonce,
2814 size_t nonce_length,
2815 const uint8_t *additional_data,
2816 size_t additional_data_length,
2817 const uint8_t *plaintext,
2818 size_t plaintext_length,
2819 uint8_t *ciphertext,
2820 size_t ciphertext_size,
2821 size_t *ciphertext_length )
2822{
2823 int ret;
2824 psa_status_t status;
2825 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002826 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002827 uint8_t *tag;
2828 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002829 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002830 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002831
mohammad1603f08a5502018-06-03 15:05:47 +03002832 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002833
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002834 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2835 if( status != PSA_SUCCESS )
2836 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002837 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002838
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002839 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002840 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002841 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002842 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002843
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002844 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002845 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002846 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002847
mohammad16035955c982018-04-26 00:53:03 +03002848 if( alg == PSA_ALG_GCM )
2849 {
2850 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002851 tag_length = 16;
2852
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002853 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002854 return( PSA_ERROR_INVALID_ARGUMENT );
2855
mohammad160315223a82018-06-03 17:19:55 +03002856 //make sure we have place to hold the tag in the ciphertext buffer
2857 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002858 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002859
2860 //update the tag pointer to point to the end of the ciphertext_length
2861 tag = ciphertext + plaintext_length;
2862
mohammad16035955c982018-04-26 00:53:03 +03002863 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002864 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002865 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002866 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002867 if( ret != 0 )
2868 {
2869 mbedtls_gcm_free( &gcm );
2870 return( mbedtls_to_psa_error( ret ) );
2871 }
2872 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002873 plaintext_length, nonce,
2874 nonce_length, additional_data,
2875 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002876 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002877 mbedtls_gcm_free( &gcm );
2878 }
2879 else if( alg == PSA_ALG_CCM )
2880 {
2881 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002882 tag_length = 16;
2883
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002884 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002885 return( PSA_ERROR_INVALID_ARGUMENT );
2886
mohammad160347ddf3d2018-04-26 01:11:21 +03002887 if( nonce_length < 7 || nonce_length > 13 )
2888 return( PSA_ERROR_INVALID_ARGUMENT );
2889
mohammad160315223a82018-06-03 17:19:55 +03002890 //make sure we have place to hold the tag in the ciphertext buffer
2891 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002892 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002893
2894 //update the tag pointer to point to the end of the ciphertext_length
2895 tag = ciphertext + plaintext_length;
2896
mohammad16035955c982018-04-26 00:53:03 +03002897 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002898 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002899 slot->data.raw.data,
2900 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002901 if( ret != 0 )
2902 {
2903 mbedtls_ccm_free( &ccm );
2904 return( mbedtls_to_psa_error( ret ) );
2905 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002906 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002907 nonce, nonce_length,
2908 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002909 additional_data_length,
2910 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002911 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002912 mbedtls_ccm_free( &ccm );
2913 }
mohammad16035c8845f2018-05-09 05:40:09 -07002914 else
2915 {
mohammad1603554faad2018-06-03 15:07:38 +03002916 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002917 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002918
mohammad160315223a82018-06-03 17:19:55 +03002919 if( ret != 0 )
2920 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002921 /* If ciphertext_size is 0 then ciphertext may be NULL and then the
2922 * call to memset would have undefined behavior. */
2923 if( ciphertext_size != 0 )
2924 memset( ciphertext, 0, ciphertext_size );
mohammad160315223a82018-06-03 17:19:55 +03002925 return( mbedtls_to_psa_error( ret ) );
2926 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002927
mohammad160315223a82018-06-03 17:19:55 +03002928 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002929 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002930}
2931
Gilles Peskineee652a32018-06-01 19:23:52 +02002932/* Locate the tag in a ciphertext buffer containing the encrypted data
2933 * followed by the tag. Return the length of the part preceding the tag in
2934 * *plaintext_length. This is the size of the plaintext in modes where
2935 * the encrypted data has the same size as the plaintext, such as
2936 * CCM and GCM. */
2937static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2938 const uint8_t *ciphertext,
2939 size_t ciphertext_length,
2940 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002941 const uint8_t **p_tag )
2942{
2943 size_t payload_length;
2944 if( tag_length > ciphertext_length )
2945 return( PSA_ERROR_INVALID_ARGUMENT );
2946 payload_length = ciphertext_length - tag_length;
2947 if( payload_length > plaintext_size )
2948 return( PSA_ERROR_BUFFER_TOO_SMALL );
2949 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002950 return( PSA_SUCCESS );
2951}
2952
mohammad16035955c982018-04-26 00:53:03 +03002953psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2954 psa_algorithm_t alg,
2955 const uint8_t *nonce,
2956 size_t nonce_length,
2957 const uint8_t *additional_data,
2958 size_t additional_data_length,
2959 const uint8_t *ciphertext,
2960 size_t ciphertext_length,
2961 uint8_t *plaintext,
2962 size_t plaintext_size,
2963 size_t *plaintext_length )
2964{
2965 int ret;
2966 psa_status_t status;
2967 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002968 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002969 const uint8_t *tag;
2970 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002971 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002972 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002973
Gilles Peskineee652a32018-06-01 19:23:52 +02002974 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002975
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002976 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2977 if( status != PSA_SUCCESS )
2978 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002979 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002980
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002981 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002982 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002983 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002984 return( PSA_ERROR_NOT_SUPPORTED );
2985
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002986 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002987 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002988 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002989
mohammad16035955c982018-04-26 00:53:03 +03002990 if( alg == PSA_ALG_GCM )
2991 {
2992 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002993
Gilles Peskineee652a32018-06-01 19:23:52 +02002994 tag_length = 16;
2995 status = psa_aead_unpadded_locate_tag( tag_length,
2996 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002997 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002998 if( status != PSA_SUCCESS )
2999 return( status );
3000
mohammad16035955c982018-04-26 00:53:03 +03003001 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02003002 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003003 slot->data.raw.data,
3004 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03003005 if( ret != 0 )
3006 {
3007 mbedtls_gcm_free( &gcm );
3008 return( mbedtls_to_psa_error( ret ) );
3009 }
mohammad16035955c982018-04-26 00:53:03 +03003010
Gilles Peskineee652a32018-06-01 19:23:52 +02003011 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03003012 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003013 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03003014 additional_data,
3015 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003016 tag, tag_length,
3017 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03003018 mbedtls_gcm_free( &gcm );
3019 }
3020 else if( alg == PSA_ALG_CCM )
3021 {
3022 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02003023
mohammad160347ddf3d2018-04-26 01:11:21 +03003024 if( nonce_length < 7 || nonce_length > 13 )
3025 return( PSA_ERROR_INVALID_ARGUMENT );
3026
mohammad16039375f842018-06-03 14:28:24 +03003027 tag_length = 16;
3028 status = psa_aead_unpadded_locate_tag( tag_length,
3029 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003030 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003031 if( status != PSA_SUCCESS )
3032 return( status );
3033
mohammad16035955c982018-04-26 00:53:03 +03003034 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02003035 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003036 slot->data.raw.data,
3037 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03003038 if( ret != 0 )
3039 {
3040 mbedtls_ccm_free( &ccm );
3041 return( mbedtls_to_psa_error( ret ) );
3042 }
mohammad160360a64d02018-06-03 17:20:42 +03003043 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003044 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003045 additional_data,
3046 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003047 ciphertext, plaintext,
3048 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03003049 mbedtls_ccm_free( &ccm );
3050 }
mohammad160339574652018-06-01 04:39:53 -07003051 else
3052 {
mohammad1603554faad2018-06-03 15:07:38 +03003053 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003054 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003055
Gilles Peskineee652a32018-06-01 19:23:52 +02003056 if( ret != 0 )
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003057 {
3058 /* If plaintext_size is 0 then plaintext may be NULL and then the
3059 * call to memset has undefined behavior. */
3060 if( plaintext_size != 0 )
3061 memset( plaintext, 0, plaintext_size );
3062 }
mohammad160360a64d02018-06-03 17:20:42 +03003063 else
3064 *plaintext_length = ciphertext_length - tag_length;
3065
Gilles Peskineee652a32018-06-01 19:23:52 +02003066 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03003067}
3068
Gilles Peskinea0655c32018-04-30 17:06:50 +02003069
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003070
Gilles Peskine20035e32018-02-03 22:44:14 +01003071/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003072/* Generators */
3073/****************************************************************/
3074
3075psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3076{
3077 psa_status_t status = PSA_SUCCESS;
3078 if( generator->alg == 0 )
3079 {
3080 /* The object has (apparently) been initialized but it is not
3081 * in use. It's ok to call abort on such an object, and there's
3082 * nothing to do. */
3083 }
3084 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003085#if defined(MBEDTLS_MD_C)
3086 if( PSA_ALG_IS_HKDF( generator->alg ) )
3087 {
3088 mbedtls_free( generator->ctx.hkdf.info );
3089 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3090 }
3091 else
3092#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003093 {
3094 status = PSA_ERROR_BAD_STATE;
3095 }
3096 memset( generator, 0, sizeof( *generator ) );
3097 return( status );
3098}
3099
3100
3101psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3102 size_t *capacity)
3103{
3104 *capacity = generator->capacity;
3105 return( PSA_SUCCESS );
3106}
3107
Gilles Peskinebef7f142018-07-12 17:22:21 +02003108#if defined(MBEDTLS_MD_C)
3109/* Read some bytes from an HKDF-based generator. This performs a chunk
3110 * of the expand phase of the HKDF algorithm. */
3111static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3112 psa_algorithm_t hash_alg,
3113 uint8_t *output,
3114 size_t output_length )
3115{
3116 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3117 psa_status_t status;
3118
3119 while( output_length != 0 )
3120 {
3121 /* Copy what remains of the current block */
3122 uint8_t n = hash_length - hkdf->offset_in_block;
3123 if( n > output_length )
3124 n = (uint8_t) output_length;
3125 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3126 output += n;
3127 output_length -= n;
3128 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003129 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003130 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003131 /* We can't be wanting more output after block 0xff, otherwise
3132 * the capacity check in psa_generator_read() would have
3133 * prevented this call. It could happen only if the generator
3134 * object was corrupted or if this function is called directly
3135 * inside the library. */
3136 if( hkdf->block_number == 0xff )
3137 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003138
3139 /* We need a new block */
3140 ++hkdf->block_number;
3141 hkdf->offset_in_block = 0;
3142 status = psa_hmac_setup_internal( &hkdf->hmac,
3143 hkdf->prk, hash_length,
3144 hash_alg );
3145 if( status != PSA_SUCCESS )
3146 return( status );
3147 if( hkdf->block_number != 1 )
3148 {
3149 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3150 hkdf->output_block,
3151 hash_length );
3152 if( status != PSA_SUCCESS )
3153 return( status );
3154 }
3155 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3156 hkdf->info,
3157 hkdf->info_length );
3158 if( status != PSA_SUCCESS )
3159 return( status );
3160 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3161 &hkdf->block_number, 1 );
3162 if( status != PSA_SUCCESS )
3163 return( status );
3164 status = psa_hmac_finish_internal( &hkdf->hmac,
3165 hkdf->output_block,
3166 sizeof( hkdf->output_block ) );
3167 if( status != PSA_SUCCESS )
3168 return( status );
3169 }
3170
3171 return( PSA_SUCCESS );
3172}
3173#endif /* MBEDTLS_MD_C */
3174
Gilles Peskineeab56e42018-07-12 17:12:33 +02003175psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3176 uint8_t *output,
3177 size_t output_length )
3178{
3179 psa_status_t status;
3180
3181 if( output_length > generator->capacity )
3182 {
3183 generator->capacity = 0;
3184 /* Go through the error path to wipe all confidential data now
3185 * that the generator object is useless. */
3186 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3187 goto exit;
3188 }
3189 if( output_length == 0 &&
3190 generator->capacity == 0 && generator->alg == 0 )
3191 {
3192 /* Edge case: this is a blank or finished generator, and 0
3193 * bytes were requested. The right error in this case could
3194 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3195 * INSUFFICIENT_CAPACITY, which is right for a finished
3196 * generator, for consistency with the case when
3197 * output_length > 0. */
3198 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3199 }
3200 generator->capacity -= output_length;
3201
Gilles Peskinebef7f142018-07-12 17:22:21 +02003202#if defined(MBEDTLS_MD_C)
3203 if( PSA_ALG_IS_HKDF( generator->alg ) )
3204 {
3205 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3206 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3207 output, output_length );
3208 }
3209 else
3210#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003211 {
3212 return( PSA_ERROR_BAD_STATE );
3213 }
3214
3215exit:
3216 if( status != PSA_SUCCESS )
3217 {
3218 psa_generator_abort( generator );
3219 memset( output, '!', output_length );
3220 }
3221 return( status );
3222}
3223
Gilles Peskine08542d82018-07-19 17:05:42 +02003224#if defined(MBEDTLS_DES_C)
3225static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3226{
3227 if( data_size >= 8 )
3228 mbedtls_des_key_set_parity( data );
3229 if( data_size >= 16 )
3230 mbedtls_des_key_set_parity( data + 8 );
3231 if( data_size >= 24 )
3232 mbedtls_des_key_set_parity( data + 16 );
3233}
3234#endif /* MBEDTLS_DES_C */
3235
Gilles Peskineeab56e42018-07-12 17:12:33 +02003236psa_status_t psa_generator_import_key( psa_key_slot_t key,
3237 psa_key_type_t type,
3238 size_t bits,
3239 psa_crypto_generator_t *generator )
3240{
3241 uint8_t *data = NULL;
3242 size_t bytes = PSA_BITS_TO_BYTES( bits );
3243 psa_status_t status;
3244
3245 if( ! key_type_is_raw_bytes( type ) )
3246 return( PSA_ERROR_INVALID_ARGUMENT );
3247 if( bits % 8 != 0 )
3248 return( PSA_ERROR_INVALID_ARGUMENT );
3249 data = mbedtls_calloc( 1, bytes );
3250 if( data == NULL )
3251 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3252
3253 status = psa_generator_read( generator, data, bytes );
3254 if( status != PSA_SUCCESS )
3255 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003256#if defined(MBEDTLS_DES_C)
3257 if( type == PSA_KEY_TYPE_DES )
3258 psa_des_set_key_parity( data, bytes );
3259#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003260 status = psa_import_key( key, type, data, bytes );
3261
3262exit:
3263 mbedtls_free( data );
3264 return( status );
3265}
3266
3267
3268
3269/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003270/* Key derivation */
3271/****************************************************************/
3272
Gilles Peskinebef7f142018-07-12 17:22:21 +02003273/* Set up an HKDF-based generator. This is exactly the extract phase
3274 * of the HKDF algorithm. */
3275static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
3276 key_slot_t *slot,
3277 psa_algorithm_t hash_alg,
3278 const uint8_t *salt,
3279 size_t salt_length,
3280 const uint8_t *label,
3281 size_t label_length )
3282{
3283 psa_status_t status;
3284 status = psa_hmac_setup_internal( &hkdf->hmac,
3285 salt, salt_length,
3286 PSA_ALG_HMAC_HASH( hash_alg ) );
3287 if( status != PSA_SUCCESS )
3288 return( status );
3289 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3290 slot->data.raw.data,
3291 slot->data.raw.bytes );
3292 if( status != PSA_SUCCESS )
3293 return( status );
3294 status = psa_hmac_finish_internal( &hkdf->hmac,
3295 hkdf->prk,
3296 sizeof( hkdf->prk ) );
3297 if( status != PSA_SUCCESS )
3298 return( status );
3299 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3300 hkdf->block_number = 0;
3301 hkdf->info_length = label_length;
3302 if( label_length != 0 )
3303 {
3304 hkdf->info = mbedtls_calloc( 1, label_length );
3305 if( hkdf->info == NULL )
3306 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3307 memcpy( hkdf->info, label, label_length );
3308 }
3309 return( PSA_SUCCESS );
3310}
3311
Gilles Peskineea0fb492018-07-12 17:17:20 +02003312psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01003313 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003314 psa_algorithm_t alg,
3315 const uint8_t *salt,
3316 size_t salt_length,
3317 const uint8_t *label,
3318 size_t label_length,
3319 size_t capacity )
3320{
3321 key_slot_t *slot;
3322 psa_status_t status;
3323
3324 if( generator->alg != 0 )
3325 return( PSA_ERROR_BAD_STATE );
3326
3327 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
3328 if( status != PSA_SUCCESS )
3329 return( status );
3330 if( slot->type != PSA_KEY_TYPE_DERIVE )
3331 return( PSA_ERROR_INVALID_ARGUMENT );
3332
3333 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
3334 return( PSA_ERROR_INVALID_ARGUMENT );
3335
Gilles Peskinebef7f142018-07-12 17:22:21 +02003336#if defined(MBEDTLS_MD_C)
3337 if( PSA_ALG_IS_HKDF( alg ) )
3338 {
3339 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3340 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3341 if( hash_size == 0 )
3342 return( PSA_ERROR_NOT_SUPPORTED );
3343 if( capacity > 255 * hash_size )
3344 return( PSA_ERROR_INVALID_ARGUMENT );
3345 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3346 slot,
3347 hash_alg,
3348 salt, salt_length,
3349 label, label_length );
3350 }
3351 else
3352#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +02003353 {
3354 return( PSA_ERROR_NOT_SUPPORTED );
3355 }
3356
3357 /* Set generator->alg even on failure so that abort knows what to do. */
3358 generator->alg = alg;
3359 if( status == PSA_SUCCESS )
3360 generator->capacity = capacity;
3361 else
3362 psa_generator_abort( generator );
3363 return( status );
3364}
3365
3366
3367
3368/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003369/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02003370/****************************************************************/
3371
3372psa_status_t psa_generate_random( uint8_t *output,
3373 size_t output_size )
3374{
itayzafrir0adf0fc2018-09-06 16:24:41 +03003375 int ret;
3376 GUARD_MODULE_INITIALIZED;
3377
3378 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02003379 return( mbedtls_to_psa_error( ret ) );
3380}
3381
3382psa_status_t psa_generate_key( psa_key_slot_t key,
3383 psa_key_type_t type,
3384 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003385 const void *extra,
3386 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02003387{
Gilles Peskine12313cd2018-06-20 00:20:32 +02003388 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003389 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003390
Gilles Peskine53d991e2018-07-12 01:14:59 +02003391 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003392 return( PSA_ERROR_INVALID_ARGUMENT );
3393
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003394 status = psa_get_empty_key_slot( key, &slot );
3395 if( status != PSA_SUCCESS )
3396 return( status );
3397
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003398 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003399 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003400 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003401 if( status != PSA_SUCCESS )
3402 return( status );
3403 status = psa_generate_random( slot->data.raw.data,
3404 slot->data.raw.bytes );
3405 if( status != PSA_SUCCESS )
3406 {
3407 mbedtls_free( slot->data.raw.data );
3408 return( status );
3409 }
3410#if defined(MBEDTLS_DES_C)
3411 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02003412 psa_des_set_key_parity( slot->data.raw.data,
3413 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003414#endif /* MBEDTLS_DES_C */
3415 }
3416 else
3417
3418#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3419 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3420 {
3421 mbedtls_rsa_context *rsa;
3422 int ret;
3423 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003424 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3425 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02003426 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003427 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02003428 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003429 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003430 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003431#if INT_MAX < 0xffffffff
3432 /* Check that the uint32_t value passed by the caller fits
3433 * in the range supported by this implementation. */
3434 if( p->e > INT_MAX )
3435 return( PSA_ERROR_NOT_SUPPORTED );
3436#endif
3437 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003438 }
3439 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3440 if( rsa == NULL )
3441 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3442 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3443 ret = mbedtls_rsa_gen_key( rsa,
3444 mbedtls_ctr_drbg_random,
3445 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003446 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003447 exponent );
3448 if( ret != 0 )
3449 {
3450 mbedtls_rsa_free( rsa );
3451 mbedtls_free( rsa );
3452 return( mbedtls_to_psa_error( ret ) );
3453 }
3454 slot->data.rsa = rsa;
3455 }
3456 else
3457#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3458
3459#if defined(MBEDTLS_ECP_C)
3460 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3461 {
3462 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3463 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3464 const mbedtls_ecp_curve_info *curve_info =
3465 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3466 mbedtls_ecp_keypair *ecp;
3467 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003468 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003469 return( PSA_ERROR_NOT_SUPPORTED );
3470 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3471 return( PSA_ERROR_NOT_SUPPORTED );
3472 if( curve_info->bit_size != bits )
3473 return( PSA_ERROR_INVALID_ARGUMENT );
3474 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3475 if( ecp == NULL )
3476 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3477 mbedtls_ecp_keypair_init( ecp );
3478 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3479 mbedtls_ctr_drbg_random,
3480 &global_data.ctr_drbg );
3481 if( ret != 0 )
3482 {
3483 mbedtls_ecp_keypair_free( ecp );
3484 mbedtls_free( ecp );
3485 return( mbedtls_to_psa_error( ret ) );
3486 }
3487 slot->data.ecp = ecp;
3488 }
3489 else
3490#endif /* MBEDTLS_ECP_C */
3491
3492 return( PSA_ERROR_NOT_SUPPORTED );
3493
3494 slot->type = type;
3495 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003496}
3497
3498
3499/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003500/* Module setup */
3501/****************************************************************/
3502
Gilles Peskinee59236f2018-01-27 23:32:46 +01003503void mbedtls_psa_crypto_free( void )
3504{
Jaeden Amero045bd502018-06-26 14:00:08 +01003505 psa_key_slot_t key;
Gilles Peskine9a056342018-08-01 15:46:54 +02003506 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003507 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003508 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3509 mbedtls_entropy_free( &global_data.entropy );
3510 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3511}
3512
3513psa_status_t psa_crypto_init( void )
3514{
3515 int ret;
3516 const unsigned char drbg_seed[] = "PSA";
3517
3518 if( global_data.initialized != 0 )
3519 return( PSA_SUCCESS );
3520
3521 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3522 mbedtls_entropy_init( &global_data.entropy );
3523 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3524
3525 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3526 mbedtls_entropy_func,
3527 &global_data.entropy,
3528 drbg_seed, sizeof( drbg_seed ) - 1 );
3529 if( ret != 0 )
3530 goto exit;
3531
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003532 global_data.initialized = 1;
3533
Gilles Peskinee59236f2018-01-27 23:32:46 +01003534exit:
3535 if( ret != 0 )
3536 mbedtls_psa_crypto_free( );
3537 return( mbedtls_to_psa_error( ret ) );
3538}
3539
3540#endif /* MBEDTLS_PSA_CRYPTO_C */