blob: 2670e41398b185b707dd68bb731362772b7d4f81 [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
Gilles Peskine828ed142018-06-18 23:25:51 +0200639 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100640 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100641 slot = &global_data.key_slots[key];
642 if( slot->type == PSA_KEY_TYPE_NONE )
643 return( PSA_ERROR_EMPTY_SLOT );
644
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200645 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300646 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +0300647
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200648 if( ! export_public_key &&
649 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) &&
650 ( slot->policy.usage & PSA_KEY_USAGE_EXPORT ) == 0 )
Moran Peker87567632018-06-04 18:41:37 +0300651 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine2d277862018-06-18 15:41:12 +0200652
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200653 if( key_type_is_raw_bytes( slot->type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100654 {
655 if( slot->data.raw.bytes > data_size )
656 return( PSA_ERROR_BUFFER_TOO_SMALL );
657 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
658 *data_length = slot->data.raw.bytes;
659 return( PSA_SUCCESS );
660 }
661 else
Moran Peker17e36e12018-05-02 12:55:20 +0300662 {
Gilles Peskine969ac722018-01-28 18:16:59 +0100663#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskinec66ea6a2018-02-03 22:43:28 +0100664 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
Moran Pekera998bc62018-04-16 18:16:20 +0300665 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
666 PSA_KEY_TYPE_IS_ECC( slot->type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +0100667 {
Moran Pekera998bc62018-04-16 18:16:20 +0300668 mbedtls_pk_context pk;
669 int ret;
670 mbedtls_pk_init( &pk );
671 if( slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY ||
672 slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
673 {
674 pk.pk_info = &mbedtls_rsa_info;
675 pk.pk_ctx = slot->data.rsa;
676 }
677 else
678 {
679 pk.pk_info = &mbedtls_eckey_info;
680 pk.pk_ctx = slot->data.ecp;
681 }
Moran Pekerd7326592018-05-29 16:56:39 +0300682 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->type ) )
Moran Pekera998bc62018-04-16 18:16:20 +0300683 ret = mbedtls_pk_write_pubkey_der( &pk, data, data_size );
Moran Peker17e36e12018-05-02 12:55:20 +0300684 else
685 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Moran Peker60364322018-04-29 11:34:58 +0300686 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200687 {
688 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +0300689 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200690 }
Gilles Peskine0e231582018-06-20 00:11:07 +0200691 /* The mbedtls_pk_xxx functions write to the end of the buffer.
692 * Move the data to the beginning and erase remaining data
693 * at the original location. */
694 if( 2 * (size_t) ret <= data_size )
695 {
696 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200697 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200698 }
699 else if( (size_t) ret < data_size )
700 {
701 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +0200702 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +0200703 }
Moran Pekera998bc62018-04-16 18:16:20 +0300704 *data_length = ret;
705 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +0100706 }
707 else
Gilles Peskine6d912132018-03-07 16:41:37 +0100708#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +0300709 {
710 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +0200711 it is valid for a special-purpose implementation to omit
712 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +0300713 return( PSA_ERROR_NOT_SUPPORTED );
714 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100715 }
716}
717
Gilles Peskine2d277862018-06-18 15:41:12 +0200718psa_status_t psa_export_key( psa_key_slot_t key,
719 uint8_t *data,
720 size_t data_size,
721 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +0300722{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200723 return( psa_internal_export_key( key, data, data_size,
724 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100725}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100726
Gilles Peskine2d277862018-06-18 15:41:12 +0200727psa_status_t psa_export_public_key( psa_key_slot_t key,
728 uint8_t *data,
729 size_t data_size,
730 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +0300731{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +0200732 return( psa_internal_export_key( key, data, data_size,
733 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +0300734}
735
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200736
737
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100738/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +0100739/* Message digests */
740/****************************************************************/
741
Gilles Peskinedc2fc842018-03-07 16:42:59 +0100742static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +0100743{
744 switch( alg )
745 {
746#if defined(MBEDTLS_MD2_C)
747 case PSA_ALG_MD2:
748 return( &mbedtls_md2_info );
749#endif
750#if defined(MBEDTLS_MD4_C)
751 case PSA_ALG_MD4:
752 return( &mbedtls_md4_info );
753#endif
754#if defined(MBEDTLS_MD5_C)
755 case PSA_ALG_MD5:
756 return( &mbedtls_md5_info );
757#endif
758#if defined(MBEDTLS_RIPEMD160_C)
759 case PSA_ALG_RIPEMD160:
760 return( &mbedtls_ripemd160_info );
761#endif
762#if defined(MBEDTLS_SHA1_C)
763 case PSA_ALG_SHA_1:
764 return( &mbedtls_sha1_info );
765#endif
766#if defined(MBEDTLS_SHA256_C)
767 case PSA_ALG_SHA_224:
768 return( &mbedtls_sha224_info );
769 case PSA_ALG_SHA_256:
770 return( &mbedtls_sha256_info );
771#endif
772#if defined(MBEDTLS_SHA512_C)
773 case PSA_ALG_SHA_384:
774 return( &mbedtls_sha384_info );
775 case PSA_ALG_SHA_512:
776 return( &mbedtls_sha512_info );
777#endif
778 default:
779 return( NULL );
780 }
781}
782
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100783psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
784{
785 switch( operation->alg )
786 {
Gilles Peskine81736312018-06-26 15:04:31 +0200787 case 0:
788 /* The object has (apparently) been initialized but it is not
789 * in use. It's ok to call abort on such an object, and there's
790 * nothing to do. */
791 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100792#if defined(MBEDTLS_MD2_C)
793 case PSA_ALG_MD2:
794 mbedtls_md2_free( &operation->ctx.md2 );
795 break;
796#endif
797#if defined(MBEDTLS_MD4_C)
798 case PSA_ALG_MD4:
799 mbedtls_md4_free( &operation->ctx.md4 );
800 break;
801#endif
802#if defined(MBEDTLS_MD5_C)
803 case PSA_ALG_MD5:
804 mbedtls_md5_free( &operation->ctx.md5 );
805 break;
806#endif
807#if defined(MBEDTLS_RIPEMD160_C)
808 case PSA_ALG_RIPEMD160:
809 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
810 break;
811#endif
812#if defined(MBEDTLS_SHA1_C)
813 case PSA_ALG_SHA_1:
814 mbedtls_sha1_free( &operation->ctx.sha1 );
815 break;
816#endif
817#if defined(MBEDTLS_SHA256_C)
818 case PSA_ALG_SHA_224:
819 case PSA_ALG_SHA_256:
820 mbedtls_sha256_free( &operation->ctx.sha256 );
821 break;
822#endif
823#if defined(MBEDTLS_SHA512_C)
824 case PSA_ALG_SHA_384:
825 case PSA_ALG_SHA_512:
826 mbedtls_sha512_free( &operation->ctx.sha512 );
827 break;
828#endif
829 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +0200830 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100831 }
832 operation->alg = 0;
833 return( PSA_SUCCESS );
834}
835
836psa_status_t psa_hash_start( psa_hash_operation_t *operation,
837 psa_algorithm_t alg )
838{
839 int ret;
840 operation->alg = 0;
841 switch( alg )
842 {
843#if defined(MBEDTLS_MD2_C)
844 case PSA_ALG_MD2:
845 mbedtls_md2_init( &operation->ctx.md2 );
846 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
847 break;
848#endif
849#if defined(MBEDTLS_MD4_C)
850 case PSA_ALG_MD4:
851 mbedtls_md4_init( &operation->ctx.md4 );
852 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
853 break;
854#endif
855#if defined(MBEDTLS_MD5_C)
856 case PSA_ALG_MD5:
857 mbedtls_md5_init( &operation->ctx.md5 );
858 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
859 break;
860#endif
861#if defined(MBEDTLS_RIPEMD160_C)
862 case PSA_ALG_RIPEMD160:
863 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
864 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
865 break;
866#endif
867#if defined(MBEDTLS_SHA1_C)
868 case PSA_ALG_SHA_1:
869 mbedtls_sha1_init( &operation->ctx.sha1 );
870 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
871 break;
872#endif
873#if defined(MBEDTLS_SHA256_C)
874 case PSA_ALG_SHA_224:
875 mbedtls_sha256_init( &operation->ctx.sha256 );
876 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
877 break;
878 case PSA_ALG_SHA_256:
879 mbedtls_sha256_init( &operation->ctx.sha256 );
880 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
881 break;
882#endif
883#if defined(MBEDTLS_SHA512_C)
884 case PSA_ALG_SHA_384:
885 mbedtls_sha512_init( &operation->ctx.sha512 );
886 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
887 break;
888 case PSA_ALG_SHA_512:
889 mbedtls_sha512_init( &operation->ctx.sha512 );
890 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
891 break;
892#endif
893 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +0200894 return( PSA_ALG_IS_HASH( alg ) ?
895 PSA_ERROR_NOT_SUPPORTED :
896 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100897 }
898 if( ret == 0 )
899 operation->alg = alg;
900 else
901 psa_hash_abort( operation );
902 return( mbedtls_to_psa_error( ret ) );
903}
904
905psa_status_t psa_hash_update( psa_hash_operation_t *operation,
906 const uint8_t *input,
907 size_t input_length )
908{
909 int ret;
910 switch( operation->alg )
911 {
912#if defined(MBEDTLS_MD2_C)
913 case PSA_ALG_MD2:
914 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
915 input, input_length );
916 break;
917#endif
918#if defined(MBEDTLS_MD4_C)
919 case PSA_ALG_MD4:
920 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
921 input, input_length );
922 break;
923#endif
924#if defined(MBEDTLS_MD5_C)
925 case PSA_ALG_MD5:
926 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
927 input, input_length );
928 break;
929#endif
930#if defined(MBEDTLS_RIPEMD160_C)
931 case PSA_ALG_RIPEMD160:
932 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
933 input, input_length );
934 break;
935#endif
936#if defined(MBEDTLS_SHA1_C)
937 case PSA_ALG_SHA_1:
938 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
939 input, input_length );
940 break;
941#endif
942#if defined(MBEDTLS_SHA256_C)
943 case PSA_ALG_SHA_224:
944 case PSA_ALG_SHA_256:
945 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
946 input, input_length );
947 break;
948#endif
949#if defined(MBEDTLS_SHA512_C)
950 case PSA_ALG_SHA_384:
951 case PSA_ALG_SHA_512:
952 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
953 input, input_length );
954 break;
955#endif
956 default:
957 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
958 break;
959 }
960 if( ret != 0 )
961 psa_hash_abort( operation );
962 return( mbedtls_to_psa_error( ret ) );
963}
964
965psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
966 uint8_t *hash,
967 size_t hash_size,
968 size_t *hash_length )
969{
970 int ret;
Gilles Peskine71bb7b72018-04-19 08:29:59 +0200971 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100972
973 /* Fill the output buffer with something that isn't a valid hash
974 * (barring an attack on the hash and deliberately-crafted input),
975 * in case the caller doesn't check the return status properly. */
976 *hash_length = actual_hash_length;
977 memset( hash, '!', hash_size );
978
979 if( hash_size < actual_hash_length )
980 return( PSA_ERROR_BUFFER_TOO_SMALL );
981
982 switch( operation->alg )
983 {
984#if defined(MBEDTLS_MD2_C)
985 case PSA_ALG_MD2:
986 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
987 break;
988#endif
989#if defined(MBEDTLS_MD4_C)
990 case PSA_ALG_MD4:
991 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
992 break;
993#endif
994#if defined(MBEDTLS_MD5_C)
995 case PSA_ALG_MD5:
996 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
997 break;
998#endif
999#if defined(MBEDTLS_RIPEMD160_C)
1000 case PSA_ALG_RIPEMD160:
1001 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
1002 break;
1003#endif
1004#if defined(MBEDTLS_SHA1_C)
1005 case PSA_ALG_SHA_1:
1006 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
1007 break;
1008#endif
1009#if defined(MBEDTLS_SHA256_C)
1010 case PSA_ALG_SHA_224:
1011 case PSA_ALG_SHA_256:
1012 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
1013 break;
1014#endif
1015#if defined(MBEDTLS_SHA512_C)
1016 case PSA_ALG_SHA_384:
1017 case PSA_ALG_SHA_512:
1018 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
1019 break;
1020#endif
1021 default:
1022 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1023 break;
1024 }
1025
1026 if( ret == 0 )
1027 {
1028 return( psa_hash_abort( operation ) );
1029 }
1030 else
1031 {
1032 psa_hash_abort( operation );
1033 return( mbedtls_to_psa_error( ret ) );
1034 }
1035}
1036
Gilles Peskine2d277862018-06-18 15:41:12 +02001037psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
1038 const uint8_t *hash,
1039 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001040{
1041 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
1042 size_t actual_hash_length;
1043 psa_status_t status = psa_hash_finish( operation,
1044 actual_hash, sizeof( actual_hash ),
1045 &actual_hash_length );
1046 if( status != PSA_SUCCESS )
1047 return( status );
1048 if( actual_hash_length != hash_length )
1049 return( PSA_ERROR_INVALID_SIGNATURE );
1050 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
1051 return( PSA_ERROR_INVALID_SIGNATURE );
1052 return( PSA_SUCCESS );
1053}
1054
1055
1056
Gilles Peskine9ef733f2018-02-07 21:05:37 +01001057/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01001058/* MAC */
1059/****************************************************************/
1060
Gilles Peskinedc2fc842018-03-07 16:42:59 +01001061static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01001062 psa_algorithm_t alg,
1063 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02001064 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03001065 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001066{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001067 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03001068 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001069
1070 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
1071 {
1072 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) )
mohammad16038481e742018-03-18 13:57:31 +02001073 {
1074 alg &= ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
1075 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001076
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001077 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001078 {
1079 case PSA_ALG_STREAM_CIPHER:
1080 mode = MBEDTLS_MODE_STREAM;
1081 break;
1082 case PSA_ALG_CBC_BASE:
1083 mode = MBEDTLS_MODE_CBC;
1084 break;
1085 case PSA_ALG_CFB_BASE:
1086 mode = MBEDTLS_MODE_CFB;
1087 break;
1088 case PSA_ALG_OFB_BASE:
1089 mode = MBEDTLS_MODE_OFB;
1090 break;
1091 case PSA_ALG_CTR:
1092 mode = MBEDTLS_MODE_CTR;
1093 break;
1094 case PSA_ALG_CCM:
1095 mode = MBEDTLS_MODE_CCM;
1096 break;
1097 case PSA_ALG_GCM:
1098 mode = MBEDTLS_MODE_GCM;
1099 break;
1100 default:
1101 return( NULL );
1102 }
1103 }
1104 else if( alg == PSA_ALG_CMAC )
1105 mode = MBEDTLS_MODE_ECB;
1106 else if( alg == PSA_ALG_GMAC )
1107 mode = MBEDTLS_MODE_GCM;
1108 else
1109 return( NULL );
1110
1111 switch( key_type )
1112 {
1113 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03001114 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001115 break;
1116 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001117 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
1118 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01001119 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03001120 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001121 else
mohammad1603f4f0d612018-06-03 15:04:51 +03001122 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02001123 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
1124 * but two-key Triple-DES is functionally three-key Triple-DES
1125 * with K1=K3, so that's how we present it to mbedtls. */
1126 if( key_bits == 128 )
1127 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001128 break;
1129 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03001130 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001131 break;
1132 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03001133 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001134 break;
1135 default:
1136 return( NULL );
1137 }
mohammad1603f4f0d612018-06-03 15:04:51 +03001138 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03001139 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001140
mohammad1603f4f0d612018-06-03 15:04:51 +03001141 return( mbedtls_cipher_info_from_values( cipher_id_tmp, key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001142}
1143
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001144static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001145{
Gilles Peskine2d277862018-06-18 15:41:12 +02001146 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03001147 {
1148 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001149 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001150 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001151 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001152 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001153 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001154 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001155 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001156 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001157 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001158 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001159 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001160 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001161 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001162 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03001163 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001164 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02001165 return( 128 );
1166 default:
1167 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03001168 }
1169}
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001170
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001171/* Initialize the MAC operation structure. Once this function has been
1172 * called, psa_mac_abort can run and will do the right thing. */
1173static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
1174 psa_algorithm_t alg )
1175{
1176 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
1177
1178 operation->alg = alg;
1179 operation->key_set = 0;
1180 operation->iv_set = 0;
1181 operation->iv_required = 0;
1182 operation->has_input = 0;
1183 operation->key_usage_sign = 0;
1184 operation->key_usage_verify = 0;
1185
1186#if defined(MBEDTLS_CMAC_C)
1187 if( alg == PSA_ALG_CMAC )
1188 {
1189 operation->iv_required = 0;
1190 mbedtls_cipher_init( &operation->ctx.cmac );
1191 status = PSA_SUCCESS;
1192 }
1193 else
1194#endif /* MBEDTLS_CMAC_C */
1195#if defined(MBEDTLS_MD_C)
1196 if( PSA_ALG_IS_HMAC( operation->alg ) )
1197 {
1198 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1199 PSA_ALG_HMAC_HASH( alg ) );
1200 }
1201 else
1202#endif /* MBEDTLS_MD_C */
1203 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02001204 if( ! PSA_ALG_IS_MAC( alg ) )
1205 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001206 }
1207
1208 if( status != PSA_SUCCESS )
1209 memset( operation, 0, sizeof( *operation ) );
1210 return( status );
1211}
1212
Gilles Peskine8c9def32018-02-08 10:02:12 +01001213psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
1214{
1215 switch( operation->alg )
1216 {
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001217 case 0:
Gilles Peskine81736312018-06-26 15:04:31 +02001218 /* The object has (apparently) been initialized but it is not
1219 * in use. It's ok to call abort on such an object, and there's
1220 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001221 return( PSA_SUCCESS );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001222#if defined(MBEDTLS_CMAC_C)
1223 case PSA_ALG_CMAC:
1224 mbedtls_cipher_free( &operation->ctx.cmac );
1225 break;
1226#endif /* MBEDTLS_CMAC_C */
1227 default:
1228#if defined(MBEDTLS_MD_C)
1229 if( PSA_ALG_IS_HMAC( operation->alg ) )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001230 {
Gilles Peskine99bc6492018-06-11 17:13:00 +02001231 unsigned int block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001232 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001233
Gilles Peskine99bc6492018-06-11 17:13:00 +02001234 if( block_size == 0 )
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001235 return( PSA_ERROR_NOT_SUPPORTED );
1236
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001237 psa_hash_abort( &operation->ctx.hmac.hash_ctx );
Gilles Peskine2d277862018-06-18 15:41:12 +02001238 mbedtls_zeroize( operation->ctx.hmac.opad, block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001239 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001240 else
1241#endif /* MBEDTLS_MD_C */
Gilles Peskinef9c2c092018-06-21 16:57:07 +02001242 {
1243 /* Sanity check (shouldn't happen: operation->alg should
1244 * always have been initialized to a valid value). */
1245 return( PSA_ERROR_BAD_STATE );
1246 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01001247 }
Moran Peker41deec42018-04-04 15:43:05 +03001248
Gilles Peskine8c9def32018-02-08 10:02:12 +01001249 operation->alg = 0;
1250 operation->key_set = 0;
1251 operation->iv_set = 0;
1252 operation->iv_required = 0;
1253 operation->has_input = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001254 operation->key_usage_sign = 0;
1255 operation->key_usage_verify = 0;
Moran Peker41deec42018-04-04 15:43:05 +03001256
Gilles Peskine8c9def32018-02-08 10:02:12 +01001257 return( PSA_SUCCESS );
1258}
1259
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001260#if defined(MBEDTLS_CMAC_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001261static int psa_cmac_start( psa_mac_operation_t *operation,
1262 size_t key_bits,
1263 key_slot_t *slot,
1264 const mbedtls_cipher_info_t *cipher_info )
1265{
1266 int ret;
1267
1268 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001269
1270 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
1271 if( ret != 0 )
1272 return( ret );
1273
1274 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
1275 slot->data.raw.data,
1276 key_bits );
1277 return( ret );
1278}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02001279#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001280
Gilles Peskine248051a2018-06-20 16:09:38 +02001281#if defined(MBEDTLS_MD_C)
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001282static int psa_hmac_start( psa_mac_operation_t *operation,
1283 psa_key_type_t key_type,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001284 key_slot_t *slot,
1285 psa_algorithm_t alg )
1286{
Gilles Peskineb3e6e5d2018-06-18 22:16:43 +02001287 unsigned char ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001288 unsigned char *opad = operation->ctx.hmac.opad;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001289 size_t i;
Gilles Peskinee1bc6802018-06-11 17:36:05 +02001290 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001291 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001292 unsigned int digest_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001293 PSA_HASH_SIZE( PSA_ALG_HMAC_HASH( alg ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001294 size_t key_length = slot->data.raw.bytes;
1295 psa_status_t status;
1296
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001297 if( block_size == 0 || digest_size == 0 )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001298 return( PSA_ERROR_NOT_SUPPORTED );
1299
1300 if( key_type != PSA_KEY_TYPE_HMAC )
1301 return( PSA_ERROR_INVALID_ARGUMENT );
1302
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001303 operation->mac_size = digest_size;
1304
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001305 /* The hash was started earlier in psa_mac_init. */
Gilles Peskined223b522018-06-11 18:12:58 +02001306 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001307 {
1308 status = psa_hash_update( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001309 slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001310 if( status != PSA_SUCCESS )
1311 return( status );
1312 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02001313 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001314 if( status != PSA_SUCCESS )
1315 return( status );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001316 }
Gilles Peskined223b522018-06-11 18:12:58 +02001317 else
1318 memcpy( ipad, slot->data.raw.data, slot->data.raw.bytes );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001319
Gilles Peskined223b522018-06-11 18:12:58 +02001320 /* ipad contains the key followed by garbage. Xor and fill with 0x36
1321 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001322 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02001323 ipad[i] ^= 0x36;
1324 memset( ipad + key_length, 0x36, block_size - key_length );
1325
1326 /* Copy the key material from ipad to opad, flipping the requisite bits,
1327 * and filling the rest of opad with the requisite constant. */
1328 for( i = 0; i < key_length; i++ )
1329 opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
1330 memset( opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001331
1332 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1333 PSA_ALG_HMAC_HASH( alg ) );
1334 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001335 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001336
1337 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, ipad,
1338 block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001339
1340cleanup:
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001341 mbedtls_zeroize( ipad, key_length );
1342 /* opad is in the context. It needs to stay in memory if this function
1343 * succeeds, and it will be wiped by psa_mac_abort() called from
1344 * psa_mac_start in the error case. */
1345
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001346 return( status );
1347}
Gilles Peskine248051a2018-06-20 16:09:38 +02001348#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001349
Gilles Peskine8c9def32018-02-08 10:02:12 +01001350psa_status_t psa_mac_start( psa_mac_operation_t *operation,
1351 psa_key_slot_t key,
1352 psa_algorithm_t alg )
1353{
Gilles Peskine8c9def32018-02-08 10:02:12 +01001354 psa_status_t status;
1355 key_slot_t *slot;
1356 psa_key_type_t key_type;
1357 size_t key_bits;
Gilles Peskine828ed142018-06-18 23:25:51 +02001358 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001359
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001360 status = psa_mac_init( operation, alg );
1361 if( status != PSA_SUCCESS )
1362 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001363
1364 status = psa_get_key_information( key, &key_type, &key_bits );
1365 if( status != PSA_SUCCESS )
1366 return( status );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001367
Gilles Peskine8c9def32018-02-08 10:02:12 +01001368 slot = &global_data.key_slots[key];
Gilles Peskine99bc6492018-06-11 17:13:00 +02001369 if( slot->type == PSA_KEY_TYPE_NONE )
1370 return( PSA_ERROR_EMPTY_SLOT );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001371
Moran Pekerd7326592018-05-29 16:56:39 +03001372 if( ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001373 operation->key_usage_sign = 1;
1374
Moran Pekerd7326592018-05-29 16:56:39 +03001375 if( ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) != 0 )
mohammad16036df908f2018-04-02 08:34:15 -07001376 operation->key_usage_verify = 1;
1377
Gilles Peskine8c9def32018-02-08 10:02:12 +01001378 if( ! PSA_ALG_IS_HMAC( alg ) )
1379 {
mohammad1603f4f0d612018-06-03 15:04:51 +03001380 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001381 if( cipher_info == NULL )
1382 return( PSA_ERROR_NOT_SUPPORTED );
1383 operation->mac_size = cipher_info->block_size;
1384 }
1385 switch( alg )
1386 {
1387#if defined(MBEDTLS_CMAC_C)
1388 case PSA_ALG_CMAC:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001389 status = mbedtls_to_psa_error( psa_cmac_start( operation,
1390 key_bits,
1391 slot,
1392 cipher_info ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001393 break;
1394#endif /* MBEDTLS_CMAC_C */
1395 default:
1396#if defined(MBEDTLS_MD_C)
1397 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskined223b522018-06-11 18:12:58 +02001398 status = psa_hmac_start( operation, key_type, slot, alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001399 else
1400#endif /* MBEDTLS_MD_C */
1401 return( PSA_ERROR_NOT_SUPPORTED );
1402 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001403
Gilles Peskine8c9def32018-02-08 10:02:12 +01001404 /* If we reach this point, then the algorithm-specific part of the
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001405 * context may contain data that needs to be wiped on error. */
1406 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001407 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001408 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001409 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001410 else
1411 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02001412 operation->key_set = 1;
1413 }
1414 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001415}
1416
1417psa_status_t psa_mac_update( psa_mac_operation_t *operation,
1418 const uint8_t *input,
1419 size_t input_length )
1420{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001421 int ret = 0 ;
1422 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001423 if( ! operation->key_set )
1424 return( PSA_ERROR_BAD_STATE );
1425 if( operation->iv_required && ! operation->iv_set )
1426 return( PSA_ERROR_BAD_STATE );
1427 operation->has_input = 1;
1428
1429 switch( operation->alg )
1430 {
1431#if defined(MBEDTLS_CMAC_C)
1432 case PSA_ALG_CMAC:
1433 ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
1434 input, input_length );
1435 break;
1436#endif /* MBEDTLS_CMAC_C */
1437 default:
1438#if defined(MBEDTLS_MD_C)
1439 if( PSA_ALG_IS_HMAC( operation->alg ) )
1440 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001441 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
Gilles Peskine2d277862018-06-18 15:41:12 +02001442 input_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001443 }
1444 else
1445#endif /* MBEDTLS_MD_C */
1446 {
1447 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1448 }
1449 break;
1450 }
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001451 if( ret != 0 || status != PSA_SUCCESS )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001452 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001453 psa_mac_abort( operation );
Gilles Peskine2d277862018-06-18 15:41:12 +02001454 if( ret != 0 )
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03001455 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001456 }
1457
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001458 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001459}
1460
mohammad16036df908f2018-04-02 08:34:15 -07001461static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02001462 uint8_t *mac,
1463 size_t mac_size,
1464 size_t *mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001465{
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001466 int ret = 0;
1467 psa_status_t status = PSA_SUCCESS;
Gilles Peskine8c9def32018-02-08 10:02:12 +01001468 if( ! operation->key_set )
1469 return( PSA_ERROR_BAD_STATE );
1470 if( operation->iv_required && ! operation->iv_set )
1471 return( PSA_ERROR_BAD_STATE );
1472
1473 /* Fill the output buffer with something that isn't a valid mac
1474 * (barring an attack on the mac and deliberately-crafted input),
1475 * in case the caller doesn't check the return status properly. */
1476 *mac_length = operation->mac_size;
1477 memset( mac, '!', mac_size );
1478
1479 if( mac_size < operation->mac_size )
1480 return( PSA_ERROR_BUFFER_TOO_SMALL );
1481
1482 switch( operation->alg )
1483 {
1484#if defined(MBEDTLS_CMAC_C)
1485 case PSA_ALG_CMAC:
1486 ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, mac );
1487 break;
1488#endif /* MBEDTLS_CMAC_C */
1489 default:
1490#if defined(MBEDTLS_MD_C)
1491 if( PSA_ALG_IS_HMAC( operation->alg ) )
1492 {
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001493 unsigned char tmp[MBEDTLS_MD_MAX_SIZE];
Nir Sonnenschein5ca65472018-06-17 14:03:40 +03001494 unsigned char *opad = operation->ctx.hmac.opad;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001495 size_t hash_size = 0;
Nir Sonnenschein96272412018-06-17 14:41:10 +03001496 size_t block_size =
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001497 psa_get_hash_block_size( PSA_ALG_HMAC_HASH( operation->alg ) );
Nir Sonnenschein084832d2018-06-08 22:52:24 +03001498
Gilles Peskine99bc6492018-06-11 17:13:00 +02001499 if( block_size == 0 )
1500 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001501
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001502 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine99bc6492018-06-11 17:13:00 +02001503 sizeof( tmp ), &hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001504 if( status != PSA_SUCCESS )
1505 goto cleanup;
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001506 /* From here on, tmp needs to be wiped. */
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001507
1508 status = psa_hash_start( &operation->ctx.hmac.hash_ctx,
1509 PSA_ALG_HMAC_HASH( operation->alg ) );
1510 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001511 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001512
1513 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, opad,
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03001514 block_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001515 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001516 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001517
1518 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, tmp,
Gilles Peskine2d277862018-06-18 15:41:12 +02001519 hash_size );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001520 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001521 goto hmac_cleanup;
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001522
1523 status = psa_hash_finish( &operation->ctx.hmac.hash_ctx, mac,
1524 mac_size, mac_length );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02001525 hmac_cleanup:
1526 mbedtls_zeroize( tmp, hash_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001527 }
1528 else
1529#endif /* MBEDTLS_MD_C */
1530 {
1531 ret = MBEDTLS_ERR_MD_BAD_INPUT_DATA;
1532 }
1533 break;
1534 }
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001535cleanup:
Gilles Peskine8c9def32018-02-08 10:02:12 +01001536
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001537 if( ret == 0 && status == PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01001538 {
1539 return( psa_mac_abort( operation ) );
1540 }
1541 else
1542 {
1543 psa_mac_abort( operation );
Gilles Peskine99bc6492018-06-11 17:13:00 +02001544 if( ret != 0 )
Gilles Peskine2d277862018-06-18 15:41:12 +02001545 status = mbedtls_to_psa_error( ret );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03001546
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001547 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001548 }
1549}
1550
mohammad16036df908f2018-04-02 08:34:15 -07001551psa_status_t psa_mac_finish( psa_mac_operation_t *operation,
1552 uint8_t *mac,
1553 size_t mac_size,
1554 size_t *mac_length )
1555{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001556 if( ! operation->key_usage_sign )
mohammad16036df908f2018-04-02 08:34:15 -07001557 return( PSA_ERROR_NOT_PERMITTED );
1558
Gilles Peskine99bc6492018-06-11 17:13:00 +02001559 return( psa_mac_finish_internal( operation, mac,
1560 mac_size, mac_length ) );
mohammad16036df908f2018-04-02 08:34:15 -07001561}
1562
Gilles Peskine828ed142018-06-18 23:25:51 +02001563#define PSA_MAC_MAX_SIZE \
Gilles Peskine2d277862018-06-18 15:41:12 +02001564 ( MBEDTLS_MD_MAX_SIZE > MBEDTLS_MAX_BLOCK_LENGTH ? \
1565 MBEDTLS_MD_MAX_SIZE : \
Gilles Peskine8c9def32018-02-08 10:02:12 +01001566 MBEDTLS_MAX_BLOCK_LENGTH )
1567psa_status_t psa_mac_verify( psa_mac_operation_t *operation,
1568 const uint8_t *mac,
1569 size_t mac_length )
1570{
Gilles Peskine828ed142018-06-18 23:25:51 +02001571 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
Gilles Peskine8c9def32018-02-08 10:02:12 +01001572 size_t actual_mac_length;
mohammad16036df908f2018-04-02 08:34:15 -07001573 psa_status_t status;
1574
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001575 if( ! operation->key_usage_verify )
mohammad16036df908f2018-04-02 08:34:15 -07001576 return( PSA_ERROR_NOT_PERMITTED );
1577
1578 status = psa_mac_finish_internal( operation,
1579 actual_mac, sizeof( actual_mac ),
1580 &actual_mac_length );
Gilles Peskine8c9def32018-02-08 10:02:12 +01001581 if( status != PSA_SUCCESS )
1582 return( status );
1583 if( actual_mac_length != mac_length )
1584 return( PSA_ERROR_INVALID_SIGNATURE );
1585 if( safer_memcmp( mac, actual_mac, actual_mac_length ) != 0 )
1586 return( PSA_ERROR_INVALID_SIGNATURE );
1587 return( PSA_SUCCESS );
1588}
1589
1590
Gilles Peskine20035e32018-02-03 22:44:14 +01001591
Gilles Peskine20035e32018-02-03 22:44:14 +01001592/****************************************************************/
1593/* Asymmetric cryptography */
1594/****************************************************************/
1595
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001596/* Decode the hash algorithm from alg and store the mbedtls encoding in
1597 * md_alg. Verify that the hash length is consistent. */
1598static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
1599 size_t hash_length,
1600 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001601{
Gilles Peskine61b91d42018-06-08 16:09:36 +02001602 psa_algorithm_t hash_alg = PSA_ALG_RSA_GET_HASH( alg );
1603 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
1604 *md_alg = hash_alg == 0 ? MBEDTLS_MD_NONE : mbedtls_md_get_type( md_info );
1605 if( *md_alg == MBEDTLS_MD_NONE )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001606 {
1607#if SIZE_MAX > UINT_MAX
Gilles Peskine61b91d42018-06-08 16:09:36 +02001608 if( hash_length > UINT_MAX )
1609 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001610#endif
1611 }
1612 else
1613 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001614 if( mbedtls_md_get_size( md_info ) != hash_length )
1615 return( PSA_ERROR_INVALID_ARGUMENT );
1616 if( md_info == NULL )
1617 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001618 }
Gilles Peskine61b91d42018-06-08 16:09:36 +02001619 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001620}
1621
Gilles Peskine61b91d42018-06-08 16:09:36 +02001622psa_status_t psa_asymmetric_sign( psa_key_slot_t key,
1623 psa_algorithm_t alg,
1624 const uint8_t *hash,
1625 size_t hash_length,
1626 const uint8_t *salt,
1627 size_t salt_length,
1628 uint8_t *signature,
1629 size_t signature_size,
1630 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01001631{
1632 key_slot_t *slot;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001633 psa_status_t status;
Gilles Peskine93aa0332018-02-03 23:58:03 +01001634 *signature_length = 0;
1635 (void) salt;
1636 (void) salt_length;
1637
Gilles Peskine828ed142018-06-18 23:25:51 +02001638 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Gilles Peskine20035e32018-02-03 22:44:14 +01001639 return( PSA_ERROR_EMPTY_SLOT );
1640 slot = &global_data.key_slots[key];
1641 if( slot->type == PSA_KEY_TYPE_NONE )
1642 return( PSA_ERROR_EMPTY_SLOT );
1643 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1644 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001645 if( ! ( slot->policy.usage & PSA_KEY_USAGE_SIGN ) )
mohammad160306e79202018-03-28 13:17:44 +03001646 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine20035e32018-02-03 22:44:14 +01001647
Gilles Peskine20035e32018-02-03 22:44:14 +01001648#if defined(MBEDTLS_RSA_C)
1649 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1650 {
1651 mbedtls_rsa_context *rsa = slot->data.rsa;
1652 int ret;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001653 mbedtls_md_type_t md_alg;
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001654 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001655 if( status != PSA_SUCCESS )
1656 return( status );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001657
Gilles Peskine20035e32018-02-03 22:44:14 +01001658 if( signature_size < rsa->len )
1659 return( PSA_ERROR_BUFFER_TOO_SMALL );
1660#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskinea5926232018-03-28 14:16:50 +02001661 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01001662 {
1663 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1664 MBEDTLS_MD_NONE );
1665 ret = mbedtls_rsa_pkcs1_sign( rsa,
1666 mbedtls_ctr_drbg_random,
1667 &global_data.ctr_drbg,
1668 MBEDTLS_RSA_PRIVATE,
1669 md_alg, hash_length, hash,
1670 signature );
1671 }
1672 else
1673#endif /* MBEDTLS_PKCS1_V15 */
1674#if defined(MBEDTLS_PKCS1_V21)
1675 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1676 {
1677 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1678 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
1679 mbedtls_ctr_drbg_random,
1680 &global_data.ctr_drbg,
1681 MBEDTLS_RSA_PRIVATE,
1682 md_alg, hash_length, hash,
1683 signature );
1684 }
1685 else
1686#endif /* MBEDTLS_PKCS1_V21 */
1687 {
1688 return( PSA_ERROR_INVALID_ARGUMENT );
1689 }
Gilles Peskine93aa0332018-02-03 23:58:03 +01001690 if( ret == 0 )
1691 *signature_length = rsa->len;
Gilles Peskine20035e32018-02-03 22:44:14 +01001692 return( mbedtls_to_psa_error( ret ) );
1693 }
1694 else
1695#endif /* defined(MBEDTLS_RSA_C) */
1696#if defined(MBEDTLS_ECP_C)
1697 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1698 {
itayzafrir5c753392018-05-08 11:18:38 +03001699 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1700 int ret;
1701 const mbedtls_md_info_t *md_info;
1702 mbedtls_md_type_t md_alg;
1703 if( signature_size < PSA_ECDSA_SIGNATURE_SIZE( ecdsa->grp.pbits ) )
1704 return( PSA_ERROR_BUFFER_TOO_SMALL );
1705 md_info = mbedtls_md_info_from_psa( alg );
1706 md_alg = mbedtls_md_get_type( md_info );
1707 ret = mbedtls_ecdsa_write_signature( ecdsa, md_alg, hash, hash_length,
Gilles Peskine61b91d42018-06-08 16:09:36 +02001708 signature, signature_length,
1709 mbedtls_ctr_drbg_random,
1710 &global_data.ctr_drbg );
itayzafrir5c753392018-05-08 11:18:38 +03001711 return( mbedtls_to_psa_error( ret ) );
1712 }
1713 else
1714#endif /* defined(MBEDTLS_ECP_C) */
1715 {
Gilles Peskine20035e32018-02-03 22:44:14 +01001716 return( PSA_ERROR_NOT_SUPPORTED );
1717 }
itayzafrir5c753392018-05-08 11:18:38 +03001718}
1719
1720psa_status_t psa_asymmetric_verify( psa_key_slot_t key,
1721 psa_algorithm_t alg,
1722 const uint8_t *hash,
1723 size_t hash_length,
1724 const uint8_t *salt,
1725 size_t salt_length,
1726 uint8_t *signature,
1727 size_t signature_size )
1728{
1729 key_slot_t *slot;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001730 psa_status_t status;
itayzafrir5c753392018-05-08 11:18:38 +03001731 (void) salt;
1732 (void) salt_length;
1733
Gilles Peskine828ed142018-06-18 23:25:51 +02001734 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
itayzafrir5c753392018-05-08 11:18:38 +03001735 return( PSA_ERROR_INVALID_ARGUMENT );
1736 slot = &global_data.key_slots[key];
1737 if( slot->type == PSA_KEY_TYPE_NONE )
1738 return( PSA_ERROR_EMPTY_SLOT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001739 if( ! ( slot->policy.usage & PSA_KEY_USAGE_VERIFY ) )
itayzafrir5c753392018-05-08 11:18:38 +03001740 return( PSA_ERROR_NOT_PERMITTED );
1741
Gilles Peskine61b91d42018-06-08 16:09:36 +02001742#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001743 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
1744 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001745 {
1746 mbedtls_rsa_context *rsa = slot->data.rsa;
1747 int ret;
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001748 mbedtls_md_type_t md_alg;
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02001749 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001750 if( status != PSA_SUCCESS )
1751 return( status );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03001752
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001753 if( signature_size < rsa->len )
1754 return( PSA_ERROR_BUFFER_TOO_SMALL );
1755#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001756 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001757 {
1758 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
1759 MBEDTLS_MD_NONE );
1760
1761 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine61b91d42018-06-08 16:09:36 +02001762 mbedtls_ctr_drbg_random,
1763 &global_data.ctr_drbg,
1764 MBEDTLS_RSA_PUBLIC,
1765 md_alg,
1766 hash_length,
1767 hash,
1768 signature );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001769
1770 }
1771 else
1772#endif /* MBEDTLS_PKCS1_V15 */
1773#if defined(MBEDTLS_PKCS1_V21)
1774 if( alg == PSA_ALG_RSA_PSS_MGF1 )
1775 {
Gilles Peskinebeb49482018-06-08 17:44:35 +02001776 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
1777 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
1778 mbedtls_ctr_drbg_random,
1779 &global_data.ctr_drbg,
1780 MBEDTLS_RSA_PUBLIC,
1781 md_alg, hash_length, hash,
1782 signature );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001783 }
1784 else
1785#endif /* MBEDTLS_PKCS1_V21 */
1786 {
1787 return( PSA_ERROR_INVALID_ARGUMENT );
1788 }
1789 return( mbedtls_to_psa_error( ret ) );
1790 }
1791 else
1792#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03001793#if defined(MBEDTLS_ECP_C)
1794 if( PSA_KEY_TYPE_IS_ECC( slot->type ) )
1795 {
1796 mbedtls_ecp_keypair *ecdsa = slot->data.ecp;
1797 int ret;
Gilles Peskine2d277862018-06-18 15:41:12 +02001798 (void) alg;
Gilles Peskine61b91d42018-06-08 16:09:36 +02001799 ret = mbedtls_ecdsa_read_signature( ecdsa, hash, hash_length,
1800 signature, signature_size );
itayzafrir5c753392018-05-08 11:18:38 +03001801 return( mbedtls_to_psa_error( ret ) );
1802 }
Gilles Peskine20035e32018-02-03 22:44:14 +01001803 else
1804#endif /* defined(MBEDTLS_ECP_C) */
1805 {
1806 return( PSA_ERROR_NOT_SUPPORTED );
1807 }
1808}
1809
Gilles Peskine61b91d42018-06-08 16:09:36 +02001810psa_status_t psa_asymmetric_encrypt( psa_key_slot_t key,
1811 psa_algorithm_t alg,
1812 const uint8_t *input,
1813 size_t input_length,
1814 const uint8_t *salt,
1815 size_t salt_length,
1816 uint8_t *output,
1817 size_t output_size,
1818 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001819{
1820 key_slot_t *slot;
1821 (void) salt;
1822 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001823 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001824
Gilles Peskine828ed142018-06-18 23:25:51 +02001825 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein7f5a3192018-05-06 22:26:54 +03001826 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001827 slot = &global_data.key_slots[key];
1828 if( slot->type == PSA_KEY_TYPE_NONE )
1829 return( PSA_ERROR_EMPTY_SLOT );
1830 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1831 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001832 if( ! ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) )
1833 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001834
1835#if defined(MBEDTLS_RSA_C)
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001836 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR ||
1837 slot->type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001838 {
1839 mbedtls_rsa_context *rsa = slot->data.rsa;
1840 int ret;
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001841 if( output_size < rsa->len )
Gilles Peskine61b91d42018-06-08 16:09:36 +02001842 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001843#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001844 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001845 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001846 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
1847 mbedtls_ctr_drbg_random,
1848 &global_data.ctr_drbg,
1849 MBEDTLS_RSA_PUBLIC,
1850 input_length,
1851 input,
1852 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001853 }
1854 else
1855#endif /* MBEDTLS_PKCS1_V15 */
1856#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine625b01c2018-06-08 17:43:16 +02001857 if( PSA_ALG_IS_RSA_OAEP_MGF1( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001858 {
1859 return( PSA_ERROR_NOT_SUPPORTED );
1860 }
1861 else
1862#endif /* MBEDTLS_PKCS1_V21 */
1863 {
1864 return( PSA_ERROR_INVALID_ARGUMENT );
1865 }
1866 if( ret == 0 )
Nir Sonnenschein717a0402018-06-04 16:36:15 +03001867 *output_length = rsa->len;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001868 return( mbedtls_to_psa_error( ret ) );
1869 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001870 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02001871#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001872 {
1873 return( PSA_ERROR_NOT_SUPPORTED );
1874 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001875}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001876
Gilles Peskine61b91d42018-06-08 16:09:36 +02001877psa_status_t psa_asymmetric_decrypt( psa_key_slot_t key,
1878 psa_algorithm_t alg,
1879 const uint8_t *input,
1880 size_t input_length,
1881 const uint8_t *salt,
1882 size_t salt_length,
1883 uint8_t *output,
1884 size_t output_size,
1885 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001886{
1887 key_slot_t *slot;
1888 (void) salt;
1889 (void) salt_length;
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03001890 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001891
Gilles Peskine828ed142018-06-18 23:25:51 +02001892 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001893 return( PSA_ERROR_EMPTY_SLOT );
1894 slot = &global_data.key_slots[key];
1895 if( slot->type == PSA_KEY_TYPE_NONE )
1896 return( PSA_ERROR_EMPTY_SLOT );
1897 if( ! PSA_KEY_TYPE_IS_KEYPAIR( slot->type ) )
1898 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine61b91d42018-06-08 16:09:36 +02001899 if( ! ( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
1900 return( PSA_ERROR_NOT_PERMITTED );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001901
1902#if defined(MBEDTLS_RSA_C)
1903 if( slot->type == PSA_KEY_TYPE_RSA_KEYPAIR )
1904 {
1905 mbedtls_rsa_context *rsa = slot->data.rsa;
1906 int ret;
1907
Gilles Peskinec4def2f2018-06-08 17:53:48 +02001908 if( input_length != rsa->len )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001909 return( PSA_ERROR_INVALID_ARGUMENT );
1910
1911#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02001912 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001913 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02001914 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
1915 mbedtls_ctr_drbg_random,
1916 &global_data.ctr_drbg,
1917 MBEDTLS_RSA_PRIVATE,
1918 output_length,
1919 input,
1920 output,
1921 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001922 }
1923 else
1924#endif /* MBEDTLS_PKCS1_V15 */
1925#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine625b01c2018-06-08 17:43:16 +02001926 if( PSA_ALG_IS_RSA_OAEP_MGF1( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001927 {
1928 return( PSA_ERROR_NOT_SUPPORTED );
1929 }
1930 else
1931#endif /* MBEDTLS_PKCS1_V21 */
1932 {
1933 return( PSA_ERROR_INVALID_ARGUMENT );
1934 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03001935
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001936 return( mbedtls_to_psa_error( ret ) );
1937 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001938 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02001939#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03001940 {
1941 return( PSA_ERROR_NOT_SUPPORTED );
1942 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02001943}
Gilles Peskine20035e32018-02-03 22:44:14 +01001944
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02001945
1946
mohammad1603503973b2018-03-12 15:59:30 +02001947/****************************************************************/
1948/* Symmetric cryptography */
1949/****************************************************************/
1950
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001951/* Initialize the cipher operation structure. Once this function has been
1952 * called, psa_cipher_abort can run and will do the right thing. */
1953static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
1954 psa_algorithm_t alg )
1955{
Gilles Peskinec06e0712018-06-20 16:21:04 +02001956 if( ! PSA_ALG_IS_CIPHER( alg ) )
1957 {
1958 memset( operation, 0, sizeof( *operation ) );
1959 return( PSA_ERROR_INVALID_ARGUMENT );
1960 }
1961
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001962 operation->alg = alg;
1963 operation->key_set = 0;
1964 operation->iv_set = 0;
1965 operation->iv_required = 1;
1966 operation->iv_size = 0;
1967 operation->block_size = 0;
1968 mbedtls_cipher_init( &operation->ctx.cipher );
1969 return( PSA_SUCCESS );
1970}
1971
Gilles Peskinee553c652018-06-04 16:22:46 +02001972static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
1973 psa_key_slot_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02001974 psa_algorithm_t alg,
1975 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02001976{
1977 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1978 psa_status_t status;
1979 key_slot_t *slot;
1980 psa_key_type_t key_type;
1981 size_t key_bits;
1982 const mbedtls_cipher_info_t *cipher_info = NULL;
1983
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02001984 status = psa_cipher_init( operation, alg );
1985 if( status != PSA_SUCCESS )
1986 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02001987
1988 status = psa_get_key_information( key, &key_type, &key_bits );
1989 if( status != PSA_SUCCESS )
1990 return( status );
1991 slot = &global_data.key_slots[key];
1992
Gilles Peskinebb1072f2018-06-08 18:46:05 +02001993 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02001994 if( cipher_info == NULL )
1995 return( PSA_ERROR_NOT_SUPPORTED );
1996
mohammad1603503973b2018-03-12 15:59:30 +02001997 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03001998 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02001999 {
2000 psa_cipher_abort( operation );
2001 return( mbedtls_to_psa_error( ret ) );
2002 }
2003
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002004#if defined(MBEDTLS_DES_C)
2005 if( key_type == PSA_KEY_TYPE_DES && key_bits == 128 )
2006 {
2007 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
2008 unsigned char keys[24];
2009 memcpy( keys, slot->data.raw.data, 16 );
2010 memcpy( keys + 16, slot->data.raw.data, 8 );
2011 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2012 keys,
2013 192, cipher_operation );
2014 }
2015 else
2016#endif
2017 {
2018 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
2019 slot->data.raw.data,
2020 key_bits, cipher_operation );
2021 }
Moran Peker41deec42018-04-04 15:43:05 +03002022 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002023 {
2024 psa_cipher_abort( operation );
2025 return( mbedtls_to_psa_error( ret ) );
2026 }
2027
mohammad16038481e742018-03-18 13:57:31 +02002028#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Moran Peker7cb22b82018-06-05 11:40:02 +03002029 if( ( alg & ~PSA_ALG_BLOCK_CIPHER_PADDING_MASK ) == PSA_ALG_CBC_BASE )
mohammad16038481e742018-03-18 13:57:31 +02002030 {
Gilles Peskine53514202018-06-06 15:11:46 +02002031 psa_algorithm_t padding_mode = alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
2032 mbedtls_cipher_padding_t mode;
mohammad16038481e742018-03-18 13:57:31 +02002033
Moran Pekera28258c2018-05-29 16:25:04 +03002034 switch ( padding_mode )
mohammad16038481e742018-03-18 13:57:31 +02002035 {
2036 case PSA_ALG_BLOCK_CIPHER_PAD_PKCS7:
2037 mode = MBEDTLS_PADDING_PKCS7;
2038 break;
2039 case PSA_ALG_BLOCK_CIPHER_PAD_NONE:
2040 mode = MBEDTLS_PADDING_NONE;
2041 break;
2042 default:
Moran Pekerae382792018-05-31 14:06:17 +03002043 psa_cipher_abort( operation );
2044 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038481e742018-03-18 13:57:31 +02002045 }
2046 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher, mode );
Moran Pekera28258c2018-05-29 16:25:04 +03002047 if( ret != 0 )
Moran Peker71f19ae2018-04-22 20:23:16 +03002048 {
2049 psa_cipher_abort( operation );
mohammad16038481e742018-03-18 13:57:31 +02002050 return( mbedtls_to_psa_error( ret ) );
Moran Peker71f19ae2018-04-22 20:23:16 +03002051 }
mohammad16038481e742018-03-18 13:57:31 +02002052 }
2053#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
2054
mohammad1603503973b2018-03-12 15:59:30 +02002055 operation->key_set = 1;
Gilles Peskine7e928852018-06-04 16:23:10 +02002056 operation->block_size = ( PSA_ALG_IS_BLOCK_CIPHER( alg ) ?
2057 PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) :
2058 1 );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002059 if( PSA_ALG_IS_BLOCK_CIPHER( alg ) || alg == PSA_ALG_CTR )
mohammad16038481e742018-03-18 13:57:31 +02002060 {
mohammad160389e0f462018-04-12 08:48:45 +03002061 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type );
mohammad16038481e742018-03-18 13:57:31 +02002062 }
mohammad1603503973b2018-03-12 15:59:30 +02002063
Moran Peker395db872018-05-31 14:07:14 +03002064 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002065}
2066
Gilles Peskinee553c652018-06-04 16:22:46 +02002067psa_status_t psa_encrypt_setup( psa_cipher_operation_t *operation,
2068 psa_key_slot_t key,
2069 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002070{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002071 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002072}
2073
Gilles Peskinee553c652018-06-04 16:22:46 +02002074psa_status_t psa_decrypt_setup( psa_cipher_operation_t *operation,
2075 psa_key_slot_t key,
2076 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02002077{
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002078 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02002079}
2080
Gilles Peskinee553c652018-06-04 16:22:46 +02002081psa_status_t psa_encrypt_generate_iv( psa_cipher_operation_t *operation,
2082 unsigned char *iv,
2083 size_t iv_size,
2084 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002085{
Moran Peker41deec42018-04-04 15:43:05 +03002086 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002087 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002088 return( PSA_ERROR_BAD_STATE );
2089 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02002090 {
Moran Peker41deec42018-04-04 15:43:05 +03002091 ret = PSA_ERROR_BUFFER_TOO_SMALL;
2092 goto exit;
2093 }
Gilles Peskine7e928852018-06-04 16:23:10 +02002094 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2095 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03002096 if( ret != 0 )
2097 {
2098 ret = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02002099 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02002100 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002101
mohammad16038481e742018-03-18 13:57:31 +02002102 *iv_length = operation->iv_size;
mohammad160389e0f462018-04-12 08:48:45 +03002103 ret = psa_encrypt_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002104
Moran Peker395db872018-05-31 14:07:14 +03002105exit:
2106 if( ret != PSA_SUCCESS )
2107 psa_cipher_abort( operation );
2108 return( ret );
mohammad1603503973b2018-03-12 15:59:30 +02002109}
2110
Gilles Peskinee553c652018-06-04 16:22:46 +02002111psa_status_t psa_encrypt_set_iv( psa_cipher_operation_t *operation,
2112 const unsigned char *iv,
2113 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02002114{
Moran Peker41deec42018-04-04 15:43:05 +03002115 int ret = PSA_SUCCESS;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002116 if( operation->iv_set || ! operation->iv_required )
Moran Peker41deec42018-04-04 15:43:05 +03002117 return( PSA_ERROR_BAD_STATE );
Moran Pekera28258c2018-05-29 16:25:04 +03002118 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03002119 {
Moran Pekerae382792018-05-31 14:06:17 +03002120 psa_cipher_abort( operation );
2121 return( PSA_ERROR_INVALID_ARGUMENT );
Moran Peker71f19ae2018-04-22 20:23:16 +03002122 }
mohammad1603503973b2018-03-12 15:59:30 +02002123 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03002124 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002125 {
2126 psa_cipher_abort( operation );
2127 return( mbedtls_to_psa_error( ret ) );
2128 }
2129
2130 operation->iv_set = 1;
mohammad1603503973b2018-03-12 15:59:30 +02002131
Moran Peker395db872018-05-31 14:07:14 +03002132 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002133}
2134
Gilles Peskinee553c652018-06-04 16:22:46 +02002135psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
2136 const uint8_t *input,
2137 size_t input_length,
2138 unsigned char *output,
2139 size_t output_size,
2140 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002141{
2142 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskine89d789c2018-06-04 17:17:16 +02002143 size_t expected_output_size;
2144 if( PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
2145 {
2146 /* Take the unprocessed partial block left over from previous
2147 * update calls, if any, plus the input to this call. Remove
2148 * the last partial block, if any. You get the data that will be
2149 * output in this call. */
2150 expected_output_size =
2151 ( operation->ctx.cipher.unprocessed_len + input_length )
2152 / operation->block_size * operation->block_size;
2153 }
2154 else
2155 {
2156 expected_output_size = input_length;
2157 }
2158 if( output_size < expected_output_size )
Moran Peker395db872018-05-31 14:07:14 +03002159 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160382759612018-03-12 18:16:40 +02002160
mohammad1603503973b2018-03-12 15:59:30 +02002161 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02002162 input_length, output, output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002163 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002164 {
2165 psa_cipher_abort( operation );
2166 return( mbedtls_to_psa_error( ret ) );
2167 }
2168
Moran Peker395db872018-05-31 14:07:14 +03002169 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002170}
2171
Gilles Peskinee553c652018-06-04 16:22:46 +02002172psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
2173 uint8_t *output,
2174 size_t output_size,
2175 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02002176{
2177 int ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02002178 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03002179
mohammad1603503973b2018-03-12 15:59:30 +02002180 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002181 {
Moran Peker7cb22b82018-06-05 11:40:02 +03002182 psa_cipher_abort( operation );
2183 return( PSA_ERROR_BAD_STATE );
2184 }
2185 if( operation->iv_required && ! operation->iv_set )
2186 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002187 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002188 return( PSA_ERROR_BAD_STATE );
2189 }
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002190 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
2191 PSA_ALG_IS_BLOCK_CIPHER( operation->alg ) )
Moran Peker7cb22b82018-06-05 11:40:02 +03002192 {
Gilles Peskine53514202018-06-06 15:11:46 +02002193 psa_algorithm_t padding_mode =
2194 operation->alg & PSA_ALG_BLOCK_CIPHER_PADDING_MASK;
Moran Peker7cb22b82018-06-05 11:40:02 +03002195 if( operation->ctx.cipher.unprocessed_len >= operation->block_size )
Gilles Peskine89d789c2018-06-04 17:17:16 +02002196 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002197 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002198 return( PSA_ERROR_TAMPERING_DETECTED );
2199 }
Gilles Peskine53514202018-06-06 15:11:46 +02002200 if( padding_mode == PSA_ALG_BLOCK_CIPHER_PAD_NONE )
Moran Peker7cb22b82018-06-05 11:40:02 +03002201 {
2202 if( operation->ctx.cipher.unprocessed_len != 0 )
2203 {
Gilles Peskine2c5219a2018-06-06 15:12:32 +02002204 psa_cipher_abort( operation );
Moran Peker7cb22b82018-06-05 11:40:02 +03002205 return( PSA_ERROR_INVALID_ARGUMENT );
2206 }
Gilles Peskine89d789c2018-06-04 17:17:16 +02002207 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002208 }
2209
2210 ret = mbedtls_cipher_finish( &operation->ctx.cipher, temp_output_buffer,
Gilles Peskinee553c652018-06-04 16:22:46 +02002211 output_length );
Moran Peker41deec42018-04-04 15:43:05 +03002212 if( ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02002213 {
2214 psa_cipher_abort( operation );
2215 return( mbedtls_to_psa_error( ret ) );
2216 }
Gilles Peskinee553c652018-06-04 16:22:46 +02002217 if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03002218 memcpy( output, temp_output_buffer, *output_length );
2219 else
2220 {
2221 psa_cipher_abort( operation );
2222 return( PSA_ERROR_BUFFER_TOO_SMALL );
2223 }
mohammad1603503973b2018-03-12 15:59:30 +02002224
Moran Peker4c80d832018-04-22 20:15:31 +03002225 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002226}
2227
Gilles Peskinee553c652018-06-04 16:22:46 +02002228psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
2229{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002230 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02002231 {
2232 /* The object has (apparently) been initialized but it is not
2233 * in use. It's ok to call abort on such an object, and there's
2234 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002235 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02002236 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002237
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002238 /* Sanity check (shouldn't happen: operation->alg should
2239 * always have been initialized to a valid value). */
2240 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
2241 return( PSA_ERROR_BAD_STATE );
2242
mohammad1603503973b2018-03-12 15:59:30 +02002243 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02002244
Moran Peker41deec42018-04-04 15:43:05 +03002245 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002246 operation->key_set = 0;
2247 operation->iv_set = 0;
2248 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002249 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03002250 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002251
Moran Peker395db872018-05-31 14:07:14 +03002252 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02002253}
2254
Gilles Peskinea0655c32018-04-30 17:06:50 +02002255
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002256
mohammad16038cc1cee2018-03-28 01:21:33 +03002257/****************************************************************/
2258/* Key Policy */
2259/****************************************************************/
2260
Gilles Peskine2d277862018-06-18 15:41:12 +02002261void psa_key_policy_init( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002262{
Gilles Peskine803ce742018-06-18 16:07:14 +02002263 memset( policy, 0, sizeof( *policy ) );
mohammad16038cc1cee2018-03-28 01:21:33 +03002264}
2265
Gilles Peskine2d277862018-06-18 15:41:12 +02002266void psa_key_policy_set_usage( psa_key_policy_t *policy,
2267 psa_key_usage_t usage,
2268 psa_algorithm_t alg )
mohammad16038cc1cee2018-03-28 01:21:33 +03002269{
mohammad16034eed7572018-03-28 05:14:59 -07002270 policy->usage = usage;
2271 policy->alg = alg;
mohammad16038cc1cee2018-03-28 01:21:33 +03002272}
2273
Gilles Peskine2d277862018-06-18 15:41:12 +02002274psa_key_usage_t psa_key_policy_get_usage( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002275{
mohammad16036df908f2018-04-02 08:34:15 -07002276 return( policy->usage );
mohammad16038cc1cee2018-03-28 01:21:33 +03002277}
2278
Gilles Peskine2d277862018-06-18 15:41:12 +02002279psa_algorithm_t psa_key_policy_get_algorithm( psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002280{
mohammad16036df908f2018-04-02 08:34:15 -07002281 return( policy->alg );
mohammad16038cc1cee2018-03-28 01:21:33 +03002282}
2283
Gilles Peskine2d277862018-06-18 15:41:12 +02002284psa_status_t psa_set_key_policy( psa_key_slot_t key,
2285 const psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002286{
2287 key_slot_t *slot;
mohammad16038cc1cee2018-03-28 01:21:33 +03002288
Gilles Peskine828ed142018-06-18 23:25:51 +02002289 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002290 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002291
mohammad16038cc1cee2018-03-28 01:21:33 +03002292 slot = &global_data.key_slots[key];
2293 if( slot->type != PSA_KEY_TYPE_NONE )
2294 return( PSA_ERROR_OCCUPIED_SLOT );
2295
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002296 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
2297 PSA_KEY_USAGE_ENCRYPT |
2298 PSA_KEY_USAGE_DECRYPT |
2299 PSA_KEY_USAGE_SIGN |
2300 PSA_KEY_USAGE_VERIFY ) ) != 0 )
mohammad16035feda722018-04-16 04:38:57 -07002301 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16038cc1cee2018-03-28 01:21:33 +03002302
mohammad16036df908f2018-04-02 08:34:15 -07002303 slot->policy = *policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002304
2305 return( PSA_SUCCESS );
2306}
2307
Gilles Peskine2d277862018-06-18 15:41:12 +02002308psa_status_t psa_get_key_policy( psa_key_slot_t key,
2309 psa_key_policy_t *policy )
mohammad16038cc1cee2018-03-28 01:21:33 +03002310{
2311 key_slot_t *slot;
2312
Gilles Peskine828ed142018-06-18 23:25:51 +02002313 if( key == 0 || key > PSA_KEY_SLOT_COUNT || policy == NULL )
mohammad16038cc1cee2018-03-28 01:21:33 +03002314 return( PSA_ERROR_INVALID_ARGUMENT );
2315
2316 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002317
mohammad16036df908f2018-04-02 08:34:15 -07002318 *policy = slot->policy;
mohammad16038cc1cee2018-03-28 01:21:33 +03002319
2320 return( PSA_SUCCESS );
2321}
Gilles Peskine20035e32018-02-03 22:44:14 +01002322
Gilles Peskinea0655c32018-04-30 17:06:50 +02002323
2324
mohammad1603804cd712018-03-20 22:44:08 +02002325/****************************************************************/
2326/* Key Lifetime */
2327/****************************************************************/
2328
Gilles Peskine2d277862018-06-18 15:41:12 +02002329psa_status_t psa_get_key_lifetime( psa_key_slot_t key,
2330 psa_key_lifetime_t *lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002331{
2332 key_slot_t *slot;
2333
Gilles Peskine828ed142018-06-18 23:25:51 +02002334 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002335 return( PSA_ERROR_INVALID_ARGUMENT );
2336
2337 slot = &global_data.key_slots[key];
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002338
mohammad1603804cd712018-03-20 22:44:08 +02002339 *lifetime = slot->lifetime;
2340
2341 return( PSA_SUCCESS );
2342}
2343
Gilles Peskine2d277862018-06-18 15:41:12 +02002344psa_status_t psa_set_key_lifetime( psa_key_slot_t key,
2345 const psa_key_lifetime_t lifetime )
mohammad1603804cd712018-03-20 22:44:08 +02002346{
2347 key_slot_t *slot;
2348
Gilles Peskine828ed142018-06-18 23:25:51 +02002349 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
mohammad1603804cd712018-03-20 22:44:08 +02002350 return( PSA_ERROR_INVALID_ARGUMENT );
2351
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002352 if( lifetime != PSA_KEY_LIFETIME_VOLATILE &&
2353 lifetime != PSA_KEY_LIFETIME_PERSISTENT &&
mohammad1603ba178512018-03-21 04:35:20 -07002354 lifetime != PSA_KEY_LIFETIME_WRITE_ONCE)
2355 return( PSA_ERROR_INVALID_ARGUMENT );
2356
mohammad1603804cd712018-03-20 22:44:08 +02002357 slot = &global_data.key_slots[key];
mohammad16035d7ec202018-03-28 01:29:41 +03002358 if( slot->type != PSA_KEY_TYPE_NONE )
2359 return( PSA_ERROR_OCCUPIED_SLOT );
mohammad1603804cd712018-03-20 22:44:08 +02002360
Moran Pekerd7326592018-05-29 16:56:39 +03002361 if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
mohammad1603ba178512018-03-21 04:35:20 -07002362 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02002363
mohammad1603060ad8a2018-03-20 14:28:38 -07002364 slot->lifetime = lifetime;
mohammad1603804cd712018-03-20 22:44:08 +02002365
2366 return( PSA_SUCCESS );
2367}
2368
Gilles Peskine20035e32018-02-03 22:44:14 +01002369
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002370
mohammad16035955c982018-04-26 00:53:03 +03002371/****************************************************************/
2372/* AEAD */
2373/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002374
mohammad16035955c982018-04-26 00:53:03 +03002375psa_status_t psa_aead_encrypt( psa_key_slot_t key,
2376 psa_algorithm_t alg,
2377 const uint8_t *nonce,
2378 size_t nonce_length,
2379 const uint8_t *additional_data,
2380 size_t additional_data_length,
2381 const uint8_t *plaintext,
2382 size_t plaintext_length,
2383 uint8_t *ciphertext,
2384 size_t ciphertext_size,
2385 size_t *ciphertext_length )
2386{
2387 int ret;
2388 psa_status_t status;
2389 key_slot_t *slot;
2390 psa_key_type_t key_type;
2391 size_t key_bits;
mohammad160315223a82018-06-03 17:19:55 +03002392 uint8_t *tag;
2393 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002394 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002395 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002396
mohammad1603f08a5502018-06-03 15:05:47 +03002397 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07002398
mohammad16035955c982018-04-26 00:53:03 +03002399 status = psa_get_key_information( key, &key_type, &key_bits );
2400 if( status != PSA_SUCCESS )
2401 return( status );
2402 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002403 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002404 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002405
mohammad16035ed06212018-06-06 13:09:34 +03002406 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2407 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002408 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002409 return( PSA_ERROR_NOT_SUPPORTED );
mohammad1603dad36fa2018-05-09 02:24:42 -07002410
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002411 if( ( slot->policy.usage & PSA_KEY_USAGE_ENCRYPT ) == 0 )
mohammad1603f14394b2018-06-04 14:33:19 +03002412 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002413
Gilles Peskine2d277862018-06-18 15:41:12 +02002414 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2415 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603db624732018-04-30 17:21:50 +03002416 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002417
mohammad16035955c982018-04-26 00:53:03 +03002418 if( alg == PSA_ALG_GCM )
2419 {
2420 mbedtls_gcm_context gcm;
mohammad160315223a82018-06-03 17:19:55 +03002421 tag_length = 16;
2422
mohammad160396910d82018-06-04 14:33:00 +03002423 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2424 return( PSA_ERROR_INVALID_ARGUMENT );
2425
mohammad160315223a82018-06-03 17:19:55 +03002426 //make sure we have place to hold the tag in the ciphertext buffer
2427 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002428 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002429
2430 //update the tag pointer to point to the end of the ciphertext_length
2431 tag = ciphertext + plaintext_length;
2432
mohammad16035955c982018-04-26 00:53:03 +03002433 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002434 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad160395893f82018-06-03 15:06:17 +03002435 slot->data.raw.data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002436 key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002437 if( ret != 0 )
2438 {
2439 mbedtls_gcm_free( &gcm );
2440 return( mbedtls_to_psa_error( ret ) );
2441 }
2442 ret = mbedtls_gcm_crypt_and_tag( &gcm, MBEDTLS_GCM_ENCRYPT,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002443 plaintext_length, nonce,
2444 nonce_length, additional_data,
2445 additional_data_length, plaintext,
mohammad160315223a82018-06-03 17:19:55 +03002446 ciphertext, tag_length, tag );
mohammad16035955c982018-04-26 00:53:03 +03002447 mbedtls_gcm_free( &gcm );
2448 }
2449 else if( alg == PSA_ALG_CCM )
2450 {
2451 mbedtls_ccm_context ccm;
mohammad160315223a82018-06-03 17:19:55 +03002452 tag_length = 16;
2453
mohammad160396910d82018-06-04 14:33:00 +03002454 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( key_type ) != 16 )
2455 return( PSA_ERROR_INVALID_ARGUMENT );
2456
mohammad160347ddf3d2018-04-26 01:11:21 +03002457 if( nonce_length < 7 || nonce_length > 13 )
2458 return( PSA_ERROR_INVALID_ARGUMENT );
2459
mohammad160315223a82018-06-03 17:19:55 +03002460 //make sure we have place to hold the tag in the ciphertext buffer
2461 if( ciphertext_size < ( plaintext_length + tag_length ) )
mohammad1603e3cb8a82018-06-06 13:45:03 +03002462 return( PSA_ERROR_BUFFER_TOO_SMALL );
mohammad160315223a82018-06-03 17:19:55 +03002463
2464 //update the tag pointer to point to the end of the ciphertext_length
2465 tag = ciphertext + plaintext_length;
2466
mohammad16035955c982018-04-26 00:53:03 +03002467 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002468 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002469 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002470 if( ret != 0 )
2471 {
2472 mbedtls_ccm_free( &ccm );
2473 return( mbedtls_to_psa_error( ret ) );
2474 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002475 ret = mbedtls_ccm_encrypt_and_tag( &ccm, plaintext_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002476 nonce, nonce_length,
2477 additional_data,
Gilles Peskinea40d7742018-06-01 16:28:30 +02002478 additional_data_length,
2479 plaintext, ciphertext,
mohammad160315223a82018-06-03 17:19:55 +03002480 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002481 mbedtls_ccm_free( &ccm );
2482 }
mohammad16035c8845f2018-05-09 05:40:09 -07002483 else
2484 {
mohammad1603554faad2018-06-03 15:07:38 +03002485 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07002486 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002487
mohammad160315223a82018-06-03 17:19:55 +03002488 if( ret != 0 )
2489 {
2490 memset( ciphertext, 0, ciphertext_size );
2491 return( mbedtls_to_psa_error( ret ) );
2492 }
Gilles Peskine2d277862018-06-18 15:41:12 +02002493
mohammad160315223a82018-06-03 17:19:55 +03002494 *ciphertext_length = plaintext_length + tag_length;
mohammad160347ddf3d2018-04-26 01:11:21 +03002495 return( PSA_SUCCESS );
mohammad16035955c982018-04-26 00:53:03 +03002496}
2497
Gilles Peskineee652a32018-06-01 19:23:52 +02002498/* Locate the tag in a ciphertext buffer containing the encrypted data
2499 * followed by the tag. Return the length of the part preceding the tag in
2500 * *plaintext_length. This is the size of the plaintext in modes where
2501 * the encrypted data has the same size as the plaintext, such as
2502 * CCM and GCM. */
2503static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
2504 const uint8_t *ciphertext,
2505 size_t ciphertext_length,
2506 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02002507 const uint8_t **p_tag )
2508{
2509 size_t payload_length;
2510 if( tag_length > ciphertext_length )
2511 return( PSA_ERROR_INVALID_ARGUMENT );
2512 payload_length = ciphertext_length - tag_length;
2513 if( payload_length > plaintext_size )
2514 return( PSA_ERROR_BUFFER_TOO_SMALL );
2515 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02002516 return( PSA_SUCCESS );
2517}
2518
mohammad16035955c982018-04-26 00:53:03 +03002519psa_status_t psa_aead_decrypt( psa_key_slot_t key,
2520 psa_algorithm_t alg,
2521 const uint8_t *nonce,
2522 size_t nonce_length,
2523 const uint8_t *additional_data,
2524 size_t additional_data_length,
2525 const uint8_t *ciphertext,
2526 size_t ciphertext_length,
2527 uint8_t *plaintext,
2528 size_t plaintext_size,
2529 size_t *plaintext_length )
2530{
2531 int ret;
2532 psa_status_t status;
2533 key_slot_t *slot;
2534 psa_key_type_t key_type;
2535 size_t key_bits;
Gilles Peskineee652a32018-06-01 19:23:52 +02002536 const uint8_t *tag;
2537 size_t tag_length;
mohammad1603dad36fa2018-05-09 02:24:42 -07002538 mbedtls_cipher_id_t cipher_id;
mohammad16030f214652018-06-03 15:10:06 +03002539 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02002540
Gilles Peskineee652a32018-06-01 19:23:52 +02002541 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03002542
mohammad16035955c982018-04-26 00:53:03 +03002543 status = psa_get_key_information( key, &key_type, &key_bits );
2544 if( status != PSA_SUCCESS )
2545 return( status );
2546 slot = &global_data.key_slots[key];
mohammad1603a1d98012018-06-06 13:45:55 +03002547 if( slot->type == PSA_KEY_TYPE_NONE )
Gilles Peskine2d277862018-06-18 15:41:12 +02002548 return( PSA_ERROR_EMPTY_SLOT );
mohammad16035955c982018-04-26 00:53:03 +03002549
mohammad16035ed06212018-06-06 13:09:34 +03002550 cipher_info = mbedtls_cipher_info_from_psa( alg, key_type,
2551 key_bits, &cipher_id );
mohammad16030f214652018-06-03 15:10:06 +03002552 if( cipher_info == NULL )
Gilles Peskine2d277862018-06-18 15:41:12 +02002553 return( PSA_ERROR_NOT_SUPPORTED );
2554
mohammad1603f14394b2018-06-04 14:33:19 +03002555 if( !( slot->policy.usage & PSA_KEY_USAGE_DECRYPT ) )
2556 return( PSA_ERROR_NOT_PERMITTED );
mohammad16035955c982018-04-26 00:53:03 +03002557
Gilles Peskine2d277862018-06-18 15:41:12 +02002558 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) !=
2559 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
mohammad1603a7e6df72018-04-30 17:25:45 +03002560 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad16035955c982018-04-26 00:53:03 +03002561
mohammad16035955c982018-04-26 00:53:03 +03002562 if( alg == PSA_ALG_GCM )
2563 {
2564 mbedtls_gcm_context gcm;
mohammad160347ddf3d2018-04-26 01:11:21 +03002565
Gilles Peskineee652a32018-06-01 19:23:52 +02002566 tag_length = 16;
2567 status = psa_aead_unpadded_locate_tag( tag_length,
2568 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002569 plaintext_size, &tag );
Gilles Peskineee652a32018-06-01 19:23:52 +02002570 if( status != PSA_SUCCESS )
2571 return( status );
2572
mohammad16035955c982018-04-26 00:53:03 +03002573 mbedtls_gcm_init( &gcm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002574 ret = mbedtls_gcm_setkey( &gcm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002575 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002576 if( ret != 0 )
2577 {
2578 mbedtls_gcm_free( &gcm );
2579 return( mbedtls_to_psa_error( ret ) );
2580 }
mohammad16035955c982018-04-26 00:53:03 +03002581
Gilles Peskineee652a32018-06-01 19:23:52 +02002582 ret = mbedtls_gcm_auth_decrypt( &gcm,
mohammad160360a64d02018-06-03 17:20:42 +03002583 ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002584 nonce, nonce_length,
mohammad16035ed06212018-06-06 13:09:34 +03002585 additional_data,
2586 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002587 tag, tag_length,
2588 ciphertext, plaintext );
mohammad16035955c982018-04-26 00:53:03 +03002589 mbedtls_gcm_free( &gcm );
2590 }
2591 else if( alg == PSA_ALG_CCM )
2592 {
2593 mbedtls_ccm_context ccm;
Gilles Peskinea40d7742018-06-01 16:28:30 +02002594
mohammad160347ddf3d2018-04-26 01:11:21 +03002595 if( nonce_length < 7 || nonce_length > 13 )
2596 return( PSA_ERROR_INVALID_ARGUMENT );
2597
mohammad16039375f842018-06-03 14:28:24 +03002598 tag_length = 16;
2599 status = psa_aead_unpadded_locate_tag( tag_length,
2600 ciphertext, ciphertext_length,
mohammad160360a64d02018-06-03 17:20:42 +03002601 plaintext_size, &tag );
mohammad16039375f842018-06-03 14:28:24 +03002602 if( status != PSA_SUCCESS )
2603 return( status );
2604
mohammad16035955c982018-04-26 00:53:03 +03002605 mbedtls_ccm_init( &ccm );
Gilles Peskinea40d7742018-06-01 16:28:30 +02002606 ret = mbedtls_ccm_setkey( &ccm, cipher_id,
mohammad16036bbd8c72018-04-30 17:22:52 +03002607 slot->data.raw.data, key_bits );
mohammad16035955c982018-04-26 00:53:03 +03002608 if( ret != 0 )
2609 {
2610 mbedtls_ccm_free( &ccm );
2611 return( mbedtls_to_psa_error( ret ) );
2612 }
mohammad160360a64d02018-06-03 17:20:42 +03002613 ret = mbedtls_ccm_auth_decrypt( &ccm, ciphertext_length - tag_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002614 nonce, nonce_length,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002615 additional_data,
2616 additional_data_length,
Gilles Peskineee652a32018-06-01 19:23:52 +02002617 ciphertext, plaintext,
2618 tag, tag_length );
mohammad16035955c982018-04-26 00:53:03 +03002619 mbedtls_ccm_free( &ccm );
2620 }
mohammad160339574652018-06-01 04:39:53 -07002621 else
2622 {
mohammad1603554faad2018-06-03 15:07:38 +03002623 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07002624 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02002625
Gilles Peskineee652a32018-06-01 19:23:52 +02002626 if( ret != 0 )
mohammad160360a64d02018-06-03 17:20:42 +03002627 memset( plaintext, 0, plaintext_size );
2628 else
2629 *plaintext_length = ciphertext_length - tag_length;
2630
Gilles Peskineee652a32018-06-01 19:23:52 +02002631 return( mbedtls_to_psa_error( ret ) );
mohammad16035955c982018-04-26 00:53:03 +03002632}
2633
Gilles Peskinea0655c32018-04-30 17:06:50 +02002634
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002635
Gilles Peskine20035e32018-02-03 22:44:14 +01002636/****************************************************************/
Gilles Peskine05d69892018-06-19 22:00:52 +02002637/* Key generation */
2638/****************************************************************/
2639
2640psa_status_t psa_generate_random( uint8_t *output,
2641 size_t output_size )
2642{
2643 int ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
2644 output, output_size );
2645 return( mbedtls_to_psa_error( ret ) );
2646}
2647
2648psa_status_t psa_generate_key( psa_key_slot_t key,
2649 psa_key_type_t type,
2650 size_t bits,
2651 const void *parameters,
2652 size_t parameters_size )
2653{
Gilles Peskine12313cd2018-06-20 00:20:32 +02002654 key_slot_t *slot;
2655
2656 if( key == 0 || key > PSA_KEY_SLOT_COUNT )
2657 return( PSA_ERROR_INVALID_ARGUMENT );
2658 slot = &global_data.key_slots[key];
2659 if( slot->type != PSA_KEY_TYPE_NONE )
2660 return( PSA_ERROR_OCCUPIED_SLOT );
2661 if( parameters == NULL && parameters_size != 0 )
2662 return( PSA_ERROR_INVALID_ARGUMENT );
2663
Gilles Peskine48c0ea12018-06-21 14:15:31 +02002664 if( key_type_is_raw_bytes( type ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02002665 {
2666 psa_status_t status = prepare_raw_data_slot( type, bits,
2667 &slot->data.raw );
2668 if( status != PSA_SUCCESS )
2669 return( status );
2670 status = psa_generate_random( slot->data.raw.data,
2671 slot->data.raw.bytes );
2672 if( status != PSA_SUCCESS )
2673 {
2674 mbedtls_free( slot->data.raw.data );
2675 return( status );
2676 }
2677#if defined(MBEDTLS_DES_C)
2678 if( type == PSA_KEY_TYPE_DES )
2679 {
2680 mbedtls_des_key_set_parity( slot->data.raw.data );
2681 if( slot->data.raw.bytes >= 16 )
2682 mbedtls_des_key_set_parity( slot->data.raw.data + 8 );
2683 if( slot->data.raw.bytes == 24 )
2684 mbedtls_des_key_set_parity( slot->data.raw.data + 16 );
2685 }
2686#endif /* MBEDTLS_DES_C */
2687 }
2688 else
2689
2690#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
2691 if ( type == PSA_KEY_TYPE_RSA_KEYPAIR )
2692 {
2693 mbedtls_rsa_context *rsa;
2694 int ret;
2695 int exponent = 65537;
2696 if( parameters != NULL )
2697 {
2698 const unsigned *p = parameters;
2699 if( parameters_size != sizeof( *p ) )
2700 return( PSA_ERROR_INVALID_ARGUMENT );
2701 if( *p > INT_MAX )
2702 return( PSA_ERROR_INVALID_ARGUMENT );
2703 exponent = *p;
2704 }
2705 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
2706 if( rsa == NULL )
2707 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2708 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
2709 ret = mbedtls_rsa_gen_key( rsa,
2710 mbedtls_ctr_drbg_random,
2711 &global_data.ctr_drbg,
2712 bits,
2713 exponent );
2714 if( ret != 0 )
2715 {
2716 mbedtls_rsa_free( rsa );
2717 mbedtls_free( rsa );
2718 return( mbedtls_to_psa_error( ret ) );
2719 }
2720 slot->data.rsa = rsa;
2721 }
2722 else
2723#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
2724
2725#if defined(MBEDTLS_ECP_C)
2726 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEYPAIR( type ) )
2727 {
2728 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
2729 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
2730 const mbedtls_ecp_curve_info *curve_info =
2731 mbedtls_ecp_curve_info_from_grp_id( grp_id );
2732 mbedtls_ecp_keypair *ecp;
2733 int ret;
2734 if( parameters != NULL )
2735 return( PSA_ERROR_NOT_SUPPORTED );
2736 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
2737 return( PSA_ERROR_NOT_SUPPORTED );
2738 if( curve_info->bit_size != bits )
2739 return( PSA_ERROR_INVALID_ARGUMENT );
2740 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
2741 if( ecp == NULL )
2742 return( PSA_ERROR_INSUFFICIENT_MEMORY );
2743 mbedtls_ecp_keypair_init( ecp );
2744 ret = mbedtls_ecp_gen_key( grp_id, ecp,
2745 mbedtls_ctr_drbg_random,
2746 &global_data.ctr_drbg );
2747 if( ret != 0 )
2748 {
2749 mbedtls_ecp_keypair_free( ecp );
2750 mbedtls_free( ecp );
2751 return( mbedtls_to_psa_error( ret ) );
2752 }
2753 slot->data.ecp = ecp;
2754 }
2755 else
2756#endif /* MBEDTLS_ECP_C */
2757
2758 return( PSA_ERROR_NOT_SUPPORTED );
2759
2760 slot->type = type;
2761 return( PSA_SUCCESS );
Gilles Peskine05d69892018-06-19 22:00:52 +02002762}
2763
2764
2765/****************************************************************/
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002766/* Module setup */
2767/****************************************************************/
2768
Gilles Peskinee59236f2018-01-27 23:32:46 +01002769void mbedtls_psa_crypto_free( void )
2770{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002771 size_t key;
Gilles Peskine828ed142018-06-18 23:25:51 +02002772 for( key = 1; key < PSA_KEY_SLOT_COUNT; key++ )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002773 psa_destroy_key( key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01002774 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
2775 mbedtls_entropy_free( &global_data.entropy );
2776 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2777}
2778
2779psa_status_t psa_crypto_init( void )
2780{
2781 int ret;
2782 const unsigned char drbg_seed[] = "PSA";
2783
2784 if( global_data.initialized != 0 )
2785 return( PSA_SUCCESS );
2786
2787 mbedtls_zeroize( &global_data, sizeof( global_data ) );
2788 mbedtls_entropy_init( &global_data.entropy );
2789 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
2790
2791 ret = mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
2792 mbedtls_entropy_func,
2793 &global_data.entropy,
2794 drbg_seed, sizeof( drbg_seed ) - 1 );
2795 if( ret != 0 )
2796 goto exit;
2797
Gilles Peskinee4ebc122018-03-07 14:16:44 +01002798 global_data.initialized = 1;
2799
Gilles Peskinee59236f2018-01-27 23:32:46 +01002800exit:
2801 if( ret != 0 )
2802 mbedtls_psa_crypto_free( );
2803 return( mbedtls_to_psa_error( ret ) );
2804}
2805
2806#endif /* MBEDTLS_PSA_CRYPTO_C */