blob: 8b25dac1a32d7f163f9ae190127b722fbfb4f057 [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{
138 psa_key_type_t category = type & PSA_KEY_TYPE_CATEGORY_MASK;
139 return( category == PSA_KEY_TYPE_RAW_DATA ||
140 category == PSA_KEY_TYPE_CATEGORY_SYMMETRIC );
141}
142
Gilles Peskine2d277862018-06-18 15:41:12 +0200143typedef struct
144{
Gilles Peskinee59236f2018-01-27 23:32:46 +0100145 int initialized;
146 mbedtls_entropy_context entropy;
147 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200148 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinee59236f2018-01-27 23:32:46 +0100149} psa_global_data_t;
150
151static psa_global_data_t global_data;
152
153static psa_status_t mbedtls_to_psa_error( int ret )
154{
Gilles Peskinea5905292018-02-07 20:59:33 +0100155 /* If there's both a high-level code and low-level code, dispatch on
156 * the high-level code. */
157 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100158 {
159 case 0:
160 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100161
162 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
163 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
164 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
165 return( PSA_ERROR_NOT_SUPPORTED );
166 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
167 return( PSA_ERROR_HARDWARE_FAILURE );
168
169 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
170 return( PSA_ERROR_HARDWARE_FAILURE );
171
Gilles Peskine9a944802018-06-21 09:35:35 +0200172 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
173 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
174 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
175 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
176 case MBEDTLS_ERR_ASN1_INVALID_DATA:
177 return( PSA_ERROR_INVALID_ARGUMENT );
178 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
179 return( PSA_ERROR_INSUFFICIENT_MEMORY );
180 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
181 return( PSA_ERROR_BUFFER_TOO_SMALL );
182
Gilles Peskinea5905292018-02-07 20:59:33 +0100183 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
184 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
185 return( PSA_ERROR_NOT_SUPPORTED );
186 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
187 return( PSA_ERROR_HARDWARE_FAILURE );
188
189 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
190 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
191 return( PSA_ERROR_NOT_SUPPORTED );
192 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
193 return( PSA_ERROR_HARDWARE_FAILURE );
194
195 case MBEDTLS_ERR_CCM_BAD_INPUT:
196 return( PSA_ERROR_INVALID_ARGUMENT );
197 case MBEDTLS_ERR_CCM_AUTH_FAILED:
198 return( PSA_ERROR_INVALID_SIGNATURE );
199 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
200 return( PSA_ERROR_HARDWARE_FAILURE );
201
202 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
203 return( PSA_ERROR_NOT_SUPPORTED );
204 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
205 return( PSA_ERROR_INVALID_ARGUMENT );
206 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
207 return( PSA_ERROR_INSUFFICIENT_MEMORY );
208 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
209 return( PSA_ERROR_INVALID_PADDING );
210 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
211 return( PSA_ERROR_BAD_STATE );
212 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
213 return( PSA_ERROR_INVALID_SIGNATURE );
214 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
215 return( PSA_ERROR_TAMPERING_DETECTED );
216 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
217 return( PSA_ERROR_HARDWARE_FAILURE );
218
219 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
220 return( PSA_ERROR_HARDWARE_FAILURE );
221
222 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
223 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
224 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
225 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
226 return( PSA_ERROR_NOT_SUPPORTED );
227 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
228 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
229
230 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
231 return( PSA_ERROR_NOT_SUPPORTED );
232 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
233 return( PSA_ERROR_HARDWARE_FAILURE );
234
Gilles Peskinee59236f2018-01-27 23:32:46 +0100235 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
236 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
237 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
238 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100239
240 case MBEDTLS_ERR_GCM_AUTH_FAILED:
241 return( PSA_ERROR_INVALID_SIGNATURE );
242 case MBEDTLS_ERR_GCM_BAD_INPUT:
243 return( PSA_ERROR_NOT_SUPPORTED );
244 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
245 return( PSA_ERROR_HARDWARE_FAILURE );
246
247 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
248 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
249 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
250 return( PSA_ERROR_HARDWARE_FAILURE );
251
252 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
253 return( PSA_ERROR_NOT_SUPPORTED );
254 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
255 return( PSA_ERROR_INVALID_ARGUMENT );
256 case MBEDTLS_ERR_MD_ALLOC_FAILED:
257 return( PSA_ERROR_INSUFFICIENT_MEMORY );
258 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
259 return( PSA_ERROR_STORAGE_FAILURE );
260 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
261 return( PSA_ERROR_HARDWARE_FAILURE );
262
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100263 case MBEDTLS_ERR_PK_ALLOC_FAILED:
264 return( PSA_ERROR_INSUFFICIENT_MEMORY );
265 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
266 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100269 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100270 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
271 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
272 return( PSA_ERROR_INVALID_ARGUMENT );
273 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
274 return( PSA_ERROR_NOT_SUPPORTED );
275 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
276 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
277 return( PSA_ERROR_NOT_PERMITTED );
278 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
279 return( PSA_ERROR_INVALID_ARGUMENT );
280 case MBEDTLS_ERR_PK_INVALID_ALG:
281 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
282 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
283 return( PSA_ERROR_NOT_SUPPORTED );
284 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
285 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100286 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
287 return( PSA_ERROR_HARDWARE_FAILURE );
288
289 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
290 return( PSA_ERROR_HARDWARE_FAILURE );
291
292 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
293 return( PSA_ERROR_INVALID_ARGUMENT );
294 case MBEDTLS_ERR_RSA_INVALID_PADDING:
295 return( PSA_ERROR_INVALID_PADDING );
296 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
297 return( PSA_ERROR_HARDWARE_FAILURE );
298 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
299 return( PSA_ERROR_INVALID_ARGUMENT );
300 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
301 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
302 return( PSA_ERROR_TAMPERING_DETECTED );
303 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
304 return( PSA_ERROR_INVALID_SIGNATURE );
305 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
306 return( PSA_ERROR_BUFFER_TOO_SMALL );
307 case MBEDTLS_ERR_RSA_RNG_FAILED:
308 return( PSA_ERROR_INSUFFICIENT_MEMORY );
309 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
310 return( PSA_ERROR_NOT_SUPPORTED );
311 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
312 return( PSA_ERROR_HARDWARE_FAILURE );
313
314 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
315 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
316 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
317 return( PSA_ERROR_HARDWARE_FAILURE );
318
319 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
320 return( PSA_ERROR_INVALID_ARGUMENT );
321 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
322 return( PSA_ERROR_HARDWARE_FAILURE );
323
itayzafrir5c753392018-05-08 11:18:38 +0300324 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300325 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300326 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300327 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
328 return( PSA_ERROR_BUFFER_TOO_SMALL );
329 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
330 return( PSA_ERROR_NOT_SUPPORTED );
331 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
332 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
333 return( PSA_ERROR_INVALID_SIGNATURE );
334 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
335 return( PSA_ERROR_INSUFFICIENT_MEMORY );
336 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
337 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300338
Gilles Peskinee59236f2018-01-27 23:32:46 +0100339 default:
340 return( PSA_ERROR_UNKNOWN_ERROR );
341 }
342}
343
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200344/* Retrieve a key slot, occupied or not. */
345static psa_status_t psa_get_key_slot( psa_key_slot_t key,
346 key_slot_t **p_slot )
347{
Gilles Peskine996deb12018-08-01 15:45:45 +0200348 /* 0 is not a valid slot number under any circumstance. This
349 * implementation provides slots number 1 to N where N is the
350 * number of available slots. */
351 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200352 return( PSA_ERROR_INVALID_ARGUMENT );
353
Gilles Peskine996deb12018-08-01 15:45:45 +0200354 *p_slot = &global_data.key_slots[key - 1];
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200355 return( PSA_SUCCESS );
356}
357
358/* Retrieve an empty key slot (slot with no key data, but possibly
359 * with some metadata such as a policy). */
360static psa_status_t psa_get_empty_key_slot( psa_key_slot_t key,
361 key_slot_t **p_slot )
362{
363 psa_status_t status;
364 key_slot_t *slot = NULL;
365
366 *p_slot = NULL;
367
368 status = psa_get_key_slot( key, &slot );
369 if( status != PSA_SUCCESS )
370 return( status );
371
372 if( slot->type != PSA_KEY_TYPE_NONE )
373 return( PSA_ERROR_OCCUPIED_SLOT );
374
375 *p_slot = slot;
376 return( status );
377}
378
Jaeden Amerob4fa8c92018-07-11 15:57:44 +0100379/** Retrieve a slot which must contain a key. The key must have allow all the
380 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
381 * operations with this algorithm. */
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200382static psa_status_t psa_get_key_from_slot( psa_key_slot_t key,
383 key_slot_t **p_slot,
384 psa_key_usage_t usage,
385 psa_algorithm_t alg )
386{
387 psa_status_t status;
388 key_slot_t *slot = NULL;
389
390 *p_slot = NULL;
391
392 status = psa_get_key_slot( key, &slot );
393 if( status != PSA_SUCCESS )
394 return( status );
395 if( slot->type == PSA_KEY_TYPE_NONE )
396 return( PSA_ERROR_EMPTY_SLOT );
397
398 /* Enforce that usage policy for the key slot contains all the flags
399 * required by the usage parameter. There is one exception: public
400 * keys can always be exported, so we treat public key objects as
401 * if they had the export flag. */
402 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
403 usage &= ~PSA_KEY_USAGE_EXPORT;
404 if( ( slot->policy.usage & usage ) != usage )
405 return( PSA_ERROR_NOT_PERMITTED );
406 if( alg != 0 && ( alg != slot->policy.alg ) )
407 return( PSA_ERROR_NOT_PERMITTED );
408
409 *p_slot = slot;
410 return( PSA_SUCCESS );
411}
412
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200413
414
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100415/****************************************************************/
416/* Key management */
417/****************************************************************/
418
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200419static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
420{
421 switch( grpid )
422 {
423 case MBEDTLS_ECP_DP_SECP192R1:
424 return( PSA_ECC_CURVE_SECP192R1 );
425 case MBEDTLS_ECP_DP_SECP224R1:
426 return( PSA_ECC_CURVE_SECP224R1 );
427 case MBEDTLS_ECP_DP_SECP256R1:
428 return( PSA_ECC_CURVE_SECP256R1 );
429 case MBEDTLS_ECP_DP_SECP384R1:
430 return( PSA_ECC_CURVE_SECP384R1 );
431 case MBEDTLS_ECP_DP_SECP521R1:
432 return( PSA_ECC_CURVE_SECP521R1 );
433 case MBEDTLS_ECP_DP_BP256R1:
434 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
435 case MBEDTLS_ECP_DP_BP384R1:
436 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
437 case MBEDTLS_ECP_DP_BP512R1:
438 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
439 case MBEDTLS_ECP_DP_CURVE25519:
440 return( PSA_ECC_CURVE_CURVE25519 );
441 case MBEDTLS_ECP_DP_SECP192K1:
442 return( PSA_ECC_CURVE_SECP192K1 );
443 case MBEDTLS_ECP_DP_SECP224K1:
444 return( PSA_ECC_CURVE_SECP224K1 );
445 case MBEDTLS_ECP_DP_SECP256K1:
446 return( PSA_ECC_CURVE_SECP256K1 );
447 case MBEDTLS_ECP_DP_CURVE448:
448 return( PSA_ECC_CURVE_CURVE448 );
449 default:
450 return( 0 );
451 }
452}
453
Gilles Peskine12313cd2018-06-20 00:20:32 +0200454static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
455{
456 switch( curve )
457 {
458 case PSA_ECC_CURVE_SECP192R1:
459 return( MBEDTLS_ECP_DP_SECP192R1 );
460 case PSA_ECC_CURVE_SECP224R1:
461 return( MBEDTLS_ECP_DP_SECP224R1 );
462 case PSA_ECC_CURVE_SECP256R1:
463 return( MBEDTLS_ECP_DP_SECP256R1 );
464 case PSA_ECC_CURVE_SECP384R1:
465 return( MBEDTLS_ECP_DP_SECP384R1 );
466 case PSA_ECC_CURVE_SECP521R1:
467 return( MBEDTLS_ECP_DP_SECP521R1 );
468 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
469 return( MBEDTLS_ECP_DP_BP256R1 );
470 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
471 return( MBEDTLS_ECP_DP_BP384R1 );
472 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
473 return( MBEDTLS_ECP_DP_BP512R1 );
474 case PSA_ECC_CURVE_CURVE25519:
475 return( MBEDTLS_ECP_DP_CURVE25519 );
476 case PSA_ECC_CURVE_SECP192K1:
477 return( MBEDTLS_ECP_DP_SECP192K1 );
478 case PSA_ECC_CURVE_SECP224K1:
479 return( MBEDTLS_ECP_DP_SECP224K1 );
480 case PSA_ECC_CURVE_SECP256K1:
481 return( MBEDTLS_ECP_DP_SECP256K1 );
482 case PSA_ECC_CURVE_CURVE448:
483 return( MBEDTLS_ECP_DP_CURVE448 );
484 default:
485 return( MBEDTLS_ECP_DP_NONE );
486 }
487}
488
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200489static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
490 size_t bits,
491 struct raw_data *raw )
492{
493 /* Check that the bit size is acceptable for the key type */
494 switch( type )
495 {
496 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200497 if( bits == 0 )
498 {
499 raw->bytes = 0;
500 raw->data = NULL;
501 return( PSA_SUCCESS );
502 }
503 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200504#if defined(MBEDTLS_MD_C)
505 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200506#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200507 case PSA_KEY_TYPE_DERIVE:
508 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200509#if defined(MBEDTLS_AES_C)
510 case PSA_KEY_TYPE_AES:
511 if( bits != 128 && bits != 192 && bits != 256 )
512 return( PSA_ERROR_INVALID_ARGUMENT );
513 break;
514#endif
515#if defined(MBEDTLS_CAMELLIA_C)
516 case PSA_KEY_TYPE_CAMELLIA:
517 if( bits != 128 && bits != 192 && bits != 256 )
518 return( PSA_ERROR_INVALID_ARGUMENT );
519 break;
520#endif
521#if defined(MBEDTLS_DES_C)
522 case PSA_KEY_TYPE_DES:
523 if( bits != 64 && bits != 128 && bits != 192 )
524 return( PSA_ERROR_INVALID_ARGUMENT );
525 break;
526#endif
527#if defined(MBEDTLS_ARC4_C)
528 case PSA_KEY_TYPE_ARC4:
529 if( bits < 8 || bits > 2048 )
530 return( PSA_ERROR_INVALID_ARGUMENT );
531 break;
532#endif
533 default:
534 return( PSA_ERROR_NOT_SUPPORTED );
535 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200536 if( bits % 8 != 0 )
537 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200538
539 /* Allocate memory for the key */
540 raw->bytes = PSA_BITS_TO_BYTES( bits );
541 raw->data = mbedtls_calloc( 1, raw->bytes );
542 if( raw->data == NULL )
543 {
544 raw->bytes = 0;
545 return( PSA_ERROR_INSUFFICIENT_MEMORY );
546 }
547 return( PSA_SUCCESS );
548}
549
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200550#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
551static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
552 mbedtls_rsa_context **p_rsa )
553{
554 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
555 return( PSA_ERROR_INVALID_ARGUMENT );
556 else
557 {
558 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
559 size_t bits = mbedtls_rsa_get_bitlen( rsa );
560 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
561 return( PSA_ERROR_NOT_SUPPORTED );
562 *p_rsa = rsa;
563 return( PSA_SUCCESS );
564 }
565}
566#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
567
568#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
569static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
570 mbedtls_pk_context *pk,
571 mbedtls_ecp_keypair **p_ecp )
572{
573 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
574 return( PSA_ERROR_INVALID_ARGUMENT );
575 else
576 {
577 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
578 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
579 if( actual_curve != expected_curve )
580 return( PSA_ERROR_INVALID_ARGUMENT );
581 *p_ecp = ecp;
582 return( PSA_SUCCESS );
583 }
584}
585#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
586
Gilles Peskine2d277862018-06-18 15:41:12 +0200587psa_status_t psa_import_key( psa_key_slot_t key,
588 psa_key_type_t type,
589 const uint8_t *data,
590 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100591{
592 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200593 psa_status_t status = PSA_SUCCESS;
594 status = psa_get_empty_key_slot( key, &slot );
595 if( status != PSA_SUCCESS )
596 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100597
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200598 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100599 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100600 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100601 if( data_length > SIZE_MAX / 8 )
602 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200603 status = prepare_raw_data_slot( type,
604 PSA_BYTES_TO_BITS( data_length ),
605 &slot->data.raw );
606 if( status != PSA_SUCCESS )
607 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200608 if( data_length != 0 )
609 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100610 }
611 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100612#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200613 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100614 {
615 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100616 mbedtls_pk_context pk;
617 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200618
619 /* Parse the data. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100620 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
621 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
622 else
623 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100624 if( ret != 0 )
625 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200626
627 /* We have something that the pkparse module recognizes.
628 * If it has the expected type and passes any type-specific
629 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100630#if defined(MBEDTLS_RSA_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200631 if( PSA_KEY_TYPE_IS_RSA( type ) )
632 status = psa_import_rsa_key( &pk, &slot->data.rsa );
633 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100634#endif /* MBEDTLS_RSA_C */
635#if defined(MBEDTLS_ECP_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200636 if( PSA_KEY_TYPE_IS_ECC( type ) )
637 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( type ),
638 &pk, &slot->data.ecp );
639 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100640#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200641 {
642 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200643 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200644
Gilles Peskinec648d692018-06-28 08:46:13 +0200645 /* Free the content of the pk object only on error. On success,
646 * the content of the object has been stored in the slot. */
647 if( status != PSA_SUCCESS )
648 {
649 mbedtls_pk_free( &pk );
650 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100651 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100652 }
653 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100654#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100655 {
656 return( PSA_ERROR_NOT_SUPPORTED );
657 }
658
659 slot->type = type;
660 return( PSA_SUCCESS );
661}
662
Gilles Peskine2d277862018-06-18 15:41:12 +0200663psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100664{
665 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200666 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100667
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200668 status = psa_get_key_slot( key, &slot );
669 if( status != PSA_SUCCESS )
670 return( status );
671
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100672 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200673 {
674 /* No key material to clean, but do zeroize the slot below to wipe
675 * metadata such as policies. */
676 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200677 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678 {
679 mbedtls_free( slot->data.raw.data );
680 }
681 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100682#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200683 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100684 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100685 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100686 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100687 }
688 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100689#endif /* defined(MBEDTLS_RSA_C) */
690#if defined(MBEDTLS_ECP_C)
691 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
692 {
693 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100694 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100695 }
696 else
697#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100698 {
699 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100700 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100701 return( PSA_ERROR_TAMPERING_DETECTED );
702 }
703
704 mbedtls_zeroize( slot, sizeof( *slot ) );
705 return( PSA_SUCCESS );
706}
707
Gilles Peskineb870b182018-07-06 16:02:09 +0200708/* Return the size of the key in the given slot, in bits. */
709static size_t psa_get_key_bits( const key_slot_t *slot )
710{
711 if( key_type_is_raw_bytes( slot->type ) )
712 return( slot->data.raw.bytes * 8 );
713#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200714 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineb870b182018-07-06 16:02:09 +0200715 return( mbedtls_rsa_get_bitlen( slot->data.rsa ) );
716#endif /* defined(MBEDTLS_RSA_C) */
717#if defined(MBEDTLS_ECP_C)
718 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
719 return( slot->data.ecp->grp.pbits );
720#endif /* defined(MBEDTLS_ECP_C) */
721 /* Shouldn't happen except on an empty slot. */
722 return( 0 );
723}
724
Gilles Peskine2d277862018-06-18 15:41:12 +0200725psa_status_t psa_get_key_information( psa_key_slot_t key,
726 psa_key_type_t *type,
727 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100728{
729 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200730 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100731
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100732 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200733 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100734 if( bits != NULL )
735 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200736 status = psa_get_key_slot( key, &slot );
737 if( status != PSA_SUCCESS )
738 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200739
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740 if( slot->type == PSA_KEY_TYPE_NONE )
741 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200742 if( type != NULL )
743 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200744 if( bits != NULL )
745 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746 return( PSA_SUCCESS );
747}
748
Gilles Peskine2d277862018-06-18 15:41:12 +0200749static psa_status_t psa_internal_export_key( psa_key_slot_t key,
750 uint8_t *data,
751 size_t data_size,
752 size_t *data_length,
753 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100754{
755 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200756 psa_status_t status;
757 /* Exporting a public key doesn't require a usage flag. If we're
758 * called by psa_export_public_key(), don't require the EXPORT flag.
759 * If we're called by psa_export_key(), do require the EXPORT flag;
760 * if the key turns out to be public key object, psa_get_key_from_slot()
761 * will ignore this flag. */
762 psa_key_usage_t usage = export_public_key ? 0 : PSA_KEY_USAGE_EXPORT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100763
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100764 /* Set the key to empty now, so that even when there are errors, we always
765 * set data_length to a value between 0 and data_size. On error, setting
766 * the key to empty is a good choice because an empty key representation is
767 * unlikely to be accepted anywhere. */
768 *data_length = 0;
769
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200770 status = psa_get_key_from_slot( key, &slot, usage, 0 );
771 if( status != PSA_SUCCESS )
772 return( status );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200773 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300774 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300775
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200776 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100777 {
778 if( slot->data.raw.bytes > data_size )
779 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200780 if( slot->data.raw.bytes != 0 )
781 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100782 *data_length = slot->data.raw.bytes;
783 return( PSA_SUCCESS );
784 }
785 else
Moran Peker17e36e12018-05-02 12:55:20 +0300786 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100787#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200788 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300789 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100790 {
Moran Pekera998bc62018-04-16 18:16:20 +0300791 mbedtls_pk_context pk;
792 int ret;
793 mbedtls_pk_init( &pk );
Gilles Peskined8008d62018-06-29 19:51:51 +0200794 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300795 {
796 pk.pk_info = &mbedtls_rsa_info;
797 pk.pk_ctx = slot->data.rsa;
798 }
799 else
800 {
801 pk.pk_info = &mbedtls_eckey_info;
802 pk.pk_ctx = slot->data.ecp;
803 }
Moran Pekerd7326592018-05-29 16:56:39 +0300804 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300805 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300806 else
807 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300808 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200809 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200810 /* If data_size is 0 then data may be NULL and then the
811 * call to memset would have undefined behavior. */
812 if( data_size != 0 )
813 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300814 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200815 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200816 /* The mbedtls_pk_xxx functions write to the end of the buffer.
817 * Move the data to the beginning and erase remaining data
818 * at the original location. */
819 if( 2 * (size_t) ret <= data_size )
820 {
821 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200822 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200823 }
824 else if( (size_t) ret < data_size )
825 {
826 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200827 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200828 }
Moran Pekera998bc62018-04-16 18:16:20 +0300829 *data_length = ret;
830 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100831 }
832 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100833#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300834 {
835 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200836 it is valid for a special-purpose implementation to omit
837 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300838 return( PSA_ERROR_NOT_SUPPORTED );
839 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100840 }
841}
842
Gilles Peskine2d277862018-06-18 15:41:12 +0200843psa_status_t psa_export_key( psa_key_slot_t key,
844 uint8_t *data,
845 size_t data_size,
846 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300847{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200848 return( psa_internal_export_key( key, data, data_size,
849 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100850}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100851
Gilles Peskine2d277862018-06-18 15:41:12 +0200852psa_status_t psa_export_public_key( psa_key_slot_t key,
853 uint8_t *data,
854 size_t data_size,
855 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300856{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200857 return( psa_internal_export_key( key, data, data_size,
858 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300859}
860
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200861
862
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100863/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100864/* Message digests */
865/****************************************************************/
866
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100867static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100868{
869 switch( alg )
870 {
871#if defined(MBEDTLS_MD2_C)
872 case PSA_ALG_MD2:
873 return( &mbedtls_md2_info );
874#endif
875#if defined(MBEDTLS_MD4_C)
876 case PSA_ALG_MD4:
877 return( &mbedtls_md4_info );
878#endif
879#if defined(MBEDTLS_MD5_C)
880 case PSA_ALG_MD5:
881 return( &mbedtls_md5_info );
882#endif
883#if defined(MBEDTLS_RIPEMD160_C)
884 case PSA_ALG_RIPEMD160:
885 return( &mbedtls_ripemd160_info );
886#endif
887#if defined(MBEDTLS_SHA1_C)
888 case PSA_ALG_SHA_1:
889 return( &mbedtls_sha1_info );
890#endif
891#if defined(MBEDTLS_SHA256_C)
892 case PSA_ALG_SHA_224:
893 return( &mbedtls_sha224_info );
894 case PSA_ALG_SHA_256:
895 return( &mbedtls_sha256_info );
896#endif
897#if defined(MBEDTLS_SHA512_C)
898 case PSA_ALG_SHA_384:
899 return( &mbedtls_sha384_info );
900 case PSA_ALG_SHA_512:
901 return( &mbedtls_sha512_info );
902#endif
903 default:
904 return( NULL );
905 }
906}
907
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100908psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
909{
910 switch( operation->alg )
911 {
Gilles Peskine81736312018-06-26 15:04:31 +0200912 case 0:
913 /* The object has (apparently) been initialized but it is not
914 * in use. It's ok to call abort on such an object, and there's
915 * nothing to do. */
916 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100917#if defined(MBEDTLS_MD2_C)
918 case PSA_ALG_MD2:
919 mbedtls_md2_free( &operation->ctx.md2 );
920 break;
921#endif
922#if defined(MBEDTLS_MD4_C)
923 case PSA_ALG_MD4:
924 mbedtls_md4_free( &operation->ctx.md4 );
925 break;
926#endif
927#if defined(MBEDTLS_MD5_C)
928 case PSA_ALG_MD5:
929 mbedtls_md5_free( &operation->ctx.md5 );
930 break;
931#endif
932#if defined(MBEDTLS_RIPEMD160_C)
933 case PSA_ALG_RIPEMD160:
934 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
935 break;
936#endif
937#if defined(MBEDTLS_SHA1_C)
938 case PSA_ALG_SHA_1:
939 mbedtls_sha1_free( &operation->ctx.sha1 );
940 break;
941#endif
942#if defined(MBEDTLS_SHA256_C)
943 case PSA_ALG_SHA_224:
944 case PSA_ALG_SHA_256:
945 mbedtls_sha256_free( &operation->ctx.sha256 );
946 break;
947#endif
948#if defined(MBEDTLS_SHA512_C)
949 case PSA_ALG_SHA_384:
950 case PSA_ALG_SHA_512:
951 mbedtls_sha512_free( &operation->ctx.sha512 );
952 break;
953#endif
954 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +0200955 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100956 }
957 operation->alg = 0;
958 return( PSA_SUCCESS );
959}
960
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200961psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100962 psa_algorithm_t alg )
963{
964 int ret;
965 operation->alg = 0;
966 switch( alg )
967 {
968#if defined(MBEDTLS_MD2_C)
969 case PSA_ALG_MD2:
970 mbedtls_md2_init( &operation->ctx.md2 );
971 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
972 break;
973#endif
974#if defined(MBEDTLS_MD4_C)
975 case PSA_ALG_MD4:
976 mbedtls_md4_init( &operation->ctx.md4 );
977 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
978 break;
979#endif
980#if defined(MBEDTLS_MD5_C)
981 case PSA_ALG_MD5:
982 mbedtls_md5_init( &operation->ctx.md5 );
983 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
984 break;
985#endif
986#if defined(MBEDTLS_RIPEMD160_C)
987 case PSA_ALG_RIPEMD160:
988 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
989 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
990 break;
991#endif
992#if defined(MBEDTLS_SHA1_C)
993 case PSA_ALG_SHA_1:
994 mbedtls_sha1_init( &operation->ctx.sha1 );
995 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
996 break;
997#endif
998#if defined(MBEDTLS_SHA256_C)
999 case PSA_ALG_SHA_224:
1000 mbedtls_sha256_init( &operation->ctx.sha256 );
1001 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1002 break;
1003 case PSA_ALG_SHA_256:
1004 mbedtls_sha256_init( &operation->ctx.sha256 );
1005 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1006 break;
1007#endif
1008#if defined(MBEDTLS_SHA512_C)
1009 case PSA_ALG_SHA_384:
1010 mbedtls_sha512_init( &operation->ctx.sha512 );
1011 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1012 break;
1013 case PSA_ALG_SHA_512:
1014 mbedtls_sha512_init( &operation->ctx.sha512 );
1015 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1016 break;
1017#endif
1018 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001019 return( PSA_ALG_IS_HASH( alg ) ?
1020 PSA_ERROR_NOT_SUPPORTED :
1021 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001022 }
1023 if( ret == 0 )
1024 operation->alg = alg;
1025 else
1026 psa_hash_abort( operation );
1027 return( mbedtls_to_psa_error( ret ) );
1028}
1029
1030psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1031 const uint8_t *input,
1032 size_t input_length )
1033{
1034 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001035
1036 /* Don't require hash implementations to behave correctly on a
1037 * zero-length input, which may have an invalid pointer. */
1038 if( input_length == 0 )
1039 return( PSA_SUCCESS );
1040
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001041 switch( operation->alg )
1042 {
1043#if defined(MBEDTLS_MD2_C)
1044 case PSA_ALG_MD2:
1045 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1046 input, input_length );
1047 break;
1048#endif
1049#if defined(MBEDTLS_MD4_C)
1050 case PSA_ALG_MD4:
1051 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1052 input, input_length );
1053 break;
1054#endif
1055#if defined(MBEDTLS_MD5_C)
1056 case PSA_ALG_MD5:
1057 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1058 input, input_length );
1059 break;
1060#endif
1061#if defined(MBEDTLS_RIPEMD160_C)
1062 case PSA_ALG_RIPEMD160:
1063 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1064 input, input_length );
1065 break;
1066#endif
1067#if defined(MBEDTLS_SHA1_C)
1068 case PSA_ALG_SHA_1:
1069 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1070 input, input_length );
1071 break;
1072#endif
1073#if defined(MBEDTLS_SHA256_C)
1074 case PSA_ALG_SHA_224:
1075 case PSA_ALG_SHA_256:
1076 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1077 input, input_length );
1078 break;
1079#endif
1080#if defined(MBEDTLS_SHA512_C)
1081 case PSA_ALG_SHA_384:
1082 case PSA_ALG_SHA_512:
1083 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1084 input, input_length );
1085 break;
1086#endif
1087 default:
1088 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1089 break;
1090 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001091
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001092 if( ret != 0 )
1093 psa_hash_abort( operation );
1094 return( mbedtls_to_psa_error( ret ) );
1095}
1096
1097psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1098 uint8_t *hash,
1099 size_t hash_size,
1100 size_t *hash_length )
1101{
1102 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001103 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001104
1105 /* Fill the output buffer with something that isn't a valid hash
1106 * (barring an attack on the hash and deliberately-crafted input),
1107 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001108 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001109 /* If hash_size is 0 then hash may be NULL and then the
1110 * call to memset would have undefined behavior. */
1111 if( hash_size != 0 )
1112 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001113
1114 if( hash_size < actual_hash_length )
1115 return( PSA_ERROR_BUFFER_TOO_SMALL );
1116
1117 switch( operation->alg )
1118 {
1119#if defined(MBEDTLS_MD2_C)
1120 case PSA_ALG_MD2:
1121 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1122 break;
1123#endif
1124#if defined(MBEDTLS_MD4_C)
1125 case PSA_ALG_MD4:
1126 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1127 break;
1128#endif
1129#if defined(MBEDTLS_MD5_C)
1130 case PSA_ALG_MD5:
1131 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1132 break;
1133#endif
1134#if defined(MBEDTLS_RIPEMD160_C)
1135 case PSA_ALG_RIPEMD160:
1136 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1137 break;
1138#endif
1139#if defined(MBEDTLS_SHA1_C)
1140 case PSA_ALG_SHA_1:
1141 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1142 break;
1143#endif
1144#if defined(MBEDTLS_SHA256_C)
1145 case PSA_ALG_SHA_224:
1146 case PSA_ALG_SHA_256:
1147 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1148 break;
1149#endif
1150#if defined(MBEDTLS_SHA512_C)
1151 case PSA_ALG_SHA_384:
1152 case PSA_ALG_SHA_512:
1153 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1154 break;
1155#endif
1156 default:
1157 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1158 break;
1159 }
1160
1161 if( ret == 0 )
1162 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001163 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001164 return( psa_hash_abort( operation ) );
1165 }
1166 else
1167 {
1168 psa_hash_abort( operation );
1169 return( mbedtls_to_psa_error( ret ) );
1170 }
1171}
1172
Gilles Peskine2d277862018-06-18 15:41:12 +02001173psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1174 const uint8_t *hash,
1175 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001176{
1177 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1178 size_t actual_hash_length;
1179 psa_status_t status = psa_hash_finish( operation,
1180 actual_hash, sizeof( actual_hash ),
1181 &actual_hash_length );
1182 if( status != PSA_SUCCESS )
1183 return( status );
1184 if( actual_hash_length != hash_length )
1185 return( PSA_ERROR_INVALID_SIGNATURE );
1186 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1187 return( PSA_ERROR_INVALID_SIGNATURE );
1188 return( PSA_SUCCESS );
1189}
1190
1191
1192
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001193/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001194/* MAC */
1195/****************************************************************/
1196
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001197static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001198 psa_algorithm_t alg,
1199 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001200 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001201 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001202{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001203 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001204 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001205
1206 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1207 {
1208 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001209 {
1210 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1211 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001212
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001213 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001214 {
Gilles Peskine5d1888e2018-07-12 00:32:42 +02001215 case PSA_ALG_STREAM_CIPHER_BASE:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001216 mode = MBEDTLS_MODE_STREAM;
1217 break;
1218 case PSA_ALG_CBC_BASE:
1219 mode = MBEDTLS_MODE_CBC;
1220 break;
1221 case PSA_ALG_CFB_BASE:
1222 mode = MBEDTLS_MODE_CFB;
1223 break;
1224 case PSA_ALG_OFB_BASE:
1225 mode = MBEDTLS_MODE_OFB;
1226 break;
1227 case PSA_ALG_CTR:
1228 mode = MBEDTLS_MODE_CTR;
1229 break;
1230 case PSA_ALG_CCM:
1231 mode = MBEDTLS_MODE_CCM;
1232 break;
1233 case PSA_ALG_GCM:
1234 mode = MBEDTLS_MODE_GCM;
1235 break;
1236 default:
1237 return( NULL );
1238 }
1239 }
1240 else if( alg == PSA_ALG_CMAC )
1241 mode = MBEDTLS_MODE_ECB;
1242 else if( alg == PSA_ALG_GMAC )
1243 mode = MBEDTLS_MODE_GCM;
1244 else
1245 return( NULL );
1246
1247 switch( key_type )
1248 {
1249 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001250 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001251 break;
1252 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001253 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1254 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001255 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001256 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001257 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001258 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001259 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1260 * but two-key Triple-DES is functionally three-key Triple-DES
1261 * with K1=K3, so that's how we present it to mbedtls. */
1262 if( key_bits == 128 )
1263 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001264 break;
1265 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001266 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001267 break;
1268 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001269 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001270 break;
1271 default:
1272 return( NULL );
1273 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001274 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001275 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001276
Jaeden Amero23bbb752018-06-26 14:16:54 +01001277 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1278 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001279}
1280
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001281static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001282{
Gilles Peskine2d277862018-06-18 15:41:12 +02001283 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001284 {
1285 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001286 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001287 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001288 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001289 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001290 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001291 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001292 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001293 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001294 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001295 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001296 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001297 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001298 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001299 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001300 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001301 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001302 return( 128 );
1303 default:
1304 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001305 }
1306}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001307
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001308/* Initialize the MAC operation structure. Once this function has been
1309 * called, psa_mac_abort can run and will do the right thing. */
1310static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1311 psa_algorithm_t alg )
1312{
1313 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1314
1315 operation->alg = alg;
1316 operation->key_set = 0;
1317 operation->iv_set = 0;
1318 operation->iv_required = 0;
1319 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001320 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001321
1322#if defined(MBEDTLS_CMAC_C)
1323 if( alg == PSA_ALG_CMAC )
1324 {
1325 operation->iv_required = 0;
1326 mbedtls_cipher_init( &operation->ctx.cmac );
1327 status = PSA_SUCCESS;
1328 }
1329 else
1330#endif /* MBEDTLS_CMAC_C */
1331#if defined(MBEDTLS_MD_C)
1332 if( PSA_ALG_IS_HMAC( operation->alg ) )
1333 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001334 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1335 operation->ctx.hmac.hash_ctx.alg = 0;
1336 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001337 }
1338 else
1339#endif /* MBEDTLS_MD_C */
1340 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001341 if( ! PSA_ALG_IS_MAC( alg ) )
1342 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001343 }
1344
1345 if( status != PSA_SUCCESS )
1346 memset( operation, 0, sizeof( *operation ) );
1347 return( status );
1348}
1349
Gilles Peskine01126fa2018-07-12 17:04:55 +02001350#if defined(MBEDTLS_MD_C)
1351static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1352{
1353 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1354 return( psa_hash_abort( &hmac->hash_ctx ) );
1355}
1356#endif /* MBEDTLS_MD_C */
1357
Gilles Peskine8c9def32018-02-08 10:02:12 +01001358psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1359{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001360 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001361 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001362 /* The object has (apparently) been initialized but it is not
1363 * in use. It's ok to call abort on such an object, and there's
1364 * nothing to do. */
1365 return( PSA_SUCCESS );
1366 }
1367 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001368#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001369 if( operation->alg == PSA_ALG_CMAC )
1370 {
1371 mbedtls_cipher_free( &operation->ctx.cmac );
1372 }
1373 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001374#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001375#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001376 if( PSA_ALG_IS_HMAC( operation->alg ) )
1377 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001378 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001379 }
1380 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001381#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001382 {
1383 /* Sanity check (shouldn't happen: operation->alg should
1384 * always have been initialized to a valid value). */
1385 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001386 }
Moran Peker41deec42018-04-04 15:43:05 +03001387
Gilles Peskine8c9def32018-02-08 10:02:12 +01001388 operation->alg = 0;
1389 operation->key_set = 0;
1390 operation->iv_set = 0;
1391 operation->iv_required = 0;
1392 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001393 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001394
Gilles Peskine8c9def32018-02-08 10:02:12 +01001395 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001396
1397bad_state:
1398 /* If abort is called on an uninitialized object, we can't trust
1399 * anything. Wipe the object in case it contains confidential data.
1400 * This may result in a memory leak if a pointer gets overwritten,
1401 * but it's too late to do anything about this. */
1402 memset( operation, 0, sizeof( *operation ) );
1403 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001404}
1405
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001406#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001407static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001408 size_t key_bits,
1409 key_slot_t *slot,
1410 const mbedtls_cipher_info_t *cipher_info )
1411{
1412 int ret;
1413
1414 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001415
1416 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1417 if( ret != 0 )
1418 return( ret );
1419
1420 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1421 slot->data.raw.data,
1422 key_bits );
1423 return( ret );
1424}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001425#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001426
Gilles Peskine248051a2018-06-20 16:09:38 +02001427#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001428static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1429 const uint8_t *key,
1430 size_t key_length,
1431 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001432{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001433 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001434 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001435 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001436 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001437 psa_status_t status;
1438
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001439 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1440 * overflow below. This should never trigger if the hash algorithm
1441 * is implemented correctly. */
1442 /* The size checks against the ipad and opad buffers cannot be written
1443 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1444 * because that triggers -Wlogical-op on GCC 7.3. */
1445 if( block_size > sizeof( ipad ) )
1446 return( PSA_ERROR_NOT_SUPPORTED );
1447 if( block_size > sizeof( hmac->opad ) )
1448 return( PSA_ERROR_NOT_SUPPORTED );
1449 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001450 return( PSA_ERROR_NOT_SUPPORTED );
1451
Gilles Peskined223b522018-06-11 18:12:58 +02001452 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001453 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001454 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001455 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001456 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001457 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001458 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001459 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001460 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001461 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001462 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001463 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001464 }
Gilles Peskine96889972018-07-12 17:07:03 +02001465 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1466 * but it is permitted. It is common when HMAC is used in HKDF, for
1467 * example. Don't call `memcpy` in the 0-length because `key` could be
1468 * an invalid pointer which would make the behavior undefined. */
1469 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001470 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001471
Gilles Peskined223b522018-06-11 18:12:58 +02001472 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1473 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001474 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001475 ipad[i] ^= 0x36;
1476 memset( ipad + key_length, 0x36, block_size - key_length );
1477
1478 /* Copy the key material from ipad to opad, flipping the requisite bits,
1479 * and filling the rest of opad with the requisite constant. */
1480 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001481 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1482 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001483
Gilles Peskine01126fa2018-07-12 17:04:55 +02001484 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001485 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001486 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001487
Gilles Peskine01126fa2018-07-12 17:04:55 +02001488 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001489
1490cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001491 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001492
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001493 return( status );
1494}
Gilles Peskine248051a2018-06-20 16:09:38 +02001495#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001496
Gilles Peskine89167cb2018-07-08 20:12:23 +02001497static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1498 psa_key_slot_t key,
1499 psa_algorithm_t alg,
1500 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001501{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001502 psa_status_t status;
1503 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001504 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001505 psa_key_usage_t usage =
1506 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001507
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001508 status = psa_mac_init( operation, alg );
1509 if( status != PSA_SUCCESS )
1510 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001511 if( is_sign )
1512 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001513
Gilles Peskine89167cb2018-07-08 20:12:23 +02001514 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001515 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001516 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001517 key_bits = psa_get_key_bits( slot );
1518
Gilles Peskine8c9def32018-02-08 10:02:12 +01001519#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001520 if( alg == PSA_ALG_CMAC )
1521 {
1522 const mbedtls_cipher_info_t *cipher_info =
1523 mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
1524 int ret;
1525 if( cipher_info == NULL )
1526 {
1527 status = PSA_ERROR_NOT_SUPPORTED;
1528 goto exit;
1529 }
1530 operation->mac_size = cipher_info->block_size;
1531 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1532 status = mbedtls_to_psa_error( ret );
1533 }
1534 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001535#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001536#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001537 if( PSA_ALG_IS_HMAC( alg ) )
1538 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001539 psa_algorithm_t hash_alg = PSA_ALG_HMAC_HASH( alg );
1540 if( hash_alg == 0 )
1541 {
1542 status = PSA_ERROR_NOT_SUPPORTED;
1543 goto exit;
1544 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001545
1546 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1547 /* Sanity check. This shouldn't fail on a valid configuration. */
1548 if( operation->mac_size == 0 ||
1549 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1550 {
1551 status = PSA_ERROR_NOT_SUPPORTED;
1552 goto exit;
1553 }
1554
Gilles Peskine01126fa2018-07-12 17:04:55 +02001555 if( slot->type != PSA_KEY_TYPE_HMAC )
1556 {
1557 status = PSA_ERROR_INVALID_ARGUMENT;
1558 goto exit;
1559 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001560
Gilles Peskine01126fa2018-07-12 17:04:55 +02001561 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1562 slot->data.raw.data,
1563 slot->data.raw.bytes,
1564 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001565 }
1566 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001567#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001568 {
1569 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001570 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001571
Gilles Peskinefbfac682018-07-08 20:51:54 +02001572exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001573 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001574 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001575 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001576 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001577 else
1578 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001579 operation->key_set = 1;
1580 }
1581 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001582}
1583
Gilles Peskine89167cb2018-07-08 20:12:23 +02001584psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1585 psa_key_slot_t key,
1586 psa_algorithm_t alg )
1587{
1588 return( psa_mac_setup( operation, key, alg, 1 ) );
1589}
1590
1591psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1592 psa_key_slot_t key,
1593 psa_algorithm_t alg )
1594{
1595 return( psa_mac_setup( operation, key, alg, 0 ) );
1596}
1597
Gilles Peskine8c9def32018-02-08 10:02:12 +01001598psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1599 const uint8_t *input,
1600 size_t input_length )
1601{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001602 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001603 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001604 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001605 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001606 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001607 operation->has_input = 1;
1608
Gilles Peskine8c9def32018-02-08 10:02:12 +01001609#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001610 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001611 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001612 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1613 input, input_length );
1614 status = mbedtls_to_psa_error( ret );
1615 }
1616 else
1617#endif /* MBEDTLS_CMAC_C */
1618#if defined(MBEDTLS_MD_C)
1619 if( PSA_ALG_IS_HMAC( operation->alg ) )
1620 {
1621 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1622 input_length );
1623 }
1624 else
1625#endif /* MBEDTLS_MD_C */
1626 {
1627 /* This shouldn't happen if `operation` was initialized by
1628 * a setup function. */
1629 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001630 }
1631
Gilles Peskinefbfac682018-07-08 20:51:54 +02001632cleanup:
1633 if( status != PSA_SUCCESS )
1634 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001635 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001636}
1637
Gilles Peskine01126fa2018-07-12 17:04:55 +02001638#if defined(MBEDTLS_MD_C)
1639static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1640 uint8_t *mac,
1641 size_t mac_size )
1642{
1643 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1644 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1645 size_t hash_size = 0;
1646 size_t block_size = psa_get_hash_block_size( hash_alg );
1647 psa_status_t status;
1648
Gilles Peskine01126fa2018-07-12 17:04:55 +02001649 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1650 if( status != PSA_SUCCESS )
1651 return( status );
1652 /* From here on, tmp needs to be wiped. */
1653
1654 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1655 if( status != PSA_SUCCESS )
1656 goto exit;
1657
1658 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1659 if( status != PSA_SUCCESS )
1660 goto exit;
1661
1662 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1663 if( status != PSA_SUCCESS )
1664 goto exit;
1665
1666 status = psa_hash_finish( &hmac->hash_ctx, mac, mac_size, &hash_size );
1667
1668exit:
1669 mbedtls_zeroize( tmp, hash_size );
1670 return( status );
1671}
1672#endif /* MBEDTLS_MD_C */
1673
mohammad16036df908f2018-04-02 08:34:15 -07001674static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001675 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001676 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001677{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001678 if( ! operation->key_set )
1679 return( PSA_ERROR_BAD_STATE );
1680 if( operation->iv_required && ! operation->iv_set )
1681 return( PSA_ERROR_BAD_STATE );
1682
Gilles Peskine8c9def32018-02-08 10:02:12 +01001683 if( mac_size < operation->mac_size )
1684 return( PSA_ERROR_BUFFER_TOO_SMALL );
1685
Gilles Peskine8c9def32018-02-08 10:02:12 +01001686#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001687 if( operation->alg == PSA_ALG_CMAC )
1688 {
1689 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1690 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001691 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001692 else
1693#endif /* MBEDTLS_CMAC_C */
1694#if defined(MBEDTLS_MD_C)
1695 if( PSA_ALG_IS_HMAC( operation->alg ) )
1696 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001697 return( psa_hmac_finish_internal( &operation->ctx.hmac,
1698 mac, mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001699 }
1700 else
1701#endif /* MBEDTLS_MD_C */
1702 {
1703 /* This shouldn't happen if `operation` was initialized by
1704 * a setup function. */
1705 return( PSA_ERROR_BAD_STATE );
1706 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001707}
1708
Gilles Peskineacd4be32018-07-08 19:56:25 +02001709psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1710 uint8_t *mac,
1711 size_t mac_size,
1712 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001713{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001714 psa_status_t status;
1715
1716 /* Fill the output buffer with something that isn't a valid mac
1717 * (barring an attack on the mac and deliberately-crafted input),
1718 * in case the caller doesn't check the return status properly. */
1719 *mac_length = mac_size;
1720 /* If mac_size is 0 then mac may be NULL and then the
1721 * call to memset would have undefined behavior. */
1722 if( mac_size != 0 )
1723 memset( mac, '!', mac_size );
1724
Gilles Peskine89167cb2018-07-08 20:12:23 +02001725 if( ! operation->is_sign )
1726 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001727 status = PSA_ERROR_BAD_STATE;
1728 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001729 }
mohammad16036df908f2018-04-02 08:34:15 -07001730
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001731 status = psa_mac_finish_internal( operation, mac, mac_size );
1732
1733cleanup:
1734 if( status == PSA_SUCCESS )
1735 {
1736 status = psa_mac_abort( operation );
1737 if( status == PSA_SUCCESS )
1738 *mac_length = operation->mac_size;
1739 else
1740 memset( mac, '!', mac_size );
1741 }
1742 else
1743 psa_mac_abort( operation );
1744 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001745}
1746
Gilles Peskineacd4be32018-07-08 19:56:25 +02001747psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1748 const uint8_t *mac,
1749 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001750{
Gilles Peskine828ed142018-06-18 23:25:51 +02001751 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001752 psa_status_t status;
1753
Gilles Peskine89167cb2018-07-08 20:12:23 +02001754 if( operation->is_sign )
1755 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001756 status = PSA_ERROR_BAD_STATE;
1757 goto cleanup;
1758 }
1759 if( operation->mac_size != mac_length )
1760 {
1761 status = PSA_ERROR_INVALID_SIGNATURE;
1762 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001763 }
mohammad16036df908f2018-04-02 08:34:15 -07001764
1765 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001766 actual_mac, sizeof( actual_mac ) );
1767
1768 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1769 status = PSA_ERROR_INVALID_SIGNATURE;
1770
1771cleanup:
1772 if( status == PSA_SUCCESS )
1773 status = psa_mac_abort( operation );
1774 else
1775 psa_mac_abort( operation );
1776
1777 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001778}
1779
1780
Gilles Peskine20035e32018-02-03 22:44:14 +01001781
Gilles Peskine20035e32018-02-03 22:44:14 +01001782/****************************************************************/
1783/* Asymmetric cryptography */
1784/****************************************************************/
1785
Gilles Peskine2b450e32018-06-27 15:42:46 +02001786#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001787/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001788 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001789static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1790 size_t hash_length,
1791 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001792{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001793 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001794 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001795 *md_alg = mbedtls_md_get_type( md_info );
1796
1797 /* The Mbed TLS RSA module uses an unsigned int for hash length
1798 * parameters. Validate that it fits so that we don't risk an
1799 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001800#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001801 if( hash_length > UINT_MAX )
1802 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001803#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001804
1805#if defined(MBEDTLS_PKCS1_V15)
1806 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
1807 * must be correct. */
1808 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
1809 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001810 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001811 if( md_info == NULL )
1812 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001813 if( mbedtls_md_get_size( md_info ) != hash_length )
1814 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001815 }
1816#endif /* MBEDTLS_PKCS1_V15 */
1817
1818#if defined(MBEDTLS_PKCS1_V21)
1819 /* PSS requires a hash internally. */
1820 if( PSA_ALG_IS_RSA_PSS( alg ) )
1821 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001822 if( md_info == NULL )
1823 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001824 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001825#endif /* MBEDTLS_PKCS1_V21 */
1826
Gilles Peskine61b91d42018-06-08 16:09:36 +02001827 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001828}
1829
Gilles Peskine2b450e32018-06-27 15:42:46 +02001830static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1831 psa_algorithm_t alg,
1832 const uint8_t *hash,
1833 size_t hash_length,
1834 uint8_t *signature,
1835 size_t signature_size,
1836 size_t *signature_length )
1837{
1838 psa_status_t status;
1839 int ret;
1840 mbedtls_md_type_t md_alg;
1841
1842 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1843 if( status != PSA_SUCCESS )
1844 return( status );
1845
Gilles Peskine630a18a2018-06-29 17:49:35 +02001846 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001847 return( PSA_ERROR_BUFFER_TOO_SMALL );
1848
1849#if defined(MBEDTLS_PKCS1_V15)
1850 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1851 {
1852 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1853 MBEDTLS_MD_NONE );
1854 ret = mbedtls_rsa_pkcs1_sign( rsa,
1855 mbedtls_ctr_drbg_random,
1856 &global_data.ctr_drbg,
1857 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001858 md_alg,
1859 (unsigned int) hash_length,
1860 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001861 signature );
1862 }
1863 else
1864#endif /* MBEDTLS_PKCS1_V15 */
1865#if defined(MBEDTLS_PKCS1_V21)
1866 if( PSA_ALG_IS_RSA_PSS( alg ) )
1867 {
1868 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1869 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1870 mbedtls_ctr_drbg_random,
1871 &global_data.ctr_drbg,
1872 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001873 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001874 (unsigned int) hash_length,
1875 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001876 signature );
1877 }
1878 else
1879#endif /* MBEDTLS_PKCS1_V21 */
1880 {
1881 return( PSA_ERROR_INVALID_ARGUMENT );
1882 }
1883
1884 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02001885 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02001886 return( mbedtls_to_psa_error( ret ) );
1887}
1888
1889static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1890 psa_algorithm_t alg,
1891 const uint8_t *hash,
1892 size_t hash_length,
1893 const uint8_t *signature,
1894 size_t signature_length )
1895{
1896 psa_status_t status;
1897 int ret;
1898 mbedtls_md_type_t md_alg;
1899
1900 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1901 if( status != PSA_SUCCESS )
1902 return( status );
1903
Gilles Peskine630a18a2018-06-29 17:49:35 +02001904 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001905 return( PSA_ERROR_BUFFER_TOO_SMALL );
1906
1907#if defined(MBEDTLS_PKCS1_V15)
1908 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1909 {
1910 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1911 MBEDTLS_MD_NONE );
1912 ret = mbedtls_rsa_pkcs1_verify( rsa,
1913 mbedtls_ctr_drbg_random,
1914 &global_data.ctr_drbg,
1915 MBEDTLS_RSA_PUBLIC,
1916 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001917 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001918 hash,
1919 signature );
1920 }
1921 else
1922#endif /* MBEDTLS_PKCS1_V15 */
1923#if defined(MBEDTLS_PKCS1_V21)
1924 if( PSA_ALG_IS_RSA_PSS( alg ) )
1925 {
1926 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1927 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1928 mbedtls_ctr_drbg_random,
1929 &global_data.ctr_drbg,
1930 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001931 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001932 (unsigned int) hash_length,
1933 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001934 signature );
1935 }
1936 else
1937#endif /* MBEDTLS_PKCS1_V21 */
1938 {
1939 return( PSA_ERROR_INVALID_ARGUMENT );
1940 }
1941 return( mbedtls_to_psa_error( ret ) );
1942}
1943#endif /* MBEDTLS_RSA_C */
1944
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001945#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001946/* `ecp` cannot be const because `ecp->grp` needs to be non-const
1947 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
1948 * (even though these functions don't modify it). */
1949static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
1950 psa_algorithm_t alg,
1951 const uint8_t *hash,
1952 size_t hash_length,
1953 uint8_t *signature,
1954 size_t signature_size,
1955 size_t *signature_length )
1956{
1957 int ret;
1958 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001959 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001960 mbedtls_mpi_init( &r );
1961 mbedtls_mpi_init( &s );
1962
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001963 if( signature_size < 2 * curve_bytes )
1964 {
1965 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
1966 goto cleanup;
1967 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001968
1969 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
1970 {
1971 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
1972 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1973 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
1974 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
1975 hash, hash_length,
1976 md_alg ) );
1977 }
1978 else
1979 {
1980 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
1981 hash, hash_length,
1982 mbedtls_ctr_drbg_random,
1983 &global_data.ctr_drbg ) );
1984 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001985
1986 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
1987 signature,
1988 curve_bytes ) );
1989 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
1990 signature + curve_bytes,
1991 curve_bytes ) );
1992
1993cleanup:
1994 mbedtls_mpi_free( &r );
1995 mbedtls_mpi_free( &s );
1996 if( ret == 0 )
1997 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001998 return( mbedtls_to_psa_error( ret ) );
1999}
2000
2001static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2002 const uint8_t *hash,
2003 size_t hash_length,
2004 const uint8_t *signature,
2005 size_t signature_length )
2006{
2007 int ret;
2008 mbedtls_mpi r, s;
2009 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2010 mbedtls_mpi_init( &r );
2011 mbedtls_mpi_init( &s );
2012
2013 if( signature_length != 2 * curve_bytes )
2014 return( PSA_ERROR_INVALID_SIGNATURE );
2015
2016 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2017 signature,
2018 curve_bytes ) );
2019 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2020 signature + curve_bytes,
2021 curve_bytes ) );
2022
2023 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2024 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002025
2026cleanup:
2027 mbedtls_mpi_free( &r );
2028 mbedtls_mpi_free( &s );
2029 return( mbedtls_to_psa_error( ret ) );
2030}
2031#endif /* MBEDTLS_ECDSA_C */
2032
Gilles Peskine61b91d42018-06-08 16:09:36 +02002033psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2034 psa_algorithm_t alg,
2035 const uint8_t *hash,
2036 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002037 uint8_t *signature,
2038 size_t signature_size,
2039 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002040{
2041 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002042 psa_status_t status;
2043
2044 *signature_length = signature_size;
2045
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002046 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2047 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002048 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002049 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002050 {
2051 status = PSA_ERROR_INVALID_ARGUMENT;
2052 goto exit;
2053 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002054
Gilles Peskine20035e32018-02-03 22:44:14 +01002055#if defined(MBEDTLS_RSA_C)
2056 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2057 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002058 status = psa_rsa_sign( slot->data.rsa,
2059 alg,
2060 hash, hash_length,
2061 signature, signature_size,
2062 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002063 }
2064 else
2065#endif /* defined(MBEDTLS_RSA_C) */
2066#if defined(MBEDTLS_ECP_C)
2067 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2068 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002069#if defined(MBEDTLS_ECDSA_C)
2070 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002071 status = psa_ecdsa_sign( slot->data.ecp,
2072 alg,
2073 hash, hash_length,
2074 signature, signature_size,
2075 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002076 else
2077#endif /* defined(MBEDTLS_ECDSA_C) */
2078 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002079 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002080 }
itayzafrir5c753392018-05-08 11:18:38 +03002081 }
2082 else
2083#endif /* defined(MBEDTLS_ECP_C) */
2084 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002085 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002086 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002087
2088exit:
2089 /* Fill the unused part of the output buffer (the whole buffer on error,
2090 * the trailing part on success) with something that isn't a valid mac
2091 * (barring an attack on the mac and deliberately-crafted input),
2092 * in case the caller doesn't check the return status properly. */
2093 if( status == PSA_SUCCESS )
2094 memset( signature + *signature_length, '!',
2095 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002096 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002097 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002098 /* If signature_size is 0 then we have nothing to do. We must not call
2099 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002100 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002101}
2102
2103psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2104 psa_algorithm_t alg,
2105 const uint8_t *hash,
2106 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002107 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002108 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002109{
2110 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002111 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002112
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002113 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2114 if( status != PSA_SUCCESS )
2115 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002116
Gilles Peskine61b91d42018-06-08 16:09:36 +02002117#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002118 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002119 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002120 return( psa_rsa_verify( slot->data.rsa,
2121 alg,
2122 hash, hash_length,
2123 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002124 }
2125 else
2126#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002127#if defined(MBEDTLS_ECP_C)
2128 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2129 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002130#if defined(MBEDTLS_ECDSA_C)
2131 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002132 return( psa_ecdsa_verify( slot->data.ecp,
2133 hash, hash_length,
2134 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002135 else
2136#endif /* defined(MBEDTLS_ECDSA_C) */
2137 {
2138 return( PSA_ERROR_INVALID_ARGUMENT );
2139 }
itayzafrir5c753392018-05-08 11:18:38 +03002140 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002141 else
2142#endif /* defined(MBEDTLS_ECP_C) */
2143 {
2144 return( PSA_ERROR_NOT_SUPPORTED );
2145 }
2146}
2147
Gilles Peskine072ac562018-06-30 00:21:29 +02002148#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2149static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2150 mbedtls_rsa_context *rsa )
2151{
2152 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2153 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2154 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2155 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2156}
2157#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2158
Gilles Peskine61b91d42018-06-08 16:09:36 +02002159psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2160 psa_algorithm_t alg,
2161 const uint8_t *input,
2162 size_t input_length,
2163 const uint8_t *salt,
2164 size_t salt_length,
2165 uint8_t *output,
2166 size_t output_size,
2167 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002168{
2169 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002170 psa_status_t status;
2171
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002172 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002173
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002174 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2175 return( PSA_ERROR_INVALID_ARGUMENT );
2176
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002177 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2178 if( status != PSA_SUCCESS )
2179 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002180 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2181 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002182 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002183
2184#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002185 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002186 {
2187 mbedtls_rsa_context *rsa = slot->data.rsa;
2188 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002189 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002190 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002191#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002192 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002193 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002194 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2195 mbedtls_ctr_drbg_random,
2196 &global_data.ctr_drbg,
2197 MBEDTLS_RSA_PUBLIC,
2198 input_length,
2199 input,
2200 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002201 }
2202 else
2203#endif /* MBEDTLS_PKCS1_V15 */
2204#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002205 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002206 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002207 psa_rsa_oaep_set_padding_mode( alg, rsa );
2208 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2209 mbedtls_ctr_drbg_random,
2210 &global_data.ctr_drbg,
2211 MBEDTLS_RSA_PUBLIC,
2212 salt, salt_length,
2213 input_length,
2214 input,
2215 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002216 }
2217 else
2218#endif /* MBEDTLS_PKCS1_V21 */
2219 {
2220 return( PSA_ERROR_INVALID_ARGUMENT );
2221 }
2222 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002223 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002224 return( mbedtls_to_psa_error( ret ) );
2225 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002226 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002227#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002228 {
2229 return( PSA_ERROR_NOT_SUPPORTED );
2230 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002231}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002232
Gilles Peskine61b91d42018-06-08 16:09:36 +02002233psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2234 psa_algorithm_t alg,
2235 const uint8_t *input,
2236 size_t input_length,
2237 const uint8_t *salt,
2238 size_t salt_length,
2239 uint8_t *output,
2240 size_t output_size,
2241 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002242{
2243 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002244 psa_status_t status;
2245
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002246 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002247
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002248 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2249 return( PSA_ERROR_INVALID_ARGUMENT );
2250
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002251 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2252 if( status != PSA_SUCCESS )
2253 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002254 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2255 return( PSA_ERROR_INVALID_ARGUMENT );
2256
2257#if defined(MBEDTLS_RSA_C)
2258 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2259 {
2260 mbedtls_rsa_context *rsa = slot->data.rsa;
2261 int ret;
2262
Gilles Peskine630a18a2018-06-29 17:49:35 +02002263 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002264 return( PSA_ERROR_INVALID_ARGUMENT );
2265
2266#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002267 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002268 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002269 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2270 mbedtls_ctr_drbg_random,
2271 &global_data.ctr_drbg,
2272 MBEDTLS_RSA_PRIVATE,
2273 output_length,
2274 input,
2275 output,
2276 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002277 }
2278 else
2279#endif /* MBEDTLS_PKCS1_V15 */
2280#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002281 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002282 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002283 psa_rsa_oaep_set_padding_mode( alg, rsa );
2284 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2285 mbedtls_ctr_drbg_random,
2286 &global_data.ctr_drbg,
2287 MBEDTLS_RSA_PRIVATE,
2288 salt, salt_length,
2289 output_length,
2290 input,
2291 output,
2292 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002293 }
2294 else
2295#endif /* MBEDTLS_PKCS1_V21 */
2296 {
2297 return( PSA_ERROR_INVALID_ARGUMENT );
2298 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002299
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002300 return( mbedtls_to_psa_error( ret ) );
2301 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002302 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002303#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002304 {
2305 return( PSA_ERROR_NOT_SUPPORTED );
2306 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002307}
Gilles Peskine20035e32018-02-03 22:44:14 +01002308
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002309
2310
mohammad1603503973b2018-03-12 15:59:30 +02002311/****************************************************************/
2312/* Symmetric cryptography */
2313/****************************************************************/
2314
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002315/* Initialize the cipher operation structure. Once this function has been
2316 * called, psa_cipher_abort can run and will do the right thing. */
2317static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2318 psa_algorithm_t alg )
2319{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002320 if( ! PSA_ALG_IS_CIPHER( alg ) )
2321 {
2322 memset( operation, 0, sizeof( *operation ) );
2323 return( PSA_ERROR_INVALID_ARGUMENT );
2324 }
2325
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002326 operation->alg = alg;
2327 operation->key_set = 0;
2328 operation->iv_set = 0;
2329 operation->iv_required = 1;
2330 operation->iv_size = 0;
2331 operation->block_size = 0;
2332 mbedtls_cipher_init( &operation->ctx.cipher );
2333 return( PSA_SUCCESS );
2334}
2335
Gilles Peskinee553c652018-06-04 16:22:46 +02002336static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2337 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002338 psa_algorithm_t alg,
2339 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002340{
2341 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2342 psa_status_t status;
2343 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002344 size_t key_bits;
2345 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002346 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2347 PSA_KEY_USAGE_ENCRYPT :
2348 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002349
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002350 status = psa_cipher_init( operation, alg );
2351 if( status != PSA_SUCCESS )
2352 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002353
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002354 status = psa_get_key_from_slot( key, &slot, usage, alg);
2355 if( status != PSA_SUCCESS )
2356 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002357 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002358
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002359 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002360 if( cipher_info == NULL )
2361 return( PSA_ERROR_NOT_SUPPORTED );
2362
mohammad1603503973b2018-03-12 15:59:30 +02002363 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002364 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002365 {
2366 psa_cipher_abort( operation );
2367 return( mbedtls_to_psa_error( ret ) );
2368 }
2369
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002370#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002371 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002372 {
2373 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2374 unsigned char keys[24];
2375 memcpy( keys, slot->data.raw.data, 16 );
2376 memcpy( keys + 16, slot->data.raw.data, 8 );
2377 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2378 keys,
2379 192, cipher_operation );
2380 }
2381 else
2382#endif
2383 {
2384 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2385 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002386 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002387 }
Moran Peker41deec42018-04-04 15:43:05 +03002388 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002389 {
2390 psa_cipher_abort( operation );
2391 return( mbedtls_to_psa_error( ret ) );
2392 }
2393
mohammad16038481e742018-03-18 13:57:31 +02002394#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002395 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002396 {
Gilles Peskine53514202018-06-06 15:11:46 +02002397 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2398 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002399
Moran Pekera28258c2018-05-29 16:25:04 +03002400 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002401 {
2402 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2403 mode = MBEDTLS_PADDING_PKCS7;
2404 break;
2405 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2406 mode = MBEDTLS_PADDING_NONE;
2407 break;
2408 default:
Moran Pekerae382792018-05-31 14:06:17 +03002409 psa_cipher_abort( operation );
2410 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002411 }
2412 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002413 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002414 {
2415 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002416 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002417 }
mohammad16038481e742018-03-18 13:57:31 +02002418 }
2419#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2420
mohammad1603503973b2018-03-12 15:59:30 +02002421 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002422 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002423 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) :
Gilles Peskine7e928852018-06-04 16:23:10 +02002424 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002425 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002426 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002427 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002428 }
mohammad1603503973b2018-03-12 15:59:30 +02002429
Moran Peker395db872018-05-31 14:07:14 +03002430 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002431}
2432
Gilles Peskinefe119512018-07-08 21:39:34 +02002433psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2434 psa_key_slot_t key,
2435 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002436{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002437 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002438}
2439
Gilles Peskinefe119512018-07-08 21:39:34 +02002440psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2441 psa_key_slot_t key,
2442 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002443{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002444 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002445}
2446
Gilles Peskinefe119512018-07-08 21:39:34 +02002447psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2448 unsigned char *iv,
2449 size_t iv_size,
2450 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002451{
Moran Peker41deec42018-04-04 15:43:05 +03002452 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002453 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002454 return( PSA_ERROR_BAD_STATE );
2455 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002456 {
Moran Peker41deec42018-04-04 15:43:05 +03002457 ret = PSA_ERROR_BUFFER_TOO_SMALL;
2458 goto exit;
2459 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002460 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2461 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002462 if( ret != 0 )
2463 {
2464 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002465 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002466 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002467
mohammad16038481e742018-03-18 13:57:31 +02002468 *iv_length = operation->iv_size;
Gilles Peskinefe119512018-07-08 21:39:34 +02002469 ret = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002470
Moran Peker395db872018-05-31 14:07:14 +03002471exit:
2472 if( ret != PSA_SUCCESS )
2473 psa_cipher_abort( operation );
2474 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02002475}
2476
Gilles Peskinefe119512018-07-08 21:39:34 +02002477psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2478 const unsigned char *iv,
2479 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002480{
Moran Peker41deec42018-04-04 15:43:05 +03002481 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002482 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002483 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03002484 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002485 {
Moran Pekerae382792018-05-31 14:06:17 +03002486 psa_cipher_abort( operation );
2487 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03002488 }
mohammad1603503973b2018-03-12 15:59:30 +02002489 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002490 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002491 {
2492 psa_cipher_abort( operation );
2493 return( mbedtls_to_psa_error( ret ) );
2494 }
2495
2496 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02002497
Moran Peker395db872018-05-31 14:07:14 +03002498 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002499}
2500
Gilles Peskinee553c652018-06-04 16:22:46 +02002501psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2502 const uint8_t *input,
2503 size_t input_length,
2504 unsigned char *output,
2505 size_t output_size,
2506 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002507{
2508 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002509 size_t expected_output_size;
2510 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2511 {
2512 /* Take the unprocessed partial block left over from previous
2513 * update calls, if any, plus the input to this call. Remove
2514 * the last partial block, if any. You get the data that will be
2515 * output in this call. */
2516 expected_output_size =
2517 ( operation->ctx.cipher.unprocessed_len + input_length )
2518 / operation->block_size * operation->block_size;
2519 }
2520 else
2521 {
2522 expected_output_size = input_length;
2523 }
2524 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03002525 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02002526
mohammad1603503973b2018-03-12 15:59:30 +02002527 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002528 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002529 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002530 {
2531 psa_cipher_abort( operation );
2532 return( mbedtls_to_psa_error( ret ) );
2533 }
2534
Moran Peker395db872018-05-31 14:07:14 +03002535 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002536}
2537
Gilles Peskinee553c652018-06-04 16:22:46 +02002538psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2539 uint8_t *output,
2540 size_t output_size,
2541 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002542{
Janos Follath315b51c2018-07-09 16:04:51 +01002543 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2544 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002545 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002546
mohammad1603503973b2018-03-12 15:59:30 +02002547 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002548 {
Janos Follath315b51c2018-07-09 16:04:51 +01002549 status = PSA_ERROR_BAD_STATE;
2550 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002551 }
2552 if( operation->iv_required && ! operation->iv_set )
2553 {
Janos Follath315b51c2018-07-09 16:04:51 +01002554 status = PSA_ERROR_BAD_STATE;
2555 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002556 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002557 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2558 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002559 {
Gilles Peskine53514202018-06-06 15:11:46 +02002560 psa_algorithm_t padding_mode =
2561 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002562 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002563 {
Janos Follath315b51c2018-07-09 16:04:51 +01002564 status = PSA_ERROR_TAMPERING_DETECTED;
2565 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002566 }
Gilles Peskine53514202018-06-06 15:11:46 +02002567 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002568 {
2569 if( operation->ctx.cipher.unprocessed_len != 0 )
2570 {
Janos Follath315b51c2018-07-09 16:04:51 +01002571 status = PSA_ERROR_INVALID_ARGUMENT;
2572 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002573 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002574 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002575 }
2576
Janos Follath315b51c2018-07-09 16:04:51 +01002577 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2578 temp_output_buffer,
2579 output_length );
2580 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002581 {
Janos Follath315b51c2018-07-09 16:04:51 +01002582 status = mbedtls_to_psa_error( cipher_ret );
2583 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002584 }
Janos Follath315b51c2018-07-09 16:04:51 +01002585
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002586 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002587 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002588 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002589 memcpy( output, temp_output_buffer, *output_length );
2590 else
2591 {
Janos Follath315b51c2018-07-09 16:04:51 +01002592 status = PSA_ERROR_BUFFER_TOO_SMALL;
2593 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002594 }
mohammad1603503973b2018-03-12 15:59:30 +02002595
Janos Follath279ab8e2018-07-09 16:13:21 +01002596 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002597 status = psa_cipher_abort( operation );
2598
2599 return( status );
2600
2601error:
2602
2603 *output_length = 0;
2604
Janos Follath279ab8e2018-07-09 16:13:21 +01002605 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002606 (void) psa_cipher_abort( operation );
2607
2608 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002609}
2610
Gilles Peskinee553c652018-06-04 16:22:46 +02002611psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2612{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002613 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002614 {
2615 /* The object has (apparently) been initialized but it is not
2616 * in use. It's ok to call abort on such an object, and there's
2617 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002618 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002619 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002620
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002621 /* Sanity check (shouldn't happen: operation->alg should
2622 * always have been initialized to a valid value). */
2623 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2624 return( PSA_ERROR_BAD_STATE );
2625
mohammad1603503973b2018-03-12 15:59:30 +02002626 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002627
Moran Peker41deec42018-04-04 15:43:05 +03002628 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002629 operation->key_set = 0;
2630 operation->iv_set = 0;
2631 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002632 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002633 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002634
Moran Peker395db872018-05-31 14:07:14 +03002635 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002636}
2637
Gilles Peskinea0655c32018-04-30 17:06:50 +02002638
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002639
mohammad16038cc1cee2018-03-28 01:21:33 +03002640/****************************************************************/
2641/* Key Policy */
2642/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002643
mohammad160327010052018-07-03 13:16:15 +03002644#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002645void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002646{
Gilles Peskine803ce742018-06-18 16:07:14 +02002647 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002648}
2649
Gilles Peskine2d277862018-06-18 15:41:12 +02002650void psa_key_policy_set_usage( psa_key_policy_t *policy,
2651 psa_key_usage_t usage,
2652 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002653{
mohammad16034eed7572018-03-28 05:14:59 -07002654 policy->usage = usage;
2655 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002656}
2657
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002658psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002659{
mohammad16036df908f2018-04-02 08:34:15 -07002660 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002661}
2662
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002663psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002664{
mohammad16036df908f2018-04-02 08:34:15 -07002665 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002666}
mohammad160327010052018-07-03 13:16:15 +03002667#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002668
Gilles Peskine2d277862018-06-18 15:41:12 +02002669psa_status_t psa_set_key_policy( psa_key_slot_t key,
2670 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002671{
2672 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002673 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002674
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002675 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002676 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002677
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002678 status = psa_get_empty_key_slot( key, &slot );
2679 if( status != PSA_SUCCESS )
2680 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002681
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002682 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2683 PSA_KEY_USAGE_ENCRYPT |
2684 PSA_KEY_USAGE_DECRYPT |
2685 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002686 PSA_KEY_USAGE_VERIFY |
2687 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002688 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002689
mohammad16036df908f2018-04-02 08:34:15 -07002690 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002691
2692 return( PSA_SUCCESS );
2693}
2694
Gilles Peskine2d277862018-06-18 15:41:12 +02002695psa_status_t psa_get_key_policy( psa_key_slot_t key,
2696 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002697{
2698 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002699 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002700
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002701 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002702 return( PSA_ERROR_INVALID_ARGUMENT );
2703
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002704 status = psa_get_key_slot( key, &slot );
2705 if( status != PSA_SUCCESS )
2706 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002707
mohammad16036df908f2018-04-02 08:34:15 -07002708 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002709
2710 return( PSA_SUCCESS );
2711}
Gilles Peskine20035e32018-02-03 22:44:14 +01002712
Gilles Peskinea0655c32018-04-30 17:06:50 +02002713
2714
mohammad1603804cd712018-03-20 22:44:08 +02002715/****************************************************************/
2716/* Key Lifetime */
2717/****************************************************************/
2718
Gilles Peskine2d277862018-06-18 15:41:12 +02002719psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2720 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002721{
2722 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002723 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002724
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002725 status = psa_get_key_slot( key, &slot );
2726 if( status != PSA_SUCCESS )
2727 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002728
mohammad1603804cd712018-03-20 22:44:08 +02002729 *lifetime = slot->lifetime;
2730
2731 return( PSA_SUCCESS );
2732}
2733
Gilles Peskine2d277862018-06-18 15:41:12 +02002734psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002735 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002736{
2737 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002738 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002739
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002740 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2741 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002742 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2743 return( PSA_ERROR_INVALID_ARGUMENT );
2744
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002745 status = psa_get_empty_key_slot( key, &slot );
2746 if( status != PSA_SUCCESS )
2747 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002748
Moran Pekerd7326592018-05-29 16:56:39 +03002749 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002750 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002751
mohammad1603060ad8a2018-03-20 14:28:38 -07002752 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002753
2754 return( PSA_SUCCESS );
2755}
2756
Gilles Peskine20035e32018-02-03 22:44:14 +01002757
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002758
mohammad16035955c982018-04-26 00:53:03 +03002759/****************************************************************/
2760/* AEAD */
2761/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002762
mohammad16035955c982018-04-26 00:53:03 +03002763psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2764 psa_algorithm_t alg,
2765 const uint8_t *nonce,
2766 size_t nonce_length,
2767 const uint8_t *additional_data,
2768 size_t additional_data_length,
2769 const uint8_t *plaintext,
2770 size_t plaintext_length,
2771 uint8_t *ciphertext,
2772 size_t ciphertext_size,
2773 size_t *ciphertext_length )
2774{
2775 int ret;
2776 psa_status_t status;
2777 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002778 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002779 uint8_t *tag;
2780 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002781 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002782 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002783
mohammad1603f08a5502018-06-03 15:05:47 +03002784 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002785
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002786 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2787 if( status != PSA_SUCCESS )
2788 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002789 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002790
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002791 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002792 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002793 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002794 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002795
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002796 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002797 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002798 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002799
mohammad16035955c982018-04-26 00:53:03 +03002800 if( alg == PSA_ALG_GCM )
2801 {
2802 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002803 tag_length = 16;
2804
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002805 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002806 return( PSA_ERROR_INVALID_ARGUMENT );
2807
mohammad160315223a82018-06-03 17:19:55 +03002808 //make sure we have place to hold the tag in the ciphertext buffer
2809 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002810 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002811
2812 //update the tag pointer to point to the end of the ciphertext_length
2813 tag = ciphertext + plaintext_length;
2814
mohammad16035955c982018-04-26 00:53:03 +03002815 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002816 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002817 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002818 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002819 if( ret != 0 )
2820 {
2821 mbedtls_gcm_free( &gcm );
2822 return( mbedtls_to_psa_error( ret ) );
2823 }
2824 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002825 plaintext_length, nonce,
2826 nonce_length, additional_data,
2827 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002828 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002829 mbedtls_gcm_free( &gcm );
2830 }
2831 else if( alg == PSA_ALG_CCM )
2832 {
2833 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002834 tag_length = 16;
2835
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002836 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002837 return( PSA_ERROR_INVALID_ARGUMENT );
2838
mohammad160347ddf3d2018-04-26 01:11:21 +03002839 if( nonce_length < 7 || nonce_length > 13 )
2840 return( PSA_ERROR_INVALID_ARGUMENT );
2841
mohammad160315223a82018-06-03 17:19:55 +03002842 //make sure we have place to hold the tag in the ciphertext buffer
2843 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002844 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002845
2846 //update the tag pointer to point to the end of the ciphertext_length
2847 tag = ciphertext + plaintext_length;
2848
mohammad16035955c982018-04-26 00:53:03 +03002849 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002850 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002851 slot->data.raw.data,
2852 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002853 if( ret != 0 )
2854 {
2855 mbedtls_ccm_free( &ccm );
2856 return( mbedtls_to_psa_error( ret ) );
2857 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002858 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002859 nonce, nonce_length,
2860 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002861 additional_data_length,
2862 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002863 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002864 mbedtls_ccm_free( &ccm );
2865 }
mohammad16035c8845f2018-05-09 05:40:09 -07002866 else
2867 {
mohammad1603554faad2018-06-03 15:07:38 +03002868 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002869 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002870
mohammad160315223a82018-06-03 17:19:55 +03002871 if( ret != 0 )
2872 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002873 /* If ciphertext_size is 0 then ciphertext may be NULL and then the
2874 * call to memset would have undefined behavior. */
2875 if( ciphertext_size != 0 )
2876 memset( ciphertext, 0, ciphertext_size );
mohammad160315223a82018-06-03 17:19:55 +03002877 return( mbedtls_to_psa_error( ret ) );
2878 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002879
mohammad160315223a82018-06-03 17:19:55 +03002880 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002881 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002882}
2883
Gilles Peskineee652a32018-06-01 19:23:52 +02002884/* Locate the tag in a ciphertext buffer containing the encrypted data
2885 * followed by the tag. Return the length of the part preceding the tag in
2886 * *plaintext_length. This is the size of the plaintext in modes where
2887 * the encrypted data has the same size as the plaintext, such as
2888 * CCM and GCM. */
2889static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2890 const uint8_t *ciphertext,
2891 size_t ciphertext_length,
2892 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002893 const uint8_t **p_tag )
2894{
2895 size_t payload_length;
2896 if( tag_length > ciphertext_length )
2897 return( PSA_ERROR_INVALID_ARGUMENT );
2898 payload_length = ciphertext_length - tag_length;
2899 if( payload_length > plaintext_size )
2900 return( PSA_ERROR_BUFFER_TOO_SMALL );
2901 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002902 return( PSA_SUCCESS );
2903}
2904
mohammad16035955c982018-04-26 00:53:03 +03002905psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2906 psa_algorithm_t alg,
2907 const uint8_t *nonce,
2908 size_t nonce_length,
2909 const uint8_t *additional_data,
2910 size_t additional_data_length,
2911 const uint8_t *ciphertext,
2912 size_t ciphertext_length,
2913 uint8_t *plaintext,
2914 size_t plaintext_size,
2915 size_t *plaintext_length )
2916{
2917 int ret;
2918 psa_status_t status;
2919 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002920 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002921 const uint8_t *tag;
2922 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002923 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002924 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002925
Gilles Peskineee652a32018-06-01 19:23:52 +02002926 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002927
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002928 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2929 if( status != PSA_SUCCESS )
2930 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002931 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002932
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002933 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002934 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002935 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002936 return( PSA_ERROR_NOT_SUPPORTED );
2937
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002938 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002939 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002940 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002941
mohammad16035955c982018-04-26 00:53:03 +03002942 if( alg == PSA_ALG_GCM )
2943 {
2944 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002945
Gilles Peskineee652a32018-06-01 19:23:52 +02002946 tag_length = 16;
2947 status = psa_aead_unpadded_locate_tag( tag_length,
2948 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002949 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002950 if( status != PSA_SUCCESS )
2951 return( status );
2952
mohammad16035955c982018-04-26 00:53:03 +03002953 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002954 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002955 slot->data.raw.data,
2956 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002957 if( ret != 0 )
2958 {
2959 mbedtls_gcm_free( &gcm );
2960 return( mbedtls_to_psa_error( ret ) );
2961 }
mohammad16035955c982018-04-26 00:53:03 +03002962
Gilles Peskineee652a32018-06-01 19:23:52 +02002963 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002964 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002965 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002966 additional_data,
2967 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002968 tag, tag_length,
2969 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002970 mbedtls_gcm_free( &gcm );
2971 }
2972 else if( alg == PSA_ALG_CCM )
2973 {
2974 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002975
mohammad160347ddf3d2018-04-26 01:11:21 +03002976 if( nonce_length < 7 || nonce_length > 13 )
2977 return( PSA_ERROR_INVALID_ARGUMENT );
2978
mohammad16039375f842018-06-03 14:28:24 +03002979 tag_length = 16;
2980 status = psa_aead_unpadded_locate_tag( tag_length,
2981 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002982 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002983 if( status != PSA_SUCCESS )
2984 return( status );
2985
mohammad16035955c982018-04-26 00:53:03 +03002986 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002987 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002988 slot->data.raw.data,
2989 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002990 if( ret != 0 )
2991 {
2992 mbedtls_ccm_free( &ccm );
2993 return( mbedtls_to_psa_error( ret ) );
2994 }
mohammad160360a64d02018-06-03 17:20:42 +03002995 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002996 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002997 additional_data,
2998 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002999 ciphertext, plaintext,
3000 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03003001 mbedtls_ccm_free( &ccm );
3002 }
mohammad160339574652018-06-01 04:39:53 -07003003 else
3004 {
mohammad1603554faad2018-06-03 15:07:38 +03003005 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003006 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003007
Gilles Peskineee652a32018-06-01 19:23:52 +02003008 if( ret != 0 )
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003009 {
3010 /* If plaintext_size is 0 then plaintext may be NULL and then the
3011 * call to memset has undefined behavior. */
3012 if( plaintext_size != 0 )
3013 memset( plaintext, 0, plaintext_size );
3014 }
mohammad160360a64d02018-06-03 17:20:42 +03003015 else
3016 *plaintext_length = ciphertext_length - tag_length;
3017
Gilles Peskineee652a32018-06-01 19:23:52 +02003018 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03003019}
3020
Gilles Peskinea0655c32018-04-30 17:06:50 +02003021
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003022
Gilles Peskine20035e32018-02-03 22:44:14 +01003023/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003024/* Generators */
3025/****************************************************************/
3026
3027psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3028{
3029 psa_status_t status = PSA_SUCCESS;
3030 if( generator->alg == 0 )
3031 {
3032 /* The object has (apparently) been initialized but it is not
3033 * in use. It's ok to call abort on such an object, and there's
3034 * nothing to do. */
3035 }
3036 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003037#if defined(MBEDTLS_MD_C)
3038 if( PSA_ALG_IS_HKDF( generator->alg ) )
3039 {
3040 mbedtls_free( generator->ctx.hkdf.info );
3041 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3042 }
3043 else
3044#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003045 {
3046 status = PSA_ERROR_BAD_STATE;
3047 }
3048 memset( generator, 0, sizeof( *generator ) );
3049 return( status );
3050}
3051
3052
3053psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3054 size_t *capacity)
3055{
3056 *capacity = generator->capacity;
3057 return( PSA_SUCCESS );
3058}
3059
Gilles Peskinebef7f142018-07-12 17:22:21 +02003060#if defined(MBEDTLS_MD_C)
3061/* Read some bytes from an HKDF-based generator. This performs a chunk
3062 * of the expand phase of the HKDF algorithm. */
3063static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3064 psa_algorithm_t hash_alg,
3065 uint8_t *output,
3066 size_t output_length )
3067{
3068 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3069 psa_status_t status;
3070
3071 while( output_length != 0 )
3072 {
3073 /* Copy what remains of the current block */
3074 uint8_t n = hash_length - hkdf->offset_in_block;
3075 if( n > output_length )
3076 n = (uint8_t) output_length;
3077 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3078 output += n;
3079 output_length -= n;
3080 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003081 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003082 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003083 /* We can't be wanting more output after block 0xff, otherwise
3084 * the capacity check in psa_generator_read() would have
3085 * prevented this call. It could happen only if the generator
3086 * object was corrupted or if this function is called directly
3087 * inside the library. */
3088 if( hkdf->block_number == 0xff )
3089 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003090
3091 /* We need a new block */
3092 ++hkdf->block_number;
3093 hkdf->offset_in_block = 0;
3094 status = psa_hmac_setup_internal( &hkdf->hmac,
3095 hkdf->prk, hash_length,
3096 hash_alg );
3097 if( status != PSA_SUCCESS )
3098 return( status );
3099 if( hkdf->block_number != 1 )
3100 {
3101 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3102 hkdf->output_block,
3103 hash_length );
3104 if( status != PSA_SUCCESS )
3105 return( status );
3106 }
3107 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3108 hkdf->info,
3109 hkdf->info_length );
3110 if( status != PSA_SUCCESS )
3111 return( status );
3112 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3113 &hkdf->block_number, 1 );
3114 if( status != PSA_SUCCESS )
3115 return( status );
3116 status = psa_hmac_finish_internal( &hkdf->hmac,
3117 hkdf->output_block,
3118 sizeof( hkdf->output_block ) );
3119 if( status != PSA_SUCCESS )
3120 return( status );
3121 }
3122
3123 return( PSA_SUCCESS );
3124}
3125#endif /* MBEDTLS_MD_C */
3126
Gilles Peskineeab56e42018-07-12 17:12:33 +02003127psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3128 uint8_t *output,
3129 size_t output_length )
3130{
3131 psa_status_t status;
3132
3133 if( output_length > generator->capacity )
3134 {
3135 generator->capacity = 0;
3136 /* Go through the error path to wipe all confidential data now
3137 * that the generator object is useless. */
3138 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3139 goto exit;
3140 }
3141 if( output_length == 0 &&
3142 generator->capacity == 0 && generator->alg == 0 )
3143 {
3144 /* Edge case: this is a blank or finished generator, and 0
3145 * bytes were requested. The right error in this case could
3146 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3147 * INSUFFICIENT_CAPACITY, which is right for a finished
3148 * generator, for consistency with the case when
3149 * output_length > 0. */
3150 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3151 }
3152 generator->capacity -= output_length;
3153
Gilles Peskinebef7f142018-07-12 17:22:21 +02003154#if defined(MBEDTLS_MD_C)
3155 if( PSA_ALG_IS_HKDF( generator->alg ) )
3156 {
3157 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3158 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3159 output, output_length );
3160 }
3161 else
3162#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003163 {
3164 return( PSA_ERROR_BAD_STATE );
3165 }
3166
3167exit:
3168 if( status != PSA_SUCCESS )
3169 {
3170 psa_generator_abort( generator );
3171 memset( output, '!', output_length );
3172 }
3173 return( status );
3174}
3175
Gilles Peskine08542d82018-07-19 17:05:42 +02003176#if defined(MBEDTLS_DES_C)
3177static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3178{
3179 if( data_size >= 8 )
3180 mbedtls_des_key_set_parity( data );
3181 if( data_size >= 16 )
3182 mbedtls_des_key_set_parity( data + 8 );
3183 if( data_size >= 24 )
3184 mbedtls_des_key_set_parity( data + 16 );
3185}
3186#endif /* MBEDTLS_DES_C */
3187
Gilles Peskineeab56e42018-07-12 17:12:33 +02003188psa_status_t psa_generator_import_key( psa_key_slot_t key,
3189 psa_key_type_t type,
3190 size_t bits,
3191 psa_crypto_generator_t *generator )
3192{
3193 uint8_t *data = NULL;
3194 size_t bytes = PSA_BITS_TO_BYTES( bits );
3195 psa_status_t status;
3196
3197 if( ! key_type_is_raw_bytes( type ) )
3198 return( PSA_ERROR_INVALID_ARGUMENT );
3199 if( bits % 8 != 0 )
3200 return( PSA_ERROR_INVALID_ARGUMENT );
3201 data = mbedtls_calloc( 1, bytes );
3202 if( data == NULL )
3203 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3204
3205 status = psa_generator_read( generator, data, bytes );
3206 if( status != PSA_SUCCESS )
3207 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003208#if defined(MBEDTLS_DES_C)
3209 if( type == PSA_KEY_TYPE_DES )
3210 psa_des_set_key_parity( data, bytes );
3211#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003212 status = psa_import_key( key, type, data, bytes );
3213
3214exit:
3215 mbedtls_free( data );
3216 return( status );
3217}
3218
3219
3220
3221/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003222/* Key derivation */
3223/****************************************************************/
3224
Gilles Peskinebef7f142018-07-12 17:22:21 +02003225/* Set up an HKDF-based generator. This is exactly the extract phase
3226 * of the HKDF algorithm. */
3227static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
3228 key_slot_t *slot,
3229 psa_algorithm_t hash_alg,
3230 const uint8_t *salt,
3231 size_t salt_length,
3232 const uint8_t *label,
3233 size_t label_length )
3234{
3235 psa_status_t status;
3236 status = psa_hmac_setup_internal( &hkdf->hmac,
3237 salt, salt_length,
3238 PSA_ALG_HMAC_HASH( hash_alg ) );
3239 if( status != PSA_SUCCESS )
3240 return( status );
3241 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3242 slot->data.raw.data,
3243 slot->data.raw.bytes );
3244 if( status != PSA_SUCCESS )
3245 return( status );
3246 status = psa_hmac_finish_internal( &hkdf->hmac,
3247 hkdf->prk,
3248 sizeof( hkdf->prk ) );
3249 if( status != PSA_SUCCESS )
3250 return( status );
3251 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3252 hkdf->block_number = 0;
3253 hkdf->info_length = label_length;
3254 if( label_length != 0 )
3255 {
3256 hkdf->info = mbedtls_calloc( 1, label_length );
3257 if( hkdf->info == NULL )
3258 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3259 memcpy( hkdf->info, label, label_length );
3260 }
3261 return( PSA_SUCCESS );
3262}
3263
Gilles Peskineea0fb492018-07-12 17:17:20 +02003264psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01003265 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003266 psa_algorithm_t alg,
3267 const uint8_t *salt,
3268 size_t salt_length,
3269 const uint8_t *label,
3270 size_t label_length,
3271 size_t capacity )
3272{
3273 key_slot_t *slot;
3274 psa_status_t status;
3275
3276 if( generator->alg != 0 )
3277 return( PSA_ERROR_BAD_STATE );
3278
3279 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
3280 if( status != PSA_SUCCESS )
3281 return( status );
3282 if( slot->type != PSA_KEY_TYPE_DERIVE )
3283 return( PSA_ERROR_INVALID_ARGUMENT );
3284
3285 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
3286 return( PSA_ERROR_INVALID_ARGUMENT );
3287
Gilles Peskinebef7f142018-07-12 17:22:21 +02003288#if defined(MBEDTLS_MD_C)
3289 if( PSA_ALG_IS_HKDF( alg ) )
3290 {
3291 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3292 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3293 if( hash_size == 0 )
3294 return( PSA_ERROR_NOT_SUPPORTED );
3295 if( capacity > 255 * hash_size )
3296 return( PSA_ERROR_INVALID_ARGUMENT );
3297 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3298 slot,
3299 hash_alg,
3300 salt, salt_length,
3301 label, label_length );
3302 }
3303 else
3304#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +02003305 {
3306 return( PSA_ERROR_NOT_SUPPORTED );
3307 }
3308
3309 /* Set generator->alg even on failure so that abort knows what to do. */
3310 generator->alg = alg;
3311 if( status == PSA_SUCCESS )
3312 generator->capacity = capacity;
3313 else
3314 psa_generator_abort( generator );
3315 return( status );
3316}
3317
3318
3319
3320/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003321/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02003322/****************************************************************/
3323
3324psa_status_t psa_generate_random( uint8_t *output,
3325 size_t output_size )
3326{
3327 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3328 output, output_size );
3329 return( mbedtls_to_psa_error( ret ) );
3330}
3331
3332psa_status_t psa_generate_key( psa_key_slot_t key,
3333 psa_key_type_t type,
3334 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003335 const void *extra,
3336 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02003337{
Gilles Peskine12313cd2018-06-20 00:20:32 +02003338 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003339 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003340
Gilles Peskine53d991e2018-07-12 01:14:59 +02003341 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003342 return( PSA_ERROR_INVALID_ARGUMENT );
3343
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003344 status = psa_get_empty_key_slot( key, &slot );
3345 if( status != PSA_SUCCESS )
3346 return( status );
3347
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003348 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003349 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003350 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003351 if( status != PSA_SUCCESS )
3352 return( status );
3353 status = psa_generate_random( slot->data.raw.data,
3354 slot->data.raw.bytes );
3355 if( status != PSA_SUCCESS )
3356 {
3357 mbedtls_free( slot->data.raw.data );
3358 return( status );
3359 }
3360#if defined(MBEDTLS_DES_C)
3361 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02003362 psa_des_set_key_parity( slot->data.raw.data,
3363 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003364#endif /* MBEDTLS_DES_C */
3365 }
3366 else
3367
3368#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3369 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3370 {
3371 mbedtls_rsa_context *rsa;
3372 int ret;
3373 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003374 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3375 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02003376 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003377 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02003378 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003379 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003380 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003381#if INT_MAX < 0xffffffff
3382 /* Check that the uint32_t value passed by the caller fits
3383 * in the range supported by this implementation. */
3384 if( p->e > INT_MAX )
3385 return( PSA_ERROR_NOT_SUPPORTED );
3386#endif
3387 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003388 }
3389 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3390 if( rsa == NULL )
3391 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3392 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3393 ret = mbedtls_rsa_gen_key( rsa,
3394 mbedtls_ctr_drbg_random,
3395 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003396 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003397 exponent );
3398 if( ret != 0 )
3399 {
3400 mbedtls_rsa_free( rsa );
3401 mbedtls_free( rsa );
3402 return( mbedtls_to_psa_error( ret ) );
3403 }
3404 slot->data.rsa = rsa;
3405 }
3406 else
3407#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3408
3409#if defined(MBEDTLS_ECP_C)
3410 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3411 {
3412 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3413 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3414 const mbedtls_ecp_curve_info *curve_info =
3415 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3416 mbedtls_ecp_keypair *ecp;
3417 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003418 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003419 return( PSA_ERROR_NOT_SUPPORTED );
3420 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3421 return( PSA_ERROR_NOT_SUPPORTED );
3422 if( curve_info->bit_size != bits )
3423 return( PSA_ERROR_INVALID_ARGUMENT );
3424 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3425 if( ecp == NULL )
3426 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3427 mbedtls_ecp_keypair_init( ecp );
3428 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3429 mbedtls_ctr_drbg_random,
3430 &global_data.ctr_drbg );
3431 if( ret != 0 )
3432 {
3433 mbedtls_ecp_keypair_free( ecp );
3434 mbedtls_free( ecp );
3435 return( mbedtls_to_psa_error( ret ) );
3436 }
3437 slot->data.ecp = ecp;
3438 }
3439 else
3440#endif /* MBEDTLS_ECP_C */
3441
3442 return( PSA_ERROR_NOT_SUPPORTED );
3443
3444 slot->type = type;
3445 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003446}
3447
3448
3449/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003450/* Module setup */
3451/****************************************************************/
3452
Gilles Peskinee59236f2018-01-27 23:32:46 +01003453void mbedtls_psa_crypto_free( void )
3454{
Jaeden Amero045bd502018-06-26 14:00:08 +01003455 psa_key_slot_t key;
Gilles Peskine9a056342018-08-01 15:46:54 +02003456 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003457 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003458 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3459 mbedtls_entropy_free( &global_data.entropy );
3460 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3461}
3462
3463psa_status_t psa_crypto_init( void )
3464{
3465 int ret;
3466 const unsigned char drbg_seed[] = "PSA";
3467
3468 if( global_data.initialized != 0 )
3469 return( PSA_SUCCESS );
3470
3471 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3472 mbedtls_entropy_init( &global_data.entropy );
3473 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3474
3475 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3476 mbedtls_entropy_func,
3477 &global_data.entropy,
3478 drbg_seed, sizeof( drbg_seed ) - 1 );
3479 if( ret != 0 )
3480 goto exit;
3481
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003482 global_data.initialized = 1;
3483
Gilles Peskinee59236f2018-01-27 23:32:46 +01003484exit:
3485 if( ret != 0 )
3486 mbedtls_psa_crypto_free( );
3487 return( mbedtls_to_psa_error( ret ) );
3488}
3489
3490#endif /* MBEDTLS_PSA_CRYPTO_C */