blob: dfbb6800f5d33ced5fcd065112baa1757ce707b0 [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
151static psa_status_t mbedtls_to_psa_error( int ret )
152{
Gilles Peskinea5905292018-02-07 20:59:33 +0100153 /* If there's both a high-level code and low-level code, dispatch on
154 * the high-level code. */
155 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100156 {
157 case 0:
158 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100159
160 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
161 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
162 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
163 return( PSA_ERROR_NOT_SUPPORTED );
164 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
165 return( PSA_ERROR_HARDWARE_FAILURE );
166
167 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
168 return( PSA_ERROR_HARDWARE_FAILURE );
169
Gilles Peskine9a944802018-06-21 09:35:35 +0200170 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
171 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
172 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
173 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
174 case MBEDTLS_ERR_ASN1_INVALID_DATA:
175 return( PSA_ERROR_INVALID_ARGUMENT );
176 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
177 return( PSA_ERROR_INSUFFICIENT_MEMORY );
178 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
179 return( PSA_ERROR_BUFFER_TOO_SMALL );
180
Gilles Peskinea5905292018-02-07 20:59:33 +0100181 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
182 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
183 return( PSA_ERROR_NOT_SUPPORTED );
184 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
185 return( PSA_ERROR_HARDWARE_FAILURE );
186
187 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
188 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
189 return( PSA_ERROR_NOT_SUPPORTED );
190 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
191 return( PSA_ERROR_HARDWARE_FAILURE );
192
193 case MBEDTLS_ERR_CCM_BAD_INPUT:
194 return( PSA_ERROR_INVALID_ARGUMENT );
195 case MBEDTLS_ERR_CCM_AUTH_FAILED:
196 return( PSA_ERROR_INVALID_SIGNATURE );
197 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
198 return( PSA_ERROR_HARDWARE_FAILURE );
199
200 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
201 return( PSA_ERROR_NOT_SUPPORTED );
202 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
203 return( PSA_ERROR_INVALID_ARGUMENT );
204 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
205 return( PSA_ERROR_INSUFFICIENT_MEMORY );
206 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
207 return( PSA_ERROR_INVALID_PADDING );
208 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
209 return( PSA_ERROR_BAD_STATE );
210 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
211 return( PSA_ERROR_INVALID_SIGNATURE );
212 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
213 return( PSA_ERROR_TAMPERING_DETECTED );
214 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
215 return( PSA_ERROR_HARDWARE_FAILURE );
216
217 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
218 return( PSA_ERROR_HARDWARE_FAILURE );
219
220 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
221 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
222 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
223 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
224 return( PSA_ERROR_NOT_SUPPORTED );
225 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
226 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
227
228 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
229 return( PSA_ERROR_NOT_SUPPORTED );
230 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
231 return( PSA_ERROR_HARDWARE_FAILURE );
232
Gilles Peskinee59236f2018-01-27 23:32:46 +0100233 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
234 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
235 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
236 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100237
238 case MBEDTLS_ERR_GCM_AUTH_FAILED:
239 return( PSA_ERROR_INVALID_SIGNATURE );
240 case MBEDTLS_ERR_GCM_BAD_INPUT:
241 return( PSA_ERROR_NOT_SUPPORTED );
242 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
243 return( PSA_ERROR_HARDWARE_FAILURE );
244
245 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
246 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
247 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
248 return( PSA_ERROR_HARDWARE_FAILURE );
249
250 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
251 return( PSA_ERROR_NOT_SUPPORTED );
252 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
253 return( PSA_ERROR_INVALID_ARGUMENT );
254 case MBEDTLS_ERR_MD_ALLOC_FAILED:
255 return( PSA_ERROR_INSUFFICIENT_MEMORY );
256 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
257 return( PSA_ERROR_STORAGE_FAILURE );
258 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
259 return( PSA_ERROR_HARDWARE_FAILURE );
260
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100261 case MBEDTLS_ERR_PK_ALLOC_FAILED:
262 return( PSA_ERROR_INSUFFICIENT_MEMORY );
263 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
264 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
265 return( PSA_ERROR_INVALID_ARGUMENT );
266 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100267 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100268 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
269 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
272 return( PSA_ERROR_NOT_SUPPORTED );
273 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
274 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
275 return( PSA_ERROR_NOT_PERMITTED );
276 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_PK_INVALID_ALG:
279 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
280 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
281 return( PSA_ERROR_NOT_SUPPORTED );
282 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
283 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100284 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
285 return( PSA_ERROR_HARDWARE_FAILURE );
286
287 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
288 return( PSA_ERROR_HARDWARE_FAILURE );
289
290 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_RSA_INVALID_PADDING:
293 return( PSA_ERROR_INVALID_PADDING );
294 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
297 return( PSA_ERROR_INVALID_ARGUMENT );
298 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
299 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
300 return( PSA_ERROR_TAMPERING_DETECTED );
301 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
302 return( PSA_ERROR_INVALID_SIGNATURE );
303 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
304 return( PSA_ERROR_BUFFER_TOO_SMALL );
305 case MBEDTLS_ERR_RSA_RNG_FAILED:
306 return( PSA_ERROR_INSUFFICIENT_MEMORY );
307 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
308 return( PSA_ERROR_NOT_SUPPORTED );
309 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
310 return( PSA_ERROR_HARDWARE_FAILURE );
311
312 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
313 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
314 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
315 return( PSA_ERROR_HARDWARE_FAILURE );
316
317 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
318 return( PSA_ERROR_INVALID_ARGUMENT );
319 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
320 return( PSA_ERROR_HARDWARE_FAILURE );
321
itayzafrir5c753392018-05-08 11:18:38 +0300322 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300323 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300324 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300325 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
326 return( PSA_ERROR_BUFFER_TOO_SMALL );
327 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
328 return( PSA_ERROR_NOT_SUPPORTED );
329 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
330 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
331 return( PSA_ERROR_INVALID_SIGNATURE );
332 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
333 return( PSA_ERROR_INSUFFICIENT_MEMORY );
334 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
335 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300336
Gilles Peskinee59236f2018-01-27 23:32:46 +0100337 default:
338 return( PSA_ERROR_UNKNOWN_ERROR );
339 }
340}
341
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200342/* Retrieve a key slot, occupied or not. */
343static psa_status_t psa_get_key_slot( psa_key_slot_t key,
344 key_slot_t **p_slot )
345{
Gilles Peskine996deb12018-08-01 15:45:45 +0200346 /* 0 is not a valid slot number under any circumstance. This
347 * implementation provides slots number 1 to N where N is the
348 * number of available slots. */
349 if( key == 0 || key > ARRAY_LENGTH( global_data.key_slots ) )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200350 return( PSA_ERROR_INVALID_ARGUMENT );
351
Gilles Peskine996deb12018-08-01 15:45:45 +0200352 *p_slot = &global_data.key_slots[key - 1];
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200353 return( PSA_SUCCESS );
354}
355
356/* Retrieve an empty key slot (slot with no key data, but possibly
357 * with some metadata such as a policy). */
358static psa_status_t psa_get_empty_key_slot( psa_key_slot_t key,
359 key_slot_t **p_slot )
360{
361 psa_status_t status;
362 key_slot_t *slot = NULL;
363
364 *p_slot = NULL;
365
366 status = psa_get_key_slot( key, &slot );
367 if( status != PSA_SUCCESS )
368 return( status );
369
370 if( slot->type != PSA_KEY_TYPE_NONE )
371 return( PSA_ERROR_OCCUPIED_SLOT );
372
373 *p_slot = slot;
374 return( status );
375}
376
Jaeden Amerob4fa8c92018-07-11 15:57:44 +0100377/** Retrieve a slot which must contain a key. The key must have allow all the
378 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
379 * operations with this algorithm. */
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200380static psa_status_t psa_get_key_from_slot( psa_key_slot_t key,
381 key_slot_t **p_slot,
382 psa_key_usage_t usage,
383 psa_algorithm_t alg )
384{
385 psa_status_t status;
386 key_slot_t *slot = NULL;
387
388 *p_slot = NULL;
389
390 status = psa_get_key_slot( key, &slot );
391 if( status != PSA_SUCCESS )
392 return( status );
393 if( slot->type == PSA_KEY_TYPE_NONE )
394 return( PSA_ERROR_EMPTY_SLOT );
395
396 /* Enforce that usage policy for the key slot contains all the flags
397 * required by the usage parameter. There is one exception: public
398 * keys can always be exported, so we treat public key objects as
399 * if they had the export flag. */
400 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
401 usage &= ~PSA_KEY_USAGE_EXPORT;
402 if( ( slot->policy.usage & usage ) != usage )
403 return( PSA_ERROR_NOT_PERMITTED );
404 if( alg != 0 && ( alg != slot->policy.alg ) )
405 return( PSA_ERROR_NOT_PERMITTED );
406
407 *p_slot = slot;
408 return( PSA_SUCCESS );
409}
410
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200411
412
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100413/****************************************************************/
414/* Key management */
415/****************************************************************/
416
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100417#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200418static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
419{
420 switch( grpid )
421 {
422 case MBEDTLS_ECP_DP_SECP192R1:
423 return( PSA_ECC_CURVE_SECP192R1 );
424 case MBEDTLS_ECP_DP_SECP224R1:
425 return( PSA_ECC_CURVE_SECP224R1 );
426 case MBEDTLS_ECP_DP_SECP256R1:
427 return( PSA_ECC_CURVE_SECP256R1 );
428 case MBEDTLS_ECP_DP_SECP384R1:
429 return( PSA_ECC_CURVE_SECP384R1 );
430 case MBEDTLS_ECP_DP_SECP521R1:
431 return( PSA_ECC_CURVE_SECP521R1 );
432 case MBEDTLS_ECP_DP_BP256R1:
433 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
434 case MBEDTLS_ECP_DP_BP384R1:
435 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
436 case MBEDTLS_ECP_DP_BP512R1:
437 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
438 case MBEDTLS_ECP_DP_CURVE25519:
439 return( PSA_ECC_CURVE_CURVE25519 );
440 case MBEDTLS_ECP_DP_SECP192K1:
441 return( PSA_ECC_CURVE_SECP192K1 );
442 case MBEDTLS_ECP_DP_SECP224K1:
443 return( PSA_ECC_CURVE_SECP224K1 );
444 case MBEDTLS_ECP_DP_SECP256K1:
445 return( PSA_ECC_CURVE_SECP256K1 );
446 case MBEDTLS_ECP_DP_CURVE448:
447 return( PSA_ECC_CURVE_CURVE448 );
448 default:
449 return( 0 );
450 }
451}
452
Gilles Peskine12313cd2018-06-20 00:20:32 +0200453static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
454{
455 switch( curve )
456 {
457 case PSA_ECC_CURVE_SECP192R1:
458 return( MBEDTLS_ECP_DP_SECP192R1 );
459 case PSA_ECC_CURVE_SECP224R1:
460 return( MBEDTLS_ECP_DP_SECP224R1 );
461 case PSA_ECC_CURVE_SECP256R1:
462 return( MBEDTLS_ECP_DP_SECP256R1 );
463 case PSA_ECC_CURVE_SECP384R1:
464 return( MBEDTLS_ECP_DP_SECP384R1 );
465 case PSA_ECC_CURVE_SECP521R1:
466 return( MBEDTLS_ECP_DP_SECP521R1 );
467 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
468 return( MBEDTLS_ECP_DP_BP256R1 );
469 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
470 return( MBEDTLS_ECP_DP_BP384R1 );
471 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
472 return( MBEDTLS_ECP_DP_BP512R1 );
473 case PSA_ECC_CURVE_CURVE25519:
474 return( MBEDTLS_ECP_DP_CURVE25519 );
475 case PSA_ECC_CURVE_SECP192K1:
476 return( MBEDTLS_ECP_DP_SECP192K1 );
477 case PSA_ECC_CURVE_SECP224K1:
478 return( MBEDTLS_ECP_DP_SECP224K1 );
479 case PSA_ECC_CURVE_SECP256K1:
480 return( MBEDTLS_ECP_DP_SECP256K1 );
481 case PSA_ECC_CURVE_CURVE448:
482 return( MBEDTLS_ECP_DP_CURVE448 );
483 default:
484 return( MBEDTLS_ECP_DP_NONE );
485 }
486}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100487#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200488
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200489static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
490 size_t bits,
491 struct raw_data *raw )
492{
493 /* Check that the bit size is acceptable for the key type */
494 switch( type )
495 {
496 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200497 if( bits == 0 )
498 {
499 raw->bytes = 0;
500 raw->data = NULL;
501 return( PSA_SUCCESS );
502 }
503 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200504#if defined(MBEDTLS_MD_C)
505 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200506#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200507 case PSA_KEY_TYPE_DERIVE:
508 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200509#if defined(MBEDTLS_AES_C)
510 case PSA_KEY_TYPE_AES:
511 if( bits != 128 && bits != 192 && bits != 256 )
512 return( PSA_ERROR_INVALID_ARGUMENT );
513 break;
514#endif
515#if defined(MBEDTLS_CAMELLIA_C)
516 case PSA_KEY_TYPE_CAMELLIA:
517 if( bits != 128 && bits != 192 && bits != 256 )
518 return( PSA_ERROR_INVALID_ARGUMENT );
519 break;
520#endif
521#if defined(MBEDTLS_DES_C)
522 case PSA_KEY_TYPE_DES:
523 if( bits != 64 && bits != 128 && bits != 192 )
524 return( PSA_ERROR_INVALID_ARGUMENT );
525 break;
526#endif
527#if defined(MBEDTLS_ARC4_C)
528 case PSA_KEY_TYPE_ARC4:
529 if( bits < 8 || bits > 2048 )
530 return( PSA_ERROR_INVALID_ARGUMENT );
531 break;
532#endif
533 default:
534 return( PSA_ERROR_NOT_SUPPORTED );
535 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200536 if( bits % 8 != 0 )
537 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200538
539 /* Allocate memory for the key */
540 raw->bytes = PSA_BITS_TO_BYTES( bits );
541 raw->data = mbedtls_calloc( 1, raw->bytes );
542 if( raw->data == NULL )
543 {
544 raw->bytes = 0;
545 return( PSA_ERROR_INSUFFICIENT_MEMORY );
546 }
547 return( PSA_SUCCESS );
548}
549
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200550#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
551static psa_status_t psa_import_rsa_key( mbedtls_pk_context *pk,
552 mbedtls_rsa_context **p_rsa )
553{
554 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_RSA )
555 return( PSA_ERROR_INVALID_ARGUMENT );
556 else
557 {
558 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *pk );
559 size_t bits = mbedtls_rsa_get_bitlen( rsa );
560 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
561 return( PSA_ERROR_NOT_SUPPORTED );
562 *p_rsa = rsa;
563 return( PSA_SUCCESS );
564 }
565}
566#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
567
568#if defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C)
569static psa_status_t psa_import_ecp_key( psa_ecc_curve_t expected_curve,
570 mbedtls_pk_context *pk,
571 mbedtls_ecp_keypair **p_ecp )
572{
573 if( mbedtls_pk_get_type( pk ) != MBEDTLS_PK_ECKEY )
574 return( PSA_ERROR_INVALID_ARGUMENT );
575 else
576 {
577 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( *pk );
578 psa_ecc_curve_t actual_curve = mbedtls_ecc_group_to_psa( ecp->grp.id );
579 if( actual_curve != expected_curve )
580 return( PSA_ERROR_INVALID_ARGUMENT );
581 *p_ecp = ecp;
582 return( PSA_SUCCESS );
583 }
584}
585#endif /* defined(MBEDTLS_ECP_C) && defined(MBEDTLS_PK_PARSE_C) */
586
Gilles Peskine2d277862018-06-18 15:41:12 +0200587psa_status_t psa_import_key( psa_key_slot_t key,
588 psa_key_type_t type,
589 const uint8_t *data,
590 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100591{
592 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200593 psa_status_t status = PSA_SUCCESS;
594 status = psa_get_empty_key_slot( key, &slot );
595 if( status != PSA_SUCCESS )
596 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100597
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200598 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100599 {
Gilles Peskine6d912132018-03-07 16:41:37 +0100600 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100601 if( data_length > SIZE_MAX / 8 )
602 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200603 status = prepare_raw_data_slot( type,
604 PSA_BYTES_TO_BITS( data_length ),
605 &slot->data.raw );
606 if( status != PSA_SUCCESS )
607 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200608 if( data_length != 0 )
609 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100610 }
611 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100612#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200613 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100614 {
615 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100616 mbedtls_pk_context pk;
617 mbedtls_pk_init( &pk );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200618
619 /* Parse the data. */
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100620 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
621 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
622 else
623 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100624 if( ret != 0 )
625 return( mbedtls_to_psa_error( ret ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200626
627 /* We have something that the pkparse module recognizes.
628 * If it has the expected type and passes any type-specific
629 * checks, store it. */
Gilles Peskine969ac722018-01-28 18:16:59 +0100630#if defined(MBEDTLS_RSA_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200631 if( PSA_KEY_TYPE_IS_RSA( type ) )
632 status = psa_import_rsa_key( &pk, &slot->data.rsa );
633 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100634#endif /* MBEDTLS_RSA_C */
635#if defined(MBEDTLS_ECP_C)
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200636 if( PSA_KEY_TYPE_IS_ECC( type ) )
637 status = psa_import_ecp_key( PSA_KEY_TYPE_GET_CURVE( type ),
638 &pk, &slot->data.ecp );
639 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100640#endif /* MBEDTLS_ECP_C */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200641 {
642 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskinec648d692018-06-28 08:46:13 +0200643 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200644
Gilles Peskinec648d692018-06-28 08:46:13 +0200645 /* Free the content of the pk object only on error. On success,
646 * the content of the object has been stored in the slot. */
647 if( status != PSA_SUCCESS )
648 {
649 mbedtls_pk_free( &pk );
650 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100651 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100652 }
653 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100654#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100655 {
656 return( PSA_ERROR_NOT_SUPPORTED );
657 }
658
659 slot->type = type;
660 return( PSA_SUCCESS );
661}
662
Gilles Peskine2d277862018-06-18 15:41:12 +0200663psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100664{
665 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200666 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100667
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200668 status = psa_get_key_slot( key, &slot );
669 if( status != PSA_SUCCESS )
670 return( status );
671
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100672 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200673 {
674 /* No key material to clean, but do zeroize the slot below to wipe
675 * metadata such as policies. */
676 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200677 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100678 {
679 mbedtls_free( slot->data.raw.data );
680 }
681 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100682#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200683 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100684 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100685 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100686 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100687 }
688 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100689#endif /* defined(MBEDTLS_RSA_C) */
690#if defined(MBEDTLS_ECP_C)
691 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
692 {
693 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100694 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100695 }
696 else
697#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100698 {
699 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100700 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100701 return( PSA_ERROR_TAMPERING_DETECTED );
702 }
703
704 mbedtls_zeroize( slot, sizeof( *slot ) );
705 return( PSA_SUCCESS );
706}
707
Gilles Peskineb870b182018-07-06 16:02:09 +0200708/* Return the size of the key in the given slot, in bits. */
709static size_t psa_get_key_bits( const key_slot_t *slot )
710{
711 if( key_type_is_raw_bytes( slot->type ) )
712 return( slot->data.raw.bytes * 8 );
713#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200714 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Gilles Peskineb870b182018-07-06 16:02:09 +0200715 return( mbedtls_rsa_get_bitlen( slot->data.rsa ) );
716#endif /* defined(MBEDTLS_RSA_C) */
717#if defined(MBEDTLS_ECP_C)
718 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
719 return( slot->data.ecp->grp.pbits );
720#endif /* defined(MBEDTLS_ECP_C) */
721 /* Shouldn't happen except on an empty slot. */
722 return( 0 );
723}
724
Gilles Peskine2d277862018-06-18 15:41:12 +0200725psa_status_t psa_get_key_information( psa_key_slot_t key,
726 psa_key_type_t *type,
727 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100728{
729 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200730 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100731
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100732 if( type != NULL )
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200733 *type = 0;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100734 if( bits != NULL )
735 *bits = 0;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200736 status = psa_get_key_slot( key, &slot );
737 if( status != PSA_SUCCESS )
738 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +0200739
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740 if( slot->type == PSA_KEY_TYPE_NONE )
741 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200742 if( type != NULL )
743 *type = slot->type;
Gilles Peskineb870b182018-07-06 16:02:09 +0200744 if( bits != NULL )
745 *bits = psa_get_key_bits( slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100746 return( PSA_SUCCESS );
747}
748
Gilles Peskine2d277862018-06-18 15:41:12 +0200749static psa_status_t psa_internal_export_key( psa_key_slot_t key,
750 uint8_t *data,
751 size_t data_size,
752 size_t *data_length,
753 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100754{
755 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200756 psa_status_t status;
757 /* Exporting a public key doesn't require a usage flag. If we're
758 * called by psa_export_public_key(), don't require the EXPORT flag.
759 * If we're called by psa_export_key(), do require the EXPORT flag;
760 * if the key turns out to be public key object, psa_get_key_from_slot()
761 * will ignore this flag. */
762 psa_key_usage_t usage = export_public_key ? 0 : PSA_KEY_USAGE_EXPORT;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100763
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100764 /* Set the key to empty now, so that even when there are errors, we always
765 * set data_length to a value between 0 and data_size. On error, setting
766 * the key to empty is a good choice because an empty key representation is
767 * unlikely to be accepted anywhere. */
768 *data_length = 0;
769
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200770 status = psa_get_key_from_slot( key, &slot, usage, 0 );
771 if( status != PSA_SUCCESS )
772 return( status );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200773 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300774 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300775
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200776 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100777 {
778 if( slot->data.raw.bytes > data_size )
779 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200780 if( slot->data.raw.bytes != 0 )
781 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100782 *data_length = slot->data.raw.bytes;
783 return( PSA_SUCCESS );
784 }
785 else
Moran Peker17e36e12018-05-02 12:55:20 +0300786 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100787#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskined8008d62018-06-29 19:51:51 +0200788 if( PSA_KEY_TYPE_IS_RSA( slot->type ) ||
Moran Pekera998bc62018-04-16 18:16:20 +0300789 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100790 {
Moran Pekera998bc62018-04-16 18:16:20 +0300791 mbedtls_pk_context pk;
792 int ret;
Gilles Peskined8008d62018-06-29 19:51:51 +0200793 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300794 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100795#if defined(MBEDTLS_RSA_C)
796 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300797 pk.pk_info = &mbedtls_rsa_info;
798 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100799#else
800 return( PSA_ERROR_NOT_SUPPORTED );
801#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300802 }
803 else
804 {
Darryl Green9e2d7a02018-07-24 16:33:30 +0100805#if defined(MBEDTLS_ECP_C)
806 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +0300807 pk.pk_info = &mbedtls_eckey_info;
808 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +0100809#else
810 return( PSA_ERROR_NOT_SUPPORTED );
811#endif
Moran Pekera998bc62018-04-16 18:16:20 +0300812 }
Moran Pekerd7326592018-05-29 16:56:39 +0300813 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300814 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300815 else
816 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300817 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200818 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200819 /* If data_size is 0 then data may be NULL and then the
820 * call to memset would have undefined behavior. */
821 if( data_size != 0 )
822 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300823 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200824 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200825 /* The mbedtls_pk_xxx functions write to the end of the buffer.
826 * Move the data to the beginning and erase remaining data
827 * at the original location. */
828 if( 2 * (size_t) ret <= data_size )
829 {
830 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200831 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200832 }
833 else if( (size_t) ret < data_size )
834 {
835 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200836 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200837 }
Moran Pekera998bc62018-04-16 18:16:20 +0300838 *data_length = ret;
839 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100840 }
841 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100842#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300843 {
844 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200845 it is valid for a special-purpose implementation to omit
846 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300847 return( PSA_ERROR_NOT_SUPPORTED );
848 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100849 }
850}
851
Gilles Peskine2d277862018-06-18 15:41:12 +0200852psa_status_t psa_export_key( psa_key_slot_t key,
853 uint8_t *data,
854 size_t data_size,
855 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300856{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200857 return( psa_internal_export_key( key, data, data_size,
858 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100859}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100860
Gilles Peskine2d277862018-06-18 15:41:12 +0200861psa_status_t psa_export_public_key( psa_key_slot_t key,
862 uint8_t *data,
863 size_t data_size,
864 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300865{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200866 return( psa_internal_export_key( key, data, data_size,
867 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300868}
869
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200870
871
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100872/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100873/* Message digests */
874/****************************************************************/
875
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100876static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100877{
878 switch( alg )
879 {
880#if defined(MBEDTLS_MD2_C)
881 case PSA_ALG_MD2:
882 return( &mbedtls_md2_info );
883#endif
884#if defined(MBEDTLS_MD4_C)
885 case PSA_ALG_MD4:
886 return( &mbedtls_md4_info );
887#endif
888#if defined(MBEDTLS_MD5_C)
889 case PSA_ALG_MD5:
890 return( &mbedtls_md5_info );
891#endif
892#if defined(MBEDTLS_RIPEMD160_C)
893 case PSA_ALG_RIPEMD160:
894 return( &mbedtls_ripemd160_info );
895#endif
896#if defined(MBEDTLS_SHA1_C)
897 case PSA_ALG_SHA_1:
898 return( &mbedtls_sha1_info );
899#endif
900#if defined(MBEDTLS_SHA256_C)
901 case PSA_ALG_SHA_224:
902 return( &mbedtls_sha224_info );
903 case PSA_ALG_SHA_256:
904 return( &mbedtls_sha256_info );
905#endif
906#if defined(MBEDTLS_SHA512_C)
907 case PSA_ALG_SHA_384:
908 return( &mbedtls_sha384_info );
909 case PSA_ALG_SHA_512:
910 return( &mbedtls_sha512_info );
911#endif
912 default:
913 return( NULL );
914 }
915}
916
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100917psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
918{
919 switch( operation->alg )
920 {
Gilles Peskine81736312018-06-26 15:04:31 +0200921 case 0:
922 /* The object has (apparently) been initialized but it is not
923 * in use. It's ok to call abort on such an object, and there's
924 * nothing to do. */
925 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100926#if defined(MBEDTLS_MD2_C)
927 case PSA_ALG_MD2:
928 mbedtls_md2_free( &operation->ctx.md2 );
929 break;
930#endif
931#if defined(MBEDTLS_MD4_C)
932 case PSA_ALG_MD4:
933 mbedtls_md4_free( &operation->ctx.md4 );
934 break;
935#endif
936#if defined(MBEDTLS_MD5_C)
937 case PSA_ALG_MD5:
938 mbedtls_md5_free( &operation->ctx.md5 );
939 break;
940#endif
941#if defined(MBEDTLS_RIPEMD160_C)
942 case PSA_ALG_RIPEMD160:
943 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
944 break;
945#endif
946#if defined(MBEDTLS_SHA1_C)
947 case PSA_ALG_SHA_1:
948 mbedtls_sha1_free( &operation->ctx.sha1 );
949 break;
950#endif
951#if defined(MBEDTLS_SHA256_C)
952 case PSA_ALG_SHA_224:
953 case PSA_ALG_SHA_256:
954 mbedtls_sha256_free( &operation->ctx.sha256 );
955 break;
956#endif
957#if defined(MBEDTLS_SHA512_C)
958 case PSA_ALG_SHA_384:
959 case PSA_ALG_SHA_512:
960 mbedtls_sha512_free( &operation->ctx.sha512 );
961 break;
962#endif
963 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +0200964 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100965 }
966 operation->alg = 0;
967 return( PSA_SUCCESS );
968}
969
Gilles Peskineda8191d2018-07-08 19:46:38 +0200970psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100971 psa_algorithm_t alg )
972{
973 int ret;
974 operation->alg = 0;
975 switch( alg )
976 {
977#if defined(MBEDTLS_MD2_C)
978 case PSA_ALG_MD2:
979 mbedtls_md2_init( &operation->ctx.md2 );
980 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
981 break;
982#endif
983#if defined(MBEDTLS_MD4_C)
984 case PSA_ALG_MD4:
985 mbedtls_md4_init( &operation->ctx.md4 );
986 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
987 break;
988#endif
989#if defined(MBEDTLS_MD5_C)
990 case PSA_ALG_MD5:
991 mbedtls_md5_init( &operation->ctx.md5 );
992 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
993 break;
994#endif
995#if defined(MBEDTLS_RIPEMD160_C)
996 case PSA_ALG_RIPEMD160:
997 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
998 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
999 break;
1000#endif
1001#if defined(MBEDTLS_SHA1_C)
1002 case PSA_ALG_SHA_1:
1003 mbedtls_sha1_init( &operation->ctx.sha1 );
1004 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
1005 break;
1006#endif
1007#if defined(MBEDTLS_SHA256_C)
1008 case PSA_ALG_SHA_224:
1009 mbedtls_sha256_init( &operation->ctx.sha256 );
1010 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
1011 break;
1012 case PSA_ALG_SHA_256:
1013 mbedtls_sha256_init( &operation->ctx.sha256 );
1014 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
1015 break;
1016#endif
1017#if defined(MBEDTLS_SHA512_C)
1018 case PSA_ALG_SHA_384:
1019 mbedtls_sha512_init( &operation->ctx.sha512 );
1020 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
1021 break;
1022 case PSA_ALG_SHA_512:
1023 mbedtls_sha512_init( &operation->ctx.sha512 );
1024 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
1025 break;
1026#endif
1027 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02001028 return( PSA_ALG_IS_HASH( alg ) ?
1029 PSA_ERROR_NOT_SUPPORTED :
1030 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001031 }
1032 if( ret == 0 )
1033 operation->alg = alg;
1034 else
1035 psa_hash_abort( operation );
1036 return( mbedtls_to_psa_error( ret ) );
1037}
1038
1039psa_status_t psa_hash_update( psa_hash_operation_t *operation,
1040 const uint8_t *input,
1041 size_t input_length )
1042{
1043 int ret;
Gilles Peskine94e44542018-07-12 16:58:43 +02001044
1045 /* Don't require hash implementations to behave correctly on a
1046 * zero-length input, which may have an invalid pointer. */
1047 if( input_length == 0 )
1048 return( PSA_SUCCESS );
1049
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001050 switch( operation->alg )
1051 {
1052#if defined(MBEDTLS_MD2_C)
1053 case PSA_ALG_MD2:
1054 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
1055 input, input_length );
1056 break;
1057#endif
1058#if defined(MBEDTLS_MD4_C)
1059 case PSA_ALG_MD4:
1060 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
1061 input, input_length );
1062 break;
1063#endif
1064#if defined(MBEDTLS_MD5_C)
1065 case PSA_ALG_MD5:
1066 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
1067 input, input_length );
1068 break;
1069#endif
1070#if defined(MBEDTLS_RIPEMD160_C)
1071 case PSA_ALG_RIPEMD160:
1072 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
1073 input, input_length );
1074 break;
1075#endif
1076#if defined(MBEDTLS_SHA1_C)
1077 case PSA_ALG_SHA_1:
1078 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
1079 input, input_length );
1080 break;
1081#endif
1082#if defined(MBEDTLS_SHA256_C)
1083 case PSA_ALG_SHA_224:
1084 case PSA_ALG_SHA_256:
1085 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
1086 input, input_length );
1087 break;
1088#endif
1089#if defined(MBEDTLS_SHA512_C)
1090 case PSA_ALG_SHA_384:
1091 case PSA_ALG_SHA_512:
1092 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
1093 input, input_length );
1094 break;
1095#endif
1096 default:
1097 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1098 break;
1099 }
Gilles Peskine94e44542018-07-12 16:58:43 +02001100
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001101 if( ret != 0 )
1102 psa_hash_abort( operation );
1103 return( mbedtls_to_psa_error( ret ) );
1104}
1105
1106psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
1107 uint8_t *hash,
1108 size_t hash_size,
1109 size_t *hash_length )
1110{
itayzafrir40835d42018-08-02 13:14:17 +03001111 psa_status_t status;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001112 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02001113 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001114
1115 /* Fill the output buffer with something that isn't a valid hash
1116 * (barring an attack on the hash and deliberately-crafted input),
1117 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02001118 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02001119 /* If hash_size is 0 then hash may be NULL and then the
1120 * call to memset would have undefined behavior. */
1121 if( hash_size != 0 )
1122 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001123
1124 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03001125 {
1126 status = PSA_ERROR_BUFFER_TOO_SMALL;
1127 goto exit;
1128 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001129
1130 switch( operation->alg )
1131 {
1132#if defined(MBEDTLS_MD2_C)
1133 case PSA_ALG_MD2:
1134 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1135 break;
1136#endif
1137#if defined(MBEDTLS_MD4_C)
1138 case PSA_ALG_MD4:
1139 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1140 break;
1141#endif
1142#if defined(MBEDTLS_MD5_C)
1143 case PSA_ALG_MD5:
1144 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1145 break;
1146#endif
1147#if defined(MBEDTLS_RIPEMD160_C)
1148 case PSA_ALG_RIPEMD160:
1149 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1150 break;
1151#endif
1152#if defined(MBEDTLS_SHA1_C)
1153 case PSA_ALG_SHA_1:
1154 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1155 break;
1156#endif
1157#if defined(MBEDTLS_SHA256_C)
1158 case PSA_ALG_SHA_224:
1159 case PSA_ALG_SHA_256:
1160 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1161 break;
1162#endif
1163#if defined(MBEDTLS_SHA512_C)
1164 case PSA_ALG_SHA_384:
1165 case PSA_ALG_SHA_512:
1166 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1167 break;
1168#endif
1169 default:
1170 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1171 break;
1172 }
itayzafrir40835d42018-08-02 13:14:17 +03001173 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001174
itayzafrir40835d42018-08-02 13:14:17 +03001175exit:
1176 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001177 {
Gilles Peskineaee13332018-07-02 12:15:28 +02001178 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001179 return( psa_hash_abort( operation ) );
1180 }
1181 else
1182 {
1183 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03001184 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001185 }
1186}
1187
Gilles Peskine2d277862018-06-18 15:41:12 +02001188psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1189 const uint8_t *hash,
1190 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001191{
1192 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1193 size_t actual_hash_length;
1194 psa_status_t status = psa_hash_finish( operation,
1195 actual_hash, sizeof( actual_hash ),
1196 &actual_hash_length );
1197 if( status != PSA_SUCCESS )
1198 return( status );
1199 if( actual_hash_length != hash_length )
1200 return( PSA_ERROR_INVALID_SIGNATURE );
1201 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1202 return( PSA_ERROR_INVALID_SIGNATURE );
1203 return( PSA_SUCCESS );
1204}
1205
1206
1207
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001208/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001209/* MAC */
1210/****************************************************************/
1211
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001212static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001213 psa_algorithm_t alg,
1214 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001215 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001216 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001217{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001218 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001219 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001220
1221 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1222 {
1223 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001224 {
1225 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1226 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001227
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001228 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001229 {
Gilles Peskine5d1888e2018-07-12 00:32:42 +02001230 case PSA_ALG_STREAM_CIPHER_BASE:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001231 mode = MBEDTLS_MODE_STREAM;
1232 break;
1233 case PSA_ALG_CBC_BASE:
1234 mode = MBEDTLS_MODE_CBC;
1235 break;
1236 case PSA_ALG_CFB_BASE:
1237 mode = MBEDTLS_MODE_CFB;
1238 break;
1239 case PSA_ALG_OFB_BASE:
1240 mode = MBEDTLS_MODE_OFB;
1241 break;
1242 case PSA_ALG_CTR:
1243 mode = MBEDTLS_MODE_CTR;
1244 break;
1245 case PSA_ALG_CCM:
1246 mode = MBEDTLS_MODE_CCM;
1247 break;
1248 case PSA_ALG_GCM:
1249 mode = MBEDTLS_MODE_GCM;
1250 break;
1251 default:
1252 return( NULL );
1253 }
1254 }
1255 else if( alg == PSA_ALG_CMAC )
1256 mode = MBEDTLS_MODE_ECB;
1257 else if( alg == PSA_ALG_GMAC )
1258 mode = MBEDTLS_MODE_GCM;
1259 else
1260 return( NULL );
1261
1262 switch( key_type )
1263 {
1264 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001265 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001266 break;
1267 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001268 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1269 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001270 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001271 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001272 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001273 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001274 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1275 * but two-key Triple-DES is functionally three-key Triple-DES
1276 * with K1=K3, so that's how we present it to mbedtls. */
1277 if( key_bits == 128 )
1278 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001279 break;
1280 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001281 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001282 break;
1283 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001284 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001285 break;
1286 default:
1287 return( NULL );
1288 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001289 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001290 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001291
Jaeden Amero23bbb752018-06-26 14:16:54 +01001292 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1293 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001294}
1295
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001296static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001297{
Gilles Peskine2d277862018-06-18 15:41:12 +02001298 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001299 {
1300 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001301 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001302 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001303 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001304 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001305 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001306 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001307 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001308 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001309 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001310 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001311 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001312 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001313 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001314 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001315 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001316 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001317 return( 128 );
1318 default:
1319 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001320 }
1321}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001322
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001323/* Initialize the MAC operation structure. Once this function has been
1324 * called, psa_mac_abort can run and will do the right thing. */
1325static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1326 psa_algorithm_t alg )
1327{
1328 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1329
1330 operation->alg = alg;
1331 operation->key_set = 0;
1332 operation->iv_set = 0;
1333 operation->iv_required = 0;
1334 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001335 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001336
1337#if defined(MBEDTLS_CMAC_C)
1338 if( alg == PSA_ALG_CMAC )
1339 {
1340 operation->iv_required = 0;
1341 mbedtls_cipher_init( &operation->ctx.cmac );
1342 status = PSA_SUCCESS;
1343 }
1344 else
1345#endif /* MBEDTLS_CMAC_C */
1346#if defined(MBEDTLS_MD_C)
1347 if( PSA_ALG_IS_HMAC( operation->alg ) )
1348 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001349 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1350 operation->ctx.hmac.hash_ctx.alg = 0;
1351 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001352 }
1353 else
1354#endif /* MBEDTLS_MD_C */
1355 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001356 if( ! PSA_ALG_IS_MAC( alg ) )
1357 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001358 }
1359
1360 if( status != PSA_SUCCESS )
1361 memset( operation, 0, sizeof( *operation ) );
1362 return( status );
1363}
1364
Gilles Peskine01126fa2018-07-12 17:04:55 +02001365#if defined(MBEDTLS_MD_C)
1366static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1367{
1368 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1369 return( psa_hash_abort( &hmac->hash_ctx ) );
1370}
1371#endif /* MBEDTLS_MD_C */
1372
Gilles Peskine8c9def32018-02-08 10:02:12 +01001373psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1374{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001375 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001376 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001377 /* The object has (apparently) been initialized but it is not
1378 * in use. It's ok to call abort on such an object, and there's
1379 * nothing to do. */
1380 return( PSA_SUCCESS );
1381 }
1382 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001383#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001384 if( operation->alg == PSA_ALG_CMAC )
1385 {
1386 mbedtls_cipher_free( &operation->ctx.cmac );
1387 }
1388 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001389#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001390#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001391 if( PSA_ALG_IS_HMAC( operation->alg ) )
1392 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001393 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001394 }
1395 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001396#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001397 {
1398 /* Sanity check (shouldn't happen: operation->alg should
1399 * always have been initialized to a valid value). */
1400 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001401 }
Moran Peker41deec42018-04-04 15:43:05 +03001402
Gilles Peskine8c9def32018-02-08 10:02:12 +01001403 operation->alg = 0;
1404 operation->key_set = 0;
1405 operation->iv_set = 0;
1406 operation->iv_required = 0;
1407 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001408 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001409
Gilles Peskine8c9def32018-02-08 10:02:12 +01001410 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001411
1412bad_state:
1413 /* If abort is called on an uninitialized object, we can't trust
1414 * anything. Wipe the object in case it contains confidential data.
1415 * This may result in a memory leak if a pointer gets overwritten,
1416 * but it's too late to do anything about this. */
1417 memset( operation, 0, sizeof( *operation ) );
1418 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001419}
1420
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001421#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001422static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001423 size_t key_bits,
1424 key_slot_t *slot,
1425 const mbedtls_cipher_info_t *cipher_info )
1426{
1427 int ret;
1428
1429 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001430
1431 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1432 if( ret != 0 )
1433 return( ret );
1434
1435 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1436 slot->data.raw.data,
1437 key_bits );
1438 return( ret );
1439}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001440#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001441
Gilles Peskine248051a2018-06-20 16:09:38 +02001442#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001443static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1444 const uint8_t *key,
1445 size_t key_length,
1446 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001447{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001448 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001449 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001450 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001451 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001452 psa_status_t status;
1453
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001454 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1455 * overflow below. This should never trigger if the hash algorithm
1456 * is implemented correctly. */
1457 /* The size checks against the ipad and opad buffers cannot be written
1458 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1459 * because that triggers -Wlogical-op on GCC 7.3. */
1460 if( block_size > sizeof( ipad ) )
1461 return( PSA_ERROR_NOT_SUPPORTED );
1462 if( block_size > sizeof( hmac->opad ) )
1463 return( PSA_ERROR_NOT_SUPPORTED );
1464 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001465 return( PSA_ERROR_NOT_SUPPORTED );
1466
Gilles Peskined223b522018-06-11 18:12:58 +02001467 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001468 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001469 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001470 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001471 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001472 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001473 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001474 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001475 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001476 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001477 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001478 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001479 }
Gilles Peskine96889972018-07-12 17:07:03 +02001480 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1481 * but it is permitted. It is common when HMAC is used in HKDF, for
1482 * example. Don't call `memcpy` in the 0-length because `key` could be
1483 * an invalid pointer which would make the behavior undefined. */
1484 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001485 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001486
Gilles Peskined223b522018-06-11 18:12:58 +02001487 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1488 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001489 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001490 ipad[i] ^= 0x36;
1491 memset( ipad + key_length, 0x36, block_size - key_length );
1492
1493 /* Copy the key material from ipad to opad, flipping the requisite bits,
1494 * and filling the rest of opad with the requisite constant. */
1495 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001496 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1497 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001498
Gilles Peskine01126fa2018-07-12 17:04:55 +02001499 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001500 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001501 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001502
Gilles Peskine01126fa2018-07-12 17:04:55 +02001503 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001504
1505cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001506 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001507
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001508 return( status );
1509}
Gilles Peskine248051a2018-06-20 16:09:38 +02001510#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001511
Gilles Peskine89167cb2018-07-08 20:12:23 +02001512static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1513 psa_key_slot_t key,
1514 psa_algorithm_t alg,
1515 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001516{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001517 psa_status_t status;
1518 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001519 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001520 psa_key_usage_t usage =
1521 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001522
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001523 status = psa_mac_init( operation, alg );
1524 if( status != PSA_SUCCESS )
1525 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001526 if( is_sign )
1527 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001528
Gilles Peskine89167cb2018-07-08 20:12:23 +02001529 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001530 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001531 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001532 key_bits = psa_get_key_bits( slot );
1533
Gilles Peskine8c9def32018-02-08 10:02:12 +01001534#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001535 if( alg == PSA_ALG_CMAC )
1536 {
1537 const mbedtls_cipher_info_t *cipher_info =
1538 mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
1539 int ret;
1540 if( cipher_info == NULL )
1541 {
1542 status = PSA_ERROR_NOT_SUPPORTED;
1543 goto exit;
1544 }
1545 operation->mac_size = cipher_info->block_size;
1546 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1547 status = mbedtls_to_psa_error( ret );
1548 }
1549 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001550#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001551#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001552 if( PSA_ALG_IS_HMAC( alg ) )
1553 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001554 psa_algorithm_t hash_alg = PSA_ALG_HMAC_HASH( alg );
1555 if( hash_alg == 0 )
1556 {
1557 status = PSA_ERROR_NOT_SUPPORTED;
1558 goto exit;
1559 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001560
1561 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1562 /* Sanity check. This shouldn't fail on a valid configuration. */
1563 if( operation->mac_size == 0 ||
1564 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1565 {
1566 status = PSA_ERROR_NOT_SUPPORTED;
1567 goto exit;
1568 }
1569
Gilles Peskine01126fa2018-07-12 17:04:55 +02001570 if( slot->type != PSA_KEY_TYPE_HMAC )
1571 {
1572 status = PSA_ERROR_INVALID_ARGUMENT;
1573 goto exit;
1574 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001575
Gilles Peskine01126fa2018-07-12 17:04:55 +02001576 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1577 slot->data.raw.data,
1578 slot->data.raw.bytes,
1579 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001580 }
1581 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001582#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001583 {
1584 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001585 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001586
Gilles Peskinefbfac682018-07-08 20:51:54 +02001587exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001588 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001589 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001590 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001591 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001592 else
1593 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001594 operation->key_set = 1;
1595 }
1596 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001597}
1598
Gilles Peskine89167cb2018-07-08 20:12:23 +02001599psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1600 psa_key_slot_t key,
1601 psa_algorithm_t alg )
1602{
1603 return( psa_mac_setup( operation, key, alg, 1 ) );
1604}
1605
1606psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1607 psa_key_slot_t key,
1608 psa_algorithm_t alg )
1609{
1610 return( psa_mac_setup( operation, key, alg, 0 ) );
1611}
1612
Gilles Peskine8c9def32018-02-08 10:02:12 +01001613psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1614 const uint8_t *input,
1615 size_t input_length )
1616{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001617 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001618 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001619 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001620 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001621 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001622 operation->has_input = 1;
1623
Gilles Peskine8c9def32018-02-08 10:02:12 +01001624#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001625 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001626 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001627 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1628 input, input_length );
1629 status = mbedtls_to_psa_error( ret );
1630 }
1631 else
1632#endif /* MBEDTLS_CMAC_C */
1633#if defined(MBEDTLS_MD_C)
1634 if( PSA_ALG_IS_HMAC( operation->alg ) )
1635 {
1636 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1637 input_length );
1638 }
1639 else
1640#endif /* MBEDTLS_MD_C */
1641 {
1642 /* This shouldn't happen if `operation` was initialized by
1643 * a setup function. */
1644 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001645 }
1646
Gilles Peskinefbfac682018-07-08 20:51:54 +02001647cleanup:
1648 if( status != PSA_SUCCESS )
1649 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001650 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001651}
1652
Gilles Peskine01126fa2018-07-12 17:04:55 +02001653#if defined(MBEDTLS_MD_C)
1654static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1655 uint8_t *mac,
1656 size_t mac_size )
1657{
1658 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1659 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1660 size_t hash_size = 0;
1661 size_t block_size = psa_get_hash_block_size( hash_alg );
1662 psa_status_t status;
1663
Gilles Peskine01126fa2018-07-12 17:04:55 +02001664 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1665 if( status != PSA_SUCCESS )
1666 return( status );
1667 /* From here on, tmp needs to be wiped. */
1668
1669 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1670 if( status != PSA_SUCCESS )
1671 goto exit;
1672
1673 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1674 if( status != PSA_SUCCESS )
1675 goto exit;
1676
1677 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1678 if( status != PSA_SUCCESS )
1679 goto exit;
1680
1681 status = psa_hash_finish( &hmac->hash_ctx, mac, mac_size, &hash_size );
1682
1683exit:
1684 mbedtls_zeroize( tmp, hash_size );
1685 return( status );
1686}
1687#endif /* MBEDTLS_MD_C */
1688
mohammad16036df908f2018-04-02 08:34:15 -07001689static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001690 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001691 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001692{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001693 if( ! operation->key_set )
1694 return( PSA_ERROR_BAD_STATE );
1695 if( operation->iv_required && ! operation->iv_set )
1696 return( PSA_ERROR_BAD_STATE );
1697
Gilles Peskine8c9def32018-02-08 10:02:12 +01001698 if( mac_size < operation->mac_size )
1699 return( PSA_ERROR_BUFFER_TOO_SMALL );
1700
Gilles Peskine8c9def32018-02-08 10:02:12 +01001701#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001702 if( operation->alg == PSA_ALG_CMAC )
1703 {
1704 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1705 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001706 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001707 else
1708#endif /* MBEDTLS_CMAC_C */
1709#if defined(MBEDTLS_MD_C)
1710 if( PSA_ALG_IS_HMAC( operation->alg ) )
1711 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001712 return( psa_hmac_finish_internal( &operation->ctx.hmac,
1713 mac, mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001714 }
1715 else
1716#endif /* MBEDTLS_MD_C */
1717 {
1718 /* This shouldn't happen if `operation` was initialized by
1719 * a setup function. */
1720 return( PSA_ERROR_BAD_STATE );
1721 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001722}
1723
Gilles Peskineacd4be32018-07-08 19:56:25 +02001724psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1725 uint8_t *mac,
1726 size_t mac_size,
1727 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001728{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001729 psa_status_t status;
1730
1731 /* Fill the output buffer with something that isn't a valid mac
1732 * (barring an attack on the mac and deliberately-crafted input),
1733 * in case the caller doesn't check the return status properly. */
1734 *mac_length = mac_size;
1735 /* If mac_size is 0 then mac may be NULL and then the
1736 * call to memset would have undefined behavior. */
1737 if( mac_size != 0 )
1738 memset( mac, '!', mac_size );
1739
Gilles Peskine89167cb2018-07-08 20:12:23 +02001740 if( ! operation->is_sign )
1741 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001742 status = PSA_ERROR_BAD_STATE;
1743 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001744 }
mohammad16036df908f2018-04-02 08:34:15 -07001745
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001746 status = psa_mac_finish_internal( operation, mac, mac_size );
1747
1748cleanup:
1749 if( status == PSA_SUCCESS )
1750 {
1751 status = psa_mac_abort( operation );
1752 if( status == PSA_SUCCESS )
1753 *mac_length = operation->mac_size;
1754 else
1755 memset( mac, '!', mac_size );
1756 }
1757 else
1758 psa_mac_abort( operation );
1759 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001760}
1761
Gilles Peskineacd4be32018-07-08 19:56:25 +02001762psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1763 const uint8_t *mac,
1764 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001765{
Gilles Peskine828ed142018-06-18 23:25:51 +02001766 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001767 psa_status_t status;
1768
Gilles Peskine89167cb2018-07-08 20:12:23 +02001769 if( operation->is_sign )
1770 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001771 status = PSA_ERROR_BAD_STATE;
1772 goto cleanup;
1773 }
1774 if( operation->mac_size != mac_length )
1775 {
1776 status = PSA_ERROR_INVALID_SIGNATURE;
1777 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001778 }
mohammad16036df908f2018-04-02 08:34:15 -07001779
1780 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001781 actual_mac, sizeof( actual_mac ) );
1782
1783 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1784 status = PSA_ERROR_INVALID_SIGNATURE;
1785
1786cleanup:
1787 if( status == PSA_SUCCESS )
1788 status = psa_mac_abort( operation );
1789 else
1790 psa_mac_abort( operation );
1791
1792 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001793}
1794
1795
Gilles Peskine20035e32018-02-03 22:44:14 +01001796
Gilles Peskine20035e32018-02-03 22:44:14 +01001797/****************************************************************/
1798/* Asymmetric cryptography */
1799/****************************************************************/
1800
Gilles Peskine2b450e32018-06-27 15:42:46 +02001801#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001802/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001803 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001804static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1805 size_t hash_length,
1806 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001807{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001808 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001809 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001810 *md_alg = mbedtls_md_get_type( md_info );
1811
1812 /* The Mbed TLS RSA module uses an unsigned int for hash length
1813 * parameters. Validate that it fits so that we don't risk an
1814 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001815#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001816 if( hash_length > UINT_MAX )
1817 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001818#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001819
1820#if defined(MBEDTLS_PKCS1_V15)
1821 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
1822 * must be correct. */
1823 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
1824 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001825 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001826 if( md_info == NULL )
1827 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001828 if( mbedtls_md_get_size( md_info ) != hash_length )
1829 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001830 }
1831#endif /* MBEDTLS_PKCS1_V15 */
1832
1833#if defined(MBEDTLS_PKCS1_V21)
1834 /* PSS requires a hash internally. */
1835 if( PSA_ALG_IS_RSA_PSS( alg ) )
1836 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001837 if( md_info == NULL )
1838 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001839 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001840#endif /* MBEDTLS_PKCS1_V21 */
1841
Gilles Peskine61b91d42018-06-08 16:09:36 +02001842 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001843}
1844
Gilles Peskine2b450e32018-06-27 15:42:46 +02001845static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1846 psa_algorithm_t alg,
1847 const uint8_t *hash,
1848 size_t hash_length,
1849 uint8_t *signature,
1850 size_t signature_size,
1851 size_t *signature_length )
1852{
1853 psa_status_t status;
1854 int ret;
1855 mbedtls_md_type_t md_alg;
1856
1857 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1858 if( status != PSA_SUCCESS )
1859 return( status );
1860
Gilles Peskine630a18a2018-06-29 17:49:35 +02001861 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001862 return( PSA_ERROR_BUFFER_TOO_SMALL );
1863
1864#if defined(MBEDTLS_PKCS1_V15)
1865 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1866 {
1867 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1868 MBEDTLS_MD_NONE );
1869 ret = mbedtls_rsa_pkcs1_sign( rsa,
1870 mbedtls_ctr_drbg_random,
1871 &global_data.ctr_drbg,
1872 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001873 md_alg,
1874 (unsigned int) hash_length,
1875 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001876 signature );
1877 }
1878 else
1879#endif /* MBEDTLS_PKCS1_V15 */
1880#if defined(MBEDTLS_PKCS1_V21)
1881 if( PSA_ALG_IS_RSA_PSS( alg ) )
1882 {
1883 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1884 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1885 mbedtls_ctr_drbg_random,
1886 &global_data.ctr_drbg,
1887 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001888 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001889 (unsigned int) hash_length,
1890 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001891 signature );
1892 }
1893 else
1894#endif /* MBEDTLS_PKCS1_V21 */
1895 {
1896 return( PSA_ERROR_INVALID_ARGUMENT );
1897 }
1898
1899 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02001900 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02001901 return( mbedtls_to_psa_error( ret ) );
1902}
1903
1904static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1905 psa_algorithm_t alg,
1906 const uint8_t *hash,
1907 size_t hash_length,
1908 const uint8_t *signature,
1909 size_t signature_length )
1910{
1911 psa_status_t status;
1912 int ret;
1913 mbedtls_md_type_t md_alg;
1914
1915 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1916 if( status != PSA_SUCCESS )
1917 return( status );
1918
Gilles Peskine630a18a2018-06-29 17:49:35 +02001919 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001920 return( PSA_ERROR_BUFFER_TOO_SMALL );
1921
1922#if defined(MBEDTLS_PKCS1_V15)
1923 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1924 {
1925 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1926 MBEDTLS_MD_NONE );
1927 ret = mbedtls_rsa_pkcs1_verify( rsa,
1928 mbedtls_ctr_drbg_random,
1929 &global_data.ctr_drbg,
1930 MBEDTLS_RSA_PUBLIC,
1931 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001932 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001933 hash,
1934 signature );
1935 }
1936 else
1937#endif /* MBEDTLS_PKCS1_V15 */
1938#if defined(MBEDTLS_PKCS1_V21)
1939 if( PSA_ALG_IS_RSA_PSS( alg ) )
1940 {
1941 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1942 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1943 mbedtls_ctr_drbg_random,
1944 &global_data.ctr_drbg,
1945 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001946 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001947 (unsigned int) hash_length,
1948 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001949 signature );
1950 }
1951 else
1952#endif /* MBEDTLS_PKCS1_V21 */
1953 {
1954 return( PSA_ERROR_INVALID_ARGUMENT );
1955 }
1956 return( mbedtls_to_psa_error( ret ) );
1957}
1958#endif /* MBEDTLS_RSA_C */
1959
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001960#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001961/* `ecp` cannot be const because `ecp->grp` needs to be non-const
1962 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
1963 * (even though these functions don't modify it). */
1964static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
1965 psa_algorithm_t alg,
1966 const uint8_t *hash,
1967 size_t hash_length,
1968 uint8_t *signature,
1969 size_t signature_size,
1970 size_t *signature_length )
1971{
1972 int ret;
1973 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001974 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001975 mbedtls_mpi_init( &r );
1976 mbedtls_mpi_init( &s );
1977
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001978 if( signature_size < 2 * curve_bytes )
1979 {
1980 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
1981 goto cleanup;
1982 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001983
1984 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
1985 {
1986 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
1987 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1988 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
1989 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
1990 hash, hash_length,
1991 md_alg ) );
1992 }
1993 else
1994 {
1995 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
1996 hash, hash_length,
1997 mbedtls_ctr_drbg_random,
1998 &global_data.ctr_drbg ) );
1999 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002000
2001 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2002 signature,
2003 curve_bytes ) );
2004 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2005 signature + curve_bytes,
2006 curve_bytes ) );
2007
2008cleanup:
2009 mbedtls_mpi_free( &r );
2010 mbedtls_mpi_free( &s );
2011 if( ret == 0 )
2012 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002013 return( mbedtls_to_psa_error( ret ) );
2014}
2015
2016static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2017 const uint8_t *hash,
2018 size_t hash_length,
2019 const uint8_t *signature,
2020 size_t signature_length )
2021{
2022 int ret;
2023 mbedtls_mpi r, s;
2024 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2025 mbedtls_mpi_init( &r );
2026 mbedtls_mpi_init( &s );
2027
2028 if( signature_length != 2 * curve_bytes )
2029 return( PSA_ERROR_INVALID_SIGNATURE );
2030
2031 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2032 signature,
2033 curve_bytes ) );
2034 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2035 signature + curve_bytes,
2036 curve_bytes ) );
2037
2038 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2039 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002040
2041cleanup:
2042 mbedtls_mpi_free( &r );
2043 mbedtls_mpi_free( &s );
2044 return( mbedtls_to_psa_error( ret ) );
2045}
2046#endif /* MBEDTLS_ECDSA_C */
2047
Gilles Peskine61b91d42018-06-08 16:09:36 +02002048psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2049 psa_algorithm_t alg,
2050 const uint8_t *hash,
2051 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002052 uint8_t *signature,
2053 size_t signature_size,
2054 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002055{
2056 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002057 psa_status_t status;
2058
2059 *signature_length = signature_size;
2060
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002061 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2062 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002063 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002064 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002065 {
2066 status = PSA_ERROR_INVALID_ARGUMENT;
2067 goto exit;
2068 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002069
Gilles Peskine20035e32018-02-03 22:44:14 +01002070#if defined(MBEDTLS_RSA_C)
2071 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2072 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002073 status = psa_rsa_sign( slot->data.rsa,
2074 alg,
2075 hash, hash_length,
2076 signature, signature_size,
2077 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002078 }
2079 else
2080#endif /* defined(MBEDTLS_RSA_C) */
2081#if defined(MBEDTLS_ECP_C)
2082 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2083 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002084#if defined(MBEDTLS_ECDSA_C)
2085 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002086 status = psa_ecdsa_sign( slot->data.ecp,
2087 alg,
2088 hash, hash_length,
2089 signature, signature_size,
2090 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002091 else
2092#endif /* defined(MBEDTLS_ECDSA_C) */
2093 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002094 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002095 }
itayzafrir5c753392018-05-08 11:18:38 +03002096 }
2097 else
2098#endif /* defined(MBEDTLS_ECP_C) */
2099 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002100 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002101 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002102
2103exit:
2104 /* Fill the unused part of the output buffer (the whole buffer on error,
2105 * the trailing part on success) with something that isn't a valid mac
2106 * (barring an attack on the mac and deliberately-crafted input),
2107 * in case the caller doesn't check the return status properly. */
2108 if( status == PSA_SUCCESS )
2109 memset( signature + *signature_length, '!',
2110 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002111 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002112 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002113 /* If signature_size is 0 then we have nothing to do. We must not call
2114 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002115 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002116}
2117
2118psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2119 psa_algorithm_t alg,
2120 const uint8_t *hash,
2121 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002122 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002123 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002124{
2125 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002126 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002127
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002128 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2129 if( status != PSA_SUCCESS )
2130 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002131
Gilles Peskine61b91d42018-06-08 16:09:36 +02002132#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002133 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002134 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002135 return( psa_rsa_verify( slot->data.rsa,
2136 alg,
2137 hash, hash_length,
2138 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002139 }
2140 else
2141#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002142#if defined(MBEDTLS_ECP_C)
2143 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2144 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002145#if defined(MBEDTLS_ECDSA_C)
2146 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002147 return( psa_ecdsa_verify( slot->data.ecp,
2148 hash, hash_length,
2149 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002150 else
2151#endif /* defined(MBEDTLS_ECDSA_C) */
2152 {
2153 return( PSA_ERROR_INVALID_ARGUMENT );
2154 }
itayzafrir5c753392018-05-08 11:18:38 +03002155 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002156 else
2157#endif /* defined(MBEDTLS_ECP_C) */
2158 {
2159 return( PSA_ERROR_NOT_SUPPORTED );
2160 }
2161}
2162
Gilles Peskine072ac562018-06-30 00:21:29 +02002163#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2164static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2165 mbedtls_rsa_context *rsa )
2166{
2167 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2168 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2169 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2170 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2171}
2172#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2173
Gilles Peskine61b91d42018-06-08 16:09:36 +02002174psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2175 psa_algorithm_t alg,
2176 const uint8_t *input,
2177 size_t input_length,
2178 const uint8_t *salt,
2179 size_t salt_length,
2180 uint8_t *output,
2181 size_t output_size,
2182 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002183{
2184 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002185 psa_status_t status;
2186
Darryl Green5cc689a2018-07-24 15:34:10 +01002187 (void) input;
2188 (void) input_length;
2189 (void) salt;
2190 (void) output;
2191 (void) output_size;
2192
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002193 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002194
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002195 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2196 return( PSA_ERROR_INVALID_ARGUMENT );
2197
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002198 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2199 if( status != PSA_SUCCESS )
2200 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002201 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2202 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002203 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002204
2205#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002206 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002207 {
2208 mbedtls_rsa_context *rsa = slot->data.rsa;
2209 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002210 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002211 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002212#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002213 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002214 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002215 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2216 mbedtls_ctr_drbg_random,
2217 &global_data.ctr_drbg,
2218 MBEDTLS_RSA_PUBLIC,
2219 input_length,
2220 input,
2221 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002222 }
2223 else
2224#endif /* MBEDTLS_PKCS1_V15 */
2225#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002226 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002227 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002228 psa_rsa_oaep_set_padding_mode( alg, rsa );
2229 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2230 mbedtls_ctr_drbg_random,
2231 &global_data.ctr_drbg,
2232 MBEDTLS_RSA_PUBLIC,
2233 salt, salt_length,
2234 input_length,
2235 input,
2236 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002237 }
2238 else
2239#endif /* MBEDTLS_PKCS1_V21 */
2240 {
2241 return( PSA_ERROR_INVALID_ARGUMENT );
2242 }
2243 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002244 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002245 return( mbedtls_to_psa_error( ret ) );
2246 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002247 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002248#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002249 {
2250 return( PSA_ERROR_NOT_SUPPORTED );
2251 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002252}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002253
Gilles Peskine61b91d42018-06-08 16:09:36 +02002254psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2255 psa_algorithm_t alg,
2256 const uint8_t *input,
2257 size_t input_length,
2258 const uint8_t *salt,
2259 size_t salt_length,
2260 uint8_t *output,
2261 size_t output_size,
2262 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002263{
2264 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002265 psa_status_t status;
2266
Darryl Green5cc689a2018-07-24 15:34:10 +01002267 (void) input;
2268 (void) input_length;
2269 (void) salt;
2270 (void) output;
2271 (void) output_size;
2272
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002273 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002274
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002275 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2276 return( PSA_ERROR_INVALID_ARGUMENT );
2277
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002278 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2279 if( status != PSA_SUCCESS )
2280 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002281 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2282 return( PSA_ERROR_INVALID_ARGUMENT );
2283
2284#if defined(MBEDTLS_RSA_C)
2285 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2286 {
2287 mbedtls_rsa_context *rsa = slot->data.rsa;
2288 int ret;
2289
Gilles Peskine630a18a2018-06-29 17:49:35 +02002290 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002291 return( PSA_ERROR_INVALID_ARGUMENT );
2292
2293#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002294 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002295 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002296 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2297 mbedtls_ctr_drbg_random,
2298 &global_data.ctr_drbg,
2299 MBEDTLS_RSA_PRIVATE,
2300 output_length,
2301 input,
2302 output,
2303 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002304 }
2305 else
2306#endif /* MBEDTLS_PKCS1_V15 */
2307#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002308 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002309 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002310 psa_rsa_oaep_set_padding_mode( alg, rsa );
2311 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2312 mbedtls_ctr_drbg_random,
2313 &global_data.ctr_drbg,
2314 MBEDTLS_RSA_PRIVATE,
2315 salt, salt_length,
2316 output_length,
2317 input,
2318 output,
2319 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002320 }
2321 else
2322#endif /* MBEDTLS_PKCS1_V21 */
2323 {
2324 return( PSA_ERROR_INVALID_ARGUMENT );
2325 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002326
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002327 return( mbedtls_to_psa_error( ret ) );
2328 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002329 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002330#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002331 {
2332 return( PSA_ERROR_NOT_SUPPORTED );
2333 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002334}
Gilles Peskine20035e32018-02-03 22:44:14 +01002335
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002336
2337
mohammad1603503973b2018-03-12 15:59:30 +02002338/****************************************************************/
2339/* Symmetric cryptography */
2340/****************************************************************/
2341
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002342/* Initialize the cipher operation structure. Once this function has been
2343 * called, psa_cipher_abort can run and will do the right thing. */
2344static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2345 psa_algorithm_t alg )
2346{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002347 if( ! PSA_ALG_IS_CIPHER( alg ) )
2348 {
2349 memset( operation, 0, sizeof( *operation ) );
2350 return( PSA_ERROR_INVALID_ARGUMENT );
2351 }
2352
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002353 operation->alg = alg;
2354 operation->key_set = 0;
2355 operation->iv_set = 0;
2356 operation->iv_required = 1;
2357 operation->iv_size = 0;
2358 operation->block_size = 0;
2359 mbedtls_cipher_init( &operation->ctx.cipher );
2360 return( PSA_SUCCESS );
2361}
2362
Gilles Peskinee553c652018-06-04 16:22:46 +02002363static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2364 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002365 psa_algorithm_t alg,
2366 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002367{
2368 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2369 psa_status_t status;
2370 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002371 size_t key_bits;
2372 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002373 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2374 PSA_KEY_USAGE_ENCRYPT :
2375 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002376
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002377 status = psa_cipher_init( operation, alg );
2378 if( status != PSA_SUCCESS )
2379 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002380
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002381 status = psa_get_key_from_slot( key, &slot, usage, alg);
2382 if( status != PSA_SUCCESS )
2383 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002384 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002385
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002386 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002387 if( cipher_info == NULL )
2388 return( PSA_ERROR_NOT_SUPPORTED );
2389
mohammad1603503973b2018-03-12 15:59:30 +02002390 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002391 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002392 {
2393 psa_cipher_abort( operation );
2394 return( mbedtls_to_psa_error( ret ) );
2395 }
2396
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002397#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002398 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002399 {
2400 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2401 unsigned char keys[24];
2402 memcpy( keys, slot->data.raw.data, 16 );
2403 memcpy( keys + 16, slot->data.raw.data, 8 );
2404 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2405 keys,
2406 192, cipher_operation );
2407 }
2408 else
2409#endif
2410 {
2411 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2412 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002413 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002414 }
Moran Peker41deec42018-04-04 15:43:05 +03002415 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002416 {
2417 psa_cipher_abort( operation );
2418 return( mbedtls_to_psa_error( ret ) );
2419 }
2420
mohammad16038481e742018-03-18 13:57:31 +02002421#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002422 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002423 {
Gilles Peskine53514202018-06-06 15:11:46 +02002424 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2425 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002426
Moran Pekera28258c2018-05-29 16:25:04 +03002427 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002428 {
2429 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2430 mode = MBEDTLS_PADDING_PKCS7;
2431 break;
2432 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2433 mode = MBEDTLS_PADDING_NONE;
2434 break;
2435 default:
Moran Pekerae382792018-05-31 14:06:17 +03002436 psa_cipher_abort( operation );
2437 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002438 }
2439 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002440 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002441 {
2442 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002443 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002444 }
mohammad16038481e742018-03-18 13:57:31 +02002445 }
2446#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2447
mohammad1603503973b2018-03-12 15:59:30 +02002448 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002449 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002450 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) :
Gilles Peskine7e928852018-06-04 16:23:10 +02002451 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002452 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002453 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002454 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002455 }
mohammad1603503973b2018-03-12 15:59:30 +02002456
Moran Peker395db872018-05-31 14:07:14 +03002457 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002458}
2459
Gilles Peskinefe119512018-07-08 21:39:34 +02002460psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2461 psa_key_slot_t key,
2462 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002463{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002464 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002465}
2466
Gilles Peskinefe119512018-07-08 21:39:34 +02002467psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
2468 psa_key_slot_t key,
2469 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002470{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002471 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002472}
2473
Gilles Peskinefe119512018-07-08 21:39:34 +02002474psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2475 unsigned char *iv,
2476 size_t iv_size,
2477 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002478{
itayzafrir534bd7c2018-08-02 13:56:32 +03002479 psa_status_t status;
2480 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002481 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002482 {
2483 status = PSA_ERROR_BAD_STATE;
2484 goto exit;
2485 }
Moran Peker41deec42018-04-04 15:43:05 +03002486 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002487 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002488 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002489 goto exit;
2490 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002491 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2492 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002493 if( ret != 0 )
2494 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002495 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002496 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002497 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002498
mohammad16038481e742018-03-18 13:57:31 +02002499 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002500 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002501
Moran Peker395db872018-05-31 14:07:14 +03002502exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002503 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002504 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002505 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002506}
2507
Gilles Peskinefe119512018-07-08 21:39:34 +02002508psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2509 const unsigned char *iv,
2510 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002511{
itayzafrir534bd7c2018-08-02 13:56:32 +03002512 psa_status_t status;
2513 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002514 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002515 {
2516 status = PSA_ERROR_BAD_STATE;
2517 goto exit;
2518 }
Moran Pekera28258c2018-05-29 16:25:04 +03002519 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002520 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002521 status = PSA_ERROR_INVALID_ARGUMENT;
2522 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002523 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002524 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2525 status = mbedtls_to_psa_error( ret );
2526exit:
2527 if( status == PSA_SUCCESS )
2528 operation->iv_set = 1;
2529 else
mohammad1603503973b2018-03-12 15:59:30 +02002530 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002531 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002532}
2533
Gilles Peskinee553c652018-06-04 16:22:46 +02002534psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2535 const uint8_t *input,
2536 size_t input_length,
2537 unsigned char *output,
2538 size_t output_size,
2539 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002540{
itayzafrir534bd7c2018-08-02 13:56:32 +03002541 psa_status_t status;
2542 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002543 size_t expected_output_size;
2544 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2545 {
2546 /* Take the unprocessed partial block left over from previous
2547 * update calls, if any, plus the input to this call. Remove
2548 * the last partial block, if any. You get the data that will be
2549 * output in this call. */
2550 expected_output_size =
2551 ( operation->ctx.cipher.unprocessed_len + input_length )
2552 / operation->block_size * operation->block_size;
2553 }
2554 else
2555 {
2556 expected_output_size = input_length;
2557 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002558
Gilles Peskine89d789c2018-06-04 17:17:16 +02002559 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002560 {
2561 status = PSA_ERROR_BUFFER_TOO_SMALL;
2562 goto exit;
2563 }
mohammad160382759612018-03-12 18:16:40 +02002564
mohammad1603503973b2018-03-12 15:59:30 +02002565 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002566 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002567 status = mbedtls_to_psa_error( ret );
2568exit:
2569 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002570 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002571 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002572}
2573
Gilles Peskinee553c652018-06-04 16:22:46 +02002574psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2575 uint8_t *output,
2576 size_t output_size,
2577 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002578{
Janos Follath315b51c2018-07-09 16:04:51 +01002579 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2580 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002581 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002582
mohammad1603503973b2018-03-12 15:59:30 +02002583 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002584 {
Janos Follath315b51c2018-07-09 16:04:51 +01002585 status = PSA_ERROR_BAD_STATE;
2586 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002587 }
2588 if( operation->iv_required && ! operation->iv_set )
2589 {
Janos Follath315b51c2018-07-09 16:04:51 +01002590 status = PSA_ERROR_BAD_STATE;
2591 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002592 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002593 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2594 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002595 {
Gilles Peskine53514202018-06-06 15:11:46 +02002596 psa_algorithm_t padding_mode =
2597 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002598 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002599 {
Janos Follath315b51c2018-07-09 16:04:51 +01002600 status = PSA_ERROR_TAMPERING_DETECTED;
2601 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002602 }
Gilles Peskine53514202018-06-06 15:11:46 +02002603 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002604 {
2605 if( operation->ctx.cipher.unprocessed_len != 0 )
2606 {
Janos Follath315b51c2018-07-09 16:04:51 +01002607 status = PSA_ERROR_INVALID_ARGUMENT;
2608 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002609 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002610 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002611 }
2612
Janos Follath315b51c2018-07-09 16:04:51 +01002613 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2614 temp_output_buffer,
2615 output_length );
2616 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002617 {
Janos Follath315b51c2018-07-09 16:04:51 +01002618 status = mbedtls_to_psa_error( cipher_ret );
2619 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002620 }
Janos Follath315b51c2018-07-09 16:04:51 +01002621
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002622 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002623 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002624 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002625 memcpy( output, temp_output_buffer, *output_length );
2626 else
2627 {
Janos Follath315b51c2018-07-09 16:04:51 +01002628 status = PSA_ERROR_BUFFER_TOO_SMALL;
2629 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002630 }
mohammad1603503973b2018-03-12 15:59:30 +02002631
Janos Follath279ab8e2018-07-09 16:13:21 +01002632 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002633 status = psa_cipher_abort( operation );
2634
2635 return( status );
2636
2637error:
2638
2639 *output_length = 0;
2640
Janos Follath279ab8e2018-07-09 16:13:21 +01002641 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002642 (void) psa_cipher_abort( operation );
2643
2644 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002645}
2646
Gilles Peskinee553c652018-06-04 16:22:46 +02002647psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2648{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002649 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002650 {
2651 /* The object has (apparently) been initialized but it is not
2652 * in use. It's ok to call abort on such an object, and there's
2653 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002654 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002655 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002656
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002657 /* Sanity check (shouldn't happen: operation->alg should
2658 * always have been initialized to a valid value). */
2659 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2660 return( PSA_ERROR_BAD_STATE );
2661
mohammad1603503973b2018-03-12 15:59:30 +02002662 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002663
Moran Peker41deec42018-04-04 15:43:05 +03002664 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002665 operation->key_set = 0;
2666 operation->iv_set = 0;
2667 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002668 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002669 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002670
Moran Peker395db872018-05-31 14:07:14 +03002671 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002672}
2673
Gilles Peskinea0655c32018-04-30 17:06:50 +02002674
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002675
mohammad16038cc1cee2018-03-28 01:21:33 +03002676/****************************************************************/
2677/* Key Policy */
2678/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002679
mohammad160327010052018-07-03 13:16:15 +03002680#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002681void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002682{
Gilles Peskine803ce742018-06-18 16:07:14 +02002683 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002684}
2685
Gilles Peskine2d277862018-06-18 15:41:12 +02002686void psa_key_policy_set_usage( psa_key_policy_t *policy,
2687 psa_key_usage_t usage,
2688 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002689{
mohammad16034eed7572018-03-28 05:14:59 -07002690 policy->usage = usage;
2691 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002692}
2693
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002694psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002695{
mohammad16036df908f2018-04-02 08:34:15 -07002696 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002697}
2698
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002699psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002700{
mohammad16036df908f2018-04-02 08:34:15 -07002701 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002702}
mohammad160327010052018-07-03 13:16:15 +03002703#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002704
Gilles Peskine2d277862018-06-18 15:41:12 +02002705psa_status_t psa_set_key_policy( psa_key_slot_t key,
2706 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002707{
2708 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002709 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002710
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002711 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002712 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002713
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002714 status = psa_get_empty_key_slot( key, &slot );
2715 if( status != PSA_SUCCESS )
2716 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002717
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002718 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2719 PSA_KEY_USAGE_ENCRYPT |
2720 PSA_KEY_USAGE_DECRYPT |
2721 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002722 PSA_KEY_USAGE_VERIFY |
2723 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002724 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002725
mohammad16036df908f2018-04-02 08:34:15 -07002726 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002727
2728 return( PSA_SUCCESS );
2729}
2730
Gilles Peskine2d277862018-06-18 15:41:12 +02002731psa_status_t psa_get_key_policy( psa_key_slot_t key,
2732 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002733{
2734 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002735 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002736
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002737 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002738 return( PSA_ERROR_INVALID_ARGUMENT );
2739
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002740 status = psa_get_key_slot( key, &slot );
2741 if( status != PSA_SUCCESS )
2742 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002743
mohammad16036df908f2018-04-02 08:34:15 -07002744 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002745
2746 return( PSA_SUCCESS );
2747}
Gilles Peskine20035e32018-02-03 22:44:14 +01002748
Gilles Peskinea0655c32018-04-30 17:06:50 +02002749
2750
mohammad1603804cd712018-03-20 22:44:08 +02002751/****************************************************************/
2752/* Key Lifetime */
2753/****************************************************************/
2754
Gilles Peskine2d277862018-06-18 15:41:12 +02002755psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2756 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002757{
2758 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002759 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002760
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002761 status = psa_get_key_slot( key, &slot );
2762 if( status != PSA_SUCCESS )
2763 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002764
mohammad1603804cd712018-03-20 22:44:08 +02002765 *lifetime = slot->lifetime;
2766
2767 return( PSA_SUCCESS );
2768}
2769
Gilles Peskine2d277862018-06-18 15:41:12 +02002770psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002771 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002772{
2773 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002774 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002775
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002776 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2777 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002778 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2779 return( PSA_ERROR_INVALID_ARGUMENT );
2780
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002781 status = psa_get_empty_key_slot( key, &slot );
2782 if( status != PSA_SUCCESS )
2783 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002784
Moran Pekerd7326592018-05-29 16:56:39 +03002785 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002786 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002787
mohammad1603060ad8a2018-03-20 14:28:38 -07002788 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002789
2790 return( PSA_SUCCESS );
2791}
2792
Gilles Peskine20035e32018-02-03 22:44:14 +01002793
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002794
mohammad16035955c982018-04-26 00:53:03 +03002795/****************************************************************/
2796/* AEAD */
2797/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002798
mohammad16035955c982018-04-26 00:53:03 +03002799psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2800 psa_algorithm_t alg,
2801 const uint8_t *nonce,
2802 size_t nonce_length,
2803 const uint8_t *additional_data,
2804 size_t additional_data_length,
2805 const uint8_t *plaintext,
2806 size_t plaintext_length,
2807 uint8_t *ciphertext,
2808 size_t ciphertext_size,
2809 size_t *ciphertext_length )
2810{
2811 int ret;
2812 psa_status_t status;
2813 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002814 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002815 uint8_t *tag;
2816 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002817 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002818 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002819
mohammad1603f08a5502018-06-03 15:05:47 +03002820 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002821
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002822 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2823 if( status != PSA_SUCCESS )
2824 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002825 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002826
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002827 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002828 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002829 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002830 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002831
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002832 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002833 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002834 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002835
mohammad16035955c982018-04-26 00:53:03 +03002836 if( alg == PSA_ALG_GCM )
2837 {
2838 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002839 tag_length = 16;
2840
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002841 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002842 return( PSA_ERROR_INVALID_ARGUMENT );
2843
mohammad160315223a82018-06-03 17:19:55 +03002844 //make sure we have place to hold the tag in the ciphertext buffer
2845 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002846 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002847
2848 //update the tag pointer to point to the end of the ciphertext_length
2849 tag = ciphertext + plaintext_length;
2850
mohammad16035955c982018-04-26 00:53:03 +03002851 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002852 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002853 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002854 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002855 if( ret != 0 )
2856 {
2857 mbedtls_gcm_free( &gcm );
2858 return( mbedtls_to_psa_error( ret ) );
2859 }
2860 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002861 plaintext_length, nonce,
2862 nonce_length, additional_data,
2863 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002864 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002865 mbedtls_gcm_free( &gcm );
2866 }
2867 else if( alg == PSA_ALG_CCM )
2868 {
2869 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002870 tag_length = 16;
2871
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002872 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002873 return( PSA_ERROR_INVALID_ARGUMENT );
2874
mohammad160347ddf3d2018-04-26 01:11:21 +03002875 if( nonce_length < 7 || nonce_length > 13 )
2876 return( PSA_ERROR_INVALID_ARGUMENT );
2877
mohammad160315223a82018-06-03 17:19:55 +03002878 //make sure we have place to hold the tag in the ciphertext buffer
2879 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002880 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002881
2882 //update the tag pointer to point to the end of the ciphertext_length
2883 tag = ciphertext + plaintext_length;
2884
mohammad16035955c982018-04-26 00:53:03 +03002885 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002886 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002887 slot->data.raw.data,
2888 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002889 if( ret != 0 )
2890 {
2891 mbedtls_ccm_free( &ccm );
2892 return( mbedtls_to_psa_error( ret ) );
2893 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002894 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002895 nonce, nonce_length,
2896 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002897 additional_data_length,
2898 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002899 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002900 mbedtls_ccm_free( &ccm );
2901 }
mohammad16035c8845f2018-05-09 05:40:09 -07002902 else
2903 {
mohammad1603554faad2018-06-03 15:07:38 +03002904 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002905 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002906
mohammad160315223a82018-06-03 17:19:55 +03002907 if( ret != 0 )
2908 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002909 /* If ciphertext_size is 0 then ciphertext may be NULL and then the
2910 * call to memset would have undefined behavior. */
2911 if( ciphertext_size != 0 )
2912 memset( ciphertext, 0, ciphertext_size );
mohammad160315223a82018-06-03 17:19:55 +03002913 return( mbedtls_to_psa_error( ret ) );
2914 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002915
mohammad160315223a82018-06-03 17:19:55 +03002916 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002917 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002918}
2919
Gilles Peskineee652a32018-06-01 19:23:52 +02002920/* Locate the tag in a ciphertext buffer containing the encrypted data
2921 * followed by the tag. Return the length of the part preceding the tag in
2922 * *plaintext_length. This is the size of the plaintext in modes where
2923 * the encrypted data has the same size as the plaintext, such as
2924 * CCM and GCM. */
2925static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2926 const uint8_t *ciphertext,
2927 size_t ciphertext_length,
2928 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002929 const uint8_t **p_tag )
2930{
2931 size_t payload_length;
2932 if( tag_length > ciphertext_length )
2933 return( PSA_ERROR_INVALID_ARGUMENT );
2934 payload_length = ciphertext_length - tag_length;
2935 if( payload_length > plaintext_size )
2936 return( PSA_ERROR_BUFFER_TOO_SMALL );
2937 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002938 return( PSA_SUCCESS );
2939}
2940
mohammad16035955c982018-04-26 00:53:03 +03002941psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2942 psa_algorithm_t alg,
2943 const uint8_t *nonce,
2944 size_t nonce_length,
2945 const uint8_t *additional_data,
2946 size_t additional_data_length,
2947 const uint8_t *ciphertext,
2948 size_t ciphertext_length,
2949 uint8_t *plaintext,
2950 size_t plaintext_size,
2951 size_t *plaintext_length )
2952{
2953 int ret;
2954 psa_status_t status;
2955 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002956 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002957 const uint8_t *tag;
2958 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002959 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002960 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002961
Gilles Peskineee652a32018-06-01 19:23:52 +02002962 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002963
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002964 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2965 if( status != PSA_SUCCESS )
2966 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002967 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002968
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002969 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002970 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002971 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002972 return( PSA_ERROR_NOT_SUPPORTED );
2973
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002974 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002975 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002976 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002977
mohammad16035955c982018-04-26 00:53:03 +03002978 if( alg == PSA_ALG_GCM )
2979 {
2980 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002981
Gilles Peskineee652a32018-06-01 19:23:52 +02002982 tag_length = 16;
2983 status = psa_aead_unpadded_locate_tag( tag_length,
2984 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002985 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002986 if( status != PSA_SUCCESS )
2987 return( status );
2988
mohammad16035955c982018-04-26 00:53:03 +03002989 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002990 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002991 slot->data.raw.data,
2992 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002993 if( ret != 0 )
2994 {
2995 mbedtls_gcm_free( &gcm );
2996 return( mbedtls_to_psa_error( ret ) );
2997 }
mohammad16035955c982018-04-26 00:53:03 +03002998
Gilles Peskineee652a32018-06-01 19:23:52 +02002999 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03003000 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003001 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03003002 additional_data,
3003 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003004 tag, tag_length,
3005 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03003006 mbedtls_gcm_free( &gcm );
3007 }
3008 else if( alg == PSA_ALG_CCM )
3009 {
3010 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02003011
mohammad160347ddf3d2018-04-26 01:11:21 +03003012 if( nonce_length < 7 || nonce_length > 13 )
3013 return( PSA_ERROR_INVALID_ARGUMENT );
3014
mohammad16039375f842018-06-03 14:28:24 +03003015 tag_length = 16;
3016 status = psa_aead_unpadded_locate_tag( tag_length,
3017 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003018 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003019 if( status != PSA_SUCCESS )
3020 return( status );
3021
mohammad16035955c982018-04-26 00:53:03 +03003022 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02003023 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003024 slot->data.raw.data,
3025 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03003026 if( ret != 0 )
3027 {
3028 mbedtls_ccm_free( &ccm );
3029 return( mbedtls_to_psa_error( ret ) );
3030 }
mohammad160360a64d02018-06-03 17:20:42 +03003031 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003032 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003033 additional_data,
3034 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003035 ciphertext, plaintext,
3036 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03003037 mbedtls_ccm_free( &ccm );
3038 }
mohammad160339574652018-06-01 04:39:53 -07003039 else
3040 {
mohammad1603554faad2018-06-03 15:07:38 +03003041 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003042 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003043
Gilles Peskineee652a32018-06-01 19:23:52 +02003044 if( ret != 0 )
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003045 {
3046 /* If plaintext_size is 0 then plaintext may be NULL and then the
3047 * call to memset has undefined behavior. */
3048 if( plaintext_size != 0 )
3049 memset( plaintext, 0, plaintext_size );
3050 }
mohammad160360a64d02018-06-03 17:20:42 +03003051 else
3052 *plaintext_length = ciphertext_length - tag_length;
3053
Gilles Peskineee652a32018-06-01 19:23:52 +02003054 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03003055}
3056
Gilles Peskinea0655c32018-04-30 17:06:50 +02003057
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003058
Gilles Peskine20035e32018-02-03 22:44:14 +01003059/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003060/* Generators */
3061/****************************************************************/
3062
3063psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3064{
3065 psa_status_t status = PSA_SUCCESS;
3066 if( generator->alg == 0 )
3067 {
3068 /* The object has (apparently) been initialized but it is not
3069 * in use. It's ok to call abort on such an object, and there's
3070 * nothing to do. */
3071 }
3072 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003073#if defined(MBEDTLS_MD_C)
3074 if( PSA_ALG_IS_HKDF( generator->alg ) )
3075 {
3076 mbedtls_free( generator->ctx.hkdf.info );
3077 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3078 }
3079 else
3080#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003081 {
3082 status = PSA_ERROR_BAD_STATE;
3083 }
3084 memset( generator, 0, sizeof( *generator ) );
3085 return( status );
3086}
3087
3088
3089psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3090 size_t *capacity)
3091{
3092 *capacity = generator->capacity;
3093 return( PSA_SUCCESS );
3094}
3095
Gilles Peskinebef7f142018-07-12 17:22:21 +02003096#if defined(MBEDTLS_MD_C)
3097/* Read some bytes from an HKDF-based generator. This performs a chunk
3098 * of the expand phase of the HKDF algorithm. */
3099static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3100 psa_algorithm_t hash_alg,
3101 uint8_t *output,
3102 size_t output_length )
3103{
3104 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3105 psa_status_t status;
3106
3107 while( output_length != 0 )
3108 {
3109 /* Copy what remains of the current block */
3110 uint8_t n = hash_length - hkdf->offset_in_block;
3111 if( n > output_length )
3112 n = (uint8_t) output_length;
3113 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3114 output += n;
3115 output_length -= n;
3116 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003117 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003118 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003119 /* We can't be wanting more output after block 0xff, otherwise
3120 * the capacity check in psa_generator_read() would have
3121 * prevented this call. It could happen only if the generator
3122 * object was corrupted or if this function is called directly
3123 * inside the library. */
3124 if( hkdf->block_number == 0xff )
3125 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003126
3127 /* We need a new block */
3128 ++hkdf->block_number;
3129 hkdf->offset_in_block = 0;
3130 status = psa_hmac_setup_internal( &hkdf->hmac,
3131 hkdf->prk, hash_length,
3132 hash_alg );
3133 if( status != PSA_SUCCESS )
3134 return( status );
3135 if( hkdf->block_number != 1 )
3136 {
3137 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3138 hkdf->output_block,
3139 hash_length );
3140 if( status != PSA_SUCCESS )
3141 return( status );
3142 }
3143 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3144 hkdf->info,
3145 hkdf->info_length );
3146 if( status != PSA_SUCCESS )
3147 return( status );
3148 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3149 &hkdf->block_number, 1 );
3150 if( status != PSA_SUCCESS )
3151 return( status );
3152 status = psa_hmac_finish_internal( &hkdf->hmac,
3153 hkdf->output_block,
3154 sizeof( hkdf->output_block ) );
3155 if( status != PSA_SUCCESS )
3156 return( status );
3157 }
3158
3159 return( PSA_SUCCESS );
3160}
3161#endif /* MBEDTLS_MD_C */
3162
Gilles Peskineeab56e42018-07-12 17:12:33 +02003163psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3164 uint8_t *output,
3165 size_t output_length )
3166{
3167 psa_status_t status;
3168
3169 if( output_length > generator->capacity )
3170 {
3171 generator->capacity = 0;
3172 /* Go through the error path to wipe all confidential data now
3173 * that the generator object is useless. */
3174 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3175 goto exit;
3176 }
3177 if( output_length == 0 &&
3178 generator->capacity == 0 && generator->alg == 0 )
3179 {
3180 /* Edge case: this is a blank or finished generator, and 0
3181 * bytes were requested. The right error in this case could
3182 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3183 * INSUFFICIENT_CAPACITY, which is right for a finished
3184 * generator, for consistency with the case when
3185 * output_length > 0. */
3186 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3187 }
3188 generator->capacity -= output_length;
3189
Gilles Peskinebef7f142018-07-12 17:22:21 +02003190#if defined(MBEDTLS_MD_C)
3191 if( PSA_ALG_IS_HKDF( generator->alg ) )
3192 {
3193 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3194 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3195 output, output_length );
3196 }
3197 else
3198#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003199 {
3200 return( PSA_ERROR_BAD_STATE );
3201 }
3202
3203exit:
3204 if( status != PSA_SUCCESS )
3205 {
3206 psa_generator_abort( generator );
3207 memset( output, '!', output_length );
3208 }
3209 return( status );
3210}
3211
Gilles Peskine08542d82018-07-19 17:05:42 +02003212#if defined(MBEDTLS_DES_C)
3213static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3214{
3215 if( data_size >= 8 )
3216 mbedtls_des_key_set_parity( data );
3217 if( data_size >= 16 )
3218 mbedtls_des_key_set_parity( data + 8 );
3219 if( data_size >= 24 )
3220 mbedtls_des_key_set_parity( data + 16 );
3221}
3222#endif /* MBEDTLS_DES_C */
3223
Gilles Peskineeab56e42018-07-12 17:12:33 +02003224psa_status_t psa_generator_import_key( psa_key_slot_t key,
3225 psa_key_type_t type,
3226 size_t bits,
3227 psa_crypto_generator_t *generator )
3228{
3229 uint8_t *data = NULL;
3230 size_t bytes = PSA_BITS_TO_BYTES( bits );
3231 psa_status_t status;
3232
3233 if( ! key_type_is_raw_bytes( type ) )
3234 return( PSA_ERROR_INVALID_ARGUMENT );
3235 if( bits % 8 != 0 )
3236 return( PSA_ERROR_INVALID_ARGUMENT );
3237 data = mbedtls_calloc( 1, bytes );
3238 if( data == NULL )
3239 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3240
3241 status = psa_generator_read( generator, data, bytes );
3242 if( status != PSA_SUCCESS )
3243 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003244#if defined(MBEDTLS_DES_C)
3245 if( type == PSA_KEY_TYPE_DES )
3246 psa_des_set_key_parity( data, bytes );
3247#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003248 status = psa_import_key( key, type, data, bytes );
3249
3250exit:
3251 mbedtls_free( data );
3252 return( status );
3253}
3254
3255
3256
3257/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003258/* Key derivation */
3259/****************************************************************/
3260
Gilles Peskinebef7f142018-07-12 17:22:21 +02003261/* Set up an HKDF-based generator. This is exactly the extract phase
3262 * of the HKDF algorithm. */
3263static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
3264 key_slot_t *slot,
3265 psa_algorithm_t hash_alg,
3266 const uint8_t *salt,
3267 size_t salt_length,
3268 const uint8_t *label,
3269 size_t label_length )
3270{
3271 psa_status_t status;
3272 status = psa_hmac_setup_internal( &hkdf->hmac,
3273 salt, salt_length,
3274 PSA_ALG_HMAC_HASH( hash_alg ) );
3275 if( status != PSA_SUCCESS )
3276 return( status );
3277 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3278 slot->data.raw.data,
3279 slot->data.raw.bytes );
3280 if( status != PSA_SUCCESS )
3281 return( status );
3282 status = psa_hmac_finish_internal( &hkdf->hmac,
3283 hkdf->prk,
3284 sizeof( hkdf->prk ) );
3285 if( status != PSA_SUCCESS )
3286 return( status );
3287 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3288 hkdf->block_number = 0;
3289 hkdf->info_length = label_length;
3290 if( label_length != 0 )
3291 {
3292 hkdf->info = mbedtls_calloc( 1, label_length );
3293 if( hkdf->info == NULL )
3294 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3295 memcpy( hkdf->info, label, label_length );
3296 }
3297 return( PSA_SUCCESS );
3298}
3299
Gilles Peskineea0fb492018-07-12 17:17:20 +02003300psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01003301 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003302 psa_algorithm_t alg,
3303 const uint8_t *salt,
3304 size_t salt_length,
3305 const uint8_t *label,
3306 size_t label_length,
3307 size_t capacity )
3308{
3309 key_slot_t *slot;
3310 psa_status_t status;
3311
3312 if( generator->alg != 0 )
3313 return( PSA_ERROR_BAD_STATE );
3314
3315 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
3316 if( status != PSA_SUCCESS )
3317 return( status );
3318 if( slot->type != PSA_KEY_TYPE_DERIVE )
3319 return( PSA_ERROR_INVALID_ARGUMENT );
3320
3321 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
3322 return( PSA_ERROR_INVALID_ARGUMENT );
3323
Gilles Peskinebef7f142018-07-12 17:22:21 +02003324#if defined(MBEDTLS_MD_C)
3325 if( PSA_ALG_IS_HKDF( alg ) )
3326 {
3327 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3328 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3329 if( hash_size == 0 )
3330 return( PSA_ERROR_NOT_SUPPORTED );
3331 if( capacity > 255 * hash_size )
3332 return( PSA_ERROR_INVALID_ARGUMENT );
3333 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3334 slot,
3335 hash_alg,
3336 salt, salt_length,
3337 label, label_length );
3338 }
3339 else
3340#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +02003341 {
3342 return( PSA_ERROR_NOT_SUPPORTED );
3343 }
3344
3345 /* Set generator->alg even on failure so that abort knows what to do. */
3346 generator->alg = alg;
3347 if( status == PSA_SUCCESS )
3348 generator->capacity = capacity;
3349 else
3350 psa_generator_abort( generator );
3351 return( status );
3352}
3353
3354
3355
3356/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003357/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02003358/****************************************************************/
3359
3360psa_status_t psa_generate_random( uint8_t *output,
3361 size_t output_size )
3362{
3363 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3364 output, output_size );
3365 return( mbedtls_to_psa_error( ret ) );
3366}
3367
3368psa_status_t psa_generate_key( psa_key_slot_t key,
3369 psa_key_type_t type,
3370 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003371 const void *extra,
3372 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02003373{
Gilles Peskine12313cd2018-06-20 00:20:32 +02003374 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003375 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003376
Gilles Peskine53d991e2018-07-12 01:14:59 +02003377 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003378 return( PSA_ERROR_INVALID_ARGUMENT );
3379
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003380 status = psa_get_empty_key_slot( key, &slot );
3381 if( status != PSA_SUCCESS )
3382 return( status );
3383
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003384 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003385 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003386 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003387 if( status != PSA_SUCCESS )
3388 return( status );
3389 status = psa_generate_random( slot->data.raw.data,
3390 slot->data.raw.bytes );
3391 if( status != PSA_SUCCESS )
3392 {
3393 mbedtls_free( slot->data.raw.data );
3394 return( status );
3395 }
3396#if defined(MBEDTLS_DES_C)
3397 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02003398 psa_des_set_key_parity( slot->data.raw.data,
3399 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003400#endif /* MBEDTLS_DES_C */
3401 }
3402 else
3403
3404#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3405 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3406 {
3407 mbedtls_rsa_context *rsa;
3408 int ret;
3409 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003410 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3411 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02003412 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003413 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02003414 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003415 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003416 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003417#if INT_MAX < 0xffffffff
3418 /* Check that the uint32_t value passed by the caller fits
3419 * in the range supported by this implementation. */
3420 if( p->e > INT_MAX )
3421 return( PSA_ERROR_NOT_SUPPORTED );
3422#endif
3423 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003424 }
3425 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3426 if( rsa == NULL )
3427 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3428 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3429 ret = mbedtls_rsa_gen_key( rsa,
3430 mbedtls_ctr_drbg_random,
3431 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003432 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003433 exponent );
3434 if( ret != 0 )
3435 {
3436 mbedtls_rsa_free( rsa );
3437 mbedtls_free( rsa );
3438 return( mbedtls_to_psa_error( ret ) );
3439 }
3440 slot->data.rsa = rsa;
3441 }
3442 else
3443#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3444
3445#if defined(MBEDTLS_ECP_C)
3446 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3447 {
3448 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3449 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3450 const mbedtls_ecp_curve_info *curve_info =
3451 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3452 mbedtls_ecp_keypair *ecp;
3453 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003454 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003455 return( PSA_ERROR_NOT_SUPPORTED );
3456 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3457 return( PSA_ERROR_NOT_SUPPORTED );
3458 if( curve_info->bit_size != bits )
3459 return( PSA_ERROR_INVALID_ARGUMENT );
3460 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3461 if( ecp == NULL )
3462 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3463 mbedtls_ecp_keypair_init( ecp );
3464 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3465 mbedtls_ctr_drbg_random,
3466 &global_data.ctr_drbg );
3467 if( ret != 0 )
3468 {
3469 mbedtls_ecp_keypair_free( ecp );
3470 mbedtls_free( ecp );
3471 return( mbedtls_to_psa_error( ret ) );
3472 }
3473 slot->data.ecp = ecp;
3474 }
3475 else
3476#endif /* MBEDTLS_ECP_C */
3477
3478 return( PSA_ERROR_NOT_SUPPORTED );
3479
3480 slot->type = type;
3481 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003482}
3483
3484
3485/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003486/* Module setup */
3487/****************************************************************/
3488
Gilles Peskinee59236f2018-01-27 23:32:46 +01003489void mbedtls_psa_crypto_free( void )
3490{
Jaeden Amero045bd502018-06-26 14:00:08 +01003491 psa_key_slot_t key;
Gilles Peskine9a056342018-08-01 15:46:54 +02003492 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003493 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003494 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3495 mbedtls_entropy_free( &global_data.entropy );
3496 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3497}
3498
3499psa_status_t psa_crypto_init( void )
3500{
3501 int ret;
3502 const unsigned char drbg_seed[] = "PSA";
3503
3504 if( global_data.initialized != 0 )
3505 return( PSA_SUCCESS );
3506
3507 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3508 mbedtls_entropy_init( &global_data.entropy );
3509 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3510
3511 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3512 mbedtls_entropy_func,
3513 &global_data.entropy,
3514 drbg_seed, sizeof( drbg_seed ) - 1 );
3515 if( ret != 0 )
3516 goto exit;
3517
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003518 global_data.initialized = 1;
3519
Gilles Peskinee59236f2018-01-27 23:32:46 +01003520exit:
3521 if( ret != 0 )
3522 mbedtls_psa_crypto_free( );
3523 return( mbedtls_to_psa_error( ret ) );
3524}
3525
3526#endif /* MBEDTLS_PSA_CRYPTO_C */