blob: de2bf40a9be8ffe0eb907286ad9feb0c35eb1fcb [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 Peskinea5905292018-02-07 20:59:33 +010043#include "mbedtls/blowfish.h"
44#include "mbedtls/camellia.h"
45#include "mbedtls/cipher.h"
46#include "mbedtls/ccm.h"
47#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010048#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010049#include "mbedtls/des.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010050#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010051#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/error.h"
53#include "mbedtls/gcm.h"
54#include "mbedtls/md2.h"
55#include "mbedtls/md4.h"
56#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010057#include "mbedtls/md.h"
58#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010059#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010060#include "mbedtls/pk_internal.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010062#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/sha1.h"
64#include "mbedtls/sha256.h"
65#include "mbedtls/sha512.h"
66#include "mbedtls/xtea.h"
67
Gilles Peskinee59236f2018-01-27 23:32:46 +010068
69
70/* Implementation that should never be optimized out by the compiler */
71static void mbedtls_zeroize( void *v, size_t n )
72{
73 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
74}
75
Gilles Peskine9ef733f2018-02-07 21:05:37 +010076/* constant-time buffer comparison */
77static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
78{
79 size_t i;
80 unsigned char diff = 0;
81
82 for( i = 0; i < n; i++ )
83 diff |= a[i] ^ b[i];
84
85 return( diff );
86}
87
88
89
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010090/****************************************************************/
91/* Global data, support functions and library management */
92/****************************************************************/
93
94/* Number of key slots (plus one because 0 is not used).
95 * The value is a compile-time constant for now, for simplicity. */
Gilles Peskine828ed142018-06-18 23:25:51 +020096#define PSA_KEY_SLOT_COUNT 32
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010097
Gilles Peskine2d277862018-06-18 15:41:12 +020098typedef struct
99{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100100 psa_key_type_t type;
mohammad16038cc1cee2018-03-28 01:21:33 +0300101 psa_key_policy_t policy;
mohammad1603804cd712018-03-20 22:44:08 +0200102 psa_key_lifetime_t lifetime;
Gilles Peskine2d277862018-06-18 15:41:12 +0200103 union
104 {
105 struct raw_data
106 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100107 uint8_t *data;
108 size_t bytes;
109 } raw;
Gilles Peskine969ac722018-01-28 18:16:59 +0100110#if defined(MBEDTLS_RSA_C)
111 mbedtls_rsa_context *rsa;
112#endif /* MBEDTLS_RSA_C */
113#if defined(MBEDTLS_ECP_C)
114 mbedtls_ecp_keypair *ecp;
115#endif /* MBEDTLS_ECP_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100116 } data;
117} key_slot_t;
118
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200119static int key_type_is_raw_bytes( psa_key_type_t type )
120{
121 psa_key_type_t category = type & PSA_KEY_TYPE_CATEGORY_MASK;
122 return( category == PSA_KEY_TYPE_RAW_DATA ||
123 category == PSA_KEY_TYPE_CATEGORY_SYMMETRIC );
124}
125
Gilles Peskine2d277862018-06-18 15:41:12 +0200126typedef struct
127{
Gilles Peskinee59236f2018-01-27 23:32:46 +0100128 int initialized;
129 mbedtls_entropy_context entropy;
130 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskine828ed142018-06-18 23:25:51 +0200131 key_slot_t key_slots[PSA_KEY_SLOT_COUNT];
Gilles Peskinee59236f2018-01-27 23:32:46 +0100132} psa_global_data_t;
133
134static psa_global_data_t global_data;
135
136static psa_status_t mbedtls_to_psa_error( int ret )
137{
Gilles Peskinea5905292018-02-07 20:59:33 +0100138 /* If there's both a high-level code and low-level code, dispatch on
139 * the high-level code. */
140 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100141 {
142 case 0:
143 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100144
145 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
146 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
147 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
148 return( PSA_ERROR_NOT_SUPPORTED );
149 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
150 return( PSA_ERROR_HARDWARE_FAILURE );
151
152 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
153 return( PSA_ERROR_HARDWARE_FAILURE );
154
Gilles Peskine9a944802018-06-21 09:35:35 +0200155 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
156 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
157 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
158 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
159 case MBEDTLS_ERR_ASN1_INVALID_DATA:
160 return( PSA_ERROR_INVALID_ARGUMENT );
161 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
162 return( PSA_ERROR_INSUFFICIENT_MEMORY );
163 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
164 return( PSA_ERROR_BUFFER_TOO_SMALL );
165
Gilles Peskinea5905292018-02-07 20:59:33 +0100166 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
167 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
168 return( PSA_ERROR_NOT_SUPPORTED );
169 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
170 return( PSA_ERROR_HARDWARE_FAILURE );
171
172 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
173 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
174 return( PSA_ERROR_NOT_SUPPORTED );
175 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
176 return( PSA_ERROR_HARDWARE_FAILURE );
177
178 case MBEDTLS_ERR_CCM_BAD_INPUT:
179 return( PSA_ERROR_INVALID_ARGUMENT );
180 case MBEDTLS_ERR_CCM_AUTH_FAILED:
181 return( PSA_ERROR_INVALID_SIGNATURE );
182 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
183 return( PSA_ERROR_HARDWARE_FAILURE );
184
185 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
186 return( PSA_ERROR_NOT_SUPPORTED );
187 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
188 return( PSA_ERROR_INVALID_ARGUMENT );
189 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
190 return( PSA_ERROR_INSUFFICIENT_MEMORY );
191 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
192 return( PSA_ERROR_INVALID_PADDING );
193 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
194 return( PSA_ERROR_BAD_STATE );
195 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
196 return( PSA_ERROR_INVALID_SIGNATURE );
197 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
198 return( PSA_ERROR_TAMPERING_DETECTED );
199 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
200 return( PSA_ERROR_HARDWARE_FAILURE );
201
202 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
203 return( PSA_ERROR_HARDWARE_FAILURE );
204
205 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
206 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
207 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
208 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
209 return( PSA_ERROR_NOT_SUPPORTED );
210 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
211 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
212
213 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
214 return( PSA_ERROR_NOT_SUPPORTED );
215 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
216 return( PSA_ERROR_HARDWARE_FAILURE );
217
Gilles Peskinee59236f2018-01-27 23:32:46 +0100218 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
219 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
220 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
221 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100222
223 case MBEDTLS_ERR_GCM_AUTH_FAILED:
224 return( PSA_ERROR_INVALID_SIGNATURE );
225 case MBEDTLS_ERR_GCM_BAD_INPUT:
226 return( PSA_ERROR_NOT_SUPPORTED );
227 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
228 return( PSA_ERROR_HARDWARE_FAILURE );
229
230 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
231 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
232 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
233 return( PSA_ERROR_HARDWARE_FAILURE );
234
235 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
236 return( PSA_ERROR_NOT_SUPPORTED );
237 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
238 return( PSA_ERROR_INVALID_ARGUMENT );
239 case MBEDTLS_ERR_MD_ALLOC_FAILED:
240 return( PSA_ERROR_INSUFFICIENT_MEMORY );
241 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
242 return( PSA_ERROR_STORAGE_FAILURE );
243 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
244 return( PSA_ERROR_HARDWARE_FAILURE );
245
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100246 case MBEDTLS_ERR_PK_ALLOC_FAILED:
247 return( PSA_ERROR_INSUFFICIENT_MEMORY );
248 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
249 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
250 return( PSA_ERROR_INVALID_ARGUMENT );
251 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100252 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100253 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
254 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
255 return( PSA_ERROR_INVALID_ARGUMENT );
256 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
257 return( PSA_ERROR_NOT_SUPPORTED );
258 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
259 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
260 return( PSA_ERROR_NOT_PERMITTED );
261 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_PK_INVALID_ALG:
264 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
265 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
266 return( PSA_ERROR_NOT_SUPPORTED );
267 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
268 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100269 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
270 return( PSA_ERROR_HARDWARE_FAILURE );
271
272 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
273 return( PSA_ERROR_HARDWARE_FAILURE );
274
275 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
276 return( PSA_ERROR_INVALID_ARGUMENT );
277 case MBEDTLS_ERR_RSA_INVALID_PADDING:
278 return( PSA_ERROR_INVALID_PADDING );
279 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
280 return( PSA_ERROR_HARDWARE_FAILURE );
281 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
282 return( PSA_ERROR_INVALID_ARGUMENT );
283 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
284 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
285 return( PSA_ERROR_TAMPERING_DETECTED );
286 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
287 return( PSA_ERROR_INVALID_SIGNATURE );
288 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
289 return( PSA_ERROR_BUFFER_TOO_SMALL );
290 case MBEDTLS_ERR_RSA_RNG_FAILED:
291 return( PSA_ERROR_INSUFFICIENT_MEMORY );
292 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
293 return( PSA_ERROR_NOT_SUPPORTED );
294 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
295 return( PSA_ERROR_HARDWARE_FAILURE );
296
297 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
298 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
299 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
300 return( PSA_ERROR_HARDWARE_FAILURE );
301
302 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
303 return( PSA_ERROR_INVALID_ARGUMENT );
304 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
305 return( PSA_ERROR_HARDWARE_FAILURE );
306
itayzafrir5c753392018-05-08 11:18:38 +0300307 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300308 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300309 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300310 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
311 return( PSA_ERROR_BUFFER_TOO_SMALL );
312 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
313 return( PSA_ERROR_NOT_SUPPORTED );
314 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
315 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
316 return( PSA_ERROR_INVALID_SIGNATURE );
317 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
318 return( PSA_ERROR_INSUFFICIENT_MEMORY );
319 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
320 return( PSA_ERROR_HARDWARE_FAILURE );
itayzafrir5c753392018-05-08 11:18:38 +0300321
Gilles Peskinee59236f2018-01-27 23:32:46 +0100322 default:
323 return( PSA_ERROR_UNKNOWN_ERROR );
324 }
325}
326
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200327
328
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100329/****************************************************************/
330/* Key management */
331/****************************************************************/
332
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200333static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
334{
335 switch( grpid )
336 {
337 case MBEDTLS_ECP_DP_SECP192R1:
338 return( PSA_ECC_CURVE_SECP192R1 );
339 case MBEDTLS_ECP_DP_SECP224R1:
340 return( PSA_ECC_CURVE_SECP224R1 );
341 case MBEDTLS_ECP_DP_SECP256R1:
342 return( PSA_ECC_CURVE_SECP256R1 );
343 case MBEDTLS_ECP_DP_SECP384R1:
344 return( PSA_ECC_CURVE_SECP384R1 );
345 case MBEDTLS_ECP_DP_SECP521R1:
346 return( PSA_ECC_CURVE_SECP521R1 );
347 case MBEDTLS_ECP_DP_BP256R1:
348 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
349 case MBEDTLS_ECP_DP_BP384R1:
350 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
351 case MBEDTLS_ECP_DP_BP512R1:
352 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
353 case MBEDTLS_ECP_DP_CURVE25519:
354 return( PSA_ECC_CURVE_CURVE25519 );
355 case MBEDTLS_ECP_DP_SECP192K1:
356 return( PSA_ECC_CURVE_SECP192K1 );
357 case MBEDTLS_ECP_DP_SECP224K1:
358 return( PSA_ECC_CURVE_SECP224K1 );
359 case MBEDTLS_ECP_DP_SECP256K1:
360 return( PSA_ECC_CURVE_SECP256K1 );
361 case MBEDTLS_ECP_DP_CURVE448:
362 return( PSA_ECC_CURVE_CURVE448 );
363 default:
364 return( 0 );
365 }
366}
367
Gilles Peskine12313cd2018-06-20 00:20:32 +0200368static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
369{
370 switch( curve )
371 {
372 case PSA_ECC_CURVE_SECP192R1:
373 return( MBEDTLS_ECP_DP_SECP192R1 );
374 case PSA_ECC_CURVE_SECP224R1:
375 return( MBEDTLS_ECP_DP_SECP224R1 );
376 case PSA_ECC_CURVE_SECP256R1:
377 return( MBEDTLS_ECP_DP_SECP256R1 );
378 case PSA_ECC_CURVE_SECP384R1:
379 return( MBEDTLS_ECP_DP_SECP384R1 );
380 case PSA_ECC_CURVE_SECP521R1:
381 return( MBEDTLS_ECP_DP_SECP521R1 );
382 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
383 return( MBEDTLS_ECP_DP_BP256R1 );
384 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
385 return( MBEDTLS_ECP_DP_BP384R1 );
386 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
387 return( MBEDTLS_ECP_DP_BP512R1 );
388 case PSA_ECC_CURVE_CURVE25519:
389 return( MBEDTLS_ECP_DP_CURVE25519 );
390 case PSA_ECC_CURVE_SECP192K1:
391 return( MBEDTLS_ECP_DP_SECP192K1 );
392 case PSA_ECC_CURVE_SECP224K1:
393 return( MBEDTLS_ECP_DP_SECP224K1 );
394 case PSA_ECC_CURVE_SECP256K1:
395 return( MBEDTLS_ECP_DP_SECP256K1 );
396 case PSA_ECC_CURVE_CURVE448:
397 return( MBEDTLS_ECP_DP_CURVE448 );
398 default:
399 return( MBEDTLS_ECP_DP_NONE );
400 }
401}
402
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200403static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
404 size_t bits,
405 struct raw_data *raw )
406{
407 /* Check that the bit size is acceptable for the key type */
408 switch( type )
409 {
410 case PSA_KEY_TYPE_RAW_DATA:
411#if defined(MBEDTLS_MD_C)
412 case PSA_KEY_TYPE_HMAC:
413#endif
414 break;
415#if defined(MBEDTLS_AES_C)
416 case PSA_KEY_TYPE_AES:
417 if( bits != 128 && bits != 192 && bits != 256 )
418 return( PSA_ERROR_INVALID_ARGUMENT );
419 break;
420#endif
421#if defined(MBEDTLS_CAMELLIA_C)
422 case PSA_KEY_TYPE_CAMELLIA:
423 if( bits != 128 && bits != 192 && bits != 256 )
424 return( PSA_ERROR_INVALID_ARGUMENT );
425 break;
426#endif
427#if defined(MBEDTLS_DES_C)
428 case PSA_KEY_TYPE_DES:
429 if( bits != 64 && bits != 128 && bits != 192 )
430 return( PSA_ERROR_INVALID_ARGUMENT );
431 break;
432#endif
433#if defined(MBEDTLS_ARC4_C)
434 case PSA_KEY_TYPE_ARC4:
435 if( bits < 8 || bits > 2048 )
436 return( PSA_ERROR_INVALID_ARGUMENT );
437 break;
438#endif
439 default:
440 return( PSA_ERROR_NOT_SUPPORTED );
441 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200442 if( bits % 8 != 0 )
443 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200444
445 /* Allocate memory for the key */
446 raw->bytes = PSA_BITS_TO_BYTES( bits );
447 raw->data = mbedtls_calloc( 1, raw->bytes );
448 if( raw->data == NULL )
449 {
450 raw->bytes = 0;
451 return( PSA_ERROR_INSUFFICIENT_MEMORY );
452 }
453 return( PSA_SUCCESS );
454}
455
Gilles Peskine2d277862018-06-18 15:41:12 +0200456psa_status_t psa_import_key( psa_key_slot_t key,
457 psa_key_type_t type,
458 const uint8_t *data,
459 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100460{
461 key_slot_t *slot;
462
Gilles Peskine828ed142018-06-18 23:25:51 +0200463 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100464 return( PSA_ERROR_INVALID_ARGUMENT );
465 slot = &global_data.key_slots[key];
466 if( slot->type != PSA_KEY_TYPE_NONE )
467 return( PSA_ERROR_OCCUPIED_SLOT );
468
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200469 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100470 {
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200471 psa_status_t status;
Gilles Peskine6d912132018-03-07 16:41:37 +0100472 /* Ensure that a bytes-to-bit conversion won't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100473 if( data_length > SIZE_MAX / 8 )
474 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200475 status = prepare_raw_data_slot( type,
476 PSA_BYTES_TO_BITS( data_length ),
477 &slot->data.raw );
478 if( status != PSA_SUCCESS )
479 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100480 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100481 }
482 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100483#if defined(MBEDTLS_PK_PARSE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100484 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
485 type == PSA_KEY_TYPE_RSA_KEYPAIR ||
486 PSA_KEY_TYPE_IS_ECC( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100487 {
488 int ret;
Gilles Peskine969ac722018-01-28 18:16:59 +0100489 mbedtls_pk_context pk;
490 mbedtls_pk_init( &pk );
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100491 if( PSA_KEY_TYPE_IS_KEYPAIR( type ) )
492 ret = mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 );
493 else
494 ret = mbedtls_pk_parse_public_key( &pk, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100495 if( ret != 0 )
496 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine969ac722018-01-28 18:16:59 +0100497 switch( mbedtls_pk_get_type( &pk ) )
498 {
499#if defined(MBEDTLS_RSA_C)
500 case MBEDTLS_PK_RSA:
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100501 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
502 type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200503 slot->data.rsa = mbedtls_pk_rsa( pk );
Gilles Peskine969ac722018-01-28 18:16:59 +0100504 else
505 return( PSA_ERROR_INVALID_ARGUMENT );
506 break;
507#endif /* MBEDTLS_RSA_C */
508#if defined(MBEDTLS_ECP_C)
509 case MBEDTLS_PK_ECKEY:
510 if( PSA_KEY_TYPE_IS_ECC( type ) )
511 {
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200512 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk );
513 psa_ecc_curve_t actual_curve =
514 mbedtls_ecc_group_to_psa( ecp->grp.id );
515 psa_ecc_curve_t expected_curve =
516 PSA_KEY_TYPE_GET_CURVE( type );
517 if( actual_curve != expected_curve )
518 return( PSA_ERROR_INVALID_ARGUMENT );
519 slot->data.ecp = ecp;
Gilles Peskine969ac722018-01-28 18:16:59 +0100520 }
521 else
522 return( PSA_ERROR_INVALID_ARGUMENT );
523 break;
524#endif /* MBEDTLS_ECP_C */
525 default:
526 return( PSA_ERROR_INVALID_ARGUMENT );
527 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100528 }
529 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100530#endif /* defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100531 {
532 return( PSA_ERROR_NOT_SUPPORTED );
533 }
534
535 slot->type = type;
536 return( PSA_SUCCESS );
537}
538
Gilles Peskine2d277862018-06-18 15:41:12 +0200539psa_status_t psa_destroy_key( psa_key_slot_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100540{
541 key_slot_t *slot;
542
Gilles Peskine828ed142018-06-18 23:25:51 +0200543 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100544 return( PSA_ERROR_INVALID_ARGUMENT );
545 slot = &global_data.key_slots[key];
546 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine154bd952018-04-19 08:38:16 +0200547 {
548 /* No key material to clean, but do zeroize the slot below to wipe
549 * metadata such as policies. */
550 }
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200551 else if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100552 {
553 mbedtls_free( slot->data.raw.data );
554 }
555 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100556#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100557 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
558 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100559 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100560 mbedtls_rsa_free( slot->data.rsa );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100561 mbedtls_free( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100562 }
563 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100564#endif /* defined(MBEDTLS_RSA_C) */
565#if defined(MBEDTLS_ECP_C)
566 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
567 {
568 mbedtls_ecp_keypair_free( slot->data.ecp );
Gilles Peskine3c6e9702018-03-07 16:42:44 +0100569 mbedtls_free( slot->data.ecp );
Gilles Peskine969ac722018-01-28 18:16:59 +0100570 }
571 else
572#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100573 {
574 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100575 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100576 return( PSA_ERROR_TAMPERING_DETECTED );
577 }
578
579 mbedtls_zeroize( slot, sizeof( *slot ) );
580 return( PSA_SUCCESS );
581}
582
Gilles Peskine2d277862018-06-18 15:41:12 +0200583psa_status_t psa_get_key_information( psa_key_slot_t key,
584 psa_key_type_t *type,
585 size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100586{
587 key_slot_t *slot;
588
Gilles Peskine828ed142018-06-18 23:25:51 +0200589 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100590 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100591 slot = &global_data.key_slots[key];
592 if( type != NULL )
593 *type = slot->type;
594 if( bits != NULL )
595 *bits = 0;
596 if( slot->type == PSA_KEY_TYPE_NONE )
597 return( PSA_ERROR_EMPTY_SLOT );
598
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200599 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100600 {
601 if( bits != NULL )
602 *bits = slot->data.raw.bytes * 8;
603 }
604 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100605#if defined(MBEDTLS_RSA_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100606 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
607 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100608 {
609 if( bits != NULL )
Gilles Peskine969ac722018-01-28 18:16:59 +0100610 *bits = mbedtls_rsa_get_bitlen( slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100611 }
612 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100613#endif /* defined(MBEDTLS_RSA_C) */
614#if defined(MBEDTLS_ECP_C)
615 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
616 {
617 if( bits != NULL )
618 *bits = slot->data.ecp->grp.pbits;
619 }
620 else
621#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100622 {
623 /* Shouldn't happen: the key type is not any type that we
Gilles Peskine6d912132018-03-07 16:41:37 +0100624 * put in. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100625 return( PSA_ERROR_TAMPERING_DETECTED );
626 }
627
628 return( PSA_SUCCESS );
629}
630
Gilles Peskine2d277862018-06-18 15:41:12 +0200631static psa_status_t psa_internal_export_key( psa_key_slot_t key,
632 uint8_t *data,
633 size_t data_size,
634 size_t *data_length,
635 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100636{
637 key_slot_t *slot;
638
Jaeden Amerof24c7f82018-06-27 17:20:43 +0100639 /* Set the key to empty now, so that even when there are errors, we always
640 * set data_length to a value between 0 and data_size. On error, setting
641 * the key to empty is a good choice because an empty key representation is
642 * unlikely to be accepted anywhere. */
643 *data_length = 0;
644
Gilles Peskine828ed142018-06-18 23:25:51 +0200645 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100646 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100647 slot = &global_data.key_slots[key];
648 if( slot->type == PSA_KEY_TYPE_NONE )
649 return( PSA_ERROR_EMPTY_SLOT );
650
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200651 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300652 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300653
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200654 if( ! export_public_key &&
655 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) &&
656 ( slot->policy.usage & PSA_KEY_USAGE_EXPORT ) == 0 )
Moran Peker87567632018-06-04 18:41:37 +0300657 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine2d277862018-06-18 15:41:12 +0200658
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200659 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100660 {
661 if( slot->data.raw.bytes > data_size )
662 return( PSA_ERROR_BUFFER_TOO_SMALL );
663 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
664 *data_length = slot->data.raw.bytes;
665 return( PSA_SUCCESS );
666 }
667 else
Moran Peker17e36e12018-05-02 12:55:20 +0300668 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100669#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100670 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
Moran Pekera998bc62018-04-16 18:16:20 +0300671 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
672 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100673 {
Moran Pekera998bc62018-04-16 18:16:20 +0300674 mbedtls_pk_context pk;
675 int ret;
676 mbedtls_pk_init( &pk );
677 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
678 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
679 {
680 pk.pk_info = &mbedtls_rsa_info;
681 pk.pk_ctx = slot->data.rsa;
682 }
683 else
684 {
685 pk.pk_info = &mbedtls_eckey_info;
686 pk.pk_ctx = slot->data.ecp;
687 }
Moran Pekerd7326592018-05-29 16:56:39 +0300688 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300689 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300690 else
691 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300692 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200693 {
694 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300695 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200696 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200697 /* The mbedtls_pk_xxx functions write to the end of the buffer.
698 * Move the data to the beginning and erase remaining data
699 * at the original location. */
700 if( 2 * (size_t) ret <= data_size )
701 {
702 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200703 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200704 }
705 else if( (size_t) ret < data_size )
706 {
707 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200708 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200709 }
Moran Pekera998bc62018-04-16 18:16:20 +0300710 *data_length = ret;
711 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100712 }
713 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100714#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300715 {
716 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200717 it is valid for a special-purpose implementation to omit
718 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300719 return( PSA_ERROR_NOT_SUPPORTED );
720 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100721 }
722}
723
Gilles Peskine2d277862018-06-18 15:41:12 +0200724psa_status_t psa_export_key( psa_key_slot_t key,
725 uint8_t *data,
726 size_t data_size,
727 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300728{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200729 return( psa_internal_export_key( key, data, data_size,
730 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100731}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100732
Gilles Peskine2d277862018-06-18 15:41:12 +0200733psa_status_t psa_export_public_key( psa_key_slot_t key,
734 uint8_t *data,
735 size_t data_size,
736 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300737{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200738 return( psa_internal_export_key( key, data, data_size,
739 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300740}
741
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200742
743
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100744/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100745/* Message digests */
746/****************************************************************/
747
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100748static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100749{
750 switch( alg )
751 {
752#if defined(MBEDTLS_MD2_C)
753 case PSA_ALG_MD2:
754 return( &mbedtls_md2_info );
755#endif
756#if defined(MBEDTLS_MD4_C)
757 case PSA_ALG_MD4:
758 return( &mbedtls_md4_info );
759#endif
760#if defined(MBEDTLS_MD5_C)
761 case PSA_ALG_MD5:
762 return( &mbedtls_md5_info );
763#endif
764#if defined(MBEDTLS_RIPEMD160_C)
765 case PSA_ALG_RIPEMD160:
766 return( &mbedtls_ripemd160_info );
767#endif
768#if defined(MBEDTLS_SHA1_C)
769 case PSA_ALG_SHA_1:
770 return( &mbedtls_sha1_info );
771#endif
772#if defined(MBEDTLS_SHA256_C)
773 case PSA_ALG_SHA_224:
774 return( &mbedtls_sha224_info );
775 case PSA_ALG_SHA_256:
776 return( &mbedtls_sha256_info );
777#endif
778#if defined(MBEDTLS_SHA512_C)
779 case PSA_ALG_SHA_384:
780 return( &mbedtls_sha384_info );
781 case PSA_ALG_SHA_512:
782 return( &mbedtls_sha512_info );
783#endif
784 default:
785 return( NULL );
786 }
787}
788
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100789psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
790{
791 switch( operation->alg )
792 {
Gilles Peskine81736312018-06-26 15:04:31 +0200793 case 0:
794 /* The object has (apparently) been initialized but it is not
795 * in use. It's ok to call abort on such an object, and there's
796 * nothing to do. */
797 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100798#if defined(MBEDTLS_MD2_C)
799 case PSA_ALG_MD2:
800 mbedtls_md2_free( &operation->ctx.md2 );
801 break;
802#endif
803#if defined(MBEDTLS_MD4_C)
804 case PSA_ALG_MD4:
805 mbedtls_md4_free( &operation->ctx.md4 );
806 break;
807#endif
808#if defined(MBEDTLS_MD5_C)
809 case PSA_ALG_MD5:
810 mbedtls_md5_free( &operation->ctx.md5 );
811 break;
812#endif
813#if defined(MBEDTLS_RIPEMD160_C)
814 case PSA_ALG_RIPEMD160:
815 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
816 break;
817#endif
818#if defined(MBEDTLS_SHA1_C)
819 case PSA_ALG_SHA_1:
820 mbedtls_sha1_free( &operation->ctx.sha1 );
821 break;
822#endif
823#if defined(MBEDTLS_SHA256_C)
824 case PSA_ALG_SHA_224:
825 case PSA_ALG_SHA_256:
826 mbedtls_sha256_free( &operation->ctx.sha256 );
827 break;
828#endif
829#if defined(MBEDTLS_SHA512_C)
830 case PSA_ALG_SHA_384:
831 case PSA_ALG_SHA_512:
832 mbedtls_sha512_free( &operation->ctx.sha512 );
833 break;
834#endif
835 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +0200836 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100837 }
838 operation->alg = 0;
839 return( PSA_SUCCESS );
840}
841
842psa_status_t psa_hash_start( psa_hash_operation_t *operation,
843 psa_algorithm_t alg )
844{
845 int ret;
846 operation->alg = 0;
847 switch( alg )
848 {
849#if defined(MBEDTLS_MD2_C)
850 case PSA_ALG_MD2:
851 mbedtls_md2_init( &operation->ctx.md2 );
852 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
853 break;
854#endif
855#if defined(MBEDTLS_MD4_C)
856 case PSA_ALG_MD4:
857 mbedtls_md4_init( &operation->ctx.md4 );
858 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
859 break;
860#endif
861#if defined(MBEDTLS_MD5_C)
862 case PSA_ALG_MD5:
863 mbedtls_md5_init( &operation->ctx.md5 );
864 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
865 break;
866#endif
867#if defined(MBEDTLS_RIPEMD160_C)
868 case PSA_ALG_RIPEMD160:
869 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
870 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
871 break;
872#endif
873#if defined(MBEDTLS_SHA1_C)
874 case PSA_ALG_SHA_1:
875 mbedtls_sha1_init( &operation->ctx.sha1 );
876 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
877 break;
878#endif
879#if defined(MBEDTLS_SHA256_C)
880 case PSA_ALG_SHA_224:
881 mbedtls_sha256_init( &operation->ctx.sha256 );
882 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
883 break;
884 case PSA_ALG_SHA_256:
885 mbedtls_sha256_init( &operation->ctx.sha256 );
886 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
887 break;
888#endif
889#if defined(MBEDTLS_SHA512_C)
890 case PSA_ALG_SHA_384:
891 mbedtls_sha512_init( &operation->ctx.sha512 );
892 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
893 break;
894 case PSA_ALG_SHA_512:
895 mbedtls_sha512_init( &operation->ctx.sha512 );
896 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
897 break;
898#endif
899 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +0200900 return( PSA_ALG_IS_HASH( alg ) ?
901 PSA_ERROR_NOT_SUPPORTED :
902 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100903 }
904 if( ret == 0 )
905 operation->alg = alg;
906 else
907 psa_hash_abort( operation );
908 return( mbedtls_to_psa_error( ret ) );
909}
910
911psa_status_t psa_hash_update( psa_hash_operation_t *operation,
912 const uint8_t *input,
913 size_t input_length )
914{
915 int ret;
916 switch( operation->alg )
917 {
918#if defined(MBEDTLS_MD2_C)
919 case PSA_ALG_MD2:
920 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
921 input, input_length );
922 break;
923#endif
924#if defined(MBEDTLS_MD4_C)
925 case PSA_ALG_MD4:
926 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
927 input, input_length );
928 break;
929#endif
930#if defined(MBEDTLS_MD5_C)
931 case PSA_ALG_MD5:
932 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
933 input, input_length );
934 break;
935#endif
936#if defined(MBEDTLS_RIPEMD160_C)
937 case PSA_ALG_RIPEMD160:
938 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
939 input, input_length );
940 break;
941#endif
942#if defined(MBEDTLS_SHA1_C)
943 case PSA_ALG_SHA_1:
944 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
945 input, input_length );
946 break;
947#endif
948#if defined(MBEDTLS_SHA256_C)
949 case PSA_ALG_SHA_224:
950 case PSA_ALG_SHA_256:
951 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
952 input, input_length );
953 break;
954#endif
955#if defined(MBEDTLS_SHA512_C)
956 case PSA_ALG_SHA_384:
957 case PSA_ALG_SHA_512:
958 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
959 input, input_length );
960 break;
961#endif
962 default:
963 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
964 break;
965 }
966 if( ret != 0 )
967 psa_hash_abort( operation );
968 return( mbedtls_to_psa_error( ret ) );
969}
970
971psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
972 uint8_t *hash,
973 size_t hash_size,
974 size_t *hash_length )
975{
976 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +0200977 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100978
979 /* Fill the output buffer with something that isn't a valid hash
980 * (barring an attack on the hash and deliberately-crafted input),
981 * in case the caller doesn't check the return status properly. */
982 *hash_length = actual_hash_length;
983 memset( hash, '!', hash_size );
984
985 if( hash_size < actual_hash_length )
986 return( PSA_ERROR_BUFFER_TOO_SMALL );
987
988 switch( operation->alg )
989 {
990#if defined(MBEDTLS_MD2_C)
991 case PSA_ALG_MD2:
992 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
993 break;
994#endif
995#if defined(MBEDTLS_MD4_C)
996 case PSA_ALG_MD4:
997 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
998 break;
999#endif
1000#if defined(MBEDTLS_MD5_C)
1001 case PSA_ALG_MD5:
1002 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
1003 break;
1004#endif
1005#if defined(MBEDTLS_RIPEMD160_C)
1006 case PSA_ALG_RIPEMD160:
1007 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1008 break;
1009#endif
1010#if defined(MBEDTLS_SHA1_C)
1011 case PSA_ALG_SHA_1:
1012 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1013 break;
1014#endif
1015#if defined(MBEDTLS_SHA256_C)
1016 case PSA_ALG_SHA_224:
1017 case PSA_ALG_SHA_256:
1018 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1019 break;
1020#endif
1021#if defined(MBEDTLS_SHA512_C)
1022 case PSA_ALG_SHA_384:
1023 case PSA_ALG_SHA_512:
1024 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1025 break;
1026#endif
1027 default:
1028 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1029 break;
1030 }
1031
1032 if( ret == 0 )
1033 {
1034 return( psa_hash_abort( operation ) );
1035 }
1036 else
1037 {
1038 psa_hash_abort( operation );
1039 return( mbedtls_to_psa_error( ret ) );
1040 }
1041}
1042
Gilles Peskine2d277862018-06-18 15:41:12 +02001043psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1044 const uint8_t *hash,
1045 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001046{
1047 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1048 size_t actual_hash_length;
1049 psa_status_t status = psa_hash_finish( operation,
1050 actual_hash, sizeof( actual_hash ),
1051 &actual_hash_length );
1052 if( status != PSA_SUCCESS )
1053 return( status );
1054 if( actual_hash_length != hash_length )
1055 return( PSA_ERROR_INVALID_SIGNATURE );
1056 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1057 return( PSA_ERROR_INVALID_SIGNATURE );
1058 return( PSA_SUCCESS );
1059}
1060
1061
1062
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001063/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001064/* MAC */
1065/****************************************************************/
1066
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001067static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001068 psa_algorithm_t alg,
1069 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001070 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001071 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001072{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001073 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001074 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001075
1076 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1077 {
1078 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001079 {
1080 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1081 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001082
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001083 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001084 {
1085 case PSA_ALG_STREAM_CIPHER:
1086 mode = MBEDTLS_MODE_STREAM;
1087 break;
1088 case PSA_ALG_CBC_BASE:
1089 mode = MBEDTLS_MODE_CBC;
1090 break;
1091 case PSA_ALG_CFB_BASE:
1092 mode = MBEDTLS_MODE_CFB;
1093 break;
1094 case PSA_ALG_OFB_BASE:
1095 mode = MBEDTLS_MODE_OFB;
1096 break;
1097 case PSA_ALG_CTR:
1098 mode = MBEDTLS_MODE_CTR;
1099 break;
1100 case PSA_ALG_CCM:
1101 mode = MBEDTLS_MODE_CCM;
1102 break;
1103 case PSA_ALG_GCM:
1104 mode = MBEDTLS_MODE_GCM;
1105 break;
1106 default:
1107 return( NULL );
1108 }
1109 }
1110 else if( alg == PSA_ALG_CMAC )
1111 mode = MBEDTLS_MODE_ECB;
1112 else if( alg == PSA_ALG_GMAC )
1113 mode = MBEDTLS_MODE_GCM;
1114 else
1115 return( NULL );
1116
1117 switch( key_type )
1118 {
1119 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001120 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001121 break;
1122 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001123 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1124 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001125 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001126 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001127 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001128 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001129 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1130 * but two-key Triple-DES is functionally three-key Triple-DES
1131 * with K1=K3, so that's how we present it to mbedtls. */
1132 if( key_bits == 128 )
1133 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001134 break;
1135 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001136 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001137 break;
1138 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001139 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001140 break;
1141 default:
1142 return( NULL );
1143 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001144 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001145 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001146
mohammad1603f4f0d612018-06-03 15:04:51 +03001147 return( mbedtls_cipher_info_from_values( cipher_id_tmp, key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001148}
1149
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001150static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001151{
Gilles Peskine2d277862018-06-18 15:41:12 +02001152 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001153 {
1154 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001155 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001156 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001157 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001158 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001159 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001160 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001161 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001162 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001163 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001164 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001165 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001166 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001167 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001168 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001169 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001170 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001171 return( 128 );
1172 default:
1173 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001174 }
1175}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001176
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001177/* Initialize the MAC operation structure. Once this function has been
1178 * called, psa_mac_abort can run and will do the right thing. */
1179static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1180 psa_algorithm_t alg )
1181{
1182 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1183
1184 operation->alg = alg;
1185 operation->key_set = 0;
1186 operation->iv_set = 0;
1187 operation->iv_required = 0;
1188 operation->has_input = 0;
1189 operation->key_usage_sign = 0;
1190 operation->key_usage_verify = 0;
1191
1192#if defined(MBEDTLS_CMAC_C)
1193 if( alg == PSA_ALG_CMAC )
1194 {
1195 operation->iv_required = 0;
1196 mbedtls_cipher_init( &operation->ctx.cmac );
1197 status = PSA_SUCCESS;
1198 }
1199 else
1200#endif /* MBEDTLS_CMAC_C */
1201#if defined(MBEDTLS_MD_C)
1202 if( PSA_ALG_IS_HMAC( operation->alg ) )
1203 {
1204 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1205 PSA_ALG_HMAC_HASH( alg ) );
1206 }
1207 else
1208#endif /* MBEDTLS_MD_C */
1209 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001210 if( ! PSA_ALG_IS_MAC( alg ) )
1211 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001212 }
1213
1214 if( status != PSA_SUCCESS )
1215 memset( operation, 0, sizeof( *operation ) );
1216 return( status );
1217}
1218
Gilles Peskine8c9def32018-02-08 10:02:12 +01001219psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1220{
1221 switch( operation->alg )
1222 {
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001223 case 0:
Gilles Peskine81736312018-06-26 15:04:31 +02001224 /* The object has (apparently) been initialized but it is not
1225 * in use. It's ok to call abort on such an object, and there's
1226 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001227 return( PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001228#if defined(MBEDTLS_CMAC_C)
1229 case PSA_ALG_CMAC:
1230 mbedtls_cipher_free( &operation->ctx.cmac );
1231 break;
1232#endif /* MBEDTLS_CMAC_C */
1233 default:
1234#if defined(MBEDTLS_MD_C)
1235 if( PSA_ALG_IS_HMAC( operation->alg ) )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001236 {
Gilles Peskine99bc6492018-06-11 17:13:00 +02001237 unsigned int block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001238 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001239
Gilles Peskine99bc6492018-06-11 17:13:00 +02001240 if( block_size == 0 )
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001241 return( PSA_ERROR_NOT_SUPPORTED );
1242
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001243 psa_hash_abort( &operation->ctx.hmac.hash_ctx );
Gilles Peskine2d277862018-06-18 15:41:12 +02001244 mbedtls_zeroize( operation->ctx.hmac.opad, block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001245 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001246 else
1247#endif /* MBEDTLS_MD_C */
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001248 {
1249 /* Sanity check (shouldn't happen: operation->alg should
1250 * always have been initialized to a valid value). */
1251 return( PSA_ERROR_BAD_STATE );
1252 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001253 }
Moran Peker41deec42018-04-04 15:43:05 +03001254
Gilles Peskine8c9def32018-02-08 10:02:12 +01001255 operation->alg = 0;
1256 operation->key_set = 0;
1257 operation->iv_set = 0;
1258 operation->iv_required = 0;
1259 operation->has_input = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001260 operation->key_usage_sign = 0;
1261 operation->key_usage_verify = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001262
Gilles Peskine8c9def32018-02-08 10:02:12 +01001263 return( PSA_SUCCESS );
1264}
1265
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001266#if defined(MBEDTLS_CMAC_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001267static int psa_cmac_start( psa_mac_operation_t *operation,
1268 size_t key_bits,
1269 key_slot_t *slot,
1270 const mbedtls_cipher_info_t *cipher_info )
1271{
1272 int ret;
1273
1274 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001275
1276 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1277 if( ret != 0 )
1278 return( ret );
1279
1280 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1281 slot->data.raw.data,
1282 key_bits );
1283 return( ret );
1284}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001285#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001286
Gilles Peskine248051a2018-06-20 16:09:38 +02001287#if defined(MBEDTLS_MD_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001288static int psa_hmac_start( psa_mac_operation_t *operation,
1289 psa_key_type_t key_type,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001290 key_slot_t *slot,
1291 psa_algorithm_t alg )
1292{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001293 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001294 unsigned char *opad = operation->ctx.hmac.opad;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001295 size_t i;
Gilles Peskinee1bc6802018-06-11 17:36:05 +02001296 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001297 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001298 unsigned int digest_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001299 PSA_HASH_SIZE( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001300 size_t key_length = slot->data.raw.bytes;
1301 psa_status_t status;
1302
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001303 if( block_size == 0 || digest_size == 0 )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001304 return( PSA_ERROR_NOT_SUPPORTED );
1305
1306 if( key_type != PSA_KEY_TYPE_HMAC )
1307 return( PSA_ERROR_INVALID_ARGUMENT );
1308
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001309 operation->mac_size = digest_size;
1310
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001311 /* The hash was started earlier in psa_mac_init. */
Gilles Peskined223b522018-06-11 18:12:58 +02001312 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001313 {
1314 status = psa_hash_update( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001315 slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001316 if( status != PSA_SUCCESS )
1317 return( status );
1318 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001319 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001320 if( status != PSA_SUCCESS )
1321 return( status );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001322 }
Gilles Peskined223b522018-06-11 18:12:58 +02001323 else
1324 memcpy( ipad, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001325
Gilles Peskined223b522018-06-11 18:12:58 +02001326 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1327 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001328 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001329 ipad[i] ^= 0x36;
1330 memset( ipad + key_length, 0x36, block_size - key_length );
1331
1332 /* Copy the key material from ipad to opad, flipping the requisite bits,
1333 * and filling the rest of opad with the requisite constant. */
1334 for( i = 0; i < key_length; i++ )
1335 opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1336 memset( opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001337
1338 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1339 PSA_ALG_HMAC_HASH( alg ) );
1340 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001341 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001342
1343 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, ipad,
1344 block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001345
1346cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001347 mbedtls_zeroize( ipad, key_length );
1348 /* opad is in the context. It needs to stay in memory if this function
1349 * succeeds, and it will be wiped by psa_mac_abort() called from
1350 * psa_mac_start in the error case. */
1351
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001352 return( status );
1353}
Gilles Peskine248051a2018-06-20 16:09:38 +02001354#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001355
Gilles Peskine8c9def32018-02-08 10:02:12 +01001356psa_status_t psa_mac_start( psa_mac_operation_t *operation,
1357 psa_key_slot_t key,
1358 psa_algorithm_t alg )
1359{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001360 psa_status_t status;
1361 key_slot_t *slot;
1362 psa_key_type_t key_type;
1363 size_t key_bits;
Gilles Peskine828ed142018-06-18 23:25:51 +02001364 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001365
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001366 status = psa_mac_init( operation, alg );
1367 if( status != PSA_SUCCESS )
1368 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001369
1370 status = psa_get_key_information( key, &key_type, &key_bits );
1371 if( status != PSA_SUCCESS )
1372 return( status );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001373
Gilles Peskine8c9def32018-02-08 10:02:12 +01001374 slot = &global_data.key_slots[key];
Gilles Peskine99bc6492018-06-11 17:13:00 +02001375 if( slot->type == PSA_KEY_TYPE_NONE )
1376 return( PSA_ERROR_EMPTY_SLOT );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001377
Moran Pekerd7326592018-05-29 16:56:39 +03001378 if( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001379 operation->key_usage_sign = 1;
1380
Moran Pekerd7326592018-05-29 16:56:39 +03001381 if( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001382 operation->key_usage_verify = 1;
1383
Gilles Peskine8c9def32018-02-08 10:02:12 +01001384 if( ! PSA_ALG_IS_HMAC( alg ) )
1385 {
mohammad1603f4f0d612018-06-03 15:04:51 +03001386 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001387 if( cipher_info == NULL )
1388 return( PSA_ERROR_NOT_SUPPORTED );
1389 operation->mac_size = cipher_info->block_size;
1390 }
1391 switch( alg )
1392 {
1393#if defined(MBEDTLS_CMAC_C)
1394 case PSA_ALG_CMAC:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001395 status = mbedtls_to_psa_error( psa_cmac_start( operation,
1396 key_bits,
1397 slot,
1398 cipher_info ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001399 break;
1400#endif /* MBEDTLS_CMAC_C */
1401 default:
1402#if defined(MBEDTLS_MD_C)
1403 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskined223b522018-06-11 18:12:58 +02001404 status = psa_hmac_start( operation, key_type, slot, alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001405 else
1406#endif /* MBEDTLS_MD_C */
1407 return( PSA_ERROR_NOT_SUPPORTED );
1408 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001409
Gilles Peskine8c9def32018-02-08 10:02:12 +01001410 /* If we reach this point, then the algorithm-specific part of the
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001411 * context may contain data that needs to be wiped on error. */
1412 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001413 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001414 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001415 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001416 else
1417 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001418 operation->key_set = 1;
1419 }
1420 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001421}
1422
1423psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1424 const uint8_t *input,
1425 size_t input_length )
1426{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001427 int ret = 0 ;
1428 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001429 if( ! operation->key_set )
1430 return( PSA_ERROR_BAD_STATE );
1431 if( operation->iv_required && ! operation->iv_set )
1432 return( PSA_ERROR_BAD_STATE );
1433 operation->has_input = 1;
1434
1435 switch( operation->alg )
1436 {
1437#if defined(MBEDTLS_CMAC_C)
1438 case PSA_ALG_CMAC:
1439 ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1440 input, input_length );
1441 break;
1442#endif /* MBEDTLS_CMAC_C */
1443 default:
1444#if defined(MBEDTLS_MD_C)
1445 if( PSA_ALG_IS_HMAC( operation->alg ) )
1446 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001447 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
Gilles Peskine2d277862018-06-18 15:41:12 +02001448 input_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001449 }
1450 else
1451#endif /* MBEDTLS_MD_C */
1452 {
1453 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1454 }
1455 break;
1456 }
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001457 if( ret != 0 || status != PSA_SUCCESS )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001458 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001459 psa_mac_abort( operation );
Gilles Peskine2d277862018-06-18 15:41:12 +02001460 if( ret != 0 )
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001461 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001462 }
1463
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001464 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001465}
1466
mohammad16036df908f2018-04-02 08:34:15 -07001467static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001468 uint8_t *mac,
1469 size_t mac_size,
1470 size_t *mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001471{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001472 int ret = 0;
1473 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001474 if( ! operation->key_set )
1475 return( PSA_ERROR_BAD_STATE );
1476 if( operation->iv_required && ! operation->iv_set )
1477 return( PSA_ERROR_BAD_STATE );
1478
1479 /* Fill the output buffer with something that isn't a valid mac
1480 * (barring an attack on the mac and deliberately-crafted input),
1481 * in case the caller doesn't check the return status properly. */
1482 *mac_length = operation->mac_size;
1483 memset( mac, '!', mac_size );
1484
1485 if( mac_size < operation->mac_size )
1486 return( PSA_ERROR_BUFFER_TOO_SMALL );
1487
1488 switch( operation->alg )
1489 {
1490#if defined(MBEDTLS_CMAC_C)
1491 case PSA_ALG_CMAC:
1492 ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1493 break;
1494#endif /* MBEDTLS_CMAC_C */
1495 default:
1496#if defined(MBEDTLS_MD_C)
1497 if( PSA_ALG_IS_HMAC( operation->alg ) )
1498 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001499 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001500 unsigned char *opad = operation->ctx.hmac.opad;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001501 size_t hash_size = 0;
Nir Sonnenschein96272412018-06-17 14:41:10 +03001502 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001503 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001504
Gilles Peskine99bc6492018-06-11 17:13:00 +02001505 if( block_size == 0 )
1506 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001507
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001508 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine99bc6492018-06-11 17:13:00 +02001509 sizeof( tmp ), &hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001510 if( status != PSA_SUCCESS )
1511 goto cleanup;
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001512 /* From here on, tmp needs to be wiped. */
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001513
1514 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1515 PSA_ALG_HMAC_HASH( operation->alg ) );
1516 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001517 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001518
1519 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, opad,
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001520 block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001521 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001522 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001523
1524 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine2d277862018-06-18 15:41:12 +02001525 hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001526 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001527 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001528
1529 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, mac,
1530 mac_size, mac_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001531 hmac_cleanup:
1532 mbedtls_zeroize( tmp, hash_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001533 }
1534 else
1535#endif /* MBEDTLS_MD_C */
1536 {
1537 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1538 }
1539 break;
1540 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001541cleanup:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001542
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001543 if( ret == 0 && status == PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001544 {
1545 return( psa_mac_abort( operation ) );
1546 }
1547 else
1548 {
1549 psa_mac_abort( operation );
Gilles Peskine99bc6492018-06-11 17:13:00 +02001550 if( ret != 0 )
Gilles Peskine2d277862018-06-18 15:41:12 +02001551 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001552
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001553 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001554 }
1555}
1556
mohammad16036df908f2018-04-02 08:34:15 -07001557psa_status_t psa_mac_finish( psa_mac_operation_t *operation,
1558 uint8_t *mac,
1559 size_t mac_size,
1560 size_t *mac_length )
1561{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001562 if( ! operation->key_usage_sign )
mohammad16036df908f2018-04-02 08:34:15 -07001563 return( PSA_ERROR_NOT_PERMITTED );
1564
Gilles Peskine99bc6492018-06-11 17:13:00 +02001565 return( psa_mac_finish_internal( operation, mac,
1566 mac_size, mac_length ) );
mohammad16036df908f2018-04-02 08:34:15 -07001567}
1568
Gilles Peskine828ed142018-06-18 23:25:51 +02001569#define PSA_MAC_MAX_SIZE \
Gilles Peskine2d277862018-06-18 15:41:12 +02001570 ( MBEDTLS_MD_MAX_SIZE > MBEDTLS_MAX_BLOCK_LENGTH ? \
1571 MBEDTLS_MD_MAX_SIZE : \
Gilles Peskine8c9def32018-02-08 10:02:12 +01001572 MBEDTLS_MAX_BLOCK_LENGTH )
1573psa_status_t psa_mac_verify( psa_mac_operation_t *operation,
1574 const uint8_t *mac,
1575 size_t mac_length )
1576{
Gilles Peskine828ed142018-06-18 23:25:51 +02001577 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Gilles Peskine8c9def32018-02-08 10:02:12 +01001578 size_t actual_mac_length;
mohammad16036df908f2018-04-02 08:34:15 -07001579 psa_status_t status;
1580
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001581 if( ! operation->key_usage_verify )
mohammad16036df908f2018-04-02 08:34:15 -07001582 return( PSA_ERROR_NOT_PERMITTED );
1583
1584 status = psa_mac_finish_internal( operation,
1585 actual_mac, sizeof( actual_mac ),
1586 &actual_mac_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001587 if( status != PSA_SUCCESS )
1588 return( status );
1589 if( actual_mac_length != mac_length )
1590 return( PSA_ERROR_INVALID_SIGNATURE );
1591 if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
1592 return( PSA_ERROR_INVALID_SIGNATURE );
1593 return( PSA_SUCCESS );
1594}
1595
1596
Gilles Peskine20035e32018-02-03 22:44:14 +01001597
Gilles Peskine20035e32018-02-03 22:44:14 +01001598/****************************************************************/
1599/* Asymmetric cryptography */
1600/****************************************************************/
1601
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001602/* Decode the hash algorithm from alg and store the mbedtls encoding in
1603 * md_alg. Verify that the hash length is consistent. */
1604static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1605 size_t hash_length,
1606 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001607{
Gilles Peskine61b91d42018-06-08 16:09:36 +02001608 psa_algorithm_t hash_alg = PSA_ALG_RSA_GET_HASH( alg );
1609 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1610 *md_alg = hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1611 if( *md_alg == MBEDTLS_MD_NONE )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001612 {
1613#if SIZE_MAX > UINT_MAX
Gilles Peskine61b91d42018-06-08 16:09:36 +02001614 if( hash_length > UINT_MAX )
1615 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001616#endif
1617 }
1618 else
1619 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001620 if( mbedtls_md_get_size( md_info ) != hash_length )
1621 return( PSA_ERROR_INVALID_ARGUMENT );
1622 if( md_info == NULL )
1623 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001624 }
Gilles Peskine61b91d42018-06-08 16:09:36 +02001625 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001626}
1627
Gilles Peskine61b91d42018-06-08 16:09:36 +02001628psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
1629 psa_algorithm_t alg,
1630 const uint8_t *hash,
1631 size_t hash_length,
1632 const uint8_t *salt,
1633 size_t salt_length,
1634 uint8_t *signature,
1635 size_t signature_size,
1636 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01001637{
1638 key_slot_t *slot;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001639 psa_status_t status;
Gilles Peskine93aa0332018-02-03 23:58:03 +01001640 *signature_length = 0;
1641 (void) salt;
1642 (void) salt_length;
1643
Gilles Peskine828ed142018-06-18 23:25:51 +02001644 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskine20035e32018-02-03 22:44:14 +01001645 return( PSA_ERROR_EMPTY_SLOT );
1646 slot = &global_data.key_slots[key];
1647 if( slot->type == PSA_KEY_TYPE_NONE )
1648 return( PSA_ERROR_EMPTY_SLOT );
1649 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1650 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001651 if( ! ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) )
mohammad160306e79202018-03-28 13:17:44 +03001652 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine20035e32018-02-03 22:44:14 +01001653
Gilles Peskine20035e32018-02-03 22:44:14 +01001654#if defined(MBEDTLS_RSA_C)
1655 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1656 {
1657 mbedtls_rsa_context *rsa = slot->data.rsa;
1658 int ret;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001659 mbedtls_md_type_t md_alg;
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001660 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001661 if( status != PSA_SUCCESS )
1662 return( status );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001663
Gilles Peskine20035e32018-02-03 22:44:14 +01001664 if( signature_size < rsa->len )
1665 return( PSA_ERROR_BUFFER_TOO_SMALL );
1666#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinea5926232018-03-28 14:16:50 +02001667 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01001668 {
1669 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1670 MBEDTLS_MD_NONE );
1671 ret = mbedtls_rsa_pkcs1_sign( rsa,
1672 mbedtls_ctr_drbg_random,
1673 &global_data.ctr_drbg,
1674 MBEDTLS_RSA_PRIVATE,
1675 md_alg, hash_length, hash,
1676 signature );
1677 }
1678 else
1679#endif /* MBEDTLS_PKCS1_V15 */
1680#if defined(MBEDTLS_PKCS1_V21)
1681 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1682 {
1683 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1684 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1685 mbedtls_ctr_drbg_random,
1686 &global_data.ctr_drbg,
1687 MBEDTLS_RSA_PRIVATE,
1688 md_alg, hash_length, hash,
1689 signature );
1690 }
1691 else
1692#endif /* MBEDTLS_PKCS1_V21 */
1693 {
1694 return( PSA_ERROR_INVALID_ARGUMENT );
1695 }
Gilles Peskine93aa0332018-02-03 23:58:03 +01001696 if( ret == 0 )
1697 *signature_length = rsa->len;
Gilles Peskine20035e32018-02-03 22:44:14 +01001698 return( mbedtls_to_psa_error( ret ) );
1699 }
1700 else
1701#endif /* defined(MBEDTLS_RSA_C) */
1702#if defined(MBEDTLS_ECP_C)
1703 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1704 {
itayzafrir5c753392018-05-08 11:18:38 +03001705 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1706 int ret;
1707 const mbedtls_md_info_t *md_info;
1708 mbedtls_md_type_t md_alg;
1709 if( signature_size < PSA_ECDSA_SIGNATURE_SIZE( ecdsa->grp.pbits ) )
1710 return( PSA_ERROR_BUFFER_TOO_SMALL );
1711 md_info = mbedtls_md_info_from_psa( alg );
1712 md_alg = mbedtls_md_get_type( md_info );
1713 ret = mbedtls_ecdsa_write_signature( ecdsa, md_alg, hash, hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02001714 signature, signature_length,
1715 mbedtls_ctr_drbg_random,
1716 &global_data.ctr_drbg );
itayzafrir5c753392018-05-08 11:18:38 +03001717 return( mbedtls_to_psa_error( ret ) );
1718 }
1719 else
1720#endif /* defined(MBEDTLS_ECP_C) */
1721 {
Gilles Peskine20035e32018-02-03 22:44:14 +01001722 return( PSA_ERROR_NOT_SUPPORTED );
1723 }
itayzafrir5c753392018-05-08 11:18:38 +03001724}
1725
1726psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
1727 psa_algorithm_t alg,
1728 const uint8_t *hash,
1729 size_t hash_length,
1730 const uint8_t *salt,
1731 size_t salt_length,
1732 uint8_t *signature,
1733 size_t signature_size )
1734{
1735 key_slot_t *slot;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001736 psa_status_t status;
itayzafrir5c753392018-05-08 11:18:38 +03001737 (void) salt;
1738 (void) salt_length;
1739
Gilles Peskine828ed142018-06-18 23:25:51 +02001740 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
itayzafrir5c753392018-05-08 11:18:38 +03001741 return( PSA_ERROR_INVALID_ARGUMENT );
1742 slot = &global_data.key_slots[key];
1743 if( slot->type == PSA_KEY_TYPE_NONE )
1744 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001745 if( ! ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) )
itayzafrir5c753392018-05-08 11:18:38 +03001746 return( PSA_ERROR_NOT_PERMITTED );
1747
Gilles Peskine61b91d42018-06-08 16:09:36 +02001748#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001749 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
1750 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001751 {
1752 mbedtls_rsa_context *rsa = slot->data.rsa;
1753 int ret;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001754 mbedtls_md_type_t md_alg;
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001755 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001756 if( status != PSA_SUCCESS )
1757 return( status );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001758
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001759 if( signature_size < rsa->len )
1760 return( PSA_ERROR_BUFFER_TOO_SMALL );
1761#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001762 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001763 {
1764 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1765 MBEDTLS_MD_NONE );
1766
1767 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02001768 mbedtls_ctr_drbg_random,
1769 &global_data.ctr_drbg,
1770 MBEDTLS_RSA_PUBLIC,
1771 md_alg,
1772 hash_length,
1773 hash,
1774 signature );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001775
1776 }
1777 else
1778#endif /* MBEDTLS_PKCS1_V15 */
1779#if defined(MBEDTLS_PKCS1_V21)
1780 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1781 {
Gilles Peskinebeb49482018-06-08 17:44:35 +02001782 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1783 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1784 mbedtls_ctr_drbg_random,
1785 &global_data.ctr_drbg,
1786 MBEDTLS_RSA_PUBLIC,
1787 md_alg, hash_length, hash,
1788 signature );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001789 }
1790 else
1791#endif /* MBEDTLS_PKCS1_V21 */
1792 {
1793 return( PSA_ERROR_INVALID_ARGUMENT );
1794 }
1795 return( mbedtls_to_psa_error( ret ) );
1796 }
1797 else
1798#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03001799#if defined(MBEDTLS_ECP_C)
1800 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1801 {
1802 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1803 int ret;
Gilles Peskine2d277862018-06-18 15:41:12 +02001804 (void) alg;
Gilles Peskine61b91d42018-06-08 16:09:36 +02001805 ret = mbedtls_ecdsa_read_signature( ecdsa, hash, hash_length,
1806 signature, signature_size );
itayzafrir5c753392018-05-08 11:18:38 +03001807 return( mbedtls_to_psa_error( ret ) );
1808 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001809 else
1810#endif /* defined(MBEDTLS_ECP_C) */
1811 {
1812 return( PSA_ERROR_NOT_SUPPORTED );
1813 }
1814}
1815
Gilles Peskine61b91d42018-06-08 16:09:36 +02001816psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
1817 psa_algorithm_t alg,
1818 const uint8_t *input,
1819 size_t input_length,
1820 const uint8_t *salt,
1821 size_t salt_length,
1822 uint8_t *output,
1823 size_t output_size,
1824 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001825{
1826 key_slot_t *slot;
1827 (void) salt;
1828 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001829 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001830
Gilles Peskine828ed142018-06-18 23:25:51 +02001831 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein7f5a3192018-05-06 22:26:54 +03001832 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001833 slot = &global_data.key_slots[key];
1834 if( slot->type == PSA_KEY_TYPE_NONE )
1835 return( PSA_ERROR_EMPTY_SLOT );
1836 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1837 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001838 if( ! ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) )
1839 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001840
1841#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001842 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
1843 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001844 {
1845 mbedtls_rsa_context *rsa = slot->data.rsa;
1846 int ret;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001847 if( output_size < rsa->len )
Gilles Peskine61b91d42018-06-08 16:09:36 +02001848 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001849#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001850 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001851 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001852 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
1853 mbedtls_ctr_drbg_random,
1854 &global_data.ctr_drbg,
1855 MBEDTLS_RSA_PUBLIC,
1856 input_length,
1857 input,
1858 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001859 }
1860 else
1861#endif /* MBEDTLS_PKCS1_V15 */
1862#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine625b01c2018-06-08 17:43:16 +02001863 if( PSA_ALG_IS_RSA_OAEP_MGF1( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001864 {
1865 return( PSA_ERROR_NOT_SUPPORTED );
1866 }
1867 else
1868#endif /* MBEDTLS_PKCS1_V21 */
1869 {
1870 return( PSA_ERROR_INVALID_ARGUMENT );
1871 }
1872 if( ret == 0 )
Nir Sonnenschein717a0402018-06-04 16:36:15 +03001873 *output_length = rsa->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001874 return( mbedtls_to_psa_error( ret ) );
1875 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001876 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02001877#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001878 {
1879 return( PSA_ERROR_NOT_SUPPORTED );
1880 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001881}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001882
Gilles Peskine61b91d42018-06-08 16:09:36 +02001883psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
1884 psa_algorithm_t alg,
1885 const uint8_t *input,
1886 size_t input_length,
1887 const uint8_t *salt,
1888 size_t salt_length,
1889 uint8_t *output,
1890 size_t output_size,
1891 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001892{
1893 key_slot_t *slot;
1894 (void) salt;
1895 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001896 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001897
Gilles Peskine828ed142018-06-18 23:25:51 +02001898 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001899 return( PSA_ERROR_EMPTY_SLOT );
1900 slot = &global_data.key_slots[key];
1901 if( slot->type == PSA_KEY_TYPE_NONE )
1902 return( PSA_ERROR_EMPTY_SLOT );
1903 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1904 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001905 if( ! ( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
1906 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001907
1908#if defined(MBEDTLS_RSA_C)
1909 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1910 {
1911 mbedtls_rsa_context *rsa = slot->data.rsa;
1912 int ret;
1913
Gilles Peskinec4def2f2018-06-08 17:53:48 +02001914 if( input_length != rsa->len )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001915 return( PSA_ERROR_INVALID_ARGUMENT );
1916
1917#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001918 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001919 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001920 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
1921 mbedtls_ctr_drbg_random,
1922 &global_data.ctr_drbg,
1923 MBEDTLS_RSA_PRIVATE,
1924 output_length,
1925 input,
1926 output,
1927 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001928 }
1929 else
1930#endif /* MBEDTLS_PKCS1_V15 */
1931#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine625b01c2018-06-08 17:43:16 +02001932 if( PSA_ALG_IS_RSA_OAEP_MGF1( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001933 {
1934 return( PSA_ERROR_NOT_SUPPORTED );
1935 }
1936 else
1937#endif /* MBEDTLS_PKCS1_V21 */
1938 {
1939 return( PSA_ERROR_INVALID_ARGUMENT );
1940 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03001941
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001942 return( mbedtls_to_psa_error( ret ) );
1943 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001944 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02001945#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001946 {
1947 return( PSA_ERROR_NOT_SUPPORTED );
1948 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001949}
Gilles Peskine20035e32018-02-03 22:44:14 +01001950
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001951
1952
mohammad1603503973b2018-03-12 15:59:30 +02001953/****************************************************************/
1954/* Symmetric cryptography */
1955/****************************************************************/
1956
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001957/* Initialize the cipher operation structure. Once this function has been
1958 * called, psa_cipher_abort can run and will do the right thing. */
1959static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
1960 psa_algorithm_t alg )
1961{
Gilles Peskinec06e0712018-06-20 16:21:04 +02001962 if( ! PSA_ALG_IS_CIPHER( alg ) )
1963 {
1964 memset( operation, 0, sizeof( *operation ) );
1965 return( PSA_ERROR_INVALID_ARGUMENT );
1966 }
1967
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001968 operation->alg = alg;
1969 operation->key_set = 0;
1970 operation->iv_set = 0;
1971 operation->iv_required = 1;
1972 operation->iv_size = 0;
1973 operation->block_size = 0;
1974 mbedtls_cipher_init( &operation->ctx.cipher );
1975 return( PSA_SUCCESS );
1976}
1977
Gilles Peskinee553c652018-06-04 16:22:46 +02001978static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
1979 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02001980 psa_algorithm_t alg,
1981 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02001982{
1983 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1984 psa_status_t status;
1985 key_slot_t *slot;
1986 psa_key_type_t key_type;
1987 size_t key_bits;
1988 const mbedtls_cipher_info_t *cipher_info = NULL;
1989
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001990 status = psa_cipher_init( operation, alg );
1991 if( status != PSA_SUCCESS )
1992 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02001993
1994 status = psa_get_key_information( key, &key_type, &key_bits );
1995 if( status != PSA_SUCCESS )
1996 return( status );
1997 slot = &global_data.key_slots[key];
1998
Gilles Peskinebb1072f2018-06-08 18:46:05 +02001999 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02002000 if( cipher_info == NULL )
2001 return( PSA_ERROR_NOT_SUPPORTED );
2002
mohammad1603503973b2018-03-12 15:59:30 +02002003 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03002004 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002005 {
2006 psa_cipher_abort( operation );
2007 return( mbedtls_to_psa_error( ret ) );
2008 }
2009
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002010#if defined(MBEDTLS_DES_C)
2011 if( key_type == PSA_KEY_TYPE_DES && key_bits == 128 )
2012 {
2013 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2014 unsigned char keys[24];
2015 memcpy( keys, slot->data.raw.data, 16 );
2016 memcpy( keys + 16, slot->data.raw.data, 8 );
2017 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2018 keys,
2019 192, cipher_operation );
2020 }
2021 else
2022#endif
2023 {
2024 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2025 slot->data.raw.data,
2026 key_bits, cipher_operation );
2027 }
Moran Peker41deec42018-04-04 15:43:05 +03002028 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002029 {
2030 psa_cipher_abort( operation );
2031 return( mbedtls_to_psa_error( ret ) );
2032 }
2033
mohammad16038481e742018-03-18 13:57:31 +02002034#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002035 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002036 {
Gilles Peskine53514202018-06-06 15:11:46 +02002037 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2038 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002039
Moran Pekera28258c2018-05-29 16:25:04 +03002040 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002041 {
2042 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2043 mode = MBEDTLS_PADDING_PKCS7;
2044 break;
2045 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2046 mode = MBEDTLS_PADDING_NONE;
2047 break;
2048 default:
Moran Pekerae382792018-05-31 14:06:17 +03002049 psa_cipher_abort( operation );
2050 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002051 }
2052 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002053 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002054 {
2055 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002056 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002057 }
mohammad16038481e742018-03-18 13:57:31 +02002058 }
2059#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2060
mohammad1603503973b2018-03-12 15:59:30 +02002061 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002062 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
2063 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) :
2064 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002065 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002066 {
mohammad160389e0f462018-04-12 08:48:45 +03002067 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
mohammad16038481e742018-03-18 13:57:31 +02002068 }
mohammad1603503973b2018-03-12 15:59:30 +02002069
Moran Peker395db872018-05-31 14:07:14 +03002070 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002071}
2072
Gilles Peskinee553c652018-06-04 16:22:46 +02002073psa_status_t psa_encrypt_setup( psa_cipher_operation_t *operation,
2074 psa_key_slot_t key,
2075 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002076{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002077 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002078}
2079
Gilles Peskinee553c652018-06-04 16:22:46 +02002080psa_status_t psa_decrypt_setup( psa_cipher_operation_t *operation,
2081 psa_key_slot_t key,
2082 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002083{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002084 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002085}
2086
Gilles Peskinee553c652018-06-04 16:22:46 +02002087psa_status_t psa_encrypt_generate_iv( psa_cipher_operation_t *operation,
2088 unsigned char *iv,
2089 size_t iv_size,
2090 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002091{
Moran Peker41deec42018-04-04 15:43:05 +03002092 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002093 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002094 return( PSA_ERROR_BAD_STATE );
2095 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002096 {
Moran Peker41deec42018-04-04 15:43:05 +03002097 ret = PSA_ERROR_BUFFER_TOO_SMALL;
2098 goto exit;
2099 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002100 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2101 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002102 if( ret != 0 )
2103 {
2104 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002105 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002106 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002107
mohammad16038481e742018-03-18 13:57:31 +02002108 *iv_length = operation->iv_size;
mohammad160389e0f462018-04-12 08:48:45 +03002109 ret = psa_encrypt_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002110
Moran Peker395db872018-05-31 14:07:14 +03002111exit:
2112 if( ret != PSA_SUCCESS )
2113 psa_cipher_abort( operation );
2114 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02002115}
2116
Gilles Peskinee553c652018-06-04 16:22:46 +02002117psa_status_t psa_encrypt_set_iv( psa_cipher_operation_t *operation,
2118 const unsigned char *iv,
2119 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002120{
Moran Peker41deec42018-04-04 15:43:05 +03002121 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002122 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002123 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03002124 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002125 {
Moran Pekerae382792018-05-31 14:06:17 +03002126 psa_cipher_abort( operation );
2127 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03002128 }
mohammad1603503973b2018-03-12 15:59:30 +02002129 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002130 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002131 {
2132 psa_cipher_abort( operation );
2133 return( mbedtls_to_psa_error( ret ) );
2134 }
2135
2136 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02002137
Moran Peker395db872018-05-31 14:07:14 +03002138 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002139}
2140
Gilles Peskinee553c652018-06-04 16:22:46 +02002141psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2142 const uint8_t *input,
2143 size_t input_length,
2144 unsigned char *output,
2145 size_t output_size,
2146 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002147{
2148 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002149 size_t expected_output_size;
2150 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2151 {
2152 /* Take the unprocessed partial block left over from previous
2153 * update calls, if any, plus the input to this call. Remove
2154 * the last partial block, if any. You get the data that will be
2155 * output in this call. */
2156 expected_output_size =
2157 ( operation->ctx.cipher.unprocessed_len + input_length )
2158 / operation->block_size * operation->block_size;
2159 }
2160 else
2161 {
2162 expected_output_size = input_length;
2163 }
2164 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03002165 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02002166
mohammad1603503973b2018-03-12 15:59:30 +02002167 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002168 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002169 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002170 {
2171 psa_cipher_abort( operation );
2172 return( mbedtls_to_psa_error( ret ) );
2173 }
2174
Moran Peker395db872018-05-31 14:07:14 +03002175 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002176}
2177
Gilles Peskinee553c652018-06-04 16:22:46 +02002178psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2179 uint8_t *output,
2180 size_t output_size,
2181 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002182{
2183 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002184 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002185
mohammad1603503973b2018-03-12 15:59:30 +02002186 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002187 {
Moran Peker7cb22b82018-06-05 11:40:02 +03002188 psa_cipher_abort( operation );
2189 return( PSA_ERROR_BAD_STATE );
2190 }
2191 if( operation->iv_required && ! operation->iv_set )
2192 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002193 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002194 return( PSA_ERROR_BAD_STATE );
2195 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002196 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2197 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002198 {
Gilles Peskine53514202018-06-06 15:11:46 +02002199 psa_algorithm_t padding_mode =
2200 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002201 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002202 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002203 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002204 return( PSA_ERROR_TAMPERING_DETECTED );
2205 }
Gilles Peskine53514202018-06-06 15:11:46 +02002206 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002207 {
2208 if( operation->ctx.cipher.unprocessed_len != 0 )
2209 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002210 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002211 return( PSA_ERROR_INVALID_ARGUMENT );
2212 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002213 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002214 }
2215
2216 ret = mbedtls_cipher_finish( &operation->ctx.cipher, temp_output_buffer,
Gilles Peskinee553c652018-06-04 16:22:46 +02002217 output_length );
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 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002223 if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002224 memcpy( output, temp_output_buffer, *output_length );
2225 else
2226 {
2227 psa_cipher_abort( operation );
2228 return( PSA_ERROR_BUFFER_TOO_SMALL );
2229 }
mohammad1603503973b2018-03-12 15:59:30 +02002230
Moran Peker4c80d832018-04-22 20:15:31 +03002231 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002232}
2233
Gilles Peskinee553c652018-06-04 16:22:46 +02002234psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2235{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002236 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002237 {
2238 /* The object has (apparently) been initialized but it is not
2239 * in use. It's ok to call abort on such an object, and there's
2240 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002241 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002242 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002243
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002244 /* Sanity check (shouldn't happen: operation->alg should
2245 * always have been initialized to a valid value). */
2246 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2247 return( PSA_ERROR_BAD_STATE );
2248
mohammad1603503973b2018-03-12 15:59:30 +02002249 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002250
Moran Peker41deec42018-04-04 15:43:05 +03002251 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002252 operation->key_set = 0;
2253 operation->iv_set = 0;
2254 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002255 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002256 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002257
Moran Peker395db872018-05-31 14:07:14 +03002258 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002259}
2260
Gilles Peskinea0655c32018-04-30 17:06:50 +02002261
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002262
mohammad16038cc1cee2018-03-28 01:21:33 +03002263/****************************************************************/
2264/* Key Policy */
2265/****************************************************************/
2266
Gilles Peskine2d277862018-06-18 15:41:12 +02002267void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002268{
Gilles Peskine803ce742018-06-18 16:07:14 +02002269 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002270}
2271
Gilles Peskine2d277862018-06-18 15:41:12 +02002272void psa_key_policy_set_usage( psa_key_policy_t *policy,
2273 psa_key_usage_t usage,
2274 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002275{
mohammad16034eed7572018-03-28 05:14:59 -07002276 policy->usage = usage;
2277 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002278}
2279
Gilles Peskine2d277862018-06-18 15:41:12 +02002280psa_key_usage_t psa_key_policy_get_usage( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002281{
mohammad16036df908f2018-04-02 08:34:15 -07002282 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002283}
2284
Gilles Peskine2d277862018-06-18 15:41:12 +02002285psa_algorithm_t psa_key_policy_get_algorithm( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002286{
mohammad16036df908f2018-04-02 08:34:15 -07002287 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002288}
2289
Gilles Peskine2d277862018-06-18 15:41:12 +02002290psa_status_t psa_set_key_policy( psa_key_slot_t key,
2291 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002292{
2293 key_slot_t *slot;
mohammad16038cc1cee2018-03-28 01:21:33 +03002294
Gilles Peskine828ed142018-06-18 23:25:51 +02002295 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002296 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002297
mohammad16038cc1cee2018-03-28 01:21:33 +03002298 slot = &global_data.key_slots[key];
2299 if( slot->type != PSA_KEY_TYPE_NONE )
2300 return( PSA_ERROR_OCCUPIED_SLOT );
2301
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002302 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2303 PSA_KEY_USAGE_ENCRYPT |
2304 PSA_KEY_USAGE_DECRYPT |
2305 PSA_KEY_USAGE_SIGN |
2306 PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002307 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002308
mohammad16036df908f2018-04-02 08:34:15 -07002309 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002310
2311 return( PSA_SUCCESS );
2312}
2313
Gilles Peskine2d277862018-06-18 15:41:12 +02002314psa_status_t psa_get_key_policy( psa_key_slot_t key,
2315 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002316{
2317 key_slot_t *slot;
2318
Gilles Peskine828ed142018-06-18 23:25:51 +02002319 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002320 return( PSA_ERROR_INVALID_ARGUMENT );
2321
2322 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002323
mohammad16036df908f2018-04-02 08:34:15 -07002324 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002325
2326 return( PSA_SUCCESS );
2327}
Gilles Peskine20035e32018-02-03 22:44:14 +01002328
Gilles Peskinea0655c32018-04-30 17:06:50 +02002329
2330
mohammad1603804cd712018-03-20 22:44:08 +02002331/****************************************************************/
2332/* Key Lifetime */
2333/****************************************************************/
2334
Gilles Peskine2d277862018-06-18 15:41:12 +02002335psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2336 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002337{
2338 key_slot_t *slot;
2339
Gilles Peskine828ed142018-06-18 23:25:51 +02002340 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002341 return( PSA_ERROR_INVALID_ARGUMENT );
2342
2343 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002344
mohammad1603804cd712018-03-20 22:44:08 +02002345 *lifetime = slot->lifetime;
2346
2347 return( PSA_SUCCESS );
2348}
2349
Gilles Peskine2d277862018-06-18 15:41:12 +02002350psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
2351 const psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002352{
2353 key_slot_t *slot;
2354
Gilles Peskine828ed142018-06-18 23:25:51 +02002355 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002356 return( PSA_ERROR_INVALID_ARGUMENT );
2357
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002358 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2359 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002360 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2361 return( PSA_ERROR_INVALID_ARGUMENT );
2362
mohammad1603804cd712018-03-20 22:44:08 +02002363 slot = &global_data.key_slots[key];
mohammad16035d7ec202018-03-28 01:29:41 +03002364 if( slot->type != PSA_KEY_TYPE_NONE )
2365 return( PSA_ERROR_OCCUPIED_SLOT );
mohammad1603804cd712018-03-20 22:44:08 +02002366
Moran Pekerd7326592018-05-29 16:56:39 +03002367 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002368 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002369
mohammad1603060ad8a2018-03-20 14:28:38 -07002370 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002371
2372 return( PSA_SUCCESS );
2373}
2374
Gilles Peskine20035e32018-02-03 22:44:14 +01002375
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002376
mohammad16035955c982018-04-26 00:53:03 +03002377/****************************************************************/
2378/* AEAD */
2379/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002380
mohammad16035955c982018-04-26 00:53:03 +03002381psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2382 psa_algorithm_t alg,
2383 const uint8_t *nonce,
2384 size_t nonce_length,
2385 const uint8_t *additional_data,
2386 size_t additional_data_length,
2387 const uint8_t *plaintext,
2388 size_t plaintext_length,
2389 uint8_t *ciphertext,
2390 size_t ciphertext_size,
2391 size_t *ciphertext_length )
2392{
2393 int ret;
2394 psa_status_t status;
2395 key_slot_t *slot;
2396 psa_key_type_t key_type;
2397 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002398 uint8_t *tag;
2399 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002400 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002401 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002402
mohammad1603f08a5502018-06-03 15:05:47 +03002403 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002404
mohammad16035955c982018-04-26 00:53:03 +03002405 status = psa_get_key_information( key, &key_type, &key_bits );
2406 if( status != PSA_SUCCESS )
2407 return( status );
2408 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002409 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002410 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002411
mohammad16035ed06212018-06-06 13:09:34 +03002412 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2413 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002414 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002415 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002416
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002417 if( ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) == 0 )
mohammad1603f14394b2018-06-04 14:33:19 +03002418 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002419
Gilles Peskine2d277862018-06-18 15:41:12 +02002420 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2421 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002422 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002423
mohammad16035955c982018-04-26 00:53:03 +03002424 if( alg == PSA_ALG_GCM )
2425 {
2426 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002427 tag_length = 16;
2428
mohammad160396910d82018-06-04 14:33:00 +03002429 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2430 return( PSA_ERROR_INVALID_ARGUMENT );
2431
mohammad160315223a82018-06-03 17:19:55 +03002432 //make sure we have place to hold the tag in the ciphertext buffer
2433 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002434 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002435
2436 //update the tag pointer to point to the end of the ciphertext_length
2437 tag = ciphertext + plaintext_length;
2438
mohammad16035955c982018-04-26 00:53:03 +03002439 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002440 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002441 slot->data.raw.data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002442 key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002443 if( ret != 0 )
2444 {
2445 mbedtls_gcm_free( &gcm );
2446 return( mbedtls_to_psa_error( ret ) );
2447 }
2448 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002449 plaintext_length, nonce,
2450 nonce_length, additional_data,
2451 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002452 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002453 mbedtls_gcm_free( &gcm );
2454 }
2455 else if( alg == PSA_ALG_CCM )
2456 {
2457 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002458 tag_length = 16;
2459
mohammad160396910d82018-06-04 14:33:00 +03002460 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2461 return( PSA_ERROR_INVALID_ARGUMENT );
2462
mohammad160347ddf3d2018-04-26 01:11:21 +03002463 if( nonce_length < 7 || nonce_length > 13 )
2464 return( PSA_ERROR_INVALID_ARGUMENT );
2465
mohammad160315223a82018-06-03 17:19:55 +03002466 //make sure we have place to hold the tag in the ciphertext buffer
2467 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002468 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002469
2470 //update the tag pointer to point to the end of the ciphertext_length
2471 tag = ciphertext + plaintext_length;
2472
mohammad16035955c982018-04-26 00:53:03 +03002473 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002474 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002475 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002476 if( ret != 0 )
2477 {
2478 mbedtls_ccm_free( &ccm );
2479 return( mbedtls_to_psa_error( ret ) );
2480 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002481 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002482 nonce, nonce_length,
2483 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002484 additional_data_length,
2485 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002486 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002487 mbedtls_ccm_free( &ccm );
2488 }
mohammad16035c8845f2018-05-09 05:40:09 -07002489 else
2490 {
mohammad1603554faad2018-06-03 15:07:38 +03002491 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002492 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002493
mohammad160315223a82018-06-03 17:19:55 +03002494 if( ret != 0 )
2495 {
2496 memset( ciphertext, 0, ciphertext_size );
2497 return( mbedtls_to_psa_error( ret ) );
2498 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002499
mohammad160315223a82018-06-03 17:19:55 +03002500 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002501 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002502}
2503
Gilles Peskineee652a32018-06-01 19:23:52 +02002504/* Locate the tag in a ciphertext buffer containing the encrypted data
2505 * followed by the tag. Return the length of the part preceding the tag in
2506 * *plaintext_length. This is the size of the plaintext in modes where
2507 * the encrypted data has the same size as the plaintext, such as
2508 * CCM and GCM. */
2509static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2510 const uint8_t *ciphertext,
2511 size_t ciphertext_length,
2512 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002513 const uint8_t **p_tag )
2514{
2515 size_t payload_length;
2516 if( tag_length > ciphertext_length )
2517 return( PSA_ERROR_INVALID_ARGUMENT );
2518 payload_length = ciphertext_length - tag_length;
2519 if( payload_length > plaintext_size )
2520 return( PSA_ERROR_BUFFER_TOO_SMALL );
2521 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002522 return( PSA_SUCCESS );
2523}
2524
mohammad16035955c982018-04-26 00:53:03 +03002525psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2526 psa_algorithm_t alg,
2527 const uint8_t *nonce,
2528 size_t nonce_length,
2529 const uint8_t *additional_data,
2530 size_t additional_data_length,
2531 const uint8_t *ciphertext,
2532 size_t ciphertext_length,
2533 uint8_t *plaintext,
2534 size_t plaintext_size,
2535 size_t *plaintext_length )
2536{
2537 int ret;
2538 psa_status_t status;
2539 key_slot_t *slot;
2540 psa_key_type_t key_type;
2541 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002542 const uint8_t *tag;
2543 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002544 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002545 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002546
Gilles Peskineee652a32018-06-01 19:23:52 +02002547 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002548
mohammad16035955c982018-04-26 00:53:03 +03002549 status = psa_get_key_information( key, &key_type, &key_bits );
2550 if( status != PSA_SUCCESS )
2551 return( status );
2552 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002553 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002554 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002555
mohammad16035ed06212018-06-06 13:09:34 +03002556 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2557 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002558 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002559 return( PSA_ERROR_NOT_SUPPORTED );
2560
mohammad1603f14394b2018-06-04 14:33:19 +03002561 if( !( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
2562 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002563
Gilles Peskine2d277862018-06-18 15:41:12 +02002564 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2565 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002566 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002567
mohammad16035955c982018-04-26 00:53:03 +03002568 if( alg == PSA_ALG_GCM )
2569 {
2570 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002571
Gilles Peskineee652a32018-06-01 19:23:52 +02002572 tag_length = 16;
2573 status = psa_aead_unpadded_locate_tag( tag_length,
2574 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002575 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002576 if( status != PSA_SUCCESS )
2577 return( status );
2578
mohammad16035955c982018-04-26 00:53:03 +03002579 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002580 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002581 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002582 if( ret != 0 )
2583 {
2584 mbedtls_gcm_free( &gcm );
2585 return( mbedtls_to_psa_error( ret ) );
2586 }
mohammad16035955c982018-04-26 00:53:03 +03002587
Gilles Peskineee652a32018-06-01 19:23:52 +02002588 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002589 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002590 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002591 additional_data,
2592 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002593 tag, tag_length,
2594 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002595 mbedtls_gcm_free( &gcm );
2596 }
2597 else if( alg == PSA_ALG_CCM )
2598 {
2599 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002600
mohammad160347ddf3d2018-04-26 01:11:21 +03002601 if( nonce_length < 7 || nonce_length > 13 )
2602 return( PSA_ERROR_INVALID_ARGUMENT );
2603
mohammad16039375f842018-06-03 14:28:24 +03002604 tag_length = 16;
2605 status = psa_aead_unpadded_locate_tag( tag_length,
2606 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002607 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002608 if( status != PSA_SUCCESS )
2609 return( status );
2610
mohammad16035955c982018-04-26 00:53:03 +03002611 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002612 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002613 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002614 if( ret != 0 )
2615 {
2616 mbedtls_ccm_free( &ccm );
2617 return( mbedtls_to_psa_error( ret ) );
2618 }
mohammad160360a64d02018-06-03 17:20:42 +03002619 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002620 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002621 additional_data,
2622 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002623 ciphertext, plaintext,
2624 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002625 mbedtls_ccm_free( &ccm );
2626 }
mohammad160339574652018-06-01 04:39:53 -07002627 else
2628 {
mohammad1603554faad2018-06-03 15:07:38 +03002629 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07002630 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002631
Gilles Peskineee652a32018-06-01 19:23:52 +02002632 if( ret != 0 )
mohammad160360a64d02018-06-03 17:20:42 +03002633 memset( plaintext, 0, plaintext_size );
2634 else
2635 *plaintext_length = ciphertext_length - tag_length;
2636
Gilles Peskineee652a32018-06-01 19:23:52 +02002637 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03002638}
2639
Gilles Peskinea0655c32018-04-30 17:06:50 +02002640
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002641
Gilles Peskine20035e32018-02-03 22:44:14 +01002642/****************************************************************/
Gilles Peskine05d69892018-06-19 22:00:52 +02002643/* Key generation */
2644/****************************************************************/
2645
2646psa_status_t psa_generate_random( uint8_t *output,
2647 size_t output_size )
2648{
2649 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2650 output, output_size );
2651 return( mbedtls_to_psa_error( ret ) );
2652}
2653
2654psa_status_t psa_generate_key( psa_key_slot_t key,
2655 psa_key_type_t type,
2656 size_t bits,
2657 const void *parameters,
2658 size_t parameters_size )
2659{
Gilles Peskine12313cd2018-06-20 00:20:32 +02002660 key_slot_t *slot;
2661
2662 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
2663 return( PSA_ERROR_INVALID_ARGUMENT );
2664 slot = &global_data.key_slots[key];
2665 if( slot->type != PSA_KEY_TYPE_NONE )
2666 return( PSA_ERROR_OCCUPIED_SLOT );
2667 if( parameters == NULL && parameters_size != 0 )
2668 return( PSA_ERROR_INVALID_ARGUMENT );
2669
Gilles Peskine48c0ea12018-06-21 14:15:31 +02002670 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002671 {
2672 psa_status_t status = prepare_raw_data_slot( type, bits,
2673 &slot->data.raw );
2674 if( status != PSA_SUCCESS )
2675 return( status );
2676 status = psa_generate_random( slot->data.raw.data,
2677 slot->data.raw.bytes );
2678 if( status != PSA_SUCCESS )
2679 {
2680 mbedtls_free( slot->data.raw.data );
2681 return( status );
2682 }
2683#if defined(MBEDTLS_DES_C)
2684 if( type == PSA_KEY_TYPE_DES )
2685 {
2686 mbedtls_des_key_set_parity( slot->data.raw.data );
2687 if( slot->data.raw.bytes >= 16 )
2688 mbedtls_des_key_set_parity( slot->data.raw.data + 8 );
2689 if( slot->data.raw.bytes == 24 )
2690 mbedtls_des_key_set_parity( slot->data.raw.data + 16 );
2691 }
2692#endif /* MBEDTLS_DES_C */
2693 }
2694 else
2695
2696#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
2697 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2698 {
2699 mbedtls_rsa_context *rsa;
2700 int ret;
2701 int exponent = 65537;
2702 if( parameters != NULL )
2703 {
2704 const unsigned *p = parameters;
2705 if( parameters_size != sizeof( *p ) )
2706 return( PSA_ERROR_INVALID_ARGUMENT );
2707 if( *p > INT_MAX )
2708 return( PSA_ERROR_INVALID_ARGUMENT );
2709 exponent = *p;
2710 }
2711 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
2712 if( rsa == NULL )
2713 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2714 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
2715 ret = mbedtls_rsa_gen_key( rsa,
2716 mbedtls_ctr_drbg_random,
2717 &global_data.ctr_drbg,
2718 bits,
2719 exponent );
2720 if( ret != 0 )
2721 {
2722 mbedtls_rsa_free( rsa );
2723 mbedtls_free( rsa );
2724 return( mbedtls_to_psa_error( ret ) );
2725 }
2726 slot->data.rsa = rsa;
2727 }
2728 else
2729#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
2730
2731#if defined(MBEDTLS_ECP_C)
2732 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
2733 {
2734 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
2735 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
2736 const mbedtls_ecp_curve_info *curve_info =
2737 mbedtls_ecp_curve_info_from_grp_id( grp_id );
2738 mbedtls_ecp_keypair *ecp;
2739 int ret;
2740 if( parameters != NULL )
2741 return( PSA_ERROR_NOT_SUPPORTED );
2742 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
2743 return( PSA_ERROR_NOT_SUPPORTED );
2744 if( curve_info->bit_size != bits )
2745 return( PSA_ERROR_INVALID_ARGUMENT );
2746 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
2747 if( ecp == NULL )
2748 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2749 mbedtls_ecp_keypair_init( ecp );
2750 ret = mbedtls_ecp_gen_key( grp_id, ecp,
2751 mbedtls_ctr_drbg_random,
2752 &global_data.ctr_drbg );
2753 if( ret != 0 )
2754 {
2755 mbedtls_ecp_keypair_free( ecp );
2756 mbedtls_free( ecp );
2757 return( mbedtls_to_psa_error( ret ) );
2758 }
2759 slot->data.ecp = ecp;
2760 }
2761 else
2762#endif /* MBEDTLS_ECP_C */
2763
2764 return( PSA_ERROR_NOT_SUPPORTED );
2765
2766 slot->type = type;
2767 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02002768}
2769
2770
2771/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002772/* Module setup */
2773/****************************************************************/
2774
Gilles Peskinee59236f2018-01-27 23:32:46 +01002775void mbedtls_psa_crypto_free( void )
2776{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002777 size_t key;
Gilles Peskine828ed142018-06-18 23:25:51 +02002778 for( key = 1; key < PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002779 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002780 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
2781 mbedtls_entropy_free( &global_data.entropy );
2782 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2783}
2784
2785psa_status_t psa_crypto_init( void )
2786{
2787 int ret;
2788 const unsigned char drbg_seed[] = "PSA";
2789
2790 if( global_data.initialized != 0 )
2791 return( PSA_SUCCESS );
2792
2793 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2794 mbedtls_entropy_init( &global_data.entropy );
2795 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
2796
2797 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
2798 mbedtls_entropy_func,
2799 &global_data.entropy,
2800 drbg_seed, sizeof( drbg_seed ) - 1 );
2801 if( ret != 0 )
2802 goto exit;
2803
Gilles Peskinee4ebc122018-03-07 14:16:44 +01002804 global_data.initialized = 1;
2805
Gilles Peskinee59236f2018-01-27 23:32:46 +01002806exit:
2807 if( ret != 0 )
2808 mbedtls_psa_crypto_free( );
2809 return( mbedtls_to_psa_error( ret ) );
2810}
2811
2812#endif /* MBEDTLS_PSA_CRYPTO_C */