blob: 66a6feb3a2ab2c6963d7913a33bbb8908609f207 [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 Peskineda8191d1c2018-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 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001223 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001224 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001225 case PSA_ALG_ARC4:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001226 mode = MBEDTLS_MODE_STREAM;
1227 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001228 case PSA_ALG_CTR:
1229 mode = MBEDTLS_MODE_CTR;
1230 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02001231 case PSA_ALG_CFB:
1232 mode = MBEDTLS_MODE_CFB;
1233 break;
1234 case PSA_ALG_OFB:
1235 mode = MBEDTLS_MODE_OFB;
1236 break;
1237 case PSA_ALG_CBC_NO_PADDING:
1238 mode = MBEDTLS_MODE_CBC;
1239 break;
1240 case PSA_ALG_CBC_PKCS7:
1241 mode = MBEDTLS_MODE_CBC;
1242 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001243 case PSA_ALG_CCM:
1244 mode = MBEDTLS_MODE_CCM;
1245 break;
1246 case PSA_ALG_GCM:
1247 mode = MBEDTLS_MODE_GCM;
1248 break;
1249 default:
1250 return( NULL );
1251 }
1252 }
1253 else if( alg == PSA_ALG_CMAC )
1254 mode = MBEDTLS_MODE_ECB;
1255 else if( alg == PSA_ALG_GMAC )
1256 mode = MBEDTLS_MODE_GCM;
1257 else
1258 return( NULL );
1259
1260 switch( key_type )
1261 {
1262 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001263 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001264 break;
1265 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001266 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1267 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001268 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001269 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001270 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001271 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001272 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1273 * but two-key Triple-DES is functionally three-key Triple-DES
1274 * with K1=K3, so that's how we present it to mbedtls. */
1275 if( key_bits == 128 )
1276 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001277 break;
1278 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001279 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001280 break;
1281 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001282 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001283 break;
1284 default:
1285 return( NULL );
1286 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001287 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001288 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001289
Jaeden Amero23bbb752018-06-26 14:16:54 +01001290 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1291 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001292}
1293
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001294static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001295{
Gilles Peskine2d277862018-06-18 15:41:12 +02001296 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001297 {
1298 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001299 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001300 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001301 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001302 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001303 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001304 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001305 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001306 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001307 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001308 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001309 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001310 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001311 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001312 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001313 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001314 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001315 return( 128 );
1316 default:
1317 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001318 }
1319}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001320
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001321/* Initialize the MAC operation structure. Once this function has been
1322 * called, psa_mac_abort can run and will do the right thing. */
1323static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1324 psa_algorithm_t alg )
1325{
1326 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1327
1328 operation->alg = alg;
1329 operation->key_set = 0;
1330 operation->iv_set = 0;
1331 operation->iv_required = 0;
1332 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001333 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001334
1335#if defined(MBEDTLS_CMAC_C)
1336 if( alg == PSA_ALG_CMAC )
1337 {
1338 operation->iv_required = 0;
1339 mbedtls_cipher_init( &operation->ctx.cmac );
1340 status = PSA_SUCCESS;
1341 }
1342 else
1343#endif /* MBEDTLS_CMAC_C */
1344#if defined(MBEDTLS_MD_C)
1345 if( PSA_ALG_IS_HMAC( operation->alg ) )
1346 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02001347 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
1348 operation->ctx.hmac.hash_ctx.alg = 0;
1349 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001350 }
1351 else
1352#endif /* MBEDTLS_MD_C */
1353 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001354 if( ! PSA_ALG_IS_MAC( alg ) )
1355 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001356 }
1357
1358 if( status != PSA_SUCCESS )
1359 memset( operation, 0, sizeof( *operation ) );
1360 return( status );
1361}
1362
Gilles Peskine01126fa2018-07-12 17:04:55 +02001363#if defined(MBEDTLS_MD_C)
1364static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
1365{
1366 mbedtls_zeroize( hmac->opad, sizeof( hmac->opad ) );
1367 return( psa_hash_abort( &hmac->hash_ctx ) );
1368}
1369#endif /* MBEDTLS_MD_C */
1370
Gilles Peskine8c9def32018-02-08 10:02:12 +01001371psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1372{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001373 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001374 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001375 /* The object has (apparently) been initialized but it is not
1376 * in use. It's ok to call abort on such an object, and there's
1377 * nothing to do. */
1378 return( PSA_SUCCESS );
1379 }
1380 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001381#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001382 if( operation->alg == PSA_ALG_CMAC )
1383 {
1384 mbedtls_cipher_free( &operation->ctx.cmac );
1385 }
1386 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001387#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001388#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001389 if( PSA_ALG_IS_HMAC( operation->alg ) )
1390 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001391 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001392 }
1393 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001394#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001395 {
1396 /* Sanity check (shouldn't happen: operation->alg should
1397 * always have been initialized to a valid value). */
1398 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001399 }
Moran Peker41deec42018-04-04 15:43:05 +03001400
Gilles Peskine8c9def32018-02-08 10:02:12 +01001401 operation->alg = 0;
1402 operation->key_set = 0;
1403 operation->iv_set = 0;
1404 operation->iv_required = 0;
1405 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001406 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001407
Gilles Peskine8c9def32018-02-08 10:02:12 +01001408 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001409
1410bad_state:
1411 /* If abort is called on an uninitialized object, we can't trust
1412 * anything. Wipe the object in case it contains confidential data.
1413 * This may result in a memory leak if a pointer gets overwritten,
1414 * but it's too late to do anything about this. */
1415 memset( operation, 0, sizeof( *operation ) );
1416 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001417}
1418
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001419#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02001420static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001421 size_t key_bits,
1422 key_slot_t *slot,
1423 const mbedtls_cipher_info_t *cipher_info )
1424{
1425 int ret;
1426
1427 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001428
1429 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1430 if( ret != 0 )
1431 return( ret );
1432
1433 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1434 slot->data.raw.data,
1435 key_bits );
1436 return( ret );
1437}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001438#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001439
Gilles Peskine248051a2018-06-20 16:09:38 +02001440#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02001441static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
1442 const uint8_t *key,
1443 size_t key_length,
1444 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001445{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001446 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001447 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001448 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02001449 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001450 psa_status_t status;
1451
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001452 /* Sanity checks on block_size, to guarantee that there won't be a buffer
1453 * overflow below. This should never trigger if the hash algorithm
1454 * is implemented correctly. */
1455 /* The size checks against the ipad and opad buffers cannot be written
1456 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
1457 * because that triggers -Wlogical-op on GCC 7.3. */
1458 if( block_size > sizeof( ipad ) )
1459 return( PSA_ERROR_NOT_SUPPORTED );
1460 if( block_size > sizeof( hmac->opad ) )
1461 return( PSA_ERROR_NOT_SUPPORTED );
1462 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001463 return( PSA_ERROR_NOT_SUPPORTED );
1464
Gilles Peskined223b522018-06-11 18:12:58 +02001465 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001466 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001467 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001468 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02001469 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001470 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001471 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001472 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02001473 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001474 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001475 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02001476 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001477 }
Gilles Peskine96889972018-07-12 17:07:03 +02001478 /* A 0-length key is not commonly used in HMAC when used as a MAC,
1479 * but it is permitted. It is common when HMAC is used in HKDF, for
1480 * example. Don't call `memcpy` in the 0-length because `key` could be
1481 * an invalid pointer which would make the behavior undefined. */
1482 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001483 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001484
Gilles Peskined223b522018-06-11 18:12:58 +02001485 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1486 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001487 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001488 ipad[i] ^= 0x36;
1489 memset( ipad + key_length, 0x36, block_size - key_length );
1490
1491 /* Copy the key material from ipad to opad, flipping the requisite bits,
1492 * and filling the rest of opad with the requisite constant. */
1493 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02001494 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1495 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001496
Gilles Peskine01126fa2018-07-12 17:04:55 +02001497 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001498 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001499 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001500
Gilles Peskine01126fa2018-07-12 17:04:55 +02001501 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001502
1503cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001504 mbedtls_zeroize( ipad, key_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001505
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001506 return( status );
1507}
Gilles Peskine248051a2018-06-20 16:09:38 +02001508#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001509
Gilles Peskine89167cb2018-07-08 20:12:23 +02001510static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
1511 psa_key_slot_t key,
1512 psa_algorithm_t alg,
1513 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001514{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001515 psa_status_t status;
1516 key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001517 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001518 psa_key_usage_t usage =
1519 is_sign ? PSA_KEY_USAGE_SIGN : PSA_KEY_USAGE_VERIFY;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001520
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001521 status = psa_mac_init( operation, alg );
1522 if( status != PSA_SUCCESS )
1523 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02001524 if( is_sign )
1525 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001526
Gilles Peskine89167cb2018-07-08 20:12:23 +02001527 status = psa_get_key_from_slot( key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001528 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001529 goto exit;
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02001530 key_bits = psa_get_key_bits( slot );
1531
Gilles Peskine8c9def32018-02-08 10:02:12 +01001532#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001533 if( alg == PSA_ALG_CMAC )
1534 {
1535 const mbedtls_cipher_info_t *cipher_info =
1536 mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
1537 int ret;
1538 if( cipher_info == NULL )
1539 {
1540 status = PSA_ERROR_NOT_SUPPORTED;
1541 goto exit;
1542 }
1543 operation->mac_size = cipher_info->block_size;
1544 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
1545 status = mbedtls_to_psa_error( ret );
1546 }
1547 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001548#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001549#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001550 if( PSA_ALG_IS_HMAC( alg ) )
1551 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001552 psa_algorithm_t hash_alg = PSA_ALG_HMAC_HASH( alg );
1553 if( hash_alg == 0 )
1554 {
1555 status = PSA_ERROR_NOT_SUPPORTED;
1556 goto exit;
1557 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001558
1559 operation->mac_size = PSA_HASH_SIZE( hash_alg );
1560 /* Sanity check. This shouldn't fail on a valid configuration. */
1561 if( operation->mac_size == 0 ||
1562 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
1563 {
1564 status = PSA_ERROR_NOT_SUPPORTED;
1565 goto exit;
1566 }
1567
Gilles Peskine01126fa2018-07-12 17:04:55 +02001568 if( slot->type != PSA_KEY_TYPE_HMAC )
1569 {
1570 status = PSA_ERROR_INVALID_ARGUMENT;
1571 goto exit;
1572 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02001573
Gilles Peskine01126fa2018-07-12 17:04:55 +02001574 status = psa_hmac_setup_internal( &operation->ctx.hmac,
1575 slot->data.raw.data,
1576 slot->data.raw.bytes,
1577 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001578 }
1579 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01001580#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02001581 {
1582 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001583 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001584
Gilles Peskinefbfac682018-07-08 20:51:54 +02001585exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001586 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001587 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001588 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001589 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001590 else
1591 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001592 operation->key_set = 1;
1593 }
1594 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001595}
1596
Gilles Peskine89167cb2018-07-08 20:12:23 +02001597psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
1598 psa_key_slot_t key,
1599 psa_algorithm_t alg )
1600{
1601 return( psa_mac_setup( operation, key, alg, 1 ) );
1602}
1603
1604psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
1605 psa_key_slot_t key,
1606 psa_algorithm_t alg )
1607{
1608 return( psa_mac_setup( operation, key, alg, 0 ) );
1609}
1610
Gilles Peskine8c9def32018-02-08 10:02:12 +01001611psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1612 const uint8_t *input,
1613 size_t input_length )
1614{
Gilles Peskinefbfac682018-07-08 20:51:54 +02001615 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001616 if( ! operation->key_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001617 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001618 if( operation->iv_required && ! operation->iv_set )
Gilles Peskinefbfac682018-07-08 20:51:54 +02001619 goto cleanup;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001620 operation->has_input = 1;
1621
Gilles Peskine8c9def32018-02-08 10:02:12 +01001622#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001623 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001624 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02001625 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1626 input, input_length );
1627 status = mbedtls_to_psa_error( ret );
1628 }
1629 else
1630#endif /* MBEDTLS_CMAC_C */
1631#if defined(MBEDTLS_MD_C)
1632 if( PSA_ALG_IS_HMAC( operation->alg ) )
1633 {
1634 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
1635 input_length );
1636 }
1637 else
1638#endif /* MBEDTLS_MD_C */
1639 {
1640 /* This shouldn't happen if `operation` was initialized by
1641 * a setup function. */
1642 status = PSA_ERROR_BAD_STATE;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001643 }
1644
Gilles Peskinefbfac682018-07-08 20:51:54 +02001645cleanup:
1646 if( status != PSA_SUCCESS )
1647 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001648 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001649}
1650
Gilles Peskine01126fa2018-07-12 17:04:55 +02001651#if defined(MBEDTLS_MD_C)
1652static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
1653 uint8_t *mac,
1654 size_t mac_size )
1655{
1656 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
1657 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
1658 size_t hash_size = 0;
1659 size_t block_size = psa_get_hash_block_size( hash_alg );
1660 psa_status_t status;
1661
Gilles Peskine01126fa2018-07-12 17:04:55 +02001662 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
1663 if( status != PSA_SUCCESS )
1664 return( status );
1665 /* From here on, tmp needs to be wiped. */
1666
1667 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
1668 if( status != PSA_SUCCESS )
1669 goto exit;
1670
1671 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
1672 if( status != PSA_SUCCESS )
1673 goto exit;
1674
1675 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
1676 if( status != PSA_SUCCESS )
1677 goto exit;
1678
1679 status = psa_hash_finish( &hmac->hash_ctx, mac, mac_size, &hash_size );
1680
1681exit:
1682 mbedtls_zeroize( tmp, hash_size );
1683 return( status );
1684}
1685#endif /* MBEDTLS_MD_C */
1686
mohammad16036df908f2018-04-02 08:34:15 -07001687static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001688 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001689 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001690{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02001691 if( ! operation->key_set )
1692 return( PSA_ERROR_BAD_STATE );
1693 if( operation->iv_required && ! operation->iv_set )
1694 return( PSA_ERROR_BAD_STATE );
1695
Gilles Peskine8c9def32018-02-08 10:02:12 +01001696 if( mac_size < operation->mac_size )
1697 return( PSA_ERROR_BUFFER_TOO_SMALL );
1698
Gilles Peskine8c9def32018-02-08 10:02:12 +01001699#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02001700 if( operation->alg == PSA_ALG_CMAC )
1701 {
1702 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1703 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001704 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02001705 else
1706#endif /* MBEDTLS_CMAC_C */
1707#if defined(MBEDTLS_MD_C)
1708 if( PSA_ALG_IS_HMAC( operation->alg ) )
1709 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02001710 return( psa_hmac_finish_internal( &operation->ctx.hmac,
1711 mac, mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02001712 }
1713 else
1714#endif /* MBEDTLS_MD_C */
1715 {
1716 /* This shouldn't happen if `operation` was initialized by
1717 * a setup function. */
1718 return( PSA_ERROR_BAD_STATE );
1719 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001720}
1721
Gilles Peskineacd4be32018-07-08 19:56:25 +02001722psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
1723 uint8_t *mac,
1724 size_t mac_size,
1725 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07001726{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001727 psa_status_t status;
1728
1729 /* Fill the output buffer with something that isn't a valid mac
1730 * (barring an attack on the mac and deliberately-crafted input),
1731 * in case the caller doesn't check the return status properly. */
1732 *mac_length = mac_size;
1733 /* If mac_size is 0 then mac may be NULL and then the
1734 * call to memset would have undefined behavior. */
1735 if( mac_size != 0 )
1736 memset( mac, '!', mac_size );
1737
Gilles Peskine89167cb2018-07-08 20:12:23 +02001738 if( ! operation->is_sign )
1739 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001740 status = PSA_ERROR_BAD_STATE;
1741 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001742 }
mohammad16036df908f2018-04-02 08:34:15 -07001743
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001744 status = psa_mac_finish_internal( operation, mac, mac_size );
1745
1746cleanup:
1747 if( status == PSA_SUCCESS )
1748 {
1749 status = psa_mac_abort( operation );
1750 if( status == PSA_SUCCESS )
1751 *mac_length = operation->mac_size;
1752 else
1753 memset( mac, '!', mac_size );
1754 }
1755 else
1756 psa_mac_abort( operation );
1757 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07001758}
1759
Gilles Peskineacd4be32018-07-08 19:56:25 +02001760psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
1761 const uint8_t *mac,
1762 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001763{
Gilles Peskine828ed142018-06-18 23:25:51 +02001764 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07001765 psa_status_t status;
1766
Gilles Peskine89167cb2018-07-08 20:12:23 +02001767 if( operation->is_sign )
1768 {
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001769 status = PSA_ERROR_BAD_STATE;
1770 goto cleanup;
1771 }
1772 if( operation->mac_size != mac_length )
1773 {
1774 status = PSA_ERROR_INVALID_SIGNATURE;
1775 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02001776 }
mohammad16036df908f2018-04-02 08:34:15 -07001777
1778 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02001779 actual_mac, sizeof( actual_mac ) );
1780
1781 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
1782 status = PSA_ERROR_INVALID_SIGNATURE;
1783
1784cleanup:
1785 if( status == PSA_SUCCESS )
1786 status = psa_mac_abort( operation );
1787 else
1788 psa_mac_abort( operation );
1789
1790 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001791}
1792
1793
Gilles Peskine20035e32018-02-03 22:44:14 +01001794
Gilles Peskine20035e32018-02-03 22:44:14 +01001795/****************************************************************/
1796/* Asymmetric cryptography */
1797/****************************************************************/
1798
Gilles Peskine2b450e32018-06-27 15:42:46 +02001799#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001800/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001801 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001802static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1803 size_t hash_length,
1804 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001805{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001806 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001807 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001808 *md_alg = mbedtls_md_get_type( md_info );
1809
1810 /* The Mbed TLS RSA module uses an unsigned int for hash length
1811 * parameters. Validate that it fits so that we don't risk an
1812 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001813#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001814 if( hash_length > UINT_MAX )
1815 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001816#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001817
1818#if defined(MBEDTLS_PKCS1_V15)
1819 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
1820 * must be correct. */
1821 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
1822 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001823 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001824 if( md_info == NULL )
1825 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001826 if( mbedtls_md_get_size( md_info ) != hash_length )
1827 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001828 }
1829#endif /* MBEDTLS_PKCS1_V15 */
1830
1831#if defined(MBEDTLS_PKCS1_V21)
1832 /* PSS requires a hash internally. */
1833 if( PSA_ALG_IS_RSA_PSS( alg ) )
1834 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001835 if( md_info == NULL )
1836 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001837 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001838#endif /* MBEDTLS_PKCS1_V21 */
1839
Gilles Peskine61b91d42018-06-08 16:09:36 +02001840 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001841}
1842
Gilles Peskine2b450e32018-06-27 15:42:46 +02001843static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1844 psa_algorithm_t alg,
1845 const uint8_t *hash,
1846 size_t hash_length,
1847 uint8_t *signature,
1848 size_t signature_size,
1849 size_t *signature_length )
1850{
1851 psa_status_t status;
1852 int ret;
1853 mbedtls_md_type_t md_alg;
1854
1855 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1856 if( status != PSA_SUCCESS )
1857 return( status );
1858
Gilles Peskine630a18a2018-06-29 17:49:35 +02001859 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001860 return( PSA_ERROR_BUFFER_TOO_SMALL );
1861
1862#if defined(MBEDTLS_PKCS1_V15)
1863 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1864 {
1865 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1866 MBEDTLS_MD_NONE );
1867 ret = mbedtls_rsa_pkcs1_sign( rsa,
1868 mbedtls_ctr_drbg_random,
1869 &global_data.ctr_drbg,
1870 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001871 md_alg,
1872 (unsigned int) hash_length,
1873 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001874 signature );
1875 }
1876 else
1877#endif /* MBEDTLS_PKCS1_V15 */
1878#if defined(MBEDTLS_PKCS1_V21)
1879 if( PSA_ALG_IS_RSA_PSS( alg ) )
1880 {
1881 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1882 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1883 mbedtls_ctr_drbg_random,
1884 &global_data.ctr_drbg,
1885 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001886 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001887 (unsigned int) hash_length,
1888 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001889 signature );
1890 }
1891 else
1892#endif /* MBEDTLS_PKCS1_V21 */
1893 {
1894 return( PSA_ERROR_INVALID_ARGUMENT );
1895 }
1896
1897 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02001898 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02001899 return( mbedtls_to_psa_error( ret ) );
1900}
1901
1902static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1903 psa_algorithm_t alg,
1904 const uint8_t *hash,
1905 size_t hash_length,
1906 const uint8_t *signature,
1907 size_t signature_length )
1908{
1909 psa_status_t status;
1910 int ret;
1911 mbedtls_md_type_t md_alg;
1912
1913 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1914 if( status != PSA_SUCCESS )
1915 return( status );
1916
Gilles Peskine630a18a2018-06-29 17:49:35 +02001917 if( signature_length < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02001918 return( PSA_ERROR_BUFFER_TOO_SMALL );
1919
1920#if defined(MBEDTLS_PKCS1_V15)
1921 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1922 {
1923 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1924 MBEDTLS_MD_NONE );
1925 ret = mbedtls_rsa_pkcs1_verify( rsa,
1926 mbedtls_ctr_drbg_random,
1927 &global_data.ctr_drbg,
1928 MBEDTLS_RSA_PUBLIC,
1929 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001930 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001931 hash,
1932 signature );
1933 }
1934 else
1935#endif /* MBEDTLS_PKCS1_V15 */
1936#if defined(MBEDTLS_PKCS1_V21)
1937 if( PSA_ALG_IS_RSA_PSS( alg ) )
1938 {
1939 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1940 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1941 mbedtls_ctr_drbg_random,
1942 &global_data.ctr_drbg,
1943 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02001944 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001945 (unsigned int) hash_length,
1946 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001947 signature );
1948 }
1949 else
1950#endif /* MBEDTLS_PKCS1_V21 */
1951 {
1952 return( PSA_ERROR_INVALID_ARGUMENT );
1953 }
1954 return( mbedtls_to_psa_error( ret ) );
1955}
1956#endif /* MBEDTLS_RSA_C */
1957
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001958#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001959/* `ecp` cannot be const because `ecp->grp` needs to be non-const
1960 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
1961 * (even though these functions don't modify it). */
1962static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
1963 psa_algorithm_t alg,
1964 const uint8_t *hash,
1965 size_t hash_length,
1966 uint8_t *signature,
1967 size_t signature_size,
1968 size_t *signature_length )
1969{
1970 int ret;
1971 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001972 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001973 mbedtls_mpi_init( &r );
1974 mbedtls_mpi_init( &s );
1975
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001976 if( signature_size < 2 * curve_bytes )
1977 {
1978 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
1979 goto cleanup;
1980 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001981
1982 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
1983 {
1984 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
1985 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1986 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
1987 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
1988 hash, hash_length,
1989 md_alg ) );
1990 }
1991 else
1992 {
1993 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
1994 hash, hash_length,
1995 mbedtls_ctr_drbg_random,
1996 &global_data.ctr_drbg ) );
1997 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001998
1999 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
2000 signature,
2001 curve_bytes ) );
2002 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
2003 signature + curve_bytes,
2004 curve_bytes ) );
2005
2006cleanup:
2007 mbedtls_mpi_free( &r );
2008 mbedtls_mpi_free( &s );
2009 if( ret == 0 )
2010 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002011 return( mbedtls_to_psa_error( ret ) );
2012}
2013
2014static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
2015 const uint8_t *hash,
2016 size_t hash_length,
2017 const uint8_t *signature,
2018 size_t signature_length )
2019{
2020 int ret;
2021 mbedtls_mpi r, s;
2022 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
2023 mbedtls_mpi_init( &r );
2024 mbedtls_mpi_init( &s );
2025
2026 if( signature_length != 2 * curve_bytes )
2027 return( PSA_ERROR_INVALID_SIGNATURE );
2028
2029 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
2030 signature,
2031 curve_bytes ) );
2032 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
2033 signature + curve_bytes,
2034 curve_bytes ) );
2035
2036 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
2037 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002038
2039cleanup:
2040 mbedtls_mpi_free( &r );
2041 mbedtls_mpi_free( &s );
2042 return( mbedtls_to_psa_error( ret ) );
2043}
2044#endif /* MBEDTLS_ECDSA_C */
2045
Gilles Peskine61b91d42018-06-08 16:09:36 +02002046psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
2047 psa_algorithm_t alg,
2048 const uint8_t *hash,
2049 size_t hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02002050 uint8_t *signature,
2051 size_t signature_size,
2052 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002053{
2054 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002055 psa_status_t status;
2056
2057 *signature_length = signature_size;
2058
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002059 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_SIGN, alg );
2060 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002061 goto exit;
Gilles Peskine20035e32018-02-03 22:44:14 +01002062 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002063 {
2064 status = PSA_ERROR_INVALID_ARGUMENT;
2065 goto exit;
2066 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002067
Gilles Peskine20035e32018-02-03 22:44:14 +01002068#if defined(MBEDTLS_RSA_C)
2069 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2070 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002071 status = psa_rsa_sign( slot->data.rsa,
2072 alg,
2073 hash, hash_length,
2074 signature, signature_size,
2075 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01002076 }
2077 else
2078#endif /* defined(MBEDTLS_RSA_C) */
2079#if defined(MBEDTLS_ECP_C)
2080 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2081 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002082#if defined(MBEDTLS_ECDSA_C)
2083 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002084 status = psa_ecdsa_sign( slot->data.ecp,
2085 alg,
2086 hash, hash_length,
2087 signature, signature_size,
2088 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002089 else
2090#endif /* defined(MBEDTLS_ECDSA_C) */
2091 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002092 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002093 }
itayzafrir5c753392018-05-08 11:18:38 +03002094 }
2095 else
2096#endif /* defined(MBEDTLS_ECP_C) */
2097 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002098 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01002099 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002100
2101exit:
2102 /* Fill the unused part of the output buffer (the whole buffer on error,
2103 * the trailing part on success) with something that isn't a valid mac
2104 * (barring an attack on the mac and deliberately-crafted input),
2105 * in case the caller doesn't check the return status properly. */
2106 if( status == PSA_SUCCESS )
2107 memset( signature + *signature_length, '!',
2108 signature_size - *signature_length );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002109 else if( signature_size != 0 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002110 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002111 /* If signature_size is 0 then we have nothing to do. We must not call
2112 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002113 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002114}
2115
2116psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
2117 psa_algorithm_t alg,
2118 const uint8_t *hash,
2119 size_t hash_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02002120 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02002121 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03002122{
2123 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002124 psa_status_t status;
Gilles Peskine2b450e32018-06-27 15:42:46 +02002125
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002126 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_VERIFY, alg );
2127 if( status != PSA_SUCCESS )
2128 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03002129
Gilles Peskine61b91d42018-06-08 16:09:36 +02002130#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002131 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002132 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02002133 return( psa_rsa_verify( slot->data.rsa,
2134 alg,
2135 hash, hash_length,
2136 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002137 }
2138 else
2139#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03002140#if defined(MBEDTLS_ECP_C)
2141 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
2142 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002143#if defined(MBEDTLS_ECDSA_C)
2144 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02002145 return( psa_ecdsa_verify( slot->data.ecp,
2146 hash, hash_length,
2147 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002148 else
2149#endif /* defined(MBEDTLS_ECDSA_C) */
2150 {
2151 return( PSA_ERROR_INVALID_ARGUMENT );
2152 }
itayzafrir5c753392018-05-08 11:18:38 +03002153 }
Gilles Peskine20035e32018-02-03 22:44:14 +01002154 else
2155#endif /* defined(MBEDTLS_ECP_C) */
2156 {
2157 return( PSA_ERROR_NOT_SUPPORTED );
2158 }
2159}
2160
Gilles Peskine072ac562018-06-30 00:21:29 +02002161#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
2162static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
2163 mbedtls_rsa_context *rsa )
2164{
2165 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
2166 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
2167 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
2168 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
2169}
2170#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
2171
Gilles Peskine61b91d42018-06-08 16:09:36 +02002172psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2173 psa_algorithm_t alg,
2174 const uint8_t *input,
2175 size_t input_length,
2176 const uint8_t *salt,
2177 size_t salt_length,
2178 uint8_t *output,
2179 size_t output_size,
2180 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002181{
2182 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002183 psa_status_t status;
2184
Darryl Green5cc689a2018-07-24 15:34:10 +01002185 (void) input;
2186 (void) input_length;
2187 (void) salt;
2188 (void) output;
2189 (void) output_size;
2190
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002191 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002192
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002193 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2194 return( PSA_ERROR_INVALID_ARGUMENT );
2195
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002196 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2197 if( status != PSA_SUCCESS )
2198 return( status );
Gilles Peskine35da9a22018-06-29 19:17:49 +02002199 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) ||
2200 PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002201 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002202
2203#if defined(MBEDTLS_RSA_C)
Gilles Peskined8008d62018-06-29 19:51:51 +02002204 if( PSA_KEY_TYPE_IS_RSA( slot->type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002205 {
2206 mbedtls_rsa_context *rsa = slot->data.rsa;
2207 int ret;
Gilles Peskine630a18a2018-06-29 17:49:35 +02002208 if( output_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002209 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002210#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002211 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002212 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002213 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2214 mbedtls_ctr_drbg_random,
2215 &global_data.ctr_drbg,
2216 MBEDTLS_RSA_PUBLIC,
2217 input_length,
2218 input,
2219 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002220 }
2221 else
2222#endif /* MBEDTLS_PKCS1_V15 */
2223#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002224 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002225 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002226 psa_rsa_oaep_set_padding_mode( alg, rsa );
2227 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
2228 mbedtls_ctr_drbg_random,
2229 &global_data.ctr_drbg,
2230 MBEDTLS_RSA_PUBLIC,
2231 salt, salt_length,
2232 input_length,
2233 input,
2234 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002235 }
2236 else
2237#endif /* MBEDTLS_PKCS1_V21 */
2238 {
2239 return( PSA_ERROR_INVALID_ARGUMENT );
2240 }
2241 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02002242 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002243 return( mbedtls_to_psa_error( ret ) );
2244 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002245 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002246#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002247 {
2248 return( PSA_ERROR_NOT_SUPPORTED );
2249 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002250}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002251
Gilles Peskine61b91d42018-06-08 16:09:36 +02002252psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2253 psa_algorithm_t alg,
2254 const uint8_t *input,
2255 size_t input_length,
2256 const uint8_t *salt,
2257 size_t salt_length,
2258 uint8_t *output,
2259 size_t output_size,
2260 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002261{
2262 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002263 psa_status_t status;
2264
Darryl Green5cc689a2018-07-24 15:34:10 +01002265 (void) input;
2266 (void) input_length;
2267 (void) salt;
2268 (void) output;
2269 (void) output_size;
2270
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002271 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002272
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02002273 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
2274 return( PSA_ERROR_INVALID_ARGUMENT );
2275
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002276 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2277 if( status != PSA_SUCCESS )
2278 return( status );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002279 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2280 return( PSA_ERROR_INVALID_ARGUMENT );
2281
2282#if defined(MBEDTLS_RSA_C)
2283 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2284 {
2285 mbedtls_rsa_context *rsa = slot->data.rsa;
2286 int ret;
2287
Gilles Peskine630a18a2018-06-29 17:49:35 +02002288 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002289 return( PSA_ERROR_INVALID_ARGUMENT );
2290
2291#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002292 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002293 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002294 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2295 mbedtls_ctr_drbg_random,
2296 &global_data.ctr_drbg,
2297 MBEDTLS_RSA_PRIVATE,
2298 output_length,
2299 input,
2300 output,
2301 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002302 }
2303 else
2304#endif /* MBEDTLS_PKCS1_V15 */
2305#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002306 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002307 {
Gilles Peskine072ac562018-06-30 00:21:29 +02002308 psa_rsa_oaep_set_padding_mode( alg, rsa );
2309 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
2310 mbedtls_ctr_drbg_random,
2311 &global_data.ctr_drbg,
2312 MBEDTLS_RSA_PRIVATE,
2313 salt, salt_length,
2314 output_length,
2315 input,
2316 output,
2317 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002318 }
2319 else
2320#endif /* MBEDTLS_PKCS1_V21 */
2321 {
2322 return( PSA_ERROR_INVALID_ARGUMENT );
2323 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002324
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002325 return( mbedtls_to_psa_error( ret ) );
2326 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002327 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002328#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002329 {
2330 return( PSA_ERROR_NOT_SUPPORTED );
2331 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002332}
Gilles Peskine20035e32018-02-03 22:44:14 +01002333
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002334
2335
mohammad1603503973b2018-03-12 15:59:30 +02002336/****************************************************************/
2337/* Symmetric cryptography */
2338/****************************************************************/
2339
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002340/* Initialize the cipher operation structure. Once this function has been
2341 * called, psa_cipher_abort can run and will do the right thing. */
2342static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2343 psa_algorithm_t alg )
2344{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002345 if( ! PSA_ALG_IS_CIPHER( alg ) )
2346 {
2347 memset( operation, 0, sizeof( *operation ) );
2348 return( PSA_ERROR_INVALID_ARGUMENT );
2349 }
2350
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002351 operation->alg = alg;
2352 operation->key_set = 0;
2353 operation->iv_set = 0;
2354 operation->iv_required = 1;
2355 operation->iv_size = 0;
2356 operation->block_size = 0;
2357 mbedtls_cipher_init( &operation->ctx.cipher );
2358 return( PSA_SUCCESS );
2359}
2360
Gilles Peskinee553c652018-06-04 16:22:46 +02002361static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2362 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002363 psa_algorithm_t alg,
2364 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002365{
2366 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2367 psa_status_t status;
2368 key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02002369 size_t key_bits;
2370 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002371 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
2372 PSA_KEY_USAGE_ENCRYPT :
2373 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02002374
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002375 status = psa_cipher_init( operation, alg );
2376 if( status != PSA_SUCCESS )
2377 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002378
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002379 status = psa_get_key_from_slot( key, &slot, usage, alg);
2380 if( status != PSA_SUCCESS )
2381 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002382 key_bits = psa_get_key_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02002383
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002384 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002385 if( cipher_info == NULL )
2386 return( PSA_ERROR_NOT_SUPPORTED );
2387
mohammad1603503973b2018-03-12 15:59:30 +02002388 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002389 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002390 {
2391 psa_cipher_abort( operation );
2392 return( mbedtls_to_psa_error( ret ) );
2393 }
2394
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002395#if defined(MBEDTLS_DES_C)
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002396 if( slot->type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002397 {
2398 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2399 unsigned char keys[24];
2400 memcpy( keys, slot->data.raw.data, 16 );
2401 memcpy( keys + 16, slot->data.raw.data, 8 );
2402 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2403 keys,
2404 192, cipher_operation );
2405 }
2406 else
2407#endif
2408 {
2409 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2410 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002411 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002412 }
Moran Peker41deec42018-04-04 15:43:05 +03002413 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002414 {
2415 psa_cipher_abort( operation );
2416 return( mbedtls_to_psa_error( ret ) );
2417 }
2418
mohammad16038481e742018-03-18 13:57:31 +02002419#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002420 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02002421 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002422 case PSA_ALG_CBC_NO_PADDING:
2423 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2424 MBEDTLS_PADDING_NONE );
2425 break;
2426 case PSA_ALG_CBC_PKCS7:
2427 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
2428 MBEDTLS_PADDING_PKCS7 );
2429 break;
2430 default:
2431 /* The algorithm doesn't involve padding. */
2432 ret = 0;
2433 break;
2434 }
2435 if( ret != 0 )
2436 {
2437 psa_cipher_abort( operation );
2438 return( mbedtls_to_psa_error( ret ) );
mohammad16038481e742018-03-18 13:57:31 +02002439 }
2440#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2441
mohammad1603503973b2018-03-12 15:59:30 +02002442 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002443 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
2444 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) );
2445 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02002446 {
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002447 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type );
mohammad16038481e742018-03-18 13:57:31 +02002448 }
mohammad1603503973b2018-03-12 15:59:30 +02002449
Moran Peker395db872018-05-31 14:07:14 +03002450 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002451}
2452
Gilles Peskinefe119512018-07-08 21:39:34 +02002453psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
2454 psa_key_slot_t key,
2455 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002456{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002457 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002458}
2459
Gilles Peskinefe119512018-07-08 21:39:34 +02002460psa_status_t psa_cipher_decrypt_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_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002465}
2466
Gilles Peskinefe119512018-07-08 21:39:34 +02002467psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
2468 unsigned char *iv,
2469 size_t iv_size,
2470 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002471{
itayzafrir534bd7c2018-08-02 13:56:32 +03002472 psa_status_t status;
2473 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002474 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002475 {
2476 status = PSA_ERROR_BAD_STATE;
2477 goto exit;
2478 }
Moran Peker41deec42018-04-04 15:43:05 +03002479 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002480 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002481 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03002482 goto exit;
2483 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002484 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2485 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002486 if( ret != 0 )
2487 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002488 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002489 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002490 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002491
mohammad16038481e742018-03-18 13:57:31 +02002492 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03002493 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002494
Moran Peker395db872018-05-31 14:07:14 +03002495exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03002496 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03002497 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002498 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002499}
2500
Gilles Peskinefe119512018-07-08 21:39:34 +02002501psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
2502 const unsigned char *iv,
2503 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002504{
itayzafrir534bd7c2018-08-02 13:56:32 +03002505 psa_status_t status;
2506 int ret;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002507 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03002508 {
2509 status = PSA_ERROR_BAD_STATE;
2510 goto exit;
2511 }
Moran Pekera28258c2018-05-29 16:25:04 +03002512 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002513 {
itayzafrir534bd7c2018-08-02 13:56:32 +03002514 status = PSA_ERROR_INVALID_ARGUMENT;
2515 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03002516 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002517 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
2518 status = mbedtls_to_psa_error( ret );
2519exit:
2520 if( status == PSA_SUCCESS )
2521 operation->iv_set = 1;
2522 else
mohammad1603503973b2018-03-12 15:59:30 +02002523 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002524 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002525}
2526
Gilles Peskinee553c652018-06-04 16:22:46 +02002527psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2528 const uint8_t *input,
2529 size_t input_length,
2530 unsigned char *output,
2531 size_t output_size,
2532 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002533{
itayzafrir534bd7c2018-08-02 13:56:32 +03002534 psa_status_t status;
2535 int ret;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002536 size_t expected_output_size;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002537 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002538 {
2539 /* Take the unprocessed partial block left over from previous
2540 * update calls, if any, plus the input to this call. Remove
2541 * the last partial block, if any. You get the data that will be
2542 * output in this call. */
2543 expected_output_size =
2544 ( operation->ctx.cipher.unprocessed_len + input_length )
2545 / operation->block_size * operation->block_size;
2546 }
2547 else
2548 {
2549 expected_output_size = input_length;
2550 }
itayzafrir534bd7c2018-08-02 13:56:32 +03002551
Gilles Peskine89d789c2018-06-04 17:17:16 +02002552 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03002553 {
2554 status = PSA_ERROR_BUFFER_TOO_SMALL;
2555 goto exit;
2556 }
mohammad160382759612018-03-12 18:16:40 +02002557
mohammad1603503973b2018-03-12 15:59:30 +02002558 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002559 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03002560 status = mbedtls_to_psa_error( ret );
2561exit:
2562 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02002563 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03002564 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002565}
2566
Gilles Peskinee553c652018-06-04 16:22:46 +02002567psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2568 uint8_t *output,
2569 size_t output_size,
2570 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002571{
Janos Follath315b51c2018-07-09 16:04:51 +01002572 psa_status_t status = PSA_ERROR_UNKNOWN_ERROR;
2573 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002574 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002575
mohammad1603503973b2018-03-12 15:59:30 +02002576 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002577 {
Janos Follath315b51c2018-07-09 16:04:51 +01002578 status = PSA_ERROR_BAD_STATE;
2579 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002580 }
2581 if( operation->iv_required && ! operation->iv_set )
2582 {
Janos Follath315b51c2018-07-09 16:04:51 +01002583 status = PSA_ERROR_BAD_STATE;
2584 goto error;
Moran Peker7cb22b82018-06-05 11:40:02 +03002585 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002586
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002587 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002588 operation->alg == PSA_ALG_CBC_NO_PADDING &&
2589 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03002590 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002591 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01002592 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002593 }
2594
Janos Follath315b51c2018-07-09 16:04:51 +01002595 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
2596 temp_output_buffer,
2597 output_length );
2598 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002599 {
Janos Follath315b51c2018-07-09 16:04:51 +01002600 status = mbedtls_to_psa_error( cipher_ret );
2601 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02002602 }
Janos Follath315b51c2018-07-09 16:04:51 +01002603
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002604 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01002605 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002606 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002607 memcpy( output, temp_output_buffer, *output_length );
2608 else
2609 {
Janos Follath315b51c2018-07-09 16:04:51 +01002610 status = PSA_ERROR_BUFFER_TOO_SMALL;
2611 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002612 }
mohammad1603503973b2018-03-12 15:59:30 +02002613
Janos Follath279ab8e2018-07-09 16:13:21 +01002614 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002615 status = psa_cipher_abort( operation );
2616
2617 return( status );
2618
2619error:
2620
2621 *output_length = 0;
2622
Janos Follath279ab8e2018-07-09 16:13:21 +01002623 mbedtls_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01002624 (void) psa_cipher_abort( operation );
2625
2626 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002627}
2628
Gilles Peskinee553c652018-06-04 16:22:46 +02002629psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2630{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002631 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002632 {
2633 /* The object has (apparently) been initialized but it is not
2634 * in use. It's ok to call abort on such an object, and there's
2635 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002636 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002637 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002638
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002639 /* Sanity check (shouldn't happen: operation->alg should
2640 * always have been initialized to a valid value). */
2641 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2642 return( PSA_ERROR_BAD_STATE );
2643
mohammad1603503973b2018-03-12 15:59:30 +02002644 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002645
Moran Peker41deec42018-04-04 15:43:05 +03002646 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002647 operation->key_set = 0;
2648 operation->iv_set = 0;
2649 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002650 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002651 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002652
Moran Peker395db872018-05-31 14:07:14 +03002653 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002654}
2655
Gilles Peskinea0655c32018-04-30 17:06:50 +02002656
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002657
mohammad16038cc1cee2018-03-28 01:21:33 +03002658/****************************************************************/
2659/* Key Policy */
2660/****************************************************************/
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002661
mohammad160327010052018-07-03 13:16:15 +03002662#if !defined(MBEDTLS_PSA_CRYPTO_SPM)
Gilles Peskine2d277862018-06-18 15:41:12 +02002663void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002664{
Gilles Peskine803ce742018-06-18 16:07:14 +02002665 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002666}
2667
Gilles Peskine2d277862018-06-18 15:41:12 +02002668void psa_key_policy_set_usage( psa_key_policy_t *policy,
2669 psa_key_usage_t usage,
2670 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002671{
mohammad16034eed7572018-03-28 05:14:59 -07002672 policy->usage = usage;
2673 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002674}
2675
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002676psa_key_usage_t psa_key_policy_get_usage( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002677{
mohammad16036df908f2018-04-02 08:34:15 -07002678 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002679}
2680
Gilles Peskineaa7bc472018-07-12 00:54:56 +02002681psa_algorithm_t psa_key_policy_get_algorithm( const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002682{
mohammad16036df908f2018-04-02 08:34:15 -07002683 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002684}
mohammad160327010052018-07-03 13:16:15 +03002685#endif /* !defined(MBEDTLS_PSA_CRYPTO_SPM) */
Mohammad Abo Mokha5c7b7d2018-07-04 15:57:00 +03002686
Gilles Peskine2d277862018-06-18 15:41:12 +02002687psa_status_t psa_set_key_policy( psa_key_slot_t key,
2688 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002689{
2690 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002691 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002692
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002693 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002694 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002695
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002696 status = psa_get_empty_key_slot( key, &slot );
2697 if( status != PSA_SUCCESS )
2698 return( status );
mohammad16038cc1cee2018-03-28 01:21:33 +03002699
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002700 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2701 PSA_KEY_USAGE_ENCRYPT |
2702 PSA_KEY_USAGE_DECRYPT |
2703 PSA_KEY_USAGE_SIGN |
Gilles Peskineea0fb492018-07-12 17:17:20 +02002704 PSA_KEY_USAGE_VERIFY |
2705 PSA_KEY_USAGE_DERIVE ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002706 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002707
mohammad16036df908f2018-04-02 08:34:15 -07002708 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002709
2710 return( PSA_SUCCESS );
2711}
2712
Gilles Peskine2d277862018-06-18 15:41:12 +02002713psa_status_t psa_get_key_policy( psa_key_slot_t key,
2714 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002715{
2716 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002717 psa_status_t status;
mohammad16038cc1cee2018-03-28 01:21:33 +03002718
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002719 if( policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002720 return( PSA_ERROR_INVALID_ARGUMENT );
2721
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002722 status = psa_get_key_slot( key, &slot );
2723 if( status != PSA_SUCCESS )
2724 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002725
mohammad16036df908f2018-04-02 08:34:15 -07002726 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002727
2728 return( PSA_SUCCESS );
2729}
Gilles Peskine20035e32018-02-03 22:44:14 +01002730
Gilles Peskinea0655c32018-04-30 17:06:50 +02002731
2732
mohammad1603804cd712018-03-20 22:44:08 +02002733/****************************************************************/
2734/* Key Lifetime */
2735/****************************************************************/
2736
Gilles Peskine2d277862018-06-18 15:41:12 +02002737psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2738 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002739{
2740 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002741 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002742
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002743 status = psa_get_key_slot( key, &slot );
2744 if( status != PSA_SUCCESS )
2745 return( status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002746
mohammad1603804cd712018-03-20 22:44:08 +02002747 *lifetime = slot->lifetime;
2748
2749 return( PSA_SUCCESS );
2750}
2751
Gilles Peskine2d277862018-06-18 15:41:12 +02002752psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002753 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002754{
2755 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002756 psa_status_t status;
mohammad1603804cd712018-03-20 22:44:08 +02002757
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002758 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2759 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002760 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2761 return( PSA_ERROR_INVALID_ARGUMENT );
2762
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002763 status = psa_get_empty_key_slot( key, &slot );
2764 if( status != PSA_SUCCESS )
2765 return( status );
mohammad1603804cd712018-03-20 22:44:08 +02002766
Moran Pekerd7326592018-05-29 16:56:39 +03002767 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002768 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002769
mohammad1603060ad8a2018-03-20 14:28:38 -07002770 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002771
2772 return( PSA_SUCCESS );
2773}
2774
Gilles Peskine20035e32018-02-03 22:44:14 +01002775
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002776
mohammad16035955c982018-04-26 00:53:03 +03002777/****************************************************************/
2778/* AEAD */
2779/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002780
mohammad16035955c982018-04-26 00:53:03 +03002781psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2782 psa_algorithm_t alg,
2783 const uint8_t *nonce,
2784 size_t nonce_length,
2785 const uint8_t *additional_data,
2786 size_t additional_data_length,
2787 const uint8_t *plaintext,
2788 size_t plaintext_length,
2789 uint8_t *ciphertext,
2790 size_t ciphertext_size,
2791 size_t *ciphertext_length )
2792{
2793 int ret;
2794 psa_status_t status;
2795 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002796 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002797 uint8_t *tag;
2798 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002799 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002800 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002801
mohammad1603f08a5502018-06-03 15:05:47 +03002802 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002803
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002804 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
2805 if( status != PSA_SUCCESS )
2806 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002807 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002808
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002809 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002810 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002811 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002812 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002813
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002814 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002815 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002816 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002817
mohammad16035955c982018-04-26 00:53:03 +03002818 if( alg == PSA_ALG_GCM )
2819 {
2820 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002821 tag_length = 16;
2822
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002823 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002824 return( PSA_ERROR_INVALID_ARGUMENT );
2825
mohammad160315223a82018-06-03 17:19:55 +03002826 //make sure we have place to hold the tag in the ciphertext buffer
2827 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002828 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002829
2830 //update the tag pointer to point to the end of the ciphertext_length
2831 tag = ciphertext + plaintext_length;
2832
mohammad16035955c982018-04-26 00:53:03 +03002833 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002834 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002835 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002836 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002837 if( ret != 0 )
2838 {
2839 mbedtls_gcm_free( &gcm );
2840 return( mbedtls_to_psa_error( ret ) );
2841 }
2842 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002843 plaintext_length, nonce,
2844 nonce_length, additional_data,
2845 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002846 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002847 mbedtls_gcm_free( &gcm );
2848 }
2849 else if( alg == PSA_ALG_CCM )
2850 {
2851 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002852 tag_length = 16;
2853
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002854 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->type ) != 16 )
mohammad160396910d82018-06-04 14:33:00 +03002855 return( PSA_ERROR_INVALID_ARGUMENT );
2856
mohammad160347ddf3d2018-04-26 01:11:21 +03002857 if( nonce_length < 7 || nonce_length > 13 )
2858 return( PSA_ERROR_INVALID_ARGUMENT );
2859
mohammad160315223a82018-06-03 17:19:55 +03002860 //make sure we have place to hold the tag in the ciphertext buffer
2861 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002862 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002863
2864 //update the tag pointer to point to the end of the ciphertext_length
2865 tag = ciphertext + plaintext_length;
2866
mohammad16035955c982018-04-26 00:53:03 +03002867 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002868 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002869 slot->data.raw.data,
2870 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002871 if( ret != 0 )
2872 {
2873 mbedtls_ccm_free( &ccm );
2874 return( mbedtls_to_psa_error( ret ) );
2875 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002876 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002877 nonce, nonce_length,
2878 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002879 additional_data_length,
2880 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002881 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002882 mbedtls_ccm_free( &ccm );
2883 }
mohammad16035c8845f2018-05-09 05:40:09 -07002884 else
2885 {
mohammad1603554faad2018-06-03 15:07:38 +03002886 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002887 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002888
mohammad160315223a82018-06-03 17:19:55 +03002889 if( ret != 0 )
2890 {
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002891 /* If ciphertext_size is 0 then ciphertext may be NULL and then the
2892 * call to memset would have undefined behavior. */
2893 if( ciphertext_size != 0 )
2894 memset( ciphertext, 0, ciphertext_size );
mohammad160315223a82018-06-03 17:19:55 +03002895 return( mbedtls_to_psa_error( ret ) );
2896 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002897
mohammad160315223a82018-06-03 17:19:55 +03002898 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002899 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002900}
2901
Gilles Peskineee652a32018-06-01 19:23:52 +02002902/* Locate the tag in a ciphertext buffer containing the encrypted data
2903 * followed by the tag. Return the length of the part preceding the tag in
2904 * *plaintext_length. This is the size of the plaintext in modes where
2905 * the encrypted data has the same size as the plaintext, such as
2906 * CCM and GCM. */
2907static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2908 const uint8_t *ciphertext,
2909 size_t ciphertext_length,
2910 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002911 const uint8_t **p_tag )
2912{
2913 size_t payload_length;
2914 if( tag_length > ciphertext_length )
2915 return( PSA_ERROR_INVALID_ARGUMENT );
2916 payload_length = ciphertext_length - tag_length;
2917 if( payload_length > plaintext_size )
2918 return( PSA_ERROR_BUFFER_TOO_SMALL );
2919 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002920 return( PSA_SUCCESS );
2921}
2922
mohammad16035955c982018-04-26 00:53:03 +03002923psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2924 psa_algorithm_t alg,
2925 const uint8_t *nonce,
2926 size_t nonce_length,
2927 const uint8_t *additional_data,
2928 size_t additional_data_length,
2929 const uint8_t *ciphertext,
2930 size_t ciphertext_length,
2931 uint8_t *plaintext,
2932 size_t plaintext_size,
2933 size_t *plaintext_length )
2934{
2935 int ret;
2936 psa_status_t status;
2937 key_slot_t *slot;
mohammad16035955c982018-04-26 00:53:03 +03002938 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002939 const uint8_t *tag;
2940 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002941 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002942 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002943
Gilles Peskineee652a32018-06-01 19:23:52 +02002944 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002945
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002946 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
2947 if( status != PSA_SUCCESS )
2948 return( status );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002949 key_bits = psa_get_key_bits( slot );
mohammad16035955c982018-04-26 00:53:03 +03002950
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002951 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->type,
mohammad16035ed06212018-06-06 13:09:34 +03002952 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002953 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002954 return( PSA_ERROR_NOT_SUPPORTED );
2955
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002956 if( ( slot->type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
Gilles Peskine2d277862018-06-18 15:41:12 +02002957 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002958 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002959
mohammad16035955c982018-04-26 00:53:03 +03002960 if( alg == PSA_ALG_GCM )
2961 {
2962 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002963
Gilles Peskineee652a32018-06-01 19:23:52 +02002964 tag_length = 16;
2965 status = psa_aead_unpadded_locate_tag( tag_length,
2966 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002967 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002968 if( status != PSA_SUCCESS )
2969 return( status );
2970
mohammad16035955c982018-04-26 00:53:03 +03002971 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002972 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002973 slot->data.raw.data,
2974 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002975 if( ret != 0 )
2976 {
2977 mbedtls_gcm_free( &gcm );
2978 return( mbedtls_to_psa_error( ret ) );
2979 }
mohammad16035955c982018-04-26 00:53:03 +03002980
Gilles Peskineee652a32018-06-01 19:23:52 +02002981 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002982 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002983 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002984 additional_data,
2985 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002986 tag, tag_length,
2987 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002988 mbedtls_gcm_free( &gcm );
2989 }
2990 else if( alg == PSA_ALG_CCM )
2991 {
2992 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002993
mohammad160347ddf3d2018-04-26 01:11:21 +03002994 if( nonce_length < 7 || nonce_length > 13 )
2995 return( PSA_ERROR_INVALID_ARGUMENT );
2996
mohammad16039375f842018-06-03 14:28:24 +03002997 tag_length = 16;
2998 status = psa_aead_unpadded_locate_tag( tag_length,
2999 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03003000 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03003001 if( status != PSA_SUCCESS )
3002 return( status );
3003
mohammad16035955c982018-04-26 00:53:03 +03003004 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02003005 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003006 slot->data.raw.data,
3007 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03003008 if( ret != 0 )
3009 {
3010 mbedtls_ccm_free( &ccm );
3011 return( mbedtls_to_psa_error( ret ) );
3012 }
mohammad160360a64d02018-06-03 17:20:42 +03003013 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003014 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003015 additional_data,
3016 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02003017 ciphertext, plaintext,
3018 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03003019 mbedtls_ccm_free( &ccm );
3020 }
mohammad160339574652018-06-01 04:39:53 -07003021 else
3022 {
mohammad1603554faad2018-06-03 15:07:38 +03003023 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07003024 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02003025
Gilles Peskineee652a32018-06-01 19:23:52 +02003026 if( ret != 0 )
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003027 {
3028 /* If plaintext_size is 0 then plaintext may be NULL and then the
3029 * call to memset has undefined behavior. */
3030 if( plaintext_size != 0 )
3031 memset( plaintext, 0, plaintext_size );
3032 }
mohammad160360a64d02018-06-03 17:20:42 +03003033 else
3034 *plaintext_length = ciphertext_length - tag_length;
3035
Gilles Peskineee652a32018-06-01 19:23:52 +02003036 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03003037}
3038
Gilles Peskinea0655c32018-04-30 17:06:50 +02003039
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003040
Gilles Peskine20035e32018-02-03 22:44:14 +01003041/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003042/* Generators */
3043/****************************************************************/
3044
3045psa_status_t psa_generator_abort( psa_crypto_generator_t *generator )
3046{
3047 psa_status_t status = PSA_SUCCESS;
3048 if( generator->alg == 0 )
3049 {
3050 /* The object has (apparently) been initialized but it is not
3051 * in use. It's ok to call abort on such an object, and there's
3052 * nothing to do. */
3053 }
3054 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02003055#if defined(MBEDTLS_MD_C)
3056 if( PSA_ALG_IS_HKDF( generator->alg ) )
3057 {
3058 mbedtls_free( generator->ctx.hkdf.info );
3059 status = psa_hmac_abort_internal( &generator->ctx.hkdf.hmac );
3060 }
3061 else
3062#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003063 {
3064 status = PSA_ERROR_BAD_STATE;
3065 }
3066 memset( generator, 0, sizeof( *generator ) );
3067 return( status );
3068}
3069
3070
3071psa_status_t psa_get_generator_capacity(const psa_crypto_generator_t *generator,
3072 size_t *capacity)
3073{
3074 *capacity = generator->capacity;
3075 return( PSA_SUCCESS );
3076}
3077
Gilles Peskinebef7f142018-07-12 17:22:21 +02003078#if defined(MBEDTLS_MD_C)
3079/* Read some bytes from an HKDF-based generator. This performs a chunk
3080 * of the expand phase of the HKDF algorithm. */
3081static psa_status_t psa_generator_hkdf_read( psa_hkdf_generator_t *hkdf,
3082 psa_algorithm_t hash_alg,
3083 uint8_t *output,
3084 size_t output_length )
3085{
3086 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
3087 psa_status_t status;
3088
3089 while( output_length != 0 )
3090 {
3091 /* Copy what remains of the current block */
3092 uint8_t n = hash_length - hkdf->offset_in_block;
3093 if( n > output_length )
3094 n = (uint8_t) output_length;
3095 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
3096 output += n;
3097 output_length -= n;
3098 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02003099 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02003100 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02003101 /* We can't be wanting more output after block 0xff, otherwise
3102 * the capacity check in psa_generator_read() would have
3103 * prevented this call. It could happen only if the generator
3104 * object was corrupted or if this function is called directly
3105 * inside the library. */
3106 if( hkdf->block_number == 0xff )
3107 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02003108
3109 /* We need a new block */
3110 ++hkdf->block_number;
3111 hkdf->offset_in_block = 0;
3112 status = psa_hmac_setup_internal( &hkdf->hmac,
3113 hkdf->prk, hash_length,
3114 hash_alg );
3115 if( status != PSA_SUCCESS )
3116 return( status );
3117 if( hkdf->block_number != 1 )
3118 {
3119 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3120 hkdf->output_block,
3121 hash_length );
3122 if( status != PSA_SUCCESS )
3123 return( status );
3124 }
3125 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3126 hkdf->info,
3127 hkdf->info_length );
3128 if( status != PSA_SUCCESS )
3129 return( status );
3130 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3131 &hkdf->block_number, 1 );
3132 if( status != PSA_SUCCESS )
3133 return( status );
3134 status = psa_hmac_finish_internal( &hkdf->hmac,
3135 hkdf->output_block,
3136 sizeof( hkdf->output_block ) );
3137 if( status != PSA_SUCCESS )
3138 return( status );
3139 }
3140
3141 return( PSA_SUCCESS );
3142}
3143#endif /* MBEDTLS_MD_C */
3144
Gilles Peskineeab56e42018-07-12 17:12:33 +02003145psa_status_t psa_generator_read( psa_crypto_generator_t *generator,
3146 uint8_t *output,
3147 size_t output_length )
3148{
3149 psa_status_t status;
3150
3151 if( output_length > generator->capacity )
3152 {
3153 generator->capacity = 0;
3154 /* Go through the error path to wipe all confidential data now
3155 * that the generator object is useless. */
3156 status = PSA_ERROR_INSUFFICIENT_CAPACITY;
3157 goto exit;
3158 }
3159 if( output_length == 0 &&
3160 generator->capacity == 0 && generator->alg == 0 )
3161 {
3162 /* Edge case: this is a blank or finished generator, and 0
3163 * bytes were requested. The right error in this case could
3164 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
3165 * INSUFFICIENT_CAPACITY, which is right for a finished
3166 * generator, for consistency with the case when
3167 * output_length > 0. */
3168 return( PSA_ERROR_INSUFFICIENT_CAPACITY );
3169 }
3170 generator->capacity -= output_length;
3171
Gilles Peskinebef7f142018-07-12 17:22:21 +02003172#if defined(MBEDTLS_MD_C)
3173 if( PSA_ALG_IS_HKDF( generator->alg ) )
3174 {
3175 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( generator->alg );
3176 status = psa_generator_hkdf_read( &generator->ctx.hkdf, hash_alg,
3177 output, output_length );
3178 }
3179 else
3180#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003181 {
3182 return( PSA_ERROR_BAD_STATE );
3183 }
3184
3185exit:
3186 if( status != PSA_SUCCESS )
3187 {
3188 psa_generator_abort( generator );
3189 memset( output, '!', output_length );
3190 }
3191 return( status );
3192}
3193
Gilles Peskine08542d82018-07-19 17:05:42 +02003194#if defined(MBEDTLS_DES_C)
3195static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
3196{
3197 if( data_size >= 8 )
3198 mbedtls_des_key_set_parity( data );
3199 if( data_size >= 16 )
3200 mbedtls_des_key_set_parity( data + 8 );
3201 if( data_size >= 24 )
3202 mbedtls_des_key_set_parity( data + 16 );
3203}
3204#endif /* MBEDTLS_DES_C */
3205
Gilles Peskineeab56e42018-07-12 17:12:33 +02003206psa_status_t psa_generator_import_key( psa_key_slot_t key,
3207 psa_key_type_t type,
3208 size_t bits,
3209 psa_crypto_generator_t *generator )
3210{
3211 uint8_t *data = NULL;
3212 size_t bytes = PSA_BITS_TO_BYTES( bits );
3213 psa_status_t status;
3214
3215 if( ! key_type_is_raw_bytes( type ) )
3216 return( PSA_ERROR_INVALID_ARGUMENT );
3217 if( bits % 8 != 0 )
3218 return( PSA_ERROR_INVALID_ARGUMENT );
3219 data = mbedtls_calloc( 1, bytes );
3220 if( data == NULL )
3221 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3222
3223 status = psa_generator_read( generator, data, bytes );
3224 if( status != PSA_SUCCESS )
3225 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02003226#if defined(MBEDTLS_DES_C)
3227 if( type == PSA_KEY_TYPE_DES )
3228 psa_des_set_key_parity( data, bytes );
3229#endif /* MBEDTLS_DES_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02003230 status = psa_import_key( key, type, data, bytes );
3231
3232exit:
3233 mbedtls_free( data );
3234 return( status );
3235}
3236
3237
3238
3239/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02003240/* Key derivation */
3241/****************************************************************/
3242
Gilles Peskinebef7f142018-07-12 17:22:21 +02003243/* Set up an HKDF-based generator. This is exactly the extract phase
3244 * of the HKDF algorithm. */
3245static psa_status_t psa_generator_hkdf_setup( psa_hkdf_generator_t *hkdf,
3246 key_slot_t *slot,
3247 psa_algorithm_t hash_alg,
3248 const uint8_t *salt,
3249 size_t salt_length,
3250 const uint8_t *label,
3251 size_t label_length )
3252{
3253 psa_status_t status;
3254 status = psa_hmac_setup_internal( &hkdf->hmac,
3255 salt, salt_length,
3256 PSA_ALG_HMAC_HASH( hash_alg ) );
3257 if( status != PSA_SUCCESS )
3258 return( status );
3259 status = psa_hash_update( &hkdf->hmac.hash_ctx,
3260 slot->data.raw.data,
3261 slot->data.raw.bytes );
3262 if( status != PSA_SUCCESS )
3263 return( status );
3264 status = psa_hmac_finish_internal( &hkdf->hmac,
3265 hkdf->prk,
3266 sizeof( hkdf->prk ) );
3267 if( status != PSA_SUCCESS )
3268 return( status );
3269 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
3270 hkdf->block_number = 0;
3271 hkdf->info_length = label_length;
3272 if( label_length != 0 )
3273 {
3274 hkdf->info = mbedtls_calloc( 1, label_length );
3275 if( hkdf->info == NULL )
3276 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3277 memcpy( hkdf->info, label, label_length );
3278 }
3279 return( PSA_SUCCESS );
3280}
3281
Gilles Peskineea0fb492018-07-12 17:17:20 +02003282psa_status_t psa_key_derivation( psa_crypto_generator_t *generator,
Darryl Green88001362018-07-26 13:59:04 +01003283 psa_key_slot_t key,
Gilles Peskineea0fb492018-07-12 17:17:20 +02003284 psa_algorithm_t alg,
3285 const uint8_t *salt,
3286 size_t salt_length,
3287 const uint8_t *label,
3288 size_t label_length,
3289 size_t capacity )
3290{
3291 key_slot_t *slot;
3292 psa_status_t status;
3293
3294 if( generator->alg != 0 )
3295 return( PSA_ERROR_BAD_STATE );
3296
3297 status = psa_get_key_from_slot( key, &slot, PSA_KEY_USAGE_DERIVE, alg );
3298 if( status != PSA_SUCCESS )
3299 return( status );
3300 if( slot->type != PSA_KEY_TYPE_DERIVE )
3301 return( PSA_ERROR_INVALID_ARGUMENT );
3302
3303 if( ! PSA_ALG_IS_KEY_DERIVATION( alg ) )
3304 return( PSA_ERROR_INVALID_ARGUMENT );
3305
Gilles Peskinebef7f142018-07-12 17:22:21 +02003306#if defined(MBEDTLS_MD_C)
3307 if( PSA_ALG_IS_HKDF( alg ) )
3308 {
3309 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
3310 size_t hash_size = PSA_HASH_SIZE( hash_alg );
3311 if( hash_size == 0 )
3312 return( PSA_ERROR_NOT_SUPPORTED );
3313 if( capacity > 255 * hash_size )
3314 return( PSA_ERROR_INVALID_ARGUMENT );
3315 status = psa_generator_hkdf_setup( &generator->ctx.hkdf,
3316 slot,
3317 hash_alg,
3318 salt, salt_length,
3319 label, label_length );
3320 }
3321 else
3322#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +02003323 {
3324 return( PSA_ERROR_NOT_SUPPORTED );
3325 }
3326
3327 /* Set generator->alg even on failure so that abort knows what to do. */
3328 generator->alg = alg;
3329 if( status == PSA_SUCCESS )
3330 generator->capacity = capacity;
3331 else
3332 psa_generator_abort( generator );
3333 return( status );
3334}
3335
3336
3337
3338/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02003339/* Random generation */
Gilles Peskine05d69892018-06-19 22:00:52 +02003340/****************************************************************/
3341
3342psa_status_t psa_generate_random( uint8_t *output,
3343 size_t output_size )
3344{
3345 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3346 output, output_size );
3347 return( mbedtls_to_psa_error( ret ) );
3348}
3349
3350psa_status_t psa_generate_key( psa_key_slot_t key,
3351 psa_key_type_t type,
3352 size_t bits,
Gilles Peskine53d991e2018-07-12 01:14:59 +02003353 const void *extra,
3354 size_t extra_size )
Gilles Peskine05d69892018-06-19 22:00:52 +02003355{
Gilles Peskine12313cd2018-06-20 00:20:32 +02003356 key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003357 psa_status_t status;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003358
Gilles Peskine53d991e2018-07-12 01:14:59 +02003359 if( extra == NULL && extra_size != 0 )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003360 return( PSA_ERROR_INVALID_ARGUMENT );
3361
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003362 status = psa_get_empty_key_slot( key, &slot );
3363 if( status != PSA_SUCCESS )
3364 return( status );
3365
Gilles Peskine48c0ea12018-06-21 14:15:31 +02003366 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003367 {
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003368 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003369 if( status != PSA_SUCCESS )
3370 return( status );
3371 status = psa_generate_random( slot->data.raw.data,
3372 slot->data.raw.bytes );
3373 if( status != PSA_SUCCESS )
3374 {
3375 mbedtls_free( slot->data.raw.data );
3376 return( status );
3377 }
3378#if defined(MBEDTLS_DES_C)
3379 if( type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02003380 psa_des_set_key_parity( slot->data.raw.data,
3381 slot->data.raw.bytes );
Gilles Peskine12313cd2018-06-20 00:20:32 +02003382#endif /* MBEDTLS_DES_C */
3383 }
3384 else
3385
3386#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
3387 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
3388 {
3389 mbedtls_rsa_context *rsa;
3390 int ret;
3391 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02003392 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
3393 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine53d991e2018-07-12 01:14:59 +02003394 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003395 {
Gilles Peskine4c317f42018-07-12 01:24:09 +02003396 const psa_generate_key_extra_rsa *p = extra;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003397 if( extra_size != sizeof( *p ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003398 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4c317f42018-07-12 01:24:09 +02003399#if INT_MAX < 0xffffffff
3400 /* Check that the uint32_t value passed by the caller fits
3401 * in the range supported by this implementation. */
3402 if( p->e > INT_MAX )
3403 return( PSA_ERROR_NOT_SUPPORTED );
3404#endif
3405 exponent = p->e;
Gilles Peskine12313cd2018-06-20 00:20:32 +02003406 }
3407 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
3408 if( rsa == NULL )
3409 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3410 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
3411 ret = mbedtls_rsa_gen_key( rsa,
3412 mbedtls_ctr_drbg_random,
3413 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003414 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02003415 exponent );
3416 if( ret != 0 )
3417 {
3418 mbedtls_rsa_free( rsa );
3419 mbedtls_free( rsa );
3420 return( mbedtls_to_psa_error( ret ) );
3421 }
3422 slot->data.rsa = rsa;
3423 }
3424 else
3425#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
3426
3427#if defined(MBEDTLS_ECP_C)
3428 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
3429 {
3430 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
3431 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
3432 const mbedtls_ecp_curve_info *curve_info =
3433 mbedtls_ecp_curve_info_from_grp_id( grp_id );
3434 mbedtls_ecp_keypair *ecp;
3435 int ret;
Gilles Peskine53d991e2018-07-12 01:14:59 +02003436 if( extra != NULL )
Gilles Peskine12313cd2018-06-20 00:20:32 +02003437 return( PSA_ERROR_NOT_SUPPORTED );
3438 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
3439 return( PSA_ERROR_NOT_SUPPORTED );
3440 if( curve_info->bit_size != bits )
3441 return( PSA_ERROR_INVALID_ARGUMENT );
3442 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
3443 if( ecp == NULL )
3444 return( PSA_ERROR_INSUFFICIENT_MEMORY );
3445 mbedtls_ecp_keypair_init( ecp );
3446 ret = mbedtls_ecp_gen_key( grp_id, ecp,
3447 mbedtls_ctr_drbg_random,
3448 &global_data.ctr_drbg );
3449 if( ret != 0 )
3450 {
3451 mbedtls_ecp_keypair_free( ecp );
3452 mbedtls_free( ecp );
3453 return( mbedtls_to_psa_error( ret ) );
3454 }
3455 slot->data.ecp = ecp;
3456 }
3457 else
3458#endif /* MBEDTLS_ECP_C */
3459
3460 return( PSA_ERROR_NOT_SUPPORTED );
3461
3462 slot->type = type;
3463 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02003464}
3465
3466
3467/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003468/* Module setup */
3469/****************************************************************/
3470
Gilles Peskinee59236f2018-01-27 23:32:46 +01003471void mbedtls_psa_crypto_free( void )
3472{
Jaeden Amero045bd502018-06-26 14:00:08 +01003473 psa_key_slot_t key;
Gilles Peskine9a056342018-08-01 15:46:54 +02003474 for( key = 1; key <= PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01003475 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003476 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
3477 mbedtls_entropy_free( &global_data.entropy );
3478 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3479}
3480
3481psa_status_t psa_crypto_init( void )
3482{
3483 int ret;
3484 const unsigned char drbg_seed[] = "PSA";
3485
3486 if( global_data.initialized != 0 )
3487 return( PSA_SUCCESS );
3488
3489 mbedtls_zeroize( &global_data, sizeof( global_data ) );
3490 mbedtls_entropy_init( &global_data.entropy );
3491 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
3492
3493 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
3494 mbedtls_entropy_func,
3495 &global_data.entropy,
3496 drbg_seed, sizeof( drbg_seed ) - 1 );
3497 if( ret != 0 )
3498 goto exit;
3499
Gilles Peskinee4ebc122018-03-07 14:16:44 +01003500 global_data.initialized = 1;
3501
Gilles Peskinee59236f2018-01-27 23:32:46 +01003502exit:
3503 if( ret != 0 )
3504 mbedtls_psa_crypto_free( );
3505 return( mbedtls_to_psa_error( ret ) );
3506}
3507
3508#endif /* MBEDTLS_PSA_CRYPTO_C */