blob: 01dbf3c3a7bc4fff97a7818d7f88bd49d5242577 [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{
Gilles Peskine996deb12018-08-01 15:45:45 +0200350 /* 0 is not a valid slot number under any circumstance. This
351 * implementation provides slots number 1 to N where N is the
352 * number of available slots. */
353 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200354 return( PSA_ERROR_INVALID_ARGUMENT );
355
Gilles Peskine996deb12018-08-01 15:45:45 +0200356 *p_slot = &global_data.key_slots[key - 1];
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200357 return( PSA_SUCCESS );
358}
359
360/* Retrieve an empty key slot (slot with no key data, but possibly
361 * with some metadata such as a policy). */
362static psa_status_t psa_get_empty_key_slot( psa_key_slot_t key,
363 key_slot_t **p_slot )
364{
365 psa_status_t status;
366 key_slot_t *slot = NULL;
367
368 *p_slot = NULL;
369
370 status = psa_get_key_slot( key, &slot );
371 if( status != PSA_SUCCESS )
372 return( status );
373
374 if( slot->type != PSA_KEY_TYPE_NONE )
375 return( PSA_ERROR_OCCUPIED_SLOT );
376
377 *p_slot = slot;
378 return( status );
379}
380
Jaeden Amerob4fa8c92018-07-11 15:57:44 +0100381/** Retrieve a slot which must contain a key. The key must have allow all the
382 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
383 * operations with this algorithm. */
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200384static psa_status_t psa_get_key_from_slot( psa_key_slot_t key,
385 key_slot_t **p_slot,
386 psa_key_usage_t usage,
387 psa_algorithm_t alg )
388{
389 psa_status_t status;
390 key_slot_t *slot = NULL;
391
392 *p_slot = NULL;
393
394 status = psa_get_key_slot( key, &slot );
395 if( status != PSA_SUCCESS )
396 return( status );
397 if( slot->type == PSA_KEY_TYPE_NONE )
398 return( PSA_ERROR_EMPTY_SLOT );
399
400 /* Enforce that usage policy for the key slot contains all the flags
401 * required by the usage parameter. There is one exception: public
402 * keys can always be exported, so we treat public key objects as
403 * if they had the export flag. */
404 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
405 usage &= ~PSA_KEY_USAGE_EXPORT;
406 if( ( slot->policy.usage & usage ) != usage )
407 return( PSA_ERROR_NOT_PERMITTED );
408 if( alg != 0 && ( alg != slot->policy.alg ) )
409 return( PSA_ERROR_NOT_PERMITTED );
410
411 *p_slot = slot;
412 return( PSA_SUCCESS );
413}
414
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200415
416
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100417/****************************************************************/
418/* Key management */
419/****************************************************************/
420
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100421#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200422static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
423{
424 switch( grpid )
425 {
426 case MBEDTLS_ECP_DP_SECP192R1:
427 return( PSA_ECC_CURVE_SECP192R1 );
428 case MBEDTLS_ECP_DP_SECP224R1:
429 return( PSA_ECC_CURVE_SECP224R1 );
430 case MBEDTLS_ECP_DP_SECP256R1:
431 return( PSA_ECC_CURVE_SECP256R1 );
432 case MBEDTLS_ECP_DP_SECP384R1:
433 return( PSA_ECC_CURVE_SECP384R1 );
434 case MBEDTLS_ECP_DP_SECP521R1:
435 return( PSA_ECC_CURVE_SECP521R1 );
436 case MBEDTLS_ECP_DP_BP256R1:
437 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
438 case MBEDTLS_ECP_DP_BP384R1:
439 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
440 case MBEDTLS_ECP_DP_BP512R1:
441 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
442 case MBEDTLS_ECP_DP_CURVE25519:
443 return( PSA_ECC_CURVE_CURVE25519 );
444 case MBEDTLS_ECP_DP_SECP192K1:
445 return( PSA_ECC_CURVE_SECP192K1 );
446 case MBEDTLS_ECP_DP_SECP224K1:
447 return( PSA_ECC_CURVE_SECP224K1 );
448 case MBEDTLS_ECP_DP_SECP256K1:
449 return( PSA_ECC_CURVE_SECP256K1 );
450 case MBEDTLS_ECP_DP_CURVE448:
451 return( PSA_ECC_CURVE_CURVE448 );
452 default:
453 return( 0 );
454 }
455}
456
Gilles Peskine12313cd2018-06-20 00:20:32 +0200457static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
458{
459 switch( curve )
460 {
461 case PSA_ECC_CURVE_SECP192R1:
462 return( MBEDTLS_ECP_DP_SECP192R1 );
463 case PSA_ECC_CURVE_SECP224R1:
464 return( MBEDTLS_ECP_DP_SECP224R1 );
465 case PSA_ECC_CURVE_SECP256R1:
466 return( MBEDTLS_ECP_DP_SECP256R1 );
467 case PSA_ECC_CURVE_SECP384R1:
468 return( MBEDTLS_ECP_DP_SECP384R1 );
469 case PSA_ECC_CURVE_SECP521R1:
470 return( MBEDTLS_ECP_DP_SECP521R1 );
471 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
472 return( MBEDTLS_ECP_DP_BP256R1 );
473 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
474 return( MBEDTLS_ECP_DP_BP384R1 );
475 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
476 return( MBEDTLS_ECP_DP_BP512R1 );
477 case PSA_ECC_CURVE_CURVE25519:
478 return( MBEDTLS_ECP_DP_CURVE25519 );
479 case PSA_ECC_CURVE_SECP192K1:
480 return( MBEDTLS_ECP_DP_SECP192K1 );
481 case PSA_ECC_CURVE_SECP224K1:
482 return( MBEDTLS_ECP_DP_SECP224K1 );
483 case PSA_ECC_CURVE_SECP256K1:
484 return( MBEDTLS_ECP_DP_SECP256K1 );
485 case PSA_ECC_CURVE_CURVE448:
486 return( MBEDTLS_ECP_DP_CURVE448 );
487 default:
488 return( MBEDTLS_ECP_DP_NONE );
489 }
490}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100491#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200492
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200493static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
494 size_t bits,
495 struct raw_data *raw )
496{
497 /* Check that the bit size is acceptable for the key type */
498 switch( type )
499 {
500 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200501 if( bits == 0 )
502 {
503 raw->bytes = 0;
504 raw->data = NULL;
505 return( PSA_SUCCESS );
506 }
507 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200508#if defined(MBEDTLS_MD_C)
509 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200510#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200511 case PSA_KEY_TYPE_DERIVE:
512 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200513#if defined(MBEDTLS_AES_C)
514 case PSA_KEY_TYPE_AES:
515 if( bits != 128 && bits != 192 && bits != 256 )
516 return( PSA_ERROR_INVALID_ARGUMENT );
517 break;
518#endif
519#if defined(MBEDTLS_CAMELLIA_C)
520 case PSA_KEY_TYPE_CAMELLIA:
521 if( bits != 128 && bits != 192 && bits != 256 )
522 return( PSA_ERROR_INVALID_ARGUMENT );
523 break;
524#endif
525#if defined(MBEDTLS_DES_C)
526 case PSA_KEY_TYPE_DES:
527 if( bits != 64 && bits != 128 && bits != 192 )
528 return( PSA_ERROR_INVALID_ARGUMENT );
529 break;
530#endif
531#if defined(MBEDTLS_ARC4_C)
532 case PSA_KEY_TYPE_ARC4:
533 if( bits < 8 || bits > 2048 )
534 return( PSA_ERROR_INVALID_ARGUMENT );
535 break;
536#endif
537 default:
538 return( PSA_ERROR_NOT_SUPPORTED );
539 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200540 if( bits % 8 != 0 )
541 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200542
543 /* Allocate memory for the key */
544 raw->bytes = PSA_BITS_TO_BYTES( bits );
545 raw->data = mbedtls_calloc( 1, raw->bytes );
546 if( raw->data == NULL )
547 {
548 raw->bytes = 0;
549 return( PSA_ERROR_INSUFFICIENT_MEMORY );
550 }
551 return( PSA_SUCCESS );
552}
553
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200554#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
555static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
556 mbedtls_rsa_context **p_rsa )
557{
558 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
559 return( PSA_ERROR_INVALID_ARGUMENT );
560 else
561 {
562 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
563 size_t bits = mbedtls_rsa_get_bitlen( rsa );
564 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
565 return( PSA_ERROR_NOT_SUPPORTED );
566 *p_rsa = rsa;
567 return( PSA_SUCCESS );
568 }
569}
570#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
571
572#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
573static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
574 mbedtls_pk_context *pk,
575 mbedtls_ecp_keypair **p_ecp )
576{
577 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
578 return( PSA_ERROR_INVALID_ARGUMENT );
579 else
580 {
581 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
582 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
583 if( actual_curve != expected_curve )
584 return( PSA_ERROR_INVALID_ARGUMENT );
585 *p_ecp = ecp;
586 return( PSA_SUCCESS );
587 }
588}
589#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
590
Gilles Peskine2d277862018-06-18 15:41:12 +0200591psa_status_t psa_import_key( psa_key_slot_t key,
592 psa_key_type_t type,
593 const uint8_t *data,
594 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100595{
596 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200597 psa_status_t status = PSA_SUCCESS;
598 status = psa_get_empty_key_slot( key, &slot );
599 if( status != PSA_SUCCESS )
600 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100601
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200602 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100603 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100604 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100605 if( data_length > SIZE_MAX / 8 )
606 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200607 status = prepare_raw_data_slot( type,
608 PSA_BYTES_TO_BITS( data_length ),
609 &slot->data.raw );
610 if( status != PSA_SUCCESS )
611 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200612 if( data_length != 0 )
613 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100614 }
615 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100616#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200617 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100618 {
619 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100620 mbedtls_pk_context pk;
621 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200622
623 /* Parse the data. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100624 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
625 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
626 else
627 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100628 if( ret != 0 )
629 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200630
631 /* We have something that the pkparse module recognizes.
632 * If it has the expected type and passes any type-specific
633 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100634#if defined(MBEDTLS_RSA_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200635 if( PSA_KEY_TYPE_IS_RSA( type ) )
636 status = psa_import_rsa_key( &pk, &slot->data.rsa );
637 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100638#endif /* MBEDTLS_RSA_C */
639#if defined(MBEDTLS_ECP_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200640 if( PSA_KEY_TYPE_IS_ECC( type ) )
641 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( type ),
642 &pk, &slot->data.ecp );
643 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100644#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200645 {
646 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200647 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200648
Gilles Peskinec648d692018-06-28 08:46:13 +0200649 /* Free the content of the pk object only on error. On success,
650 * the content of the object has been stored in the slot. */
651 if( status != PSA_SUCCESS )
652 {
653 mbedtls_pk_free( &pk );
654 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100655 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100656 }
657 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100658#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100659 {
660 return( PSA_ERROR_NOT_SUPPORTED );
661 }
662
663 slot->type = type;
664 return( PSA_SUCCESS );
665}
666
Gilles Peskine2d277862018-06-18 15:41:12 +0200667psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100668{
669 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200670 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100671
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200672 status = psa_get_key_slot( key, &slot );
673 if( status != PSA_SUCCESS )
674 return( status );
675
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100676 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200677 {
678 /* No key material to clean, but do zeroize the slot below to wipe
679 * metadata such as policies. */
680 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200681 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100682 {
683 mbedtls_free( slot->data.raw.data );
684 }
685 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100686#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200687 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100688 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100689 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100690 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100691 }
692 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100693#endif /* defined(MBEDTLS_RSA_C) */
694#if defined(MBEDTLS_ECP_C)
695 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
696 {
697 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100698 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100699 }
700 else
701#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100702 {
703 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100704 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100705 return( PSA_ERROR_TAMPERING_DETECTED );
706 }
707
708 mbedtls_zeroize( slot, sizeof( *slot ) );
709 return( PSA_SUCCESS );
710}
711
Gilles Peskineb870b182018-07-06 16:02:09 +0200712/* Return the size of the key in the given slot, in bits. */
713static size_t psa_get_key_bits( const key_slot_t *slot )
714{
715 if( key_type_is_raw_bytes( slot->type ) )
716 return( slot->data.raw.bytes * 8 );
717#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200718 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineb870b182018-07-06 16:02:09 +0200719 return( mbedtls_rsa_get_bitlen( slot->data.rsa ) );
720#endif /* defined(MBEDTLS_RSA_C) */
721#if defined(MBEDTLS_ECP_C)
722 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
723 return( slot->data.ecp->grp.pbits );
724#endif /* defined(MBEDTLS_ECP_C) */
725 /* Shouldn't happen except on an empty slot. */
726 return( 0 );
727}
728
Gilles Peskine2d277862018-06-18 15:41:12 +0200729psa_status_t psa_get_key_information( psa_key_slot_t key,
730 psa_key_type_t *type,
731 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100732{
733 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200734 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100735
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100736 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200737 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738 if( bits != NULL )
739 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200740 status = psa_get_key_slot( key, &slot );
741 if( status != PSA_SUCCESS )
742 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200743
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100744 if( slot->type == PSA_KEY_TYPE_NONE )
745 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200746 if( type != NULL )
747 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200748 if( bits != NULL )
749 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100750 return( PSA_SUCCESS );
751}
752
Gilles Peskine2d277862018-06-18 15:41:12 +0200753static psa_status_t psa_internal_export_key( psa_key_slot_t key,
754 uint8_t *data,
755 size_t data_size,
756 size_t *data_length,
757 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100758{
759 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200760 psa_status_t status;
761 /* Exporting a public key doesn't require a usage flag. If we're
762 * called by psa_export_public_key(), don't require the EXPORT flag.
763 * If we're called by psa_export_key(), do require the EXPORT flag;
764 * if the key turns out to be public key object, psa_get_key_from_slot()
765 * will ignore this flag. */
766 psa_key_usage_t usage = export_public_key ? 0 : PSA_KEY_USAGE_EXPORT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100767
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100768 /* Set the key to empty now, so that even when there are errors, we always
769 * set data_length to a value between 0 and data_size. On error, setting
770 * the key to empty is a good choice because an empty key representation is
771 * unlikely to be accepted anywhere. */
772 *data_length = 0;
773
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200774 status = psa_get_key_from_slot( key, &slot, usage, 0 );
775 if( status != PSA_SUCCESS )
776 return( status );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200777 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300778 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300779
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200780 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100781 {
782 if( slot->data.raw.bytes > data_size )
783 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200784 if( slot->data.raw.bytes != 0 )
785 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100786 *data_length = slot->data.raw.bytes;
787 return( PSA_SUCCESS );
788 }
789 else
Moran Peker17e36e12018-05-02 12:55:20 +0300790 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100791#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200792 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300793 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100794 {
Moran Pekera998bc62018-04-16 18:16:20 +0300795 mbedtls_pk_context pk;
796 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +0200797 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300798 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100799#if defined(MBEDTLS_RSA_C)
800 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300801 pk.pk_info = &mbedtls_rsa_info;
802 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100803#else
804 return( PSA_ERROR_NOT_SUPPORTED );
805#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300806 }
807 else
808 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100809#if defined(MBEDTLS_ECP_C)
810 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300811 pk.pk_info = &mbedtls_eckey_info;
812 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100813#else
814 return( PSA_ERROR_NOT_SUPPORTED );
815#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300816 }
Moran Pekerd7326592018-05-29 16:56:39 +0300817 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300818 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300819 else
820 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300821 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200822 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200823 /* If data_size is 0 then data may be NULL and then the
824 * call to memset would have undefined behavior. */
825 if( data_size != 0 )
826 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300827 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200828 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200829 /* The mbedtls_pk_xxx functions write to the end of the buffer.
830 * Move the data to the beginning and erase remaining data
831 * at the original location. */
832 if( 2 * (size_t) ret <= data_size )
833 {
834 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200835 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200836 }
837 else if( (size_t) ret < data_size )
838 {
839 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200840 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200841 }
Moran Pekera998bc62018-04-16 18:16:20 +0300842 *data_length = ret;
843 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100844 }
845 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100846#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300847 {
848 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200849 it is valid for a special-purpose implementation to omit
850 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300851 return( PSA_ERROR_NOT_SUPPORTED );
852 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100853 }
854}
855
Gilles Peskine2d277862018-06-18 15:41:12 +0200856psa_status_t psa_export_key( psa_key_slot_t key,
857 uint8_t *data,
858 size_t data_size,
859 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300860{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200861 return( psa_internal_export_key( key, data, data_size,
862 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100863}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100864
Gilles Peskine2d277862018-06-18 15:41:12 +0200865psa_status_t psa_export_public_key( psa_key_slot_t key,
866 uint8_t *data,
867 size_t data_size,
868 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300869{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200870 return( psa_internal_export_key( key, data, data_size,
871 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300872}
873
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200874
875
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100876/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100877/* Message digests */
878/****************************************************************/
879
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100880static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100881{
882 switch( alg )
883 {
884#if defined(MBEDTLS_MD2_C)
885 case PSA_ALG_MD2:
886 return( &mbedtls_md2_info );
887#endif
888#if defined(MBEDTLS_MD4_C)
889 case PSA_ALG_MD4:
890 return( &mbedtls_md4_info );
891#endif
892#if defined(MBEDTLS_MD5_C)
893 case PSA_ALG_MD5:
894 return( &mbedtls_md5_info );
895#endif
896#if defined(MBEDTLS_RIPEMD160_C)
897 case PSA_ALG_RIPEMD160:
898 return( &mbedtls_ripemd160_info );
899#endif
900#if defined(MBEDTLS_SHA1_C)
901 case PSA_ALG_SHA_1:
902 return( &mbedtls_sha1_info );
903#endif
904#if defined(MBEDTLS_SHA256_C)
905 case PSA_ALG_SHA_224:
906 return( &mbedtls_sha224_info );
907 case PSA_ALG_SHA_256:
908 return( &mbedtls_sha256_info );
909#endif
910#if defined(MBEDTLS_SHA512_C)
911 case PSA_ALG_SHA_384:
912 return( &mbedtls_sha384_info );
913 case PSA_ALG_SHA_512:
914 return( &mbedtls_sha512_info );
915#endif
916 default:
917 return( NULL );
918 }
919}
920
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100921psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
922{
923 switch( operation->alg )
924 {
Gilles Peskine81736312018-06-26 15:04:31 +0200925 case 0:
926 /* The object has (apparently) been initialized but it is not
927 * in use. It's ok to call abort on such an object, and there's
928 * nothing to do. */
929 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100930#if defined(MBEDTLS_MD2_C)
931 case PSA_ALG_MD2:
932 mbedtls_md2_free( &operation->ctx.md2 );
933 break;
934#endif
935#if defined(MBEDTLS_MD4_C)
936 case PSA_ALG_MD4:
937 mbedtls_md4_free( &operation->ctx.md4 );
938 break;
939#endif
940#if defined(MBEDTLS_MD5_C)
941 case PSA_ALG_MD5:
942 mbedtls_md5_free( &operation->ctx.md5 );
943 break;
944#endif
945#if defined(MBEDTLS_RIPEMD160_C)
946 case PSA_ALG_RIPEMD160:
947 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
948 break;
949#endif
950#if defined(MBEDTLS_SHA1_C)
951 case PSA_ALG_SHA_1:
952 mbedtls_sha1_free( &operation->ctx.sha1 );
953 break;
954#endif
955#if defined(MBEDTLS_SHA256_C)
956 case PSA_ALG_SHA_224:
957 case PSA_ALG_SHA_256:
958 mbedtls_sha256_free( &operation->ctx.sha256 );
959 break;
960#endif
961#if defined(MBEDTLS_SHA512_C)
962 case PSA_ALG_SHA_384:
963 case PSA_ALG_SHA_512:
964 mbedtls_sha512_free( &operation->ctx.sha512 );
965 break;
966#endif
967 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +0200968 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100969 }
970 operation->alg = 0;
971 return( PSA_SUCCESS );
972}
973
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200974psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100975 psa_algorithm_t alg )
976{
977 int ret;
978 operation->alg = 0;
979 switch( alg )
980 {
981#if defined(MBEDTLS_MD2_C)
982 case PSA_ALG_MD2:
983 mbedtls_md2_init( &operation->ctx.md2 );
984 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
985 break;
986#endif
987#if defined(MBEDTLS_MD4_C)
988 case PSA_ALG_MD4:
989 mbedtls_md4_init( &operation->ctx.md4 );
990 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
991 break;
992#endif
993#if defined(MBEDTLS_MD5_C)
994 case PSA_ALG_MD5:
995 mbedtls_md5_init( &operation->ctx.md5 );
996 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
997 break;
998#endif
999#if defined(MBEDTLS_RIPEMD160_C)
1000 case PSA_ALG_RIPEMD160:
1001 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1002 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1003 break;
1004#endif
1005#if defined(MBEDTLS_SHA1_C)
1006 case PSA_ALG_SHA_1:
1007 mbedtls_sha1_init( &operation->ctx.sha1 );
1008 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1009 break;
1010#endif
1011#if defined(MBEDTLS_SHA256_C)
1012 case PSA_ALG_SHA_224:
1013 mbedtls_sha256_init( &operation->ctx.sha256 );
1014 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1015 break;
1016 case PSA_ALG_SHA_256:
1017 mbedtls_sha256_init( &operation->ctx.sha256 );
1018 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1019 break;
1020#endif
1021#if defined(MBEDTLS_SHA512_C)
1022 case PSA_ALG_SHA_384:
1023 mbedtls_sha512_init( &operation->ctx.sha512 );
1024 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1025 break;
1026 case PSA_ALG_SHA_512:
1027 mbedtls_sha512_init( &operation->ctx.sha512 );
1028 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1029 break;
1030#endif
1031 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001032 return( PSA_ALG_IS_HASH( alg ) ?
1033 PSA_ERROR_NOT_SUPPORTED :
1034 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001035 }
1036 if( ret == 0 )
1037 operation->alg = alg;
1038 else
1039 psa_hash_abort( operation );
1040 return( mbedtls_to_psa_error( ret ) );
1041}
1042
1043psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1044 const uint8_t *input,
1045 size_t input_length )
1046{
1047 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001048
1049 /* Don't require hash implementations to behave correctly on a
1050 * zero-length input, which may have an invalid pointer. */
1051 if( input_length == 0 )
1052 return( PSA_SUCCESS );
1053
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001054 switch( operation->alg )
1055 {
1056#if defined(MBEDTLS_MD2_C)
1057 case PSA_ALG_MD2:
1058 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1059 input, input_length );
1060 break;
1061#endif
1062#if defined(MBEDTLS_MD4_C)
1063 case PSA_ALG_MD4:
1064 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1065 input, input_length );
1066 break;
1067#endif
1068#if defined(MBEDTLS_MD5_C)
1069 case PSA_ALG_MD5:
1070 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1071 input, input_length );
1072 break;
1073#endif
1074#if defined(MBEDTLS_RIPEMD160_C)
1075 case PSA_ALG_RIPEMD160:
1076 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1077 input, input_length );
1078 break;
1079#endif
1080#if defined(MBEDTLS_SHA1_C)
1081 case PSA_ALG_SHA_1:
1082 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1083 input, input_length );
1084 break;
1085#endif
1086#if defined(MBEDTLS_SHA256_C)
1087 case PSA_ALG_SHA_224:
1088 case PSA_ALG_SHA_256:
1089 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1090 input, input_length );
1091 break;
1092#endif
1093#if defined(MBEDTLS_SHA512_C)
1094 case PSA_ALG_SHA_384:
1095 case PSA_ALG_SHA_512:
1096 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1097 input, input_length );
1098 break;
1099#endif
1100 default:
1101 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1102 break;
1103 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001104
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001105 if( ret != 0 )
1106 psa_hash_abort( operation );
1107 return( mbedtls_to_psa_error( ret ) );
1108}
1109
1110psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1111 uint8_t *hash,
1112 size_t hash_size,
1113 size_t *hash_length )
1114{
itayzafrir40835d42018-08-02 13:14:17 +03001115 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001116 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001117 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001118
1119 /* Fill the output buffer with something that isn't a valid hash
1120 * (barring an attack on the hash and deliberately-crafted input),
1121 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001122 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001123 /* If hash_size is 0 then hash may be NULL and then the
1124 * call to memset would have undefined behavior. */
1125 if( hash_size != 0 )
1126 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001127
1128 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001129 {
1130 status = PSA_ERROR_BUFFER_TOO_SMALL;
1131 goto exit;
1132 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001133
1134 switch( operation->alg )
1135 {
1136#if defined(MBEDTLS_MD2_C)
1137 case PSA_ALG_MD2:
1138 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1139 break;
1140#endif
1141#if defined(MBEDTLS_MD4_C)
1142 case PSA_ALG_MD4:
1143 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1144 break;
1145#endif
1146#if defined(MBEDTLS_MD5_C)
1147 case PSA_ALG_MD5:
1148 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1149 break;
1150#endif
1151#if defined(MBEDTLS_RIPEMD160_C)
1152 case PSA_ALG_RIPEMD160:
1153 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1154 break;
1155#endif
1156#if defined(MBEDTLS_SHA1_C)
1157 case PSA_ALG_SHA_1:
1158 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1159 break;
1160#endif
1161#if defined(MBEDTLS_SHA256_C)
1162 case PSA_ALG_SHA_224:
1163 case PSA_ALG_SHA_256:
1164 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1165 break;
1166#endif
1167#if defined(MBEDTLS_SHA512_C)
1168 case PSA_ALG_SHA_384:
1169 case PSA_ALG_SHA_512:
1170 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1171 break;
1172#endif
1173 default:
1174 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1175 break;
1176 }
itayzafrir40835d42018-08-02 13:14:17 +03001177 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001178
itayzafrir40835d42018-08-02 13:14:17 +03001179exit:
1180 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001181 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001182 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001183 return( psa_hash_abort( operation ) );
1184 }
1185 else
1186 {
1187 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001188 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001189 }
1190}
1191
Gilles Peskine2d277862018-06-18 15:41:12 +02001192psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1193 const uint8_t *hash,
1194 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001195{
1196 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1197 size_t actual_hash_length;
1198 psa_status_t status = psa_hash_finish( operation,
1199 actual_hash, sizeof( actual_hash ),
1200 &actual_hash_length );
1201 if( status != PSA_SUCCESS )
1202 return( status );
1203 if( actual_hash_length != hash_length )
1204 return( PSA_ERROR_INVALID_SIGNATURE );
1205 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1206 return( PSA_ERROR_INVALID_SIGNATURE );
1207 return( PSA_SUCCESS );
1208}
1209
1210
1211
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001212/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001213/* MAC */
1214/****************************************************************/
1215
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001216static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001217 psa_algorithm_t alg,
1218 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001219 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001220 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001221{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001222 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001223 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001224
1225 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1226 {
1227 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001228 {
1229 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1230 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001231
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001232 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001233 {
Gilles Peskine5d1888e2018-07-12 00:32:42 +02001234 case PSA_ALG_STREAM_CIPHER_BASE:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001235 mode = MBEDTLS_MODE_STREAM;
1236 break;
1237 case PSA_ALG_CBC_BASE:
1238 mode = MBEDTLS_MODE_CBC;
1239 break;
1240 case PSA_ALG_CFB_BASE:
1241 mode = MBEDTLS_MODE_CFB;
1242 break;
1243 case PSA_ALG_OFB_BASE:
1244 mode = MBEDTLS_MODE_OFB;
1245 break;
1246 case PSA_ALG_CTR:
1247 mode = MBEDTLS_MODE_CTR;
1248 break;
1249 case PSA_ALG_CCM:
1250 mode = MBEDTLS_MODE_CCM;
1251 break;
1252 case PSA_ALG_GCM:
1253 mode = MBEDTLS_MODE_GCM;
1254 break;
1255 default:
1256 return( NULL );
1257 }
1258 }
1259 else if( alg == PSA_ALG_CMAC )
1260 mode = MBEDTLS_MODE_ECB;
1261 else if( alg == PSA_ALG_GMAC )
1262 mode = MBEDTLS_MODE_GCM;
1263 else
1264 return( NULL );
1265
1266 switch( key_type )
1267 {
1268 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001269 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001270 break;
1271 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001272 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1273 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001274 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001275 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001276 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001277 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001278 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1279 * but two-key Triple-DES is functionally three-key Triple-DES
1280 * with K1=K3, so that's how we present it to mbedtls. */
1281 if( key_bits == 128 )
1282 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001283 break;
1284 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001285 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001286 break;
1287 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001288 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001289 break;
1290 default:
1291 return( NULL );
1292 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001293 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001294 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001295
Jaeden Amero23bbb752018-06-26 14:16:54 +01001296 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1297 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001298}
1299
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001300static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001301{
Gilles Peskine2d277862018-06-18 15:41:12 +02001302 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001303 {
1304 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001305 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001306 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001307 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001308 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001309 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001310 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001311 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001312 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001313 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001314 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001315 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001316 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001317 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001318 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001319 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001320 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001321 return( 128 );
1322 default:
1323 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001324 }
1325}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001326
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001327/* Initialize the MAC operation structure. Once this function has been
1328 * called, psa_mac_abort can run and will do the right thing. */
1329static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1330 psa_algorithm_t alg )
1331{
1332 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1333
1334 operation->alg = alg;
1335 operation->key_set = 0;
1336 operation->iv_set = 0;
1337 operation->iv_required = 0;
1338 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001339 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001340
1341#if defined(MBEDTLS_CMAC_C)
1342 if( alg == PSA_ALG_CMAC )
1343 {
1344 operation->iv_required = 0;
1345 mbedtls_cipher_init( &operation->ctx.cmac );
1346 status = PSA_SUCCESS;
1347 }
1348 else
1349#endif /* MBEDTLS_CMAC_C */
1350#if defined(MBEDTLS_MD_C)
1351 if( PSA_ALG_IS_HMAC( operation->alg ) )
1352 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001353 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1354 operation->ctx.hmac.hash_ctx.alg = 0;
1355 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001356 }
1357 else
1358#endif /* MBEDTLS_MD_C */
1359 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001360 if( ! PSA_ALG_IS_MAC( alg ) )
1361 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001362 }
1363
1364 if( status != PSA_SUCCESS )
1365 memset( operation, 0, sizeof( *operation ) );
1366 return( status );
1367}
1368
Gilles Peskine01126fa2018-07-12 17:04:55 +02001369#if defined(MBEDTLS_MD_C)
1370static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1371{
1372 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1373 return( psa_hash_abort( &hmac->hash_ctx ) );
1374}
1375#endif /* MBEDTLS_MD_C */
1376
Gilles Peskine8c9def32018-02-08 10:02:12 +01001377psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1378{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001379 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001380 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001381 /* The object has (apparently) been initialized but it is not
1382 * in use. It's ok to call abort on such an object, and there's
1383 * nothing to do. */
1384 return( PSA_SUCCESS );
1385 }
1386 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001387#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001388 if( operation->alg == PSA_ALG_CMAC )
1389 {
1390 mbedtls_cipher_free( &operation->ctx.cmac );
1391 }
1392 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001393#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001394#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001395 if( PSA_ALG_IS_HMAC( operation->alg ) )
1396 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001397 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001398 }
1399 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001400#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001401 {
1402 /* Sanity check (shouldn't happen: operation->alg should
1403 * always have been initialized to a valid value). */
1404 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001405 }
Moran Peker41deec42018-04-04 15:43:05 +03001406
Gilles Peskine8c9def32018-02-08 10:02:12 +01001407 operation->alg = 0;
1408 operation->key_set = 0;
1409 operation->iv_set = 0;
1410 operation->iv_required = 0;
1411 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001412 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001413
Gilles Peskine8c9def32018-02-08 10:02:12 +01001414 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001415
1416bad_state:
1417 /* If abort is called on an uninitialized object, we can't trust
1418 * anything. Wipe the object in case it contains confidential data.
1419 * This may result in a memory leak if a pointer gets overwritten,
1420 * but it's too late to do anything about this. */
1421 memset( operation, 0, sizeof( *operation ) );
1422 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001423}
1424
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001425#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001426static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001427 size_t key_bits,
1428 key_slot_t *slot,
1429 const mbedtls_cipher_info_t *cipher_info )
1430{
1431 int ret;
1432
1433 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001434
1435 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1436 if( ret != 0 )
1437 return( ret );
1438
1439 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1440 slot->data.raw.data,
1441 key_bits );
1442 return( ret );
1443}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001444#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001445
Gilles Peskine248051a2018-06-20 16:09:38 +02001446#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001447static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1448 const uint8_t *key,
1449 size_t key_length,
1450 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001451{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001452 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001453 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001454 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001455 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001456 psa_status_t status;
1457
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001458 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1459 * overflow below. This should never trigger if the hash algorithm
1460 * is implemented correctly. */
1461 /* The size checks against the ipad and opad buffers cannot be written
1462 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1463 * because that triggers -Wlogical-op on GCC 7.3. */
1464 if( block_size > sizeof( ipad ) )
1465 return( PSA_ERROR_NOT_SUPPORTED );
1466 if( block_size > sizeof( hmac->opad ) )
1467 return( PSA_ERROR_NOT_SUPPORTED );
1468 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001469 return( PSA_ERROR_NOT_SUPPORTED );
1470
Gilles Peskined223b522018-06-11 18:12:58 +02001471 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001472 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001473 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001474 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001475 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001476 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001477 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001478 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001479 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001480 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001481 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001482 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001483 }
Gilles Peskine96889972018-07-12 17:07:03 +02001484 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1485 * but it is permitted. It is common when HMAC is used in HKDF, for
1486 * example. Don't call `memcpy` in the 0-length because `key` could be
1487 * an invalid pointer which would make the behavior undefined. */
1488 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001489 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001490
Gilles Peskined223b522018-06-11 18:12:58 +02001491 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1492 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001493 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001494 ipad[i] ^= 0x36;
1495 memset( ipad + key_length, 0x36, block_size - key_length );
1496
1497 /* Copy the key material from ipad to opad, flipping the requisite bits,
1498 * and filling the rest of opad with the requisite constant. */
1499 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001500 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1501 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001502
Gilles Peskine01126fa2018-07-12 17:04:55 +02001503 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001504 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001505 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001506
Gilles Peskine01126fa2018-07-12 17:04:55 +02001507 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001508
1509cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001510 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001511
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001512 return( status );
1513}
Gilles Peskine248051a2018-06-20 16:09:38 +02001514#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001515
Gilles Peskine89167cb2018-07-08 20:12:23 +02001516static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1517 psa_key_slot_t key,
1518 psa_algorithm_t alg,
1519 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001520{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001521 psa_status_t status;
1522 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001523 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001524 psa_key_usage_t usage =
1525 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001526
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001527 status = psa_mac_init( operation, alg );
1528 if( status != PSA_SUCCESS )
1529 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001530 if( is_sign )
1531 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001532
Gilles Peskine89167cb2018-07-08 20:12:23 +02001533 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001534 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001535 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001536 key_bits = psa_get_key_bits( slot );
1537
Gilles Peskine8c9def32018-02-08 10:02:12 +01001538#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001539 if( alg == PSA_ALG_CMAC )
1540 {
1541 const mbedtls_cipher_info_t *cipher_info =
1542 mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
1543 int ret;
1544 if( cipher_info == NULL )
1545 {
1546 status = PSA_ERROR_NOT_SUPPORTED;
1547 goto exit;
1548 }
1549 operation->mac_size = cipher_info->block_size;
1550 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1551 status = mbedtls_to_psa_error( ret );
1552 }
1553 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001554#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001555#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001556 if( PSA_ALG_IS_HMAC( alg ) )
1557 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001558 psa_algorithm_t hash_alg = PSA_ALG_HMAC_HASH( alg );
1559 if( hash_alg == 0 )
1560 {
1561 status = PSA_ERROR_NOT_SUPPORTED;
1562 goto exit;
1563 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001564
1565 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1566 /* Sanity check. This shouldn't fail on a valid configuration. */
1567 if( operation->mac_size == 0 ||
1568 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1569 {
1570 status = PSA_ERROR_NOT_SUPPORTED;
1571 goto exit;
1572 }
1573
Gilles Peskine01126fa2018-07-12 17:04:55 +02001574 if( slot->type != PSA_KEY_TYPE_HMAC )
1575 {
1576 status = PSA_ERROR_INVALID_ARGUMENT;
1577 goto exit;
1578 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001579
Gilles Peskine01126fa2018-07-12 17:04:55 +02001580 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1581 slot->data.raw.data,
1582 slot->data.raw.bytes,
1583 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001584 }
1585 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001586#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001587 {
1588 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001589 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001590
Gilles Peskinefbfac682018-07-08 20:51:54 +02001591exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001592 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001593 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001594 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001595 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001596 else
1597 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001598 operation->key_set = 1;
1599 }
1600 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001601}
1602
Gilles Peskine89167cb2018-07-08 20:12:23 +02001603psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1604 psa_key_slot_t key,
1605 psa_algorithm_t alg )
1606{
1607 return( psa_mac_setup( operation, key, alg, 1 ) );
1608}
1609
1610psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1611 psa_key_slot_t key,
1612 psa_algorithm_t alg )
1613{
1614 return( psa_mac_setup( operation, key, alg, 0 ) );
1615}
1616
Gilles Peskine8c9def32018-02-08 10:02:12 +01001617psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1618 const uint8_t *input,
1619 size_t input_length )
1620{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001621 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001622 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001623 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001624 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001625 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001626 operation->has_input = 1;
1627
Gilles Peskine8c9def32018-02-08 10:02:12 +01001628#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001629 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001630 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001631 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1632 input, input_length );
1633 status = mbedtls_to_psa_error( ret );
1634 }
1635 else
1636#endif /* MBEDTLS_CMAC_C */
1637#if defined(MBEDTLS_MD_C)
1638 if( PSA_ALG_IS_HMAC( operation->alg ) )
1639 {
1640 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1641 input_length );
1642 }
1643 else
1644#endif /* MBEDTLS_MD_C */
1645 {
1646 /* This shouldn't happen if `operation` was initialized by
1647 * a setup function. */
1648 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001649 }
1650
Gilles Peskinefbfac682018-07-08 20:51:54 +02001651cleanup:
1652 if( status != PSA_SUCCESS )
1653 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001654 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001655}
1656
Gilles Peskine01126fa2018-07-12 17:04:55 +02001657#if defined(MBEDTLS_MD_C)
1658static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1659 uint8_t *mac,
1660 size_t mac_size )
1661{
1662 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1663 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1664 size_t hash_size = 0;
1665 size_t block_size = psa_get_hash_block_size( hash_alg );
1666 psa_status_t status;
1667
Gilles Peskine01126fa2018-07-12 17:04:55 +02001668 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1669 if( status != PSA_SUCCESS )
1670 return( status );
1671 /* From here on, tmp needs to be wiped. */
1672
1673 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1674 if( status != PSA_SUCCESS )
1675 goto exit;
1676
1677 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1678 if( status != PSA_SUCCESS )
1679 goto exit;
1680
1681 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1682 if( status != PSA_SUCCESS )
1683 goto exit;
1684
1685 status = psa_hash_finish( &hmac->hash_ctx, mac, mac_size, &hash_size );
1686
1687exit:
1688 mbedtls_zeroize( tmp, hash_size );
1689 return( status );
1690}
1691#endif /* MBEDTLS_MD_C */
1692
mohammad16036df908f2018-04-02 08:34:15 -07001693static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001694 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001695 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001696{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001697 if( ! operation->key_set )
1698 return( PSA_ERROR_BAD_STATE );
1699 if( operation->iv_required && ! operation->iv_set )
1700 return( PSA_ERROR_BAD_STATE );
1701
Gilles Peskine8c9def32018-02-08 10:02:12 +01001702 if( mac_size < operation->mac_size )
1703 return( PSA_ERROR_BUFFER_TOO_SMALL );
1704
Gilles Peskine8c9def32018-02-08 10:02:12 +01001705#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001706 if( operation->alg == PSA_ALG_CMAC )
1707 {
1708 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1709 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001710 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001711 else
1712#endif /* MBEDTLS_CMAC_C */
1713#if defined(MBEDTLS_MD_C)
1714 if( PSA_ALG_IS_HMAC( operation->alg ) )
1715 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001716 return( psa_hmac_finish_internal( &operation->ctx.hmac,
1717 mac, mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001718 }
1719 else
1720#endif /* MBEDTLS_MD_C */
1721 {
1722 /* This shouldn't happen if `operation` was initialized by
1723 * a setup function. */
1724 return( PSA_ERROR_BAD_STATE );
1725 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001726}
1727
Gilles Peskineacd4be32018-07-08 19:56:25 +02001728psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1729 uint8_t *mac,
1730 size_t mac_size,
1731 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001732{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001733 psa_status_t status;
1734
1735 /* Fill the output buffer with something that isn't a valid mac
1736 * (barring an attack on the mac and deliberately-crafted input),
1737 * in case the caller doesn't check the return status properly. */
1738 *mac_length = mac_size;
1739 /* If mac_size is 0 then mac may be NULL and then the
1740 * call to memset would have undefined behavior. */
1741 if( mac_size != 0 )
1742 memset( mac, '!', mac_size );
1743
Gilles Peskine89167cb2018-07-08 20:12:23 +02001744 if( ! operation->is_sign )
1745 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001746 status = PSA_ERROR_BAD_STATE;
1747 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001748 }
mohammad16036df908f2018-04-02 08:34:15 -07001749
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001750 status = psa_mac_finish_internal( operation, mac, mac_size );
1751
1752cleanup:
1753 if( status == PSA_SUCCESS )
1754 {
1755 status = psa_mac_abort( operation );
1756 if( status == PSA_SUCCESS )
1757 *mac_length = operation->mac_size;
1758 else
1759 memset( mac, '!', mac_size );
1760 }
1761 else
1762 psa_mac_abort( operation );
1763 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001764}
1765
Gilles Peskineacd4be32018-07-08 19:56:25 +02001766psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1767 const uint8_t *mac,
1768 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001769{
Gilles Peskine828ed142018-06-18 23:25:51 +02001770 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001771 psa_status_t status;
1772
Gilles Peskine89167cb2018-07-08 20:12:23 +02001773 if( operation->is_sign )
1774 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001775 status = PSA_ERROR_BAD_STATE;
1776 goto cleanup;
1777 }
1778 if( operation->mac_size != mac_length )
1779 {
1780 status = PSA_ERROR_INVALID_SIGNATURE;
1781 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001782 }
mohammad16036df908f2018-04-02 08:34:15 -07001783
1784 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001785 actual_mac, sizeof( actual_mac ) );
1786
1787 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1788 status = PSA_ERROR_INVALID_SIGNATURE;
1789
1790cleanup:
1791 if( status == PSA_SUCCESS )
1792 status = psa_mac_abort( operation );
1793 else
1794 psa_mac_abort( operation );
1795
1796 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001797}
1798
1799
Gilles Peskine20035e32018-02-03 22:44:14 +01001800
Gilles Peskine20035e32018-02-03 22:44:14 +01001801/****************************************************************/
1802/* Asymmetric cryptography */
1803/****************************************************************/
1804
Gilles Peskine2b450e32018-06-27 15:42:46 +02001805#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001806/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001807 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001808static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1809 size_t hash_length,
1810 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001811{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001812 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001813 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001814 *md_alg = mbedtls_md_get_type( md_info );
1815
1816 /* The Mbed TLS RSA module uses an unsigned int for hash length
1817 * parameters. Validate that it fits so that we don't risk an
1818 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001819#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001820 if( hash_length > UINT_MAX )
1821 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001822#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001823
1824#if defined(MBEDTLS_PKCS1_V15)
1825 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
1826 * must be correct. */
1827 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
1828 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001829 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001830 if( md_info == NULL )
1831 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001832 if( mbedtls_md_get_size( md_info ) != hash_length )
1833 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001834 }
1835#endif /* MBEDTLS_PKCS1_V15 */
1836
1837#if defined(MBEDTLS_PKCS1_V21)
1838 /* PSS requires a hash internally. */
1839 if( PSA_ALG_IS_RSA_PSS( alg ) )
1840 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001841 if( md_info == NULL )
1842 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001843 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001844#endif /* MBEDTLS_PKCS1_V21 */
1845
Gilles Peskine61b91d42018-06-08 16:09:36 +02001846 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001847}
1848
Gilles Peskine2b450e32018-06-27 15:42:46 +02001849static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1850 psa_algorithm_t alg,
1851 const uint8_t *hash,
1852 size_t hash_length,
1853 uint8_t *signature,
1854 size_t signature_size,
1855 size_t *signature_length )
1856{
1857 psa_status_t status;
1858 int ret;
1859 mbedtls_md_type_t md_alg;
1860
1861 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1862 if( status != PSA_SUCCESS )
1863 return( status );
1864
Gilles Peskine630a18a2018-06-29 17:49:35 +02001865 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001866 return( PSA_ERROR_BUFFER_TOO_SMALL );
1867
1868#if defined(MBEDTLS_PKCS1_V15)
1869 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1870 {
1871 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1872 MBEDTLS_MD_NONE );
1873 ret = mbedtls_rsa_pkcs1_sign( rsa,
1874 mbedtls_ctr_drbg_random,
1875 &global_data.ctr_drbg,
1876 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001877 md_alg,
1878 (unsigned int) hash_length,
1879 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001880 signature );
1881 }
1882 else
1883#endif /* MBEDTLS_PKCS1_V15 */
1884#if defined(MBEDTLS_PKCS1_V21)
1885 if( PSA_ALG_IS_RSA_PSS( alg ) )
1886 {
1887 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1888 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1889 mbedtls_ctr_drbg_random,
1890 &global_data.ctr_drbg,
1891 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001892 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001893 (unsigned int) hash_length,
1894 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001895 signature );
1896 }
1897 else
1898#endif /* MBEDTLS_PKCS1_V21 */
1899 {
1900 return( PSA_ERROR_INVALID_ARGUMENT );
1901 }
1902
1903 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02001904 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02001905 return( mbedtls_to_psa_error( ret ) );
1906}
1907
1908static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1909 psa_algorithm_t alg,
1910 const uint8_t *hash,
1911 size_t hash_length,
1912 const uint8_t *signature,
1913 size_t signature_length )
1914{
1915 psa_status_t status;
1916 int ret;
1917 mbedtls_md_type_t md_alg;
1918
1919 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1920 if( status != PSA_SUCCESS )
1921 return( status );
1922
Gilles Peskine630a18a2018-06-29 17:49:35 +02001923 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001924 return( PSA_ERROR_BUFFER_TOO_SMALL );
1925
1926#if defined(MBEDTLS_PKCS1_V15)
1927 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1928 {
1929 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1930 MBEDTLS_MD_NONE );
1931 ret = mbedtls_rsa_pkcs1_verify( rsa,
1932 mbedtls_ctr_drbg_random,
1933 &global_data.ctr_drbg,
1934 MBEDTLS_RSA_PUBLIC,
1935 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001936 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001937 hash,
1938 signature );
1939 }
1940 else
1941#endif /* MBEDTLS_PKCS1_V15 */
1942#if defined(MBEDTLS_PKCS1_V21)
1943 if( PSA_ALG_IS_RSA_PSS( alg ) )
1944 {
1945 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1946 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1947 mbedtls_ctr_drbg_random,
1948 &global_data.ctr_drbg,
1949 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001950 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001951 (unsigned int) hash_length,
1952 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001953 signature );
1954 }
1955 else
1956#endif /* MBEDTLS_PKCS1_V21 */
1957 {
1958 return( PSA_ERROR_INVALID_ARGUMENT );
1959 }
1960 return( mbedtls_to_psa_error( ret ) );
1961}
1962#endif /* MBEDTLS_RSA_C */
1963
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001964#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001965/* `ecp` cannot be const because `ecp->grp` needs to be non-const
1966 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
1967 * (even though these functions don't modify it). */
1968static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
1969 psa_algorithm_t alg,
1970 const uint8_t *hash,
1971 size_t hash_length,
1972 uint8_t *signature,
1973 size_t signature_size,
1974 size_t *signature_length )
1975{
1976 int ret;
1977 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001978 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001979 mbedtls_mpi_init( &r );
1980 mbedtls_mpi_init( &s );
1981
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001982 if( signature_size < 2 * curve_bytes )
1983 {
1984 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
1985 goto cleanup;
1986 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001987
1988 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
1989 {
1990 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
1991 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1992 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
1993 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
1994 hash, hash_length,
1995 md_alg ) );
1996 }
1997 else
1998 {
1999 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2000 hash, hash_length,
2001 mbedtls_ctr_drbg_random,
2002 &global_data.ctr_drbg ) );
2003 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002004
2005 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2006 signature,
2007 curve_bytes ) );
2008 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2009 signature + curve_bytes,
2010 curve_bytes ) );
2011
2012cleanup:
2013 mbedtls_mpi_free( &r );
2014 mbedtls_mpi_free( &s );
2015 if( ret == 0 )
2016 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002017 return( mbedtls_to_psa_error( ret ) );
2018}
2019
2020static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2021 const uint8_t *hash,
2022 size_t hash_length,
2023 const uint8_t *signature,
2024 size_t signature_length )
2025{
2026 int ret;
2027 mbedtls_mpi r, s;
2028 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2029 mbedtls_mpi_init( &r );
2030 mbedtls_mpi_init( &s );
2031
2032 if( signature_length != 2 * curve_bytes )
2033 return( PSA_ERROR_INVALID_SIGNATURE );
2034
2035 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2036 signature,
2037 curve_bytes ) );
2038 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2039 signature + curve_bytes,
2040 curve_bytes ) );
2041
2042 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2043 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002044
2045cleanup:
2046 mbedtls_mpi_free( &r );
2047 mbedtls_mpi_free( &s );
2048 return( mbedtls_to_psa_error( ret ) );
2049}
2050#endif /* MBEDTLS_ECDSA_C */
2051
Gilles Peskine61b91d42018-06-08 16:09:36 +02002052psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2053 psa_algorithm_t alg,
2054 const uint8_t *hash,
2055 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002056 uint8_t *signature,
2057 size_t signature_size,
2058 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002059{
2060 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002061 psa_status_t status;
2062
2063 *signature_length = signature_size;
2064
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002065 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2066 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002067 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002068 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002069 {
2070 status = PSA_ERROR_INVALID_ARGUMENT;
2071 goto exit;
2072 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002073
Gilles Peskine20035e32018-02-03 22:44:14 +01002074#if defined(MBEDTLS_RSA_C)
2075 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2076 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002077 status = psa_rsa_sign( slot->data.rsa,
2078 alg,
2079 hash, hash_length,
2080 signature, signature_size,
2081 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002082 }
2083 else
2084#endif /* defined(MBEDTLS_RSA_C) */
2085#if defined(MBEDTLS_ECP_C)
2086 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2087 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002088#if defined(MBEDTLS_ECDSA_C)
2089 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002090 status = psa_ecdsa_sign( slot->data.ecp,
2091 alg,
2092 hash, hash_length,
2093 signature, signature_size,
2094 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002095 else
2096#endif /* defined(MBEDTLS_ECDSA_C) */
2097 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002098 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002099 }
itayzafrir5c753392018-05-08 11:18:38 +03002100 }
2101 else
2102#endif /* defined(MBEDTLS_ECP_C) */
2103 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002104 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002105 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002106
2107exit:
2108 /* Fill the unused part of the output buffer (the whole buffer on error,
2109 * the trailing part on success) with something that isn't a valid mac
2110 * (barring an attack on the mac and deliberately-crafted input),
2111 * in case the caller doesn't check the return status properly. */
2112 if( status == PSA_SUCCESS )
2113 memset( signature + *signature_length, '!',
2114 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002115 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002116 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002117 /* If signature_size is 0 then we have nothing to do. We must not call
2118 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002119 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002120}
2121
2122psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2123 psa_algorithm_t alg,
2124 const uint8_t *hash,
2125 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002126 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002127 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002128{
2129 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002130 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002131
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002132 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2133 if( status != PSA_SUCCESS )
2134 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002135
Gilles Peskine61b91d42018-06-08 16:09:36 +02002136#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002137 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002138 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002139 return( psa_rsa_verify( slot->data.rsa,
2140 alg,
2141 hash, hash_length,
2142 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002143 }
2144 else
2145#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002146#if defined(MBEDTLS_ECP_C)
2147 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2148 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002149#if defined(MBEDTLS_ECDSA_C)
2150 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002151 return( psa_ecdsa_verify( slot->data.ecp,
2152 hash, hash_length,
2153 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002154 else
2155#endif /* defined(MBEDTLS_ECDSA_C) */
2156 {
2157 return( PSA_ERROR_INVALID_ARGUMENT );
2158 }
itayzafrir5c753392018-05-08 11:18:38 +03002159 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002160 else
2161#endif /* defined(MBEDTLS_ECP_C) */
2162 {
2163 return( PSA_ERROR_NOT_SUPPORTED );
2164 }
2165}
2166
Gilles Peskine072ac562018-06-30 00:21:29 +02002167#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2168static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2169 mbedtls_rsa_context *rsa )
2170{
2171 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2172 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2173 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2174 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2175}
2176#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2177
Gilles Peskine61b91d42018-06-08 16:09:36 +02002178psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2179 psa_algorithm_t alg,
2180 const uint8_t *input,
2181 size_t input_length,
2182 const uint8_t *salt,
2183 size_t salt_length,
2184 uint8_t *output,
2185 size_t output_size,
2186 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002187{
2188 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002189 psa_status_t status;
2190
Darryl Green5cc689a2018-07-24 15:34:10 +01002191 (void) input;
2192 (void) input_length;
2193 (void) salt;
2194 (void) output;
2195 (void) output_size;
2196
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002197 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002198
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002199 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2200 return( PSA_ERROR_INVALID_ARGUMENT );
2201
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002202 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2203 if( status != PSA_SUCCESS )
2204 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002205 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2206 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002207 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002208
2209#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002210 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002211 {
2212 mbedtls_rsa_context *rsa = slot->data.rsa;
2213 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002214 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002215 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002216#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002217 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002218 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002219 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2220 mbedtls_ctr_drbg_random,
2221 &global_data.ctr_drbg,
2222 MBEDTLS_RSA_PUBLIC,
2223 input_length,
2224 input,
2225 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002226 }
2227 else
2228#endif /* MBEDTLS_PKCS1_V15 */
2229#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002230 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002231 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002232 psa_rsa_oaep_set_padding_mode( alg, rsa );
2233 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2234 mbedtls_ctr_drbg_random,
2235 &global_data.ctr_drbg,
2236 MBEDTLS_RSA_PUBLIC,
2237 salt, salt_length,
2238 input_length,
2239 input,
2240 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002241 }
2242 else
2243#endif /* MBEDTLS_PKCS1_V21 */
2244 {
2245 return( PSA_ERROR_INVALID_ARGUMENT );
2246 }
2247 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002248 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002249 return( mbedtls_to_psa_error( ret ) );
2250 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002251 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002252#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002253 {
2254 return( PSA_ERROR_NOT_SUPPORTED );
2255 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002256}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002257
Gilles Peskine61b91d42018-06-08 16:09:36 +02002258psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2259 psa_algorithm_t alg,
2260 const uint8_t *input,
2261 size_t input_length,
2262 const uint8_t *salt,
2263 size_t salt_length,
2264 uint8_t *output,
2265 size_t output_size,
2266 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002267{
2268 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002269 psa_status_t status;
2270
Darryl Green5cc689a2018-07-24 15:34:10 +01002271 (void) input;
2272 (void) input_length;
2273 (void) salt;
2274 (void) output;
2275 (void) output_size;
2276
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002277 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002278
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002279 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2280 return( PSA_ERROR_INVALID_ARGUMENT );
2281
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002282 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2283 if( status != PSA_SUCCESS )
2284 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002285 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2286 return( PSA_ERROR_INVALID_ARGUMENT );
2287
2288#if defined(MBEDTLS_RSA_C)
2289 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2290 {
2291 mbedtls_rsa_context *rsa = slot->data.rsa;
2292 int ret;
2293
Gilles Peskine630a18a2018-06-29 17:49:35 +02002294 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002295 return( PSA_ERROR_INVALID_ARGUMENT );
2296
2297#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002298 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002299 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002300 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2301 mbedtls_ctr_drbg_random,
2302 &global_data.ctr_drbg,
2303 MBEDTLS_RSA_PRIVATE,
2304 output_length,
2305 input,
2306 output,
2307 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002308 }
2309 else
2310#endif /* MBEDTLS_PKCS1_V15 */
2311#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002312 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002313 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002314 psa_rsa_oaep_set_padding_mode( alg, rsa );
2315 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2316 mbedtls_ctr_drbg_random,
2317 &global_data.ctr_drbg,
2318 MBEDTLS_RSA_PRIVATE,
2319 salt, salt_length,
2320 output_length,
2321 input,
2322 output,
2323 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002324 }
2325 else
2326#endif /* MBEDTLS_PKCS1_V21 */
2327 {
2328 return( PSA_ERROR_INVALID_ARGUMENT );
2329 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002330
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002331 return( mbedtls_to_psa_error( ret ) );
2332 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002333 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002334#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002335 {
2336 return( PSA_ERROR_NOT_SUPPORTED );
2337 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002338}
Gilles Peskine20035e32018-02-03 22:44:14 +01002339
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002340
2341
mohammad1603503973b2018-03-12 15:59:30 +02002342/****************************************************************/
2343/* Symmetric cryptography */
2344/****************************************************************/
2345
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002346/* Initialize the cipher operation structure. Once this function has been
2347 * called, psa_cipher_abort can run and will do the right thing. */
2348static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2349 psa_algorithm_t alg )
2350{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002351 if( ! PSA_ALG_IS_CIPHER( alg ) )
2352 {
2353 memset( operation, 0, sizeof( *operation ) );
2354 return( PSA_ERROR_INVALID_ARGUMENT );
2355 }
2356
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002357 operation->alg = alg;
2358 operation->key_set = 0;
2359 operation->iv_set = 0;
2360 operation->iv_required = 1;
2361 operation->iv_size = 0;
2362 operation->block_size = 0;
2363 mbedtls_cipher_init( &operation->ctx.cipher );
2364 return( PSA_SUCCESS );
2365}
2366
Gilles Peskinee553c652018-06-04 16:22:46 +02002367static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2368 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002369 psa_algorithm_t alg,
2370 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002371{
2372 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2373 psa_status_t status;
2374 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002375 size_t key_bits;
2376 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002377 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2378 PSA_KEY_USAGE_ENCRYPT :
2379 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002380
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002381 status = psa_cipher_init( operation, alg );
2382 if( status != PSA_SUCCESS )
2383 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002384
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002385 status = psa_get_key_from_slot( key, &slot, usage, alg);
2386 if( status != PSA_SUCCESS )
2387 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002388 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002389
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002390 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002391 if( cipher_info == NULL )
2392 return( PSA_ERROR_NOT_SUPPORTED );
2393
mohammad1603503973b2018-03-12 15:59:30 +02002394 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002395 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002396 {
2397 psa_cipher_abort( operation );
2398 return( mbedtls_to_psa_error( ret ) );
2399 }
2400
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002401#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002402 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002403 {
2404 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2405 unsigned char keys[24];
2406 memcpy( keys, slot->data.raw.data, 16 );
2407 memcpy( keys + 16, slot->data.raw.data, 8 );
2408 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2409 keys,
2410 192, cipher_operation );
2411 }
2412 else
2413#endif
2414 {
2415 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2416 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002417 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002418 }
Moran Peker41deec42018-04-04 15:43:05 +03002419 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002420 {
2421 psa_cipher_abort( operation );
2422 return( mbedtls_to_psa_error( ret ) );
2423 }
2424
mohammad16038481e742018-03-18 13:57:31 +02002425#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002426 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002427 {
Gilles Peskine53514202018-06-06 15:11:46 +02002428 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2429 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002430
Moran Pekera28258c2018-05-29 16:25:04 +03002431 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002432 {
2433 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2434 mode = MBEDTLS_PADDING_PKCS7;
2435 break;
2436 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2437 mode = MBEDTLS_PADDING_NONE;
2438 break;
2439 default:
Moran Pekerae382792018-05-31 14:06:17 +03002440 psa_cipher_abort( operation );
2441 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002442 }
2443 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002444 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002445 {
2446 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002447 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002448 }
mohammad16038481e742018-03-18 13:57:31 +02002449 }
2450#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2451
mohammad1603503973b2018-03-12 15:59:30 +02002452 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002453 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002454 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) :
Gilles Peskine7e928852018-06-04 16:23:10 +02002455 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002456 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002457 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002458 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002459 }
mohammad1603503973b2018-03-12 15:59:30 +02002460
Moran Peker395db872018-05-31 14:07:14 +03002461 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002462}
2463
Gilles Peskinefe119512018-07-08 21:39:34 +02002464psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2465 psa_key_slot_t key,
2466 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002467{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002468 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002469}
2470
Gilles Peskinefe119512018-07-08 21:39:34 +02002471psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2472 psa_key_slot_t key,
2473 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002474{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002475 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002476}
2477
Gilles Peskinefe119512018-07-08 21:39:34 +02002478psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2479 unsigned char *iv,
2480 size_t iv_size,
2481 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002482{
itayzafrir534bd7c2018-08-02 13:56:32 +03002483 psa_status_t status;
2484 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002485 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002486 {
2487 status = PSA_ERROR_BAD_STATE;
2488 goto exit;
2489 }
Moran Peker41deec42018-04-04 15:43:05 +03002490 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002491 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002492 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002493 goto exit;
2494 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002495 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2496 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002497 if( ret != 0 )
2498 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002499 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002500 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002501 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002502
mohammad16038481e742018-03-18 13:57:31 +02002503 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002504 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002505
Moran Peker395db872018-05-31 14:07:14 +03002506exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002507 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002508 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002509 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002510}
2511
Gilles Peskinefe119512018-07-08 21:39:34 +02002512psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2513 const unsigned char *iv,
2514 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002515{
itayzafrir534bd7c2018-08-02 13:56:32 +03002516 psa_status_t status;
2517 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002518 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002519 {
2520 status = PSA_ERROR_BAD_STATE;
2521 goto exit;
2522 }
Moran Pekera28258c2018-05-29 16:25:04 +03002523 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002524 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002525 status = PSA_ERROR_INVALID_ARGUMENT;
2526 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002527 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002528 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2529 status = mbedtls_to_psa_error( ret );
2530exit:
2531 if( status == PSA_SUCCESS )
2532 operation->iv_set = 1;
2533 else
mohammad1603503973b2018-03-12 15:59:30 +02002534 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002535 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002536}
2537
Gilles Peskinee553c652018-06-04 16:22:46 +02002538psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2539 const uint8_t *input,
2540 size_t input_length,
2541 unsigned char *output,
2542 size_t output_size,
2543 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002544{
itayzafrir534bd7c2018-08-02 13:56:32 +03002545 psa_status_t status;
2546 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002547 size_t expected_output_size;
2548 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2549 {
2550 /* Take the unprocessed partial block left over from previous
2551 * update calls, if any, plus the input to this call. Remove
2552 * the last partial block, if any. You get the data that will be
2553 * output in this call. */
2554 expected_output_size =
2555 ( operation->ctx.cipher.unprocessed_len + input_length )
2556 / operation->block_size * operation->block_size;
2557 }
2558 else
2559 {
2560 expected_output_size = input_length;
2561 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002562
Gilles Peskine89d789c2018-06-04 17:17:16 +02002563 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002564 {
2565 status = PSA_ERROR_BUFFER_TOO_SMALL;
2566 goto exit;
2567 }
mohammad160382759612018-03-12 18:16:40 +02002568
mohammad1603503973b2018-03-12 15:59:30 +02002569 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002570 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002571 status = mbedtls_to_psa_error( ret );
2572exit:
2573 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002574 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002575 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002576}
2577
Gilles Peskinee553c652018-06-04 16:22:46 +02002578psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2579 uint8_t *output,
2580 size_t output_size,
2581 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002582{
Janos Follath315b51c2018-07-09 16:04:51 +01002583 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2584 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002585 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002586
mohammad1603503973b2018-03-12 15:59:30 +02002587 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002588 {
Janos Follath315b51c2018-07-09 16:04:51 +01002589 status = PSA_ERROR_BAD_STATE;
2590 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002591 }
2592 if( operation->iv_required && ! operation->iv_set )
2593 {
Janos Follath315b51c2018-07-09 16:04:51 +01002594 status = PSA_ERROR_BAD_STATE;
2595 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002596 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002597 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2598 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002599 {
Gilles Peskine53514202018-06-06 15:11:46 +02002600 psa_algorithm_t padding_mode =
2601 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002602 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002603 {
Janos Follath315b51c2018-07-09 16:04:51 +01002604 status = PSA_ERROR_TAMPERING_DETECTED;
2605 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002606 }
Gilles Peskine53514202018-06-06 15:11:46 +02002607 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002608 {
2609 if( operation->ctx.cipher.unprocessed_len != 0 )
2610 {
Janos Follath315b51c2018-07-09 16:04:51 +01002611 status = PSA_ERROR_INVALID_ARGUMENT;
2612 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002613 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002614 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002615 }
2616
Janos Follath315b51c2018-07-09 16:04:51 +01002617 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2618 temp_output_buffer,
2619 output_length );
2620 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002621 {
Janos Follath315b51c2018-07-09 16:04:51 +01002622 status = mbedtls_to_psa_error( cipher_ret );
2623 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002624 }
Janos Follath315b51c2018-07-09 16:04:51 +01002625
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002626 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002627 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002628 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002629 memcpy( output, temp_output_buffer, *output_length );
2630 else
2631 {
Janos Follath315b51c2018-07-09 16:04:51 +01002632 status = PSA_ERROR_BUFFER_TOO_SMALL;
2633 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002634 }
mohammad1603503973b2018-03-12 15:59:30 +02002635
Janos Follath279ab8e2018-07-09 16:13:21 +01002636 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002637 status = psa_cipher_abort( operation );
2638
2639 return( status );
2640
2641error:
2642
2643 *output_length = 0;
2644
Janos Follath279ab8e2018-07-09 16:13:21 +01002645 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002646 (void) psa_cipher_abort( operation );
2647
2648 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002649}
2650
Gilles Peskinee553c652018-06-04 16:22:46 +02002651psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2652{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002653 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002654 {
2655 /* The object has (apparently) been initialized but it is not
2656 * in use. It's ok to call abort on such an object, and there's
2657 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002658 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002659 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002660
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002661 /* Sanity check (shouldn't happen: operation->alg should
2662 * always have been initialized to a valid value). */
2663 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2664 return( PSA_ERROR_BAD_STATE );
2665
mohammad1603503973b2018-03-12 15:59:30 +02002666 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002667
Moran Peker41deec42018-04-04 15:43:05 +03002668 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002669 operation->key_set = 0;
2670 operation->iv_set = 0;
2671 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002672 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002673 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002674
Moran Peker395db872018-05-31 14:07:14 +03002675 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002676}
2677
Gilles Peskinea0655c32018-04-30 17:06:50 +02002678
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002679
mohammad16038cc1cee2018-03-28 01:21:33 +03002680/****************************************************************/
2681/* Key Policy */
2682/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002683
mohammad160327010052018-07-03 13:16:15 +03002684#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002685void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002686{
Gilles Peskine803ce742018-06-18 16:07:14 +02002687 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002688}
2689
Gilles Peskine2d277862018-06-18 15:41:12 +02002690void psa_key_policy_set_usage( psa_key_policy_t *policy,
2691 psa_key_usage_t usage,
2692 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002693{
mohammad16034eed7572018-03-28 05:14:59 -07002694 policy->usage = usage;
2695 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002696}
2697
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002698psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002699{
mohammad16036df908f2018-04-02 08:34:15 -07002700 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002701}
2702
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002703psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002704{
mohammad16036df908f2018-04-02 08:34:15 -07002705 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002706}
mohammad160327010052018-07-03 13:16:15 +03002707#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002708
Gilles Peskine2d277862018-06-18 15:41:12 +02002709psa_status_t psa_set_key_policy( psa_key_slot_t key,
2710 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002711{
2712 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002713 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002714
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002715 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002716 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002717
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002718 status = psa_get_empty_key_slot( key, &slot );
2719 if( status != PSA_SUCCESS )
2720 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002721
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002722 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2723 PSA_KEY_USAGE_ENCRYPT |
2724 PSA_KEY_USAGE_DECRYPT |
2725 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002726 PSA_KEY_USAGE_VERIFY |
2727 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002728 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002729
mohammad16036df908f2018-04-02 08:34:15 -07002730 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002731
2732 return( PSA_SUCCESS );
2733}
2734
Gilles Peskine2d277862018-06-18 15:41:12 +02002735psa_status_t psa_get_key_policy( psa_key_slot_t key,
2736 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002737{
2738 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002739 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002740
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002741 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002742 return( PSA_ERROR_INVALID_ARGUMENT );
2743
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002744 status = psa_get_key_slot( key, &slot );
2745 if( status != PSA_SUCCESS )
2746 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002747
mohammad16036df908f2018-04-02 08:34:15 -07002748 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002749
2750 return( PSA_SUCCESS );
2751}
Gilles Peskine20035e32018-02-03 22:44:14 +01002752
Gilles Peskinea0655c32018-04-30 17:06:50 +02002753
2754
mohammad1603804cd712018-03-20 22:44:08 +02002755/****************************************************************/
2756/* Key Lifetime */
2757/****************************************************************/
2758
Gilles Peskine2d277862018-06-18 15:41:12 +02002759psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2760 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002761{
2762 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002763 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002764
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002765 status = psa_get_key_slot( key, &slot );
2766 if( status != PSA_SUCCESS )
2767 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002768
mohammad1603804cd712018-03-20 22:44:08 +02002769 *lifetime = slot->lifetime;
2770
2771 return( PSA_SUCCESS );
2772}
2773
Gilles Peskine2d277862018-06-18 15:41:12 +02002774psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002775 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002776{
2777 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002778 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002779
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002780 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2781 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002782 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2783 return( PSA_ERROR_INVALID_ARGUMENT );
2784
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002785 status = psa_get_empty_key_slot( key, &slot );
2786 if( status != PSA_SUCCESS )
2787 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002788
Moran Pekerd7326592018-05-29 16:56:39 +03002789 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002790 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002791
mohammad1603060ad8a2018-03-20 14:28:38 -07002792 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002793
2794 return( PSA_SUCCESS );
2795}
2796
Gilles Peskine20035e32018-02-03 22:44:14 +01002797
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002798
mohammad16035955c982018-04-26 00:53:03 +03002799/****************************************************************/
2800/* AEAD */
2801/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002802
mohammad16035955c982018-04-26 00:53:03 +03002803psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2804 psa_algorithm_t alg,
2805 const uint8_t *nonce,
2806 size_t nonce_length,
2807 const uint8_t *additional_data,
2808 size_t additional_data_length,
2809 const uint8_t *plaintext,
2810 size_t plaintext_length,
2811 uint8_t *ciphertext,
2812 size_t ciphertext_size,
2813 size_t *ciphertext_length )
2814{
2815 int ret;
2816 psa_status_t status;
2817 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002818 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002819 uint8_t *tag;
2820 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002821 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002822 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002823
mohammad1603f08a5502018-06-03 15:05:47 +03002824 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002825
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002826 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2827 if( status != PSA_SUCCESS )
2828 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002829 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002830
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002831 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002832 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002833 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002834 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002835
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002836 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002837 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002838 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002839
mohammad16035955c982018-04-26 00:53:03 +03002840 if( alg == PSA_ALG_GCM )
2841 {
2842 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002843 tag_length = 16;
2844
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002845 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002846 return( PSA_ERROR_INVALID_ARGUMENT );
2847
mohammad160315223a82018-06-03 17:19:55 +03002848 //make sure we have place to hold the tag in the ciphertext buffer
2849 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002850 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002851
2852 //update the tag pointer to point to the end of the ciphertext_length
2853 tag = ciphertext + plaintext_length;
2854
mohammad16035955c982018-04-26 00:53:03 +03002855 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002856 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002857 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002858 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002859 if( ret != 0 )
2860 {
2861 mbedtls_gcm_free( &gcm );
2862 return( mbedtls_to_psa_error( ret ) );
2863 }
2864 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002865 plaintext_length, nonce,
2866 nonce_length, additional_data,
2867 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002868 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002869 mbedtls_gcm_free( &gcm );
2870 }
2871 else if( alg == PSA_ALG_CCM )
2872 {
2873 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002874 tag_length = 16;
2875
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002876 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002877 return( PSA_ERROR_INVALID_ARGUMENT );
2878
mohammad160347ddf3d2018-04-26 01:11:21 +03002879 if( nonce_length < 7 || nonce_length > 13 )
2880 return( PSA_ERROR_INVALID_ARGUMENT );
2881
mohammad160315223a82018-06-03 17:19:55 +03002882 //make sure we have place to hold the tag in the ciphertext buffer
2883 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002884 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002885
2886 //update the tag pointer to point to the end of the ciphertext_length
2887 tag = ciphertext + plaintext_length;
2888
mohammad16035955c982018-04-26 00:53:03 +03002889 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002890 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002891 slot->data.raw.data,
2892 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002893 if( ret != 0 )
2894 {
2895 mbedtls_ccm_free( &ccm );
2896 return( mbedtls_to_psa_error( ret ) );
2897 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002898 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002899 nonce, nonce_length,
2900 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002901 additional_data_length,
2902 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002903 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002904 mbedtls_ccm_free( &ccm );
2905 }
mohammad16035c8845f2018-05-09 05:40:09 -07002906 else
2907 {
mohammad1603554faad2018-06-03 15:07:38 +03002908 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002909 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002910
mohammad160315223a82018-06-03 17:19:55 +03002911 if( ret != 0 )
2912 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002913 /* If ciphertext_size is 0 then ciphertext may be NULL and then the
2914 * call to memset would have undefined behavior. */
2915 if( ciphertext_size != 0 )
2916 memset( ciphertext, 0, ciphertext_size );
mohammad160315223a82018-06-03 17:19:55 +03002917 return( mbedtls_to_psa_error( ret ) );
2918 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002919
mohammad160315223a82018-06-03 17:19:55 +03002920 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002921 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002922}
2923
Gilles Peskineee652a32018-06-01 19:23:52 +02002924/* Locate the tag in a ciphertext buffer containing the encrypted data
2925 * followed by the tag. Return the length of the part preceding the tag in
2926 * *plaintext_length. This is the size of the plaintext in modes where
2927 * the encrypted data has the same size as the plaintext, such as
2928 * CCM and GCM. */
2929static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2930 const uint8_t *ciphertext,
2931 size_t ciphertext_length,
2932 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002933 const uint8_t **p_tag )
2934{
2935 size_t payload_length;
2936 if( tag_length > ciphertext_length )
2937 return( PSA_ERROR_INVALID_ARGUMENT );
2938 payload_length = ciphertext_length - tag_length;
2939 if( payload_length > plaintext_size )
2940 return( PSA_ERROR_BUFFER_TOO_SMALL );
2941 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002942 return( PSA_SUCCESS );
2943}
2944
mohammad16035955c982018-04-26 00:53:03 +03002945psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2946 psa_algorithm_t alg,
2947 const uint8_t *nonce,
2948 size_t nonce_length,
2949 const uint8_t *additional_data,
2950 size_t additional_data_length,
2951 const uint8_t *ciphertext,
2952 size_t ciphertext_length,
2953 uint8_t *plaintext,
2954 size_t plaintext_size,
2955 size_t *plaintext_length )
2956{
2957 int ret;
2958 psa_status_t status;
2959 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002960 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002961 const uint8_t *tag;
2962 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002963 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002964 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002965
Gilles Peskineee652a32018-06-01 19:23:52 +02002966 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002967
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002968 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2969 if( status != PSA_SUCCESS )
2970 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002971 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002972
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002973 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002974 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002975 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002976 return( PSA_ERROR_NOT_SUPPORTED );
2977
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002978 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002979 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002980 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002981
mohammad16035955c982018-04-26 00:53:03 +03002982 if( alg == PSA_ALG_GCM )
2983 {
2984 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002985
Gilles Peskineee652a32018-06-01 19:23:52 +02002986 tag_length = 16;
2987 status = psa_aead_unpadded_locate_tag( tag_length,
2988 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002989 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002990 if( status != PSA_SUCCESS )
2991 return( status );
2992
mohammad16035955c982018-04-26 00:53:03 +03002993 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002994 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002995 slot->data.raw.data,
2996 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002997 if( ret != 0 )
2998 {
2999 mbedtls_gcm_free( &gcm );
3000 return( mbedtls_to_psa_error( ret ) );
3001 }
mohammad16035955c982018-04-26 00:53:03 +03003002
Gilles Peskineee652a32018-06-01 19:23:52 +02003003 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03003004 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003005 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03003006 additional_data,
3007 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003008 tag, tag_length,
3009 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03003010 mbedtls_gcm_free( &gcm );
3011 }
3012 else if( alg == PSA_ALG_CCM )
3013 {
3014 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02003015
mohammad160347ddf3d2018-04-26 01:11:21 +03003016 if( nonce_length < 7 || nonce_length > 13 )
3017 return( PSA_ERROR_INVALID_ARGUMENT );
3018
mohammad16039375f842018-06-03 14:28:24 +03003019 tag_length = 16;
3020 status = psa_aead_unpadded_locate_tag( tag_length,
3021 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003022 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003023 if( status != PSA_SUCCESS )
3024 return( status );
3025
mohammad16035955c982018-04-26 00:53:03 +03003026 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02003027 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003028 slot->data.raw.data,
3029 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03003030 if( ret != 0 )
3031 {
3032 mbedtls_ccm_free( &ccm );
3033 return( mbedtls_to_psa_error( ret ) );
3034 }
mohammad160360a64d02018-06-03 17:20:42 +03003035 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003036 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003037 additional_data,
3038 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003039 ciphertext, plaintext,
3040 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03003041 mbedtls_ccm_free( &ccm );
3042 }
mohammad160339574652018-06-01 04:39:53 -07003043 else
3044 {
mohammad1603554faad2018-06-03 15:07:38 +03003045 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003046 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003047
Gilles Peskineee652a32018-06-01 19:23:52 +02003048 if( ret != 0 )
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003049 {
3050 /* If plaintext_size is 0 then plaintext may be NULL and then the
3051 * call to memset has undefined behavior. */
3052 if( plaintext_size != 0 )
3053 memset( plaintext, 0, plaintext_size );
3054 }
mohammad160360a64d02018-06-03 17:20:42 +03003055 else
3056 *plaintext_length = ciphertext_length - tag_length;
3057
Gilles Peskineee652a32018-06-01 19:23:52 +02003058 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03003059}
3060
Gilles Peskinea0655c32018-04-30 17:06:50 +02003061
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003062
Gilles Peskine20035e32018-02-03 22:44:14 +01003063/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003064/* Generators */
3065/****************************************************************/
3066
3067psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3068{
3069 psa_status_t status = PSA_SUCCESS;
3070 if( generator->alg == 0 )
3071 {
3072 /* The object has (apparently) been initialized but it is not
3073 * in use. It's ok to call abort on such an object, and there's
3074 * nothing to do. */
3075 }
3076 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003077#if defined(MBEDTLS_MD_C)
3078 if( PSA_ALG_IS_HKDF( generator->alg ) )
3079 {
3080 mbedtls_free( generator->ctx.hkdf.info );
3081 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3082 }
3083 else
3084#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003085 {
3086 status = PSA_ERROR_BAD_STATE;
3087 }
3088 memset( generator, 0, sizeof( *generator ) );
3089 return( status );
3090}
3091
3092
3093psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3094 size_t *capacity)
3095{
3096 *capacity = generator->capacity;
3097 return( PSA_SUCCESS );
3098}
3099
Gilles Peskinebef7f142018-07-12 17:22:21 +02003100#if defined(MBEDTLS_MD_C)
3101/* Read some bytes from an HKDF-based generator. This performs a chunk
3102 * of the expand phase of the HKDF algorithm. */
3103static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3104 psa_algorithm_t hash_alg,
3105 uint8_t *output,
3106 size_t output_length )
3107{
3108 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3109 psa_status_t status;
3110
3111 while( output_length != 0 )
3112 {
3113 /* Copy what remains of the current block */
3114 uint8_t n = hash_length - hkdf->offset_in_block;
3115 if( n > output_length )
3116 n = (uint8_t) output_length;
3117 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3118 output += n;
3119 output_length -= n;
3120 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003121 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003122 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003123 /* We can't be wanting more output after block 0xff, otherwise
3124 * the capacity check in psa_generator_read() would have
3125 * prevented this call. It could happen only if the generator
3126 * object was corrupted or if this function is called directly
3127 * inside the library. */
3128 if( hkdf->block_number == 0xff )
3129 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003130
3131 /* We need a new block */
3132 ++hkdf->block_number;
3133 hkdf->offset_in_block = 0;
3134 status = psa_hmac_setup_internal( &hkdf->hmac,
3135 hkdf->prk, hash_length,
3136 hash_alg );
3137 if( status != PSA_SUCCESS )
3138 return( status );
3139 if( hkdf->block_number != 1 )
3140 {
3141 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3142 hkdf->output_block,
3143 hash_length );
3144 if( status != PSA_SUCCESS )
3145 return( status );
3146 }
3147 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3148 hkdf->info,
3149 hkdf->info_length );
3150 if( status != PSA_SUCCESS )
3151 return( status );
3152 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3153 &hkdf->block_number, 1 );
3154 if( status != PSA_SUCCESS )
3155 return( status );
3156 status = psa_hmac_finish_internal( &hkdf->hmac,
3157 hkdf->output_block,
3158 sizeof( hkdf->output_block ) );
3159 if( status != PSA_SUCCESS )
3160 return( status );
3161 }
3162
3163 return( PSA_SUCCESS );
3164}
3165#endif /* MBEDTLS_MD_C */
3166
Gilles Peskineeab56e42018-07-12 17:12:33 +02003167psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3168 uint8_t *output,
3169 size_t output_length )
3170{
3171 psa_status_t status;
3172
3173 if( output_length > generator->capacity )
3174 {
3175 generator->capacity = 0;
3176 /* Go through the error path to wipe all confidential data now
3177 * that the generator object is useless. */
3178 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3179 goto exit;
3180 }
3181 if( output_length == 0 &&
3182 generator->capacity == 0 && generator->alg == 0 )
3183 {
3184 /* Edge case: this is a blank or finished generator, and 0
3185 * bytes were requested. The right error in this case could
3186 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3187 * INSUFFICIENT_CAPACITY, which is right for a finished
3188 * generator, for consistency with the case when
3189 * output_length > 0. */
3190 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3191 }
3192 generator->capacity -= output_length;
3193
Gilles Peskinebef7f142018-07-12 17:22:21 +02003194#if defined(MBEDTLS_MD_C)
3195 if( PSA_ALG_IS_HKDF( generator->alg ) )
3196 {
3197 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3198 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3199 output, output_length );
3200 }
3201 else
3202#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003203 {
3204 return( PSA_ERROR_BAD_STATE );
3205 }
3206
3207exit:
3208 if( status != PSA_SUCCESS )
3209 {
3210 psa_generator_abort( generator );
3211 memset( output, '!', output_length );
3212 }
3213 return( status );
3214}
3215
Gilles Peskine08542d82018-07-19 17:05:42 +02003216#if defined(MBEDTLS_DES_C)
3217static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3218{
3219 if( data_size >= 8 )
3220 mbedtls_des_key_set_parity( data );
3221 if( data_size >= 16 )
3222 mbedtls_des_key_set_parity( data + 8 );
3223 if( data_size >= 24 )
3224 mbedtls_des_key_set_parity( data + 16 );
3225}
3226#endif /* MBEDTLS_DES_C */
3227
Gilles Peskineeab56e42018-07-12 17:12:33 +02003228psa_status_t psa_generator_import_key( psa_key_slot_t key,
3229 psa_key_type_t type,
3230 size_t bits,
3231 psa_crypto_generator_t *generator )
3232{
3233 uint8_t *data = NULL;
3234 size_t bytes = PSA_BITS_TO_BYTES( bits );
3235 psa_status_t status;
3236
3237 if( ! key_type_is_raw_bytes( type ) )
3238 return( PSA_ERROR_INVALID_ARGUMENT );
3239 if( bits % 8 != 0 )
3240 return( PSA_ERROR_INVALID_ARGUMENT );
3241 data = mbedtls_calloc( 1, bytes );
3242 if( data == NULL )
3243 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3244
3245 status = psa_generator_read( generator, data, bytes );
3246 if( status != PSA_SUCCESS )
3247 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003248#if defined(MBEDTLS_DES_C)
3249 if( type == PSA_KEY_TYPE_DES )
3250 psa_des_set_key_parity( data, bytes );
3251#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003252 status = psa_import_key( key, type, data, bytes );
3253
3254exit:
3255 mbedtls_free( data );
3256 return( status );
3257}
3258
3259
3260
3261/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003262/* Key derivation */
3263/****************************************************************/
3264
Gilles Peskinebef7f142018-07-12 17:22:21 +02003265/* Set up an HKDF-based generator. This is exactly the extract phase
3266 * of the HKDF algorithm. */
3267static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
3268 key_slot_t *slot,
3269 psa_algorithm_t hash_alg,
3270 const uint8_t *salt,
3271 size_t salt_length,
3272 const uint8_t *label,
3273 size_t label_length )
3274{
3275 psa_status_t status;
3276 status = psa_hmac_setup_internal( &hkdf->hmac,
3277 salt, salt_length,
3278 PSA_ALG_HMAC_HASH( hash_alg ) );
3279 if( status != PSA_SUCCESS )
3280 return( status );
3281 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3282 slot->data.raw.data,
3283 slot->data.raw.bytes );
3284 if( status != PSA_SUCCESS )
3285 return( status );
3286 status = psa_hmac_finish_internal( &hkdf->hmac,
3287 hkdf->prk,
3288 sizeof( hkdf->prk ) );
3289 if( status != PSA_SUCCESS )
3290 return( status );
3291 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3292 hkdf->block_number = 0;
3293 hkdf->info_length = label_length;
3294 if( label_length != 0 )
3295 {
3296 hkdf->info = mbedtls_calloc( 1, label_length );
3297 if( hkdf->info == NULL )
3298 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3299 memcpy( hkdf->info, label, label_length );
3300 }
3301 return( PSA_SUCCESS );
3302}
3303
Gilles Peskineea0fb492018-07-12 17:17:20 +02003304psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01003305 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003306 psa_algorithm_t alg,
3307 const uint8_t *salt,
3308 size_t salt_length,
3309 const uint8_t *label,
3310 size_t label_length,
3311 size_t capacity )
3312{
3313 key_slot_t *slot;
3314 psa_status_t status;
3315
3316 if( generator->alg != 0 )
3317 return( PSA_ERROR_BAD_STATE );
3318
3319 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
3320 if( status != PSA_SUCCESS )
3321 return( status );
3322 if( slot->type != PSA_KEY_TYPE_DERIVE )
3323 return( PSA_ERROR_INVALID_ARGUMENT );
3324
3325 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
3326 return( PSA_ERROR_INVALID_ARGUMENT );
3327
Gilles Peskinebef7f142018-07-12 17:22:21 +02003328#if defined(MBEDTLS_MD_C)
3329 if( PSA_ALG_IS_HKDF( alg ) )
3330 {
3331 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3332 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3333 if( hash_size == 0 )
3334 return( PSA_ERROR_NOT_SUPPORTED );
3335 if( capacity > 255 * hash_size )
3336 return( PSA_ERROR_INVALID_ARGUMENT );
3337 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3338 slot,
3339 hash_alg,
3340 salt, salt_length,
3341 label, label_length );
3342 }
3343 else
3344#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +02003345 {
3346 return( PSA_ERROR_NOT_SUPPORTED );
3347 }
3348
3349 /* Set generator->alg even on failure so that abort knows what to do. */
3350 generator->alg = alg;
3351 if( status == PSA_SUCCESS )
3352 generator->capacity = capacity;
3353 else
3354 psa_generator_abort( generator );
3355 return( status );
3356}
3357
3358
3359
3360/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003361/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02003362/****************************************************************/
3363
3364psa_status_t psa_generate_random( uint8_t *output,
3365 size_t output_size )
3366{
itayzafrir0adf0fc2018-09-06 16:24:41 +03003367 int ret;
3368 GUARD_MODULE_INITIALIZED;
3369
3370 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02003371 return( mbedtls_to_psa_error( ret ) );
3372}
3373
3374psa_status_t psa_generate_key( psa_key_slot_t key,
3375 psa_key_type_t type,
3376 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003377 const void *extra,
3378 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02003379{
Gilles Peskine12313cd2018-06-20 00:20:32 +02003380 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003381 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003382
Gilles Peskine53d991e2018-07-12 01:14:59 +02003383 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003384 return( PSA_ERROR_INVALID_ARGUMENT );
3385
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003386 status = psa_get_empty_key_slot( key, &slot );
3387 if( status != PSA_SUCCESS )
3388 return( status );
3389
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003390 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003391 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003392 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003393 if( status != PSA_SUCCESS )
3394 return( status );
3395 status = psa_generate_random( slot->data.raw.data,
3396 slot->data.raw.bytes );
3397 if( status != PSA_SUCCESS )
3398 {
3399 mbedtls_free( slot->data.raw.data );
3400 return( status );
3401 }
3402#if defined(MBEDTLS_DES_C)
3403 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02003404 psa_des_set_key_parity( slot->data.raw.data,
3405 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003406#endif /* MBEDTLS_DES_C */
3407 }
3408 else
3409
3410#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3411 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3412 {
3413 mbedtls_rsa_context *rsa;
3414 int ret;
3415 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003416 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3417 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02003418 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003419 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02003420 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003421 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003422 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003423#if INT_MAX < 0xffffffff
3424 /* Check that the uint32_t value passed by the caller fits
3425 * in the range supported by this implementation. */
3426 if( p->e > INT_MAX )
3427 return( PSA_ERROR_NOT_SUPPORTED );
3428#endif
3429 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003430 }
3431 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3432 if( rsa == NULL )
3433 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3434 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3435 ret = mbedtls_rsa_gen_key( rsa,
3436 mbedtls_ctr_drbg_random,
3437 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003438 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003439 exponent );
3440 if( ret != 0 )
3441 {
3442 mbedtls_rsa_free( rsa );
3443 mbedtls_free( rsa );
3444 return( mbedtls_to_psa_error( ret ) );
3445 }
3446 slot->data.rsa = rsa;
3447 }
3448 else
3449#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3450
3451#if defined(MBEDTLS_ECP_C)
3452 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3453 {
3454 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3455 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3456 const mbedtls_ecp_curve_info *curve_info =
3457 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3458 mbedtls_ecp_keypair *ecp;
3459 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003460 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003461 return( PSA_ERROR_NOT_SUPPORTED );
3462 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3463 return( PSA_ERROR_NOT_SUPPORTED );
3464 if( curve_info->bit_size != bits )
3465 return( PSA_ERROR_INVALID_ARGUMENT );
3466 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3467 if( ecp == NULL )
3468 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3469 mbedtls_ecp_keypair_init( ecp );
3470 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3471 mbedtls_ctr_drbg_random,
3472 &global_data.ctr_drbg );
3473 if( ret != 0 )
3474 {
3475 mbedtls_ecp_keypair_free( ecp );
3476 mbedtls_free( ecp );
3477 return( mbedtls_to_psa_error( ret ) );
3478 }
3479 slot->data.ecp = ecp;
3480 }
3481 else
3482#endif /* MBEDTLS_ECP_C */
3483
3484 return( PSA_ERROR_NOT_SUPPORTED );
3485
3486 slot->type = type;
3487 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003488}
3489
3490
3491/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003492/* Module setup */
3493/****************************************************************/
3494
Gilles Peskinee59236f2018-01-27 23:32:46 +01003495void mbedtls_psa_crypto_free( void )
3496{
Jaeden Amero045bd502018-06-26 14:00:08 +01003497 psa_key_slot_t key;
Gilles Peskine9a056342018-08-01 15:46:54 +02003498 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003499 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003500 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3501 mbedtls_entropy_free( &global_data.entropy );
3502 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3503}
3504
3505psa_status_t psa_crypto_init( void )
3506{
3507 int ret;
3508 const unsigned char drbg_seed[] = "PSA";
3509
3510 if( global_data.initialized != 0 )
3511 return( PSA_SUCCESS );
3512
3513 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3514 mbedtls_entropy_init( &global_data.entropy );
3515 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3516
3517 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3518 mbedtls_entropy_func,
3519 &global_data.entropy,
3520 drbg_seed, sizeof( drbg_seed ) - 1 );
3521 if( ret != 0 )
3522 goto exit;
3523
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003524 global_data.initialized = 1;
3525
Gilles Peskinee59236f2018-01-27 23:32:46 +01003526exit:
3527 if( ret != 0 )
3528 mbedtls_psa_crypto_free( );
3529 return( mbedtls_to_psa_error( ret ) );
3530}
3531
3532#endif /* MBEDTLS_PSA_CRYPTO_C */