blob: 4a33639527d84c3656c4c0938302119ca0e5c464 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
29
30#include "psa/crypto.h"
31
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010032#include <stdlib.h>
33#include <string.h>
34#if defined(MBEDTLS_PLATFORM_C)
35#include "mbedtls/platform.h"
36#else
37#define mbedtls_calloc calloc
38#define mbedtls_free free
39#endif
40
Gilles Peskinea5905292018-02-07 20:59:33 +010041#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020042#include "mbedtls/asn1.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020043#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010044#include "mbedtls/blowfish.h"
45#include "mbedtls/camellia.h"
46#include "mbedtls/cipher.h"
47#include "mbedtls/ccm.h"
48#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010049#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010050#include "mbedtls/des.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010051#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010052#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/error.h"
54#include "mbedtls/gcm.h"
55#include "mbedtls/md2.h"
56#include "mbedtls/md4.h"
57#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010058#include "mbedtls/md.h"
59#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010060#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010061#include "mbedtls/pk_internal.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010063#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/sha1.h"
65#include "mbedtls/sha256.h"
66#include "mbedtls/sha512.h"
67#include "mbedtls/xtea.h"
68
Gilles Peskinee59236f2018-01-27 23:32:46 +010069
70
71/* Implementation that should never be optimized out by the compiler */
72static void mbedtls_zeroize( void *v, size_t n )
73{
74 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
75}
76
Gilles Peskine9ef733f2018-02-07 21:05:37 +010077/* constant-time buffer comparison */
78static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
79{
80 size_t i;
81 unsigned char diff = 0;
82
83 for( i = 0; i < n; i++ )
84 diff |= a[i] ^ b[i];
85
86 return( diff );
87}
88
89
90
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010091/****************************************************************/
92/* Global data, support functions and library management */
93/****************************************************************/
94
95/* Number of key slots (plus one because 0 is not used).
96 * The value is a compile-time constant for now, for simplicity. */
Gilles Peskine828ed142018-06-18 23:25:51 +020097#define PSA_KEY_SLOT_COUNT 32
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010098
Gilles Peskine2d277862018-06-18 15:41:12 +020099typedef struct
100{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100101 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +0300102 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200103 psa_key_lifetime_t lifetime;
Gilles Peskine2d277862018-06-18 15:41:12 +0200104 union
105 {
106 struct raw_data
107 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100108 uint8_t *data;
109 size_t bytes;
110 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100111#if defined(MBEDTLS_RSA_C)
112 mbedtls_rsa_context *rsa;
113#endif /* MBEDTLS_RSA_C */
114#if defined(MBEDTLS_ECP_C)
115 mbedtls_ecp_keypair *ecp;
116#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100117 } data;
118} key_slot_t;
119
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200120static int key_type_is_raw_bytes( psa_key_type_t type )
121{
122 psa_key_type_t category = type & PSA_KEY_TYPE_CATEGORY_MASK;
123 return( category == PSA_KEY_TYPE_RAW_DATA ||
124 category == PSA_KEY_TYPE_CATEGORY_SYMMETRIC );
125}
126
Gilles Peskine2d277862018-06-18 15:41:12 +0200127typedef struct
128{
Gilles Peskinee59236f2018-01-27 23:32:46 +0100129 int initialized;
130 mbedtls_entropy_context entropy;
131 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200132 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinee59236f2018-01-27 23:32:46 +0100133} psa_global_data_t;
134
135static psa_global_data_t global_data;
136
137static psa_status_t mbedtls_to_psa_error( int ret )
138{
Gilles Peskinea5905292018-02-07 20:59:33 +0100139 /* If there's both a high-level code and low-level code, dispatch on
140 * the high-level code. */
141 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100142 {
143 case 0:
144 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100145
146 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
147 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
148 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
149 return( PSA_ERROR_NOT_SUPPORTED );
150 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
151 return( PSA_ERROR_HARDWARE_FAILURE );
152
153 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
154 return( PSA_ERROR_HARDWARE_FAILURE );
155
Gilles Peskine9a944802018-06-21 09:35:35 +0200156 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
157 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
158 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
159 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
160 case MBEDTLS_ERR_ASN1_INVALID_DATA:
161 return( PSA_ERROR_INVALID_ARGUMENT );
162 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
163 return( PSA_ERROR_INSUFFICIENT_MEMORY );
164 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
165 return( PSA_ERROR_BUFFER_TOO_SMALL );
166
Gilles Peskinea5905292018-02-07 20:59:33 +0100167 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
168 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
169 return( PSA_ERROR_NOT_SUPPORTED );
170 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
171 return( PSA_ERROR_HARDWARE_FAILURE );
172
173 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
174 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
175 return( PSA_ERROR_NOT_SUPPORTED );
176 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
177 return( PSA_ERROR_HARDWARE_FAILURE );
178
179 case MBEDTLS_ERR_CCM_BAD_INPUT:
180 return( PSA_ERROR_INVALID_ARGUMENT );
181 case MBEDTLS_ERR_CCM_AUTH_FAILED:
182 return( PSA_ERROR_INVALID_SIGNATURE );
183 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
184 return( PSA_ERROR_HARDWARE_FAILURE );
185
186 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
187 return( PSA_ERROR_NOT_SUPPORTED );
188 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
189 return( PSA_ERROR_INVALID_ARGUMENT );
190 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
191 return( PSA_ERROR_INSUFFICIENT_MEMORY );
192 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
193 return( PSA_ERROR_INVALID_PADDING );
194 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
195 return( PSA_ERROR_BAD_STATE );
196 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
197 return( PSA_ERROR_INVALID_SIGNATURE );
198 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
199 return( PSA_ERROR_TAMPERING_DETECTED );
200 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
201 return( PSA_ERROR_HARDWARE_FAILURE );
202
203 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
204 return( PSA_ERROR_HARDWARE_FAILURE );
205
206 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
207 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
208 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
209 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
210 return( PSA_ERROR_NOT_SUPPORTED );
211 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
212 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
213
214 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
215 return( PSA_ERROR_NOT_SUPPORTED );
216 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
217 return( PSA_ERROR_HARDWARE_FAILURE );
218
Gilles Peskinee59236f2018-01-27 23:32:46 +0100219 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
220 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
221 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
222 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100223
224 case MBEDTLS_ERR_GCM_AUTH_FAILED:
225 return( PSA_ERROR_INVALID_SIGNATURE );
226 case MBEDTLS_ERR_GCM_BAD_INPUT:
227 return( PSA_ERROR_NOT_SUPPORTED );
228 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
229 return( PSA_ERROR_HARDWARE_FAILURE );
230
231 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
232 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
233 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
234 return( PSA_ERROR_HARDWARE_FAILURE );
235
236 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
237 return( PSA_ERROR_NOT_SUPPORTED );
238 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
239 return( PSA_ERROR_INVALID_ARGUMENT );
240 case MBEDTLS_ERR_MD_ALLOC_FAILED:
241 return( PSA_ERROR_INSUFFICIENT_MEMORY );
242 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
243 return( PSA_ERROR_STORAGE_FAILURE );
244 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
245 return( PSA_ERROR_HARDWARE_FAILURE );
246
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100247 case MBEDTLS_ERR_PK_ALLOC_FAILED:
248 return( PSA_ERROR_INSUFFICIENT_MEMORY );
249 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
250 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
251 return( PSA_ERROR_INVALID_ARGUMENT );
252 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100253 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100254 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
255 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
256 return( PSA_ERROR_INVALID_ARGUMENT );
257 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
258 return( PSA_ERROR_NOT_SUPPORTED );
259 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
260 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
261 return( PSA_ERROR_NOT_PERMITTED );
262 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_PK_INVALID_ALG:
265 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
266 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
267 return( PSA_ERROR_NOT_SUPPORTED );
268 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
269 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100270 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
271 return( PSA_ERROR_HARDWARE_FAILURE );
272
273 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
274 return( PSA_ERROR_HARDWARE_FAILURE );
275
276 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_RSA_INVALID_PADDING:
279 return( PSA_ERROR_INVALID_PADDING );
280 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
281 return( PSA_ERROR_HARDWARE_FAILURE );
282 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
283 return( PSA_ERROR_INVALID_ARGUMENT );
284 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
285 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
286 return( PSA_ERROR_TAMPERING_DETECTED );
287 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
288 return( PSA_ERROR_INVALID_SIGNATURE );
289 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
290 return( PSA_ERROR_BUFFER_TOO_SMALL );
291 case MBEDTLS_ERR_RSA_RNG_FAILED:
292 return( PSA_ERROR_INSUFFICIENT_MEMORY );
293 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
294 return( PSA_ERROR_NOT_SUPPORTED );
295 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
296 return( PSA_ERROR_HARDWARE_FAILURE );
297
298 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
299 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
300 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
301 return( PSA_ERROR_HARDWARE_FAILURE );
302
303 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
304 return( PSA_ERROR_INVALID_ARGUMENT );
305 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
306 return( PSA_ERROR_HARDWARE_FAILURE );
307
itayzafrir5c753392018-05-08 11:18:38 +0300308 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300309 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300310 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300311 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
312 return( PSA_ERROR_BUFFER_TOO_SMALL );
313 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
314 return( PSA_ERROR_NOT_SUPPORTED );
315 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
316 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
317 return( PSA_ERROR_INVALID_SIGNATURE );
318 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
319 return( PSA_ERROR_INSUFFICIENT_MEMORY );
320 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
321 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300322
Gilles Peskinee59236f2018-01-27 23:32:46 +0100323 default:
324 return( PSA_ERROR_UNKNOWN_ERROR );
325 }
326}
327
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200328
329
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100330/****************************************************************/
331/* Key management */
332/****************************************************************/
333
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200334static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
335{
336 switch( grpid )
337 {
338 case MBEDTLS_ECP_DP_SECP192R1:
339 return( PSA_ECC_CURVE_SECP192R1 );
340 case MBEDTLS_ECP_DP_SECP224R1:
341 return( PSA_ECC_CURVE_SECP224R1 );
342 case MBEDTLS_ECP_DP_SECP256R1:
343 return( PSA_ECC_CURVE_SECP256R1 );
344 case MBEDTLS_ECP_DP_SECP384R1:
345 return( PSA_ECC_CURVE_SECP384R1 );
346 case MBEDTLS_ECP_DP_SECP521R1:
347 return( PSA_ECC_CURVE_SECP521R1 );
348 case MBEDTLS_ECP_DP_BP256R1:
349 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
350 case MBEDTLS_ECP_DP_BP384R1:
351 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
352 case MBEDTLS_ECP_DP_BP512R1:
353 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
354 case MBEDTLS_ECP_DP_CURVE25519:
355 return( PSA_ECC_CURVE_CURVE25519 );
356 case MBEDTLS_ECP_DP_SECP192K1:
357 return( PSA_ECC_CURVE_SECP192K1 );
358 case MBEDTLS_ECP_DP_SECP224K1:
359 return( PSA_ECC_CURVE_SECP224K1 );
360 case MBEDTLS_ECP_DP_SECP256K1:
361 return( PSA_ECC_CURVE_SECP256K1 );
362 case MBEDTLS_ECP_DP_CURVE448:
363 return( PSA_ECC_CURVE_CURVE448 );
364 default:
365 return( 0 );
366 }
367}
368
Gilles Peskine12313cd2018-06-20 00:20:32 +0200369static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
370{
371 switch( curve )
372 {
373 case PSA_ECC_CURVE_SECP192R1:
374 return( MBEDTLS_ECP_DP_SECP192R1 );
375 case PSA_ECC_CURVE_SECP224R1:
376 return( MBEDTLS_ECP_DP_SECP224R1 );
377 case PSA_ECC_CURVE_SECP256R1:
378 return( MBEDTLS_ECP_DP_SECP256R1 );
379 case PSA_ECC_CURVE_SECP384R1:
380 return( MBEDTLS_ECP_DP_SECP384R1 );
381 case PSA_ECC_CURVE_SECP521R1:
382 return( MBEDTLS_ECP_DP_SECP521R1 );
383 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
384 return( MBEDTLS_ECP_DP_BP256R1 );
385 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
386 return( MBEDTLS_ECP_DP_BP384R1 );
387 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
388 return( MBEDTLS_ECP_DP_BP512R1 );
389 case PSA_ECC_CURVE_CURVE25519:
390 return( MBEDTLS_ECP_DP_CURVE25519 );
391 case PSA_ECC_CURVE_SECP192K1:
392 return( MBEDTLS_ECP_DP_SECP192K1 );
393 case PSA_ECC_CURVE_SECP224K1:
394 return( MBEDTLS_ECP_DP_SECP224K1 );
395 case PSA_ECC_CURVE_SECP256K1:
396 return( MBEDTLS_ECP_DP_SECP256K1 );
397 case PSA_ECC_CURVE_CURVE448:
398 return( MBEDTLS_ECP_DP_CURVE448 );
399 default:
400 return( MBEDTLS_ECP_DP_NONE );
401 }
402}
403
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200404static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
405 size_t bits,
406 struct raw_data *raw )
407{
408 /* Check that the bit size is acceptable for the key type */
409 switch( type )
410 {
411 case PSA_KEY_TYPE_RAW_DATA:
412#if defined(MBEDTLS_MD_C)
413 case PSA_KEY_TYPE_HMAC:
414#endif
415 break;
416#if defined(MBEDTLS_AES_C)
417 case PSA_KEY_TYPE_AES:
418 if( bits != 128 && bits != 192 && bits != 256 )
419 return( PSA_ERROR_INVALID_ARGUMENT );
420 break;
421#endif
422#if defined(MBEDTLS_CAMELLIA_C)
423 case PSA_KEY_TYPE_CAMELLIA:
424 if( bits != 128 && bits != 192 && bits != 256 )
425 return( PSA_ERROR_INVALID_ARGUMENT );
426 break;
427#endif
428#if defined(MBEDTLS_DES_C)
429 case PSA_KEY_TYPE_DES:
430 if( bits != 64 && bits != 128 && bits != 192 )
431 return( PSA_ERROR_INVALID_ARGUMENT );
432 break;
433#endif
434#if defined(MBEDTLS_ARC4_C)
435 case PSA_KEY_TYPE_ARC4:
436 if( bits < 8 || bits > 2048 )
437 return( PSA_ERROR_INVALID_ARGUMENT );
438 break;
439#endif
440 default:
441 return( PSA_ERROR_NOT_SUPPORTED );
442 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200443 if( bits % 8 != 0 )
444 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200445
446 /* Allocate memory for the key */
447 raw->bytes = PSA_BITS_TO_BYTES( bits );
448 raw->data = mbedtls_calloc( 1, raw->bytes );
449 if( raw->data == NULL )
450 {
451 raw->bytes = 0;
452 return( PSA_ERROR_INSUFFICIENT_MEMORY );
453 }
454 return( PSA_SUCCESS );
455}
456
Gilles Peskine2d277862018-06-18 15:41:12 +0200457psa_status_t psa_import_key( psa_key_slot_t key,
458 psa_key_type_t type,
459 const uint8_t *data,
460 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100461{
462 key_slot_t *slot;
463
Gilles Peskine828ed142018-06-18 23:25:51 +0200464 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100465 return( PSA_ERROR_INVALID_ARGUMENT );
466 slot = &global_data.key_slots[key];
467 if( slot->type != PSA_KEY_TYPE_NONE )
468 return( PSA_ERROR_OCCUPIED_SLOT );
469
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200470 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100471 {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200472 psa_status_t status;
Gilles Peskine6d912132018-03-07 16:41:37 +0100473 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100474 if( data_length > SIZE_MAX / 8 )
475 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200476 status = prepare_raw_data_slot( type,
477 PSA_BYTES_TO_BITS( data_length ),
478 &slot->data.raw );
479 if( status != PSA_SUCCESS )
480 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100481 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100482 }
483 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100484#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100485 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
486 type == PSA_KEY_TYPE_RSA_KEYPAIR ||
487 PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100488 {
489 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100490 mbedtls_pk_context pk;
Gilles Peskinec648d692018-06-28 08:46:13 +0200491 psa_status_t status = PSA_SUCCESS;
Gilles Peskine969ac722018-01-28 18:16:59 +0100492 mbedtls_pk_init( &pk );
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100493 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
494 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
495 else
496 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100497 if( ret != 0 )
498 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine969ac722018-01-28 18:16:59 +0100499 switch( mbedtls_pk_get_type( &pk ) )
500 {
501#if defined(MBEDTLS_RSA_C)
502 case MBEDTLS_PK_RSA:
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100503 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
504 type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskineaf3baab2018-06-27 22:55:52 +0200505 {
506 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk );
507 size_t bits = mbedtls_rsa_get_bitlen( rsa );
508 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
509 return( PSA_ERROR_NOT_SUPPORTED );
510 slot->data.rsa = rsa;
511 }
Gilles Peskine969ac722018-01-28 18:16:59 +0100512 else
Gilles Peskinec648d692018-06-28 08:46:13 +0200513 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine969ac722018-01-28 18:16:59 +0100514 break;
515#endif /* MBEDTLS_RSA_C */
516#if defined(MBEDTLS_ECP_C)
517 case MBEDTLS_PK_ECKEY:
518 if( PSA_KEY_TYPE_IS_ECC( type ) )
519 {
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200520 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
521 psa_ecc_curve_t actual_curve =
522 mbedtls_ecc_group_to_psa( ecp->grp.id );
523 psa_ecc_curve_t expected_curve =
524 PSA_KEY_TYPE_GET_CURVE( type );
525 if( actual_curve != expected_curve )
Gilles Peskinec648d692018-06-28 08:46:13 +0200526 {
527 status = PSA_ERROR_INVALID_ARGUMENT;
528 break;
529 }
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200530 slot->data.ecp = ecp;
Gilles Peskine969ac722018-01-28 18:16:59 +0100531 }
532 else
Gilles Peskinec648d692018-06-28 08:46:13 +0200533 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine969ac722018-01-28 18:16:59 +0100534 break;
535#endif /* MBEDTLS_ECP_C */
536 default:
Gilles Peskinec648d692018-06-28 08:46:13 +0200537 status = PSA_ERROR_INVALID_ARGUMENT;
538 break;
539 }
540 /* Free the content of the pk object only on error. On success,
541 * the content of the object has been stored in the slot. */
542 if( status != PSA_SUCCESS )
543 {
544 mbedtls_pk_free( &pk );
545 return( status );
Gilles Peskine969ac722018-01-28 18:16:59 +0100546 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100547 }
548 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100549#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100550 {
551 return( PSA_ERROR_NOT_SUPPORTED );
552 }
553
554 slot->type = type;
555 return( PSA_SUCCESS );
556}
557
Gilles Peskine2d277862018-06-18 15:41:12 +0200558psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100559{
560 key_slot_t *slot;
561
Gilles Peskine828ed142018-06-18 23:25:51 +0200562 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100563 return( PSA_ERROR_INVALID_ARGUMENT );
564 slot = &global_data.key_slots[key];
565 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200566 {
567 /* No key material to clean, but do zeroize the slot below to wipe
568 * metadata such as policies. */
569 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200570 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100571 {
572 mbedtls_free( slot->data.raw.data );
573 }
574 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100575#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100576 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
577 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100578 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100579 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100580 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100581 }
582 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100583#endif /* defined(MBEDTLS_RSA_C) */
584#if defined(MBEDTLS_ECP_C)
585 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
586 {
587 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100588 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100589 }
590 else
591#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100592 {
593 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100594 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100595 return( PSA_ERROR_TAMPERING_DETECTED );
596 }
597
598 mbedtls_zeroize( slot, sizeof( *slot ) );
599 return( PSA_SUCCESS );
600}
601
Gilles Peskine2d277862018-06-18 15:41:12 +0200602psa_status_t psa_get_key_information( psa_key_slot_t key,
603 psa_key_type_t *type,
604 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100605{
606 key_slot_t *slot;
607
Gilles Peskine828ed142018-06-18 23:25:51 +0200608 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100609 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100610 slot = &global_data.key_slots[key];
611 if( type != NULL )
612 *type = slot->type;
613 if( bits != NULL )
614 *bits = 0;
615 if( slot->type == PSA_KEY_TYPE_NONE )
616 return( PSA_ERROR_EMPTY_SLOT );
617
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200618 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100619 {
620 if( bits != NULL )
621 *bits = slot->data.raw.bytes * 8;
622 }
623 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100624#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100625 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
626 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100627 {
628 if( bits != NULL )
Gilles Peskine969ac722018-01-28 18:16:59 +0100629 *bits = mbedtls_rsa_get_bitlen( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100630 }
631 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100632#endif /* defined(MBEDTLS_RSA_C) */
633#if defined(MBEDTLS_ECP_C)
634 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
635 {
636 if( bits != NULL )
637 *bits = slot->data.ecp->grp.pbits;
638 }
639 else
640#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100641 {
642 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100643 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100644 return( PSA_ERROR_TAMPERING_DETECTED );
645 }
646
647 return( PSA_SUCCESS );
648}
649
Gilles Peskine2d277862018-06-18 15:41:12 +0200650static psa_status_t psa_internal_export_key( psa_key_slot_t key,
651 uint8_t *data,
652 size_t data_size,
653 size_t *data_length,
654 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100655{
656 key_slot_t *slot;
657
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100658 /* Set the key to empty now, so that even when there are errors, we always
659 * set data_length to a value between 0 and data_size. On error, setting
660 * the key to empty is a good choice because an empty key representation is
661 * unlikely to be accepted anywhere. */
662 *data_length = 0;
663
Gilles Peskine828ed142018-06-18 23:25:51 +0200664 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100665 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100666 slot = &global_data.key_slots[key];
667 if( slot->type == PSA_KEY_TYPE_NONE )
668 return( PSA_ERROR_EMPTY_SLOT );
669
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200670 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300671 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300672
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200673 if( ! export_public_key &&
674 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) &&
675 ( slot->policy.usage & PSA_KEY_USAGE_EXPORT ) == 0 )
Moran Peker87567632018-06-04 18:41:37 +0300676 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine2d277862018-06-18 15:41:12 +0200677
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200678 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100679 {
680 if( slot->data.raw.bytes > data_size )
681 return( PSA_ERROR_BUFFER_TOO_SMALL );
682 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
683 *data_length = slot->data.raw.bytes;
684 return( PSA_SUCCESS );
685 }
686 else
Moran Peker17e36e12018-05-02 12:55:20 +0300687 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100688#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100689 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
Moran Pekera998bc62018-04-16 18:16:20 +0300690 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
691 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100692 {
Moran Pekera998bc62018-04-16 18:16:20 +0300693 mbedtls_pk_context pk;
694 int ret;
695 mbedtls_pk_init( &pk );
696 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
697 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
698 {
699 pk.pk_info = &mbedtls_rsa_info;
700 pk.pk_ctx = slot->data.rsa;
701 }
702 else
703 {
704 pk.pk_info = &mbedtls_eckey_info;
705 pk.pk_ctx = slot->data.ecp;
706 }
Moran Pekerd7326592018-05-29 16:56:39 +0300707 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300708 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300709 else
710 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300711 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200712 {
713 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300714 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200715 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200716 /* The mbedtls_pk_xxx functions write to the end of the buffer.
717 * Move the data to the beginning and erase remaining data
718 * at the original location. */
719 if( 2 * (size_t) ret <= data_size )
720 {
721 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200722 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200723 }
724 else if( (size_t) ret < data_size )
725 {
726 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200727 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200728 }
Moran Pekera998bc62018-04-16 18:16:20 +0300729 *data_length = ret;
730 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100731 }
732 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100733#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300734 {
735 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200736 it is valid for a special-purpose implementation to omit
737 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300738 return( PSA_ERROR_NOT_SUPPORTED );
739 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740 }
741}
742
Gilles Peskine2d277862018-06-18 15:41:12 +0200743psa_status_t psa_export_key( psa_key_slot_t key,
744 uint8_t *data,
745 size_t data_size,
746 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300747{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200748 return( psa_internal_export_key( key, data, data_size,
749 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100750}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100751
Gilles Peskine2d277862018-06-18 15:41:12 +0200752psa_status_t psa_export_public_key( psa_key_slot_t key,
753 uint8_t *data,
754 size_t data_size,
755 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300756{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200757 return( psa_internal_export_key( key, data, data_size,
758 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300759}
760
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200761
762
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100763/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100764/* Message digests */
765/****************************************************************/
766
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100767static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100768{
769 switch( alg )
770 {
771#if defined(MBEDTLS_MD2_C)
772 case PSA_ALG_MD2:
773 return( &mbedtls_md2_info );
774#endif
775#if defined(MBEDTLS_MD4_C)
776 case PSA_ALG_MD4:
777 return( &mbedtls_md4_info );
778#endif
779#if defined(MBEDTLS_MD5_C)
780 case PSA_ALG_MD5:
781 return( &mbedtls_md5_info );
782#endif
783#if defined(MBEDTLS_RIPEMD160_C)
784 case PSA_ALG_RIPEMD160:
785 return( &mbedtls_ripemd160_info );
786#endif
787#if defined(MBEDTLS_SHA1_C)
788 case PSA_ALG_SHA_1:
789 return( &mbedtls_sha1_info );
790#endif
791#if defined(MBEDTLS_SHA256_C)
792 case PSA_ALG_SHA_224:
793 return( &mbedtls_sha224_info );
794 case PSA_ALG_SHA_256:
795 return( &mbedtls_sha256_info );
796#endif
797#if defined(MBEDTLS_SHA512_C)
798 case PSA_ALG_SHA_384:
799 return( &mbedtls_sha384_info );
800 case PSA_ALG_SHA_512:
801 return( &mbedtls_sha512_info );
802#endif
803 default:
804 return( NULL );
805 }
806}
807
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100808psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
809{
810 switch( operation->alg )
811 {
Gilles Peskine81736312018-06-26 15:04:31 +0200812 case 0:
813 /* The object has (apparently) been initialized but it is not
814 * in use. It's ok to call abort on such an object, and there's
815 * nothing to do. */
816 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100817#if defined(MBEDTLS_MD2_C)
818 case PSA_ALG_MD2:
819 mbedtls_md2_free( &operation->ctx.md2 );
820 break;
821#endif
822#if defined(MBEDTLS_MD4_C)
823 case PSA_ALG_MD4:
824 mbedtls_md4_free( &operation->ctx.md4 );
825 break;
826#endif
827#if defined(MBEDTLS_MD5_C)
828 case PSA_ALG_MD5:
829 mbedtls_md5_free( &operation->ctx.md5 );
830 break;
831#endif
832#if defined(MBEDTLS_RIPEMD160_C)
833 case PSA_ALG_RIPEMD160:
834 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
835 break;
836#endif
837#if defined(MBEDTLS_SHA1_C)
838 case PSA_ALG_SHA_1:
839 mbedtls_sha1_free( &operation->ctx.sha1 );
840 break;
841#endif
842#if defined(MBEDTLS_SHA256_C)
843 case PSA_ALG_SHA_224:
844 case PSA_ALG_SHA_256:
845 mbedtls_sha256_free( &operation->ctx.sha256 );
846 break;
847#endif
848#if defined(MBEDTLS_SHA512_C)
849 case PSA_ALG_SHA_384:
850 case PSA_ALG_SHA_512:
851 mbedtls_sha512_free( &operation->ctx.sha512 );
852 break;
853#endif
854 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +0200855 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100856 }
857 operation->alg = 0;
858 return( PSA_SUCCESS );
859}
860
861psa_status_t psa_hash_start( psa_hash_operation_t *operation,
862 psa_algorithm_t alg )
863{
864 int ret;
865 operation->alg = 0;
866 switch( alg )
867 {
868#if defined(MBEDTLS_MD2_C)
869 case PSA_ALG_MD2:
870 mbedtls_md2_init( &operation->ctx.md2 );
871 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
872 break;
873#endif
874#if defined(MBEDTLS_MD4_C)
875 case PSA_ALG_MD4:
876 mbedtls_md4_init( &operation->ctx.md4 );
877 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
878 break;
879#endif
880#if defined(MBEDTLS_MD5_C)
881 case PSA_ALG_MD5:
882 mbedtls_md5_init( &operation->ctx.md5 );
883 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
884 break;
885#endif
886#if defined(MBEDTLS_RIPEMD160_C)
887 case PSA_ALG_RIPEMD160:
888 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
889 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
890 break;
891#endif
892#if defined(MBEDTLS_SHA1_C)
893 case PSA_ALG_SHA_1:
894 mbedtls_sha1_init( &operation->ctx.sha1 );
895 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
896 break;
897#endif
898#if defined(MBEDTLS_SHA256_C)
899 case PSA_ALG_SHA_224:
900 mbedtls_sha256_init( &operation->ctx.sha256 );
901 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
902 break;
903 case PSA_ALG_SHA_256:
904 mbedtls_sha256_init( &operation->ctx.sha256 );
905 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
906 break;
907#endif
908#if defined(MBEDTLS_SHA512_C)
909 case PSA_ALG_SHA_384:
910 mbedtls_sha512_init( &operation->ctx.sha512 );
911 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
912 break;
913 case PSA_ALG_SHA_512:
914 mbedtls_sha512_init( &operation->ctx.sha512 );
915 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
916 break;
917#endif
918 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +0200919 return( PSA_ALG_IS_HASH( alg ) ?
920 PSA_ERROR_NOT_SUPPORTED :
921 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100922 }
923 if( ret == 0 )
924 operation->alg = alg;
925 else
926 psa_hash_abort( operation );
927 return( mbedtls_to_psa_error( ret ) );
928}
929
930psa_status_t psa_hash_update( psa_hash_operation_t *operation,
931 const uint8_t *input,
932 size_t input_length )
933{
934 int ret;
935 switch( operation->alg )
936 {
937#if defined(MBEDTLS_MD2_C)
938 case PSA_ALG_MD2:
939 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
940 input, input_length );
941 break;
942#endif
943#if defined(MBEDTLS_MD4_C)
944 case PSA_ALG_MD4:
945 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
946 input, input_length );
947 break;
948#endif
949#if defined(MBEDTLS_MD5_C)
950 case PSA_ALG_MD5:
951 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
952 input, input_length );
953 break;
954#endif
955#if defined(MBEDTLS_RIPEMD160_C)
956 case PSA_ALG_RIPEMD160:
957 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
958 input, input_length );
959 break;
960#endif
961#if defined(MBEDTLS_SHA1_C)
962 case PSA_ALG_SHA_1:
963 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
964 input, input_length );
965 break;
966#endif
967#if defined(MBEDTLS_SHA256_C)
968 case PSA_ALG_SHA_224:
969 case PSA_ALG_SHA_256:
970 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
971 input, input_length );
972 break;
973#endif
974#if defined(MBEDTLS_SHA512_C)
975 case PSA_ALG_SHA_384:
976 case PSA_ALG_SHA_512:
977 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
978 input, input_length );
979 break;
980#endif
981 default:
982 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
983 break;
984 }
985 if( ret != 0 )
986 psa_hash_abort( operation );
987 return( mbedtls_to_psa_error( ret ) );
988}
989
990psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
991 uint8_t *hash,
992 size_t hash_size,
993 size_t *hash_length )
994{
995 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +0200996 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100997
998 /* Fill the output buffer with something that isn't a valid hash
999 * (barring an attack on the hash and deliberately-crafted input),
1000 * in case the caller doesn't check the return status properly. */
1001 *hash_length = actual_hash_length;
1002 memset( hash, '!', hash_size );
1003
1004 if( hash_size < actual_hash_length )
1005 return( PSA_ERROR_BUFFER_TOO_SMALL );
1006
1007 switch( operation->alg )
1008 {
1009#if defined(MBEDTLS_MD2_C)
1010 case PSA_ALG_MD2:
1011 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
1012 break;
1013#endif
1014#if defined(MBEDTLS_MD4_C)
1015 case PSA_ALG_MD4:
1016 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
1017 break;
1018#endif
1019#if defined(MBEDTLS_MD5_C)
1020 case PSA_ALG_MD5:
1021 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1022 break;
1023#endif
1024#if defined(MBEDTLS_RIPEMD160_C)
1025 case PSA_ALG_RIPEMD160:
1026 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1027 break;
1028#endif
1029#if defined(MBEDTLS_SHA1_C)
1030 case PSA_ALG_SHA_1:
1031 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1032 break;
1033#endif
1034#if defined(MBEDTLS_SHA256_C)
1035 case PSA_ALG_SHA_224:
1036 case PSA_ALG_SHA_256:
1037 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1038 break;
1039#endif
1040#if defined(MBEDTLS_SHA512_C)
1041 case PSA_ALG_SHA_384:
1042 case PSA_ALG_SHA_512:
1043 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1044 break;
1045#endif
1046 default:
1047 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1048 break;
1049 }
1050
1051 if( ret == 0 )
1052 {
1053 return( psa_hash_abort( operation ) );
1054 }
1055 else
1056 {
1057 psa_hash_abort( operation );
1058 return( mbedtls_to_psa_error( ret ) );
1059 }
1060}
1061
Gilles Peskine2d277862018-06-18 15:41:12 +02001062psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1063 const uint8_t *hash,
1064 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001065{
1066 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1067 size_t actual_hash_length;
1068 psa_status_t status = psa_hash_finish( operation,
1069 actual_hash, sizeof( actual_hash ),
1070 &actual_hash_length );
1071 if( status != PSA_SUCCESS )
1072 return( status );
1073 if( actual_hash_length != hash_length )
1074 return( PSA_ERROR_INVALID_SIGNATURE );
1075 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1076 return( PSA_ERROR_INVALID_SIGNATURE );
1077 return( PSA_SUCCESS );
1078}
1079
1080
1081
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001082/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001083/* MAC */
1084/****************************************************************/
1085
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001086static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001087 psa_algorithm_t alg,
1088 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001089 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001090 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001091{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001092 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001093 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001094
1095 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1096 {
1097 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001098 {
1099 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1100 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001101
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001102 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001103 {
1104 case PSA_ALG_STREAM_CIPHER:
1105 mode = MBEDTLS_MODE_STREAM;
1106 break;
1107 case PSA_ALG_CBC_BASE:
1108 mode = MBEDTLS_MODE_CBC;
1109 break;
1110 case PSA_ALG_CFB_BASE:
1111 mode = MBEDTLS_MODE_CFB;
1112 break;
1113 case PSA_ALG_OFB_BASE:
1114 mode = MBEDTLS_MODE_OFB;
1115 break;
1116 case PSA_ALG_CTR:
1117 mode = MBEDTLS_MODE_CTR;
1118 break;
1119 case PSA_ALG_CCM:
1120 mode = MBEDTLS_MODE_CCM;
1121 break;
1122 case PSA_ALG_GCM:
1123 mode = MBEDTLS_MODE_GCM;
1124 break;
1125 default:
1126 return( NULL );
1127 }
1128 }
1129 else if( alg == PSA_ALG_CMAC )
1130 mode = MBEDTLS_MODE_ECB;
1131 else if( alg == PSA_ALG_GMAC )
1132 mode = MBEDTLS_MODE_GCM;
1133 else
1134 return( NULL );
1135
1136 switch( key_type )
1137 {
1138 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001139 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001140 break;
1141 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001142 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1143 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001144 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001145 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001146 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001147 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001148 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1149 * but two-key Triple-DES is functionally three-key Triple-DES
1150 * with K1=K3, so that's how we present it to mbedtls. */
1151 if( key_bits == 128 )
1152 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001153 break;
1154 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001155 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001156 break;
1157 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001158 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001159 break;
1160 default:
1161 return( NULL );
1162 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001163 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001164 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001165
Jaeden Amero23bbb752018-06-26 14:16:54 +01001166 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
1167 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001168}
1169
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001170static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001171{
Gilles Peskine2d277862018-06-18 15:41:12 +02001172 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001173 {
1174 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001175 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001176 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001177 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001178 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001179 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001180 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001181 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001182 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001183 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001184 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001185 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001186 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001187 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001188 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001189 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001190 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001191 return( 128 );
1192 default:
1193 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001194 }
1195}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001196
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001197/* Initialize the MAC operation structure. Once this function has been
1198 * called, psa_mac_abort can run and will do the right thing. */
1199static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1200 psa_algorithm_t alg )
1201{
1202 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1203
1204 operation->alg = alg;
1205 operation->key_set = 0;
1206 operation->iv_set = 0;
1207 operation->iv_required = 0;
1208 operation->has_input = 0;
1209 operation->key_usage_sign = 0;
1210 operation->key_usage_verify = 0;
1211
1212#if defined(MBEDTLS_CMAC_C)
1213 if( alg == PSA_ALG_CMAC )
1214 {
1215 operation->iv_required = 0;
1216 mbedtls_cipher_init( &operation->ctx.cmac );
1217 status = PSA_SUCCESS;
1218 }
1219 else
1220#endif /* MBEDTLS_CMAC_C */
1221#if defined(MBEDTLS_MD_C)
1222 if( PSA_ALG_IS_HMAC( operation->alg ) )
1223 {
1224 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1225 PSA_ALG_HMAC_HASH( alg ) );
1226 }
1227 else
1228#endif /* MBEDTLS_MD_C */
1229 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001230 if( ! PSA_ALG_IS_MAC( alg ) )
1231 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001232 }
1233
1234 if( status != PSA_SUCCESS )
1235 memset( operation, 0, sizeof( *operation ) );
1236 return( status );
1237}
1238
Gilles Peskine8c9def32018-02-08 10:02:12 +01001239psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1240{
1241 switch( operation->alg )
1242 {
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001243 case 0:
Gilles Peskine81736312018-06-26 15:04:31 +02001244 /* The object has (apparently) been initialized but it is not
1245 * in use. It's ok to call abort on such an object, and there's
1246 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001247 return( PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001248#if defined(MBEDTLS_CMAC_C)
1249 case PSA_ALG_CMAC:
1250 mbedtls_cipher_free( &operation->ctx.cmac );
1251 break;
1252#endif /* MBEDTLS_CMAC_C */
1253 default:
1254#if defined(MBEDTLS_MD_C)
1255 if( PSA_ALG_IS_HMAC( operation->alg ) )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001256 {
Jaeden Amero5390f692018-06-26 14:18:50 +01001257 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001258 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001259
Gilles Peskine99bc6492018-06-11 17:13:00 +02001260 if( block_size == 0 )
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001261 return( PSA_ERROR_NOT_SUPPORTED );
1262
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001263 psa_hash_abort( &operation->ctx.hmac.hash_ctx );
Gilles Peskine2d277862018-06-18 15:41:12 +02001264 mbedtls_zeroize( operation->ctx.hmac.opad, block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001265 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001266 else
1267#endif /* MBEDTLS_MD_C */
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001268 {
1269 /* Sanity check (shouldn't happen: operation->alg should
1270 * always have been initialized to a valid value). */
1271 return( PSA_ERROR_BAD_STATE );
1272 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001273 }
Moran Peker41deec42018-04-04 15:43:05 +03001274
Gilles Peskine8c9def32018-02-08 10:02:12 +01001275 operation->alg = 0;
1276 operation->key_set = 0;
1277 operation->iv_set = 0;
1278 operation->iv_required = 0;
1279 operation->has_input = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001280 operation->key_usage_sign = 0;
1281 operation->key_usage_verify = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001282
Gilles Peskine8c9def32018-02-08 10:02:12 +01001283 return( PSA_SUCCESS );
1284}
1285
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001286#if defined(MBEDTLS_CMAC_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001287static int psa_cmac_start( psa_mac_operation_t *operation,
1288 size_t key_bits,
1289 key_slot_t *slot,
1290 const mbedtls_cipher_info_t *cipher_info )
1291{
1292 int ret;
1293
1294 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001295
1296 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1297 if( ret != 0 )
1298 return( ret );
1299
1300 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1301 slot->data.raw.data,
1302 key_bits );
1303 return( ret );
1304}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001305#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001306
Gilles Peskine248051a2018-06-20 16:09:38 +02001307#if defined(MBEDTLS_MD_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001308static int psa_hmac_start( psa_mac_operation_t *operation,
1309 psa_key_type_t key_type,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001310 key_slot_t *slot,
1311 psa_algorithm_t alg )
1312{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001313 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001314 unsigned char *opad = operation->ctx.hmac.opad;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001315 size_t i;
Gilles Peskinee1bc6802018-06-11 17:36:05 +02001316 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001317 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001318 unsigned int digest_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001319 PSA_HASH_SIZE( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001320 size_t key_length = slot->data.raw.bytes;
1321 psa_status_t status;
1322
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001323 if( block_size == 0 || digest_size == 0 )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001324 return( PSA_ERROR_NOT_SUPPORTED );
1325
1326 if( key_type != PSA_KEY_TYPE_HMAC )
1327 return( PSA_ERROR_INVALID_ARGUMENT );
1328
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001329 operation->mac_size = digest_size;
1330
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001331 /* The hash was started earlier in psa_mac_init. */
Gilles Peskined223b522018-06-11 18:12:58 +02001332 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001333 {
1334 status = psa_hash_update( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001335 slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001336 if( status != PSA_SUCCESS )
1337 return( status );
1338 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001339 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001340 if( status != PSA_SUCCESS )
1341 return( status );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001342 }
Gilles Peskined223b522018-06-11 18:12:58 +02001343 else
1344 memcpy( ipad, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001345
Gilles Peskined223b522018-06-11 18:12:58 +02001346 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1347 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001348 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001349 ipad[i] ^= 0x36;
1350 memset( ipad + key_length, 0x36, block_size - key_length );
1351
1352 /* Copy the key material from ipad to opad, flipping the requisite bits,
1353 * and filling the rest of opad with the requisite constant. */
1354 for( i = 0; i < key_length; i++ )
1355 opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1356 memset( opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001357
1358 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1359 PSA_ALG_HMAC_HASH( alg ) );
1360 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001361 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001362
1363 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, ipad,
1364 block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001365
1366cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001367 mbedtls_zeroize( ipad, key_length );
1368 /* opad is in the context. It needs to stay in memory if this function
1369 * succeeds, and it will be wiped by psa_mac_abort() called from
1370 * psa_mac_start in the error case. */
1371
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001372 return( status );
1373}
Gilles Peskine248051a2018-06-20 16:09:38 +02001374#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001375
Gilles Peskine8c9def32018-02-08 10:02:12 +01001376psa_status_t psa_mac_start( psa_mac_operation_t *operation,
1377 psa_key_slot_t key,
1378 psa_algorithm_t alg )
1379{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001380 psa_status_t status;
1381 key_slot_t *slot;
1382 psa_key_type_t key_type;
1383 size_t key_bits;
Gilles Peskine828ed142018-06-18 23:25:51 +02001384 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001385
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001386 status = psa_mac_init( operation, alg );
1387 if( status != PSA_SUCCESS )
1388 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001389
1390 status = psa_get_key_information( key, &key_type, &key_bits );
1391 if( status != PSA_SUCCESS )
1392 return( status );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001393
Gilles Peskine8c9def32018-02-08 10:02:12 +01001394 slot = &global_data.key_slots[key];
Gilles Peskine99bc6492018-06-11 17:13:00 +02001395 if( slot->type == PSA_KEY_TYPE_NONE )
1396 return( PSA_ERROR_EMPTY_SLOT );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001397
Moran Pekerd7326592018-05-29 16:56:39 +03001398 if( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001399 operation->key_usage_sign = 1;
1400
Moran Pekerd7326592018-05-29 16:56:39 +03001401 if( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001402 operation->key_usage_verify = 1;
1403
Gilles Peskine8c9def32018-02-08 10:02:12 +01001404 if( ! PSA_ALG_IS_HMAC( alg ) )
1405 {
mohammad1603f4f0d612018-06-03 15:04:51 +03001406 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001407 if( cipher_info == NULL )
1408 return( PSA_ERROR_NOT_SUPPORTED );
1409 operation->mac_size = cipher_info->block_size;
1410 }
1411 switch( alg )
1412 {
1413#if defined(MBEDTLS_CMAC_C)
1414 case PSA_ALG_CMAC:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001415 status = mbedtls_to_psa_error( psa_cmac_start( operation,
1416 key_bits,
1417 slot,
1418 cipher_info ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001419 break;
1420#endif /* MBEDTLS_CMAC_C */
1421 default:
1422#if defined(MBEDTLS_MD_C)
1423 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskined223b522018-06-11 18:12:58 +02001424 status = psa_hmac_start( operation, key_type, slot, alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001425 else
1426#endif /* MBEDTLS_MD_C */
1427 return( PSA_ERROR_NOT_SUPPORTED );
1428 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001429
Gilles Peskine8c9def32018-02-08 10:02:12 +01001430 /* If we reach this point, then the algorithm-specific part of the
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001431 * context may contain data that needs to be wiped on error. */
1432 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001433 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001434 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001435 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001436 else
1437 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001438 operation->key_set = 1;
1439 }
1440 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001441}
1442
1443psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1444 const uint8_t *input,
1445 size_t input_length )
1446{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001447 int ret = 0 ;
1448 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001449 if( ! operation->key_set )
1450 return( PSA_ERROR_BAD_STATE );
1451 if( operation->iv_required && ! operation->iv_set )
1452 return( PSA_ERROR_BAD_STATE );
1453 operation->has_input = 1;
1454
1455 switch( operation->alg )
1456 {
1457#if defined(MBEDTLS_CMAC_C)
1458 case PSA_ALG_CMAC:
1459 ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1460 input, input_length );
1461 break;
1462#endif /* MBEDTLS_CMAC_C */
1463 default:
1464#if defined(MBEDTLS_MD_C)
1465 if( PSA_ALG_IS_HMAC( operation->alg ) )
1466 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001467 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
Gilles Peskine2d277862018-06-18 15:41:12 +02001468 input_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001469 }
1470 else
1471#endif /* MBEDTLS_MD_C */
1472 {
1473 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1474 }
1475 break;
1476 }
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001477 if( ret != 0 || status != PSA_SUCCESS )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001478 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001479 psa_mac_abort( operation );
Gilles Peskine2d277862018-06-18 15:41:12 +02001480 if( ret != 0 )
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001481 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001482 }
1483
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001484 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001485}
1486
mohammad16036df908f2018-04-02 08:34:15 -07001487static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001488 uint8_t *mac,
1489 size_t mac_size,
1490 size_t *mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001491{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001492 int ret = 0;
1493 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001494 if( ! operation->key_set )
1495 return( PSA_ERROR_BAD_STATE );
1496 if( operation->iv_required && ! operation->iv_set )
1497 return( PSA_ERROR_BAD_STATE );
1498
1499 /* Fill the output buffer with something that isn't a valid mac
1500 * (barring an attack on the mac and deliberately-crafted input),
1501 * in case the caller doesn't check the return status properly. */
1502 *mac_length = operation->mac_size;
1503 memset( mac, '!', mac_size );
1504
1505 if( mac_size < operation->mac_size )
1506 return( PSA_ERROR_BUFFER_TOO_SMALL );
1507
1508 switch( operation->alg )
1509 {
1510#if defined(MBEDTLS_CMAC_C)
1511 case PSA_ALG_CMAC:
1512 ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1513 break;
1514#endif /* MBEDTLS_CMAC_C */
1515 default:
1516#if defined(MBEDTLS_MD_C)
1517 if( PSA_ALG_IS_HMAC( operation->alg ) )
1518 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001519 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001520 unsigned char *opad = operation->ctx.hmac.opad;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001521 size_t hash_size = 0;
Nir Sonnenschein96272412018-06-17 14:41:10 +03001522 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001523 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001524
Gilles Peskine99bc6492018-06-11 17:13:00 +02001525 if( block_size == 0 )
1526 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001527
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001528 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine99bc6492018-06-11 17:13:00 +02001529 sizeof( tmp ), &hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001530 if( status != PSA_SUCCESS )
1531 goto cleanup;
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001532 /* From here on, tmp needs to be wiped. */
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001533
1534 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1535 PSA_ALG_HMAC_HASH( operation->alg ) );
1536 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001537 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001538
1539 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, opad,
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001540 block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001541 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001542 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001543
1544 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine2d277862018-06-18 15:41:12 +02001545 hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001546 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001547 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001548
1549 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, mac,
1550 mac_size, mac_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001551 hmac_cleanup:
1552 mbedtls_zeroize( tmp, hash_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001553 }
1554 else
1555#endif /* MBEDTLS_MD_C */
1556 {
1557 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1558 }
1559 break;
1560 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001561cleanup:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001562
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001563 if( ret == 0 && status == PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001564 {
1565 return( psa_mac_abort( operation ) );
1566 }
1567 else
1568 {
1569 psa_mac_abort( operation );
Gilles Peskine99bc6492018-06-11 17:13:00 +02001570 if( ret != 0 )
Gilles Peskine2d277862018-06-18 15:41:12 +02001571 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001572
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001573 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001574 }
1575}
1576
mohammad16036df908f2018-04-02 08:34:15 -07001577psa_status_t psa_mac_finish( psa_mac_operation_t *operation,
1578 uint8_t *mac,
1579 size_t mac_size,
1580 size_t *mac_length )
1581{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001582 if( ! operation->key_usage_sign )
mohammad16036df908f2018-04-02 08:34:15 -07001583 return( PSA_ERROR_NOT_PERMITTED );
1584
Gilles Peskine99bc6492018-06-11 17:13:00 +02001585 return( psa_mac_finish_internal( operation, mac,
1586 mac_size, mac_length ) );
mohammad16036df908f2018-04-02 08:34:15 -07001587}
1588
Gilles Peskine8c9def32018-02-08 10:02:12 +01001589psa_status_t psa_mac_verify( psa_mac_operation_t *operation,
1590 const uint8_t *mac,
1591 size_t mac_length )
1592{
Gilles Peskine828ed142018-06-18 23:25:51 +02001593 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Gilles Peskine8c9def32018-02-08 10:02:12 +01001594 size_t actual_mac_length;
mohammad16036df908f2018-04-02 08:34:15 -07001595 psa_status_t status;
1596
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001597 if( ! operation->key_usage_verify )
mohammad16036df908f2018-04-02 08:34:15 -07001598 return( PSA_ERROR_NOT_PERMITTED );
1599
1600 status = psa_mac_finish_internal( operation,
1601 actual_mac, sizeof( actual_mac ),
1602 &actual_mac_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001603 if( status != PSA_SUCCESS )
1604 return( status );
1605 if( actual_mac_length != mac_length )
1606 return( PSA_ERROR_INVALID_SIGNATURE );
1607 if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
1608 return( PSA_ERROR_INVALID_SIGNATURE );
1609 return( PSA_SUCCESS );
1610}
1611
1612
Gilles Peskine20035e32018-02-03 22:44:14 +01001613
Gilles Peskine20035e32018-02-03 22:44:14 +01001614/****************************************************************/
1615/* Asymmetric cryptography */
1616/****************************************************************/
1617
Gilles Peskine2b450e32018-06-27 15:42:46 +02001618#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001619/* Decode the hash algorithm from alg and store the mbedtls encoding in
1620 * md_alg. Verify that the hash length is consistent. */
1621static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1622 size_t hash_length,
1623 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001624{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02001625 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001626 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1627 *md_alg = hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1628 if( *md_alg == MBEDTLS_MD_NONE )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001629 {
1630#if SIZE_MAX > UINT_MAX
Gilles Peskine61b91d42018-06-08 16:09:36 +02001631 if( hash_length > UINT_MAX )
1632 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001633#endif
1634 }
1635 else
1636 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001637 if( mbedtls_md_get_size( md_info ) != hash_length )
1638 return( PSA_ERROR_INVALID_ARGUMENT );
1639 if( md_info == NULL )
1640 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001641 }
Gilles Peskine61b91d42018-06-08 16:09:36 +02001642 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001643}
1644
Gilles Peskine2b450e32018-06-27 15:42:46 +02001645static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
1646 psa_algorithm_t alg,
1647 const uint8_t *hash,
1648 size_t hash_length,
1649 uint8_t *signature,
1650 size_t signature_size,
1651 size_t *signature_length )
1652{
1653 psa_status_t status;
1654 int ret;
1655 mbedtls_md_type_t md_alg;
1656
1657 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1658 if( status != PSA_SUCCESS )
1659 return( status );
1660
1661 if( signature_size < rsa->len )
1662 return( PSA_ERROR_BUFFER_TOO_SMALL );
1663
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001664 /* The Mbed TLS RSA module uses an unsigned int for hash_length. See if
1665 * hash_length will fit and return an error if it doesn't. */
1666#if defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21)
1667#if SIZE_MAX > UINT_MAX
1668 if( hash_length > UINT_MAX )
1669 return( PSA_ERROR_NOT_SUPPORTED );
1670#endif
1671#endif
1672
Gilles Peskine2b450e32018-06-27 15:42:46 +02001673#if defined(MBEDTLS_PKCS1_V15)
1674 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1675 {
1676 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1677 MBEDTLS_MD_NONE );
1678 ret = mbedtls_rsa_pkcs1_sign( rsa,
1679 mbedtls_ctr_drbg_random,
1680 &global_data.ctr_drbg,
1681 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001682 md_alg,
1683 (unsigned int) hash_length,
1684 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001685 signature );
1686 }
1687 else
1688#endif /* MBEDTLS_PKCS1_V15 */
1689#if defined(MBEDTLS_PKCS1_V21)
1690 if( PSA_ALG_IS_RSA_PSS( alg ) )
1691 {
1692 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1693 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1694 mbedtls_ctr_drbg_random,
1695 &global_data.ctr_drbg,
1696 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001697 md_alg,
1698 (unsigned int) hash_length,
1699 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001700 signature );
1701 }
1702 else
1703#endif /* MBEDTLS_PKCS1_V21 */
1704 {
1705 return( PSA_ERROR_INVALID_ARGUMENT );
1706 }
1707
1708 if( ret == 0 )
1709 *signature_length = rsa->len;
1710 return( mbedtls_to_psa_error( ret ) );
1711}
1712
1713static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
1714 psa_algorithm_t alg,
1715 const uint8_t *hash,
1716 size_t hash_length,
1717 const uint8_t *signature,
1718 size_t signature_length )
1719{
1720 psa_status_t status;
1721 int ret;
1722 mbedtls_md_type_t md_alg;
1723
1724 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
1725 if( status != PSA_SUCCESS )
1726 return( status );
1727
1728 if( signature_length < rsa->len )
1729 return( PSA_ERROR_BUFFER_TOO_SMALL );
1730
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001731#if defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21)
1732#if SIZE_MAX > UINT_MAX
1733 /* The Mbed TLS RSA module uses an unsigned int for hash_length. See if
1734 * hash_length will fit and return an error if it doesn't. */
1735 if( hash_length > UINT_MAX )
1736 return( PSA_ERROR_NOT_SUPPORTED );
1737#endif
1738#endif
1739
Gilles Peskine2b450e32018-06-27 15:42:46 +02001740#if defined(MBEDTLS_PKCS1_V15)
1741 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
1742 {
1743 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1744 MBEDTLS_MD_NONE );
1745 ret = mbedtls_rsa_pkcs1_verify( rsa,
1746 mbedtls_ctr_drbg_random,
1747 &global_data.ctr_drbg,
1748 MBEDTLS_RSA_PUBLIC,
1749 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001750 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001751 hash,
1752 signature );
1753 }
1754 else
1755#endif /* MBEDTLS_PKCS1_V15 */
1756#if defined(MBEDTLS_PKCS1_V21)
1757 if( PSA_ALG_IS_RSA_PSS( alg ) )
1758 {
1759 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1760 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1761 mbedtls_ctr_drbg_random,
1762 &global_data.ctr_drbg,
1763 MBEDTLS_RSA_PUBLIC,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01001764 md_alg,
1765 (unsigned int) hash_length,
1766 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02001767 signature );
1768 }
1769 else
1770#endif /* MBEDTLS_PKCS1_V21 */
1771 {
1772 return( PSA_ERROR_INVALID_ARGUMENT );
1773 }
1774 return( mbedtls_to_psa_error( ret ) );
1775}
1776#endif /* MBEDTLS_RSA_C */
1777
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001778#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001779/* `ecp` cannot be const because `ecp->grp` needs to be non-const
1780 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
1781 * (even though these functions don't modify it). */
1782static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
1783 psa_algorithm_t alg,
1784 const uint8_t *hash,
1785 size_t hash_length,
1786 uint8_t *signature,
1787 size_t signature_size,
1788 size_t *signature_length )
1789{
1790 int ret;
1791 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001792 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001793 mbedtls_mpi_init( &r );
1794 mbedtls_mpi_init( &s );
1795
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001796 if( signature_size < 2 * curve_bytes )
1797 {
1798 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
1799 goto cleanup;
1800 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001801
1802 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
1803 {
1804 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
1805 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1806 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
1807 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det( &ecp->grp, &r, &s, &ecp->d,
1808 hash, hash_length,
1809 md_alg ) );
1810 }
1811 else
1812 {
1813 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
1814 hash, hash_length,
1815 mbedtls_ctr_drbg_random,
1816 &global_data.ctr_drbg ) );
1817 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001818
1819 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
1820 signature,
1821 curve_bytes ) );
1822 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
1823 signature + curve_bytes,
1824 curve_bytes ) );
1825
1826cleanup:
1827 mbedtls_mpi_free( &r );
1828 mbedtls_mpi_free( &s );
1829 if( ret == 0 )
1830 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001831 return( mbedtls_to_psa_error( ret ) );
1832}
1833
1834static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
1835 const uint8_t *hash,
1836 size_t hash_length,
1837 const uint8_t *signature,
1838 size_t signature_length )
1839{
1840 int ret;
1841 mbedtls_mpi r, s;
1842 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
1843 mbedtls_mpi_init( &r );
1844 mbedtls_mpi_init( &s );
1845
1846 if( signature_length != 2 * curve_bytes )
1847 return( PSA_ERROR_INVALID_SIGNATURE );
1848
1849 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
1850 signature,
1851 curve_bytes ) );
1852 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
1853 signature + curve_bytes,
1854 curve_bytes ) );
1855
1856 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
1857 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001858
1859cleanup:
1860 mbedtls_mpi_free( &r );
1861 mbedtls_mpi_free( &s );
1862 return( mbedtls_to_psa_error( ret ) );
1863}
1864#endif /* MBEDTLS_ECDSA_C */
1865
Gilles Peskine61b91d42018-06-08 16:09:36 +02001866psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
1867 psa_algorithm_t alg,
1868 const uint8_t *hash,
1869 size_t hash_length,
1870 const uint8_t *salt,
1871 size_t salt_length,
1872 uint8_t *signature,
1873 size_t signature_size,
1874 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01001875{
1876 key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001877 psa_status_t status;
1878
1879 *signature_length = signature_size;
1880
Gilles Peskine93aa0332018-02-03 23:58:03 +01001881 (void) salt;
1882 (void) salt_length;
1883
Gilles Peskine828ed142018-06-18 23:25:51 +02001884 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001885 {
1886 status = PSA_ERROR_EMPTY_SLOT;
1887 goto exit;
1888 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001889 slot = &global_data.key_slots[key];
1890 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001891 {
1892 status = PSA_ERROR_EMPTY_SLOT;
1893 goto exit;
1894 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001895 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001896 {
1897 status = PSA_ERROR_INVALID_ARGUMENT;
1898 goto exit;
1899 }
Gilles Peskine61b91d42018-06-08 16:09:36 +02001900 if( ! ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001901 {
1902 status = PSA_ERROR_NOT_PERMITTED;
1903 goto exit;
1904 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001905
Gilles Peskine20035e32018-02-03 22:44:14 +01001906#if defined(MBEDTLS_RSA_C)
1907 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1908 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001909 status = psa_rsa_sign( slot->data.rsa,
1910 alg,
1911 hash, hash_length,
1912 signature, signature_size,
1913 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01001914 }
1915 else
1916#endif /* defined(MBEDTLS_RSA_C) */
1917#if defined(MBEDTLS_ECP_C)
1918 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1919 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001920#if defined(MBEDTLS_ECDSA_C)
1921 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001922 status = psa_ecdsa_sign( slot->data.ecp,
1923 alg,
1924 hash, hash_length,
1925 signature, signature_size,
1926 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001927 else
1928#endif /* defined(MBEDTLS_ECDSA_C) */
1929 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001930 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001931 }
itayzafrir5c753392018-05-08 11:18:38 +03001932 }
1933 else
1934#endif /* defined(MBEDTLS_ECP_C) */
1935 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001936 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01001937 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02001938
1939exit:
1940 /* Fill the unused part of the output buffer (the whole buffer on error,
1941 * the trailing part on success) with something that isn't a valid mac
1942 * (barring an attack on the mac and deliberately-crafted input),
1943 * in case the caller doesn't check the return status properly. */
1944 if( status == PSA_SUCCESS )
1945 memset( signature + *signature_length, '!',
1946 signature_size - *signature_length );
1947 else
1948 memset( signature, '!', signature_size );
1949 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03001950}
1951
1952psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
1953 psa_algorithm_t alg,
1954 const uint8_t *hash,
1955 size_t hash_length,
1956 const uint8_t *salt,
1957 size_t salt_length,
Gilles Peskinee9191ff2018-06-27 14:58:41 +02001958 const uint8_t *signature,
Gilles Peskine526fab02018-06-27 18:19:40 +02001959 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03001960{
1961 key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02001962
itayzafrir5c753392018-05-08 11:18:38 +03001963 (void) salt;
1964 (void) salt_length;
1965
Gilles Peskine828ed142018-06-18 23:25:51 +02001966 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
itayzafrir5c753392018-05-08 11:18:38 +03001967 return( PSA_ERROR_INVALID_ARGUMENT );
1968 slot = &global_data.key_slots[key];
1969 if( slot->type == PSA_KEY_TYPE_NONE )
1970 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001971 if( ! ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) )
itayzafrir5c753392018-05-08 11:18:38 +03001972 return( PSA_ERROR_NOT_PERMITTED );
1973
Gilles Peskine61b91d42018-06-08 16:09:36 +02001974#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001975 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
1976 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001977 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02001978 return( psa_rsa_verify( slot->data.rsa,
1979 alg,
1980 hash, hash_length,
1981 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001982 }
1983 else
1984#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03001985#if defined(MBEDTLS_ECP_C)
1986 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1987 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001988#if defined(MBEDTLS_ECDSA_C)
1989 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02001990 return( psa_ecdsa_verify( slot->data.ecp,
1991 hash, hash_length,
1992 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02001993 else
1994#endif /* defined(MBEDTLS_ECDSA_C) */
1995 {
1996 return( PSA_ERROR_INVALID_ARGUMENT );
1997 }
itayzafrir5c753392018-05-08 11:18:38 +03001998 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001999 else
2000#endif /* defined(MBEDTLS_ECP_C) */
2001 {
2002 return( PSA_ERROR_NOT_SUPPORTED );
2003 }
2004}
2005
Gilles Peskine61b91d42018-06-08 16:09:36 +02002006psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
2007 psa_algorithm_t alg,
2008 const uint8_t *input,
2009 size_t input_length,
2010 const uint8_t *salt,
2011 size_t salt_length,
2012 uint8_t *output,
2013 size_t output_size,
2014 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002015{
2016 key_slot_t *slot;
2017 (void) salt;
2018 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002019 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002020
Gilles Peskine828ed142018-06-18 23:25:51 +02002021 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein7f5a3192018-05-06 22:26:54 +03002022 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002023 slot = &global_data.key_slots[key];
2024 if( slot->type == PSA_KEY_TYPE_NONE )
2025 return( PSA_ERROR_EMPTY_SLOT );
2026 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2027 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002028 if( ! ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) )
2029 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002030
2031#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002032 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
2033 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002034 {
2035 mbedtls_rsa_context *rsa = slot->data.rsa;
2036 int ret;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002037 if( output_size < rsa->len )
Gilles Peskine61b91d42018-06-08 16:09:36 +02002038 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002039#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002040 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002041 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002042 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
2043 mbedtls_ctr_drbg_random,
2044 &global_data.ctr_drbg,
2045 MBEDTLS_RSA_PUBLIC,
2046 input_length,
2047 input,
2048 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002049 }
2050 else
2051#endif /* MBEDTLS_PKCS1_V15 */
2052#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002053 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002054 {
2055 return( PSA_ERROR_NOT_SUPPORTED );
2056 }
2057 else
2058#endif /* MBEDTLS_PKCS1_V21 */
2059 {
2060 return( PSA_ERROR_INVALID_ARGUMENT );
2061 }
2062 if( ret == 0 )
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002063 *output_length = rsa->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002064 return( mbedtls_to_psa_error( ret ) );
2065 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002066 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002067#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002068 {
2069 return( PSA_ERROR_NOT_SUPPORTED );
2070 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002071}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002072
Gilles Peskine61b91d42018-06-08 16:09:36 +02002073psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
2074 psa_algorithm_t alg,
2075 const uint8_t *input,
2076 size_t input_length,
2077 const uint8_t *salt,
2078 size_t salt_length,
2079 uint8_t *output,
2080 size_t output_size,
2081 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002082{
2083 key_slot_t *slot;
2084 (void) salt;
2085 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03002086 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002087
Gilles Peskine828ed142018-06-18 23:25:51 +02002088 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002089 return( PSA_ERROR_EMPTY_SLOT );
2090 slot = &global_data.key_slots[key];
2091 if( slot->type == PSA_KEY_TYPE_NONE )
2092 return( PSA_ERROR_EMPTY_SLOT );
2093 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
2094 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02002095 if( ! ( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
2096 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002097
2098#if defined(MBEDTLS_RSA_C)
2099 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
2100 {
2101 mbedtls_rsa_context *rsa = slot->data.rsa;
2102 int ret;
2103
Gilles Peskinec4def2f2018-06-08 17:53:48 +02002104 if( input_length != rsa->len )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002105 return( PSA_ERROR_INVALID_ARGUMENT );
2106
2107#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02002108 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002109 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02002110 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
2111 mbedtls_ctr_drbg_random,
2112 &global_data.ctr_drbg,
2113 MBEDTLS_RSA_PRIVATE,
2114 output_length,
2115 input,
2116 output,
2117 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002118 }
2119 else
2120#endif /* MBEDTLS_PKCS1_V15 */
2121#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02002122 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002123 {
2124 return( PSA_ERROR_NOT_SUPPORTED );
2125 }
2126 else
2127#endif /* MBEDTLS_PKCS1_V21 */
2128 {
2129 return( PSA_ERROR_INVALID_ARGUMENT );
2130 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03002131
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002132 return( mbedtls_to_psa_error( ret ) );
2133 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002134 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02002135#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03002136 {
2137 return( PSA_ERROR_NOT_SUPPORTED );
2138 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002139}
Gilles Peskine20035e32018-02-03 22:44:14 +01002140
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002141
2142
mohammad1603503973b2018-03-12 15:59:30 +02002143/****************************************************************/
2144/* Symmetric cryptography */
2145/****************************************************************/
2146
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002147/* Initialize the cipher operation structure. Once this function has been
2148 * called, psa_cipher_abort can run and will do the right thing. */
2149static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
2150 psa_algorithm_t alg )
2151{
Gilles Peskinec06e0712018-06-20 16:21:04 +02002152 if( ! PSA_ALG_IS_CIPHER( alg ) )
2153 {
2154 memset( operation, 0, sizeof( *operation ) );
2155 return( PSA_ERROR_INVALID_ARGUMENT );
2156 }
2157
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002158 operation->alg = alg;
2159 operation->key_set = 0;
2160 operation->iv_set = 0;
2161 operation->iv_required = 1;
2162 operation->iv_size = 0;
2163 operation->block_size = 0;
2164 mbedtls_cipher_init( &operation->ctx.cipher );
2165 return( PSA_SUCCESS );
2166}
2167
Gilles Peskinee553c652018-06-04 16:22:46 +02002168static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
2169 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02002170 psa_algorithm_t alg,
2171 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02002172{
2173 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
2174 psa_status_t status;
2175 key_slot_t *slot;
2176 psa_key_type_t key_type;
2177 size_t key_bits;
2178 const mbedtls_cipher_info_t *cipher_info = NULL;
2179
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002180 status = psa_cipher_init( operation, alg );
2181 if( status != PSA_SUCCESS )
2182 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02002183
2184 status = psa_get_key_information( key, &key_type, &key_bits );
2185 if( status != PSA_SUCCESS )
2186 return( status );
2187 slot = &global_data.key_slots[key];
2188
Gilles Peskinebb1072f2018-06-08 18:46:05 +02002189 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002190 if( cipher_info == NULL )
2191 return( PSA_ERROR_NOT_SUPPORTED );
2192
mohammad1603503973b2018-03-12 15:59:30 +02002193 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002194 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002195 {
2196 psa_cipher_abort( operation );
2197 return( mbedtls_to_psa_error( ret ) );
2198 }
2199
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002200#if defined(MBEDTLS_DES_C)
2201 if( key_type == PSA_KEY_TYPE_DES && key_bits == 128 )
2202 {
2203 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2204 unsigned char keys[24];
2205 memcpy( keys, slot->data.raw.data, 16 );
2206 memcpy( keys + 16, slot->data.raw.data, 8 );
2207 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2208 keys,
2209 192, cipher_operation );
2210 }
2211 else
2212#endif
2213 {
2214 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2215 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002216 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002217 }
Moran Peker41deec42018-04-04 15:43:05 +03002218 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002219 {
2220 psa_cipher_abort( operation );
2221 return( mbedtls_to_psa_error( ret ) );
2222 }
2223
mohammad16038481e742018-03-18 13:57:31 +02002224#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002225 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002226 {
Gilles Peskine53514202018-06-06 15:11:46 +02002227 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2228 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002229
Moran Pekera28258c2018-05-29 16:25:04 +03002230 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002231 {
2232 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2233 mode = MBEDTLS_PADDING_PKCS7;
2234 break;
2235 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2236 mode = MBEDTLS_PADDING_NONE;
2237 break;
2238 default:
Moran Pekerae382792018-05-31 14:06:17 +03002239 psa_cipher_abort( operation );
2240 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002241 }
2242 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002243 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002244 {
2245 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002246 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002247 }
mohammad16038481e742018-03-18 13:57:31 +02002248 }
2249#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2250
mohammad1603503973b2018-03-12 15:59:30 +02002251 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002252 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
2253 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) :
2254 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002255 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002256 {
mohammad160389e0f462018-04-12 08:48:45 +03002257 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
mohammad16038481e742018-03-18 13:57:31 +02002258 }
mohammad1603503973b2018-03-12 15:59:30 +02002259
Moran Peker395db872018-05-31 14:07:14 +03002260 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002261}
2262
Gilles Peskinee553c652018-06-04 16:22:46 +02002263psa_status_t psa_encrypt_setup( psa_cipher_operation_t *operation,
2264 psa_key_slot_t key,
2265 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002266{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002267 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002268}
2269
Gilles Peskinee553c652018-06-04 16:22:46 +02002270psa_status_t psa_decrypt_setup( psa_cipher_operation_t *operation,
2271 psa_key_slot_t key,
2272 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002273{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002274 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002275}
2276
Gilles Peskinee553c652018-06-04 16:22:46 +02002277psa_status_t psa_encrypt_generate_iv( psa_cipher_operation_t *operation,
2278 unsigned char *iv,
2279 size_t iv_size,
2280 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002281{
Moran Peker41deec42018-04-04 15:43:05 +03002282 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002283 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002284 return( PSA_ERROR_BAD_STATE );
2285 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002286 {
Moran Peker41deec42018-04-04 15:43:05 +03002287 ret = PSA_ERROR_BUFFER_TOO_SMALL;
2288 goto exit;
2289 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002290 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2291 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002292 if( ret != 0 )
2293 {
2294 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002295 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002296 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002297
mohammad16038481e742018-03-18 13:57:31 +02002298 *iv_length = operation->iv_size;
mohammad160389e0f462018-04-12 08:48:45 +03002299 ret = psa_encrypt_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002300
Moran Peker395db872018-05-31 14:07:14 +03002301exit:
2302 if( ret != PSA_SUCCESS )
2303 psa_cipher_abort( operation );
2304 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02002305}
2306
Gilles Peskinee553c652018-06-04 16:22:46 +02002307psa_status_t psa_encrypt_set_iv( psa_cipher_operation_t *operation,
2308 const unsigned char *iv,
2309 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002310{
Moran Peker41deec42018-04-04 15:43:05 +03002311 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002312 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002313 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03002314 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002315 {
Moran Pekerae382792018-05-31 14:06:17 +03002316 psa_cipher_abort( operation );
2317 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03002318 }
mohammad1603503973b2018-03-12 15:59:30 +02002319 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002320 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002321 {
2322 psa_cipher_abort( operation );
2323 return( mbedtls_to_psa_error( ret ) );
2324 }
2325
2326 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02002327
Moran Peker395db872018-05-31 14:07:14 +03002328 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002329}
2330
Gilles Peskinee553c652018-06-04 16:22:46 +02002331psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2332 const uint8_t *input,
2333 size_t input_length,
2334 unsigned char *output,
2335 size_t output_size,
2336 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002337{
2338 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002339 size_t expected_output_size;
2340 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2341 {
2342 /* Take the unprocessed partial block left over from previous
2343 * update calls, if any, plus the input to this call. Remove
2344 * the last partial block, if any. You get the data that will be
2345 * output in this call. */
2346 expected_output_size =
2347 ( operation->ctx.cipher.unprocessed_len + input_length )
2348 / operation->block_size * operation->block_size;
2349 }
2350 else
2351 {
2352 expected_output_size = input_length;
2353 }
2354 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03002355 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02002356
mohammad1603503973b2018-03-12 15:59:30 +02002357 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002358 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002359 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002360 {
2361 psa_cipher_abort( operation );
2362 return( mbedtls_to_psa_error( ret ) );
2363 }
2364
Moran Peker395db872018-05-31 14:07:14 +03002365 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002366}
2367
Gilles Peskinee553c652018-06-04 16:22:46 +02002368psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2369 uint8_t *output,
2370 size_t output_size,
2371 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002372{
2373 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002374 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002375
mohammad1603503973b2018-03-12 15:59:30 +02002376 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002377 {
Moran Peker7cb22b82018-06-05 11:40:02 +03002378 psa_cipher_abort( operation );
2379 return( PSA_ERROR_BAD_STATE );
2380 }
2381 if( operation->iv_required && ! operation->iv_set )
2382 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002383 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002384 return( PSA_ERROR_BAD_STATE );
2385 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002386 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2387 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002388 {
Gilles Peskine53514202018-06-06 15:11:46 +02002389 psa_algorithm_t padding_mode =
2390 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002391 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002392 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002393 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002394 return( PSA_ERROR_TAMPERING_DETECTED );
2395 }
Gilles Peskine53514202018-06-06 15:11:46 +02002396 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002397 {
2398 if( operation->ctx.cipher.unprocessed_len != 0 )
2399 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002400 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002401 return( PSA_ERROR_INVALID_ARGUMENT );
2402 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002403 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002404 }
2405
2406 ret = mbedtls_cipher_finish( &operation->ctx.cipher, temp_output_buffer,
Gilles Peskinee553c652018-06-04 16:22:46 +02002407 output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002408 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002409 {
2410 psa_cipher_abort( operation );
2411 return( mbedtls_to_psa_error( ret ) );
2412 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002413 if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002414 memcpy( output, temp_output_buffer, *output_length );
2415 else
2416 {
2417 psa_cipher_abort( operation );
2418 return( PSA_ERROR_BUFFER_TOO_SMALL );
2419 }
mohammad1603503973b2018-03-12 15:59:30 +02002420
Moran Peker4c80d832018-04-22 20:15:31 +03002421 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002422}
2423
Gilles Peskinee553c652018-06-04 16:22:46 +02002424psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2425{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002426 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002427 {
2428 /* The object has (apparently) been initialized but it is not
2429 * in use. It's ok to call abort on such an object, and there's
2430 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002431 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002432 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002433
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002434 /* Sanity check (shouldn't happen: operation->alg should
2435 * always have been initialized to a valid value). */
2436 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2437 return( PSA_ERROR_BAD_STATE );
2438
mohammad1603503973b2018-03-12 15:59:30 +02002439 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002440
Moran Peker41deec42018-04-04 15:43:05 +03002441 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002442 operation->key_set = 0;
2443 operation->iv_set = 0;
2444 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002445 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002446 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002447
Moran Peker395db872018-05-31 14:07:14 +03002448 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002449}
2450
Gilles Peskinea0655c32018-04-30 17:06:50 +02002451
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002452
mohammad16038cc1cee2018-03-28 01:21:33 +03002453/****************************************************************/
2454/* Key Policy */
2455/****************************************************************/
2456
Gilles Peskine2d277862018-06-18 15:41:12 +02002457void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002458{
Gilles Peskine803ce742018-06-18 16:07:14 +02002459 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002460}
2461
Gilles Peskine2d277862018-06-18 15:41:12 +02002462void psa_key_policy_set_usage( psa_key_policy_t *policy,
2463 psa_key_usage_t usage,
2464 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002465{
mohammad16034eed7572018-03-28 05:14:59 -07002466 policy->usage = usage;
2467 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002468}
2469
Gilles Peskine2d277862018-06-18 15:41:12 +02002470psa_key_usage_t psa_key_policy_get_usage( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002471{
mohammad16036df908f2018-04-02 08:34:15 -07002472 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002473}
2474
Gilles Peskine2d277862018-06-18 15:41:12 +02002475psa_algorithm_t psa_key_policy_get_algorithm( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002476{
mohammad16036df908f2018-04-02 08:34:15 -07002477 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002478}
2479
Gilles Peskine2d277862018-06-18 15:41:12 +02002480psa_status_t psa_set_key_policy( psa_key_slot_t key,
2481 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002482{
2483 key_slot_t *slot;
mohammad16038cc1cee2018-03-28 01:21:33 +03002484
Gilles Peskine828ed142018-06-18 23:25:51 +02002485 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002486 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002487
mohammad16038cc1cee2018-03-28 01:21:33 +03002488 slot = &global_data.key_slots[key];
2489 if( slot->type != PSA_KEY_TYPE_NONE )
2490 return( PSA_ERROR_OCCUPIED_SLOT );
2491
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002492 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2493 PSA_KEY_USAGE_ENCRYPT |
2494 PSA_KEY_USAGE_DECRYPT |
2495 PSA_KEY_USAGE_SIGN |
2496 PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002497 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002498
mohammad16036df908f2018-04-02 08:34:15 -07002499 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002500
2501 return( PSA_SUCCESS );
2502}
2503
Gilles Peskine2d277862018-06-18 15:41:12 +02002504psa_status_t psa_get_key_policy( psa_key_slot_t key,
2505 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002506{
2507 key_slot_t *slot;
2508
Gilles Peskine828ed142018-06-18 23:25:51 +02002509 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002510 return( PSA_ERROR_INVALID_ARGUMENT );
2511
2512 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002513
mohammad16036df908f2018-04-02 08:34:15 -07002514 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002515
2516 return( PSA_SUCCESS );
2517}
Gilles Peskine20035e32018-02-03 22:44:14 +01002518
Gilles Peskinea0655c32018-04-30 17:06:50 +02002519
2520
mohammad1603804cd712018-03-20 22:44:08 +02002521/****************************************************************/
2522/* Key Lifetime */
2523/****************************************************************/
2524
Gilles Peskine2d277862018-06-18 15:41:12 +02002525psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2526 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002527{
2528 key_slot_t *slot;
2529
Gilles Peskine828ed142018-06-18 23:25:51 +02002530 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002531 return( PSA_ERROR_INVALID_ARGUMENT );
2532
2533 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002534
mohammad1603804cd712018-03-20 22:44:08 +02002535 *lifetime = slot->lifetime;
2536
2537 return( PSA_SUCCESS );
2538}
2539
Gilles Peskine2d277862018-06-18 15:41:12 +02002540psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
Jaeden Amero65fb2362018-06-26 13:55:30 +01002541 psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002542{
2543 key_slot_t *slot;
2544
Gilles Peskine828ed142018-06-18 23:25:51 +02002545 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002546 return( PSA_ERROR_INVALID_ARGUMENT );
2547
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002548 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2549 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002550 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2551 return( PSA_ERROR_INVALID_ARGUMENT );
2552
mohammad1603804cd712018-03-20 22:44:08 +02002553 slot = &global_data.key_slots[key];
mohammad16035d7ec202018-03-28 01:29:41 +03002554 if( slot->type != PSA_KEY_TYPE_NONE )
2555 return( PSA_ERROR_OCCUPIED_SLOT );
mohammad1603804cd712018-03-20 22:44:08 +02002556
Moran Pekerd7326592018-05-29 16:56:39 +03002557 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002558 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002559
mohammad1603060ad8a2018-03-20 14:28:38 -07002560 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002561
2562 return( PSA_SUCCESS );
2563}
2564
Gilles Peskine20035e32018-02-03 22:44:14 +01002565
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002566
mohammad16035955c982018-04-26 00:53:03 +03002567/****************************************************************/
2568/* AEAD */
2569/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002570
mohammad16035955c982018-04-26 00:53:03 +03002571psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2572 psa_algorithm_t alg,
2573 const uint8_t *nonce,
2574 size_t nonce_length,
2575 const uint8_t *additional_data,
2576 size_t additional_data_length,
2577 const uint8_t *plaintext,
2578 size_t plaintext_length,
2579 uint8_t *ciphertext,
2580 size_t ciphertext_size,
2581 size_t *ciphertext_length )
2582{
2583 int ret;
2584 psa_status_t status;
2585 key_slot_t *slot;
2586 psa_key_type_t key_type;
2587 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002588 uint8_t *tag;
2589 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002590 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002591 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002592
mohammad1603f08a5502018-06-03 15:05:47 +03002593 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002594
mohammad16035955c982018-04-26 00:53:03 +03002595 status = psa_get_key_information( key, &key_type, &key_bits );
2596 if( status != PSA_SUCCESS )
2597 return( status );
2598 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002599 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002600 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002601
mohammad16035ed06212018-06-06 13:09:34 +03002602 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2603 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002604 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002605 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002606
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002607 if( ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) == 0 )
mohammad1603f14394b2018-06-04 14:33:19 +03002608 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002609
Gilles Peskine2d277862018-06-18 15:41:12 +02002610 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2611 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002612 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002613
mohammad16035955c982018-04-26 00:53:03 +03002614 if( alg == PSA_ALG_GCM )
2615 {
2616 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002617 tag_length = 16;
2618
mohammad160396910d82018-06-04 14:33:00 +03002619 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2620 return( PSA_ERROR_INVALID_ARGUMENT );
2621
mohammad160315223a82018-06-03 17:19:55 +03002622 //make sure we have place to hold the tag in the ciphertext buffer
2623 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002624 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002625
2626 //update the tag pointer to point to the end of the ciphertext_length
2627 tag = ciphertext + plaintext_length;
2628
mohammad16035955c982018-04-26 00:53:03 +03002629 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002630 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002631 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002632 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002633 if( ret != 0 )
2634 {
2635 mbedtls_gcm_free( &gcm );
2636 return( mbedtls_to_psa_error( ret ) );
2637 }
2638 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002639 plaintext_length, nonce,
2640 nonce_length, additional_data,
2641 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002642 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002643 mbedtls_gcm_free( &gcm );
2644 }
2645 else if( alg == PSA_ALG_CCM )
2646 {
2647 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002648 tag_length = 16;
2649
mohammad160396910d82018-06-04 14:33:00 +03002650 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2651 return( PSA_ERROR_INVALID_ARGUMENT );
2652
mohammad160347ddf3d2018-04-26 01:11:21 +03002653 if( nonce_length < 7 || nonce_length > 13 )
2654 return( PSA_ERROR_INVALID_ARGUMENT );
2655
mohammad160315223a82018-06-03 17:19:55 +03002656 //make sure we have place to hold the tag in the ciphertext buffer
2657 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002658 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002659
2660 //update the tag pointer to point to the end of the ciphertext_length
2661 tag = ciphertext + plaintext_length;
2662
mohammad16035955c982018-04-26 00:53:03 +03002663 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002664 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002665 slot->data.raw.data,
2666 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002667 if( ret != 0 )
2668 {
2669 mbedtls_ccm_free( &ccm );
2670 return( mbedtls_to_psa_error( ret ) );
2671 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002672 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002673 nonce, nonce_length,
2674 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002675 additional_data_length,
2676 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002677 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002678 mbedtls_ccm_free( &ccm );
2679 }
mohammad16035c8845f2018-05-09 05:40:09 -07002680 else
2681 {
mohammad1603554faad2018-06-03 15:07:38 +03002682 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002683 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002684
mohammad160315223a82018-06-03 17:19:55 +03002685 if( ret != 0 )
2686 {
2687 memset( ciphertext, 0, ciphertext_size );
2688 return( mbedtls_to_psa_error( ret ) );
2689 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002690
mohammad160315223a82018-06-03 17:19:55 +03002691 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002692 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002693}
2694
Gilles Peskineee652a32018-06-01 19:23:52 +02002695/* Locate the tag in a ciphertext buffer containing the encrypted data
2696 * followed by the tag. Return the length of the part preceding the tag in
2697 * *plaintext_length. This is the size of the plaintext in modes where
2698 * the encrypted data has the same size as the plaintext, such as
2699 * CCM and GCM. */
2700static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2701 const uint8_t *ciphertext,
2702 size_t ciphertext_length,
2703 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002704 const uint8_t **p_tag )
2705{
2706 size_t payload_length;
2707 if( tag_length > ciphertext_length )
2708 return( PSA_ERROR_INVALID_ARGUMENT );
2709 payload_length = ciphertext_length - tag_length;
2710 if( payload_length > plaintext_size )
2711 return( PSA_ERROR_BUFFER_TOO_SMALL );
2712 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002713 return( PSA_SUCCESS );
2714}
2715
mohammad16035955c982018-04-26 00:53:03 +03002716psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2717 psa_algorithm_t alg,
2718 const uint8_t *nonce,
2719 size_t nonce_length,
2720 const uint8_t *additional_data,
2721 size_t additional_data_length,
2722 const uint8_t *ciphertext,
2723 size_t ciphertext_length,
2724 uint8_t *plaintext,
2725 size_t plaintext_size,
2726 size_t *plaintext_length )
2727{
2728 int ret;
2729 psa_status_t status;
2730 key_slot_t *slot;
2731 psa_key_type_t key_type;
2732 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002733 const uint8_t *tag;
2734 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002735 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002736 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002737
Gilles Peskineee652a32018-06-01 19:23:52 +02002738 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002739
mohammad16035955c982018-04-26 00:53:03 +03002740 status = psa_get_key_information( key, &key_type, &key_bits );
2741 if( status != PSA_SUCCESS )
2742 return( status );
2743 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002744 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002745 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002746
mohammad16035ed06212018-06-06 13:09:34 +03002747 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2748 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002749 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002750 return( PSA_ERROR_NOT_SUPPORTED );
2751
mohammad1603f14394b2018-06-04 14:33:19 +03002752 if( !( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
2753 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002754
Gilles Peskine2d277862018-06-18 15:41:12 +02002755 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2756 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002757 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002758
mohammad16035955c982018-04-26 00:53:03 +03002759 if( alg == PSA_ALG_GCM )
2760 {
2761 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002762
Gilles Peskineee652a32018-06-01 19:23:52 +02002763 tag_length = 16;
2764 status = psa_aead_unpadded_locate_tag( tag_length,
2765 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002766 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002767 if( status != PSA_SUCCESS )
2768 return( status );
2769
mohammad16035955c982018-04-26 00:53:03 +03002770 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002771 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002772 slot->data.raw.data,
2773 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002774 if( ret != 0 )
2775 {
2776 mbedtls_gcm_free( &gcm );
2777 return( mbedtls_to_psa_error( ret ) );
2778 }
mohammad16035955c982018-04-26 00:53:03 +03002779
Gilles Peskineee652a32018-06-01 19:23:52 +02002780 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002781 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002782 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002783 additional_data,
2784 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002785 tag, tag_length,
2786 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002787 mbedtls_gcm_free( &gcm );
2788 }
2789 else if( alg == PSA_ALG_CCM )
2790 {
2791 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002792
mohammad160347ddf3d2018-04-26 01:11:21 +03002793 if( nonce_length < 7 || nonce_length > 13 )
2794 return( PSA_ERROR_INVALID_ARGUMENT );
2795
mohammad16039375f842018-06-03 14:28:24 +03002796 tag_length = 16;
2797 status = psa_aead_unpadded_locate_tag( tag_length,
2798 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002799 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002800 if( status != PSA_SUCCESS )
2801 return( status );
2802
mohammad16035955c982018-04-26 00:53:03 +03002803 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002804 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002805 slot->data.raw.data,
2806 (unsigned int) key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002807 if( ret != 0 )
2808 {
2809 mbedtls_ccm_free( &ccm );
2810 return( mbedtls_to_psa_error( ret ) );
2811 }
mohammad160360a64d02018-06-03 17:20:42 +03002812 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002813 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002814 additional_data,
2815 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002816 ciphertext, plaintext,
2817 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002818 mbedtls_ccm_free( &ccm );
2819 }
mohammad160339574652018-06-01 04:39:53 -07002820 else
2821 {
mohammad1603554faad2018-06-03 15:07:38 +03002822 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07002823 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002824
Gilles Peskineee652a32018-06-01 19:23:52 +02002825 if( ret != 0 )
mohammad160360a64d02018-06-03 17:20:42 +03002826 memset( plaintext, 0, plaintext_size );
2827 else
2828 *plaintext_length = ciphertext_length - tag_length;
2829
Gilles Peskineee652a32018-06-01 19:23:52 +02002830 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03002831}
2832
Gilles Peskinea0655c32018-04-30 17:06:50 +02002833
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002834
Gilles Peskine20035e32018-02-03 22:44:14 +01002835/****************************************************************/
Gilles Peskine05d69892018-06-19 22:00:52 +02002836/* Key generation */
2837/****************************************************************/
2838
2839psa_status_t psa_generate_random( uint8_t *output,
2840 size_t output_size )
2841{
2842 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2843 output, output_size );
2844 return( mbedtls_to_psa_error( ret ) );
2845}
2846
2847psa_status_t psa_generate_key( psa_key_slot_t key,
2848 psa_key_type_t type,
2849 size_t bits,
2850 const void *parameters,
2851 size_t parameters_size )
2852{
Gilles Peskine12313cd2018-06-20 00:20:32 +02002853 key_slot_t *slot;
2854
2855 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
2856 return( PSA_ERROR_INVALID_ARGUMENT );
2857 slot = &global_data.key_slots[key];
2858 if( slot->type != PSA_KEY_TYPE_NONE )
2859 return( PSA_ERROR_OCCUPIED_SLOT );
2860 if( parameters == NULL && parameters_size != 0 )
2861 return( PSA_ERROR_INVALID_ARGUMENT );
2862
Gilles Peskine48c0ea12018-06-21 14:15:31 +02002863 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002864 {
2865 psa_status_t status = prepare_raw_data_slot( type, bits,
2866 &slot->data.raw );
2867 if( status != PSA_SUCCESS )
2868 return( status );
2869 status = psa_generate_random( slot->data.raw.data,
2870 slot->data.raw.bytes );
2871 if( status != PSA_SUCCESS )
2872 {
2873 mbedtls_free( slot->data.raw.data );
2874 return( status );
2875 }
2876#if defined(MBEDTLS_DES_C)
2877 if( type == PSA_KEY_TYPE_DES )
2878 {
2879 mbedtls_des_key_set_parity( slot->data.raw.data );
2880 if( slot->data.raw.bytes >= 16 )
2881 mbedtls_des_key_set_parity( slot->data.raw.data + 8 );
2882 if( slot->data.raw.bytes == 24 )
2883 mbedtls_des_key_set_parity( slot->data.raw.data + 16 );
2884 }
2885#endif /* MBEDTLS_DES_C */
2886 }
2887 else
2888
2889#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
2890 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2891 {
2892 mbedtls_rsa_context *rsa;
2893 int ret;
2894 int exponent = 65537;
Gilles Peskineaf3baab2018-06-27 22:55:52 +02002895 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
2896 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine12313cd2018-06-20 00:20:32 +02002897 if( parameters != NULL )
2898 {
2899 const unsigned *p = parameters;
2900 if( parameters_size != sizeof( *p ) )
2901 return( PSA_ERROR_INVALID_ARGUMENT );
2902 if( *p > INT_MAX )
2903 return( PSA_ERROR_INVALID_ARGUMENT );
2904 exponent = *p;
2905 }
2906 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
2907 if( rsa == NULL )
2908 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2909 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
2910 ret = mbedtls_rsa_gen_key( rsa,
2911 mbedtls_ctr_drbg_random,
2912 &global_data.ctr_drbg,
Jaeden Amero23bbb752018-06-26 14:16:54 +01002913 (unsigned int) bits,
Gilles Peskine12313cd2018-06-20 00:20:32 +02002914 exponent );
2915 if( ret != 0 )
2916 {
2917 mbedtls_rsa_free( rsa );
2918 mbedtls_free( rsa );
2919 return( mbedtls_to_psa_error( ret ) );
2920 }
2921 slot->data.rsa = rsa;
2922 }
2923 else
2924#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
2925
2926#if defined(MBEDTLS_ECP_C)
2927 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
2928 {
2929 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
2930 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
2931 const mbedtls_ecp_curve_info *curve_info =
2932 mbedtls_ecp_curve_info_from_grp_id( grp_id );
2933 mbedtls_ecp_keypair *ecp;
2934 int ret;
2935 if( parameters != NULL )
2936 return( PSA_ERROR_NOT_SUPPORTED );
2937 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
2938 return( PSA_ERROR_NOT_SUPPORTED );
2939 if( curve_info->bit_size != bits )
2940 return( PSA_ERROR_INVALID_ARGUMENT );
2941 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
2942 if( ecp == NULL )
2943 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2944 mbedtls_ecp_keypair_init( ecp );
2945 ret = mbedtls_ecp_gen_key( grp_id, ecp,
2946 mbedtls_ctr_drbg_random,
2947 &global_data.ctr_drbg );
2948 if( ret != 0 )
2949 {
2950 mbedtls_ecp_keypair_free( ecp );
2951 mbedtls_free( ecp );
2952 return( mbedtls_to_psa_error( ret ) );
2953 }
2954 slot->data.ecp = ecp;
2955 }
2956 else
2957#endif /* MBEDTLS_ECP_C */
2958
2959 return( PSA_ERROR_NOT_SUPPORTED );
2960
2961 slot->type = type;
2962 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02002963}
2964
2965
2966/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002967/* Module setup */
2968/****************************************************************/
2969
Gilles Peskinee59236f2018-01-27 23:32:46 +01002970void mbedtls_psa_crypto_free( void )
2971{
Jaeden Amero045bd502018-06-26 14:00:08 +01002972 psa_key_slot_t key;
Gilles Peskine828ed142018-06-18 23:25:51 +02002973 for( key = 1; key < PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002974 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002975 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
2976 mbedtls_entropy_free( &global_data.entropy );
2977 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2978}
2979
2980psa_status_t psa_crypto_init( void )
2981{
2982 int ret;
2983 const unsigned char drbg_seed[] = "PSA";
2984
2985 if( global_data.initialized != 0 )
2986 return( PSA_SUCCESS );
2987
2988 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2989 mbedtls_entropy_init( &global_data.entropy );
2990 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
2991
2992 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
2993 mbedtls_entropy_func,
2994 &global_data.entropy,
2995 drbg_seed, sizeof( drbg_seed ) - 1 );
2996 if( ret != 0 )
2997 goto exit;
2998
Gilles Peskinee4ebc122018-03-07 14:16:44 +01002999 global_data.initialized = 1;
3000
Gilles Peskinee59236f2018-01-27 23:32:46 +01003001exit:
3002 if( ret != 0 )
3003 mbedtls_psa_crypto_free( );
3004 return( mbedtls_to_psa_error( ret ) );
3005}
3006
3007#endif /* MBEDTLS_PSA_CRYPTO_C */