blob: ab9ec725e704ef4bfa280216b4de0a0b4634250d [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030029/*
30 * In case MBEDTLS_PSA_CRYPTO_SPM is defined the code is built for SPM (Secure
31 * Partition Manager) integration which separate the code into two parts
32 * NSPE (Non-Secure Process Environment) and SPE (Secure Process Environment).
33 * In this mode an additional header file should be included.
34 */
mohammad160327010052018-07-03 13:16:15 +030035#if defined(MBEDTLS_PSA_CRYPTO_SPM)
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +030036/*
37 * PSA_CRYPTO_SECURE means that this file is compiled to the SPE side.
38 * some headers will be affected by this flag.
39 */
mohammad160327010052018-07-03 13:16:15 +030040#define PSA_CRYPTO_SECURE 1
41#include "crypto_spe.h"
42#endif
43
Gilles Peskinee59236f2018-01-27 23:32:46 +010044#include "psa/crypto.h"
45
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include <stdlib.h>
47#include <string.h>
48#if defined(MBEDTLS_PLATFORM_C)
49#include "mbedtls/platform.h"
50#else
51#define mbedtls_calloc calloc
52#define mbedtls_free free
53#endif
54
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020056#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020057#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/blowfish.h"
59#include "mbedtls/camellia.h"
60#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010065#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010066#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/error.h"
68#include "mbedtls/gcm.h"
69#include "mbedtls/md2.h"
70#include "mbedtls/md4.h"
71#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010072#include "mbedtls/md.h"
73#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010074#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010075#include "mbedtls/pk_internal.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010077#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/sha1.h"
79#include "mbedtls/sha256.h"
80#include "mbedtls/sha512.h"
81#include "mbedtls/xtea.h"
82
Gilles Peskinee59236f2018-01-27 23:32:46 +010083
84
Gilles Peskine996deb12018-08-01 15:45:45 +020085#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
86
Gilles Peskinee59236f2018-01-27 23:32:46 +010087/* Implementation that should never be optimized out by the compiler */
88static void mbedtls_zeroize( void *v, size_t n )
89{
90 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
91}
92
Gilles Peskine9ef733f2018-02-07 21:05:37 +010093/* constant-time buffer comparison */
94static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
95{
96 size_t i;
97 unsigned char diff = 0;
98
99 for( i = 0; i < n; i++ )
100 diff |= a[i] ^ b[i];
101
102 return( diff );
103}
104
105
106
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100107/****************************************************************/
108/* Global data, support functions and library management */
109/****************************************************************/
110
111/* Number of key slots (plus one because 0 is not used).
112 * The value is a compile-time constant for now, for simplicity. */
Gilles Peskine828ed142018-06-18 23:25:51 +0200113#define PSA_KEY_SLOT_COUNT 32
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100114
Gilles Peskine2d277862018-06-18 15:41:12 +0200115typedef struct
116{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100117 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +0300118 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200119 psa_key_lifetime_t lifetime;
Gilles Peskine2d277862018-06-18 15:41:12 +0200120 union
121 {
122 struct raw_data
123 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100124 uint8_t *data;
125 size_t bytes;
126 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100127#if defined(MBEDTLS_RSA_C)
128 mbedtls_rsa_context *rsa;
129#endif /* MBEDTLS_RSA_C */
130#if defined(MBEDTLS_ECP_C)
131 mbedtls_ecp_keypair *ecp;
132#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100133 } data;
134} key_slot_t;
135
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200136static int key_type_is_raw_bytes( psa_key_type_t type )
137{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200138 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200139}
140
Gilles Peskine2d277862018-06-18 15:41:12 +0200141typedef struct
142{
Gilles Peskinee59236f2018-01-27 23:32:46 +0100143 int initialized;
144 mbedtls_entropy_context entropy;
145 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200146 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinee59236f2018-01-27 23:32:46 +0100147} psa_global_data_t;
148
149static psa_global_data_t global_data;
150
itayzafrir0adf0fc2018-09-06 16:24:41 +0300151#define GUARD_MODULE_INITIALIZED \
152 if( global_data.initialized == 0 ) \
153 return( PSA_ERROR_BAD_STATE );
154
Gilles Peskinee59236f2018-01-27 23:32:46 +0100155static psa_status_t mbedtls_to_psa_error( int ret )
156{
Gilles Peskinea5905292018-02-07 20:59:33 +0100157 /* If there's both a high-level code and low-level code, dispatch on
158 * the high-level code. */
159 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100160 {
161 case 0:
162 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100163
164 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
165 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
166 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
167 return( PSA_ERROR_NOT_SUPPORTED );
168 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
169 return( PSA_ERROR_HARDWARE_FAILURE );
170
171 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
172 return( PSA_ERROR_HARDWARE_FAILURE );
173
Gilles Peskine9a944802018-06-21 09:35:35 +0200174 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
175 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
176 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
177 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
178 case MBEDTLS_ERR_ASN1_INVALID_DATA:
179 return( PSA_ERROR_INVALID_ARGUMENT );
180 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
181 return( PSA_ERROR_INSUFFICIENT_MEMORY );
182 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
183 return( PSA_ERROR_BUFFER_TOO_SMALL );
184
Gilles Peskinea5905292018-02-07 20:59:33 +0100185 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
186 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
187 return( PSA_ERROR_NOT_SUPPORTED );
188 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
189 return( PSA_ERROR_HARDWARE_FAILURE );
190
191 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
192 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
193 return( PSA_ERROR_NOT_SUPPORTED );
194 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
195 return( PSA_ERROR_HARDWARE_FAILURE );
196
197 case MBEDTLS_ERR_CCM_BAD_INPUT:
198 return( PSA_ERROR_INVALID_ARGUMENT );
199 case MBEDTLS_ERR_CCM_AUTH_FAILED:
200 return( PSA_ERROR_INVALID_SIGNATURE );
201 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
202 return( PSA_ERROR_HARDWARE_FAILURE );
203
204 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
205 return( PSA_ERROR_NOT_SUPPORTED );
206 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
207 return( PSA_ERROR_INVALID_ARGUMENT );
208 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
209 return( PSA_ERROR_INSUFFICIENT_MEMORY );
210 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
211 return( PSA_ERROR_INVALID_PADDING );
212 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
213 return( PSA_ERROR_BAD_STATE );
214 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
215 return( PSA_ERROR_INVALID_SIGNATURE );
216 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
217 return( PSA_ERROR_TAMPERING_DETECTED );
218 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
219 return( PSA_ERROR_HARDWARE_FAILURE );
220
221 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
222 return( PSA_ERROR_HARDWARE_FAILURE );
223
224 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
225 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
226 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
227 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
228 return( PSA_ERROR_NOT_SUPPORTED );
229 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
230 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
231
232 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
233 return( PSA_ERROR_NOT_SUPPORTED );
234 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
235 return( PSA_ERROR_HARDWARE_FAILURE );
236
Gilles Peskinee59236f2018-01-27 23:32:46 +0100237 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
238 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
239 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
240 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100241
242 case MBEDTLS_ERR_GCM_AUTH_FAILED:
243 return( PSA_ERROR_INVALID_SIGNATURE );
244 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200245 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100246 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
247 return( PSA_ERROR_HARDWARE_FAILURE );
248
249 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
250 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
251 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
252 return( PSA_ERROR_HARDWARE_FAILURE );
253
254 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
255 return( PSA_ERROR_NOT_SUPPORTED );
256 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
257 return( PSA_ERROR_INVALID_ARGUMENT );
258 case MBEDTLS_ERR_MD_ALLOC_FAILED:
259 return( PSA_ERROR_INSUFFICIENT_MEMORY );
260 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
261 return( PSA_ERROR_STORAGE_FAILURE );
262 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
263 return( PSA_ERROR_HARDWARE_FAILURE );
264
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100265 case MBEDTLS_ERR_PK_ALLOC_FAILED:
266 return( PSA_ERROR_INSUFFICIENT_MEMORY );
267 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
268 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100271 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100272 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
273 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
276 return( PSA_ERROR_NOT_SUPPORTED );
277 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
278 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
279 return( PSA_ERROR_NOT_PERMITTED );
280 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
281 return( PSA_ERROR_INVALID_ARGUMENT );
282 case MBEDTLS_ERR_PK_INVALID_ALG:
283 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
284 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
285 return( PSA_ERROR_NOT_SUPPORTED );
286 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
287 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100288 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
289 return( PSA_ERROR_HARDWARE_FAILURE );
290
291 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
292 return( PSA_ERROR_HARDWARE_FAILURE );
293
294 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
295 return( PSA_ERROR_INVALID_ARGUMENT );
296 case MBEDTLS_ERR_RSA_INVALID_PADDING:
297 return( PSA_ERROR_INVALID_PADDING );
298 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
299 return( PSA_ERROR_HARDWARE_FAILURE );
300 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
301 return( PSA_ERROR_INVALID_ARGUMENT );
302 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
303 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
304 return( PSA_ERROR_TAMPERING_DETECTED );
305 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
306 return( PSA_ERROR_INVALID_SIGNATURE );
307 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
308 return( PSA_ERROR_BUFFER_TOO_SMALL );
309 case MBEDTLS_ERR_RSA_RNG_FAILED:
310 return( PSA_ERROR_INSUFFICIENT_MEMORY );
311 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
312 return( PSA_ERROR_NOT_SUPPORTED );
313 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
314 return( PSA_ERROR_HARDWARE_FAILURE );
315
316 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
317 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
318 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
319 return( PSA_ERROR_HARDWARE_FAILURE );
320
321 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
322 return( PSA_ERROR_INVALID_ARGUMENT );
323 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
324 return( PSA_ERROR_HARDWARE_FAILURE );
325
itayzafrir5c753392018-05-08 11:18:38 +0300326 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300327 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300328 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300329 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
330 return( PSA_ERROR_BUFFER_TOO_SMALL );
331 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
332 return( PSA_ERROR_NOT_SUPPORTED );
333 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
334 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
335 return( PSA_ERROR_INVALID_SIGNATURE );
336 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
337 return( PSA_ERROR_INSUFFICIENT_MEMORY );
338 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
339 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300340
Gilles Peskinee59236f2018-01-27 23:32:46 +0100341 default:
342 return( PSA_ERROR_UNKNOWN_ERROR );
343 }
344}
345
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200346/* Retrieve a key slot, occupied or not. */
347static psa_status_t psa_get_key_slot( psa_key_slot_t key,
348 key_slot_t **p_slot )
349{
itayzafrir90d8c7a2018-09-12 11:44:52 +0300350 GUARD_MODULE_INITIALIZED;
351
Gilles Peskine996deb12018-08-01 15:45:45 +0200352 /* 0 is not a valid slot number under any circumstance. This
353 * implementation provides slots number 1 to N where N is the
354 * number of available slots. */
355 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200356 return( PSA_ERROR_INVALID_ARGUMENT );
357
Gilles Peskine996deb12018-08-01 15:45:45 +0200358 *p_slot = &global_data.key_slots[key - 1];
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200359 return( PSA_SUCCESS );
360}
361
362/* Retrieve an empty key slot (slot with no key data, but possibly
363 * with some metadata such as a policy). */
364static psa_status_t psa_get_empty_key_slot( psa_key_slot_t key,
365 key_slot_t **p_slot )
366{
367 psa_status_t status;
368 key_slot_t *slot = NULL;
369
370 *p_slot = NULL;
371
372 status = psa_get_key_slot( key, &slot );
373 if( status != PSA_SUCCESS )
374 return( status );
375
376 if( slot->type != PSA_KEY_TYPE_NONE )
377 return( PSA_ERROR_OCCUPIED_SLOT );
378
379 *p_slot = slot;
380 return( status );
381}
382
Jaeden Amerob4fa8c92018-07-11 15:57:44 +0100383/** Retrieve a slot which must contain a key. The key must have allow all the
384 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
385 * operations with this algorithm. */
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200386static psa_status_t psa_get_key_from_slot( psa_key_slot_t key,
387 key_slot_t **p_slot,
388 psa_key_usage_t usage,
389 psa_algorithm_t alg )
390{
391 psa_status_t status;
392 key_slot_t *slot = NULL;
393
394 *p_slot = NULL;
395
396 status = psa_get_key_slot( key, &slot );
397 if( status != PSA_SUCCESS )
398 return( status );
399 if( slot->type == PSA_KEY_TYPE_NONE )
400 return( PSA_ERROR_EMPTY_SLOT );
401
402 /* Enforce that usage policy for the key slot contains all the flags
403 * required by the usage parameter. There is one exception: public
404 * keys can always be exported, so we treat public key objects as
405 * if they had the export flag. */
406 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
407 usage &= ~PSA_KEY_USAGE_EXPORT;
408 if( ( slot->policy.usage & usage ) != usage )
409 return( PSA_ERROR_NOT_PERMITTED );
410 if( alg != 0 && ( alg != slot->policy.alg ) )
411 return( PSA_ERROR_NOT_PERMITTED );
412
413 *p_slot = slot;
414 return( PSA_SUCCESS );
415}
416
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200417
418
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100419/****************************************************************/
420/* Key management */
421/****************************************************************/
422
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100423#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200424static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
425{
426 switch( grpid )
427 {
428 case MBEDTLS_ECP_DP_SECP192R1:
429 return( PSA_ECC_CURVE_SECP192R1 );
430 case MBEDTLS_ECP_DP_SECP224R1:
431 return( PSA_ECC_CURVE_SECP224R1 );
432 case MBEDTLS_ECP_DP_SECP256R1:
433 return( PSA_ECC_CURVE_SECP256R1 );
434 case MBEDTLS_ECP_DP_SECP384R1:
435 return( PSA_ECC_CURVE_SECP384R1 );
436 case MBEDTLS_ECP_DP_SECP521R1:
437 return( PSA_ECC_CURVE_SECP521R1 );
438 case MBEDTLS_ECP_DP_BP256R1:
439 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
440 case MBEDTLS_ECP_DP_BP384R1:
441 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
442 case MBEDTLS_ECP_DP_BP512R1:
443 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
444 case MBEDTLS_ECP_DP_CURVE25519:
445 return( PSA_ECC_CURVE_CURVE25519 );
446 case MBEDTLS_ECP_DP_SECP192K1:
447 return( PSA_ECC_CURVE_SECP192K1 );
448 case MBEDTLS_ECP_DP_SECP224K1:
449 return( PSA_ECC_CURVE_SECP224K1 );
450 case MBEDTLS_ECP_DP_SECP256K1:
451 return( PSA_ECC_CURVE_SECP256K1 );
452 case MBEDTLS_ECP_DP_CURVE448:
453 return( PSA_ECC_CURVE_CURVE448 );
454 default:
455 return( 0 );
456 }
457}
458
Gilles Peskine12313cd2018-06-20 00:20:32 +0200459static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
460{
461 switch( curve )
462 {
463 case PSA_ECC_CURVE_SECP192R1:
464 return( MBEDTLS_ECP_DP_SECP192R1 );
465 case PSA_ECC_CURVE_SECP224R1:
466 return( MBEDTLS_ECP_DP_SECP224R1 );
467 case PSA_ECC_CURVE_SECP256R1:
468 return( MBEDTLS_ECP_DP_SECP256R1 );
469 case PSA_ECC_CURVE_SECP384R1:
470 return( MBEDTLS_ECP_DP_SECP384R1 );
471 case PSA_ECC_CURVE_SECP521R1:
472 return( MBEDTLS_ECP_DP_SECP521R1 );
473 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
474 return( MBEDTLS_ECP_DP_BP256R1 );
475 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
476 return( MBEDTLS_ECP_DP_BP384R1 );
477 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
478 return( MBEDTLS_ECP_DP_BP512R1 );
479 case PSA_ECC_CURVE_CURVE25519:
480 return( MBEDTLS_ECP_DP_CURVE25519 );
481 case PSA_ECC_CURVE_SECP192K1:
482 return( MBEDTLS_ECP_DP_SECP192K1 );
483 case PSA_ECC_CURVE_SECP224K1:
484 return( MBEDTLS_ECP_DP_SECP224K1 );
485 case PSA_ECC_CURVE_SECP256K1:
486 return( MBEDTLS_ECP_DP_SECP256K1 );
487 case PSA_ECC_CURVE_CURVE448:
488 return( MBEDTLS_ECP_DP_CURVE448 );
489 default:
490 return( MBEDTLS_ECP_DP_NONE );
491 }
492}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100493#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200494
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
496 size_t bits,
497 struct raw_data *raw )
498{
499 /* Check that the bit size is acceptable for the key type */
500 switch( type )
501 {
502 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200503 if( bits == 0 )
504 {
505 raw->bytes = 0;
506 raw->data = NULL;
507 return( PSA_SUCCESS );
508 }
509 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200510#if defined(MBEDTLS_MD_C)
511 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200512#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200513 case PSA_KEY_TYPE_DERIVE:
514 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200515#if defined(MBEDTLS_AES_C)
516 case PSA_KEY_TYPE_AES:
517 if( bits != 128 && bits != 192 && bits != 256 )
518 return( PSA_ERROR_INVALID_ARGUMENT );
519 break;
520#endif
521#if defined(MBEDTLS_CAMELLIA_C)
522 case PSA_KEY_TYPE_CAMELLIA:
523 if( bits != 128 && bits != 192 && bits != 256 )
524 return( PSA_ERROR_INVALID_ARGUMENT );
525 break;
526#endif
527#if defined(MBEDTLS_DES_C)
528 case PSA_KEY_TYPE_DES:
529 if( bits != 64 && bits != 128 && bits != 192 )
530 return( PSA_ERROR_INVALID_ARGUMENT );
531 break;
532#endif
533#if defined(MBEDTLS_ARC4_C)
534 case PSA_KEY_TYPE_ARC4:
535 if( bits < 8 || bits > 2048 )
536 return( PSA_ERROR_INVALID_ARGUMENT );
537 break;
538#endif
539 default:
540 return( PSA_ERROR_NOT_SUPPORTED );
541 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200542 if( bits % 8 != 0 )
543 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200544
545 /* Allocate memory for the key */
546 raw->bytes = PSA_BITS_TO_BYTES( bits );
547 raw->data = mbedtls_calloc( 1, raw->bytes );
548 if( raw->data == NULL )
549 {
550 raw->bytes = 0;
551 return( PSA_ERROR_INSUFFICIENT_MEMORY );
552 }
553 return( PSA_SUCCESS );
554}
555
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200556#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
557static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
558 mbedtls_rsa_context **p_rsa )
559{
560 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
561 return( PSA_ERROR_INVALID_ARGUMENT );
562 else
563 {
564 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
565 size_t bits = mbedtls_rsa_get_bitlen( rsa );
566 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
567 return( PSA_ERROR_NOT_SUPPORTED );
568 *p_rsa = rsa;
569 return( PSA_SUCCESS );
570 }
571}
572#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
573
574#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
575static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
576 mbedtls_pk_context *pk,
577 mbedtls_ecp_keypair **p_ecp )
578{
579 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
580 return( PSA_ERROR_INVALID_ARGUMENT );
581 else
582 {
583 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
584 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
585 if( actual_curve != expected_curve )
586 return( PSA_ERROR_INVALID_ARGUMENT );
587 *p_ecp = ecp;
588 return( PSA_SUCCESS );
589 }
590}
591#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
592
Gilles Peskine2d277862018-06-18 15:41:12 +0200593psa_status_t psa_import_key( psa_key_slot_t key,
594 psa_key_type_t type,
595 const uint8_t *data,
596 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100597{
598 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200599 psa_status_t status = PSA_SUCCESS;
600 status = psa_get_empty_key_slot( key, &slot );
601 if( status != PSA_SUCCESS )
602 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100603
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200604 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100605 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100606 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100607 if( data_length > SIZE_MAX / 8 )
608 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200609 status = prepare_raw_data_slot( type,
610 PSA_BYTES_TO_BITS( data_length ),
611 &slot->data.raw );
612 if( status != PSA_SUCCESS )
613 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200614 if( data_length != 0 )
615 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100616 }
617 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100618#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200619 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100620 {
621 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100622 mbedtls_pk_context pk;
623 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200624
625 /* Parse the data. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100626 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
627 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
628 else
629 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100630 if( ret != 0 )
631 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200632
633 /* We have something that the pkparse module recognizes.
634 * If it has the expected type and passes any type-specific
635 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100636#if defined(MBEDTLS_RSA_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200637 if( PSA_KEY_TYPE_IS_RSA( type ) )
638 status = psa_import_rsa_key( &pk, &slot->data.rsa );
639 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100640#endif /* MBEDTLS_RSA_C */
641#if defined(MBEDTLS_ECP_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200642 if( PSA_KEY_TYPE_IS_ECC( type ) )
643 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( type ),
644 &pk, &slot->data.ecp );
645 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100646#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200647 {
648 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200649 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200650
Gilles Peskinec648d692018-06-28 08:46:13 +0200651 /* Free the content of the pk object only on error. On success,
652 * the content of the object has been stored in the slot. */
653 if( status != PSA_SUCCESS )
654 {
655 mbedtls_pk_free( &pk );
656 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100657 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100658 }
659 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100660#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100661 {
662 return( PSA_ERROR_NOT_SUPPORTED );
663 }
664
665 slot->type = type;
666 return( PSA_SUCCESS );
667}
668
Gilles Peskine2d277862018-06-18 15:41:12 +0200669psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100670{
671 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200672 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100673
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200674 status = psa_get_key_slot( key, &slot );
675 if( status != PSA_SUCCESS )
676 return( status );
677
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200679 {
680 /* No key material to clean, but do zeroize the slot below to wipe
681 * metadata such as policies. */
682 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200683 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100684 {
685 mbedtls_free( slot->data.raw.data );
686 }
687 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100688#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200689 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100690 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100691 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100692 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100693 }
694 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100695#endif /* defined(MBEDTLS_RSA_C) */
696#if defined(MBEDTLS_ECP_C)
697 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
698 {
699 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100700 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100701 }
702 else
703#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100704 {
705 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100706 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100707 return( PSA_ERROR_TAMPERING_DETECTED );
708 }
709
710 mbedtls_zeroize( slot, sizeof( *slot ) );
711 return( PSA_SUCCESS );
712}
713
Gilles Peskineb870b182018-07-06 16:02:09 +0200714/* Return the size of the key in the given slot, in bits. */
715static size_t psa_get_key_bits( const key_slot_t *slot )
716{
717 if( key_type_is_raw_bytes( slot->type ) )
718 return( slot->data.raw.bytes * 8 );
719#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200720 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineb870b182018-07-06 16:02:09 +0200721 return( mbedtls_rsa_get_bitlen( slot->data.rsa ) );
722#endif /* defined(MBEDTLS_RSA_C) */
723#if defined(MBEDTLS_ECP_C)
724 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
725 return( slot->data.ecp->grp.pbits );
726#endif /* defined(MBEDTLS_ECP_C) */
727 /* Shouldn't happen except on an empty slot. */
728 return( 0 );
729}
730
Gilles Peskine2d277862018-06-18 15:41:12 +0200731psa_status_t psa_get_key_information( psa_key_slot_t key,
732 psa_key_type_t *type,
733 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100734{
735 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200736 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100737
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200739 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740 if( bits != NULL )
741 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200742 status = psa_get_key_slot( key, &slot );
743 if( status != PSA_SUCCESS )
744 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200745
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746 if( slot->type == PSA_KEY_TYPE_NONE )
747 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200748 if( type != NULL )
749 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200750 if( bits != NULL )
751 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100752 return( PSA_SUCCESS );
753}
754
Gilles Peskine2d277862018-06-18 15:41:12 +0200755static psa_status_t psa_internal_export_key( psa_key_slot_t key,
756 uint8_t *data,
757 size_t data_size,
758 size_t *data_length,
759 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100760{
761 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200762 psa_status_t status;
763 /* Exporting a public key doesn't require a usage flag. If we're
764 * called by psa_export_public_key(), don't require the EXPORT flag.
765 * If we're called by psa_export_key(), do require the EXPORT flag;
766 * if the key turns out to be public key object, psa_get_key_from_slot()
767 * will ignore this flag. */
768 psa_key_usage_t usage = export_public_key ? 0 : PSA_KEY_USAGE_EXPORT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100769
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100770 /* Set the key to empty now, so that even when there are errors, we always
771 * set data_length to a value between 0 and data_size. On error, setting
772 * the key to empty is a good choice because an empty key representation is
773 * unlikely to be accepted anywhere. */
774 *data_length = 0;
775
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200776 status = psa_get_key_from_slot( key, &slot, usage, 0 );
777 if( status != PSA_SUCCESS )
778 return( status );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200779 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300780 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300781
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200782 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100783 {
784 if( slot->data.raw.bytes > data_size )
785 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200786 if( slot->data.raw.bytes != 0 )
787 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100788 *data_length = slot->data.raw.bytes;
789 return( PSA_SUCCESS );
790 }
791 else
Moran Peker17e36e12018-05-02 12:55:20 +0300792 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100793#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200794 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300795 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100796 {
Moran Pekera998bc62018-04-16 18:16:20 +0300797 mbedtls_pk_context pk;
798 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +0200799 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300800 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100801#if defined(MBEDTLS_RSA_C)
802 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300803 pk.pk_info = &mbedtls_rsa_info;
804 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100805#else
806 return( PSA_ERROR_NOT_SUPPORTED );
807#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300808 }
809 else
810 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100811#if defined(MBEDTLS_ECP_C)
812 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300813 pk.pk_info = &mbedtls_eckey_info;
814 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100815#else
816 return( PSA_ERROR_NOT_SUPPORTED );
817#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300818 }
Moran Pekerd7326592018-05-29 16:56:39 +0300819 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300820 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300821 else
822 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300823 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200824 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200825 /* If data_size is 0 then data may be NULL and then the
826 * call to memset would have undefined behavior. */
827 if( data_size != 0 )
828 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300829 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200830 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200831 /* The mbedtls_pk_xxx functions write to the end of the buffer.
832 * Move the data to the beginning and erase remaining data
833 * at the original location. */
834 if( 2 * (size_t) ret <= data_size )
835 {
836 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200837 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200838 }
839 else if( (size_t) ret < data_size )
840 {
841 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200842 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200843 }
Moran Pekera998bc62018-04-16 18:16:20 +0300844 *data_length = ret;
845 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100846 }
847 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100848#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300849 {
850 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200851 it is valid for a special-purpose implementation to omit
852 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300853 return( PSA_ERROR_NOT_SUPPORTED );
854 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100855 }
856}
857
Gilles Peskine2d277862018-06-18 15:41:12 +0200858psa_status_t psa_export_key( psa_key_slot_t key,
859 uint8_t *data,
860 size_t data_size,
861 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300862{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200863 return( psa_internal_export_key( key, data, data_size,
864 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100865}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100866
Gilles Peskine2d277862018-06-18 15:41:12 +0200867psa_status_t psa_export_public_key( psa_key_slot_t key,
868 uint8_t *data,
869 size_t data_size,
870 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300871{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200872 return( psa_internal_export_key( key, data, data_size,
873 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300874}
875
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200876
877
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100878/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100879/* Message digests */
880/****************************************************************/
881
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100882static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100883{
884 switch( alg )
885 {
886#if defined(MBEDTLS_MD2_C)
887 case PSA_ALG_MD2:
888 return( &mbedtls_md2_info );
889#endif
890#if defined(MBEDTLS_MD4_C)
891 case PSA_ALG_MD4:
892 return( &mbedtls_md4_info );
893#endif
894#if defined(MBEDTLS_MD5_C)
895 case PSA_ALG_MD5:
896 return( &mbedtls_md5_info );
897#endif
898#if defined(MBEDTLS_RIPEMD160_C)
899 case PSA_ALG_RIPEMD160:
900 return( &mbedtls_ripemd160_info );
901#endif
902#if defined(MBEDTLS_SHA1_C)
903 case PSA_ALG_SHA_1:
904 return( &mbedtls_sha1_info );
905#endif
906#if defined(MBEDTLS_SHA256_C)
907 case PSA_ALG_SHA_224:
908 return( &mbedtls_sha224_info );
909 case PSA_ALG_SHA_256:
910 return( &mbedtls_sha256_info );
911#endif
912#if defined(MBEDTLS_SHA512_C)
913 case PSA_ALG_SHA_384:
914 return( &mbedtls_sha384_info );
915 case PSA_ALG_SHA_512:
916 return( &mbedtls_sha512_info );
917#endif
918 default:
919 return( NULL );
920 }
921}
922
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100923psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
924{
925 switch( operation->alg )
926 {
Gilles Peskine81736312018-06-26 15:04:31 +0200927 case 0:
928 /* The object has (apparently) been initialized but it is not
929 * in use. It's ok to call abort on such an object, and there's
930 * nothing to do. */
931 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100932#if defined(MBEDTLS_MD2_C)
933 case PSA_ALG_MD2:
934 mbedtls_md2_free( &operation->ctx.md2 );
935 break;
936#endif
937#if defined(MBEDTLS_MD4_C)
938 case PSA_ALG_MD4:
939 mbedtls_md4_free( &operation->ctx.md4 );
940 break;
941#endif
942#if defined(MBEDTLS_MD5_C)
943 case PSA_ALG_MD5:
944 mbedtls_md5_free( &operation->ctx.md5 );
945 break;
946#endif
947#if defined(MBEDTLS_RIPEMD160_C)
948 case PSA_ALG_RIPEMD160:
949 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
950 break;
951#endif
952#if defined(MBEDTLS_SHA1_C)
953 case PSA_ALG_SHA_1:
954 mbedtls_sha1_free( &operation->ctx.sha1 );
955 break;
956#endif
957#if defined(MBEDTLS_SHA256_C)
958 case PSA_ALG_SHA_224:
959 case PSA_ALG_SHA_256:
960 mbedtls_sha256_free( &operation->ctx.sha256 );
961 break;
962#endif
963#if defined(MBEDTLS_SHA512_C)
964 case PSA_ALG_SHA_384:
965 case PSA_ALG_SHA_512:
966 mbedtls_sha512_free( &operation->ctx.sha512 );
967 break;
968#endif
969 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +0200970 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100971 }
972 operation->alg = 0;
973 return( PSA_SUCCESS );
974}
975
Gilles Peskineda8191d1c2018-07-08 19:46:38 +0200976psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100977 psa_algorithm_t alg )
978{
979 int ret;
980 operation->alg = 0;
981 switch( alg )
982 {
983#if defined(MBEDTLS_MD2_C)
984 case PSA_ALG_MD2:
985 mbedtls_md2_init( &operation->ctx.md2 );
986 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
987 break;
988#endif
989#if defined(MBEDTLS_MD4_C)
990 case PSA_ALG_MD4:
991 mbedtls_md4_init( &operation->ctx.md4 );
992 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
993 break;
994#endif
995#if defined(MBEDTLS_MD5_C)
996 case PSA_ALG_MD5:
997 mbedtls_md5_init( &operation->ctx.md5 );
998 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
999 break;
1000#endif
1001#if defined(MBEDTLS_RIPEMD160_C)
1002 case PSA_ALG_RIPEMD160:
1003 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
1004 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
1005 break;
1006#endif
1007#if defined(MBEDTLS_SHA1_C)
1008 case PSA_ALG_SHA_1:
1009 mbedtls_sha1_init( &operation->ctx.sha1 );
1010 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1011 break;
1012#endif
1013#if defined(MBEDTLS_SHA256_C)
1014 case PSA_ALG_SHA_224:
1015 mbedtls_sha256_init( &operation->ctx.sha256 );
1016 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1017 break;
1018 case PSA_ALG_SHA_256:
1019 mbedtls_sha256_init( &operation->ctx.sha256 );
1020 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1021 break;
1022#endif
1023#if defined(MBEDTLS_SHA512_C)
1024 case PSA_ALG_SHA_384:
1025 mbedtls_sha512_init( &operation->ctx.sha512 );
1026 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1027 break;
1028 case PSA_ALG_SHA_512:
1029 mbedtls_sha512_init( &operation->ctx.sha512 );
1030 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1031 break;
1032#endif
1033 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001034 return( PSA_ALG_IS_HASH( alg ) ?
1035 PSA_ERROR_NOT_SUPPORTED :
1036 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001037 }
1038 if( ret == 0 )
1039 operation->alg = alg;
1040 else
1041 psa_hash_abort( operation );
1042 return( mbedtls_to_psa_error( ret ) );
1043}
1044
1045psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1046 const uint8_t *input,
1047 size_t input_length )
1048{
1049 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001050
1051 /* Don't require hash implementations to behave correctly on a
1052 * zero-length input, which may have an invalid pointer. */
1053 if( input_length == 0 )
1054 return( PSA_SUCCESS );
1055
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001056 switch( operation->alg )
1057 {
1058#if defined(MBEDTLS_MD2_C)
1059 case PSA_ALG_MD2:
1060 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1061 input, input_length );
1062 break;
1063#endif
1064#if defined(MBEDTLS_MD4_C)
1065 case PSA_ALG_MD4:
1066 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1067 input, input_length );
1068 break;
1069#endif
1070#if defined(MBEDTLS_MD5_C)
1071 case PSA_ALG_MD5:
1072 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1073 input, input_length );
1074 break;
1075#endif
1076#if defined(MBEDTLS_RIPEMD160_C)
1077 case PSA_ALG_RIPEMD160:
1078 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1079 input, input_length );
1080 break;
1081#endif
1082#if defined(MBEDTLS_SHA1_C)
1083 case PSA_ALG_SHA_1:
1084 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1085 input, input_length );
1086 break;
1087#endif
1088#if defined(MBEDTLS_SHA256_C)
1089 case PSA_ALG_SHA_224:
1090 case PSA_ALG_SHA_256:
1091 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1092 input, input_length );
1093 break;
1094#endif
1095#if defined(MBEDTLS_SHA512_C)
1096 case PSA_ALG_SHA_384:
1097 case PSA_ALG_SHA_512:
1098 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1099 input, input_length );
1100 break;
1101#endif
1102 default:
1103 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1104 break;
1105 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001106
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001107 if( ret != 0 )
1108 psa_hash_abort( operation );
1109 return( mbedtls_to_psa_error( ret ) );
1110}
1111
1112psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1113 uint8_t *hash,
1114 size_t hash_size,
1115 size_t *hash_length )
1116{
itayzafrir40835d42018-08-02 13:14:17 +03001117 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001118 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001119 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001120
1121 /* Fill the output buffer with something that isn't a valid hash
1122 * (barring an attack on the hash and deliberately-crafted input),
1123 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001124 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001125 /* If hash_size is 0 then hash may be NULL and then the
1126 * call to memset would have undefined behavior. */
1127 if( hash_size != 0 )
1128 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001129
1130 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001131 {
1132 status = PSA_ERROR_BUFFER_TOO_SMALL;
1133 goto exit;
1134 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001135
1136 switch( operation->alg )
1137 {
1138#if defined(MBEDTLS_MD2_C)
1139 case PSA_ALG_MD2:
1140 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1141 break;
1142#endif
1143#if defined(MBEDTLS_MD4_C)
1144 case PSA_ALG_MD4:
1145 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1146 break;
1147#endif
1148#if defined(MBEDTLS_MD5_C)
1149 case PSA_ALG_MD5:
1150 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1151 break;
1152#endif
1153#if defined(MBEDTLS_RIPEMD160_C)
1154 case PSA_ALG_RIPEMD160:
1155 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1156 break;
1157#endif
1158#if defined(MBEDTLS_SHA1_C)
1159 case PSA_ALG_SHA_1:
1160 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1161 break;
1162#endif
1163#if defined(MBEDTLS_SHA256_C)
1164 case PSA_ALG_SHA_224:
1165 case PSA_ALG_SHA_256:
1166 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1167 break;
1168#endif
1169#if defined(MBEDTLS_SHA512_C)
1170 case PSA_ALG_SHA_384:
1171 case PSA_ALG_SHA_512:
1172 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1173 break;
1174#endif
1175 default:
1176 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1177 break;
1178 }
itayzafrir40835d42018-08-02 13:14:17 +03001179 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001180
itayzafrir40835d42018-08-02 13:14:17 +03001181exit:
1182 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001183 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001184 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001185 return( psa_hash_abort( operation ) );
1186 }
1187 else
1188 {
1189 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001190 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001191 }
1192}
1193
Gilles Peskine2d277862018-06-18 15:41:12 +02001194psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1195 const uint8_t *hash,
1196 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001197{
1198 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1199 size_t actual_hash_length;
1200 psa_status_t status = psa_hash_finish( operation,
1201 actual_hash, sizeof( actual_hash ),
1202 &actual_hash_length );
1203 if( status != PSA_SUCCESS )
1204 return( status );
1205 if( actual_hash_length != hash_length )
1206 return( PSA_ERROR_INVALID_SIGNATURE );
1207 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1208 return( PSA_ERROR_INVALID_SIGNATURE );
1209 return( PSA_SUCCESS );
1210}
1211
1212
1213
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001214/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001215/* MAC */
1216/****************************************************************/
1217
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001218static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001219 psa_algorithm_t alg,
1220 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001221 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001222 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001223{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001224 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001225 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001226
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001227 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001228 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02001229
Gilles Peskine8c9def32018-02-08 10:02:12 +01001230 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1231 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001232 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001233 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001234 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001235 mode = MBEDTLS_MODE_STREAM;
1236 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001237 case PSA_ALG_CTR:
1238 mode = MBEDTLS_MODE_CTR;
1239 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001240 case PSA_ALG_CFB:
1241 mode = MBEDTLS_MODE_CFB;
1242 break;
1243 case PSA_ALG_OFB:
1244 mode = MBEDTLS_MODE_OFB;
1245 break;
1246 case PSA_ALG_CBC_NO_PADDING:
1247 mode = MBEDTLS_MODE_CBC;
1248 break;
1249 case PSA_ALG_CBC_PKCS7:
1250 mode = MBEDTLS_MODE_CBC;
1251 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001252 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001253 mode = MBEDTLS_MODE_CCM;
1254 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02001255 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01001256 mode = MBEDTLS_MODE_GCM;
1257 break;
1258 default:
1259 return( NULL );
1260 }
1261 }
1262 else if( alg == PSA_ALG_CMAC )
1263 mode = MBEDTLS_MODE_ECB;
1264 else if( alg == PSA_ALG_GMAC )
1265 mode = MBEDTLS_MODE_GCM;
1266 else
1267 return( NULL );
1268
1269 switch( key_type )
1270 {
1271 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001272 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001273 break;
1274 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001275 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1276 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001277 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001278 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001279 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001280 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001281 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1282 * but two-key Triple-DES is functionally three-key Triple-DES
1283 * with K1=K3, so that's how we present it to mbedtls. */
1284 if( key_bits == 128 )
1285 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001286 break;
1287 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001288 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001289 break;
1290 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001291 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001292 break;
1293 default:
1294 return( NULL );
1295 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001296 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001297 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001298
Jaeden Amero23bbb752018-06-26 14:16:54 +01001299 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1300 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001301}
1302
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001303static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001304{
Gilles Peskine2d277862018-06-18 15:41:12 +02001305 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001306 {
1307 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001308 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001309 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001310 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001311 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001312 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001313 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001314 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001315 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001316 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001317 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001318 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001319 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001320 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001321 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001322 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001323 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001324 return( 128 );
1325 default:
1326 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001327 }
1328}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001329
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001330/* Initialize the MAC operation structure. Once this function has been
1331 * called, psa_mac_abort can run and will do the right thing. */
1332static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1333 psa_algorithm_t alg )
1334{
1335 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1336
1337 operation->alg = alg;
1338 operation->key_set = 0;
1339 operation->iv_set = 0;
1340 operation->iv_required = 0;
1341 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001342 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001343
1344#if defined(MBEDTLS_CMAC_C)
1345 if( alg == PSA_ALG_CMAC )
1346 {
1347 operation->iv_required = 0;
1348 mbedtls_cipher_init( &operation->ctx.cmac );
1349 status = PSA_SUCCESS;
1350 }
1351 else
1352#endif /* MBEDTLS_CMAC_C */
1353#if defined(MBEDTLS_MD_C)
1354 if( PSA_ALG_IS_HMAC( operation->alg ) )
1355 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001356 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1357 operation->ctx.hmac.hash_ctx.alg = 0;
1358 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001359 }
1360 else
1361#endif /* MBEDTLS_MD_C */
1362 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001363 if( ! PSA_ALG_IS_MAC( alg ) )
1364 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001365 }
1366
1367 if( status != PSA_SUCCESS )
1368 memset( operation, 0, sizeof( *operation ) );
1369 return( status );
1370}
1371
Gilles Peskine01126fa2018-07-12 17:04:55 +02001372#if defined(MBEDTLS_MD_C)
1373static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1374{
1375 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1376 return( psa_hash_abort( &hmac->hash_ctx ) );
1377}
1378#endif /* MBEDTLS_MD_C */
1379
Gilles Peskine8c9def32018-02-08 10:02:12 +01001380psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1381{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001382 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001383 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001384 /* The object has (apparently) been initialized but it is not
1385 * in use. It's ok to call abort on such an object, and there's
1386 * nothing to do. */
1387 return( PSA_SUCCESS );
1388 }
1389 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001390#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001391 if( operation->alg == PSA_ALG_CMAC )
1392 {
1393 mbedtls_cipher_free( &operation->ctx.cmac );
1394 }
1395 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001396#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001397#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001398 if( PSA_ALG_IS_HMAC( operation->alg ) )
1399 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001400 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001401 }
1402 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001403#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001404 {
1405 /* Sanity check (shouldn't happen: operation->alg should
1406 * always have been initialized to a valid value). */
1407 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001408 }
Moran Peker41deec42018-04-04 15:43:05 +03001409
Gilles Peskine8c9def32018-02-08 10:02:12 +01001410 operation->alg = 0;
1411 operation->key_set = 0;
1412 operation->iv_set = 0;
1413 operation->iv_required = 0;
1414 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001415 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001416
Gilles Peskine8c9def32018-02-08 10:02:12 +01001417 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001418
1419bad_state:
1420 /* If abort is called on an uninitialized object, we can't trust
1421 * anything. Wipe the object in case it contains confidential data.
1422 * This may result in a memory leak if a pointer gets overwritten,
1423 * but it's too late to do anything about this. */
1424 memset( operation, 0, sizeof( *operation ) );
1425 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001426}
1427
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001428#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001429static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001430 size_t key_bits,
1431 key_slot_t *slot,
1432 const mbedtls_cipher_info_t *cipher_info )
1433{
1434 int ret;
1435
1436 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001437
1438 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1439 if( ret != 0 )
1440 return( ret );
1441
1442 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1443 slot->data.raw.data,
1444 key_bits );
1445 return( ret );
1446}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001447#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001448
Gilles Peskine248051a2018-06-20 16:09:38 +02001449#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001450static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1451 const uint8_t *key,
1452 size_t key_length,
1453 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001454{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001455 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001456 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001457 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001458 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001459 psa_status_t status;
1460
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001461 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1462 * overflow below. This should never trigger if the hash algorithm
1463 * is implemented correctly. */
1464 /* The size checks against the ipad and opad buffers cannot be written
1465 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1466 * because that triggers -Wlogical-op on GCC 7.3. */
1467 if( block_size > sizeof( ipad ) )
1468 return( PSA_ERROR_NOT_SUPPORTED );
1469 if( block_size > sizeof( hmac->opad ) )
1470 return( PSA_ERROR_NOT_SUPPORTED );
1471 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001472 return( PSA_ERROR_NOT_SUPPORTED );
1473
Gilles Peskined223b522018-06-11 18:12:58 +02001474 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001475 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001476 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001477 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001478 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001479 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001480 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001481 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001482 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001483 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001484 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001485 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001486 }
Gilles Peskine96889972018-07-12 17:07:03 +02001487 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1488 * but it is permitted. It is common when HMAC is used in HKDF, for
1489 * example. Don't call `memcpy` in the 0-length because `key` could be
1490 * an invalid pointer which would make the behavior undefined. */
1491 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001492 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001493
Gilles Peskined223b522018-06-11 18:12:58 +02001494 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1495 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001496 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001497 ipad[i] ^= 0x36;
1498 memset( ipad + key_length, 0x36, block_size - key_length );
1499
1500 /* Copy the key material from ipad to opad, flipping the requisite bits,
1501 * and filling the rest of opad with the requisite constant. */
1502 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001503 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1504 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001505
Gilles Peskine01126fa2018-07-12 17:04:55 +02001506 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001507 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001508 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001509
Gilles Peskine01126fa2018-07-12 17:04:55 +02001510 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001511
1512cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001513 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001514
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001515 return( status );
1516}
Gilles Peskine248051a2018-06-20 16:09:38 +02001517#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001518
Gilles Peskine89167cb2018-07-08 20:12:23 +02001519static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1520 psa_key_slot_t key,
1521 psa_algorithm_t alg,
1522 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001523{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001524 psa_status_t status;
1525 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001526 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001527 psa_key_usage_t usage =
1528 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskined911eb72018-08-14 15:18:45 +02001529 unsigned char truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
1530 psa_algorithm_t full_length_alg = alg & ~PSA_ALG_MAC_TRUNCATION_MASK;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001531
Gilles Peskined911eb72018-08-14 15:18:45 +02001532 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001533 if( status != PSA_SUCCESS )
1534 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001535 if( is_sign )
1536 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001537
Gilles Peskine89167cb2018-07-08 20:12:23 +02001538 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001539 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001540 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001541 key_bits = psa_get_key_bits( slot );
1542
Gilles Peskine8c9def32018-02-08 10:02:12 +01001543#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001544 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001545 {
1546 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02001547 mbedtls_cipher_info_from_psa( full_length_alg,
1548 slot->type, key_bits, NULL );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001549 int ret;
1550 if( cipher_info == NULL )
1551 {
1552 status = PSA_ERROR_NOT_SUPPORTED;
1553 goto exit;
1554 }
1555 operation->mac_size = cipher_info->block_size;
1556 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1557 status = mbedtls_to_psa_error( ret );
1558 }
1559 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001560#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001561#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02001562 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001563 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02001564 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001565 if( hash_alg == 0 )
1566 {
1567 status = PSA_ERROR_NOT_SUPPORTED;
1568 goto exit;
1569 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001570
1571 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1572 /* Sanity check. This shouldn't fail on a valid configuration. */
1573 if( operation->mac_size == 0 ||
1574 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1575 {
1576 status = PSA_ERROR_NOT_SUPPORTED;
1577 goto exit;
1578 }
1579
Gilles Peskine01126fa2018-07-12 17:04:55 +02001580 if( slot->type != PSA_KEY_TYPE_HMAC )
1581 {
1582 status = PSA_ERROR_INVALID_ARGUMENT;
1583 goto exit;
1584 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001585
Gilles Peskine01126fa2018-07-12 17:04:55 +02001586 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1587 slot->data.raw.data,
1588 slot->data.raw.bytes,
1589 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001590 }
1591 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001592#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001593 {
1594 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001595 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001596
Gilles Peskined911eb72018-08-14 15:18:45 +02001597 if( truncated == 0 )
1598 {
1599 /* The "normal" case: untruncated algorithm. Nothing to do. */
1600 }
1601 else if( truncated < 4 )
1602 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02001603 /* A very short MAC is too short for security since it can be
1604 * brute-forced. Ancient protocols with 32-bit MACs do exist,
1605 * so we make this our minimum, even though 32 bits is still
1606 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02001607 status = PSA_ERROR_NOT_SUPPORTED;
1608 }
1609 else if( truncated > operation->mac_size )
1610 {
1611 /* It's impossible to "truncate" to a larger length. */
1612 status = PSA_ERROR_INVALID_ARGUMENT;
1613 }
1614 else
1615 operation->mac_size = truncated;
1616
Gilles Peskinefbfac682018-07-08 20:51:54 +02001617exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001618 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001619 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001620 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001621 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001622 else
1623 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001624 operation->key_set = 1;
1625 }
1626 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001627}
1628
Gilles Peskine89167cb2018-07-08 20:12:23 +02001629psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1630 psa_key_slot_t key,
1631 psa_algorithm_t alg )
1632{
1633 return( psa_mac_setup( operation, key, alg, 1 ) );
1634}
1635
1636psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1637 psa_key_slot_t key,
1638 psa_algorithm_t alg )
1639{
1640 return( psa_mac_setup( operation, key, alg, 0 ) );
1641}
1642
Gilles Peskine8c9def32018-02-08 10:02:12 +01001643psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1644 const uint8_t *input,
1645 size_t input_length )
1646{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001647 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001648 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001649 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001650 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001651 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001652 operation->has_input = 1;
1653
Gilles Peskine8c9def32018-02-08 10:02:12 +01001654#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001655 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001656 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001657 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1658 input, input_length );
1659 status = mbedtls_to_psa_error( ret );
1660 }
1661 else
1662#endif /* MBEDTLS_CMAC_C */
1663#if defined(MBEDTLS_MD_C)
1664 if( PSA_ALG_IS_HMAC( operation->alg ) )
1665 {
1666 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1667 input_length );
1668 }
1669 else
1670#endif /* MBEDTLS_MD_C */
1671 {
1672 /* This shouldn't happen if `operation` was initialized by
1673 * a setup function. */
1674 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001675 }
1676
Gilles Peskinefbfac682018-07-08 20:51:54 +02001677cleanup:
1678 if( status != PSA_SUCCESS )
1679 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001680 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001681}
1682
Gilles Peskine01126fa2018-07-12 17:04:55 +02001683#if defined(MBEDTLS_MD_C)
1684static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1685 uint8_t *mac,
1686 size_t mac_size )
1687{
1688 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1689 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1690 size_t hash_size = 0;
1691 size_t block_size = psa_get_hash_block_size( hash_alg );
1692 psa_status_t status;
1693
Gilles Peskine01126fa2018-07-12 17:04:55 +02001694 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1695 if( status != PSA_SUCCESS )
1696 return( status );
1697 /* From here on, tmp needs to be wiped. */
1698
1699 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1700 if( status != PSA_SUCCESS )
1701 goto exit;
1702
1703 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1704 if( status != PSA_SUCCESS )
1705 goto exit;
1706
1707 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1708 if( status != PSA_SUCCESS )
1709 goto exit;
1710
Gilles Peskined911eb72018-08-14 15:18:45 +02001711 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1712 if( status != PSA_SUCCESS )
1713 goto exit;
1714
1715 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001716
1717exit:
1718 mbedtls_zeroize( tmp, hash_size );
1719 return( status );
1720}
1721#endif /* MBEDTLS_MD_C */
1722
mohammad16036df908f2018-04-02 08:34:15 -07001723static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001724 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001725 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001726{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001727 if( ! operation->key_set )
1728 return( PSA_ERROR_BAD_STATE );
1729 if( operation->iv_required && ! operation->iv_set )
1730 return( PSA_ERROR_BAD_STATE );
1731
Gilles Peskine8c9def32018-02-08 10:02:12 +01001732 if( mac_size < operation->mac_size )
1733 return( PSA_ERROR_BUFFER_TOO_SMALL );
1734
Gilles Peskine8c9def32018-02-08 10:02:12 +01001735#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001736 if( operation->alg == PSA_ALG_CMAC )
1737 {
Gilles Peskined911eb72018-08-14 15:18:45 +02001738 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
1739 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
1740 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02001741 memcpy( mac, tmp, operation->mac_size );
Gilles Peskined911eb72018-08-14 15:18:45 +02001742 mbedtls_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001743 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001744 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001745 else
1746#endif /* MBEDTLS_CMAC_C */
1747#if defined(MBEDTLS_MD_C)
1748 if( PSA_ALG_IS_HMAC( operation->alg ) )
1749 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001750 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02001751 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001752 }
1753 else
1754#endif /* MBEDTLS_MD_C */
1755 {
1756 /* This shouldn't happen if `operation` was initialized by
1757 * a setup function. */
1758 return( PSA_ERROR_BAD_STATE );
1759 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001760}
1761
Gilles Peskineacd4be32018-07-08 19:56:25 +02001762psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1763 uint8_t *mac,
1764 size_t mac_size,
1765 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001766{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001767 psa_status_t status;
1768
1769 /* Fill the output buffer with something that isn't a valid mac
1770 * (barring an attack on the mac and deliberately-crafted input),
1771 * in case the caller doesn't check the return status properly. */
1772 *mac_length = mac_size;
1773 /* If mac_size is 0 then mac may be NULL and then the
1774 * call to memset would have undefined behavior. */
1775 if( mac_size != 0 )
1776 memset( mac, '!', mac_size );
1777
Gilles Peskine89167cb2018-07-08 20:12:23 +02001778 if( ! operation->is_sign )
1779 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001780 status = PSA_ERROR_BAD_STATE;
1781 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001782 }
mohammad16036df908f2018-04-02 08:34:15 -07001783
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001784 status = psa_mac_finish_internal( operation, mac, mac_size );
1785
1786cleanup:
1787 if( status == PSA_SUCCESS )
1788 {
1789 status = psa_mac_abort( operation );
1790 if( status == PSA_SUCCESS )
1791 *mac_length = operation->mac_size;
1792 else
1793 memset( mac, '!', mac_size );
1794 }
1795 else
1796 psa_mac_abort( operation );
1797 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001798}
1799
Gilles Peskineacd4be32018-07-08 19:56:25 +02001800psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1801 const uint8_t *mac,
1802 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001803{
Gilles Peskine828ed142018-06-18 23:25:51 +02001804 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001805 psa_status_t status;
1806
Gilles Peskine89167cb2018-07-08 20:12:23 +02001807 if( operation->is_sign )
1808 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001809 status = PSA_ERROR_BAD_STATE;
1810 goto cleanup;
1811 }
1812 if( operation->mac_size != mac_length )
1813 {
1814 status = PSA_ERROR_INVALID_SIGNATURE;
1815 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001816 }
mohammad16036df908f2018-04-02 08:34:15 -07001817
1818 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001819 actual_mac, sizeof( actual_mac ) );
1820
1821 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1822 status = PSA_ERROR_INVALID_SIGNATURE;
1823
1824cleanup:
1825 if( status == PSA_SUCCESS )
1826 status = psa_mac_abort( operation );
1827 else
1828 psa_mac_abort( operation );
1829
Gilles Peskine99b7d6b2018-08-21 14:56:19 +02001830 mbedtls_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02001831
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001832 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001833}
1834
1835
Gilles Peskine20035e32018-02-03 22:44:14 +01001836
Gilles Peskine20035e32018-02-03 22:44:14 +01001837/****************************************************************/
1838/* Asymmetric cryptography */
1839/****************************************************************/
1840
Gilles Peskine2b450e32018-06-27 15:42:46 +02001841#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001842/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001843 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001844static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1845 size_t hash_length,
1846 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001847{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001848 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001849 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001850 *md_alg = mbedtls_md_get_type( md_info );
1851
1852 /* The Mbed TLS RSA module uses an unsigned int for hash length
1853 * parameters. Validate that it fits so that we don't risk an
1854 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001855#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001856 if( hash_length > UINT_MAX )
1857 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001858#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001859
1860#if defined(MBEDTLS_PKCS1_V15)
1861 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
1862 * must be correct. */
1863 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
1864 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001865 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001866 if( md_info == NULL )
1867 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001868 if( mbedtls_md_get_size( md_info ) != hash_length )
1869 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001870 }
1871#endif /* MBEDTLS_PKCS1_V15 */
1872
1873#if defined(MBEDTLS_PKCS1_V21)
1874 /* PSS requires a hash internally. */
1875 if( PSA_ALG_IS_RSA_PSS( alg ) )
1876 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001877 if( md_info == NULL )
1878 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001879 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001880#endif /* MBEDTLS_PKCS1_V21 */
1881
Gilles Peskine61b91d42018-06-08 16:09:36 +02001882 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001883}
1884
Gilles Peskine2b450e32018-06-27 15:42:46 +02001885static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1886 psa_algorithm_t alg,
1887 const uint8_t *hash,
1888 size_t hash_length,
1889 uint8_t *signature,
1890 size_t signature_size,
1891 size_t *signature_length )
1892{
1893 psa_status_t status;
1894 int ret;
1895 mbedtls_md_type_t md_alg;
1896
1897 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1898 if( status != PSA_SUCCESS )
1899 return( status );
1900
Gilles Peskine630a18a2018-06-29 17:49:35 +02001901 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001902 return( PSA_ERROR_BUFFER_TOO_SMALL );
1903
1904#if defined(MBEDTLS_PKCS1_V15)
1905 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1906 {
1907 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1908 MBEDTLS_MD_NONE );
1909 ret = mbedtls_rsa_pkcs1_sign( rsa,
1910 mbedtls_ctr_drbg_random,
1911 &global_data.ctr_drbg,
1912 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001913 md_alg,
1914 (unsigned int) hash_length,
1915 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001916 signature );
1917 }
1918 else
1919#endif /* MBEDTLS_PKCS1_V15 */
1920#if defined(MBEDTLS_PKCS1_V21)
1921 if( PSA_ALG_IS_RSA_PSS( alg ) )
1922 {
1923 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1924 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1925 mbedtls_ctr_drbg_random,
1926 &global_data.ctr_drbg,
1927 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001928 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001929 (unsigned int) hash_length,
1930 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001931 signature );
1932 }
1933 else
1934#endif /* MBEDTLS_PKCS1_V21 */
1935 {
1936 return( PSA_ERROR_INVALID_ARGUMENT );
1937 }
1938
1939 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02001940 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02001941 return( mbedtls_to_psa_error( ret ) );
1942}
1943
1944static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1945 psa_algorithm_t alg,
1946 const uint8_t *hash,
1947 size_t hash_length,
1948 const uint8_t *signature,
1949 size_t signature_length )
1950{
1951 psa_status_t status;
1952 int ret;
1953 mbedtls_md_type_t md_alg;
1954
1955 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1956 if( status != PSA_SUCCESS )
1957 return( status );
1958
Gilles Peskine630a18a2018-06-29 17:49:35 +02001959 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001960 return( PSA_ERROR_BUFFER_TOO_SMALL );
1961
1962#if defined(MBEDTLS_PKCS1_V15)
1963 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1964 {
1965 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1966 MBEDTLS_MD_NONE );
1967 ret = mbedtls_rsa_pkcs1_verify( rsa,
1968 mbedtls_ctr_drbg_random,
1969 &global_data.ctr_drbg,
1970 MBEDTLS_RSA_PUBLIC,
1971 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001972 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001973 hash,
1974 signature );
1975 }
1976 else
1977#endif /* MBEDTLS_PKCS1_V15 */
1978#if defined(MBEDTLS_PKCS1_V21)
1979 if( PSA_ALG_IS_RSA_PSS( alg ) )
1980 {
1981 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1982 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1983 mbedtls_ctr_drbg_random,
1984 &global_data.ctr_drbg,
1985 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001986 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001987 (unsigned int) hash_length,
1988 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001989 signature );
1990 }
1991 else
1992#endif /* MBEDTLS_PKCS1_V21 */
1993 {
1994 return( PSA_ERROR_INVALID_ARGUMENT );
1995 }
Gilles Peskineef12c632018-09-13 20:37:48 +02001996
1997 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
1998 * the rest of the signature is invalid". This has little use in
1999 * practice and PSA doesn't report this distinction. */
2000 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
2001 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02002002 return( mbedtls_to_psa_error( ret ) );
2003}
2004#endif /* MBEDTLS_RSA_C */
2005
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002006#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002007/* `ecp` cannot be const because `ecp->grp` needs to be non-const
2008 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
2009 * (even though these functions don't modify it). */
2010static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
2011 psa_algorithm_t alg,
2012 const uint8_t *hash,
2013 size_t hash_length,
2014 uint8_t *signature,
2015 size_t signature_size,
2016 size_t *signature_length )
2017{
2018 int ret;
2019 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002020 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002021 mbedtls_mpi_init( &r );
2022 mbedtls_mpi_init( &s );
2023
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002024 if( signature_size < 2 * curve_bytes )
2025 {
2026 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
2027 goto cleanup;
2028 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002029
2030 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
2031 {
2032 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
2033 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2034 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2035 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
2036 hash, hash_length,
2037 md_alg ) );
2038 }
2039 else
2040 {
2041 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
2042 hash, hash_length,
2043 mbedtls_ctr_drbg_random,
2044 &global_data.ctr_drbg ) );
2045 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002046
2047 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2048 signature,
2049 curve_bytes ) );
2050 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2051 signature + curve_bytes,
2052 curve_bytes ) );
2053
2054cleanup:
2055 mbedtls_mpi_free( &r );
2056 mbedtls_mpi_free( &s );
2057 if( ret == 0 )
2058 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002059 return( mbedtls_to_psa_error( ret ) );
2060}
2061
2062static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2063 const uint8_t *hash,
2064 size_t hash_length,
2065 const uint8_t *signature,
2066 size_t signature_length )
2067{
2068 int ret;
2069 mbedtls_mpi r, s;
2070 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2071 mbedtls_mpi_init( &r );
2072 mbedtls_mpi_init( &s );
2073
2074 if( signature_length != 2 * curve_bytes )
2075 return( PSA_ERROR_INVALID_SIGNATURE );
2076
2077 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2078 signature,
2079 curve_bytes ) );
2080 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2081 signature + curve_bytes,
2082 curve_bytes ) );
2083
2084 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2085 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002086
2087cleanup:
2088 mbedtls_mpi_free( &r );
2089 mbedtls_mpi_free( &s );
2090 return( mbedtls_to_psa_error( ret ) );
2091}
2092#endif /* MBEDTLS_ECDSA_C */
2093
Gilles Peskine61b91d42018-06-08 16:09:36 +02002094psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2095 psa_algorithm_t alg,
2096 const uint8_t *hash,
2097 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002098 uint8_t *signature,
2099 size_t signature_size,
2100 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002101{
2102 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002103 psa_status_t status;
2104
2105 *signature_length = signature_size;
2106
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002107 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2108 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002109 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002110 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002111 {
2112 status = PSA_ERROR_INVALID_ARGUMENT;
2113 goto exit;
2114 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002115
Gilles Peskine20035e32018-02-03 22:44:14 +01002116#if defined(MBEDTLS_RSA_C)
2117 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2118 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002119 status = psa_rsa_sign( slot->data.rsa,
2120 alg,
2121 hash, hash_length,
2122 signature, signature_size,
2123 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002124 }
2125 else
2126#endif /* defined(MBEDTLS_RSA_C) */
2127#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 Peskinea26ff6a2018-06-28 12:21:19 +02002132 status = psa_ecdsa_sign( slot->data.ecp,
2133 alg,
2134 hash, hash_length,
2135 signature, signature_size,
2136 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002137 else
2138#endif /* defined(MBEDTLS_ECDSA_C) */
2139 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002140 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002141 }
itayzafrir5c753392018-05-08 11:18:38 +03002142 }
2143 else
2144#endif /* defined(MBEDTLS_ECP_C) */
2145 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002146 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002147 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002148
2149exit:
2150 /* Fill the unused part of the output buffer (the whole buffer on error,
2151 * the trailing part on success) with something that isn't a valid mac
2152 * (barring an attack on the mac and deliberately-crafted input),
2153 * in case the caller doesn't check the return status properly. */
2154 if( status == PSA_SUCCESS )
2155 memset( signature + *signature_length, '!',
2156 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002157 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002158 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002159 /* If signature_size is 0 then we have nothing to do. We must not call
2160 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002161 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002162}
2163
2164psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2165 psa_algorithm_t alg,
2166 const uint8_t *hash,
2167 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002168 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002169 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002170{
2171 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002172 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002173
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002174 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2175 if( status != PSA_SUCCESS )
2176 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002177
Gilles Peskine61b91d42018-06-08 16:09:36 +02002178#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002179 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002180 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002181 return( psa_rsa_verify( slot->data.rsa,
2182 alg,
2183 hash, hash_length,
2184 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002185 }
2186 else
2187#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002188#if defined(MBEDTLS_ECP_C)
2189 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2190 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002191#if defined(MBEDTLS_ECDSA_C)
2192 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002193 return( psa_ecdsa_verify( slot->data.ecp,
2194 hash, hash_length,
2195 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002196 else
2197#endif /* defined(MBEDTLS_ECDSA_C) */
2198 {
2199 return( PSA_ERROR_INVALID_ARGUMENT );
2200 }
itayzafrir5c753392018-05-08 11:18:38 +03002201 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002202 else
2203#endif /* defined(MBEDTLS_ECP_C) */
2204 {
2205 return( PSA_ERROR_NOT_SUPPORTED );
2206 }
2207}
2208
Gilles Peskine072ac562018-06-30 00:21:29 +02002209#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2210static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2211 mbedtls_rsa_context *rsa )
2212{
2213 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2214 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2215 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2216 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2217}
2218#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2219
Gilles Peskine61b91d42018-06-08 16:09:36 +02002220psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2221 psa_algorithm_t alg,
2222 const uint8_t *input,
2223 size_t input_length,
2224 const uint8_t *salt,
2225 size_t salt_length,
2226 uint8_t *output,
2227 size_t output_size,
2228 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002229{
2230 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002231 psa_status_t status;
2232
Darryl Green5cc689a2018-07-24 15:34:10 +01002233 (void) input;
2234 (void) input_length;
2235 (void) salt;
2236 (void) output;
2237 (void) output_size;
2238
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002239 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002240
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002241 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2242 return( PSA_ERROR_INVALID_ARGUMENT );
2243
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002244 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2245 if( status != PSA_SUCCESS )
2246 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002247 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2248 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002249 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002250
2251#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002252 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002253 {
2254 mbedtls_rsa_context *rsa = slot->data.rsa;
2255 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002256 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002257 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002258#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002259 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002260 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002261 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2262 mbedtls_ctr_drbg_random,
2263 &global_data.ctr_drbg,
2264 MBEDTLS_RSA_PUBLIC,
2265 input_length,
2266 input,
2267 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002268 }
2269 else
2270#endif /* MBEDTLS_PKCS1_V15 */
2271#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002272 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002273 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002274 psa_rsa_oaep_set_padding_mode( alg, rsa );
2275 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2276 mbedtls_ctr_drbg_random,
2277 &global_data.ctr_drbg,
2278 MBEDTLS_RSA_PUBLIC,
2279 salt, salt_length,
2280 input_length,
2281 input,
2282 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002283 }
2284 else
2285#endif /* MBEDTLS_PKCS1_V21 */
2286 {
2287 return( PSA_ERROR_INVALID_ARGUMENT );
2288 }
2289 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002290 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002291 return( mbedtls_to_psa_error( ret ) );
2292 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002293 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002294#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002295 {
2296 return( PSA_ERROR_NOT_SUPPORTED );
2297 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002298}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002299
Gilles Peskine61b91d42018-06-08 16:09:36 +02002300psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2301 psa_algorithm_t alg,
2302 const uint8_t *input,
2303 size_t input_length,
2304 const uint8_t *salt,
2305 size_t salt_length,
2306 uint8_t *output,
2307 size_t output_size,
2308 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002309{
2310 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002311 psa_status_t status;
2312
Darryl Green5cc689a2018-07-24 15:34:10 +01002313 (void) input;
2314 (void) input_length;
2315 (void) salt;
2316 (void) output;
2317 (void) output_size;
2318
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002319 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002320
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002321 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2322 return( PSA_ERROR_INVALID_ARGUMENT );
2323
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002324 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2325 if( status != PSA_SUCCESS )
2326 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002327 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2328 return( PSA_ERROR_INVALID_ARGUMENT );
2329
2330#if defined(MBEDTLS_RSA_C)
2331 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2332 {
2333 mbedtls_rsa_context *rsa = slot->data.rsa;
2334 int ret;
2335
Gilles Peskine630a18a2018-06-29 17:49:35 +02002336 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002337 return( PSA_ERROR_INVALID_ARGUMENT );
2338
2339#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002340 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002341 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002342 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2343 mbedtls_ctr_drbg_random,
2344 &global_data.ctr_drbg,
2345 MBEDTLS_RSA_PRIVATE,
2346 output_length,
2347 input,
2348 output,
2349 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002350 }
2351 else
2352#endif /* MBEDTLS_PKCS1_V15 */
2353#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002354 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002355 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002356 psa_rsa_oaep_set_padding_mode( alg, rsa );
2357 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2358 mbedtls_ctr_drbg_random,
2359 &global_data.ctr_drbg,
2360 MBEDTLS_RSA_PRIVATE,
2361 salt, salt_length,
2362 output_length,
2363 input,
2364 output,
2365 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002366 }
2367 else
2368#endif /* MBEDTLS_PKCS1_V21 */
2369 {
2370 return( PSA_ERROR_INVALID_ARGUMENT );
2371 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002372
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002373 return( mbedtls_to_psa_error( ret ) );
2374 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002375 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002376#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002377 {
2378 return( PSA_ERROR_NOT_SUPPORTED );
2379 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002380}
Gilles Peskine20035e32018-02-03 22:44:14 +01002381
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002382
2383
mohammad1603503973b2018-03-12 15:59:30 +02002384/****************************************************************/
2385/* Symmetric cryptography */
2386/****************************************************************/
2387
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002388/* Initialize the cipher operation structure. Once this function has been
2389 * called, psa_cipher_abort can run and will do the right thing. */
2390static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2391 psa_algorithm_t alg )
2392{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002393 if( ! PSA_ALG_IS_CIPHER( alg ) )
2394 {
2395 memset( operation, 0, sizeof( *operation ) );
2396 return( PSA_ERROR_INVALID_ARGUMENT );
2397 }
2398
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002399 operation->alg = alg;
2400 operation->key_set = 0;
2401 operation->iv_set = 0;
2402 operation->iv_required = 1;
2403 operation->iv_size = 0;
2404 operation->block_size = 0;
2405 mbedtls_cipher_init( &operation->ctx.cipher );
2406 return( PSA_SUCCESS );
2407}
2408
Gilles Peskinee553c652018-06-04 16:22:46 +02002409static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2410 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002411 psa_algorithm_t alg,
2412 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002413{
2414 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2415 psa_status_t status;
2416 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002417 size_t key_bits;
2418 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002419 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2420 PSA_KEY_USAGE_ENCRYPT :
2421 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002422
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002423 status = psa_cipher_init( operation, alg );
2424 if( status != PSA_SUCCESS )
2425 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002426
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002427 status = psa_get_key_from_slot( key, &slot, usage, alg);
2428 if( status != PSA_SUCCESS )
2429 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002430 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002431
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002432 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002433 if( cipher_info == NULL )
2434 return( PSA_ERROR_NOT_SUPPORTED );
2435
mohammad1603503973b2018-03-12 15:59:30 +02002436 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002437 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002438 {
2439 psa_cipher_abort( operation );
2440 return( mbedtls_to_psa_error( ret ) );
2441 }
2442
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002443#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002444 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002445 {
2446 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2447 unsigned char keys[24];
2448 memcpy( keys, slot->data.raw.data, 16 );
2449 memcpy( keys + 16, slot->data.raw.data, 8 );
2450 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2451 keys,
2452 192, cipher_operation );
2453 }
2454 else
2455#endif
2456 {
2457 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2458 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002459 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002460 }
Moran Peker41deec42018-04-04 15:43:05 +03002461 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002462 {
2463 psa_cipher_abort( operation );
2464 return( mbedtls_to_psa_error( ret ) );
2465 }
2466
mohammad16038481e742018-03-18 13:57:31 +02002467#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002468 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002469 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002470 case PSA_ALG_CBC_NO_PADDING:
2471 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2472 MBEDTLS_PADDING_NONE );
2473 break;
2474 case PSA_ALG_CBC_PKCS7:
2475 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2476 MBEDTLS_PADDING_PKCS7 );
2477 break;
2478 default:
2479 /* The algorithm doesn't involve padding. */
2480 ret = 0;
2481 break;
2482 }
2483 if( ret != 0 )
2484 {
2485 psa_cipher_abort( operation );
2486 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002487 }
2488#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2489
mohammad1603503973b2018-03-12 15:59:30 +02002490 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002491 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2492 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2493 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002494 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002495 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002496 }
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 Peskinefe119512018-07-08 21:39:34 +02002501psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2502 psa_key_slot_t key,
2503 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002504{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002505 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002506}
2507
Gilles Peskinefe119512018-07-08 21:39:34 +02002508psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2509 psa_key_slot_t key,
2510 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002511{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002512 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002513}
2514
Gilles Peskinefe119512018-07-08 21:39:34 +02002515psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2516 unsigned char *iv,
2517 size_t iv_size,
2518 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002519{
itayzafrir534bd7c2018-08-02 13:56:32 +03002520 psa_status_t status;
2521 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002522 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002523 {
2524 status = PSA_ERROR_BAD_STATE;
2525 goto exit;
2526 }
Moran Peker41deec42018-04-04 15:43:05 +03002527 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002528 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002529 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002530 goto exit;
2531 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002532 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2533 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002534 if( ret != 0 )
2535 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002536 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002537 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002538 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002539
mohammad16038481e742018-03-18 13:57:31 +02002540 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002541 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002542
Moran Peker395db872018-05-31 14:07:14 +03002543exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002544 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002545 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002546 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002547}
2548
Gilles Peskinefe119512018-07-08 21:39:34 +02002549psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2550 const unsigned char *iv,
2551 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002552{
itayzafrir534bd7c2018-08-02 13:56:32 +03002553 psa_status_t status;
2554 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002555 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002556 {
2557 status = PSA_ERROR_BAD_STATE;
2558 goto exit;
2559 }
Moran Pekera28258c2018-05-29 16:25:04 +03002560 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002561 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002562 status = PSA_ERROR_INVALID_ARGUMENT;
2563 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002564 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002565 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2566 status = mbedtls_to_psa_error( ret );
2567exit:
2568 if( status == PSA_SUCCESS )
2569 operation->iv_set = 1;
2570 else
mohammad1603503973b2018-03-12 15:59:30 +02002571 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002572 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002573}
2574
Gilles Peskinee553c652018-06-04 16:22:46 +02002575psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2576 const uint8_t *input,
2577 size_t input_length,
2578 unsigned char *output,
2579 size_t output_size,
2580 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002581{
itayzafrir534bd7c2018-08-02 13:56:32 +03002582 psa_status_t status;
2583 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002584 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002585 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002586 {
2587 /* Take the unprocessed partial block left over from previous
2588 * update calls, if any, plus the input to this call. Remove
2589 * the last partial block, if any. You get the data that will be
2590 * output in this call. */
2591 expected_output_size =
2592 ( operation->ctx.cipher.unprocessed_len + input_length )
2593 / operation->block_size * operation->block_size;
2594 }
2595 else
2596 {
2597 expected_output_size = input_length;
2598 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002599
Gilles Peskine89d789c2018-06-04 17:17:16 +02002600 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002601 {
2602 status = PSA_ERROR_BUFFER_TOO_SMALL;
2603 goto exit;
2604 }
mohammad160382759612018-03-12 18:16:40 +02002605
mohammad1603503973b2018-03-12 15:59:30 +02002606 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002607 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002608 status = mbedtls_to_psa_error( ret );
2609exit:
2610 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002611 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002612 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002613}
2614
Gilles Peskinee553c652018-06-04 16:22:46 +02002615psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2616 uint8_t *output,
2617 size_t output_size,
2618 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002619{
Janos Follath315b51c2018-07-09 16:04:51 +01002620 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2621 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002622 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002623
mohammad1603503973b2018-03-12 15:59:30 +02002624 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002625 {
Janos Follath315b51c2018-07-09 16:04:51 +01002626 status = PSA_ERROR_BAD_STATE;
2627 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002628 }
2629 if( operation->iv_required && ! operation->iv_set )
2630 {
Janos Follath315b51c2018-07-09 16:04:51 +01002631 status = PSA_ERROR_BAD_STATE;
2632 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002633 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002634
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002635 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002636 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2637 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002638 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002639 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002640 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002641 }
2642
Janos Follath315b51c2018-07-09 16:04:51 +01002643 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2644 temp_output_buffer,
2645 output_length );
2646 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002647 {
Janos Follath315b51c2018-07-09 16:04:51 +01002648 status = mbedtls_to_psa_error( cipher_ret );
2649 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002650 }
Janos Follath315b51c2018-07-09 16:04:51 +01002651
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002652 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002653 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002654 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002655 memcpy( output, temp_output_buffer, *output_length );
2656 else
2657 {
Janos Follath315b51c2018-07-09 16:04:51 +01002658 status = PSA_ERROR_BUFFER_TOO_SMALL;
2659 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002660 }
mohammad1603503973b2018-03-12 15:59:30 +02002661
Janos Follath279ab8e2018-07-09 16:13:21 +01002662 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002663 status = psa_cipher_abort( operation );
2664
2665 return( status );
2666
2667error:
2668
2669 *output_length = 0;
2670
Janos Follath279ab8e2018-07-09 16:13:21 +01002671 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002672 (void) psa_cipher_abort( operation );
2673
2674 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002675}
2676
Gilles Peskinee553c652018-06-04 16:22:46 +02002677psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2678{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002679 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002680 {
2681 /* The object has (apparently) been initialized but it is not
2682 * in use. It's ok to call abort on such an object, and there's
2683 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002684 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002685 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002686
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002687 /* Sanity check (shouldn't happen: operation->alg should
2688 * always have been initialized to a valid value). */
2689 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2690 return( PSA_ERROR_BAD_STATE );
2691
mohammad1603503973b2018-03-12 15:59:30 +02002692 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002693
Moran Peker41deec42018-04-04 15:43:05 +03002694 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002695 operation->key_set = 0;
2696 operation->iv_set = 0;
2697 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002698 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002699 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002700
Moran Peker395db872018-05-31 14:07:14 +03002701 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002702}
2703
Gilles Peskinea0655c32018-04-30 17:06:50 +02002704
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002705
mohammad16038cc1cee2018-03-28 01:21:33 +03002706/****************************************************************/
2707/* Key Policy */
2708/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002709
mohammad160327010052018-07-03 13:16:15 +03002710#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002711void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002712{
Gilles Peskine803ce742018-06-18 16:07:14 +02002713 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002714}
2715
Gilles Peskine2d277862018-06-18 15:41:12 +02002716void psa_key_policy_set_usage( psa_key_policy_t *policy,
2717 psa_key_usage_t usage,
2718 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002719{
mohammad16034eed7572018-03-28 05:14:59 -07002720 policy->usage = usage;
2721 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002722}
2723
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002724psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002725{
mohammad16036df908f2018-04-02 08:34:15 -07002726 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002727}
2728
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002729psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002730{
mohammad16036df908f2018-04-02 08:34:15 -07002731 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002732}
mohammad160327010052018-07-03 13:16:15 +03002733#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002734
Gilles Peskine2d277862018-06-18 15:41:12 +02002735psa_status_t psa_set_key_policy( psa_key_slot_t key,
2736 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002737{
2738 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002739 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002740
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002741 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002742 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002743
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002744 status = psa_get_empty_key_slot( key, &slot );
2745 if( status != PSA_SUCCESS )
2746 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002747
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002748 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2749 PSA_KEY_USAGE_ENCRYPT |
2750 PSA_KEY_USAGE_DECRYPT |
2751 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002752 PSA_KEY_USAGE_VERIFY |
2753 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002754 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002755
mohammad16036df908f2018-04-02 08:34:15 -07002756 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002757
2758 return( PSA_SUCCESS );
2759}
2760
Gilles Peskine2d277862018-06-18 15:41:12 +02002761psa_status_t psa_get_key_policy( psa_key_slot_t key,
2762 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002763{
2764 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002765 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002766
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002767 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002768 return( PSA_ERROR_INVALID_ARGUMENT );
2769
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002770 status = psa_get_key_slot( key, &slot );
2771 if( status != PSA_SUCCESS )
2772 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002773
mohammad16036df908f2018-04-02 08:34:15 -07002774 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002775
2776 return( PSA_SUCCESS );
2777}
Gilles Peskine20035e32018-02-03 22:44:14 +01002778
Gilles Peskinea0655c32018-04-30 17:06:50 +02002779
2780
mohammad1603804cd712018-03-20 22:44:08 +02002781/****************************************************************/
2782/* Key Lifetime */
2783/****************************************************************/
2784
Gilles Peskine2d277862018-06-18 15:41:12 +02002785psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2786 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002787{
2788 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002789 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002790
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002791 status = psa_get_key_slot( key, &slot );
2792 if( status != PSA_SUCCESS )
2793 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002794
mohammad1603804cd712018-03-20 22:44:08 +02002795 *lifetime = slot->lifetime;
2796
2797 return( PSA_SUCCESS );
2798}
2799
Gilles Peskine2d277862018-06-18 15:41:12 +02002800psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002801 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002802{
2803 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002804 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002805
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002806 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2807 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002808 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2809 return( PSA_ERROR_INVALID_ARGUMENT );
2810
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002811 status = psa_get_empty_key_slot( key, &slot );
2812 if( status != PSA_SUCCESS )
2813 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002814
Moran Pekerd7326592018-05-29 16:56:39 +03002815 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002816 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002817
mohammad1603060ad8a2018-03-20 14:28:38 -07002818 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002819
2820 return( PSA_SUCCESS );
2821}
2822
Gilles Peskine20035e32018-02-03 22:44:14 +01002823
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002824
mohammad16035955c982018-04-26 00:53:03 +03002825/****************************************************************/
2826/* AEAD */
2827/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002828
Gilles Peskineedf9a652018-08-17 18:11:56 +02002829typedef struct
2830{
2831 key_slot_t *slot;
2832 const mbedtls_cipher_info_t *cipher_info;
2833 union
2834 {
2835#if defined(MBEDTLS_CCM_C)
2836 mbedtls_ccm_context ccm;
2837#endif /* MBEDTLS_CCM_C */
2838#if defined(MBEDTLS_GCM_C)
2839 mbedtls_gcm_context gcm;
2840#endif /* MBEDTLS_GCM_C */
2841 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002842 psa_algorithm_t core_alg;
2843 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02002844 uint8_t tag_length;
2845} aead_operation_t;
2846
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02002847static void psa_aead_abort( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02002848{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02002849 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02002850 {
2851#if defined(MBEDTLS_CCM_C)
2852 case PSA_ALG_CCM:
2853 mbedtls_ccm_free( &operation->ctx.ccm );
2854 break;
2855#endif /* MBEDTLS_CCM_C */
2856#if defined(MBEDTLS_CCM_C)
2857 case PSA_ALG_GCM:
2858 mbedtls_gcm_free( &operation->ctx.gcm );
2859 break;
2860#endif /* MBEDTLS_GCM_C */
2861 }
2862}
2863
2864static psa_status_t psa_aead_setup( aead_operation_t *operation,
2865 psa_key_slot_t key,
2866 psa_key_usage_t usage,
2867 psa_algorithm_t alg )
2868{
2869 psa_status_t status;
2870 size_t key_bits;
2871 mbedtls_cipher_id_t cipher_id;
2872
2873 status = psa_get_key_from_slot( key, &operation->slot, usage, alg );
2874 if( status != PSA_SUCCESS )
2875 return( status );
2876
2877 key_bits = psa_get_key_bits( operation->slot );
2878
2879 operation->cipher_info =
2880 mbedtls_cipher_info_from_psa( alg, operation->slot->type, key_bits,
2881 &cipher_id );
2882 if( operation->cipher_info == NULL )
2883 return( PSA_ERROR_NOT_SUPPORTED );
2884
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002885 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02002886 {
2887#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002888 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
2889 operation->core_alg = PSA_ALG_CCM;
2890 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02002891 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
2892 return( PSA_ERROR_INVALID_ARGUMENT );
2893 mbedtls_ccm_init( &operation->ctx.ccm );
2894 status = mbedtls_to_psa_error(
2895 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
2896 operation->slot->data.raw.data,
2897 (unsigned int) key_bits ) );
2898 if( status != 0 )
2899 goto cleanup;
2900 break;
2901#endif /* MBEDTLS_CCM_C */
2902
2903#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002904 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
2905 operation->core_alg = PSA_ALG_GCM;
2906 operation->full_tag_length = 16;
Gilles Peskineedf9a652018-08-17 18:11:56 +02002907 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->type ) != 16 )
2908 return( PSA_ERROR_INVALID_ARGUMENT );
2909 mbedtls_gcm_init( &operation->ctx.gcm );
2910 status = mbedtls_to_psa_error(
2911 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
2912 operation->slot->data.raw.data,
2913 (unsigned int) key_bits ) );
2914 break;
2915#endif /* MBEDTLS_GCM_C */
2916
2917 default:
2918 return( PSA_ERROR_NOT_SUPPORTED );
2919 }
2920
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002921 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
2922 {
2923 status = PSA_ERROR_INVALID_ARGUMENT;
2924 goto cleanup;
2925 }
2926 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
2927 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
2928 * GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
2929 * In both cases, mbedtls_xxx will validate the tag length below. */
2930
Gilles Peskineedf9a652018-08-17 18:11:56 +02002931 return( PSA_SUCCESS );
2932
2933cleanup:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02002934 psa_aead_abort( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02002935 return( status );
2936}
2937
mohammad16035955c982018-04-26 00:53:03 +03002938psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2939 psa_algorithm_t alg,
2940 const uint8_t *nonce,
2941 size_t nonce_length,
2942 const uint8_t *additional_data,
2943 size_t additional_data_length,
2944 const uint8_t *plaintext,
2945 size_t plaintext_length,
2946 uint8_t *ciphertext,
2947 size_t ciphertext_size,
2948 size_t *ciphertext_length )
2949{
mohammad16035955c982018-04-26 00:53:03 +03002950 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02002951 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03002952 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02002953
mohammad1603f08a5502018-06-03 15:05:47 +03002954 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002955
Gilles Peskineedf9a652018-08-17 18:11:56 +02002956 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002957 if( status != PSA_SUCCESS )
2958 return( status );
mohammad16035955c982018-04-26 00:53:03 +03002959
Gilles Peskineedf9a652018-08-17 18:11:56 +02002960 /* For all currently supported modes, the tag is at the end of the
2961 * ciphertext. */
2962 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
2963 {
2964 status = PSA_ERROR_BUFFER_TOO_SMALL;
2965 goto exit;
2966 }
2967 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03002968
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002969 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03002970 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02002971 status = mbedtls_to_psa_error(
2972 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
2973 MBEDTLS_GCM_ENCRYPT,
2974 plaintext_length,
2975 nonce, nonce_length,
2976 additional_data, additional_data_length,
2977 plaintext, ciphertext,
2978 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03002979 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002980 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03002981 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02002982 status = mbedtls_to_psa_error(
2983 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
2984 plaintext_length,
2985 nonce, nonce_length,
2986 additional_data,
2987 additional_data_length,
2988 plaintext, ciphertext,
2989 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03002990 }
mohammad16035c8845f2018-05-09 05:40:09 -07002991 else
2992 {
mohammad1603554faad2018-06-03 15:07:38 +03002993 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002994 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002995
Gilles Peskineedf9a652018-08-17 18:11:56 +02002996 if( status != PSA_SUCCESS && ciphertext_size != 0 )
2997 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02002998
Gilles Peskineedf9a652018-08-17 18:11:56 +02002999exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003000 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003001 if( status == PSA_SUCCESS )
3002 *ciphertext_length = plaintext_length + operation.tag_length;
3003 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003004}
3005
Gilles Peskineee652a32018-06-01 19:23:52 +02003006/* Locate the tag in a ciphertext buffer containing the encrypted data
3007 * followed by the tag. Return the length of the part preceding the tag in
3008 * *plaintext_length. This is the size of the plaintext in modes where
3009 * the encrypted data has the same size as the plaintext, such as
3010 * CCM and GCM. */
3011static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
3012 const uint8_t *ciphertext,
3013 size_t ciphertext_length,
3014 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02003015 const uint8_t **p_tag )
3016{
3017 size_t payload_length;
3018 if( tag_length > ciphertext_length )
3019 return( PSA_ERROR_INVALID_ARGUMENT );
3020 payload_length = ciphertext_length - tag_length;
3021 if( payload_length > plaintext_size )
3022 return( PSA_ERROR_BUFFER_TOO_SMALL );
3023 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02003024 return( PSA_SUCCESS );
3025}
3026
mohammad16035955c982018-04-26 00:53:03 +03003027psa_status_t psa_aead_decrypt( psa_key_slot_t key,
3028 psa_algorithm_t alg,
3029 const uint8_t *nonce,
3030 size_t nonce_length,
3031 const uint8_t *additional_data,
3032 size_t additional_data_length,
3033 const uint8_t *ciphertext,
3034 size_t ciphertext_length,
3035 uint8_t *plaintext,
3036 size_t plaintext_size,
3037 size_t *plaintext_length )
3038{
mohammad16035955c982018-04-26 00:53:03 +03003039 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003040 aead_operation_t operation;
3041 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02003042
Gilles Peskineee652a32018-06-01 19:23:52 +02003043 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03003044
Gilles Peskineedf9a652018-08-17 18:11:56 +02003045 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003046 if( status != PSA_SUCCESS )
3047 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003048
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003049 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03003050 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003051 status = psa_aead_unpadded_locate_tag( operation.tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003052 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003053 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02003054 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003055 goto exit;
Gilles Peskineee652a32018-06-01 19:23:52 +02003056
Gilles Peskineedf9a652018-08-17 18:11:56 +02003057 status = mbedtls_to_psa_error(
3058 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
3059 ciphertext_length - operation.tag_length,
3060 nonce, nonce_length,
3061 additional_data,
3062 additional_data_length,
3063 tag, operation.tag_length,
3064 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03003065 }
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003066 else if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03003067 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02003068 status = psa_aead_unpadded_locate_tag( operation.tag_length,
mohammad16039375f842018-06-03 14:28:24 +03003069 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003070 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003071 if( status != PSA_SUCCESS )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003072 goto exit;
mohammad16039375f842018-06-03 14:28:24 +03003073
Gilles Peskineedf9a652018-08-17 18:11:56 +02003074 status = mbedtls_to_psa_error(
3075 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
3076 ciphertext_length - operation.tag_length,
3077 nonce, nonce_length,
3078 additional_data,
3079 additional_data_length,
3080 ciphertext, plaintext,
3081 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03003082 }
mohammad160339574652018-06-01 04:39:53 -07003083 else
3084 {
mohammad1603554faad2018-06-03 15:07:38 +03003085 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003086 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003087
Gilles Peskineedf9a652018-08-17 18:11:56 +02003088 if( status != PSA_SUCCESS && plaintext_size != 0 )
3089 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03003090
Gilles Peskineedf9a652018-08-17 18:11:56 +02003091exit:
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003092 psa_aead_abort( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003093 if( status == PSA_SUCCESS )
3094 *plaintext_length = ciphertext_length - operation.tag_length;
3095 return( status );
mohammad16035955c982018-04-26 00:53:03 +03003096}
3097
Gilles Peskinea0655c32018-04-30 17:06:50 +02003098
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003099
Gilles Peskine20035e32018-02-03 22:44:14 +01003100/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003101/* Generators */
3102/****************************************************************/
3103
3104psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3105{
3106 psa_status_t status = PSA_SUCCESS;
3107 if( generator->alg == 0 )
3108 {
3109 /* The object has (apparently) been initialized but it is not
3110 * in use. It's ok to call abort on such an object, and there's
3111 * nothing to do. */
3112 }
3113 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003114#if defined(MBEDTLS_MD_C)
3115 if( PSA_ALG_IS_HKDF( generator->alg ) )
3116 {
3117 mbedtls_free( generator->ctx.hkdf.info );
3118 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3119 }
3120 else
3121#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003122 {
3123 status = PSA_ERROR_BAD_STATE;
3124 }
3125 memset( generator, 0, sizeof( *generator ) );
3126 return( status );
3127}
3128
3129
3130psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3131 size_t *capacity)
3132{
3133 *capacity = generator->capacity;
3134 return( PSA_SUCCESS );
3135}
3136
Gilles Peskinebef7f142018-07-12 17:22:21 +02003137#if defined(MBEDTLS_MD_C)
3138/* Read some bytes from an HKDF-based generator. This performs a chunk
3139 * of the expand phase of the HKDF algorithm. */
3140static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3141 psa_algorithm_t hash_alg,
3142 uint8_t *output,
3143 size_t output_length )
3144{
3145 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3146 psa_status_t status;
3147
3148 while( output_length != 0 )
3149 {
3150 /* Copy what remains of the current block */
3151 uint8_t n = hash_length - hkdf->offset_in_block;
3152 if( n > output_length )
3153 n = (uint8_t) output_length;
3154 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3155 output += n;
3156 output_length -= n;
3157 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003158 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003159 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003160 /* We can't be wanting more output after block 0xff, otherwise
3161 * the capacity check in psa_generator_read() would have
3162 * prevented this call. It could happen only if the generator
3163 * object was corrupted or if this function is called directly
3164 * inside the library. */
3165 if( hkdf->block_number == 0xff )
3166 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003167
3168 /* We need a new block */
3169 ++hkdf->block_number;
3170 hkdf->offset_in_block = 0;
3171 status = psa_hmac_setup_internal( &hkdf->hmac,
3172 hkdf->prk, hash_length,
3173 hash_alg );
3174 if( status != PSA_SUCCESS )
3175 return( status );
3176 if( hkdf->block_number != 1 )
3177 {
3178 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3179 hkdf->output_block,
3180 hash_length );
3181 if( status != PSA_SUCCESS )
3182 return( status );
3183 }
3184 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3185 hkdf->info,
3186 hkdf->info_length );
3187 if( status != PSA_SUCCESS )
3188 return( status );
3189 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3190 &hkdf->block_number, 1 );
3191 if( status != PSA_SUCCESS )
3192 return( status );
3193 status = psa_hmac_finish_internal( &hkdf->hmac,
3194 hkdf->output_block,
3195 sizeof( hkdf->output_block ) );
3196 if( status != PSA_SUCCESS )
3197 return( status );
3198 }
3199
3200 return( PSA_SUCCESS );
3201}
3202#endif /* MBEDTLS_MD_C */
3203
Gilles Peskineeab56e42018-07-12 17:12:33 +02003204psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3205 uint8_t *output,
3206 size_t output_length )
3207{
3208 psa_status_t status;
3209
3210 if( output_length > generator->capacity )
3211 {
3212 generator->capacity = 0;
3213 /* Go through the error path to wipe all confidential data now
3214 * that the generator object is useless. */
3215 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3216 goto exit;
3217 }
3218 if( output_length == 0 &&
3219 generator->capacity == 0 && generator->alg == 0 )
3220 {
3221 /* Edge case: this is a blank or finished generator, and 0
3222 * bytes were requested. The right error in this case could
3223 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3224 * INSUFFICIENT_CAPACITY, which is right for a finished
3225 * generator, for consistency with the case when
3226 * output_length > 0. */
3227 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3228 }
3229 generator->capacity -= output_length;
3230
Gilles Peskinebef7f142018-07-12 17:22:21 +02003231#if defined(MBEDTLS_MD_C)
3232 if( PSA_ALG_IS_HKDF( generator->alg ) )
3233 {
3234 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3235 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3236 output, output_length );
3237 }
3238 else
3239#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003240 {
3241 return( PSA_ERROR_BAD_STATE );
3242 }
3243
3244exit:
3245 if( status != PSA_SUCCESS )
3246 {
3247 psa_generator_abort( generator );
3248 memset( output, '!', output_length );
3249 }
3250 return( status );
3251}
3252
Gilles Peskine08542d82018-07-19 17:05:42 +02003253#if defined(MBEDTLS_DES_C)
3254static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3255{
3256 if( data_size >= 8 )
3257 mbedtls_des_key_set_parity( data );
3258 if( data_size >= 16 )
3259 mbedtls_des_key_set_parity( data + 8 );
3260 if( data_size >= 24 )
3261 mbedtls_des_key_set_parity( data + 16 );
3262}
3263#endif /* MBEDTLS_DES_C */
3264
Gilles Peskineeab56e42018-07-12 17:12:33 +02003265psa_status_t psa_generator_import_key( psa_key_slot_t key,
3266 psa_key_type_t type,
3267 size_t bits,
3268 psa_crypto_generator_t *generator )
3269{
3270 uint8_t *data = NULL;
3271 size_t bytes = PSA_BITS_TO_BYTES( bits );
3272 psa_status_t status;
3273
3274 if( ! key_type_is_raw_bytes( type ) )
3275 return( PSA_ERROR_INVALID_ARGUMENT );
3276 if( bits % 8 != 0 )
3277 return( PSA_ERROR_INVALID_ARGUMENT );
3278 data = mbedtls_calloc( 1, bytes );
3279 if( data == NULL )
3280 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3281
3282 status = psa_generator_read( generator, data, bytes );
3283 if( status != PSA_SUCCESS )
3284 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003285#if defined(MBEDTLS_DES_C)
3286 if( type == PSA_KEY_TYPE_DES )
3287 psa_des_set_key_parity( data, bytes );
3288#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003289 status = psa_import_key( key, type, data, bytes );
3290
3291exit:
3292 mbedtls_free( data );
3293 return( status );
3294}
3295
3296
3297
3298/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003299/* Key derivation */
3300/****************************************************************/
3301
Gilles Peskinebef7f142018-07-12 17:22:21 +02003302/* Set up an HKDF-based generator. This is exactly the extract phase
3303 * of the HKDF algorithm. */
3304static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
3305 key_slot_t *slot,
3306 psa_algorithm_t hash_alg,
3307 const uint8_t *salt,
3308 size_t salt_length,
3309 const uint8_t *label,
3310 size_t label_length )
3311{
3312 psa_status_t status;
3313 status = psa_hmac_setup_internal( &hkdf->hmac,
3314 salt, salt_length,
Gilles Peskine00709fa2018-08-22 18:25:41 +02003315 PSA_ALG_HMAC_GET_HASH( hash_alg ) );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003316 if( status != PSA_SUCCESS )
3317 return( status );
3318 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3319 slot->data.raw.data,
3320 slot->data.raw.bytes );
3321 if( status != PSA_SUCCESS )
3322 return( status );
3323 status = psa_hmac_finish_internal( &hkdf->hmac,
3324 hkdf->prk,
3325 sizeof( hkdf->prk ) );
3326 if( status != PSA_SUCCESS )
3327 return( status );
3328 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3329 hkdf->block_number = 0;
3330 hkdf->info_length = label_length;
3331 if( label_length != 0 )
3332 {
3333 hkdf->info = mbedtls_calloc( 1, label_length );
3334 if( hkdf->info == NULL )
3335 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3336 memcpy( hkdf->info, label, label_length );
3337 }
3338 return( PSA_SUCCESS );
3339}
3340
Gilles Peskineea0fb492018-07-12 17:17:20 +02003341psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01003342 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003343 psa_algorithm_t alg,
3344 const uint8_t *salt,
3345 size_t salt_length,
3346 const uint8_t *label,
3347 size_t label_length,
3348 size_t capacity )
3349{
3350 key_slot_t *slot;
3351 psa_status_t status;
3352
3353 if( generator->alg != 0 )
3354 return( PSA_ERROR_BAD_STATE );
3355
3356 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
3357 if( status != PSA_SUCCESS )
3358 return( status );
3359 if( slot->type != PSA_KEY_TYPE_DERIVE )
3360 return( PSA_ERROR_INVALID_ARGUMENT );
3361
3362 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
3363 return( PSA_ERROR_INVALID_ARGUMENT );
3364
Gilles Peskinebef7f142018-07-12 17:22:21 +02003365#if defined(MBEDTLS_MD_C)
3366 if( PSA_ALG_IS_HKDF( alg ) )
3367 {
3368 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3369 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3370 if( hash_size == 0 )
3371 return( PSA_ERROR_NOT_SUPPORTED );
3372 if( capacity > 255 * hash_size )
3373 return( PSA_ERROR_INVALID_ARGUMENT );
3374 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3375 slot,
3376 hash_alg,
3377 salt, salt_length,
3378 label, label_length );
3379 }
3380 else
3381#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +02003382 {
3383 return( PSA_ERROR_NOT_SUPPORTED );
3384 }
3385
3386 /* Set generator->alg even on failure so that abort knows what to do. */
3387 generator->alg = alg;
3388 if( status == PSA_SUCCESS )
3389 generator->capacity = capacity;
3390 else
3391 psa_generator_abort( generator );
3392 return( status );
3393}
3394
3395
3396
3397/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003398/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02003399/****************************************************************/
3400
3401psa_status_t psa_generate_random( uint8_t *output,
3402 size_t output_size )
3403{
itayzafrir0adf0fc2018-09-06 16:24:41 +03003404 int ret;
3405 GUARD_MODULE_INITIALIZED;
3406
3407 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
Gilles Peskine05d69892018-06-19 22:00:52 +02003408 return( mbedtls_to_psa_error( ret ) );
3409}
3410
3411psa_status_t psa_generate_key( psa_key_slot_t key,
3412 psa_key_type_t type,
3413 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003414 const void *extra,
3415 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02003416{
Gilles Peskine12313cd2018-06-20 00:20:32 +02003417 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003418 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003419
Gilles Peskine53d991e2018-07-12 01:14:59 +02003420 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003421 return( PSA_ERROR_INVALID_ARGUMENT );
3422
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003423 status = psa_get_empty_key_slot( key, &slot );
3424 if( status != PSA_SUCCESS )
3425 return( status );
3426
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003427 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003428 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003429 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003430 if( status != PSA_SUCCESS )
3431 return( status );
3432 status = psa_generate_random( slot->data.raw.data,
3433 slot->data.raw.bytes );
3434 if( status != PSA_SUCCESS )
3435 {
3436 mbedtls_free( slot->data.raw.data );
3437 return( status );
3438 }
3439#if defined(MBEDTLS_DES_C)
3440 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02003441 psa_des_set_key_parity( slot->data.raw.data,
3442 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003443#endif /* MBEDTLS_DES_C */
3444 }
3445 else
3446
3447#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3448 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3449 {
3450 mbedtls_rsa_context *rsa;
3451 int ret;
3452 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003453 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3454 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02003455 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003456 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02003457 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003458 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003459 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003460#if INT_MAX < 0xffffffff
3461 /* Check that the uint32_t value passed by the caller fits
3462 * in the range supported by this implementation. */
3463 if( p->e > INT_MAX )
3464 return( PSA_ERROR_NOT_SUPPORTED );
3465#endif
3466 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003467 }
3468 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3469 if( rsa == NULL )
3470 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3471 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3472 ret = mbedtls_rsa_gen_key( rsa,
3473 mbedtls_ctr_drbg_random,
3474 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003475 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003476 exponent );
3477 if( ret != 0 )
3478 {
3479 mbedtls_rsa_free( rsa );
3480 mbedtls_free( rsa );
3481 return( mbedtls_to_psa_error( ret ) );
3482 }
3483 slot->data.rsa = rsa;
3484 }
3485 else
3486#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3487
3488#if defined(MBEDTLS_ECP_C)
3489 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3490 {
3491 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3492 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3493 const mbedtls_ecp_curve_info *curve_info =
3494 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3495 mbedtls_ecp_keypair *ecp;
3496 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003497 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003498 return( PSA_ERROR_NOT_SUPPORTED );
3499 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3500 return( PSA_ERROR_NOT_SUPPORTED );
3501 if( curve_info->bit_size != bits )
3502 return( PSA_ERROR_INVALID_ARGUMENT );
3503 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3504 if( ecp == NULL )
3505 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3506 mbedtls_ecp_keypair_init( ecp );
3507 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3508 mbedtls_ctr_drbg_random,
3509 &global_data.ctr_drbg );
3510 if( ret != 0 )
3511 {
3512 mbedtls_ecp_keypair_free( ecp );
3513 mbedtls_free( ecp );
3514 return( mbedtls_to_psa_error( ret ) );
3515 }
3516 slot->data.ecp = ecp;
3517 }
3518 else
3519#endif /* MBEDTLS_ECP_C */
3520
3521 return( PSA_ERROR_NOT_SUPPORTED );
3522
3523 slot->type = type;
3524 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003525}
3526
3527
3528/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003529/* Module setup */
3530/****************************************************************/
3531
Gilles Peskinee59236f2018-01-27 23:32:46 +01003532void mbedtls_psa_crypto_free( void )
3533{
Jaeden Amero045bd502018-06-26 14:00:08 +01003534 psa_key_slot_t key;
Gilles Peskine9a056342018-08-01 15:46:54 +02003535 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003536 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003537 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3538 mbedtls_entropy_free( &global_data.entropy );
3539 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3540}
3541
3542psa_status_t psa_crypto_init( void )
3543{
3544 int ret;
3545 const unsigned char drbg_seed[] = "PSA";
3546
3547 if( global_data.initialized != 0 )
3548 return( PSA_SUCCESS );
3549
3550 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3551 mbedtls_entropy_init( &global_data.entropy );
3552 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3553
3554 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3555 mbedtls_entropy_func,
3556 &global_data.entropy,
3557 drbg_seed, sizeof( drbg_seed ) - 1 );
3558 if( ret != 0 )
3559 goto exit;
3560
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003561 global_data.initialized = 1;
3562
Gilles Peskinee59236f2018-01-27 23:32:46 +01003563exit:
3564 if( ret != 0 )
3565 mbedtls_psa_crypto_free( );
3566 return( mbedtls_to_psa_error( ret ) );
3567}
3568
3569#endif /* MBEDTLS_PSA_CRYPTO_C */