blob: 1bea9ed37c7a5617899c67a4626dafd37f4e0cf4 [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)
29
30#include "psa/crypto.h"
31
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010032#include <stdlib.h>
33#include <string.h>
34#if defined(MBEDTLS_PLATFORM_C)
35#include "mbedtls/platform.h"
36#else
37#define mbedtls_calloc calloc
38#define mbedtls_free free
39#endif
40
Gilles Peskinea5905292018-02-07 20:59:33 +010041#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020042#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020043#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010044#include "mbedtls/blowfish.h"
45#include "mbedtls/camellia.h"
46#include "mbedtls/cipher.h"
47#include "mbedtls/ccm.h"
48#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010049#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010050#include "mbedtls/des.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010051#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/error.h"
54#include "mbedtls/gcm.h"
55#include "mbedtls/md2.h"
56#include "mbedtls/md4.h"
57#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010058#include "mbedtls/md.h"
59#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010060#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010061#include "mbedtls/pk_internal.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010063#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/sha1.h"
65#include "mbedtls/sha256.h"
66#include "mbedtls/sha512.h"
67#include "mbedtls/xtea.h"
68
Gilles Peskinee59236f2018-01-27 23:32:46 +010069
70
71/* Implementation that should never be optimized out by the compiler */
72static void mbedtls_zeroize( void *v, size_t n )
73{
74 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
75}
76
Gilles Peskine9ef733f2018-02-07 21:05:37 +010077/* constant-time buffer comparison */
78static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
79{
80 size_t i;
81 unsigned char diff = 0;
82
83 for( i = 0; i < n; i++ )
84 diff |= a[i] ^ b[i];
85
86 return( diff );
87}
88
89
90
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010091/****************************************************************/
92/* Global data, support functions and library management */
93/****************************************************************/
94
95/* Number of key slots (plus one because 0 is not used).
96 * The value is a compile-time constant for now, for simplicity. */
Gilles Peskine828ed142018-06-18 23:25:51 +020097#define PSA_KEY_SLOT_COUNT 32
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010098
Gilles Peskine2d277862018-06-18 15:41:12 +020099typedef struct
100{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100101 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +0300102 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200103 psa_key_lifetime_t lifetime;
Gilles Peskine2d277862018-06-18 15:41:12 +0200104 union
105 {
106 struct raw_data
107 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100108 uint8_t *data;
109 size_t bytes;
110 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100111#if defined(MBEDTLS_RSA_C)
112 mbedtls_rsa_context *rsa;
113#endif /* MBEDTLS_RSA_C */
114#if defined(MBEDTLS_ECP_C)
115 mbedtls_ecp_keypair *ecp;
116#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100117 } data;
118} key_slot_t;
119
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200120static int key_type_is_raw_bytes( psa_key_type_t type )
121{
122 psa_key_type_t category = type & PSA_KEY_TYPE_CATEGORY_MASK;
123 return( category == PSA_KEY_TYPE_RAW_DATA ||
124 category == PSA_KEY_TYPE_CATEGORY_SYMMETRIC );
125}
126
Gilles Peskine2d277862018-06-18 15:41:12 +0200127typedef struct
128{
Gilles Peskinee59236f2018-01-27 23:32:46 +0100129 int initialized;
130 mbedtls_entropy_context entropy;
131 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200132 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinee59236f2018-01-27 23:32:46 +0100133} psa_global_data_t;
134
135static psa_global_data_t global_data;
136
137static psa_status_t mbedtls_to_psa_error( int ret )
138{
Gilles Peskinea5905292018-02-07 20:59:33 +0100139 /* If there's both a high-level code and low-level code, dispatch on
140 * the high-level code. */
141 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100142 {
143 case 0:
144 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100145
146 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
147 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
148 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
149 return( PSA_ERROR_NOT_SUPPORTED );
150 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
151 return( PSA_ERROR_HARDWARE_FAILURE );
152
153 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
154 return( PSA_ERROR_HARDWARE_FAILURE );
155
Gilles Peskine9a944802018-06-21 09:35:35 +0200156 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
157 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
158 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
159 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
160 case MBEDTLS_ERR_ASN1_INVALID_DATA:
161 return( PSA_ERROR_INVALID_ARGUMENT );
162 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
163 return( PSA_ERROR_INSUFFICIENT_MEMORY );
164 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
165 return( PSA_ERROR_BUFFER_TOO_SMALL );
166
Gilles Peskinea5905292018-02-07 20:59:33 +0100167 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
168 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
169 return( PSA_ERROR_NOT_SUPPORTED );
170 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
171 return( PSA_ERROR_HARDWARE_FAILURE );
172
173 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
174 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
175 return( PSA_ERROR_NOT_SUPPORTED );
176 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
177 return( PSA_ERROR_HARDWARE_FAILURE );
178
179 case MBEDTLS_ERR_CCM_BAD_INPUT:
180 return( PSA_ERROR_INVALID_ARGUMENT );
181 case MBEDTLS_ERR_CCM_AUTH_FAILED:
182 return( PSA_ERROR_INVALID_SIGNATURE );
183 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
184 return( PSA_ERROR_HARDWARE_FAILURE );
185
186 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
187 return( PSA_ERROR_NOT_SUPPORTED );
188 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
189 return( PSA_ERROR_INVALID_ARGUMENT );
190 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
191 return( PSA_ERROR_INSUFFICIENT_MEMORY );
192 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
193 return( PSA_ERROR_INVALID_PADDING );
194 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
195 return( PSA_ERROR_BAD_STATE );
196 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
197 return( PSA_ERROR_INVALID_SIGNATURE );
198 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
199 return( PSA_ERROR_TAMPERING_DETECTED );
200 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
201 return( PSA_ERROR_HARDWARE_FAILURE );
202
203 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
204 return( PSA_ERROR_HARDWARE_FAILURE );
205
206 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
207 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
208 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
209 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
210 return( PSA_ERROR_NOT_SUPPORTED );
211 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
212 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
213
214 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
215 return( PSA_ERROR_NOT_SUPPORTED );
216 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
217 return( PSA_ERROR_HARDWARE_FAILURE );
218
Gilles Peskinee59236f2018-01-27 23:32:46 +0100219 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
220 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
221 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
222 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100223
224 case MBEDTLS_ERR_GCM_AUTH_FAILED:
225 return( PSA_ERROR_INVALID_SIGNATURE );
226 case MBEDTLS_ERR_GCM_BAD_INPUT:
227 return( PSA_ERROR_NOT_SUPPORTED );
228 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
229 return( PSA_ERROR_HARDWARE_FAILURE );
230
231 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
232 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
233 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
234 return( PSA_ERROR_HARDWARE_FAILURE );
235
236 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
237 return( PSA_ERROR_NOT_SUPPORTED );
238 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
239 return( PSA_ERROR_INVALID_ARGUMENT );
240 case MBEDTLS_ERR_MD_ALLOC_FAILED:
241 return( PSA_ERROR_INSUFFICIENT_MEMORY );
242 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
243 return( PSA_ERROR_STORAGE_FAILURE );
244 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
245 return( PSA_ERROR_HARDWARE_FAILURE );
246
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100247 case MBEDTLS_ERR_PK_ALLOC_FAILED:
248 return( PSA_ERROR_INSUFFICIENT_MEMORY );
249 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
250 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
251 return( PSA_ERROR_INVALID_ARGUMENT );
252 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100253 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100254 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
255 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
256 return( PSA_ERROR_INVALID_ARGUMENT );
257 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
258 return( PSA_ERROR_NOT_SUPPORTED );
259 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
260 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
261 return( PSA_ERROR_NOT_PERMITTED );
262 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_PK_INVALID_ALG:
265 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
266 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
267 return( PSA_ERROR_NOT_SUPPORTED );
268 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
269 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100270 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
271 return( PSA_ERROR_HARDWARE_FAILURE );
272
273 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
274 return( PSA_ERROR_HARDWARE_FAILURE );
275
276 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_RSA_INVALID_PADDING:
279 return( PSA_ERROR_INVALID_PADDING );
280 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
281 return( PSA_ERROR_HARDWARE_FAILURE );
282 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
283 return( PSA_ERROR_INVALID_ARGUMENT );
284 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
285 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
286 return( PSA_ERROR_TAMPERING_DETECTED );
287 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
288 return( PSA_ERROR_INVALID_SIGNATURE );
289 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
290 return( PSA_ERROR_BUFFER_TOO_SMALL );
291 case MBEDTLS_ERR_RSA_RNG_FAILED:
292 return( PSA_ERROR_INSUFFICIENT_MEMORY );
293 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
294 return( PSA_ERROR_NOT_SUPPORTED );
295 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
296 return( PSA_ERROR_HARDWARE_FAILURE );
297
298 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
299 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
300 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
301 return( PSA_ERROR_HARDWARE_FAILURE );
302
303 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
304 return( PSA_ERROR_INVALID_ARGUMENT );
305 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
306 return( PSA_ERROR_HARDWARE_FAILURE );
307
itayzafrir5c753392018-05-08 11:18:38 +0300308 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300309 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300310 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300311 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
312 return( PSA_ERROR_BUFFER_TOO_SMALL );
313 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
314 return( PSA_ERROR_NOT_SUPPORTED );
315 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
316 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
317 return( PSA_ERROR_INVALID_SIGNATURE );
318 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
319 return( PSA_ERROR_INSUFFICIENT_MEMORY );
320 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
321 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300322
Gilles Peskinee59236f2018-01-27 23:32:46 +0100323 default:
324 return( PSA_ERROR_UNKNOWN_ERROR );
325 }
326}
327
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200328
329
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100330/****************************************************************/
331/* Key management */
332/****************************************************************/
333
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200334static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
335{
336 switch( grpid )
337 {
338 case MBEDTLS_ECP_DP_SECP192R1:
339 return( PSA_ECC_CURVE_SECP192R1 );
340 case MBEDTLS_ECP_DP_SECP224R1:
341 return( PSA_ECC_CURVE_SECP224R1 );
342 case MBEDTLS_ECP_DP_SECP256R1:
343 return( PSA_ECC_CURVE_SECP256R1 );
344 case MBEDTLS_ECP_DP_SECP384R1:
345 return( PSA_ECC_CURVE_SECP384R1 );
346 case MBEDTLS_ECP_DP_SECP521R1:
347 return( PSA_ECC_CURVE_SECP521R1 );
348 case MBEDTLS_ECP_DP_BP256R1:
349 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
350 case MBEDTLS_ECP_DP_BP384R1:
351 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
352 case MBEDTLS_ECP_DP_BP512R1:
353 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
354 case MBEDTLS_ECP_DP_CURVE25519:
355 return( PSA_ECC_CURVE_CURVE25519 );
356 case MBEDTLS_ECP_DP_SECP192K1:
357 return( PSA_ECC_CURVE_SECP192K1 );
358 case MBEDTLS_ECP_DP_SECP224K1:
359 return( PSA_ECC_CURVE_SECP224K1 );
360 case MBEDTLS_ECP_DP_SECP256K1:
361 return( PSA_ECC_CURVE_SECP256K1 );
362 case MBEDTLS_ECP_DP_CURVE448:
363 return( PSA_ECC_CURVE_CURVE448 );
364 default:
365 return( 0 );
366 }
367}
368
Gilles Peskine12313cd2018-06-20 00:20:32 +0200369static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
370{
371 switch( curve )
372 {
373 case PSA_ECC_CURVE_SECP192R1:
374 return( MBEDTLS_ECP_DP_SECP192R1 );
375 case PSA_ECC_CURVE_SECP224R1:
376 return( MBEDTLS_ECP_DP_SECP224R1 );
377 case PSA_ECC_CURVE_SECP256R1:
378 return( MBEDTLS_ECP_DP_SECP256R1 );
379 case PSA_ECC_CURVE_SECP384R1:
380 return( MBEDTLS_ECP_DP_SECP384R1 );
381 case PSA_ECC_CURVE_SECP521R1:
382 return( MBEDTLS_ECP_DP_SECP521R1 );
383 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
384 return( MBEDTLS_ECP_DP_BP256R1 );
385 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
386 return( MBEDTLS_ECP_DP_BP384R1 );
387 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
388 return( MBEDTLS_ECP_DP_BP512R1 );
389 case PSA_ECC_CURVE_CURVE25519:
390 return( MBEDTLS_ECP_DP_CURVE25519 );
391 case PSA_ECC_CURVE_SECP192K1:
392 return( MBEDTLS_ECP_DP_SECP192K1 );
393 case PSA_ECC_CURVE_SECP224K1:
394 return( MBEDTLS_ECP_DP_SECP224K1 );
395 case PSA_ECC_CURVE_SECP256K1:
396 return( MBEDTLS_ECP_DP_SECP256K1 );
397 case PSA_ECC_CURVE_CURVE448:
398 return( MBEDTLS_ECP_DP_CURVE448 );
399 default:
400 return( MBEDTLS_ECP_DP_NONE );
401 }
402}
403
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200404static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
405 size_t bits,
406 struct raw_data *raw )
407{
408 /* Check that the bit size is acceptable for the key type */
409 switch( type )
410 {
411 case PSA_KEY_TYPE_RAW_DATA:
412#if defined(MBEDTLS_MD_C)
413 case PSA_KEY_TYPE_HMAC:
414#endif
415 break;
416#if defined(MBEDTLS_AES_C)
417 case PSA_KEY_TYPE_AES:
418 if( bits != 128 && bits != 192 && bits != 256 )
419 return( PSA_ERROR_INVALID_ARGUMENT );
420 break;
421#endif
422#if defined(MBEDTLS_CAMELLIA_C)
423 case PSA_KEY_TYPE_CAMELLIA:
424 if( bits != 128 && bits != 192 && bits != 256 )
425 return( PSA_ERROR_INVALID_ARGUMENT );
426 break;
427#endif
428#if defined(MBEDTLS_DES_C)
429 case PSA_KEY_TYPE_DES:
430 if( bits != 64 && bits != 128 && bits != 192 )
431 return( PSA_ERROR_INVALID_ARGUMENT );
432 break;
433#endif
434#if defined(MBEDTLS_ARC4_C)
435 case PSA_KEY_TYPE_ARC4:
436 if( bits < 8 || bits > 2048 )
437 return( PSA_ERROR_INVALID_ARGUMENT );
438 break;
439#endif
440 default:
441 return( PSA_ERROR_NOT_SUPPORTED );
442 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200443 if( bits % 8 != 0 )
444 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445
446 /* Allocate memory for the key */
447 raw->bytes = PSA_BITS_TO_BYTES( bits );
448 raw->data = mbedtls_calloc( 1, raw->bytes );
449 if( raw->data == NULL )
450 {
451 raw->bytes = 0;
452 return( PSA_ERROR_INSUFFICIENT_MEMORY );
453 }
454 return( PSA_SUCCESS );
455}
456
Gilles Peskine2d277862018-06-18 15:41:12 +0200457psa_status_t psa_import_key( psa_key_slot_t key,
458 psa_key_type_t type,
459 const uint8_t *data,
460 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100461{
462 key_slot_t *slot;
463
Gilles Peskine828ed142018-06-18 23:25:51 +0200464 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100465 return( PSA_ERROR_INVALID_ARGUMENT );
466 slot = &global_data.key_slots[key];
467 if( slot->type != PSA_KEY_TYPE_NONE )
468 return( PSA_ERROR_OCCUPIED_SLOT );
469
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200470 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100471 {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200472 psa_status_t status;
Gilles Peskine6d912132018-03-07 16:41:37 +0100473 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100474 if( data_length > SIZE_MAX / 8 )
475 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200476 status = prepare_raw_data_slot( type,
477 PSA_BYTES_TO_BITS( data_length ),
478 &slot->data.raw );
479 if( status != PSA_SUCCESS )
480 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100481 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100482 }
483 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100484#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100485 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
486 type == PSA_KEY_TYPE_RSA_KEYPAIR ||
487 PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100488 {
489 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100490 mbedtls_pk_context pk;
Gilles Peskinec648d692018-06-28 08:46:13 +0200491 psa_status_t status = PSA_SUCCESS;
Gilles Peskine969ac722018-01-28 18:16:59 +0100492 mbedtls_pk_init( &pk );
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100493 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
494 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
495 else
496 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100497 if( ret != 0 )
498 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine969ac722018-01-28 18:16:59 +0100499 switch( mbedtls_pk_get_type( &pk ) )
500 {
501#if defined(MBEDTLS_RSA_C)
502 case MBEDTLS_PK_RSA:
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100503 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
504 type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200505 {
506 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
507 size_t bits = mbedtls_rsa_get_bitlen( rsa );
508 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
509 return( PSA_ERROR_NOT_SUPPORTED );
510 slot->data.rsa = rsa;
511 }
Gilles Peskine969ac722018-01-28 18:16:59 +0100512 else
Gilles Peskinec648d692018-06-28 08:46:13 +0200513 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine969ac722018-01-28 18:16:59 +0100514 break;
515#endif /* MBEDTLS_RSA_C */
516#if defined(MBEDTLS_ECP_C)
517 case MBEDTLS_PK_ECKEY:
518 if( PSA_KEY_TYPE_IS_ECC( type ) )
519 {
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200520 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
521 psa_ecc_curve_t actual_curve =
522 mbedtls_ecc_group_to_psa( ecp->grp.id );
523 psa_ecc_curve_t expected_curve =
524 PSA_KEY_TYPE_GET_CURVE( type );
525 if( actual_curve != expected_curve )
Gilles Peskinec648d692018-06-28 08:46:13 +0200526 {
527 status = PSA_ERROR_INVALID_ARGUMENT;
528 break;
529 }
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200530 slot->data.ecp = ecp;
Gilles Peskine969ac722018-01-28 18:16:59 +0100531 }
532 else
Gilles Peskinec648d692018-06-28 08:46:13 +0200533 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine969ac722018-01-28 18:16:59 +0100534 break;
535#endif /* MBEDTLS_ECP_C */
536 default:
Gilles Peskinec648d692018-06-28 08:46:13 +0200537 status = PSA_ERROR_INVALID_ARGUMENT;
538 break;
539 }
540 /* Free the content of the pk object only on error. On success,
541 * the content of the object has been stored in the slot. */
542 if( status != PSA_SUCCESS )
543 {
544 mbedtls_pk_free( &pk );
545 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100546 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100547 }
548 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100549#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100550 {
551 return( PSA_ERROR_NOT_SUPPORTED );
552 }
553
554 slot->type = type;
555 return( PSA_SUCCESS );
556}
557
Gilles Peskine2d277862018-06-18 15:41:12 +0200558psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100559{
560 key_slot_t *slot;
561
Gilles Peskine828ed142018-06-18 23:25:51 +0200562 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100563 return( PSA_ERROR_INVALID_ARGUMENT );
564 slot = &global_data.key_slots[key];
565 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200566 {
567 /* No key material to clean, but do zeroize the slot below to wipe
568 * metadata such as policies. */
569 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200570 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100571 {
572 mbedtls_free( slot->data.raw.data );
573 }
574 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100575#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100576 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
577 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100578 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100579 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100580 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100581 }
582 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100583#endif /* defined(MBEDTLS_RSA_C) */
584#if defined(MBEDTLS_ECP_C)
585 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
586 {
587 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100588 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100589 }
590 else
591#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100592 {
593 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100594 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100595 return( PSA_ERROR_TAMPERING_DETECTED );
596 }
597
598 mbedtls_zeroize( slot, sizeof( *slot ) );
599 return( PSA_SUCCESS );
600}
601
Gilles Peskine2d277862018-06-18 15:41:12 +0200602psa_status_t psa_get_key_information( psa_key_slot_t key,
603 psa_key_type_t *type,
604 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100605{
606 key_slot_t *slot;
607
Gilles Peskine828ed142018-06-18 23:25:51 +0200608 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100609 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100610 slot = &global_data.key_slots[key];
611 if( type != NULL )
612 *type = slot->type;
613 if( bits != NULL )
614 *bits = 0;
615 if( slot->type == PSA_KEY_TYPE_NONE )
616 return( PSA_ERROR_EMPTY_SLOT );
617
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200618 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100619 {
620 if( bits != NULL )
621 *bits = slot->data.raw.bytes * 8;
622 }
623 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100624#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100625 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
626 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100627 {
628 if( bits != NULL )
Gilles Peskine969ac722018-01-28 18:16:59 +0100629 *bits = mbedtls_rsa_get_bitlen( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100630 }
631 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100632#endif /* defined(MBEDTLS_RSA_C) */
633#if defined(MBEDTLS_ECP_C)
634 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
635 {
636 if( bits != NULL )
637 *bits = slot->data.ecp->grp.pbits;
638 }
639 else
640#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100641 {
642 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100643 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100644 return( PSA_ERROR_TAMPERING_DETECTED );
645 }
646
647 return( PSA_SUCCESS );
648}
649
Gilles Peskine2d277862018-06-18 15:41:12 +0200650static psa_status_t psa_internal_export_key( psa_key_slot_t key,
651 uint8_t *data,
652 size_t data_size,
653 size_t *data_length,
654 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100655{
656 key_slot_t *slot;
657
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100658 /* Set the key to empty now, so that even when there are errors, we always
659 * set data_length to a value between 0 and data_size. On error, setting
660 * the key to empty is a good choice because an empty key representation is
661 * unlikely to be accepted anywhere. */
662 *data_length = 0;
663
Gilles Peskine828ed142018-06-18 23:25:51 +0200664 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100665 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100666 slot = &global_data.key_slots[key];
667 if( slot->type == PSA_KEY_TYPE_NONE )
668 return( PSA_ERROR_EMPTY_SLOT );
669
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200670 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300671 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300672
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200673 if( ! export_public_key &&
674 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) &&
675 ( slot->policy.usage & PSA_KEY_USAGE_EXPORT ) == 0 )
Moran Peker87567632018-06-04 18:41:37 +0300676 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine2d277862018-06-18 15:41:12 +0200677
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200678 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100679 {
680 if( slot->data.raw.bytes > data_size )
681 return( PSA_ERROR_BUFFER_TOO_SMALL );
682 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
683 *data_length = slot->data.raw.bytes;
684 return( PSA_SUCCESS );
685 }
686 else
Moran Peker17e36e12018-05-02 12:55:20 +0300687 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100688#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100689 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
Moran Pekera998bc62018-04-16 18:16:20 +0300690 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
691 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100692 {
Moran Pekera998bc62018-04-16 18:16:20 +0300693 mbedtls_pk_context pk;
694 int ret;
695 mbedtls_pk_init( &pk );
696 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
697 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
698 {
699 pk.pk_info = &mbedtls_rsa_info;
700 pk.pk_ctx = slot->data.rsa;
701 }
702 else
703 {
704 pk.pk_info = &mbedtls_eckey_info;
705 pk.pk_ctx = slot->data.ecp;
706 }
Moran Pekerd7326592018-05-29 16:56:39 +0300707 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300708 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300709 else
710 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300711 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200712 {
713 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300714 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200715 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200716 /* The mbedtls_pk_xxx functions write to the end of the buffer.
717 * Move the data to the beginning and erase remaining data
718 * at the original location. */
719 if( 2 * (size_t) ret <= data_size )
720 {
721 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200722 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200723 }
724 else if( (size_t) ret < data_size )
725 {
726 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200727 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200728 }
Moran Pekera998bc62018-04-16 18:16:20 +0300729 *data_length = ret;
730 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100731 }
732 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100733#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300734 {
735 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200736 it is valid for a special-purpose implementation to omit
737 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300738 return( PSA_ERROR_NOT_SUPPORTED );
739 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740 }
741}
742
Gilles Peskine2d277862018-06-18 15:41:12 +0200743psa_status_t psa_export_key( psa_key_slot_t key,
744 uint8_t *data,
745 size_t data_size,
746 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300747{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200748 return( psa_internal_export_key( key, data, data_size,
749 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100750}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100751
Gilles Peskine2d277862018-06-18 15:41:12 +0200752psa_status_t psa_export_public_key( psa_key_slot_t key,
753 uint8_t *data,
754 size_t data_size,
755 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300756{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200757 return( psa_internal_export_key( key, data, data_size,
758 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300759}
760
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200761
762
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100763/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100764/* Message digests */
765/****************************************************************/
766
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100767static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100768{
769 switch( alg )
770 {
771#if defined(MBEDTLS_MD2_C)
772 case PSA_ALG_MD2:
773 return( &mbedtls_md2_info );
774#endif
775#if defined(MBEDTLS_MD4_C)
776 case PSA_ALG_MD4:
777 return( &mbedtls_md4_info );
778#endif
779#if defined(MBEDTLS_MD5_C)
780 case PSA_ALG_MD5:
781 return( &mbedtls_md5_info );
782#endif
783#if defined(MBEDTLS_RIPEMD160_C)
784 case PSA_ALG_RIPEMD160:
785 return( &mbedtls_ripemd160_info );
786#endif
787#if defined(MBEDTLS_SHA1_C)
788 case PSA_ALG_SHA_1:
789 return( &mbedtls_sha1_info );
790#endif
791#if defined(MBEDTLS_SHA256_C)
792 case PSA_ALG_SHA_224:
793 return( &mbedtls_sha224_info );
794 case PSA_ALG_SHA_256:
795 return( &mbedtls_sha256_info );
796#endif
797#if defined(MBEDTLS_SHA512_C)
798 case PSA_ALG_SHA_384:
799 return( &mbedtls_sha384_info );
800 case PSA_ALG_SHA_512:
801 return( &mbedtls_sha512_info );
802#endif
803 default:
804 return( NULL );
805 }
806}
807
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100808psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
809{
810 switch( operation->alg )
811 {
Gilles Peskine81736312018-06-26 15:04:31 +0200812 case 0:
813 /* The object has (apparently) been initialized but it is not
814 * in use. It's ok to call abort on such an object, and there's
815 * nothing to do. */
816 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100817#if defined(MBEDTLS_MD2_C)
818 case PSA_ALG_MD2:
819 mbedtls_md2_free( &operation->ctx.md2 );
820 break;
821#endif
822#if defined(MBEDTLS_MD4_C)
823 case PSA_ALG_MD4:
824 mbedtls_md4_free( &operation->ctx.md4 );
825 break;
826#endif
827#if defined(MBEDTLS_MD5_C)
828 case PSA_ALG_MD5:
829 mbedtls_md5_free( &operation->ctx.md5 );
830 break;
831#endif
832#if defined(MBEDTLS_RIPEMD160_C)
833 case PSA_ALG_RIPEMD160:
834 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
835 break;
836#endif
837#if defined(MBEDTLS_SHA1_C)
838 case PSA_ALG_SHA_1:
839 mbedtls_sha1_free( &operation->ctx.sha1 );
840 break;
841#endif
842#if defined(MBEDTLS_SHA256_C)
843 case PSA_ALG_SHA_224:
844 case PSA_ALG_SHA_256:
845 mbedtls_sha256_free( &operation->ctx.sha256 );
846 break;
847#endif
848#if defined(MBEDTLS_SHA512_C)
849 case PSA_ALG_SHA_384:
850 case PSA_ALG_SHA_512:
851 mbedtls_sha512_free( &operation->ctx.sha512 );
852 break;
853#endif
854 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +0200855 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100856 }
857 operation->alg = 0;
858 return( PSA_SUCCESS );
859}
860
861psa_status_t psa_hash_start( psa_hash_operation_t *operation,
862 psa_algorithm_t alg )
863{
864 int ret;
865 operation->alg = 0;
866 switch( alg )
867 {
868#if defined(MBEDTLS_MD2_C)
869 case PSA_ALG_MD2:
870 mbedtls_md2_init( &operation->ctx.md2 );
871 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
872 break;
873#endif
874#if defined(MBEDTLS_MD4_C)
875 case PSA_ALG_MD4:
876 mbedtls_md4_init( &operation->ctx.md4 );
877 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
878 break;
879#endif
880#if defined(MBEDTLS_MD5_C)
881 case PSA_ALG_MD5:
882 mbedtls_md5_init( &operation->ctx.md5 );
883 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
884 break;
885#endif
886#if defined(MBEDTLS_RIPEMD160_C)
887 case PSA_ALG_RIPEMD160:
888 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
889 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
890 break;
891#endif
892#if defined(MBEDTLS_SHA1_C)
893 case PSA_ALG_SHA_1:
894 mbedtls_sha1_init( &operation->ctx.sha1 );
895 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
896 break;
897#endif
898#if defined(MBEDTLS_SHA256_C)
899 case PSA_ALG_SHA_224:
900 mbedtls_sha256_init( &operation->ctx.sha256 );
901 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
902 break;
903 case PSA_ALG_SHA_256:
904 mbedtls_sha256_init( &operation->ctx.sha256 );
905 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
906 break;
907#endif
908#if defined(MBEDTLS_SHA512_C)
909 case PSA_ALG_SHA_384:
910 mbedtls_sha512_init( &operation->ctx.sha512 );
911 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
912 break;
913 case PSA_ALG_SHA_512:
914 mbedtls_sha512_init( &operation->ctx.sha512 );
915 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
916 break;
917#endif
918 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +0200919 return( PSA_ALG_IS_HASH( alg ) ?
920 PSA_ERROR_NOT_SUPPORTED :
921 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100922 }
923 if( ret == 0 )
924 operation->alg = alg;
925 else
926 psa_hash_abort( operation );
927 return( mbedtls_to_psa_error( ret ) );
928}
929
930psa_status_t psa_hash_update( psa_hash_operation_t *operation,
931 const uint8_t *input,
932 size_t input_length )
933{
934 int ret;
935 switch( operation->alg )
936 {
937#if defined(MBEDTLS_MD2_C)
938 case PSA_ALG_MD2:
939 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
940 input, input_length );
941 break;
942#endif
943#if defined(MBEDTLS_MD4_C)
944 case PSA_ALG_MD4:
945 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
946 input, input_length );
947 break;
948#endif
949#if defined(MBEDTLS_MD5_C)
950 case PSA_ALG_MD5:
951 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
952 input, input_length );
953 break;
954#endif
955#if defined(MBEDTLS_RIPEMD160_C)
956 case PSA_ALG_RIPEMD160:
957 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
958 input, input_length );
959 break;
960#endif
961#if defined(MBEDTLS_SHA1_C)
962 case PSA_ALG_SHA_1:
963 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
964 input, input_length );
965 break;
966#endif
967#if defined(MBEDTLS_SHA256_C)
968 case PSA_ALG_SHA_224:
969 case PSA_ALG_SHA_256:
970 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
971 input, input_length );
972 break;
973#endif
974#if defined(MBEDTLS_SHA512_C)
975 case PSA_ALG_SHA_384:
976 case PSA_ALG_SHA_512:
977 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
978 input, input_length );
979 break;
980#endif
981 default:
982 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
983 break;
984 }
985 if( ret != 0 )
986 psa_hash_abort( operation );
987 return( mbedtls_to_psa_error( ret ) );
988}
989
990psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
991 uint8_t *hash,
992 size_t hash_size,
993 size_t *hash_length )
994{
995 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +0200996 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100997
998 /* Fill the output buffer with something that isn't a valid hash
999 * (barring an attack on the hash and deliberately-crafted input),
1000 * in case the caller doesn't check the return status properly. */
1001 *hash_length = actual_hash_length;
1002 memset( hash, '!', hash_size );
1003
1004 if( hash_size < actual_hash_length )
1005 return( PSA_ERROR_BUFFER_TOO_SMALL );
1006
1007 switch( operation->alg )
1008 {
1009#if defined(MBEDTLS_MD2_C)
1010 case PSA_ALG_MD2:
1011 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1012 break;
1013#endif
1014#if defined(MBEDTLS_MD4_C)
1015 case PSA_ALG_MD4:
1016 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1017 break;
1018#endif
1019#if defined(MBEDTLS_MD5_C)
1020 case PSA_ALG_MD5:
1021 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1022 break;
1023#endif
1024#if defined(MBEDTLS_RIPEMD160_C)
1025 case PSA_ALG_RIPEMD160:
1026 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1027 break;
1028#endif
1029#if defined(MBEDTLS_SHA1_C)
1030 case PSA_ALG_SHA_1:
1031 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1032 break;
1033#endif
1034#if defined(MBEDTLS_SHA256_C)
1035 case PSA_ALG_SHA_224:
1036 case PSA_ALG_SHA_256:
1037 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1038 break;
1039#endif
1040#if defined(MBEDTLS_SHA512_C)
1041 case PSA_ALG_SHA_384:
1042 case PSA_ALG_SHA_512:
1043 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1044 break;
1045#endif
1046 default:
1047 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1048 break;
1049 }
1050
1051 if( ret == 0 )
1052 {
1053 return( psa_hash_abort( operation ) );
1054 }
1055 else
1056 {
1057 psa_hash_abort( operation );
1058 return( mbedtls_to_psa_error( ret ) );
1059 }
1060}
1061
Gilles Peskine2d277862018-06-18 15:41:12 +02001062psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1063 const uint8_t *hash,
1064 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001065{
1066 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1067 size_t actual_hash_length;
1068 psa_status_t status = psa_hash_finish( operation,
1069 actual_hash, sizeof( actual_hash ),
1070 &actual_hash_length );
1071 if( status != PSA_SUCCESS )
1072 return( status );
1073 if( actual_hash_length != hash_length )
1074 return( PSA_ERROR_INVALID_SIGNATURE );
1075 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1076 return( PSA_ERROR_INVALID_SIGNATURE );
1077 return( PSA_SUCCESS );
1078}
1079
1080
1081
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001082/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001083/* MAC */
1084/****************************************************************/
1085
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001086static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001087 psa_algorithm_t alg,
1088 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001089 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001090 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001091{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001092 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001093 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001094
1095 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1096 {
1097 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001098 {
1099 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1100 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001101
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001102 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001103 {
1104 case PSA_ALG_STREAM_CIPHER:
1105 mode = MBEDTLS_MODE_STREAM;
1106 break;
1107 case PSA_ALG_CBC_BASE:
1108 mode = MBEDTLS_MODE_CBC;
1109 break;
1110 case PSA_ALG_CFB_BASE:
1111 mode = MBEDTLS_MODE_CFB;
1112 break;
1113 case PSA_ALG_OFB_BASE:
1114 mode = MBEDTLS_MODE_OFB;
1115 break;
1116 case PSA_ALG_CTR:
1117 mode = MBEDTLS_MODE_CTR;
1118 break;
1119 case PSA_ALG_CCM:
1120 mode = MBEDTLS_MODE_CCM;
1121 break;
1122 case PSA_ALG_GCM:
1123 mode = MBEDTLS_MODE_GCM;
1124 break;
1125 default:
1126 return( NULL );
1127 }
1128 }
1129 else if( alg == PSA_ALG_CMAC )
1130 mode = MBEDTLS_MODE_ECB;
1131 else if( alg == PSA_ALG_GMAC )
1132 mode = MBEDTLS_MODE_GCM;
1133 else
1134 return( NULL );
1135
1136 switch( key_type )
1137 {
1138 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001139 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001140 break;
1141 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001142 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1143 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001144 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001145 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001146 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001147 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001148 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1149 * but two-key Triple-DES is functionally three-key Triple-DES
1150 * with K1=K3, so that's how we present it to mbedtls. */
1151 if( key_bits == 128 )
1152 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001153 break;
1154 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001155 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001156 break;
1157 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001158 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001159 break;
1160 default:
1161 return( NULL );
1162 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001163 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001164 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001165
Jaeden Amero23bbb752018-06-26 14:16:54 +01001166 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1167 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001168}
1169
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001170static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001171{
Gilles Peskine2d277862018-06-18 15:41:12 +02001172 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001173 {
1174 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001175 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001176 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001177 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001178 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001179 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001180 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001181 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001182 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001183 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001184 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001185 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001186 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001187 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001188 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001189 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001190 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001191 return( 128 );
1192 default:
1193 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001194 }
1195}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001196
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001197/* Initialize the MAC operation structure. Once this function has been
1198 * called, psa_mac_abort can run and will do the right thing. */
1199static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1200 psa_algorithm_t alg )
1201{
1202 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1203
1204 operation->alg = alg;
1205 operation->key_set = 0;
1206 operation->iv_set = 0;
1207 operation->iv_required = 0;
1208 operation->has_input = 0;
1209 operation->key_usage_sign = 0;
1210 operation->key_usage_verify = 0;
1211
1212#if defined(MBEDTLS_CMAC_C)
1213 if( alg == PSA_ALG_CMAC )
1214 {
1215 operation->iv_required = 0;
1216 mbedtls_cipher_init( &operation->ctx.cmac );
1217 status = PSA_SUCCESS;
1218 }
1219 else
1220#endif /* MBEDTLS_CMAC_C */
1221#if defined(MBEDTLS_MD_C)
1222 if( PSA_ALG_IS_HMAC( operation->alg ) )
1223 {
1224 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1225 PSA_ALG_HMAC_HASH( alg ) );
1226 }
1227 else
1228#endif /* MBEDTLS_MD_C */
1229 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001230 if( ! PSA_ALG_IS_MAC( alg ) )
1231 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001232 }
1233
1234 if( status != PSA_SUCCESS )
1235 memset( operation, 0, sizeof( *operation ) );
1236 return( status );
1237}
1238
Gilles Peskine8c9def32018-02-08 10:02:12 +01001239psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1240{
1241 switch( operation->alg )
1242 {
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001243 case 0:
Gilles Peskine81736312018-06-26 15:04:31 +02001244 /* The object has (apparently) been initialized but it is not
1245 * in use. It's ok to call abort on such an object, and there's
1246 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001247 return( PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001248#if defined(MBEDTLS_CMAC_C)
1249 case PSA_ALG_CMAC:
1250 mbedtls_cipher_free( &operation->ctx.cmac );
1251 break;
1252#endif /* MBEDTLS_CMAC_C */
1253 default:
1254#if defined(MBEDTLS_MD_C)
1255 if( PSA_ALG_IS_HMAC( operation->alg ) )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001256 {
Jaeden Amero5390f692018-06-26 14:18:50 +01001257 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001258 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001259
Gilles Peskine99bc6492018-06-11 17:13:00 +02001260 if( block_size == 0 )
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001261 return( PSA_ERROR_NOT_SUPPORTED );
1262
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001263 psa_hash_abort( &operation->ctx.hmac.hash_ctx );
Gilles Peskine2d277862018-06-18 15:41:12 +02001264 mbedtls_zeroize( operation->ctx.hmac.opad, block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001265 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001266 else
1267#endif /* MBEDTLS_MD_C */
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001268 {
1269 /* Sanity check (shouldn't happen: operation->alg should
1270 * always have been initialized to a valid value). */
1271 return( PSA_ERROR_BAD_STATE );
1272 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001273 }
Moran Peker41deec42018-04-04 15:43:05 +03001274
Gilles Peskine8c9def32018-02-08 10:02:12 +01001275 operation->alg = 0;
1276 operation->key_set = 0;
1277 operation->iv_set = 0;
1278 operation->iv_required = 0;
1279 operation->has_input = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001280 operation->key_usage_sign = 0;
1281 operation->key_usage_verify = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001282
Gilles Peskine8c9def32018-02-08 10:02:12 +01001283 return( PSA_SUCCESS );
1284}
1285
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001286#if defined(MBEDTLS_CMAC_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001287static int psa_cmac_start( psa_mac_operation_t *operation,
1288 size_t key_bits,
1289 key_slot_t *slot,
1290 const mbedtls_cipher_info_t *cipher_info )
1291{
1292 int ret;
1293
1294 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001295
1296 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1297 if( ret != 0 )
1298 return( ret );
1299
1300 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1301 slot->data.raw.data,
1302 key_bits );
1303 return( ret );
1304}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001305#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001306
Gilles Peskine248051a2018-06-20 16:09:38 +02001307#if defined(MBEDTLS_MD_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001308static int psa_hmac_start( psa_mac_operation_t *operation,
1309 psa_key_type_t key_type,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001310 key_slot_t *slot,
1311 psa_algorithm_t alg )
1312{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001313 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001314 unsigned char *opad = operation->ctx.hmac.opad;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001315 size_t i;
Gilles Peskinee1bc6802018-06-11 17:36:05 +02001316 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001317 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001318 unsigned int digest_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001319 PSA_HASH_SIZE( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001320 size_t key_length = slot->data.raw.bytes;
1321 psa_status_t status;
1322
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001323 if( block_size == 0 || digest_size == 0 )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001324 return( PSA_ERROR_NOT_SUPPORTED );
1325
1326 if( key_type != PSA_KEY_TYPE_HMAC )
1327 return( PSA_ERROR_INVALID_ARGUMENT );
1328
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001329 operation->mac_size = digest_size;
1330
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001331 /* The hash was started earlier in psa_mac_init. */
Gilles Peskined223b522018-06-11 18:12:58 +02001332 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001333 {
1334 status = psa_hash_update( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001335 slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001336 if( status != PSA_SUCCESS )
1337 return( status );
1338 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001339 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001340 if( status != PSA_SUCCESS )
1341 return( status );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001342 }
Gilles Peskined223b522018-06-11 18:12:58 +02001343 else
1344 memcpy( ipad, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001345
Gilles Peskined223b522018-06-11 18:12:58 +02001346 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1347 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001348 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001349 ipad[i] ^= 0x36;
1350 memset( ipad + key_length, 0x36, block_size - key_length );
1351
1352 /* Copy the key material from ipad to opad, flipping the requisite bits,
1353 * and filling the rest of opad with the requisite constant. */
1354 for( i = 0; i < key_length; i++ )
1355 opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1356 memset( opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001357
1358 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1359 PSA_ALG_HMAC_HASH( alg ) );
1360 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001361 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001362
1363 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, ipad,
1364 block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001365
1366cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001367 mbedtls_zeroize( ipad, key_length );
1368 /* opad is in the context. It needs to stay in memory if this function
1369 * succeeds, and it will be wiped by psa_mac_abort() called from
1370 * psa_mac_start in the error case. */
1371
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001372 return( status );
1373}
Gilles Peskine248051a2018-06-20 16:09:38 +02001374#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001375
Gilles Peskine8c9def32018-02-08 10:02:12 +01001376psa_status_t psa_mac_start( psa_mac_operation_t *operation,
1377 psa_key_slot_t key,
1378 psa_algorithm_t alg )
1379{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001380 psa_status_t status;
1381 key_slot_t *slot;
1382 psa_key_type_t key_type;
1383 size_t key_bits;
Gilles Peskine828ed142018-06-18 23:25:51 +02001384 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001385
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001386 status = psa_mac_init( operation, alg );
1387 if( status != PSA_SUCCESS )
1388 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001389
1390 status = psa_get_key_information( key, &key_type, &key_bits );
1391 if( status != PSA_SUCCESS )
1392 return( status );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001393
Gilles Peskine8c9def32018-02-08 10:02:12 +01001394 slot = &global_data.key_slots[key];
Gilles Peskine99bc6492018-06-11 17:13:00 +02001395 if( slot->type == PSA_KEY_TYPE_NONE )
1396 return( PSA_ERROR_EMPTY_SLOT );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001397
Moran Pekerd7326592018-05-29 16:56:39 +03001398 if( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001399 operation->key_usage_sign = 1;
1400
Moran Pekerd7326592018-05-29 16:56:39 +03001401 if( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001402 operation->key_usage_verify = 1;
1403
Gilles Peskine8c9def32018-02-08 10:02:12 +01001404 if( ! PSA_ALG_IS_HMAC( alg ) )
1405 {
mohammad1603f4f0d612018-06-03 15:04:51 +03001406 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001407 if( cipher_info == NULL )
1408 return( PSA_ERROR_NOT_SUPPORTED );
1409 operation->mac_size = cipher_info->block_size;
1410 }
1411 switch( alg )
1412 {
1413#if defined(MBEDTLS_CMAC_C)
1414 case PSA_ALG_CMAC:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001415 status = mbedtls_to_psa_error( psa_cmac_start( operation,
1416 key_bits,
1417 slot,
1418 cipher_info ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001419 break;
1420#endif /* MBEDTLS_CMAC_C */
1421 default:
1422#if defined(MBEDTLS_MD_C)
1423 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskined223b522018-06-11 18:12:58 +02001424 status = psa_hmac_start( operation, key_type, slot, alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001425 else
1426#endif /* MBEDTLS_MD_C */
1427 return( PSA_ERROR_NOT_SUPPORTED );
1428 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001429
Gilles Peskine8c9def32018-02-08 10:02:12 +01001430 /* If we reach this point, then the algorithm-specific part of the
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001431 * context may contain data that needs to be wiped on error. */
1432 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001433 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001434 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001435 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001436 else
1437 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001438 operation->key_set = 1;
1439 }
1440 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001441}
1442
1443psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1444 const uint8_t *input,
1445 size_t input_length )
1446{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001447 int ret = 0 ;
1448 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001449 if( ! operation->key_set )
1450 return( PSA_ERROR_BAD_STATE );
1451 if( operation->iv_required && ! operation->iv_set )
1452 return( PSA_ERROR_BAD_STATE );
1453 operation->has_input = 1;
1454
1455 switch( operation->alg )
1456 {
1457#if defined(MBEDTLS_CMAC_C)
1458 case PSA_ALG_CMAC:
1459 ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1460 input, input_length );
1461 break;
1462#endif /* MBEDTLS_CMAC_C */
1463 default:
1464#if defined(MBEDTLS_MD_C)
1465 if( PSA_ALG_IS_HMAC( operation->alg ) )
1466 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001467 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
Gilles Peskine2d277862018-06-18 15:41:12 +02001468 input_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001469 }
1470 else
1471#endif /* MBEDTLS_MD_C */
1472 {
1473 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1474 }
1475 break;
1476 }
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001477 if( ret != 0 || status != PSA_SUCCESS )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001478 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001479 psa_mac_abort( operation );
Gilles Peskine2d277862018-06-18 15:41:12 +02001480 if( ret != 0 )
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001481 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001482 }
1483
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001484 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001485}
1486
mohammad16036df908f2018-04-02 08:34:15 -07001487static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001488 uint8_t *mac,
1489 size_t mac_size,
1490 size_t *mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001491{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001492 int ret = 0;
1493 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001494 if( ! operation->key_set )
1495 return( PSA_ERROR_BAD_STATE );
1496 if( operation->iv_required && ! operation->iv_set )
1497 return( PSA_ERROR_BAD_STATE );
1498
1499 /* Fill the output buffer with something that isn't a valid mac
1500 * (barring an attack on the mac and deliberately-crafted input),
1501 * in case the caller doesn't check the return status properly. */
1502 *mac_length = operation->mac_size;
1503 memset( mac, '!', mac_size );
1504
1505 if( mac_size < operation->mac_size )
1506 return( PSA_ERROR_BUFFER_TOO_SMALL );
1507
1508 switch( operation->alg )
1509 {
1510#if defined(MBEDTLS_CMAC_C)
1511 case PSA_ALG_CMAC:
1512 ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1513 break;
1514#endif /* MBEDTLS_CMAC_C */
1515 default:
1516#if defined(MBEDTLS_MD_C)
1517 if( PSA_ALG_IS_HMAC( operation->alg ) )
1518 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001519 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001520 unsigned char *opad = operation->ctx.hmac.opad;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001521 size_t hash_size = 0;
Nir Sonnenschein96272412018-06-17 14:41:10 +03001522 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001523 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001524
Gilles Peskine99bc6492018-06-11 17:13:00 +02001525 if( block_size == 0 )
1526 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001527
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001528 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine99bc6492018-06-11 17:13:00 +02001529 sizeof( tmp ), &hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001530 if( status != PSA_SUCCESS )
1531 goto cleanup;
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001532 /* From here on, tmp needs to be wiped. */
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001533
1534 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1535 PSA_ALG_HMAC_HASH( operation->alg ) );
1536 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001537 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001538
1539 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, opad,
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001540 block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001541 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001542 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001543
1544 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine2d277862018-06-18 15:41:12 +02001545 hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001546 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001547 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001548
1549 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, mac,
1550 mac_size, mac_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001551 hmac_cleanup:
1552 mbedtls_zeroize( tmp, hash_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001553 }
1554 else
1555#endif /* MBEDTLS_MD_C */
1556 {
1557 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1558 }
1559 break;
1560 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001561cleanup:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001562
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001563 if( ret == 0 && status == PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001564 {
1565 return( psa_mac_abort( operation ) );
1566 }
1567 else
1568 {
1569 psa_mac_abort( operation );
Gilles Peskine99bc6492018-06-11 17:13:00 +02001570 if( ret != 0 )
Gilles Peskine2d277862018-06-18 15:41:12 +02001571 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001572
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001573 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001574 }
1575}
1576
mohammad16036df908f2018-04-02 08:34:15 -07001577psa_status_t psa_mac_finish( psa_mac_operation_t *operation,
1578 uint8_t *mac,
1579 size_t mac_size,
1580 size_t *mac_length )
1581{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001582 if( ! operation->key_usage_sign )
mohammad16036df908f2018-04-02 08:34:15 -07001583 return( PSA_ERROR_NOT_PERMITTED );
1584
Gilles Peskine99bc6492018-06-11 17:13:00 +02001585 return( psa_mac_finish_internal( operation, mac,
1586 mac_size, mac_length ) );
mohammad16036df908f2018-04-02 08:34:15 -07001587}
1588
Gilles Peskine8c9def32018-02-08 10:02:12 +01001589psa_status_t psa_mac_verify( psa_mac_operation_t *operation,
1590 const uint8_t *mac,
1591 size_t mac_length )
1592{
Gilles Peskine828ed142018-06-18 23:25:51 +02001593 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Gilles Peskine8c9def32018-02-08 10:02:12 +01001594 size_t actual_mac_length;
mohammad16036df908f2018-04-02 08:34:15 -07001595 psa_status_t status;
1596
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001597 if( ! operation->key_usage_verify )
mohammad16036df908f2018-04-02 08:34:15 -07001598 return( PSA_ERROR_NOT_PERMITTED );
1599
1600 status = psa_mac_finish_internal( operation,
1601 actual_mac, sizeof( actual_mac ),
1602 &actual_mac_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001603 if( status != PSA_SUCCESS )
1604 return( status );
1605 if( actual_mac_length != mac_length )
1606 return( PSA_ERROR_INVALID_SIGNATURE );
1607 if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
1608 return( PSA_ERROR_INVALID_SIGNATURE );
1609 return( PSA_SUCCESS );
1610}
1611
1612
Gilles Peskine20035e32018-02-03 22:44:14 +01001613
Gilles Peskine20035e32018-02-03 22:44:14 +01001614/****************************************************************/
1615/* Asymmetric cryptography */
1616/****************************************************************/
1617
Gilles Peskine2b450e32018-06-27 15:42:46 +02001618#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001619/* Decode the hash algorithm from alg and store the mbedtls encoding in
1620 * md_alg. Verify that the hash length is consistent. */
1621static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1622 size_t hash_length,
1623 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001624{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001625 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001626 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1627 *md_alg = hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1628 if( *md_alg == MBEDTLS_MD_NONE )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001629 {
1630#if SIZE_MAX > UINT_MAX
Gilles Peskine61b91d42018-06-08 16:09:36 +02001631 if( hash_length > UINT_MAX )
1632 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001633#endif
1634 }
1635 else
1636 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001637 if( mbedtls_md_get_size( md_info ) != hash_length )
1638 return( PSA_ERROR_INVALID_ARGUMENT );
1639 if( md_info == NULL )
1640 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001641 }
Gilles Peskine61b91d42018-06-08 16:09:36 +02001642 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001643}
1644
Gilles Peskine2b450e32018-06-27 15:42:46 +02001645static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1646 psa_algorithm_t alg,
1647 const uint8_t *hash,
1648 size_t hash_length,
1649 uint8_t *signature,
1650 size_t signature_size,
1651 size_t *signature_length )
1652{
1653 psa_status_t status;
1654 int ret;
1655 mbedtls_md_type_t md_alg;
1656
1657 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1658 if( status != PSA_SUCCESS )
1659 return( status );
1660
1661 if( signature_size < rsa->len )
1662 return( PSA_ERROR_BUFFER_TOO_SMALL );
1663
1664#if defined(MBEDTLS_PKCS1_V15)
1665 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1666 {
1667 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1668 MBEDTLS_MD_NONE );
1669 ret = mbedtls_rsa_pkcs1_sign( rsa,
1670 mbedtls_ctr_drbg_random,
1671 &global_data.ctr_drbg,
1672 MBEDTLS_RSA_PRIVATE,
1673 md_alg, hash_length, hash,
1674 signature );
1675 }
1676 else
1677#endif /* MBEDTLS_PKCS1_V15 */
1678#if defined(MBEDTLS_PKCS1_V21)
1679 if( PSA_ALG_IS_RSA_PSS( alg ) )
1680 {
1681 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1682 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1683 mbedtls_ctr_drbg_random,
1684 &global_data.ctr_drbg,
1685 MBEDTLS_RSA_PRIVATE,
1686 md_alg, hash_length, hash,
1687 signature );
1688 }
1689 else
1690#endif /* MBEDTLS_PKCS1_V21 */
1691 {
1692 return( PSA_ERROR_INVALID_ARGUMENT );
1693 }
1694
1695 if( ret == 0 )
1696 *signature_length = rsa->len;
1697 return( mbedtls_to_psa_error( ret ) );
1698}
1699
1700static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1701 psa_algorithm_t alg,
1702 const uint8_t *hash,
1703 size_t hash_length,
1704 const uint8_t *signature,
1705 size_t signature_length )
1706{
1707 psa_status_t status;
1708 int ret;
1709 mbedtls_md_type_t md_alg;
1710
1711 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1712 if( status != PSA_SUCCESS )
1713 return( status );
1714
1715 if( signature_length < rsa->len )
1716 return( PSA_ERROR_BUFFER_TOO_SMALL );
1717
1718#if defined(MBEDTLS_PKCS1_V15)
1719 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1720 {
1721 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1722 MBEDTLS_MD_NONE );
1723 ret = mbedtls_rsa_pkcs1_verify( rsa,
1724 mbedtls_ctr_drbg_random,
1725 &global_data.ctr_drbg,
1726 MBEDTLS_RSA_PUBLIC,
1727 md_alg,
1728 hash_length,
1729 hash,
1730 signature );
1731 }
1732 else
1733#endif /* MBEDTLS_PKCS1_V15 */
1734#if defined(MBEDTLS_PKCS1_V21)
1735 if( PSA_ALG_IS_RSA_PSS( alg ) )
1736 {
1737 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1738 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1739 mbedtls_ctr_drbg_random,
1740 &global_data.ctr_drbg,
1741 MBEDTLS_RSA_PUBLIC,
1742 md_alg, hash_length, hash,
1743 signature );
1744 }
1745 else
1746#endif /* MBEDTLS_PKCS1_V21 */
1747 {
1748 return( PSA_ERROR_INVALID_ARGUMENT );
1749 }
1750 return( mbedtls_to_psa_error( ret ) );
1751}
1752#endif /* MBEDTLS_RSA_C */
1753
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001754#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001755/* `ecp` cannot be const because `ecp->grp` needs to be non-const
1756 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
1757 * (even though these functions don't modify it). */
1758static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
1759 psa_algorithm_t alg,
1760 const uint8_t *hash,
1761 size_t hash_length,
1762 uint8_t *signature,
1763 size_t signature_size,
1764 size_t *signature_length )
1765{
1766 int ret;
1767 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001768 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001769 mbedtls_mpi_init( &r );
1770 mbedtls_mpi_init( &s );
1771
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001772 if( signature_size < 2 * curve_bytes )
1773 {
1774 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
1775 goto cleanup;
1776 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001777
1778 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
1779 {
1780 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
1781 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1782 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
1783 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
1784 hash, hash_length,
1785 md_alg ) );
1786 }
1787 else
1788 {
1789 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
1790 hash, hash_length,
1791 mbedtls_ctr_drbg_random,
1792 &global_data.ctr_drbg ) );
1793 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001794
1795 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
1796 signature,
1797 curve_bytes ) );
1798 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
1799 signature + curve_bytes,
1800 curve_bytes ) );
1801
1802cleanup:
1803 mbedtls_mpi_free( &r );
1804 mbedtls_mpi_free( &s );
1805 if( ret == 0 )
1806 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001807 return( mbedtls_to_psa_error( ret ) );
1808}
1809
1810static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
1811 const uint8_t *hash,
1812 size_t hash_length,
1813 const uint8_t *signature,
1814 size_t signature_length )
1815{
1816 int ret;
1817 mbedtls_mpi r, s;
1818 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
1819 mbedtls_mpi_init( &r );
1820 mbedtls_mpi_init( &s );
1821
1822 if( signature_length != 2 * curve_bytes )
1823 return( PSA_ERROR_INVALID_SIGNATURE );
1824
1825 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
1826 signature,
1827 curve_bytes ) );
1828 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
1829 signature + curve_bytes,
1830 curve_bytes ) );
1831
1832 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
1833 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001834
1835cleanup:
1836 mbedtls_mpi_free( &r );
1837 mbedtls_mpi_free( &s );
1838 return( mbedtls_to_psa_error( ret ) );
1839}
1840#endif /* MBEDTLS_ECDSA_C */
1841
Gilles Peskine61b91d42018-06-08 16:09:36 +02001842psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
1843 psa_algorithm_t alg,
1844 const uint8_t *hash,
1845 size_t hash_length,
1846 const uint8_t *salt,
1847 size_t salt_length,
1848 uint8_t *signature,
1849 size_t signature_size,
1850 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01001851{
1852 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001853 psa_status_t status;
1854
1855 *signature_length = signature_size;
1856
Gilles Peskine93aa0332018-02-03 23:58:03 +01001857 (void) salt;
1858 (void) salt_length;
1859
Gilles Peskine828ed142018-06-18 23:25:51 +02001860 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001861 {
1862 status = PSA_ERROR_EMPTY_SLOT;
1863 goto exit;
1864 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001865 slot = &global_data.key_slots[key];
1866 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001867 {
1868 status = PSA_ERROR_EMPTY_SLOT;
1869 goto exit;
1870 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001871 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001872 {
1873 status = PSA_ERROR_INVALID_ARGUMENT;
1874 goto exit;
1875 }
Gilles Peskine61b91d42018-06-08 16:09:36 +02001876 if( ! ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001877 {
1878 status = PSA_ERROR_NOT_PERMITTED;
1879 goto exit;
1880 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001881
Gilles Peskine20035e32018-02-03 22:44:14 +01001882#if defined(MBEDTLS_RSA_C)
1883 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1884 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001885 status = psa_rsa_sign( slot->data.rsa,
1886 alg,
1887 hash, hash_length,
1888 signature, signature_size,
1889 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01001890 }
1891 else
1892#endif /* defined(MBEDTLS_RSA_C) */
1893#if defined(MBEDTLS_ECP_C)
1894 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1895 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001896#if defined(MBEDTLS_ECDSA_C)
1897 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001898 status = psa_ecdsa_sign( slot->data.ecp,
1899 alg,
1900 hash, hash_length,
1901 signature, signature_size,
1902 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001903 else
1904#endif /* defined(MBEDTLS_ECDSA_C) */
1905 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001906 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001907 }
itayzafrir5c753392018-05-08 11:18:38 +03001908 }
1909 else
1910#endif /* defined(MBEDTLS_ECP_C) */
1911 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001912 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01001913 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001914
1915exit:
1916 /* Fill the unused part of the output buffer (the whole buffer on error,
1917 * the trailing part on success) with something that isn't a valid mac
1918 * (barring an attack on the mac and deliberately-crafted input),
1919 * in case the caller doesn't check the return status properly. */
1920 if( status == PSA_SUCCESS )
1921 memset( signature + *signature_length, '!',
1922 signature_size - *signature_length );
1923 else
1924 memset( signature, '!', signature_size );
1925 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03001926}
1927
1928psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
1929 psa_algorithm_t alg,
1930 const uint8_t *hash,
1931 size_t hash_length,
1932 const uint8_t *salt,
1933 size_t salt_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02001934 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02001935 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03001936{
1937 key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02001938
itayzafrir5c753392018-05-08 11:18:38 +03001939 (void) salt;
1940 (void) salt_length;
1941
Gilles Peskine828ed142018-06-18 23:25:51 +02001942 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
itayzafrir5c753392018-05-08 11:18:38 +03001943 return( PSA_ERROR_INVALID_ARGUMENT );
1944 slot = &global_data.key_slots[key];
1945 if( slot->type == PSA_KEY_TYPE_NONE )
1946 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001947 if( ! ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) )
itayzafrir5c753392018-05-08 11:18:38 +03001948 return( PSA_ERROR_NOT_PERMITTED );
1949
Gilles Peskine61b91d42018-06-08 16:09:36 +02001950#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001951 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
1952 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001953 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02001954 return( psa_rsa_verify( slot->data.rsa,
1955 alg,
1956 hash, hash_length,
1957 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001958 }
1959 else
1960#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03001961#if defined(MBEDTLS_ECP_C)
1962 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1963 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001964#if defined(MBEDTLS_ECDSA_C)
1965 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001966 return( psa_ecdsa_verify( slot->data.ecp,
1967 hash, hash_length,
1968 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001969 else
1970#endif /* defined(MBEDTLS_ECDSA_C) */
1971 {
1972 return( PSA_ERROR_INVALID_ARGUMENT );
1973 }
itayzafrir5c753392018-05-08 11:18:38 +03001974 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001975 else
1976#endif /* defined(MBEDTLS_ECP_C) */
1977 {
1978 return( PSA_ERROR_NOT_SUPPORTED );
1979 }
1980}
1981
Gilles Peskine61b91d42018-06-08 16:09:36 +02001982psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
1983 psa_algorithm_t alg,
1984 const uint8_t *input,
1985 size_t input_length,
1986 const uint8_t *salt,
1987 size_t salt_length,
1988 uint8_t *output,
1989 size_t output_size,
1990 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001991{
1992 key_slot_t *slot;
1993 (void) salt;
1994 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001995 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001996
Gilles Peskine828ed142018-06-18 23:25:51 +02001997 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein7f5a3192018-05-06 22:26:54 +03001998 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001999 slot = &global_data.key_slots[key];
2000 if( slot->type == PSA_KEY_TYPE_NONE )
2001 return( PSA_ERROR_EMPTY_SLOT );
2002 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2003 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002004 if( ! ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) )
2005 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002006
2007#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002008 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
2009 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002010 {
2011 mbedtls_rsa_context *rsa = slot->data.rsa;
2012 int ret;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002013 if( output_size < rsa->len )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002014 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002015#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002016 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002017 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002018 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2019 mbedtls_ctr_drbg_random,
2020 &global_data.ctr_drbg,
2021 MBEDTLS_RSA_PUBLIC,
2022 input_length,
2023 input,
2024 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002025 }
2026 else
2027#endif /* MBEDTLS_PKCS1_V15 */
2028#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002029 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002030 {
2031 return( PSA_ERROR_NOT_SUPPORTED );
2032 }
2033 else
2034#endif /* MBEDTLS_PKCS1_V21 */
2035 {
2036 return( PSA_ERROR_INVALID_ARGUMENT );
2037 }
2038 if( ret == 0 )
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002039 *output_length = rsa->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002040 return( mbedtls_to_psa_error( ret ) );
2041 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002042 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002043#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002044 {
2045 return( PSA_ERROR_NOT_SUPPORTED );
2046 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002047}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002048
Gilles Peskine61b91d42018-06-08 16:09:36 +02002049psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2050 psa_algorithm_t alg,
2051 const uint8_t *input,
2052 size_t input_length,
2053 const uint8_t *salt,
2054 size_t salt_length,
2055 uint8_t *output,
2056 size_t output_size,
2057 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002058{
2059 key_slot_t *slot;
2060 (void) salt;
2061 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002062 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002063
Gilles Peskine828ed142018-06-18 23:25:51 +02002064 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002065 return( PSA_ERROR_EMPTY_SLOT );
2066 slot = &global_data.key_slots[key];
2067 if( slot->type == PSA_KEY_TYPE_NONE )
2068 return( PSA_ERROR_EMPTY_SLOT );
2069 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2070 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002071 if( ! ( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
2072 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002073
2074#if defined(MBEDTLS_RSA_C)
2075 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2076 {
2077 mbedtls_rsa_context *rsa = slot->data.rsa;
2078 int ret;
2079
Gilles Peskinec4def2f2018-06-08 17:53:48 +02002080 if( input_length != rsa->len )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002081 return( PSA_ERROR_INVALID_ARGUMENT );
2082
2083#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002084 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002085 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002086 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2087 mbedtls_ctr_drbg_random,
2088 &global_data.ctr_drbg,
2089 MBEDTLS_RSA_PRIVATE,
2090 output_length,
2091 input,
2092 output,
2093 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002094 }
2095 else
2096#endif /* MBEDTLS_PKCS1_V15 */
2097#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002098 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002099 {
2100 return( PSA_ERROR_NOT_SUPPORTED );
2101 }
2102 else
2103#endif /* MBEDTLS_PKCS1_V21 */
2104 {
2105 return( PSA_ERROR_INVALID_ARGUMENT );
2106 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002107
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002108 return( mbedtls_to_psa_error( ret ) );
2109 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002110 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002111#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002112 {
2113 return( PSA_ERROR_NOT_SUPPORTED );
2114 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002115}
Gilles Peskine20035e32018-02-03 22:44:14 +01002116
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002117
2118
mohammad1603503973b2018-03-12 15:59:30 +02002119/****************************************************************/
2120/* Symmetric cryptography */
2121/****************************************************************/
2122
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002123/* Initialize the cipher operation structure. Once this function has been
2124 * called, psa_cipher_abort can run and will do the right thing. */
2125static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2126 psa_algorithm_t alg )
2127{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002128 if( ! PSA_ALG_IS_CIPHER( alg ) )
2129 {
2130 memset( operation, 0, sizeof( *operation ) );
2131 return( PSA_ERROR_INVALID_ARGUMENT );
2132 }
2133
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002134 operation->alg = alg;
2135 operation->key_set = 0;
2136 operation->iv_set = 0;
2137 operation->iv_required = 1;
2138 operation->iv_size = 0;
2139 operation->block_size = 0;
2140 mbedtls_cipher_init( &operation->ctx.cipher );
2141 return( PSA_SUCCESS );
2142}
2143
Gilles Peskinee553c652018-06-04 16:22:46 +02002144static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2145 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002146 psa_algorithm_t alg,
2147 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002148{
2149 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2150 psa_status_t status;
2151 key_slot_t *slot;
2152 psa_key_type_t key_type;
2153 size_t key_bits;
2154 const mbedtls_cipher_info_t *cipher_info = NULL;
2155
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002156 status = psa_cipher_init( operation, alg );
2157 if( status != PSA_SUCCESS )
2158 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002159
2160 status = psa_get_key_information( key, &key_type, &key_bits );
2161 if( status != PSA_SUCCESS )
2162 return( status );
2163 slot = &global_data.key_slots[key];
2164
Gilles Peskinebb1072f2018-06-08 18:46:05 +02002165 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002166 if( cipher_info == NULL )
2167 return( PSA_ERROR_NOT_SUPPORTED );
2168
mohammad1603503973b2018-03-12 15:59:30 +02002169 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002170 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002171 {
2172 psa_cipher_abort( operation );
2173 return( mbedtls_to_psa_error( ret ) );
2174 }
2175
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002176#if defined(MBEDTLS_DES_C)
2177 if( key_type == PSA_KEY_TYPE_DES && key_bits == 128 )
2178 {
2179 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2180 unsigned char keys[24];
2181 memcpy( keys, slot->data.raw.data, 16 );
2182 memcpy( keys + 16, slot->data.raw.data, 8 );
2183 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2184 keys,
2185 192, cipher_operation );
2186 }
2187 else
2188#endif
2189 {
2190 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2191 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002192 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002193 }
Moran Peker41deec42018-04-04 15:43:05 +03002194 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002195 {
2196 psa_cipher_abort( operation );
2197 return( mbedtls_to_psa_error( ret ) );
2198 }
2199
mohammad16038481e742018-03-18 13:57:31 +02002200#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002201 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002202 {
Gilles Peskine53514202018-06-06 15:11:46 +02002203 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2204 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002205
Moran Pekera28258c2018-05-29 16:25:04 +03002206 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002207 {
2208 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2209 mode = MBEDTLS_PADDING_PKCS7;
2210 break;
2211 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2212 mode = MBEDTLS_PADDING_NONE;
2213 break;
2214 default:
Moran Pekerae382792018-05-31 14:06:17 +03002215 psa_cipher_abort( operation );
2216 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002217 }
2218 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002219 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002220 {
2221 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002222 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002223 }
mohammad16038481e742018-03-18 13:57:31 +02002224 }
2225#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2226
mohammad1603503973b2018-03-12 15:59:30 +02002227 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002228 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
2229 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) :
2230 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002231 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002232 {
mohammad160389e0f462018-04-12 08:48:45 +03002233 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
mohammad16038481e742018-03-18 13:57:31 +02002234 }
mohammad1603503973b2018-03-12 15:59:30 +02002235
Moran Peker395db872018-05-31 14:07:14 +03002236 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002237}
2238
Gilles Peskinee553c652018-06-04 16:22:46 +02002239psa_status_t psa_encrypt_setup( psa_cipher_operation_t *operation,
2240 psa_key_slot_t key,
2241 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002242{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002243 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002244}
2245
Gilles Peskinee553c652018-06-04 16:22:46 +02002246psa_status_t psa_decrypt_setup( psa_cipher_operation_t *operation,
2247 psa_key_slot_t key,
2248 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002249{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002250 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002251}
2252
Gilles Peskinee553c652018-06-04 16:22:46 +02002253psa_status_t psa_encrypt_generate_iv( psa_cipher_operation_t *operation,
2254 unsigned char *iv,
2255 size_t iv_size,
2256 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002257{
Moran Peker41deec42018-04-04 15:43:05 +03002258 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002259 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002260 return( PSA_ERROR_BAD_STATE );
2261 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002262 {
Moran Peker41deec42018-04-04 15:43:05 +03002263 ret = PSA_ERROR_BUFFER_TOO_SMALL;
2264 goto exit;
2265 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002266 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2267 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002268 if( ret != 0 )
2269 {
2270 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002271 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002272 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002273
mohammad16038481e742018-03-18 13:57:31 +02002274 *iv_length = operation->iv_size;
mohammad160389e0f462018-04-12 08:48:45 +03002275 ret = psa_encrypt_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002276
Moran Peker395db872018-05-31 14:07:14 +03002277exit:
2278 if( ret != PSA_SUCCESS )
2279 psa_cipher_abort( operation );
2280 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02002281}
2282
Gilles Peskinee553c652018-06-04 16:22:46 +02002283psa_status_t psa_encrypt_set_iv( psa_cipher_operation_t *operation,
2284 const unsigned char *iv,
2285 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002286{
Moran Peker41deec42018-04-04 15:43:05 +03002287 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002288 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002289 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03002290 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002291 {
Moran Pekerae382792018-05-31 14:06:17 +03002292 psa_cipher_abort( operation );
2293 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03002294 }
mohammad1603503973b2018-03-12 15:59:30 +02002295 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002296 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002297 {
2298 psa_cipher_abort( operation );
2299 return( mbedtls_to_psa_error( ret ) );
2300 }
2301
2302 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02002303
Moran Peker395db872018-05-31 14:07:14 +03002304 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002305}
2306
Gilles Peskinee553c652018-06-04 16:22:46 +02002307psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2308 const uint8_t *input,
2309 size_t input_length,
2310 unsigned char *output,
2311 size_t output_size,
2312 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002313{
2314 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002315 size_t expected_output_size;
2316 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2317 {
2318 /* Take the unprocessed partial block left over from previous
2319 * update calls, if any, plus the input to this call. Remove
2320 * the last partial block, if any. You get the data that will be
2321 * output in this call. */
2322 expected_output_size =
2323 ( operation->ctx.cipher.unprocessed_len + input_length )
2324 / operation->block_size * operation->block_size;
2325 }
2326 else
2327 {
2328 expected_output_size = input_length;
2329 }
2330 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03002331 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02002332
mohammad1603503973b2018-03-12 15:59:30 +02002333 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002334 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002335 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002336 {
2337 psa_cipher_abort( operation );
2338 return( mbedtls_to_psa_error( ret ) );
2339 }
2340
Moran Peker395db872018-05-31 14:07:14 +03002341 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002342}
2343
Gilles Peskinee553c652018-06-04 16:22:46 +02002344psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2345 uint8_t *output,
2346 size_t output_size,
2347 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002348{
2349 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002350 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002351
mohammad1603503973b2018-03-12 15:59:30 +02002352 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002353 {
Moran Peker7cb22b82018-06-05 11:40:02 +03002354 psa_cipher_abort( operation );
2355 return( PSA_ERROR_BAD_STATE );
2356 }
2357 if( operation->iv_required && ! operation->iv_set )
2358 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002359 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002360 return( PSA_ERROR_BAD_STATE );
2361 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002362 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2363 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002364 {
Gilles Peskine53514202018-06-06 15:11:46 +02002365 psa_algorithm_t padding_mode =
2366 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002367 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002368 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002369 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002370 return( PSA_ERROR_TAMPERING_DETECTED );
2371 }
Gilles Peskine53514202018-06-06 15:11:46 +02002372 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002373 {
2374 if( operation->ctx.cipher.unprocessed_len != 0 )
2375 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002376 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002377 return( PSA_ERROR_INVALID_ARGUMENT );
2378 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002379 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002380 }
2381
2382 ret = mbedtls_cipher_finish( &operation->ctx.cipher, temp_output_buffer,
Gilles Peskinee553c652018-06-04 16:22:46 +02002383 output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002384 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002385 {
2386 psa_cipher_abort( operation );
2387 return( mbedtls_to_psa_error( ret ) );
2388 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002389 if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002390 memcpy( output, temp_output_buffer, *output_length );
2391 else
2392 {
2393 psa_cipher_abort( operation );
2394 return( PSA_ERROR_BUFFER_TOO_SMALL );
2395 }
mohammad1603503973b2018-03-12 15:59:30 +02002396
Moran Peker4c80d832018-04-22 20:15:31 +03002397 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002398}
2399
Gilles Peskinee553c652018-06-04 16:22:46 +02002400psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2401{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002402 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002403 {
2404 /* The object has (apparently) been initialized but it is not
2405 * in use. It's ok to call abort on such an object, and there's
2406 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002407 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002408 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002409
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002410 /* Sanity check (shouldn't happen: operation->alg should
2411 * always have been initialized to a valid value). */
2412 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2413 return( PSA_ERROR_BAD_STATE );
2414
mohammad1603503973b2018-03-12 15:59:30 +02002415 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002416
Moran Peker41deec42018-04-04 15:43:05 +03002417 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002418 operation->key_set = 0;
2419 operation->iv_set = 0;
2420 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002421 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002422 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002423
Moran Peker395db872018-05-31 14:07:14 +03002424 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002425}
2426
Gilles Peskinea0655c32018-04-30 17:06:50 +02002427
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002428
mohammad16038cc1cee2018-03-28 01:21:33 +03002429/****************************************************************/
2430/* Key Policy */
2431/****************************************************************/
2432
Gilles Peskine2d277862018-06-18 15:41:12 +02002433void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002434{
Gilles Peskine803ce742018-06-18 16:07:14 +02002435 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002436}
2437
Gilles Peskine2d277862018-06-18 15:41:12 +02002438void psa_key_policy_set_usage( psa_key_policy_t *policy,
2439 psa_key_usage_t usage,
2440 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002441{
mohammad16034eed7572018-03-28 05:14:59 -07002442 policy->usage = usage;
2443 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002444}
2445
Gilles Peskine2d277862018-06-18 15:41:12 +02002446psa_key_usage_t psa_key_policy_get_usage( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002447{
mohammad16036df908f2018-04-02 08:34:15 -07002448 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002449}
2450
Gilles Peskine2d277862018-06-18 15:41:12 +02002451psa_algorithm_t psa_key_policy_get_algorithm( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002452{
mohammad16036df908f2018-04-02 08:34:15 -07002453 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002454}
2455
Gilles Peskine2d277862018-06-18 15:41:12 +02002456psa_status_t psa_set_key_policy( psa_key_slot_t key,
2457 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002458{
2459 key_slot_t *slot;
mohammad16038cc1cee2018-03-28 01:21:33 +03002460
Gilles Peskine828ed142018-06-18 23:25:51 +02002461 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002462 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002463
mohammad16038cc1cee2018-03-28 01:21:33 +03002464 slot = &global_data.key_slots[key];
2465 if( slot->type != PSA_KEY_TYPE_NONE )
2466 return( PSA_ERROR_OCCUPIED_SLOT );
2467
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002468 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2469 PSA_KEY_USAGE_ENCRYPT |
2470 PSA_KEY_USAGE_DECRYPT |
2471 PSA_KEY_USAGE_SIGN |
2472 PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002473 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002474
mohammad16036df908f2018-04-02 08:34:15 -07002475 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002476
2477 return( PSA_SUCCESS );
2478}
2479
Gilles Peskine2d277862018-06-18 15:41:12 +02002480psa_status_t psa_get_key_policy( psa_key_slot_t key,
2481 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002482{
2483 key_slot_t *slot;
2484
Gilles Peskine828ed142018-06-18 23:25:51 +02002485 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002486 return( PSA_ERROR_INVALID_ARGUMENT );
2487
2488 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002489
mohammad16036df908f2018-04-02 08:34:15 -07002490 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002491
2492 return( PSA_SUCCESS );
2493}
Gilles Peskine20035e32018-02-03 22:44:14 +01002494
Gilles Peskinea0655c32018-04-30 17:06:50 +02002495
2496
mohammad1603804cd712018-03-20 22:44:08 +02002497/****************************************************************/
2498/* Key Lifetime */
2499/****************************************************************/
2500
Gilles Peskine2d277862018-06-18 15:41:12 +02002501psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2502 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002503{
2504 key_slot_t *slot;
2505
Gilles Peskine828ed142018-06-18 23:25:51 +02002506 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002507 return( PSA_ERROR_INVALID_ARGUMENT );
2508
2509 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002510
mohammad1603804cd712018-03-20 22:44:08 +02002511 *lifetime = slot->lifetime;
2512
2513 return( PSA_SUCCESS );
2514}
2515
Gilles Peskine2d277862018-06-18 15:41:12 +02002516psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002517 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002518{
2519 key_slot_t *slot;
2520
Gilles Peskine828ed142018-06-18 23:25:51 +02002521 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002522 return( PSA_ERROR_INVALID_ARGUMENT );
2523
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002524 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2525 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002526 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2527 return( PSA_ERROR_INVALID_ARGUMENT );
2528
mohammad1603804cd712018-03-20 22:44:08 +02002529 slot = &global_data.key_slots[key];
mohammad16035d7ec202018-03-28 01:29:41 +03002530 if( slot->type != PSA_KEY_TYPE_NONE )
2531 return( PSA_ERROR_OCCUPIED_SLOT );
mohammad1603804cd712018-03-20 22:44:08 +02002532
Moran Pekerd7326592018-05-29 16:56:39 +03002533 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002534 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002535
mohammad1603060ad8a2018-03-20 14:28:38 -07002536 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002537
2538 return( PSA_SUCCESS );
2539}
2540
Gilles Peskine20035e32018-02-03 22:44:14 +01002541
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002542
mohammad16035955c982018-04-26 00:53:03 +03002543/****************************************************************/
2544/* AEAD */
2545/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002546
mohammad16035955c982018-04-26 00:53:03 +03002547psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2548 psa_algorithm_t alg,
2549 const uint8_t *nonce,
2550 size_t nonce_length,
2551 const uint8_t *additional_data,
2552 size_t additional_data_length,
2553 const uint8_t *plaintext,
2554 size_t plaintext_length,
2555 uint8_t *ciphertext,
2556 size_t ciphertext_size,
2557 size_t *ciphertext_length )
2558{
2559 int ret;
2560 psa_status_t status;
2561 key_slot_t *slot;
2562 psa_key_type_t key_type;
2563 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002564 uint8_t *tag;
2565 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002566 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002567 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002568
mohammad1603f08a5502018-06-03 15:05:47 +03002569 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002570
mohammad16035955c982018-04-26 00:53:03 +03002571 status = psa_get_key_information( key, &key_type, &key_bits );
2572 if( status != PSA_SUCCESS )
2573 return( status );
2574 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002575 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002576 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002577
mohammad16035ed06212018-06-06 13:09:34 +03002578 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2579 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002580 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002581 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002582
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002583 if( ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) == 0 )
mohammad1603f14394b2018-06-04 14:33:19 +03002584 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002585
Gilles Peskine2d277862018-06-18 15:41:12 +02002586 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2587 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002588 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002589
mohammad16035955c982018-04-26 00:53:03 +03002590 if( alg == PSA_ALG_GCM )
2591 {
2592 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002593 tag_length = 16;
2594
mohammad160396910d82018-06-04 14:33:00 +03002595 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2596 return( PSA_ERROR_INVALID_ARGUMENT );
2597
mohammad160315223a82018-06-03 17:19:55 +03002598 //make sure we have place to hold the tag in the ciphertext buffer
2599 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002600 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002601
2602 //update the tag pointer to point to the end of the ciphertext_length
2603 tag = ciphertext + plaintext_length;
2604
mohammad16035955c982018-04-26 00:53:03 +03002605 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002606 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002607 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002608 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002609 if( ret != 0 )
2610 {
2611 mbedtls_gcm_free( &gcm );
2612 return( mbedtls_to_psa_error( ret ) );
2613 }
2614 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002615 plaintext_length, nonce,
2616 nonce_length, additional_data,
2617 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002618 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002619 mbedtls_gcm_free( &gcm );
2620 }
2621 else if( alg == PSA_ALG_CCM )
2622 {
2623 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002624 tag_length = 16;
2625
mohammad160396910d82018-06-04 14:33:00 +03002626 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2627 return( PSA_ERROR_INVALID_ARGUMENT );
2628
mohammad160347ddf3d2018-04-26 01:11:21 +03002629 if( nonce_length < 7 || nonce_length > 13 )
2630 return( PSA_ERROR_INVALID_ARGUMENT );
2631
mohammad160315223a82018-06-03 17:19:55 +03002632 //make sure we have place to hold the tag in the ciphertext buffer
2633 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002634 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002635
2636 //update the tag pointer to point to the end of the ciphertext_length
2637 tag = ciphertext + plaintext_length;
2638
mohammad16035955c982018-04-26 00:53:03 +03002639 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002640 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002641 slot->data.raw.data,
2642 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002643 if( ret != 0 )
2644 {
2645 mbedtls_ccm_free( &ccm );
2646 return( mbedtls_to_psa_error( ret ) );
2647 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002648 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002649 nonce, nonce_length,
2650 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002651 additional_data_length,
2652 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002653 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002654 mbedtls_ccm_free( &ccm );
2655 }
mohammad16035c8845f2018-05-09 05:40:09 -07002656 else
2657 {
mohammad1603554faad2018-06-03 15:07:38 +03002658 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002659 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002660
mohammad160315223a82018-06-03 17:19:55 +03002661 if( ret != 0 )
2662 {
2663 memset( ciphertext, 0, ciphertext_size );
2664 return( mbedtls_to_psa_error( ret ) );
2665 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002666
mohammad160315223a82018-06-03 17:19:55 +03002667 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002668 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002669}
2670
Gilles Peskineee652a32018-06-01 19:23:52 +02002671/* Locate the tag in a ciphertext buffer containing the encrypted data
2672 * followed by the tag. Return the length of the part preceding the tag in
2673 * *plaintext_length. This is the size of the plaintext in modes where
2674 * the encrypted data has the same size as the plaintext, such as
2675 * CCM and GCM. */
2676static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2677 const uint8_t *ciphertext,
2678 size_t ciphertext_length,
2679 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002680 const uint8_t **p_tag )
2681{
2682 size_t payload_length;
2683 if( tag_length > ciphertext_length )
2684 return( PSA_ERROR_INVALID_ARGUMENT );
2685 payload_length = ciphertext_length - tag_length;
2686 if( payload_length > plaintext_size )
2687 return( PSA_ERROR_BUFFER_TOO_SMALL );
2688 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002689 return( PSA_SUCCESS );
2690}
2691
mohammad16035955c982018-04-26 00:53:03 +03002692psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2693 psa_algorithm_t alg,
2694 const uint8_t *nonce,
2695 size_t nonce_length,
2696 const uint8_t *additional_data,
2697 size_t additional_data_length,
2698 const uint8_t *ciphertext,
2699 size_t ciphertext_length,
2700 uint8_t *plaintext,
2701 size_t plaintext_size,
2702 size_t *plaintext_length )
2703{
2704 int ret;
2705 psa_status_t status;
2706 key_slot_t *slot;
2707 psa_key_type_t key_type;
2708 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002709 const uint8_t *tag;
2710 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002711 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002712 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002713
Gilles Peskineee652a32018-06-01 19:23:52 +02002714 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002715
mohammad16035955c982018-04-26 00:53:03 +03002716 status = psa_get_key_information( key, &key_type, &key_bits );
2717 if( status != PSA_SUCCESS )
2718 return( status );
2719 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002720 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002721 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002722
mohammad16035ed06212018-06-06 13:09:34 +03002723 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2724 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002725 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002726 return( PSA_ERROR_NOT_SUPPORTED );
2727
mohammad1603f14394b2018-06-04 14:33:19 +03002728 if( !( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
2729 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002730
Gilles Peskine2d277862018-06-18 15:41:12 +02002731 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2732 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002733 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002734
mohammad16035955c982018-04-26 00:53:03 +03002735 if( alg == PSA_ALG_GCM )
2736 {
2737 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002738
Gilles Peskineee652a32018-06-01 19:23:52 +02002739 tag_length = 16;
2740 status = psa_aead_unpadded_locate_tag( tag_length,
2741 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002742 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002743 if( status != PSA_SUCCESS )
2744 return( status );
2745
mohammad16035955c982018-04-26 00:53:03 +03002746 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002747 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002748 slot->data.raw.data,
2749 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002750 if( ret != 0 )
2751 {
2752 mbedtls_gcm_free( &gcm );
2753 return( mbedtls_to_psa_error( ret ) );
2754 }
mohammad16035955c982018-04-26 00:53:03 +03002755
Gilles Peskineee652a32018-06-01 19:23:52 +02002756 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002757 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002758 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002759 additional_data,
2760 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002761 tag, tag_length,
2762 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002763 mbedtls_gcm_free( &gcm );
2764 }
2765 else if( alg == PSA_ALG_CCM )
2766 {
2767 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002768
mohammad160347ddf3d2018-04-26 01:11:21 +03002769 if( nonce_length < 7 || nonce_length > 13 )
2770 return( PSA_ERROR_INVALID_ARGUMENT );
2771
mohammad16039375f842018-06-03 14:28:24 +03002772 tag_length = 16;
2773 status = psa_aead_unpadded_locate_tag( tag_length,
2774 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002775 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002776 if( status != PSA_SUCCESS )
2777 return( status );
2778
mohammad16035955c982018-04-26 00:53:03 +03002779 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002780 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002781 slot->data.raw.data,
2782 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002783 if( ret != 0 )
2784 {
2785 mbedtls_ccm_free( &ccm );
2786 return( mbedtls_to_psa_error( ret ) );
2787 }
mohammad160360a64d02018-06-03 17:20:42 +03002788 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002789 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002790 additional_data,
2791 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002792 ciphertext, plaintext,
2793 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002794 mbedtls_ccm_free( &ccm );
2795 }
mohammad160339574652018-06-01 04:39:53 -07002796 else
2797 {
mohammad1603554faad2018-06-03 15:07:38 +03002798 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07002799 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002800
Gilles Peskineee652a32018-06-01 19:23:52 +02002801 if( ret != 0 )
mohammad160360a64d02018-06-03 17:20:42 +03002802 memset( plaintext, 0, plaintext_size );
2803 else
2804 *plaintext_length = ciphertext_length - tag_length;
2805
Gilles Peskineee652a32018-06-01 19:23:52 +02002806 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03002807}
2808
Gilles Peskinea0655c32018-04-30 17:06:50 +02002809
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002810
Gilles Peskine20035e32018-02-03 22:44:14 +01002811/****************************************************************/
Gilles Peskine05d69892018-06-19 22:00:52 +02002812/* Key generation */
2813/****************************************************************/
2814
2815psa_status_t psa_generate_random( uint8_t *output,
2816 size_t output_size )
2817{
2818 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2819 output, output_size );
2820 return( mbedtls_to_psa_error( ret ) );
2821}
2822
2823psa_status_t psa_generate_key( psa_key_slot_t key,
2824 psa_key_type_t type,
2825 size_t bits,
2826 const void *parameters,
2827 size_t parameters_size )
2828{
Gilles Peskine12313cd2018-06-20 00:20:32 +02002829 key_slot_t *slot;
2830
2831 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
2832 return( PSA_ERROR_INVALID_ARGUMENT );
2833 slot = &global_data.key_slots[key];
2834 if( slot->type != PSA_KEY_TYPE_NONE )
2835 return( PSA_ERROR_OCCUPIED_SLOT );
2836 if( parameters == NULL && parameters_size != 0 )
2837 return( PSA_ERROR_INVALID_ARGUMENT );
2838
Gilles Peskine48c0ea12018-06-21 14:15:31 +02002839 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002840 {
2841 psa_status_t status = prepare_raw_data_slot( type, bits,
2842 &slot->data.raw );
2843 if( status != PSA_SUCCESS )
2844 return( status );
2845 status = psa_generate_random( slot->data.raw.data,
2846 slot->data.raw.bytes );
2847 if( status != PSA_SUCCESS )
2848 {
2849 mbedtls_free( slot->data.raw.data );
2850 return( status );
2851 }
2852#if defined(MBEDTLS_DES_C)
2853 if( type == PSA_KEY_TYPE_DES )
2854 {
2855 mbedtls_des_key_set_parity( slot->data.raw.data );
2856 if( slot->data.raw.bytes >= 16 )
2857 mbedtls_des_key_set_parity( slot->data.raw.data + 8 );
2858 if( slot->data.raw.bytes == 24 )
2859 mbedtls_des_key_set_parity( slot->data.raw.data + 16 );
2860 }
2861#endif /* MBEDTLS_DES_C */
2862 }
2863 else
2864
2865#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
2866 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2867 {
2868 mbedtls_rsa_context *rsa;
2869 int ret;
2870 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02002871 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
2872 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine12313cd2018-06-20 00:20:32 +02002873 if( parameters != NULL )
2874 {
2875 const unsigned *p = parameters;
2876 if( parameters_size != sizeof( *p ) )
2877 return( PSA_ERROR_INVALID_ARGUMENT );
2878 if( *p > INT_MAX )
2879 return( PSA_ERROR_INVALID_ARGUMENT );
2880 exponent = *p;
2881 }
2882 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
2883 if( rsa == NULL )
2884 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2885 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
2886 ret = mbedtls_rsa_gen_key( rsa,
2887 mbedtls_ctr_drbg_random,
2888 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002889 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02002890 exponent );
2891 if( ret != 0 )
2892 {
2893 mbedtls_rsa_free( rsa );
2894 mbedtls_free( rsa );
2895 return( mbedtls_to_psa_error( ret ) );
2896 }
2897 slot->data.rsa = rsa;
2898 }
2899 else
2900#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
2901
2902#if defined(MBEDTLS_ECP_C)
2903 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
2904 {
2905 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
2906 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
2907 const mbedtls_ecp_curve_info *curve_info =
2908 mbedtls_ecp_curve_info_from_grp_id( grp_id );
2909 mbedtls_ecp_keypair *ecp;
2910 int ret;
2911 if( parameters != NULL )
2912 return( PSA_ERROR_NOT_SUPPORTED );
2913 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
2914 return( PSA_ERROR_NOT_SUPPORTED );
2915 if( curve_info->bit_size != bits )
2916 return( PSA_ERROR_INVALID_ARGUMENT );
2917 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
2918 if( ecp == NULL )
2919 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2920 mbedtls_ecp_keypair_init( ecp );
2921 ret = mbedtls_ecp_gen_key( grp_id, ecp,
2922 mbedtls_ctr_drbg_random,
2923 &global_data.ctr_drbg );
2924 if( ret != 0 )
2925 {
2926 mbedtls_ecp_keypair_free( ecp );
2927 mbedtls_free( ecp );
2928 return( mbedtls_to_psa_error( ret ) );
2929 }
2930 slot->data.ecp = ecp;
2931 }
2932 else
2933#endif /* MBEDTLS_ECP_C */
2934
2935 return( PSA_ERROR_NOT_SUPPORTED );
2936
2937 slot->type = type;
2938 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02002939}
2940
2941
2942/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002943/* Module setup */
2944/****************************************************************/
2945
Gilles Peskinee59236f2018-01-27 23:32:46 +01002946void mbedtls_psa_crypto_free( void )
2947{
Jaeden Amero045bd502018-06-26 14:00:08 +01002948 psa_key_slot_t key;
Gilles Peskine828ed142018-06-18 23:25:51 +02002949 for( key = 1; key < PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002950 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002951 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
2952 mbedtls_entropy_free( &global_data.entropy );
2953 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2954}
2955
2956psa_status_t psa_crypto_init( void )
2957{
2958 int ret;
2959 const unsigned char drbg_seed[] = "PSA";
2960
2961 if( global_data.initialized != 0 )
2962 return( PSA_SUCCESS );
2963
2964 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2965 mbedtls_entropy_init( &global_data.entropy );
2966 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
2967
2968 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
2969 mbedtls_entropy_func,
2970 &global_data.entropy,
2971 drbg_seed, sizeof( drbg_seed ) - 1 );
2972 if( ret != 0 )
2973 goto exit;
2974
Gilles Peskinee4ebc122018-03-07 14:16:44 +01002975 global_data.initialized = 1;
2976
Gilles Peskinee59236f2018-01-27 23:32:46 +01002977exit:
2978 if( ret != 0 )
2979 mbedtls_psa_crypto_free( );
2980 return( mbedtls_to_psa_error( ret ) );
2981}
2982
2983#endif /* MBEDTLS_PSA_CRYPTO_C */