blob: dc0a27d6b0dcc19b407417cb4ffcc5b38f8b66dc [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
mohammad1603f4f0d612018-06-03 15:04:51 +03001166 return( mbedtls_cipher_info_from_values( cipher_id_tmp, key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001167}
1168
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001169static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001170{
Gilles Peskine2d277862018-06-18 15:41:12 +02001171 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001172 {
1173 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001174 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001175 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001176 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001177 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001178 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001179 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001180 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001181 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001182 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001183 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001184 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001185 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001186 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001187 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001188 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001189 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001190 return( 128 );
1191 default:
1192 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001193 }
1194}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001195
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001196/* Initialize the MAC operation structure. Once this function has been
1197 * called, psa_mac_abort can run and will do the right thing. */
1198static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1199 psa_algorithm_t alg )
1200{
1201 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1202
1203 operation->alg = alg;
1204 operation->key_set = 0;
1205 operation->iv_set = 0;
1206 operation->iv_required = 0;
1207 operation->has_input = 0;
1208 operation->key_usage_sign = 0;
1209 operation->key_usage_verify = 0;
1210
1211#if defined(MBEDTLS_CMAC_C)
1212 if( alg == PSA_ALG_CMAC )
1213 {
1214 operation->iv_required = 0;
1215 mbedtls_cipher_init( &operation->ctx.cmac );
1216 status = PSA_SUCCESS;
1217 }
1218 else
1219#endif /* MBEDTLS_CMAC_C */
1220#if defined(MBEDTLS_MD_C)
1221 if( PSA_ALG_IS_HMAC( operation->alg ) )
1222 {
1223 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1224 PSA_ALG_HMAC_HASH( alg ) );
1225 }
1226 else
1227#endif /* MBEDTLS_MD_C */
1228 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001229 if( ! PSA_ALG_IS_MAC( alg ) )
1230 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001231 }
1232
1233 if( status != PSA_SUCCESS )
1234 memset( operation, 0, sizeof( *operation ) );
1235 return( status );
1236}
1237
Gilles Peskine8c9def32018-02-08 10:02:12 +01001238psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1239{
1240 switch( operation->alg )
1241 {
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001242 case 0:
Gilles Peskine81736312018-06-26 15:04:31 +02001243 /* The object has (apparently) been initialized but it is not
1244 * in use. It's ok to call abort on such an object, and there's
1245 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001246 return( PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001247#if defined(MBEDTLS_CMAC_C)
1248 case PSA_ALG_CMAC:
1249 mbedtls_cipher_free( &operation->ctx.cmac );
1250 break;
1251#endif /* MBEDTLS_CMAC_C */
1252 default:
1253#if defined(MBEDTLS_MD_C)
1254 if( PSA_ALG_IS_HMAC( operation->alg ) )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001255 {
Jaeden Amero5390f692018-06-26 14:18:50 +01001256 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001257 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001258
Gilles Peskine99bc6492018-06-11 17:13:00 +02001259 if( block_size == 0 )
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001260 return( PSA_ERROR_NOT_SUPPORTED );
1261
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001262 psa_hash_abort( &operation->ctx.hmac.hash_ctx );
Gilles Peskine2d277862018-06-18 15:41:12 +02001263 mbedtls_zeroize( operation->ctx.hmac.opad, block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001264 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001265 else
1266#endif /* MBEDTLS_MD_C */
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001267 {
1268 /* Sanity check (shouldn't happen: operation->alg should
1269 * always have been initialized to a valid value). */
1270 return( PSA_ERROR_BAD_STATE );
1271 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001272 }
Moran Peker41deec42018-04-04 15:43:05 +03001273
Gilles Peskine8c9def32018-02-08 10:02:12 +01001274 operation->alg = 0;
1275 operation->key_set = 0;
1276 operation->iv_set = 0;
1277 operation->iv_required = 0;
1278 operation->has_input = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001279 operation->key_usage_sign = 0;
1280 operation->key_usage_verify = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001281
Gilles Peskine8c9def32018-02-08 10:02:12 +01001282 return( PSA_SUCCESS );
1283}
1284
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001285#if defined(MBEDTLS_CMAC_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001286static int psa_cmac_start( psa_mac_operation_t *operation,
1287 size_t key_bits,
1288 key_slot_t *slot,
1289 const mbedtls_cipher_info_t *cipher_info )
1290{
1291 int ret;
1292
1293 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001294
1295 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1296 if( ret != 0 )
1297 return( ret );
1298
1299 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1300 slot->data.raw.data,
1301 key_bits );
1302 return( ret );
1303}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001304#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001305
Gilles Peskine248051a2018-06-20 16:09:38 +02001306#if defined(MBEDTLS_MD_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001307static int psa_hmac_start( psa_mac_operation_t *operation,
1308 psa_key_type_t key_type,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001309 key_slot_t *slot,
1310 psa_algorithm_t alg )
1311{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001312 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001313 unsigned char *opad = operation->ctx.hmac.opad;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001314 size_t i;
Gilles Peskinee1bc6802018-06-11 17:36:05 +02001315 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001316 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001317 unsigned int digest_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001318 PSA_HASH_SIZE( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001319 size_t key_length = slot->data.raw.bytes;
1320 psa_status_t status;
1321
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001322 if( block_size == 0 || digest_size == 0 )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001323 return( PSA_ERROR_NOT_SUPPORTED );
1324
1325 if( key_type != PSA_KEY_TYPE_HMAC )
1326 return( PSA_ERROR_INVALID_ARGUMENT );
1327
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001328 operation->mac_size = digest_size;
1329
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001330 /* The hash was started earlier in psa_mac_init. */
Gilles Peskined223b522018-06-11 18:12:58 +02001331 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001332 {
1333 status = psa_hash_update( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001334 slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001335 if( status != PSA_SUCCESS )
1336 return( status );
1337 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001338 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001339 if( status != PSA_SUCCESS )
1340 return( status );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001341 }
Gilles Peskined223b522018-06-11 18:12:58 +02001342 else
1343 memcpy( ipad, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001344
Gilles Peskined223b522018-06-11 18:12:58 +02001345 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1346 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001347 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001348 ipad[i] ^= 0x36;
1349 memset( ipad + key_length, 0x36, block_size - key_length );
1350
1351 /* Copy the key material from ipad to opad, flipping the requisite bits,
1352 * and filling the rest of opad with the requisite constant. */
1353 for( i = 0; i < key_length; i++ )
1354 opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1355 memset( opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001356
1357 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1358 PSA_ALG_HMAC_HASH( alg ) );
1359 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001360 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001361
1362 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, ipad,
1363 block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001364
1365cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001366 mbedtls_zeroize( ipad, key_length );
1367 /* opad is in the context. It needs to stay in memory if this function
1368 * succeeds, and it will be wiped by psa_mac_abort() called from
1369 * psa_mac_start in the error case. */
1370
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001371 return( status );
1372}
Gilles Peskine248051a2018-06-20 16:09:38 +02001373#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001374
Gilles Peskine8c9def32018-02-08 10:02:12 +01001375psa_status_t psa_mac_start( psa_mac_operation_t *operation,
1376 psa_key_slot_t key,
1377 psa_algorithm_t alg )
1378{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001379 psa_status_t status;
1380 key_slot_t *slot;
1381 psa_key_type_t key_type;
1382 size_t key_bits;
Gilles Peskine828ed142018-06-18 23:25:51 +02001383 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001384
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001385 status = psa_mac_init( operation, alg );
1386 if( status != PSA_SUCCESS )
1387 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001388
1389 status = psa_get_key_information( key, &key_type, &key_bits );
1390 if( status != PSA_SUCCESS )
1391 return( status );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001392
Gilles Peskine8c9def32018-02-08 10:02:12 +01001393 slot = &global_data.key_slots[key];
Gilles Peskine99bc6492018-06-11 17:13:00 +02001394 if( slot->type == PSA_KEY_TYPE_NONE )
1395 return( PSA_ERROR_EMPTY_SLOT );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001396
Moran Pekerd7326592018-05-29 16:56:39 +03001397 if( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001398 operation->key_usage_sign = 1;
1399
Moran Pekerd7326592018-05-29 16:56:39 +03001400 if( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001401 operation->key_usage_verify = 1;
1402
Gilles Peskine8c9def32018-02-08 10:02:12 +01001403 if( ! PSA_ALG_IS_HMAC( alg ) )
1404 {
mohammad1603f4f0d612018-06-03 15:04:51 +03001405 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001406 if( cipher_info == NULL )
1407 return( PSA_ERROR_NOT_SUPPORTED );
1408 operation->mac_size = cipher_info->block_size;
1409 }
1410 switch( alg )
1411 {
1412#if defined(MBEDTLS_CMAC_C)
1413 case PSA_ALG_CMAC:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001414 status = mbedtls_to_psa_error( psa_cmac_start( operation,
1415 key_bits,
1416 slot,
1417 cipher_info ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001418 break;
1419#endif /* MBEDTLS_CMAC_C */
1420 default:
1421#if defined(MBEDTLS_MD_C)
1422 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskined223b522018-06-11 18:12:58 +02001423 status = psa_hmac_start( operation, key_type, slot, alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001424 else
1425#endif /* MBEDTLS_MD_C */
1426 return( PSA_ERROR_NOT_SUPPORTED );
1427 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001428
Gilles Peskine8c9def32018-02-08 10:02:12 +01001429 /* If we reach this point, then the algorithm-specific part of the
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001430 * context may contain data that needs to be wiped on error. */
1431 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001432 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001433 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001434 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001435 else
1436 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001437 operation->key_set = 1;
1438 }
1439 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001440}
1441
1442psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1443 const uint8_t *input,
1444 size_t input_length )
1445{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001446 int ret = 0 ;
1447 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001448 if( ! operation->key_set )
1449 return( PSA_ERROR_BAD_STATE );
1450 if( operation->iv_required && ! operation->iv_set )
1451 return( PSA_ERROR_BAD_STATE );
1452 operation->has_input = 1;
1453
1454 switch( operation->alg )
1455 {
1456#if defined(MBEDTLS_CMAC_C)
1457 case PSA_ALG_CMAC:
1458 ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1459 input, input_length );
1460 break;
1461#endif /* MBEDTLS_CMAC_C */
1462 default:
1463#if defined(MBEDTLS_MD_C)
1464 if( PSA_ALG_IS_HMAC( operation->alg ) )
1465 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001466 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
Gilles Peskine2d277862018-06-18 15:41:12 +02001467 input_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001468 }
1469 else
1470#endif /* MBEDTLS_MD_C */
1471 {
1472 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1473 }
1474 break;
1475 }
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001476 if( ret != 0 || status != PSA_SUCCESS )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001477 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001478 psa_mac_abort( operation );
Gilles Peskine2d277862018-06-18 15:41:12 +02001479 if( ret != 0 )
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001480 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001481 }
1482
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001483 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001484}
1485
mohammad16036df908f2018-04-02 08:34:15 -07001486static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001487 uint8_t *mac,
1488 size_t mac_size,
1489 size_t *mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001490{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001491 int ret = 0;
1492 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001493 if( ! operation->key_set )
1494 return( PSA_ERROR_BAD_STATE );
1495 if( operation->iv_required && ! operation->iv_set )
1496 return( PSA_ERROR_BAD_STATE );
1497
1498 /* Fill the output buffer with something that isn't a valid mac
1499 * (barring an attack on the mac and deliberately-crafted input),
1500 * in case the caller doesn't check the return status properly. */
1501 *mac_length = operation->mac_size;
1502 memset( mac, '!', mac_size );
1503
1504 if( mac_size < operation->mac_size )
1505 return( PSA_ERROR_BUFFER_TOO_SMALL );
1506
1507 switch( operation->alg )
1508 {
1509#if defined(MBEDTLS_CMAC_C)
1510 case PSA_ALG_CMAC:
1511 ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1512 break;
1513#endif /* MBEDTLS_CMAC_C */
1514 default:
1515#if defined(MBEDTLS_MD_C)
1516 if( PSA_ALG_IS_HMAC( operation->alg ) )
1517 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001518 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001519 unsigned char *opad = operation->ctx.hmac.opad;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001520 size_t hash_size = 0;
Nir Sonnenschein96272412018-06-17 14:41:10 +03001521 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001522 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001523
Gilles Peskine99bc6492018-06-11 17:13:00 +02001524 if( block_size == 0 )
1525 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001526
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001527 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine99bc6492018-06-11 17:13:00 +02001528 sizeof( tmp ), &hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001529 if( status != PSA_SUCCESS )
1530 goto cleanup;
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001531 /* From here on, tmp needs to be wiped. */
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001532
1533 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1534 PSA_ALG_HMAC_HASH( operation->alg ) );
1535 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001536 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001537
1538 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, opad,
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001539 block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001540 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001541 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001542
1543 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine2d277862018-06-18 15:41:12 +02001544 hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001545 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001546 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001547
1548 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, mac,
1549 mac_size, mac_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001550 hmac_cleanup:
1551 mbedtls_zeroize( tmp, hash_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001552 }
1553 else
1554#endif /* MBEDTLS_MD_C */
1555 {
1556 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1557 }
1558 break;
1559 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001560cleanup:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001561
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001562 if( ret == 0 && status == PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001563 {
1564 return( psa_mac_abort( operation ) );
1565 }
1566 else
1567 {
1568 psa_mac_abort( operation );
Gilles Peskine99bc6492018-06-11 17:13:00 +02001569 if( ret != 0 )
Gilles Peskine2d277862018-06-18 15:41:12 +02001570 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001571
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001572 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001573 }
1574}
1575
mohammad16036df908f2018-04-02 08:34:15 -07001576psa_status_t psa_mac_finish( psa_mac_operation_t *operation,
1577 uint8_t *mac,
1578 size_t mac_size,
1579 size_t *mac_length )
1580{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001581 if( ! operation->key_usage_sign )
mohammad16036df908f2018-04-02 08:34:15 -07001582 return( PSA_ERROR_NOT_PERMITTED );
1583
Gilles Peskine99bc6492018-06-11 17:13:00 +02001584 return( psa_mac_finish_internal( operation, mac,
1585 mac_size, mac_length ) );
mohammad16036df908f2018-04-02 08:34:15 -07001586}
1587
Gilles Peskine8c9def32018-02-08 10:02:12 +01001588psa_status_t psa_mac_verify( psa_mac_operation_t *operation,
1589 const uint8_t *mac,
1590 size_t mac_length )
1591{
Gilles Peskine828ed142018-06-18 23:25:51 +02001592 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Gilles Peskine8c9def32018-02-08 10:02:12 +01001593 size_t actual_mac_length;
mohammad16036df908f2018-04-02 08:34:15 -07001594 psa_status_t status;
1595
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001596 if( ! operation->key_usage_verify )
mohammad16036df908f2018-04-02 08:34:15 -07001597 return( PSA_ERROR_NOT_PERMITTED );
1598
1599 status = psa_mac_finish_internal( operation,
1600 actual_mac, sizeof( actual_mac ),
1601 &actual_mac_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001602 if( status != PSA_SUCCESS )
1603 return( status );
1604 if( actual_mac_length != mac_length )
1605 return( PSA_ERROR_INVALID_SIGNATURE );
1606 if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
1607 return( PSA_ERROR_INVALID_SIGNATURE );
1608 return( PSA_SUCCESS );
1609}
1610
1611
Gilles Peskine20035e32018-02-03 22:44:14 +01001612
Gilles Peskine20035e32018-02-03 22:44:14 +01001613/****************************************************************/
1614/* Asymmetric cryptography */
1615/****************************************************************/
1616
Gilles Peskine2b450e32018-06-27 15:42:46 +02001617#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001618/* Decode the hash algorithm from alg and store the mbedtls encoding in
1619 * md_alg. Verify that the hash length is consistent. */
1620static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1621 size_t hash_length,
1622 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001623{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001624 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001625 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1626 *md_alg = hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1627 if( *md_alg == MBEDTLS_MD_NONE )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001628 {
1629#if SIZE_MAX > UINT_MAX
Gilles Peskine61b91d42018-06-08 16:09:36 +02001630 if( hash_length > UINT_MAX )
1631 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001632#endif
1633 }
1634 else
1635 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001636 if( mbedtls_md_get_size( md_info ) != hash_length )
1637 return( PSA_ERROR_INVALID_ARGUMENT );
1638 if( md_info == NULL )
1639 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001640 }
Gilles Peskine61b91d42018-06-08 16:09:36 +02001641 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001642}
1643
Gilles Peskine2b450e32018-06-27 15:42:46 +02001644static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1645 psa_algorithm_t alg,
1646 const uint8_t *hash,
1647 size_t hash_length,
1648 uint8_t *signature,
1649 size_t signature_size,
1650 size_t *signature_length )
1651{
1652 psa_status_t status;
1653 int ret;
1654 mbedtls_md_type_t md_alg;
1655
1656 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1657 if( status != PSA_SUCCESS )
1658 return( status );
1659
1660 if( signature_size < rsa->len )
1661 return( PSA_ERROR_BUFFER_TOO_SMALL );
1662
1663#if defined(MBEDTLS_PKCS1_V15)
1664 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1665 {
1666 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1667 MBEDTLS_MD_NONE );
1668 ret = mbedtls_rsa_pkcs1_sign( rsa,
1669 mbedtls_ctr_drbg_random,
1670 &global_data.ctr_drbg,
1671 MBEDTLS_RSA_PRIVATE,
1672 md_alg, hash_length, hash,
1673 signature );
1674 }
1675 else
1676#endif /* MBEDTLS_PKCS1_V15 */
1677#if defined(MBEDTLS_PKCS1_V21)
1678 if( PSA_ALG_IS_RSA_PSS( alg ) )
1679 {
1680 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1681 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1682 mbedtls_ctr_drbg_random,
1683 &global_data.ctr_drbg,
1684 MBEDTLS_RSA_PRIVATE,
1685 md_alg, hash_length, hash,
1686 signature );
1687 }
1688 else
1689#endif /* MBEDTLS_PKCS1_V21 */
1690 {
1691 return( PSA_ERROR_INVALID_ARGUMENT );
1692 }
1693
1694 if( ret == 0 )
1695 *signature_length = rsa->len;
1696 return( mbedtls_to_psa_error( ret ) );
1697}
1698
1699static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1700 psa_algorithm_t alg,
1701 const uint8_t *hash,
1702 size_t hash_length,
1703 const uint8_t *signature,
1704 size_t signature_length )
1705{
1706 psa_status_t status;
1707 int ret;
1708 mbedtls_md_type_t md_alg;
1709
1710 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1711 if( status != PSA_SUCCESS )
1712 return( status );
1713
1714 if( signature_length < rsa->len )
1715 return( PSA_ERROR_BUFFER_TOO_SMALL );
1716
1717#if defined(MBEDTLS_PKCS1_V15)
1718 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1719 {
1720 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1721 MBEDTLS_MD_NONE );
1722 ret = mbedtls_rsa_pkcs1_verify( rsa,
1723 mbedtls_ctr_drbg_random,
1724 &global_data.ctr_drbg,
1725 MBEDTLS_RSA_PUBLIC,
1726 md_alg,
1727 hash_length,
1728 hash,
1729 signature );
1730 }
1731 else
1732#endif /* MBEDTLS_PKCS1_V15 */
1733#if defined(MBEDTLS_PKCS1_V21)
1734 if( PSA_ALG_IS_RSA_PSS( alg ) )
1735 {
1736 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1737 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1738 mbedtls_ctr_drbg_random,
1739 &global_data.ctr_drbg,
1740 MBEDTLS_RSA_PUBLIC,
1741 md_alg, hash_length, hash,
1742 signature );
1743 }
1744 else
1745#endif /* MBEDTLS_PKCS1_V21 */
1746 {
1747 return( PSA_ERROR_INVALID_ARGUMENT );
1748 }
1749 return( mbedtls_to_psa_error( ret ) );
1750}
1751#endif /* MBEDTLS_RSA_C */
1752
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001753#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001754/* `ecp` cannot be const because `ecp->grp` needs to be non-const
1755 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
1756 * (even though these functions don't modify it). */
1757static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
1758 psa_algorithm_t alg,
1759 const uint8_t *hash,
1760 size_t hash_length,
1761 uint8_t *signature,
1762 size_t signature_size,
1763 size_t *signature_length )
1764{
1765 int ret;
1766 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001767 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001768 mbedtls_mpi_init( &r );
1769 mbedtls_mpi_init( &s );
1770
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001771 if( signature_size < 2 * curve_bytes )
1772 {
1773 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
1774 goto cleanup;
1775 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001776
1777 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
1778 {
1779 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
1780 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1781 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
1782 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
1783 hash, hash_length,
1784 md_alg ) );
1785 }
1786 else
1787 {
1788 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
1789 hash, hash_length,
1790 mbedtls_ctr_drbg_random,
1791 &global_data.ctr_drbg ) );
1792 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001793
1794 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
1795 signature,
1796 curve_bytes ) );
1797 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
1798 signature + curve_bytes,
1799 curve_bytes ) );
1800
1801cleanup:
1802 mbedtls_mpi_free( &r );
1803 mbedtls_mpi_free( &s );
1804 if( ret == 0 )
1805 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001806 return( mbedtls_to_psa_error( ret ) );
1807}
1808
1809static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
1810 const uint8_t *hash,
1811 size_t hash_length,
1812 const uint8_t *signature,
1813 size_t signature_length )
1814{
1815 int ret;
1816 mbedtls_mpi r, s;
1817 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
1818 mbedtls_mpi_init( &r );
1819 mbedtls_mpi_init( &s );
1820
1821 if( signature_length != 2 * curve_bytes )
1822 return( PSA_ERROR_INVALID_SIGNATURE );
1823
1824 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
1825 signature,
1826 curve_bytes ) );
1827 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
1828 signature + curve_bytes,
1829 curve_bytes ) );
1830
1831 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
1832 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001833
1834cleanup:
1835 mbedtls_mpi_free( &r );
1836 mbedtls_mpi_free( &s );
1837 return( mbedtls_to_psa_error( ret ) );
1838}
1839#endif /* MBEDTLS_ECDSA_C */
1840
Gilles Peskine61b91d42018-06-08 16:09:36 +02001841psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
1842 psa_algorithm_t alg,
1843 const uint8_t *hash,
1844 size_t hash_length,
1845 const uint8_t *salt,
1846 size_t salt_length,
1847 uint8_t *signature,
1848 size_t signature_size,
1849 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01001850{
1851 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001852 psa_status_t status;
1853
1854 *signature_length = signature_size;
1855
Gilles Peskine93aa0332018-02-03 23:58:03 +01001856 (void) salt;
1857 (void) salt_length;
1858
Gilles Peskine828ed142018-06-18 23:25:51 +02001859 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001860 {
1861 status = PSA_ERROR_EMPTY_SLOT;
1862 goto exit;
1863 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001864 slot = &global_data.key_slots[key];
1865 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001866 {
1867 status = PSA_ERROR_EMPTY_SLOT;
1868 goto exit;
1869 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001870 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001871 {
1872 status = PSA_ERROR_INVALID_ARGUMENT;
1873 goto exit;
1874 }
Gilles Peskine61b91d42018-06-08 16:09:36 +02001875 if( ! ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001876 {
1877 status = PSA_ERROR_NOT_PERMITTED;
1878 goto exit;
1879 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001880
Gilles Peskine20035e32018-02-03 22:44:14 +01001881#if defined(MBEDTLS_RSA_C)
1882 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1883 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001884 status = psa_rsa_sign( slot->data.rsa,
1885 alg,
1886 hash, hash_length,
1887 signature, signature_size,
1888 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01001889 }
1890 else
1891#endif /* defined(MBEDTLS_RSA_C) */
1892#if defined(MBEDTLS_ECP_C)
1893 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1894 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001895#if defined(MBEDTLS_ECDSA_C)
1896 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001897 status = psa_ecdsa_sign( slot->data.ecp,
1898 alg,
1899 hash, hash_length,
1900 signature, signature_size,
1901 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001902 else
1903#endif /* defined(MBEDTLS_ECDSA_C) */
1904 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001905 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001906 }
itayzafrir5c753392018-05-08 11:18:38 +03001907 }
1908 else
1909#endif /* defined(MBEDTLS_ECP_C) */
1910 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001911 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01001912 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001913
1914exit:
1915 /* Fill the unused part of the output buffer (the whole buffer on error,
1916 * the trailing part on success) with something that isn't a valid mac
1917 * (barring an attack on the mac and deliberately-crafted input),
1918 * in case the caller doesn't check the return status properly. */
1919 if( status == PSA_SUCCESS )
1920 memset( signature + *signature_length, '!',
1921 signature_size - *signature_length );
1922 else
1923 memset( signature, '!', signature_size );
1924 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03001925}
1926
1927psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
1928 psa_algorithm_t alg,
1929 const uint8_t *hash,
1930 size_t hash_length,
1931 const uint8_t *salt,
1932 size_t salt_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02001933 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02001934 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03001935{
1936 key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02001937
itayzafrir5c753392018-05-08 11:18:38 +03001938 (void) salt;
1939 (void) salt_length;
1940
Gilles Peskine828ed142018-06-18 23:25:51 +02001941 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
itayzafrir5c753392018-05-08 11:18:38 +03001942 return( PSA_ERROR_INVALID_ARGUMENT );
1943 slot = &global_data.key_slots[key];
1944 if( slot->type == PSA_KEY_TYPE_NONE )
1945 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001946 if( ! ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) )
itayzafrir5c753392018-05-08 11:18:38 +03001947 return( PSA_ERROR_NOT_PERMITTED );
1948
Gilles Peskine61b91d42018-06-08 16:09:36 +02001949#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001950 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
1951 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001952 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02001953 return( psa_rsa_verify( slot->data.rsa,
1954 alg,
1955 hash, hash_length,
1956 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001957 }
1958 else
1959#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03001960#if defined(MBEDTLS_ECP_C)
1961 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1962 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001963#if defined(MBEDTLS_ECDSA_C)
1964 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001965 return( psa_ecdsa_verify( slot->data.ecp,
1966 hash, hash_length,
1967 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001968 else
1969#endif /* defined(MBEDTLS_ECDSA_C) */
1970 {
1971 return( PSA_ERROR_INVALID_ARGUMENT );
1972 }
itayzafrir5c753392018-05-08 11:18:38 +03001973 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001974 else
1975#endif /* defined(MBEDTLS_ECP_C) */
1976 {
1977 return( PSA_ERROR_NOT_SUPPORTED );
1978 }
1979}
1980
Gilles Peskine61b91d42018-06-08 16:09:36 +02001981psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
1982 psa_algorithm_t alg,
1983 const uint8_t *input,
1984 size_t input_length,
1985 const uint8_t *salt,
1986 size_t salt_length,
1987 uint8_t *output,
1988 size_t output_size,
1989 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001990{
1991 key_slot_t *slot;
1992 (void) salt;
1993 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001994 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001995
Gilles Peskine828ed142018-06-18 23:25:51 +02001996 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein7f5a3192018-05-06 22:26:54 +03001997 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001998 slot = &global_data.key_slots[key];
1999 if( slot->type == PSA_KEY_TYPE_NONE )
2000 return( PSA_ERROR_EMPTY_SLOT );
2001 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2002 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002003 if( ! ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) )
2004 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002005
2006#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002007 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
2008 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002009 {
2010 mbedtls_rsa_context *rsa = slot->data.rsa;
2011 int ret;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002012 if( output_size < rsa->len )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002013 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002014#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002015 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002016 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002017 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2018 mbedtls_ctr_drbg_random,
2019 &global_data.ctr_drbg,
2020 MBEDTLS_RSA_PUBLIC,
2021 input_length,
2022 input,
2023 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002024 }
2025 else
2026#endif /* MBEDTLS_PKCS1_V15 */
2027#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002028 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002029 {
2030 return( PSA_ERROR_NOT_SUPPORTED );
2031 }
2032 else
2033#endif /* MBEDTLS_PKCS1_V21 */
2034 {
2035 return( PSA_ERROR_INVALID_ARGUMENT );
2036 }
2037 if( ret == 0 )
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002038 *output_length = rsa->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002039 return( mbedtls_to_psa_error( ret ) );
2040 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002041 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002042#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002043 {
2044 return( PSA_ERROR_NOT_SUPPORTED );
2045 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002046}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002047
Gilles Peskine61b91d42018-06-08 16:09:36 +02002048psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2049 psa_algorithm_t alg,
2050 const uint8_t *input,
2051 size_t input_length,
2052 const uint8_t *salt,
2053 size_t salt_length,
2054 uint8_t *output,
2055 size_t output_size,
2056 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002057{
2058 key_slot_t *slot;
2059 (void) salt;
2060 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002061 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002062
Gilles Peskine828ed142018-06-18 23:25:51 +02002063 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002064 return( PSA_ERROR_EMPTY_SLOT );
2065 slot = &global_data.key_slots[key];
2066 if( slot->type == PSA_KEY_TYPE_NONE )
2067 return( PSA_ERROR_EMPTY_SLOT );
2068 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2069 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002070 if( ! ( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
2071 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002072
2073#if defined(MBEDTLS_RSA_C)
2074 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2075 {
2076 mbedtls_rsa_context *rsa = slot->data.rsa;
2077 int ret;
2078
Gilles Peskinec4def2f2018-06-08 17:53:48 +02002079 if( input_length != rsa->len )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002080 return( PSA_ERROR_INVALID_ARGUMENT );
2081
2082#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002083 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002084 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002085 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2086 mbedtls_ctr_drbg_random,
2087 &global_data.ctr_drbg,
2088 MBEDTLS_RSA_PRIVATE,
2089 output_length,
2090 input,
2091 output,
2092 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002093 }
2094 else
2095#endif /* MBEDTLS_PKCS1_V15 */
2096#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002097 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002098 {
2099 return( PSA_ERROR_NOT_SUPPORTED );
2100 }
2101 else
2102#endif /* MBEDTLS_PKCS1_V21 */
2103 {
2104 return( PSA_ERROR_INVALID_ARGUMENT );
2105 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002106
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002107 return( mbedtls_to_psa_error( ret ) );
2108 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002109 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002110#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002111 {
2112 return( PSA_ERROR_NOT_SUPPORTED );
2113 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002114}
Gilles Peskine20035e32018-02-03 22:44:14 +01002115
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002116
2117
mohammad1603503973b2018-03-12 15:59:30 +02002118/****************************************************************/
2119/* Symmetric cryptography */
2120/****************************************************************/
2121
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002122/* Initialize the cipher operation structure. Once this function has been
2123 * called, psa_cipher_abort can run and will do the right thing. */
2124static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2125 psa_algorithm_t alg )
2126{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002127 if( ! PSA_ALG_IS_CIPHER( alg ) )
2128 {
2129 memset( operation, 0, sizeof( *operation ) );
2130 return( PSA_ERROR_INVALID_ARGUMENT );
2131 }
2132
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002133 operation->alg = alg;
2134 operation->key_set = 0;
2135 operation->iv_set = 0;
2136 operation->iv_required = 1;
2137 operation->iv_size = 0;
2138 operation->block_size = 0;
2139 mbedtls_cipher_init( &operation->ctx.cipher );
2140 return( PSA_SUCCESS );
2141}
2142
Gilles Peskinee553c652018-06-04 16:22:46 +02002143static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2144 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002145 psa_algorithm_t alg,
2146 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002147{
2148 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2149 psa_status_t status;
2150 key_slot_t *slot;
2151 psa_key_type_t key_type;
2152 size_t key_bits;
2153 const mbedtls_cipher_info_t *cipher_info = NULL;
2154
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002155 status = psa_cipher_init( operation, alg );
2156 if( status != PSA_SUCCESS )
2157 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002158
2159 status = psa_get_key_information( key, &key_type, &key_bits );
2160 if( status != PSA_SUCCESS )
2161 return( status );
2162 slot = &global_data.key_slots[key];
2163
Gilles Peskinebb1072f2018-06-08 18:46:05 +02002164 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002165 if( cipher_info == NULL )
2166 return( PSA_ERROR_NOT_SUPPORTED );
2167
mohammad1603503973b2018-03-12 15:59:30 +02002168 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002169 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002170 {
2171 psa_cipher_abort( operation );
2172 return( mbedtls_to_psa_error( ret ) );
2173 }
2174
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002175#if defined(MBEDTLS_DES_C)
2176 if( key_type == PSA_KEY_TYPE_DES && key_bits == 128 )
2177 {
2178 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2179 unsigned char keys[24];
2180 memcpy( keys, slot->data.raw.data, 16 );
2181 memcpy( keys + 16, slot->data.raw.data, 8 );
2182 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2183 keys,
2184 192, cipher_operation );
2185 }
2186 else
2187#endif
2188 {
2189 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2190 slot->data.raw.data,
2191 key_bits, cipher_operation );
2192 }
Moran Peker41deec42018-04-04 15:43:05 +03002193 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002194 {
2195 psa_cipher_abort( operation );
2196 return( mbedtls_to_psa_error( ret ) );
2197 }
2198
mohammad16038481e742018-03-18 13:57:31 +02002199#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002200 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002201 {
Gilles Peskine53514202018-06-06 15:11:46 +02002202 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2203 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002204
Moran Pekera28258c2018-05-29 16:25:04 +03002205 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002206 {
2207 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2208 mode = MBEDTLS_PADDING_PKCS7;
2209 break;
2210 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2211 mode = MBEDTLS_PADDING_NONE;
2212 break;
2213 default:
Moran Pekerae382792018-05-31 14:06:17 +03002214 psa_cipher_abort( operation );
2215 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002216 }
2217 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002218 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002219 {
2220 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002221 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002222 }
mohammad16038481e742018-03-18 13:57:31 +02002223 }
2224#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2225
mohammad1603503973b2018-03-12 15:59:30 +02002226 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002227 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
2228 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) :
2229 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002230 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002231 {
mohammad160389e0f462018-04-12 08:48:45 +03002232 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
mohammad16038481e742018-03-18 13:57:31 +02002233 }
mohammad1603503973b2018-03-12 15:59:30 +02002234
Moran Peker395db872018-05-31 14:07:14 +03002235 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002236}
2237
Gilles Peskinee553c652018-06-04 16:22:46 +02002238psa_status_t psa_encrypt_setup( psa_cipher_operation_t *operation,
2239 psa_key_slot_t key,
2240 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002241{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002242 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002243}
2244
Gilles Peskinee553c652018-06-04 16:22:46 +02002245psa_status_t psa_decrypt_setup( psa_cipher_operation_t *operation,
2246 psa_key_slot_t key,
2247 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002248{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002249 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002250}
2251
Gilles Peskinee553c652018-06-04 16:22:46 +02002252psa_status_t psa_encrypt_generate_iv( psa_cipher_operation_t *operation,
2253 unsigned char *iv,
2254 size_t iv_size,
2255 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002256{
Moran Peker41deec42018-04-04 15:43:05 +03002257 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002258 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002259 return( PSA_ERROR_BAD_STATE );
2260 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002261 {
Moran Peker41deec42018-04-04 15:43:05 +03002262 ret = PSA_ERROR_BUFFER_TOO_SMALL;
2263 goto exit;
2264 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002265 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2266 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002267 if( ret != 0 )
2268 {
2269 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002270 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002271 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002272
mohammad16038481e742018-03-18 13:57:31 +02002273 *iv_length = operation->iv_size;
mohammad160389e0f462018-04-12 08:48:45 +03002274 ret = psa_encrypt_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002275
Moran Peker395db872018-05-31 14:07:14 +03002276exit:
2277 if( ret != PSA_SUCCESS )
2278 psa_cipher_abort( operation );
2279 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02002280}
2281
Gilles Peskinee553c652018-06-04 16:22:46 +02002282psa_status_t psa_encrypt_set_iv( psa_cipher_operation_t *operation,
2283 const unsigned char *iv,
2284 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002285{
Moran Peker41deec42018-04-04 15:43:05 +03002286 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002287 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002288 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03002289 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002290 {
Moran Pekerae382792018-05-31 14:06:17 +03002291 psa_cipher_abort( operation );
2292 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03002293 }
mohammad1603503973b2018-03-12 15:59:30 +02002294 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002295 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002296 {
2297 psa_cipher_abort( operation );
2298 return( mbedtls_to_psa_error( ret ) );
2299 }
2300
2301 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02002302
Moran Peker395db872018-05-31 14:07:14 +03002303 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002304}
2305
Gilles Peskinee553c652018-06-04 16:22:46 +02002306psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2307 const uint8_t *input,
2308 size_t input_length,
2309 unsigned char *output,
2310 size_t output_size,
2311 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002312{
2313 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002314 size_t expected_output_size;
2315 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2316 {
2317 /* Take the unprocessed partial block left over from previous
2318 * update calls, if any, plus the input to this call. Remove
2319 * the last partial block, if any. You get the data that will be
2320 * output in this call. */
2321 expected_output_size =
2322 ( operation->ctx.cipher.unprocessed_len + input_length )
2323 / operation->block_size * operation->block_size;
2324 }
2325 else
2326 {
2327 expected_output_size = input_length;
2328 }
2329 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03002330 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02002331
mohammad1603503973b2018-03-12 15:59:30 +02002332 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002333 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002334 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002335 {
2336 psa_cipher_abort( operation );
2337 return( mbedtls_to_psa_error( ret ) );
2338 }
2339
Moran Peker395db872018-05-31 14:07:14 +03002340 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002341}
2342
Gilles Peskinee553c652018-06-04 16:22:46 +02002343psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2344 uint8_t *output,
2345 size_t output_size,
2346 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002347{
2348 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002349 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002350
mohammad1603503973b2018-03-12 15:59:30 +02002351 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002352 {
Moran Peker7cb22b82018-06-05 11:40:02 +03002353 psa_cipher_abort( operation );
2354 return( PSA_ERROR_BAD_STATE );
2355 }
2356 if( operation->iv_required && ! operation->iv_set )
2357 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002358 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002359 return( PSA_ERROR_BAD_STATE );
2360 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002361 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2362 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002363 {
Gilles Peskine53514202018-06-06 15:11:46 +02002364 psa_algorithm_t padding_mode =
2365 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002366 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002367 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002368 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002369 return( PSA_ERROR_TAMPERING_DETECTED );
2370 }
Gilles Peskine53514202018-06-06 15:11:46 +02002371 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002372 {
2373 if( operation->ctx.cipher.unprocessed_len != 0 )
2374 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002375 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002376 return( PSA_ERROR_INVALID_ARGUMENT );
2377 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002378 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002379 }
2380
2381 ret = mbedtls_cipher_finish( &operation->ctx.cipher, temp_output_buffer,
Gilles Peskinee553c652018-06-04 16:22:46 +02002382 output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002383 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002384 {
2385 psa_cipher_abort( operation );
2386 return( mbedtls_to_psa_error( ret ) );
2387 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002388 if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002389 memcpy( output, temp_output_buffer, *output_length );
2390 else
2391 {
2392 psa_cipher_abort( operation );
2393 return( PSA_ERROR_BUFFER_TOO_SMALL );
2394 }
mohammad1603503973b2018-03-12 15:59:30 +02002395
Moran Peker4c80d832018-04-22 20:15:31 +03002396 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002397}
2398
Gilles Peskinee553c652018-06-04 16:22:46 +02002399psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2400{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002401 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002402 {
2403 /* The object has (apparently) been initialized but it is not
2404 * in use. It's ok to call abort on such an object, and there's
2405 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002406 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002407 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002408
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002409 /* Sanity check (shouldn't happen: operation->alg should
2410 * always have been initialized to a valid value). */
2411 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2412 return( PSA_ERROR_BAD_STATE );
2413
mohammad1603503973b2018-03-12 15:59:30 +02002414 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002415
Moran Peker41deec42018-04-04 15:43:05 +03002416 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002417 operation->key_set = 0;
2418 operation->iv_set = 0;
2419 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002420 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002421 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002422
Moran Peker395db872018-05-31 14:07:14 +03002423 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002424}
2425
Gilles Peskinea0655c32018-04-30 17:06:50 +02002426
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002427
mohammad16038cc1cee2018-03-28 01:21:33 +03002428/****************************************************************/
2429/* Key Policy */
2430/****************************************************************/
2431
Gilles Peskine2d277862018-06-18 15:41:12 +02002432void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002433{
Gilles Peskine803ce742018-06-18 16:07:14 +02002434 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002435}
2436
Gilles Peskine2d277862018-06-18 15:41:12 +02002437void psa_key_policy_set_usage( psa_key_policy_t *policy,
2438 psa_key_usage_t usage,
2439 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002440{
mohammad16034eed7572018-03-28 05:14:59 -07002441 policy->usage = usage;
2442 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002443}
2444
Gilles Peskine2d277862018-06-18 15:41:12 +02002445psa_key_usage_t psa_key_policy_get_usage( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002446{
mohammad16036df908f2018-04-02 08:34:15 -07002447 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002448}
2449
Gilles Peskine2d277862018-06-18 15:41:12 +02002450psa_algorithm_t psa_key_policy_get_algorithm( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002451{
mohammad16036df908f2018-04-02 08:34:15 -07002452 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002453}
2454
Gilles Peskine2d277862018-06-18 15:41:12 +02002455psa_status_t psa_set_key_policy( psa_key_slot_t key,
2456 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002457{
2458 key_slot_t *slot;
mohammad16038cc1cee2018-03-28 01:21:33 +03002459
Gilles Peskine828ed142018-06-18 23:25:51 +02002460 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002461 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002462
mohammad16038cc1cee2018-03-28 01:21:33 +03002463 slot = &global_data.key_slots[key];
2464 if( slot->type != PSA_KEY_TYPE_NONE )
2465 return( PSA_ERROR_OCCUPIED_SLOT );
2466
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002467 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2468 PSA_KEY_USAGE_ENCRYPT |
2469 PSA_KEY_USAGE_DECRYPT |
2470 PSA_KEY_USAGE_SIGN |
2471 PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002472 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002473
mohammad16036df908f2018-04-02 08:34:15 -07002474 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002475
2476 return( PSA_SUCCESS );
2477}
2478
Gilles Peskine2d277862018-06-18 15:41:12 +02002479psa_status_t psa_get_key_policy( psa_key_slot_t key,
2480 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002481{
2482 key_slot_t *slot;
2483
Gilles Peskine828ed142018-06-18 23:25:51 +02002484 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002485 return( PSA_ERROR_INVALID_ARGUMENT );
2486
2487 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002488
mohammad16036df908f2018-04-02 08:34:15 -07002489 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002490
2491 return( PSA_SUCCESS );
2492}
Gilles Peskine20035e32018-02-03 22:44:14 +01002493
Gilles Peskinea0655c32018-04-30 17:06:50 +02002494
2495
mohammad1603804cd712018-03-20 22:44:08 +02002496/****************************************************************/
2497/* Key Lifetime */
2498/****************************************************************/
2499
Gilles Peskine2d277862018-06-18 15:41:12 +02002500psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2501 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002502{
2503 key_slot_t *slot;
2504
Gilles Peskine828ed142018-06-18 23:25:51 +02002505 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002506 return( PSA_ERROR_INVALID_ARGUMENT );
2507
2508 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002509
mohammad1603804cd712018-03-20 22:44:08 +02002510 *lifetime = slot->lifetime;
2511
2512 return( PSA_SUCCESS );
2513}
2514
Gilles Peskine2d277862018-06-18 15:41:12 +02002515psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002516 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002517{
2518 key_slot_t *slot;
2519
Gilles Peskine828ed142018-06-18 23:25:51 +02002520 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002521 return( PSA_ERROR_INVALID_ARGUMENT );
2522
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002523 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2524 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002525 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2526 return( PSA_ERROR_INVALID_ARGUMENT );
2527
mohammad1603804cd712018-03-20 22:44:08 +02002528 slot = &global_data.key_slots[key];
mohammad16035d7ec202018-03-28 01:29:41 +03002529 if( slot->type != PSA_KEY_TYPE_NONE )
2530 return( PSA_ERROR_OCCUPIED_SLOT );
mohammad1603804cd712018-03-20 22:44:08 +02002531
Moran Pekerd7326592018-05-29 16:56:39 +03002532 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002533 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002534
mohammad1603060ad8a2018-03-20 14:28:38 -07002535 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002536
2537 return( PSA_SUCCESS );
2538}
2539
Gilles Peskine20035e32018-02-03 22:44:14 +01002540
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002541
mohammad16035955c982018-04-26 00:53:03 +03002542/****************************************************************/
2543/* AEAD */
2544/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002545
mohammad16035955c982018-04-26 00:53:03 +03002546psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2547 psa_algorithm_t alg,
2548 const uint8_t *nonce,
2549 size_t nonce_length,
2550 const uint8_t *additional_data,
2551 size_t additional_data_length,
2552 const uint8_t *plaintext,
2553 size_t plaintext_length,
2554 uint8_t *ciphertext,
2555 size_t ciphertext_size,
2556 size_t *ciphertext_length )
2557{
2558 int ret;
2559 psa_status_t status;
2560 key_slot_t *slot;
2561 psa_key_type_t key_type;
2562 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002563 uint8_t *tag;
2564 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002565 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002566 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002567
mohammad1603f08a5502018-06-03 15:05:47 +03002568 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002569
mohammad16035955c982018-04-26 00:53:03 +03002570 status = psa_get_key_information( key, &key_type, &key_bits );
2571 if( status != PSA_SUCCESS )
2572 return( status );
2573 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002574 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002575 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002576
mohammad16035ed06212018-06-06 13:09:34 +03002577 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2578 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002579 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002580 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002581
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002582 if( ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) == 0 )
mohammad1603f14394b2018-06-04 14:33:19 +03002583 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002584
Gilles Peskine2d277862018-06-18 15:41:12 +02002585 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2586 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002587 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002588
mohammad16035955c982018-04-26 00:53:03 +03002589 if( alg == PSA_ALG_GCM )
2590 {
2591 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002592 tag_length = 16;
2593
mohammad160396910d82018-06-04 14:33:00 +03002594 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2595 return( PSA_ERROR_INVALID_ARGUMENT );
2596
mohammad160315223a82018-06-03 17:19:55 +03002597 //make sure we have place to hold the tag in the ciphertext buffer
2598 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002599 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002600
2601 //update the tag pointer to point to the end of the ciphertext_length
2602 tag = ciphertext + plaintext_length;
2603
mohammad16035955c982018-04-26 00:53:03 +03002604 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002605 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002606 slot->data.raw.data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002607 key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002608 if( ret != 0 )
2609 {
2610 mbedtls_gcm_free( &gcm );
2611 return( mbedtls_to_psa_error( ret ) );
2612 }
2613 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002614 plaintext_length, nonce,
2615 nonce_length, additional_data,
2616 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002617 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002618 mbedtls_gcm_free( &gcm );
2619 }
2620 else if( alg == PSA_ALG_CCM )
2621 {
2622 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002623 tag_length = 16;
2624
mohammad160396910d82018-06-04 14:33:00 +03002625 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2626 return( PSA_ERROR_INVALID_ARGUMENT );
2627
mohammad160347ddf3d2018-04-26 01:11:21 +03002628 if( nonce_length < 7 || nonce_length > 13 )
2629 return( PSA_ERROR_INVALID_ARGUMENT );
2630
mohammad160315223a82018-06-03 17:19:55 +03002631 //make sure we have place to hold the tag in the ciphertext buffer
2632 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002633 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002634
2635 //update the tag pointer to point to the end of the ciphertext_length
2636 tag = ciphertext + plaintext_length;
2637
mohammad16035955c982018-04-26 00:53:03 +03002638 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002639 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002640 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002641 if( ret != 0 )
2642 {
2643 mbedtls_ccm_free( &ccm );
2644 return( mbedtls_to_psa_error( ret ) );
2645 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002646 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002647 nonce, nonce_length,
2648 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002649 additional_data_length,
2650 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002651 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002652 mbedtls_ccm_free( &ccm );
2653 }
mohammad16035c8845f2018-05-09 05:40:09 -07002654 else
2655 {
mohammad1603554faad2018-06-03 15:07:38 +03002656 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002657 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002658
mohammad160315223a82018-06-03 17:19:55 +03002659 if( ret != 0 )
2660 {
2661 memset( ciphertext, 0, ciphertext_size );
2662 return( mbedtls_to_psa_error( ret ) );
2663 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002664
mohammad160315223a82018-06-03 17:19:55 +03002665 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002666 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002667}
2668
Gilles Peskineee652a32018-06-01 19:23:52 +02002669/* Locate the tag in a ciphertext buffer containing the encrypted data
2670 * followed by the tag. Return the length of the part preceding the tag in
2671 * *plaintext_length. This is the size of the plaintext in modes where
2672 * the encrypted data has the same size as the plaintext, such as
2673 * CCM and GCM. */
2674static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2675 const uint8_t *ciphertext,
2676 size_t ciphertext_length,
2677 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002678 const uint8_t **p_tag )
2679{
2680 size_t payload_length;
2681 if( tag_length > ciphertext_length )
2682 return( PSA_ERROR_INVALID_ARGUMENT );
2683 payload_length = ciphertext_length - tag_length;
2684 if( payload_length > plaintext_size )
2685 return( PSA_ERROR_BUFFER_TOO_SMALL );
2686 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002687 return( PSA_SUCCESS );
2688}
2689
mohammad16035955c982018-04-26 00:53:03 +03002690psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2691 psa_algorithm_t alg,
2692 const uint8_t *nonce,
2693 size_t nonce_length,
2694 const uint8_t *additional_data,
2695 size_t additional_data_length,
2696 const uint8_t *ciphertext,
2697 size_t ciphertext_length,
2698 uint8_t *plaintext,
2699 size_t plaintext_size,
2700 size_t *plaintext_length )
2701{
2702 int ret;
2703 psa_status_t status;
2704 key_slot_t *slot;
2705 psa_key_type_t key_type;
2706 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002707 const uint8_t *tag;
2708 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002709 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002710 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002711
Gilles Peskineee652a32018-06-01 19:23:52 +02002712 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002713
mohammad16035955c982018-04-26 00:53:03 +03002714 status = psa_get_key_information( key, &key_type, &key_bits );
2715 if( status != PSA_SUCCESS )
2716 return( status );
2717 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002718 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002719 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002720
mohammad16035ed06212018-06-06 13:09:34 +03002721 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2722 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002723 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002724 return( PSA_ERROR_NOT_SUPPORTED );
2725
mohammad1603f14394b2018-06-04 14:33:19 +03002726 if( !( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
2727 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002728
Gilles Peskine2d277862018-06-18 15:41:12 +02002729 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2730 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002731 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002732
mohammad16035955c982018-04-26 00:53:03 +03002733 if( alg == PSA_ALG_GCM )
2734 {
2735 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002736
Gilles Peskineee652a32018-06-01 19:23:52 +02002737 tag_length = 16;
2738 status = psa_aead_unpadded_locate_tag( tag_length,
2739 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002740 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002741 if( status != PSA_SUCCESS )
2742 return( status );
2743
mohammad16035955c982018-04-26 00:53:03 +03002744 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002745 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002746 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002747 if( ret != 0 )
2748 {
2749 mbedtls_gcm_free( &gcm );
2750 return( mbedtls_to_psa_error( ret ) );
2751 }
mohammad16035955c982018-04-26 00:53:03 +03002752
Gilles Peskineee652a32018-06-01 19:23:52 +02002753 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002754 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002755 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002756 additional_data,
2757 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002758 tag, tag_length,
2759 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002760 mbedtls_gcm_free( &gcm );
2761 }
2762 else if( alg == PSA_ALG_CCM )
2763 {
2764 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002765
mohammad160347ddf3d2018-04-26 01:11:21 +03002766 if( nonce_length < 7 || nonce_length > 13 )
2767 return( PSA_ERROR_INVALID_ARGUMENT );
2768
mohammad16039375f842018-06-03 14:28:24 +03002769 tag_length = 16;
2770 status = psa_aead_unpadded_locate_tag( tag_length,
2771 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002772 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002773 if( status != PSA_SUCCESS )
2774 return( status );
2775
mohammad16035955c982018-04-26 00:53:03 +03002776 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002777 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002778 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002779 if( ret != 0 )
2780 {
2781 mbedtls_ccm_free( &ccm );
2782 return( mbedtls_to_psa_error( ret ) );
2783 }
mohammad160360a64d02018-06-03 17:20:42 +03002784 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002785 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002786 additional_data,
2787 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002788 ciphertext, plaintext,
2789 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002790 mbedtls_ccm_free( &ccm );
2791 }
mohammad160339574652018-06-01 04:39:53 -07002792 else
2793 {
mohammad1603554faad2018-06-03 15:07:38 +03002794 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07002795 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002796
Gilles Peskineee652a32018-06-01 19:23:52 +02002797 if( ret != 0 )
mohammad160360a64d02018-06-03 17:20:42 +03002798 memset( plaintext, 0, plaintext_size );
2799 else
2800 *plaintext_length = ciphertext_length - tag_length;
2801
Gilles Peskineee652a32018-06-01 19:23:52 +02002802 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03002803}
2804
Gilles Peskinea0655c32018-04-30 17:06:50 +02002805
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002806
Gilles Peskine20035e32018-02-03 22:44:14 +01002807/****************************************************************/
Gilles Peskine05d69892018-06-19 22:00:52 +02002808/* Key generation */
2809/****************************************************************/
2810
2811psa_status_t psa_generate_random( uint8_t *output,
2812 size_t output_size )
2813{
2814 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2815 output, output_size );
2816 return( mbedtls_to_psa_error( ret ) );
2817}
2818
2819psa_status_t psa_generate_key( psa_key_slot_t key,
2820 psa_key_type_t type,
2821 size_t bits,
2822 const void *parameters,
2823 size_t parameters_size )
2824{
Gilles Peskine12313cd2018-06-20 00:20:32 +02002825 key_slot_t *slot;
2826
2827 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
2828 return( PSA_ERROR_INVALID_ARGUMENT );
2829 slot = &global_data.key_slots[key];
2830 if( slot->type != PSA_KEY_TYPE_NONE )
2831 return( PSA_ERROR_OCCUPIED_SLOT );
2832 if( parameters == NULL && parameters_size != 0 )
2833 return( PSA_ERROR_INVALID_ARGUMENT );
2834
Gilles Peskine48c0ea12018-06-21 14:15:31 +02002835 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002836 {
2837 psa_status_t status = prepare_raw_data_slot( type, bits,
2838 &slot->data.raw );
2839 if( status != PSA_SUCCESS )
2840 return( status );
2841 status = psa_generate_random( slot->data.raw.data,
2842 slot->data.raw.bytes );
2843 if( status != PSA_SUCCESS )
2844 {
2845 mbedtls_free( slot->data.raw.data );
2846 return( status );
2847 }
2848#if defined(MBEDTLS_DES_C)
2849 if( type == PSA_KEY_TYPE_DES )
2850 {
2851 mbedtls_des_key_set_parity( slot->data.raw.data );
2852 if( slot->data.raw.bytes >= 16 )
2853 mbedtls_des_key_set_parity( slot->data.raw.data + 8 );
2854 if( slot->data.raw.bytes == 24 )
2855 mbedtls_des_key_set_parity( slot->data.raw.data + 16 );
2856 }
2857#endif /* MBEDTLS_DES_C */
2858 }
2859 else
2860
2861#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
2862 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2863 {
2864 mbedtls_rsa_context *rsa;
2865 int ret;
2866 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02002867 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
2868 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine12313cd2018-06-20 00:20:32 +02002869 if( parameters != NULL )
2870 {
2871 const unsigned *p = parameters;
2872 if( parameters_size != sizeof( *p ) )
2873 return( PSA_ERROR_INVALID_ARGUMENT );
2874 if( *p > INT_MAX )
2875 return( PSA_ERROR_INVALID_ARGUMENT );
2876 exponent = *p;
2877 }
2878 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
2879 if( rsa == NULL )
2880 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2881 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
2882 ret = mbedtls_rsa_gen_key( rsa,
2883 mbedtls_ctr_drbg_random,
2884 &global_data.ctr_drbg,
2885 bits,
2886 exponent );
2887 if( ret != 0 )
2888 {
2889 mbedtls_rsa_free( rsa );
2890 mbedtls_free( rsa );
2891 return( mbedtls_to_psa_error( ret ) );
2892 }
2893 slot->data.rsa = rsa;
2894 }
2895 else
2896#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
2897
2898#if defined(MBEDTLS_ECP_C)
2899 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
2900 {
2901 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
2902 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
2903 const mbedtls_ecp_curve_info *curve_info =
2904 mbedtls_ecp_curve_info_from_grp_id( grp_id );
2905 mbedtls_ecp_keypair *ecp;
2906 int ret;
2907 if( parameters != NULL )
2908 return( PSA_ERROR_NOT_SUPPORTED );
2909 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
2910 return( PSA_ERROR_NOT_SUPPORTED );
2911 if( curve_info->bit_size != bits )
2912 return( PSA_ERROR_INVALID_ARGUMENT );
2913 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
2914 if( ecp == NULL )
2915 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2916 mbedtls_ecp_keypair_init( ecp );
2917 ret = mbedtls_ecp_gen_key( grp_id, ecp,
2918 mbedtls_ctr_drbg_random,
2919 &global_data.ctr_drbg );
2920 if( ret != 0 )
2921 {
2922 mbedtls_ecp_keypair_free( ecp );
2923 mbedtls_free( ecp );
2924 return( mbedtls_to_psa_error( ret ) );
2925 }
2926 slot->data.ecp = ecp;
2927 }
2928 else
2929#endif /* MBEDTLS_ECP_C */
2930
2931 return( PSA_ERROR_NOT_SUPPORTED );
2932
2933 slot->type = type;
2934 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02002935}
2936
2937
2938/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002939/* Module setup */
2940/****************************************************************/
2941
Gilles Peskinee59236f2018-01-27 23:32:46 +01002942void mbedtls_psa_crypto_free( void )
2943{
Jaeden Amero045bd502018-06-26 14:00:08 +01002944 psa_key_slot_t key;
Gilles Peskine828ed142018-06-18 23:25:51 +02002945 for( key = 1; key < PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002946 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002947 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
2948 mbedtls_entropy_free( &global_data.entropy );
2949 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2950}
2951
2952psa_status_t psa_crypto_init( void )
2953{
2954 int ret;
2955 const unsigned char drbg_seed[] = "PSA";
2956
2957 if( global_data.initialized != 0 )
2958 return( PSA_SUCCESS );
2959
2960 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2961 mbedtls_entropy_init( &global_data.entropy );
2962 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
2963
2964 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
2965 mbedtls_entropy_func,
2966 &global_data.entropy,
2967 drbg_seed, sizeof( drbg_seed ) - 1 );
2968 if( ret != 0 )
2969 goto exit;
2970
Gilles Peskinee4ebc122018-03-07 14:16:44 +01002971 global_data.initialized = 1;
2972
Gilles Peskinee59236f2018-01-27 23:32:46 +01002973exit:
2974 if( ret != 0 )
2975 mbedtls_psa_crypto_free( );
2976 return( mbedtls_to_psa_error( ret ) );
2977}
2978
2979#endif /* MBEDTLS_PSA_CRYPTO_C */